ObjXMPP  Diff

Differences From Artifact [d958b131ca]:

To Artifact [6b93900f41]:


1
2

3
4
5
6
7
8
9
1

2
3
4
5
6
7
8
9

-
+







/*
 * Copyright (c) 2010, 2011, Jonathan Schleifer <js@webkeks.org>
 * Copyright (c) 2010, 2011, 2019, Jonathan Schleifer <js@webkeks.org>
 * Copyright (c) 2011, 2012, Florian Zeitz <florob@babelmonkeys.de>
 *
 * https://heap.zone/objxmpp/
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
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
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







-
-
-
-
-
-
+
+
+
+
+
+





-
-
-
-
-
+
+
+
+
+




-
-
-
-
+
+
+
+




-
-
-
-
+
+
+
+
+













-
+

-
-
+
+







-
+






-
+




-
-
-
-
+
+
+
+








@implementation AppDelegate
- (void)applicationDidFinishLaunching
{
	OFArray *arguments = [OFApplication arguments];

	XMPPPresence *pres = [XMPPPresence presence];
	[pres setShow: @"xa"];
	[pres setStatus: @"Bored"];
	[pres setPriority: [OFNumber numberWithInt8: 20]];
	[pres setTo: [XMPPJID JIDWithString: @"alice@example.com"]];
	[pres setFrom: [XMPPJID JIDWithString: @"bob@example.org"]];
	assert([[pres XMLString] isEqual: @"<presence to='alice@example.com' "
	pres.show = @"xa";
	pres.status = @"Bored";
	pres.priority = [OFNumber numberWithInt8: 20];
	pres.to = [XMPPJID JIDWithString: @"alice@example.com"];
	pres.from = [XMPPJID JIDWithString: @"bob@example.org"];
	assert([pres.XMLString isEqual: @"<presence to='alice@example.com' "
	    @"from='bob@example.org'><show>xa</show>"
	    @"<status>Bored</status><priority>20</priority>"
	    @"</presence>"]);

	XMPPPresence *pres2 = [XMPPPresence presence];
	[pres2 setShow: @"away"];
	[pres2 setStatus: @"Bored"];
	[pres2 setPriority: [OFNumber numberWithInt8: 23]];
	[pres2 setTo: [XMPPJID JIDWithString: @"alice@example.com"]];
	[pres2 setFrom: [XMPPJID JIDWithString: @"bob@example.org"]];
	pres2.show = @"away";
	pres2.status = @"Bored";
	pres2.priority = [OFNumber numberWithInt8: 23];
	pres2.to = [XMPPJID JIDWithString: @"alice@example.com"];
	pres2.from = [XMPPJID JIDWithString: @"bob@example.org"];

	assert([pres compare: pres2] == OF_ORDERED_ASCENDING);

	XMPPMessage *msg = [XMPPMessage messageWithType: @"chat"];
	[msg setBody: @"Hello everyone"];
	[msg setTo: [XMPPJID JIDWithString: @"jdev@conference.jabber.org"]];
	[msg setFrom: [XMPPJID JIDWithString: @"alice@example.com"]];
	assert([[msg XMLString] isEqual: @"<message type='chat' "
	msg.body = @"Hello everyone";
	msg.to = [XMPPJID JIDWithString: @"jdev@conference.jabber.org"];
	msg.from = [XMPPJID JIDWithString: @"alice@example.com"];
	assert([msg.XMLString isEqual: @"<message type='chat' "
	    @"to='jdev@conference.jabber.org' "
	    @"from='alice@example.com'><body>Hello everyone</body>"
	    @"</message>"]);

	XMPPIQ *iq = [XMPPIQ IQWithType: @"set" ID: @"128"];
	[iq setTo: [XMPPJID JIDWithString: @"juliet@capulet.lit"]];
	[iq setFrom: [XMPPJID JIDWithString: @"romeo@montague.lit"]];
	assert([[iq XMLString] isEqual: @"<iq type='set' id='128' "
	XMPPIQ *IQ = [XMPPIQ IQWithType: @"set"
				     ID: @"128"];
	IQ.to = [XMPPJID JIDWithString: @"juliet@capulet.lit"];
	IQ.from = [XMPPJID JIDWithString: @"romeo@montague.lit"];
	assert([IQ.XMLString isEqual: @"<iq type='set' id='128' "
	    @"to='juliet@capulet.lit' "
	    @"from='romeo@montague.lit'/>"]);

	OFXMLElement *elem = [OFXMLElement elementWithName: @"iq"];
	[elem addAttributeWithName: @"from"
		       stringValue: @"bob@localhost"];
	[elem addAttributeWithName: @"to"
		       stringValue: @"alice@localhost"];
	[elem addAttributeWithName: @"type"
		       stringValue: @"get"];
	[elem addAttributeWithName: @"id"
		       stringValue: @"42"];
	XMPPStanza *stanza = [XMPPStanza stanzaWithElement: elem];
	assert([[elem XMLString] isEqual: [stanza XMLString]]);
	assert([elem.XMLString isEqual: [stanza XMLString]]);
	assert(([[OFString stringWithFormat: @"%@, %@, %@, %@",
	    [[stanza from] fullJID], [[stanza to] fullJID], [stanza type],
	    [stanza ID]] isEqual: @"bob@localhost, alice@localhost, get, 42"]));
	    stanza.from.fullJID, stanza.to.fullJID, stanza.type, stanza.ID]
	    isEqual: @"bob@localhost, alice@localhost, get, 42"]));


	conn = [[XMPPConnection alloc] init];
	[conn addDelegate: self];

	XMPPFileStorage *storage =
	    [[XMPPFileStorage alloc] initWithFile: @"storage.binarypack"];
	[conn setDataStorage: storage];
	conn.dataStorage = storage;

	roster = [[XMPPRoster alloc] initWithConnection: conn];
	[roster addDelegate: self];

	[[XMPPStreamManagement alloc] initWithConnection: conn];

	if ([arguments count] != 3) {
	if (arguments.count != 3) {
		of_log(@"Invalid count of command line arguments!");
		[OFApplication terminateWithStatus: 1];
	}

	[conn setDomain: [arguments objectAtIndex: 0]];
	[conn setUsername: [arguments objectAtIndex: 1]];
	[conn setPassword: [arguments objectAtIndex: 2]];
	[conn setResource: @"ObjXMPP"];
	conn.domain = [arguments objectAtIndex: 0];
	conn.username = [arguments objectAtIndex: 1];
	conn.password = [arguments objectAtIndex: 2];
	conn.resource = @"ObjXMPP";

	[conn asyncConnect];
}

-  (void)connection: (XMPPConnection *)conn
  didReceiveElement: (OFXMLElement *)element
{
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
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







-
+

-
+

-
+










-
+





-
+





-
+




-
+




-
+

















-
+


-
-
+
+




-
+

-
+

-
+








- (void)connectionWasAuthenticated: (XMPPConnection *)conn
{
	of_log(@"Auth successful");
}

- (void)connection: (XMPPConnection *)conn_
     wasBoundToJID: (XMPPJID *)jid
     wasBoundToJID: (XMPPJID *)JID
{
	of_log(@"Bound to JID: %@", [jid fullJID]);
	of_log(@"Bound to JID: %@", JID.fullJID);
	of_log(@"Supports SM: %@",
	    [conn_ supportsStreamManagement] ? @"true" : @"false");
	    conn_.supportsStreamManagement ? @"true" : @"false");

	XMPPDiscoEntity *discoEntity =
	    [[XMPPDiscoEntity alloc] initWithConnection: conn];

	[discoEntity addIdentity:
	    [XMPPDiscoIdentity identityWithCategory: @"client"
					       type: @"pc"
					       name: @"ObjXMPP"]];

	XMPPDiscoNode *nodeMusic =
	    [XMPPDiscoNode discoNodeWithJID: jid
	    [XMPPDiscoNode discoNodeWithJID: JID
				       node: @"music"
				       name: @"My music"];
	[discoEntity addChildNode: nodeMusic];

	XMPPDiscoNode *nodeRHCP =
	    [XMPPDiscoNode discoNodeWithJID: jid
	    [XMPPDiscoNode discoNodeWithJID: JID
				       node: @"fa3b6"
				       name: @"Red Hot Chili Peppers"];
	[nodeMusic addChildNode: nodeRHCP];

	XMPPDiscoNode *nodeStop =
	    [XMPPDiscoNode discoNodeWithJID: jid
	    [XMPPDiscoNode discoNodeWithJID: JID
				       node: @"qwe87"
				       name: @"Can't Stop"];
	[nodeRHCP addChildNode: nodeStop];

	XMPPDiscoNode *nodeClueso = [XMPPDiscoNode discoNodeWithJID: jid
	XMPPDiscoNode *nodeClueso = [XMPPDiscoNode discoNodeWithJID: JID
							       node: @"ea386"
							       name: @"Clueso"];
	[nodeMusic addChildNode: nodeClueso];

	XMPPDiscoNode *nodeChicago = [XMPPDiscoNode discoNodeWithJID: jid
	XMPPDiscoNode *nodeChicago = [XMPPDiscoNode discoNodeWithJID: JID
							      node: @"qwr87"
							      name: @"Chicago"];
	[nodeClueso addChildNode: nodeChicago];

	[discoEntity addDiscoNode: nodeMusic];
	[discoEntity addDiscoNode: nodeRHCP];
	[discoEntity addDiscoNode: nodeClueso];
	[discoEntity addDiscoNode: nodeStop];
	[discoEntity addDiscoNode: nodeChicago];

	[roster requestRoster];
}

- (void)rosterWasReceived: (XMPPRoster *)roster_
{
	XMPPPresence *pres;

	of_log(@"Got roster: %@", [roster_ rosterItems]);
	of_log(@"Got roster: %@", roster_.rosterItems);

	pres = [XMPPPresence presence];
	[pres setPriority: [OFNumber numberWithInt8: 10]];
	[pres setStatus: @"ObjXMPP test is working!"];
	pres.priority = [OFNumber numberWithInt8: 10];
	pres.status = @"ObjXMPP test is working!";

	[conn sendStanza: pres];

#ifdef OF_HAVE_BLOCKS
	XMPPIQ *iq = [XMPPIQ IQWithType: @"get"
	XMPPIQ *IQ = [XMPPIQ IQWithType: @"get"
				     ID: [conn generateStanzaID]];
	[iq addChild: [OFXMLElement elementWithName: @"ping"
	[IQ addChild: [OFXMLElement elementWithName: @"ping"
					  namespace: @"urn:xmpp:ping"]];
	[conn	   sendIQ: iq
	[conn	   sendIQ: IQ
	    callbackBlock: ^ (XMPPConnection *c, XMPPIQ *resp) {
		of_log(@"Ping response: %@", resp);
	}];
#endif
}

- (void)connectionDidUpgradeToTLS: (XMPPConnection *)conn_