summary refs log blame commit diff stats
path: root/plugins/exec/exec.c
blob: d83a4d746a22cf85a116b6f80d975ac1e21ec194 (plain) (tree)
1
2
3
4
5
6
7
8
9

                                        
  





                                                                                
  









                                                                                


                    
                 
 
                           
 
                                                

                                                       
                              
 
          


                                                            






                           



                             

                         

                                   
                                     


                                                
                                      
 

                          


                                                    


                                            

                                                          




                                                          
 
                                                                                                            








                                                     
                                                                                                                           



                                                                                                           


                                   
                                                                                                                    
                                 
                                                           
                                                              












                                                                                                

                                 




                                                                               
                                                                 
                 
 







                                                                                                       
 




                                             
         
                                                  

         
                                   
 

   
                                                                                                                             


                           


                                  
 
                                                                                                                                   
                                                        




                                                  
                            
 
                                                          

                 
/* HexChat
 * Copyright (c) 2011-2012 Berke Viktor.
 *
 * 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:
 *
 * 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.
 */

#include <windows.h>
#include <time.h>

#include "hexchat-plugin.h"

static hexchat_plugin *ph;   /* plugin handle */
static char name[] = "Exec";
static char desc[] = "Execute commands inside HexChat";
static char version[] = "1.2";

static int
run_command (char *word[], char *word_eol[], void *userdata)
{
	char commandLine[1024];
	char buffer[4096];
	DWORD dwRead = 0;
	DWORD dwLeft = 0;
	DWORD dwAvail = 0;
	time_t start;
	double timeElapsed;

	char *token;
	char *context = NULL;
	int announce = 0;

	HANDLE readPipe;
	HANDLE writePipe;
	STARTUPINFO sInfo; 
	PROCESS_INFORMATION pInfo; 
	SECURITY_ATTRIBUTES secattr; 

	ZeroMemory (&secattr, sizeof (secattr));
	secattr.nLength = sizeof (secattr);
	secattr.bInheritHandle = TRUE;

	timeElapsed = 0.0;

	if (strlen (word[2]) > 0)
	{
		strcpy (commandLine, "cmd.exe /c ");

		if (!stricmp("-O", word[2]))
		{
			strcat (commandLine, word_eol[3]);
			announce = 1;
		}
		else
		{
			strcat (commandLine, word_eol[2]);
		}

		CreatePipe (&readPipe, &writePipe, &secattr, 0); /* might be replaced with MyCreatePipeEx */

		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);

		start = time (0);
		while (PeekNamedPipe (readPipe, buffer, 1, &dwRead, &dwAvail, &dwLeft) && timeElapsed < 10)
		{
			if (dwRead)
			{
				if (ReadFile (readPipe, buffer, sizeof (buffer) - 1, &dwRead, NULL) && dwRead != 0 )
				{
					/* avoid garbage */
					buffer[dwRead] = '\0';

					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);
				}
			}
			else
			{
				/* this way we'll more likely get full lines */
				SleepEx (100, TRUE);
			}
			timeElapsed = difftime (time (0), start);
		}

		/* 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");
		}

		CloseHandle (readPipe);
		CloseHandle (pInfo.hProcess);
		CloseHandle (pInfo.hThread);
	}
	else
	{
		hexchat_command (ph, "help exec");
	}

	return HEXCHAT_EAT_HEXCHAT;
}

int
hexchat_plugin_init (hexchat_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;

	hexchat_hook_command (ph, "EXEC", HEXCHAT_PRI_NORM, run_command, "Usage: /EXEC [-O] - execute commands inside HexChat", 0);
	hexchat_printf (ph, "%s plugin loaded\n", name);

	return 1;       /* return 1 for success */
}

