Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 611 lines (486 sloc) 21.284 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
/*
* TNStropheContact.j
*
* Copyright (C) 2010 Antoine Mercadal <antoine.mercadal@inframonde.eu>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

@import <Foundation/Foundation.j>

@import "TNStropheJID.j"
@import "TNStropheGroup.j"
@import "TNStropheConnection.j"
@import "TNBase64Image.j"
@import "TNStropheGlobals.j"


/*! @ingroup strophecappuccino
this is an implementation of a XMPP Contact
*/
@implementation TNStropheContact: CPObject
{
    CPArray _messagesQueue @accessors(property=messagesQueue);
    CPArray _resources @accessors(property=resources);
    CPImage _statusIcon @accessors(property=statusIcon);
    CPNumber _numberOfEvents @accessors(property=numberOfEvents);
    TNStropheJID _JID @accessors(property=JID);
    CPString _groupName @accessors(property=groupName);
    CPString _nickname @accessors(property=nickname);
    CPString _nodeName @accessors(property=nodeName);
    CPString _type @accessors(property=type);
    CPString _vCard @accessors(property=vCard);
    CPString _XMPPShow @accessors(property=XMPPShow);
    CPString _XMPPStatus @accessors(property=XMPPStatus);
    TNBase64Image _avatar @accessors(property=avatar);
    TNStropheConnection _connection @accessors(property=connection);

    BOOL _isComposing;
    BOOL _askingVCard;
    CPImage _imageAway;
    CPImage _imageNewError;
    CPImage _imageNewMessage;
    CPImage _imageOffline;
    CPImage _imageOnline;
    CPImage _statusReminder;
}

#pragma mark -
#pragma mark Class methods

/*! create a contact using a given connection, JID and group
@param aConnection TNStropheConnection to use
@param aJID the JID of the contact
@param aGroup the group of the contact

@return an allocated and initialized TNStropheContact
*/
+ (TNStropheContact)contactWithConnection:(TNStropheConnection)aConnection JID:(CPString)aJID groupName:(CPString)aGroupName
{
    return [[TNStropheContact alloc] initWithConnection:aConnection JID:aJID groupName:aGroupName];
}

#pragma mark -
#pragma mark Initialization

/*! init a TNStropheContact with a given connection
@param aConnection TNStropheConnection to use

@return an initialized TNStropheContact
*/
- (id)initWithConnection:(TNStropheConnection)aConnection JID:(TNStropheJID)aJID groupName:(CPString)aGroupName
{
    if (self = [super init])
    {
        var bundle = [CPBundle bundleForClass:[self class]];

        _imageOffline = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"Offline.png"]];
        _imageOnline = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"Available.png"]];
        _imageBusy = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"Away.png"]];
        _imageAway = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"Idle.png"]];
        _imageDND = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"Blocked.png"]];
        _imageNewMessage = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"NewMessage.png"]];
        _imageNewError = [[CPImage alloc] initWithContentsOfFile:[bundle pathForResource:@"Error.png"]];

        _type = @"contact";
        _statusIcon = _imageOffline;
        _XMPPShow = TNStropheContactStatusOffline;
        _connection = aConnection;
        _messagesQueue = [CPArray array];
        _numberOfEvents = 0;
        _isComposing = NO;
        _askingVCard = NO;

        _resources = [CPArray array];

        _JID = aJID;
        _nickname = [_JID node];
        _groupName = aGroupName;
    }

    return self;
}


#pragma mark -
#pragma mark Status

