IChatUtils.m
author Jens Alfke <jens@mooseyard.com>
Wed Sep 02 08:41:25 2009 -0700 (2009-09-02)
changeset 35 5cab3034d3a1
parent 11 e5976864dfe9
permissions -rw-r--r--
10.6 compatibility: Fix some new compiler warnings, and work around apparent regressions in NSTask and -stringByStandardizingPath.
jens@0
     1
//
jens@0
     2
//  IChatUtils.m
jens@0
     3
//  MYUtilities
jens@0
     4
//
jens@0
     5
//  Created by Jens Alfke on 3/3/08.
jens@0
     6
//  Copyright 2008 Jens Alfke. All rights reserved.
jens@0
     7
//
jens@0
     8
jens@0
     9
#import "IChatUtils.h"
jens@0
    10
#import "iChatBridge.h"
jens@11
    11
#import <InstantMessage/IMService.h>
jens@0
    12
jens@0
    13
@implementation IChatUtils
jens@0
    14
jens@0
    15
jens@0
    16
static iChatApplication *sIChatApp;
jens@0
    17
jens@0
    18
+ (void) initialize
jens@0
    19
{
jens@0
    20
    if( ! sIChatApp ) {
jens@0
    21
        sIChatApp = [SBApplication applicationWithBundleIdentifier: @"com.apple.iChat"];
jens@0
    22
        sIChatApp.timeout = 5*60; // in ticks
jens@0
    23
    }
jens@0
    24
}
jens@0
    25
jens@0
    26
jens@0
    27
+ (SBApplication*) app  {return sIChatApp;}
jens@0
    28
+ (BOOL) isRunning      {return sIChatApp.isRunning;}
jens@0
    29
+ (void) activate       {[sIChatApp activate];}
jens@0
    30
jens@0
    31
jens@0
    32
+ (iChatTextChat*) activeChat
jens@0
    33
{
jens@0
    34
    if( ! [sIChatApp isRunning] )
jens@0
    35
        return nil;
jens@0
    36
    SBElementArray *chats = sIChatApp.textChats;
jens@0
    37
    if( chats.count==0 )
jens@0
    38
        return nil;
jens@0
    39
    iChatTextChat *chat = [chats objectAtIndex: 0];
jens@4
    40
    /*if( ! chat.active )               // somehow this returns NO for Bonjour chats
jens@4
    41
        return nil;*/
jens@0
    42
    return chat;
jens@0
    43
}    
jens@0
    44
jens@0
    45
+ (NSString*) activeChatPartner
jens@0
    46
{
jens@0
    47
    iChatTextChat *chat = [self activeChat];
jens@4
    48
    Log(@"Active chat = %@",chat);
jens@0
    49
    if( ! chat )
jens@0
    50
        return nil;
jens@0
    51
    NSMutableArray *names = $marray();
jens@0
    52
    for( iChatBuddy *b in [chat participants] )
jens@0
    53
        [names addObject: (b.fullName ?: b.name)];
jens@4
    54
    Log(@"Particpants = %@",names);
jens@0
    55
    return [names componentsJoinedByString: @", "];
jens@0
    56
}
jens@0
    57
jens@0
    58
+ (BOOL) sendMessage: (NSString*)msg
jens@0
    59
{
jens@0
    60
    iChatTextChat *chat = [self activeChat];
jens@0
    61
    if( ! chat )
jens@0
    62
        return NO;
jens@0
    63
    [sIChatApp send: msg to: chat];
jens@0
    64
    return YES;
jens@0
    65
}
jens@0
    66
jens@11
    67
