Changes to config-file structure.
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*) buddyWithScreenName: (NSString*)screenName
95 NSPredicate *pred = [NSPredicate predicateWithFormat: @"handle==%@", screenName];
97 return [[[[sIChatApp buddies] filteredArrayUsingPredicate: pred] objectAtIndex: 0] get];
98 } @catch( NSException *x ) {
99 Log(@"buddyWithScreenName got exception: %@",x);
104 + (iChatBuddy*) buddyWithInfo: (NSDictionary*)info
106 return [self buddyWithScreenName: [info objectForKey: IMPersonScreenNameKey]];
109 + (BOOL) sendMessage: (NSString*)msg toPerson: (ABPerson*)abPerson
111 NSDictionary *info = [self iChatInfoForOnlinePerson: abPerson];
113 iChatBuddy *buddy = [self buddyWithInfo: info];
115 [sIChatApp send: msg to: buddy];
123 + (BOOL) sendMessage: (NSString*)msg toBuddyWithScreenName: (NSString*)screenName
125 iChatBuddy *buddy = [self buddyWithScreenName: screenName];
128 [sIChatApp send: msg to: buddy];
130 } @catch( NSException *x ) {
131 Log(@"sendMessage:toBuddyWithScreenName: got exception: %@",x);
142 Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
144 Redistribution and use in source and binary forms, with or without modification, are permitted
145 provided that the following conditions are met:
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
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.