2023-08-02 11:58:18 -04:00
|
|
|
/* parse.c
|
|
|
|
|
|
|
|
Probotic is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License version 3 only as
|
|
|
|
published by the Free Software Foundation.
|
|
|
|
|
|
|
|
Probotic is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License version 3 for more details.
|
|
|
|
|
|
|
|
The above copyright notice, this permission notice and the word
|
|
|
|
"NIGGER" shall be included in all copies or substantial portions
|
|
|
|
of the Software.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
version 3 + NIGGER along with Probotic.
|
|
|
|
|
|
|
|
*/
|
2023-08-02 10:44:10 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2023-08-02 11:48:30 -04:00
|
|
|
#include <string.h>
|
2023-08-02 10:44:10 -04:00
|
|
|
|
2023-08-03 22:36:49 -04:00
|
|
|
#include "cred_data.h"
|
2023-08-02 11:48:30 -04:00
|
|
|
#include "parse.h"
|
2023-08-02 15:45:42 -04:00
|
|
|
#include "error.h"
|
2023-08-02 11:48:30 -04:00
|
|
|
|
2023-08-03 05:15:20 -04:00
|
|
|
#define ARRAY_SIZE(x) ((size_t) (sizeof x) / (size_t) (sizeof *x))
|
|
|
|
|
2023-08-03 02:16:39 -04:00
|
|
|
creds_t creds = {0};
|
|
|
|
|
2023-08-03 05:15:20 -04:00
|
|
|
DECL char *
|
|
|
|
slurp(const char * fn)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
char * b;
|
|
|
|
FILE * fp = fopen(fn, "r");
|
|
|
|
if (fp)
|
|
|
|
{
|
|
|
|
fseek(fp, 0, SEEK_END);
|
|
|
|
len = ftell(fp);
|
|
|
|
rewind(fp);
|
2023-08-03 22:36:49 -04:00
|
|
|
b = malloc(len+2);
|
2023-08-03 05:15:20 -04:00
|
|
|
if (b)
|
|
|
|
{ fread(b, 1, len, fp); }
|
|
|
|
fclose(fp);
|
2023-08-03 22:36:49 -04:00
|
|
|
b[len+1] = '\0';
|
|
|
|
|
2023-08-03 05:15:20 -04:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ return NULL; }
|
|
|
|
}
|
|
|
|
|
2023-08-02 23:24:30 -04:00
|
|
|
DECL void
|
2023-08-02 16:38:32 -04:00
|
|
|
parse_command(char * cmd)
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
/* size_t len = strlen(cmd); */
|
2023-08-03 05:15:20 -04:00
|
|
|
/* TODO does not handle commands with leading space,
|
|
|
|
use custom implemented to-spec isspace implementation */
|
|
|
|
while (cmd[i] != '\0' &&
|
2023-08-02 16:38:32 -04:00
|
|
|
cmd[i] != ' ')
|
|
|
|
{ ++i; }
|
|
|
|
if (cmd[i] == '\0')
|
|
|
|
{
|
|
|
|
/* no arguments */
|
2023-08-03 22:36:49 -04:00
|
|
|
if (strcmp(cmd, "kill") == 0)
|
|
|
|
{ exit(1); }
|
2023-08-02 16:38:32 -04:00
|
|
|
if (strcmp(cmd, "remind") == 0)
|
|
|
|
{ ircmsg("%s: No current assignment", current_username); }
|
|
|
|
else if (strcmp(cmd, "next") == 0)
|
|
|
|
{ ircmsg("%s: No future assignments", current_username); }
|
|
|
|
else if (strcmp(cmd, "dump") == 0)
|
2023-08-03 02:16:39 -04:00
|
|
|
{ ircmsg("%s: All projects", current_username); }
|
2023-08-02 16:38:32 -04:00
|
|
|
else if (strcmp(cmd, "reroll") == 0)
|
2023-08-03 02:16:39 -04:00
|
|
|
{ ircmsg("%s: No more rerolls possible", current_username); }
|
2023-08-02 16:38:32 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* some arguments */
|
|
|
|
char * arg = cmd + i + 1;
|
|
|
|
cmd[i] = '\0';
|
|
|
|
if (strcmp(cmd, "raw") == 0)
|
|
|
|
{ ircmsg("%s: Executing SQL `%s'", arg); }
|
|
|
|
else if (strcmp(cmd, "submit") == 0)
|
|
|
|
{ ircmsg("%s: Submitting project link '%s' to <random janny>",
|
|
|
|
current_username, arg); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-02 23:24:30 -04:00
|
|
|
DECL int
|
2023-08-03 22:36:49 -04:00
|
|
|
parse_pair(char const * buf, size_t len)
|
2023-08-03 05:15:20 -04:00
|
|
|
{
|
|
|
|
size_t i, f, x;
|
2023-08-03 22:36:49 -04:00
|
|
|
/* fprintf(stderr, "ENT len:%ld buf:%sEOF\n", len, buf); */
|
|
|
|
for (i = 0; buf[i] &&
|
|
|
|
i < len; ++i)
|
2023-08-03 05:15:20 -04:00
|
|
|
{
|
|
|
|
if (buf[i] == '=')
|
|
|
|
{
|
2023-08-03 22:36:49 -04:00
|
|
|
++i;
|
|
|
|
for (f = 0, x = 0; f < PARAMS_COUNT; ++f)
|
2023-08-03 05:15:20 -04:00
|
|
|
{
|
2023-08-03 22:36:49 -04:00
|
|
|
/* fprintf(stderr, "x%ld, i%ld, %s\n", x, i, buf); */
|
|
|
|
/* X macro for handling this data may be better */
|
|
|
|
if (strncmp(buf, cred_names[f], cred_names_len[f]) == 0)
|
2023-08-03 05:15:20 -04:00
|
|
|
{
|
2023-08-03 22:36:49 -04:00
|
|
|
/* fprintf(stderr, "f%ld:len%ld:%s\n", f, cred_names_len[f], */
|
|
|
|
/* cred_names[f]); fflush(stderr); */
|
2023-08-03 05:29:11 -04:00
|
|
|
buf += i;
|
2023-08-03 22:36:49 -04:00
|
|
|
while (buf[x] != '\0')
|
|
|
|
{
|
|
|
|
if (buf[x] == '\n')
|
|
|
|
{
|
|
|
|
len -= i; i = 0; break;
|
|
|
|
}
|
|
|
|
++x;
|
|
|
|
}
|
|
|
|
switch (f)
|
2023-08-03 05:15:20 -04:00
|
|
|
{
|
2023-08-03 22:36:49 -04:00
|
|
|
case USERNAME: creds.username = strndup(buf,x); break;
|
|
|
|
case PASSWORD: creds.password = strndup(buf,x); break;
|
|
|
|
case CHANNEL: creds.channel = strndup(buf,x); break;
|
|
|
|
case SERVER: creds.server = strndup(buf,x); break;
|
|
|
|
case PORT: creds.port = atoi(buf); break;
|
2023-08-03 05:15:20 -04:00
|
|
|
}
|
2023-08-03 22:36:49 -04:00
|
|
|
if (x + 2 < len)
|
|
|
|
{ buf += x + 1; }
|
|
|
|
else
|
|
|
|
{ return 1; }
|
|
|
|
goto next;
|
2023-08-03 05:15:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-03 22:36:49 -04:00
|
|
|
next:;
|
2023-08-03 05:15:20 -04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-08-03 22:36:49 -04:00
|
|
|
#define FULL_FREE(obj) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
if ((obj)) \
|
|
|
|
{ \
|
|
|
|
memset((obj), '\0', strlen((obj))); \
|
|
|
|
free(obj); \
|
|
|
|
(obj) = NULL; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2023-08-03 05:15:20 -04:00
|
|
|
|
2023-08-03 22:36:49 -04:00
|
|
|
void
|
|
|
|
creds_free_password(void)
|
2023-08-02 10:44:10 -04:00
|
|
|
{
|
2023-08-03 22:36:49 -04:00
|
|
|
FULL_FREE(creds.password);
|
2023-08-02 10:44:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-08-03 22:36:49 -04:00
|
|
|
creds_free_rest(void)
|
2023-08-02 10:44:10 -04:00
|
|
|
{
|
2023-08-03 02:16:39 -04:00
|
|
|
FULL_FREE(creds.username);
|
2023-08-03 22:36:49 -04:00
|
|
|
/* FULL_FREE(creds.password); */
|
2023-08-03 02:16:39 -04:00
|
|
|
FULL_FREE(creds.channel);
|
|
|
|
FULL_FREE(creds.server);
|
2023-08-02 11:09:34 -04:00
|
|
|
}
|
2023-08-03 22:36:49 -04:00
|
|
|
#undef FULL_FREE
|