/*! Processes presence. It populates the status of the contact
and send notifications
You should never have to use this method
@param aStanza the response TNStropheStanza
*/
- (BOOL)_didReceiveStatus:(TNStropheStanza)aStanza
{
    var resource = [aStanza fromResource],
        presenceStatus = [aStanza firstChildWithName:@"status"];

    [_JID setResource:[[aStanza from] resource]]

    if ([_JID resource] && ([_JID resource] != @"") && ![_resources containsObject:resource])
        [_resources addObject:resource];

    switch ([aStanza type])
    {
        case @"error":
            var errorCode = [[aStanza firstChildWithName:@"error"] valueForAttribute:@"code"];
            _XMPPShow = TNStropheContactStatusOffline;
            _XMPPStatus = @"Error code: " + errorCode;
            _statusIcon = _imageNewError;
            _statusReminder = _imageNewError;

            return NO;
        case @"unavailable":
            [_resources removeObject:resource];
            CPLogConsole(@"contact become unavailable from resource: " + resource + @". Resources left : " + _resources )

            if ([_resources count] == 0)
            {
                _XMPPShow = TNStropheContactStatusOffline;
                _statusIcon = _imageOffline;
                _statusReminder = _imageOffline;

                if (presenceStatus)
                    _XMPPStatus = [presenceStatus text];
            }
            break;
        case @"subscribe":
            _XMPPStatus = @"Asking subscribtion";
            break;
        case @"subscribed":
            break;
        case @"unsubscribe":
            break;
        case @"unsubscribed":
            _XMPPStatus = @"Unauthorized";
            break;
        default:
            _XMPPShow = TNStropheContactStatusOnline;
            _statusReminder = _imageOnline;
            if (_numberOfEvents === 0)
                _statusIcon = _imageOnline;

            if ([aStanza firstChildWithName:@"show"])
            {
                _XMPPShow = [[aStanza firstChildWithName:@"show"] text];
                switch (_XMPPShow)
                {
                    case TNStropheContactStatusBusy:
                        _statusIcon = _imageBusy;
                        _statusReminder = _imageBusy;
                        break;
                    case TNStropheContactStatusAway:
                        _statusIcon = _imageAway;
                        _statusReminder = _imageAway;
                        break;
                    case TNStropheContactStatusDND:
                        _statusIcon = _imageDND;
                        _statusReminder = _imageDND;
                        break;
                }
            }

            if (presenceStatus)
                _XMPPStatus = [presenceStatus text];

            if ([aStanza firstChildWithName:@"x"] && [[aStanza firstChildWithName:@"x"] valueForAttribute:@"xmlns"] == @"vcard-temp:x:update")
                [self getVCard];

            if (!_vCard && !_askingVCard)
                [self getVCard];

            break;
    }

    [[CPNotificationCenter defaultCenter] postNotificationName:TNStropheContactPresenceUpdatedNotification object:self];

    return YES;
}

- (void)sendStatus:(CPString)aStatus
{
    var statusStanza = [TNStropheStanza messageTo:_JID withAttributes:{"type": "chat"}];

    [statusStanza addChildWithName:aStatus andAttributes:{"xmlns": "http://jabber.org/protocol/chatstates"}];

    [self sendStanza:statusStanza andRegisterSelector:@selector(_didSendMessage:) ofObject:self];
}

/*! this allows to send "composing" information to a user. This will never send "paused".
you have to handle a timer if you want to automatically send pause after a while.
*/
- (void)sendComposing
{
    if (!_isComposing)
    {
        [self sendStatus:@"composing"];
        _isComposing = YES;
    }
}

/*! this allows to send "paused" information to a user.
*/
- (void)sendComposePaused
{
    [self sendStatus:@"paused"];

    _isComposing = NO;
}


#pragma mark -
#pragma mark Subscription

/*! subscribe to the contact
*/
- (void)subscribe
{
    [_connection send:[TNStropheStanza presenceTo:_JID withAttributes:{@"type": @"subscribed"} bare:YES]];
}

/*! unsubscribe from the contact
*/
- (void)unsubscribe
{
    [_connection send:[TNStropheStanza presenceTo:_JID WwthAttributes:{@"type": @"unsubscribed"} bare:YES]];
}

/*! ask subscribtion to the contact
*/
- (void)askSubscription
{
    [_connection send:[TNStropheStanza presenceTo:_JID withAttributes:{@"type": @"subscribe"} bare:YES]];
}


#pragma mark -
#pragma mark MetaData

- (CPString)description
{
    return _nickname;
}

