xbot/src/main.c

84 lines
1.3 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>
2015-03-24 10:12:35 +00:00
#include <sys/select.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"
2015-03-24 10:12:35 +00:00
2016-02-22 04:40:41 +00:00
static time_t trespond;
2015-03-24 10:12:35 +00:00
int main()
{
2016-02-22 04:40:41 +00:00
int n;
2015-03-24 10:12:35 +00:00
fd_set rd;
struct irc_conn bot;
2016-02-22 04:40:41 +00:00
struct timeval tv;
2015-03-24 10:12:35 +00:00
2015-03-26 23:20:59 +00:00
init_events();
2015-04-07 18:18:24 +00:00
// Read the config
bot = read_config(bot, "xbot.cfg");
2015-03-24 17:48:11 +00:00
2015-03-24 10:12:35 +00:00
// Connect to the server
printf("Connecting to %s...\n", bot.host);
irc_connect(&bot);
irc_auth(&bot);
for (;;)
{
FD_ZERO(&rd);
2016-02-22 04:40:41 +00:00
FD_SET(0, &rd);
2015-03-24 10:12:35 +00:00
FD_SET(fileno(bot.srv_fd), &rd);
2016-02-22 04:40:41 +00:00
tv.tv_sec = 120;
tv.tv_usec = 0;
n = select(fileno(bot.srv_fd) + 1, &rd, 0, 0, &tv);
if (n < 0)
{
if (errno == EINTR)
continue;
eprint("xbot: error on select()\n");
return 0;
}
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;
}
2015-03-24 10:12:35 +00:00
if (FD_ISSET(fileno(bot.srv_fd), &rd))
{
if (fgets(bot.in, sizeof bot.in, bot.srv_fd) == NULL)
{
eprint("xbot: remote host closed connection\n");
2015-04-18 15:51:15 +00:00
return 0;
2015-03-24 10:12:35 +00:00
}
irc_parse_raw(&bot, bot.in);
2016-02-22 04:40:41 +00:00
trespond = time(NULL);
2015-03-24 10:12:35 +00:00
}
}
return 0;
2016-02-22 04:40:41 +00:00
}