Another year is about to end... So we have to update these damn copyright information :P
[NeonServV5.git] / src / cmd_funcmds.c
1 /* cmd_funcmds.c - NeonServ v5.3
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
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 struct current_funcmd_header {
44     struct ClientSocket *client;
45     struct UserNode *user;
46     struct ChanNode *chan;
47     char send_notice;
48 };
49
50 static struct current_funcmd_header current_funcmd;
51
52 #define FUNCMD_HEADER \
53 current_funcmd.client = getTextBot(); \
54 current_funcmd.user = user; \
55 current_funcmd.chan = chan; \
56 {\
57     MYSQL_RES *res; \
58     MYSQL_ROW row; \
59     printf_mysql_query("SELECT `channel_toys` FROM `channels` WHERE `channel_name` = '%s'", escape_string(chan->name)); \
60     res = mysql_use(); \
61     row = mysql_fetch_row(res); \
62     if(!row || !strcmp(row[0], "0")) { \
63         reply(getTextBot(), user, "NS_FUN_DISABLED", chan->name); \
64         return; \
65     } else if(!strcmp(row[0], "1")) \
66         current_funcmd.send_notice = 1; \
67     else \
68         current_funcmd.send_notice = 0; \
69 }
70
71 #define REPLYTYPE_NORMAL 0
72 #define REPLYTYPE_ACTION 1
73 static void funcmd_reply(const char *text, int type, ...) {
74     if (!(current_funcmd.client->flags & SOCKET_FLAG_CONNECTED)) return;
75     const char *reply_format = get_language_string(current_funcmd.user, text);
76     if(reply_format)
77         text = reply_format;
78     char formatBuf[MAXLEN];
79     if(current_funcmd.send_notice) {
80         if(type == REPLYTYPE_ACTION)
81             sprintf(formatBuf, "NOTICE %s :%s %s", current_funcmd.user->nick, current_funcmd.client->user->nick, text);
82         else
83             sprintf(formatBuf, "NOTICE %s :%s", current_funcmd.user->nick, text);
84     } else {
85         if(type == REPLYTYPE_ACTION)
86             sprintf(formatBuf, "PRIVMSG %s :\001ACTION %s\001", current_funcmd.chan->name, text);
87         else
88             sprintf(formatBuf, "PRIVMSG %s :%s", current_funcmd.chan->name, text);
89     }
90     va_list arg_list;
91     char sendBuf[MAXLEN];
92     int pos;
93     sendBuf[0] = '\0';
94     va_start(arg_list, type);
95     pos = vsnprintf(sendBuf, MAXLEN - 2, formatBuf, arg_list);
96     va_end(arg_list);
97     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
98     sendBuf[pos] = '\n';
99     sendBuf[pos+1] = '\0';
100     write_socket(current_funcmd.client, sendBuf, pos+1);
101 }
102
103 static char* getSetting(struct UserNode *user, struct ChanNode *chan, const char *setting) {
104     char *uname = "";
105     int cid = 0;
106     MYSQL_RES *res;
107     MYSQL_ROW row;
108     if(user) {
109         uname = ((user->flags & USERFLAG_ISAUTHED) ? user->auth : "*");
110     }
111     if(chan) {
112         loadChannelSettings(chan);
113         if(chan->flags & CHANFLAG_CHAN_REGISTERED)
114             cid = chan->channel_id;
115     }
116     printf_mysql_query("SELECT `value` FROM `fundata` WHERE `user` = '%s' AND `cid` = '%d' AND `name` = '%s'", escape_string(uname), cid, escape_string(setting));
117     res = mysql_use();
118     if ((row = mysql_fetch_row(res)) != NULL) {
119         return row[0];
120     } else
121         return NULL;
122 }
123
124 static void setSetting(struct UserNode *user, struct ChanNode *chan, const char *setting, const char *value) {
125     char *uname = "";
126     int cid = 0;
127     MYSQL_RES *res;
128     MYSQL_ROW row;
129     if(user) {
130         uname = ((user->flags & USERFLAG_ISAUTHED) ? user->auth : "*");
131     }
132     if(chan) {
133         loadChannelSettings(chan);
134         if(chan->flags & CHANFLAG_CHAN_REGISTERED)
135             cid = chan->channel_id;
136     }
137     printf_mysql_query("SELECT `id`, `value` FROM `fundata` WHERE `user` = '%s' AND `cid` = '%d' AND `name` = '%s'", escape_string(uname), cid, escape_string(setting));
138     res = mysql_use();
139     if ((row = mysql_fetch_row(res)) != NULL) {
140         if(strcmp(row[1], value))
141             printf_mysql_query("UPDATE `fundata` SET `value` = '%s' WHERE `id` = '%s'", escape_string(value), row[0]);
142     } else
143         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));
144 }
145
146 CMD_BIND(funcmd_ping) {
147     FUNCMD_HEADER;
148     funcmd_reply("\002%s\002: Pong!", REPLYTYPE_NORMAL, user->nick);
149 }
150
151 CMD_BIND(funcmd_pong) {
152     FUNCMD_HEADER;
153     funcmd_reply("\002%s\002: Ping!", REPLYTYPE_NORMAL, user->nick);
154 }
155
156 CMD_BIND(funcmd_dice) {
157     FUNCMD_HEADER;
158     int max = atoi(argv[0]);
159     if(max > 1) {
160         int val = (rand() % max) + 1;
161         funcmd_reply("FUN_DICE", REPLYTYPE_NORMAL, user->nick, val, max);
162     } else
163         funcmd_reply("FUN_DICE_NUM", REPLYTYPE_NORMAL, argv[0]);
164 }
165
166 CMD_BIND(funcmd_8ball) {
167     FUNCMD_HEADER;
168     char *message = merge_argv(argv, 0, argc);
169     const char *const_replies = get_language_string(current_funcmd.user, "FUN_8BALL_REPLIES");
170     char replies[MAXLEN];
171     int i, reply_count = 1;
172     for(i = 0; const_replies[i]; i++) {
173         if(const_replies[i] == '|')
174             reply_count++;
175         replies[i] = const_replies[i];
176     }
177     replies[i] = '\0';
178     unsigned int crc32_val = (crc32(message)) % reply_count;
179     char *creply = (crc32_val == 0 ? replies : NULL);
180     reply_count = 0;
181     for(i = 0; replies[i]; i++) {
182         if(replies[i] == '|') {
183             if(creply) {
184                 replies[i] = '\0';
185                 break;
186             } else {
187                 reply_count++;
188                 if(reply_count == crc32_val) {
189                     creply = &replies[i+1];
190                 }
191             }
192         }
193     }
194     if(creply) {
195         funcmd_reply("FUN_8BALL", REPLYTYPE_NORMAL, user->nick, creply);
196     }
197 }
198
199 CMD_BIND(funcmd_cookie) {
200     FUNCMD_HEADER;
201     if(argc) {
202         if(!(user = getUserByNick(argv[0])))
203             reply(current_funcmd.client, current_funcmd.user, "NS_USER_UNKNOWN", argv[0]);
204     }
205     char *tmp;
206     int user_count = ((tmp = getSetting(user, chan, "cookies")) ? atoi(tmp) : 0);
207     int total_count = ((tmp = getSetting(user, NULL, "cookies")) ? atoi(tmp) : 0);
208     user_count++;
209     total_count++;
210     char buf[10];
211     sprintf(buf, "%d", user_count);
212     setSetting(user, chan, "cookies", buf);
213     sprintf(buf, "%d", total_count);
214     setSetting(user, NULL, "cookies", buf);
215     funcmd_reply("FUN_COOKIE", REPLYTYPE_ACTION, user->nick, total_count, user_count);
216 }