30383
|
1 /*
|
|
2 * Purple's oscar protocol plugin
|
|
3 * This file is the legal property of its developers.
|
|
4 * Please see the AUTHORS file distributed alongside this file.
|
|
5 *
|
|
6 * This library is free software; you can redistribute it and/or
|
|
7 * modify it under the terms of the GNU Lesser General Public
|
|
8 * License as published by the Free Software Foundation; either
|
|
9 * version 2 of the License, or (at your option) any later version.
|
|
10 *
|
|
11 * This library is distributed in the hope that it will be useful,
|
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
14 * Lesser General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU Lesser General Public
|
|
17 * License along with this library; if not, write to the Free Software
|
|
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
|
|
19 */
|
|
20
|
|
21 #include "encoding.h"
|
|
22
|
|
23 guint32
|
|
24 oscar_charset_check(const char *utf8)
|
|
25 {
|
|
26 int i = 0;
|
|
27 int charset = AIM_CHARSET_ASCII;
|
|
28
|
|
29 /*
|
|
30 * Can we get away with using our custom encoding?
|
|
31 */
|
|
32 while (utf8[i])
|
|
33 {
|
|
34 if ((unsigned char)utf8[i] > 0x7f) {
|
|
35 /* not ASCII! */
|
|
36 charset = AIM_CHARSET_LATIN_1;
|
|
37 break;
|
|
38 }
|
|
39 i++;
|
|
40 }
|
|
41
|
|
42 /*
|
|
43 * Must we send this message as UNICODE (in the UTF-16BE encoding)?
|
|
44 */
|
|
45 while (utf8[i])
|
|
46 {
|
|
47 /* ISO-8859-1 is 0x00-0xbf in the first byte
|
|
48 * followed by 0xc0-0xc3 in the second */
|
|
49 if ((unsigned char)utf8[i] < 0x80) {
|
|
50 i++;
|
|
51 continue;
|
|
52 } else if (((unsigned char)utf8[i] & 0xfc) == 0xc0 &&
|
|
53 ((unsigned char)utf8[i + 1] & 0xc0) == 0x80) {
|
|
54 i += 2;
|
|
55 continue;
|
|
56 }
|
|
57 charset = AIM_CHARSET_UNICODE;
|
|
58 break;
|
|
59 }
|
|
60
|
|
61 return charset;
|
|
62 }
|
|
63
|
|
64 gchar *
|
|
65 oscar_encoding_extract(const char *encoding)
|
|
66 {
|
|
67 gchar *ret = NULL;
|
|
68 char *begin, *end;
|
|
69
|
|
70 g_return_val_if_fail(encoding != NULL, NULL);
|
|
71
|
|
72 /* Make sure encoding begins with charset= */
|
|
73 if (strncmp(encoding, "text/aolrtf; charset=", 21) &&
|
|
74 strncmp(encoding, "text/x-aolrtf; charset=", 23) &&
|
|
75 strncmp(encoding, "text/plain; charset=", 20))
|
|
76 {
|
|
77 return NULL;
|
|
78 }
|
|
79
|
|
80 begin = strchr(encoding, '"');
|
|
81 end = strrchr(encoding, '"');
|
|
82
|
|
83 if ((begin == NULL) || (end == NULL) || (begin >= end))
|
|
84 return NULL;
|
|
85
|
|
86 ret = g_strndup(begin+1, (end-1) - begin);
|
|
87
|
|
88 return ret;
|
|
89 }
|
|
90
|
|
91 gchar *
|
|
92 oscar_encoding_to_utf8(PurpleAccount *account, const char *encoding, const char *text, int textlen)
|
|
93 {
|
|
94 gchar *utf8 = NULL;
|
|
95
|
|
96 if ((encoding == NULL) || encoding[0] == '\0') {
|
|
97 purple_debug_info("oscar", "Empty encoding, assuming UTF-8\n");
|
|
98 } else if (!g_ascii_strcasecmp(encoding, "iso-8859-1")) {
|
|
99 utf8 = g_convert(text, textlen, "UTF-8", "iso-8859-1", NULL, NULL, NULL);
|
|
100 } else if (!g_ascii_strcasecmp(encoding, "ISO-8859-1-Windows-3.1-Latin-1") ||
|
|
101 !g_ascii_strcasecmp(encoding, "us-ascii"))
|
|
102 {
|
|
103 utf8 = g_convert(text, textlen, "UTF-8", "Windows-1252", NULL, NULL, NULL);
|
|
104 } else if (!g_ascii_strcasecmp(encoding, "unicode-2-0")) {
|
|
105 /* Some official ICQ clients are apparently total crack,
|
|
106 * and have been known to save a UTF-8 string converted
|
|
107 * from the locale character set to UTF-16 (not from UTF-8
|
|
108 * to UTF-16!) in the away message. This hack should find
|
|
109 * and do something (un)reasonable with that, and not
|
|
110 * mess up too much else. */
|
|
111 const gchar *charset = purple_account_get_string(account, "encoding", NULL);
|
|
112 if (charset) {
|
|
113 gsize len;
|
|
114 utf8 = g_convert(text, textlen, charset, "UTF-16BE", &len, NULL, NULL);
|
|
115 if (!utf8 || len != textlen || !g_utf8_validate(utf8, -1, NULL)) {
|
|
116 g_free(utf8);
|
|
117 utf8 = NULL;
|
|
118 } else {
|
|
119 purple_debug_info("oscar", "Used broken ICQ fallback encoding\n");
|
|
120 }
|
|
121 }
|
|
122 if (!utf8)
|
|
123 utf8 = g_convert(text, textlen, "UTF-8", "UTF-16BE", NULL, NULL, NULL);
|
|
124 } else if (g_ascii_strcasecmp(encoding, "utf-8")) {
|
|
125 purple_debug_warning("oscar", "Unrecognized character encoding \"%s\", "
|
|
126 "attempting to convert to UTF-8 anyway\n", encoding);
|
|
127 utf8 = g_convert(text, textlen, "UTF-8", encoding, NULL, NULL, NULL);
|
|
128 }
|
|
129
|
|
130 /*
|
|
131 * If utf8 is still NULL then either the encoding is utf-8 or
|
|
132 * we have been unable to convert the text to utf-8 from the encoding
|
|
133 * that was specified. So we check if the text is valid utf-8 then
|
|
134 * just copy it.
|
|
135 */
|
|
136 if (utf8 == NULL) {
|
|
137 if (textlen != 0 && *text != '\0'
|
|
138 && !g_utf8_validate(text, textlen, NULL))
|
|
139 utf8 = g_strdup(_("(There was an error receiving this message. The buddy you are speaking with is probably using a different encoding than expected. If you know what encoding he is using, you can specify it in the advanced account options for your AIM/ICQ account.)"));
|
|
140 else
|
|
141 utf8 = g_strndup(text, textlen);
|
|
142 }
|
|
143
|
|
144 return utf8;
|
|
145 }
|
|
146
|
|
147 gchar *
|
|
148 oscar_utf8_try_convert(PurpleAccount *account, OscarData *od, const gchar *msg)
|
|
149 {
|
|
150 const char *charset = NULL;
|
|
151 char *ret = NULL;
|
|
152
|
|
153 if (od->icq)
|
|
154 charset = purple_account_get_string(account, "encoding", NULL);
|
|
155
|
|
156 if(charset && *charset)
|
|
157 ret = g_convert(msg, -1, "UTF-8", charset, NULL, NULL, NULL);
|
|
158
|
|
159 if(!ret)
|
|
160 ret = purple_utf8_try_convert(msg);
|
|
161
|
|
162 return ret;
|
|
163 }
|
|
164
|
|
165 static gchar *
|
|
166 oscar_convert_to_utf8(const gchar *data, gsize datalen, const char *charsetstr, gboolean fallback)
|
|
167 {
|
|
168 gchar *ret = NULL;
|
|
169 GError *err = NULL;
|
|
170
|
|
171 if ((charsetstr == NULL) || (*charsetstr == '\0'))
|
|
172 return NULL;
|
|
173
|
|
174 if (g_ascii_strcasecmp("UTF-8", charsetstr)) {
|
|
175 if (fallback)
|
|
176 ret = g_convert_with_fallback(data, datalen, "UTF-8", charsetstr, "?", NULL, NULL, &err);
|
|
177 else
|
|
178 ret = g_convert(data, datalen, "UTF-8", charsetstr, NULL, NULL, &err);
|
|
179 if (err != NULL) {
|
|
180 purple_debug_warning("oscar", "Conversion from %s failed: %s.\n",
|
|
181 charsetstr, err->message);
|
|
182 g_error_free(err);
|
|
183 }
|
|
184 } else {
|
|
185 if (g_utf8_validate(data, datalen, NULL))
|
|
186 ret = g_strndup(data, datalen);
|
|
187 else
|
|
188 purple_debug_warning("oscar", "String is not valid UTF-8.\n");
|
|
189 }
|
|
190
|
|
191 return ret;
|
|
192 }
|
|
193
|
|
194 gchar *
|
|
195 oscar_decode_im_part(PurpleAccount *account, const char *sourcebn, guint16 charset, guint16 charsubset, const gchar *data, gsize datalen)
|
|
196 {
|
|
197 gchar *ret = NULL;
|
|
198 /* charsetstr1 is always set to what the correct encoding should be. */
|
|
199 const gchar *charsetstr1, *charsetstr2, *charsetstr3 = NULL;
|
|
200
|
|
201 if ((datalen == 0) || (data == NULL))
|
|
202 return NULL;
|
|
203
|
|
204 if (charset == AIM_CHARSET_UNICODE) {
|
|
205 charsetstr1 = "UTF-16BE";
|
|
206 charsetstr2 = "UTF-8";
|
|
207 } else if (charset == AIM_CHARSET_LATIN_1) {
|
|
208 if ((sourcebn != NULL) && oscar_util_valid_name_icq(sourcebn))
|
|
209 charsetstr1 = purple_account_get_string(account, "encoding", OSCAR_DEFAULT_CUSTOM_ENCODING);
|
|
210 else
|
|
211 charsetstr1 = "ISO-8859-1";
|
|
212 charsetstr2 = "UTF-8";
|
|
213 } else if (charset == AIM_CHARSET_ASCII) {
|
|
214 /* Should just be "ASCII" */
|
|
215 charsetstr1 = "ASCII";
|
|
216 charsetstr2 = purple_account_get_string(account, "encoding", OSCAR_DEFAULT_CUSTOM_ENCODING);
|
|
217 } else if (charset == 0x000d) {
|
|
218 /* iChat sending unicode over a Direct IM connection = UTF-8 */
|
|
219 /* Mobile AIM client on multiple devices (including Blackberry Tour, Nokia 3100, and LG VX6000) = ISO-8859-1 */
|
|
220 charsetstr1 = "UTF-8";
|
|
221 charsetstr2 = "ISO-8859-1";
|
|
222 charsetstr3 = purple_account_get_string(account, "encoding", OSCAR_DEFAULT_CUSTOM_ENCODING);
|
|
223 } else {
|
|
224 /* Unknown, hope for valid UTF-8... */
|
|
225 charsetstr1 = "UTF-8";
|
|
226 charsetstr2 = purple_account_get_string(account, "encoding", OSCAR_DEFAULT_CUSTOM_ENCODING);
|
|
227 }
|
|
228
|
|
229 purple_debug_info("oscar", "Parsing IM part, charset=0x%04hx, charsubset=0x%04hx, datalen=%" G_GSIZE_FORMAT ", choice1=%s, choice2=%s, choice3=%s\n",
|
|
230 charset, charsubset, datalen, charsetstr1, charsetstr2, (charsetstr3 ? charsetstr3 : ""));
|
|
231
|
|
232 ret = oscar_convert_to_utf8(data, datalen, charsetstr1, FALSE);
|
|
233 if (ret == NULL) {
|
|
234 if (charsetstr3 != NULL) {
|
|
235 /* Try charsetstr2 without allowing substitutions, then fall through to charsetstr3 if needed */
|
|
236 ret = oscar_convert_to_utf8(data, datalen, charsetstr2, FALSE);
|
|
237 if (ret == NULL)
|
|
238 ret = oscar_convert_to_utf8(data, datalen, charsetstr3, TRUE);
|
|
239 } else {
|
|
240 /* Try charsetstr2, allowing substitutions */
|
|
241 ret = oscar_convert_to_utf8(data, datalen, charsetstr2, TRUE);
|
|
242 }
|
|
243 }
|
|
244 if (ret == NULL) {
|
|
245 char *str, *salvage, *tmp;
|
|
246
|
|
247 str = g_malloc(datalen + 1);
|
|
248 strncpy(str, data, datalen);
|
|
249 str[datalen] = '\0';
|
|
250 salvage = purple_utf8_salvage(str);
|
|
251 tmp = g_strdup_printf(_("(There was an error receiving this message. Either you and %s have different encodings selected, or %s has a buggy client.)"),
|
|
252 sourcebn, sourcebn);
|
|
253 ret = g_strdup_printf("%s %s", salvage, tmp);
|
|
254 g_free(tmp);
|
|
255 g_free(str);
|
|
256 g_free(salvage);
|
|
257 }
|
|
258
|
|
259 return ret;
|
|
260 }
|
|
261
|
|
262 void
|
|
263 oscar_convert_to_best_encoding(PurpleConnection *gc,
|
|
264 const char *destbn, const gchar *from,
|
|
265 gchar **msg, int *msglen_int,
|
|
266 guint16 *charset, guint16 *charsubset)
|
|
267 {
|
|
268 OscarData *od = purple_connection_get_protocol_data(gc);
|
|
269 PurpleAccount *account = purple_connection_get_account(gc);
|
|
270 GError *err = NULL;
|
|
271 aim_userinfo_t *userinfo = NULL;
|
|
272 const gchar *charsetstr;
|
|
273 gsize msglen;
|
|
274
|
|
275 /* Attempt to send as ASCII */
|
|
276 if (oscar_charset_check(from) == AIM_CHARSET_ASCII) {
|
|
277 *msg = g_convert(from, -1, "ASCII", "UTF-8", NULL, &msglen, NULL);
|
|
278 *charset = AIM_CHARSET_ASCII;
|
|
279 *charsubset = 0x0000;
|
|
280 *msglen_int = msglen;
|
|
281 return;
|
|
282 }
|
|
283
|
|
284 /*
|
|
285 * If we're sending to an ICQ user, and they are in our
|
|
286 * buddy list, and they are advertising the Unicode
|
|
287 * capability, and they are online, then attempt to send
|
|
288 * as UTF-16BE.
|
|
289 */
|
|
290 if ((destbn != NULL) && oscar_util_valid_name_icq(destbn))
|
|
291 userinfo = aim_locate_finduserinfo(od, destbn);
|
|
292
|
|
293 if ((userinfo != NULL) && (userinfo->capabilities & OSCAR_CAPABILITY_UNICODE))
|
|
294 {
|
|
295 PurpleBuddy *b;
|
|
296 b = purple_find_buddy(account, destbn);
|
|
297 if ((b != NULL) && (PURPLE_BUDDY_IS_ONLINE(b)))
|
|
298 {
|
|
299 *msg = g_convert(from, -1, "UTF-16BE", "UTF-8", NULL, &msglen, &err);
|
|
300 if (*msg != NULL)
|
|
301 {
|
|
302 *charset = AIM_CHARSET_UNICODE;
|
|
303 *charsubset = 0x0000;
|
|
304 *msglen_int = msglen;
|
|
305 return;
|
|
306 }
|
|
307
|
|
308 purple_debug_error("oscar", "Conversion from UTF-8 to UTF-16BE failed: %s.\n",
|
|
309 err->message);
|
|
310 g_error_free(err);
|
|
311 err = NULL;
|
|
312 }
|
|
313 }
|
|
314
|
|
315 /*
|
|
316 * If this is AIM then attempt to send as ISO-8859-1. If this is
|
|
317 * ICQ then attempt to send as the user specified character encoding.
|
|
318 */
|
|
319 charsetstr = "ISO-8859-1";
|
|
320 if ((destbn != NULL) && oscar_util_valid_name_icq(destbn))
|
|
321 charsetstr = purple_account_get_string(account, "encoding", OSCAR_DEFAULT_CUSTOM_ENCODING);
|
|
322
|
|
323 /*
|
|
324 * XXX - We need a way to only attempt to convert if we KNOW "from"
|
|
325 * can be converted to "charsetstr"
|
|
326 */
|
|
327 *msg = g_convert(from, -1, charsetstr, "UTF-8", NULL, &msglen, &err);
|
|
328 if (*msg != NULL) {
|
|
329 *charset = AIM_CHARSET_LATIN_1;
|
|
330 *charsubset = 0x0000;
|
|
331 *msglen_int = msglen;
|
|
332 return;
|
|
333 }
|
|
334
|
|
335 purple_debug_info("oscar", "Conversion from UTF-8 to %s failed (%s). Falling back to unicode.\n",
|
|
336 charsetstr, err->message);
|
|
337 g_error_free(err);
|
|
338 err = NULL;
|
|
339
|
|
340 /*
|
|
341 * Nothing else worked, so send as UTF-16BE.
|
|
342 */
|
|
343 *msg = g_convert(from, -1, "UTF-16BE", "UTF-8", NULL, &msglen, &err);
|
|
344 if (*msg != NULL) {
|
|
345 *charset = AIM_CHARSET_UNICODE;
|
|
346 *charsubset = 0x0000;
|
|
347 *msglen_int = msglen;
|
|
348 return;
|
|
349 }
|
|
350
|
|
351 purple_debug_error("oscar", "Error converting a Unicode message: %s\n", err->message);
|
|
352 g_error_free(err);
|
|
353 err = NULL;
|
|
354
|
|
355 purple_debug_error("oscar", "This should NEVER happen! Sending UTF-8 text flagged as ASCII.\n");
|
|
356 *msg = g_strdup(from);
|
|
357 *msglen_int = strlen(*msg);
|
|
358 *charset = AIM_CHARSET_ASCII;
|
|
359 *charsubset = 0x0000;
|
|
360 return;
|
|
361 }
|