-
Notifications
You must be signed in to change notification settings - Fork 2
/
XMPPRootIdentity.m
147 lines (134 loc) · 3.4 KB
/
XMPPRootIdentity.m
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
//
// XMPPRootIdentity.m
// Jabber
//
// Created by David Chisnall on 02/08/2005.
// Copyright 2005 __MyCompanyName__. All rights reserved.
//
#import "XMPPRootIdentity.h"
#import "XMPPResource.h"
#import "CompareHack.h"
#import <EtoileFoundation/EtoileFoundation.h>
@implementation XMPPRootIdentity
- (void)findType
{
NSString * serverDomain = [jid domain];
switch([jid type])
{
case serverJID:
case serverResourceJID:
case invalidJID:
basePriority = 0;
break;
case resourceJID:
case userJID:
//TODO: At some point, make this use Disco to find out what sort of server it really is.
if(
([serverDomain rangeOfString:@"msn"].location == NSNotFound) &&
([serverDomain rangeOfString:@"icq"].location == NSNotFound) &&
([serverDomain rangeOfString:@"aim"].location == NSNotFound) &&
([serverDomain rangeOfString:@"yahoo"].location == NSNotFound)
)
{
basePriority = 0;
}
else
{
basePriority = -7;
}
}
priority = basePriority;
}
- (id) initWithJID:(JID*)_jid withName:(NSString*)_name inGroup:(NSString*)_group forPerson:(id)_person
{
if(!(self = [super initWithJID:_jid
withName:_name
inGroup:_group
forPerson:_person]))
{
return nil;
}
if([jid type] == resourceJID)
{
JID * childJID = jid;
[self addResource:childJID];
jid = [jid rootJID];
}
subscription = nil;
[self findType];
return self;
}
- (id) init
{
SUPERINIT
resources = [[NSMutableDictionary alloc] init];
resourceList = [[NSMutableArray alloc] init];
return self;
}
- (void) addResource:(JID*)_jid
{
NSString * resourceName = [_jid resource];
XMPPResource * resource = [[XMPPResource alloc] initWithJID:_jid
withName:name
inGroup:group
forPerson:person];
[resource setRoot:self];
[resources setObject:resource forKey:resourceName];
[resourceList addObject:resource];
//[resourceList sortUsingSelector:@selector(compareByPriority)];
[resourceList sortUsingFunction:compareByPriority context:nil];
}
- (NSArray*) resources
{
return resourceList;
}
- (XMPPIdentity*) identityForResource:(NSString*)resource
{
return [resources objectForKey:resource];
}
- (void) setPresence:(XMPPPresence*)_presence
{
JID * presenceJID = [_presence jid];
//If we receive a presence stanza from the root JID, use that.
if([presenceJID isEqualToJID:jid])
{
//If the root identity is offline, all resources are offline
if([presence show] >= PRESENCE_OFFLINE)
{
[resources removeAllObjects];
}
[super setPresence:_presence];
}
else
{
NSString * resourceName = [presenceJID resource];
//If this is an offline presence, remove the resource referenced by it
if([_presence show] >= PRESENCE_OFFLINE)
{
[resourceList removeObjectIdenticalTo:[resources objectForKey:resourceName]];
[resources removeObjectForKey:resourceName];
}
else
{
XMPPResource * resource = [resources objectForKey:resourceName];
//Create a XMPPResource for the resource if one does not exist
if(resource == nil)
{
[self addResource:presenceJID];
resource = [resources objectForKey:resourceName];
}
[resource setPresence:_presence];
//[resourceList sortUsingSelector:@selector(compareByPriority:)];
[resourceList sortUsingFunction:compareByPriority context:nil];
}
}
}
- (XMPPPresence*) presence
{
if([resourceList count] > 0)
{
return [[resourceList objectAtIndex:0] presence];
}
return presence;
}
@end