Skip to content
Browse files

Start of Email::Simple::Header

  • Loading branch information...
1 parent db6cfed commit f3fbb2d609c5ff8be1942fbff8070ad2ed77a495 @retupmoca retupmoca committed
Showing with 79 additions and 4 deletions.
  1. +4 −4 lib/Email/Simple.pm6
  2. +75 −0 lib/Email/Simple/Header.pm6
View
8 lib/Email/Simple.pm6
@@ -25,11 +25,11 @@ grammar Message {
method new (Str $text) {
my $parsed = Message.parse($text);
- my $header_object = Email::Simple::Header.new($parsed<headers>, crlf => $parsed<newline>);
- self.bless(*, body => $parsed<body>, header => $header_object, crlf => $parsed<newline>);
+ my $header-object = Email::Simple::Header.new($parsed<headers>, crlf => $parsed<newline>);
+ self.bless(*, body => $parsed<body>, header => $header-object, crlf => $parsed<newline>);
}
-method create () {
+method create (:$header, :$body) {
die "Stub!";
}
@@ -57,7 +57,7 @@ method body-set ($text) {
$!body = $text;
}
-method as-string() {
+method as-string {
return $!header.as-string ~ $!crlf ~ $!body;
}
method Str { self.as-string; }
View
75 lib/Email/Simple/Header.pm6
@@ -1,5 +1,8 @@
class Email::Simple::Header;
+has $!crlf;
+has @!headers;
+
grammar Headers {
token TOP {
<name>: \s* <value> <newline>
@@ -16,3 +19,75 @@ grammar Headers {
}
}
+method new ($header-text, :$crlf = "\r\n") {
+
+ my $parsed = Headers.parse($header-text);
+ my @headers;
+ for 1..+@parsed<name> {
+ my $name = @parsed<name>[$_];
+ my $value = @parsed<value>[$_];
+ $value ~~ s/\s*$crlf\s*/ /g;
+ push(@headers, [$name, $value]);
+ }
+
+ self.bless(*, crlf => $crlf, headers => @headers);
+}
+
+submethod BUILD (:$!crlf, :@!headers) { }
+
+method as-string {
+ my $header-str;
+
+ for @!headers {
+ my $header = $_[0] ~ ': ' ~ $_[1];
+ $header-str ~= self!fold($header);
+ }
+
+ return $header-str;
+}
+method Str { self.as-string }
+
+method header-names {
+
+}
+
+method header-pairs {
+
+}
+
+method header ($name) {
+
+}
+
+method header-set (:$field) {
+
+}
+
+method crlf {
+ return $!crlf;
+}
+
+method !fold ($line) {
+ my $limit = self!default-fold-at - 1;
+
+ if $line.chars <= $limit {
+ return $line ~ self.crlf;
+ }
+
+ my $folded;
+ while $line.chars {
+ if $line ~~ s/^(.{0,$limit})\s// {
+ $folded ~= $1 ~ self.crlf;
+ if $line.chars {
+ $folded ~= self!default-fold-indent;
+ }
+ } else {
+ $folded ~= $line ~ self.crlf;
+ $line = '';
+ }
+ }
+
+ return $folded;
+}
+method !default-fold-at { 78 }
+method !default-fold-indent { " " }

0 comments on commit f3fbb2d

Please sign in to comment.
Something went wrong with that request. Please try again.