tried to reorder the program structure and build process
[NeonServV5.git] / src / EventLogger.c
1
2 #include "EventLogger.h"
3 #include "modcmd.h"
4 #include "mysqlConn.h"
5 #include "UserNode.h"
6 #include "ChanNode.h"
7 #include "DBHelper.h"
8
9 static struct Event *first_event = NULL, *last_event = NULL;
10
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));
13     if (!event)
14     {
15         perror("malloc() failed");
16         return NULL;
17     }
18     event->client = client;
19     event->user = user;
20     event->chan = chan;
21     event->event_time = time(0);
22     event->command = strdup(command);
23     char arguments[MAXLEN];
24     int argpos = 0;
25     int i;
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);
30     event->flags = flags;
31     event->next = NULL;
32     if(last_event) {
33         last_event->next = event;
34         last_event = event;
35     } else {
36         last_event = event;
37         first_event = event;
38     }
39     return event;
40 }
41
42 void logEvent(struct Event *event) {
43     char fullcmd[MAXLEN];
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));
50     }
51     if((event->flags & CMDFLAG_OPLOG)) {
52         MYSQL_RES *res;
53         MYSQL_ROW row;
54         int userid;
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));
57         res = mysql_use();
58         if ((row = mysql_fetch_row(res)) == NULL) 
59             userid = 0;
60         else
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));
65     }
66 }
67
68 static void destroyEvent(struct Event *event) {
69     if(event == first_event)
70         first_event = event->next;
71     if(event == last_event) {
72         struct Event *last;
73         for(last = first_event; last; last = last->next)
74             if(last->next == NULL) break;
75         last_event = last;
76     }
77     free(event->command);
78     free(event->arguments);
79     free(event);
80 }
81
82 void destroyEvents() {
83     time_t now = time(0);
84     while(first_event && now - first_event->event_time >= 60) {
85         destroyEvent(first_event);
86     }
87 }