*** VERSION 5.4.0 ***
[NeonServV5.git] / src / modules / funcmd.mod / cmd_funcmds.c
1 /* cmd_funcmds.c - NeonServ v5.4
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
29 static const struct default_language_entry msgtab[] = {
30     {"FUN_DICE", "$b%s$b: A $b%d$b shows on the %d-sided die."}, /* {ARGS: "TestUser", 5, 6} */
31     {"FUN_DICE_NUM", "I do not understand $b%s$b. Please use a single number above 1."}, /* {ARGS: "bla"} */
32     {"FUN_8BALL", "$b%s$b: %s"}, /* {ARGS: "TestUser", "Not a chance."} */
33     {"FUN_8BALL_REPLIES", "Not a chance.|In your dreams.|Absolutely!|Could be, could be.|No!"},
34     {"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} */
35     {NULL, NULL}
36 };
37
38 void init_funcmds() {
39     register_default_language_table(msgtab);
40     srand(time(NULL));
41 }
42
43 void register_commands() {
44     //Fun Commands
45     register_command_alias(3, "FunCMD");
46     #define USER_COMMAND(NAME,FUNCTION,PARAMCOUNT,FLAGS) register_command(3, NAME, module_id, FUNCTION, PARAMCOUNT, NULL, 0, FLAGS)
47     //               NAME              FUNCTION        PARAMS   FLAGS
48     //USER_COMMAND("extscript",    neonserv_cmd_extscript, 0,  CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_EMPTY_ARGS | CMDFLAG_CHAN_PARAM | CMDFLAG_FUNCMD);
49     USER_COMMAND("ping",         funcmd_ping,            0,  CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_FUNCMD);
50     USER_COMMAND("pong",         funcmd_pong,            0,  CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_FUNCMD);
51     USER_COMMAND("dice",         funcmd_dice,            1,  CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_FUNCMD);
52     USER_COMMAND("8ball",        funcmd_8ball,           1,  CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_FUNCMD);
53     USER_COMMAND("cookie",       funcmd_cookie,          0,  CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_FUNCMD);
54     #undef USER_COMMAND
55 }
56
57 struct current_funcmd_header {
58     struct ClientSocket *client;
59     struct UserNode *user;
60     struct ChanNode *chan;
61     char send_notice;
62 };
63
64 static struct current_funcmd_header current_funcmd;
65
66 #define FUNCMD_HEADER \
67 current_funcmd.client = getTextBot(); \
68 current_funcmd.user = user; \
69 current_funcmd.chan = chan; \
70 {\
71     MYSQL_RES *res; \
72     MYSQL_ROW row; \
73     printf_mysql_query("SELECT `channel_toys` FROM `channels` WHERE `channel_name` = '%s'", escape_string(chan->name)); \
74     res = mysql_use(); \
75     row = mysql_fetch_row(res); \
76     if(!row || !strcmp(row[0], "0")) { \
77         reply(getTextBot(), user, "NS_FUN_DISABLED", chan->name); \
78         return; \
79     } else if(!strcmp(row[0], "1")) \
80         current_funcmd.send_notice = 1; \
81     else \
82         current_funcmd.send_notice = 0; \
83 }
84
85 #define REPLYTYPE_NORMAL 0
86 #define REPLYTYPE_ACTION 1
87 static void funcmd_reply(const char *text, int type, ...) {
88     if (!(current_funcmd.client->flags & SOCKET_FLAG_CONNECTED)) return;
89     const char *reply_format = get_language_string(current_funcmd.user, text);
90     if(reply_format)
91         text = reply_format;
92     char formatBuf[MAXLEN];
93     if(current_funcmd.send_notice) {
94         if(type == REPLYTYPE_ACTION)
95             sprintf(formatBuf, "NOTICE %s :%s %s", current_funcmd.user->nick, current_funcmd.client->user->nick, text);
96         else
97             sprintf(formatBuf, "NOTICE %s :%s", current_funcmd.user->nick, text);
98     } else {
99         if(type == REPLYTYPE_ACTION)
100             sprintf(formatBuf, "PRIVMSG %s :\001ACTION %s\001", current_funcmd.chan->name, text);
101         else
102             sprintf(formatBuf, "PRIVMSG %s :%s", current_funcmd.chan->name, text);
103     }
104     va_list arg_list;
105     char sendBuf[MAXLEN];
106     int pos;
107     sendBuf[0] = '\0';
108     va_start(arg_list, type);
109     pos = vsnprintf(sendBuf, MAXLEN - 2, formatBuf, arg_list);
110     va_end(arg_list);
111     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
112     sendBuf[pos] = '\n';
113     sendBuf[pos+1] = '\0';
114     write_socket(current_funcmd.client, sendBuf, pos+1);
115 }
116
117 static char* getSetting(struct UserNode *user, struct ChanNode *chan, const char *setting) {
118     char *uname = "";
119     int cid = 0;
120     MYSQL_RES *res;
121     MYSQL_ROW row;
122     if(user) {
123         uname = ((user->flags & USERFLAG_ISAUTHED) ? user->auth : "*");
124     }
125     if(chan) {
126         loadChannelSettings(chan);
127         if(chan->flags & CHANFLAG_CHAN_REGISTERED)
128             cid = chan->channel_id;
129     }
130     printf_mysql_query("SELECT `value` FROM `fundata` WHERE `user` = '%s' AND `cid` = '%d' AND `name` = '%s'", escape_string(uname), cid, escape_string(setting));
131     res = mysql_use();
132     if ((row = mysql_fetch_row(res)) != NULL) {
133         return row[0];
134     } else
135         return NULL;
136 }
137
138 static void setSetting(struct UserNode *user, struct ChanNode *chan, const char *setting, const char *value) {
139     char *uname = "";
140     int cid = 0;
141     MYSQL_RES *res;
142     MYSQL_ROW row;
143     if(user) {
144         uname = ((user->flags & USERFLAG_ISAUTHED) ? user->auth : "*");
145     }
146     if(chan) {
147         loadChannelSettings(chan);
148         if(chan->flags & CHANFLAG_CHAN_REGISTERED)
149             cid = chan->channel_id;
150     }
151     printf_mysql_query("SELECT `id`, `value` FROM `fundata` WHERE `user` = '%s' AND `cid` = '%d' AND `name` = '%s'", escape_string(uname), cid, escape_string(setting));
152     res = mysql_use();
153     if ((row = mysql_fetch_row(res)) != NULL) {
154         if(strcmp(row[1], value))
155             printf_mysql_query("UPDATE `fundata` SET `value` = '%s' WHERE `id` = '%s'", escape_string(value), row[0]);
156     } else
157         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));
158 }
159
160 CMD_BIND(funcmd_ping) {
161     FUNCMD_HEADER;
162     funcmd_reply("\002%s\002: Pong!", REPLYTYPE_NORMAL, user->nick);
163 }
164
165 CMD_BIND(funcmd_pong) {
166     FUNCMD_HEADER;
167     funcmd_reply("\002%s\002: Ping!", REPLYTYPE_NORMAL, user->nick);
168 }
169
170 CMD_BIND(funcmd_dice) {
171     FUNCMD_HEADER;
172     int max = atoi(argv[0]);
173     if(max > 1) {
174         int val = (rand() % max) + 1;
175         funcmd_reply("FUN_DICE", REPLYTYPE_NORMAL, user->nick, val, max);
176     } else
177         funcmd_reply("FUN_DICE_NUM", REPLYTYPE_NORMAL, argv[0]);
178 }
179
180 CMD_BIND(funcmd_8ball) {
181     FUNCMD_HEADER;
182     char *message = merge_argv(argv, 0, argc);
183     const char *const_replies = get_language_string(current_funcmd.user, "FUN_8BALL_REPLIES");
184     char replies[MAXLEN];
185     int i, reply_count = 1;
186     for(i = 0; const_replies[i]; i++) {
187         if(const_replies[i] == '|')
188             reply_count++;
189         replies[i] = const_replies[i];
190     }
191     replies[i] = '\0';
192     unsigned int crc32_val = (crc32(message)) % reply_count;
193     char *creply = (crc32_val == 0 ? replies : NULL);
194     reply_count = 0;
195     for(i = 0; replies[i]; i++) {
196         if(replies[i] == '|') {
197             if(creply) {
198                 replies[i] = '\0';
199                 break;
200             } else {
201                 reply_count++;
202                 if(reply_count == crc32_val) {
203                     creply = &replies[i+1];
204                 }
205             }
206         }
207     }
208     if(creply) {
209         funcmd_reply("FUN_8BALL", REPLYTYPE_NORMAL, user->nick, creply);
210     }
211 }
212
213 CMD_BIND(funcmd_cookie) {
214     FUNCMD_HEADER;
215     if(argc) {
216         if(!(user = getUserByNick(argv[0]))) {
217             reply(current_funcmd.client, current_funcmd.user, "NS_USER_UNKNOWN", argv[0]);
218             return;
219         }
220     }
221     char *tmp;
222     int user_count = ((tmp = getSetting(user, chan, "cookies")) ? atoi(tmp) : 0);
223     int total_count = ((tmp = getSetting(user, NULL, "cookies")) ? atoi(tmp) : 0);
224     user_count++;
225     total_count++;
226     char buf[10];
227     sprintf(buf, "%d", user_count);
228     setSetting(user, chan, "cookies", buf);
229     sprintf(buf, "%d", total_count);
230     setSetting(user, NULL, "cookies", buf);
231     funcmd_reply("FUN_COOKIE", REPLYTYPE_ACTION, user->nick, total_count, user_count);
232 }