aboutsummaryrefslogtreecommitdiffstats
path: root/protocols/oscar/auth.c
blob: eb6a9d6472c27ac590e4f2245f697c7eacc6d415 (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
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
/*
 * Deals with the authorizer (group 0x0017=23, and old-style non-SNAC login).
 *
 */

#include <aim.h> 

#include "md5.h"

static int aim_encode_password(const char *password, unsigned char *encoded);

/* 
 * This just pushes the passed cookie onto the passed connection, without
 * the SNAC header or any of that.
 *
 * Very commonly used, as every connection except auth will require this to
 * be the first thing you send.
 *
 */
int aim_sendcookie(aim_session_t *sess, aim_conn_t *conn, const guint8 *chipsahoy)
{
	aim_frame_t *fr;
	aim_tlvlist_t *tl = NULL;

	if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x0001, 4+2+2+AIM_COOKIELEN)))
		return -ENOMEM;

	aimbs_put32(&fr->data, 0x00000001);
	aim_addtlvtochain_raw(&tl, 0x0006, AIM_COOKIELEN, chipsahoy);	
	aim_writetlvchain(&fr->data, &tl);
	aim_freetlvchain(&tl);

	aim_tx_enqueue(sess, fr);

	return 0;
}

/*
 * Normally the FLAP version is sent as the first few bytes of the cookie,
 * meaning you generally never call this.
 *
 * But there are times when something might want it seperate. Specifically,
 * libfaim sends this internally when doing SNAC login.
 *
 */
int aim_sendflapver(aim_session_t *sess, aim_conn_t *conn)
{
	aim_frame_t *fr;

	if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x01, 4)))
		return -ENOMEM;

	aimbs_put32(&fr->data, 0x00000001);

	aim_tx_enqueue(sess, fr);

	return 0;
}

/*
 * This is a bit confusing.
 *
 * Normal SNAC login goes like this:
 *   - connect
 *   - server sends flap version
 *   - client sends flap version
 *   - client sends screen name (17/6)
 *   - server sends hash key (17/7)
 *   - client sends auth request (17/2 -- aim_send_login)
 *   - server yells
 *
 * XOR login (for ICQ) goes like this:
 *   - connect
 *   - server sends flap version
 *   - client sends auth request which contains flap version (aim_send_login)
 *   - server yells
 *
 * For the client API, we make them implement the most complicated version,
 * and for the simpler version, we fake it and make it look like the more
 * complicated process.
 *
 * This is done by giving the client a faked key, just so we can convince
 * them to call aim_send_login right away, which will detect the session
 * flag that says this is XOR login and ignore the key, sending an ICQ
 * login request instead of the normal SNAC one.
 *
 * As soon as AOL makes ICQ log in the same way as AIM, this is /gone/.
 *
 * XXX This may cause problems if the client relies on callbacks only
 * being called from the context of aim_rxdispatch()...
 *
 */
static int goddamnicq(aim_session_t *sess, aim_conn_t *conn, const char *sn)
{
	aim_frame_t fr;
	aim_rxcallback_t userfunc;
	
	sess->flags &= ~AIM_SESS_FLAGS_SNACLOGIN;
	sess->flags |= AIM_SESS_FLAGS_XORLOGIN;

	fr.conn = conn;
	
	if ((userfunc = aim_callhandler(sess, conn, 0x0017, 0x0007)))
		userfunc(sess, &fr, "");

	return 0;
}

/*
 * In AIM 3.5 protocol, the first stage of login is to request login from the 
 * Authorizer, passing it the screen name for verification.  If the name is 
 * invalid, a 0017/0003 is spit back, with the standard error contents.  If 
 * valid, a 0017/0007 comes back, which is the signal to send it the main 
 * login command (0017/0002). 
 *
 */
int aim_request_login(aim_session_t *sess, aim_conn_t *conn, const char *sn)
{
	aim_frame_t *fr;
	aim_snacid_t snacid;
	aim_tlvlist_t *tl = NULL;
	
	if (!sess || !conn || !sn)
		return -EINVAL;

	if ((sn[0] >= '0') && (sn[0] <= '9'))
		return goddamnicq(sess, conn, sn);

	sess->flags |= AIM_SESS_FLAGS_SNACLOGIN;

	aim_sendflapver(sess, conn);

	if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+2+2+strlen(sn))))
		return -ENOMEM;

	snacid = aim_cachesnac(sess, 0x0017, 0x0006, 0x0000, NULL, 0);
	aim_putsnac(&fr->data, 0x0017, 0x0006, 0x0000, snacid);

	aim_addtlvtochain_raw(&tl, 0x0001, strlen(sn), (guint8 *)sn);
	aim_writetlvchain(&fr->data, &tl);
	aim_freetlvchain(&tl);

	aim_tx_enqueue(sess, fr);

	return 0;
}

/*
 * Part two of the ICQ hack.  Note the ignoring of the key and clientinfo.
 */
static int goddamnicq2(aim_session_t *sess, aim_conn_t *conn, const char *sn, const char *password)
{
	static const char clientstr[] = {"ICQ Inc. - Product of ICQ (TM) 2001b.5.17.1.3642.85"};
	static const char lang[] = {"en"};
	static const char country[] = {"us"};
	aim_frame_t *fr;
	aim_tlvlist_t *tl = NULL;
	guint8 *password_encoded;

	if (!(password_encoded = (guint8 *) g_malloc(strlen(password))))
		return -ENOMEM;

	if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x01, 1152))) {
		g_free(password_encoded);
		return -ENOMEM;
	}

	aim_encode_password(password, password_encoded);

	aimbs_put32(&fr->data, 0x00000001);
	aim_addtlvtochain_raw(&tl, 0x0001, strlen(sn), (guint8 *)sn);
	aim_addtlvtochain_raw(&tl, 0x0002, strlen(password), password_encoded);
	aim_addtlvtochain_raw(&tl, 0x0003, strlen(clientstr), (guint8 *)clientstr);
	aim_addtlvtochain16(&tl, 0x0016, 0x010a); /* cliend ID */
	aim_addtlvtochain16(&tl, 0x0017, 0x0005); /* major version */
	aim_addtlvtochain16(&tl, 0x0018, 0x0011); /* minor version */
	aim_addtlvtochain16(&tl, 0x0019, 0x0001); /* point version */
	aim_addtlvtochain16(&tl, 0x001a, 0x0e3a); /* build */
	aim_addtlvtochain32(&tl, 0x0014, 0x00000055); /* distribution chan */
	aim_addtlvtochain_raw(&tl, 0x000f, strlen(lang), (guint8 *)lang);
	aim_addtlvtochain_raw(&tl, 0x000e, strlen(country), (guint8 *)country);

	aim_writetlvchain(&fr->data, &tl);

	g_free(password_encoded);
	aim_freetlvchain(&tl);

	aim_tx_enqueue(sess, fr);

	return 0;
}

/*
 * send_login(int socket, char *sn, char *password)
 *  
 * This is the initial login request packet.
 *
 * NOTE!! If you want/need to make use of the aim_sendmemblock() function,
 * then the client information you send here must exactly match the
 * executable that you're pulling the data from.
 *
 * WinAIM 4.8.2540
 *   clientstring = "AOL Instant Messenger (SM), version 4.8.2540/WIN32"
 *   clientid = 0x0109
 *   major = 0x0004
 *   minor = 0x0008
 *   point = 0x0000
 *   build = 0x09ec
 *   t(0x0014) = 0x000000af
 *   t(0x004a) = 0x01
 *
 * WinAIM 4.3.2188:
 *   clientstring = "AOL Instant Messenger (SM), version 4.3.2188/WIN32"
 *   clientid = 0x0109
 *   major = 0x0400
 *   minor = 0x0003
 *   point = 0x0000
 *   build = 0x088c
 *   unknown = 0x00000086
 *   lang = "en"
 *   country = "us"
 *   unknown4a = 0x01
 *
 * Latest WinAIM that libfaim can emulate without server-side buddylists:
 *   clientstring = "AOL Instant Messenger (SM), version 4.1.2010/WIN32"
 *   clientid = 0x0004
 *   major  = 0x0004
 *   minor  = 0x0001
 *   point = 0x0000
 *   build  = 0x07da
 *   unknown= 0x0000004b
 *
 * WinAIM 3.5.1670:
 *   clientstring = "AOL Instant Messenger (SM), version 3.5.1670/WIN32"
 *   clientid = 0x0004
 *   major =  0x0003
 *   minor =  0x0005
 *   point = 0x0000
 *   build =  0x0686
 *   unknown =0x0000002a
 *
 * Java AIM 1.1.19:
 *   clientstring = "AOL Instant Messenger (TM) version 1.1.19 for Java built 03/24/98, freeMem 215871 totalMem 1048567, i686, Linus, #2 SMP Sun Feb 11 03:41:17 UTC 2001 2.4.1-ac9, IBM Corporation, 1.1.8, 45.3, Tue Mar 27 12:09:17 PST 2001"
 *   clientid = 0x0001
 *   major  = 0x0001
 *   minor  = 0x0001
 *   point = (not sent)
 *   build  = 0x0013
 *   unknown= (not sent)
 *   
 * AIM for Linux 1.1.112:
 *   clientstring = "AOL Instant Messenger (SM)"
 *   clientid = 0x1d09
 *   major  = 0x0001
 *   minor  = 0x0001
 *   point = 0x0001
 *   build  = 0x0070
 *   unknown= 0x0000008b
 *   serverstore = 0x01
 *
 */
