1 /* cmd_funcmds.c - NeonServ v5.6
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/>.
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"
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} */
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
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);
70 register_default_language_table(msgtab);
72 bind_freeuser(funcmd_bomb_freeuser, module_id);
73 bind_freechan(funcmd_bomb_freechan, module_id);
74 bind_freeclient(funcmd_bomb_freeclient, module_id);
77 void register_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);
92 struct funcmd_header_info {
93 struct ClientSocket *client;
94 struct UserNode *user;
95 struct ChanNode *chan;
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; \
109 printf_mysql_query("SELECT `channel_toys` FROM `channels` WHERE `channel_name` = '%s'", escape_string(chan->name)); \
111 row = mysql_fetch_row(res); \
112 if(!row || !strcmp(row[0], "0")) { \
113 reply(textclient, user, "NS_FUN_DISABLED", chan->name); \
115 } else if(!strcmp(row[0], "1")) \
116 header->send_notice = 1; \
118 header->send_notice = 0; \
121 #define FUNCMD_FOOTER \
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);
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);
137 sprintf(formatBuf, "NOTICE %s :%s", header->user->nick, text);
139 if(type & REPLYTYPE_ACTION)
140 sprintf(formatBuf, "PRIVMSG %s :\001ACTION %s\001", header->chan->name, text);
142 sprintf(formatBuf, "PRIVMSG %s :%s", header->chan->name, text);
145 char sendBuf[MAXLEN];
148 va_start(arg_list, type);
149 pos = vsnprintf(sendBuf, MAXLEN - 2, formatBuf, arg_list);
151 if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
153 sendBuf[pos+1] = '\0';
154 write_socket(header->client, sendBuf, pos+1);
157 static char* getSetting(struct UserNode *user, struct ChanNode *chan, const char *setting) {
163 uname = ((user->flags & USERFLAG_ISAUTHED) ? user->auth : "*");
166 loadChannelSettings(chan);
167 if(chan->flags & CHANFLAG_CHAN_REGISTERED)
168 cid = chan->channel_id;
170 printf_mysql_query("SELECT `value` FROM `fundata` WHERE `user` = '%s' AND `cid` = '%d' AND `name` = '%s'", escape_string(uname), cid, escape_string(setting));
172 if ((row = mysql_fetch_row(res)) != NULL) {
178 static void setSetting(struct UserNode *user, struct ChanNode *chan, const char *setting, const char *value) {
184 uname = ((user->flags & USERFLAG_ISAUTHED) ? user->auth : "*");
187 loadChannelSettings(chan);
188 if(chan->flags & CHANFLAG_CHAN_REGISTERED)
189 cid = chan->channel_id;
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));
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]);
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));
200 #include "cmd_funcmds_bomb.c"
203 CMD_BIND(funcmd_ping) {
205 funcmd_reply(header, "\002%s\002: Pong!", REPLYTYPE_NORMAL, user->nick);
209 CMD_BIND(funcmd_pong) {
211 funcmd_reply(header, "\002%s\002: Ping!", REPLYTYPE_NORMAL, user->nick);
215 CMD_BIND(funcmd_dice) {
217 int max = atoi(argv[0]);
219 int val = (rand() % max) + 1;
220 funcmd_reply(header, "FUN_DICE", REPLYTYPE_NORMAL, user->nick, val, max);
222 funcmd_reply(header, "FUN_DICE_NUM", REPLYTYPE_NORMAL, argv[0]);
226 CMD_BIND(funcmd_8ball) {
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] == '|')
235 replies[i] = const_replies[i];
238 unsigned int crc32_val = (crc32(message)) % reply_count;
239 char *creply = (crc32_val == 0 ? replies : NULL);
241 for(i = 0; replies[i]; i++) {
242 if(replies[i] == '|') {
248 if(reply_count == crc32_val) {
249 creply = &replies[i+1];
255 funcmd_reply(header, "FUN_8BALL", REPLYTYPE_NORMAL, user->nick, creply);
260 CMD_BIND(funcmd_cookie) {
263 if(!(user = getUserByNick(argv[0]))) {
264 reply(header->client, header->user, "NS_USER_UNKNOWN", argv[0]);
270 int user_count = ((tmp = getSetting(user, chan, "cookies")) ? atoi(tmp) : 0);
271 int total_count = ((tmp = getSetting(user, NULL, "cookies")) ? atoi(tmp) : 0);
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);