1 /* cmd_neonserv_extscript.c - NeonServ v5.1
2 * Copyright (C) 2011 Philipp Kreil (pk910)
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.
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.
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/>.
18 #include "cmd_neonserv.h"
21 * argv[0] 'toys' if it's a toy command (check if toys are enabled)
22 * argv[0-*] script name & parameter patterns
23 * argv[argc-1] all arguments passed to the command
26 CMD_BIND(neonserv_cmd_extscript) {
28 char *args[MAXNUMPARAMS];
35 int answere_channel = 0;
37 if(argc && !stricmp(argv[0], "toys")) {
38 if(!chan) return; //toys are not allowed via query
41 printf_mysql_query("SELECT `channel_toys` FROM `channels` WHERE `channel_name` = '%s'", escape_string(chan->name));
43 row = mysql_fetch_row(res);
44 if(!row || !strcmp(row[0], "0")) {
46 reply(getTextBot(), user, "NS_FUN_DISABLED", chan->name);
48 } else if(!strcmp(row[0], "2"))
57 next = strstr(curr, " ");
58 args[argpos++] = curr;
65 //parse command pattern and build command
66 commandpos = sprintf(command, "%s", argv[0]);
67 for(i = 1; i < argc-1; i++) {
69 if(argv[i][0] == '$') {
71 if(argv[i][strlen(argv[i])-1] == '-') {
72 argv[i][strlen(argv[i])-1] = '\0';
75 partpos = sprintf(part, "%s", merge_argv(args, j-1, argpos));
76 } else if((j = atoi(argv[i])) > 0) {
78 partpos = sprintf(part, "%s", args[j-1]);
79 } else if(!strcmp(argv[i], "c")) {
80 partpos = sprintf(part, "%s", (chan ? chan->name : ""));
81 } else if(!strcmp(argv[i], "n")) {
82 partpos = sprintf(part, "%s", user->nick);
83 } else if(!strcmp(argv[i], "a")) {
84 partpos = sprintf(part, "%s", ((user->flags & USERFLAG_ISAUTHED) ? user->auth : ""));
85 } else if(!strcmp(argv[i], "access")) {
87 partpos = sprintf(part, "%d", getChannelAccess(user, chan, 0));
90 partpos = sprintf(part, "%s", argv[i]);
92 //escape shell argument
93 command[commandpos++] = ' ';
94 command[commandpos++] = '\'';
95 for(j = 0; j < partpos; j++) {
97 command[commandpos++] = '\'';
98 command[commandpos++] = '\\';
99 command[commandpos++] = '\'';
100 command[commandpos++] = '\'';
102 command[commandpos++] = part[j];
104 command[commandpos++] = '\'';
106 command[commandpos] = '\0';
107 //we should now have a valid command
109 fp = popen(command, "r");
112 while (fgets(command, 1024, fp) != NULL) {
113 if((a = strchr(command, '\n')))
115 if(answere_channel) {
116 putsock(client, "PRIVMSG %s :%s", chan->name, command);
118 reply(getTextBot(), user, "%s", command);
123 reply(getTextBot(), user, "internal bot error - please contact an administrator!");