xbot/src/main.c

250 lines
5.4 KiB
C
Raw Normal View History

2015-03-24 10:12:35 +00:00
/*
* xbot: Just another IRC bot
*
* Written by Aaron Blakely <aaron@ephasic.org>
**/
2024-03-08 10:14:06 +00:00
2015-03-24 10:12:35 +00:00
#include <stdio.h>
#include <string.h>
2015-04-07 18:18:24 +00:00
#include <stdlib.h>
2016-02-22 04:40:41 +00:00
#include <time.h>
#include <errno.h>
2024-03-06 02:12:10 +00:00
2015-04-07 18:18:24 +00:00
#include "config.h"
2015-03-24 10:12:35 +00:00
#include "irc.h"
2024-03-06 02:12:10 +00:00
#include "db.h"
2015-03-26 23:20:59 +00:00
#include "util.h"
#include "events.h"
2024-02-16 21:28:11 +00:00
#include "module.h"
#include "channel.h"
2024-02-21 14:24:49 +00:00
#include "timers.h"
2015-03-24 10:12:35 +00:00
2024-02-13 07:22:10 +00:00
#ifdef _WIN32
#include "resource.h"
2024-02-13 07:22:10 +00:00
#include <winsock2.h>
#else
#include <sys/select.h>
2024-03-06 09:45:01 +00:00
#include <unistd.h>
2024-02-13 07:22:10 +00:00
#endif
2016-02-22 04:40:41 +00:00
static time_t trespond;
2024-03-08 10:14:06 +00:00
int main(int argc, char **argv)
2015-03-24 10:12:35 +00:00
{
2024-02-13 07:22:10 +00:00
int n;
fd_set rd;
struct irc_conn bot;
struct timeval tv;
2024-02-21 14:24:49 +00:00
struct timeval last_ping;
2024-03-08 10:14:06 +00:00
char conf[1024];
2024-02-21 14:24:49 +00:00
2024-02-16 21:28:11 +00:00
char *p;
2024-02-13 07:22:10 +00:00
int bytesRecv;
2024-02-24 01:57:59 +00:00
#ifdef _WIN32
HICON hIcon;
hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));
SendMessage(GetConsoleWindow(), WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
SetConsoleTitle("xbot [starting]");
#endif
2024-02-24 01:57:59 +00:00
bot.in = calloc(INBUF_SIZE, sizeof(char));
bot.out = calloc(OUTBUF_SIZE, sizeof(char));
2024-02-21 14:24:49 +00:00
last_ping.tv_sec = time(NULL);
2015-03-26 23:20:59 +00:00
2024-02-25 17:09:24 +00:00
set_bot(&bot);
2024-02-13 07:22:10 +00:00
init_events();
2024-02-21 14:24:49 +00:00
init_timers();
init_mods();
2024-03-08 10:14:06 +00:00
strlcpy(conf, "xbot.cfg", sizeof conf);
if (argc > 1)
{
if (argc == 2)
{
if (!strcmp(argv[1], "-v"))
{
printf("xbot version: %s\n", VERSION);
printf("Compiled on: %s %s\n\n", __DATE__, __TIME__);
printf("Written by Aaron Blakely <aaron@ephasic.org>\n");
printf("Licensed under the MIT License\n");
return 0;
}
else if (!strcmp(argv[1], "-h"))
{
printf("Usage: xbot [-v] [-h] [-c <config file>]\n");
return 0;
}
}
else if (argc == 3)
{
if (!strcmp(argv[1], "-c"))
{
free(bot.in);
free(bot.out);
bot.in = calloc(INBUF_SIZE, sizeof(char));
bot.out = calloc(OUTBUF_SIZE, sizeof(char));
strlcpy(conf, argv[2], sizeof conf);
}
}
}
2024-02-13 07:22:10 +00:00
// Read the config
2024-03-08 10:14:06 +00:00
bot = read_config(bot, conf);
2015-03-24 10:12:35 +00:00
2024-03-09 08:40:06 +00:00
log_init(bot.log_file);
2024-03-06 02:12:10 +00:00
// check if the db exists, if not, create it
2024-03-06 09:45:01 +00:00
#ifdef _WIN32
if (access(bot.db_file, 0) == -1)
#else
2024-03-06 02:12:10 +00:00
if (access(bot.db_file, F_OK) == -1)
2024-03-06 09:45:01 +00:00
#endif
2024-03-06 02:12:10 +00:00
{
2024-03-09 09:38:58 +00:00
xlog("[DB] Creating database file: %s\n", bot.db_file);
2024-03-06 02:12:10 +00:00
bot.db = (struct db_table *)malloc(sizeof(struct db_table));
memset(bot.db, 0, sizeof(struct db_table));
set_bot_db(bot.db);
2024-03-06 02:12:10 +00:00
bot.db->count = 0;
bot.db->hashes = NULL;
db_write(bot.db, bot.db_file);
2024-03-06 02:12:10 +00:00
}
else
{
2024-03-09 09:38:58 +00:00
xlog("[DB] Reading database file: %s\n", bot.db_file);
bot.db = db_read(bot.db_file);
set_bot_db(bot.db);
2024-03-06 02:12:10 +00:00
}
// run autoload
run_autoload(&bot);
2024-02-13 07:22:10 +00:00
// Connect to the server
2024-03-09 09:38:58 +00:00
xlog("[IRC] Connecting to %s...\n", bot.host);
2015-03-24 10:12:35 +00:00
2024-02-13 07:22:10 +00:00
irc_connect(&bot);
2024-02-24 01:57:59 +00:00
trespond = time(NULL);
2024-02-13 07:22:10 +00:00
irc_auth(&bot);
2016-02-22 04:40:41 +00:00
2024-02-13 07:22:10 +00:00
for (;;)
{
2024-02-21 14:24:49 +00:00
fire_timers();
fire_handler(&bot, TICK, NULL);
2024-02-13 07:22:10 +00:00
FD_ZERO(&rd);
#ifdef _WIN32
FD_SET(bot.srv_fd, &rd);
#else
FD_SET(0, &rd);
FD_SET(fileno(bot.srv_fd), &rd);
#endif
2024-02-21 14:24:49 +00:00
tv.tv_sec = 1;
2024-02-13 07:22:10 +00:00
tv.tv_usec = 0;
#ifdef _WIN32
n = select(bot.srv_fd, &rd, NULL, NULL, &tv);
if (n == SOCKET_ERROR)
2016-02-22 04:40:41 +00:00
{
2024-02-13 07:22:10 +00:00
eprint("xbot: error on select(): %d\n", WSAGetLastError());
closesocket(bot.srv_fd);
WSACleanup();
2016-02-22 04:40:41 +00:00
2024-02-13 07:22:10 +00:00
return -1;
2016-02-22 04:40:41 +00:00
}
2024-02-13 07:22:10 +00:00
#else
n = select(fileno(bot.srv_fd) + 1, &rd, 0, 0, &tv);
2024-02-24 01:57:59 +00:00
if (n < 0)
2016-02-22 04:40:41 +00:00
{
2024-02-13 07:22:10 +00:00
if (errno == EINTR)
continue;
eprint("xbot: error on select()\n");
return -1;
}
#endif
else if (n == 0)
{
if (time(NULL) - trespond >= 300)
{
eprint("xbot shutting down: parse timeout\n");
return -1;
}
2024-02-21 14:24:49 +00:00
if (time(NULL) - last_ping.tv_sec >= 120)
{
last_ping.tv_sec = time(NULL);
irc_raw(&bot, "PING %s", bot.host);
}
2024-02-13 07:22:10 +00:00
continue;
}
#ifdef _WIN32
if (FD_ISSET(bot.srv_fd, &rd))
{
2024-02-24 01:57:59 +00:00
bytesRecv = recv(bot.srv_fd, bot.in, INBUF_SIZE, 0);
2024-02-13 07:22:10 +00:00
if (bytesRecv == SOCKET_ERROR)
2016-02-22 04:40:41 +00:00
{
2024-02-13 07:22:10 +00:00
eprint("Error receiving data: %d\n", WSAGetLastError());
closesocket(bot.srv_fd);
WSACleanup();
2016-02-22 04:40:41 +00:00
return -1;
}
if (bytesRecv == 0)
{
eprint("xbot: remote host closed connection\n");
return 0;
}
2024-02-13 07:22:10 +00:00
bot.in[bytesRecv] = '\0';
2024-02-16 21:28:11 +00:00
printf("recv: %s\r\n", bot.in);
// split bot.in into lines by \r\n and parse each one
while (1)
{
2024-02-16 23:32:54 +00:00
// remove \r
p = strchr(bot.in, '\r');
2024-02-16 21:28:11 +00:00
p = strchr(bot.in, '\n');
if (p == NULL)
break;
*p = '\0';
irc_parse_raw(&bot, bot.in);
memmove(bot.in, p + 1, strlen(p + 1) + 1);
}
2024-02-24 01:57:59 +00:00
free(p);
2024-02-13 07:22:10 +00:00
#else
2015-03-24 10:12:35 +00:00
if (FD_ISSET(fileno(bot.srv_fd), &rd))
{
2024-02-24 01:57:59 +00:00
if (fgets(bot.in, INBUF_SIZE, bot.srv_fd) == NULL)
2024-02-13 07:22:10 +00:00
{
eprint("xbot: remote host closed connection\n");
return 0;
}
2024-02-16 21:28:11 +00:00
2024-02-13 07:22:10 +00:00
printf("recv: %s\r\n", bot.in);
irc_parse_raw(&bot, bot.in);
2024-02-16 21:28:11 +00:00
#endif
2024-02-13 07:22:10 +00:00
trespond = time(NULL);
}
2024-02-21 14:24:49 +00:00
2024-02-13 07:22:10 +00:00
}
return 0;
2016-02-22 04:40:41 +00:00
}