added 2 small ASCII's to cmd_funcmds.c
[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     const char *reply_format = get_language_string((header->null_language ? NULL : header->user), text);
131     if(reply_format)
132         text = reply_format;
133     char formatBuf[MAXLEN];
134     if(header->send_notice || (type & REPLYTYPE_NOTICE)) {
135         if(type & REPLYTYPE_ACTION)
136             sprintf(formatBuf, "NOTICE %s :%s %s", header->user->nick, header->client->user->nick, text);
137         else
138             sprintf(formatBuf, "NOTICE %s :%s", header->user->nick, text);
139     } else {
140         if(type & REPLYTYPE_ACTION)
141             sprintf(formatBuf, "PRIVMSG %s :\001ACTION %s\001", header->chan->name, text);
142         else
143             sprintf(formatBuf, "PRIVMSG %s :%s", header->chan->name, text);
144     }
145     va_list arg_list;
146     char sendBuf[MAXLEN];
147     int pos;
148     sendBuf[0] = '\0';
149     va_start(arg_list, type);
150     pos = vsnprintf(sendBuf, MAXLEN - 2, formatBuf, arg_list);
151     va_end(arg_list);
152     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
153     sendBuf[pos] = '\n';
154     sendBuf[pos+1] = '\0';
155     write_socket(header->client, sendBuf, pos+1);
156 }
157
158 static char* getSetting(struct UserNode *user, struct ChanNode *chan, const char *setting) {
159     char *uname = "";
160     int cid = 0;
161     MYSQL_RES *res;
162     MYSQL_ROW row;
163     if(user) {
164         uname = ((user->flags & USERFLAG_ISAUTHED) ? user->auth : "*");
165     }
166     if(chan) {
167         loadChannelSettings(chan);
168         if(chan->flags & CHANFLAG_CHAN_REGISTERED)
169             cid = chan->channel_id;
170     }
171     printf_mysql_query("SELECT `value` FROM `fundata` WHERE `user` = '%s' AND `cid` = '%d' AND `name` = '%s'", escape_string(uname), cid, escape_string(setting));
172     res = mysql_use();
173     if ((row = mysql_fetch_row(res)) != NULL) {
174         return row[0];
175     } else
176         return NULL;
177 }
178
179 static void setSetting(struct UserNode *user, struct ChanNode *chan, const char *setting, const char *value) {
180     char *uname = "";
181     int cid = 0;
182     MYSQL_RES *res;
183     MYSQL_ROW row;
184     if(user) {
185         uname = ((user->flags & USERFLAG_ISAUTHED) ? user->auth : "*");
186     }
187     if(chan) {
188         loadChannelSettings(chan);
189         if(chan->flags & CHANFLAG_CHAN_REGISTERED)
190             cid = chan->channel_id;
191     }
192     printf_mysql_query("SELECT `id`, `value` FROM `fundata` WHERE `user` = '%s' AND `cid` = '%d' AND `name` = '%s'", escape_string(uname), cid, escape_string(setting));
193     res = mysql_use();
194     if ((row = mysql_fetch_row(res)) != NULL) {
195         if(strcmp(row[1], value))
196             printf_mysql_query("UPDATE `fundata` SET `value` = '%s' WHERE `id` = '%s'", escape_string(value), row[0]);
197     } else
198         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));
199 }
200
201 #include "cmd_funcmds_bomb.c"
202
203
204 CMD_BIND(funcmd_ping) {
205     FUNCMD_HEADER;
206     funcmd_reply(header, "\002%s\002: Pong!", REPLYTYPE_NORMAL, user->nick);
207     FUNCMD_FOOTER;
208 }
209
210 CMD_BIND(funcmd_pong) {
211     FUNCMD_HEADER;
212     funcmd_reply(header, "\002%s\002: Ping!", REPLYTYPE_NORMAL, user->nick);
213     FUNCMD_FOOTER;
214 }
215
216 CMD_BIND(funcmd_dice) {
217     FUNCMD_HEADER;
218     int max = atoi(argv[0]);
219     if(max > 1) {
220         int val = (rand() % max) + 1;
221         funcmd_reply(header, "%s", REPLYTYPE_NORMAL, "     ____");
222         funcmd_reply(header, "%s", REPLYTYPE_NORMAL, "    /\\' .\\    _____");
223         funcmd_reply(header, "%s", REPLYTYPE_NORMAL, "   /: \\___\\  / .  /\\");
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, "FUN_DICE", REPLYTYPE_NORMAL, user->nick, val, max);
228     } else
229         funcmd_reply(header, "FUN_DICE_NUM", REPLYTYPE_NORMAL, argv[0]);
230     FUNCMD_FOOTER;
231 }
232
233 CMD_BIND(funcmd_8ball) {
234     FUNCMD_HEADER;
235     char *message = merge_argv(argv, 0, argc);
236     const char *const_replies = get_language_string(header->user, "FUN_8BALL_REPLIES");
237     char replies[MAXLEN];
238     int i, reply_count = 1;
239     for(i = 0; const_replies[i]; i++) {
240         if(const_replies[i] == '|')
241             reply_count++;
242         replies[i] = const_replies[i];
243     }
244     replies[i] = '\0';
245     unsigned int crc32_val = (crc32(message)) % reply_count;
246     char *creply = (crc32_val == 0 ? replies : NULL);
247     reply_count = 0;
248     for(i = 0; replies[i]; i++) {
249         if(replies[i] == '|') {
250             if(creply) {
251                 replies[i] = '\0';
252                 break;
253             } else {
254                 reply_count++;
255                 if(reply_count == crc32_val) {
256                     creply = &replies[i+1];
257                 }
258             }
259         }
260     }
261     if(creply) {
262         funcmd_reply(header, "FUN_8BALL", REPLYTYPE_NORMAL, user->nick, creply);
263     }
264     FUNCMD_FOOTER;
265 }
266
267 CMD_BIND(funcmd_cookie) {
268     FUNCMD_HEADER;
269     if(argc) {
270         if(!(user = getUserByNick(argv[0]))) {
271             reply(header->client, header->user, "NS_USER_UNKNOWN", argv[0]);
272             FUNCMD_FOOTER;
273             return;
274         }
275     }
276     char *tmp;
277     int user_count = ((tmp = getSetting(user, chan, "cookies")) ? atoi(tmp) : 0);
278     int total_count = ((tmp = getSetting(user, NULL, "cookies")) ? atoi(tmp) : 0);
279     user_count++;
280     total_count++;
281     char buf[10];
282     sprintf(buf, "%d", user_count);
283     setSetting(user, chan, "cookies", buf);
284     sprintf(buf, "%d", total_count);
285     setSetting(user, NULL, "cookies", buf);
286     funcmd_reply(header, "FUN_COOKIE", REPLYTYPE_ACTION, user->nick, total_count, user_count);
287     FUNCMD_FOOTER;
288 }
289
290 CMD_BIND(funcmd_ship) {
291     FUNCMD_HEADER;
292     funcmd_reply(header, "%s", REPLYTYPE_NORMAL, "\0030,0------------------------\0031|");
293     funcmd_reply(header, "%s", REPLYTYPE_NORMAL, "\0030,0----------------------\00314)_)_)");
294     funcmd_reply(header, "%s%c%s", REPLYTYPE_NORMAL, "\0030,0--------------------\0031,4\002=", header->client->user->nick[0], "=\002\00314,0|)___)");
295     funcmd_reply(header, "%s", REPLYTYPE_NORMAL, "\0030,0-------------------\00314)__) )___) )");
296     funcmd_reply(header, "%s", REPLYTYPE_NORMAL, "\0030,0------------------\00314)___) )____))_)");
297     funcmd_reply(header, "%s", REPLYTYPE_NORMAL, "\0030,0----------------\00314)____)_____))__)\\");
298     funcmd_reply(header, "%s", REPLYTYPE_NORMAL, "\0030,0---------------\00314)\0035__\00314|\0035____\00314/|\0035___\00314|\0035___\00314-\\\\---");
299     funcmd_reply(header, "%s", REPLYTYPE_NORMAL, "\0030,0-------\00312^^^^^^\0035~~~\\ \002oo oo oo oo\002 /~~\00312^^^^^^");
300     funcmd_reply(header, "%s", REPLYTYPE_NORMAL, "\0030,0------------\00312~^^^^ ~~~~^^~~~~^^~~^^~~~~~");
301     funcmd_reply(header, "%s", REPLYTYPE_NORMAL, "\0030,0-----------------\00312~~^^ ~^^~ ~^~ ~^");
302     FUNCMD_FOOTER;
303 }