From 0e527221619eaa452973cb0770b3b789242f9e64 Mon Sep 17 00:00:00 2001 From: anon Date: Thu, 3 Aug 2023 18:52:49 +0200 Subject: [PATCH] parsing support for channel commands --- include/parse.h | 2 +- src/irc.c | 26 ++++++++++++++------------ src/parse.c | 11 +++++------ 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/include/parse.h b/include/parse.h index 006fe05..6afff8e 100644 --- a/include/parse.h +++ b/include/parse.h @@ -11,7 +11,7 @@ typedef struct extern creds_t creds; -DECL void parse_command(char * cmd); +DECL void parse_command(const char * const cmd); DECL int parse_creds(char const * creds_file); DECL void clean_creds(void); diff --git a/src/irc.c b/src/irc.c index 798be41..419aad5 100644 --- a/src/irc.c +++ b/src/irc.c @@ -33,11 +33,12 @@ #include "help.h" #define PREFIX_COMMAND_CHAR '!' +#define PREFIX_CHANNEL_COMMAND_CHAR '%' irc_session_t * session; irc_callbacks_t callbacks; -char * current_username; +char * current_username = NULL; int f = -1; #define IRCMSG(msg) irc_cmd_msg(session, creds.channel, msg) @@ -112,20 +113,21 @@ event_channel(irc_session_t * session, /* (void) channel; */ (void) message; (void) count; - current_username = get_username(origin); + /* parses the command */ + switch(*message){ + case PREFIX_CHANNEL_COMMAND_CHAR: + current_username = strdup(creds.channel); + break; + case PREFIX_COMMAND_CHAR: + current_username = get_username(origin); + break; + } + if(!current_username || *(message+1) == '\00'){ return; } --f; if(f == 0){ ircmsg(fmsg, current_username); } - /* parses the command */ - if (*message == PREFIX_COMMAND_CHAR) - { - char * pmsg = strdup(++message); - if (pmsg) - { - parse_command(pmsg); - free(pmsg); - } - } + parse_command(message+1); free(current_username); + current_username = NULL; } int diff --git a/src/parse.c b/src/parse.c index 19f8068..43a52a5 100644 --- a/src/parse.c +++ b/src/parse.c @@ -48,7 +48,7 @@ char const * cred_param_names_g[] = { }; DECL void -parse_command(char * cmd) +parse_command(const char * const cmd) { size_t i = 0; char* msgswp = NULL; @@ -92,15 +92,14 @@ parse_command(char * cmd) else { /* some arguments */ - char * arg = cmd + i + 1; - cmd[i] = '\0'; - if (strcmp(cmd, "raw") == 0) + const char * const arg = cmd + i + 1; + if (strncmp(cmd, "raw", i) == 0) { ircmsg("%s: Executing SQL `%s'.", current_username, arg); msgswp = raw(arg); ircmsg(msgswp); } - else if (strcmp(cmd, "set_repo") == 0) + else if (strncmp(cmd, "set_repo", i) == 0) { ircmsg("%s: Setting project repository...", current_username); set_repo(creds.channel, arg); @@ -108,7 +107,7 @@ parse_command(char * cmd) ircmsg("%s: %s", current_username, msgswp); } // XXX: what is this suppose to do? - else if (strcmp(cmd, "submit") == 0) // TODO: implement + else if (strncmp(cmd, "submit", i) == 0) // TODO: implement { ircmsg("%s: Submitting project link '%s' to ", current_username, arg);