1 /* bot_NeonHelp.c - NeonServ v5.3
2 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
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.
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.
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/>.
18 #include "bot_NeonHelp.h"
20 #include "cmd_neonhelp.h"
22 #include "mysqlConn.h"
23 #include "ClientSocket.h"
27 #include "IRCEvents.h"
28 #include "IRCParser.h"
31 #include "WHOHandler.h"
34 #define BOTALIAS "NeonHelp"
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"} */
56 static void neonhelp_bot_ready(struct ClientSocket *client) {
60 printf_mysql_query("SELECT `automodes` FROM `bots` WHERE `id` = '%d'", client->clientid);
62 if ((row = mysql_fetch_row(res)) != NULL) {
63 putsock(client, "MODE %s +%s", client->user->nick, row[0]);
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);
69 while ((row = mysql_fetch_row(res)) != NULL) {
70 putsock(client, "JOIN %s %s", row[0], row[1]);
74 static void neonhelp_trigger_callback(int clientid, struct ChanNode *chan, char *trigger) {
77 loadChannelSettings(chan);
78 if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
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);
84 row = mysql_fetch_row(res);
86 strcpy(trigger, row[0]);
88 strcpy(trigger, ((row[1] && *row[1]) ? row[1] : "!"));
91 static void start_bots() {
92 struct ClientSocket *client;
93 MYSQL_RES *res, *res2;
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);
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->flags |= SOCKET_FLAG_SILENT;
105 client->botid = BOTID;
106 client->clientid = atoi(row[7]);
107 connect_socket(client);
110 printf_mysql_query("SELECT `command`, `function`, `parameters`, `global_access`, `chan_access`, `flags` FROM `bot_binds` WHERE `botclass` = '%d'", BOTID);
112 while ((row = mysql_fetch_row(res2)) != NULL) {
113 if(bind_cmd_to_command(BOTID, row[0], row[1])) {
114 if(row[2] && strcmp(row[2], "")) {
115 bind_set_parameters(BOTID, row[0], row[2]);
118 bind_set_global_access(BOTID, row[0], atoi(row[3]));
121 bind_set_channel_access(BOTID, row[0], row[4]);
123 if(strcmp(row[5], "0"))
124 bind_set_bind_flags(BOTID, row[0], atoi(row[5]));
127 bind_unbound_required_functions(BOTID);
130 static void neonhelp_event_privmsg_async(struct ClientSocket *client, struct UserNode *user, struct UserNode *target, char *message);
131 static USERAUTH_CALLBACK(neonhelp_event_privmsg_nick_lookup);
132 struct neonhelp_event_privmsg_cache {
133 struct ClientSocket *client;
134 struct UserNode *user, *target;
138 static void neonhelp_event_privmsg(struct UserNode *user, struct UserNode *target, char *message) {
139 struct ClientSocket *client;
140 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
141 if(client->user == target) {
142 if(client->botid != BOTID) return;
146 if(!client) return; //we got the message but we have no client that could receive it???
147 if(user->flags & USERFLAG_ISAUTHED) {
148 neonhelp_event_privmsg_async(client, user, target, message);
150 struct neonhelp_event_privmsg_cache *cache = malloc(sizeof(*cache));
152 cache->client = client;
154 cache->target = target;
155 cache->message = strdup(message);
156 get_userauth(user, neonhelp_event_privmsg_nick_lookup, cache);
160 static USERAUTH_CALLBACK(neonhelp_event_privmsg_nick_lookup) {
161 struct neonhelp_event_privmsg_cache *cache = data;
162 neonhelp_event_privmsg_async(cache->client, cache->user, cache->target, cache->message);
163 free(cache->message);
167 static void neonhelp_event_privmsg_async(struct ClientSocket *client, struct UserNode *user, struct UserNode *target, char *message) {
170 printf_mysql_query("SELECT `helpserv_support`, `helpserv_public`, `helpserv_intern` FROM `helpserv_settings` WHERE `helpserv_botid` = '%d'", client->clientid);
172 if (!(row = mysql_fetch_row(res))) return;
173 //check if the user is a supporter (access in the support channel)
174 if((user->flags & USERFLAG_ISAUTHED)) {
177 if(user->flags & USERFLAG_HAS_USERID)
178 userid = user->user_id;
180 printf_mysql_query("SELECT `user_id` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
182 if ((row2 = mysql_fetch_row(res)) != NULL) {
183 userid = atoi(row2[0]);
184 user->user_id = userid;
185 user->flags |= USERFLAG_HAS_USERID;
189 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]));
191 if ((row2 = mysql_fetch_row(res)) != NULL) {
192 int cflags = atoi(row2[1]);
193 if(!(cflags & DB_CHANUSER_SUSPENDED))
194 caccess = atoi(row2[0]);
196 if(caccess) return; //ignore messages from supporters
198 //check if the user is in one of the bot's channels
199 struct ChanUser *chanuser;
200 struct ChanNode *chan;
201 for(chanuser = getUserChannels(target, NULL); chanuser; chanuser = getUserChannels(target, chanuser)) {
202 chan = chanuser->chan;
203 if((!stricmp(chan->name, row[0]) || (row[1] && !stricmp(chan->name, row[1]))) && isUserOnChan(user, chan))
208 reply(client, user, "NH_NOT_ON_CHAN_2", row[0], row[1]);
210 reply(client, user, "NH_NOT_ON_CHAN_1", row[0]);
213 //check if there is already a support request
215 if(client->flags & SOCKET_HAVE_HELPNODE) {
216 struct NeonHelpNode *helpnode;
217 for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
218 if(helpnode->user == user) {
219 //simply append the message to the database
220 printf_mysql_query("SELECT `text` FROM `helpserv_requests` WHERE `id` = %d", helpnode->suppid);
222 if ((row2 = mysql_fetch_row(res)) != NULL) {
223 char *old_msg = escape_string(row2[0]);
224 char *new_msg = escape_string(message);
225 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);
233 struct NeonHelpNode *helpnode = malloc(sizeof(*helpnode));
234 if(!helpnode) return;
235 helpnode->user = user;
236 helpnode->status = 0;
237 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));
238 helpnode->suppid = (int) mysql_insert_id(mysql_conn);
239 helpnode->next = ((client->flags & SOCKET_HAVE_HELPNODE) ? client->botclass_helpnode : NULL);
240 client->botclass_helpnode = helpnode;
241 client->flags |= SOCKET_HAVE_HELPNODE;
242 //build the user reply...
243 char user_reply[MAXLEN];
244 int user_reply_pos = 0;
245 char reply_buff[MAXLEN];
246 //1st part: NH_REQUEST_RECORDED
247 strcpy(user_reply + user_reply_pos, build_language_string(user, reply_buff, "NH_REQUEST_RECORDED", helpnode->suppid));
248 user_reply_pos += strlen(reply_buff);
249 //2nd part: NH_REQUEST_OTHERS_0 / NH_REQUEST_OTHERS_1 / NH_REQUEST_OTHERS_2
250 user_reply[user_reply_pos++] = ' ';
252 strcpy(user_reply + user_reply_pos, build_language_string(user, reply_buff, (others ? "NH_REQUEST_OTHERS_1" : "NH_REQUEST_OTHERS_0")));
254 strcpy(user_reply + user_reply_pos, build_language_string(user, reply_buff, "NH_REQUEST_OTHERS_2", others));
255 user_reply_pos += strlen(reply_buff);
256 //3th part: NH_REQUEST_FOOTER_1 / NH_REQUEST_FOOTER_2
257 user_reply[user_reply_pos++] = ' ';
259 strcpy(user_reply + user_reply_pos, build_language_string(user, reply_buff, "NH_REQUEST_FOOTER_2", row[0], row[1]));
261 strcpy(user_reply + user_reply_pos, build_language_string(user, reply_buff, "NH_REQUEST_FOOTER_1", row[0]));
262 user_reply_pos += strlen(reply_buff);
263 reply(client, user, "%s", user_reply);
264 //sent a message to the internal channel / onotice to supp channel
265 build_language_string(user, reply_buff, "NH_NEW_REQUEST", helpnode->suppid, user->nick, message);
267 putsock(client, "PRIVMSG %s :%s", row[2], reply_buff);
269 putsock(client, "NOTICE @%s :%s", row[0], reply_buff);
273 static void destroy_support_request(struct ClientSocket *client, struct NeonHelpNode *helpnode, int do_reply) {
274 printf_mysql_query("UPDATE `helpserv_requests` SET `status` = '2' WHERE `id` = %d", helpnode->suppid);
276 reply(client, helpnode->user, "NH_DELETED", helpnode->suppid);
281 static void neonhelp_event_kick(struct UserNode *user, struct ChanUser *target, char *reason) {
282 struct ClientSocket *client;
285 struct ChanNode *support, *public;
287 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
288 if(client->botid == BOTID && isUserOnChan(client->user, target->chan)) {
290 struct NeonHelpNode *helpnode, *prev_helpnode = NULL;
291 if(client->flags & SOCKET_HAVE_HELPNODE) {
292 for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
293 if(helpnode->user == target->user) {
297 prev_helpnode = helpnode;
300 if(!userHasRequest) continue;
301 printf_mysql_query("SELECT `helpserv_support`, `helpserv_public`, `helpserv_intern` FROM `helpserv_settings` WHERE `helpserv_botid` = '%d'", client->clientid);
303 if (!(row = mysql_fetch_row(res))) continue;
304 support = getChanByName(row[0]);
305 public = (row[1] ? getChanByName(row[1]) : NULL);
306 if(target->chan == support || !((support && isUserOnChan(target->user, support)) || (public && isUserOnChan(target->user, public)))) {
307 //free the user's support request
309 prev_helpnode->next = helpnode->next;
311 client->botclass_helpnode = helpnode->next;
312 destroy_support_request(client, helpnode, 1);
318 static void neonhelp_event_part(struct ChanUser *target, char *reason) {
319 struct ClientSocket *client;
322 struct ChanNode *support, *public;
324 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
325 if(client->botid == BOTID && isUserOnChan(client->user, target->chan)) {
327 struct NeonHelpNode *helpnode, *prev_helpnode = NULL;
328 if(client->flags & SOCKET_HAVE_HELPNODE) {
329 for(helpnode = client->botclass_helpnode; helpnode; helpnode = helpnode->next) {
330 if(helpnode->user == target->user) {
334 prev_helpnode = helpnode;
337 if(!userHasRequest) continue;
338 printf_mysql_query("SELECT `helpserv_support`, `helpserv_public`, `helpserv_intern` FROM `helpserv_settings` WHERE `helpserv_botid` = '%d'", client->clientid);
340 if (!(row = mysql_fetch_row(res))) continue;
341 support = getChanByName(row[0]);
342 public = (row[1] ? getChanByName(row[1]) : NULL);
343 if(target->chan == support || !((support && isUserOnChan(target->user, support)) || (public && isUserOnChan(target->user, public)))) {
344 //free the user's support request
346 prev_helpnode->next = helpnode->next;
348 client->botclass_helpnode = helpnode->next;
349 destroy_support_request(client, helpnode, 1);
355 static void neonhelp_event_quit(struct UserNode *target, char *reason) {
356 struct ClientSocket *client;
358 for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
359 if(client->botid == BOTID) {
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) {
368 prev_helpnode = helpnode;
371 if(!userHasRequest) continue;
372 //free the user's support request
374 prev_helpnode->next = helpnode->next;
376 client->botclass_helpnode = helpnode->next;
377 destroy_support_request(client, helpnode, 0);
382 void init_NeonHelp() {
384 set_bot_alias(BOTID, BOTALIAS);
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);
394 set_trigger_callback(BOTID, neonhelp_trigger_callback);
396 register_default_language_table(msgtab);
399 void loop_NeonHelp() {
403 void free_NeonHelp() {