1 # UVreadmail: functions for reading and processing mailfiles
2 # Used by uvvote.pl, uvcfv.pl, uvbounce.pl
12 use POSIX qw(strftime);
14 use vars qw($VERSION);
21 # $filename: file containing bounces or (if POP3 is enabled) where
22 # mails should be saved
23 # $callsub: reference to a sub which should be called for each mail
24 # $caller: 0 = uvvote.pl, 1 = uvcfv.pl, 2 = uvbounce.pl
25 # 3 = uvbounce.pl but POP3 disabled (overrides $config{pop3}
28 my ($filename, $callsub, $caller) = @_;
29 my ($voter_addr, $voter_name, $body);
31 my ($pop3server, $pop3user, $pop3pass, $pop3delete, $pop3uidlcache);
35 if ($config{pop3} && $caller<3) {
38 # Ballot request (personal = 1 set in usevote.cfg) from uvcfv.pl
39 $pop3server = $config{pop3server_req} . ':' . $config{pop3port_req};
40 $pop3user = $config{pop3user_req};
41 $pop3pass = $config{pop3pass_req};
42 $pop3delete = $config{pop3delete_req};
43 $pop3uidlcache = $config{pop3uidlcache_req};
44 } elsif ($caller == 2) {
45 # called from uvbounce.pl
46 $pop3server = $config{pop3server_bounce} . ':' . $config{pop3port_bounce};
47 $pop3user = $config{pop3user_bounce};
48 $pop3pass = $config{pop3pass_bounce};
49 $pop3delete = $config{pop3delete_bounce};
50 $pop3uidlcache = $config{pop3uidlcache_bounce};
52 $pop3server = $config{pop3server} . ':' . $config{pop3port};
53 $pop3user = $config{pop3user};
54 $pop3pass = $config{pop3pass};
55 $pop3delete = $config{pop3delete};
56 $pop3uidlcache = $config{pop3uidlcache};
59 # read list of seen mails (UIDLs)
60 my %uidls = (); # hash for quick searching
61 my @uidls = (); # array to preserve order
63 open (UIDLCACHE, "<$pop3uidlcache") or $cacheexist = 0;
65 while (my $uidl = <UIDLCACHE>) {
73 print UVmessage::get("READMAIL_STATUS"), "\n" unless ($caller == 2);
75 # open POP3 connection and get new mails
77 my $pop = Net::POP3->new($pop3server)
78 or die UVmessage::get("READMAIL_NOCONNECTION") . "\n\n";
80 my $mailcount = $pop->login($pop3user, $pop3pass);
82 die UVmessage::get("READMAIL_NOLOGIN") . "\n\n" unless ($mailcount);
84 for (my $n=1; $n<=$mailcount; $n++) {
85 my $uidl = $pop->uidl($n);
87 next if ($uidls{$uidl});
91 my $mailref = $pop->get($n)
92 or print STDERR UVmessage::get("READMAIL_GET_PROBLEM", (NR => $n)) . "\n";
93 my $mail = join ('', @$mailref);
94 my $fromline = 'From ';
95 if ($mail =~ /From: .*?<(.+?)>/) {
97 } elsif ($mail =~ /From:\s+?(\S+?\@\S+?)\s/) {
100 $fromline .= 'foo@bar.invalid';
102 $fromline .= ' ' . strftime ('%a %b %d %H:%M:%S %Y', localtime) . "\n";
103 push (@mails, $fromline . $mail);
106 or print STDERR UVmessage::get("READMAIL_DEL_PROBLEM", (NR => $n)) . "\n";
112 open (UIDLCACHE, ">$pop3uidlcache") or $uidlerr = 1;
114 print STDERR UVmessage::get("READMAIL_UIDL_PROBLEM") . "\n";
115 print STDERR UVmessage::get("READMAIL_UIDL_PROBLEM2") . "\n";
117 print UIDLCACHE join("\n", @uidls);
118 close (UIDLCACHE) or print STDERR UVmessage::get("READMAIL_UIDL_CLOSE") . "\n";
121 # make archive of all mails
123 open (VOTES, ">$filename") or $fileproblem = 1;
125 print STDERR UVmessage::get("READMAIL_ARCHIVE_PROBLEM",
126 (FILE => $filename)) . "\n";
128 print VOTES join ("\n", @mails);
130 or print STDERR UVmessage::get("READMAIL_ARCHIVE_CLOSE",
131 (FILE => $filename)) . "\n";
138 open(VOTES, "<$filename")
139 or die UVmessage::get("READMAIL_NOMAILFILE", (FILE => $filename)) . "\n\n";
144 if (/$config{mailstart}/) {
147 $mails[$i] = ($mails[$i] || "") . $_;
154 foreach my $mail (@mails) {
157 # split mail into array and remove first line (from line)
158 my @mail = split(/\n/, $mail);
159 shift (@mail) if ($mail[0] =~ /^From /);
161 # generate MIME-Parser object for the mail
162 my $parser = new MIME::Parser;
163 # headers are to be decoded
164 $parser->decode_headers(1);
165 # don't write into file
166 $parser->output_to_core(1);
169 my $entity = $parser->parse_data(join("\n", @mail));
170 my $head = $entity->head;
172 # extract address and name
173 my $from = $head->get('From') || '';
175 if ($from =~ /\s*([^<]\S+\@\S+[^>]) \((.+)\)/) {
176 ($voter_addr, $voter_name) = ($1, $2);
177 } elsif ($from =~ /\s*\"?([^\"]+)\"?\s*<(\S+\@\S+)>/) {
178 ($voter_name, $voter_addr) = ($1, $2);
179 $voter_name =~ s/\s+$//; # kill spaces at the end
180 } elsif ($from =~ /\s*<?(\S+\@[^\s>]+)>?[^\(\)]*/) {
181 ($voter_addr, $voter_name) = ($1, '');
183 # initialize with empty value
189 if ($config{replyto}) {
191 my $replyto = Mail::Field->new('Reply-To', $head->get('Reply-To'));
193 # Address in Reply-To?
194 ($voter_addr) = $replyto->addresses() if ($replyto->addresses());
197 if ($replyto->names()) {
198 my ($nametmp) = $replyto->names();
199 $voter_name = $nametmp unless ($nametmp =~ /^\s*$/);
205 my $encoding = $head->get('Content-Transfer-Encoding') || '';
206 if ($encoding =~ /quoted-printable/i) {
207 $body = decode_qp($entity->stringify_body);
208 } elsif ($encoding =~ /base64/i) {
209 $body = decode_base64($entity->stringify_body);
211 $body = $entity->stringify_body;
214 my $h_date = $head->get('Date') || '';
217 # call referred sub and increase counter
218 &$callsub($voter_addr, $voter_name, $h_date, $entity, \$body);