added cmd_stats to NeonHelp
[NeonServV5.git] / src / 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() {
109     struct ClientSocket *client;
110     MYSQL_RES *res, *res2;
111     MYSQL_ROW row;
112     
113     printf_mysql_query("SELECT `nick`, `ident`, `realname`, `server`, `port`, `pass`, `textbot`, `id`, `queue`, `ssl`, `bind` FROM `bots` WHERE `botclass` = '%d' AND `active` = '1'", BOTID);
114     res = mysql_use();
115     
116     while ((row = mysql_fetch_row(res)) != NULL) {
117         client = create_socket(row[3], atoi(row[4]), row[10], row[5], row[0], row[1], row[2]);
118         client->flags |= (strcmp(row[6], "0") ? SOCKET_FLAG_PREFERRED : 0);
119         client->flags |= (strcmp(row[8], "0") ? SOCKET_FLAG_USE_QUEUE : 0);
120         client->flags |= (strcmp(row[9], "0") ? SOCKET_FLAG_SSL : 0);
121         client->flags |= SOCKET_FLAG_SILENT;
122         client->botid = BOTID;
123         client->clientid = atoi(row[7]);
124         connect_socket(client);
125         //close old, still opened requests
126         printf_mysql_query("UPDATE `helpserv_requests` SET `status` = '2' WHERE `botid` = '%d'", client->clientid);
127     }
128     
129     printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access`, `flags` FROM `bot_binds` WHERE `botclass` = '%d'", BOTID);
130     res2 = mysql_use();
131     while ((row = mysql_fetch_row(res2)) != NULL) {
132         if(bind_cmd_to_command(BOTID, row[0], row[1])) {
133             if(row[2] && strcmp(row[2], "")) {
134                 bind_set_parameters(BOTID, row[0], row[2]);
135             }
136             if(row[3]) {
137                 bind_set_global_access(BOTID, row[0], atoi(row[3]));
138             }
139             if(row[4]) {
140                 bind_set_channel_access(BOTID, row[0], row[4]);
141             }
142             if(strcmp(row[5], "0"))
143                 bind_set_bind_flags(BOTID, row[0], atoi(row[5]));
144         }
145     }
146     bind_unbound_required_functions(BOTID);
147 }
148
149 static void neonhelp_event_privmsg_async(struct ClientSocket *client, struct UserNode *user, struct UserNode *target, char *message);
150 static USERAUTH_CALLBACK(neonhelp_event_privmsg_nick_lookup);
151 struct neonhelp_event_privmsg_cache {
152     struct ClientSocket *client;
153     struct UserNode *user, *target;
154     char *message;
155 };
156
157 static void neonhelp_event_privmsg(struct UserNode *user, struct UserNode *target, char *message) {
158     struct ClientSocket *client;
159     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
160         if(client->user == target) {
161             if(client->botid != BOTID) return;
162             break;
163         }
164     }
165     if(!client) return; //we got the message but we have no client that could receive it???
166     if(user->flags & USERFLAG_ISAUTHED) {
167         neonhelp_event_privmsg_async(client, user, target, message);
168     } else {
169         struct neonhelp_event_privmsg_cache *cache = malloc(sizeof(*cache));
170         if(!cache) return;
171         cache->client = client;
172         cache->user = user;
173         cache->target = target;
174         cache->message = strdup(message);
175         get_userauth(user, neonhelp_event_privmsg_nick_lookup, cache);
176     }
177 }
178
179 static USERAUTH_CALLBACK(neonhelp_event_privmsg_nick_lookup) {
180     struct neonhelp_event_privmsg_cache *cache = data;
181     neonhelp_event_privmsg_async(cache->client, cache->user, cache->target, cache->message);
182     free(cache->message);
183     free(cache);
184 }
185
186 static TIMEQ_CALLBACK(neonhelp_remind_open_requests);
187
188 static void neonhelp_event_privmsg_async(struct ClientSocket *client, struct UserNode *user, struct UserNode *target, char *message) {
189     MYSQL_RES *res;
190     MYSQL_ROW row, row2;
191     printf_mysql_query("SELECT `helpserv_support`, `helpserv_public`, `helpserv_intern`, `helpserv_intern_announce` FROM `helpserv_settings` WHERE `helpserv_botid` = '%d'", client->clientid);
192     res = mysql_use();
193     if (!(row = mysql_fetch_row(res))) return;
194     //check if the user is a supporter (access in the support channel)
195     if((user->flags & USERFLAG_ISAUTHED)) {
196         int caccess = 0;
197         int userid;
198         if(user->flags & USERFLAG_HAS_USERID)
199             userid = user->user_id;
200         else {
201             printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
202             res = mysql_use();
203             if ((row2 = mysql_fetch_row(res)) != NULL) {
204                 userid = atoi(row2[0]);
205                 user->user_id = userid;
206                 user->flags |= USERFLAG_HAS_USERID;
207             } else
208                 userid = 0;
209         }
210         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]));
211         res = mysql_use();
212         if ((row2 = mysql_fetch_row(res)) != NULL) {
213             int cflags = atoi(row2[1]);
214             if(!(cflags & DB_CHANUSER_SUSPENDED))
215                 caccess = atoi(row2[0]);
216         }
217         if(caccess) return; //ignore messages from supporters
218     }
219     //check if the user is in one of the bot's channels
220     struct ChanUser *chanuser;
221     struct ChanNode *chan;
222     for(chanuser = getUserChannels(target, NULL); chanuser; chanuser = getUserChannels(target, chanuser)) {
223         chan = chanuser->chan;
224         if((!stricmp(chan->name, row[0]) || (row[1] && !stricmp(chan->name, row[1]))) && isUserOnChan(user, chan))
225             break;
226     }
227     if(!chanuser) {
228         if(row[1])
229             reply(client, user, "NH_NOT_ON_CHAN_2", row[0], row[1]);
230         else
231             reply(client, user, "NH_NOT_ON_CHAN_1", row[0]);
232         return;
233     }
234     //check if there is already a support request
235     int others = 0;
236     if(client->flags & SOCKET_HAVE_HELPNODE) {
237         struct NeonHelpNode *helpnode;
238         for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
239             if(helpnode->user == user) {
240                 //simply append the message to the database
241                 printf_mysql_query("SELECT `text` FROM `helpserv_requests` WHERE `id` = %d", helpnode->suppid);
242                 res = mysql_use();
243                 if ((row2 = mysql_fetch_row(res)) != NULL) {
244                     char *old_msg = escape_string(row2[0]);
245                     char *new_msg = escape_string(message);
246                     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);
247                 }
248                 return;
249             }
250             others++;
251         }
252     }
253     //add new request
254     struct NeonHelpNode *helpnode = malloc(sizeof(*helpnode));
255     if(!helpnode) return;
256     helpnode->user = user;
257     helpnode->logchan = getChanByName(row[0]);
258     helpnode->status = 0;
259     helpnode->announce = (row[2] && strcmp(row[3], "0") ? 1 : 0);
260     if(helpnode->announce) {
261         char nameBuf[30];
262         sprintf(nameBuf, "neonhelp_%d", client->clientid);
263         if(!timeq_name_exists(nameBuf)) {
264             int *cidptr = malloc(sizeof(int));
265             *cidptr = client->clientid;
266             timeq_add_name(nameBuf, 300, neonhelp_remind_open_requests, cidptr);
267         }
268     }
269     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));
270     helpnode->suppid = (int) mysql_insert_id(get_mysql_conn());
271     helpnode->log = NULL;
272     helpnode->next = ((client->flags & SOCKET_HAVE_HELPNODE) ? client->botclass_helpnode : NULL);
273     client->botclass_helpnode = helpnode;
274     client->flags |= SOCKET_HAVE_HELPNODE;
275     //build the user reply...
276     char user_reply[MAXLEN];
277     int user_reply_pos = 0;
278     char reply_buff[MAXLEN];
279     //1st part: NH_REQUEST_RECORDED
280     strcpy(user_reply + user_reply_pos, build_language_string(user, reply_buff, "NH_REQUEST_RECORDED", helpnode->suppid));
281     user_reply_pos += strlen(reply_buff);
282     //2nd part: NH_REQUEST_OTHERS_0 / NH_REQUEST_OTHERS_1 / NH_REQUEST_OTHERS_2
283     user_reply[user_reply_pos++] = ' ';
284     if(others <= 1)
285         strcpy(user_reply + user_reply_pos, build_language_string(user, reply_buff, (others ? "NH_REQUEST_OTHERS_1" : "NH_REQUEST_OTHERS_0")));
286     else
287         strcpy(user_reply + user_reply_pos, build_language_string(user, reply_buff, "NH_REQUEST_OTHERS_2", others));
288     user_reply_pos += strlen(reply_buff);
289     //3th part: NH_REQUEST_FOOTER_1 / NH_REQUEST_FOOTER_2
290     user_reply[user_reply_pos++] = ' ';
291     if(row[1])
292         strcpy(user_reply + user_reply_pos, build_language_string(user, reply_buff, "NH_REQUEST_FOOTER_2", row[0], row[1]));
293     else
294         strcpy(user_reply + user_reply_pos, build_language_string(user, reply_buff, "NH_REQUEST_FOOTER_1", row[0]));
295     user_reply_pos += strlen(reply_buff);
296     reply(client, user, "%s", user_reply);
297     //sent a message to the internal channel / onotice to supp channel
298     build_language_string(user, reply_buff, "NH_NEW_REQUEST", helpnode->suppid, user->nick, message);
299     if(row[2]) {
300         putsock(client, "PRIVMSG %s :%s", row[2], reply_buff);
301     } else {
302         putsock(client, "NOTICE @%s :%s", row[0], reply_buff);
303     }
304 }
305
306 static TIMEQ_CALLBACK(neonhelp_remind_open_requests) {
307     int clientid = *((int*)data);
308     MYSQL_RES *res;
309     MYSQL_ROW row;
310     printf_mysql_query("SELECT `helpserv_support`, `helpserv_public`, `helpserv_intern`, `helpserv_intern_announce` FROM `helpserv_settings` WHERE `helpserv_botid` = '%d'", clientid);
311     res = mysql_use();
312     if (!(row = mysql_fetch_row(res)) || !row[2]) {
313         free(data);
314         return;
315     }
316     struct ClientSocket *client;
317     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
318         if(client->clientid == clientid)
319             break;
320     }
321     if(!client) {
322         free(data);
323         return;
324     }
325     //count open requests
326     int requests = 0;
327     struct NeonHelpNode *helpnode;
328     if(client->flags & SOCKET_HAVE_HELPNODE) {
329         for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
330             if(helpnode->status == 0) {
331                 requests++;
332             }
333         }
334     }
335     if(requests) {
336         char nameBuf[30];
337         sprintf(nameBuf, "neonhelp_%d", client->clientid);
338         if(!timeq_name_exists(nameBuf)) {
339             timeq_add_name(nameBuf, 300, neonhelp_remind_open_requests, data);
340         }
341         char replybuf[MAXLEN];
342         build_language_string(NULL, replybuf, (requests == 1 ? "NH_REMIND_OPEN_REQUESTS_1" : "NH_REMIND_OPEN_REQUESTS_2"), requests);
343         putsock(client, "PRIVMSG %s :%s", row[2], replybuf);
344     } else
345         free(data);
346 }
347
348 static void neonhelp_event_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
349     char logline[MAXLEN];
350     sprintf(logline, "<%s> %s", user->nick, message);
351     struct ClientSocket *client;
352     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
353         if(client->botid == BOTID) {
354             struct NeonHelpNode *helpnode;
355             if(client->flags & SOCKET_HAVE_HELPNODE) {
356                 for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
357                     if(helpnode->logchan == chan && helpnode->status == 1) {
358                         if(!helpnode->log) {
359                             helpnode->log = calloc(LOGBUFFERLINES, sizeof(char*));
360                             if(!helpnode->log) return;
361                         }
362                         int i;
363                         for(i = 0; i < LOGBUFFERLINES; i++) {
364                             if(!helpnode->log[i]) {
365                                 helpnode->log[i] = strdup(logline);
366                                 break;
367                             }
368                         }
369                         if(i == LOGBUFFERLINES) {
370                             //write buffer to database
371                             char logbuff[MAXLEN * LOGBUFFERLINES];
372                             int len = 0;
373                             for(i = 0; i < LOGBUFFERLINES; i++) {
374                                 len += sprintf(logbuff + len, "%s\n", helpnode->log[i]);
375                                 free(helpnode->log[i]);
376                                 helpnode->log[i] = NULL;
377                             }
378                             printf_long_mysql_query(1024 + len, "UPDATE `helpserv_requests` SET `log` = CONCAT(`log`, '%s') WHERE `id` = %d", escape_string(logbuff), helpnode->suppid);
379                         }
380                         break;
381                     }
382                 }
383             }
384         }
385     }
386 }
387
388 static void destroy_support_request(struct ClientSocket *client, struct NeonHelpNode *helpnode, int do_reply) {
389     //write buffer to database
390     char logbuff[MAXLEN * LOGBUFFERLINES];
391     int len = 0;
392     int i;
393     if(helpnode->log) {
394         for(i = 0; i < LOGBUFFERLINES; i++) {
395             if(!helpnode->log[i]) break;
396             len += sprintf(logbuff + len, "%s\n", helpnode->log[i]);
397             free(helpnode->log[i]);
398             helpnode->log[i] = NULL;
399         }
400         free(helpnode->log);
401     } else
402         logbuff[0] = '\0';
403     printf_long_mysql_query(1024 + len, "UPDATE `helpserv_requests` SET `status`='2', `log` = CONCAT(`log`, '%s') WHERE `id` = %d", escape_string(logbuff), helpnode->suppid);
404     if(do_reply) {
405         reply(client, helpnode->user, "NH_DELETED", helpnode->suppid);
406     }
407     free(helpnode);
408 }
409
410 static void neonhelp_event_kick(struct UserNode *user, struct ChanUser *target, char *reason) {
411     struct ClientSocket *client;
412     MYSQL_RES *res;
413     MYSQL_ROW row;
414     struct ChanNode *support, *public;
415     int userHasRequest;
416     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
417         if(client->botid == BOTID && isUserOnChan(client->user, target->chan)) {
418             userHasRequest = 0;
419             struct NeonHelpNode *helpnode, *prev_helpnode = NULL;
420             if(client->flags & SOCKET_HAVE_HELPNODE) {
421                 for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
422                     if(helpnode->user == target->user) {
423                         userHasRequest = 1;
424                         break;
425                     } else
426                         prev_helpnode = helpnode;
427                 }
428             }
429             if(!userHasRequest) continue;
430             printf_mysql_query("SELECT `helpserv_support`, `helpserv_public`, `helpserv_intern` FROM `helpserv_settings` WHERE `helpserv_botid` = '%d'", client->clientid);
431             res = mysql_use();
432             if (!(row = mysql_fetch_row(res))) continue;
433             support = getChanByName(row[0]);
434             public = (row[1] ? getChanByName(row[1]) : NULL);
435             if(target->chan == support || !((support && isUserOnChan(target->user, support)) || (public && isUserOnChan(target->user, public)))) {
436                 //free the user's support request
437                 if(prev_helpnode)
438                     prev_helpnode->next = helpnode->next;
439                 else
440                     client->botclass_helpnode = helpnode->next;
441                 destroy_support_request(client, helpnode, 1);
442             }
443         }
444     }
445 }
446
447 static void neonhelp_event_part(struct ChanUser *target, char *reason) {
448     struct ClientSocket *client;
449     MYSQL_RES *res;
450     MYSQL_ROW row;
451     struct ChanNode *support, *public;
452     int userHasRequest;
453     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
454         if(client->botid == BOTID && isUserOnChan(client->user, target->chan)) {
455             userHasRequest = 0;
456             struct NeonHelpNode *helpnode, *prev_helpnode = NULL;
457             if(client->flags & SOCKET_HAVE_HELPNODE) {
458                 for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
459                     if(helpnode->user == target->user) {
460                         userHasRequest = 1;
461                         break;
462                     } else
463                         prev_helpnode = helpnode;
464                 }
465             }
466             if(!userHasRequest) continue;
467             printf_mysql_query("SELECT `helpserv_support`, `helpserv_public`, `helpserv_intern` FROM `helpserv_settings` WHERE `helpserv_botid` = '%d'", client->clientid);
468             res = mysql_use();
469             if (!(row = mysql_fetch_row(res))) continue;
470             support = getChanByName(row[0]);
471             public = (row[1] ? getChanByName(row[1]) : NULL);
472             if(target->chan == support || !((support && isUserOnChan(target->user, support)) || (public && isUserOnChan(target->user, public)))) {
473                 //free the user's support request
474                 if(prev_helpnode)
475                     prev_helpnode->next = helpnode->next;
476                 else
477                     client->botclass_helpnode = helpnode->next;
478                 destroy_support_request(client, helpnode, 1);
479             }
480         }
481     }
482 }
483
484 static void neonhelp_event_quit(struct UserNode *target, char *reason) {
485     struct ClientSocket *client;
486     int userHasRequest;
487     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
488         if(client->botid == BOTID) {
489             userHasRequest = 0;
490             struct NeonHelpNode *helpnode, *prev_helpnode = NULL;
491             if(client->flags & SOCKET_HAVE_HELPNODE) {
492                 for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
493                     if(helpnode->user == target) {
494                         userHasRequest = 1;
495                         break;
496                     } else
497                         prev_helpnode = helpnode;
498                 }
499             }
500             if(!userHasRequest) continue;
501             //free the user's support request
502             if(prev_helpnode)
503                 prev_helpnode->next = helpnode->next;
504             else
505                 client->botclass_helpnode = helpnode->next;
506             destroy_support_request(client, helpnode, 0);
507         }
508     }
509 }
510
511 void init_NeonHelp() {
512     
513     set_bot_alias(BOTID, BOTALIAS);
514     start_bots();
515     
516     //register events
517     bind_bot_ready(neonhelp_bot_ready);
518     bind_privmsg(neonhelp_event_privmsg);
519     bind_chanmsg(neonhelp_event_chanmsg);
520     bind_part(neonhelp_event_part);
521     bind_kick(neonhelp_event_kick);
522     bind_quit(neonhelp_event_quit);
523     
524     set_trigger_callback(BOTID, neonhelp_trigger_callback);
525     
526     register_default_language_table(msgtab);
527 }
528
529 void loop_NeonHelp() {
530     
531 }
532
533 void free_NeonHelp() {
534     
535 }
536
537 #undef BOTID
538 #undef BOTALIAS