From 7c5dd19f4e89530ce6d7825252aed4e9c57e8505 Mon Sep 17 00:00:00 2001 From: pk910 Date: Fri, 6 Jan 2012 23:10:01 +0100 Subject: [PATCH] added some more helpserv functions --- Makefile.am | 1 + database.sql | 1 + database.upgrade.sql | 6 +- src/bot_NeonHelp.c | 62 ++++++++++++++++++++- src/bot_NeonHelp.h | 7 +++ src/cmd_neonhelp.h | 2 + src/cmd_neonhelp_requests.c | 107 ++++++++++++++++++++++++++++++++++++ src/commands.c | 1 + src/mysqlConn.c | 2 +- 9 files changed, 185 insertions(+), 4 deletions(-) create mode 100644 src/cmd_neonhelp_requests.c diff --git a/Makefile.am b/Makefile.am index 3403d4a..dee97f3 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 diff --git a/database.sql b/database.sql index 718b058..5c35810 100644 --- a/database.sql +++ b/database.sql @@ -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, diff --git a/database.upgrade.sql b/database.upgrade.sql index 7051a44..8b2fa16 100644 --- a/database.upgrade.sql +++ b/database.upgrade.sql @@ -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 diff --git a/src/bot_NeonHelp.c b/src/bot_NeonHelp.c index 292344d..19a0e9f 100644 --- a/src/bot_NeonHelp.c +++ b/src/bot_NeonHelp.c @@ -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); diff --git a/src/bot_NeonHelp.h b/src/bot_NeonHelp.h index 52d6389..a29086c 100644 --- a/src/bot_NeonHelp.h +++ b/src/bot_NeonHelp.h @@ -23,11 +23,18 @@ #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(); diff --git a/src/cmd_neonhelp.h b/src/cmd_neonhelp.h index 5993886..d52c96c 100644 --- a/src/cmd_neonhelp.h +++ b/src/cmd_neonhelp.h @@ -27,8 +27,10 @@ #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 index 0000000..5c23f8c --- /dev/null +++ b/src/cmd_neonhelp_requests.c @@ -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 . + */ + +#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"); +} diff --git a/src/commands.c b/src/commands.c index fcfe78b..79620bd 100644 --- a/src/commands.c +++ b/src/commands.c @@ -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 diff --git a/src/mysqlConn.c b/src/mysqlConn.c index 88d63be..1275f59 100644 --- a/src/mysqlConn.c +++ b/src/mysqlConn.c @@ -16,7 +16,7 @@ */ #include "mysqlConn.h" -#define DATABASE_VERSION "11" +#define DATABASE_VERSION "12" struct used_result { MYSQL_RES *result; -- 2.20.1