summaryrefslogtreecommitdiff
path: root/libpurple/protocols/silc/util.c
blob: ddc86244dd6d1f4e00fa25451cbb19519aac617b (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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/*

  silcpurple_util.c

  Author: Pekka Riikonen <priikone@silcnet.org>

  Copyright (C) 2004 - 2007 Pekka Riikonen

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; version 2 of the License.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

*/

#include "internal.h"
#include "glibcompat.h" /* for purple_g_stat on win32 */
#include "image-store.h"

PURPLE_BEGIN_IGNORE_CAST_ALIGN
#include "silc.h"
PURPLE_END_IGNORE_CAST_ALIGN
#include "silcclient.h"
#include "silcpurple.h"

/**************************** Utility Routines *******************************/

static char str[256], str2[256];

const char *silcpurple_silcdir(void)
{
	const char *hd = purple_home_dir();
	memset(str, 0, sizeof(str));
	g_snprintf(str, sizeof(str) - 1, "%s" G_DIR_SEPARATOR_S ".silc", hd ? hd : "/tmp");
	return (const char *)str;
}

const char *silcpurple_session_file(const char *account)
{
	memset(str2, 0, sizeof(str2));
	g_snprintf(str2, sizeof(str2) - 1, "%s" G_DIR_SEPARATOR_S "%s_session",
		   silcpurple_silcdir(), account);
	return (const char *)str2;
}

gboolean silcpurple_ip_is_private(const char *ip)
{
	if (silc_net_is_ip4(ip)) {
		if (!strncmp(ip, "10.", 3)) {
			return TRUE;
		} else if (!strncmp(ip, "172.", 4) && strlen(ip) > 6) {
			char tmp[3];
			int s;
			memset(tmp, 0, sizeof(tmp));
			strncpy(tmp, ip + 4, 2);
			s = atoi(tmp);
			if (s >= 16 && s <= 31)
				return TRUE;
		} else if (!strncmp(ip, "192.168.", 8)) {
			return TRUE;
		}
	}

	return FALSE;
}

/* This checks stats for various SILC files and directories. First it
   checks if ~/.silc directory exist and is owned by the correct user. If
   it doesn't exist, it will create the directory. After that it checks if
   user's Public and Private key files exists and creates them if needed. */

gboolean silcpurple_check_silc_dir(PurpleConnection *gc)
{
	char filename[256], file_public_key[256], file_private_key[256];
	char servfilename[256], clientfilename[256], friendsfilename[256];
	char pkd[256], prd[256];
	GStatBuf st;
	struct passwd *pw;
	int fd;

	pw = getpwuid(getuid());
	if (!pw) {
		purple_debug_error("silc", "silc: %s\n", g_strerror(errno));
		return FALSE;
	}

	g_snprintf(filename, sizeof(filename) - 1, "%s", silcpurple_silcdir());
	g_snprintf(servfilename, sizeof(servfilename) - 1, "%s" G_DIR_SEPARATOR_S "serverkeys",
		   silcpurple_silcdir());
	g_snprintf(clientfilename, sizeof(clientfilename) - 1, "%s" G_DIR_SEPARATOR_S "clientkeys",
		   silcpurple_silcdir());
	g_snprintf(friendsfilename, sizeof(friendsfilename) - 1, "%s" G_DIR_SEPARATOR_S "friends",
		   silcpurple_silcdir());

	if (pw->pw_uid != geteuid()) {
		purple_debug_error("silc", "Couldn't create directories due to wrong uid!\n");
		return FALSE;
	}

	/*
	 * Check ~/.silc directory
	 */
	if (g_mkdir(filename, 0755) != 0 && errno != EEXIST) {
		purple_debug_error("silc", "Couldn't create '%s' directory\n", filename);
		return FALSE;
	}

#ifndef _WIN32
	if ((g_stat(filename, &st)) == -1) {
		purple_debug_error("silc", "Couldn't stat '%s' directory, error: %s\n", filename, g_strerror(errno));
		return FALSE;
	} else {
		/* Check the owner of the dir */
		if (st.st_uid != 0 && st.st_uid != pw->pw_uid) {
			purple_debug_error("silc", "You don't seem to own '%s' directory\n",
				filename);
			return FALSE;
		}
	}
#endif

	/*
	 * Check ~./silc/serverkeys directory
	 */
	if (g_mkdir(servfilename, 0755) != 0 && errno != EEXIST) {
		purple_debug_error("silc", "Couldn't create '%s' directory\n", servfilename);
		return FALSE;
	}

	/*
	 * Check ~./silc/clientkeys directory
	 */
	if (g_mkdir(clientfilename, 0755) != 0 && errno != EEXIST) {
		purple_debug_error("silc", "Couldn't create '%s' directory\n", clientfilename);
		return FALSE;
	}

	/*
	 * Check ~./silc/friends directory
	 */
	if (g_mkdir(friendsfilename, 0755) != 0 && errno != EEXIST) {
		purple_debug_error("silc", "Couldn't create '%s' directory\n", friendsfilename);
		return FALSE;
	}

	/*
	 * Check Public and Private keys
	 */
	g_snprintf(pkd, sizeof(pkd), "%s" G_DIR_SEPARATOR_S "public_key.pub", silcpurple_silcdir());
	g_snprintf(prd, sizeof(prd), "%s" G_DIR_SEPARATOR_S "private_key.prv", silcpurple_silcdir());
	g_snprintf(file_public_key, sizeof(file_public_key) - 1, "%s",
		   purple_account_get_string(purple_connection_get_account(gc), "public-key", pkd));
	g_snprintf(file_private_key, sizeof(file_public_key) - 1, "%s",
		   purple_account_get_string(purple_connection_get_account(gc), "private-key", prd));

	if ((g_stat(file_public_key, &st)) == -1) {
		/* If file doesn't exist */
		if (errno == ENOENT) {
			purple_connection_update_progress(gc, _("Creating SILC key pair..."), 1, 5);
			if (!silc_create_key_pair(SILCPURPLE_DEF_PKCS,
						  SILCPURPLE_DEF_PKCS_LEN,
						  file_public_key,
						  file_private_key, NULL,
						  (purple_connection_get_password(gc) == NULL) ? "" : purple_connection_get_password(gc),
						  NULL, NULL, FALSE)) {
				purple_connection_error(gc, PURPLE_CONNECTION_ERROR_OTHER_ERROR,
				                             _("Unable to create SILC key pair"));
				return FALSE;
			}

			if ((g_stat(file_public_key, &st)) == -1) {
				purple_debug_error("silc", "Couldn't stat '%s' public key, error: %s\n",
						   file_public_key, g_strerror(errno));
				return FALSE;
			}
		} else {
			purple_debug_error("silc", "Couldn't stat '%s' public key, error: %s\n",
					   file_public_key, g_strerror(errno));
			return FALSE;
		}
	}

#ifndef _WIN32
	/* Check the owner of the public key */
	if (st.st_uid != 0 && st.st_uid != pw->pw_uid) {
		purple_debug_error("silc", "You don't seem to own your public key!?\n");
		return FALSE;
	}
#endif

	if ((fd = g_open(file_private_key, O_RDONLY, 0)) != -1) {
		if (_purple_fstat(fd, &st) == -1) {
			purple_debug_error("silc", "Couldn't stat '%s' private key, error: %s\n",
					   file_private_key, g_strerror(errno));
			close(fd);
			return FALSE;
		}
	} else {
		/* If file doesn't exist */
		if (errno == ENOENT) {
			purple_connection_update_progress(gc, _("Creating SILC key pair..."), 1, 5);
			if (!silc_create_key_pair(SILCPURPLE_DEF_PKCS,
						  SILCPURPLE_DEF_PKCS_LEN,
						  file_public_key,
						  file_private_key, NULL,
						  (purple_connection_get_password(gc) == NULL) ? "" : purple_connection_get_password(gc),
						  NULL, NULL, FALSE)) {
				purple_connection_error(gc, PURPLE_CONNECTION_ERROR_OTHER_ERROR,
				                             _("Unable to create SILC key pair"));
				return FALSE;
			}

			if ((fd = g_open(file_private_key, O_RDONLY, 0)) != -1) {
				if (_purple_fstat(fd, &st) == -1) {
					purple_debug_error("silc", "Couldn't stat '%s' private key, error: %s\n",
							   file_private_key, g_strerror(errno));
					close(fd);
					return FALSE;
				}
			} else {
				purple_debug_error("silc", "Couldn't open '%s' "
					"private key, error: %s\n",
					file_private_key, g_strerror(errno));
				return FALSE;
			}
		} else {
			purple_debug_error("silc", "Couldn't open '%s' private key, error: %s\n",
					   file_private_key, g_strerror(errno));
			return FALSE;
		}
	}

#ifndef _WIN32
	/* Check the owner of the private key */
	if (st.st_uid != 0 && st.st_uid != pw->pw_uid) {
		purple_debug_error("silc", "You don't seem to own your private key!?\n");
		if (fd != -1)
			close(fd);
		return FALSE;
	}

	/* Check the permissions for the private key */
	if ((st.st_mode & 0777) != 0600) {
		purple_debug_warning("silc", "Wrong permissions in your private key file `%s'!\n"
			"Trying to change them ...\n", file_private_key);
		if ((fd == -1) || (g_fchmod(fd, S_IRUSR | S_IWUSR)) == -1) {
			purple_debug_error("silc",
				"Failed to change permissions for private key file!\n"
				"Permissions for your private key file must be 0600.\n");
			if (fd != -1)
				close(fd);
			return FALSE;
		}
		purple_debug_warning("silc", "Done.\n\n");
	}
#endif

	if (fd != -1)
		close(fd);

#ifdef _WIN32
	/* on win32, we calloc pw so pass it to free
	 * (see the getpwuid code below)
	 */
	free(pw);
#endif

	return TRUE;
}

#ifdef _WIN32
struct passwd *getpwuid(uid_t uid) {
	struct passwd *pwd = calloc(1, sizeof(struct passwd));
	return pwd;
}

uid_t getuid() {
	return 0;
}

uid_t geteuid() {
	return 0;
}
#endif

void silcpurple_show_public_key(SilcPurple sg,
				const char *name, SilcPublicKey public_key,
				GCallback callback, void *context)
{
	SilcPublicKeyIdentifier ident;
	SilcSILCPublicKey silc_pubkey;
	char *fingerprint, *babbleprint;
	unsigned char *pk;
	SilcUInt32 pk_len, key_len = 0;
	GString *s;

	/* We support showing only SILC public keys for now */
	if (silc_pkcs_get_type(public_key) != SILC_PKCS_SILC)
	  return;

	silc_pubkey = silc_pkcs_get_context(SILC_PKCS_SILC, public_key);
	ident = &silc_pubkey->identifier;
	key_len = silc_pkcs_public_key_get_len(public_key);

	pk = silc_pkcs_public_key_encode(public_key, &pk_len);
	if (!pk)
	  return;
	fingerprint = silc_hash_fingerprint(NULL, pk, pk_len);
	babbleprint = silc_hash_babbleprint(NULL, pk, pk_len);
	if (!fingerprint || !babbleprint)
	  return;

	s = g_string_new("");
	if (ident->realname)
		/* Hint for translators: Please check the tabulator width here and in
		   the next strings (short strings: 2 tabs, longer strings 1 tab,
		   sum: 3 tabs or 24 characters) */
		g_string_append_printf(s, _("Real Name: \t%s\n"), ident->realname);
	if (ident->username)
		g_string_append_printf(s, _("User Name: \t%s\n"), ident->username);
	if (ident->email)
		g_string_append_printf(s, _("Email: \t\t%s\n"), ident->email);
	if (ident->host)
		g_string_append_printf(s, _("Host Name: \t%s\n"), ident->host);
	if (ident->org)
		g_string_append_printf(s, _("Organization: \t%s\n"), ident->org);
	if (ident->country)
		g_string_append_printf(s, _("Country: \t%s\n"), ident->country);
	g_string_append_printf(s, _("Algorithm: \t%s\n"), silc_pubkey->pkcs->name);
	g_string_append_printf(s, _("Key Length: \t%d bits\n"), (int)key_len);
	if (ident->version)
	  g_string_append_printf(s, _("Version: \t%s\n"), ident->version);
	g_string_append_printf(s, "\n");
	g_string_append_printf(s, _("Public Key Fingerprint:\n%s\n\n"), fingerprint);
	g_string_append_printf(s, _("Public Key Babbleprint:\n%s"), babbleprint);

	purple_request_action(sg->gc, _("Public Key Information"),
			      _("Public Key Information"),
			      s->str, 0, purple_request_cpar_from_connection(sg->gc),
			      context, 1, _("Close"), callback);

	g_string_free(s, TRUE);
	silc_free(fingerprint);
	silc_free(babbleprint);
	silc_free(pk);
}

SilcAttributePayload
silcpurple_get_attr(SilcDList attrs, SilcAttribute attribute)
{
	SilcAttributePayload attr = NULL;

	if (!attrs)
		return NULL;

	silc_dlist_start(attrs);
	while ((attr = silc_dlist_get(attrs)) != SILC_LIST_END)
		if (attribute == silc_attribute_get_attribute(attr))
			break;

	return attr;
}

void silcpurple_get_umode_string(SilcUInt32 mode, char *buf,
				 SilcUInt32 buf_size)
{
	memset(buf, 0, buf_size);
	if ((mode & SILC_UMODE_SERVER_OPERATOR) ||
	    (mode & SILC_UMODE_ROUTER_OPERATOR)) {
		strcat(buf, (mode & SILC_UMODE_SERVER_OPERATOR) ?
		       "[server operator] " :
		       (mode & SILC_UMODE_ROUTER_OPERATOR) ?
		       "[SILC operator] " : "[unknown mode] ");
	}
	if (mode & SILC_UMODE_GONE)
		strcat(buf, "[away] ");
	if (mode & SILC_UMODE_INDISPOSED)
		strcat(buf, "[indisposed] ");
	if (mode & SILC_UMODE_BUSY)
		strcat(buf, "[busy] ");
	if (mode & SILC_UMODE_PAGE)
		strcat(buf, "[wake me up] ");
	if (mode & SILC_UMODE_HYPER)
		strcat(buf, "[hyperactive] ");
	if (mode & SILC_UMODE_ROBOT)
		strcat(buf, "[robot] ");
	if (mode & SILC_UMODE_ANONYMOUS)
		strcat(buf, "[anonymous] ");
	if (mode & SILC_UMODE_BLOCK_PRIVMSG)
		strcat(buf, "[blocks private messages] ");
	if (mode & SILC_UMODE_DETACHED)
		strcat(buf, "[detached] ");
	if (mode & SILC_UMODE_REJECT_WATCHING)
		strcat(buf, "[rejects watching] ");
	if (mode & SILC_UMODE_BLOCK_INVITE)
		strcat(buf, "[blocks invites] ");
	g_strchomp(buf);
}

void silcpurple_get_chmode_string(SilcUInt32 mode, char *buf,
				  SilcUInt32 buf_size)
{
	memset(buf, 0, buf_size);
	if (mode & SILC_CHANNEL_MODE_FOUNDER_AUTH)
		strcat(buf, "[permanent] ");
	if (mode & SILC_CHANNEL_MODE_PRIVATE)
		strcat(buf, "[private] ");
	if (mode & SILC_CHANNEL_MODE_SECRET)
		strcat(buf, "[secret] ");
	if (mode & SILC_CHANNEL_MODE_PRIVKEY)
		strcat(buf, "[private key] ");
	if (mode & SILC_CHANNEL_MODE_INVITE)
		strcat(buf, "[invite only] ");
	if (mode & SILC_CHANNEL_MODE_TOPIC)
		strcat(buf, "[topic restricted] ");
	if (mode & SILC_CHANNEL_MODE_ULIMIT)
		strcat(buf, "[user count limit] ");
	if (mode & SILC_CHANNEL_MODE_PASSPHRASE)
		strcat(buf, "[passphrase auth] ");
	if (mode & SILC_CHANNEL_MODE_CHANNEL_AUTH)
		strcat(buf, "[public key auth] ");
	if (mode & SILC_CHANNEL_MODE_SILENCE_USERS)
		strcat(buf, "[users silenced] ");
	if (mode & SILC_CHANNEL_MODE_SILENCE_OPERS)
		strcat(buf, "[operators silenced] ");
	g_strchomp(buf);
}

void silcpurple_get_chumode_string(SilcUInt32 mode, char *buf,
				 SilcUInt32 buf_size)
{
	memset(buf, 0, buf_size);
	if (mode & SILC_CHANNEL_UMODE_CHANFO)
		strcat(buf, "[founder] ");
	if (mode & SILC_CHANNEL_UMODE_CHANOP)
		strcat(buf, "[operator] ");
	if (mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES)
		strcat(buf, "[blocks messages] ");
	if (mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS)
		strcat(buf, "[blocks user messages] ");
	if (mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS)
		strcat(buf, "[blocks robot messages] ");
	if (mode & SILC_CHANNEL_UMODE_QUIET)
		strcat(buf, "[quieted] ");
	g_strchomp(buf);
}

void
silcpurple_parse_attrs(SilcDList attrs, char **moodstr, char **statusstr,
		       char **contactstr, char **langstr, char **devicestr,
		       char **tzstr, char **geostr)
{
	SilcAttributePayload attr;
	SilcAttributeMood mood = 0;
	SilcAttributeContact contact;
	SilcAttributeObjDevice device;
	SilcAttributeObjGeo geo;

	char tmp[1024];
	GString *s;

	*moodstr = NULL;
	*statusstr = NULL;
	*contactstr = NULL;
	*langstr = NULL;
	*devicestr = NULL;
	*tzstr = NULL;
	*geostr = NULL;

	if (!attrs)
		return;

	s = g_string_new("");
	attr = silcpurple_get_attr(attrs, SILC_ATTRIBUTE_STATUS_MOOD);
	if (attr && silc_attribute_get_object(attr, &mood, sizeof(mood))) {
		if (mood & SILC_ATTRIBUTE_MOOD_HAPPY)
			g_string_append_printf(s, "[%s] ", _("Happy"));
		if (mood & SILC_ATTRIBUTE_MOOD_SAD)
			g_string_append_printf(s, "[%s] ", _("Sad"));
		if (mood & SILC_ATTRIBUTE_MOOD_ANGRY)
			g_string_append_printf(s, "[%s] ", _("Angry"));
		if (mood & SILC_ATTRIBUTE_MOOD_JEALOUS)
			g_string_append_printf(s, "[%s] ", _("Jealous"));
		if (mood & SILC_ATTRIBUTE_MOOD_ASHAMED)
			g_string_append_printf(s, "[%s] ", _("Ashamed"));
		if (mood & SILC_ATTRIBUTE_MOOD_INVINCIBLE)
			g_string_append_printf(s, "[%s] ", _("Invincible"));
		if (mood & SILC_ATTRIBUTE_MOOD_INLOVE)
			g_string_append_printf(s, "[%s] ", _("In Love"));
		if (mood & SILC_ATTRIBUTE_MOOD_SLEEPY)
			g_string_append_printf(s, "[%s] ", _("Sleepy"));
		if (mood & SILC_ATTRIBUTE_MOOD_BORED)
			g_string_append_printf(s, "[%s] ", _("Bored"));
		if (mood & SILC_ATTRIBUTE_MOOD_EXCITED)
			g_string_append_printf(s, "[%s] ", _("Excited"));
		if (mood & SILC_ATTRIBUTE_MOOD_ANXIOUS)
			g_string_append_printf(s, "[%s] ", _("Anxious"));
	}
	if (*s->str != '\0') {
		*moodstr = g_string_free(s, FALSE);
		g_strchomp(*moodstr);
	} else
		g_string_free(s, TRUE);

	attr = silcpurple_get_attr(attrs, SILC_ATTRIBUTE_STATUS_FREETEXT);
	memset(tmp, 0, sizeof(tmp));
	if (attr && silc_attribute_get_object(attr, tmp, sizeof(tmp)))
		*statusstr = g_strdup(tmp);

	s = g_string_new("");
	attr = silcpurple_get_attr(attrs, SILC_ATTRIBUTE_PREFERRED_CONTACT);
	if (attr && silc_attribute_get_object(attr, &contact, sizeof(contact))) {
		if (contact & SILC_ATTRIBUTE_CONTACT_CHAT)
			g_string_append_printf(s, "[%s] ", _("Chat"));
		if (contact & SILC_ATTRIBUTE_CONTACT_EMAIL)
			g_string_append_printf(s, "[%s] ", _("Email"));
		if (contact & SILC_ATTRIBUTE_CONTACT_CALL)
			g_string_append_printf(s, "[%s] ", _("Phone"));
		if (contact & SILC_ATTRIBUTE_CONTACT_PAGE)
			g_string_append_printf(s, "[%s] ", _("Paging"));
		if (contact & SILC_ATTRIBUTE_CONTACT_SMS)
			g_string_append_printf(s, "[%s] ", _("SMS"));
		if (contact & SILC_ATTRIBUTE_CONTACT_MMS)
			g_string_append_printf(s, "[%s] ", _("MMS"));
		if (contact & SILC_ATTRIBUTE_CONTACT_VIDEO)
			g_string_append_printf(s, "[%s] ", _("Video Conferencing"));
	}
	if (*s->str != '\0') {
		*contactstr = g_string_free(s, FALSE);
		g_strchomp(*contactstr);
	} else
		g_string_free(s, TRUE);

	attr = silcpurple_get_attr(attrs, SILC_ATTRIBUTE_PREFERRED_LANGUAGE);
	memset(tmp, 0, sizeof(tmp));
	if (attr && silc_attribute_get_object(attr, tmp, sizeof(tmp)))
		*langstr = g_strdup(tmp);

	s = g_string_new("");
	attr = silcpurple_get_attr(attrs, SILC_ATTRIBUTE_DEVICE_INFO);
	memset(&device, 0, sizeof(device));
	if (attr && silc_attribute_get_object(attr, &device, sizeof(device))) {
		if (device.type == SILC_ATTRIBUTE_DEVICE_COMPUTER)
			g_string_append_printf(s, "%s: ", _("Computer"));
		if (device.type == SILC_ATTRIBUTE_DEVICE_MOBILE_PHONE)
			g_string_append_printf(s, "%s: ", _("Mobile Phone"));
		if (device.type == SILC_ATTRIBUTE_DEVICE_PDA)
			g_string_append_printf(s, "%s: ", _("PDA"));
		if (device.type == SILC_ATTRIBUTE_DEVICE_TERMINAL)
			g_string_append_printf(s, "%s: ", _("Terminal"));
		g_string_append_printf(s, "%s %s %s %s",
				device.manufacturer ? device.manufacturer : "",
				device.version ? device.version : "",
				device.model ? device.model : "",
				device.language ? device.language : "");
	}
	if (*s->str != '\0')
		*devicestr = g_string_free(s, FALSE);
	else
		g_string_free(s, TRUE);

	attr = silcpurple_get_attr(attrs, SILC_ATTRIBUTE_TIMEZONE);
	memset(tmp, 0, sizeof(tmp));
	if (attr && silc_attribute_get_object(attr, tmp, sizeof(tmp)))
		*tzstr = g_strdup(tmp);

	attr = silcpurple_get_attr(attrs, SILC_ATTRIBUTE_GEOLOCATION);
	memset(&geo, 0, sizeof(geo));
	if (attr && silc_attribute_get_object(attr, &geo, sizeof(geo)))
		*geostr = g_strdup_printf("%s %s %s (%s)",
				geo.longitude ? geo.longitude : "",
				geo.latitude ? geo.latitude : "",
				geo.altitude ? geo.altitude : "",
				geo.accuracy ? geo.accuracy : "");
}

/* Checks if message has images, and assembles MIME message if it has.
   If only one image is present, creates simple MIME image message.  If
   there are multiple images and/or text with images multipart MIME
   message is created. */

SilcDList silcpurple_image_message(const char *msg, SilcMessageFlags *mflags)
{
	SilcMime mime = NULL, p;
	SilcDList list, parts = NULL;
	const char *start, *end, *last;
	GData *attribs;
	gboolean images = FALSE;

	last = msg;
	while (last && *last && purple_markup_find_tag("img", last, &start,
						     &end, &attribs)) {
		PurpleImage *image = NULL;
		const gchar *uri;

		/* Check if there is text before image */
		if (start - last) {
			char *text, *tmp;
			p = silc_mime_alloc();

			/* Add content type */
			silc_mime_add_field(p, "Content-Type",
					    "text/plain; charset=utf-8");

			tmp = g_strndup(last, start - last);
			text = purple_unescape_html(tmp);
			g_free(tmp);

			/* Add text */
			silc_mime_add_data(p, (const unsigned char *)text, strlen(text));
			g_free(text);

			if (!parts)
				parts = silc_dlist_init();
			silc_dlist_add(parts, p);
		}

		uri = g_datalist_get_data(&attribs, "src");
		if (uri)
			image = purple_image_store_get_from_uri(uri);
		if (uri) {
			unsigned long imglen = purple_image_get_data_size(image);
			gconstpointer img = purple_image_get_data(image);
			const gchar *type;

			p = silc_mime_alloc();

			/* Add content type */
			type = purple_image_get_mimetype(image);
			if (!type) {
				g_datalist_clear(&attribs);
				last = end + 1;
				continue;
			}
			silc_mime_add_field(p, "Content-Type", type);

			/* Add content transfer encoding */
			silc_mime_add_field(p, "Content-Transfer-Encoding", "binary");

			/* Add image data */
			silc_mime_add_data(p, img, imglen);

			if (!parts)
				parts = silc_dlist_init();
			silc_dlist_add(parts, p);
			images = TRUE;
		}

		g_datalist_clear(&attribs);

		/* Continue after tag */
		last = end + 1;
	}

	/* Check for text after the image(s) */
	if (images && last && *last) {
		char *tmp = purple_unescape_html(last);
		p = silc_mime_alloc();

		/* Add content type */
		silc_mime_add_field(p, "Content-Type",
				    "text/plain; charset=utf-8");

		/* Add text */
		silc_mime_add_data(p, (const unsigned char *)tmp, strlen(tmp));
		g_free(tmp);

		if (!parts)
			parts = silc_dlist_init();
		silc_dlist_add(parts, p);
	}

	/* If there weren't any images, don't return anything. */
	if (!images) {
		if (parts)
			silc_dlist_uninit(parts);
		return NULL;
	}

	if (silc_dlist_count(parts) > 1) {
		/* Multipart MIME message */
		char b[32];
		mime = silc_mime_alloc();
		silc_mime_add_field(mime, "MIME-Version", "1.0");
		g_snprintf(b, sizeof(b), "b%4X%4X",
			   (unsigned int)time(NULL),
			   silc_dlist_count(parts));
		silc_mime_set_multipart(mime, "mixed", b);
		silc_dlist_start(parts);
		while ((p = silc_dlist_get(parts)) != SILC_LIST_END)
			silc_mime_add_multipart(mime, p);
	} else {
		/* Simple MIME message */
		silc_dlist_start(parts);
		mime = silc_dlist_get(parts);
		silc_mime_add_field(mime, "MIME-Version", "1.0");
	}

	*mflags &= ~SILC_MESSAGE_FLAG_UTF8;
	*mflags |= SILC_MESSAGE_FLAG_DATA;

	/* Encode message. Fragment if it is too large */
	list = silc_mime_encode_partial(mime, 0xfc00);

	silc_dlist_uninit(parts);

	/* Added multiparts gets freed here */
	silc_mime_free(mime);

	return list;
}