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