/* irc.c - IRC interface 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. */ #include #include #include #include #include "irc.h" #include "parse.h" #include "api.h" #include "error.h" #define PREFIX_COMMAND_CHAR '!' irc_session_t * session; irc_callbacks_t callbacks; char * current_username; #define IRCMSG(msg) irc_cmd_msg(session, creds.channel, msg) 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); strncpy(r, origin, i); r[i] = '\00'; return r; } DECL void ircmsg(const char* fmt, ...) { va_list args; char * fmtdmsg; va_start(args, fmt); if(vasprintf(&fmtdmsg, fmt, args) == -1) { exit(1); } puts(fmtdmsg); IRCMSG(fmtdmsg); free(fmtdmsg); va_end(args); } DECL void event_connect(irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count) { (void) event; (void) origin; (void) params; (void) count; /* msg ChanServ IDENTIFY? */ irc_cmd_join(session, creds.channel, 0); } DECL void event_channel(irc_session_t * session, char const * event, char const * origin, char const ** params, unsigned int count) { /* char const * channel = params[0]; */ char const * message = params[1]; (void) session; (void) event; (void) origin; /* (void) channel; */ (void) message; (void) count; current_username = get_username(origin); /* parses the command */ if (*message == PREFIX_COMMAND_CHAR) { char * pmsg = strdup(++message); if (pmsg) { parse_command(pmsg); free(pmsg); } } free(current_username); } DECL int init(void) { if(api_init()) { ERR(DB_ERROR, "Error initializing database."); } memset(&callbacks, 0, sizeof(callbacks)); callbacks.event_connect = event_connect; callbacks.event_channel = event_channel; session = irc_create_session(&callbacks); if (!session) { ERRMSG("Error creating IRC session"); goto fail; } assert(creds.username != NULL); assert(creds.server != NULL); irc_connect(session, creds.server, creds.port, creds.password, creds.username, creds.username, creds.username); creds_free_password(); atexit(rope); return 0; fail: creds_free_password(); return 1; } DECL int loop(void) { /* We should figure out how the failure happens so we can tell the user that. */ if (irc_run(session) != 0) { ERR(1, "Error running IRC session\nPossible issue: bad URL," " no network connection, bad port, refused connection."); } return 0; }