diff options
Diffstat (limited to 'service/src/main.c')
| -rw-r--r-- | service/src/main.c | 60 |
1 files changed, 46 insertions, 14 deletions
diff --git a/service/src/main.c b/service/src/main.c index 4b28637..5d99fc1 100644 --- a/service/src/main.c +++ b/service/src/main.c @@ -128,7 +128,7 @@ handle_download(const char *scandir) fseek(f, 0, SEEK_END); size = ftell(f); fseek(f, 0, SEEK_SET); - if (size > MAXFILESIZE) { + if (size >= MAXFILESIZE) { ERR("File is too large!\n"); goto fail; } @@ -150,6 +150,31 @@ fail: } void +motd() +{ + char linebuf[80]; + int msgc, msgi, i; + FILE *f; + + if (!(f = fopen("msgs/motd", "r"))) return; + + if (!fgets(linebuf, sizeof(linebuf), f)) + goto exit; + + srand(time(NULL)); + if ((msgc = atoi(linebuf))) { + msgi = rand() % msgc; + for (i = 0; i < msgi + 1; i++) + if (!fgets(linebuf, sizeof(linebuf), f)) + return; + printf("%s\n", linebuf); + } + +exit: + fclose(f); +} + +void cat_cmd(const char *arg) { if (arg && !strncmp(arg, "flag", 4)) @@ -193,34 +218,41 @@ echo_cmd(const char *arg) void upload_cmd(const char *arg) { + char *end, *contents, *modelname; const char *resp; - char *end, *contents; size_t len; + modelname = checkp(strdup(ask("Enter a model name: "))); + if (!strlen(modelname)) { + ERR("Empty model names are not allowed"); + goto exit; + } + resp = ask("How large is your file? "); len = strtoul(resp, &end, 10); if (len <= 0 || len >= MAXFILESIZE || *end) { ERR("Invalid file length!\n"); - return; + goto exit; } printf("Ok! Im listening..\n"); contents = checkp(malloc(len + 1)); if (fread(contents, 1, len, stdin) != len) { ERR("Not enough data received!\n"); - goto cleanup; + goto exit; } contents[len] = '\0'; - if ((cached.valid = parse_file(&cached, contents, len))) { + if ((cached.valid = parse_file(&cached, contents, len, &modelname))) { if (save_submission(&cached, contents, len) != OK) ERR("Failed to save your submission!\n"); else printf("Your file was saved with ID %s!\n", cached.hash); } -cleanup: +exit: free(contents); + free(modelname); } void @@ -264,7 +296,7 @@ search_cmd(const char *arg) if (pathc == 0) { ERR("Couldn't find a matching scan result!\n"); - goto cleanup; + goto exit; } while (1) { @@ -273,15 +305,15 @@ search_cmd(const char *arg) which = strtoul(resp, &end, 10); if (which >= pathc || which < 0 || *end) { ERR("Invalid index!\n"); - goto cleanup; + goto exit; } scandir = aprintf("%s/%s", resultdir, paths[which]); - if (handle_download(scandir) != OK) goto cleanup; + if (handle_download(scandir) != OK) goto exit; FREE(scandir); } -cleanup: +exit: FREE(scandir); for (i = 0; i < pathc; i++) free(paths[i]); @@ -355,7 +387,6 @@ auth_cmd(const char *arg) void cleanexit() { - printf("see you later!\n"); free_info(&cached); free(resultdir); } @@ -364,8 +395,8 @@ int main() { const char *cmd, *envstr; - char *cp, *arg; int exit, i, cmdlen; + char *cp, *arg; if (!(envstr = getenv("RESULTDIR"))) die("RESULTDIR not defined\n"); @@ -378,12 +409,13 @@ main() atexit(cleanexit); - dump("msgs/welcome"); + dump("msgs/banner"); + motd(); exit = 0; while (!exit) { errno = 0; - cmd = ask("$ "); + cmd = ask("\r$ "); if (!*cmd && errno == EBADMSG) break; if (!*cmd) continue; |
