2011-02-23 17:29:34 -07:00
|
|
|
#include <stdio.h>
|
2010-09-11 22:57:46 -06:00
|
|
|
#include <stdlib.h>
|
2010-09-16 12:21:16 -06:00
|
|
|
#include <ctype.h>
|
2010-09-11 22:57:46 -06:00
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
int
|
2010-09-13 12:23:39 -06:00
|
|
|
main(int argc, char *argv[])
|
2010-09-11 22:57:46 -06:00
|
|
|
{
|
2010-09-14 18:04:33 -06:00
|
|
|
char team[TEAM_MAX] = {0};
|
|
|
|
char category[CAT_MAX] = {0};
|
|
|
|
char points_str[11] = {0};
|
|
|
|
char answer[500] = {0};
|
|
|
|
long points = 0;
|
2010-09-13 12:23:39 -06:00
|
|
|
|
|
|
|
if (-1 == cgi_init(argv)) {
|
2010-09-11 22:57:46 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read in team and answer */
|
|
|
|
while (1) {
|
|
|
|
size_t len;
|
|
|
|
char key[20];
|
|
|
|
|
|
|
|
len = cgi_item(key, sizeof(key));
|
|
|
|
if (0 == len) break;
|
|
|
|
switch (key[0]) {
|
|
|
|
case 't':
|
|
|
|
cgi_item(team, sizeof(team));
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
cgi_item(category, sizeof(category));
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
cgi_item(points_str, sizeof(points_str));
|
2010-09-12 22:00:58 -06:00
|
|
|
points = atol(points_str);
|
2010-09-11 22:57:46 -06:00
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
cgi_item(answer, sizeof(answer));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check to see if team exists */
|
|
|
|
if (! team_exists(team)) {
|
|
|
|
cgi_page("No such team", "");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Validate category name (prevent directory traversal) */
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
for (p = category; *p; p += 1) {
|
2010-10-22 11:15:37 -06:00
|
|
|
if ((! isalnum(*p)) && ('-' != *p)) {
|
2010-09-11 22:57:46 -06:00
|
|
|
cgi_page("Invalid category", "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check answer (also assures category exists) */
|
|
|
|
{
|
2010-09-12 22:00:58 -06:00
|
|
|
char needle[400];
|
2010-09-11 22:57:46 -06:00
|
|
|
|
2010-09-23 18:23:00 -06:00
|
|
|
my_snprintf(needle, sizeof(needle), "%ld %s", points, answer);
|
2011-02-23 17:29:34 -07:00
|
|
|
{
|
|
|
|
FILE *f = fopen("/tmp/form", "w");
|
|
|
|
fprintf(f, "%s\n%s\n", answer, needle);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
2010-09-13 12:23:39 -06:00
|
|
|
if (! fgrepx(needle,
|
2010-09-23 18:23:00 -06:00
|
|
|
package_path("%s/answers.txt", category))) {
|
2010-09-11 22:57:46 -06:00
|
|
|
cgi_page("Wrong answer", "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-14 18:04:33 -06:00
|
|
|
{
|
2010-09-16 23:18:16 -06:00
|
|
|
char line[200];
|
2010-09-14 18:04:33 -06:00
|
|
|
|
|
|
|
my_snprintf(line, sizeof(line),
|
|
|
|
"%s %s %ld", team, category, points);
|
|
|
|
award_and_log_uniquely(team, category, points,
|
2010-09-24 17:24:43 -06:00
|
|
|
state_path("puzzles.db"), line);
|
2010-09-14 18:04:33 -06:00
|
|
|
}
|
2010-09-11 22:57:46 -06:00
|
|
|
|
|
|
|
cgi_page("Points awarded",
|
2010-09-24 22:28:40 -06:00
|
|
|
("<p>%d points for %s.</p>\n"
|
|
|
|
"<p><a href=\"/puzzles.html\">Back to puzzles</a></p>\n"
|
2010-09-13 12:23:39 -06:00
|
|
|
"<!-- awarded %d -->"),
|
|
|
|
points, team, points);
|
2010-09-11 22:57:46 -06:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|