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

139 lines
3.3 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.
*/
2023-08-03 02:16:39 -04:00
#include <assert.h>
2023-08-02 15:27:59 -04:00
#include <stdio.h>
#include <stdarg.h>
2023-08-02 15:27:59 -04:00
#include <string.h>
#include "irc.h"
2023-08-02 16:38:32 -04:00
#include "parse.h"
2023-08-02 15:27:59 -04:00
#include "api.h"
#include "error.h"
2023-08-02 16:38:32 -04:00
#define PREFIX_COMMAND_CHAR '!'
irc_session_t * session;
irc_callbacks_t callbacks;
2023-08-02 16:38:32 -04:00
char * current_username;
2023-08-03 02:16:39 -04:00
#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
2023-08-02 16:38:32 -04:00
ircmsg(const char* fmt,
...)
{
va_list args;
char * fmtdmsg;
va_start(args, fmt);
if(vasprintf(&fmtdmsg, fmt, args) == -1)
{ exit(1); }
puts(fmtdmsg);
2023-08-03 02:16:39 -04:00
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;
2023-08-03 02:16:39 -04:00
/* msg ChanServ IDENTIFY? */
irc_cmd_join(session, creds.channel, 0);
}
DECL void
event_channel(irc_session_t * session,
2023-08-02 16:38:32 -04:00
char const * event,
char const * origin,
char const ** params,
unsigned int count)
{
2023-08-02 16:38:32 -04:00
/* char const * channel = params[0]; */
char const * message = params[1];
(void) session;
(void) event;
(void) origin;
2023-08-02 16:38:32 -04:00
/* (void) channel; */
(void) message;
(void) count;
2023-08-02 16:38:32 -04:00
current_username = get_username(origin);
/* parses the command */
2023-08-02 16:38:32 -04:00
if (*message == PREFIX_COMMAND_CHAR)
{
2023-08-02 16:38:32 -04:00
char * pmsg = strdup(++message);
if (pmsg)
{
parse_command(pmsg);
free(pmsg);
}
}
2023-08-02 16:38:32 -04:00
free(current_username);
}
2023-08-02 15:27:59 -04:00
int
2023-08-03 02:16:39 -04:00
init(void)
{
2023-08-02 15:27:59 -04:00
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)
{ ERR(1, "Error creating IRC session"); }
2023-08-03 02:16:39 -04:00
assert(creds.username != NULL);
assert(creds.server != NULL);
irc_connect(session,
creds.server, creds.port, creds.password,
creds.username, creds.username, creds.username);
atexit(rope);
/* 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;
}