c.sexchat/plugins/exec/exec.c

139 lines
3.8 KiB
C
Raw Normal View History

2012-07-13 22:27:12 +02:00
/* HexChat
* Copyright (c) 2011-2012 Berke Viktor.
2011-11-27 07:24:42 +01: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 07:24:42 +01: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 07:24:42 +01:00
*/
#include <windows.h>
2011-11-27 08:53:17 +01:00
#include <time.h>
2011-11-27 07:24:42 +01:00
2012-10-24 21:33:02 +02:00
#include "hexchat-plugin.h"
2011-11-27 07:24:42 +01:00
static xchat_plugin *ph; /* plugin handle */
2012-10-26 13:38:13 +02:00
static char name[] = "Exec";
static char desc[] = "Execute commands inside HexChat";
static char version[] = "1.1";
2011-11-27 07:24:42 +01:00
2011-11-27 08:53:17 +01:00
static int
2011-11-27 07:24:42 +01:00
run_command (char *word[], char *word_eol[], void *userdata)
{
char commandLine[1024];
2011-11-27 08:53:17 +01: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 07:24:42 +01:00
STARTUPINFO sInfo;
PROCESS_INFORMATION pInfo;
SECURITY_ATTRIBUTES secattr;
2011-11-27 08:53:17 +01:00
ZeroMemory (&secattr, sizeof (secattr));
secattr.nLength = sizeof (secattr);
2011-11-27 07:24:42 +01:00
secattr.bInheritHandle = TRUE;
2011-11-27 08:53:17 +01:00
2011-11-27 07:24:42 +01:00
if (strlen (word[2]) > 0)
{
strcpy (commandLine, "cmd.exe /c ");
2011-12-03 19:53:20 +01:00
if (!stricmp("-O", word[2]))
{
/*strcat (commandLine, word_eol[3]);*/
xchat_printf (ph, "Printing Exec output to others is not supported yet.\n");
return XCHAT_EAT_XCHAT;
}
else
{
strcat (commandLine, word_eol[2]);
}
2011-11-27 07:24:42 +01:00
2011-11-27 11:05:38 +01:00
CreatePipe (&readPipe, &writePipe, &secattr, 0); /* might be replaced with MyCreatePipeEx */
2011-11-27 08:53:17 +01: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 11:05:38 +01:00
CreateProcess (0, commandLine, 0, 0, TRUE, NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW, 0, 0, &sInfo, &pInfo);
2011-11-27 08:53:17 +01:00
CloseHandle (writePipe);
start = time (0);
while (PeekNamedPipe (readPipe, buffer, 1, &dwRead, &dwAvail, &dwLeft) && timeElapsed < 10)
2011-11-27 07:24:42 +01:00
{
if (dwRead)
{
2011-11-27 08:53:17 +01:00
if (ReadFile (readPipe, buffer, sizeof (buffer) - 1, &dwRead, NULL) && dwRead != 0 )
2011-11-27 07:24:42 +01:00
{
2011-11-27 11:05:38 +01:00
/* avoid garbage */
2011-11-27 08:53:17 +01:00
buffer[dwRead] = '\0';
2011-11-27 11:05:38 +01:00
xchat_printf (ph, "%s", buffer);
2011-11-27 07:24:42 +01:00
}
}
2011-11-27 11:05:38 +01:00
else
{
/* this way we'll more likely get full lines */
SleepEx (100, TRUE);
}
2011-11-27 08:53:17 +01:00
timeElapsed = difftime (time (0), start);
2011-11-27 07:24:42 +01:00
}
}
2011-11-27 11:15:40 +01:00
/* display a newline to separate things */
xchat_printf (ph, "\n");
2011-11-27 08:53:17 +01:00
if (timeElapsed >= 10)
2011-11-27 07:24:42 +01:00
{
2011-11-27 11:05:38 +01:00
xchat_printf (ph, "Command took too much time to run, execution aborted.\n");
2011-11-27 07:24:42 +01:00
}
2011-11-27 08:53:17 +01:00
CloseHandle (readPipe);
CloseHandle (pInfo.hProcess);
CloseHandle (pInfo.hThread);
2011-11-27 07:24:42 +01:00
2011-11-27 08:53:17 +01:00
return XCHAT_EAT_XCHAT;
}
2011-11-27 07:24:42 +01: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 = name;
*plugin_desc = desc;
*plugin_version = version;
2011-11-27 07:24:42 +01:00
2011-12-03 19:53:20 +01:00
xchat_hook_command (ph, "EXEC", XCHAT_PRI_NORM, run_command, "Usage: /EXEC [-O] - execute commands inside XChat", 0);
xchat_printf (ph, "%s plugin loaded\n", name);
2011-11-27 07:24:42 +01:00
return 1; /* return 1 for success */
}
int
xchat_plugin_deinit (void)
{
2011-12-02 08:07:27 +01:00
xchat_printf (ph, "%s plugin unloaded\n", name);
2011-11-27 07:24:42 +01:00
return 1;
}