From db349a8ccc0cb420ee9fcdf6ff999273a3ae8b3d Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Tue, 4 Dec 2007 17:26:30 +0100 Subject: [PATCH] syslog listener --- Makefile | 2 +- irclogbot.c | 10 ++++++++- irclogbot.conf | 2 ++ ircsession.h | 3 ++- syslog.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ syslog.h | 7 +++++++ 6 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 syslog.c create mode 100644 syslog.h diff --git a/Makefile b/Makefile index 27f1b6f..2d66541 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CFLAGS := -O2 -pipe -Wall -OBJS := configfile.o event.o ircsession.o linebuffer.o logging.o sockaddr.o tcpsocket.o +OBJS := configfile.o event.o ircsession.o linebuffer.o logging.o sockaddr.o syslog.o tcpsocket.o all: irclogbot diff --git a/irclogbot.c b/irclogbot.c index 17508fd..a9bcd53 100644 --- a/irclogbot.c +++ b/irclogbot.c @@ -5,9 +5,9 @@ #include "configfile.h" #include "event.h" #include "ircsession.h" -#include "list.h" #include "logging.h" #include "sockaddr.h" +#include "syslog.h" #define DEFAULT_CONFIG "irclogbot.conf" @@ -17,6 +17,11 @@ int main(int argc, char *argv[]) if (config_parse(DEFAULT_CONFIG)) exit(1); + char *syslog_addr = (char *)config_get_string("global", "syslog-addr", "0.0.0.0:514"); + int logsock = syslog_listen(syslog_addr); + if (logsock == -1) + exit(1); + char *server_str = (char *)config_get_string("global", "server", NULL); if (server_str == NULL) exit(1); @@ -33,8 +38,11 @@ int main(int argc, char *argv[]) session->channel_key = (char *)config_get_string("global", "channel-key", NULL); irc_connect(session); + + event_add_readfd(NULL, logsock, syslog_cb, session); event_loop(); + close(logsock); config_free(); return 0; } diff --git a/irclogbot.conf b/irclogbot.conf index 7ab5385..93fdabe 100644 --- a/irclogbot.conf +++ b/irclogbot.conf @@ -1,4 +1,6 @@ [global] +syslog-addr 0.0.0.0:514 + server 83.140.172.211:6667 #server-pass test diff --git a/ircsession.h b/ircsession.h index 4f72009..e38c6b0 100644 --- a/ircsession.h +++ b/ircsession.h @@ -33,7 +33,8 @@ enum { struct irc_session * irc_create_session(void); void irc_destroy_session(struct irc_session *session); - int irc_connect(struct irc_session *session); +int irc_send(struct irc_session *session, const char *fmt, ...); + #endif /* _IRCSESSION_H_ */ diff --git a/syslog.c b/syslog.c new file mode 100644 index 0000000..6b37240 --- /dev/null +++ b/syslog.c @@ -0,0 +1,55 @@ +#include +#include +#include + +#include +#include +#include + +#include "ircsession.h" +#include "logging.h" +#include "sockaddr.h" + +int syslog_listen(const char *addr_str) +{ + struct sockaddr_in addr; + parse_sockaddr(addr_str, &addr); + + int sock = socket(PF_INET, SOCK_DGRAM, 0); + if (sock < 0) { + log_print(LOG_ERROR, "socket()"); + return -1; + } + + if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { + log_print(LOG_ERROR, "bind()"); + return -1; + } + + return sock; +} + +int syslog_cb(int fd, void *privdata) +{ + struct irc_session *session = (struct irc_session *)privdata; + + char buf[1500]; + struct sockaddr_in addr; + unsigned int i = sizeof(addr); + + int len = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&addr, &i); + if (len <= 0) + return -1; + + if (session->state != IRC_JOINED) + return 0; + + buf[len] = '\0'; + + char *tmp = strchr(buf, ' '); + tmp = strchr(tmp +1, ' '); + tmp = strchr(tmp +1, ' '); + + irc_send(session, "PRIVMSG %s :%s: %s", session->channel, inet_ntoa(addr.sin_addr), tmp +1); + return 0; +} diff --git a/syslog.h b/syslog.h new file mode 100644 index 0000000..a3cc48c --- /dev/null +++ b/syslog.h @@ -0,0 +1,7 @@ +#ifndef _SYSLOG_H_ +#define _SYSLOG_H_ + +int syslog_listen(const char *addr_str); +int syslog_cb(int fd, void *privdata); + +#endif /* _SYSLOG_H_ */