xbot/src/main.c

152 lines
2.9 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>
**/
#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>
2015-04-07 18:18:24 +00:00
#include "config.h"
2015-03-24 10:12:35 +00:00
#include "irc.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"
2015-03-24 10:12:35 +00:00
2024-02-13 07:22:10 +00:00
#ifdef _WIN32
#include <winsock2.h>
#else
#include <sys/select.h>
#endif
2016-02-22 04:40:41 +00:00
static time_t trespond;
2015-03-24 10:12:35 +00:00
int main()
{
2024-02-13 07:22:10 +00:00
int n;
fd_set rd;
struct irc_conn bot;
struct timeval tv;
2015-03-24 10:12:35 +00:00
2024-02-16 21:28:11 +00:00
char *p;
2024-02-13 07:22:10 +00:00
int bytesRecv;
2015-03-26 23:20:59 +00:00
2024-02-13 07:22:10 +00:00
init_events();
init_mods();
2015-03-24 17:48:11 +00:00
2024-02-13 07:22:10 +00:00
// Read the config
bot = read_config(bot, "xbot.cfg");
2015-03-24 10:12:35 +00:00
2024-02-13 07:22:10 +00:00
// Connect to the server
printf("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);
irc_auth(&bot);
2016-02-22 04:40:41 +00:00
2024-02-13 07:22:10 +00:00
for (;;)
{
FD_ZERO(&rd);
#ifdef _WIN32
FD_SET(bot.srv_fd, &rd);
#else
FD_SET(0, &rd);
FD_SET(fileno(bot.srv_fd), &rd);
#endif
tv.tv_sec = 120;
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);
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;
}
irc_raw(&bot, "PING %s", bot.host);
continue;
}
#ifdef _WIN32
if (FD_ISSET(bot.srv_fd, &rd))
{
bytesRecv = recv(bot.srv_fd, bot.in, sizeof(bot.in), 0);
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-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-13 07:22:10 +00:00
if (fgets(bot.in, sizeof bot.in, bot.srv_fd) == NULL)
{
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);
}
}
return 0;
2016-02-22 04:40:41 +00:00
}