Merge branch 'pu/fixdupnovote'
[usenet/usevote.git] / UVconfig.pm
1 # UVconfig: Reads config files and tests configuration
2 # Used by all components
3
4 package UVconfig;
5
6 use strict;
7 use Net::Domain qw(hostname hostfqdn hostdomain);
8 use UVmessage;
9 use vars qw(@ISA @EXPORT $VERSION $usevote_version %config %messages
10             @rules @groups $bdsg_regexp $bdsg2_regexp %ids %functions);
11
12 require Exporter;
13 @ISA = qw(Exporter);
14 @EXPORT = qw($usevote_version %config %messages @rules @groups
15              $bdsg_regexp $bdsg2_regexp %ids %functions);
16
17 # Module version
18 $VERSION = "0.18";
19
20 # Usevote version
21 $usevote_version = "UseVoteGer 4.09";
22
23 sub read_config {
24
25   my ($cfgfile, $redir_errors) = @_;
26   
27   # Default configuration options (overwritten in usevote.cfg)
28   %config = (votefile             => "votes",
29              votename             => "unkonfiguriertes Usevote",
30              resultfile           => "ergebnis.alle",
31              rulefile             => "usevote.rul",
32              badaddrfile          => "mailpatterns.cfg",
33              messagefile          => "messages.cfg",
34              idfile               => "scheinkennungen",
35              requestfile          => "anforderung",
36              errorfile            => "errors.log",
37              lockfile             => "usevote.lock",
38              replyto              => 0,
39              personal             => 0,
40              proportional         => 0,
41              bdsg                 => 0,
42              onestep              => 0,
43              multigroup           => 0,
44              voteack              => 1,
45              voteaccount          => "<> (unkonfiguriertes Usevote)",
46              mailfrom             => "<> (unkonfiguriertes Usevote)",
47              envelopefrom         => "<>",
48              mailstart            => "^From ",
49              archivedir           => "fertig",
50              tmpdir               => "tmp",
51              templatedir          => "templates",
52              formats              => "UVformats.pm",
53              domailfile           => "tmp/domail",
54              controlfile          => "tmp/ack.control",
55              mailcmd              => "sendmail -oi -oem",
56              mailcc               => "",
57              sleepcmd             => "sleep 1",
58              clearcmd             => "clear",
59              pager                => "less",
60              pop3                 => 0,
61              pop3server           => "localhost",
62              pop3port             => 110,
63              pop3user             => "default",
64              pop3pass             => "default",
65              pop3delete           => 0,
66              pop3uidlcache        => "uidlcache",
67              pop3server_req       => "localhost",
68              pop3port_req         => 110,
69              pop3user_req         => "default",
70              pop3pass_req         => "default",
71              pop3delete_req       => 0,
72              pop3uidlcache_req    => "uidlcache_req",
73              pop3server_bounce    => "localhost",
74              pop3port_bounce      => 110,
75              pop3user_bounce      => "default",
76              pop3pass_bounce      => "default",
77              pop3delete_bounce    => 0,
78              pop3uidlcache_bounce => 'uidlcache_bounce',
79              smtp                 => 0,
80              smtpserver           => 'localhost',
81              smtpport             => 25,
82              smtphelo             => hostfqdn(),
83              fqdn                 => hostfqdn(),
84              smtpauth             => 0,
85              smtpuser             => '',
86              smtppass             => '',
87              name_re              => '[a-zA-ZäöüÄÖÜß-]{2,} +.*[a-zA-ZäöüÄÖÜß]{2,}',
88              ja_stimme            => '(J\s*A|J|(D\s*A\s*)?F\s*U\s*E\s*R)',
89              nein_stimme          => '(N\s*E\s*I\s*N|N|(D\s*A\s*)?G\s*E\s*G\s*E\s*N)',
90              enth_stimme          => '(E|E\s*N\s*T\s*H\s*A\s*L\s*T\s*U\s*N\s*G)',
91              ann_stimme           => 'A\s*N\s*N\s*U\s*L\s*L\s*I\s*E\s*R\s*U\s*N\s*G',
92              condition1           => '$yes>=2*$no', # twice as many yes as no
93              condition2           => '$yes>=60',    # min 60 yes votes
94              prop_formula         => '$yes/$no',
95              tpl_ack_mail         => 'ack-mail',
96              tpl_bouncelist       => 'bouncelist',
97              tpl_mailheader       => 'mailheader',
98              tpl_result_multi     => 'result-multi',
99              tpl_result_single    => 'result-single',
100              tpl_result_prop      => 'result-proportional',
101              tpl_votes_multi      => 'votes-multi',
102              tpl_votes_single     => 'votes-single',
103              tpl_voterlist        => 'voterlist',
104              tpl_ballot           => 'ballot',
105              tpl_ballot_request   => 'ballot-request',
106              tpl_ballot_personal  => 'ballot-personal',
107              tpl_addr_reg         => 'address-not-registered',
108              tpl_no_ballotid      => 'no-ballotid',
109              tpl_wrong_ballotid   => 'wrong-ballotid',
110              tpl_bdsg_error       => 'bdsg-error',
111              tpl_cancelled        => 'cancelled',
112              tpl_invalid_account  => 'invalid-account',
113              tpl_invalid_name     => 'invalid-name',
114              tpl_multiple_votes   => 'multiple-votes',
115              tpl_no_ballot        => 'no-ballot',
116              tpl_no_votes         => 'no-votes',
117              tpl_rule_violated    => 'rule-violated',
118              begin_divider        => 'Alles vor dieser Zeile bitte loeschen',
119              end_divider          => 'Alles nach dieser Zeile bitte loeschen',
120              nametext             => 'Dein Realname, falls nicht im FROM-Header:',
121              nametext2            => 'Waehlername:',
122              addresstext          => 'Waehleradresse:',
123              ballotidtext         => 'Wahlscheinkennung:',
124              bdsgtext             => 'Datenschutzklausel - Zustimmung',
125              bdsgfile             => 'bdsgtext.cfg',
126              rightmargin          => 72,
127              usevote_version      => $usevote_version); # needed for use in templates
128
129   # read config
130   read_file($cfgfile);
131
132   # read message file
133   open (RES, "<$config{messagefile}")
134      or die "Could not read message file $config{messagefile}!\n\n";
135   my @lines = <RES>;
136   close(RES);
137
138   foreach my $line (@lines) {
139     chomp($line);
140     $line =~ s/^#.*//;        # Delete comments
141     if ($line =~ m/^\s*([A-Za-z0-9_-]+)\s*=\s*(.+)\s*$/){
142       $messages{$1} = $2;
143     }
144   } 
145
146   # missing "groupX =" lines in config file?
147   die UVmessage::get("CONF_NOGROUPS", CONFIGFILE=>$cfgfile) . "\n\n" unless (@groups);
148
149   # redirect errors to a file if desired by calling script
150   open (STDERR, ">$config{errorfile}") if ($redir_errors);
151
152   # check for data protection law? read text for ballot
153   parse_bdsgtext() if ($config{bdsg});
154
155   # personalized ballots? read ballot IDs
156   read_ballot_ids() if ($config{personal});
157
158   load_formats() if ($config{formats});
159  
160 }
161
162
163 ##############################################################################
164 # read config file                                                           #
165 ##############################################################################
166
167 sub read_file {
168
169   my $cfgfile = shift;
170   my $CONFIG;
171   open ($CONFIG, "<$cfgfile") or die "Could not find config file $cfgfile!\n\n";
172
173   while (<$CONFIG>) {
174     next if (/^#/);     # line is a comment
175     chomp;              # delete \n
176     s/\r//;             # delete \r if present
177     s/([^\\])#.*$/$1/;  # Remove comments not starting at beginning of line.
178                         # (ignore escaped comment sign \#)
179
180
181     if (/^include (\S+)$/) {
182       # include other config file
183       read_file($1);
184
185     } elsif (my($key, $value) = split (/\s*=\s*/, $_, 2)) {
186       # delete trailing spaces
187       $value =~ s/\s*$//;
188
189       # evaluate quotation marks
190       $value =~ s/^\"([^\"]+[^\\\"])\".*$/$1/;
191       $value =~ s/\\"/"/g;
192
193       if ($key =~ /^group(\d+)$/) {
194         my $num = $1;
195         $groups[$num-1] = $value;    # internal index starts at 0
196       } else {
197         $config{$key} = $value;
198       }
199     }
200   }
201
202   close ($CONFIG);
203
204 }
205
206
207 ##############################################################################
208 # parse data protection law texts                                            #
209 ##############################################################################
210
211 sub parse_bdsgtext {
212
213   open (BDSG, "<$config{bdsgfile}") or die UVmessage::get("CONF_NOBDSGFILE",
214                             ('BDSGFILE' => "$config{bdsgfile}")) . "\n\n";
215   my @bdsg = <BDSG>;
216   close BDSG;
217
218   $config{bdsginfo} = '';
219
220   foreach my $line (@bdsg) {
221     $config{bdsginfo} .= $line unless ($line =~ /^\s*#/);
222   }
223
224   my $bdsgtmp = $config{bdsginfo};
225   $bdsgtmp =~ s/\"/\\\"/g;
226   $bdsgtmp =~ s/\'/\\\'/g;
227   $bdsgtmp =~ s/\(/\\\(/g;
228   $bdsgtmp =~ s/\)/\\\)/g;
229   $bdsgtmp =~ s/\[/\\\[/g;
230   $bdsgtmp =~ s/\]/\\\]/g;
231   $bdsgtmp =~ s/\./\\\./g;
232   $bdsgtmp =~ s/\!/\\\!/g;
233   my @bdsgtext = split(' ', $bdsgtmp);
234
235   # Build Regular Expression from single words.
236   # There has to be at least a space between two words, additional characters
237   # are allowed, e.g. quotation marks (but no letters)
238   $bdsg_regexp = join('\s\W*?', @bdsgtext);
239
240   # Build Regular Expression from $config{bdsgtext}
241   $bdsg2_regexp = join('\s\W*?', split(' ', $config{bdsgtext}));
242 }
243   
244
245 ##############################################################################
246 # Read suspicious mail addresses (normally mailpatterns.cfg)                 #
247 ##############################################################################
248
249 sub read_badaddr {
250   
251   my @bad_addr = ();
252
253   open (BADADDR, "<$config{badaddrfile}") or die 
254     UVmessage::get("CONF_NOBADADDR",(BADADDRFILE => $config{badaddrfile})) . "\n\n";
255
256   while (<BADADDR>) {
257     chomp;
258     # Comment line? Not only whitespaces?
259     if (/^[^#]/ && /[^\s]/) {
260       push(@bad_addr, $_);
261     }
262   }
263
264   close (BADADDR);
265   return @bad_addr;
266 }
267
268
269 ##############################################################################
270 # Read ballot IDs                                                            #
271 ##############################################################################
272
273 sub read_ballot_ids {
274   # open file with ballot ids
275   open(FILE, "<$config{idfile}") or return 1;
276   while (<FILE>) {
277     chomp;
278     # Format: mailaddress (whitespace) ballot ID
279     if (/^(.+@.+)\s+([a-z0-9]+)/) {
280       # $1=mailadresse, $2=ballot ID
281       $ids{$1} = $2;
282     }
283   }
284   close(FILE);
285   return 0;
286 }
287
288
289 ##############################################################################
290 # Funktionen für Templates laden                                             #
291 ##############################################################################
292
293 sub load_formats {
294   my $modules = $config{formats};
295
296   my @modules = split(/\s*,\s*/, $modules);
297
298   foreach my $module (@modules){
299     if (-r $module){
300       require $module;
301     }
302   }
303 }
304
305
306 ##############################################################################
307 # config test                                                                #
308 ##############################################################################
309
310 sub test_config {
311   print UVmessage::get("CONF_CONFIG"), "\n\n";
312   foreach my $option (keys %config) {
313     print "$option = $config{$option}\n";
314   }
315
316   print "\n", UVmessage::get("CONF_TEST_RULES");
317   if (@rules) {
318     print "\n\n";
319     for (my $n=0; $n<@rules; $n++) {
320       my $text = UVrules::rule_print($n);
321       print $text;
322     }
323     print "\n";
324   } else {
325     print UVmessage::get("CONF_NO_RULES"), "\n\n";
326   }
327 }
328
329 1;
This page took 0.019407 seconds and 3 git commands to generate.