From 5eee43a7cfa37d5dd8d8a612a94de09a048b30c0 Mon Sep 17 00:00:00 2001 From: pk910 Date: Tue, 1 Nov 2011 23:54:12 +0100 Subject: [PATCH] added structure for future fun-commands --- Makefile.am | 1 + src/cmd_funcmds.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++ src/cmd_funcmds.h | 25 ++++++++++++++ src/commands.c | 15 +++++++-- src/modcmd.c | 7 +++- src/modcmd.h | 3 +- 6 files changed, 130 insertions(+), 4 deletions(-) create mode 100644 src/cmd_funcmds.c create mode 100644 src/cmd_funcmds.h diff --git a/Makefile.am b/Makefile.am index 121bb07..5bea9fd 100644 --- a/Makefile.am +++ b/Makefile.am @@ -112,6 +112,7 @@ neonserv_SOURCES = src/version.c \ src/cmd_neonserv_unvisited.c \ src/cmd_neonserv_extscript.c \ src/cmd_neonspam_set.c \ + src/cmd_funcmds.c \ src/lib/ini.c neonserv_LDADD = $(MYSQL_LIBS) $(WINSOCK_LIBS) diff --git a/src/cmd_funcmds.c b/src/cmd_funcmds.c new file mode 100644 index 0000000..1c5d8c2 --- /dev/null +++ b/src/cmd_funcmds.c @@ -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 . + */ + +#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); +} diff --git a/src/cmd_funcmds.h b/src/cmd_funcmds.h new file mode 100644 index 0000000..e012359 --- /dev/null +++ b/src/cmd_funcmds.h @@ -0,0 +1,25 @@ +/* cmd_funcmds.h - 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 . + */ +#ifndef _cmd_funcmds_h +#define _cmd_funcmds_h +#include "main.h" +#include "modcmd.h" + +CMD_BIND(funcmd_ping); +CMD_BIND(funcmd_pong); + +#endif \ No newline at end of file diff --git a/src/commands.c b/src/commands.c index 87e7305..51d4b3f 100644 --- a/src/commands.c +++ b/src/commands.c @@ -18,6 +18,7 @@ #include "cmd_global.h" #include "cmd_neonserv.h" #include "cmd_neonspam.h" +#include "cmd_funcmds.h" #include "modcmd.h" void register_commands() { @@ -47,7 +48,7 @@ void register_commands() { #undef OPER_COMMAND //NeonServ Commands - register_bot_alias(1, "NeonServ"); + register_command_alias(1, "NeonServ"); #define USER_COMMAND(NAME,FUNCTION,PARAMCOUNT,PRIVS,FLAGS) register_command(1, NAME, FUNCTION, PARAMCOUNT, PRIVS, 0, FLAGS) // NAME FUNCTION PARAMS PRIVS FLAGS @@ -120,12 +121,22 @@ void register_commands() { #undef OPER_COMMAND //NeonSpam Commands - register_bot_alias(2, "NeonSpam"); + register_command_alias(2, "NeonSpam"); #define USER_COMMAND(NAME,FUNCTION,PARAMCOUNT,PRIVS,FLAGS) register_command(2, NAME, FUNCTION, PARAMCOUNT, PRIVS, 0, FLAGS) // NAME FUNCTION PARAMS PRIVS FLAGS USER_COMMAND("set", neonspam_cmd_set, 0, "#channel_setters", CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH | CMDFLAG_LOG); #undef USER_COMMAND + //Fun Commands + register_command_alias(3, "FunCMD"); + #define USER_COMMAND(NAME,FUNCTION,PARAMCOUNT,FLAGS) register_command(3, NAME, FUNCTION, PARAMCOUNT, NULL, 0, FLAGS) + // NAME FUNCTION PARAMS FLAGS + USER_COMMAND("extscript", neonserv_cmd_extscript, 0, CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_EMPTY_ARGS | CMDFLAG_CHAN_PARAM | CMDFLAG_FUNCMD); + USER_COMMAND("ping", funcmd_ping, 0, CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_FUNCMD); + USER_COMMAND("pong", funcmd_pong, 0, CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_FUNCMD); + #undef USER_COMMAND + + } \ No newline at end of file diff --git a/src/modcmd.c b/src/modcmd.c index bfefb4b..8b9c72c 100644 --- a/src/modcmd.c +++ b/src/modcmd.c @@ -153,6 +153,11 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s struct cmd_binding *cbind; for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) { if(cbind->botid == client->botid && stricmp(cbind->cmd, message) == 0) { + if((cbind->flags & CMDFLAG_FUNCMD)) { + if(!sent_chan) + break; + chan = sent_chan; + } //get a text bot tmp_text_client = get_prefered_bot(client->botid); //parse the arguments... @@ -768,7 +773,7 @@ void bind_unbound_required_functions(int botid) { } } -void register_bot_alias(int botid, char *alias) { +void register_command_alias(int botid, char *alias) { struct cmd_bot_alias *botalias; for(botalias = bot_aliases; botalias; botalias = botalias->next) { if(!stricmp(botalias->alias, alias)) diff --git a/src/modcmd.h b/src/modcmd.h index 40ad373..4ba89a9 100644 --- a/src/modcmd.h +++ b/src/modcmd.h @@ -33,6 +33,7 @@ #define CMDFLAG_EMPTY_ARGS 0x0400 #define CMDFLAG_REQUIRED 0x0800 #define CMDFLAG_TEMPONARY_BIND 0x1000 +#define CMDFLAG_FUNCMD 0x2000 struct ClientSocket; struct UserNode; @@ -92,7 +93,7 @@ void bind_set_channel_access(int botid, char *cmd, char *chanaccess); struct cmd_binding *find_cmd_binding(int botid, char *cmd); void bind_unbound_required_functions(int botid); -void register_bot_alias(int botid, char *alias); +void register_command_alias(int botid, char *alias); struct cmd_binding *getAllBinds(struct cmd_binding *last); #endif \ No newline at end of file -- 2.20.1