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;
}
ass="w"> "auto_reconnect" ) && set_getbool( &a->set, "auto_reconnect" ) && ( delay = account_reconnect_delay( a ) ) > 0 ) { imcb_log( ic, "Reconnecting in %d seconds..", delay ); a->reconnect = b_timeout_add( delay * 1000, auto_reconnect, a ); } imc_free( ic ); } /* dialogs.c */ void imcb_ask( struct im_connection *ic, char *msg, void *data, query_callback doit, query_callback dont ) { query_add( ic->irc, ic, msg, doit, dont, data ); } /* list.c */ void imcb_add_buddy( struct im_connection *ic, const char *handle, const char *group ) { user_t *u; char nick[MAX_NICK_LENGTH+1], *s; irc_t *irc = ic->irc; if( user_findhandle( ic, handle ) ) { if( set_getbool( &irc->set, "debug" ) ) imcb_log( ic, "User already exists, ignoring add request: %s", handle ); return; /* Buddy seems to exist already. Let's ignore this request then... Eventually subsequent calls to this function *should* be possible when a buddy is in multiple groups. But for now BitlBee doesn't even support groups so let's silently ignore this for now. */ } memset( nick, 0, MAX_NICK_LENGTH + 1 ); strcpy( nick, nick_get( ic->acc, handle ) ); u = user_add( ic->irc, nick ); // if( !realname || !*realname ) realname = nick; // u->realname = g_strdup( realname ); if( ( s = strchr( handle, '@' ) ) ) { u->host = g_strdup( s + 1 ); u->user = g_strndup( handle, s - handle ); } else if( ic->acc->server ) { u->host = g_strdup( ic->acc->server ); u->user = g_strdup( handle ); /* s/ /_/ ... important for AOL screennames */ for( s = u->user; *s; s ++ ) if( *s == ' ' ) *s = '_'; } else { u->host = g_strdup( ic->acc->prpl->name ); u->user = g_strdup( handle ); } u->ic = ic; u->handle = g_strdup( handle ); if( group ) u->group = g_strdup( group ); u->send_handler = buddy_send_handler; u->last_typing_notice = 0; } struct buddy *imcb_find_buddy( struct im_connection *ic, char *handle ) { static struct buddy b[1]; user_t *u; u = user_findhandle( ic, handle ); if( !u ) return( NULL ); memset( b, 0, sizeof( b ) ); strncpy( b->name, handle, 80 ); strncpy( b->show, u->realname, BUDDY_ALIAS_MAXLEN ); b->present = u->online; b->ic = u->ic; return( b ); } void imcb_rename_buddy( struct im_connection *ic, const char *handle, const char *realname ) { user_t *u = user_findhandle( ic, handle ); char *set; if( !u || !realname ) return; if( g_strcasecmp( u->realname, realname ) != 0 ) { if( u->realname != u->nick ) g_free( u->realname ); u->realname = g_strdup( realname ); if( ( ic->flags & OPT_LOGGED_IN ) && set_getbool( &ic->irc->set, "display_namechanges" ) ) imcb_log( ic, "User `%s' changed name to `%s'", u->nick, u->realname ); } set = set_getstr( &ic->acc->set, "nick_source" ); if( strcmp( set, "handle" ) != 0 ) { char *name = g_strdup( realname ); if( strcmp( set, "first_name" ) == 0 ) { int i; for( i = 0; name[i] && !isspace( name[i] ); i ++ ) {} name[i] = '\0'; } imcb_buddy_nick_hint( ic, handle, name ); g_free( name ); } } void imcb_remove_buddy( struct im_connection *ic, const char *handle, char *group ) { user_t *u; if( ( u = user_findhandle( ic, handle ) ) ) user_del( ic->irc, u->nick ); } /* Mainly meant for ICQ (and now also for Jabber conferences) to allow IM modules to suggest a nickname for a handle. */ void imcb_buddy_nick_hint( struct im_connection *ic, const char *handle, const char *nick ) { user_t *u = user_findhandle( ic, handle ); char newnick[MAX_NICK_LENGTH+1], *orig_nick; if( u && !u->online && !nick_saved( ic->acc, handle ) ) { /* Only do this if the person isn't online yet (which should be the case if we just added it) and if the user hasn't assigned a nickname to this buddy already. */ strncpy( newnick, nick, MAX_NICK_LENGTH ); newnick[MAX_NICK_LENGTH] = 0; /* Some processing to make sure this string is a valid IRC nickname. */ nick_strip( newnick ); if( set_getbool( &ic->irc->set, "lcnicks" ) ) nick_lc( newnick ); if( strcmp( u->nick, newnick ) != 0 ) { /* Only do this if newnick is different from the current one. If rejoining a channel, maybe we got this nick already (and dedupe would only add an underscore. */ nick_dedupe( ic->acc, handle, newnick ); /* u->nick will be freed halfway the process, so it can't be passed as an argument. */ orig_nick = g_strdup( u->nick ); user_rename( ic->irc, orig_nick, newnick ); g_free( orig_nick ); } } } struct imcb_ask_cb_data { struct im_connection *ic; char *handle; }; static void imcb_ask_auth_cb_no( void *data ) { struct imcb_ask_cb_data *cbd = data; cbd->ic->acc->prpl->auth_deny( cbd->ic, cbd->handle ); g_free( cbd->handle ); g_free( cbd ); } static void imcb_ask_auth_cb_yes( void *data ) { struct imcb_ask_cb_data *cbd = data; cbd->ic->acc->prpl->auth_allow( cbd->ic, cbd->handle ); g_free( cbd->handle ); g_free( cbd ); } void imcb_ask_auth( struct im_connection *ic, const char *handle, const char *realname ) { struct imcb_ask_cb_data *data = g_new0( struct imcb_ask_cb_data, 1 ); char *s, *realname_ = NULL; if( realname != NULL ) realname_ = g_strdup_printf( " (%s)", realname ); s = g_strdup_printf( "The user %s%s wants to add you to his/her buddy list.", handle, realname_ ?: "" ); g_free( realname_ ); data->ic = ic; data->handle = g_strdup( handle ); query_add( ic->irc, ic, s, imcb_ask_auth_cb_yes, imcb_ask_auth_cb_no, data ); } static void imcb_ask_add_cb_no( void *data ) { g_free( ((struct imcb_ask_cb_data*)data)->handle ); g_free( data ); } static void imcb_ask_add_cb_yes( void *data ) { struct imcb_ask_cb_data *cbd = data; cbd->ic->acc->prpl->add_buddy( cbd->ic, cbd->handle, NULL ); return imcb_ask_add_cb_no( data ); } void imcb_ask_add( struct im_connection *ic, const char *handle, const char *realname ) { struct imcb_ask_cb_data *data = g_new0( struct imcb_ask_cb_data, 1 ); char *s; /* TODO: Make a setting for this! */ if( user_findhandle( ic, handle ) != NULL ) return; s = g_strdup_printf( "The user %s is not in your buddy list yet. Do you want to add him/her now?", handle ); data->ic = ic; data->handle = g_strdup( handle ); query_add( ic->irc, ic, s, imcb_ask_add_cb_yes, imcb_ask_add_cb_no, data ); } /* server.c */ void imcb_buddy_status( struct im_connection *ic, const char *handle, int flags, const char *state, const char *message ) { user_t *u; int oa, oo; u = user_findhandle( ic, (char*) handle ); if( !u ) { if( g_strcasecmp( set_getstr( &ic->irc->set, "handle_unknown" ), "add" ) == 0 ) { imcb_add_buddy( ic, (char*) handle, NULL ); u = user_findhandle( ic, (char*) handle ); } else { if( set_getbool( &ic->irc->set, "debug" ) || g_strcasecmp( set_getstr( &ic->irc->set, "handle_unknown" ), "ignore" ) != 0 ) { imcb_log( ic, "imcb_buddy_status() for unknown handle %s:", handle ); imcb_log( ic, "flags = %d, state = %s, message = %s", flags, state ? state : "NULL", message ? message : "NULL" ); } return; } } oa = u->away != NULL; oo = u->online; g_free( u->away ); g_free( u->status_msg ); u->away = u->status_msg = NULL; if( ( flags & OPT_LOGGED_IN ) && !u->online ) { irc_spawn( ic->irc, u ); u->online = 1; } else if( !( flags & OPT_LOGGED_IN ) && u->online ) { struct groupchat *c; irc_kill( ic->irc, u ); u->online = 0; /* Remove him/her from the groupchats to prevent PART messages after he/she QUIT already */ for( c = ic->groupchats; c; c = c->next ) remove_chat_buddy_silent( c, handle ); } if( flags & OPT_AWAY ) { if( state && message ) { u->away = g_strdup_printf( "%s (%s)", state, message ); } else if( state ) { u->away = g_strdup( state ); } else if( message ) { u->away = g_strdup( message ); } else { u->away = g_strdup( "Away" ); } } else { u->status_msg = g_strdup( message ); } /* LISPy... */ if( ( set_getbool( &ic->irc->set, "away_devoice" ) ) && /* Don't do a thing when user doesn't want it */ ( u->online ) && /* Don't touch offline people */ ( ( ( u->online != oo ) && !u->away ) || /* Voice joining people */ ( ( u->online == oo ) && ( oa == !u->away ) ) ) ) /* (De)voice people changing state */ { char *from; if( set_getbool( &ic->irc->set, "simulate_netsplit" ) ) { from = g_strdup( ic->irc->myhost ); } else { from = g_strdup_printf( "%s!%s@%s", ic->irc->mynick, ic->irc->mynick, ic->irc->myhost ); } irc_write( ic->irc, ":%s MODE %s %cv %s", from, ic->irc->channel, u->away?'-':'+', u->nick ); g_free( from ); } } void imcb_buddy_msg( struct im_connection *ic, const char *handle, char *msg, uint32_t flags, time_t sent_at ) { irc_t *irc = ic->irc; char *wrapped, *ts = NULL; user_t *u; u = user_findhandle( ic, handle ); if( !u ) { char *h = set_getstr( &irc->set, "handle_unknown" ); if( g_strcasecmp( h, "ignore" ) == 0 ) { if( set_getbool( &irc->set, "debug" ) ) imcb_log( ic, "Ignoring message from unknown handle %s", handle ); return; } else if( g_strncasecmp( h, "add", 3 ) == 0 ) { int private = set_getbool( &irc->set, "private" ); if( h[3] ) { if( g_strcasecmp( h + 3, "_private" ) == 0 ) private = 1; else if( g_strcasecmp( h + 3, "_channel" ) == 0 ) private = 0; } imcb_add_buddy( ic, handle, NULL ); u = user_findhandle( ic, handle ); u->is_private = private; } else { imcb_log( ic, "Message from unknown handle %s:", handle ); u = user_find( irc, irc->mynick ); } } if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) || ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) ) strip_html( msg ); if( set_getbool( &ic->irc->set, "display_timestamps" ) && ( ts = format_timestamp( irc, sent_at ) ) ) { char *new = g_strconcat( ts, msg, NULL ); g_free( ts ); ts = msg = new; } wrapped = word_wrap( msg, 425 ); irc_msgfrom( irc, u->nick, wrapped ); g_free( wrapped ); g_free( ts ); } void imcb_buddy_typing( struct im_connection *ic, char *handle, uint32_t flags ) { user_t *u; if( !set_getbool( &ic->irc->set, "typing_notice" ) ) return; if( ( u = user_findhandle( ic, handle ) ) ) { char buf[256]; g_snprintf( buf, 256, "\1TYPING %d\1", ( flags >> 8 ) & 3 ); irc_privmsg( ic->irc, u, "PRIVMSG", ic->irc->nick, NULL, buf ); } } struct groupchat *imcb_chat_new( struct im_connection *ic, const char *handle ) { struct groupchat *c; /* This one just creates the conversation structure, user won't see anything yet */ if( ic->groupchats ) { for( c = ic->groupchats; c->next; c = c->next ); c = c->next = g_new0( struct groupchat, 1 ); } else ic->groupchats = c = g_new0( struct groupchat, 1 ); c->ic = ic; c->title = g_strdup( handle ); c->channel = g_strdup_printf( "&chat_%03d", ic->irc->c_id++ ); c->topic = g_strdup_printf( "BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!", c->title ); if( set_getbool( &ic->irc->set, "debug" ) ) imcb_log( ic, "Creating new conversation: (id=%p,handle=%s)", c, handle ); return c; } void imcb_chat_name_hint( struct groupchat *c, const char *name ) { if( !c->joined ) { struct im_connection *ic = c->ic; char stripped[MAX_NICK_LENGTH+1], *full_name; strncpy( stripped, name, MAX_NICK_LENGTH ); stripped[MAX_NICK_LENGTH] = '\0'; nick_strip( stripped ); if( set_getbool( &ic->irc->set, "lcnicks" ) ) nick_lc( stripped ); full_name = g_strdup_printf( "&%s", stripped ); if( stripped[0] && nick_cmp( stripped, ic->irc->channel + 1 ) != 0 && irc_chat_by_channel( ic->irc, full_name ) == NULL ) { g_free( c->channel ); c->channel = full_name; } else { g_free( full_name ); } } } void imcb_chat_free( struct groupchat *c ) { struct im_connection *ic = c->ic; struct groupchat *l; GList *ir; if( set_getbool( &ic->irc->set, "debug" ) ) imcb_log( ic, "You were removed from conversation %p", c ); if( c ) { if( c->joined ) { user_t *u, *r; r = user_find( ic->irc, ic->irc->mynick ); irc_privmsg( ic->irc, r, "PRIVMSG", c->channel, "", "Cleaning up channel, bye!" ); u = user_find( ic->irc, ic->irc->nick ); irc_kick( ic->irc, u, c->channel, r ); /* irc_part( ic->irc, u, c->channel ); */ } /* Find the previous chat in the linked list. */ for( l = ic->groupchats; l && l->next != c; l = l->next ); if( l ) l->next = c->next; else ic->groupchats = c->next; for( ir = c->in_room; ir; ir = ir->next ) g_free( ir->data ); g_list_free( c->in_room ); g_free( c->channel ); g_free( c->title ); g_free( c->topic ); g_free( c ); } } void imcb_chat_msg( struct groupchat *c, const char *who, char *msg, uint32_t flags, time_t sent_at ) { struct im_connection *ic = c->ic; char *wrapped; user_t *u; /* Gaim sends own messages through this too. IRC doesn't want this, so kill them */ if( g_strcasecmp( who, ic->acc->user ) == 0 ) return; u = user_findhandle( ic, who ); if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) || ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) ) strip_html( msg ); wrapped = word_wrap( msg, 425 ); if( c && u ) { char *ts = NULL; if( set_getbool( &ic->irc->set, "display_timestamps" ) ) ts = format_timestamp( ic->irc, sent_at ); irc_privmsg( ic->irc, u, "PRIVMSG", c->channel, ts ? : "", wrapped ); g_free( ts ); } else { imcb_log( ic, "Message from/to conversation %s@%p (unknown conv/user): %s", who, c, wrapped ); } g_free( wrapped ); } void imcb_chat_log( struct groupchat *c, char *format, ... ) { irc_t *irc = c->ic->irc; va_list params; char *text; user_t *u; va_start( params, format ); text = g_strdup_vprintf( format, params ); va_end( params ); u = user_find( irc, irc->mynick ); irc_privmsg( irc, u, "PRIVMSG", c->channel, "System message: ", text ); g_free( text ); } void imcb_chat_topic( struct groupchat *c, char *who, char *topic, time_t set_at ) { struct im_connection *ic = c->ic; user_t *u = NULL; if( who == NULL) u = user_find( ic->irc, ic->irc->mynick ); else if( g_strcasecmp( who, ic->acc->user ) == 0 ) u = user_find( ic->irc, ic->irc->nick ); else u = user_findhandle( ic, who ); if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) || ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) ) strip_html( topic ); g_free( c->topic ); c->topic = g_strdup( topic ); if( c->joined && u ) irc_write( ic->irc, ":%s!%s@%s TOPIC %s :%s", u->nick, u->user, u->host, c->channel, topic ); } /* buddy_chat.c */ void imcb_chat_add_buddy( struct groupchat *b, const char *handle ) { user_t *u = user_findhandle( b->ic, handle ); int me = 0; if( set_getbool( &b->ic->irc->set, "debug" ) ) imcb_log( b->ic, "User %s added to conversation %p", handle, b ); /* It might be yourself! */ if( b->ic->acc->prpl->handle_cmp( handle, b->ic->acc->user ) == 0 ) { u = user_find( b->ic->irc, b->ic->irc->nick ); if( !b->joined ) irc_join( b->ic->irc, u, b->channel ); b->joined = me = 1; } /* Most protocols allow people to join, even when they're not in your contact list. Try to handle that here */ if( !u ) { imcb_add_buddy( b->ic, handle, NULL ); u = user_findhandle( b->ic, handle ); } /* Add the handle to the room userlist, if it's not 'me' */ if( !me ) { if( b->joined ) irc_join( b->ic->irc, u, b->channel ); b->in_room = g_list_append( b->in_room, g_strdup( handle ) ); } } /* This function is one BIG hack... :-( EREWRITE */ void imcb_chat_remove_buddy( struct groupchat *b, const char *handle, const char *reason ) { user_t *u; int me = 0; if( set_getbool( &b->ic->irc->set, "debug" ) ) imcb_log( b->ic, "User %s removed from conversation %p (%s)", handle, b, reason ? reason : "" ); /* It might be yourself! */ if( g_strcasecmp( handle, b->ic->acc->user ) == 0 ) { if( b->joined == 0 ) return; u = user_find( b->ic->irc, b->ic->irc->nick ); b->joined = 0; me = 1; } else { u = user_findhandle( b->ic, handle ); } if( me || ( remove_chat_buddy_silent( b, handle ) && b->joined && u ) ) irc_part( b->ic->irc, u, b->channel ); } static int remove_chat_buddy_silent( struct groupchat *b, const char *handle ) { GList *i; /* Find the handle in the room userlist and shoot it */ i = b->in_room; while( i ) { if( g_strcasecmp( handle, i->data ) == 0 ) { g_free( i->data ); b->in_room = g_list_remove( b->in_room, i->data ); return( 1 ); } i = i->next; } return( 0 ); } /* Misc. BitlBee stuff which shouldn't really be here */ char *set_eval_away_devoice( set_t *set, char *value ) { irc_t *irc = set->data; int st; if( !is_bool( value ) ) return SET_INVALID; st = bool2int( value ); /* Horror.... */ if( st != set_getbool( &irc->set, "away_devoice" ) ) { char list[80] = ""; user_t *u = irc->users; int i = 0, count = 0; char pm; char v[80]; if( st ) pm = '+'; else pm = '-'; while( u ) { if( u->ic && u->online && !u->away ) { if( ( strlen( list ) + strlen( u->nick ) ) >= 79 ) { for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0; irc_write( irc, ":%s MODE %s %c%s%s", irc->myhost, irc->channel, pm, v, list ); *list = 0; count = 0; } sprintf( list + strlen( list ), " %s", u->nick ); count ++; } u = u->next; } /* $v = 'v' x $i */ for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0; irc_write( irc, ":%s MODE %s %c%s%s", irc->myhost, irc->channel, pm, v, list ); } return value; } char *set_eval_timezone( set_t *set, char *value ) { char *s; if( strcmp( value, "local" ) == 0 || strcmp( value, "gmt" ) == 0 || strcmp( value, "utc" ) == 0 ) return value; /* Otherwise: +/- at the beginning optional, then one or more numbers, possibly followed by a colon and more numbers. Don't bother bound- checking them since users are free to shoot themselves in the foot. */ s = value; if( *s == '+' || *s == '-' ) s ++; /* \d+ */ if( !isdigit( *s ) ) return SET_INVALID; while( *s && isdigit( *s ) ) s ++; /* EOS? */ if( *s == '\0' ) return value; /* Otherwise, colon */ if( *s != ':' ) return SET_INVALID; s ++; /* \d+ */ if( !isdigit( *s ) ) return SET_INVALID; while( *s && isdigit( *s ) ) s ++; /* EOS */ return *s == '\0' ? value : SET_INVALID; } static char *format_timestamp( irc_t *irc, time_t msg_ts ) { time_t now_ts = time( NULL ); struct tm now, msg; char *set; /* If the timestamp is <= 0 or less than a minute ago, discard it as it doesn't seem to add to much useful info and/or might be noise. */ if( msg_ts <= 0 || msg_ts > now_ts - 60 ) return NULL; set = set_getstr( &irc->set, "timezone" ); if( strcmp( set, "local" ) == 0 ) { localtime_r( &now_ts, &now ); localtime_r( &msg_ts, &msg ); } else { int hr, min = 0, sign = 60; if( set[0] == '-' ) { sign *= -1; set ++; } else if( set[0] == '+' ) { set ++; } if( sscanf( set, "%d:%d", &hr, &min ) >= 1 ) { msg_ts += sign * ( hr * 60 + min ); now_ts += sign * ( hr * 60 + min ); } gmtime_r( &now_ts, &now ); gmtime_r( &msg_ts, &msg ); } if( msg.tm_year == now.tm_year && msg.tm_yday == now.tm_yday ) return g_strdup_printf( "\x02[\x02\x02\x02%02d:%02d:%02d\x02]\x02 ", msg.tm_hour, msg.tm_min, msg.tm_sec ); else return g_strdup_printf( "\x02[\x02\x02\x02%04d-%02d-%02d " "%02d:%02d:%02d\x02]\x02 ", msg.tm_year + 1900, msg.tm_mon, msg.tm_mday, msg.tm_hour, msg.tm_min, msg.tm_sec ); } /* The plan is to not allow straight calls to prpl functions anymore, but do them all from some wrappers. We'll start to define some down here: */ int imc_buddy_msg( struct im_connection *ic, char *handle, char *msg, int flags ) { char *buf = NULL; int st; if( ( ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) ) { buf = escape_html( msg ); msg = buf; } st = ic->acc->prpl->buddy_msg( ic, handle, msg, flags ); g_free( buf ); return st; } int imc_chat_msg( struct groupchat *c, char *msg, int flags ) { char *buf = NULL; if( ( c->ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) ) { buf = escape_html( msg ); msg = buf; } c->ic->acc->prpl->chat_msg( c, msg, flags ); g_free( buf ); return 1; } static char *imc_away_state_find( GList *gcm, char *away, char **message ); int imc_away_send_update( struct im_connection *ic ) { char *away, *msg = NULL; if( ic->acc->prpl->away_states == NULL || ic->acc->prpl->set_away == NULL ) return 0; away = set_getstr( &ic->acc->set, "away" ) ? : set_getstr( &ic->irc->set, "away" ); if( away && *away ) { GList *m = ic->acc->prpl->away_states( ic ); msg = ic->acc->flags & ACC_FLAG_AWAY_MESSAGE ? away : NULL; away = imc_away_state_find( m, away, &msg ) ? : m->data; } else if( ic->acc->flags & ACC_FLAG_STATUS_MESSAGE ) { away = NULL; msg = set_getstr( &ic->acc->set, "status" ) ? : set_getstr( &ic->irc->set, "status" ); } ic->acc->prpl->set_away( ic, away, msg ); return 1; } static char *imc_away_alias_list[8][5] = { { "Away from computer", "Away", "Extended away", NULL }, { "NA", "N/A", "Not available", NULL }, { "Busy", "Do not disturb", "DND", "Occupied", NULL }, { "Be right back", "BRB", NULL }, { "On the phone", "Phone", "On phone", NULL }, { "Out to lunch", "Lunch", "Food", NULL }, { "Invisible", "Hidden" }, { NULL } }; static char *imc_away_state_find( GList *gcm, char *away, char **message ) { GList *m; int i, j; for( m = gcm; m; m = m->next ) if( g_strncasecmp( m->data, away, strlen( m->data ) ) == 0 ) { /* At least the Yahoo! module works better if message contains no data unless it adds something to what we have in state already. */ if( strlen( m->data ) == strlen( away ) ) *message = NULL; return m->data; } for( i = 0; *imc_away_alias_list[i]; i ++ ) { int keep_message; for( j = 0; imc_away_alias_list[i][j]; j ++ ) if( g_strncasecmp( away, imc_away_alias_list[i][j], strlen( imc_away_alias_list[i][j] ) ) == 0 ) { keep_message = strlen( away ) != strlen( imc_away_alias_list[i][j] ); break; } if( !imc_away_alias_list[i][j] ) /* If we reach the end, this row */ continue; /* is not what we want. Next! */ /* Now find an entry in this row which exists in gcm */ for( j = 0; imc_away_alias_list[i][j]; j ++ ) { for( m = gcm; m; m = m->next ) if( g_strcasecmp( imc_away_alias_list[i][j], m->data ) == 0 ) { if( !keep_message ) *message = NULL; return imc_away_alias_list[i][j]; } } /* No need to look further, apparently this state doesn't have any good alias for this protocol. */ break; } return NULL; } void imc_add_allow( struct im_connection *ic, char *handle ) { if( g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL ) { ic->permit = g_slist_prepend( ic->permit, g_strdup( handle ) ); } ic->acc->prpl->add_permit( ic, handle ); } void imc_rem_allow( struct im_connection *ic, char *handle ) { GSList *l; if( ( l = g_slist_find_custom( ic->permit, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) ) { g_free( l->data ); ic->permit = g_slist_delete_link( ic->permit, l ); } ic->acc->prpl->rem_permit( ic, handle ); } void imc_add_block( struct im_connection *ic, char *handle ) { if( g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) == NULL ) { ic->deny = g_slist_prepend( ic->deny, g_strdup( handle ) ); } ic->acc->prpl->add_deny( ic, handle ); } void imc_rem_block( struct im_connection *ic, char *handle ) { GSList *l; if( ( l = g_slist_find_custom( ic->deny, handle, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) ) { g_free( l->data ); ic->deny = g_slist_delete_link( ic->deny, l ); } ic->acc->prpl->rem_deny( ic, handle ); } void imcb_clean_handle( struct im_connection *ic, char *handle ) { /* Accepts a handle and does whatever is necessary to make it BitlBee-friendly. Currently this means removing everything outside 33-127 (ASCII printable excl spaces), @ (only one is allowed) and ! and : */ char out[strlen(handle)+1]; int s, d; s = d = 0; while( handle[s] ) { if( handle[s] > ' ' && handle[s] != '!' && handle[s] != ':' && ( handle[s] & 0x80 ) == 0 ) { if( handle[s] == '@' ) { /* See if we got an @ already? */ out[d] = 0; if( strchr( out, '@' ) ) continue; } out[d++] = handle[s]; } s ++; } out[d] = handle[s]; strcpy( handle, out ); }