int aim_send_login(aim_session_t *sess, aim_conn_t *conn, const char *sn, const char *password, struct client_info_s *ci, const char *key)
{
	aim_frame_t *fr;
	aim_tlvlist_t *tl = NULL;
	guint8 digest[16];
	aim_snacid_t snacid;

	if (!ci || !sn || !password)
		return -EINVAL;

	/*
	 * What the XORLOGIN flag _really_ means is that its an ICQ login,
	 * which is really stupid and painful, so its not done here.
	 *
	 */
	if (sess->flags & AIM_SESS_FLAGS_XORLOGIN)
		return goddamnicq2(sess, conn, sn, password);


	if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 1152)))
		return -ENOMEM;

	snacid = aim_cachesnac(sess, 0x0017, 0x0002, 0x0000, NULL, 0);
	aim_putsnac(&fr->data, 0x0017, 0x0002, 0x0000, snacid);

	aim_addtlvtochain_raw(&tl, 0x0001, strlen(sn), (guint8 *)sn);

	aim_encode_password_md5(password, key, digest);
	aim_addtlvtochain_raw(&tl, 0x0025, 16, digest);

	/*
	 * Newer versions of winaim have an empty type x004c TLV here.
	 */

	if (ci->clientstring)
		aim_addtlvtochain_raw(&tl, 0x0003, strlen(ci->clientstring), (guint8 *)ci->clientstring);
	aim_addtlvtochain16(&tl, 0x0016, (guint16)ci->clientid);
	aim_addtlvtochain16(&tl, 0x0017, (guint16)ci->major);
	aim_addtlvtochain16(&tl, 0x0018, (guint16)ci->minor);
	aim_addtlvtochain16(&tl, 0x0019, (guint16)ci->point);
	aim_addtlvtochain16(&tl, 0x001a, (guint16)ci->build);
	aim_addtlvtochain_raw(&tl, 0x000e, strlen(ci->country), (guint8 *)ci->country);
	aim_addtlvtochain_raw(&tl, 0x000f, strlen(ci->lang), (guint8 *)ci->lang);

	/*
	 * If set, old-fashioned buddy lists will not work. You will need
	 * to use SSI.
	 */
	aim_addtlvtochain8(&tl, 0x004a, 0x01);

	aim_writetlvchain(&fr->data, &tl);

	aim_freetlvchain(&tl);
	
	aim_tx_enqueue(sess, fr);

	return 0;
}

int aim_encode_password_md5(const char *password, const char *key, guint8 *digest)
{
	md5_state_t state;

	md5_init(&state);	
	md5_append(&state, (const md5_byte_t *)key, strlen(key));
	md5_append(&state, (const md5_byte_t *)password, strlen(password));
	md5_append(&state, (const md5_byte_t *)AIM_MD5_STRING, strlen(AIM_MD5_STRING));
	md5_finish(&state, (md5_byte_t *)digest);

	return 0;
}

/**
 * aim_encode_password - Encode a password using old XOR method
 * @password: incoming password
 * @encoded: buffer to put encoded password
 *
 * This takes a const pointer to a (null terminated) string
 * containing the unencoded password.  It also gets passed
 * an already allocated buffer to store the encoded password.
 * This buffer should be the exact length of the password without
 * the null.  The encoded password buffer /is not %NULL terminated/.
 *
 * The encoding_table seems to be a fixed set of values.  We'll
 * hope it doesn't change over time!  
 *
 * This is only used for the XOR method, not the better MD5 method.
 *
 */
static int aim_encode_password(const char *password, guint8 *encoded)
{
	guint8 encoding_table[] = {
	/* v2.1 table, also works for ICQ */
		0xf3, 0x26, 0x81, 0xc4,
		0x39, 0x86, 0xdb, 0x92,
		0x71, 0xa3, 0xb9, 0xe6,
		0x53, 0x7a, 0x95, 0x7c
	};
	int i;

	for (i = 0; i < strlen(password); i++)
		encoded[i] = (password[i] ^ encoding_table[i]);

	return 0;
}

/*
 * This is sent back as a general response to the login command.
 * It can be either an error or a success, depending on the
 * precense of certain TLVs.  
 *
 * The client should check the value passed as errorcode. If
 * its nonzero, there was an error.
 *
 */
static int parse(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
{
	aim_tlvlist_t *tlvlist;
	aim_rxcallback_t userfunc;
	struct aim_authresp_info info;
	int ret = 0;

	memset(&info, 0, sizeof(info));

	/*
	 * Read block of TLVs.  All further data is derived
	 * from what is parsed here.
	 */
	tlvlist = aim_readtlvchain(bs);

	/*
	 * No matter what, we should have a screen name.
	 */
	memset(sess->sn, 0, sizeof(sess->sn));
	if (aim_gettlv(tlvlist, 0x0001, 1)) {
		info.sn = aim_gettlv_str(tlvlist, 0x0001, 1);
		strncpy(sess->sn, info.sn, sizeof(sess->sn));
	}

	/*
	 * Check for an error code.  If so, we should also
	 * have an error url.
	 */
	if (aim_gettlv(tlvlist, 0x0008, 1)) 
		info.errorcode = aim_gettlv16(tlvlist, 0x0008, 1);
	if (aim_gettlv(tlvlist, 0x0004, 1))
		info.errorurl = aim_gettlv_str(tlvlist, 0x0004, 1);

	/*
	 * BOS server address.
	 */
	if (aim_gettlv(tlvlist, 0x0005, 1))
		info.bosip = aim_gettlv_str(tlvlist, 0x0005, 1);

	/*
	 * Authorization cookie.
	 */
	if (aim_gettlv(tlvlist, 0x0006, 1)) {
		aim_tlv_t *tmptlv;

		tmptlv = aim_gettlv(tlvlist, 0x0006, 1);

		info.cookie = tmptlv->value;
	}

	/*
	 * The email address attached to this account
	 *   Not available for ICQ logins.
	 */
	if (aim_gettlv(tlvlist, 0x0011, 1))
		info.email = aim_gettlv_str(tlvlist, 0x0011, 1);

	/*
	 * The registration status.  (Not real sure what it means.)
	 *   Not available for ICQ logins.
	 *
	 *   1 = No disclosure
	 *   2 = Limited disclosure
	 *   3 = Full disclosure
	 *
	 * This has to do with whether your email address is available
	 * to other users or not.  AFAIK, this feature is no longer used.
	 *
	 */
	if (aim_gettlv(tlvlist, 0x0013, 1))
		info.regstatus = aim_gettlv16(tlvlist, 0x0013, 1);

	if (aim_gettlv(tlvlist, 0x0040, 1))
		info.latestbeta.build = aim_gettlv32(tlvlist, 0x0040, 1);
	if (aim_gettlv(tlvlist, 0x0041, 1))
		info.latestbeta.url = aim_gettlv_str(tlvlist, 0x0041, 1);
	if (aim_gettlv(tlvlist, 0x0042, 1))
		info.latestbeta.info = aim_gettlv_str(tlvlist, 0x0042, 1);
	if (aim_gettlv(tlvlist, 0x0043, 1))
		info.latestbeta.name = aim_gettlv_str(tlvlist, 0x0043, 1);
	if (aim_gettlv(tlvlist, 0x0048, 1))
		; /* no idea what this is */

	if (aim_gettlv(tlvlist, 0x0044, 1))
		info.latestrelease.build = aim_gettlv32(tlvlist, 0x0044, 1);
	if (aim_gettlv(tlvlist, 0x0045, 1))
		info.latestrelease.url = aim_gettlv_str(tlvlist, 0x0045, 1);
	if (aim_gettlv(tlvlist, 0x0046, 1))
		info.latestrelease.info = aim_gettlv_str(tlvlist, 0x0046, 1);
	if (aim_gettlv(tlvlist, 0x0047, 1))
		info.latestrelease.name = aim_gettlv_str(tlvlist, 0x0047, 1);
	if (aim_gettlv(tlvlist, 0x0049, 1))
		; /* no idea what this is */


	if ((userfunc = aim_callhandler(sess, rx->conn, snac ? snac->family : 0x0017, snac ? snac->subtype : 0x0003)))
		ret = userfunc(sess, rx, &info);

	g_free(info.sn);
	g_free(info.bosip);
	g_free(info.errorurl);
	g_free(info.email);
	g_free(info.latestrelease.name);
	g_free(info.latestrelease.url);
	g_free(info.latestrelease.info);
	g_free(info.latestbeta.name);
	g_free(info.latestbeta.url);
	g_free(info.latestbeta.info);

	aim_freetlvchain(&tlvlist);

	return ret;
}

/*
 * Middle handler for 0017/0007 SNACs.  Contains the auth key prefixed
 * by only its length in a two byte word.
 *
 * Calls the client, which should then use the value to call aim_send_login.
 *
 */
static int keyparse(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
{
	int keylen, ret = 1;
	aim_rxcallback_t userfunc;
	char *keystr;

	keylen = aimbs_get16(bs);
	keystr = aimbs_getstr(bs, keylen);

	if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype)))
		ret = userfunc(sess, rx, keystr);

	g_free(keystr); 

	return ret;
}

static int snachandler(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs)
{

	if (snac->subtype == 0x0003)
		return parse(sess, mod, rx, snac, bs);
	else if (snac->subtype == 0x0007)
		return keyparse(sess, mod, rx, snac, bs);

	return 0;
}

