added structure for future fun-commands
[NeonServV5.git] / src / cmd_funcmds.c
diff --git a/src/cmd_funcmds.c b/src/cmd_funcmds.c
new file mode 100644 (file)
index 0000000..1c5d8c2
--- /dev/null
@@ -0,0 +1,83 @@
+/* cmd_funcmds.c - NeonServ v5.2
+ * Copyright (C) 2011  Philipp Kreil (pk910)
+ * 
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. 
+ */
+
+#include "cmd_funcmds.h"
+#include "modcmd.h"
+#include "mysqlConn.h"
+#include "IRCParser.h"
+#include "ClientSocket.h"
+#include "UserNode.h"
+#include "ChanNode.h"
+
+struct current_funcmd_header {
+    struct ClientSocket *client;
+    struct UserNode *user;
+    struct ChanNode *chan;
+    char send_notice;
+};
+
+static struct current_funcmd_header current_funcmd;
+
+#define FUNCMD_HEADER \
+current_funcmd.client = getTextBot(); \
+current_funcmd.user = user; \
+current_funcmd.chan = chan; \
+{\
+    MYSQL_RES *res; \
+    MYSQL_ROW row; \
+    printf_mysql_query("SELECT `channel_toys` FROM `channels` WHERE `channel_name` = '%s'", escape_string(chan->name)); \
+    res = mysql_use(); \
+    row = mysql_fetch_row(res); \
+    if(!row || !strcmp(row[0], "0")) { \
+        reply(getTextBot(), user, "NS_FUN_DISABLED", chan->name); \
+        return; \
+    } else if(!strcmp(row[0], "1")) \
+        current_funcmd.send_notice = 1; \
+    else \
+        current_funcmd.send_notice = 0; \
+}
+
+
+static void funcmd_reply(const char *text, ...) {
+    char formatBuf[MAXLEN];
+    if(current_funcmd.send_notice)
+        sprintf(formatBuf, "NOTICE %s :%s", current_funcmd.user->nick, text);
+    else
+        sprintf(formatBuf, "PRIVMSG %s :%s", current_funcmd.chan->name, text);
+    va_list arg_list;
+    char sendBuf[MAXLEN];
+    int pos;
+    if (!(current_funcmd.client->flags & SOCKET_FLAG_CONNECTED)) return;
+    sendBuf[0] = '\0';
+    va_start(arg_list, text);
+    pos = vsnprintf(sendBuf, MAXLEN - 2, formatBuf, arg_list);
+    va_end(arg_list);
+    if (pos < 0 || pos > (MAXLEN - 2)) pos = MAXLEN - 2;
+    sendBuf[pos] = '\n';
+    sendBuf[pos+1] = '\0';
+    write_socket(current_funcmd.client, sendBuf, pos+1);
+}
+
+CMD_BIND(funcmd_ping) {
+    FUNCMD_HEADER;
+    funcmd_reply("\002%s\002: Pong!", user->nick);
+}
+
+CMD_BIND(funcmd_pong) {
+    FUNCMD_HEADER;
+    funcmd_reply("\002%s\002: Ping!", user->nick);
+}