Added IntegerArray.
5 // Created by Jens Alfke on 3/3/08.
6 // Copyright 2008 Jens Alfke. All rights reserved.
10 #import "iChatBridge.h"
11 #import <InstantMessage/IMService.h>
13 @implementation IChatUtils
16 static iChatApplication *sIChatApp;
21 sIChatApp = [SBApplication applicationWithBundleIdentifier: @"com.apple.iChat"];
22 sIChatApp.timeout = 5*60; // in ticks
27 + (SBApplication*) app {return sIChatApp;}
28 + (BOOL) isRunning {return sIChatApp.isRunning;}
29 + (void) activate {[sIChatApp activate];}
32 + (iChatTextChat*) activeChat
34 if( ! [sIChatApp isRunning] )
36 SBElementArray *chats = sIChatApp.textChats;
39 iChatTextChat *chat = [chats objectAtIndex: 0];
40 /*if( ! chat.active ) // somehow this returns NO for Bonjour chats
45 + (NSString*) activeChatPartner
47 iChatTextChat *chat = [self activeChat];
48 Log(@"Active chat = %@",chat);
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: @", "];
58 + (BOOL) sendMessage: (NSString*)msg
60 iChatTextChat *chat = [self activeChat];
63 [sIChatApp send: msg to: chat];
67 + (NSDictionary*) iChatInfoForOnlinePerson: (ABPerson*)abPerson
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
88 + (BOOL) isPersonOnline: (ABPerson*)abPerson
90 return [self iChatInfoForOnlinePerson: abPerson] != nil;
93 + (iChatBuddy*) buddyWithInfo: (NSDictionary*)info
95 NSString *ident = [info objectForKey: IMPersonScreenNameKey];
96 NSPredicate *pred = [NSPredicate predicateWithFormat: @"handle==%@", ident];
98 return [[[[sIChatApp buddies] filteredArrayUsingPredicate: pred] objectAtIndex: 0] get];
99 } @catch( NSException *x ) {
100 Log(@"buddyWithInfo got exception: %@",x);
105 + (BOOL) sendMessage: (NSString*)msg toPerson: (ABPerson*)abPerson
107 NSDictionary *info = [self iChatInfoForOnlinePerson: abPerson];
109 iChatBuddy *buddy = [self buddyWithInfo: info];
111 [sIChatApp send: msg to: buddy];
123 Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
125 Redistribution and use in source and binary forms, with or without modification, are permitted
126 provided that the following conditions are met:
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
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.