syslog listener

This commit is contained in:
Olaf Rempel 2007-12-04 17:26:30 +01:00
parent 83e07a13b1
commit db349a8ccc
6 changed files with 76 additions and 3 deletions

View File

@ -1,6 +1,6 @@
CFLAGS := -O2 -pipe -Wall 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 all: irclogbot

View File

@ -5,9 +5,9 @@
#include "configfile.h" #include "configfile.h"
#include "event.h" #include "event.h"
#include "ircsession.h" #include "ircsession.h"
#include "list.h"
#include "logging.h" #include "logging.h"
#include "sockaddr.h" #include "sockaddr.h"
#include "syslog.h"
#define DEFAULT_CONFIG "irclogbot.conf" #define DEFAULT_CONFIG "irclogbot.conf"
@ -17,6 +17,11 @@ int main(int argc, char *argv[])
if (config_parse(DEFAULT_CONFIG)) if (config_parse(DEFAULT_CONFIG))
exit(1); 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); char *server_str = (char *)config_get_string("global", "server", NULL);
if (server_str == NULL) if (server_str == NULL)
exit(1); exit(1);
@ -33,8 +38,11 @@ int main(int argc, char *argv[])
session->channel_key = (char *)config_get_string("global", "channel-key", NULL); session->channel_key = (char *)config_get_string("global", "channel-key", NULL);
irc_connect(session); irc_connect(session);
event_add_readfd(NULL, logsock, syslog_cb, session);
event_loop(); event_loop();
close(logsock);
config_free(); config_free();
return 0; return 0;
} }

View File

@ -1,4 +1,6 @@
[global] [global]
syslog-addr 0.0.0.0:514
server 83.140.172.211:6667 server 83.140.172.211:6667
#server-pass test #server-pass test

View File

@ -33,7 +33,8 @@ enum {
struct irc_session * irc_create_session(void); struct irc_session * irc_create_session(void);
void irc_destroy_session(struct irc_session *session); void irc_destroy_session(struct irc_session *session);
int irc_connect(struct irc_session *session); int irc_connect(struct irc_session *session);
int irc_send(struct irc_session *session, const char *fmt, ...);
#endif /* _IRCSESSION_H_ */ #endif /* _IRCSESSION_H_ */

55
syslog.c Normal file
View File

@ -0,0 +1,55 @@
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#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;
}

7
syslog.h Normal file
View File

@ -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_ */