added structure for future fun-commands
[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
26 struct current_funcmd_header {
27     struct ClientSocket *client;
28     struct UserNode *user;
29     struct ChanNode *chan;
30     char send_notice;
31 };
32
33 static struct current_funcmd_header current_funcmd;
34
35 #define FUNCMD_HEADER \
36 current_funcmd.client = getTextBot(); \
37 current_funcmd.user = user; \
38 current_funcmd.chan = chan; \
39 {\
40     MYSQL_RES *res; \
41     MYSQL_ROW row; \
42     printf_mysql_query("SELECT `channel_toys` FROM `channels` WHERE `channel_name` = '%s'", escape_string(chan->name)); \
43     res = mysql_use(); \
44     row = mysql_fetch_row(res); \
45     if(!row || !strcmp(row[0], "0")) { \
46         reply(getTextBot(), user, "NS_FUN_DISABLED", chan->name); \
47         return; \
48     } else if(!strcmp(row[0], "1")) \
49         current_funcmd.send_notice = 1; \
50     else \
51         current_funcmd.send_notice = 0; \
52 }
53
54
55 static void funcmd_reply(const char *text, ...) {
56     char formatBuf[MAXLEN];
57     if(current_funcmd.send_notice)
58         sprintf(formatBuf, "NOTICE %s :%s", current_funcmd.user->nick, text);
59     else
60         sprintf(formatBuf, "PRIVMSG %s :%s", current_funcmd.chan->name, text);
61     va_list arg_list;
62     char sendBuf[MAXLEN];
63     int pos;
64     if (!(current_funcmd.client->flags & SOCKET_FLAG_CONNECTED)) return;
65     sendBuf[0] = '\0';
66     va_start(arg_list, text);
67     pos = vsnprintf(sendBuf, MAXLEN - 2, formatBuf, arg_list);
68     va_end(arg_list);
69     if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
70     sendBuf[pos] = '\n';
71     sendBuf[pos+1] = '\0';
72     write_socket(current_funcmd.client, sendBuf, pos+1);
73 }
74
75 CMD_BIND(funcmd_ping) {
76     FUNCMD_HEADER;
77     funcmd_reply("\002%s\002: Pong!", user->nick);
78 }
79
80 CMD_BIND(funcmd_pong) {
81     FUNCMD_HEADER;
82     funcmd_reply("\002%s\002: Ping!", user->nick);
83 }