1 /* cmd_neonserv_extscript.c - NeonServ v5.3
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"
22 * argv[0] 'toys' if it's a toy command (check if toys are enabled)
23 * argv[0-*] script name & parameter patterns
24 * argv[argc-1] all arguments passed to the command
27 static TIMEQ_CALLBACK(neonserv_cmd_extscript_callback);
29 struct neonserv_cmd_extscript_cache {
30 struct ClientSocket *client, *textclient;
31 struct UserNode *user;
32 struct ChanNode *chan;
37 CMD_BIND(neonserv_cmd_extscript) {
39 char *args[MAXNUMPARAMS];
47 int answere_channel = 0;
49 if(argc && !stricmp(argv[0], "toys")) {
50 if(!chan) return; //toys are not allowed via query
53 printf_mysql_query("SELECT `channel_toys` FROM `channels` WHERE `channel_name` = '%s'", escape_string(chan->name));
55 row = mysql_fetch_row(res);
56 if(!row || !strcmp(row[0], "0")) {
58 reply(getTextBot(), user, "NS_FUN_DISABLED", chan->name);
60 } else if(!strcmp(row[0], "2"))
69 next = strstr(curr, " ");
70 args[argpos++] = curr;
77 //parse command pattern and build command
78 commandpos = sprintf(command, "%s", argv[0]);
79 for(i = 1; i < argc-1; i++) {
82 if(argv[i][0] == '$') {
84 if(argv[i][strlen(argv[i])-1] == '-') {
85 argv[i][strlen(argv[i])-1] = '\0';
88 partpos = sprintf(part, "%s", merge_argv(args, j-1, argpos));
89 } else if((j = atoi(argv[i])) > 0) {
91 partpos = sprintf(part, "%s", args[j-1]);
92 } else if(!strcmp(argv[i], "c")) {
93 partpos = sprintf(part, "%s", (chan ? chan->name : ""));
94 } else if(!strcmp(argv[i], "n")) {
95 partpos = sprintf(part, "%s", user->nick);
96 } else if(!strcmp(argv[i], "a")) {
97 partpos = sprintf(part, "%s", ((user->flags & USERFLAG_ISAUTHED) ? user->auth : ""));
98 } else if(!strcmp(argv[i], "access")) {
100 partpos = sprintf(part, "%d", getChannelAccess(user, chan));
103 partpos = sprintf(part, "%s", argv[i]);
106 //escape shell argument
107 command[commandpos++] = ' ';
109 command[commandpos++] = '\'';
110 for(j = 0; j < partpos; j++) {
111 if(part[j] == '\'') {
112 command[commandpos++] = '\'';
113 command[commandpos++] = '\\';
114 command[commandpos++] = '\'';
115 command[commandpos++] = '\'';
117 command[commandpos++] = part[j];
119 command[commandpos++] = '\'';
121 commandpos += sprintf(command + commandpos, " %s", part);
123 command[commandpos] = '\0';
124 //we should now have a valid command
126 struct neonserv_cmd_extscript_cache *cache = malloc(sizeof(*cache));
128 perror("malloc() failed");
131 cache->client = client;
132 cache->textclient = getTextBot();
135 cache->answere_channel = answere_channel;
136 cache->pipe = popen(command, "r");
138 fcntl(fileno(cache->pipe), F_SETFL, O_NONBLOCK);
140 timeq_add(1, neonserv_cmd_extscript_callback, cache);
143 static TIMEQ_CALLBACK(neonserv_cmd_extscript_callback) {
144 struct neonserv_cmd_extscript_cache *cache = data;
147 if(feof(cache->pipe)) {
152 while (fgets(command, 512, cache->pipe) != NULL) {
153 if((a = strchr(command, '\n')))
155 if(cache->answere_channel)
156 putsock(cache->client, "PRIVMSG %s :%s", cache->chan->name, command);
158 reply(cache->textclient, cache->user, "%s", command);
160 timeq_add(1, neonserv_cmd_extscript_callback, cache);