search another bot if unable to send funcmd replies over textbot
[NeonServV5.git] / src / modules / funcmd.mod / cmd_funcmds.c
1 /* cmd_funcmds.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 "cmd_funcmds.h"
19 #include "../../modcmd.h"
20 #include "../../mysqlConn.h"
21 #include "../../IRCParser.h"
22 #include "../../ClientSocket.h"
23 #include "../../UserNode.h"
24 #include "../../ChanNode.h"
25 #include "../../lang.h"
26 #include "../../tools.h"
27 #include "../../DBHelper.h"
28 #include "../../IRCEvents.h"
29 #include "../../timeq.h"
30 #include "../../WHOHandler.h"
31 #include "../../EventLogger.h"
32
33 static const struct default_language_entry msgtab[] = {
34     {"FUN_DICE", "$b%s$b: A $b%d$b shows on the %d-sided die."}, /* {ARGS: "TestUser", 5, 6} */
35     {"FUN_DICE_NUM", "I do not understand $b%s$b. Please use a single number above 1."}, /* {ARGS: "bla"} */
36     {"FUN_8BALL", "$b%s$b: %s"}, /* {ARGS: "TestUser", "Not a chance."} */
37     {"FUN_8BALL_REPLIES", "Not a chance.|In your dreams.|Absolutely!|Could be, could be.|No!"},
38     {"FUN_COOKIE", "gives %1$s a very big chocolate cookie. %1$s has got %2$d cookies until now (%3$d in this channel)."}, /* {ARGS: "TestUser", 20, 50} */
39     {"FUN_BOMB_MENU", "There are the following commands:"},
40     {"FUN_BOMB_MENU_PLANT",  "$b%s plant <user> [wires] $b Plant a bomb in front of another users feet ([wires] is the number of wires - default: %d)"}, /* {ARGS: "bomb", 3} */
41     {"FUN_BOMB_MENU_KPLANT", "$b%s kplant <user> [wires]$b Plant a kick-bomb in front of another users feet ([wires] is the number of wires - default: %d) (OP only)"}, /* {ARGS: "bomb", 3} */
42     {"FUN_BOMB_MENU_DEFUSE", "$b%s defuse <wire>        $b Defuses a bomb (maybe :D)"}, /* {ARGS: "bomb"} */
43     {"FUN_BOMB_MENU_RETURN", "$b%s return               $b Pushes a bomb planted infront of you back to the owner ($k4WARNING$k: There is a highly chance that the bomb detonates)"}, /* {ARGS: "bomb"} */
44     {"FUN_BOMB_SELF", "You can not plant a bomb in front of yourself..."},
45     {"FUN_BOMB_TARGET_ACTIVE", "%s has already an active bomb..."},
46     {"FUN_BOMB_OWNER_ACTIVE", "You have already planted another bomb..."},
47     {"FUN_BOMB_PLANTED", "%1$s planted a bomb in front of %2$s's feet. %2$s, you have $b%3$d seconds$b to defuse the bomb by cutting one of the following wires: %4$s ($uuse:$u %5$s defuse <wire>)"}, /* {ARGS: "TestUser", "TestTarget", 30, "blue, red, green", "bomb"} */
48     {"FUN_BOMB_KPLANTED", "%1$s planted a kick-bomb in front of %2$s's feet. %2$s, you have $b%3$d seconds$b to defuse the bomb by cutting one of the following wires: %4$s ($uuse:$u %5$s defuse <wire>)"}, /* {ARGS: "TestUser", "TestTarget", 30, "blue, red, green", "bomb"} */
49     {"FUN_BOMB_PLANT_SERVICE", "You can't plant a bomb in front of this user (bot)..."},
50     {"FUN_BOMB_PLANT_PROTECTED", "You can't plant a bomb in front of this user (protect)..."},
51     {"FUN_BOMB_PLANT_MAXWIRES", "A bomb can only have %d-%d wires!"}, /* {ARGS: 2, 8} */
52     {"FUN_BOMB_WIRES", "$k4red$k|$k3green$k|$k8yellow$k|$k12blue$k|$k1black$k|$k6purple$k|$k7orange$k|$k11aqua$k"},
53     {"FUN_BOMB_NOBOMB", "There is no bomb you could defuse..."},
54     {"FUN_BOMB_UNKNOWN_WIRE", "%s is an unknown wire..."}, /* {ARGS: "white"} */
55     {"FUN_BOMB_DEFUSED", "%1$s has successfully defused the bomb! %1$s got %2$d bombs (%3$d in this channel) and has defused %4$d of them."}, /* {ARGS: "TestUser", 20, 10, 5} */
56     {"FUN_BOMB_DETONATED", "*BOOOOOOM* The bomb explodes in front of %1$s!!! %2$s was the right wire. %1$s got %3$d bombs (%4$d in this channel)"}, /* {ARGS: "TestUser", "white", 20, 10} */
57     {NULL, NULL}
58 };
59
60 #define FUNCMD_BOMB_WIRES_DEFAULT 3
61 #define FUNCMD_BOMB_WIRES_MIN     2
62 #define FUNCMD_BOMB_WIRES_MAX     8
63 #define FUNCMD_BOMB_TIME          30
64
65 static int funcmd_bomb_freeuser(struct UserNode *user);
66 static int funcmd_bomb_freechan(struct ChanNode *chan);
67 static void funcmd_bomb_freeclient(struct ClientSocket *client);
68
69 void init_funcmds() {
70     register_default_language_table(msgtab);
71     srand(time(NULL));
72     bind_freeuser(funcmd_bomb_freeuser, module_id);
73     bind_freechan(funcmd_bomb_freechan, module_id);
74     bind_freeclient(funcmd_bomb_freeclient, module_id);
75 }
76
77 void register_commands() {
78     //Fun Commands
79     register_command_alias(3, "FunCMD");
80     #define USER_COMMAND(NAME,FUNCTION,PARAMCOUNT,FLAGS) register_command(3, NAME, module_id, FUNCTION, PARAMCOUNT, NULL, 0, FLAGS)
81     //               NAME              FUNCTION        PARAMS   FLAGS
82     //USER_COMMAND("extscript",    neonserv_cmd_extscript, 0,  CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_EMPTY_ARGS | CMDFLAG_CHAN_PARAM | CMDFLAG_FUNCMD);
83     USER_COMMAND("ping",         funcmd_ping,            0,  CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_FUNCMD);
84     USER_COMMAND("pong",         funcmd_pong,            0,  CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_FUNCMD);
85     USER_COMMAND("dice",         funcmd_dice,            1,  CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_FUNCMD);
86     USER_COMMAND("8ball",        funcmd_8ball,           1,  CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_FUNCMD);
87     USER_COMMAND("cookie",       funcmd_cookie,          0,  CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_FUNCMD);
88     USER_COMMAND("bomb",         funcmd_bomb,            0,  CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_FUNCMD);
89     USER_COMMAND("ship",         funcmd_ship,            0,  CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_FUNCMD);
90     #undef USER_COMMAND
91 }
92
93 struct funcmd_header_info {
94     struct ClientSocket *client;
95     struct UserNode *user;
96     struct ChanNode *chan;
97     char send_notice;
98     char null_language;
99 };
100
101 #define FUNCMD_HEADER \
102 struct funcmd_header_info *header = malloc(sizeof(*header)); \
103 header->client = textclient; \
104 header->user = user; \
105 header->chan = chan; \
106 header->null_language = 0; \
107 {\
108     MYSQL_RES *res; \
109     MYSQL_ROW row; \
110     printf_mysql_query("SELECT `channel_toys` FROM `channels` WHERE `channel_name` = '%s'", escape_string(chan->name)); \
111     res = mysql_use(); \
112     row = mysql_fetch_row(res); \
113     if(!row || !strcmp(row[0], "0")) { \
114         reply(textclient, user, "NS_FUN_DISABLED", chan->name); \
115         return; \
116     } else if(!strcmp(row[0], "1")) \
117         header->send_notice = 1; \
118     else \
119         header->send_notice = 0; \
120 }
121
122 #define FUNCMD_FOOTER \
123 free(header);
124
125 #define REPLYTYPE_NORMAL 0x01
126 #define REPLYTYPE_ACTION 0x02
127 #define REPLYTYPE_NOTICE 0x04
128 static void funcmd_reply(struct funcmd_header_info *header, const char *text, int type, ...) {
129     if (!(header->client->flags & SOCKET_FLAG_CONNECTED)) return;
130     struct ClientSocket *client = header->client;
131     const char *reply_format = get_language_string((header->null_language ? NULL : header->user), text);
132     if(reply_format)
133         text = reply_format;
134     char formatBuf[MAXLEN];
135     if(header->send_notice || (type & REPLYTYPE_NOTICE)) {
136         if(type & REPLYTYPE_ACTION)
137             sprintf(formatBuf, "NOTICE %s :%s %s", header->user->nick, header->client->user->nick, text);
138         else
139             sprintf(formatBuf, "NOTICE %s :%s", header->user->nick, text);
140     } else {
141         if(type & REPLYTYPE_ACTION)
142             sprintf(formatBuf, "PRIVMSG %s :\001ACTION %s\001", header->chan->name, text);
143         else
144             sprintf(formatBuf, "PRIVMSG %s :%s", header->chan->name, text);
145         if(!isUserOnChan(client->user, header->chan) && isModeSet(header->chan->modes, 'n'))
146             client = getChannelBot(header->chan, 0);
147     }
148     va_list arg_list;
149     char sendBuf[MAXLEN];
150     int pos;
151     sendBuf[0] = '\0';
152     va_start(arg_list, type);
153     pos = vsnprintf(sendBuf, MAXLEN - 2, formatBuf, arg_list);
154     va_end(arg_list);
155     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
156     sendBuf[pos] = '\n';
157     sendBuf[pos+1] = '\0';
158     write_socket(client, sendBuf, pos+1);
159 }
160
161 static char* getSetting(struct UserNode *user, struct ChanNode *chan, const char *setting) {
162     char *uname = "";
163     int cid = 0;
164     MYSQL_RES *res;
165     MYSQL_ROW row;
166     if(user) {
167         uname = ((user->flags & USERFLAG_ISAUTHED) ? user->auth : "*");
168     }
169     if(chan) {
170         loadChannelSettings(chan);
171         if(chan->flags & CHANFLAG_CHAN_REGISTERED)
172             cid = chan->channel_id;
173     }
174     printf_mysql_query("SELECT `value` FROM `fundata` WHERE `user` = '%s' AND `cid` = '%d' AND `name` = '%s'", escape_string(uname), cid, escape_string(setting));
175     res = mysql_use();
176     if ((row = mysql_fetch_row(res)) != NULL) {
177         return row[0];
178     } else
179         return NULL;
180 }
181
182 static void setSetting(struct UserNode *user, struct ChanNode *chan, const char *setting, const char *value) {
183     char *uname = "";
184     int cid = 0;
185     MYSQL_RES *res;
186     MYSQL_ROW row;
187     if(user) {
188         uname = ((user->flags & USERFLAG_ISAUTHED) ? user->auth : "*");
189     }
190     if(chan) {
191         loadChannelSettings(chan);
192         if(chan->flags & CHANFLAG_CHAN_REGISTERED)
193             cid = chan->channel_id;
194     }
195     printf_mysql_query("SELECT `id`, `value` FROM `fundata` WHERE `user` = '%s' AND `cid` = '%d' AND `name` = '%s'", escape_string(uname), cid, escape_string(setting));
196     res = mysql_use();
197     if ((row = mysql_fetch_row(res)) != NULL) {
198         if(strcmp(row[1], value))
199             printf_mysql_query("UPDATE `fundata` SET `value` = '%s' WHERE `id` = '%s'", escape_string(value), row[0]);
200     } else
201         printf_mysql_query("INSERT INTO `fundata` (`user`, `cid`, `name`, `value`) VALUES ('%s', '%d', '%s', '%s')", escape_string(uname), cid, escape_string(setting), escape_string(value));
202 }
203
204 #include "cmd_funcmds_bomb.c"
205
206
207 CMD_BIND(funcmd_ping) {
208     FUNCMD_HEADER;
209     funcmd_reply(header, "\002%s\002: Pong!", REPLYTYPE_NORMAL, user->nick);
210     FUNCMD_FOOTER;
211 }
212
213 CMD_BIND(funcmd_pong) {
214     FUNCMD_HEADER;
215     funcmd_reply(header, "\002%s\002: Ping!", REPLYTYPE_NORMAL, user->nick);
216     FUNCMD_FOOTER;
217 }
218
219 CMD_BIND(funcmd_dice) {
220     FUNCMD_HEADER;
221     int max = atoi(argv[0]);
222     if(max > 1) {
223         int val = (rand() % max) + 1;
224         funcmd_reply(header, "%s", REPLYTYPE_NORMAL, "     ____");
225         funcmd_reply(header, "%s", REPLYTYPE_NORMAL, "    /\\' .\\    _____");
226         funcmd_reply(header, "%s", REPLYTYPE_NORMAL, "   /: \\___\\  / .  /\\");
227         funcmd_reply(header, "%s", REPLYTYPE_NORMAL, "   \\' / . / /____/..\\");
228         funcmd_reply(header, "%s", REPLYTYPE_NORMAL, "    \\/___/  \\'  '\\  /");
229         funcmd_reply(header, "%s", REPLYTYPE_NORMAL, "             \\'__'\\/ ");
230         funcmd_reply(header, "FUN_DICE", REPLYTYPE_NORMAL, user->nick, val, max);
231     } else
232         funcmd_reply(header, "FUN_DICE_NUM", REPLYTYPE_NORMAL, argv[0]);
233     FUNCMD_FOOTER;
234 }
235
236 CMD_BIND(funcmd_8ball) {
237     FUNCMD_HEADER;
238     char *message = merge_argv(argv, 0, argc);
239     const char *const_replies = get_language_string(header->user, "FUN_8BALL_REPLIES");
240     char replies[MAXLEN];
241     int i, reply_count = 1;
242     for(i = 0; const_replies[i]; i++) {
243         if(const_replies[i] == '|')
244             reply_count++;
245         replies[i] = const_replies[i];
246     }
247     replies[i] = '\0';
248     unsigned int crc32_val = (crc32(message)) % reply_count;
249     char *creply = (crc32_val == 0 ? replies : NULL);
250     reply_count = 0;
251     for(i = 0; replies[i]; i++) {
252         if(replies[i] == '|') {
253             if(creply) {
254                 replies[i] = '\0';
255                 break;
256             } else {
257                 reply_count++;
258                 if(reply_count == crc32_val) {
259                     creply = &replies[i+1];
260                 }
261             }
262         }
263     }
264     if(creply) {
265         funcmd_reply(header, "FUN_8BALL", REPLYTYPE_NORMAL, user->nick, creply);
266     }
267     FUNCMD_FOOTER;
268 }
269
270 CMD_BIND(funcmd_cookie) {
271     FUNCMD_HEADER;
272     if(argc) {
273         if(!(user = getUserByNick(argv[0]))) {
274             reply(header->client, header->user, "NS_USER_UNKNOWN", argv[0]);
275             FUNCMD_FOOTER;
276             return;
277         }
278     }
279     char *tmp;
280     int user_count = ((tmp = getSetting(user, chan, "cookies")) ? atoi(tmp) : 0);
281     int total_count = ((tmp = getSetting(user, NULL, "cookies")) ? atoi(tmp) : 0);
282     user_count++;
283     total_count++;
284     char buf[10];
285     sprintf(buf, "%d", user_count);
286     setSetting(user, chan, "cookies", buf);
287     sprintf(buf, "%d", total_count);
288     setSetting(user, NULL, "cookies", buf);
289     funcmd_reply(header, "FUN_COOKIE", REPLYTYPE_ACTION, user->nick, total_count, user_count);
290     FUNCMD_FOOTER;
291 }
292
293 CMD_BIND(funcmd_ship) {
294     FUNCMD_HEADER;
295     funcmd_reply(header, "%s", REPLYTYPE_NORMAL, "\0030,0------------------------\0031|");
296     funcmd_reply(header, "%s", REPLYTYPE_NORMAL, "\0030,0----------------------\00314)_)_)");
297     funcmd_reply(header, "%s%c%s", REPLYTYPE_NORMAL, "\0030,0--------------------\0031,4\002=", header->client->user->nick[0], "=\002\00314,0|)___)");
298     funcmd_reply(header, "%s", REPLYTYPE_NORMAL, "\0030,0-------------------\00314)__) )___) )");
299     funcmd_reply(header, "%s", REPLYTYPE_NORMAL, "\0030,0------------------\00314)___) )____))_)");
300     funcmd_reply(header, "%s", REPLYTYPE_NORMAL, "\0030,0----------------\00314)____)_____))__)\\");
301     funcmd_reply(header, "%s", REPLYTYPE_NORMAL, "\0030,0---------------\00314)\0035__\00314|\0035____\00314/|\0035___\00314|\0035___\00314-\\\\---");
302     funcmd_reply(header, "%s", REPLYTYPE_NORMAL, "\0030,0-------\00312^^^^^^\0035~~~\\ \002oo oo oo oo\002 /~~\00312^^^^^^");
303     funcmd_reply(header, "%s", REPLYTYPE_NORMAL, "\0030,0------------\00312~^^^^ ~~~~^^~~~~^^~~^^~~~~~");
304     funcmd_reply(header, "%s", REPLYTYPE_NORMAL, "\0030,0-----------------\00312~~^^ ~^^~ ~^~ ~^");
305     FUNCMD_FOOTER;
306 }