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/irc.c

144 lines
3.7 KiB
C
Raw Normal View History

/* 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 <string.h>
#include <stdarg.h>
irc_session_t * session;
irc_callbacks_t callbacks;
const char * channel;
#define IRCMSG(msg) irc_cmd_msg(session, channel, msg)
DELC 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;
}
DELC void
msg_wrapper(const char* fmt,
...)
{
va_list args;
char * fmtdmsg;
va_start(args, fmt);
if(vasprintf(&fmtdmsg, fmt, args) == -1)
{ exit(1); }
puts(fmtdmsg);
irc_cmd_msg(session, channel, fmtdmsg);
free(fmtdmsg);
va_end(args);
}
DELC 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;
irc_cmd_join(session, CHANNEL, 0);
}
DELC void
event_channel(irc_session_t * session,
const char * event,
const char * origin,
const char ** params,
unsigned int count)
{
size_t i = 0, len;
const char * channel = params[0];
char * message = params[1];
char * arg;
char * swp;
(void) session;
(void) event;
(void) origin;
(void) channel;
(void) message;
(void) count;
swp = get_username(origin);
/* msg_wrapper("%s, you are a faggot for this opinion.", swp); */
/* parses the command */
len = strlen(message);
while (message[i] != ' ')
{ ++i; }
message[i] = '\0';
arg = message + i + 1;
if (*message == '!')
{
++message;
/* if (strcmp(message, "stop") == 0) { exit(1); } */
if (strcmp(message, "remind") == 0)
{ msg_wrapper("%s: No current assignment", swp); }
else if (strcmp(message, "next") == 0)
{ msg_wrapper("%s: No future assignments", swp); }
else if (strcmp(message, "raw") == 0)
{ msg_wrapper("%s: Executing SQL `%s'", arg); }
else if (strcmp(message, "dump") == 0)
{ msg_wrapper("%s: All projects"); }
else if (strcmp(message, "submit") == 0)
{ msg_wrapper("%s: Submitting project link '%s' to <random janny>", swp, arg); }
else if (strcmp(message, "reroll") == 0)
{ msg_wrapper("%s: No more rerolls possible.", swp); }
#ifdef NDEBUG
else
{ msg_wrapper("No such command '%s'", message); }
#endif /* NDEBUG */
}
free(swp);
}
DELC int
init(char const * username, char const * server, int port)
{
memset(&callbacks, 0, sizeof(callbacks));
callbacks.event_connect = event_connect;
callbacks.event_channel = event_channel;
session = irc_create_session(&callbacks);
if (!session)
{ ERR(1, "Error creating IRC session"); }
irc_connect(session, server, port, 0, username, username, username);
/* 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;
}