1
0
mirror of https://github.com/moparisthebest/hexchat synced 2024-11-14 21:35:06 -05:00
hexchat/plugins/exec/exec.c

161 lines
4.2 KiB
C
Raw Normal View History

2012-07-13 16:27:12 -04:00
/* HexChat
* Copyright (c) 2011-2012 Berke Viktor.
2011-11-27 01:24:42 -05: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 01:24:42 -05: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 01:24:42 -05:00
*/
#include <windows.h>
2011-11-27 02:53:17 -05:00
#include <time.h>
2011-11-27 01:24:42 -05:00
2012-10-24 15:33:02 -04:00
#include "hexchat-plugin.h"
2011-11-27 01:24:42 -05:00
2012-10-30 03:42:48 -04:00
static hexchat_plugin *ph; /* plugin handle */
2012-10-26 07:38:13 -04:00
static char name[] = "Exec";
static char desc[] = "Execute commands inside HexChat";
static char version[] = "1.2";
2011-11-27 01:24:42 -05:00
2011-11-27 02:53:17 -05:00
static int
2011-11-27 01:24:42 -05:00
run_command (char *word[], char *word_eol[], void *userdata)
{
char commandLine[1024];
2011-11-27 02:53:17 -05:00
char buffer[4096];
DWORD dwRead = 0;
DWORD dwLeft = 0;
DWORD dwAvail = 0;
time_t start;
double timeElapsed;
2013-08-26 12:53:17 -04:00
char *token;
char *context = NULL;
int announce = 0;
2011-11-27 02:53:17 -05:00
HANDLE readPipe;
HANDLE writePipe;
2011-11-27 01:24:42 -05:00
STARTUPINFO sInfo;
PROCESS_INFORMATION pInfo;
SECURITY_ATTRIBUTES secattr;
2011-11-27 02:53:17 -05:00
ZeroMemory (&secattr, sizeof (secattr));
secattr.nLength = sizeof (secattr);
2011-11-27 01:24:42 -05:00
secattr.bInheritHandle = TRUE;
2011-11-27 02:53:17 -05:00
timeElapsed = 0.0;
2011-11-27 01:24:42 -05:00
if (strlen (word[2]) > 0)
{
strcpy (commandLine, "cmd.exe /c ");
2011-12-03 13:53:20 -05:00
if (!stricmp("-O", word[2]))
{
2013-08-26 12:53:17 -04:00
strcat (commandLine, word_eol[3]);
announce = 1;
2011-12-03 13:53:20 -05:00
}
else
{
strcat (commandLine, word_eol[2]);
}
2011-11-27 01:24:42 -05:00
2011-11-27 05:05:38 -05:00
CreatePipe (&readPipe, &writePipe, &secattr, 0); /* might be replaced with MyCreatePipeEx */
2011-11-27 02:53:17 -05: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 05:05:38 -05:00
CreateProcess (0, commandLine, 0, 0, TRUE, NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW, 0, 0, &sInfo, &pInfo);
2011-11-27 02:53:17 -05:00
CloseHandle (writePipe);
start = time (0);
while (PeekNamedPipe (readPipe, buffer, 1, &dwRead, &dwAvail, &dwLeft) && timeElapsed < 10)
2011-11-27 01:24:42 -05:00
{
if (dwRead)
{
2011-11-27 02:53:17 -05:00
if (ReadFile (readPipe, buffer, sizeof (buffer) - 1, &dwRead, NULL) && dwRead != 0 )
2011-11-27 01:24:42 -05:00
{
2011-11-27 05:05:38 -05:00
/* avoid garbage */
2011-11-27 02:53:17 -05:00
buffer[dwRead] = '\0';
2013-08-26 12:53:17 -04:00
if (announce)
{
/* Say each line seperately, TODO: improve... */
token = strtok_s (buffer, "\n", &context);
while (token != NULL)
{
hexchat_commandf (ph, "SAY %s", token);
token = strtok_s (NULL, "\n", &context);
}
}
else
hexchat_printf (ph, "%s", buffer);
2011-11-27 01:24:42 -05:00
}
}
2011-11-27 05:05:38 -05:00
else
{
/* this way we'll more likely get full lines */
SleepEx (100, TRUE);
}
2011-11-27 02:53:17 -05:00
timeElapsed = difftime (time (0), start);
2011-11-27 01:24:42 -05:00
}
2011-11-27 05:15:40 -05:00
/* display a newline to separate things */
if (!announce)
hexchat_printf (ph, "\n");
if (timeElapsed >= 10)
{
hexchat_printf (ph, "Command took too much time to run, execution aborted.\n");
}
2011-11-27 05:15:40 -05:00
CloseHandle (readPipe);
CloseHandle (pInfo.hProcess);
CloseHandle (pInfo.hThread);
}
else
2011-11-27 01:24:42 -05:00
{
hexchat_command (ph, "help exec");
2011-11-27 01:24:42 -05:00
}
2012-10-30 05:42:37 -04:00
return HEXCHAT_EAT_HEXCHAT;
2011-11-27 02:53:17 -05:00
}
2011-11-27 01:24:42 -05:00
int
2012-10-30 03:42:48 -04:00
hexchat_plugin_init (hexchat_plugin *plugin_handle, char **plugin_name, char **plugin_desc, char **plugin_version, char *arg)
2011-11-27 01:24:42 -05:00
{
ph = plugin_handle;
*plugin_name = name;
*plugin_desc = desc;
*plugin_version = version;
2011-11-27 01:24:42 -05:00
2012-10-30 06:47:12 -04:00
hexchat_hook_command (ph, "EXEC", HEXCHAT_PRI_NORM, run_command, "Usage: /EXEC [-O] - execute commands inside HexChat", 0);
2012-10-30 03:42:48 -04:00
hexchat_printf (ph, "%s plugin loaded\n", name);
2011-11-27 01:24:42 -05:00
return 1; /* return 1 for success */
}
int
2012-10-30 03:42:48 -04:00
hexchat_plugin_deinit (void)
2011-11-27 01:24:42 -05:00
{
2012-10-30 03:42:48 -04:00
hexchat_printf (ph, "%s plugin unloaded\n", name);
2011-11-27 01:24:42 -05:00
return 1;
}