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