int
hexchat_plugin_deinit (void)
{
	hexchat_printf (ph, "%s plugin unloaded\n", name);
	return 1;
}
t::Internal::hook_fd( $fileno, $cb, $flags, { DATA => $data, FD => $fd, CB => $callback, FLAGS => $flags, }, $package ); push @{$pkg_info->{hooks}}, $hook if defined $hook; return $hook; } sub unhook { my $hook = shift @_; my $package = shift @_; ($package) = caller unless $package; my $pkg_info = Xchat::Embed::pkg_info( $package ); if( defined( $hook ) && $hook =~ /^\d+$/ && grep { $_ == $hook } @{$pkg_info->{hooks}} ) { $pkg_info->{hooks} = [grep { $_ != $hook } @{$pkg_info->{hooks}}]; return Xchat::Internal::unhook( $hook ); } return (); } sub _do_for_each { my ($cb, $channels, $servers) = @_; # not specifying any channels or servers is not the same as specifying # undef for both # - not specifying either results in calling the callback inthe current ctx # - specifying undef for for both results in calling the callback in the # front/currently selected tab if( @_ == 3 && !($channels || $servers) ) { $channels = [ undef ]; $servers = [ undef ]; } elsif( !($channels || $servers) ) { $cb->(); return 1; } $channels = [ $channels ] unless ref( $channels ) eq 'ARRAY'; if( $servers ) { $servers = [ $servers ] unless ref( $servers ) eq 'ARRAY'; } else { $servers = [ undef ]; } my $num_done = 0; my $old_ctx = Xchat::get_context(); for my $server ( @$servers ) { for my $channel ( @$channels ) { if( Xchat::set_context( $channel, $server ) ) { $cb->(); $num_done++ } } } Xchat::set_context( $old_ctx ); return $num_done; } sub print { my $text = shift @_; return "" unless defined $text; if( ref( $text ) eq 'ARRAY' ) { if( $, ) { $text = join $, , @$text; } else { $text = join "", @$text; } } return _do_for_each( sub { Xchat::Internal::print( $text ); }, @_ ); } sub printf { my $format = shift; Xchat::print( sprintf( $format, @_ ) ); } # make Xchat::prnt() and Xchat::prntf() as aliases for Xchat::print() and # Xchat::printf(), mainly useful when these functions are exported sub prnt { goto &Xchat::print; } sub prntf { goto &Xchat::printf; } sub command { my $command = shift; return "" unless defined $command; my @commands; if( ref( $command ) eq 'ARRAY' ) { @commands = @$command; } else { @commands = ($command); } return _do_for_each( sub { Xchat::Internal::command( $_ ) foreach @commands }, @_ ); } sub commandf { my $format = shift; Xchat::command( sprintf( $format, @_ ) ); } sub set_context { my $context; if( @_ == 2 ) { my ($channel, $server) = @_; $context = Xchat::find_context( $channel, $server ); } elsif( @_ == 1 ) { if( defined $_[0] && $_[0] =~ /^\d+$/ ) { $context = $_[0]; } else { $context = Xchat::find_context( $_[0] ); } } elsif( @_ == 0 ) { $context = Xchat::find_context(); } return $context ? Xchat::Internal::set_context( $context ) : 0; } sub get_info { my $id = shift; my $info; if( defined( $id ) ) { if( grep { $id eq $_ } qw(state_cursor id) ) { $info = Xchat::get_prefs( $id ); } else { $info = Xchat::Internal::get_info( $id ); } } return $info; } sub user_info { my $nick = Xchat::strip_code(shift @_ || Xchat::get_info( "nick" )); my $user; for (Xchat::get_list( "users" ) ) { if ( Xchat::nickcmp( $_->{nick}, $nick ) == 0 ) { $user = $_; last; } } return $user; } sub context_info { my $ctx = shift @_ || Xchat::get_context; my $old_ctx = Xchat::get_context; my @fields = ( qw(away channel charset host id inputbox libdirfs modes network), qw(nick nickserv server topic version win_ptr win_status), qw(configdir xchatdir xchatdirfs state_cursor), ); if( Xchat::set_context( $ctx ) ) { my %info; for my $field ( @fields ) { $info{$field} = Xchat::get_info( $field ); } my $ctx_info = Xchat::Internal::context_info; @info{keys %$ctx_info} = values %$ctx_info; Xchat::set_context( $old_ctx ); return %info if wantarray; return \%info; } else { return undef; } } sub get_list { unless( grep { $_[0] eq $_ } qw(channels dcc ignore notify users networks) ) { Carp::carp( "'$_[0]' does not appear to be a valid list name" ); } if( $_[0] eq 'networks' ) { return Xchat::List::Network->get(); } else { return Xchat::Internal::get_list( $_[0] ); } } sub strip_code { my $pattern = qr< \cB| #Bold \cC\d{0,2}(?:,\d{1,2})?| #Color \e\[(?:\d{1,2}(?:;\d{1,2})*)?m| # ANSI color code \cG| #Beep \cO| #Reset \cV| #Reverse \c_ #Underline >x; if( defined wantarray ) { my $msg = shift; $msg =~ s/$pattern//g; return $msg; } else { $_[0] =~ s/$pattern//g if defined $_[0]; } } 1