added funcmd_8ball
[NeonServV5.git] / src / cmd_funcmds.c
1 /* cmd_funcmds.c - NeonServ v5.2
2  * Copyright (C) 2011  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
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
28 static const struct default_language_entry msgtab[] = {
29     {"FUN_DICE", "$b%s$b: A $b%d$b shows on the %d-sided die."}, /* {ARGS: "TestUser", 5, 6} */
30     {"FUN_DICE_NUM", "I do not understand $b%s$b. Please use a single number above 1."}, /* {ARGS: "bla"} */
31     {"FUN_8BALL", "$b%s$b: %s"}, /* {ARGS: "TestUser", "Not a chance."} */
32     {"FUN_8BALL_REPLIES", "Not a chance.|In your dreams.|Absolutely!|Could be, could be.|No!"},
33     {NULL, NULL}
34 };
35
36 void init_funcmds() {
37     register_default_language_table(msgtab);
38     srand(time(NULL));
39 }
40
41 struct current_funcmd_header {
42     struct ClientSocket *client;
43     struct UserNode *user;
44     struct ChanNode *chan;
45     char send_notice;
46 };
47
48 static struct current_funcmd_header current_funcmd;
49
50 #define FUNCMD_HEADER \
51 current_funcmd.client = getTextBot(); \
52 current_funcmd.user = user; \
53 current_funcmd.chan = chan; \
54 {\
55     MYSQL_RES *res; \
56     MYSQL_ROW row; \
57     printf_mysql_query("SELECT `channel_toys` FROM `channels` WHERE `channel_name` = '%s'", escape_string(chan->name)); \
58     res = mysql_use(); \
59     row = mysql_fetch_row(res); \
60     if(!row || !strcmp(row[0], "0")) { \
61         reply(getTextBot(), user, "NS_FUN_DISABLED", chan->name); \
62         return; \
63     } else if(!strcmp(row[0], "1")) \
64         current_funcmd.send_notice = 1; \
65     else \
66         current_funcmd.send_notice = 0; \
67 }
68
69 static void funcmd_reply(const char *text, ...) {
70     const char *reply_format = get_language_string(current_funcmd.user, text);
71     if(reply_format)
72         text = reply_format;
73     char formatBuf[MAXLEN];
74     if(current_funcmd.send_notice)
75         sprintf(formatBuf, "NOTICE %s :%s", current_funcmd.user->nick, text);
76     else
77         sprintf(formatBuf, "PRIVMSG %s :%s", current_funcmd.chan->name, text);
78     va_list arg_list;
79     char sendBuf[MAXLEN];
80     int pos;
81     if (!(current_funcmd.client->flags & SOCKET_FLAG_CONNECTED)) return;
82     sendBuf[0] = '\0';
83     va_start(arg_list, text);
84     pos = vsnprintf(sendBuf, MAXLEN - 2, formatBuf, arg_list);
85     va_end(arg_list);
86     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
87     sendBuf[pos] = '\n';
88     sendBuf[pos+1] = '\0';
89     write_socket(current_funcmd.client, sendBuf, pos+1);
90 }
91
92 CMD_BIND(funcmd_ping) {
93     FUNCMD_HEADER;
94     funcmd_reply("\002%s\002: Pong!", user->nick);
95 }
96
97 CMD_BIND(funcmd_pong) {
98     FUNCMD_HEADER;
99     funcmd_reply("\002%s\002: Ping!", user->nick);
100 }
101
102 CMD_BIND(funcmd_dice) {
103     FUNCMD_HEADER;
104     int max = atoi(argv[0]);
105     if(max > 1) {
106         int val = (rand() % max) + 1;
107         funcmd_reply("FUN_DICE", user->nick, val, max);
108     } else
109         funcmd_reply("FUN_DICE_NUM", argv[0]);
110 }
111
112 CMD_BIND(funcmd_8ball) {
113     FUNCMD_HEADER;
114     char *message = merge_argv(argv, 0, argc);
115     const char *const_replies = get_language_string(current_funcmd.user, "FUN_8BALL_REPLIES");
116     char replies[MAXLEN];
117     int i, reply_count = 1;
118     for(i = 0; const_replies[i]; i++) {
119         if(const_replies[i] == '|')
120             reply_count++;
121         replies[i] = const_replies[i];
122     }
123     replies[i] = '\0';
124     unsigned int crc32_val = (crc32(message)) % reply_count;
125     char *creply = (crc32_val == 0 ? replies : NULL);
126     reply_count = 0;
127     for(i = 0; replies[i]; i++) {
128         if(replies[i] == '|') {
129             if(creply) {
130                 replies[i] = '\0';
131                 break;
132             } else {
133                 reply_count++;
134                 if(reply_count == crc32_val) {
135                     creply = &replies[i+1];
136                 }
137             }
138         }
139     }
140     if(creply) {
141         funcmd_reply("FUN_8BALL", user->nick, creply);
142     }
143 }