|
|
|
@ -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;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strncmp(line, "PING", 4) == 0)
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|