2 #include "EventLogger.h"
9 static struct Event *first_event = NULL, *last_event = NULL;
11 struct Event *createEvent(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *command, char **args, int argc, int flags) {
12 struct Event *event = malloc(sizeof(*event));
15 perror("malloc() failed");
18 event->client = client;
21 event->event_time = time(0);
22 event->command = strdup(command);
23 char arguments[MAXLEN];
26 for(i = 0; i < argc; i++)
27 argpos += sprintf(arguments + argpos, "%s ", args[i]);
28 arguments[(argpos ? argpos-1 : 0)] = '\0';
29 event->arguments = strdup(arguments);
33 last_event->next = event;
42 void logEvent(struct Event *event) {
44 sprintf(fullcmd, "%s %s", event->command, event->arguments);
45 if((event->flags & CMDFLAG_LOG) && event->chan) {
46 char *auth = ((event->user->flags & USERFLAG_ISAUTHED) ? event->user->auth : "*");
47 loadChannelSettings(event->chan);
48 if((event->chan->flags & CHANFLAG_CHAN_REGISTERED))
49 printf_mysql_query("INSERT INTO `events` (`cid`, `nick`, `auth`, `time`, `command`) VALUES ('%d', '%s', '%s', UNIX_TIMESTAMP(), '%s')", event->chan->channel_id, escape_string(event->user->nick), auth, escape_string(fullcmd));
51 if((event->flags & CMDFLAG_OPLOG)) {
55 char *auth = ((event->user->flags & USERFLAG_ISAUTHED) ? event->user->auth : "*");
56 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
58 if ((row = mysql_fetch_row(res)) == NULL)
61 userid = atoi(row[0]);
62 loadChannelSettings(event->chan);
63 if((event->chan->flags & CHANFLAG_CHAN_REGISTERED))
64 printf_mysql_query("INSERT INTO `godlog` (`godlog_cid`, `godlog_uid`, `godlog_time`, `godlog_cmd`) VALUES ('%d', '%d', UNIX_TIMESTAMP(), '%s')", event->chan->channel_id, userid, escape_string(fullcmd));
68 static void destroyEvent(struct Event *event) {
69 if(event == first_event)
70 first_event = event->next;
71 if(event == last_event) {
73 for(last = first_event; last; last = last->next)
74 if(last->next == NULL) break;
78 free(event->arguments);
82 void destroyEvents() {
84 while(first_event && now - first_event->event_time >= 60) {
85 destroyEvent(first_event);