IChatUtils.m
author Jens Alfke <jens@mooseyard.com>
Sun May 03 10:13:31 2009 -0700 (2009-05-03)
changeset 27 256370e8935a
parent 11 e5976864dfe9
permissions -rw-r--r--
* MYTask: Added -commandLine, overhauled logging.
* MYDirectoryWatcher: Overhauled logging.
* MYErrorUtils: Disabled use of Security API (so it'll build without linking against Security.framework.)
     1 //
     2 //  IChatUtils.m
     3 //  MYUtilities
     4 //
     5 //  Created by Jens Alfke on 3/3/08.
     6 //  Copyright 2008 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import "IChatUtils.h"
    10 #import "iChatBridge.h"
    11 #import <InstantMessage/IMService.h>
    12 
    13 @implementation IChatUtils
    14 
    15 
    16 static iChatApplication *sIChatApp;
    17 
    18 + (void) initialize
    19 {
    20     if( ! sIChatApp ) {
    21         sIChatApp = [SBApplication applicationWithBundleIdentifier: @"com.apple.iChat"];
    22         sIChatApp.timeout = 5*60; // in ticks
    23     }
    24 }
    25 
    26 
    27 + (SBApplication*) app  {return sIChatApp;}
    28 + (BOOL) isRunning      {return sIChatApp.isRunning;}
    29 + (void) activate       {[sIChatApp activate];}
    30 
    31 
    32 + (iChatTextChat*) activeChat
    33 {
    34     if( ! [sIChatApp isRunning] )
    35         return nil;
    36     SBElementArray *chats = sIChatApp.textChats;
    37     if( chats.count==0 )
    38         return nil;
    39     iChatTextChat *chat = [chats objectAtIndex: 0];
    40     /*if( ! chat.active )               // somehow this returns NO for Bonjour chats
    41         return nil;*/
    42     return chat;
    43 }    
    44 
    45 + (NSString*) activeChatPartner
    46 {
    47     iChatTextChat *chat = [self activeChat];
    48     Log(@"Active chat = %@",chat);
    49     if( ! chat )
    50         return nil;
    51     NSMutableArray *names = $marray();
    52     for( iChatBuddy *b in [chat participants] )
    53         [names addObject: (b.fullName ?: b.name)];
    54     Log(@"Particpants = %@",names);
    55     return [names componentsJoinedByString: @", "];
    56 }
    57 
    58 + (BOOL) sendMessage: (NSString*)msg
    59 {
    60     iChatTextChat *chat = [self activeChat];
    61     if( ! chat )
    62         return NO;
    63     [sIChatApp send: msg to: chat];
    64     return YES;
    65 }
    66 
    67 + (NSDictionary*) iChatInfoForOnlinePerson: (ABPerson*)abPerson
    68 {
    69     if( ! abPerson )
    70         return nil;
    71     IMPersonStatus bestStatus = IMPersonStatusOffline;
    72     NSDictionary *bestInfo = nil;
    73     for( IMService *service in [IMService allServices] ) {
    74         for( NSString *name in [service screenNamesForPerson: abPerson] ) {
    75             NSDictionary *info = [service infoForScreenName: name];
    76             if( [[info objectForKey: IMPersonCapabilitiesKey] containsObject: IMCapabilityText] ) {
    77                 IMPersonStatus status = [[info objectForKey: IMPersonStatusKey] intValue];
    78                 if( IMComparePersonStatus(status,bestStatus) < 0 ) {    // yes, it returns the wrong sign
    79                     bestInfo = info;
    80                     bestStatus = status;
    81                 }
    82             }
    83         }
    84     }
    85     return bestInfo;
    86 }
    87 
    88 + (BOOL) isPersonOnline: (ABPerson*)abPerson
    89 {
    90     return [self iChatInfoForOnlinePerson: abPerson] != nil;
    91 }
    92 
    93 + (iChatBuddy*) buddyWithScreenName: (NSString*)screenName
    94 {
    95     NSPredicate *pred = [NSPredicate predicateWithFormat: @"handle==%@", screenName];
    96     @try{
    97         return [[[[sIChatApp buddies] filteredArrayUsingPredicate: pred] objectAtIndex: 0] get];
    98     } @catch( NSException *x ) {
    99         Log(@"buddyWithScreenName got exception: %@",x);
   100     }
   101     return nil;
   102 }
   103 
   104 + (iChatBuddy*) buddyWithInfo: (NSDictionary*)info
   105 {
   106     return [self buddyWithScreenName: [info objectForKey: IMPersonScreenNameKey]];
   107 }
   108 
   109 + (BOOL) sendMessage: (NSString*)msg toPerson: (ABPerson*)abPerson
   110 {
   111     NSDictionary *info = [self iChatInfoForOnlinePerson: abPerson];
   112     if( info ) {
   113         iChatBuddy *buddy = [self buddyWithInfo: info];
   114         if( buddy ) {
   115             [sIChatApp send: msg to: buddy];
   116             return YES;
   117         }
   118     } 
   119     return NO;
   120 }
   121 
   122 
   123 + (BOOL) sendMessage: (NSString*)msg toBuddyWithScreenName: (NSString*)screenName
   124 {
   125     iChatBuddy *buddy = [self buddyWithScreenName: screenName];
   126     if( buddy ) {
   127         @try{
   128             [sIChatApp send: msg to: buddy];
   129             return YES;
   130         } @catch( NSException *x ) {
   131             Log(@"sendMessage:toBuddyWithScreenName: got exception: %@",x);
   132         }
   133     } 
   134     return NO;
   135 }
   136 
   137 
   138 @end
   139 
   140 
   141 /*
   142  Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
   143  
   144  Redistribution and use in source and binary forms, with or without modification, are permitted
   145  provided that the following conditions are met:
   146  
   147  * Redistributions of source code must retain the above copyright notice, this list of conditions
   148  and the following disclaimer.
   149  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
   150  and the following disclaimer in the documentation and/or other materials provided with the
   151  distribution.
   152  
   153  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
   154  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
   155  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
   156  BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   157  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
   158   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
   159  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
   160  THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   161  */