summaryrefslogtreecommitdiffstats
path: root/src/jabber.c
blob: cb90f2d271d5f27930ac2d3ddc72c5f84db4e484 (plain)
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
/* iksemel (XML parser for Jabber)
** Copyright (C) 2000-2003 Gurer Ozen
** This code is free software; you can redistribute it and/or
** modify it under the terms of GNU Lesser General Public License.
*/

#include "common.h"
#include "iksemel.h"

iksid *
iks_id_new (ikstack *s, const char *jid)
{
	iksid *id;
	char *src, *tmp;

/* FIXME: add jabber id validity checks to this function */
/* which characters are allowed in id parts? */

	if (!jid) return NULL;
	id = iks_stack_alloc (s, sizeof (iksid));
	if (!id) return NULL;
	memset (id, 0, sizeof (iksid));

	/* skip scheme */
	if (strncmp ("jabber:", jid, 7) == 0) jid += 7;

	id->full = iks_stack_strdup (s, jid, 0);
	src = id->full;

	/* split resource */
	tmp = strchr (src, '/');
	if (tmp) {
		id->partial = iks_stack_strdup (s, src, tmp - src);
		id->resource = tmp + 1;
		src = id->partial;
	} else {
		id->partial = src;
	}

	/* split user */
	tmp = strchr (src, '@');
	if (tmp) {
		id->user = iks_stack_strdup (s, src, tmp - src);
		src = ++tmp;
	}

	id->server = src;

	return id;
}

int
iks_id_cmp (iksid *a, iksid *b, int parts)
{
	int diff;

	if (!a || !b) return (IKS_ID_RESOURCE | IKS_ID_USER | IKS_ID_SERVER);
	diff = 0;
	if (parts & IKS_ID_RESOURCE && !(!a->resource && !b->resource) && iks_strcmp (a->resource, b->resource) != 0)
		diff += IKS_ID_RESOURCE;
	if (parts & IKS_ID_USER && !(!a->user && !b->user) && iks_strcasecmp (a->user, b->user) != 0)
		diff += IKS_ID_USER;
	if (parts & IKS_ID_SERVER && !(!a->server && !b->server) && iks_strcmp (a->server, b->server) != 0)
		diff += IKS_ID_SERVER;
	return diff;
}

