join a channel
This commit is contained in:
parent
ef25dbe436
commit
83e07a13b1
@ -29,8 +29,10 @@ int main(int argc, char *argv[])
|
|||||||
session->username = (char *)config_get_string("global", "username", NULL);
|
session->username = (char *)config_get_string("global", "username", NULL);
|
||||||
session->realname = (char *)config_get_string("global", "realname", 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();
|
event_loop();
|
||||||
|
|
||||||
config_free();
|
config_free();
|
||||||
|
@ -5,3 +5,6 @@ server 83.140.172.211:6667
|
|||||||
nickname logtest_
|
nickname logtest_
|
||||||
#username logtest_
|
#username logtest_
|
||||||
#realname logtest_
|
#realname logtest_
|
||||||
|
|
||||||
|
channel #logtest
|
||||||
|
#channel-key test
|
||||||
|
56
ircsession.c
56
ircsession.c
@ -2,6 +2,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
#include "event.h"
|
#include "event.h"
|
||||||
#include "ircsession.h"
|
#include "ircsession.h"
|
||||||
@ -16,6 +17,8 @@ struct irc_session * irc_create_session(void)
|
|||||||
if (session == NULL)
|
if (session == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
memset(session, 0, sizeof(struct irc_session));
|
||||||
|
|
||||||
session->state = IRC_NONE;
|
session->state = IRC_NONE;
|
||||||
session->inbuf = create_linebuffer(4096);
|
session->inbuf = create_linebuffer(4096);
|
||||||
session->outbuf = create_linebuffer(4096);
|
session->outbuf = create_linebuffer(4096);
|
||||||
@ -66,15 +69,60 @@ static int irc_read_cb(int fd, void *privdata)
|
|||||||
}
|
}
|
||||||
|
|
||||||
char *line;
|
char *line;
|
||||||
while ((line = linebuffer_getline(session->inbuf, NULL)) != NULL) {
|
while ((line = linebuffer_getline(session->inbuf, &len)) != NULL) {
|
||||||
log_print(LOG_DEBUG, "irc_read_cb(): from %s: %s", get_sockaddr_buf(&session->srv_addr), line);
|
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);
|
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);
|
linebuffer_freeline(session->inbuf);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,6 +16,9 @@ struct irc_session {
|
|||||||
char *nickname;
|
char *nickname;
|
||||||
char *username;
|
char *username;
|
||||||
char *realname;
|
char *realname;
|
||||||
|
|
||||||
|
char *channel;
|
||||||
|
char *channel_key;
|
||||||
};
|
};
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
@ -23,6 +26,8 @@ enum {
|
|||||||
IRC_CONNECTING,
|
IRC_CONNECTING,
|
||||||
IRC_CONNECTION_FAILED,
|
IRC_CONNECTION_FAILED,
|
||||||
IRC_CONNECTED,
|
IRC_CONNECTED,
|
||||||
|
IRC_MOTD_RECEIVED,
|
||||||
|
IRC_JOINED,
|
||||||
IRC_DISCONNECTED,
|
IRC_DISCONNECTED,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user