added bomb funcmd
[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     #undef USER_COMMAND
90 }
91
92 struct funcmd_header_info {
93     struct ClientSocket *client;
94     struct UserNode *user;
95     struct ChanNode *chan;
96     char send_notice;
97     char null_language;
98 };
99
100 #define FUNCMD_HEADER \
101 struct funcmd_header_info *header = malloc(sizeof(*header)); \
102 header->client = textclient; \
103 header->user = user; \
104 header->chan = chan; \
105 header->null_language = 0; \
106 {\
107     MYSQL_RES *res; \
108     MYSQL_ROW row; \
109     printf_mysql_query("SELECT `channel_toys` FROM `channels` WHERE `channel_name` = '%s'", escape_string(chan->name)); \
110     res = mysql_use(); \
111     row = mysql_fetch_row(res); \
112     if(!row || !strcmp(row[0], "0")) { \
113         reply(textclient, user, "NS_FUN_DISABLED", chan->name); \
114         return; \
115     } else if(!strcmp(row[0], "1")) \
116         header->send_notice = 1; \
117     else \
118         header->send_notice = 0; \
119 }
120
121 #define FUNCMD_FOOTER \
122 free(header);
123
124 #define REPLYTYPE_NORMAL 0x01
125 #define REPLYTYPE_ACTION 0x02
126 #define REPLYTYPE_NOTICE 0x04
127 static void funcmd_reply(struct funcmd_header_info *header, const char *text, int type, ...) {
128     if (!(header->client->flags & SOCKET_FLAG_CONNECTED)) return;
129     const char *reply_format = get_language_string((header->null_language ? NULL : header->user), text);
130     if(reply_format)
131         text = reply_format;
132     char formatBuf[MAXLEN];
133     if(header->send_notice || (type & REPLYTYPE_NOTICE)) {
134         if(type & REPLYTYPE_ACTION)
135             sprintf(formatBuf, "NOTICE %s :%s %s", header->user->nick, header->client->user->nick, text);
136         else
137             sprintf(formatBuf, "NOTICE %s :%s", header->user->nick, text);
138     } else {
139         if(type & REPLYTYPE_ACTION)
140             sprintf(formatBuf, "PRIVMSG %s :\001ACTION %s\001", header->chan->name, text);
141         else
142             sprintf(formatBuf, "PRIVMSG %s :%s", header->chan->name, text);
143     }
144     va_list arg_list;
145     char sendBuf[MAXLEN];
146     int pos;
147     sendBuf[0] = '\0';
148     va_start(arg_list, type);
149     pos = vsnprintf(sendBuf, MAXLEN - 2, formatBuf, arg_list);
150     va_end(arg_list);
151     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
152     sendBuf[pos] = '\n';
153     sendBuf[pos+1] = '\0';
154     write_socket(header->client, sendBuf, pos+1);
155 }
156
157 static char* getSetting(struct UserNode *user, struct ChanNode *chan, const char *setting) {
158     char *uname = "";
159     int cid = 0;
160     MYSQL_RES *res;
161     MYSQL_ROW row;
162     if(user) {
163         uname = ((user->flags & USERFLAG_ISAUTHED) ? user->auth : "*");
164     }
165     if(chan) {
166         loadChannelSettings(chan);
167         if(chan->flags & CHANFLAG_CHAN_REGISTERED)
168             cid = chan->channel_id;
169     }
170     printf_mysql_query("SELECT `value` FROM `fundata` WHERE `user` = '%s' AND `cid` = '%d' AND `name` = '%s'", escape_string(uname), cid, escape_string(setting));
171     res = mysql_use();
172     if ((row = mysql_fetch_row(res)) != NULL) {
173         return row[0];
174     } else
175         return NULL;
176 }
177
178 static void setSetting(struct UserNode *user, struct ChanNode *chan, const char *setting, const char *value) {
179     char *uname = "";
180     int cid = 0;
181     MYSQL_RES *res;
182     MYSQL_ROW row;
183     if(user) {
184         uname = ((user->flags & USERFLAG_ISAUTHED) ? user->auth : "*");
185     }
186     if(chan) {
187         loadChannelSettings(chan);
188         if(chan->flags & CHANFLAG_CHAN_REGISTERED)
189             cid = chan->channel_id;
190     }
191     printf_mysql_query("SELECT `id`, `value` FROM `fundata` WHERE `user` = '%s' AND `cid` = '%d' AND `name` = '%s'", escape_string(uname), cid, escape_string(setting));
192     res = mysql_use();
193     if ((row = mysql_fetch_row(res)) != NULL) {
194         if(strcmp(row[1], value))
195             printf_mysql_query("UPDATE `fundata` SET `value` = '%s' WHERE `id` = '%s'", escape_string(value), row[0]);
196     } else
197         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));
198 }
199
200 #include "cmd_funcmds_bomb.c"
201
202
203 CMD_BIND(funcmd_ping) {
204     FUNCMD_HEADER;
205     funcmd_reply(header, "\002%s\002: Pong!", REPLYTYPE_NORMAL, user->nick);
206     FUNCMD_FOOTER;
207 }
208
209 CMD_BIND(funcmd_pong) {
210     FUNCMD_HEADER;
211     funcmd_reply(header, "\002%s\002: Ping!", REPLYTYPE_NORMAL, user->nick);
212     FUNCMD_FOOTER;
213 }
214
215 CMD_BIND(funcmd_dice) {
216     FUNCMD_HEADER;
217     int max = atoi(argv[0]);
218     if(max > 1) {
219         int val = (rand() % max) + 1;
220         funcmd_reply(header, "FUN_DICE", REPLYTYPE_NORMAL, user->nick, val, max);
221     } else
222         funcmd_reply(header, "FUN_DICE_NUM", REPLYTYPE_NORMAL, argv[0]);
223     FUNCMD_FOOTER;
224 }
225
226 CMD_BIND(funcmd_8ball) {
227     FUNCMD_HEADER;
228     char *message = merge_argv(argv, 0, argc);
229     const char *const_replies = get_language_string(header->user, "FUN_8BALL_REPLIES");
230     char replies[MAXLEN];
231     int i, reply_count = 1;
232     for(i = 0; const_replies[i]; i++) {
233         if(const_replies[i] == '|')
234             reply_count++;
235         replies[i] = const_replies[i];
236     }
237     replies[i] = '\0';
238     unsigned int crc32_val = (crc32(message)) % reply_count;
239     char *creply = (crc32_val == 0 ? replies : NULL);
240     reply_count = 0;
241     for(i = 0; replies[i]; i++) {
242         if(replies[i] == '|') {
243             if(creply) {
244                 replies[i] = '\0';
245                 break;
246             } else {
247                 reply_count++;
248                 if(reply_count == crc32_val) {
249                     creply = &replies[i+1];
250                 }
251             }
252         }
253     }
254     if(creply) {
255         funcmd_reply(header, "FUN_8BALL", REPLYTYPE_NORMAL, user->nick, creply);
256     }
257     FUNCMD_FOOTER;
258 }
259
260 CMD_BIND(funcmd_cookie) {
261     FUNCMD_HEADER;
262     if(argc) {
263         if(!(user = getUserByNick(argv[0]))) {
264             reply(header->client, header->user, "NS_USER_UNKNOWN", argv[0]);
265             FUNCMD_FOOTER;
266             return;
267         }
268     }
269     char *tmp;
270     int user_count = ((tmp = getSetting(user, chan, "cookies")) ? atoi(tmp) : 0);
271     int total_count = ((tmp = getSetting(user, NULL, "cookies")) ? atoi(tmp) : 0);
272     user_count++;
273     total_count++;
274     char buf[10];
275     sprintf(buf, "%d", user_count);
276     setSetting(user, chan, "cookies", buf);
277     sprintf(buf, "%d", total_count);
278     setSetting(user, NULL, "cookies", buf);
279     funcmd_reply(header, "FUN_COOKIE", REPLYTYPE_ACTION, user->nick, total_count, user_count);
280     FUNCMD_FOOTER;
281 }