moved cmd_extscript from NeonServ.mod to global.mod
authorpk910 <philipp@zoelle1.de>
Sun, 12 Feb 2012 20:33:14 +0000 (21:33 +0100)
committerpk910 <philipp@zoelle1.de>
Sun, 12 Feb 2012 20:33:14 +0000 (21:33 +0100)
Makefile.am
src/modules/NeonServ.mod/cmd_neonserv.c
src/modules/NeonServ.mod/cmd_neonserv.h
src/modules/NeonServ.mod/cmd_neonserv_extscript.c [deleted file]
src/modules/global.mod/cmd_global.c
src/modules/global.mod/cmd_global.h
src/modules/global.mod/cmd_global_extscript.c [new file with mode: 0644]

index 9a64dcd42684b90ad0542ec6afb2e0fa4d1ca491..97f61f7ae7353c493f210727e3e3087b4d541614 100644 (file)
@@ -45,6 +45,7 @@ libglobalcmd_la_SOURCES = src/modules/global.mod/cmd_global.c \
       src/modules/global.mod/cmd_global_reconnect.c \
       src/modules/global.mod/cmd_global_modcmd.c \
       src/modules/global.mod/cmd_global_meminfo.c \
       src/modules/global.mod/cmd_global_reconnect.c \
       src/modules/global.mod/cmd_global_modcmd.c \
       src/modules/global.mod/cmd_global_meminfo.c \
+      src/modules/global.mod/cmd_global_extscript.c \
       src/modules/global.mod/module.c
 libglobalcmd_la_LDFLAGS = -module -rpath /nowhere -avoid-version
 
       src/modules/global.mod/module.c
 libglobalcmd_la_LDFLAGS = -module -rpath /nowhere -avoid-version
 
@@ -120,7 +121,6 @@ libNeonServ_la_SOURCES = src/modules/NeonServ.mod/bot_NeonServ.c \
       src/modules/NeonServ.mod/cmd_neonserv_info.c \
       src/modules/NeonServ.mod/cmd_neonserv_rename.c \
       src/modules/NeonServ.mod/cmd_neonserv_unvisited.c \
       src/modules/NeonServ.mod/cmd_neonserv_info.c \
       src/modules/NeonServ.mod/cmd_neonserv_rename.c \
       src/modules/NeonServ.mod/cmd_neonserv_unvisited.c \
-      src/modules/NeonServ.mod/cmd_neonserv_extscript.c \
       src/modules/NeonServ.mod/cmd_neonserv_noregister.c \
       src/modules/NeonServ.mod/cmd_neonserv_nicklist.c \
       src/modules/NeonServ.mod/cmd_neonserv_halfop.c \
       src/modules/NeonServ.mod/cmd_neonserv_noregister.c \
       src/modules/NeonServ.mod/cmd_neonserv_nicklist.c \
       src/modules/NeonServ.mod/cmd_neonserv_halfop.c \
