added some more helpserv functions
[NeonServV5.git] / src / bot_NeonHelp.c
index 292344d996d86d20db3f2de4e947755d83ee8cda..19a0e9fd4a4d2711b2de7cd004557c89967251c5 100644 (file)
@@ -105,6 +105,8 @@ static void start_bots() {
         client->botid = BOTID;
         client->clientid = atoi(row[7]);
         connect_socket(client);
+        //close old, still opened requests
+        printf_mysql_query("UPDATE `helpserv_requests` SET `status` = '2' WHERE `botid` = '%d'", client->clientid);
     }
     
     printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access`, `flags` FROM `bot_binds` WHERE `botclass` = '%d'", BOTID);
@@ -233,9 +235,11 @@ static void neonhelp_event_privmsg_async(struct ClientSocket *client, struct Use
     struct NeonHelpNode *helpnode = malloc(sizeof(*helpnode));
     if(!helpnode) return;
     helpnode->user = user;
+    helpnode->logchan = getChanByName(row[0]);
     helpnode->status = 0;
-    printf_mysql_query("INSERT INTO `helpserv_requests` (`host`, `hand`, `nick`, `status`, `supporter`, `time`, `text`) VALUES ('%s@%s', '%s', '%s', '0', '-1', UNIX_TIMESTAMP(), '%s')", escape_string(user->ident), escape_string(user->host), ((user->flags & USERFLAG_ISAUTHED) ? escape_string(user->auth) : "*"), escape_string(user->nick), escape_string(message));
+    printf_mysql_query("INSERT INTO `helpserv_requests` (`botid`, `host`, `hand`, `nick`, `status`, `supporter`, `time`, `text`) VALUES ('%d', '%s@%s', '%s', '%s', '0', '-1', UNIX_TIMESTAMP(), '%s')", client->clientid, escape_string(user->ident), escape_string(user->host), ((user->flags & USERFLAG_ISAUTHED) ? escape_string(user->auth) : "*"), escape_string(user->nick), escape_string(message));
     helpnode->suppid = (int) mysql_insert_id(mysql_conn);
+    helpnode->log = NULL;
     helpnode->next = ((client->flags & SOCKET_HAVE_HELPNODE) ? client->botclass_helpnode : NULL);
     client->botclass_helpnode = helpnode;
     client->flags |= SOCKET_HAVE_HELPNODE;
@@ -270,8 +274,61 @@ static void neonhelp_event_privmsg_async(struct ClientSocket *client, struct Use
     }
 }
 
+static void neonhelp_event_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
+    char logline[MAXLEN];
+    sprintf(logline, "<%s> %s", user->nick, message);
+    struct ClientSocket *client;
+    for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
+        if(client->botid == BOTID) {
+            struct NeonHelpNode *helpnode;
+            if(client->flags & SOCKET_HAVE_HELPNODE) {
+                for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
+                    if(helpnode->logchan == chan && helpnode->status == 1) {
+                        if(!helpnode->log) {
+                            helpnode->log = calloc(LOGBUFFERLINES, sizeof(char*));
+                            if(!helpnode->log) return;
+                        }
+                        int i;
+                        for(i = 0; i < LOGBUFFERLINES; i++) {
+                            if(!helpnode->log[i]) {
+                                helpnode->log[i] = strdup(logline);
+                                break;
+                            }
+                        }
+                        if(i == LOGBUFFERLINES) {
+                            //write buffer to database
+                            char logbuff[MAXLEN * LOGBUFFERLINES];
+                            int len = 0;
+                            for(i = 0; i < LOGBUFFERLINES; i++) {
+                                len += sprintf(logbuff + len, "%s\n", helpnode->log[i]);
+                                free(helpnode->log[i]);
+                                helpnode->log[i] = NULL;
+                            }
+                            printf_long_mysql_query(1024 + len, "UPDATE `helpserv_requests` SET `log` = CONCAT(`log`, '%s') WHERE `id` = %d", escape_string(logbuff), helpnode->suppid);
+                        }
+                        break;
+                    }
+                }
+            }
+        }
+    }
+}
+
 static void destroy_support_request(struct ClientSocket *client, struct NeonHelpNode *helpnode, int do_reply) {
-    printf_mysql_query("UPDATE `helpserv_requests` SET `status` = '2' WHERE `id` = %d", helpnode->suppid);
+    //write buffer to database
+    char logbuff[MAXLEN * LOGBUFFERLINES];
+    int len = 0;
+    int i;
+    if(helpnode->log) {
+        for(i = 0; i < LOGBUFFERLINES; i++) {
+            if(!helpnode->log[i]) break;
+            len += sprintf(logbuff + len, "%s\n", helpnode->log[i]);
+            free(helpnode->log[i]);
+            helpnode->log[i] = NULL;
+        }
+        free(helpnode->log);
+    }
+    printf_long_mysql_query(1024 + len, "UPDATE `helpserv_requests` SET `status`='2', `log` = CONCAT(`log`, '%s') WHERE `id` = %d", escape_string(logbuff), helpnode->suppid);
     if(do_reply) {
         reply(client, helpnode->user, "NH_DELETED", helpnode->suppid);
     }
@@ -387,6 +444,7 @@ void init_NeonHelp() {
     //register events
     bind_bot_ready(neonhelp_bot_ready);
     bind_privmsg(neonhelp_event_privmsg);
+    bind_chanmsg(neonhelp_event_chanmsg);
     bind_part(neonhelp_event_part);
     bind_kick(neonhelp_event_kick);
     bind_quit(neonhelp_event_quit);