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