X-Git-Url: http://git.pk910.de/?a=blobdiff_plain;f=src%2Fcmd_neonserv_extscript.c;h=13f96a31b074b2dbb7f609792ec0363e6f60aec5;hb=44436a96352a38631237978c9fd431cef3d85cfb;hp=2d7e0bd80c29e3fa9e625d2e89a41ff665c29794;hpb=c73dc6f2c494e7f6f8e71c03a008923ec76a9758;p=NeonServV5.git diff --git a/src/cmd_neonserv_extscript.c b/src/cmd_neonserv_extscript.c index 2d7e0bd..13f96a3 100644 --- a/src/cmd_neonserv_extscript.c +++ b/src/cmd_neonserv_extscript.c @@ -1,4 +1,4 @@ -/* cmd_neonserv_extscript.c - NeonServ v5.2 +/* cmd_neonserv_extscript.c - NeonServ v5.3 * Copyright (C) 2011 Philipp Kreil (pk910) * * This program is free software: you can redistribute it and/or modify @@ -16,6 +16,7 @@ */ #include "cmd_neonserv.h" +#include /* * argv[0] 'toys' if it's a toy command (check if toys are enabled) @@ -23,6 +24,16 @@ * 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 UserNode *user; + struct ChanNode *chan; + int answere_channel; + FILE *pipe; +}; + CMD_BIND(neonserv_cmd_extscript) { int i, j; char *args[MAXNUMPARAMS]; @@ -111,33 +122,43 @@ CMD_BIND(neonserv_cmd_extscript) { } command[commandpos] = '\0'; //we should now have a valid command - FILE *fp; - fp = popen(command, "r"); - if (fp) { - struct ClientSocket *textbot = getTextBot(); - pid_t pID = fork(); - if (pID == 0) { //We're the child process :D - char *a; - while (fgets(command, 1024, fp) != NULL) { - if((a = strchr(command, '\n'))) - *a = '\0'; - if(answere_channel) - putsock(client, "PRIVMSG %s :%s", chan->name, command); - else - reply(textbot, user, "%s", command); - } - pclose(fp); - exit(0); - } else if (pID < 0) { - reply(getTextBot(), user, "internal bot error - please contact an administrator!"); - pclose(fp); - } else { - //parent bot - continue program - } - } else { - //error - reply(getTextBot(), user, "internal bot error - please contact an administrator!"); + + struct neonserv_cmd_extscript_cache *cache = malloc(sizeof(*cache)); + if (!cache) { + perror("malloc() failed"); + return; + } + cache->client = client; + cache->textclient = getTextBot(); + 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_add(1, 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(cache->answere_channel) + putsock(cache->client, "PRIVMSG %s :%s", cache->chan->name, command); + else + reply(cache->textclient, cache->user, "%s", command); + } + timeq_add(1, neonserv_cmd_extscript_callback, cache); } + +