exec cleanup

This commit is contained in:
Berke Viktor 2011-11-27 08:53:17 +01:00
parent 76a8166320
commit 5d50ec01cb
1 changed files with 38 additions and 115 deletions

View File

@ -25,155 +25,78 @@
*/ */
#include <windows.h> #include <windows.h>
#include <time.h>
#include "xchat-plugin.h" #include "xchat-plugin.h"
static xchat_plugin *ph; /* plugin handle */ static xchat_plugin *ph; /* plugin handle */
static int
/*static void
run_command (char *word[], char *word_eol[], void *userdata) run_command (char *word[], char *word_eol[], void *userdata)
{ {
char commandLine[1024]; char commandLine[1024];
char buf[100]; char buffer[4096];
char buff1[256]; DWORD dwRead = 0;
DWORD dwRead,tdwRead,tdwAfail,tdwLeft, dwAvail, ctr = 0; DWORD dwLeft = 0;
DWORD dwAvail = 0;
time_t start;
double timeElapsed;
HANDLE readPipe, writePipe; HANDLE readPipe;
HANDLE writePipe;
STARTUPINFO sInfo; STARTUPINFO sInfo;
PROCESS_INFORMATION pInfo; PROCESS_INFORMATION pInfo;
BOOL res;
DWORD reDword;
SECURITY_ATTRIBUTES secattr; SECURITY_ATTRIBUTES secattr;
ZeroMemory(&secattr,sizeof(secattr));
secattr.nLength = sizeof(secattr); ZeroMemory (&secattr, sizeof (secattr));
secattr.nLength = sizeof (secattr);
secattr.bInheritHandle = TRUE; secattr.bInheritHandle = TRUE;
xchat_printf (ph, "%d", strlen(word[2]));
if (strlen (word[2]) > 0) if (strlen (word[2]) > 0)
{ {
strcpy (commandLine, "cmd.exe /c "); strcpy (commandLine, "cmd.exe /c ");
strcat (commandLine, word_eol[2]); 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)); CreateProcess (0, commandLine, 0, 0, TRUE, NORMAL_PRIORITY_CLASS|CREATE_NO_WINDOW, 0, 0, &sInfo, &pInfo);
ZeroMemory(&pInfo,sizeof(pInfo)); CloseHandle (writePipe);
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); start = time (0);
CloseHandle(writePipe); while (PeekNamedPipe (readPipe, buffer, 1, &dwRead, &dwAvail, &dwLeft) && timeElapsed < 10)
//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))
{ {
if (dwRead) 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'; buffer[dwRead] = '\0';
//cout << buff1; xchat_printf (ph, "%s\n", buffer);
xchat_printf (ph, "%s\n", buff1);
//cout.flush();
//fflush(buff1);
//memset(&buff1[0], 0, sizeof(buff1));
strcpy (buff1, "");
} }
} }
timeElapsed = difftime (time (0), start);
} }
} }
return XCHAT_EAT_ALL; if (timeElapsed >= 10)
{
xchat_printf (ph, "Execution took too long, aborting.\n");
}
}*/ CloseHandle (readPipe);
CloseHandle (pInfo.hProcess);
CloseHandle (pInfo.hThread);
static void return XCHAT_EAT_XCHAT;
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))
{
xchat_print (ph, "Error launching the Command Line!");
}
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 (pipeWriter);
CloseHandle (pipeReader);
CloseHandle (pi.hProcess);
CloseHandle (pi.hThread);
}*/
int int
xchat_plugin_init (xchat_plugin *plugin_handle, char **plugin_name, char **plugin_desc, char **plugin_version, char *arg) 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"; *plugin_version = "1.0";
xchat_hook_command (ph, "EXEC", XCHAT_PRI_NORM, run_command, 0, 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 */ return 1; /* return 1 for success */
} }