diff options
Diffstat (limited to 'votify')
-rwxr-xr-x | votify | 220 |
1 files changed, 220 insertions, 0 deletions
@@ -0,0 +1,220 @@ +#!/usr/bin/perl -w +# $Id: votify,v 1.5 2005/05/16 04:03:46 agriffis Exp $ +# +# Copyright 2005 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 +# +# votify: generate, verify and submit voting ballots for trustee elections +# + +#BEGIN { push @INC, (getpwnam 'fox2mike')[7].'/elections' } +BEGIN { push @INC, '/etc/elections/current' } + +use POSIX; +use Getopt::Long; +use List::Util; +use Votify 'user'; +use strict; + +###################################################################### +# Global vars +###################################################################### + +(my $zero = $0) =~ s,.*/,,; +(my $version = '$Revision: 1.5 $') =~ s/.*?(\d.*\d).*/$zero version $1\n/; +my (%opt); + +# Collect the open elections +my (@open_elections, $usage_elections); +opendir(D, "$Votify::datadir/") or die; +@open_elections = sort grep { + s/^start-// and do { + my ($starttime) = (stat _)[9] if stat("$Votify::datadir/start-$_"); + my ($stoptime) = (stat _)[9] if stat("$Votify::datadir/stop-$_"); + ((not defined $starttime or $starttime < time) and + (not defined $stoptime or $stoptime > time)) + } +} readdir D; +closedir D; +if (@open_elections) { + $usage_elections = join("\n ", @open_elections); +} else { + $usage_elections = "(no elections currently open)"; +} + +my $usage = <<EOT; + +usage: $zero <command> <election> + +where <command> is one of: + + --new Generate a new ~/.ballot-<election> for editing + --verify Verify content of ~/.ballot-<election> + --submit Submit ~/.ballot-<election> to be counted + --help Show this help message + --version Show version information + +and <election> is one of the elections currently in-progress. The following +elections are currently open: + + $usage_elections + +Instructions: + +(1) Create a new ballot with the --new command. This generates a file in your + home directory called ~/.ballot-<election>. The file contains a shuffled + listing of the candidates. Note that the permissions on the file are set so + that only you have read+write permissions. + + \$ $zero --new <election> + +(2) Edit ~/.ballot-<election> and rearrange the candidates linewise in order of + preference. Candidates you consider equal can be listed on the same line. + Any candidates you omit are implied on the last line. For example, if tom, + jerry, sam, linford and karin are running, you could put: + + karin + linford jerry + + In this case, you prefer karin over everybody else. You prefer linford and + jerry over tom and sam. + + The file format is case-insensitive, ignores empty lines and comments that + start with the hash '#' symbol. + +(3) Verify your choices. The $zero program will explain in English if it + appears that you've made any errors in your ballot. + + \$ $zero --verify <election> + +(4) Submit your ballot. This renames your ballot to + ~/.ballot-<election>-submitted so that it will be tallied when the votes + are collected. + + \$ $zero --submit <election> + +EOT + +###################################################################### +# Main +###################################################################### + +package main; + +# Make sure umask is secure before we do anything +umask 077; + +# Parse the options on the cmdline. Put the short versions first in +# each optionstring so that the hash keys are created using the short +# versions. For example, use 'q|qar', not 'qar|q'. +my ($result) = GetOptions( + \%opt, + 'new', # generate new ~/.ballot-<election> + 'verify', # verify content of ~/.ballot-<election> + 'submit', # rename ~/.ballot to ~/.ballot-<election>-submitted + 'help', # help message + 'version', # version information +); +if ($opt{'help'} or not %opt) { print STDERR $usage; exit 0 } +if ($opt{'version'}) { print STDERR $version; exit 0 } +die "$zero: only one command allowed; use --help for help\n" if 1 < keys %opt; +die "$zero: election required; use --help for help\n" unless @ARGV == 1; + +my ($election) = $ARGV[0]; +my ($b) = Ballot->new($election); + +# Check if the election is open. This should really happen in an +# Election class in Votify.pm eventually +my ($starttime, $stoptime); +if (stat("$Votify::datadir/start-$election")) { $starttime = (stat _)[9] } +if (stat("$Votify::datadir/stop-$election")) { $stoptime = (stat _)[9] } +if ($starttime && $starttime > time) { + print "\n", "*" x 75, "\n"; + print "WARNING: Specified election doesn't start until ", + scalar(localtime $starttime), "\n"; + print "*" x 75, "\n"; +} +if ($stoptime && $stoptime < time) { + print "\n", "*" x 75, "\n"; + print "WARNING: Specified election ended at ", + scalar(localtime $stoptime), "\n"; + print "*" x 75, "\n"; +} + +if ($opt{'new'}) { + # Make sure the user is eligible + open(F, "<$Votify::datadir/voters-$election") or die "Failed to open voters file"; + my (@voters) = <F>; + chomp(@voters); + close(F); + + unless (grep { $_ eq getpwuid($>) } @voters) { + print STDERR <<EOT; + +I'm sorry, you are not a registered voter for this election. Please +contact one of the election officials if you feel this is in error, +either on IRC or in email. + +EOT + exit 1; + } + + $b->populate(); + $b->write(); + + # Let the user know what we did + print <<EOF; + +Welcome to the $election election! Your ballot has been written to + + $b->{filename} + +Please edit this file with your preferred editor. Additional instructions can +be read at the top of the ballot. + +EOF + exit 0; +} + +if ($opt{'verify'} or $opt{'submit'}) { + $b->read(); + $b->verify(); + if ($opt{'verify'}) { + print <<EOF; +Your ballot checks out ok, congratulations! Your next step should be + + $zero --submit $election + +EOF + exit 0; + } + + my ($s) = $b->{'filename'}.'-submitted'; + die("File already exists, please remove $s") if -e $s; + rename($b->{'default_filename'}, $s) or die("Couldn't rename $b->{default_filename}"); + print <<EOF; +Your ballot has been renamed to $s +so that it will be counted when the election is over. Please do not remove your +ballot until the election results have been announced. Thank you for +participating in the $election election! + +EOF + exit 0; +} + +__END__ + +$Log: votify,v $ +Revision 1.5 2005/05/16 04:03:46 agriffis +add first pass at countify --rank + +Revision 1.3 2005/05/09 23:12:02 agriffis +Add support for registered voters + +Revision 1.2 2005/05/05 23:03:46 agriffis +Fix indentation (and some output as well) + +Revision 1.1 2005/05/05 22:05:34 agriffis +first pass at Gentoo Foundation voting program + +# vim:sw=4 et |