IChatUtils.m
author Jens Alfke <jens@mooseyard.com>
Tue Jun 24 12:05:24 2008 -0700 (2008-06-24)
changeset 15 06e7d03fd23e
parent 4 64823cdde6a5
child 17 a1044ae95953
permissions -rw-r--r--
Added IntegerArray.
     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*) buddyWithInfo: (NSDictionary*)info
    94 {
    95     NSString *ident = [info objectForKey: IMPersonScreenNameKey];
    96     NSPredicate *pred = [NSPredicate predicateWithFormat: @"handle==%@", ident];
    97     @try{
    98         return [[[[sIChatApp buddies] filteredArrayUsingPredicate: pred] objectAtIndex: 0] get];
    99     } @catch( NSException *x ) {
   100         Log(@"buddyWithInfo got exception: %@",x);
   101     }
   102     return nil;
   103 }
   104 
   105 + (BOOL) sendMessage: (NSString*)msg toPerson: (ABPerson*)abPerson
   106 {
   107     NSDictionary *info = [self iChatInfoForOnlinePerson: abPerson];
   108     if( info ) {
   109         iChatBuddy *buddy = [self buddyWithInfo: info];
   110         if( buddy ) {
   111             [sIChatApp send: msg to: buddy];
   112             return YES;
   113         }
   114     } 
   115     return NO;
   116 }
   117 
   118 
   119 @end
   120 
   121 
   122 /*
   123  Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
   124  
   125  Redistribution and use in source and binary forms, with or without modification, are permitted
   126  provided that the following conditions are met:
   127  
   128  * Redistributions of source code must retain the above copyright notice, this list of conditions
   129  and the following disclaimer.
   130  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
   131  and the following disclaimer in the documentation and/or other materials provided with the
   132  distribution.
   133  
   134  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
   135  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
   136  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
   137  BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   138  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
   139   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
   140  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
   141  THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   142  */