index 8630e4453f8000b80205933c3c75ac657e0c350b..9781c675d44df6b0571887e33a50a368336aeb4c 100644 (file)
@@ -78,7 +78,6 @@ void register_commands() {
     USER_COMMAND("help",         neonserv_cmd_help,      0, NULL,                   0);
     USER_COMMAND("events",       neonserv_cmd_events,    0, "1",                    CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH);
     USER_COMMAND("info",         neonserv_cmd_info,      0, NULL,                   CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_NO_CROSSCHAN);
     USER_COMMAND("help",         neonserv_cmd_help,      0, NULL,                   0);
     USER_COMMAND("events",       neonserv_cmd_events,    0, "1",                    CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH);
     USER_COMMAND("info",         neonserv_cmd_info,      0, NULL,                   CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_NO_CROSSCHAN);
-    USER_COMMAND("extscript",    neonserv_cmd_extscript, 0, NULL,                   CMDFLAG_EMPTY_ARGS | CMDFLAG_CHAN_PARAM);
     USER_COMMAND("nicklist",     neonserv_cmd_nicklist,  0, "1",                    CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH);
     #undef USER_COMMAND
     
     USER_COMMAND("nicklist",     neonserv_cmd_nicklist,  0, "1",                    CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH);
     #undef USER_COMMAND
     
index 0c824e962824f2badcbd1223569632efd702dfbd..cf3fe3058adf169528f3fe4a775ca992f51056c0 100644 (file)
@@ -67,7 +67,6 @@ CMD_BIND(neonserv_cmd_devoiceall);
 CMD_BIND(neonserv_cmd_down);
 CMD_BIND(neonserv_cmd_downall);
 CMD_BIND(neonserv_cmd_events);
 CMD_BIND(neonserv_cmd_down);
 CMD_BIND(neonserv_cmd_downall);
 CMD_BIND(neonserv_cmd_events);
-CMD_BIND(neonserv_cmd_extscript);
 CMD_BIND(neonserv_cmd_giveowner);
 CMD_BIND(neonserv_cmd_halfop);
 CMD_BIND(neonserv_cmd_halfopall);
 CMD_BIND(neonserv_cmd_giveowner);
 CMD_BIND(neonserv_cmd_halfop);
 CMD_BIND(neonserv_cmd_halfopall);
diff --git a/src/modules/NeonServ.mod/cmd_neonserv_extscript.c b/src/modules/NeonServ.mod/cmd_neonserv_extscript.c
deleted file mode 100644 (file)
index 35564e2..0000000
+++ /dev/null
@@ -1,170 +0,0 @@
-/* cmd_neonserv_extscript.c - NeonServ v5.3
- * Copyright (C) 2011-2012  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_neonserv.h"
-#include <fcntl.h>
-
-/*
-* argv[0]      'toys' if it's a toy command (check if toys are enabled)
-* argv[0-*]    script name & parameter patterns
-* argv[argc-1] all arguments passed to the command
-*/
-
-static TIMEQ_CALLBACK(neonserv_cmd_extscript_callback);
-
-struct neonserv_cmd_extscript_cache {
-    struct ClientSocket *client, *textclient;
-    struct Event *event;
-    struct UserNode *user;
-    struct ChanNode *chan;
-    int answere_channel;
-    FILE *pipe;
-};
-
-CMD_BIND(neonserv_cmd_extscript) {
-    int i, j;
-    char *args[MAXNUMPARAMS];
-    int argpos = 0;
-    char *next, *curr;
-    char command[1024];
-    int commandpos = 0;
-    char part[MAXLEN];
-    int partpos;
-    int escape_param;
-    int answere_channel = 0;
-    //check first arg
-    if(argc && !stricmp(argv[0], "toys")) {
-        if(!chan) return; //toys are not allowed via query
-        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")) {
-            //disabled
-            reply(getTextBot(), user, "NS_FUN_DISABLED", chan->name);
-            return;
-        } else if(!strcmp(row[0], "2"))
-            answere_channel = 1;
-        argc--;
-        argv++;
-    }
-    //parse arguments
-    if(argc < 2) return;
-    curr = argv[argc-1];
-    while(curr) {
-        next = strstr(curr, " ");
-        args[argpos++] = curr;
-        if(next) {
-            *next = '\0';
-            curr = next+1;
-        } else
-            curr = NULL;
-    }
-    //parse command pattern and build command
-    commandpos = sprintf(command, "%s", argv[0]);
-    for(i = 1; i < argc-1; i++) {
-        partpos = 0;
-        escape_param = 1;
-        if(argv[i][0] == '$') {
-            argv[i]++;
-            if(argv[i][strlen(argv[i])-1] == '-') {
-                argv[i][strlen(argv[i])-1] = '\0';
-                j = atoi(argv[i]);
-                if(j <= argpos)
-                    partpos = sprintf(part, "%s", merge_argv(args, j-1, argpos));
-            } else if((j = atoi(argv[i])) > 0) {
-                if(j <= argpos)
-                    partpos = sprintf(part, "%s", args[j-1]);
-            } else if(!strcmp(argv[i], "c")) {
-                partpos = sprintf(part, "%s", (chan ? chan->name : ""));
-            } else if(!strcmp(argv[i], "n")) {
-                partpos = sprintf(part, "%s", user->nick);
-            } else if(!strcmp(argv[i], "a")) {
-                partpos = sprintf(part, "%s", ((user->flags & USERFLAG_ISAUTHED) ? user->auth : ""));
-            } else if(!strcmp(argv[i], "access")) {
-                if(chan)
-                    partpos = sprintf(part, "%d", getChannelAccess(user, chan));
-            }
-        } else {
-            partpos = sprintf(part, "%s", argv[i]);
-            escape_param = 0;
-        }
-        //escape shell argument
-        command[commandpos++] = ' ';
-        if(escape_param) {
-            command[commandpos++] = '\'';
-            for(j = 0; j < partpos; j++) {
-                if(part[j] == '\'') {
-                    command[commandpos++] = '\'';
-                    command[commandpos++] = '\\';
-                    command[commandpos++] = '\'';
-                    command[commandpos++] = '\'';
-                } else
-                    command[commandpos++] = part[j];
-            }
-            command[commandpos++] = '\'';
-        } else
-            commandpos += sprintf(command + commandpos, " %s", part);
-    }
-    command[commandpos] = '\0';
-    //we should now have a valid command
-    
-    struct neonserv_cmd_extscript_cache *cache = malloc(sizeof(*cache));
-    if (!cache) {
-        perror("malloc() failed");
-        return;
-    }
-    cache->client = client;
-    cache->textclient = getTextBot();
-    cache->event = event;
-    cache->user = user;
-    cache->chan = chan;
-    cache->answere_channel = answere_channel;
-    cache->pipe = popen(command, "r");
-    #ifndef WIN32
-    fcntl(fileno(cache->pipe), F_SETFL, O_NONBLOCK);
-    #endif
-    timeq_uadd(200, module_id, neonserv_cmd_extscript_callback, cache);
-}
-
-static TIMEQ_CALLBACK(neonserv_cmd_extscript_callback) {
-    struct neonserv_cmd_extscript_cache *cache = data;
-    char command[512];
-    char *a;
-    if(feof(cache->pipe)) {
-        pclose(cache->pipe);
-        free(cache);
-        return;
-    }
-    while (fgets(command, 512, cache->pipe) != NULL) {
-        if((a = strchr(command, '\n'))) 
-            *a = '\0';
-        if(!stricmp(command, "/log")) {
-            logEvent(cache->event);
-            continue;
-        }
-        if(cache->answere_channel)
-            putsock(cache->client, "PRIVMSG %s :%s", cache->chan->name, command);
-        else
-            reply(cache->textclient, cache->user, "%s", command);
-    }
-    timeq_uadd(200, module_id, neonserv_cmd_extscript_callback, cache);
-}
-
-
-
index 7f835fdd7410f8667262f585cd06c0c10acded38..4017628d6367de9ef219ad883e373fcb7dd30df8 100644 (file)
@@ -30,6 +30,7 @@ void register_commands() {
     USER_COMMAND("command",      global_cmd_command,   1, NULL,                   CMDFLAG_ESCAPE_ARGS);
     USER_COMMAND("staff",        global_cmd_staff,     0, NULL,                   0);
     USER_COMMAND("motd",         global_cmd_motd,      0, NULL,                   0);
     USER_COMMAND("command",      global_cmd_command,   1, NULL,                   CMDFLAG_ESCAPE_ARGS);
     USER_COMMAND("staff",        global_cmd_staff,     0, NULL,                   0);
     USER_COMMAND("motd",         global_cmd_motd,      0, NULL,                   0);
+    USER_COMMAND("extscript",    global_cmd_extscript, 0, NULL,                   CMDFLAG_EMPTY_ARGS | CMDFLAG_CHAN_PARAM);
     #undef USER_COMMAND
     
     #define OPER_COMMAND(NAME,FUNCTION,PARAMCOUNT,GACCESS,FLAGS) register_command(0, NAME, module_id, FUNCTION, PARAMCOUNT, NULL, GACCESS, FLAGS)
     #undef USER_COMMAND
     
     #define OPER_COMMAND(NAME,FUNCTION,PARAMCOUNT,GACCESS,FLAGS) register_command(0, NAME, module_id, FUNCTION, PARAMCOUNT, NULL, GACCESS, FLAGS)
index 70aa47ef1bac1239766c5afc57cd1817b2ff6069..8bdfad683ddb8504824af98d98acb19cc038b0e0 100644 (file)
@@ -48,6 +48,7 @@ CMD_BIND(global_cmd_commands);
 CMD_BIND(global_cmd_delbot);
 CMD_BIND(global_cmd_die);
 CMD_BIND(global_cmd_emote);
 CMD_BIND(global_cmd_delbot);
 CMD_BIND(global_cmd_die);
 CMD_BIND(global_cmd_emote);
+CMD_BIND(global_cmd_extscript);
 CMD_BIND(global_cmd_god);
 CMD_BIND(global_cmd_meminfo);
 CMD_BIND(global_cmd_modcmd);
 CMD_BIND(global_cmd_god);
 CMD_BIND(global_cmd_meminfo);
 CMD_BIND(global_cmd_modcmd);
diff --git a/src/modules/global.mod/cmd_global_extscript.c b/src/modules/global.mod/cmd_global_extscript.c
new file mode 100644 (file)
index 0000000..b0a15ea
--- /dev/null
@@ -0,0 +1,170 @@
+/* cmd_global_extscript.c - NeonServ v5.3
+ * Copyright (C) 2011-2012  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_global.h"
+#include <fcntl.h>
+
+/*
+* argv[0]      'toys' if it's a toy command (check if toys are enabled)
+* argv[0-*]    script name & parameter patterns
+* argv[argc-1] all arguments passed to the command
+*/
+
+static TIMEQ_CALLBACK(global_cmd_extscript_callback);
+
+struct global_cmd_extscript_cache {
+    struct ClientSocket *client, *textclient;
+    struct Event *event;
+    struct UserNode *user;
+    struct ChanNode *chan;
+    int answere_channel;
+    FILE *pipe;
+};
+
+CMD_BIND(global_cmd_extscript) {
+    int i, j;
+    char *args[MAXNUMPARAMS];
+    int argpos = 0;
+    char *next, *curr;
+    char command[1024];
+    int commandpos = 0;
+    char part[MAXLEN];
+    int partpos;
+    int escape_param;
+    int answere_channel = 0;
+    //check first arg
+    if(argc && !stricmp(argv[0], "toys")) {
+        if(!chan) return; //toys are not allowed via query
+        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")) {
+            //disabled
+            reply(getTextBot(), user, "NS_FUN_DISABLED", chan->name);
+            return;
+        } else if(!strcmp(row[0], "2"))
+            answere_channel = 1;
+        argc--;
+        argv++;
+    }
+    //parse arguments
+    if(argc < 2) return;
+    curr = argv[argc-1];
+    while(curr) {
+        next = strstr(curr, " ");
+        args[argpos++] = curr;
+        if(next) {
+            *next = '\0';
+            curr = next+1;
+        } else
+            curr = NULL;
+    }
+    //parse command pattern and build command
+    commandpos = sprintf(command, "%s", argv[0]);
+    for(i = 1; i < argc-1; i++) {
+        partpos = 0;
+        escape_param = 1;
+        if(argv[i][0] == '$') {
+            argv[i]++;
+            if(argv[i][strlen(argv[i])-1] == '-') {
+                argv[i][strlen(argv[i])-1] = '\0';
+                j = atoi(argv[i]);
+                if(j <= argpos)
+                    partpos = sprintf(part, "%s", merge_argv(args, j-1, argpos));
+            } else if((j = atoi(argv[i])) > 0) {
+                if(j <= argpos)
+                    partpos = sprintf(part, "%s", args[j-1]);
+            } else if(!strcmp(argv[i], "c")) {
+                partpos = sprintf(part, "%s", (chan ? chan->name : ""));
+            } else if(!strcmp(argv[i], "n")) {
+                partpos = sprintf(part, "%s", user->nick);
+            } else if(!strcmp(argv[i], "a")) {
+                partpos = sprintf(part, "%s", ((user->flags & USERFLAG_ISAUTHED) ? user->auth : ""));
+            } else if(!strcmp(argv[i], "access")) {
+                if(chan)
+                    partpos = sprintf(part, "%d", getChannelAccess(user, chan));
+            }
+        } else {
+            partpos = sprintf(part, "%s", argv[i]);
+            escape_param = 0;
+        }
+        //escape shell argument
+        command[commandpos++] = ' ';
+        if(escape_param) {
+            command[commandpos++] = '\'';
+            for(j = 0; j < partpos; j++) {
+                if(part[j] == '\'') {
+                    command[commandpos++] = '\'';
+                    command[commandpos++] = '\\';
+                    command[commandpos++] = '\'';
+                    command[commandpos++] = '\'';
+                } else
+                    command[commandpos++] = part[j];
+            }
+            command[commandpos++] = '\'';
+        } else
+            commandpos += sprintf(command + commandpos, " %s", part);
+    }
+    command[commandpos] = '\0';
+    //we should now have a valid command
+    
+    struct global_cmd_extscript_cache *cache = malloc(sizeof(*cache));
+    if (!cache) {
+        perror("malloc() failed");
+        return;
+    }
+    cache->client = client;
+    cache->textclient = getTextBot();
+    cache->event = event;
+    cache->user = user;
+    cache->chan = chan;
+    cache->answere_channel = answere_channel;
+    cache->pipe = popen(command, "r");
+    #ifndef WIN32
+    fcntl(fileno(cache->pipe), F_SETFL, O_NONBLOCK);
+    #endif
+    timeq_uadd(200, module_id, global_cmd_extscript_callback, cache);
+}
+
+static TIMEQ_CALLBACK(global_cmd_extscript_callback) {
+    struct global_cmd_extscript_cache *cache = data;
+    char command[512];
+    char *a;
+    if(feof(cache->pipe)) {
+        pclose(cache->pipe);
+        free(cache);
+        return;
+    }
+    while (fgets(command, 512, cache->pipe) != NULL) {
+        if((a = strchr(command, '\n'))) 
+            *a = '\0';
+        if(!stricmp(command, "/log")) {
+            logEvent(cache->event);
+            continue;
+        }
+        if(cache->answere_channel)
+            putsock(cache->client, "PRIVMSG %s :%s", cache->chan->name, command);
+        else
+            reply(cache->textclient, cache->user, "%s", command);
+    }
+    timeq_uadd(200, module_id, global_cmd_extscript_callback, cache);
+}
+
+
+