1 /* EventLogger.c - NeonServ v5.5
2 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "EventLogger.h"
20 #include "mysqlConn.h"
25 static struct Event *first_event = NULL, *last_event = NULL;
27 struct Event *createEvent(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, struct cmd_binding *command, char **args, int argc, int flags) {
28 struct Event *event = malloc(sizeof(*event));
31 perror("malloc() failed");
34 event->client = client;
37 event->event_time = time(0);
38 event->command = command;
39 char arguments[MAXLEN];
42 for(i = 0; i < argc; i++)
43 argpos += sprintf(arguments + argpos, "%s ", args[i]);
44 arguments[(argpos ? argpos-1 : 0)] = '\0';
45 event->arguments = strdup(arguments);
49 last_event->next = event;
58 void logEvent(struct Event *event) {
60 sprintf(fullcmd, "%s %s", event->command->func->name, event->arguments);
61 if((event->flags & CMDFLAG_LOG) && event->chan) {
62 char *auth = ((event->user->flags & USERFLAG_ISAUTHED) ? event->user->auth : "*");
63 loadChannelSettings(event->chan);
64 if((event->chan->flags & CHANFLAG_CHAN_REGISTERED))
65 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));
67 if((event->flags & CMDFLAG_OPLOG)) {
71 char *auth = ((event->user->flags & USERFLAG_ISAUTHED) ? event->user->auth : "*");
72 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
74 if ((row = mysql_fetch_row(res)) == NULL)
77 userid = atoi(row[0]);
79 loadChannelSettings(event->chan);
80 if((event->chan->flags & CHANFLAG_CHAN_REGISTERED))
81 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));
86 static void destroyEvent(struct Event *event) {
87 if(event == first_event)
88 first_event = event->next;
89 if(event == last_event) {
91 for(last = first_event; last; last = last->next)
92 if(last->next == NULL) break;
95 free(event->arguments);
99 void destroyEvents() {
100 time_t now = time(0);
101 while(first_event && now - first_event->event_time >= 60) {
102 destroyEvent(first_event);