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