| 1 | #!/usr/bin/perl -w |
| 2 | |
| 3 | ############################################################################### |
| 4 | # UseVoteGer 4.09 Bounce-Verarbeitung |
| 5 | # (c) 2001-2005 Marc Langer <uv@marclanger.de> |
| 6 | # |
| 7 | # This script package is free software; you can redistribute it and/or |
| 8 | # modify it under the terms of the GNU Public License as published by the |
| 9 | # Free Software Foundation. |
| 10 | # |
| 11 | # Use this script to process bounce messages and generate a list of |
| 12 | # undeliverable voter adresses. |
| 13 | # |
| 14 | # Many thanks to: |
| 15 | # - Ron Dippold (Usevote 3.0, 1993/94) |
| 16 | # - Frederik Ramm (German translation, 1994) |
| 17 | # - Wolfgang Behrens (UseVoteGer 3.1, based on Frederik's translation, 1998/99) |
| 18 | # - Cornell Binder for some good advice and code fragments |
| 19 | # |
| 20 | # This is a complete rewrite of UseVoteGer 3.1 in Perl (former versions were |
| 21 | # written in C). Not all functions of Usevote/UseVoteGer 3.x are implemented! |
| 22 | ############################################################################### |
| 23 | |
| 24 | use strict; |
| 25 | use Getopt::Long; |
| 26 | use FindBin qw($Bin); |
| 27 | use lib $Bin; |
| 28 | use UVconfig; |
| 29 | use UVreadmail; |
| 30 | use UVtemplate; |
| 31 | |
| 32 | my %opt_ctl = (); |
| 33 | my %bounces = (); |
| 34 | my $pop3 = 0; |
| 35 | |
| 36 | print STDERR "\n$usevote_version Bounce-Verarbeitung - (c) 2001-2005 Marc Langer\n\n"; |
| 37 | |
| 38 | # unknown parameters remain in @ARGV (for "help") |
| 39 | Getopt::Long::Configure(qw(pass_through bundling)); |
| 40 | |
| 41 | # Put known parameters in %opt_ctl |
| 42 | GetOptions(\%opt_ctl, qw(h|help f|file config-file=s c=s)); |
| 43 | |
| 44 | # Get name auf config file (default: usevote.cfg) and read it |
| 45 | my $cfgfile = $opt_ctl{'config-file'} || $opt_ctl{c} || "usevote.cfg"; |
| 46 | UVconfig::read_config($cfgfile); |
| 47 | |
| 48 | # check POP3 settings in usevote.cfg and combination with -f parameter |
| 49 | $pop3 = 1 if ($config{pop3} && !$opt_ctl{f}); |
| 50 | |
| 51 | # Additional parameters or invalid options? Show help and exit. |
| 52 | help() if ($opt_ctl{h} || !(@ARGV || $pop3)); |
| 53 | |
| 54 | # check for lock file |
| 55 | if (-e $config{lockfile}) { |
| 56 | my $lockfile = $config{lockfile}; |
| 57 | |
| 58 | # don't delete lockfile in END block ;-) |
| 59 | $config{lockfile} = ''; |
| 60 | |
| 61 | # exit |
| 62 | die UVmessage::get("ERR_LOCK", (FILE=>$lockfile)) . "\n\n"; |
| 63 | } |
| 64 | |
| 65 | # safe exit (delete lockfile) |
| 66 | $SIG{QUIT} = 'sighandler'; |
| 67 | $SIG{INT} = 'sighandler'; |
| 68 | $SIG{KILL} = 'sighandler'; |
| 69 | $SIG{TERM} = 'sighandler'; |
| 70 | $SIG{HUP} = 'sighandler'; |
| 71 | |
| 72 | # create lock file |
| 73 | open (LOCKFILE, ">$config{lockfile}"); |
| 74 | close (LOCKFILE); |
| 75 | |
| 76 | # read and process mails |
| 77 | # for each mail pass a reference to the sub to be called |
| 78 | |
| 79 | if ($pop3) { |
| 80 | unless (-d $config{archivedir}) { |
| 81 | mkdir ($config{archivedir}, 0700) |
| 82 | or die UVmessage::get("ERR_MKDIR", (DIR => $config{archivedir})) . "$!\n\n"; |
| 83 | } |
| 84 | |
| 85 | # mails are saved in file |
| 86 | # normally unixtime is sufficient for a unique file name, else append PID |
| 87 | my $ext = time; |
| 88 | |
| 89 | opendir (ARCHIVE, $config{archivedir}); |
| 90 | my @fertigfiles = readdir (ARCHIVE); |
| 91 | closedir (ARCHIVE); |
| 92 | |
| 93 | # append PID if file name already exists |
| 94 | $ext .= "-$$" if (grep (/$ext/, @fertigfiles)); |
| 95 | |
| 96 | my $file = "$config{archivedir}/bounces-" . $ext; |
| 97 | UVreadmail::process($file, \&process_bounce, 2); # 2 = POP3 |
| 98 | |
| 99 | } else { |
| 100 | foreach my $file (@ARGV) { |
| 101 | UVreadmail::process($file, \&process_bounce, 3); # 3 = existing file |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | my $template = UVtemplate->new(); |
| 106 | |
| 107 | foreach my $address (sort keys %bounces) { |
| 108 | my $name = $bounces{$address}; |
| 109 | my $request = 0; |
| 110 | my $text; |
| 111 | if ($name eq '***request***') { |
| 112 | $name = ''; |
| 113 | $text = UVmessage::get ("BOUNCE_BALLOT") . "\n"; |
| 114 | } else { |
| 115 | $text = UVmessage::get ("BOUNCE_ACKMAIL") . "\n"; |
| 116 | } |
| 117 | $name = ' ' unless($name); |
| 118 | $template->addListItem('bounces', name=>$name, mail=>$address, bouncetext=>$text); |
| 119 | } |
| 120 | |
| 121 | print $template->processTemplate($config{'tpl_bouncelist'}); |
| 122 | exit 0; |
| 123 | |
| 124 | |
| 125 | ############################################################################## |
| 126 | # Evaluate a bounce # |
| 127 | # This sub is called from UVreadmail::process() for every mail # |
| 128 | # Parameters: voter address and name, date header (strings) # |
| 129 | # complete header and body (references to strings) # |
| 130 | ############################################################################## |
| 131 | |
| 132 | sub process_bounce { |
| 133 | # last element of @_ is the body, other stuff not needed here |
| 134 | my $body = pop; |
| 135 | my ($address, $name); |
| 136 | |
| 137 | # search body for voter name and address |
| 138 | if ($$body =~ /$config{addresstext}\s+(\S+@\S+)/) { |
| 139 | $address = $1; |
| 140 | if ($$body =~ /$config{nametext2}\s+(.*)$/m) { |
| 141 | $name = $1; |
| 142 | $bounces{$address} = $name; |
| 143 | } elsif ($$body =~ /$config{nametext}/) { |
| 144 | # Text from this config option does only appear in ballots, |
| 145 | # not in acknowledge mails. So this has to be a bounced ballot request |
| 146 | $bounces{$address} = '***request***'; |
| 147 | } else { |
| 148 | $bounces{$address} = ''; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | |
| 154 | END { |
| 155 | # delete lockfile |
| 156 | unlink $config{lockfile} if ($config{lockfile}); |
| 157 | } |
| 158 | |
| 159 | sub sighandler { |
| 160 | my ($sig) = @_; |
| 161 | die "\n\nSIG$sig: deleting lockfile and exiting\n\n"; |
| 162 | } |
| 163 | |
| 164 | |
| 165 | ############################################################################## |
| 166 | # Print help text (options and syntax) on -h or --help # |
| 167 | ############################################################################## |
| 168 | |
| 169 | sub help { |
| 170 | print <<EOF; |
| 171 | Usage: uvbounce.pl [-c config_file] [-f] DATEI1 [DATEI2 [...]] |
| 172 | uvbounce.pl -h |
| 173 | |
| 174 | Liest Bounces aus den uebergebenen Dateien oder aus einer POP3-Mailbox ein |
| 175 | (Verhalten haengt von usevote.cfg ab) und generiert eine Liste von |
| 176 | unzustellbaren Adressen, die an den 2. CfV oder das Result angehaengt |
| 177 | werden kann. Falls POP3 in usevote.cfg eingeschaltet und die Option -f |
| 178 | (siehe unten) nicht benutzt wurde, werden die uebergebenen Dateinamen |
| 179 | ignoriert. |
| 180 | |
| 181 | -c config_file liest die Konfiguration aus config_file |
| 182 | (usevote.cfg falls nicht angegeben) |
| 183 | |
| 184 | -f, --file liest die Bounces aus den uebergebenen Dateien, auch |
| 185 | wenn in der Konfigurationsdatei POP3 eingeschaltet ist. |
| 186 | Diese Option wird benoetigt, falls zwar die Stimmzettel |
| 187 | per POP3 eingelesen werden sollen, nicht aber die Bounces. |
| 188 | |
| 189 | -h, --help zeigt diesen Hilfetext an |
| 190 | |
| 191 | EOF |
| 192 | |
| 193 | exit 0; |
| 194 | } |