added NeonHelp history command
[NeonServV5.git] / src / modules / NeonHelp.mod / cmd_neonhelp_history.c
diff --git a/src/modules/NeonHelp.mod/cmd_neonhelp_history.c b/src/modules/NeonHelp.mod/cmd_neonhelp_history.c
new file mode 100644 (file)
index 0000000..2378d5a
--- /dev/null
@@ -0,0 +1,136 @@
+/* cmd_neonhelp_history.c - NeonServ v5.4
+ * 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"
+
+/*
+* argv[0]   matching string
+*/
+
+CMD_BIND(neonhelp_cmd_history) {
+    //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(textclient, user, "MODCMD_ACCESS_DENIED");
+        return;
+    }
+    struct Table *table;
+    char *match_str = escape_string(argv[0]);
+    printf_mysql_query("SELECT `id`, `nick`, `hand`, `host`, `time`, `text`, `status`, `supporter`, `users`.`user_user`, MD5(CONCAT(`id`, `host`, `time`, `supporter`)) FROM `helpserv_requests` LEFT JOIN `users` ON `supporter` = `user_id` WHERE `botid` = '%d' AND (`host` LIKE '%%%s%%' OR `hand` LIKE '%%%s%%' OR `nick` LIKE '%%%s%%') ORDER BY `time` ASC", client->clientid, match_str, match_str, match_str);
+    res = mysql_use();
+    table = table_init(6, mysql_num_rows(res)*3 + 1, 0);
+    table->col_flags[1] |= TABLE_FLAG_COL_SKIP_NULL;
+    table->col_flags[2] |= TABLE_FLAG_COL_SKIP_NULL;
+    table->col_flags[3] |= TABLE_FLAG_COL_SKIP_NULL;
+    table->col_flags[4] |= TABLE_FLAG_COL_SKIP_NULL;
+    char *content[6];
+    content[0] = get_language_string(user, "NH_REQUESTS_HEADER_ID");
+    content[1] = get_language_string(user, "NH_REQUESTS_HEADER_NICK");
+    content[2] = get_language_string(user, "NH_REQUESTS_HEADER_AUTH");
+    content[3] = get_language_string(user, "NH_REQUESTS_HEADER_MASK");
+    content[4] = get_language_string(user, "NH_REQUESTS_HEADER_TIME");
+    content[5] = get_language_string(user, "NH_REQUESTS_HEADER_STATUS");
+    table_add(table, content);
+    char timestr[MAXLEN];
+    time_t rawtime;
+    struct tm *timeinfo;
+    char statestr[MAXLEN];
+    while ((row = mysql_fetch_row(res)) != NULL) {
+        content[0] = row[0];
+        content[1] = row[1];
+        content[2] = row[2];
+        content[3] = row[3];
+        rawtime = atoi(row[4]);
+        timeinfo = localtime(&rawtime);
+        strftime(timestr, MAXLEN, "%d.%m.%Y %H:%M:%S", timeinfo);
+        content[4] = timestr;
+        switch(atoi(row[6])) {
+        case 0:
+            content[5] = get_language_string(user, "NH_REQUESTS_STATE_PENDING");
+            break;
+        case 1:
+            sprintf(statestr, "%s (%s)", get_language_string(user, "NH_REQUESTS_STATE_ACTIVE"), row[8]);
+            content[5] = statestr;
+            break;
+        case 2:
+            sprintf(statestr, "%s (%s)", get_language_string(user, "NH_REQUESTS_STATE_CLOSED"), (strcmp(row[7], "0") ? row[8] : "*"));
+            content[5] = statestr;
+            break;
+        }
+        table_add(table, content);
+        char *p;
+        if((p = strstr(row[5], "\n")))
+            *p = '\0';
+        content[0] = " ";
+        content[1] = NULL;
+        content[2] = NULL;
+        content[3] = NULL;
+        content[4] = NULL;
+        sprintf(statestr, "\00314\002\002%s\003", row[5]);
+        content[5] = statestr;
+        table_add(table, content);
+        char setting[128];
+        sprintf(setting, "modules.%s.log_link", get_module_name(module_id));
+        if((p = get_string_field(setting))) {
+            statestr[0] = '\003';
+            statestr[1] = '1';
+            statestr[2] = '4';
+            int pos = sprintf(statestr+3, p, row[0], row[9]);
+            statestr[pos+3] = '\003';
+            statestr[pos+4] = '\0';
+            content[5] = statestr;
+            table_add(table, content);
+        }
+    }
+    //send the table
+    char **table_lines = table_end(table);
+    int i;
+    for(i = 0; i < table->entrys; i++) {
+        reply(textclient, user, table_lines[i]);
+    }
+    if(table->entrys == 1)
+        reply(textclient, user, "NS_TABLE_NONE");
+    table_free(table);
+}