join a channel

This commit is contained in:
Olaf Rempel 2007-12-04 17:09:14 +01:00
parent ef25dbe436
commit 83e07a13b1
4 changed files with 63 additions and 5 deletions

View File

@ -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();

View File

@ -5,3 +5,6 @@ server 83.140.172.211:6667
nickname logtest_
#username logtest_
#realname logtest_
channel #logtest
#channel-key test

View File

@ -2,6 +2,7 @@
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#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;
}

View File

@ -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,
};