aboutsummaryrefslogtreecommitdiff
path: root/votify
blob: c34fed25b6a928f9f6f65f34a3ecaff66b69ef40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/perl -w
# $Id: votify,v 1.5 2005/05/16 04:03:46 agriffis Exp $
#
# Copyright 2005-2015 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
#
# votify: generate, verify and submit voting ballots for trustee elections
#

BEGIN {
    my $dirname;
    if(-f '/etc/elections/Votify.pm') {
        $dirname = '/etc/elections';
    } else {
        use Cwd qw(abs_path);
        use File::Basename qw(dirname);
        $dirname = dirname(abs_path(__FILE__));
    }
    push @INC, $dirname;
}

use POSIX;
use Getopt::Long;
use List::Util;
use Votify 'user';
use strict;

######################################################################
# Global vars
######################################################################

(my $zero = $0) =~ s,.*/,,;
(my $version = $Votify::version) =~ s/.*?(\d.*\d).*/$zero version $1\n/;
my (%opt);

# Collect the open elections
my (%open_elections, $usage_elections);
%open_elections = Votify::get_open_elections_hash();
if (scalar keys %open_elections) {
    $usage_elections = join("\n        ", keys %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];
$election =~ s/^\.ballot-//g; # bug 326751
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);
$starttime = $open_elections{$election}{'starttime'};
$stoptime = $open_elections{$election}{'stoptime'};
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, '<', $open_elections{$election}{'votersfile'}) 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__
# vim:sw=4 et