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