fixes for multi thread support
[NeonServV5.git] / src / bot_NeonHelp.c
index 1ff01ccf2a30b7f267a1cba58e839d134747c06b..5e0e605076c9e3409f240ea6ef65536c7e9de80b 100644 (file)
@@ -18,6 +18,7 @@
 #include "bot_NeonHelp.h"
 #include "modcmd.h"
 #include "cmd_neonhelp.h"
+#include "lang.h"
 #include "mysqlConn.h"
 #include "ClientSocket.h"
 #include "UserNode.h"
@@ -27,6 +28,7 @@
 #include "IRCParser.h"
 #include "bots.h"
 #include "DBHelper.h"
+#include "WHOHandler.h"
 
 #define BOTID 4
 #define BOTALIAS "NeonHelp"
@@ -38,8 +40,8 @@ static const struct default_language_entry msgtab[] = {
     {"NH_REQUEST_OTHERS_0", "There are no other unhandled requests."},
     {"NH_REQUEST_OTHERS_1", "There is 1 other unhandled request."},
     {"NH_REQUEST_OTHERS_2", "There are %d other unhandled requests."}, /* {ARGS: 1337} */
-    {"NH_REQUEST_FOOTER_1", "Everything you tell me until you are helped (or you leave %s) will be recorded. If you part %s, your request will be lost."}, /* {ARGS: "#test"} */
-    {"NH_REQUEST_FOOTER_2", "Everything you tell me until you are helped (or you leave %s or %s) will be recorded. If you part %s or %s, your request will be lost."}, /* {ARGS: "#test", "#test-support"} */
+    {"NH_REQUEST_FOOTER_1", "Everything you tell me until you are helped (or you leave %1$s) will be recorded. If you part %1$s, your request will be lost."}, /* {ARGS: "#test"} */
+    {"NH_REQUEST_FOOTER_2", "Everything you tell me until you are helped (or you leave %1$s or %2$s) will be recorded. If you part %1$s or %2$s, your request will be lost."}, /* {ARGS: "#test", "#test-support"} */
     {"NH_NEW_REQUEST", "New request #%d by %s: %s"}, /* {ARGS: 5, "pk910", "Help, I've fallen and I can't get up!"} */
     {"NH_NEXT_NONE", "No more requests."},
     {"NH_NEXT_NOT_FOUND", "No request found."},
@@ -48,6 +50,16 @@ static const struct default_language_entry msgtab[] = {
     {"NH_NEXT_JOIN", "Please /join %s now."}, /* {ARGS: "#test-support"} */
     {"NH_DELETED", "Your request ID#%d has been deleted."}, /* {ARGS: 5} */
     {"NH_DELETED_STAFF", "Request deleted: #%d (%s)"}, /* {ARGS: 5, "pk910"} */
+    {"NH_REMIND_OPEN_REQUESTS_1", "There is %d unhandled request!"}, /* {ARGS: 1} */
+    {"NH_REMIND_OPEN_REQUESTS_2", "There are %d unhandled requests!"}, /* {ARGS: 4} */
+    {"NH_REQUESTS_HEADER_ID", "ID"},
+    {"NH_REQUESTS_HEADER_STATUS", "State"},
+    {"NH_REQUESTS_HEADER_NICK", "Nick"},
+    {"NH_REQUESTS_HEADER_TIME", "Time"},
+    {"NH_REQUESTS_HEADER_REQUEST", "Question"},
+    {"NH_REQUESTS_STATE_ACTIVE", "active"},
+    {"NH_REQUESTS_STATE_PENDING", "pending"},
+    {"NH_REQUESTS_STATE_ERROR", "ERROR"},
     {NULL, NULL}
 };
 
@@ -79,7 +91,10 @@ static void neonhelp_trigger_callback(int clientid, struct ChanNode *chan, char
     }
     printf_mysql_query("SELECT `trigger`, `defaulttrigger` FROM `bot_channels` LEFT JOIN `bots` ON `botid` = `bots`.`id` WHERE `chanid` = '%d' AND `botclass` = '%d'", chan->channel_id, BOTID);
     res = mysql_use();
-    row = mysql_fetch_row(res);
+    if(!(row = mysql_fetch_row(res))) {
+        strcpy(trigger, "!");
+        return;
+    }
     if(row[0] && *row[0])
         strcpy(trigger, row[0]);
     else
@@ -99,9 +114,12 @@ static void start_bots() {
         client->flags |= (strcmp(row[6], "0") ? SOCKET_FLAG_PREFERRED : 0);
         client->flags |= (strcmp(row[8], "0") ? SOCKET_FLAG_USE_QUEUE : 0);
         client->flags |= (strcmp(row[9], "0") ? SOCKET_FLAG_SSL : 0);
+        client->flags |= SOCKET_FLAG_SILENT;
         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);
@@ -161,10 +179,12 @@ static USERAUTH_CALLBACK(neonhelp_event_privmsg_nick_lookup) {
     free(cache);
 }
 
+static TIMEQ_CALLBACK(neonhelp_remind_open_requests);
+
 static void neonhelp_event_privmsg_async(struct ClientSocket *client, struct UserNode *user, struct UserNode *target, char *message) {
     MYSQL_RES *res;
     MYSQL_ROW row, row2;
-    printf_mysql_query("SELECT `helpserv_support`, `helpserv_public`, `helpserv_intern` FROM `helpserv_settings` WHERE `helpserv_botid` = '%d'", client->clientid);
+    printf_mysql_query("SELECT `helpserv_support`, `helpserv_public`, `helpserv_intern`, `helpserv_intern_announce` FROM `helpserv_settings` WHERE `helpserv_botid` = '%d'", client->clientid);
     res = mysql_use();
     if (!(row = mysql_fetch_row(res))) return;
     //check if the user is a supporter (access in the support channel)
@@ -230,9 +250,21 @@ 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));
-    helpnode->suppid = (int) mysql_insert_id(mysql_conn);
+    helpnode->announce = (row[2] && strcmp(row[3], "0") ? 1 : 0);
+    if(helpnode->announce) {
+        char nameBuf[30];
+        sprintf(nameBuf, "neonhelp_%d", client->clientid);
+        if(!timeq_name_exists(nameBuf)) {
+            int *cidptr = malloc(sizeof(int));
+            *cidptr = client->clientid;
+            timeq_add_name(nameBuf, 300, neonhelp_remind_open_requests, cidptr);
+        }
+    }
+    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(get_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;
@@ -267,9 +299,105 @@ static void neonhelp_event_privmsg_async(struct ClientSocket *client, struct Use
     }
 }
 
