/* * booth: the voting booth for a STV vote * * very dumb and quick */ #include #include #include #include #include #include #include char line[1000]; char from[200]; #define RANKINGS 100 char *votes[RANKINGS]; char * TRIM(char *s) { char *p; while (isspace(*s) && *s) ++s; for (p=s; *p; ++p) ; while (p>s && isspace(*p)) --p; *p = 0; return s; } /* TRIM */ main() { FILE *mail; FILE *audit; FILE *spool; char *ptr; enum {BEGIN, FROM, VOTE, END} state = BEGIN; int i; for (i=0; i') ++ptr; if (state == BEGIN && strncmp("From:", line, 5) == 0) { strncpy(from, TRIM(line+5), 80); state = FROM; } else if (state == FROM && strcmp("--vote--", ptr) == 0) { state = VOTE; } else if (state == VOTE && strcmp("--done--", ptr) == 0) { state = END; } if (state == VOTE) { /* vote format: [.*] what */ char *p; int thisrank; if (ptr[0] != '[') continue; /* invalid line */ thisrank = atoi(1+ptr); if (thisrank < 1 || thisrank > RANKINGS) continue; if (votes[thisrank-1]) exit(1); for (p=ptr; *p && *p != ']'; p++) ; if (*p) { p = strdup(TRIM(p+1)); for (i=0; i 0) strcat(line, "|"); strcat(line, votes[i]); j++; } if (audit = fopen("/usr/orc/vote/audit", "a")) { fprintf(audit, "[%s][%s]\n", from, line); fclose(audit); } data.dptr = line; data.dsize = strlen(line); dbminit("/usr/orc/vote/ballots"); i = delete(key); if (data.dsize > 0) store(key, data); dbmclose(); sprintf(line, "/usr/lib/sendmail '%s'", from); if (mail = popen(line, "w")) { int j; fprintf(mail, "To: %s\n", from); fprintf(mail, "From: vote@gehenna.apple.com (the voting machine)\n" "Subject: Your vote\n" "\n" "Your vote has been received and is saved as:\n\n"); if (data.dsize == 0) fprintf(mail,"\t>>please cancel my vote<<\n"); else { fprintf(mail, "\t--vote--\n"); for (j=1,i=0; i%s", line); pclose(mail); } } }