From 5d50ec01cb03122cdabb5fd2dace903e50c565e8 Mon Sep 17 00:00:00 2001 From: Berke Viktor Date: Sun, 27 Nov 2011 08:53:17 +0100 Subject: [PATCH] exec cleanup --- plugins/exec/exec.c | 153 +++++++++++--------------------------------- 1 file changed, 38 insertions(+), 115 deletions(-) diff --git a/plugins/exec/exec.c b/plugins/exec/exec.c index 1c8f5a06..71adf15c 100644 --- a/plugins/exec/exec.c +++ b/plugins/exec/exec.c @@ -25,154 +25,77 @@ */ #include +#include #include "xchat-plugin.h" static xchat_plugin *ph; /* plugin handle */ - - - - - -/*static void +static int run_command (char *word[], char *word_eol[], void *userdata) { char commandLine[1024]; - char buf[100]; - char buff1[256]; - DWORD dwRead,tdwRead,tdwAfail,tdwLeft, dwAvail, ctr = 0; + char buffer[4096]; + DWORD dwRead = 0; + DWORD dwLeft = 0; + DWORD dwAvail = 0; + time_t start; + double timeElapsed; - HANDLE readPipe, writePipe; + HANDLE readPipe; + HANDLE writePipe; STARTUPINFO sInfo; PROCESS_INFORMATION pInfo; - BOOL res; - DWORD reDword; - SECURITY_ATTRIBUTES secattr; - ZeroMemory(&secattr,sizeof(secattr)); - secattr.nLength = sizeof(secattr); + + ZeroMemory (&secattr, sizeof (secattr)); + secattr.nLength = sizeof (secattr); secattr.bInheritHandle = TRUE; - - xchat_printf (ph, "%d", strlen(word[2])); + if (strlen (word[2]) > 0) { strcpy (commandLine, "cmd.exe /c "); strcat (commandLine, word_eol[2]); - CreatePipe(&readPipe,&writePipe,&secattr,0); + CreatePipe (&readPipe, &writePipe, &secattr, 0); - - ZeroMemory(&sInfo,sizeof(sInfo)); - ZeroMemory(&pInfo,sizeof(pInfo)); - sInfo.cb=sizeof(sInfo); - sInfo.dwFlags=STARTF_USESTDHANDLES; - sInfo.hStdInput=NULL; - sInfo.hStdOutput=writePipe; - sInfo.hStdError=writePipe; + ZeroMemory (&sInfo, sizeof (sInfo)); + ZeroMemory (&pInfo, sizeof (pInfo)); + sInfo.cb = sizeof (sInfo); + sInfo.dwFlags = STARTF_USESTDHANDLES; + sInfo.hStdInput = NULL; + sInfo.hStdOutput = writePipe; + sInfo.hStdError = writePipe; - CreateProcess(0, commandLine, 0, 0, TRUE, NORMAL_PRIORITY_CLASS|CREATE_NO_WINDOW, 0, 0, &sInfo, &pInfo); - CloseHandle(writePipe); + CreateProcess (0, commandLine, 0, 0, TRUE, NORMAL_PRIORITY_CLASS|CREATE_NO_WINDOW, 0, 0, &sInfo, &pInfo); + CloseHandle (writePipe); - //now read the output pipe here. - - //do - //{ - // res=ReadFile(readPipe,buf,100,&reDword,0); - //csTemp=buf; - //m_csOutput+=csTemp.Left(reDword); - //xchat_printf (ph, "%s", buf); - //strcpy(buf, "\0"); - //fflush(buf); - //}while(res); - - - while (PeekNamedPipe(readPipe, buff1, 1, &dwRead, &dwAvail, &tdwLeft)) + start = time (0); + while (PeekNamedPipe (readPipe, buffer, 1, &dwRead, &dwAvail, &dwLeft) && timeElapsed < 10) { if (dwRead) { - if (ReadFile(readPipe, buff1, sizeof(buff1) - 1, &dwRead, NULL) && dwRead != 0 ) + if (ReadFile (readPipe, buffer, sizeof (buffer) - 1, &dwRead, NULL) && dwRead != 0 ) { - buff1[dwRead] = '\0'; - //cout << buff1; - xchat_printf (ph, "%s\n", buff1); - //cout.flush(); - //fflush(buff1); - //memset(&buff1[0], 0, sizeof(buff1)); - strcpy (buff1, ""); + buffer[dwRead] = '\0'; + xchat_printf (ph, "%s\n", buffer); } } + timeElapsed = difftime (time (0), start); } } - return XCHAT_EAT_ALL; - -}*/ - -static void -run_command (char *word[], char *word_eol[], void *userdata) -{ - return XCHAT_EAT_ALL; -} - - - - - - - - -/*static void -run_command (char *word[], char *word_eol[], void *userdata) -{ - STARTUPINFO si; - PROCESS_INFORMATION pi; - char commandLine[1024]; - HANDLE pipeWriter; - HANDLE pipeReader; - - char buff1[128]; - DWORD dwRead,tdwRead,tdwAfail,tdwLeft, dwAvail, ctr = 0; - - //CreatePipe (&pipeReader, &pipeWriter, NULL, NULL); - - ZeroMemory (&si, sizeof (si)); - si.cb = sizeof (si); - ZeroMemory (&pi, sizeof (pi)); - si.hStdOutput = pipeWriter; - - strcpy (commandLine, "cmd.exe /c "); - strcat (commandLine, word_eol[2]); - - if (!CreateProcess ( NULL, commandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) + if (timeElapsed >= 10) { - xchat_print (ph, "Error launching the Command Line!"); + xchat_printf (ph, "Execution took too long, aborting.\n"); } - while (PeekNamedPipe(pipeReader, buff1, 1, &dwRead, &dwAvail, &tdwLeft)) { - if (dwRead) - { - if (ReadFile(pipeReader, buff1, sizeof(buff1) - 1, &dwRead, NULL) && dwRead != 0 ) - { - buff1[dwRead] = '\0'; - xchat_printf (ph, "%s\n", buff1); - //cout.flush(); - } - } - else - { - sprintf(buff1, "No data loop count = %d. Do something here\n", ++ctr); - xchat_printf (ph, "%s\n", buff1); - //cout.flush(); - SleepEx(1000, FALSE); - } - } + CloseHandle (readPipe); + CloseHandle (pInfo.hProcess); + CloseHandle (pInfo.hThread); - CloseHandle (pipeWriter); - CloseHandle (pipeReader); - CloseHandle (pi.hProcess); - CloseHandle (pi.hThread); -}*/ + return XCHAT_EAT_XCHAT; +} int xchat_plugin_init (xchat_plugin *plugin_handle, char **plugin_name, char **plugin_desc, char **plugin_version, char *arg) @@ -184,7 +107,7 @@ xchat_plugin_init (xchat_plugin *plugin_handle, char **plugin_name, char **plugi *plugin_version = "1.0"; xchat_hook_command (ph, "EXEC", XCHAT_PRI_NORM, run_command, 0, 0); - xchat_printf (ph, "Exec plugin loaded\n"); + xchat_printf (ph, "%s plugin loaded\n", *plugin_name); return 1; /* return 1 for success */ }