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