|
- /* irc.c - IRC interface */
-
- #define PREFIX_COMMAND_CHAR '!'
-
- #define IRCMSG(msg) irc_cmd_msg(session, creds.channel, msg)
-
- typedef struct
- {
- char * username;
- char * password;
- char * channel;
- char * server;
- int port;
- } creds_t;
-
- VARDECL creds_t creds =
- {
- .username = NULL,
- .password = NULL,
- .channel = NULL,
- .server = NULL, /* localhost? */
- #ifndef IRC_SSL_SUPPORT
- .port = 6667
- #else
- .port = 6669
- #endif /* !IRC_SSL_SUPPORT */
- };
-
- VARDECL char * ident_password = NULL;
-
- VARDECL irc_session_t * session;
- VARDECL irc_callbacks_t callbacks;
-
- VARDECL char * current_username = NULL;
-
- VARDECL char const * help_msg =
- /* IRC_GREEN "!help " IRC_STOP " : This message\n" */
- IRC_GREEN "!remind " IRC_STOP " : Dump current assignment\n"
- IRC_GREEN "!reroll " IRC_STOP " : Rerolls assignment\n"
- /* IRC_GREEN "!repo <link>" IRC_STOP " : Sets project repository link\n" */
- /* IRC_GREEN "!raw <sql> " IRC_STOP " : Execute raw SQL\n" */
- /* IRC_GREEN "!dump " IRC_STOP " : List all possible projects\n" */
- IRC_GREEN "!request " IRC_STOP " : Request personal project\n"
- IRC_GREEN "!remind " IRC_STOP " : Prints your assignment\n";
-
- DECL void parse_command(char const * cmd);
-
- DECL char *
- get_username(const char * origin)
- {
- const char USERNAME_TERMINATOR = '!';
- int i = 0;
- char * r;
- while (origin[i] != USERNAME_TERMINATOR)
- { i++; }
- r = (char *) malloc(i + 1);
- if (r)
- {
- strncpy(r, origin, i);
- r[i] = '\00';
- }
- return r;
- }
-
- DECL void
- ircmsg(char const * reciever, char const * fmt,
- ...)
- {
- va_list args;
- char * fmtdmsg;
- char * swp;
- const char * delim = "\n";
- char * data;
- if(!strcmp(fmt, "") || fmt == NULL)
- { return; }
-
- va_start(args, fmt);
- if(vasprintf(&fmtdmsg, fmt, args) == -1)
- { exit(1); }
-
- puts(fmtdmsg);
- data = strtok(fmtdmsg, delim);
- do
- {
- swp = irc_color_convert_to_mirc(data);
- irc_cmd_msg(session, reciever, swp);
- free(swp);
- } while((data = strtok(NULL, delim), data));
-
- free(fmtdmsg);
- va_end(args);
- }
-
-
-
- DECL void
- event_connect(irc_session_t * lsession,
- char const * event,
- char const * origin,
- char const ** params,
- unsigned int count)
- {
- (void) event;
- (void) origin;
- (void) params;
- (void) count;
- /* msg ChanServ IDENTIFY? */
- irc_cmd_join(lsession, creds.channel, 0);
- if (ident_password)
- {
- ircmsg("NickServ", "IDENTIFY %s", ident_password);
- memset(ident_password, '\0', strlen(ident_password));
- }
-
- #ifdef INITIAL_ASSIGNMENT_MESSAGE
- if(is_no_assignment(creds.channel))
- {
- ircmsg(creds.channel, IRC_RED "No assignment for this channel. Finding a new..." IRC_STOP);
- random_assign(creds.channel);
- }
- ircmsg(creds.channel, remind(creds.channel));
- #endif /* INITIAL_ASSIGNMENT_MESSAGE */
- }
-
- DECL void
- event_channel(irc_session_t * lsession,
- char const * event,
- char const * origin,
- char const ** params,
- unsigned int count)
- {
- /* char const * channel = params[0]; */
- char const * message = params[1];
- (void) lsession;
- (void) event;
- (void) origin;
- /* (void) channel; */
- (void) message;
- (void) count;
- /* Logs the message */
- printf(message);
- /* parses the command */
- if (*message == PREFIX_COMMAND_CHAR)
- { current_username = get_username(origin); }
- if (!current_username ||
- message[1] == '\0')
- { return; }
- parse_command(message+1);
- free(current_username);
- current_username = NULL;
- }
-
- /* 'abc' SINGLE
- 'def ' SINGLE
- 'ghi jkl' MULTI */
-
- DECL int
- has_arg(char const * cmd)
- {
- char const * start = cmd;
- while (isalnum(*cmd))
- {
- if (*cmd == '\0')
- { break; }
- ++cmd;
- }
- while (*cmd != '\0')
- {
- if (!isspace(*cmd))
- { return cmd - start; }
- ++cmd;
- }
- return 0;
- }
-
- DECL void
- parse_command(char const * cmd)
- {
- size_t i = 0;
- char* msgswp = NULL;
- /* size_t len = strlen(cmd); */
- printf("Handling '%s'\n", cmd);
- if (!(i = has_arg(cmd)))
- {
- /* NO ARGUMENTS */
- if (strcmp(cmd, "remind") == 0)
- {
- msgswp = remind(current_username);
- ircmsg(creds.channel, "%s: %s", current_username, msgswp);
- }
- else if (strcmp(cmd, "help") == 0)
- { ircmsg(creds.channel, help_msg); }
- else if (strcmp(cmd, "magic") == 0)
- { ircmsg(creds.channel, "%s: " IRC_YELLOW "%d" IRC_STOP, current_username, (rand() % 100) + 1); }
- #ifndef NO_VULN_COMMANDS
- else if (strcmp(cmd, "dump") == 0)
- {
- ircmsg(creds.channel, "%s: All projects:", current_username);
- msgswp = dump();
- ircmsg(creds.channel, msgswp);
- }
- #endif /* !NO_VULN_COMMANDS */
- else if (strcmp(cmd, "reroll") == 0)
- {
- purge_assignments(current_username);
- random_assign(current_username);
- ircmsg(creds.channel, "%s: %s", current_username, remind(current_username));
- }
- }
- else /* HAS ARGUMENTS */
- {
- char const * const arg = cmd + i;
- printf("argoff: %p; i: %ld; arg: %sEOA\n", cmd + i + 1, i, arg);
- #ifndef NO_VULN_COMMANDS
- if (strncmp(cmd, "raw", 3) == 0)
- {
- printf("RAW\n");
- /* ircmsg(creds.channel, "%s: Executing SQL `%s'.", current_username, arg); */
- msgswp = raw(arg);
- ircmsg(creds.channel, msgswp);
- } else
- #endif /* !NO_VULN_COMMANDS */
- if (strncmp(cmd, "repo", 4) == 0)
- {
- /* ircmsg(creds.channel, "%s: Setting project repository...", current_username); */
- set_repo(creds.channel, arg);
- msgswp = remind(creds.channel);
- ircmsg(creds.channel, "%s: %s", current_username, msgswp);
- }
- else if (strncmp(cmd, "magic", 5) == 0)
- { ircmsg(creds.channel, "%s: " IRC_YELLOW "%d" IRC_STOP, current_username, (rand() % atoi(arg)) + 1); }
- else if (strncmp(cmd, "say", 3) == 0)
- { ircmsg(creds.channel, "%s", arg); }
- }
- free(msgswp);
- }
|