1 /* EventLogger.c - NeonServ v5.6
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"
26 static struct Event *first_event = NULL, *last_event = NULL;
28 struct Event *createEvent(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, struct cmd_binding *command, char **args, int argc, int flags) {
29 struct Event *event = malloc(sizeof(*event));
32 printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
35 event->client = client;
38 event->event_time = time(0);
39 event->command = command;
40 char arguments[MAXLEN];
43 for(i = 0; i < argc; i++)
44 argpos += sprintf(arguments + argpos, "%s ", args[i]);
45 arguments[(argpos ? argpos-1 : 0)] = '\0';
46 event->arguments = strdup(arguments);
50 last_event->next = event;
59 void logEvent(struct Event *event) {
61 sprintf(fullcmd, "%s %s", event->command->func->name, event->arguments);
62 if((event->flags & CMDFLAG_LOG) && event->chan) {
63 char *auth = ((event->user->flags & USERFLAG_ISAUTHED) ? event->user->auth : "*");
64 loadChannelSettings(event->chan);
65 if((event->chan->flags & CHANFLAG_CHAN_REGISTERED))
66 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));
68 if((event->flags & CMDFLAG_OPLOG)) {
72 char *auth = ((event->user->flags & USERFLAG_ISAUTHED) ? event->user->auth : "*");
73 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
75 if ((row = mysql_fetch_row(res)) == NULL)
78 userid = atoi(row[0]);
80 loadChannelSettings(event->chan);
81 if((event->chan->flags & CHANFLAG_CHAN_REGISTERED))
82 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));
84 printf_log("main", LOG_OVERRIDE, "[%s:%s (%s)] %s", (event->chan ? event->chan->name : "*"), event->user->nick, auth, fullcmd);
88 static void destroyEvent(struct Event *event) {
89 if(event == first_event)
90 first_event = event->next;
91 if(event == last_event) {
93 for(last = first_event; last; last = last->next)
94 if(last->next == NULL) break;
97 free(event->arguments);
101 void destroyEvents() {
102 time_t now = time(0);
103 while(first_event && now - first_event->event_time >= 60) {
104 destroyEvent(first_event);