IChatUtils.m
changeset 11 e5976864dfe9
parent 4 64823cdde6a5
child 17 a1044ae95953
     1.1 --- a/IChatUtils.m	Mon Apr 14 13:58:48 2008 -0700
     1.2 +++ b/IChatUtils.m	Sat May 24 13:24:33 2008 -0700
     1.3 @@ -8,6 +8,7 @@
     1.4  
     1.5  #import "IChatUtils.h"
     1.6  #import "iChatBridge.h"
     1.7 +#import <InstantMessage/IMService.h>
     1.8  
     1.9  @implementation IChatUtils
    1.10  
    1.11 @@ -63,5 +64,79 @@
    1.12      return YES;
    1.13  }
    1.14  
    1.15 ++ (NSDictionary*) iChatInfoForOnlinePerson: (ABPerson*)abPerson
    1.16 +{
    1.17 +    if( ! abPerson )
    1.18 +        return nil;
    1.19 +    IMPersonStatus bestStatus = IMPersonStatusOffline;
    1.20 +    NSDictionary *bestInfo = nil;
    1.21 +    for( IMService *service in [IMService allServices] ) {
    1.22 +        for( NSString *name in [service screenNamesForPerson: abPerson] ) {
    1.23 +            NSDictionary *info = [service infoForScreenName: name];
    1.24 +            if( [[info objectForKey: IMPersonCapabilitiesKey] containsObject: IMCapabilityText] ) {
    1.25 +                IMPersonStatus status = [[info objectForKey: IMPersonStatusKey] intValue];
    1.26 +                if( IMComparePersonStatus(status,bestStatus) < 0 ) {    // yes, it returns the wrong sign
    1.27 +                    bestInfo = info;
    1.28 +                    bestStatus = status;
    1.29 +                }
    1.30 +            }
    1.31 +        }
    1.32 +    }
    1.33 +    return bestInfo;
    1.34 +}
    1.35 +
    1.36 ++ (BOOL) isPersonOnline: (ABPerson*)abPerson
    1.37 +{
    1.38 +    return [self iChatInfoForOnlinePerson: abPerson] != nil;
    1.39 +}
    1.40 +
    1.41 ++ (iChatBuddy*) buddyWithInfo: (NSDictionary*)info
    1.42 +{
    1.43 +    NSString *ident = [info objectForKey: IMPersonScreenNameKey];
    1.44 +    NSPredicate *pred = [NSPredicate predicateWithFormat: @"handle==%@", ident];
    1.45 +    @try{
    1.46 +        return [[[[sIChatApp buddies] filteredArrayUsingPredicate: pred] objectAtIndex: 0] get];
    1.47 +    } @catch( NSException *x ) {
    1.48 +        Log(@"buddyWithInfo got exception: %@",x);
    1.49 +    }
    1.50 +    return nil;
    1.51 +}
    1.52 +
    1.53 ++ (BOOL) sendMessage: (NSString*)msg toPerson: (ABPerson*)abPerson
    1.54 +{
    1.55 +    NSDictionary *info = [self iChatInfoForOnlinePerson: abPerson];
    1.56 +    if( info ) {
    1.57 +        iChatBuddy *buddy = [self buddyWithInfo: info];
    1.58 +        if( buddy ) {
    1.59 +            [sIChatApp send: msg to: buddy];
    1.60 +            return YES;
    1.61 +        }
    1.62 +    } 
    1.63 +    return NO;
    1.64 +}
    1.65 +
    1.66  
    1.67  @end
    1.68 +
    1.69 +
    1.70 +/*
    1.71 + Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
    1.72 + 
    1.73 + Redistribution and use in source and binary forms, with or without modification, are permitted
    1.74 + provided that the following conditions are met:
    1.75 + 
    1.76 + * Redistributions of source code must retain the above copyright notice, this list of conditions
    1.77 + and the following disclaimer.
    1.78 + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
    1.79 + and the following disclaimer in the documentation and/or other materials provided with the
    1.80 + distribution.
    1.81 + 
    1.82 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
    1.83 + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
    1.84 + FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
    1.85 + BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    1.86 + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
    1.87 +  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
    1.88 + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
    1.89 + THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.90 + */