added some more helpserv functions
authorpk910 <philipp@zoelle1.de>
Fri, 6 Jan 2012 22:10:01 +0000 (23:10 +0100)
committerpk910 <philipp@zoelle1.de>
Fri, 6 Jan 2012 22:13:07 +0000 (23:13 +0100)
Makefile.am
database.sql
database.upgrade.sql
src/bot_NeonHelp.c
src/bot_NeonHelp.h
src/cmd_neonhelp.h
src/cmd_neonhelp_requests.c [new file with mode: 0644]
src/commands.c
src/mysqlConn.c

index 3403d4a8da0b08be883576a66cadfe6b1020c06c..dee97f345ffd22a5642c210dd35f4d233384ab77 100644 (file)
@@ -127,6 +127,7 @@ neonserv_SOURCES = src/version.c \
       src/cmd_global_modcmd.c \
       src/cmd_neonhelp_next.c \
       src/cmd_neonhelp_delete.c \
+      src/cmd_neonhelp_requests.c \
       src/cmd_funcmds.c \
       src/ConfigParser.c
 
index 718b05877b0e6523a3a91a518c816949682315cd..5c35810c87bf11d8a9d09a1c9af3c62a5f626577 100644 (file)
@@ -369,6 +369,7 @@ UNIQUE (
 
 CREATE TABLE IF NOT EXISTS `helpserv_requests` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
+  `botid` INT(11) NOT NULL,
   `host` varchar(200) NOT NULL,
   `hand` varchar(50) NOT NULL,
   `nick` varchar(50) NOT NULL,
index 7051a441f05fa12b9bfba4fd6b20632cbb6bca5e..8b2fa16521082fe17e8ea23f0bfdb41ebbee3930 100644 (file)
@@ -118,4 +118,8 @@ CREATE TABLE IF NOT EXISTS `helpserv_settings` (
   PRIMARY KEY (`helpserv_botid`)
 ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
 
--- version: 11
\ No newline at end of file
+-- version: 11
+
+ALTER TABLE `helpserv_requests` ADD `botid` INT( 11 ) NOT NULL AFTER `id`;
+
+-- version: 12
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);
index 52d63891e88bbf637c48110a750f8cb653ba3cc9..a29086c8bf445a2a60031000a3af1a246f86fda9 100644 (file)
 #define botclass_helpnode botclassvalue1 
 #define SOCKET_HAVE_HELPNODE SOCKET_HAVE_BOTCLASSVALUE1
 
+#define LOGBUFFERLINES 20
+
+struct UserNode;
+struct ChanNode;
+
 struct NeonHelpNode {
     struct UserNode *user;
     int suppid;
     char status;
     struct NeonHelpNode *next;
+    struct ChanNode *logchan;
+    char **log;
 };
 
 void init_NeonHelp();
index 5993886e534e78ec1356ad0856358f0f6ba62acf..d52c96c73ece03783e5a94226e28717947cc4df4 100644 (file)
 #include "IRCParser.h"
 #include "bot_NeonHelp.h"
 #include "lang.h"
+#include "tools.h"
 
 CMD_BIND(neonhelp_cmd_delete);
 CMD_BIND(neonhelp_cmd_next);
+CMD_BIND(neonhelp_cmd_requests);
 
 #endif
\ No newline at end of file
diff --git a/src/cmd_neonhelp_requests.c b/src/cmd_neonhelp_requests.c
new file mode 100644 (file)
index 0000000..5c23f8c
--- /dev/null
@@ -0,0 +1,107 @@
+/* cmd_neonhelp_requests.c - NeonServ v5.3
+ * Copyright (C) 2011-2012  Philipp Kreil (pk910)
+ * 
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. 
+ */
+
+#include "cmd_neonhelp.h"
+
+/*
+* no args
+*/
+
+CMD_BIND(neonhelp_cmd_requests) {
+    //check permissions
+    MYSQL_RES *res;
+    MYSQL_ROW row, row2;
+    int caccess = 0;
+    int userid;
+    printf_mysql_query("SELECT `helpserv_support`, `helpserv_public`, `helpserv_intern` 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)
+    if((user->flags & USERFLAG_ISAUTHED)) {
+        if(user->flags & USERFLAG_HAS_USERID)
+            userid = user->user_id;
+        else {
+            printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
+            res = mysql_use();
+            if ((row2 = mysql_fetch_row(res)) != NULL) {
+                userid = atoi(row2[0]);
+                user->user_id = userid;
+                user->flags |= USERFLAG_HAS_USERID;
+            } else
+                userid = 0;
+        }
+        printf_mysql_query("SELECT `chanuser_access`, `chanuser_flags` FROM `chanusers` LEFT JOIN `channels` ON `chanuser_cid` = `channel_id` WHERE `chanuser_uid` = '%d' AND `channel_name` = '%s'", userid, escape_string(row[0]));
+        res = mysql_use();
+        if ((row2 = mysql_fetch_row(res)) != NULL) {
+            int cflags = atoi(row2[1]);
+            if(!(cflags & DB_CHANUSER_SUSPENDED))
+                caccess = atoi(row2[0]);
+        }
+    }
+    if(!caccess) {
+        reply(getTextBot(), user, "MODCMD_ACCESS_DENIED");
+        return;
+    }
+    struct Table *table;
+    printf_mysql_query("SELECT `id`, `time`, `text` FROM `helpserv_requests` WHERE `botid` = '%d' ORDER BY `time` DESC", client->clientid);
+    res = mysql_use();
+    table = table_init(5, mysql_num_rows(res) + 1, 0);
+    char *content[5];
+    content[0] = get_language_string(user, "NH_REQUESTS_HEADER_ID");
+    content[1] = get_language_string(user, "NH_REQUESTS_HEADER_STATUS");
+    content[2] = get_language_string(user, "NH_REQUESTS_HEADER_NICK");
+    content[3] = get_language_string(user, "NH_REQUESTS_HEADER_TIME");
+    content[4] = get_language_string(user, "NH_REQUESTS_HEADER_REQUEST");
+    table_add(table, content);
+    char timestr[MAXLEN];
+    struct NeonHelpNode *helpnode;
+    int suppid;
+    while ((row = mysql_fetch_row(res)) != NULL) {
+        content[0] = row[0];
+        suppid = atoi(row[0]);
+        if(client->flags & SOCKET_HAVE_HELPNODE) {
+            for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
+                if(helpnode->suppid == suppid) {
+                    if(helpnode->status)
+                        content[1] = get_language_string(user, "NH_REQUESTS_STATE_ACTIVE");
+                    else
+                        content[1] = get_language_string(user, "NH_REQUESTS_STATE_PENDING");
+                    content[2] = helpnode->user->nick;
+                    break;
+                }
+            }
+        } else {
+            content[1] = get_language_string(user, "NH_REQUESTS_STATE_ERROR");
+            content[2] = NULL;
+        }
+        timeToStr(user, (time(0) - atoi(row[1])), 2, timestr);
+        content[3] = timestr;
+        char *p;
+        if((p = strstr(row[2], "\n")))
+            *p = '\0';
+        content[4] = row[2];
+        table_add(table, content);
+    }
+    //send the table
+    char **table_lines = table_end(table);
+    int i;
+    for(i = 0; i < table->entrys; i++) {
+        reply(getTextBot(), user, table_lines[i]);
+    }
+    if(table->entrys == 1)
+        reply(getTextBot(), user, "NS_TABLE_NONE");
+}
index fcfe78b852d86826d5fcc505c1ddff71483e330c..79620bdd38ab873653cddfa15e98667e34f60954 100644 (file)
@@ -162,6 +162,7 @@ void register_commands() {
     //               NAME              FUNCTION        PARAMS     PRIVS                FLAGS
     USER_COMMAND("next",         neonhelp_cmd_next,      0, NULL,                   CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH);
     USER_COMMAND("delete",       neonhelp_cmd_delete,    1, NULL,                   CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH);
+    USER_COMMAND("requests",     neonhelp_cmd_requests,  0, NULL,                   CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH);
     #undef USER_COMMAND
     
 }
\ No newline at end of file
index 88d63be09bea5ab38278f7294f4ac842ed2df7c7..1275f59d97cd980fe65b9abe9738c9aa822f28a7 100644 (file)
@@ -16,7 +16,7 @@
  */
 
 #include "mysqlConn.h"
-#define DATABASE_VERSION "11"
+#define DATABASE_VERSION "12"
 
 struct used_result {
     MYSQL_RES *result;