ikspak *
iks_packet (iks *x)
{
	ikspak *pak;
	ikstack *s;
	char *tmp;

	s = iks_stack (x);
	pak = iks_stack_alloc (s, sizeof (ikspak));
	if (!pak) return NULL;
	memset (pak, 0, sizeof (ikspak));
	pak->x = x;
	tmp = iks_find_attrib (x, "from");
	if (tmp) pak->from = iks_id_new (s, tmp);
	pak->id = iks_find_attrib (x, "id");

	tmp = iks_find_attrib (x, "type");
	if (strcmp (iks_name (x), "message") == 0) {
		pak->type = IKS_PAK_MESSAGE;
		if (tmp) {
			if (strcmp (tmp, "chat") == 0)
				pak->subtype = IKS_TYPE_CHAT;
			else if (strcmp (tmp, "groupchat") == 0)
				pak->subtype = IKS_TYPE_GROUPCHAT;
			else if (strcmp (tmp, "headline") == 0)
				pak->subtype = IKS_TYPE_HEADLINE;
			else if (strcmp (tmp, "error") == 0)
				pak->subtype = IKS_TYPE_ERROR;
		}
	} else if (strcmp (iks_name (x), "presence") == 0) {
		pak->type = IKS_PAK_S10N;
		if (tmp) {
			if (strcmp (tmp, "unavailable") == 0) {
				pak->type = IKS_PAK_PRESENCE;
				pak->subtype = IKS_TYPE_UNAVAILABLE;
				pak->show = IKS_SHOW_UNAVAILABLE;
			} else if (strcmp (tmp, "probe") == 0) {
				pak->type = IKS_PAK_PRESENCE;
				pak->subtype = IKS_TYPE_PROBE;
			} else if(strcmp(tmp, "subscribe") == 0)
				pak->subtype = IKS_TYPE_SUBSCRIBE;
			else if(strcmp(tmp, "subscribed") == 0)
				pak->subtype = IKS_TYPE_SUBSCRIBED;
			else if(strcmp(tmp, "unsubscribe") == 0)
				pak->subtype = IKS_TYPE_UNSUBSCRIBE;
			else if(strcmp(tmp, "unsubscribed") == 0)
				pak->subtype = IKS_TYPE_UNSUBSCRIBED;
			else if(strcmp(tmp, "error") == 0)
				pak->subtype = IKS_TYPE_ERROR;
		} else {
			pak->type = IKS_PAK_PRESENCE;
			pak->subtype = IKS_TYPE_AVAILABLE;
			tmp = iks_find_cdata (x, "show");
			pak->show = IKS_SHOW_AVAILABLE;
			if (tmp) {
				if (strcmp (tmp, "chat") == 0)
					pak->show = IKS_SHOW_CHAT;
				else if (strcmp (tmp, "away") == 0)
					pak->show = IKS_SHOW_AWAY;
				else if (strcmp (tmp, "xa") == 0)
					pak->show = IKS_SHOW_XA;
				else if (strcmp (tmp, "dnd") == 0)
					pak->show = IKS_SHOW_DND;
			}
		}
	} else if (strcmp (iks_name (x), "iq") == 0) {
		iks *q;
		pak->type = IKS_PAK_IQ;
		if (tmp) {
			if (strcmp (tmp, "get") == 0)
				pak->subtype = IKS_TYPE_GET;
			else if (strcmp (tmp, "set") == 0)
				pak->subtype = IKS_TYPE_SET;
			else if (strcmp (tmp, "result") == 0)
				pak->subtype = IKS_TYPE_RESULT;
			else if (strcmp (tmp, "error") == 0)
				pak->subtype = IKS_TYPE_ERROR;
		}
		for (q = iks_child (x); q; q = iks_next (q)) {
			if (IKS_TAG == iks_type (q)) {
				char *ns;
				ns = iks_find_attrib (q, "xmlns");
				if (ns) {
					pak->query = q;
					pak->ns = ns;
					break;
				}
			}
		}
	}
	return pak;
}

iks *
iks_make_auth (iksid *id, const char *pass, const char *sid)
{
	iks *x, *y;

	x = iks_new ("iq");
	iks_insert_attrib (x, "type", "set");
	y = iks_insert (x, "query");
	iks_insert_attrib (y, "xmlns", IKS_NS_AUTH);
	iks_insert_cdata (iks_insert (y, "username"), id->user, 0);
	iks_insert_cdata (iks_insert (y, "resource"), id->resource, 0);
	if(sid) {
		char buf[41];
		iksha *sha;
		sha = iks_sha_new ();
		iks_sha_hash (sha, (const unsigned char*)sid, strlen (sid), 0);
		iks_sha_hash (sha, (const unsigned char*)pass, strlen (pass), 1);
		iks_sha_print (sha, buf);
		iks_sha_delete (sha);
		iks_insert_cdata (iks_insert (y, "digest"), buf, 40);
	} else {
		iks_insert_cdata (iks_insert (y, "password"), pass, 0);
	}
	return x;
}

iks *
iks_make_msg (enum iksubtype type, const char *to, const char *body)
{
	iks *x;
	char *t = NULL;

	x = iks_new ("message");
	switch (type) {
	case IKS_TYPE_CHAT: t = "chat"; break;
	case IKS_TYPE_GROUPCHAT: t = "groupchat"; break;
	case IKS_TYPE_HEADLINE: t = "headline"; break;
	default: break;
	}
	if (t) iks_insert_attrib (x, "type", t);
	if (to) iks_insert_attrib (x, "to", to);
	if (body) iks_insert_cdata (iks_insert (x, "body"), body, 0);
	return x;
}

