Changeset 6301

Show
Ignore:
Timestamp:
11/03/08 17:55:02 (2 months ago)
Author:
morris
Message:

Enable /exec and /stats in messages and broadcasts

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/WiredClient/WCChat.h

    r5319 r6301  
    9292 
    9393 
     94+ (NSString *)outputForShellCommand:(NSString *)command; 
     95 
    9496- (id)initChatWithConnection:(WCServerConnection *)connection windowNibName:(NSString *)windowNibName name:(NSString *)name; 
    9597 
  • trunk/WiredClient/WCChat.m

    r5319 r6301  
    406406        } 
    407407        else if([command isEqualToString:@"/exec"] && [argument length] > 0) { 
    408                 NSTask                          *task; 
    409                 NSPipe                          *pipe; 
    410                 NSFileHandle            *fileHandle; 
    411                 NSDictionary            *environment; 
    412                 NSData                          *data; 
    413408                NSString                        *output; 
    414                 double                          timeout = 10.0; 
    415                  
    416                 pipe = [NSPipe pipe]; 
    417                 fileHandle = [pipe fileHandleForReading]; 
    418                  
    419                 environment     = [NSDictionary dictionaryWithObjectsAndKeys: 
    420                         @"/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin", 
    421                                 @"PATH", 
    422                         NULL]; 
    423                  
    424                 task = [[NSTask alloc] init]; 
    425                 [task setLaunchPath:@"/bin/sh"]; 
    426                 [task setArguments:[NSArray arrayWithObjects:@"-c", argument, NULL]]; 
    427                 [task setStandardOutput:pipe]; 
    428                 [task setStandardError:pipe]; 
    429                 [task setEnvironment:environment]; 
    430                 [task launch]; 
    431                  
    432                 while([task isRunning]) { 
    433                         sleep(1); 
    434                         timeout -= 1.0; 
    435                          
    436                         if(timeout <= 0.0) { 
    437                                 [task terminate]; 
    438                                  
    439                                 break; 
    440                         } 
    441                 } 
    442  
    443                 data = [fileHandle readDataToEndOfFile]; 
    444                 output = [NSString stringWithData:data encoding:NSUTF8StringEncoding]; 
     409                 
     410                output = [[self class] outputForShellCommand:argument]; 
    445411                 
    446412                if(output && [output length] > 0) { 
     
    451417                                                          withArgument:[NSSWF:@"%u", [self chatID]] 
    452418                                                          withArgument:output]; 
    453  
    454                         [[WCStats stats] addUnsignedLongLong:[output length] forKey:WCStatsChat]; 
    455419                } 
    456                  
    457                 [task release]; 
    458420                 
    459421                return YES; 
     
    494456                return YES; 
    495457        } 
    496 #ifndef RELEASE 
    497         else if([command isEqualToString:@"/dump"]) { 
    498                 [[self connection] sendCommand:WCDumpCommand]; 
    499                  
    500                 return YES; 
    501         } 
    502 #endif 
    503458        else if([command isEqualToString:@"/ping"]) { 
    504459                if(!_receivingPings) { 
     
    601556 
    602557@implementation WCChat 
     558 
     559+ (NSString *)outputForShellCommand:(NSString *)command { 
     560        NSTask                          *task; 
     561        NSPipe                          *pipe; 
     562        NSFileHandle            *fileHandle; 
     563        NSDictionary            *environment; 
     564        NSData                          *data; 
     565        double                          timeout = 5.0; 
     566         
     567        pipe = [NSPipe pipe]; 
     568        fileHandle = [pipe fileHandleForReading]; 
     569         
     570        environment     = [NSDictionary dictionaryWithObject:@"/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin" 
     571                                                                                          forKey:@"PATH"]; 
     572         
     573        task = [[[NSTask alloc] init] autorelease]; 
     574        [task setLaunchPath:@"/bin/sh"]; 
     575        [task setArguments:[NSArray arrayWithObjects:@"-c", command, NULL]]; 
     576        [task setStandardOutput:pipe]; 
     577        [task setStandardError:pipe]; 
     578        [task setEnvironment:environment]; 
     579        [task launch]; 
     580         
     581        while([task isRunning]) { 
     582                usleep(100000); 
     583                timeout -= 0.1; 
     584                 
     585                if(timeout <= 0.0) { 
     586                        [task terminate]; 
     587                         
     588                        break; 
     589                } 
     590        } 
     591         
     592        data = [fileHandle readDataToEndOfFile]; 
     593         
     594        return [NSString stringWithData:data encoding:NSUTF8StringEncoding]; 
     595} 
     596 
     597 
     598 
     599#pragma mark - 
    603600 
    604601+ (id)allocWithZone:(NSZone *)zone { 
  • trunk/WiredClient/WCConnection.h

    r5743 r6301  
    5858#define WCDeleteUserCommand                             @"DELETEUSER" 
    5959#define WCDeleteGroupCommand                    @"DELETEGROUP" 
    60 #define WCDumpCommand                                   @"DUMP" 
    6160#define WCEditUserCommand                               @"EDITUSER" 
    6261#define WCEditGroupCommand                              @"EDITGROUP" 
  • trunk/WiredClient/WCMessages.m

    r6255 r6301  
    5757 
    5858- (void)_showDialogForMessage:(WCMessage *)message; 
     59- (NSString *)_stringForMessageString:(NSString *)string; 
    5960 
    6061- (void)_update; 
     
    150151 
    151152 
     153- (NSString *)_stringForMessageString:(NSString *)string { 
     154        NSString        *command, *argument; 
     155        NSRange         range; 
     156         
     157        range = [string rangeOfString:@" "]; 
     158         
     159        if(range.location == NSNotFound) { 
     160                command = string; 
     161                argument = @""; 
     162        } else { 
     163                command = [string substringToIndex:range.location]; 
     164                argument = [string substringFromIndex:range.location + 1]; 
     165        } 
     166         
     167        if([command isEqualToString:@"/exec"] && [argument length] > 0) 
     168                return [WCChat outputForShellCommand:argument]; 
     169        else if([command isEqualToString:@"/stats"]) 
     170                return [[WCStats stats] stringValue]; 
     171         
     172        return string; 
     173} 
     174 
     175 
     176 
    152177#pragma mark - 
    153178 
     
    901926- (void)broadcastSheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo { 
    902927        if(returnCode == NSAlertDefaultReturn) 
    903                 [[self connection] sendCommand:WCBroadcastCommand withArgument:[_broadcastTextView string]]; 
     928                [[self connection] sendCommand:WCBroadcastCommand withArgument:[self _stringForMessageString:[_broadcastTextView string]]]; 
    904929 
    905930        [_broadcastPanel close]; 
     
    947972                } 
    948973                 
    949                 message                        = [WCMessage messageToUser:_messageUser string:[[[_replyTextView string] copy] autorelease] conversation:conversation]; 
     974                message = [WCMessage messageToUser:_messageUser string:[[[_replyTextView string] copy] autorelease] conversation:conversation]; 
    950975 
    951976                [_allMessages addObject:message]; 
     
    953978                [[self connection] sendCommand:WCMessageCommand 
    954979                                                  withArgument:[NSSWF:@"%u", [message userID]] 
    955                                                   withArgument:[message message]]; 
     980                                                  withArgument:[self _stringForMessageString:[message message]]]; 
    956981 
    957982                [[WCStats stats] addUnsignedInt:1 forKey:WCStatsMessagesSent];