tried to reorder the program structure and build process
[NeonServV5.git] / src / EventLogger.c
diff --git a/src/EventLogger.c b/src/EventLogger.c
new file mode 100644 (file)
index 0000000..d0b61bd
--- /dev/null
@@ -0,0 +1,87 @@
+
+#include "EventLogger.h"
+#include "modcmd.h"
+#include "mysqlConn.h"
+#include "UserNode.h"
+#include "ChanNode.h"
+#include "DBHelper.h"
+
+static struct Event *first_event = NULL, *last_event = NULL;
+
+struct Event *createEvent(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *command, char **args, int argc, int flags) {
+    struct Event *event = malloc(sizeof(*event));
+    if (!event)
+    {
+        perror("malloc() failed");
+        return NULL;
+    }
+    event->client = client;
+    event->user = user;
+    event->chan = chan;
+    event->event_time = time(0);
+    event->command = strdup(command);
+    char arguments[MAXLEN];
+    int argpos = 0;
+    int i;
+    for(i = 0; i < argc; i++)
+        argpos += sprintf(arguments + argpos, "%s ", args[i]);
+    arguments[(argpos ? argpos-1 : 0)] = '\0';
+    event->arguments = strdup(arguments);
+    event->flags = flags;
+    event->next = NULL;
+    if(last_event) {
+        last_event->next = event;
+        last_event = event;
+    } else {
+        last_event = event;
+        first_event = event;
+    }
+    return event;
+}
+
+void logEvent(struct Event *event) {
+    char fullcmd[MAXLEN];
+    sprintf(fullcmd, "%s %s", event->command, event->arguments);
+    if((event->flags & CMDFLAG_LOG) && event->chan) {
+        char *auth = ((event->user->flags & USERFLAG_ISAUTHED) ? event->user->auth : "*");
+        loadChannelSettings(event->chan);
+        if((event->chan->flags & CHANFLAG_CHAN_REGISTERED))
+            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));
+    }
+    if((event->flags & CMDFLAG_OPLOG)) {
+        MYSQL_RES *res;
+        MYSQL_ROW row;
+        int userid;
+        char *auth = ((event->user->flags & USERFLAG_ISAUTHED) ? event->user->auth : "*");
+        printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(auth));
+        res = mysql_use();
+        if ((row = mysql_fetch_row(res)) == NULL) 
+            userid = 0;
+        else
+            userid = atoi(row[0]);
+        loadChannelSettings(event->chan);
+        if((event->chan->flags & CHANFLAG_CHAN_REGISTERED))
+            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));
+    }
+}
+
+static void destroyEvent(struct Event *event) {
+    if(event == first_event)
+        first_event = event->next;
+    if(event == last_event) {
+        struct Event *last;
+        for(last = first_event; last; last = last->next)
+            if(last->next == NULL) break;
+        last_event = last;
+    }
+    free(event->command);
+    free(event->arguments);
+    free(event);
+}
+
+void destroyEvents() {
+    time_t now = time(0);
+    while(first_event && now - first_event->event_time >= 60) {
+        destroyEvent(first_event);
+    }
+}