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