int auth_modfirst(aim_session_t *sess, aim_module_t *mod)
{

	mod->family = 0x0017;
	mod->version = 0x0000;
	mod->flags = 0;
	strncpy(mod->name, "auth", sizeof(mod->name));
	mod->snachandler = snachandler;

	return 0;
}
an class="w"> if (!strchr(strchr(given, '@'), '/')) valid = g_strdup_printf("%s/%s", given, resource); else valid = g_strdup(given); return valid; } static gjconn gjab_new(char *user, char *pass, void *priv) { pool p; gjconn gjc; if (!user) return (NULL); p = pool_new(); if (!p) return (NULL); gjc = pmalloc_x(p, sizeof(gjconn_struct), 0); if (!gjc) { pool_free(p); /* no need for this anymore! */ return (NULL); } gjc->p = p; if((gjc->user = jid_new(p, user)) == NULL) { pool_free(p); /* no need for this anymore! */ return (NULL); } gjc->pass = pstrdup(p, pass); gjc->state = JCONN_STATE_OFF; gjc->was_connected = 0; gjc->id = 1; gjc->fd = -1; gjc->priv = priv; return gjc; } static void gjab_delete(gjconn gjc) { if (!gjc) return; gjab_stop(gjc); pool_free(gjc->p); } static void gjab_state_handler(gjconn gjc, gjconn_state_h h) { if (!gjc) return; gjc->on_state = h; } static void gjab_packet_handler(gjconn gjc, gjconn_packet_h h) { if (!gjc) return; gjc->on_packet = h; } static void gjab_stop(gjconn gjc) { if (!gjc || gjc->state == JCONN_STATE_OFF) return; gjab_send_raw(gjc, "</stream:stream>"); gjc->state = JCONN_STATE_OFF; gjc->was_connected = 0; if (gjc->ssl) { ssl_disconnect(gjc->ssl); gjc->ssl = NULL; } else { closesocket(gjc->fd); } gjc->fd = -1; XML_ParserFree(gjc->parser); gjc->parser = NULL; } /* static int gjab_getfd(gjconn gjc) { if (gjc) return gjc->fd; else return -1; } static jid gjab_getjid(gjconn gjc) { if (gjc) return (gjc->user); else return NULL; } static char *gjab_getsid(gjconn gjc) { if (gjc) return (gjc->sid); else return NULL; } */ static char *gjab_getid(gjconn gjc) { g_snprintf(gjc->idbuf, 8, "%d", gjc->id++); return &gjc->idbuf[0]; } static void gjab_send(gjconn gjc, xmlnode x) { if (gjc && gjc->state != JCONN_STATE_OFF) { char *buf = xmlnode2str(x); if (!buf) return; else if (gjc->ssl) ssl_write(gjc->ssl, buf, strlen(buf)); else write(gjc->fd, buf, strlen(buf)); } } static void gjab_send_raw(gjconn gjc, const char *str) { if (gjc && gjc->state != JCONN_STATE_OFF) { int len; /* * JFIXME: No error detection?!?! */ if (gjc->ssl) len = ssl_write(gjc->ssl, str, strlen(str)); else len = write(gjc->fd, str, strlen(str)); if(len < 0) { /* Do NOT write to stdout/stderr directly, IRC clients might get confused, and we don't want that... fprintf(stderr, "DBG: Problem sending. Error: %d\n", errno); fflush(stderr); */ } } } static void gjab_reqroster(gjconn gjc) { xmlnode x; x = jutil_iqnew(JPACKET__GET, NS_ROSTER); xmlnode_put_attrib(x, "id", gjab_getid(gjc)); gjab_send(gjc, x); xmlnode_free(x); } static void gjab_reqauth(gjconn gjc) { xmlnode x, y, z; char *user; if (!gjc) return; x = jutil_iqnew(JPACKET__GET, NS_AUTH); xmlnode_put_attrib(x, "id", IQID_AUTH); y = xmlnode_get_tag(x, "query"); user = gjc->user->user; if (user) { z = xmlnode_insert_tag(y, "username"); xmlnode_insert_cdata(z, user, -1); } gjab_send(gjc, x); xmlnode_free(x); } static void gjab_auth(gjconn gjc) { xmlnode x, y, z; char *hash, *user; if (!gjc) return; x = jutil_iqnew(JPACKET__SET, NS_AUTH); xmlnode_put_attrib(x, "id", IQID_AUTH); y = xmlnode_get_tag(x, "query"); user = gjc->user->user; if (user) { z = xmlnode_insert_tag(y, "username"); xmlnode_insert_cdata(z, user, -1); } z = xmlnode_insert_tag(y, "resource"); xmlnode_insert_cdata(z, gjc->user->resource, -1); if (gjc->sid) { z = xmlnode_insert_tag(y, "digest"); hash = pmalloc(x->p, strlen(gjc->sid) + strlen(gjc->pass) + 1); strcpy(hash, gjc->sid); strcat(hash, gjc->pass); hash = shahash(hash); xmlnode_insert_cdata(z, hash, 40); } else { z = xmlnode_insert_tag(y, "password"); xmlnode_insert_cdata(z, gjc->pass, -1); } gjab_send(gjc, x); xmlnode_free(x); return; } static void gjab_recv(gjconn gjc) { static char buf[4096]; int len; if (!gjc || gjc->state == JCONN_STATE_OFF) return; if (gjc->ssl) len = ssl_read(gjc->ssl, buf, sizeof(buf) - 1); else len = read(gjc->fd, buf, sizeof(buf) - 1); if (len > 0) { struct jabber_data *jd = GJ_GC(gjc)->proto_data; buf[len] = '\0'; XML_Parse(gjc->parser, buf, len, 0); if (jd->die) signoff(GJ_GC(gjc)); } else if (len == 0 || (len < 0 && (!sockerr_again() || gjc->ssl))) { STATE_EVT(JCONN_STATE_OFF) } } static void startElement(void *userdata, const char *name, const char **attribs) { xmlnode x; gjconn gjc = (gjconn) userdata; if (gjc->current) { /* Append the node to the current one */ x = xmlnode_insert_tag(gjc->current, name); xmlnode_put_expat_attribs(x, attribs); gjc->current = x; } else { x = xmlnode_new_tag(name); xmlnode_put_expat_attribs(x, attribs); if (strcmp(name, "stream:stream") == 0) { /* special case: name == stream:stream */ /* id attrib of stream is stored for digest auth */ gjc->sid = g_strdup(xmlnode_get_attrib(x, "id")); /* STATE_EVT(JCONN_STATE_AUTH) */ xmlnode_free(x); } else { gjc->current = x; } } } static void endElement(void *userdata, const char *name) { gjconn gjc = (gjconn) userdata; xmlnode x; jpacket p; if (gjc->current == NULL) { /* we got </stream:stream> */ STATE_EVT(JCONN_STATE_OFF) return; } x = xmlnode_get_parent(gjc->current); if (!x) { /* it is time to fire the event */ p = jpacket_new(gjc->current); if (gjc->on_packet) (gjc->on_packet) (gjc, p); else xmlnode_free(gjc->current); } gjc->current = x; } static gboolean jabber_callback(gpointer data, gint source, b_input_condition condition) { struct gaim_connection *gc = (struct gaim_connection *)data; struct jabber_data *jd = (struct jabber_data *)gc->proto_data; gjab_recv(jd->gjc); return TRUE; } static void charData(void *userdata, const char *s, int slen) { gjconn gjc = (gjconn) userdata; if (gjc->current) xmlnode_insert_cdata(gjc->current, s, slen); } static gboolean gjab_connected(gpointer data, gint source, b_input_condition cond) { xmlnode x; char *t, *t2; struct gaim_connection *gc = data; struct jabber_data *jd; gjconn gjc; if (!g_slist_find(get_connections(), gc)) { closesocket(source); return FALSE; } jd = gc->proto_data; gjc = jd->gjc; if (gjc->fd != source) gjc->fd = source; if (source == -1) { STATE_EVT(JCONN_STATE_OFF) return FALSE; } gjc->state = JCONN_STATE_CONNECTED; STATE_EVT(JCONN_STATE_CONNECTED) /* start stream */ x = jutil_header(NS_CLIENT, gjc->user->server); t = xmlnode2str(x); /* this is ugly, we can create the string here instead of jutil_header */ /* what do you think about it? -madcat */ t2 = strstr(t, "/>"); *t2++ = '>'; *t2 = '\0'; gjab_send_raw(gjc, "<?xml version='1.0'?>"); gjab_send_raw(gjc, t); xmlnode_free(x); gjc->state = JCONN_STATE_ON; STATE_EVT(JCONN_STATE_ON); gc = GJ_GC(gjc); gc->inpa = b_input_add(gjc->fd, GAIM_INPUT_READ, jabber_callback, gc); return FALSE; } static gboolean gjab_connected_ssl(gpointer data, void *source, b_input_condition cond) { struct gaim_connection *gc = data; struct jabber_data *jd; gjconn gjc; jd = gc->proto_data; gjc = jd->gjc; if (source == NULL) { STATE_EVT(JCONN_STATE_OFF) return FALSE; } if (!g_slist_find(get_connections(), gc)) { ssl_disconnect(source); return FALSE; } return gjab_connected(data, gjc->fd, cond); } static void gjab_start(gjconn gjc) { account_t *acc; int port = -1, ssl = 0; char *server = NULL; if (!gjc || gjc->state != JCONN_STATE_OFF) return; acc = GJ_GC(gjc)->acc; server = acc->server; port = set_getint(&acc->set, "port"); ssl = set_getbool(&acc->set, "ssl"); if (port < JABBER_PORT_MIN || port > JABBER_PORT_MAX) { serv_got_crap(GJ_GC(gjc), "For security reasons, the Jabber port number must be in the %d-%d range.", JABBER_PORT_MIN, JABBER_PORT_MAX); STATE_EVT(JCONN_STATE_OFF) return; } if (server == NULL) server = g_strdup(gjc->user->server); gjc->parser = XML_ParserCreate(NULL); XML_SetUserData(gjc->parser, (void *)gjc); XML_SetElementHandler(gjc->parser, startElement, endElement); XML_SetCharacterDataHandler(gjc->parser, charData); if (ssl) { if ((gjc->ssl = ssl_connect(server, port, gjab_connected_ssl, GJ_GC(gjc)))) gjc->fd = ssl_getfd(gjc->ssl); else gjc->fd = -1; } else { gjc->fd = proxy_connect(server, port, gjab_connected, GJ_GC(gjc)); } if (!acc->gc || (gjc->fd < 0)) { STATE_EVT(JCONN_STATE_OFF) return; } } /* * Find existing/active Jabber chat */ static struct jabber_chat *find_existing_chat(struct gaim_connection *gc, jid chat) { GSList *jcs = ((struct jabber_data *)gc->proto_data)->chats; struct jabber_chat *jc = NULL; while (jcs) { jc = jcs->data; if (jc->state == JCS_ACTIVE && !jid_cmpx(chat, jc->Jid, JID_USER | JID_SERVER)) break; jc = NULL; jcs = jcs->next; } return jc; } /* * Find pending chat */ static struct jabber_chat *find_pending_chat(struct gaim_connection *gc, jid chat) { GSList *jcs = ((struct jabber_data *)gc->proto_data)->chats; struct jabber_chat *jc = NULL; while (jcs) { jc = jcs->data; if (jc->state == JCS_PENDING && !jid_cmpx(chat, jc->Jid, JID_USER | JID_SERVER)) break; jc = NULL; jcs = jcs->next; } return jc; } static gboolean find_chat_buddy(struct conversation *b, char *name) { GList *m = b->in_room; while (m) { if (!strcmp(m->data, name)) return TRUE; m = m->next; } return FALSE; } /* * Remove a buddy from the (gaim) buddylist (if he's on it) */ static void jabber_remove_gaim_buddy(struct gaim_connection *gc, char *buddyname) { struct buddy *b; if ((b = find_buddy(gc, buddyname)) != NULL) { /* struct group *group; group = find_group_by_buddy(gc, buddyname); remove_buddy(gc, group, b); */ jabber_remove_buddy(gc, b->name, JABBER_GROUP); } } /* * keep track of away msg same as yahoo plugin */ static void jabber_track_away(gjconn gjc, jpacket p, char *name, char *type) { struct jabber_data *jd = GJ_GC(gjc)->proto_data; gpointer val = g_hash_table_lookup(jd->hash, name); char *show; char *vshow = NULL; char *status = NULL; char *msg = NULL; if (type && (g_strcasecmp(type, "unavailable") == 0)) { vshow = _("Unavailable"); } else { if((show = xmlnode_get_tag_data(p->x, "show")) != NULL) { if (!g_strcasecmp(show, "away")) { vshow = _("Away"); } else if (!g_strcasecmp(show, "chat")) { vshow = _("Online"); } else if (!g_strcasecmp(show, "xa")) { vshow = _("Extended Away"); } else if (!g_strcasecmp(show, "dnd")) { vshow = _("Do Not Disturb"); } } } status = xmlnode_get_tag_data(p->x, "status"); if(vshow != NULL || status != NULL ) { /* kinda hokey, but it works :-) */ msg = g_strdup_printf("%s%s%s", (vshow == NULL? "" : vshow), (vshow == NULL || status == NULL? "" : ": "), (status == NULL? "" : status)); } else { msg = g_strdup(_("Online")); } if (val) { g_free(val); g_hash_table_insert(jd->hash, name, msg); } else { g_hash_table_insert(jd->hash, g_strdup(name), msg); } } static time_t iso8601_to_time(char *timestamp) { struct tm t; time_t retval = 0; if(sscanf(timestamp,"%04d%02d%02dT%02d:%02d:%02d", &t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec)) { t.tm_year -= 1900; t.tm_mon -= 1; t.tm_isdst = 0; retval = mktime(&t); # ifdef HAVE_TM_GMTOFF retval += t.tm_gmtoff; # else # ifdef HAVE_TIMEZONE tzset(); /* making sure */ retval -= timezone; # endif # endif } return retval; } static void jabber_handlemessage(gjconn gjc, jpacket p) { xmlnode y, xmlns, z; time_t time_sent = time(NULL); char *from = NULL, *msg = NULL, *type = NULL; char m[BUF_LONG * 2]; type = xmlnode_get_attrib(p->x, "type"); z = xmlnode_get_firstchild(p->x); while(z) { if(NSCHECK(z,NS_DELAY)) { char *timestamp = xmlnode_get_attrib(z,"stamp"); time_sent = iso8601_to_time(timestamp); } z = xmlnode_get_nextsibling(z); } if (!type || !g_strcasecmp(type, "normal") || !g_strcasecmp(type, "chat")) { /* XXX namespaces could be handled better. (mid) */ if ((xmlns = xmlnode_get_tag(p->x, "x"))) type = xmlnode_get_attrib(xmlns, "xmlns"); from = jid_full(p->from); /* if ((y = xmlnode_get_tag(p->x, "html"))) { msg = xmlnode_get_data(y); } else */ if ((y = xmlnode_get_tag(p->x, "body"))) { msg = xmlnode_get_data(y); } if (!from) return; if (type && !g_strcasecmp(type, "jabber:x:conference")) { /* do nothing */ } else if (msg) { /* whisper */ struct jabber_chat *jc; g_snprintf(m, sizeof(m), "%s", msg); if (((jc = find_existing_chat(GJ_GC(gjc), p->from)) != NULL) && jc->b) serv_got_chat_in(GJ_GC(gjc), jc->b->id, p->from->resource, 1, m, time_sent); else { int flags = 0; if(p->from->user) { from = g_strdup_printf("%s@%s", p->from->user, p->from->server); } else { /* server message? */ from = g_strdup(p->from->server); } serv_got_im(GJ_GC(gjc), from, m, flags, time_sent, -1); g_free(from); } } } else if (!g_strcasecmp(type, "error")) { if ((y = xmlnode_get_tag(p->x, "error"))) { type = xmlnode_get_attrib(y, "code"); msg = xmlnode_get_data(y); } if (msg) { from = g_strdup_printf("Error %s", type ? type : ""); do_error_dialog(GJ_GC(gjc), msg, from); g_free(from); } } else if (!g_strcasecmp(type, "headline")) { char *subject, *body, *url; y = xmlnode_get_tag( p->x, "body" ); body = y ? g_strdup( xmlnode_get_data( y ) ) : NULL; y = xmlnode_get_tag( p->x, "subject" ); subject = y ? g_strdup( xmlnode_get_data( y ) ) : NULL; url = NULL; z = xmlnode_get_firstchild(p->x); while( z ) { char *xtype = xmlnode_get_attrib( z, "xmlns" ); if( xtype && g_strcasecmp( xtype, "jabber:x:oob" ) == 0 && ( y = xmlnode_get_tag( z, "url" ) ) ) { url = g_strdup( xmlnode_get_data( y ) ); break; } z = xmlnode_get_nextsibling( z ); } g_snprintf( m, BUF_LONG, "Subject: %s\nURL: %s\nMessage:\n%s", subject ? subject : "(none)", url ? url : "(none)", body ? body : "(none)" ); if( p->from->user ) from = g_strdup_printf( "%s@%s", p->from->user, p->from->server ); else from = g_strdup( p->from->server ); serv_got_im( GJ_GC(gjc), from, m, 0, time_sent, -1 ); g_free( from ); g_free( subject ); g_free( body ); g_free( url ); } } static void jabber_handlepresence(gjconn gjc, jpacket p) { char *to, *from, *type; struct buddy *b = NULL; jid who; char *buddy; xmlnode y; char *show; int state = 0; GSList *resources; char *res; struct conversation *cnv = NULL; struct jabber_chat *jc = NULL; to = xmlnode_get_attrib(p->x, "to"); from = xmlnode_get_attrib(p->x, "from"); type = xmlnode_get_attrib(p->x, "type"); if (type && g_strcasecmp(type, "error") == 0) { return; } else if ((y = xmlnode_get_tag(p->x, "show"))) { show = xmlnode_get_data(y); if (!show) { state = 0; } else if (!g_strcasecmp(show, "away")) { state = UC_AWAY; } else if (!g_strcasecmp(show, "chat")) { state = UC_CHAT; } else if (!g_strcasecmp(show, "xa")) { state = UC_XA; } else if (!g_strcasecmp(show, "dnd")) { state = UC_DND; } } else { state = 0; } who = jid_new(gjc->p, from); if (who->user == NULL) { /* FIXME: transport */ return; } buddy = g_strdup_printf("%s@%s", who->user, who->server); /* um. we're going to check if it's a chat. if it isn't, and there are pending * chats, create the chat. if there aren't pending chats and we don't have the * buddy on our list, simply bail out. */ if ((cnv = NULL) == NULL) { static int i = 0x70; if ((jc = find_pending_chat(GJ_GC(gjc), who)) != NULL) { jc->b = cnv = serv_got_joined_chat(GJ_GC(gjc), i++, who->user); jc->id = jc->b->id; jc->state = JCS_ACTIVE; } else if ((b = find_buddy(GJ_GC(gjc), buddy)) == NULL) { g_free(buddy); return; } } if (!cnv) { resources = b->proto_data; res = who->resource; if (res) while (resources) { if (!strcmp(res, resources->data)) break; resources = resources->next; } /* keep track of away msg same as yahoo plugin */ jabber_track_away(gjc, p, normalize(b->name), type); if (type && (g_strcasecmp(type, "unavailable") == 0)) { if (resources) { g_free(resources->data); b->proto_data = g_slist_remove(b->proto_data, resources->data); } if (!b->proto_data) { serv_got_update(GJ_GC(gjc), buddy, 0, 0, 0, 0, 0, 0); } } else { if (!resources) { b->proto_data = g_slist_append(b->proto_data, g_strdup(res)); } serv_got_update(GJ_GC(gjc), buddy, 1, 0, b->signon, b->idle, state, 0); } } else { if (who->resource) { char *buf; buf = g_strdup_printf("%s@%s/%s", who->user, who->server, who->resource); jabber_track_away(gjc, p, buf, type); g_free(buf); if (type && !g_strcasecmp(type, "unavailable")) { struct jabber_data *jd; if (!jc && !(jc = find_existing_chat(GJ_GC(gjc), who))) { g_free(buddy); return; } jd = jc->gc->proto_data; /* if it's not ourselves...*/ if (strcmp(who->resource, jc->Jid->resource) && jc->b) { remove_chat_buddy(jc->b, who->resource, NULL); g_free(buddy); return; } jc->state = JCS_CLOSED; serv_got_chat_left(GJ_GC(gjc), jc->id); /* * TBD: put back some day? jd->chats = g_slist_remove(jd->chats, jc); g_free(jc); */ } else { if ((!jc && !(jc = find_existing_chat(GJ_GC(gjc), who))) || !jc->b) { g_free(buddy); return; } if (!find_chat_buddy(jc->b, who->resource)) { add_chat_buddy(jc->b, who->resource); } } } } g_free(buddy); return; } /* * Used only by Jabber accept/deny add stuff just below */ struct jabber_add_permit { gjconn gjc; gchar *user; }; /* * Common part for Jabber accept/deny adds * * "type" says whether we'll permit/deny the subscribe request */ static void jabber_accept_deny_add(struct jabber_add_permit *jap, const char *type) { xmlnode g = xmlnode_new_tag("presence"); xmlnode_put_attrib(g, "to", jap->user); xmlnode_put_attrib(g, "type", type); gjab_send(jap->gjc, g); xmlnode_free(g); } /* * Callback from "accept" in do_ask_dialog() invoked by jabber_handles10n() */ static void jabber_accept_add(gpointer w, struct jabber_add_permit *jap) { jabber_accept_deny_add(jap, "subscribed"); /* * If we don't already have the buddy on *our* buddylist, * ask if we want him or her added. */ if(find_buddy(GJ_GC(jap->gjc), jap->user) == NULL) { show_got_added(GJ_GC(jap->gjc), jap->user, NULL); } g_free(jap->user); g_free(jap); } /* * Callback from "deny/cancel" in do_ask_dialog() invoked by jabber_handles10n() */ static void jabber_deny_add(gpointer w, struct jabber_add_permit *jap) { jabber_accept_deny_add(jap, "unsubscribed"); g_free(jap->user); g_free(jap); } /* * Handle subscription requests */ static void jabber_handles10n(gjconn gjc, jpacket p) { xmlnode g; char *Jid = xmlnode_get_attrib(p->x, "from"); char *type = xmlnode_get_attrib(p->x, "type"); g = xmlnode_new_tag("presence"); xmlnode_put_attrib(g, "to", Jid); if (!strcmp(type, "subscribe")) { /* * A "subscribe to us" request was received - put up the approval dialog */ struct jabber_add_permit *jap = g_new0(struct jabber_add_permit, 1); gchar *msg = g_strdup_printf(_("The user %s wants to add you to his/her buddy list."), Jid); jap->gjc = gjc; jap->user = g_strdup(Jid); do_ask_dialog(GJ_GC(gjc), msg, jap, jabber_accept_add, jabber_deny_add); g_free(msg); xmlnode_free(g); /* Never needed it here anyway */ return; } else if (!strcmp(type, "unsubscribe")) { /* * An "unsubscribe to us" was received - simply "approve" it */ xmlnode_put_attrib(g, "type", "unsubscribed"); } else { /* * Did we attempt to subscribe to somebody and they do not exist? */ if (!strcmp(type, "unsubscribed")) { xmlnode y; char *status; if((y = xmlnode_get_tag(p->x, "status")) && (status = xmlnode_get_data(y)) && !strcmp(status, "Not Found")) { char *msg = g_strdup_printf("%s: \"%s\"", _("No such user"), xmlnode_get_attrib(p->x, "from")); do_error_dialog(GJ_GC(gjc), msg, _("Jabber Error")); g_free(msg); } } xmlnode_free(g); return; } gjab_send(gjc, g); xmlnode_free(g); } /* * Pending subscription to a buddy? */ #define BUD_SUB_TO_PEND(sub, ask) ((!g_strcasecmp((sub), "none") || !g_strcasecmp((sub), "from")) && \ (ask) != NULL && !g_strcasecmp((ask), "subscribe")) /* * Subscribed to a buddy? */ #define BUD_SUBD_TO(sub, ask) ((!g_strcasecmp((sub), "to") || !g_strcasecmp((sub), "both")) && \ ((ask) == NULL || !g_strcasecmp((ask), "subscribe"))) /* * Pending unsubscription to a buddy? */ #define BUD_USUB_TO_PEND(sub, ask) ((!g_strcasecmp((sub), "to") || !g_strcasecmp((sub), "both")) && \ (ask) != NULL && !g_strcasecmp((ask), "unsubscribe")) /* * Unsubscribed to a buddy? */ #define BUD_USUBD_TO(sub, ask) ((!g_strcasecmp((sub), "none") || !g_strcasecmp((sub), "from")) && \ ((ask) == NULL || !g_strcasecmp((ask), "unsubscribe"))) /* * If a buddy is added or removed from the roster on another resource * jabber_handlebuddy is called * * Called with roster item node. */ static void jabber_handlebuddy(gjconn gjc, xmlnode x) { xmlnode g; char *Jid, *name, *sub, *ask; jid who; struct buddy *b = NULL; char *buddyname, *groupname = NULL; Jid = xmlnode_get_attrib(x, "jid"); name = xmlnode_get_attrib(x, "name"); sub = xmlnode_get_attrib(x, "subscription"); ask = xmlnode_get_attrib(x, "ask"); who = jid_new(gjc->p, Jid); /* JFIXME: jabber_handleroster() had a "FIXME: transport" at this * equivilent point. So... * * We haven't allocated any memory or done anything interesting to * this point, so we'll violate Good Coding Structure here by * simply bailing out. */ if (!who || !who->user) { return; } buddyname = g_strdup_printf("%s@%s", who->user, who->server); if((g = xmlnode_get_tag(x, "group")) != NULL) { groupname = xmlnode_get_data(g); } /* * Add or remove a buddy? Change buddy's alias or group? */ if (BUD_SUB_TO_PEND(sub, ask) || BUD_SUBD_TO(sub, ask)) { if ((b = find_buddy(GJ_GC(gjc), buddyname)) == NULL) { add_buddy(GJ_GC(gjc), groupname ? groupname : _("Buddies"), buddyname, name ? name : buddyname); } else { /* struct group *c_grp = find_group_by_buddy(GJ_GC(gjc), buddyname); */ /* * If the buddy's in a new group or his/her alias is changed... */ if(groupname) { int present = b->present; /* save presence state */ int uc = b->uc; /* and away state (?) */ int idle = b->idle; int signon = b->signon; /* * seems rude, but it seems to be the only way... */ /* remove_buddy(GJ_GC(gjc), c_grp, b); */ jabber_remove_buddy(GJ_GC(gjc), b->name, JABBER_GROUP); add_buddy(GJ_GC(gjc), groupname, buddyname, name ? name : buddyname); if(present) { serv_got_update(GJ_GC(gjc), buddyname, 1, 0, signon, idle, uc, 0); } } else if(name != NULL && strcmp(b->show, name)) { strncpy(b->show, name, BUDDY_ALIAS_MAXLEN); b->show[BUDDY_ALIAS_MAXLEN - 1] = '\0'; /* cheap safety feature */ serv_buddy_rename(GJ_GC(gjc), buddyname, b->show); } } } else if (BUD_USUB_TO_PEND(sub, ask) || BUD_USUBD_TO(sub, ask) || !g_strcasecmp(sub, "remove")) { jabber_remove_gaim_buddy(GJ_GC(gjc), buddyname); } g_free(buddyname); } static void jabber_handleroster(gjconn gjc, xmlnode querynode) { xmlnode x; x = xmlnode_get_firstchild(querynode); while (x) { jabber_handlebuddy(gjc, x); x = xmlnode_get_nextsibling(x); } account_online(GJ_GC(gjc)); } static void jabber_handleauthresp(gjconn gjc, jpacket p) { if (jpacket_subtype(p) == JPACKET__RESULT) { if (xmlnode_has_children(p->x)) { xmlnode query = xmlnode_get_tag(p->x, "query"); set_login_progress(GJ_GC(gjc), 4, _("Authenticating")); if (!xmlnode_get_tag(query, "digest")) { g_free(gjc->sid); gjc->sid = NULL; } gjab_auth(gjc); } else { gjab_reqroster(gjc); ((struct jabber_data *)GJ_GC(gjc)->proto_data)->did_import = TRUE; } } else { xmlnode xerr; char *errmsg = NULL; int errcode = 0; struct jabber_data *jd = GJ_GC(gjc)->proto_data; xerr = xmlnode_get_tag(p->x, "error"); if (xerr) { char msg[BUF_LONG]; errmsg = xmlnode_get_data(xerr); if (xmlnode_get_attrib(xerr, "code")) { errcode = atoi(xmlnode_get_attrib(xerr, "code")); g_snprintf(msg, sizeof(msg), "Error %d: %s", errcode, errmsg ? errmsg : "Unknown error"); } else g_snprintf(msg, sizeof(msg), "%s", errmsg); hide_login_progress(GJ_GC(gjc), msg); } else { hide_login_progress(GJ_GC(gjc), _("Unknown login error")); } jd->die = TRUE; } } static void jabber_handleversion(gjconn gjc, xmlnode iqnode) { xmlnode querynode, x; char *id, *from; char os[1024]; #ifndef _WIN32 struct utsname osinfo; uname(&osinfo); g_snprintf(os, sizeof os, "%s %s %s", osinfo.sysname, osinfo.release, osinfo.machine); #else g_snprintf(os, sizeof os, "Windows %d %d", _winmajor, _winminor); #endif id = xmlnode_get_attrib(iqnode, "id"); from = xmlnode_get_attrib(iqnode, "from"); x = jutil_iqnew(JPACKET__RESULT, NS_VERSION); xmlnode_put_attrib(x, "to", from); xmlnode_put_attrib(x, "id", id); querynode = xmlnode_get_tag(x, "query"); xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "name"), PACKAGE, -1); xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "version"), BITLBEE_VERSION, -1); xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "os"), os, -1); gjab_send(gjc, x); xmlnode_free(x); } static void jabber_handletime(gjconn gjc, xmlnode iqnode) { xmlnode querynode, x; char *id, *from; time_t now_t; struct tm *now; char buf[1024]; time(&now_t); now = localtime(&now_t); id = xmlnode_get_attrib(iqnode, "id"); from = xmlnode_get_attrib(iqnode, "from"); x = jutil_iqnew(JPACKET__RESULT, NS_TIME); xmlnode_put_attrib(x, "to", from); xmlnode_put_attrib(x, "id", id); querynode = xmlnode_get_tag(x, "query"); strftime(buf, 1024, "%Y%m%dT%T", now); xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "utc"), buf, -1); strftime(buf, 1024, "%Z", now); xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "tz"), buf, -1); strftime(buf, 1024, "%d %b %Y %T", now); xmlnode_insert_cdata(xmlnode_insert_tag(querynode, "display"), buf, -1); gjab_send(gjc, x); xmlnode_free(x); } static void jabber_handlelast(gjconn gjc, xmlnode iqnode) { xmlnode x, querytag; char *id, *from; struct jabber_data *jd = GJ_GC(gjc)->proto_data; char idle_time[32]; id = xmlnode_get_attrib(iqnode, "id"); from = xmlnode_get_attrib(iqnode, "from"); x = jutil_iqnew(JPACKET__RESULT, "jabber:iq:last"); xmlnode_put_attrib(x, "to", from); xmlnode_put_attrib(x, "id", id); querytag = xmlnode_get_tag(x, "query"); g_snprintf(idle_time, sizeof idle_time, "%ld", jd->idle ? time(NULL) - jd->idle : 0); xmlnode_put_attrib(querytag, "seconds", idle_time); gjab_send(gjc, x); xmlnode_free(x); } /* * delete == TRUE: delete found entry * * returns pointer to (local) copy of value if found, NULL otherwise * * Note: non-reentrant! Local static storage re-used on subsequent calls. * If you're going to need to keep the returned value, make a copy! */ static gchar *jabber_track_queries(GHashTable *queries, gchar *key, gboolean delete) { gpointer my_key, my_val; static gchar *ret_val = NULL; if(ret_val != NULL) { g_free(ret_val); ret_val = NULL; } /* self-protection */ if(queries != NULL && key != NULL) { if(g_hash_table_lookup_extended(queries, key, &my_key, &my_val)) { ret_val = g_strdup((gchar *) my_val); if(delete) { g_hash_table_remove(queries, key); g_free(my_key); g_free(my_val); } } } return(ret_val); } static void jabber_handlepacket(gjconn gjc, jpacket p) { char *id; switch (p->type) { case JPACKET_MESSAGE: jabber_handlemessage(gjc, p); break; case JPACKET_PRESENCE: jabber_handlepresence(gjc, p); break; case JPACKET_IQ: id = xmlnode_get_attrib(p->x, "id"); if (id != NULL && !strcmp(id, IQID_AUTH)) { jabber_handleauthresp(gjc, p); break; } if (jpacket_subtype(p) == JPACKET__SET) { xmlnode querynode; querynode = xmlnode_get_tag(p->x, "query"); if (NSCHECK(querynode, "jabber:iq:roster")) { jabber_handlebuddy(gjc, xmlnode_get_firstchild(querynode)); } } else if (jpacket_subtype(p) == JPACKET__GET) { xmlnode querynode; querynode = xmlnode_get_tag(p->x, "query"); if (NSCHECK(querynode, NS_VERSION)) { jabber_handleversion(gjc, p->x); } else if (NSCHECK(querynode, NS_TIME)) { jabber_handletime(gjc, p->x); } else if (NSCHECK(querynode, "jabber:iq:last")) { jabber_handlelast(gjc, p->x); } } else if (jpacket_subtype(p) == JPACKET__RESULT) { xmlnode querynode, vcard; /* char *xmlns; */ char *from; /* * TBD: ISTM maybe this part could use a serious re-work? */ from = xmlnode_get_attrib(p->x, "from"); querynode = xmlnode_get_tag(p->x, "query"); vcard = xmlnode_get_tag(p->x, "vCard"); if (!vcard) vcard = xmlnode_get_tag(p->x, "VCARD"); if (NSCHECK(querynode, NS_ROSTER)) { jabber_handleroster(gjc, querynode); } else if (NSCHECK(querynode, NS_VCARD)) { jabber_track_queries(gjc->queries, id, TRUE); /* delete query track */ jabber_handlevcard(gjc, querynode, from); } else if (vcard) { jabber_track_queries(gjc->queries, id, TRUE); /* delete query track */ jabber_handlevcard(gjc, vcard, from); } else { char *val; /* handle "null" query results */ if((val = jabber_track_queries(gjc->queries, id, TRUE)) != NULL) { if (!g_strncasecmp(val, "vcard", 5)) { jabber_handlevcard(gjc, NULL, from); } /* No-op */ } } } else if (jpacket_subtype(p) == JPACKET__ERROR) { xmlnode xerr; char *from, *errmsg = NULL; int errcode = 0; from = xmlnode_get_attrib(p->x, "from"); xerr = xmlnode_get_tag(p->x, "error"); if (xerr) { errmsg = xmlnode_get_data(xerr); if (xmlnode_get_attrib(xerr, "code")) errcode = atoi(xmlnode_get_attrib(xerr, "code")); } from = g_strdup_printf("Error %d (%s)", errcode, from); do_error_dialog(GJ_GC(gjc), errmsg, from); g_free(from); } break; case JPACKET_S10N: jabber_handles10n(gjc, p); break; } xmlnode_free(p->x); return; } static void jabber_handlestate(gjconn gjc, int state) { switch (state) { case JCONN_STATE_OFF: if(gjc->was_connected) { hide_login_progress_error(GJ_GC(gjc), _("Connection lost")); } else { hide_login_progress(GJ_GC(gjc), _("Unable to connect")); } signoff(GJ_GC(gjc)); break; case JCONN_STATE_CONNECTED: gjc->was_connected = 1; set_login_progress(GJ_GC(gjc), 2, _("Connected")); break; case JCONN_STATE_ON: set_login_progress(GJ_GC(gjc), 3, _("Requesting Authentication Method")); gjab_reqauth(gjc); break; } return; } static void jabber_acc_init(account_t *acc) { set_t *s; s = set_add( &acc->set, "port", "5222", set_eval_int, acc ); s->flags |= ACC_SET_OFFLINE_ONLY; s = set_add( &acc->set, "resource", "BitlBee", NULL, acc ); s->flags |= ACC_SET_OFFLINE_ONLY; s = set_add( &acc->set, "server", NULL, set_eval_account, acc ); s->flags |= ACC_SET_NOSAVE | ACC_SET_OFFLINE_ONLY; s = set_add( &acc->set, "ssl", "false", set_eval_bool, acc ); s->flags |= ACC_SET_OFFLINE_ONLY; } static void jabber_login(account_t *acc) { struct gaim_connection *gc; struct jabber_data *jd; char *resource, *loginname; /* Time to move some data/things from the old syntax to the new one: */ if (acc->server) { char *s, *tmp_server; int port; if (g_strcasecmp(acc->server, "ssl") == 0) { set_setstr(&acc->set, "server", ""); set_setint(&acc->set, "port", DEFAULT_PORT_SSL); set_setstr(&acc->set, "ssl", "true"); g_free(acc->server); acc->server = NULL; } else if ((s = strchr(acc->server, ':'))) { if (strstr(acc->server, ":ssl")) { set_setint(&acc->set, "port", DEFAULT_PORT_SSL); set_setstr(&acc->set, "ssl", "true"); } if (isdigit(s[1])) { if (sscanf(s + 1, "%d", &port) == 1) set_setint(&acc->set, "port", port); } tmp_server = g_strndup(acc->server, s - acc->server); set_setstr(&acc->set, "server", tmp_server); g_free(tmp_server); } } gc = new_gaim_conn(acc); jd = gc->proto_data = g_new0(struct jabber_data, 1); if( strchr( acc->user, '@' ) == NULL ) { hide_login_progress( gc, "Invalid account name" ); signoff( gc ); return; } resource = set_getstr(&acc->set, "resource"); loginname = create_valid_jid(acc->user, DEFAULT_SERVER, resource); jd->hash = g_hash_table_new(g_str_hash, g_str_equal); jd->chats = NULL; /* we have no chats yet */ set_login_progress(gc, 1, _("Connecting")); if (!(jd->gjc = gjab_new(loginname, acc->pass, gc))) { g_free(loginname); hide_login_progress(gc, _("Unable to connect")); signoff(gc); return; } g_free(loginname); gjab_state_handler(jd->gjc, jabber_handlestate); gjab_packet_handler(jd->gjc, jabber_handlepacket); jd->gjc->queries = g_hash_table_new(g_str_hash, g_str_equal); gjab_start(jd->gjc); } static gboolean jabber_destroy_hash(gpointer key, gpointer val, gpointer data) { g_free(key); g_free(val); return TRUE; } static gboolean jabber_free(gpointer data, gint fd, b_input_condition cond) { struct jabber_data *jd = data; if(jd->gjc != NULL) { gjab_delete(jd->gjc); /* YAY for modules with their own memory pool managers!... g_free(jd->gjc->sid); And a less sarcastic yay for valgrind. :-) */ jd->gjc = NULL; } g_free(jd); return FALSE; } static void jabber_close(struct gaim_connection *gc) { struct jabber_data *jd = gc->proto_data; if(jd) { GSList *jcs = jd->chats; /* Free-up the jabber_chat struct allocs and the list */ while (jcs) { g_free(jcs->data); jcs = jcs->next; } g_slist_free(jd->chats); /* Free-up the away status memories and the list */ if(jd->hash != NULL) { g_hash_table_foreach_remove(jd->hash, jabber_destroy_hash, NULL); g_hash_table_destroy(jd->hash); jd->hash = NULL; } /* Free-up the pending queries memories and the list */ if(jd->gjc != NULL && jd->gjc->queries != NULL) { g_hash_table_foreach_remove(jd->gjc->queries, jabber_destroy_hash, NULL); g_hash_table_destroy(jd->gjc->queries); jd->gjc->queries = NULL; } } if (gc->inpa) b_event_remove(gc->inpa); if(jd) { b_timeout_add(50, jabber_free, jd); if(jd->gjc != NULL) xmlnode_free(jd->gjc->current); } gc->proto_data = NULL; } static int jabber_send_im(struct gaim_connection *gc, char *who, char *message, int len, int flags) { xmlnode x, y; char *realwho; gjconn gjc = ((struct jabber_data *)gc->proto_data)->gjc; if (!who || !message) return 0; x = xmlnode_new_tag("message"); /* Bare username and "username" not the server itself? */ if (!strchr(who, '@') && strcmp(who, gjc->user->server) != 0) realwho = g_strdup_printf("%s@%s", who, gjc->user->server); else realwho = g_strdup(who); xmlnode_put_attrib(x, "to", realwho); g_free(realwho); xmlnode_insert_tag(x, "bitlbee"); xmlnode_put_attrib(x, "type", "chat"); if (message && strlen(message)) { y = xmlnode_insert_tag(x, "body"); xmlnode_insert_cdata(y, message, -1); } gjab_send(((struct jabber_data *)gc->proto_data)->gjc, x); xmlnode_free(x); return 1; } /* * Add/update buddy's roster entry on server */ static void jabber_roster_update(struct gaim_connection *gc, char *name) { xmlnode x, y; char *realwho; gjconn gjc; struct buddy *buddy = NULL; /* struct group *buddy_group = NULL; */ if(gc && gc->proto_data && ((struct jabber_data *)gc->proto_data)->gjc && name) { gjc = ((struct jabber_data *)gc->proto_data)->gjc; if (!strchr(name, '@')) realwho = g_strdup_printf("%s@%s", name, gjc->user->server); else { jid who = jid_new(gjc->p, name); if (who->user == NULL) { /* FIXME: transport */ return; } realwho = g_strdup_printf("%s@%s", who->user, who->server); } x = jutil_iqnew(JPACKET__SET, NS_ROSTER); y = xmlnode_insert_tag(xmlnode_get_tag(x, "query"), "item"); xmlnode_put_attrib(y, "jid", realwho); /* If we can find the buddy, there's an alias for him, it's not 0-length * and it doesn't match his JID, add the "name" attribute. */ if((buddy = find_buddy(gc, realwho)) != NULL && buddy->show != NULL && buddy->show[0] != '\0' && strcmp(realwho, buddy->show)) { xmlnode_put_attrib(y, "name", buddy->show); } /* * Find out what group the buddy's in and send that along * with the roster item. */ /* ** Bitlbee disabled ** if((buddy_group = NULL) != NULL) { xmlnode z; z = xmlnode_insert_tag(y, "group"); xmlnode_insert_cdata(z, buddy_group->name, -1); } ** End - Bitlbee ** */ gjab_send(((struct jabber_data *)gc->proto_data)->gjc, x); xmlnode_free(x); g_free(realwho); } } /* * Change buddy's group on server roster */ static void jabber_group_change(struct gaim_connection *gc, char *name, char *old_group, char *new_group) { if(strcmp(old_group, new_group)) { jabber_roster_update(gc, name); } } static void jabber_add_buddy(struct gaim_connection *gc, char *name) { xmlnode x; char *realwho; gjconn gjc = ((struct jabber_data *)gc->proto_data)->gjc; if (!((struct jabber_data *)gc->proto_data)->did_import) return; if (!name) return; if (!strcmp(gc->username, name)) return; if (!strchr(name, '@')) realwho = g_strdup_printf("%s@%s", name, gjc->user->server); else { jid who; if((who = jid_new(gjc->p, name)) == NULL) { char *msg = g_strdup_printf("%s: \"%s\"", _("Invalid Jabber I.D."), name); do_error_dialog(GJ_GC(gjc), msg, _("Jabber Error")); g_free(msg); jabber_remove_gaim_buddy(gc, name); return; } if (who->user == NULL) { /* FIXME: transport */ return; } realwho = g_strdup_printf("%s@%s", who->user, who->server); } x = xmlnode_new_tag("presence"); xmlnode_put_attrib(x, "to", realwho); xmlnode_put_attrib(x, "type", "subscribe"); gjab_send(((struct jabber_data *)gc->proto_data)->gjc, x); xmlnode_free(x); jabber_roster_update(gc, realwho); g_free(realwho); } static void jabber_remove_buddy(struct gaim_connection *gc, char *name, char *group) { xmlnode x; char *realwho; gjconn gjc = ((struct jabber_data *)gc->proto_data)->gjc; if (!name) return; if (!strchr(name, '@')) realwho = g_strdup_printf("%s@%s", name, gjc->user->server); else realwho = g_strdup(name); x = xmlnode_new_tag("presence"); xmlnode_put_attrib(x, "to", realwho); xmlnode_put_attrib(x, "type", "unsubscribe"); gjab_send(((struct jabber_data *)gc->proto_data)->gjc, x); g_free(realwho); xmlnode_free(x); } static void jabber_get_info(struct gaim_connection *gc, char *who) { xmlnode x; char *id; char *realwho; struct jabber_data *jd = gc->proto_data; gjconn gjc = jd->gjc; x = jutil_iqnew(JPACKET__GET, NS_VCARD); /* Bare username? */ if (!strchr(who, '@')) { realwho = g_strdup_printf("%s@%s", who, gjc->user->server); } else { realwho = g_strdup(who); } xmlnode_put_attrib(x, "to", realwho); g_free(realwho); id = gjab_getid(gjc); xmlnode_put_attrib(x, "id", id); g_hash_table_insert(jd->gjc->queries, g_strdup(id), g_strdup("vCard")); gjab_send(gjc, x); xmlnode_free(x); } static void jabber_get_away_msg(struct gaim_connection *gc, char *who) { struct jabber_data *jd = gc->proto_data; gjconn gjc = jd->gjc; char *status; /* space for all elements: Jabber I.D. + "status" + NULL (list terminator) */ gchar **str_arr = (gchar **) g_new(gpointer, 3); gchar **ap = str_arr; gchar *realwho, *final; /* Bare username? */ if (!strchr(who, '@')) { realwho = g_strdup_printf("%s@%s", who, gjc->user->server); } else { realwho = g_strdup(who); } *ap++ = g_strdup_printf("<B>Jabber ID:</B> %s<BR>\n", realwho); if((status = g_hash_table_lookup(jd->hash, realwho)) == NULL) { status = _("Unknown"); } *ap++ = g_strdup_printf("<B>Status:</B> %s<BR>\n", status); *ap = NULL; final= g_strjoinv(NULL, str_arr); g_strfreev(str_arr); g_free(realwho); g_free(final); } static GList *jabber_away_states(struct gaim_connection *gc) { GList *m = NULL; m = g_list_append(m, "Online"); m = g_list_append(m, "Chatty"); m = g_list_append(m, "Away"); m = g_list_append(m, "Extended Away"); m = g_list_append(m, "Do Not Disturb"); return m; } static void jabber_set_away(struct gaim_connection *gc, char *state, char *message) { xmlnode x, y; struct jabber_data *jd = gc->proto_data; gjconn gjc = jd->gjc; gc->away = NULL; /* never send an auto-response */ x = xmlnode_new_tag("presence"); if (!strcmp(state, GAIM_AWAY_CUSTOM)) { /* oh goody. Gaim is telling us what to do. */ if (message) { /* Gaim wants us to be away */ y = xmlnode_insert_tag(x, "show"); xmlnode_insert_cdata(y, "away", -1); y = xmlnode_insert_tag(x, "status"); xmlnode_insert_cdata(y, message, -1); gc->away = ""; } else { /* Gaim wants us to not be away */ /* but for Jabber, we can just send presence with no other information. */ } } else { /* state is one of our own strings. it won't be NULL. */ if (!g_strcasecmp(state, "Online")) { /* once again, we don't have to put anything here */ } else if (!g_strcasecmp(state, "Chatty")) { y = xmlnode_insert_tag(x, "show"); xmlnode_insert_cdata(y, "chat", -1); } else if (!g_strcasecmp(state, "Away")) { y = xmlnode_insert_tag(x, "show"); xmlnode_insert_cdata(y, "away", -1); gc->away = ""; } else if (!g_strcasecmp(state, "Extended Away")) { y = xmlnode_insert_tag(x, "show"); xmlnode_insert_cdata(y, "xa", -1); gc->away = ""; } else if (!g_strcasecmp(state, "Do Not Disturb")) { y = xmlnode_insert_tag(x, "show"); xmlnode_insert_cdata(y, "dnd", -1); gc->away = ""; } } gjab_send(gjc, x); xmlnode_free(x); } static void jabber_keepalive(struct gaim_connection *gc) { struct jabber_data *jd = (struct jabber_data *)gc->proto_data; gjab_send_raw(jd->gjc, " \t "); } /*---------------------------------------*/ /* Jabber "set info" (vCard) support */ /*---------------------------------------*/ /* * V-Card format: * * <vCard prodid='' version='' xmlns=''> * <FN></FN> * <N> * <FAMILY/> * <GIVEN/> * </N> * <NICKNAME/> * <URL/> * <ADR> * <STREET/> * <EXTADD/> * <LOCALITY/> * <REGION/> * <PCODE/> * <COUNTRY/> * </ADR> * <TEL/> * <EMAIL/> * <ORG> * <ORGNAME/> * <ORGUNIT/> * </ORG> * <TITLE/> * <ROLE/> * <DESC/> * <BDAY/> * </vCard> * * See also: * * http://docs.jabber.org/proto/html/vcard-temp.html * http://www.vcard-xml.org/dtd/vCard-XML-v2-20010520.dtd */ /* * Cross-reference user-friendly V-Card entry labels to vCard XML tags * and attributes. * * Order is (or should be) unimportant. For example: we have no way of * knowing in what order real data will arrive. * * Format: Label, Pre-set text, "visible" flag, "editable" flag, XML tag * name, XML tag's parent tag "path" (relative to vCard node). * * List is terminated by a NULL label pointer. * * Entries with no label text, but with XML tag and parent tag * entries, are used by V-Card XML construction routines to * "automagically" construct the appropriate XML node tree. * * Thoughts on future direction/expansion * * This is a "simple" vCard. * * It is possible for nodes other than the "vCard" node to have * attributes. Should that prove necessary/desirable, add an * "attributes" pointer to the vcard_template struct, create the * necessary tag_attr structs, and add 'em to the vcard_dflt_data * array. * * The above changes will (obviously) require changes to the vCard * construction routines. */ static struct vcard_template { char *label; /* label text pointer */ char *text; /* entry text pointer */ int visible; /* should entry field be "visible?" */ int editable; /* should entry field be editable? */ char *tag; /* tag text */ char *ptag; /* parent tag "path" text */ char *url; /* vCard display format if URL */ } vcard_template_data[] = { {N_("Full Name"), NULL, TRUE, TRUE, "FN", NULL, NULL}, {N_("Family Name"), NULL, TRUE, TRUE, "FAMILY", "N", NULL}, {N_("Given Name"), NULL, TRUE, TRUE, "GIVEN", "N", NULL}, {N_("Nickname"), NULL, TRUE, TRUE, "NICKNAME", NULL, NULL}, {N_("URL"), NULL, TRUE, TRUE, "URL", NULL, "<A HREF=\"%s\">%s</A>"}, {N_("Street Address"), NULL, TRUE, TRUE, "STREET", "ADR", NULL}, {N_("Extended Address"), NULL, TRUE, TRUE, "EXTADD", "ADR", NULL}, {N_("Locality"), NULL, TRUE, TRUE, "LOCALITY", "ADR", NULL}, {N_("Region"), NULL, TRUE, TRUE, "REGION", "ADR", NULL}, {N_("Postal Code"), NULL, TRUE, TRUE, "PCODE", "ADR", NULL}, {N_("Country"), NULL, TRUE, TRUE, "COUNTRY", "ADR", NULL}, {N_("Telephone"), NULL, TRUE, TRUE, "TELEPHONE", NULL, NULL}, {N_("Email"), NULL, TRUE, TRUE, "EMAIL", NULL, "<A HREF=\"mailto:%s\">%s</A>"}, {N_("Organization Name"), NULL, TRUE, TRUE, "ORGNAME", "ORG", NULL}, {N_("Organization Unit"), NULL, TRUE, TRUE, "ORGUNIT", "ORG", NULL}, {N_("Title"), NULL, TRUE, TRUE, "TITLE", NULL, NULL}, {N_("Role"), NULL, TRUE, TRUE, "ROLE", NULL, NULL}, {N_("Birthday"), NULL, TRUE, TRUE, "BDAY", NULL, NULL}, {N_("Description"), NULL, TRUE, TRUE, "DESC", NULL, NULL}, {"", NULL, TRUE, TRUE, "N", NULL, NULL}, {"", NULL, TRUE, TRUE, "ADR", NULL, NULL}, {"", NULL, TRUE, TRUE, "ORG", NULL, NULL}, {NULL, NULL, 0, 0, NULL, NULL, NULL} }; /* * Used by routines to parse an XML-encoded string into an xmlnode tree */ typedef struct { XML_Parser parser; xmlnode current; } *xmlstr2xmlnode_parser, xmlstr2xmlnode_parser_struct; /* * Used by XML_Parse on parsing CDATA */ static void xmlstr2xmlnode_charData(void *userdata, const char *s, int slen) { xmlstr2xmlnode_parser xmlp = (xmlstr2xmlnode_parser) userdata; if (xmlp->current) xmlnode_insert_cdata(xmlp->current, s, slen); } /* * Used by XML_Parse to start or append to an xmlnode */ static void xmlstr2xmlnode_startElement(void *userdata, const char *name, const char **attribs) { xmlnode x; xmlstr2xmlnode_parser xmlp = (xmlstr2xmlnode_parser) userdata; if (xmlp->current) { /* Append the node to the current one */ x = xmlnode_insert_tag(xmlp->current, name); xmlnode_put_expat_attribs(x, attribs); xmlp->current = x; } else { x = xmlnode_new_tag(name); xmlnode_put_expat_attribs(x, attribs); xmlp->current = x; } } /* * Used by XML_Parse to end an xmlnode */ static void xmlstr2xmlnode_endElement(void *userdata, const char *name) { xmlstr2xmlnode_parser xmlp = (xmlstr2xmlnode_parser) userdata; xmlnode x; if (xmlp->current != NULL && (x = xmlnode_get_parent(xmlp->current)) != NULL) { xmlp->current = x; } } /* * Parse an XML-encoded string into an xmlnode tree * * Caller is responsible for freeing the returned xmlnode */ static xmlnode xmlstr2xmlnode(char *xmlstring) { xmlstr2xmlnode_parser my_parser = g_new(xmlstr2xmlnode_parser_struct, 1); xmlnode x = NULL; my_parser->parser = XML_ParserCreate(NULL); my_parser->current = NULL; XML_SetUserData(my_parser->parser, (void *)my_parser); XML_SetElementHandler(my_parser->parser, xmlstr2xmlnode_startElement, xmlstr2xmlnode_endElement); XML_SetCharacterDataHandler(my_parser->parser, xmlstr2xmlnode_charData); XML_Parse(my_parser->parser, xmlstring, strlen(xmlstring), 0); x = my_parser->current; XML_ParserFree(my_parser->parser); g_free(my_parser); return(x); } /* * Insert a tag node into an xmlnode tree, recursively inserting parent tag * nodes as necessary * * Returns pointer to inserted node * * Note to hackers: this code is designed to be re-entrant (it's recursive--it * calls itself), so don't put any "static"s in here! */ static xmlnode insert_tag_to_parent_tag(xmlnode start, const char *parent_tag, const char *new_tag) { xmlnode x = NULL; /* * If the parent tag wasn't specified, see if we can get it * from the vCard template struct. */ if(parent_tag == NULL) { struct vcard_template *vc_tp = vcard_template_data; while(vc_tp->label != NULL) { if(strcmp(vc_tp->tag, new_tag) == 0) { parent_tag = vc_tp->ptag; break; } ++vc_tp; } } /* * If we have a parent tag... */ if(parent_tag != NULL ) { /* * Try to get the parent node for a tag */ if((x = xmlnode_get_tag(start, parent_tag)) == NULL) { /* * Descend? */ char *grand_parent = strcpy(g_malloc(strlen(parent_tag) + 1), parent_tag); char *parent; if((parent = strrchr(grand_parent, '/')) != NULL) { *(parent++) = '\0'; x = insert_tag_to_parent_tag(start, grand_parent, parent); } else { x = xmlnode_insert_tag(start, grand_parent); } g_free(grand_parent); } else { /* * We found *something* to be the parent node. * Note: may be the "root" node! */ xmlnode y; if((y = xmlnode_get_tag(x, new_tag)) != NULL) { return(y); } } } /* * insert the new tag into its parent node */ return(xmlnode_insert_tag((x == NULL? start : x), new_tag)); } /* * Send vCard info to Jabber server */ static void jabber_set_info(struct gaim_connection *gc, char *info) { xmlnode x, vc_node; char *id; struct jabber_data *jd = gc->proto_data; gjconn gjc = jd->gjc; x = xmlnode_new_tag("iq"); xmlnode_put_attrib(x,"type","set"); id = gjab_getid(gjc); xmlnode_put_attrib(x, "id", id); /* * Send only if there's actually any *information* to send */ if((vc_node = xmlstr2xmlnode(info)) != NULL && xmlnode_get_name(vc_node) != NULL && g_strncasecmp(xmlnode_get_name(vc_node), "vcard", 5) == 0) { xmlnode_insert_tag_node(x, vc_node); gjab_send(gjc, x); } xmlnode_free(x); } /* * displays a Jabber vCard */ static void jabber_handlevcard(gjconn gjc, xmlnode querynode, char *from) { struct jabber_data *jd = GJ_GC(gjc)->proto_data; jid who = jid_new(gjc->p, from); char *status = NULL, *text = NULL; GString *str = g_string_sized_new(100); xmlnode child; gchar *buddy = NULL; if(querynode == NULL) { serv_got_crap(GJ_GC(gjc), "%s - Received empty info reply from %s", _("User Info"), from); return; } if(who->resource != NULL && (who->resource)[0] != '\0') { buddy = g_strdup_printf("%s@%s/%s", who->user, who->server, who->resource); } else { buddy = g_strdup_printf("%s@%s", who->user, who->server); } if((status = g_hash_table_lookup(jd->hash, buddy)) == NULL) { status = _("Unknown"); } g_string_sprintfa(str, "%s: %s - %s: %s", _("Jabber ID"), buddy, _("Status"), status); for(child = querynode->firstchild; child; child = child->next) { xmlnode child2; if(child->type != NTYPE_TAG) continue; text = xmlnode_get_data(child); if(text && !strcmp(child->name, "FN")) { info_string_append(str, "\n", _("Full Name"), text); } else if (!strcmp(child->name, "N")) { for (child2 = child->firstchild; child2; child2 = child2->next) { char *text2 = NULL; if (child2->type != NTYPE_TAG) continue; text2 = xmlnode_get_data(child2); if (text2 && !strcmp(child2->name, "FAMILY")) { info_string_append(str, "\n", _("Family Name"), text2); } else if (text2 && !strcmp(child2->name, "GIVEN")) { info_string_append(str, "\n", _("Given Name"), text2); } else if (text2 && !strcmp(child2->name, "MIDDLE")) { info_string_append(str, "\n", _("Middle Name"), text2); } } } else if (text && !strcmp(child->name, "NICKNAME")) { info_string_append(str, "\n", _("Nickname"), text); } else if (text && !strcmp(child->name, "BDAY")) { info_string_append(str, "\n", _("Birthday"), text); } else if (!strcmp(child->name, "ADR")) { /* show wich address it is */ /* Just for the beauty of bitlbee if (child->firstchild) g_string_sprintfa(str, "%s:\n", _("Address")); */ for(child2 = child->firstchild; child2; child2 = child2->next) { char *text2 = NULL; if(child2->type != NTYPE_TAG) continue; text2 = xmlnode_get_data(child2); if(text2 && !strcmp(child2->name, "POBOX")) { info_string_append(str, "\n", _("P.O. Box"), text2); } else if(text2 && !strcmp(child2->name, "EXTADR")) { info_string_append(str, "\n", _("Extended Address"), text2); } else if(text2 && !strcmp(child2->name, "STREET")) { info_string_append(str, "\n", _("Street Address"), text2); } else if(text2 && !strcmp(child2->name, "LOCALITY")) { info_string_append(str, "\n", _("Locality"), text2); } else if(text2 && !strcmp(child2->name, "REGION")) { info_string_append(str, "\n", _("Region"), text2); } else if(text2 && !strcmp(child2->name, "PCODE")) { info_string_append(str, "\n", _("Postal Code"), text2); } else if(text2 && (!strcmp(child2->name, "CTRY") || !strcmp(child2->name, "COUNTRY"))) { info_string_append(str, "\n", _("Country"), text2); } } } else if(!strcmp(child->name, "TEL")) { char *number = NULL; if ((child2 = xmlnode_get_tag(child, "NUMBER"))) { /* show what kind of number it is */ number = xmlnode_get_data(child2); if(number) { info_string_append(str, "\n", _("Telephone"), number); } } else if((number = xmlnode_get_data(child))) { /* lots of clients (including gaim) do this, * but it's out of spec */ info_string_append(str, "\n", _("Telephone"), number); } } else if(!strcmp(child->name, "EMAIL")) { char *userid = NULL; if((child2 = xmlnode_get_tag(child, "USERID"))) { /* show what kind of email it is */ userid = xmlnode_get_data(child2); if(userid) { info_string_append(str, "\n", _("Email"), userid); } } else if((userid = xmlnode_get_data(child))) { /* lots of clients (including gaim) do this, * but it's out of spec */ info_string_append(str, "\n", _("Email"), userid); } } else if(!strcmp(child->name, "ORG")) { for(child2 = child->firstchild; child2; child2 = child2->next) { char *text2 = NULL; if(child2->type != NTYPE_TAG) continue; text2 = xmlnode_get_data(child2); if(text2 && !strcmp(child2->name, "ORGNAME")) { info_string_append(str, "\n", _("Organization Name"), text2); } else if(text2 && !strcmp(child2->name, "ORGUNIT")) { info_string_append(str, "\n", _("Organization Unit"), text2); } } } else if(text && !strcmp(child->name, "TITLE")) { info_string_append(str, "\n", _("Title"), text); } else if(text && !strcmp(child->name, "ROLE")) { info_string_append(str, "\n", _("Role"), text); } else if(text && !strcmp(child->name, "DESC")) { g_string_sprintfa(str, "\n%s:\n%s\n%s", _("Description"), text, _("End of Description")); } } serv_got_crap(GJ_GC(gjc), "%s\n%s", _("User Info"), str->str); g_free(buddy); g_string_free(str, TRUE); } void jabber_init() { struct prpl *ret = g_new0(struct prpl, 1); ret->name = "jabber"; ret->away_states = jabber_away_states; ret->acc_init = jabber_acc_init; ret->login = jabber_login; ret->close = jabber_close; ret->send_im = jabber_send_im; ret->set_info = jabber_set_info; ret->get_info = jabber_get_info; ret->set_away = jabber_set_away; ret->get_away = jabber_get_away_msg; ret->add_buddy = jabber_add_buddy; ret->remove_buddy = jabber_remove_buddy; ret->keepalive = jabber_keepalive; ret->alias_buddy = jabber_roster_update; ret->group_buddy = jabber_group_change; ret->handle_cmp = g_strcasecmp; register_protocol (ret); }