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