| 1 | package Cnutt::Feed::Mailbox; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | =head1 NAME | ||||||||||
| 7 | |||||||||||
| 8 | Cnutt::Feed::Mailbox - Take a feed and populate a mailbox | ||||||||||
| 9 | |||||||||||
| 10 | =head1 VERSION | ||||||||||
| 11 | |||||||||||
| 12 | Version 1.0 | ||||||||||
| 13 | |||||||||||
| 14 | =cut | ||||||||||
| 15 | |||||||||||
| 16 | 2 | our $VERSION = '1.0'; | |||||||||
| 17 | |||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||
| 19 | |||||||||||
| 20 | This file is part of cnutt-feed. You should read its documentation. | ||||||||||
| 21 | |||||||||||
| 22 | =cut | ||||||||||
| 23 | |||||||||||
| 24 | 2 | ▶ | use Encode; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 2 | ▶ | use Digest::MD5 qw(md5_base64); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | |||||||||||
| 27 | 2 | ▶ | use XML::Feed; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | |||||||||||
| 29 | 2 | ▶ | use Email::LocalDelivery; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 2 | ▶ | use Email::MIME::CreateHTML; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 2 | ▶ | use Email::Address; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 2 | ▶ | use Email::Date; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | 2 | ▶ | use Email::Folder; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | |||||||||||
| 35 | 2 | ▶ | use MIME::EncWords "encode_mimewords"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | |||||||||||
| 37 | 2 | ▶ | use HTML::Encoding "encoding_from_xml_declaration"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 2 | ▶ | use HTML::Entities; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 39 | 2 | ▶ | use HTML::TreeBuilder; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 2 | ▶ | use HTML::FormatText::WithLinks; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 41 | |||||||||||
| 42 | =head2 fetch | ||||||||||
| 43 | |||||||||||
| 44 | Cnutt::Feed::Mailbox->fetch($url, $mb, $dohtml, $delete, $verbose, | ||||||||||
| 45 | $name) | ||||||||||
| 46 | |||||||||||
| 47 | will fetch the feed given by the url in C<$url> and put the entries in | ||||||||||
| 48 | the mailbox C<$mb> (See L<Email::LocalDelivery> for the supported | ||||||||||
| 49 | formats). | ||||||||||
| 50 | |||||||||||
| 51 | If not C<undef> it uses C<$name> for dislaying errors. | ||||||||||
| 52 | |||||||||||
| 53 | If C<$dohtml> is true, an html part will be enclosed in the built | ||||||||||
| 54 | messages. | ||||||||||
| 55 | |||||||||||
| 56 | If C<$delete> is true, old messages will be deleted. NOT IMPLEMENTED. | ||||||||||
| 57 | |||||||||||
| 58 | If C<$verbose> is true, informations will be printed on STDOUT. | ||||||||||
| 59 | |||||||||||
| 60 | =cut | ||||||||||
| 61 | |||||||||||
| 62 | 0 | ▶ | sub fetch { | ||||||||
|
Subroutine
fetch: not called CC=21 SCAR=61.4
Pod
documented
| |||||||||||
| 63 | 0 | shift; | |||||||||
| 64 | 0 | my ($url, $mb, $dohtml, $delete, $verbose, $name) = @_; | |||||||||
| 65 | |||||||||||
| 66 | 0 | my $count = 0; | |||||||||
| 67 | |||||||||||
| 68 | # searching already downloaded entries | ||||||||||
| 69 | 0 | my %ids; | |||||||||
| 70 | 0 | eval { | |||||||||
| 71 | 0 | my $folder = Email::Folder->new($mb) ; | |||||||||
| 72 | 0 | ▶ | if (defined($folder)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 0 | map { $ids{$_->header("X-Id")}++ } $folder->messages; | |||||||||
| 74 | } | ||||||||||
| 75 | }; | ||||||||||
| 76 | |||||||||||
| 77 | # building uri | ||||||||||
| 78 | 0 | ▶ | if ($url =~ m!^https?://!) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 0 | $url = URI->new($url); | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | # feed parsing | ||||||||||
| 83 | 0 | my $feed = eval {XML::Feed->parse($url)}; | |||||||||
| 84 | 0 | ▶ | if (!defined($feed)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 0 | my $error = XML::Feed->errstr; | |||||||||
| 86 | 0 | ▶ | if (defined($error)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 0 | ▶ | if (defined($name)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 0 | warn "XML::Feed error: $error in $name\n"; | |||||||||
| 89 | } | ||||||||||
| 90 | else { | ||||||||||
| 91 | 0 | warn "XML::Feed error: $error in $url\n"; | |||||||||
| 92 | } | ||||||||||
| 93 | } | ||||||||||
| 94 | 0 | return undef; | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 0 | ▶ | print "From ", $feed->title, "\n" if $verbose; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | |||||||||||
| 99 | 0 | my $xml = eval {$feed->as_xml}; | |||||||||
| 100 | 0 | ▶ | $xml = "" unless defined($xml); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 0 | my $enc = "ISO-8859-15"; | |||||||||
| 102 | 0 | ▶ | $enc = encoding_from_xml_declaration($xml) if defined($xml); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | |||||||||||
| 104 | # fetching each entries | ||||||||||
| 105 | 0 | for my $entry ($feed->entries) { | |||||||||
| 106 | # identification | ||||||||||
| 107 | 0 | my $id = $entry->id; | |||||||||
| 108 | 0 | ▶ | $id = $entry->link | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | unless defined($id); | ||||||||||
| 110 | 0 | ▶ | $id = md5_base64($feed->title,$entry->title) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | unless defined($id); | ||||||||||
| 112 | |||||||||||
| 113 | 0 | ▶ | next if ($ids{$id}); # entry already seen | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | |||||||||||
| 115 | 0 | my $title = $entry->title; | |||||||||
| 116 | 0 | ▶ | $title = "" unless defined($title); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | 0 | ▶ | print "fetching «", $title, "» \033[s..." if $verbose; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | |||||||||||
| 119 | # permalink | ||||||||||
| 120 | 0 | my $link = $entry->link; | |||||||||
| 121 | 0 | ▶ | $link = "" unless defined($link); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | |||||||||||
| 123 | # from and to | ||||||||||
| 124 | 0 | my @addresses = Email::Address->parse($entry->author); | |||||||||
| 125 | 0 | ▶ | if (@addresses == 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | 0 | @addresses = (Email::Address->new($entry->author => | |||||||||
| 127 | 'invalid@localhost')); | ||||||||||
| 128 | } | ||||||||||
| 129 | 0 | my $from = "@addresses"; | |||||||||
| 130 | |||||||||||
| 131 | 0 | my $to = Email::Address->new($feed->title => 'invalid@localhost'); | |||||||||
| 132 | |||||||||||
| 133 | # html body | ||||||||||
| 134 | 0 | my $html_body = $entry->content->body; | |||||||||
| 135 | 0 | ▶ | $html_body = "" unless defined($html_body); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | |||||||||||
| 137 | 0 | my $html_sig = "<br>\n-- <br>\n" . | |||||||||
| 138 | "<a href=\"" . $link . "\">" . | ||||||||||
| 139 | $link . "</a>" . "<br>\n" . | ||||||||||
| 140 | $feed->title . "<br>\n" . | ||||||||||
| 141 | $feed->description . "<br>\n"; | ||||||||||
| 142 | 0 | my $html = $html_body . $html_sig; | |||||||||
| 143 | |||||||||||
| 144 | # text body | ||||||||||
| 145 | 0 | my $text_tree = HTML::TreeBuilder->new_from_content($html_body); | |||||||||
| 146 | 0 | my $formatter = HTML::FormatText::WithLinks->new(leftmargin => 0, | |||||||||
| 147 | rightmargin => 78); | ||||||||||
| 148 | 0 | my $text_body = $formatter->format($text_tree); | |||||||||
| 149 | 0 | $text_body =~ s/^(\n)*//s; # empty lines deletion at the beginning | |||||||||
| 150 | 0 | my $text_sig = "\n-- \n" . | |||||||||
| 151 | $link . "\n" . | ||||||||||
| 152 | $feed->title . "\n" . | ||||||||||
| 153 | $feed->description . "\n"; | ||||||||||
| 154 | 0 | my $text = $text_body . $text_sig; | |||||||||
| 155 | |||||||||||
| 156 | 0 | my $date = time(); | |||||||||
| 157 | 0 | ▶ | $date = $entry->issued->epoch if defined($entry->issued); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 158 | |||||||||||
| 159 | # building of the message | ||||||||||
| 160 | 0 | my $email = eval { | |||||||||
| 161 | 0 | Email::MIME->create_html( | |||||||||
| 162 | header => [ | ||||||||||
| 163 | 'From' => $from, | ||||||||||
| 164 | 'To' => $to, | ||||||||||
| 165 | 'Subject' => | ||||||||||
| 166 | encode_mimewords( | ||||||||||
| 167 | decode_entities($title)), | ||||||||||
| 168 | 'Date' => format_date($date), | ||||||||||
| 169 | 'X-Id' => $id, | ||||||||||
| 170 | ], | ||||||||||
| 171 | body => $html, | ||||||||||
| 172 | body_attributes => { | ||||||||||
| 173 | charset => $enc}, | ||||||||||
| 174 | text_body => $text, | ||||||||||
| 175 | text_body_attributes => { | ||||||||||
| 176 | charset => $enc}, | ||||||||||
| 177 | embed => $dohtml, | ||||||||||
| 178 | base => $feed->link, | ||||||||||
| 179 | ); | ||||||||||
| 180 | }; | ||||||||||
| 181 | |||||||||||
| 182 | # sending of the message | ||||||||||
| 183 | 0 | ▶ | if (defined($email)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 184 | 0 | my @delivered_to = Email::LocalDelivery->deliver($email->as_string, | |||||||||
| 185 | ($mb)); | ||||||||||
| 186 | } | ||||||||||
| 187 | |||||||||||
| 188 | 0 | $count++; | |||||||||
| 189 | 0 | ▶ | print " \033[u \n" if $verbose; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 190 | } | ||||||||||
| 191 | |||||||||||
| 192 | 0 | return $count; | |||||||||
| 193 | } | ||||||||||
| 194 | |||||||||||
| 195 | 2 | 1; | |||||||||
| 196 | |||||||||||