1 # UVmenu: menu for interaction with the votetaker
2 # Used by uvvote.pl, uvcfv.pl, uvcount.pl
10 use vars qw($VERSION);
12 use Text::Wrap qw(wrap $columns);
17 ##############################################################################
18 # Menu for interaction with the votetaker #
19 # Parameters: votes list and header (references to arrays) #
20 # Body, Mailadress, Name, Ballot ID (references to strings) #
21 # List of newly set fields (reference to array) #
22 # List of errors to correct (Array-Ref) #
23 # Return Values: 'w': proceed #
24 # 'i': ignore (don't save vote) #
25 ##############################################################################
28 my ($votes, $header, $body, $addr, $name, $ballot_id, $set, $errors) = @_;
30 my $voter_addr = $$addr || '';
31 my $voter_name = $$name || '';
32 my @newvotes = @$votes;
37 foreach my $error (@$errors) {
39 # unrecognized vote: extract group number und display warning
40 if ($error =~ /^UnrecognizedVote #(\d+)#(.+)$/) {
41 $errors{UnrecognizedVote} ||= UVmessage::get("MENU_UNRECOGNIZEDVOTE");
42 $errors{UnrecognizedVote} .= "\n " . UVmessage::get("MENU_UNRECOGNIZED_LIST")
45 # violated rule: extract rule number and display warning
46 } elsif ($error =~ /^ViolatedRule #(\d+)$/) {
47 $errors{ViolatedRule} ||= UVmessage::get("MENU_VIOLATEDRULE", (RULE => "#$1"));
50 # special handling if called from uvballot.pl
51 $mailonly = 1 if ($error =~ s/Ballot$//);
53 # get error message for this error from messages.cfg
54 $errors{$error} = UVmessage::get("MENU_" . uc($error));
58 # This loop is only left by 'return'
61 system($config{clearcmd});
62 print UVmessage::get("MENU_PROBLEMS") . "\n";
64 foreach my $error (keys %errors) {
65 print "* $errors{$error}\n";
68 my $menucaption = UVmessage::get("MENU_CAPTION");
69 print "\n\n$menucaption\n";
70 print "=" x length($menucaption), "\n\n";
71 print "(1) ", UVmessage::get("MENU_SHOW_MAIL"), "\n\n",
72 UVmessage::get("MENU_CHANGE_PROPERTIES"), "\n",
73 "(2) ", UVmessage::get("MENU_ADDRESS"), " [$voter_addr]\n";
75 # don't print these options if called from uvcfv.pl
77 print "(3) ", UVmessage::get("MENU_NAME"), " [$voter_name]\n";
78 print "(4) ", UVmessage::get("MENU_VOTES"), " [", @$votes, "]\n";
79 print "(5) ", UVmessage::get("MENU_BALLOT_ID"), " [$$ballot_id]\n"
80 if ($config{personal});
81 print "(6) ", UVmessage::get("MENU_BDSG"), "\n" if ($config{bdsg});
85 "(i) ", UVmessage::get("MENU_IGNORE"), "\n",
86 "(w) ", UVmessage::get("MENU_PROCEED"), "\n\n",
87 UVmessage::get("MENU_PROMPT");
89 do { $input = <STDIN>; } until ($input);
93 # only accept 1, 2, i and w if called from uvcfv.pl
94 next if ($mailonly && $input !~ /^[12iw]$/i);
97 system($config{clearcmd});
98 # ignore SIGPIPE (Bug in more and less)
99 $SIG{PIPE} = 'IGNORE';
100 open (MORE, "|$config{pager}");
101 print MORE join("\n", @$header), "\n\n", $$body, "\n";
104 print "\n", UVmessage::get("MENU_GETKEY");
107 } elsif ($input eq '2') {
110 print "[a] ", UVmessage::get("MENU_ADDRESS_OK"), "\n",
111 "[b] ", UVmessage::get("MENU_ADDRESS_CHANGE"), "\n",
112 "[c] ", UVmessage::get("MENU_ADDRESS_INVALID"), "\n\n",
113 UVmessage::get("MENU_PROMPT");
115 } until ($sel =~ /^[abc]$/i);
116 if ($sel =~ /^a$/i) {
117 delete $errors{SuspiciousAccount};
118 delete $errors{InvalidAddress};
120 } elsif ($sel =~ /^c$/i) {
121 delete $errors{SuspiciousAccount};
122 $errors{InvalidAddress} = UVmessage::get("MENU_INVALIDADDRESS") . " " .
123 UVmessage::get("MENU_INVALIDADDRESS2");
128 print "\n", UVmessage::get("MENU_ADDRESS_PROMPT"), " ";
129 $voter_addr = <STDIN>;
131 } until ($voter_addr);
132 $$addr = $voter_addr;
133 push (@$set, 'Adresse');
134 delete $errors{SuspiciousAccount};
135 delete $errors{InvalidAddress};
136 check_ballotid(\%errors, \$voter_addr, $ballot_id, \%ids);
138 } elsif ($input eq '3') {
141 print "[a] ", UVmessage::get("MENU_NAME_OK"), "\n",
142 "[b] ", UVmessage::get("MENU_NAME_CHANGE"), "\n",
143 "[c] ", UVmessage::get("MENU_NAME_INVALID"), "\n\n",
144 UVmessage::get("MENU_PROMPT");
146 } until ($sel =~ /^[abc]$/i);
147 if ($sel =~ /^a$/i) {
148 delete $errors{InvalidName};
150 } elsif ($sel =~ /^c$/i) {
151 $errors{InvalidName} = UVmessage::get("MENU_INVALIDNAME");
154 print UVmessage::get("MENU_NAME"), ": ";
155 $voter_name = <STDIN>;
157 $$name = $voter_name;
158 push (@$set, 'Name');
159 delete $errors{NoName};
160 delete $errors{InvalidName};
162 $errors{InvalidName} = UVmessage::get("MENU_INVALIDNAME")
163 unless ($voter_name =~ /$config{name_re}/);
165 } elsif ($input eq '4') {
170 print "[a] ", UVmessage::get("MENU_VOTES_OK"), "\n",
171 "[b] ", UVmessage::get("MENU_VOTES_RESET"), "\n",
172 "[c] ", UVmessage::get("MENU_VOTES_INVALID"), "\n",
173 "[d] ", UVmessage::get("MENU_VOTES_CANCELLED"), "\n\n",
174 UVmessage::get("MENU_PROMPT");
176 } until ($sel =~ /^[abcd]$/i);
177 if ($sel =~ /^[ad]$/i) {
178 delete $errors{NoVote};
179 delete $errors{UnrecognizedVote};
180 delete $errors{ViolatedRule};
181 delete $errors{DuplicateVote};
182 if ($sel =~ /^d$/i) {
183 # cancelled vote: replace all votes with an A
184 @$votes = split(//, 'A' x scalar @groups);
185 push @$set, 'Stimmen';
186 # some errors are irrelevant when cancelling a vote:
187 delete $errors{InvalidName};
188 delete $errors{NoName};
189 delete $errors{InvalidBDSG};
190 delete $errors{InvalidAddress};
191 delete $errors{SuspiciousAccount};
194 } elsif ($sel =~ /^c$/i) {
195 $errors{NoVote} = UVmessage::get("MENU_INVALIDVOTE");
199 # Set columns for Text::Wrap
200 $columns = $config{rightmargin};
201 print "\n", wrap('', '', UVmessage::get("MENU_VOTES_REENTER_ASK")), "\n\n";
202 print UVmessage::get("MENU_VOTES_REENTER_LEGEND"), "\n";
204 for (my $n=0; $n<@groups; $n++) {
206 $votes->[$n] ||= 'E';
208 # repeat while invalid character entered
209 while (!($voteinput =~ /^[JNE]$/)) {
210 my $invalid = $#groups ? 0 : 1;
211 print UVmessage::get("MENU_VOTES_REENTER", (GROUP => $groups[$n]));
212 $voteinput = <STDIN>;
214 $voteinput ||= $votes->[$n];
215 $voteinput =~ tr/jne/JNE/;
218 # valid input, save new votes
219 $newvotes[$n] = $voteinput;
223 my $oldvotes = UVmessage::get("MENU_VOTES_REENTER_OLD");
224 my $newvotes = UVmessage::get("MENU_VOTES_REENTER_NEW");
225 my $oldlen = length($oldvotes);
226 my $newlen = length($newvotes);
227 my $maxlen = 1 + (($newlen>$oldlen) ? $newlen : $oldlen);
228 print $oldvotes, ' ' x ($maxlen - length($oldvotes)), @$votes, "\n",
229 $newvotes, ' ' x ($maxlen - length($newvotes)), @newvotes, "\n\n";
232 print "[a] ", UVmessage::get("MENU_VOTES_REENTER_ACK"), " ",
233 "[b] ", UVmessage::get("MENU_VOTES_REENTER_NACK"), "\n\n",
234 UVmessage::get("MENU_PROMPT");
236 } until ($sel =~ /^[ab]$/i);
238 next if ($sel =~ /^b$/i);
240 push @$set, 'Stimmen';
241 delete $errors{UnrecognizedVote};
242 delete $errors{DuplicateVote};
243 delete $errors{NoVote};
244 delete $errors{ViolatedRule};
246 if (my $rule = UVrules::rule_check($votes)) {
247 $errors{ViolatedRule} = UVmessage::get("MENU_VIOLATEDRULE", (RULE => "#$rule"));
250 } elsif ($input eq '5' && $config{personal}) {
251 print "\n", UVmessage::get("MENU_BALLOT_ID"), ": ";
252 $$ballot_id = <STDIN>;
254 push (@$set, 'Kennung');
255 check_ballotid(\%errors, \$voter_addr, $ballot_id, \%ids);
257 } elsif ($input eq '6' && $config{bdsg}) {
260 print "[a] ", UVmessage::get("MENU_BDSG_ACCEPTED"), "\n",
261 "[b] ", UVmessage::get("MENU_BDSG_DECLINED"), "\n\n",
262 UVmessage::get("MENU_PROMPT");
264 } until ($sel =~ /^[ab]$/i);
266 if ($sel =~ /^a$/i) {
267 delete $errors{InvalidBDSG};
269 $errors{InvalidBDSG} = UVmessage::get("MENU_INVALIDBDSG");
272 } elsif ($input =~ /^i$/i) {
273 my $ignore = UVmessage::get("MENU_IGNORE_STRING");
274 # Set columns for Text::Wrap
275 $columns = $config{rightmargin};
276 print wrap('', '', UVmessage::get("MENU_IGNORE_WARNING",
277 (MENU_IGNORE_STRING => $ignore)
279 if (<STDIN> eq "$ignore\n") {
284 } elsif ($input =~ /^w$/i) {
287 if ((keys %errors)==1 && $errors{UnrecognizedVote}) {
288 # unrecognized vote lines aren't errors if votetaker
289 # did not change them
292 # Set columns for Text::Wrap
293 $columns = $config{rightmargin};
294 @$errors = keys %errors;
295 my $warning = ' ' . UVmessage::get("MENU_ERROR_WARNING") . ' ';
296 my $length = length($warning);
297 print "\n", '*' x (($config{rightmargin}-$length)/2), $warning,
298 '*' x (($config{rightmargin}-$length)/2), "\n\n",
299 wrap('', '', UVmessage::get("MENU_ERROR_TEXT")), "\n\n",
300 '*' x $config{rightmargin}, "\n\n",
301 UVmessage::get("MENU_ERROR_GETKEY");
303 next if ($input !~ /^y$/i);
310 system($config{clearcmd});
311 print "\n", UVmessage::get("MENU_PROCESSING"), "\n";
317 my ($errors, $voter_addr, $ballot_id, $ids) = @_;
319 return 0 unless ($config{personal});
321 delete $errors->{NoBallotID};
322 delete $errors->{WrongBallotID};
323 delete $errors->{AddressNotRegistered};
326 if ($ids->{$$voter_addr}) {
327 if ($ids->{$$voter_addr} ne $$ballot_id) {
328 # ballot id incorrect
329 $errors->{WrongBallotID} = UVmessage::get("MENU_WRONGBALLOTID");
332 $errors->{AddressNotRegistered} = UVmessage::get("MENU_ADDRESSNOTREGISTERED");
335 $errors->{NoBallotID} = UVmessage::get("MENU_NOBALLOTID");
342 ##############################################################################
343 # Menu for sorting out duplicate votings manually #
344 # Parameters: References to hashes with the paragraphs from the result file #
345 # and the default value #
346 # Return value: selected menu item (1, 2 or 0) #
347 ##############################################################################
350 my ($vote1, $vote2, $default) = @_;
352 print STDERR "\n", UVmessage::get("MENU_DUP_VOTE"), "\n\n";
353 print STDERR UVmessage::get("MENU_DUP_FIRST"), "\n";
354 print STDERR "A: $vote1->{A}\n";
355 print STDERR "N: $vote1->{N}\n";
356 print STDERR "D: $vote1->{D}\n";
357 print STDERR "K: $vote1->{K}\n";
358 print STDERR "S: $vote1->{S}\n\n";
359 print STDERR UVmessage::get("MENU_DUP_SECOND"), "\n";
360 print STDERR "A: $vote2->{A}\n";
361 print STDERR "N: $vote2->{N}\n";
362 print STDERR "D: $vote2->{D}\n";
363 print STDERR "K: $vote2->{K}\n";
364 print STDERR "S: $vote2->{S}\n\n";
365 print STDERR "1: ", UVmessage::get("MENU_DUP_DELFIRST"), "\n",
366 "2: ", UVmessage::get("MENU_DUP_DELSECOND"), "\n",
367 "0: ", UVmessage::get("MENU_DUP_DELNONE"), "\n\n";
372 print STDERR UVmessage::get("MENU_PROMPT"), "[$default] ";
375 } until ($input eq '' || ($input >= 0 && $input<3));
377 return $input || $default;