IChatUtils.m
author Jens Alfke <jens@mooseyard.com>
Sun Jun 01 14:01:44 2008 -0700 (2008-06-01)
changeset 13 c59dec088990
parent 4 64823cdde6a5
child 17 a1044ae95953
permissions -rw-r--r--
* Made Test.h work with regular C99 without GCC extensions.
* Copied the necessary Google Toolbox source files into the project, so users don't have to download an additional library.
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@11
    93
+ (iChatBuddy*) buddyWithInfo: (NSDictionary*)info
jens@11
    94
{
jens@11
    95
    NSString *ident = [info objectForKey: IMPersonScreenNameKey];
jens@11
    96
    NSPredicate *pred = [NSPredicate predicateWithFormat: @"handle==%@", ident];
jens@11
    97
    @try{
jens@11
    98
        return [[[[sIChatApp buddies] filteredArrayUsingPredicate: pred] objectAtIndex: 0] get];
jens@11
    99
    } @catch( NSException *x ) {
jens@11
   100
        Log(@"buddyWithInfo got exception: %@",x);
jens@11
   101
    }
jens@11
   102
    return nil;
jens@11
   103
}
jens@11
   104
jens@11
   105
+ (BOOL) sendMessage: (NSString*)msg toPerson: (ABPerson*)abPerson
jens@11
   106
{
jens@11
   107
    NSDictionary *info = [self iChatInfoForOnlinePerson: abPerson];
jens@11
   108
    if( info ) {
jens@11
   109
        iChatBuddy *buddy = [self buddyWithInfo: info];
jens@11
   110
        if( buddy ) {
jens@11
   111
            [sIChatApp send: msg to: buddy];
jens@11
   112
            return YES;
jens@11
   113
        }
jens@11
   114
    } 
jens@11
   115
    return NO;
jens@11
   116
}
jens@11
   117
jens@0
   118
jens@0
   119
@end
jens@11
   120
jens@11
   121
jens@11
   122
/*
jens@11
   123
 Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
jens@11
   124
 
jens@11
   125
 Redistribution and use in source and binary forms, with or without modification, are permitted
jens@11
   126
 provided that the following conditions are met:
jens@11
   127
 
jens@11
   128
 * Redistributions of source code must retain the above copyright notice, this list of conditions
jens@11
   129
 and the following disclaimer.
jens@11
   130
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
jens@11
   131
 and the following disclaimer in the documentation and/or other materials provided with the
jens@11
   132
 distribution.
jens@11
   133
 
jens@11
   134
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
jens@11
   135
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
jens@11
   136
 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
jens@11
   137
 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
jens@11
   138
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
jens@11
   139
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
jens@11
   140
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
jens@11
   141
 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
jens@11
   142
 */