diff -r 64823cdde6a5 -r e5976864dfe9 IChatUtils.m --- a/IChatUtils.m Mon Apr 14 13:58:48 2008 -0700 +++ b/IChatUtils.m Sat May 24 13:24:33 2008 -0700 @@ -8,6 +8,7 @@ #import "IChatUtils.h" #import "iChatBridge.h" +#import @implementation IChatUtils @@ -63,5 +64,79 @@ return YES; } ++ (NSDictionary*) iChatInfoForOnlinePerson: (ABPerson*)abPerson +{ + if( ! abPerson ) + return nil; + IMPersonStatus bestStatus = IMPersonStatusOffline; + NSDictionary *bestInfo = nil; + for( IMService *service in [IMService allServices] ) { + for( NSString *name in [service screenNamesForPerson: abPerson] ) { + NSDictionary *info = [service infoForScreenName: name]; + if( [[info objectForKey: IMPersonCapabilitiesKey] containsObject: IMCapabilityText] ) { + IMPersonStatus status = [[info objectForKey: IMPersonStatusKey] intValue]; + if( IMComparePersonStatus(status,bestStatus) < 0 ) { // yes, it returns the wrong sign + bestInfo = info; + bestStatus = status; + } + } + } + } + return bestInfo; +} + ++ (BOOL) isPersonOnline: (ABPerson*)abPerson +{ + return [self iChatInfoForOnlinePerson: abPerson] != nil; +} + ++ (iChatBuddy*) buddyWithInfo: (NSDictionary*)info +{ + NSString *ident = [info objectForKey: IMPersonScreenNameKey]; + NSPredicate *pred = [NSPredicate predicateWithFormat: @"handle==%@", ident]; + @try{ + return [[[[sIChatApp buddies] filteredArrayUsingPredicate: pred] objectAtIndex: 0] get]; + } @catch( NSException *x ) { + Log(@"buddyWithInfo got exception: %@",x); + } + return nil; +} + ++ (BOOL) sendMessage: (NSString*)msg toPerson: (ABPerson*)abPerson +{ + NSDictionary *info = [self iChatInfoForOnlinePerson: abPerson]; + if( info ) { + iChatBuddy *buddy = [self buddyWithInfo: info]; + if( buddy ) { + [sIChatApp send: msg to: buddy]; + return YES; + } + } + return NO; +} + @end + + +/* + Copyright (c) 2008, Jens Alfke . All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, are permitted + provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this list of conditions + and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions + and the following disclaimer in the documentation and/or other materials provided with the + distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI- + BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */