added NeonHelp history command
[NeonServV5.git] / src / modules / NeonHelp.mod / bot_NeonHelp.c
1 /* bot_NeonHelp.c - NeonServ v5.4
2  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17 #include "../module.h"
18 #include "../botid.h"
19
20 #include "bot_NeonHelp.h"
21 #include "../../modcmd.h"
22 #include "cmd_neonhelp.h"
23 #include "../../lang.h"
24 #include "../../mysqlConn.h"
25 #include "../../ClientSocket.h"
26 #include "../../UserNode.h"
27 #include "../../ChanNode.h"
28 #include "../../ChanUser.h"
29 #include "../../IRCEvents.h"
30 #include "../../IRCParser.h"
31 #include "../../bots.h"
32 #include "../../DBHelper.h"
33 #include "../../WHOHandler.h"
34
35 #define BOTID NEONHELP_BOTID
36 #define BOTALIAS "NeonHelp"
37
38 static const struct default_language_entry msgtab[] = {
39     {"NH_NOT_ON_CHAN_1", "You cannot open this request as you are not in %s."}, /* {ARGS: "#test"} */
40     {"NH_NOT_ON_CHAN_2", "You cannot open this request as you are not in %s or %s."}, /* {ARGS: "test", "#test-support"} */
41     {"NH_REQUEST_RECORDED", "Your message has been recorded and assigned request ID#%d A helper should contact you shortly."}, /* {ARGS: 5} */
42     {"NH_REQUEST_OTHERS_0", "There are no other unhandled requests."},
43     {"NH_REQUEST_OTHERS_1", "There is 1 other unhandled request."},
44     {"NH_REQUEST_OTHERS_2", "There are %d other unhandled requests."}, /* {ARGS: 1337} */
45     {"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"} */
46     {"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"} */
47     {"NH_NEW_REQUEST", "New request #%d by %s: %s"}, /* {ARGS: 5, "pk910", "Help, I've fallen and I can't get up!"} */
48     {"NH_NEXT_NONE", "No more requests."},
49     {"NH_NEXT_NOT_FOUND", "No request found."},
50     {"NH_NEXT_HEADER", "$bNext request: #%d %s$b"}, /* {ARGS: 5, "pk910"} */
51     {"NH_NEXT_HELPER", "Your helper for request ID#%d is %s (Current nick: %s)."}, /* {ARGS: 5, "pk910", "Skynet"} */
52     {"NH_NEXT_JOIN", "Please /join %s now."}, /* {ARGS: "#test-support"} */
53     {"NH_DELETED", "Your request ID#%d has been deleted."}, /* {ARGS: 5} */
54     {"NH_DELETED_STAFF", "Request deleted: #%d (%s)"}, /* {ARGS: 5, "pk910"} */
55     {"NH_REMIND_OPEN_REQUESTS_1", "There is %d unhandled request!"}, /* {ARGS: 1} */
56     {"NH_REMIND_OPEN_REQUESTS_2", "There are %d unhandled requests!"}, /* {ARGS: 4} */
57     {"NH_REQUESTS_HEADER_ID", "ID"},
58     {"NH_REQUESTS_HEADER_STATUS", "State"},
59     {"NH_REQUESTS_HEADER_NICK", "Nick"},
60     {"NH_REQUESTS_HEADER_TIME", "Time"},
61     {"NH_REQUESTS_HEADER_STATUS", "State"},
62     {"NH_REQUESTS_HEADER_AUTH", "Auth"},
63     {"NH_REQUESTS_HEADER_MASK", "Mask"},
64     {"NH_REQUESTS_HEADER_REQUEST", "Question"},
65     {"NH_REQUESTS_STATE_ACTIVE", "active"},
66     {"NH_REQUESTS_STATE_PENDING", "pending"},
67     {"NH_REQUESTS_STATE_CLOSED", "closed"},
68     {"NH_REQUESTS_STATE_ERROR", "ERROR"},
69     {"NH_STATS_HEADER_USER", "User"},
70     {"NH_STATS_HEADER_LAST_24H", "last 24h"},
71     {"NH_STATS_HEADER_LAST_7DAY", "last 7d"},
72     {"NH_STATS_HEADER_LAST_30DAY", "last 30d"},
73     {NULL, NULL}
74 };
75
76 static void neonhelp_bot_ready(struct ClientSocket *client) {
77     if(client->botid != BOTID)
78         return;
79     MYSQL_RES *res;
80     MYSQL_ROW row;
81     
82     printf_mysql_query("SELECT `automodes`, `oper_user`, `oper_pass` FROM `bots` WHERE `id` = '%d'", client->clientid);
83     res = mysql_use();
84     if ((row = mysql_fetch_row(res)) != NULL) {
85         if(row[1] && row[2]) {
86             putsock(client, "OPER %s %s", row[1], row[2]);
87         }
88         putsock(client, "MODE %s +%s", client->user->nick, row[0]);
89     }
90     
91     printf_mysql_query("SELECT `channel_name`, `channel_key` FROM `bot_channels` LEFT JOIN `channels` ON `chanid` = `channel_id` WHERE `botid` = '%d' AND `suspended` = '0'", client->clientid);
92     res = mysql_use();
93     
94     while ((row = mysql_fetch_row(res)) != NULL) {
95         putsock(client, "JOIN %s %s", row[0], row[1]);
96     }
97 }
98
99 static void neonhelp_trigger_callback(int clientid, struct ChanNode *chan, char *trigger) {
100     MYSQL_RES *res;
101     MYSQL_ROW row;
102     loadChannelSettings(chan);
103     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
104         strcpy(trigger, "!");
105         return;
106     }
107     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);
108     res = mysql_use();
109     if(!(row = mysql_fetch_row(res))) {
110         strcpy(trigger, "!");
111         return;
112     }
113     if(row[0] && *row[0])
114         strcpy(trigger, row[0]);
115     else
116         strcpy(trigger, ((row[1] && *row[1]) ? row[1] : "!"));
117 }
118
119 static void start_bots(int type) {
120     struct ClientSocket *client;
121     MYSQL_RES *res, *res2;
122     MYSQL_ROW row;
123     
124     if(type == MODSTATE_STARTSTOP) {
125         printf_mysql_query("SELECT `nick`, `ident`, `realname`, `server`, `port`, `pass`, `textbot`, `id`, `queue`, `ssl`, `bind` FROM `bots` WHERE `botclass` = '%d' AND `active` = '1'", BOTID);
126         res = mysql_use();
127         
128         while ((row = mysql_fetch_row(res)) != NULL) {
129             client = create_socket(row[3], atoi(row[4]), row[10], row[5], row[0], row[1], row[2]);
130             client->flags |= (strcmp(row[6], "0") ? SOCKET_FLAG_PREFERRED : 0);
131             client->flags |= (strcmp(row[8], "0") ? SOCKET_FLAG_USE_QUEUE : 0);
132             client->flags |= (strcmp(row[9], "0") ? SOCKET_FLAG_SSL : 0);
133             client->flags |= SOCKET_FLAG_SILENT;
134             client->flags |= SOCKET_FLAG_REQUEST_INVITE | SOCKET_FLAG_REQUEST_OP;
135             client->botid = BOTID;
136             client->clientid = atoi(row[7]);
137             connect_socket(client);
138             //close old, still opened requests
139             printf_mysql_query("UPDATE `helpserv_requests` SET `status` = '2' WHERE `botid` = '%d'", client->clientid);
140         }
141     }
142     
143     printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access`, `flags` FROM `bot_binds` WHERE `botclass` = '%d'", BOTID);
144     res2 = mysql_use();
145     while ((row = mysql_fetch_row(res2)) != NULL) {
146         if(bind_cmd_to_command(BOTID, row[0], row[1])) {
147             if(row[2] && strcmp(row[2], "")) {
148                 bind_set_parameters(BOTID, row[0], row[2]);
149             }
150             if(row[3]) {
151                 bind_set_global_access(BOTID, row[0], atoi(row[3]));
152             }
153             if(row[4]) {
154                 bind_set_channel_access(BOTID, row[0], row[4]);
155             }
156             if(strcmp(row[5], "0"))
157                 bind_set_bind_flags(BOTID, row[0], atoi(row[5]));
158         }
159     }
160     bind_unbound_required_functions(BOTID);
161 }
162
163 static void neonhelp_event_privmsg_async(struct ClientSocket *client, struct UserNode *user, struct UserNode *target, char *message);
164 static USERAUTH_CALLBACK(neonhelp_event_privmsg_nick_lookup);
165 struct neonhelp_event_privmsg_cache {
166     struct ClientSocket *client;
167     struct UserNode *user, *target;
168     char *message;
169 };
170
171 static void neonhelp_event_privmsg(struct UserNode *user, struct UserNode *target, char *message) {
172     struct ClientSocket *client;
173     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
174         if(client->user == target) {
175             if(client->botid != BOTID) return;
176             break;
177         }
178     }
179     if(!client) return; //we got the message but we have no client that could receive it???
180     if(user->flags & USERFLAG_ISAUTHED) {
181         neonhelp_event_privmsg_async(client, user, target, message);
182     } else {
183         struct neonhelp_event_privmsg_cache *cache = malloc(sizeof(*cache));
184         if(!cache) return;
185         cache->client = client;
186         cache->user = user;
187         cache->target = target;
188         cache->message = strdup(message);
189         get_userauth(user, module_id, neonhelp_event_privmsg_nick_lookup, cache);
190     }
191 }
192
193 static USERAUTH_CALLBACK(neonhelp_event_privmsg_nick_lookup) {
194     struct neonhelp_event_privmsg_cache *cache = data;
195     neonhelp_event_privmsg_async(cache->client, cache->user, cache->target, cache->message);
196     free(cache->message);
197     free(cache);
198 }
199
200 static TIMEQ_CALLBACK(neonhelp_remind_open_requests);
201
202 static void neonhelp_event_privmsg_async(struct ClientSocket *client, struct UserNode *user, struct UserNode *target, char *message) {
203     MYSQL_RES *res;
204     MYSQL_ROW row, row2;
205     printf_mysql_query("SELECT `helpserv_support`, `helpserv_public`, `helpserv_intern`, `helpserv_intern_announce` FROM `helpserv_settings` WHERE `helpserv_botid` = '%d'", client->clientid);
206     res = mysql_use();
207     if (!(row = mysql_fetch_row(res))) return;
208     //check if the user is a supporter (access in the support channel)
209     if((user->flags & USERFLAG_ISAUTHED)) {
210         int caccess = 0;
211         int userid;
212         if(user->flags & USERFLAG_HAS_USERID)
213             userid = user->user_id;
214         else {
215             printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
216             res = mysql_use();
217             if ((row2 = mysql_fetch_row(res)) != NULL) {
218                 userid = atoi(row2[0]);
219                 user->user_id = userid;
220                 user->flags |= USERFLAG_HAS_USERID;
221             } else
222                 userid = 0;
223         }
224         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]));
225         res = mysql_use();
226         if ((row2 = mysql_fetch_row(res)) != NULL) {
227             int cflags = atoi(row2[1]);
228             if(!(cflags & DB_CHANUSER_SUSPENDED))
229                 caccess = atoi(row2[0]);
230         }
231         if(caccess) return; //ignore messages from supporters
232     }
233     //check if the user is in one of the bot's channels
234     struct ChanUser *chanuser;
235     struct ChanNode *chan;
236     for(chanuser = getUserChannels(target, NULL); chanuser; chanuser = getUserChannels(target, chanuser)) {
237         chan = chanuser->chan;
238         if((!stricmp(chan->name, row[0]) || (row[1] && !stricmp(chan->name, row[1]))) && isUserOnChan(user, chan))
239             break;
240     }
241     if(!chanuser) {
242         if(row[1])
243             reply(client, user, "NH_NOT_ON_CHAN_2", row[0], row[1]);
244         else
245             reply(client, user, "NH_NOT_ON_CHAN_1", row[0]);
246         return;
247     }
248     //check if there is already a support request
249     int others = 0;
250     if(client->flags & SOCKET_HAVE_HELPNODE) {
251         struct NeonHelpNode *helpnode;
252         for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
253             if(helpnode->user == user) {
254                 //simply append the message to the database
255                 printf_mysql_query("SELECT `text` FROM `helpserv_requests` WHERE `id` = %d", helpnode->suppid);
256                 res = mysql_use();
257                 if ((row2 = mysql_fetch_row(res)) != NULL) {
258                     char *old_msg = escape_string(row2[0]);
259                     char *new_msg = escape_string(message);
260                     printf_long_mysql_query(1024 + strlen(old_msg) + strlen(new_msg), "UPDATE `helpserv_requests` SET `text` = '%s\n%s' WHERE `id` = %d", old_msg, new_msg, helpnode->suppid);
261                 }
262                 return;
263             }
264             others++;
265         }
266     }
267     //add new request
268     struct NeonHelpNode *helpnode = malloc(sizeof(*helpnode));
269     if(!helpnode) return;
270     helpnode->user = user;
271     helpnode->logchan = getChanByName(row[0]);
272     helpnode->status = 0;
273     helpnode->announce = (row[2] && strcmp(row[3], "0") ? 1 : 0);
274     if(helpnode->announce) {
275         char nameBuf[30];
276         sprintf(nameBuf, "neonhelp_%d", client->clientid);
277         if(!timeq_name_exists(nameBuf)) {
278             int *cidptr = malloc(sizeof(int));
279             *cidptr = client->clientid;
280             timeq_add_name(nameBuf, 300, module_id, neonhelp_remind_open_requests, cidptr);
281         }
282     }
283     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));
284     helpnode->suppid = (int) mysql_insert_id(get_mysql_conn());
285     helpnode->log = NULL;
286     helpnode->next = ((client->flags & SOCKET_HAVE_HELPNODE) ? client->botclass_helpnode : NULL);
287     client->botclass_helpnode = helpnode;
288     client->flags |= SOCKET_HAVE_HELPNODE;
289     //build the user reply...
290     char user_reply[MAXLEN];
291     int user_reply_pos = 0;
292     char reply_buff[MAXLEN];
293     //1st part: NH_REQUEST_RECORDED
294     strcpy(user_reply + user_reply_pos, build_language_string(user, reply_buff, "NH_REQUEST_RECORDED", helpnode->suppid));
295     user_reply_pos += strlen(reply_buff);
296     //2nd part: NH_REQUEST_OTHERS_0 / NH_REQUEST_OTHERS_1 / NH_REQUEST_OTHERS_2
297     user_reply[user_reply_pos++] = ' ';
298     if(others <= 1)
299         strcpy(user_reply + user_reply_pos, build_language_string(user, reply_buff, (others ? "NH_REQUEST_OTHERS_1" : "NH_REQUEST_OTHERS_0")));
300     else
301         strcpy(user_reply + user_reply_pos, build_language_string(user, reply_buff, "NH_REQUEST_OTHERS_2", others));
302     user_reply_pos += strlen(reply_buff);
303     //3th part: NH_REQUEST_FOOTER_1 / NH_REQUEST_FOOTER_2
304     user_reply[user_reply_pos++] = ' ';
305     if(row[1])
306         strcpy(user_reply + user_reply_pos, build_language_string(user, reply_buff, "NH_REQUEST_FOOTER_2", row[0], row[1]));
307     else
308         strcpy(user_reply + user_reply_pos, build_language_string(user, reply_buff, "NH_REQUEST_FOOTER_1", row[0]));
309     user_reply_pos += strlen(reply_buff);
310     reply(client, user, "%s", user_reply);
311     //sent a message to the internal channel / onotice to supp channel
312     build_language_string(user, reply_buff, "NH_NEW_REQUEST", helpnode->suppid, user->nick, message);
313     if(row[2]) {
314         putsock(client, "PRIVMSG %s :%s", row[2], reply_buff);
315     } else {
316         putsock(client, "NOTICE @%s :%s", row[0], reply_buff);
317     }
318 }
319
320 static TIMEQ_CALLBACK(neonhelp_remind_open_requests) {
321     int clientid = *((int*)data);
322     MYSQL_RES *res;
323     MYSQL_ROW row;
324     printf_mysql_query("SELECT `helpserv_support`, `helpserv_public`, `helpserv_intern`, `helpserv_intern_announce` FROM `helpserv_settings` WHERE `helpserv_botid` = '%d'", clientid);
325     res = mysql_use();
326     if (!(row = mysql_fetch_row(res)) || !row[2]) {
327         free(data);
328         return;
329     }
330     struct ClientSocket *client;
331     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
332         if(client->clientid == clientid)
333             break;
334     }
335     if(!client) {
336         free(data);
337         return;
338     }
339     //count open requests
340     int requests = 0;
341     struct NeonHelpNode *helpnode;
342     if(client->flags & SOCKET_HAVE_HELPNODE) {
343         for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
344             if(helpnode->status == 0) {
345                 requests++;
346             }
347         }
348     }
349     if(requests) {
350         char nameBuf[30];
351         sprintf(nameBuf, "neonhelp_%d", client->clientid);
352         if(!timeq_name_exists(nameBuf)) {
353             timeq_add_name(nameBuf, 300, module_id, neonhelp_remind_open_requests, data);
354         }
355         char replybuf[MAXLEN];
356         build_language_string(NULL, replybuf, (requests == 1 ? "NH_REMIND_OPEN_REQUESTS_1" : "NH_REMIND_OPEN_REQUESTS_2"), requests);
357         putsock(client, "PRIVMSG %s :%s", row[2], replybuf);
358     } else
359         free(data);
360 }
361
362 static void neonhelp_event_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
363     char logline[MAXLEN];
364     sprintf(logline, "<%s> %s", user->nick, message);
365     struct ClientSocket *client;
366     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
367         if(client->botid == BOTID) {
368             struct NeonHelpNode *helpnode;
369             if(client->flags & SOCKET_HAVE_HELPNODE) {
370                 for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
371                     if(helpnode->logchan == chan && helpnode->status == 1) {
372                         if(!helpnode->log) {
373                             helpnode->log = calloc(LOGBUFFERLINES, sizeof(char*));
374                             if(!helpnode->log) return;
375                         }
376                         int i;
377                         for(i = 0; i < LOGBUFFERLINES; i++) {
378                             if(!helpnode->log[i]) {
379                                 helpnode->log[i] = strdup(logline);
380                                 break;
381                             }
382                         }
383                         if(i == LOGBUFFERLINES) {
384                             //write buffer to database
385                             char logbuff[MAXLEN * LOGBUFFERLINES];
386                             int len = 0;
387                             for(i = 0; i < LOGBUFFERLINES; i++) {
388                                 len += sprintf(logbuff + len, "%s\n", helpnode->log[i]);
389                                 free(helpnode->log[i]);
390                                 helpnode->log[i] = NULL;
391                             }
392                             printf_long_mysql_query(1024 + len, "UPDATE `helpserv_requests` SET `log` = CONCAT(`log`, '%s') WHERE `id` = %d", escape_string(logbuff), helpnode->suppid);
393                         }
394                         break;
395                     }
396                 }
397             }
398         }
399     }
400 }
401
402 static void destroy_support_request(struct ClientSocket *client, struct NeonHelpNode *helpnode, int do_reply) {
403     //write buffer to database
404     char logbuff[MAXLEN * LOGBUFFERLINES];
405     int len = 0;
406     int i;
407     if(helpnode->log) {
408         for(i = 0; i < LOGBUFFERLINES; i++) {
409             if(!helpnode->log[i]) break;
410             len += sprintf(logbuff + len, "%s\n", helpnode->log[i]);
411             free(helpnode->log[i]);
412             helpnode->log[i] = NULL;
413         }
414         free(helpnode->log);
415     } else
416         logbuff[0] = '\0';
417     printf_long_mysql_query(1024 + len, "UPDATE `helpserv_requests` SET `status`='2', `log` = CONCAT(`log`, '%s') WHERE `id` = %d", escape_string(logbuff), helpnode->suppid);
418     if(do_reply) {
419         reply(client, helpnode->user, "NH_DELETED", helpnode->suppid);
420     }
421     free(helpnode);
422 }
423
424 static void neonhelp_event_kick(struct UserNode *user, struct ChanUser *target, char *reason) {
425     struct ClientSocket *client;
426     MYSQL_RES *res;
427     MYSQL_ROW row;
428     struct ChanNode *support, *public;
429     int userHasRequest;
430     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
431         if(client->botid == BOTID && isUserOnChan(client->user, target->chan)) {
432             userHasRequest = 0;
433             struct NeonHelpNode *helpnode, *prev_helpnode = NULL;
434             if(client->flags & SOCKET_HAVE_HELPNODE) {
435                 for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
436                     if(helpnode->user == target->user) {
437                         userHasRequest = 1;
438                         break;
439                     } else
440                         prev_helpnode = helpnode;
441                 }
442             }
443             if(!userHasRequest) continue;
444             printf_mysql_query("SELECT `helpserv_support`, `helpserv_public`, `helpserv_intern` FROM `helpserv_settings` WHERE `helpserv_botid` = '%d'", client->clientid);
445             res = mysql_use();
446             if (!(row = mysql_fetch_row(res))) continue;
447             support = getChanByName(row[0]);
448             public = (row[1] ? getChanByName(row[1]) : NULL);
449             if(target->chan == support || !((support && isUserOnChan(target->user, support)) || (public && isUserOnChan(target->user, public)))) {
450                 //free the user's support request
451                 if(prev_helpnode)
452                     prev_helpnode->next = helpnode->next;
453                 else
454                     client->botclass_helpnode = helpnode->next;
455                 destroy_support_request(client, helpnode, 1);
456             }
457         }
458     }
459 }
460
461 static void neonhelp_event_part(struct ChanUser *target, char *reason) {
462     struct ClientSocket *client;
463     MYSQL_RES *res;
464     MYSQL_ROW row;
465     struct ChanNode *support, *public;
466     int userHasRequest;
467     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
468         if(client->botid == BOTID && isUserOnChan(client->user, target->chan)) {
469             userHasRequest = 0;
470             struct NeonHelpNode *helpnode, *prev_helpnode = NULL;
471             if(client->flags & SOCKET_HAVE_HELPNODE) {
472                 for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
473                     if(helpnode->user == target->user) {
474                         userHasRequest = 1;
475                         break;
476                     } else
477                         prev_helpnode = helpnode;
478                 }
479             }
480             if(!userHasRequest) continue;
481             printf_mysql_query("SELECT `helpserv_support`, `helpserv_public`, `helpserv_intern` FROM `helpserv_settings` WHERE `helpserv_botid` = '%d'", client->clientid);
482             res = mysql_use();
483             if (!(row = mysql_fetch_row(res))) continue;
484             support = getChanByName(row[0]);
485             public = (row[1] ? getChanByName(row[1]) : NULL);
486             if(target->chan == support || !((support && isUserOnChan(target->user, support)) || (public && isUserOnChan(target->user, public)))) {
487                 //free the user's support request
488                 if(prev_helpnode)
489                     prev_helpnode->next = helpnode->next;
490                 else
491                     client->botclass_helpnode = helpnode->next;
492                 destroy_support_request(client, helpnode, 1);
493             }
494         }
495     }
496 }
497
498 static void neonhelp_event_quit(struct UserNode *target, char *reason) {
499     struct ClientSocket *client;
500     int userHasRequest;
501     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
502         if(client->botid == BOTID) {
503             userHasRequest = 0;
504             struct NeonHelpNode *helpnode, *prev_helpnode = NULL;
505             if(client->flags & SOCKET_HAVE_HELPNODE) {
506                 for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
507                     if(helpnode->user == target) {
508                         userHasRequest = 1;
509                         break;
510                     } else
511                         prev_helpnode = helpnode;
512                 }
513             }
514             if(!userHasRequest) continue;
515             //free the user's support request
516             if(prev_helpnode)
517                 prev_helpnode->next = helpnode->next;
518             else
519                 client->botclass_helpnode = helpnode->next;
520             destroy_support_request(client, helpnode, 0);
521         }
522     }
523 }
524
525 static void neonhelp_event_invite(struct ClientSocket *client, struct UserNode *user, char *channel) {
526         if(client->botid != BOTID)
527                 return;
528     MYSQL_RES *res;
529     MYSQL_ROW row;
530     printf_mysql_query("SELECT `botid`, `bot_channels`.`id`, `suspended` FROM `bot_channels` LEFT JOIN `bots` ON `bot_channels`.`botid` = `bots`.`id` LEFT JOIN `channels` ON `chanid` = `channel_id` WHERE `channel_name` = '%s' AND `botclass` = '%d'", escape_string(channel), client->botid);
531     res = mysql_use();
532     if ((row = mysql_fetch_row(res)) == NULL) {
533         reply(client, user, "NS_INVITE_FAIL", channel, client->user->nick);
534         return;
535     }
536     if(!strcmp(row[2], "1")) {
537         reply(client, user, "MODCMD_CHAN_SUSPENDED");
538         return;
539     }
540     int botid = atoi(row[0]);
541     struct ClientSocket *bot;
542     for(bot = getBots(SOCKET_FLAG_READY, NULL); bot; bot = getBots(SOCKET_FLAG_READY, bot)) {
543         if(bot->clientid == botid)
544             break;
545     }
546     if(bot) {
547         struct ChanNode *chan = getChanByName(channel);
548         if(chan && isUserOnChan(bot->user, chan)) {
549             reply(client, user, "NS_INVITE_ON_CHAN", bot->user->nick, chan->name);
550         } else
551             putsock(bot, "JOIN %s", channel);
552     }
553 }
554
555 void init_NeonHelp(int type) {
556     set_bot_alias(BOTID, BOTALIAS);
557     start_bots(type);
558     
559     if(type == MODSTATE_REBIND) return;
560     
561     //register events
562     bind_bot_ready(neonhelp_bot_ready, module_id);
563     bind_privmsg(neonhelp_event_privmsg, module_id);
564     bind_chanmsg(neonhelp_event_chanmsg, module_id);
565     bind_part(neonhelp_event_part, module_id);
566     bind_kick(neonhelp_event_kick, module_id);
567     bind_quit(neonhelp_event_quit, module_id);
568         bind_invite(neonhelp_event_invite, module_id);
569     
570     set_trigger_callback(BOTID, module_id, neonhelp_trigger_callback);
571     
572     register_default_language_table(msgtab);
573 }
574
575 void loop_NeonHelp() {
576     
577 }
578
579 void free_NeonHelp(int type) {
580     unbind_allcmd(BOTID);
581     if(type == MODSTATE_STARTSTOP) {
582         //disconnect all our bots
583         struct ClientSocket *client;
584         for(client = getBots(0, NULL); client; client = getBots(0, client)) {
585             if(client->botid == BOTID) {
586                 unbind_botwise_allcmd(0, client->clientid);
587                 close_socket(client);
588                 break;
589             }
590         }
591     }
592 }
593
594 #undef BOTID
595 #undef BOTALIAS