1 # UVreadmail: functions for reading and processing mailfiles
2 # Used by uvvote.pl, uvcfv.pl, uvbounce.pl
12 use Mail::Box::Manager;
13 use POSIX qw(strftime);
15 use vars qw($VERSION);
22 # $filename: file containing bounces or (if POP3 is enabled) where
23 # mails should be saved
24 # $callsub: reference to a sub which should be called for each mail
25 # $caller: 0 = uvvote.pl, 1 = uvcfv.pl, 2 = uvbounce.pl
26 # 3 = uvbounce.pl but POP3 disabled (overrides $config{pop3}
29 my ($filename, $callsub, $caller) = @_;
30 my ($voter_addr, $voter_name, $body);
32 my ($pop3server, $pop3user, $pop3pass, $pop3delete, $pop3uidlcache);
36 if ($config{pop3} && $caller<3) {
39 # Ballot request (personal = 1 set in usevote.cfg) from uvcfv.pl
40 $pop3server = $config{pop3server_req} . ':' . $config{pop3port_req};
41 $pop3user = $config{pop3user_req};
42 $pop3pass = $config{pop3pass_req};
43 $pop3delete = $config{pop3delete_req};
44 $pop3uidlcache = $config{pop3uidlcache_req};
45 } elsif ($caller == 2) {
46 # called from uvbounce.pl
47 $pop3server = $config{pop3server_bounce} . ':' . $config{pop3port_bounce};
48 $pop3user = $config{pop3user_bounce};
49 $pop3pass = $config{pop3pass_bounce};
50 $pop3delete = $config{pop3delete_bounce};
51 $pop3uidlcache = $config{pop3uidlcache_bounce};
53 $pop3server = $config{pop3server} . ':' . $config{pop3port};
54 $pop3user = $config{pop3user};
55 $pop3pass = $config{pop3pass};
56 $pop3delete = $config{pop3delete};
57 $pop3uidlcache = $config{pop3uidlcache};
60 # read list of seen mails (UIDLs)
61 my %uidls = (); # hash for quick searching
62 my @uidls = (); # array to preserve order
64 open (UIDLCACHE, "<$pop3uidlcache") or $cacheexist = 0;
66 while (my $uidl = <UIDLCACHE>) {
74 print UVmessage::get("READMAIL_STATUS"), "\n" unless ($caller == 2);
76 # open POP3 connection and get new mails
78 my $pop = Net::POP3->new($pop3server)
79 or die UVmessage::get("READMAIL_NOCONNECTION") . "\n\n";
81 my $mailcount = $pop->login($pop3user, $pop3pass);
83 die UVmessage::get("READMAIL_NOLOGIN") . "\n\n" unless ($mailcount);
85 for (my $n=1; $n<=$mailcount; $n++) {
86 my $uidl = $pop->uidl($n);
88 next if ($uidls{$uidl});
92 my $mailref = $pop->get($n)
93 or print STDERR UVmessage::get("READMAIL_GET_PROBLEM", (NR => $n)) . "\n";
94 my $mail = join ('', @$mailref);
95 my $fromline = 'From ';
96 if ($mail =~ /From: .*?<(.+?)>/) {
98 } elsif ($mail =~ /From:\s+?(\S+?\@\S+?)\s/) {
101 $fromline .= 'foo@bar.invalid';
103 $fromline .= ' ' . strftime ('%a %b %d %H:%M:%S %Y', localtime) . "\n";
104 push (@mails, $fromline . $mail);
107 or print STDERR UVmessage::get("READMAIL_DEL_PROBLEM", (NR => $n)) . "\n";
113 open (UIDLCACHE, ">$pop3uidlcache") or $uidlerr = 1;
115 print STDERR UVmessage::get("READMAIL_UIDL_PROBLEM") . "\n";
116 print STDERR UVmessage::get("READMAIL_UIDL_PROBLEM2") . "\n";
118 print UIDLCACHE join("\n", @uidls);
119 close (UIDLCACHE) or print STDERR UVmessage::get("READMAIL_UIDL_CLOSE") . "\n";
130 # called from uvvote.pl: use configured mailbox file
131 $readfilename = $config{votefile};
133 # else use filename provided in function call
134 $readfilename = $filename;
135 # and create backup archive filename
136 $filename .= '.processed';
139 my $mgr = Mail::Box::Manager->new;
143 $folder = $mgr->open( folder => $readfilename,
146 type => $config{mailboxtype},
148 remove_when_empty => 0,
151 die UVmessage::get("READMAIL_NOMAILFILE", (FILE => $readfilename)) . "\n\n" if $@;
153 # Iterate over the messages.
155 my $mail = $_->string;
157 my $fromline = 'From ';
158 if ($mail =~ /From: .*?<(.+?)>/) {
160 } elsif ($mail =~ /From:\s+?(\S+?\@\S+?)\s/) {
163 $fromline .= 'foo@bar.invalid';
165 $fromline .= ' ' . localtime() . "\n"; #strftime ('%a %b %d %H:%M:%S %Y', localtime) . "\n";
166 push (@mails, $fromline . $mail);
170 # make archive of all mails
172 open (VOTES, ">$filename") or $fileproblem = 1;
174 print STDERR UVmessage::get("READMAIL_ARCHIVE_PROBLEM",
175 (FILE => $filename)) . "\n";
177 print VOTES join ("\n", @mails);
179 or print STDERR UVmessage::get("READMAIL_ARCHIVE_CLOSE",
180 (FILE => $filename)) . "\n";
183 foreach my $mail (@mails) {
186 # split mail into array and remove first line (from line)
187 my @mail = split(/\n/, $mail);
188 shift (@mail) if ($mail[0] =~ /^From /);
190 # generate MIME-Parser object for the mail
191 my $parser = new MIME::Parser;
192 # headers are to be decoded
193 $parser->decode_headers(1);
194 # don't write into file
195 $parser->output_to_core(1);
198 my $entity = $parser->parse_data(join("\n", @mail));
199 my $head = $entity->head;
201 # extract address and name
202 my $from = $head->get('From') || '';
204 if ($from =~ /\s*([^<]\S+\@\S+[^>]) \((.+)\)/) {
205 ($voter_addr, $voter_name) = ($1, $2);
206 } elsif ($from =~ /\s*\"?([^\"]+)\"?\s*<(\S+\@\S+)>/) {
207 ($voter_name, $voter_addr) = ($1, $2);
208 $voter_name =~ s/\s+$//; # kill spaces at the end
209 } elsif ($from =~ /\s*<?(\S+\@[^\s>]+)>?[^\(\)]*/) {
210 ($voter_addr, $voter_name) = ($1, '');
212 # initialize with empty value
218 if ($config{replyto}) {
220 my $replyto = Mail::Field->new('Reply-To', $head->get('Reply-To'));
222 # Address in Reply-To?
223 ($voter_addr) = $replyto->addresses() if ($replyto->addresses());
226 if ($replyto->names()) {
227 my ($nametmp) = $replyto->names();
228 $voter_name = $nametmp unless ($nametmp =~ /^\s*$/);
234 my $encoding = $head->get('Content-Transfer-Encoding') || '';
235 if ($encoding =~ /quoted-printable/i) {
236 $body = decode_qp($entity->stringify_body);
237 } elsif ($encoding =~ /base64/i) {
238 $body = decode_base64($entity->stringify_body);
240 $body = $entity->stringify_body;
243 my $h_date = $head->get('Date') || '';
246 # call referred sub and increase counter
247 &$callsub($voter_addr, $voter_name, $h_date, $entity, \$body);