Merge branch 'development'
[NeonServV5.git] / src / EventLogger.c
1 /* EventLogger.c - NeonServ v5.6
2  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
3  * 
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.
8  * 
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.
13  * 
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/>. 
16  */
17
18 #include "EventLogger.h"
19 #include "modcmd.h"
20 #include "mysqlConn.h"
21 #include "UserNode.h"
22 #include "ChanNode.h"
23 #include "DBHelper.h"
24 #include "log.h"
25
26 static struct Event *first_event = NULL, *last_event = NULL;
27
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));
30     if (!event)
31     {
32         printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
33         return NULL;
34     }
35     event->client = client;
36     event->user = user;
37     event->chan = chan;
38     event->event_time = time(0);
39     event->command = command;
40     char arguments[MAXLEN];
41     int argpos = 0;
42     int i;
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);
47     event->flags = flags;
48     event->next = NULL;
49     if(last_event) {
50         last_event->next = event;
51         last_event = event;
52     } else {
53         last_event = event;
54         first_event = event;
55     }
56     return event;
57 }
58
59 void logEvent(struct Event *event) {
60     char fullcmd[MAXLEN];
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));
67     }
68     if((event->flags & CMDFLAG_OPLOG)) {
69         MYSQL_RES *res;
70         MYSQL_ROW row;
71         int userid;
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));
74         res = mysql_use();
75         if ((row = mysql_fetch_row(res)) == NULL) 
76             userid = 0;
77         else
78             userid = atoi(row[0]);
79         if(event->chan) {
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));
83         }
84         printf_log("main", LOG_OVERRIDE, "[%s:%s (%s)] %s", (event->chan ? event->chan->name : "*"), event->user->nick, auth, fullcmd);
85     }
86 }
87
88 static void destroyEvent(struct Event *event) {
89     if(event == first_event)
90         first_event = event->next;
91     if(event == last_event) {
92         struct Event *last;
93         for(last = first_event; last; last = last->next)
94             if(last->next == NULL) break;
95         last_event = last;
96     }
97     free(event->arguments);
98     free(event);
99 }
100
101 void destroyEvents() {
102     time_t now = time(0);
103     while(first_event && now - first_event->event_time >= 60) {
104         destroyEvent(first_event);
105     }
106 }