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";
127 my $mgr = Mail::Box::Manager->new;
131 $folder = $mgr->open( folder => $config{votefile},
134 type => $config{mailboxtype},
138 die UVmessage::get("READMAIL_NOMAILFILE", (FILE => $config{votefile})) . "\n\n" if $@;
140 # Iterate over the messages.
142 my $mail = $_->string;
144 my $fromline = 'From ';
145 if ($mail =~ /From: .*?<(.+?)>/) {
147 } elsif ($mail =~ /From:\s+?(\S+?\@\S+?)\s/) {
150 $fromline .= 'foo@bar.invalid';
152 $fromline .= ' ' . localtime() . "\n"; #strftime ('%a %b %d %H:%M:%S %Y', localtime) . "\n";
153 push (@mails, $fromline . $mail);
157 # make archive of all mails
159 open (VOTES, ">$filename") or $fileproblem = 1;
161 print STDERR UVmessage::get("READMAIL_ARCHIVE_PROBLEM",
162 (FILE => $filename)) . "\n";
164 print VOTES join ("\n", @mails);
166 or print STDERR UVmessage::get("READMAIL_ARCHIVE_CLOSE",
167 (FILE => $filename)) . "\n";
170 foreach my $mail (@mails) {
173 # split mail into array and remove first line (from line)
174 my @mail = split(/\n/, $mail);
175 shift (@mail) if ($mail[0] =~ /^From /);
177 # generate MIME-Parser object for the mail
178 my $parser = new MIME::Parser;
179 # headers are to be decoded
180 $parser->decode_headers(1);
181 # don't write into file
182 $parser->output_to_core(1);
185 my $entity = $parser->parse_data(join("\n", @mail));
186 my $head = $entity->head;
188 # extract address and name
189 my $from = $head->get('From') || '';
191 if ($from =~ /\s*([^<]\S+\@\S+[^>]) \((.+)\)/) {
192 ($voter_addr, $voter_name) = ($1, $2);
193 } elsif ($from =~ /\s*\"?([^\"]+)\"?\s*<(\S+\@\S+)>/) {
194 ($voter_name, $voter_addr) = ($1, $2);
195 $voter_name =~ s/\s+$//; # kill spaces at the end
196 } elsif ($from =~ /\s*<?(\S+\@[^\s>]+)>?[^\(\)]*/) {
197 ($voter_addr, $voter_name) = ($1, '');
199 # initialize with empty value
205 if ($config{replyto}) {
207 my $replyto = Mail::Field->new('Reply-To', $head->get('Reply-To'));
209 # Address in Reply-To?
210 ($voter_addr) = $replyto->addresses() if ($replyto->addresses());
213 if ($replyto->names()) {
214 my ($nametmp) = $replyto->names();
215 $voter_name = $nametmp unless ($nametmp =~ /^\s*$/);
221 my $encoding = $head->get('Content-Transfer-Encoding') || '';
222 if ($encoding =~ /quoted-printable/i) {
223 $body = decode_qp($entity->stringify_body);
224 } elsif ($encoding =~ /base64/i) {
225 $body = decode_base64($entity->stringify_body);
227 $body = $entity->stringify_body;
230 my $h_date = $head->get('Date') || '';
233 # call referred sub and increase counter
234 &$callsub($voter_addr, $voter_name, $h_date, $entity, \$body);