iks *
iks_make_s10n (enum iksubtype type, const char *to, const char *msg)
{
	iks *x;
	char *t;

	x = iks_new ("presence");
	switch (type) {
		case IKS_TYPE_SUBSCRIBE: t = "subscribe"; break;
		case IKS_TYPE_SUBSCRIBED: t = "subscribed"; break;
		case IKS_TYPE_UNSUBSCRIBE: t = "unsubscribe"; break;
		case IKS_TYPE_UNSUBSCRIBED: t = "unsubscribed"; break;
		case IKS_TYPE_PROBE: t = "probe"; break;
		default: t = NULL; break;
	}
	if (t) iks_insert_attrib (x, "type", t);
	if (to) iks_insert_attrib (x, "to", to);
	if (msg) iks_insert_cdata(iks_insert (x, "status"), msg, 0);
	return x;
}

iks *
iks_make_pres (enum ikshowtype show, const char *status)
{
	iks *x;
	char *t;

	x = iks_new ("presence");
	switch (show) {
		case IKS_SHOW_CHAT: t = "chat"; break;
		case IKS_SHOW_AWAY: t = "away"; break;
		case IKS_SHOW_XA: t = "xa"; break;
		case IKS_SHOW_DND: t = "dnd"; break;
		case IKS_SHOW_UNAVAILABLE:
			t = NULL;
			iks_insert_attrib (x, "type", "unavailable");
			break;
		default: t = NULL; break;
	}
	if (t) iks_insert_cdata (iks_insert (x, "show"), t, 0);
	if (status) iks_insert_cdata(iks_insert (x, "status"), status, 0);
	return x;
}

iks *
iks_make_iq (enum iksubtype type, const char *xmlns)
{
	iks *x;
	char *t = NULL;

	x = iks_new ("iq");
	switch (type) {
	case IKS_TYPE_GET: t = "get"; break;
	case IKS_TYPE_SET: t = "set"; break;
	case IKS_TYPE_RESULT: t = "result"; break;
	case IKS_TYPE_ERROR: t = "error"; break;
	default: break;
	}
	if (t) iks_insert_attrib (x, "type", t);
	iks_insert_attrib (iks_insert (x, "query"), "xmlns", xmlns);

	return x;
}

iks *
iks_make_resource_bind (iksid *id)
{
	iks *x, *y, *z;

	x = iks_new("iq");
	iks_insert_attrib(x, "type", "set");
	y = iks_insert(x, "bind");
	iks_insert_attrib(y, "xmlns", IKS_NS_XMPP_BIND);
	if (id->resource && iks_strcmp(id->resource, "")) {
		z = iks_insert(y, "resource");
		iks_insert_cdata(z, id->resource, 0);
	}
	return x;
}

iks *
iks_make_session (void)
{
	iks *x, *y;

	x = iks_new ("iq");
	iks_insert_attrib (x, "type", "set");
	y = iks_insert (x, "session");
	iks_insert_attrib (y, "xmlns", IKS_NS_XMPP_SESSION);
	return x;
}

static int
iks_sasl_mechanisms (iks *x)
{
	int sasl_mech = 0;

	while (x) {
		if (!iks_strcmp(iks_cdata(iks_child(x)), "DIGEST-MD5"))
			sasl_mech |= IKS_STREAM_SASL_MD5;
		else if (!iks_strcmp(iks_cdata(iks_child(x)), "PLAIN"))
			sasl_mech |= IKS_STREAM_SASL_PLAIN;
		x = iks_next_tag(x);
	}
	return sasl_mech;
}

int
iks_stream_features (iks *x)
{
	int features = 0;

	if (iks_strcmp(iks_name(x), "stream:features"))
		return 0;
	for (x = iks_child(x); x; x = iks_next_tag(x))
		if (!iks_strcmp(iks_name(x), "starttls"))
			features |= IKS_STREAM_STARTTLS;
		else if (!iks_strcmp(iks_name(x), "bind"))
			features |= IKS_STREAM_BIND;
		else if (!iks_strcmp(iks_name(x), "session"))
			features |= IKS_STREAM_SESSION;
		else if (!iks_strcmp(iks_name(x), "mechanisms"))
			features |= iks_sasl_mechanisms(iks_child(x));
	return features;
}