From 83e07a13b16c18453ee198ae6ecf44a4d05bb3db Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Tue, 4 Dec 2007 17:09:14 +0100 Subject: [PATCH] join a channel --- irclogbot.c | 4 +++- irclogbot.conf | 3 +++ ircsession.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++---- ircsession.h | 5 +++++ 4 files changed, 63 insertions(+), 5 deletions(-) diff --git a/irclogbot.c b/irclogbot.c index 912d58b..17508fd 100644 --- a/irclogbot.c +++ b/irclogbot.c @@ -29,8 +29,10 @@ int main(int argc, char *argv[]) session->username = (char *)config_get_string("global", "username", NULL); session->realname = (char *)config_get_string("global", "realname", NULL); - irc_connect(session); + session->channel = (char *)config_get_string("global", "channel", "logbot"); + session->channel_key = (char *)config_get_string("global", "channel-key", NULL); + irc_connect(session); event_loop(); config_free(); diff --git a/irclogbot.conf b/irclogbot.conf index 1bfb6d6..7ab5385 100644 --- a/irclogbot.conf +++ b/irclogbot.conf @@ -5,3 +5,6 @@ server 83.140.172.211:6667 nickname logtest_ #username logtest_ #realname logtest_ + +channel #logtest +#channel-key test diff --git a/ircsession.c b/ircsession.c index ccb6334..9181ca2 100644 --- a/ircsession.c +++ b/ircsession.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "event.h" #include "ircsession.h" @@ -16,6 +17,8 @@ struct irc_session * irc_create_session(void) if (session == NULL) return NULL; + memset(session, 0, sizeof(struct irc_session)); + session->state = IRC_NONE; session->inbuf = create_linebuffer(4096); session->outbuf = create_linebuffer(4096); @@ -66,15 +69,60 @@ static int irc_read_cb(int fd, void *privdata) } char *line; - while ((line = linebuffer_getline(session->inbuf, NULL)) != NULL) { - log_print(LOG_DEBUG, "irc_read_cb(): from %s: %s", get_sockaddr_buf(&session->srv_addr), line); + while ((line = linebuffer_getline(session->inbuf, &len)) != NULL) { + char *p = line; + char *prefix = NULL; - if (strncmp(line, "PING", 4) == 0) + log_print(LOG_DEBUG, "irc_read_cb(): from %s (%d): %s", get_sockaddr_buf(&session->srv_addr), len, line); + + if (line[0] == ':' ) { + while ( *p && *p != ' ') + p++; + + *p++ = '\0'; + + prefix = line +1; + } + + int code = 0; + char *command = NULL; + if (isdigit(p[0]) && isdigit(p[1]) && isdigit(p[2])) { + p[3] = '\0'; + code = atoi(p); + p += 4; + + } else { + command = p; + while ( *p && *p != ' ') + p++; + + *p++ = '\0'; + } + + if (strncmp(line, "PING", 4) == 0) { irc_send(session, "PONG %s", line +6); + linebuffer_freeline(session->inbuf); + continue; + } + + if (code != 0) { + if ((code == 376 || code == 422) && session->state == IRC_CONNECTED) { + session->state = IRC_MOTD_RECEIVED; + + linebuffer_freeline(session->inbuf); + if (session->channel_key) + return irc_send(session, "JOIN %s :%s", session->channel, session->channel_key); + else + return irc_send(session, "JOIN %s", session->channel); + } + + } else { + if (!strcmp(command, "JOIN")) + session->state = IRC_JOINED; + } linebuffer_freeline(session->inbuf); } - return 0; } diff --git a/ircsession.h b/ircsession.h index e7e80c7..4f72009 100644 --- a/ircsession.h +++ b/ircsession.h @@ -16,6 +16,9 @@ struct irc_session { char *nickname; char *username; char *realname; + + char *channel; + char *channel_key; }; enum { @@ -23,6 +26,8 @@ enum { IRC_CONNECTING, IRC_CONNECTION_FAILED, IRC_CONNECTED, + IRC_MOTD_RECEIVED, + IRC_JOINED, IRC_DISCONNECTED, };