lib/Message/2822.pm
changeset 12 9f127fcfdf6d
parent 8 6276861aa7d4
child 15 7c3215a97e4b
--- a/lib/Message/2822.pm	Thu Nov 10 16:18:38 2011 +0100
+++ b/lib/Message/2822.pm	Fri Nov 11 11:22:50 2011 +0100
@@ -26,30 +26,53 @@
       or die "Can't open $DATA{fn}{$self}: $!\n";
 
     local $/ = "";
-    $DATA{header}{$self}   = <$fh>;
+    chomp($DATA{header}{$self}   = <$fh>);
     $DATA{body_pos}{$self} = tell($fh);
 
     return $self;
 }
 
-sub __header_fields {
+sub header_fields {
+    my ($self, $pattern) = @_;
+    $pattern //= qr/\S+/;
+    return grep /$pattern/i => $DATA{header}{$self} =~ /^(\S+?[: ])/mg;
+}
+
+sub header_lines {
     my $self = shift;
-    my %h;
-    @h{ $DATA{header}{$self} =~ /^(\S+?[: ])/mg } = ();
-    return keys %h;
+    my $field = shift // qr/.*/;
+    my @r = grep /$field/i => $DATA{header}{$self} =~ /(^\S+?[: ]\s*.*?\n)(?=^\S|\Z)/imsg;
+    return @r if wantarray;
+
+    foreach (@r) { s/\s*?\n\s+/ /mg; }
+    return join "" => @r;
 }
 
-sub header_content {
-    my $self  = shift;
-    my $field = shift;
+sub remove_header_lines {
+    my $self = shift;
+    my $pattern = shift // die "Need a pattern!";
+    $DATA{header}{$self} =~ s/^$pattern.*?(?=^\S|\Z)//imsg;
+}
 
-    ### assert: $field =~ /[: ]$/
+sub add_header_line {
+    my $self = shift;
+    my $_ = shift;
+    $_ .= "\n" unless /\n$/;
+    $DATA{header}{$self} .= $_;
+}
 
-    # FIXME: not sure if the space following the header field name
-    # is optional or required
-    return $DATA{header}{$self} =~ /^$field\s+(.*?)(?=^\S)/imsg;
+sub header_contents {
+    my $self  = shift;
+    my $field = shift // qr/.*/;
+
+    my @r = map { (split /[: ]/, $_, 2)[1] } $self->header_lines($field);
+    return @r if wantarray;
+
+    foreach (@r) { s/\s*?\n\s+/ /mg; }
+    return join "" => @r;
 }
 
+
 sub orig_header {
     my $self = shift;
     my $fh   = $DATA{fh}{$self};
@@ -105,10 +128,28 @@
 The construcor. The file is opened r/o and read. The file will not be
 closed until the object disappears.
 
-=item @list = B<header_content>(I<field>)
+=item @list = B<header_fields>([I<pattern>])
+
+Return a list of existing header fields, matching the pattern. 
+See B<header_contents()> for information about the
+returned format. (default pattern: /.*/)
+
+
+=item @list = B<header_lines>([I<pattern>])
 
-Returns a list (*always* a list) with the contents of the specified
-header field. The I<field> has to include the colon ":" or space " ",
+Returns the header line matching the I<pattern>. See B<header_contents()>
+about the returned format. (default pattern: /.*/)
+
+=item @list = B<header_contents>([I<pattern>])
+
+Returns the contents of the header lines matching the pattern. (default
+pattern: /.*/)
+
+In list context a list of B<unmodified> header lines is returned.
+In scalar context the header lines are de-wrapped and then returned,
+delimited by single linebreak.
+
+The I<field> has to include the colon ":" or space " ",
 since it is considered a part of the field name. I<field> is case
 insensitive!