fixed #989cb8324ec8fe9caba511d89051552638dbc776
[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     {NULL, NULL}
54 };
55
56 static void neonhelp_bot_ready(struct ClientSocket *client) {
57     MYSQL_RES *res;
58     MYSQL_ROW row;
59     
60     printf_mysql_query("SELECT `automodes` FROM `bots` WHERE `id` = '%d'", client->clientid);
61     res = mysql_use();
62     if ((row = mysql_fetch_row(res)) != NULL) {
63         putsock(client, "MODE %s +%s", client->user->nick, row[0]);
64     }
65     
66     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);
67     res = mysql_use();
68     
69     while ((row = mysql_fetch_row(res)) != NULL) {
70         putsock(client, "JOIN %s %s", row[0], row[1]);
71     }
72 }
73
74 static void neonhelp_trigger_callback(int clientid, struct ChanNode *chan, char *trigger) {
75     MYSQL_RES *res;
76     MYSQL_ROW row;
77     loadChannelSettings(chan);
78     if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
79         strcpy(trigger, "!");
80         return;
81     }
82     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);
83     res = mysql_use();
84     row = mysql_fetch_row(res);
85     if(row[0] && *row[0])
86         strcpy(trigger, row[0]);
87     else
88         strcpy(trigger, ((row[1] && *row[1]) ? row[1] : "!"));
89 }
90
91 static void start_bots() {
92     struct ClientSocket *client;
93     MYSQL_RES *res, *res2;
94     MYSQL_ROW row;
95     
96     printf_mysql_query("SELECT `nick`, `ident`, `realname`, `server`, `port`, `pass`, `textbot`, `id`, `queue`, `ssl`, `bind` FROM `bots` WHERE `botclass` = '%d' AND `active` = '1'", BOTID);
97     res = mysql_use();
98     
99     while ((row = mysql_fetch_row(res)) != NULL) {
100         client = create_socket(row[3], atoi(row[4]), row[10], row[5], row[0], row[1], row[2]);
101         client->flags |= (strcmp(row[6], "0") ? SOCKET_FLAG_PREFERRED : 0);
102         client->flags |= (strcmp(row[8], "0") ? SOCKET_FLAG_USE_QUEUE : 0);
103         client->flags |= (strcmp(row[9], "0") ? SOCKET_FLAG_SSL : 0);
104         client->botid = BOTID;
105         client->clientid = atoi(row[7]);
106         connect_socket(client);
107     }
108     
109     printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access`, `flags` FROM `bot_binds` WHERE `botclass` = '%d'", BOTID);
110     res2 = mysql_use();
111     while ((row = mysql_fetch_row(res2)) != NULL) {
112         if(bind_cmd_to_command(BOTID, row[0], row[1])) {
113             if(row[2] && strcmp(row[2], "")) {
114                 bind_set_parameters(BOTID, row[0], row[2]);
115             }
116             if(row[3]) {
117                 bind_set_global_access(BOTID, row[0], atoi(row[3]));
118             }
119             if(row[4]) {
120                 bind_set_channel_access(BOTID, row[0], row[4]);
121             }
122             if(strcmp(row[5], "0"))
123                 bind_set_bind_flags(BOTID, row[0], atoi(row[5]));
124         }
125     }
126     bind_unbound_required_functions(BOTID);
127 }
128
129 static void neonhelp_event_privmsg_async(struct ClientSocket *client, struct UserNode *user, struct UserNode *target, char *message);
130 static USERAUTH_CALLBACK(neonhelp_event_privmsg_nick_lookup);
131 struct neonhelp_event_privmsg_cache {
132     struct ClientSocket *client;
133     struct UserNode *user, *target;
134     char *message;
135 };
136
137 static void neonhelp_event_privmsg(struct UserNode *user, struct UserNode *target, char *message) {
138     struct ClientSocket *client;
139     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
140         if(client->user == target) {
141             if(client->botid != BOTID) return;
142             break;
143         }
144     }
145     if(!client) return; //we got the message but we have no client that could receive it???
146     if(user->flags & USERFLAG_ISAUTHED) {
147         neonhelp_event_privmsg_async(client, user, target, message);
148     } else {
149         struct neonhelp_event_privmsg_cache *cache = malloc(sizeof(*cache));
150         if(!cache) return;
151         cache->client = client;
152         cache->user = user;
153         cache->target = target;
154         cache->message = strdup(message);
155         get_userauth(user, neonhelp_event_privmsg_nick_lookup, cache);
156     }
157 }
158
159 static USERAUTH_CALLBACK(neonhelp_event_privmsg_nick_lookup) {
160     struct neonhelp_event_privmsg_cache *cache = data;
161     neonhelp_event_privmsg_async(cache->client, cache->user, cache->target, cache->message);
162     free(cache->message);
163     free(cache);
164 }
165
166 static void neonhelp_event_privmsg_async(struct ClientSocket *client, struct UserNode *user, struct UserNode *target, char *message) {
167     MYSQL_RES *res;
168     MYSQL_ROW row, row2;
169     printf_mysql_query("SELECT `helpserv_support`, `helpserv_public`, `helpserv_intern` FROM `helpserv_settings` WHERE `helpserv_botid` = '%d'", client->clientid);
170     res = mysql_use();
171     if (!(row = mysql_fetch_row(res))) return;
172     //check if the user is a supporter (access in the support channel)
173     if((user->flags & USERFLAG_ISAUTHED)) {
174         int caccess = 0;
175         int userid;
176         if(user->flags & USERFLAG_HAS_USERID)
177             userid = user->user_id;
178         else {
179             printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
180             res = mysql_use();
181             if ((row2 = mysql_fetch_row(res)) != NULL) {
182                 userid = atoi(row2[0]);
183                 user->user_id = userid;
184                 user->flags |= USERFLAG_HAS_USERID;
185             } else
186                 userid = 0;
187         }
188         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]));
189         res = mysql_use();
190         if ((row2 = mysql_fetch_row(res)) != NULL) {
191             int cflags = atoi(row2[1]);
192             if(!(cflags & DB_CHANUSER_SUSPENDED))
193                 caccess = atoi(row2[0]);
194         }
195         if(caccess) return; //ignore messages from supporters
196     }
197     //check if the user is in one of the bot's channels
198     struct ChanUser *chanuser;
199     struct ChanNode *chan;
200     for(chanuser = getUserChannels(target, NULL); chanuser; chanuser = getUserChannels(target, chanuser)) {
201         chan = chanuser->chan;
202         if((!stricmp(chan->name, row[0]) || (row[1] && !stricmp(chan->name, row[1]))) && isUserOnChan(user, chan))
203             break;
204     }
205     if(!chanuser) {
206         if(row[1])
207             reply(client, user, "NH_NOT_ON_CHAN_2", row[0], row[1]);
208         else
209             reply(client, user, "NH_NOT_ON_CHAN_1", row[0]);
210         return;
211     }
212     //check if there is already a support request
213     int others = 0;
214     if(client->flags & SOCKET_HAVE_HELPNODE) {
215         struct NeonHelpNode *helpnode;
216         for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
217             if(helpnode->user == user) {
218                 //simply append the message to the database
219                 printf_mysql_query("SELECT `text` FROM `helpserv_requests` WHERE `id` = %d", helpnode->suppid);
220                 res = mysql_use();
221                 if ((row2 = mysql_fetch_row(res)) != NULL) {
222                     char *old_msg = escape_string(row2[0]);
223                     char *new_msg = escape_string(message);
224                     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);
225                 }
226                 return;
227             }
228             others++;
229         }
230     }
231     //add new request
232     struct NeonHelpNode *helpnode = malloc(sizeof(*helpnode));
233     if(!helpnode) return;
234     helpnode->user = user;
235     helpnode->status = 0;
236     printf_mysql_query("INSERT INTO `helpserv_requests` (`host`, `hand`, `nick`, `status`, `supporter`, `time`, `text`) VALUES ('%s@%s', '%s', '%s', '0', '-1', UNIX_TIMESTAMP(), '%s')", escape_string(user->ident), escape_string(user->host), ((user->flags & USERFLAG_ISAUTHED) ? escape_string(user->auth) : "*"), escape_string(user->nick), escape_string(message));
237     helpnode->suppid = (int) mysql_insert_id(mysql_conn);
238     helpnode->next = ((client->flags & SOCKET_HAVE_HELPNODE) ? client->botclass_helpnode : NULL);
239     client->botclass_helpnode = helpnode;
240     client->flags |= SOCKET_HAVE_HELPNODE;
241     //build the user reply...
242     char user_reply[MAXLEN];
243     int user_reply_pos = 0;
244     char reply_buff[MAXLEN];
245     //1st part: NH_REQUEST_RECORDED
246     strcpy(user_reply + user_reply_pos, build_language_string(user, reply_buff, "NH_REQUEST_RECORDED", helpnode->suppid));
247     user_reply_pos += strlen(reply_buff);
248     //2nd part: NH_REQUEST_OTHERS_0 / NH_REQUEST_OTHERS_1 / NH_REQUEST_OTHERS_2
249     user_reply[user_reply_pos++] = ' ';
250     if(others <= 1)
251         strcpy(user_reply + user_reply_pos, build_language_string(user, reply_buff, (others ? "NH_REQUEST_OTHERS_1" : "NH_REQUEST_OTHERS_0")));
252     else
253         strcpy(user_reply + user_reply_pos, build_language_string(user, reply_buff, "NH_REQUEST_OTHERS_2", others));
254     user_reply_pos += strlen(reply_buff);
255     //3th part: NH_REQUEST_FOOTER_1 / NH_REQUEST_FOOTER_2
256     user_reply[user_reply_pos++] = ' ';
257     if(row[1])
258         strcpy(user_reply + user_reply_pos, build_language_string(user, reply_buff, "NH_REQUEST_FOOTER_2", row[0], row[1]));
259     else
260         strcpy(user_reply + user_reply_pos, build_language_string(user, reply_buff, "NH_REQUEST_FOOTER_1", row[0]));
261     user_reply_pos += strlen(reply_buff);
262     reply(client, user, "%s", user_reply);
263     //sent a message to the internal channel / onotice to supp channel
264     build_language_string(user, reply_buff, "NH_NEW_REQUEST", helpnode->suppid, user->nick, message);
265     if(row[2]) {
266         putsock(client, "PRIVMSG %s :%s", row[2], reply_buff);
267     } else {
268         putsock(client, "NOTICE @%s :%s", row[0], reply_buff);
269     }
270 }
271
272 static void destroy_support_request(struct ClientSocket *client, struct NeonHelpNode *helpnode, int do_reply) {
273     printf_mysql_query("UPDATE `helpserv_requests` SET `status` = '2' WHERE `id` = %d", helpnode->suppid);
274     if(do_reply) {
275         reply(client, helpnode->user, "NH_DELETED", helpnode->suppid);
276     }
277     free(helpnode);
278 }
279
280 static void neonhelp_event_kick(struct UserNode *user, struct ChanUser *target, char *reason) {
281     struct ClientSocket *client;
282     MYSQL_RES *res;
283     MYSQL_ROW row;
284     struct ChanNode *support, *public;
285     int userHasRequest;
286     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
287         if(client->botid == BOTID && isUserOnChan(client->user, target->chan)) {
288             userHasRequest = 0;
289             struct NeonHelpNode *helpnode, *prev_helpnode = NULL;
290             if(client->flags & SOCKET_HAVE_HELPNODE) {
291                 for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
292                     if(helpnode->user == target->user) {
293                         userHasRequest = 1;
294                         break;
295                     } else
296                         prev_helpnode = helpnode;
297                 }
298             }
299             if(!userHasRequest) continue;
300             printf_mysql_query("SELECT `helpserv_support`, `helpserv_public`, `helpserv_intern` FROM `helpserv_settings` WHERE `helpserv_botid` = '%d'", client->clientid);
301             res = mysql_use();
302             if (!(row = mysql_fetch_row(res))) continue;
303             support = getChanByName(row[0]);
304             public = (row[1] ? getChanByName(row[1]) : NULL);
305             if(target->chan == support || !((support && isUserOnChan(target->user, support)) || (public && isUserOnChan(target->user, public)))) {
306                 //free the user's support request
307                 if(prev_helpnode)
308                     prev_helpnode->next = helpnode->next;
309                 else
310                     client->botclass_helpnode = helpnode->next;
311                 destroy_support_request(client, helpnode, 1);
312             }
313         }
314     }
315 }
316
317 static void neonhelp_event_part(struct ChanUser *target, char *reason) {
318     struct ClientSocket *client;
319     MYSQL_RES *res;
320     MYSQL_ROW row;
321     struct ChanNode *support, *public;
322     int userHasRequest;
323     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
324         if(client->botid == BOTID && isUserOnChan(client->user, target->chan)) {
325             userHasRequest = 0;
326             struct NeonHelpNode *helpnode, *prev_helpnode = NULL;
327             if(client->flags & SOCKET_HAVE_HELPNODE) {
328                 for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
329                     if(helpnode->user == target->user) {
330                         userHasRequest = 1;
331                         break;
332                     } else
333                         prev_helpnode = helpnode;
334                 }
335             }
336             if(!userHasRequest) continue;
337             printf_mysql_query("SELECT `helpserv_support`, `helpserv_public`, `helpserv_intern` FROM `helpserv_settings` WHERE `helpserv_botid` = '%d'", client->clientid);
338             res = mysql_use();
339             if (!(row = mysql_fetch_row(res))) continue;
340             support = getChanByName(row[0]);
341             public = (row[1] ? getChanByName(row[1]) : NULL);
342             if(target->chan == support || !((support && isUserOnChan(target->user, support)) || (public && isUserOnChan(target->user, public)))) {
343                 //free the user's support request
344                 if(prev_helpnode)
345                     prev_helpnode->next = helpnode->next;
346                 else
347                     client->botclass_helpnode = helpnode->next;
348                 destroy_support_request(client, helpnode, 1);
349             }
350         }
351     }
352 }
353
354 static void neonhelp_event_quit(struct UserNode *target, char *reason) {
355     struct ClientSocket *client;
356     int userHasRequest;
357     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
358         if(client->botid == BOTID) {
359             userHasRequest = 0;
360             struct NeonHelpNode *helpnode, *prev_helpnode = NULL;
361             if(client->flags & SOCKET_HAVE_HELPNODE) {
362                 for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
363                     if(helpnode->user == target) {
364                         userHasRequest = 1;
365                         break;
366                     } else
367                         prev_helpnode = helpnode;
368                 }
369             }
370             if(!userHasRequest) continue;
371             //free the user's support request
372             if(prev_helpnode)
373                 prev_helpnode->next = helpnode->next;
374             else
375                 client->botclass_helpnode = helpnode->next;
376             destroy_support_request(client, helpnode, 0);
377         }
378     }
379 }
380
381 void init_NeonHelp() {
382     
383     set_bot_alias(BOTID, BOTALIAS);
384     start_bots();
385     
386     //register events
387     bind_bot_ready(neonhelp_bot_ready);
388     bind_privmsg(neonhelp_event_privmsg);
389     bind_part(neonhelp_event_part);
390     bind_kick(neonhelp_event_kick);
391     bind_quit(neonhelp_event_quit);
392     
393     set_trigger_callback(BOTID, neonhelp_trigger_callback);
394     
395     register_default_language_table(msgtab);
396 }
397
398 void loop_NeonHelp() {
399     
400 }
401
402 void free_NeonHelp() {
403     
404 }
405
406 #undef BOTID
407 #undef BOTALIAS