moth/packages/mcp/src/claim.cgi.c

93 lines
1.9 KiB
C
Raw Normal View History

#include <stdlib.h>
2010-09-16 12:21:16 -06:00
#include <ctype.h>
#include "common.h"
int
main(int argc, char *argv[])
{
2011-03-14 17:13:25 -06:00
char team[TEAM_MAX] = {0};
char token[TOKEN_MAX] = {0};
if (-1 == cgi_init(argv)) {
return 0;
}
/* Read in team and token */
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 'k':
cgi_item(token, sizeof(token));
break;
}
}
if (! team_exists(team)) {
2011-03-14 17:13:25 -06:00
cgi_result(409, "No such team", "<p>There is no team with that hash.</p>");
}
/* Any weird characters in token name? */
{
char *p;
2010-10-22 17:09:00 -06:00
if ('\0' == token[0]) {
2011-03-14 17:13:25 -06:00
cgi_result(409, "Must supply token", "<p>Your request did not contain a k= parameter.</p>");
2010-10-22 17:09:00 -06:00
}
for (p = token; *p; p += 1) {
if ((! isalnum(*p)) &&
(*p != '-') &&
(*p != ':')) {
2011-03-14 17:13:25 -06:00
cgi_result(409, "Not a token", "<p>This token has untokenlike characteristics.</p>");
}
}
}
/* Does the token exist? */
2010-09-23 18:23:00 -06:00
if (! fgrepx(token, state_path("tokens.db"))) {
2011-03-14 17:13:25 -06:00
cgi_result(409, "No such token", "<p>This token has not been issued.</p>");
}
/* Award points */
{
2011-03-14 17:13:25 -06:00
char *p = token;
char *q;
char category[40];
char points_s[40];
int points;
/* Pull category name out of the token */
2011-03-14 17:13:25 -06:00
for (q = category; *p && (*p != ':'); p += 1) {
*(q++) = *p;
}
2011-03-14 17:13:25 -06:00
*q = '\0';
if (p) p += 1;
/* Pull point value out of the token (if it has one) */
for (q = points_s; *p && (*p != ':'); p += 1) {
*(q++) = *p;
}
*q = '\0';
points = atoi(points_s);
if (0 == points) points = 1;
2010-09-14 18:04:33 -06:00
{
char line[200];
2010-09-14 18:04:33 -06:00
2011-03-14 17:13:25 -06:00
my_snprintf(line, sizeof(line), "%s %s", team, token);
award_and_log_uniquely(team, category, points, state_path("claim.db"), line);
2010-09-14 18:04:33 -06:00
}
}
2011-03-14 17:13:25 -06:00
cgi_page("Point awarded", "<p>Congratulations.</p>");
return 0;
}