Exemple #1
0
int
main(int argc, char *argv[])
{
    // Compile some regexes.
    if (regcomp(&new_pr_regex, NEW_PR_PATTERN, REG_EXTENDED)) {
        fprintf(stderr, "Failed to compile regex.\n");
        return 1;
    }

    // Initialize SQLite gunk.
    int retval;

    retval = sqlite3_open(DATABASE_NAME, &db);
    if (retval) {
        fprintf(stderr, "Failed to open database: %s\n", sqlite3_errmsg(db));
        return 1;
    }

    retval = sqlite3_exec(db, INITIALIZE_DB, 0, 0, 0);
    if (retval) {
        fprintf(stderr, "Failed to initialize database: %s\n", sqlite3_errmsg(db));
        return 1;
    }
 
    // Kick off the IRC connection.
	char buf[BUF_LEN];
	struct ircbuf ircbuf;
	ircbuf_init(&ircbuf, buf, BUF_LEN);

	int fd = irc_connect(IRC_HOST, IRC_PORT);
	if (fd < 0) {
		fprintf(stderr, "Failed to open connection.\n");
		return 1;
	}

	irc_nick(fd, IRC_NICK, NULL);
	irc_join(fd, IRC_CHANNEL);

	char *line;
	while ((line = irc_getline(fd, &ircbuf))) {
		printf("%s\n", line);

		struct ircmsg msg;
		irc_parseline(line, &msg);
		if (!dispatch_handler(fd, &msg))
			return 2;
	}

	return 0;
}
Exemple #2
0
void proto_tick() {
	int nbytes;
	char data[BUFSIZE/2];

	while (bot->running) {
		memset(&data, 0, BUFSIZE/2);

tick:
		tick_functions();

		recv:
		if ((nbytes = recv(bot->socket, data, (BUFSIZE/2)-1, 0)) == -1) {
			if (errno == EINTR) {
				// what a pain in the ass,
				// we got interrupted >.>
				goto recv;
			}

			if (errno == EAGAIN) {
				usleep(16666);
				goto tick;
			}


			perror("recv failed");
			exit(4);
		}

		strcat(bot->buffer, data);

		if (nbytes == 0) {
			bot->running = 0;
		}

		char *i = irc_getline(bot);
		if (i == NULL) {
			return;
		}

		for (; i != NULL; i = irc_nextline(bot)) {

			printf("%s\n", i);

			if (startswith(i, "PING")) {
				char *t = last(i);
				sockprintf(bot->socket, "PONG %s", t);
				free(t);
				free(i);
				continue;
			}

			struct message *msg = parse(i);

			if (EQ(msg->meth, "422") || EQ(msg->meth, "376")) {
				sockprintf(bot->socket, "JOIN #bots");
			}

			Link *handlers = hashmap_get(msg->meth, bot->handlers);
			for (Link *i = handlers; i != NULL; i = i->next) {
				((handler_t)i->ptr)(bot, msg);
			}
			freemsg(msg);
			free(i);
		}
	}
}