Mercurial > pidgin
annotate src/protocols/silc/util.c @ 13890:2bac009eaa0c
[gaim-migrate @ 16372]
Some comments changes, and:
1. Don't buddy icons in Jabber
2. When clearing your buddy icon in oscar, do it the same
way that WinAIM does it
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Thu, 29 Jun 2006 08:14:29 +0000 |
parents | a92263b13380 |
children | 545dbc931e8c |
rev | line source |
---|---|
8849 | 1 /* |
2 | |
3 silcgaim_util.c | |
4 | |
5 Author: Pekka Riikonen <priikone@silcnet.org> | |
6 | |
10825 | 7 Copyright (C) 2004 - 2005 Pekka Riikonen |
8849 | 8 |
9 This program is free software; you can redistribute it and/or modify | |
10 it under the terms of the GNU General Public License as published by | |
11 the Free Software Foundation; version 2 of the License. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU General Public License for more details. | |
17 | |
18 */ | |
19 | |
20 #include "silcincludes.h" | |
21 #include "silcclient.h" | |
22 #include "silcgaim.h" | |
12217 | 23 #include "imgstore.h" |
8849 | 24 |
25 /**************************** Utility Routines *******************************/ | |
26 | |
27 static char str[256], str2[256]; | |
28 | |
29 const char *silcgaim_silcdir(void) | |
30 { | |
31 const char *hd = gaim_home_dir(); | |
32 memset(str, 0, sizeof(str)); | |
33 g_snprintf(str, sizeof(str) - 1, "%s" G_DIR_SEPARATOR_S ".silc", hd ? hd : "/tmp"); | |
34 return (const char *)str; | |
35 } | |
36 | |
37 const char *silcgaim_session_file(const char *account) | |
38 { | |
39 memset(str2, 0, sizeof(str2)); | |
40 g_snprintf(str2, sizeof(str2) - 1, "%s" G_DIR_SEPARATOR_S "%s_session", | |
41 silcgaim_silcdir(), account); | |
42 return (const char *)str2; | |
43 } | |
44 | |
45 gboolean silcgaim_ip_is_private(const char *ip) | |
46 { | |
47 if (silc_net_is_ip4(ip)) { | |
48 if (!strncmp(ip, "10.", 3)) { | |
49 return TRUE; | |
50 } else if (!strncmp(ip, "172.", 4) && strlen(ip) > 6) { | |
51 char tmp[3]; | |
8910 | 52 int s; |
8849 | 53 memset(tmp, 0, sizeof(tmp)); |
54 strncpy(tmp, ip + 4, 2); | |
8910 | 55 s = atoi(tmp); |
8849 | 56 if (s >= 16 && s <= 31) |
57 return TRUE; | |
58 } else if (!strncmp(ip, "192.168.", 8)) { | |
59 return TRUE; | |
60 } | |
61 } | |
62 | |
63 return FALSE; | |
64 } | |
65 | |
66 /* This checks stats for various SILC files and directories. First it | |
67 checks if ~/.silc directory exist and is owned by the correct user. If | |
68 it doesn't exist, it will create the directory. After that it checks if | |
69 user's Public and Private key files exists and creates them if needed. */ | |
70 | |
71 gboolean silcgaim_check_silc_dir(GaimConnection *gc) | |
72 { | |
73 char filename[256], file_public_key[256], file_private_key[256]; | |
74 char servfilename[256], clientfilename[256], friendsfilename[256]; | |
10825 | 75 char pkd[256], prd[256]; |
8849 | 76 struct stat st; |
77 struct passwd *pw; | |
13659 | 78 int fd; |
8849 | 79 |
80 pw = getpwuid(getuid()); | |
81 if (!pw) { | |
9272 | 82 gaim_debug_error("silc", "silc: %s\n", strerror(errno)); |
8849 | 83 return FALSE; |
84 } | |
85 | |
9353 | 86 g_snprintf(filename, sizeof(filename) - 1, "%s", silcgaim_silcdir()); |
8849 | 87 g_snprintf(servfilename, sizeof(servfilename) - 1, "%s" G_DIR_SEPARATOR_S "serverkeys", |
88 silcgaim_silcdir()); | |
89 g_snprintf(clientfilename, sizeof(clientfilename) - 1, "%s" G_DIR_SEPARATOR_S "clientkeys", | |
90 silcgaim_silcdir()); | |
91 g_snprintf(friendsfilename, sizeof(friendsfilename) - 1, "%s" G_DIR_SEPARATOR_S "friends", | |
92 silcgaim_silcdir()); | |
93 | |
94 /* | |
95 * Check ~/.silc directory | |
96 */ | |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
9488
diff
changeset
|
97 if ((g_stat(filename, &st)) == -1) { |
8849 | 98 /* If dir doesn't exist */ |
99 if (errno == ENOENT) { | |
100 if (pw->pw_uid == geteuid()) { | |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
9488
diff
changeset
|
101 if ((g_mkdir(filename, 0755)) == -1) { |
9272 | 102 gaim_debug_error("silc", "Couldn't create '%s' directory\n", filename); |
8849 | 103 return FALSE; |
104 } | |
105 } else { | |
9272 | 106 gaim_debug_error("silc", "Couldn't create '%s' directory due to a wrong uid!\n", |
8849 | 107 filename); |
108 return FALSE; | |
109 } | |
110 } else { | |
9272 | 111 gaim_debug_error("silc", "Couldn't stat '%s' directory, error: %s\n", filename, strerror(errno)); |
8849 | 112 return FALSE; |
113 } | |
114 } else { | |
9353 | 115 #ifndef _WIN32 |
8849 | 116 /* Check the owner of the dir */ |
117 if (st.st_uid != 0 && st.st_uid != pw->pw_uid) { | |
9272 | 118 gaim_debug_error("silc", "You don't seem to own '%s' directory\n", |
8849 | 119 filename); |
120 return FALSE; | |
121 } | |
9353 | 122 #endif |
8849 | 123 } |
124 | |
125 /* | |
126 * Check ~./silc/serverkeys directory | |
127 */ | |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
9488
diff
changeset
|
128 if ((g_stat(servfilename, &st)) == -1) { |
8849 | 129 /* If dir doesn't exist */ |
130 if (errno == ENOENT) { | |
131 if (pw->pw_uid == geteuid()) { | |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
9488
diff
changeset
|
132 if ((g_mkdir(servfilename, 0755)) == -1) { |
9272 | 133 gaim_debug_error("silc", "Couldn't create '%s' directory\n", servfilename); |
8849 | 134 return FALSE; |
135 } | |
136 } else { | |
9272 | 137 gaim_debug_error("silc", "Couldn't create '%s' directory due to a wrong uid!\n", |
8849 | 138 servfilename); |
139 return FALSE; | |
140 } | |
141 } else { | |
9272 | 142 gaim_debug_error("silc", "Couldn't stat '%s' directory, error: %s\n", |
143 servfilename, strerror(errno)); | |
8849 | 144 return FALSE; |
145 } | |
146 } | |
147 | |
148 /* | |
149 * Check ~./silc/clientkeys directory | |
150 */ | |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
9488
diff
changeset
|
151 if ((g_stat(clientfilename, &st)) == -1) { |
8849 | 152 /* If dir doesn't exist */ |
153 if (errno == ENOENT) { | |
154 if (pw->pw_uid == geteuid()) { | |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
9488
diff
changeset
|
155 if ((g_mkdir(clientfilename, 0755)) == -1) { |
9272 | 156 gaim_debug_error("silc", "Couldn't create '%s' directory\n", clientfilename); |
8849 | 157 return FALSE; |
158 } | |
159 } else { | |
9272 | 160 gaim_debug_error("silc", "Couldn't create '%s' directory due to a wrong uid!\n", |
8849 | 161 clientfilename); |
162 return FALSE; | |
163 } | |
164 } else { | |
9272 | 165 gaim_debug_error("silc", "Couldn't stat '%s' directory, error: %s\n", |
166 clientfilename, strerror(errno)); | |
8849 | 167 return FALSE; |
168 } | |
169 } | |
170 | |
171 /* | |
172 * Check ~./silc/friends directory | |
173 */ | |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
9488
diff
changeset
|
174 if ((g_stat(friendsfilename, &st)) == -1) { |
8849 | 175 /* If dir doesn't exist */ |
176 if (errno == ENOENT) { | |
177 if (pw->pw_uid == geteuid()) { | |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
9488
diff
changeset
|
178 if ((g_mkdir(friendsfilename, 0755)) == -1) { |
9272 | 179 gaim_debug_error("silc", "Couldn't create '%s' directory\n", friendsfilename); |
8849 | 180 return FALSE; |
181 } | |
182 } else { | |
9272 | 183 gaim_debug_error("silc", "Couldn't create '%s' directory due to a wrong uid!\n", |
8849 | 184 friendsfilename); |
185 return FALSE; | |
186 } | |
187 } else { | |
9272 | 188 gaim_debug_error("silc", "Couldn't stat '%s' directory, error: %s\n", |
189 friendsfilename, strerror(errno)); | |
8849 | 190 return FALSE; |
191 } | |
192 } | |
193 | |
194 /* | |
195 * Check Public and Private keys | |
196 */ | |
10825 | 197 g_snprintf(pkd, sizeof(pkd), "%s" G_DIR_SEPARATOR_S "public_key.pub", silcgaim_silcdir()); |
198 g_snprintf(prd, sizeof(prd), "%s" G_DIR_SEPARATOR_S "private_key.prv", silcgaim_silcdir()); | |
8849 | 199 g_snprintf(file_public_key, sizeof(file_public_key) - 1, "%s", |
10825 | 200 gaim_account_get_string(gc->account, "public-key", pkd)); |
8849 | 201 g_snprintf(file_private_key, sizeof(file_public_key) - 1, "%s", |
10825 | 202 gaim_account_get_string(gc->account, "private-key", prd)); |
8849 | 203 |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
9488
diff
changeset
|
204 if ((g_stat(file_public_key, &st)) == -1) { |
8849 | 205 /* If file doesn't exist */ |
206 if (errno == ENOENT) { | |
207 gaim_connection_update_progress(gc, _("Creating SILC key pair..."), 1, 5); | |
208 silc_create_key_pair(SILCGAIM_DEF_PKCS, | |
209 SILCGAIM_DEF_PKCS_LEN, | |
210 file_public_key, file_private_key, NULL, | |
10751 | 211 (gc->password == NULL) ? "" : gc->password, |
9272 | 212 NULL, NULL, NULL, FALSE); |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
9488
diff
changeset
|
213 g_stat(file_public_key, &st); |
8849 | 214 } else { |
9272 | 215 gaim_debug_error("silc", "Couldn't stat '%s' public key, error: %s\n", |
216 file_public_key, strerror(errno)); | |
8849 | 217 return FALSE; |
218 } | |
219 } | |
220 | |
9353 | 221 #ifndef _WIN32 |
8849 | 222 /* Check the owner of the public key */ |
223 if (st.st_uid != 0 && st.st_uid != pw->pw_uid) { | |
9272 | 224 gaim_debug_error("silc", "You don't seem to own your public key!?\n"); |
8849 | 225 return FALSE; |
226 } | |
9353 | 227 #endif |
8849 | 228 |
13659 | 229 fd = open(file_private_key, O_RDONLY); |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
9488
diff
changeset
|
230 if ((g_stat(file_private_key, &st)) == -1) { |
8849 | 231 /* If file doesn't exist */ |
232 if (errno == ENOENT) { | |
233 gaim_connection_update_progress(gc, _("Creating SILC key pair..."), 1, 5); | |
234 silc_create_key_pair(SILCGAIM_DEF_PKCS, | |
235 SILCGAIM_DEF_PKCS_LEN, | |
236 file_public_key, file_private_key, NULL, | |
10751 | 237 (gc->password == NULL) ? "" : gc->password, |
9272 | 238 NULL, NULL, NULL, FALSE); |
13659 | 239 if (fd != -1) |
240 close(fd); | |
241 fd = open(file_private_key, O_RDONLY); | |
10589
0f7452b1f777
[gaim-migrate @ 11994]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
9488
diff
changeset
|
242 g_stat(file_private_key, &st); |
8849 | 243 } else { |
9272 | 244 gaim_debug_error("silc", "Couldn't stat '%s' private key, error: %s\n", |
245 file_private_key, strerror(errno)); | |
13659 | 246 if (fd != -1) |
247 close(fd); | |
8849 | 248 return FALSE; |
249 } | |
250 } | |
251 | |
9353 | 252 #ifndef _WIN32 |
8849 | 253 /* Check the owner of the private key */ |
254 if (st.st_uid != 0 && st.st_uid != pw->pw_uid) { | |
9272 | 255 gaim_debug_error("silc", "You don't seem to own your private key!?\n"); |
13659 | 256 if (fd != -1) |
257 close(fd); | |
8849 | 258 return FALSE; |
259 } | |
260 | |
261 /* Check the permissions for the private key */ | |
262 if ((st.st_mode & 0777) != 0600) { | |
9272 | 263 gaim_debug_warning("silc", "Wrong permissions in your private key file `%s'!\n" |
13659 | 264 "Trying to change them ...\n", file_private_key); |
265 if ((fd != -1) && (fchmod(fd, S_IRUSR | S_IWUSR)) == -1) { | |
9272 | 266 gaim_debug_error("silc", |
8849 | 267 "Failed to change permissions for private key file!\n" |
268 "Permissions for your private key file must be 0600.\n"); | |
13659 | 269 if (fd != -1) |
270 close(fd); | |
8849 | 271 return FALSE; |
272 } | |
9272 | 273 gaim_debug_warning("silc", "Done.\n\n"); |
8849 | 274 } |
9353 | 275 #endif |
8849 | 276 |
13659 | 277 if (fd != -1) |
278 close(fd); | |
279 | |
8849 | 280 return TRUE; |
281 } | |
282 | |
9353 | 283 #ifdef _WIN32 |
284 struct passwd *getpwuid(uid_t uid) { | |
285 struct passwd *pwd = calloc(1, sizeof(struct passwd)); | |
286 return pwd; | |
287 } | |
288 | |
289 uid_t getuid() { | |
290 return 0; | |
291 } | |
292 | |
293 uid_t geteuid() { | |
294 return 0; | |
295 } | |
296 #endif | |
297 | |
8849 | 298 void silcgaim_show_public_key(SilcGaim sg, |
299 const char *name, SilcPublicKey public_key, | |
300 GCallback callback, void *context) | |
301 { | |
302 SilcPublicKeyIdentifier ident; | |
303 SilcPKCS pkcs; | |
304 char *fingerprint, *babbleprint; | |
305 unsigned char *pk; | |
306 SilcUInt32 pk_len, key_len = 0; | |
307 GString *s; | |
308 char *buf; | |
309 | |
310 ident = silc_pkcs_decode_identifier(public_key->identifier); | |
311 if (!ident) | |
312 return; | |
313 | |
314 pk = silc_pkcs_public_key_encode(public_key, &pk_len); | |
315 fingerprint = silc_hash_fingerprint(NULL, pk, pk_len); | |
316 babbleprint = silc_hash_babbleprint(NULL, pk, pk_len); | |
317 | |
11488 | 318 if (silc_pkcs_alloc((unsigned char *)public_key->name, &pkcs)) { |
8849 | 319 key_len = silc_pkcs_public_key_set(pkcs, public_key); |
320 silc_pkcs_free(pkcs); | |
321 } | |
322 | |
323 s = g_string_new(""); | |
324 if (ident->realname) | |
10825 | 325 /* Hint for translators: Please check the tabulator width here and in |
326 the next strings (short strings: 2 tabs, longer strings 1 tab, | |
9274 | 327 sum: 3 tabs or 24 characters) */ |
328 g_string_append_printf(s, _("Real Name: \t%s\n"), ident->realname); | |
8849 | 329 if (ident->username) |
9274 | 330 g_string_append_printf(s, _("User Name: \t%s\n"), ident->username); |
8849 | 331 if (ident->email) |
13545
cfc2f7fcb3dd
[gaim-migrate @ 15922]
Richard Laager <rlaager@wiktel.com>
parents:
12217
diff
changeset
|
332 g_string_append_printf(s, _("E-Mail: \t\t%s\n"), ident->email); |
8849 | 333 if (ident->host) |
9274 | 334 g_string_append_printf(s, _("Host Name: \t%s\n"), ident->host); |
8849 | 335 if (ident->org) |
9274 | 336 g_string_append_printf(s, _("Organization: \t%s\n"), ident->org); |
8849 | 337 if (ident->country) |
9274 | 338 g_string_append_printf(s, _("Country: \t%s\n"), ident->country); |
9488 | 339 g_string_append_printf(s, _("Algorithm: \t%s\n"), public_key->name); |
9274 | 340 g_string_append_printf(s, _("Key Length: \t%d bits\n"), (int)key_len); |
341 g_string_append_printf(s, "\n"); | |
342 g_string_append_printf(s, _("Public Key Fingerprint:\n%s\n\n"), fingerprint); | |
343 g_string_append_printf(s, _("Public Key Babbleprint:\n%s"), babbleprint); | |
8849 | 344 |
345 buf = g_string_free(s, FALSE); | |
346 | |
11201 | 347 gaim_request_action(sg->gc, _("Public Key Information"), |
8849 | 348 _("Public Key Information"), |
349 buf, 0, context, 1, | |
350 _("Close"), callback); | |
351 | |
352 g_free(buf); | |
353 silc_free(fingerprint); | |
354 silc_free(babbleprint); | |
355 silc_free(pk); | |
356 silc_pkcs_free_identifier(ident); | |
357 } | |
358 | |
359 SilcAttributePayload | |
360 silcgaim_get_attr(SilcDList attrs, SilcAttribute attribute) | |
361 { | |
362 SilcAttributePayload attr = NULL; | |
363 | |
364 if (!attrs) | |
365 return NULL; | |
366 | |
367 silc_dlist_start(attrs); | |
368 while ((attr = silc_dlist_get(attrs)) != SILC_LIST_END) | |
369 if (attribute == silc_attribute_get_attribute(attr)) | |
370 break; | |
371 | |
372 return attr; | |
373 } | |
374 | |
375 void silcgaim_get_umode_string(SilcUInt32 mode, char *buf, | |
376 SilcUInt32 buf_size) | |
377 { | |
378 memset(buf, 0, buf_size); | |
379 if ((mode & SILC_UMODE_SERVER_OPERATOR) || | |
380 (mode & SILC_UMODE_ROUTER_OPERATOR)) { | |
381 strcat(buf, (mode & SILC_UMODE_SERVER_OPERATOR) ? | |
382 "[server operator] " : | |
383 (mode & SILC_UMODE_ROUTER_OPERATOR) ? | |
384 "[SILC operator] " : "[unknown mode] "); | |
385 } | |
386 if (mode & SILC_UMODE_GONE) | |
387 strcat(buf, "[away] "); | |
388 if (mode & SILC_UMODE_INDISPOSED) | |
389 strcat(buf, "[indisposed] "); | |
390 if (mode & SILC_UMODE_BUSY) | |
391 strcat(buf, "[busy] "); | |
392 if (mode & SILC_UMODE_PAGE) | |
393 strcat(buf, "[wake me up] "); | |
394 if (mode & SILC_UMODE_HYPER) | |
395 strcat(buf, "[hyperactive] "); | |
396 if (mode & SILC_UMODE_ROBOT) | |
397 strcat(buf, "[robot] "); | |
398 if (mode & SILC_UMODE_ANONYMOUS) | |
399 strcat(buf, "[anonymous] "); | |
400 if (mode & SILC_UMODE_BLOCK_PRIVMSG) | |
401 strcat(buf, "[blocks private messages] "); | |
402 if (mode & SILC_UMODE_DETACHED) | |
403 strcat(buf, "[detached] "); | |
404 if (mode & SILC_UMODE_REJECT_WATCHING) | |
405 strcat(buf, "[rejects watching] "); | |
406 if (mode & SILC_UMODE_BLOCK_INVITE) | |
407 strcat(buf, "[blocks invites] "); | |
408 } | |
409 | |
410 void silcgaim_get_chmode_string(SilcUInt32 mode, char *buf, | |
411 SilcUInt32 buf_size) | |
412 { | |
413 memset(buf, 0, buf_size); | |
414 if (mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) | |
415 strcat(buf, "[permanent] "); | |
416 if (mode & SILC_CHANNEL_MODE_PRIVATE) | |
417 strcat(buf, "[private] "); | |
418 if (mode & SILC_CHANNEL_MODE_SECRET) | |
419 strcat(buf, "[secret] "); | |
420 if (mode & SILC_CHANNEL_MODE_SECRET) | |
421 strcat(buf, "[secret] "); | |
422 if (mode & SILC_CHANNEL_MODE_PRIVKEY) | |
423 strcat(buf, "[private key] "); | |
424 if (mode & SILC_CHANNEL_MODE_INVITE) | |
425 strcat(buf, "[invite only] "); | |
426 if (mode & SILC_CHANNEL_MODE_TOPIC) | |
427 strcat(buf, "[topic restricted] "); | |
428 if (mode & SILC_CHANNEL_MODE_ULIMIT) | |
429 strcat(buf, "[user count limit] "); | |
430 if (mode & SILC_CHANNEL_MODE_PASSPHRASE) | |
431 strcat(buf, "[passphrase auth] "); | |
432 if (mode & SILC_CHANNEL_MODE_CHANNEL_AUTH) | |
433 strcat(buf, "[public key auth] "); | |
434 if (mode & SILC_CHANNEL_MODE_SILENCE_USERS) | |
435 strcat(buf, "[users silenced] "); | |
436 if (mode & SILC_CHANNEL_MODE_SILENCE_OPERS) | |
437 strcat(buf, "[operators silenced] "); | |
438 } | |
439 | |
440 void silcgaim_get_chumode_string(SilcUInt32 mode, char *buf, | |
441 SilcUInt32 buf_size) | |
442 { | |
443 memset(buf, 0, buf_size); | |
444 if (mode & SILC_CHANNEL_UMODE_CHANFO) | |
445 strcat(buf, "[founder] "); | |
446 if (mode & SILC_CHANNEL_UMODE_CHANOP) | |
447 strcat(buf, "[operator] "); | |
448 if (mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES) | |
449 strcat(buf, "[blocks messages] "); | |
450 if (mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS) | |
451 strcat(buf, "[blocks user messages] "); | |
452 if (mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS) | |
453 strcat(buf, "[blocks robot messages] "); | |
454 if (mode & SILC_CHANNEL_UMODE_QUIET) | |
455 strcat(buf, "[quieted] "); | |
456 } | |
9488 | 457 |
458 void | |
459 silcgaim_parse_attrs(SilcDList attrs, char **moodstr, char **statusstr, | |
460 char **contactstr, char **langstr, char **devicestr, | |
461 char **tzstr, char **geostr) | |
462 { | |
463 SilcAttributePayload attr; | |
464 SilcAttributeMood mood = 0; | |
465 SilcAttributeContact contact; | |
466 SilcAttributeObjDevice device; | |
467 SilcAttributeObjGeo geo; | |
468 | |
469 char tmp[1024]; | |
470 GString *s; | |
471 | |
472 *moodstr = NULL; | |
473 *statusstr = NULL; | |
474 *contactstr = NULL; | |
475 *langstr = NULL; | |
476 *devicestr = NULL; | |
477 *tzstr = NULL; | |
478 *geostr = NULL; | |
479 | |
480 if (!attrs) | |
481 return; | |
482 | |
483 s = g_string_new(""); | |
484 attr = silcgaim_get_attr(attrs, SILC_ATTRIBUTE_STATUS_MOOD); | |
485 if (attr && silc_attribute_get_object(attr, &mood, sizeof(mood))) { | |
486 if (mood & SILC_ATTRIBUTE_MOOD_HAPPY) | |
487 g_string_append_printf(s, "[%s] ", _("Happy")); | |
488 if (mood & SILC_ATTRIBUTE_MOOD_SAD) | |
489 g_string_append_printf(s, "[%s] ", _("Sad")); | |
490 if (mood & SILC_ATTRIBUTE_MOOD_ANGRY) | |
491 g_string_append_printf(s, "[%s] ", _("Angry")); | |
492 if (mood & SILC_ATTRIBUTE_MOOD_JEALOUS) | |
493 g_string_append_printf(s, "[%s] ", _("Jealous")); | |
494 if (mood & SILC_ATTRIBUTE_MOOD_ASHAMED) | |
495 g_string_append_printf(s, "[%s] ", _("Ashamed")); | |
496 if (mood & SILC_ATTRIBUTE_MOOD_INVINCIBLE) | |
497 g_string_append_printf(s, "[%s] ", _("Invincible")); | |
498 if (mood & SILC_ATTRIBUTE_MOOD_INLOVE) | |
499 g_string_append_printf(s, "[%s] ", _("In Love")); | |
500 if (mood & SILC_ATTRIBUTE_MOOD_SLEEPY) | |
501 g_string_append_printf(s, "[%s] ", _("Sleepy")); | |
502 if (mood & SILC_ATTRIBUTE_MOOD_BORED) | |
503 g_string_append_printf(s, "[%s] ", _("Bored")); | |
504 if (mood & SILC_ATTRIBUTE_MOOD_EXCITED) | |
505 g_string_append_printf(s, "[%s] ", _("Excited")); | |
506 if (mood & SILC_ATTRIBUTE_MOOD_ANXIOUS) | |
507 g_string_append_printf(s, "[%s] ", _("Anxious")); | |
508 } | |
509 if (strlen(s->str)) { | |
510 *moodstr = s->str; | |
511 g_string_free(s, FALSE); | |
512 } else | |
513 g_string_free(s, TRUE); | |
514 | |
515 attr = silcgaim_get_attr(attrs, SILC_ATTRIBUTE_STATUS_FREETEXT); | |
516 memset(tmp, 0, sizeof(tmp)); | |
517 if (attr && silc_attribute_get_object(attr, tmp, sizeof(tmp))) | |
518 *statusstr = g_strdup(tmp); | |
519 | |
520 s = g_string_new(""); | |
521 attr = silcgaim_get_attr(attrs, SILC_ATTRIBUTE_PREFERRED_CONTACT); | |
522 if (attr && silc_attribute_get_object(attr, &contact, sizeof(contact))) { | |
523 if (contact & SILC_ATTRIBUTE_CONTACT_CHAT) | |
524 g_string_append_printf(s, "[%s] ", _("Chat")); | |
525 if (contact & SILC_ATTRIBUTE_CONTACT_EMAIL) | |
13545
cfc2f7fcb3dd
[gaim-migrate @ 15922]
Richard Laager <rlaager@wiktel.com>
parents:
12217
diff
changeset
|
526 g_string_append_printf(s, "[%s] ", _("E-Mail")); |
9488 | 527 if (contact & SILC_ATTRIBUTE_CONTACT_CALL) |
528 g_string_append_printf(s, "[%s] ", _("Phone")); | |
529 if (contact & SILC_ATTRIBUTE_CONTACT_PAGE) | |
530 g_string_append_printf(s, "[%s] ", _("Paging")); | |
531 if (contact & SILC_ATTRIBUTE_CONTACT_SMS) | |
532 g_string_append_printf(s, "[%s] ", _("SMS")); | |
533 if (contact & SILC_ATTRIBUTE_CONTACT_MMS) | |
534 g_string_append_printf(s, "[%s] ", _("MMS")); | |
535 if (contact & SILC_ATTRIBUTE_CONTACT_VIDEO) | |
536 g_string_append_printf(s, "[%s] ", _("Video Conferencing")); | |
537 } | |
538 if (strlen(s->str)) { | |
539 *contactstr = s->str; | |
540 g_string_free(s, FALSE); | |
541 } else | |
542 g_string_free(s, TRUE); | |
543 | |
544 attr = silcgaim_get_attr(attrs, SILC_ATTRIBUTE_PREFERRED_LANGUAGE); | |
545 memset(tmp, 0, sizeof(tmp)); | |
546 if (attr && silc_attribute_get_object(attr, tmp, sizeof(tmp))) | |
547 *langstr = g_strdup(tmp); | |
548 | |
549 s = g_string_new(""); | |
550 attr = silcgaim_get_attr(attrs, SILC_ATTRIBUTE_DEVICE_INFO); | |
551 memset(&device, 0, sizeof(device)); | |
552 if (attr && silc_attribute_get_object(attr, &device, sizeof(device))) { | |
553 if (device.type == SILC_ATTRIBUTE_DEVICE_COMPUTER) | |
554 g_string_append_printf(s, "%s: ", _("Computer")); | |
555 if (device.type == SILC_ATTRIBUTE_DEVICE_MOBILE_PHONE) | |
556 g_string_append_printf(s, "%s: ", _("Mobile Phone")); | |
557 if (device.type == SILC_ATTRIBUTE_DEVICE_PDA) | |
558 g_string_append_printf(s, "%s: ", _("PDA")); | |
559 if (device.type == SILC_ATTRIBUTE_DEVICE_TERMINAL) | |
560 g_string_append_printf(s, "%s: ", _("Terminal")); | |
561 g_string_append_printf(s, "%s %s %s %s", | |
562 device.manufacturer ? device.manufacturer : "", | |
563 device.version ? device.version : "", | |
564 device.model ? device.model : "", | |
565 device.language ? device.language : ""); | |
566 } | |
567 if (strlen(s->str)) { | |
568 *devicestr = s->str; | |
569 g_string_free(s, FALSE); | |
570 } else | |
571 g_string_free(s, TRUE); | |
572 | |
573 attr = silcgaim_get_attr(attrs, SILC_ATTRIBUTE_TIMEZONE); | |
574 memset(tmp, 0, sizeof(tmp)); | |
575 if (attr && silc_attribute_get_object(attr, tmp, sizeof(tmp))) | |
576 *tzstr = g_strdup(tmp); | |
577 | |
578 attr = silcgaim_get_attr(attrs, SILC_ATTRIBUTE_GEOLOCATION); | |
579 memset(&geo, 0, sizeof(geo)); | |
580 if (attr && silc_attribute_get_object(attr, &geo, sizeof(geo))) | |
581 *geostr = g_strdup_printf("%s %s %s (%s)", | |
582 geo.longitude ? geo.longitude : "", | |
583 geo.latitude ? geo.latitude : "", | |
584 geo.altitude ? geo.altitude : "", | |
585 geo.accuracy ? geo.accuracy : ""); | |
586 } | |
12217 | 587 |
588 #ifdef HAVE_SILCMIME_H | |
589 /* Returns MIME type of filetype */ | |
590 | |
591 char *silcgaim_file2mime(const char *filename) | |
592 { | |
593 const char *ct; | |
594 | |
595 ct = strrchr(filename, '.'); | |
596 if (!ct) | |
597 return NULL; | |
598 else if (!strcasecmp(".png", ct)) | |
599 return strdup("image/png"); | |
600 else if (!strcasecmp(".jpg", ct)) | |
601 return strdup("image/jpeg"); | |
602 else if (!strcasecmp(".jpeg", ct)) | |
603 return strdup("image/jpeg"); | |
604 else if (!strcasecmp(".gif", ct)) | |
605 return strdup("image/gif"); | |
606 else if (!strcasecmp(".tiff", ct)) | |
607 return strdup("image/tiff"); | |
608 | |
609 return NULL; | |
610 } | |
611 | |
612 /* Checks if message has images, and assembles MIME message if it has. | |
613 If only one image is present, creates simple MIME image message. If | |
614 there are multiple images and/or text with images multipart MIME | |
615 message is created. */ | |
616 | |
617 SilcDList silcgaim_image_message(const char *msg, SilcUInt32 *mflags) | |
618 { | |
619 SilcMime mime = NULL, p; | |
620 SilcDList list, parts = NULL; | |
621 const char *start, *end, *last; | |
622 GData *attribs; | |
623 char *type; | |
624 gboolean images = FALSE; | |
625 | |
626 last = msg; | |
627 while (last && *last && gaim_markup_find_tag("img", last, &start, | |
628 &end, &attribs)) { | |
629 GaimStoredImage *image = NULL; | |
630 const char *id; | |
631 | |
632 /* Check if there is text before image */ | |
633 if (start - last) { | |
634 char *text, *tmp; | |
635 p = silc_mime_alloc(); | |
636 | |
637 /* Add content type */ | |
638 silc_mime_add_field(p, "Content-Type", | |
639 "text/plain; charset=utf-8"); | |
640 | |
641 tmp = g_strndup(last, start - last); | |
642 text = gaim_unescape_html(tmp); | |
643 g_free(tmp); | |
644 /* Add text */ | |
645 silc_mime_add_data(p, text, strlen(text)); | |
646 g_free(text); | |
647 | |
648 if (!parts) | |
649 parts = silc_dlist_init(); | |
650 silc_dlist_add(parts, p); | |
651 } | |
652 | |
653 id = g_datalist_get_data(&attribs, "id"); | |
654 if (id && (image = gaim_imgstore_get(atoi(id)))) { | |
655 unsigned long imglen = gaim_imgstore_get_size(image); | |
656 gpointer img = gaim_imgstore_get_data(image); | |
657 | |
658 p = silc_mime_alloc(); | |
659 | |
660 /* Add content type */ | |
661 type = silcgaim_file2mime(gaim_imgstore_get_filename(image)); | |
662 if (!type) { | |
663 g_datalist_clear(&attribs); | |
664 last = end + 1; | |
665 continue; | |
666 } | |
667 silc_mime_add_field(p, "Content-Type", type); | |
668 silc_free(type); | |
669 | |
670 /* Add content transfer encoding */ | |
671 silc_mime_add_field(p, "Content-Transfer-Encoding", "binary"); | |
672 | |
673 /* Add image data */ | |
674 silc_mime_add_data(p, img, imglen); | |
675 | |
676 if (!parts) | |
677 parts = silc_dlist_init(); | |
678 silc_dlist_add(parts, p); | |
679 images = TRUE; | |
680 } | |
681 | |
682 g_datalist_clear(&attribs); | |
683 | |
684 /* Continue after tag */ | |
685 last = end + 1; | |
686 } | |
687 | |
688 /* Check for text after the image(s) */ | |
689 if (images && last && *last) { | |
690 char *tmp = gaim_unescape_html(last); | |
691 p = silc_mime_alloc(); | |
692 | |
693 /* Add content type */ | |
694 silc_mime_add_field(p, "Content-Type", | |
695 "text/plain; charset=utf-8"); | |
696 | |
697 /* Add text */ | |
698 silc_mime_add_data(p, tmp, strlen(tmp)); | |
699 g_free(tmp); | |
700 | |
701 if (!parts) | |
702 parts = silc_dlist_init(); | |
703 silc_dlist_add(parts, p); | |
704 } | |
705 | |
706 /* If there weren't any images, don't return anything. */ | |
707 if (!images) { | |
708 if (parts) | |
709 silc_dlist_uninit(parts); | |
710 return NULL; | |
711 } | |
712 | |
713 if (silc_dlist_count(parts) > 1) { | |
714 /* Multipart MIME message */ | |
715 char b[32]; | |
716 mime = silc_mime_alloc(); | |
717 silc_mime_add_field(mime, "MIME-Version", "1.0"); | |
718 g_snprintf(b, sizeof(b), "b%4X%4X", | |
719 (unsigned int)time(NULL), | |
720 silc_dlist_count(parts)); | |
721 silc_mime_set_multipart(mime, "mixed", b); | |
722 silc_dlist_start(parts); | |
723 while ((p = silc_dlist_get(parts)) != SILC_LIST_END) | |
724 silc_mime_add_multipart(mime, p); | |
725 } else { | |
726 /* Simple MIME message */ | |
727 silc_dlist_start(parts); | |
728 mime = silc_dlist_get(parts); | |
729 silc_mime_add_field(mime, "MIME-Version", "1.0"); | |
730 } | |
731 | |
732 *mflags &= ~SILC_MESSAGE_FLAG_UTF8; | |
733 *mflags |= SILC_MESSAGE_FLAG_DATA; | |
734 | |
735 /* Encode message. Fragment if it is too large */ | |
736 list = silc_mime_encode_partial(mime, 0xfc00); | |
737 | |
738 silc_dlist_uninit(parts); | |
739 | |
740 /* Added multiparts gets freed here */ | |
741 silc_mime_free(mime); | |
742 | |
743 return list; | |
744 } | |
745 | |
746 #endif /* HAVE_SILCMIME_H */ |