This repository has been archived on 2024-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
probotic/src/parse.c

143 lines
3.3 KiB
C
Raw Normal View History

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-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
#define PARAMS_COUNT 2
enum cred_param_ids_e
{
USERNAME,
PASSWORD
};
/* TODO: move = to the handler */
char const * cred_param_names_g[] = {
"username=",
"password="
};
2023-08-02 16:38:32 -04:00
DELC void
parse_command(char * cmd)
{
size_t i = 0;
/* size_t len = strlen(cmd); */
while (cmd[i] &&
cmd[i] != ' ')
{ ++i; }
if (cmd[i] == '\0')
{
/* no arguments */
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)
{ ircmsg("%s: All projects"); }
else if (strcmp(cmd, "reroll") == 0)
{ ircmsg("%s: No more rerolls possible.", current_username); }
}
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); }
}
}
DELC int
2023-08-02 10:44:10 -04:00
parse_creds(creds_t * creds,
char const * creds_file)
{
2023-08-02 11:09:34 -04:00
FILE * stream;
size_t nread = 0;
creds->username = NULL;
creds->password = NULL;
stream = fopen(creds_file, "r");
if (stream == NULL)
{ PERROR(PROGN); }
char * values[PARAMS_COUNT];
memset(values, 0, sizeof(char *) * PARAMS_COUNT);
char * line = NULL;
while (getline(&line, &nread, stream) > 0)
2023-08-02 11:09:34 -04:00
{
for (size_t i = 0; i < PARAMS_COUNT; i++)
{
size_t param_len = strlen(cred_param_names_g[i]);
if (!strncmp(cred_param_names_g[i], line, param_len))
{
/* Starts with the current parameter specifier */
char const * val = line + param_len;
size_t val_len = strlen(val);
/* Duplicates and gets rid of a newline */
values[i] = strndup(val, val[val_len - 1] == '\n' ? val_len - 1 : val_len);
break;
}
}
free(line);
line = NULL;
nread = 0;
2023-08-02 11:09:34 -04:00
}
creds->username = values[USERNAME];
creds->password = values[PASSWORD];
2023-08-02 11:09:34 -04:00
/* Check empty but required paramters */
if (!creds->username)
2023-08-02 11:09:34 -04:00
{
ERRMSG("Could not parse bot login");
goto fail;
2023-08-02 11:09:34 -04:00
}
fclose(stream);
return 0;
/* Releasing everything in cause of a failure */
2023-08-02 10:44:10 -04:00
fail:
2023-08-02 11:09:34 -04:00
fclose(stream);
clean_creds(creds);
return 1;
2023-08-02 10:44:10 -04:00
}
void
clean_creds(creds_t * creds)
{
2023-08-02 11:09:34 -04:00
/* Should we memset these? */
free(creds->username);
creds->username = NULL;
2023-08-02 10:44:10 -04:00
2023-08-02 11:09:34 -04:00
free(creds->password);
creds->password = NULL;
}