Documentation: Add votename to UVmenu.
[usenet/usevote.git] / UVreadmail.pm
... / ...
CommitLineData
1# UVreadmail: functions for reading and processing mailfiles
2# Used by uvvote.pl, uvcfv.pl, uvbounce.pl
3
4package UVreadmail;
5
6use strict;
7use UVconfig;
8use UVmessage;
9use MIME::QuotedPrint;
10use MIME::Base64;
11use MIME::Parser;
12use Mail::Box::Manager;
13use POSIX qw(strftime);
14
15use vars qw($VERSION);
16
17# Module version
18$VERSION = "0.11";
19
20sub process {
21
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}
27 #
28
29 my ($filename, $callsub, $caller) = @_;
30 my ($voter_addr, $voter_name, $body);
31 my $count = 0;
32 my ($pop3server, $pop3user, $pop3pass, $pop3delete, $pop3uidlcache);
33 my @mails = ();
34 $caller ||= 0;
35
36 if ($config{pop3} && $caller<3) {
37
38 if ($caller == 1) {
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};
52 } else {
53 $pop3server = $config{pop3server} . ':' . $config{pop3port};
54 $pop3user = $config{pop3user};
55 $pop3pass = $config{pop3pass};
56 $pop3delete = $config{pop3delete};
57 $pop3uidlcache = $config{pop3uidlcache};
58 }
59
60 # read list of seen mails (UIDLs)
61 my %uidls = (); # hash for quick searching
62 my @uidls = (); # array to preserve order
63 my $cacheexist = 1;
64 open (UIDLCACHE, "<$pop3uidlcache") or $cacheexist = 0;
65 if ($cacheexist) {
66 while (my $uidl = <UIDLCACHE>) {
67 chomp ($uidl);
68 $uidls{$uidl} = 1;
69 push (@uidls, $uidl);
70 }
71 close (UIDLCACHE);
72 }
73
74 print UVmessage::get("READMAIL_STATUS"), "\n" unless ($caller == 2);
75
76 # open POP3 connection and get new mails
77 use Net::POP3;
78 my $pop = Net::POP3->new($pop3server)
79 or die UVmessage::get("READMAIL_NOCONNECTION") . "\n\n";
80
81 my $mailcount = $pop->login($pop3user, $pop3pass);
82
83 die UVmessage::get("READMAIL_NOLOGIN") . "\n\n" unless ($mailcount);
84
85 for (my $n=1; $n<=$mailcount; $n++) {
86 my $uidl = $pop->uidl($n);
87 if ($uidl) {
88 next if ($uidls{$uidl});
89 $uidls{$uidl} = 1;
90 push (@uidls, $uidl);
91 }
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: .*?<(.+?)>/) {
97 $fromline .= $1;
98 } elsif ($mail =~ /From:\s+?(\S+?\@\S+?)\s/) {
99 $fromline .= $1;
100 } else {
101 $fromline .= 'foo@bar.invalid';
102 }
103 $fromline .= ' ' . strftime ('%a %b %d %H:%M:%S %Y', localtime) . "\n";
104 push (@mails, $fromline . $mail);
105 if ($pop3delete) {
106 $pop->delete($n)
107 or print STDERR UVmessage::get("READMAIL_DEL_PROBLEM", (NR => $n)) . "\n";
108 }
109 }
110
111 # save UIDLs
112 my $uidlerr = 0;
113 open (UIDLCACHE, ">$pop3uidlcache") or $uidlerr = 1;
114 if ($uidlerr) {
115 print STDERR UVmessage::get("READMAIL_UIDL_PROBLEM") . "\n";
116 print STDERR UVmessage::get("READMAIL_UIDL_PROBLEM2") . "\n";
117 } else {
118 print UIDLCACHE join("\n", @uidls);
119 close (UIDLCACHE) or print STDERR UVmessage::get("READMAIL_UIDL_CLOSE") . "\n";
120 }
121
122 $pop->quit();
123
124 # Mailbox / Maildir
125 } else {
126
127 my $mgr = Mail::Box::Manager->new;
128 my $folder;
129
130 eval{
131 $folder = $mgr->open( folder => $config{votefile},
132 create => 0,
133 access => 'rw',
134 type => $config{mailboxtype},
135 expand => 'LAZY',
136 );
137 };
138 die UVmessage::get("READMAIL_NOMAILFILE", (FILE => $config{votefile})) . "\n\n" if $@;
139
140 # Iterate over the messages.
141 foreach (@$folder) {
142 my $mail = $_->string;
143 $_->delete();
144 my $fromline = 'From ';
145 if ($mail =~ /From: .*?<(.+?)>/) {
146 $fromline .= $1;
147 } elsif ($mail =~ /From:\s+?(\S+?\@\S+?)\s/) {
148 $fromline .= $1;
149 } else {
150 $fromline .= 'foo@bar.invalid';
151 }
152 $fromline .= ' ' . localtime() . "\n"; #strftime ('%a %b %d %H:%M:%S %Y', localtime) . "\n";
153 push (@mails, $fromline . $mail);
154 }
155 }
156
157 # make archive of all mails
158 my $fileproblem = 0;
159 open (VOTES, ">$filename") or $fileproblem = 1;
160 if ($fileproblem) {
161 print STDERR UVmessage::get("READMAIL_ARCHIVE_PROBLEM",
162 (FILE => $filename)) . "\n";
163 } else {
164 print VOTES join ("\n", @mails);
165 close (VOTES)
166 or print STDERR UVmessage::get("READMAIL_ARCHIVE_CLOSE",
167 (FILE => $filename)) . "\n";
168 }
169
170 foreach my $mail (@mails) {
171 next unless $mail;
172
173 # split mail into array and remove first line (from line)
174 my @mail = split(/\n/, $mail);
175 shift (@mail) if ($mail[0] =~ /^From /);
176
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);
183
184 # read mail
185 my $entity = $parser->parse_data(join("\n", @mail));
186 my $head = $entity->head;
187
188 # extract address and name
189 my $from = $head->get('From') || '';
190
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, '');
198 } else {
199 # initialize with empty value
200 $voter_addr = '';
201 $voter_name = '';
202 }
203
204 # look at reply-to?
205 if ($config{replyto}) {
206
207 my $replyto = Mail::Field->new('Reply-To', $head->get('Reply-To'));
208
209 # Address in Reply-To?
210 ($voter_addr) = $replyto->addresses() if ($replyto->addresses());
211
212 # Name in reply-to?
213 if ($replyto->names()) {
214 my ($nametmp) = $replyto->names();
215 $voter_name = $nametmp unless ($nametmp =~ /^\s*$/);
216 }
217
218 }
219
220 # decode body
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);
226 } else {
227 $body = $entity->stringify_body;
228 }
229
230 my $h_date = $head->get('Date') || '';
231 chomp $h_date;
232
233 # call referred sub and increase counter
234 &$callsub($voter_addr, $voter_name, $h_date, $entity, \$body);
235 $count++;
236 }
237
238 return $count;
239}
240
2411;
This page took 0.010676 seconds and 4 git commands to generate.