+ (NSDictionary*) iChatInfoForOnlinePerson: (ABPerson*)abPerson
jens@11
    68
{
jens@11
    69
    if( ! abPerson )
jens@11
    70
        return nil;
jens@11
    71
    IMPersonStatus bestStatus = IMPersonStatusOffline;
jens@11
    72
    NSDictionary *bestInfo = nil;
jens@11
    73
    for( IMService *service in [IMService allServices] ) {
jens@11
    74
        for( NSString *name in [service screenNamesForPerson: abPerson] ) {
jens@11
    75
            NSDictionary *info = [service infoForScreenName: name];
jens@11
    76
            if( [[info objectForKey: IMPersonCapabilitiesKey] containsObject: IMCapabilityText] ) {
jens@11
    77
                IMPersonStatus status = [[info objectForKey: IMPersonStatusKey] intValue];
jens@11
    78
                if( IMComparePersonStatus(status,bestStatus) < 0 ) {    // yes, it returns the wrong sign
jens@11
    79
                    bestInfo = info;
jens@11
    80
                    bestStatus = status;
jens@11
    81
                }
jens@11
    82
            }
jens@11
    83
        }
jens@11
    84
    }
jens@11
    85
    return bestInfo;
jens@11
    86
}
jens@11
    87
jens@11
    88
+ (BOOL) isPersonOnline: (ABPerson*)abPerson
jens@11
    89
{
jens@11
    90
    return [self iChatInfoForOnlinePerson: abPerson] != nil;
jens@11
    91
}
jens@11
    92
jens@17
    93
+ (iChatBuddy*) buddyWithScreenName: (NSString*)screenName
jens@11
    94
{
jens@17
    95
    NSPredicate *pred = [NSPredicate predicateWithFormat: @"handle==%@", screenName];
jens@11
    96
    @try{
jens@11
    97
        return [[[[sIChatApp buddies] filteredArrayUsingPredicate: pred] objectAtIndex: 0] get];
jens@11
    98
    } @catch( NSException *x ) {
jens@17
    99
        Log(@"buddyWithScreenName got exception: %@",x);
jens@11
   100
    }
jens@11
   101
    return nil;
jens@11
   102
}
jens@11
   103
jens@17
   104
+ (iChatBuddy*) buddyWithInfo: (NSDictionary*)info
jens@17
   105
{
jens@17
   106
    return [self buddyWithScreenName: [info objectForKey: IMPersonScreenNameKey]];
jens@17
   107
}
jens@17
   108
jens@11
   109
+ (BOOL) sendMessage: (NSString*)msg toPerson: (ABPerson*)abPerson
jens@11
   110
{
jens@11
   111
    NSDictionary *info = [self iChatInfoForOnlinePerson: abPerson];
jens@11
   112
    if( info ) {
jens@11
   113
        iChatBuddy *buddy = [self buddyWithInfo: info];
jens@11
   114
        if( buddy ) {
jens@11
   115
            [sIChatApp send: msg to: buddy];
jens@11
   116
            return YES;
jens@11
   117
        }
jens@11
   118
    } 
jens@11
   119
    return NO;
jens@11
   120
}
jens@11
   121
jens@0
   122
jens@17
   123
+ (BOOL) sendMessage: (NSString*)msg toBuddyWithScreenName: (NSString*)screenName
jens@17
   124
{
jens@17
   125
    iChatBuddy *buddy = [self buddyWithScreenName: screenName];
jens@17
   126
    if( buddy ) {
jens@17
   127
        @try{
jens@17
   128
            [sIChatApp send: msg to: buddy];
jens@17
   129
            return YES;
jens@17
   130
        } @catch( NSException *x ) {
jens@17
   131
            Log(@"sendMessage:toBuddyWithScreenName: got exception: %@",x);
jens@17
   132
        }
jens@17
   133
    } 
jens@17
   134
    return NO;
jens@17
   135
}
jens@17
   136
jens@17
   137
jens@0
   138
@end
jens@11
   139
jens@11
   140
jens@11
   141
/*
jens@11
   142
 Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
jens@11
   143
 
jens@11
   144
 Redistribution and use in source and binary forms, with or without modification, are permitted
jens@11
   145
 provided that the following conditions are met:
jens@11
   146
 
jens@11
   147
 * Redistributions of source code must retain the above copyright notice, this list of conditions
jens@11
   148
 and the following disclaimer.
jens@11
   149
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
jens@11
   150
 and the following disclaimer in the documentation and/or other materials provided with the
jens@11
   151
 distribution.
jens@11
   152
 
jens@11
   153
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
jens@11
   154
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
jens@11
   155
 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
jens@11
   156
 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
jens@11
   157
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
jens@11
   158
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
jens@11
   159
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
jens@11
   160
 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
jens@11
   161
 */