-static void destroy_support_request(struct ClientSocket *client, struct NeonHelpNode *helpnode, int reply) {
-    printf_mysql_query("UPDATE `helpserv_requests` SET `status` = '2' WHERE `id` = %d", helpnode->suppid);
-    if(reply) {
+static TIMEQ_CALLBACK(neonhelp_remind_open_requests) {
+    int clientid = *((int*)data);
+    MYSQL_RES *res;
+    MYSQL_ROW row;
+    printf_mysql_query("SELECT `helpserv_support`, `helpserv_public`, `helpserv_intern`, `helpserv_intern_announce` FROM `helpserv_settings` WHERE `helpserv_botid` = '%d'", clientid);
+    res = mysql_use();
+    if (!(row = mysql_fetch_row(res)) || !row[2]) {
+        free(data);
+        return;
+    }
+    struct ClientSocket *client;
+    for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
+        if(client->clientid == clientid)
+            break;
+    }
+    if(!client) {
+        free(data);
+        return;
+    }
+    //count open requests
+    int requests = 0;
+    struct NeonHelpNode *helpnode;
+    if(client->flags & SOCKET_HAVE_HELPNODE) {
+        for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
+            if(helpnode->status == 0) {
+                requests++;
+            }
+        }
+    }
+    if(requests) {
+        char nameBuf[30];
+        sprintf(nameBuf, "neonhelp_%d", client->clientid);
+        if(!timeq_name_exists(nameBuf)) {
+            timeq_add_name(nameBuf, 300, neonhelp_remind_open_requests, data);
+        }
+        char replybuf[MAXLEN];
+        build_language_string(NULL, replybuf, (requests == 1 ? "NH_REMIND_OPEN_REQUESTS_1" : "NH_REMIND_OPEN_REQUESTS_2"), requests);
+        putsock(client, "PRIVMSG %s :%s", row[2], replybuf);
+    } else
+        free(data);
+}
+
+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) {
+    //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);
+    } else
+        logbuff[0] = '\0';
+    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);
     }
     free(helpnode);
@@ -351,9 +479,6 @@ static void neonhelp_event_part(struct ChanUser *target, char *reason) {
 
 static void neonhelp_event_quit(struct UserNode *target, char *reason) {
     struct ClientSocket *client;
-    MYSQL_RES *res;
-    MYSQL_ROW row;
-    struct ChanNode *support, *public;
     int userHasRequest;
     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
         if(client->botid == BOTID) {
@@ -387,6 +512,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);