Changeset 1790
- Timestamp:
- 10/23/06 04:34:16
- Files:
-
- trunk/plagger/lib/Plagger/Plugin/Publish/IMAP.pm (modified) (4 diffs)
- trunk/plagger/lib/Plagger/Plugin/Publish/Maildir.pm (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/plagger/lib/Plagger/Plugin/Publish/IMAP.pm
r1668 r1790 10 10 use IO::File; 11 11 use Mail::IMAPClient; 12 use Digest::MD5 qw/ md5_hex /; ;12 use Digest::MD5 qw/ md5_hex /; 13 13 14 14 sub register { … … 16 16 $self->{version} = '0.1'; 17 17 $context->register_hook( 18 $self,19 'plugin.init'=> \&initialize,20 'publish.entry'=> \&store_entry,21 'publish.finalize' => \&finalize,18 $self, 19 'plugin.init' => \&initialize, 20 'publish.entry' => \&store_entry, 21 'publish.finalize' => \&finalize, 22 22 ); 23 23 } … … 26 26 27 27 sub initialize { 28 my ($self, $context, $args) = @_; 29 my $cfg = $self->conf; 30 $self->{imap} = Mail::IMAPClient->new( 31 User => $cfg->{username}, 32 Password => $cfg->{password}, 33 Server => $cfg->{host} || 'localhost', 34 Port => $cfg->{port} || 143, 35 ) or die $context->log(error => "Cannot connect; $@"); 36 $context->log(debug => "Connected IMAP-SERVER (".$cfg->{host}.")"); 37 if ($cfg->{folder} && !$self->{imap}->exists($cfg->{folder})) { 38 $self->{imap}->create($cfg->{folder}) 39 or die $context->log(error => "Could not create $cfg->{folder}: $@"); 40 $context->log(info => "Create new folder ($cfg->{folder})"); 41 } 42 if (!$cfg->{mailfrom}) { 43 $cfg->{mailfrom} = 'plagger'; 44 } 28 my($self, $context, $args) = @_; 29 my $cfg = $self->conf; 30 $self->{imap} = Mail::IMAPClient->new( 31 User => $cfg->{username}, 32 Password => $cfg->{password}, 33 Server => $cfg->{host} || 'localhost', 34 Port => $cfg->{port} || 143, 35 ) 36 or die $context->log(error => "Cannot connect; $@"); 37 $context->log(debug => "Connected IMAP-SERVER (" . $cfg->{host} . ")"); 38 if ($cfg->{folder} && !$self->{imap}->exists($cfg->{folder})) { 39 $self->{imap}->create($cfg->{folder}) 40 or die $context->log(error => "Could not create $cfg->{folder}: $@"); 41 $context->log(info => "Create new folder ($cfg->{folder})"); 42 } 43 if (!$cfg->{mailfrom}) { 44 $cfg->{mailfrom} = 'plagger'; 45 } 45 46 } 46 47 47 48 sub finalize { 48 my($self, $context, $args) = @_;49 my $cfg= $self->{conf};50 $self->{imap}->disconnect();51 if (my $msg_count = $self->{msg}) {52 $context->log(info => "Store $msg_count Message(s)");53 }54 $context->log(debug => "Disconnected IMAP-SERVER (".$cfg->{host}.")");49 my($self, $context, $args) = @_; 50 my $cfg = $self->{conf}; 51 $self->{imap}->disconnect(); 52 if (my $msg_count = $self->{msg}) { 53 $context->log(info => "Store $msg_count Message(s)"); 54 } 55 $context->log(debug => "Disconnected IMAP-SERVER (" . $cfg->{host} . ")"); 55 56 } 56 57 57 58 sub store_entry { 58 my($self, $context, $args) = @_; 59 my $cfg = $self->conf; 60 my $msg; 61 my $entry = $args->{entry}; 62 my $feed_title = $args->{feed}->title; 63 $feed_title =~ tr/,//d; 64 my $subject = $entry->title || '(no-title)'; 65 my $body = $self->templatize('mail.tt', { entry => $args->{entry}, feed => $args->{feed} }); 66 my $now = Plagger::Date->now(timezone => $context->conf->{timezone}); 67 $msg = MIME::Lite->new( 68 Date => $now->format('Mail'), 69 From => encode('MIME-Header', qq("$feed_title" <$cfg->{mailfrom}>)), 70 To => $cfg->{mailto}, 71 Subject => encode('MIME-Header', $subject), 72 Type => 'multipart/related', 73 ); 74 $body = encode("utf-8", $body); 75 $msg->attach( 76 Type => 'text/html; charset=utf-8', 77 Data => $body, 78 Encoding => 'quoted-printable', 79 ); 80 $msg->add('X-Tags', encode('MIME-Header',join(' ',@{$entry->tags}))); 81 my $xmailer = "MIME::Lite (Publish::Maildir Ver.$self->{version} in plagger)"; 82 $msg->replace('X-Mailer',$xmailer); 83 $msg->add('In-Reply-To',"<".md5_hex($entry->id_safe).'@localhost>'); 84 store_maildir($self, $context,$msg->as_string()); 85 $self->{msg} += 1; 59 my($self, $context, $args) = @_; 60 my $cfg = $self->conf; 61 my $msg; 62 my $entry = $args->{entry}; 63 my $feed_title = $args->{feed}->title; 64 $feed_title =~ tr/,//d; 65 my $subject = $entry->title || '(no-title)'; 66 my $body = 67 $self->templatize('mail.tt', 68 { entry => $args->{entry}, feed => $args->{feed} }); 69 my $now = Plagger::Date->now(timezone => $context->conf->{timezone}); 70 $msg = MIME::Lite->new( 71 Date => $now->format('Mail'), 72 From => encode('MIME-Header', qq("$feed_title" <$cfg->{mailfrom}>)), 73 To => $cfg->{mailto}, 74 Subject => encode('MIME-Header', $subject), 75 Type => 'multipart/related', 76 ); 77 $body = encode("utf-8", $body); 78 $msg->attach( 79 Type => 'text/html; charset=utf-8', 80 Data => $body, 81 Encoding => 'quoted-printable', 82 ); 83 $msg->add('X-Tags', encode('MIME-Header', join(' ', @{ $entry->tags }))); 84 my $xmailer = 85 "MIME::Lite (Publish::Maildir Ver.$self->{version} in plagger)"; 86 $msg->replace('X-Mailer', $xmailer); 87 $msg->add('In-Reply-To', "<" . md5_hex($entry->id_safe) . '@localhost>'); 88 store_maildir($self, $context, $msg->as_string()); 89 $self->{msg} += 1; 86 90 } 87 91 88 92 sub store_maildir { 89 my($self,$context,$msg) = @_;90 my $folder = $self->conf->{folder} || 'INBOX';91 my $uid = $self->{imap}->append_string($folder,$msg)92 or die $context->log(error => "Could not append: $@");93 my($self, $context, $msg) = @_; 94 my $folder = $self->conf->{folder} || 'INBOX'; 95 my $uid = $self->{imap}->append_string($folder, $msg) 96 or die $context->log(error => "Could not append: $@"); 93 97 } 94 98 … … 121 125 122 126 =cut 127 trunk/plagger/lib/Plagger/Plugin/Publish/Maildir.pm
r1788 r1790 9 9 use HTML::Entities; 10 10 use MIME::Lite; 11 use Digest::MD5 qw/ md5_hex /; ;11 use Digest::MD5 qw/ md5_hex /; 12 12 use File::Find; 13 13 … … 15 15 my($self, $context) = @_; 16 16 $context->register_hook( 17 $self,18 'plugin.init'=> \&initialize,19 'publish.entry'=> \&store_entry,20 'publish.finalize' => \&finalize,17 $self, 18 'plugin.init' => \&initialize, 19 'publish.entry' => \&store_entry, 20 'publish.finalize' => \&finalize, 21 21 ); 22 22 } … … 25 25 26 26 sub initialize { 27 my ($self, $context, $args) = @_; 28 my $cfg = $self->conf; 29 my $permission = $cfg->{permission} || 0700; 30 if (-d $cfg->{maildir}) { 31 my $path = "$cfg->{maildir}/.$cfg->{folder}"; 32 $path =~ s/\/\//\//g; 33 $path =~ s/\/$//g; 34 unless (-d $path) { 35 mkdir($path,0700) 36 or die $context->log(error => "Could not create $path"); 37 $context->log(info => "Create new folder ($path)"); 38 } 39 unless (-d $path."/new") { 40 mkdir($path."/new",0700) 41 or die $context->log(error => "Could not Create $path/new"); 42 $context->log(info => "Create new folder($path/new)"); 43 } 44 $self->{path} = $path; 45 }else{ 46 die $context->log(error => "Could not access $cfg->{maildir}"); 47 } 27 my($self, $context, $args) = @_; 28 my $cfg = $self->conf; 29 my $permission = $cfg->{permission} || 0700; 30 if (-d $cfg->{maildir}) { 31 my $path = "$cfg->{maildir}/.$cfg->{folder}"; 32 $path =~ s/\/\//\//g; 33 $path =~ s/\/$//g; 34 unless (-d $path) { 35 mkdir($path, 0700) 36 or die $context->log(error => "Could not create $path"); 37 $context->log(info => "Create new folder ($path)"); 38 } 39 unless (-d $path . "/new") { 40 mkdir($path . "/new", 0700) 41 or die $context->log(error => "Could not Create $path/new"); 42 $context->log(info => "Create new folder($path/new)"); 43 } 44 $self->{path} = $path; 45 } 46 else { 47 die $context->log(error => "Could not access $cfg->{maildir}"); 48 } 48 49 } 49 50 50 51 sub finalize { 51 my ($self, $context, $args) = @_; 52 if (my $msg_count = $self->{msg}) { 53 if (my $update_count = $self->{update_msg}) { 54 $context->log(info => "Store $msg_count message(s) ($update_count message(s) updated)"); 55 }else{ 56 $context->log(info => "Store $msg_count message(s)"); 57 } 58 } 52 my($self, $context, $args) = @_; 53 if (my $msg_count = $self->{msg}) { 54 if (my $update_count = $self->{update_msg}) { 55 $context->log(info => 56 "Store $msg_count message(s) ($update_count message(s) updated)" 57 ); 58 } 59 else { 60 $context->log(info => "Store $msg_count message(s)"); 61 } 62 } 59 63 } 60 64 61 65 sub store_entry { 62 my($self, $context, $args) = @_; 63 my $cfg = $self->conf; 64 my $msg; 65 my $entry = $args->{entry}; 66 my $feed_title = $args->{feed}->title; 67 $feed_title =~ tr/,//d; 68 my $subject = $entry->title || '(no-title)'; 69 my $body = $self->templatize('mail.tt', $args); 70 $body = encode("utf-8", $body); 71 my $from = $cfg->{mailfrom} || 'plagger@localhost'; 72 my $id = md5_hex($entry->id_safe); 73 my $now = Plagger::Date->now(timezone => $context->conf->{timezone}); 74 my @enclosure_cb; 75 if ($self->conf->{attach_enclosures}) { 76 push @enclosure_cb, $self->prepare_enclosures($entry); 77 } 78 $msg = MIME::Lite->new( 79 Date => $now->format('Mail'), 80 From => encode('MIME-Header', qq("$feed_title" <$from>)), 81 To => $cfg->{mailto}, 82 Subject => encode('MIME-Header', $subject), 83 Type => 'multipart/related', 84 ); 85 $msg->attach( 86 Type => 'text/html; charset=utf-8', 87 Data => $body, 88 Encoding => 'quoted-printable', 89 ); 90 for my $cb (@enclosure_cb) { 91 $cb->($msg); 92 } 93 $msg->add('Message-Id', "<$id.plagger\@localhost>"); 94 $msg->add('X-Tags', encode('MIME-Header',join(' ',@{$entry->tags}))); 95 my $xmailer = "Plagger/$Plagger::VERSION"; 96 $msg->replace('X-Mailer',$xmailer); 97 store_maildir($self, $context,$msg->as_string(),$id); 98 $self->{msg} += 1; 66 my($self, $context, $args) = @_; 67 my $cfg = $self->conf; 68 my $msg; 69 my $entry = $args->{entry}; 70 my $feed_title = $args->{feed}->title; 71 $feed_title =~ tr/,//d; 72 my $subject = $entry->title || '(no-title)'; 73 my $body = $self->templatize('mail.tt', $args); 74 $body = encode("utf-8", $body); 75 my $from = $cfg->{mailfrom} || 'plagger@localhost'; 76 my $id = md5_hex($entry->id_safe); 77 my $now = Plagger::Date->now(timezone => $context->conf->{timezone}); 78 my @enclosure_cb; 79 80 if ($self->conf->{attach_enclosures}) { 81 push @enclosure_cb, $self->prepare_enclosures($entry); 82 } 83 $msg = MIME::Lite->new( 84 Date => $now->format('Mail'), 85 From => encode('MIME-Header', qq("$feed_title" <$from>)), 86 To => $cfg->{mailto}, 87 Subject => encode('MIME-Header', $subject), 88 Type => 'multipart/related', 89 ); 90 $msg->attach( 91 Type => 'text/html; charset=utf-8', 92 Data => $body, 93 Encoding => 'quoted-printable', 94 ); 95 for my $cb (@enclosure_cb) { 96 $cb->($msg); 97 } 98 $msg->add('Message-Id', "<$id.plagger\@localhost>"); 99 $msg->add('X-Tags', encode('MIME-Header', join(' ', @{ $entry->tags }))); 100 my $xmailer = "Plagger/$Plagger::VERSION"; 101 $msg->replace('X-Mailer', $xmailer); 102 store_maildir($self, $context, $msg->as_string(), $id); 103 $self->{msg} += 1; 99 104 } 100 105 … … 103 108 104 109 if (grep $_->is_inline, $entry->enclosures) { 110 105 111 # replace inline enclosures to cid: entities 106 112 my %url2enclosure = map { $_->url => $_ } $entry->enclosures; … … 108 114 my $output; 109 115 my $p = HTML::Parser->new(api_version => 3); 110 $p->handler( default => sub { $output .= $_[0] }, "text" ); 111 $p->handler( start => sub { 112 my($tag, $attr, $attrseq, $text) = @_; 113 # TODO: use HTML::Tagset? 114 if (my $url = $attr->{src}) { 115 if (my $enclosure = $url2enclosure{$url}) { 116 $attr->{src} = "cid:" . $self->enclosure_id($enclosure); 117 } 118 $output .= $self->generate_tag($tag, $attr, $attrseq); 119 } else { 120 $output .= $text; 121 } 122 }, "tag, attr, attrseq, text"); 116 $p->handler(default => sub { $output .= $_[0] }, "text"); 117 $p->handler( 118 start => sub { 119 my($tag, $attr, $attrseq, $text) = @_; 120 121 # TODO: use HTML::Tagset? 122 if (my $url = $attr->{src}) { 123 if (my $enclosure = $url2enclosure{$url}) { 124 $attr->{src} = "cid:" . $self->enclosure_id($enclosure); 125 } 126 $output .= $self->generate_tag($tag, $attr, $attrseq); 127 } 128 else { 129 $output .= $text; 130 } 131 }, 132 "tag, attr, attrseq, text" 133 ); 123 134 $p->parse($entry->body); 124 135 $p->eof; … … 132 143 for my $enclosure (grep $_->local_path, $entry->enclosures) { 133 144 my %param = ( 134 Type => $enclosure->type,135 Path => $enclosure->local_path,145 Type => $enclosure->type, 146 Path => $enclosure->local_path, 136 147 Filename => $enclosure->filename, 137 148 ); … … 140 151 $param{Id} = '<' . $self->enclosure_id($enclosure) . '>'; 141 152 $param{Disposition} = 'inline'; 142 } else { 153 } 154 else { 143 155 $param{Disposition} = 'attachment'; 144 156 } … … 146 158 $msg->attach(%param); 147 159 } 148 }160 } 149 161 } 150 162 … … 152 164 my($self, $tag, $attr, $attrseq) = @_; 153 165 154 return "<$tag " . 155 join(' ', map { $_ eq '/' ? '/' : sprintf qq(%s="%s"), $_, encode_entities($attr->{$_}, q(<>"')) } @$attrseq) . 156 '>'; 166 return "<$tag " . join( 167 ' ', 168 map { 169 $_ eq '/' ? '/' : sprintf qq(%s="%s"), $_, 170 encode_entities($attr->{$_}, q(<>"')) 171 } @$attrseq 172 ) 173 . '>'; 157 174 } 158 175 159 176 sub enclosure_id { 160 my($self, $enclosure) = @_;161 return Digest::MD5::md5_hex($enclosure->url->as_string) . '@Plagger';177 my($self, $enclosure) = @_; 178 return Digest::MD5::md5_hex($enclosure->url->as_string) . '@Plagger'; 162 179 } 163 180 164 181 sub store_maildir { 165 my($self,$context,$msg,$id) = @_;166 my $filename = $id.".plagger";167 find(168 sub {169 if ($_ =~ m!$id.*!) {170 unlink $_;171 $self->{update_msg} += 1;172 }173 },174 $self->{path}."/cur"175 );176 $context->log(debug=> "writing: new/$filename");177 my $path = $self->{path}."/new/".$filename;178 open my $fh, ">", $path or $context->error("$path: $!");179 print $fh $msg;180 close $fh;182 my($self, $context, $msg, $id) = @_; 183 my $filename = $id . ".plagger"; 184 find( 185 sub { 186 if ($_ =~ m!$id.*!) { 187 unlink $_; 188 $self->{update_msg} += 1; 189 } 190 }, 191 $self->{path} . "/cur" 192 ); 193 $context->log(debug => "writing: new/$filename"); 194 my $path = $self->{path} . "/new/" . $filename; 195 open my $fh, ">", $path or $context->error("$path: $!"); 196 print $fh $msg; 197 close $fh; 181 198 } 182 199 … … 209 226 210 227 =cut 228
