*** VERSION 5.1.0 ***
[NeonServV5.git] / src / EventLogger.c
1 /* EventLogger.c - NeonServ v5.1
2  * Copyright (C) 2011  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
25 static struct Event *first_event = NULL, *last_event = NULL;
26
27 struct Event *createEvent(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *command, char **args, int argc, int flags) {
28     struct Event *event = malloc(sizeof(*event));
29     if (!event)
30     {
31         perror("malloc() failed");
32         return NULL;
33     }
34     event->client = client;
35     event->user = user;
36     event->chan = chan;
37     event->event_time = time(0);
38     event->command = strdup(command);
39     char arguments[MAXLEN];
40     int argpos = 0;
41     int i;
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);
46     event->flags = flags;
47     event->next = NULL;
48     if(last_event) {
49         last_event->next = event;
50         last_event = event;
51     } else {
52         last_event = event;
53         first_event = event;
54     }
55     return event;
56 }
57
58 void logEvent(struct Event *event) {
59     char fullcmd[MAXLEN];
60     sprintf(fullcmd, "%s %s", event->command, 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));
66     }
67     if((event->flags & CMDFLAG_OPLOG)) {
68         MYSQL_RES *res;
69         MYSQL_ROW row;
70         int userid;
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));
73         res = mysql_use();
74         if ((row = mysql_fetch_row(res)) == NULL) 
75             userid = 0;
76         else
77             userid = atoi(row[0]);
78         if(event->chan) {
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));
82         }
83     }
84 }
85
86 static void destroyEvent(struct Event *event) {
87     if(event == first_event)
88         first_event = event->next;
89     if(event == last_event) {
90         struct Event *last;
91         for(last = first_event; last; last = last->next)
92             if(last->next == NULL) break;
93         last_event = last;
94     }
95     free(event->command);
96     free(event->arguments);
97     free(event);
98 }
99
100 void destroyEvents() {
101     time_t now = time(0);
102     while(first_event && now - first_event->event_time >= 60) {
103         destroyEvent(first_event);
104     }
105 }