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