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