2011-11-27 10:24:42 +04:00
|
|
|
/* XChat-WDK
|
|
|
|
* Copyright (c) 2011 Berke Viktor.
|
|
|
|
*
|
2011-11-29 05:04:08 +04:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2011-11-27 10:24:42 +04:00
|
|
|
*
|
2011-11-29 05:04:08 +04:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
2011-11-27 10:24:42 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <windows.h>
|
2011-11-27 11:53:17 +04:00
|
|
|
#include <time.h>
|
2011-11-27 10:24:42 +04:00
|
|
|
|
|
|
|
#include "xchat-plugin.h"
|
|
|
|
|
|
|
|
static xchat_plugin *ph; /* plugin handle */
|
|
|
|
|
2011-11-27 11:53:17 +04:00
|
|
|
static int
|
2011-11-27 10:24:42 +04:00
|
|
|
run_command (char *word[], char *word_eol[], void *userdata)
|
|
|
|
{
|
|
|
|
char commandLine[1024];
|
2011-11-27 11:53:17 +04:00
|
|
|
char buffer[4096];
|
|
|
|
DWORD dwRead = 0;
|
|
|
|
DWORD dwLeft = 0;
|
|
|
|
DWORD dwAvail = 0;
|
|
|
|
time_t start;
|
|
|
|
double timeElapsed;
|
|
|
|
|
|
|
|
HANDLE readPipe;
|
|
|
|
HANDLE writePipe;
|
2011-11-27 10:24:42 +04:00
|
|
|
STARTUPINFO sInfo;
|
|
|
|
PROCESS_INFORMATION pInfo;
|
|
|
|
SECURITY_ATTRIBUTES secattr;
|
2011-11-27 11:53:17 +04:00
|
|
|
|
|
|
|
ZeroMemory (&secattr, sizeof (secattr));
|
|
|
|
secattr.nLength = sizeof (secattr);
|
2011-11-27 10:24:42 +04:00
|
|
|
secattr.bInheritHandle = TRUE;
|
2011-11-27 11:53:17 +04:00
|
|
|
|
2011-11-27 10:24:42 +04:00
|
|
|
if (strlen (word[2]) > 0)
|
|
|
|
{
|
|
|
|
strcpy (commandLine, "cmd.exe /c ");
|
|
|
|
strcat (commandLine, word_eol[2]);
|
|
|
|
|
2011-11-27 14:05:38 +04:00
|
|
|
CreatePipe (&readPipe, &writePipe, &secattr, 0); /* might be replaced with MyCreatePipeEx */
|
2011-11-27 11:53:17 +04:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2011-11-27 14:05:38 +04:00
|
|
|
CreateProcess (0, commandLine, 0, 0, TRUE, NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW, 0, 0, &sInfo, &pInfo);
|
2011-11-27 11:53:17 +04:00
|
|
|
CloseHandle (writePipe);
|
|
|
|
|
|
|
|
start = time (0);
|
|
|
|
while (PeekNamedPipe (readPipe, buffer, 1, &dwRead, &dwAvail, &dwLeft) && timeElapsed < 10)
|
2011-11-27 10:24:42 +04:00
|
|
|
{
|
|
|
|
if (dwRead)
|
|
|
|
{
|
2011-11-27 11:53:17 +04:00
|
|
|
if (ReadFile (readPipe, buffer, sizeof (buffer) - 1, &dwRead, NULL) && dwRead != 0 )
|
2011-11-27 10:24:42 +04:00
|
|
|
{
|
2011-11-27 14:05:38 +04:00
|
|
|
/* avoid garbage */
|
2011-11-27 11:53:17 +04:00
|
|
|
buffer[dwRead] = '\0';
|
2011-11-27 14:05:38 +04:00
|
|
|
xchat_printf (ph, "%s", buffer);
|
2011-11-27 10:24:42 +04:00
|
|
|
}
|
|
|
|
}
|
2011-11-27 14:05:38 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* this way we'll more likely get full lines */
|
|
|
|
SleepEx (100, TRUE);
|
|
|
|
}
|
2011-11-27 11:53:17 +04:00
|
|
|
timeElapsed = difftime (time (0), start);
|
2011-11-27 10:24:42 +04:00
|
|
|
}
|
|
|
|
}
|
2011-11-27 14:15:40 +04:00
|
|
|
|
|
|
|
/* display a newline to separate things */
|
|
|
|
xchat_printf (ph, "\n");
|
|
|
|
|
2011-11-27 11:53:17 +04:00
|
|
|
if (timeElapsed >= 10)
|
2011-11-27 10:24:42 +04:00
|
|
|
{
|
2011-11-27 14:05:38 +04:00
|
|
|
xchat_printf (ph, "Command took too much time to run, execution aborted.\n");
|
2011-11-27 10:24:42 +04:00
|
|
|
}
|
|
|
|
|
2011-11-27 11:53:17 +04:00
|
|
|
CloseHandle (readPipe);
|
|
|
|
CloseHandle (pInfo.hProcess);
|
|
|
|
CloseHandle (pInfo.hThread);
|
2011-11-27 10:24:42 +04:00
|
|
|
|
2011-11-27 11:53:17 +04:00
|
|
|
return XCHAT_EAT_XCHAT;
|
|
|
|
}
|
2011-11-27 10:24:42 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
xchat_plugin_init (xchat_plugin *plugin_handle, char **plugin_name, char **plugin_desc, char **plugin_version, char *arg)
|
|
|
|
{
|
|
|
|
ph = plugin_handle;
|
|
|
|
|
|
|
|
*plugin_name = "Exec";
|
|
|
|
*plugin_desc = "Execute commands inside XChat";
|
|
|
|
*plugin_version = "1.0";
|
|
|
|
|
|
|
|
xchat_hook_command (ph, "EXEC", XCHAT_PRI_NORM, run_command, 0, 0);
|
2011-11-27 11:53:17 +04:00
|
|
|
xchat_printf (ph, "%s plugin loaded\n", *plugin_name);
|
2011-11-27 10:24:42 +04:00
|
|
|
|
|
|
|
return 1; /* return 1 for success */
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
xchat_plugin_deinit (void)
|
|
|
|
{
|
|
|
|
xchat_print (ph, "Exec plugin unloaded\n");
|
|
|
|
return 1;
|
|
|
|
}
|