initialize rand() seed on startup
[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
27 static const struct default_language_entry msgtab[] = {
28     {"FUN_DICE", "$b%s$b: A $b%d$b shows on the %d-sided die."}, /* {ARGS: "TestUser", 5, 6} */
29     {"FUN_DICE_NUM", "I do not understand $b%s$b. Please use a single number above 1."}, /* {ARGS: "bla"} */
30     {NULL, NULL}
31 };
32
33 void init_funcmds() {
34     register_default_language_table(msgtab);
35     srand(time(NULL));
36 }
37
38 struct current_funcmd_header {
39     struct ClientSocket *client;
40     struct UserNode *user;
41     struct ChanNode *chan;
42     char send_notice;
43 };
44
45 static struct current_funcmd_header current_funcmd;
46
47 #define FUNCMD_HEADER \
48 current_funcmd.client = getTextBot(); \
49 current_funcmd.user = user; \
50 current_funcmd.chan = chan; \
51 {\
52     MYSQL_RES *res; \
53     MYSQL_ROW row; \
54     printf_mysql_query("SELECT `channel_toys` FROM `channels` WHERE `channel_name` = '%s'", escape_string(chan->name)); \
55     res = mysql_use(); \
56     row = mysql_fetch_row(res); \
57     if(!row || !strcmp(row[0], "0")) { \
58         reply(getTextBot(), user, "NS_FUN_DISABLED", chan->name); \
59         return; \
60     } else if(!strcmp(row[0], "1")) \
61         current_funcmd.send_notice = 1; \
62     else \
63         current_funcmd.send_notice = 0; \
64 }
65
66 static void funcmd_reply(const char *text, ...) {
67     const char *reply_format = get_language_string(current_funcmd.user, text);
68     if(reply_format)
69         text = reply_format;
70     char formatBuf[MAXLEN];
71     if(current_funcmd.send_notice)
72         sprintf(formatBuf, "NOTICE %s :%s", current_funcmd.user->nick, text);
73     else
74         sprintf(formatBuf, "PRIVMSG %s :%s", current_funcmd.chan->name, text);
75     va_list arg_list;
76     char sendBuf[MAXLEN];
77     int pos;
78     if (!(current_funcmd.client->flags & SOCKET_FLAG_CONNECTED)) return;
79     sendBuf[0] = '\0';
80     va_start(arg_list, text);
81     pos = vsnprintf(sendBuf, MAXLEN - 2, formatBuf, arg_list);
82     va_end(arg_list);
83     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
84     sendBuf[pos] = '\n';
85     sendBuf[pos+1] = '\0';
86     write_socket(current_funcmd.client, sendBuf, pos+1);
87 }
88
89 CMD_BIND(funcmd_ping) {
90     FUNCMD_HEADER;
91     funcmd_reply("\002%s\002: Pong!", user->nick);
92 }
93
94 CMD_BIND(funcmd_pong) {
95     FUNCMD_HEADER;
96     funcmd_reply("\002%s\002: Ping!", user->nick);
97 }
98
99 CMD_BIND(funcmd_dice) {
100     FUNCMD_HEADER;
101     int max = atoi(argv[0]);
102     if(max > 1) {
103         int val = (rand() % max) + 1;
104         funcmd_reply("FUN_DICE", user->nick, val, max);
105     } else
106         funcmd_reply("FUN_DICE_NUM", argv[0]);
107 }