/*! probe the contact's vCard
you should never have to use this message if you are using TNStropheRoster
*/
- (void)getVCard
{
    var uid = [_connection getUniqueId],
        vcardStanza = [TNStropheStanza iqTo:_JID withAttributes:{@"type": @"get", @"id": uid} bare:YES],
        params = [CPDictionary dictionaryWithObjectsAndKeys: uid, @"id"];

    [vcardStanza addChildWithName:@"vCard" andAttributes:{@"xmlns": @"vcard-temp"}];

    _askingVCard = YES;

    [_connection registerSelector:@selector(_didReceiveVCard:) ofObject:self withDict:params];
    [_connection send:vcardStanza];
}

/*! executed on getVCard result. Will post TNStropheContactVCardReceivedNotification
and send notifications. If vCard contains a PHOTO node, it will set the avatar TNBase64Image
property of the TNStropheContact
You should never have to use this method
@param aStanza the response TNStropheStanza
*/
- (BOOL)_didReceiveVCard:(TNStropheStanza)aStanza
{
    var aVCard = [aStanza firstChildWithName:@"vCard"];

    _askingVCard = NO;

    if (aVCard)
    {
        _vCard = aVCard;

        if ([aVCard firstChildWithName:@"NAME"])
            _nickname = [[aVCard firstChildWithName:@"NAME"] text];
        else
            _nickname = [_JID node]

        var photoNode;
        if (photoNode = [aVCard firstChildWithName:@"PHOTO"])
        {
            var contentType = [[photoNode firstChildWithName:@"TYPE"] text],
                data = [[photoNode firstChildWithName:@"BINVAL"] text];

            // the delegate will send the TNStropheContactVCardReceivedNotification when image will be ready
            _avatar = [TNBase64Image base64ImageWithContentType:contentType data:data delegate:self];
        }
        else
        {
            _avatar = nil;
            // otherwise we send the notification right now.
            [[CPNotificationCenter defaultCenter] postNotificationName:TNStropheContactVCardReceivedNotification object:self];
        }
    }

    return YES;
}

/*! this allows to change the contact nickname. Will post TNStropheContactNicknameUpdatedNotification
@param newNickname the new nickname
*/
- (void)changeNickname:(CPString)newNickname
{
    _nickname = newNickname;

    var stanza = [TNStropheStanza iqWithAttributes:{"type": "set"}];
    [stanza addChildWithName:@"query" andAttributes: {'xmlns':Strophe.NS.ROSTER}];
    [stanza addChildWithName:@"item" andAttributes:{"JID": [_JID bare], "name": _nickname}];
    [stanza addChildWithName:@"group" andAttributes:nil];
    [stanza addTextNode:_groupName];

    [_connection send:stanza];

    [[CPNotificationCenter defaultCenter] postNotificationName:TNStropheContactNicknameUpdatedNotification object:self];
}

/*! this allows to change the group of the contact. Will post TNStropheContactGroupUpdatedNotification
*/
- (void)changeGroup:(TNStropheGroup)newGroup
{
    var stanza = [TNStropheStanza iqWithAttributes:{"type": "set"}];
    [stanza addChildWithName:@"query" andAttributes: {'xmlns':Strophe.NS.ROSTER}];
    [stanza addChildWithName:@"item" andAttributes:{"JID": [_JID bare], "name": _nickname}];
    [stanza addChildWithName:@"group" andAttributes:nil];
    [stanza addTextNode:[newGroup name]];

    [_connection send:stanza];

    _groupName = [newGroup name];

    [[CPNotificationCenter defaultCenter] postNotificationName:TNStropheContactGroupUpdatedNotification object:self];
}

- (void)changeGroupName:(CPString)aNewName
{
    var stanza = [TNStropheStanza iqWithAttributes:{"type": "set"}];

    [stanza addChildWithName:@"query" andAttributes: {'xmlns':Strophe.NS.ROSTER}];
    [stanza addChildWithName:@"item" andAttributes:{"JID": [_JID bare], "name": _nickname}];
    [stanza addChildWithName:@"group" andAttributes:nil];
    [stanza addTextNode:aNewName];

    [_connection send:stanza];

    _groupName = aNewName;

    [[CPNotificationCenter defaultCenter] postNotificationName:TNStropheContactGroupUpdatedNotification object:self];
}


#pragma mark -
#pragma mark Communicating

