-
Notifications
You must be signed in to change notification settings - Fork 2
/
XMPPConversation.m
169 lines (150 loc) · 4.4 KB
/
XMPPConversation.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//
// XMPPConversation.m
// Jabber
//
// Created by David Chisnall on 17/09/2004.
// Copyright 2004 __MyCompanyName__. All rights reserved.
//
#import "XMPPConversation.h"
#ifdef WITH_XMPP_OBJECTSTORE
#import "XMPPObjectStore.h"
#endif
#import "XMPPMessage.h"
#import <EtoileXML/ETXMLWriter.h>
static NSMapTable * conversations = nil;
static Class delegateClass = Nil;
static NSMutableArray * filters;
@implementation XMPPConversation
+ (void) initialize
{
conversations = [[NSMapTable alloc] init];
filters = [[NSMutableArray alloc] init];
[super initialize];
}
+ (void) setViewClass:(Class)aClass
{
if([aClass conformsToProtocol:@protocol(XMPPConversationDelegate)])
{
delegateClass = aClass;
}
}
- (id) initWithPerson:(XMPPPerson*)corespondent forAccount:(XMPPAccount*)_account
{
SELFINIT;
connection = [_account connection];
name = [corespondent name];
remoteJID = [[corespondent defaultIdentity] jid];
remotePerson = corespondent;
//Register to receive any updates to this person's presence. This will let us switch to a different default corespondent if required
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updatePresence:)
name:@"TRXMPPIdentityPresenceChanged"
object:remotePerson];
return self;
}
- (id) init
{
SUPERINIT;
connection = nil;
return self;
}
+ (id) conversationWithPerson:(XMPPPerson*)corespondent forAccount:(XMPPAccount*)_account
{
if(conversations == nil)
{
conversations = [[NSMapTable alloc] init];
}
XMPPConversation * conversation = [conversations objectForKey:corespondent];
if(conversation == nil)
{
conversation = [[XMPPConversation alloc] initWithPerson:corespondent forAccount:_account];
[conversations setObject:conversation
forKey:corespondent];
}
return conversation;
}
+ (id) conversationForPerson:(XMPPPerson*)corespondent
{
return [conversations objectForKey:corespondent];
}
+ (void) releaseAllXMPPConversations
{
[conversations removeAllObjects];
}
- (void) handleMessage:(XMPPMessage*)aMessage
{
FOREACH(filters, filter, id<MessageFilter>)
{
[filter filterMessage:aMessage];
}
if([aMessage shouldDisplay])
{
[delegate displayMessage:aMessage incoming:YES];
}
}
- (id<NSObject,XMPPConversationDelegate>) delegate
{
return delegate;
}
- (void) setDelegate:(id<NSObject,XMPPConversationDelegate>)_delegate
{
delegate = _delegate;
XMPPPresence * presence = [[remotePerson defaultIdentity] presence];
[delegate setPresence:[presence show]
withMessage:[presence status]];
[delegate newRemoteJID:[[remotePerson defaultIdentity] jid]];
}
- (void) sendText:(id)_message
{
XMPPMessage * newMessage = [XMPPMessage messageWithBody:_message for:remoteJID withSubject:nil type:MESSAGE_TYPE_CHAT];
[delegate displayMessage:newMessage incoming:NO];
[newMessage writeToXMLWriter: [connection xmlWriter]];
}
#ifdef WITH_XMPP_OBJECTSTORE
- (XMPPObjectStore*) objectStoreForObjectWithUUID: (ETUUID*)uuid
andApplication: (NSString*)registeredName
{
XMPPMessage *newMessage = [XMPPMessage messageWithBody: nil for:remoteJID
withSubject: nil type: MESSAGE_TYPE_CHAT];
[newMessage beginWritingToXMLWriter: [connection xmlWriter]];
XMPPObjectStore *store = [[XMPPObjectStore alloc] initWithXMLWriter: [connection xmlWriter]
inConversation: self];
[store beginObjectWithUUID: uuid andApplication: registeredName];
return store;
}
#endif
//If the corespondent's presence changes, we may wish to talk to a different one of their identities. Check this, then update the UI.
- (void) updatePresence:(NSNotification*)_notification
{
JID * defaultJID = [[remotePerson defaultIdentity] jid];
//Are we still talking to the same person?
if(![remoteJID isEqual:defaultJID])
{
[delegate newRemoteJID:defaultJID];
}
XMPPPresence * presence = [[remotePerson identityForJID:remoteJID] presence];
[delegate setPresence:[presence show] withMessage:[presence status]];
}
- (JID*) remoteJID
{
return remoteJID;
}
- (XMPPPerson*) remotePerson
{
return remotePerson;
}
- (void) setJID:(JID*)jid
{
if([delegate newRemoteJID:jid])
{
remoteJID = jid;
//Update the presence display in the UI as well
XMPPPresence * presence = [[remotePerson identityForJID:remoteJID] presence];
[delegate setPresence:[presence show] withMessage:[presence status]];
}
}
- (NSString*) name
{
return name;
}
@end