1 /* cmd_neonserv_extscript.c - NeonServ v5.3
2 * Copyright (C) 2011-2012 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;
32 struct UserNode *user;
33 struct ChanNode *chan;
38 CMD_BIND(neonserv_cmd_extscript) {
40 char *args[MAXNUMPARAMS];
48 int answere_channel = 0;
50 if(argc && !stricmp(argv[0], "toys")) {
51 if(!chan) return; //toys are not allowed via query
54 printf_mysql_query("SELECT `channel_toys` FROM `channels` WHERE `channel_name` = '%s'", escape_string(chan->name));
56 row = mysql_fetch_row(res);
57 if(!row || !strcmp(row[0], "0")) {
59 reply(getTextBot(), user, "NS_FUN_DISABLED", chan->name);
61 } else if(!strcmp(row[0], "2"))
70 next = strstr(curr, " ");
71 args[argpos++] = curr;
78 //parse command pattern and build command
79 commandpos = sprintf(command, "%s", argv[0]);
80 for(i = 1; i < argc-1; i++) {
83 if(argv[i][0] == '$') {
85 if(argv[i][strlen(argv[i])-1] == '-') {
86 argv[i][strlen(argv[i])-1] = '\0';
89 partpos = sprintf(part, "%s", merge_argv(args, j-1, argpos));
90 } else if((j = atoi(argv[i])) > 0) {
92 partpos = sprintf(part, "%s", args[j-1]);
93 } else if(!strcmp(argv[i], "c")) {
94 partpos = sprintf(part, "%s", (chan ? chan->name : ""));
95 } else if(!strcmp(argv[i], "n")) {
96 partpos = sprintf(part, "%s", user->nick);
97 } else if(!strcmp(argv[i], "a")) {
98 partpos = sprintf(part, "%s", ((user->flags & USERFLAG_ISAUTHED) ? user->auth : ""));
99 } else if(!strcmp(argv[i], "access")) {
101 partpos = sprintf(part, "%d", getChannelAccess(user, chan));
104 partpos = sprintf(part, "%s", argv[i]);
107 //escape shell argument
108 command[commandpos++] = ' ';
110 command[commandpos++] = '\'';
111 for(j = 0; j < partpos; j++) {
112 if(part[j] == '\'') {
113 command[commandpos++] = '\'';
114 command[commandpos++] = '\\';
115 command[commandpos++] = '\'';
116 command[commandpos++] = '\'';
118 command[commandpos++] = part[j];
120 command[commandpos++] = '\'';
122 commandpos += sprintf(command + commandpos, " %s", part);
124 command[commandpos] = '\0';
125 //we should now have a valid command
127 struct neonserv_cmd_extscript_cache *cache = malloc(sizeof(*cache));
129 perror("malloc() failed");
132 cache->client = client;
133 cache->textclient = getTextBot();
134 cache->event = event;
137 cache->answere_channel = answere_channel;
138 cache->pipe = popen(command, "r");
140 fcntl(fileno(cache->pipe), F_SETFL, O_NONBLOCK);
142 timeq_add(1, neonserv_cmd_extscript_callback, cache);
145 static TIMEQ_CALLBACK(neonserv_cmd_extscript_callback) {
146 struct neonserv_cmd_extscript_cache *cache = data;
149 if(feof(cache->pipe)) {
154 while (fgets(command, 512, cache->pipe) != NULL) {
155 if((a = strchr(command, '\n')))
157 if(!stricmp(command, "/log")) {
158 logEvent(cache->event);
161 if(cache->answere_channel)
162 putsock(cache->client, "PRIVMSG %s :%s", cache->chan->name, command);
164 reply(cache->textclient, cache->user, "%s", command);
166 timeq_add(1, neonserv_cmd_extscript_callback, cache);