- (void)sendStanza:(TNStropheStanza)aStanza
{
    [self sendStanza:aStanza withUserInfo:nil];
}

- (void)sendStanza:(TNStropheStanza)aStanza withUserInfo:(CPDictionary)userInfo
{
    [aStanza setTo:_JID];

    [_connection send:aStanza];

    [[CPNotificationCenter defaultCenter] postNotificationName:TNStropheContactStanzaSentNotification object:self userInfo:userInfo];
}

/*! send a TNStropheStanza to the contact. From, ant To value are rewritten. This message uses a given stanza id
in order to use it if you need. You should mostly use the
You should never have to use the method sendStanza:andRegisterSelector:ofObject: in most of the case

@param aStanza the TNStropheStanza to send to the contact
@param aSelector the selector to perform on response
@param anObject the object receiving the selector
@param anId the specific stanza ID to use

@return the associated registration id for the selector
*/
- (id)sendStanza:(TNStropheStanza)aStanza andRegisterSelector:(SEL)aSelector ofObject:(id)anObject withSpecificID:(id)anId
{
    var params = [CPDictionary dictionaryWithObjectsAndKeys:anId, @"id"],
        userInfo = [CPDictionary dictionaryWithObjectsAndKeys:aStanza, @"stanza", anId, @"id"],
        ret;

    [aStanza setID:anId];

    if (aSelector)
        ret = [_connection registerSelector:aSelector ofObject:anObject withDict:params];

    [self sendStanza:aStanza withUserInfo:userInfo];

    return ret;
}

/*! send a TNStropheStanza to the contact. From, ant To value are rewritten.

@param aStanza the TNStropheStanza to send to the contact
@param aSelector the selector to perform on response
@param anObject the object receiving the selector

@return the associated registration id for the selector
*/
- (id)sendStanza:(TNStropheStanza)aStanza andRegisterSelector:(SEL)aSelector ofObject:(id)anObject
{
    return [self sendStanza:aStanza andRegisterSelector:aSelector ofObject:anObject withSpecificID:[_connection getUniqueId]];
}

/*! register the contact to listen incoming messages
you should never have to use this message if you use TNStropheRoster
*/
- (void)getMessages
{
    var params = [CPDictionary dictionaryWithObjectsAndKeys:@"message", @"name",
                                                            [_JID bare], @"from",
                                                            {matchBare: true}, @"options"];

    [_connection registerSelector:@selector(_didReceiveMessage:) ofObject:self withDict:params];
}

/*! message sent when contact listening its message (using getMessages) and send appropriates notifications
you should never have to use this message.

@param aStanza the response stanza

@return YES in order to listen again
*/
- (BOOL)_didReceiveMessage:(id)aStanza
{
    var center = [CPNotificationCenter defaultCenter],
        userInfo = [CPDictionary dictionaryWithObjectsAndKeys:aStanza, @"stanza", [CPDate date], @"date"];

    if ([aStanza containsChildrenWithName:@"composing"])
        [center postNotificationName:TNStropheContactMessageComposing object:self userInfo:userInfo];

    if ([aStanza containsChildrenWithName:@"paused"])
        [center postNotificationName:TNStropheContactMessagePaused object:self userInfo:userInfo];

    if ([aStanza containsChildrenWithName:@"active"])
        [center postNotificationName:TNStropheContactMessageActive object:self userInfo:userInfo];

    if ([aStanza containsChildrenWithName:@"inactive"])
        [center postNotificationName:TNStropheContactMessageInactive object:self userInfo:userInfo];

    if ([aStanza containsChildrenWithName:@"gone"])
        [center postNotificationName:TNStropheContactMessageGone object:self userInfo:userInfo];

    if ([aStanza containsChildrenWithName:@"body"])
    {
        _statusIcon = _imageNewMessage;
        [_messagesQueue addObject:aStanza];

        _numberOfEvents++;
        [center postNotificationName:TNStropheContactMessageReceivedNotification object:self userInfo:userInfo];
    }

    return YES;
}

/*! send a message to the contact (of type chat)
@param aMessage CPString containing the message
*/
- (void)sendMessage:(CPString)aMessage
{
    [self sendMessage:aMessage withType:@"chat"];
}

