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