/*! send a message to the contact
@param aMessage CPString containing the message
@param aType CPString containing type
*/
- (void)sendMessage:(CPString)aMessage withType:(CPString)aType
{
    var messageStanza = [TNStropheStanza messageWithAttributes:{@"type":aType}];

    [messageStanza addChildWithName:@"body"];
    [messageStanza addTextNode:aMessage];

    [self sendStanza:messageStanza];
}

/*! return the last TNStropheStanza message in the message queue and remove it form the queue.
Will post TNStropheContactMessageTreatedNotification.

@return TNStropheStanza the last message in queue
*/
- (TNStropheStanza)popMessagesQueue
{
    if ([_messagesQueue count] == 0)
        return Nil;

    var message = [_messagesQueue objectAtIndex:0];

    _numberOfEvents--;
    if (_numberOfEvents === 0)
        _statusIcon = _statusReminder;

    [_messagesQueue removeObjectAtIndex:0];

    [[CPNotificationCenter defaultCenter] postNotificationName:TNStropheContactMessageTreatedNotification object:self];

    return message;
}

/*! purge all message in queue. Will post TNStropheContactMessageTreatedNotification
*/
- (void)freeMessagesQueue
{
    _numberOfEvents = 0;
    _statusIcon = _statusReminder;

    [_messagesQueue removeAllObjects];

    [[CPNotificationCenter defaultCenter] postNotificationName:TNStropheContactMessageTreatedNotification object:self];
}


#pragma mark -
#pragma mark Delegates

/*! this method is called when the avatar image is ready.
@param anImage the image that sent the message
*/
- (void)imageDidLoad:(TNBase64Image)anImage
{
    [anImage setDelegate:nil];
    [[CPNotificationCenter defaultCenter] postNotificationName:TNStropheContactVCardReceivedNotification object:self];
}

@end

@implementation TNStropheContact (CPCoding)

- (id)initWithCoder:(CPCoder)aCoder
{
    self = [super initWithCoder:aCoder];

    if (self)
    {
        _JID = [aCoder decodeObjectForKey:@"_JID"];
        _nodeName = [aCoder decodeObjectForKey:@"_nodeName"];
        _groupName = [aCoder decodeObjectForKey:@"_groupName"];
        _nickname = [aCoder decodeObjectForKey:@"_nickname"];
        _XMPPStatus = [aCoder decodeObjectForKey:@"_XMPPStatus"];
        _resources = [aCoder decodeObjectForKey:@"_resources"];
        _XMPPShow = [aCoder decodeObjectForKey:@"_XMPPShow"];
        _statusIcon = [aCoder decodeObjectForKey:@"_statusIcon"];
        _type = [aCoder decodeObjectForKey:@"_type"];
        _vCard = [aCoder decodeObjectForKey:@"_vCard"];
        _messageQueue = [aCoder decodeObjectForKey:@"_messagesQueue"];
        _numberOfEvents = [aCoder decodeObjectForKey:@"_numberOfEvents"];
    }

    return self;
}

- (void)encodeWithCoder:(CPCoder)aCoder
{
    [aCoder encodeObject:_JID forKey:@"_JID"];
    [aCoder encodeObject:_nodeName forKey:@"_nodeName"];
    [aCoder encodeObject:_groupName forKey:@"_groupName"];
    [aCoder encodeObject:_nickname forKey:@"_nickname"];
    [aCoder encodeObject:_XMPPStatus forKey:@"_XMPPStatus"];
    [aCoder encodeObject:_XMPPShow forKey:@"_XMPPShow"];
    [aCoder encodeObject:_type forKey:@"_type"];
    [aCoder encodeObject:_statusIcon forKey:@"_statusIcon"];
    [aCoder encodeObject:_messagesQueue forKey:@"_messagesQueue"];
    [aCoder encodeObject:_numberOfEvents forKey:@"_numberOfEvents"];

    if (_resources)
        [aCoder encodeObject:_resources forKey:@"_resources"];

    if (_vCard)
        [aCoder encodeObject:_vCard forKey:@"_vCard"];
}

@end
Something went wrong with that request. Please try again.