annotate libpurple/protocols/oscar/encoding.c @ 30386:ca90b6c27eb8

Refactored oscar_encoding_to_utf8(). 1. Removed elb's hack from #1645. It doesn't appear to be necessary anymore, since the recent official clients (6.5, 7.1) aren't that stupid now. 2. Simplified logic in incomingim_chan2(). 3. Removed all NULL return check for oscar_encoding_to_utf8(), because it will always return non-NULL value.
author ivan.komarov@soc.pidgin.im
date Wed, 28 Jul 2010 16:30:04 +0000
parents 9d386bf63eab
children 5661f30d1b8e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30383
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
1 /*
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
2 * Purple's oscar protocol plugin
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
3 * This file is the legal property of its developers.
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
4 * Please see the AUTHORS file distributed alongside this file.
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
5 *
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
6 * This library is free software; you can redistribute it and/or
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
7 * modify it under the terms of the GNU Lesser General Public
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
8 * License as published by the Free Software Foundation; either
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
9 * version 2 of the License, or (at your option) any later version.
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
10 *
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
11 * This library is distributed in the hope that it will be useful,
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
14 * Lesser General Public License for more details.
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
15 *
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
16 * You should have received a copy of the GNU Lesser General Public
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
17 * License along with this library; if not, write to the Free Software
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
19 */
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
20
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
21 #include "encoding.h"
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
22
30385
9d386bf63eab Stop using custom encodings (and LATIN-1, for that matter) for sending
ivan.komarov@soc.pidgin.im
parents: 30383
diff changeset
23 guint16
30383
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
24 oscar_charset_check(const char *utf8)
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
25 {
30385
9d386bf63eab Stop using custom encodings (and LATIN-1, for that matter) for sending
ivan.komarov@soc.pidgin.im
parents: 30383
diff changeset
26 while (*utf8++)
30383
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
27 {
30385
9d386bf63eab Stop using custom encodings (and LATIN-1, for that matter) for sending
ivan.komarov@soc.pidgin.im
parents: 30383
diff changeset
28 if ((unsigned char)(*utf8) > 0x7f) {
30383
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
29 /* not ASCII! */
30385
9d386bf63eab Stop using custom encodings (and LATIN-1, for that matter) for sending
ivan.komarov@soc.pidgin.im
parents: 30383
diff changeset
30 return AIM_CHARSET_UNICODE;
30383
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
31 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
32 }
30385
9d386bf63eab Stop using custom encodings (and LATIN-1, for that matter) for sending
ivan.komarov@soc.pidgin.im
parents: 30383
diff changeset
33 return AIM_CHARSET_ASCII;
30383
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
34 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
35
30386
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
36 static gchar *
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
37 encoding_extract(const char *encoding)
30383
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
38 {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
39 char *begin, *end;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
40
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
41 g_return_val_if_fail(encoding != NULL, NULL);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
42
30386
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
43 if (!g_str_has_prefix(encoding, "text/aolrtf; charset=") &&
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
44 !g_str_has_prefix(encoding, "text/x-aolrtf; charset=") &&
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
45 !g_str_has_prefix(encoding, "text/plain; charset=")) {
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
46 return g_strdup(encoding);
30383
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
47 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
48
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
49 begin = strchr(encoding, '"');
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
50 end = strrchr(encoding, '"');
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
51
30386
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
52 if ((begin == NULL) || (end == NULL) || (begin >= end)) {
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
53 return g_strdup(encoding);
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
54 }
30383
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
55
30386
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
56 return g_strndup(begin+1, (end-1) - begin);
30383
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
57 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
58
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
59 gchar *
30386
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
60 oscar_encoding_to_utf8(const char *encoding, const char *text, int textlen)
30383
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
61 {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
62 gchar *utf8 = NULL;
30386
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
63 const gchar *glib_encoding = NULL;
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
64 gchar *extracted_encoding = encoding_extract(encoding);
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
65
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
66 if (extracted_encoding == NULL || *extracted_encoding == '\0') {
30383
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
67 purple_debug_info("oscar", "Empty encoding, assuming UTF-8\n");
30386
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
68 } else if (!g_ascii_strcasecmp(extracted_encoding, "iso-8859-1")) {
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
69 glib_encoding = "iso-8859-1";
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
70 } else if (!g_ascii_strcasecmp(extracted_encoding, "ISO-8859-1-Windows-3.1-Latin-1") || !g_ascii_strcasecmp(extracted_encoding, "us-ascii")) {
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
71 glib_encoding = "Windows-1252";
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
72 } else if (!g_ascii_strcasecmp(extracted_encoding, "unicode-2-0")) {
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
73 glib_encoding = "UTF-16BE";
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
74 } else if (g_ascii_strcasecmp(extracted_encoding, "utf-8")) {
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
75 purple_debug_warning("oscar", "Unrecognized character encoding \"%s\", attempting to convert to UTF-8 anyway\n", extracted_encoding);
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
76 glib_encoding = extracted_encoding;
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
77 }
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
78
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
79 if (glib_encoding != NULL) {
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
80 utf8 = g_convert(text, textlen, "UTF-8", glib_encoding, NULL, NULL, NULL);
30383
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
81 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
82
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
83 /*
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
84 * If utf8 is still NULL then either the encoding is utf-8 or
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
85 * we have been unable to convert the text to utf-8 from the encoding
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
86 * that was specified. So we check if the text is valid utf-8 then
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
87 * just copy it.
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
88 */
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
89 if (utf8 == NULL) {
30386
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
90 if (textlen != 0 && *text != '\0' && !g_utf8_validate(text, textlen, NULL))
30383
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
91 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.)"));
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
92 else
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
93 utf8 = g_strndup(text, textlen);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
94 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
95
30386
ca90b6c27eb8 Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents: 30385
diff changeset
96 g_free(extracted_encoding);
30383
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
97 return utf8;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
98 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
99
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
100 gchar *
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
101 oscar_utf8_try_convert(PurpleAccount *account, OscarData *od, const gchar *msg)
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
102 {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
103 const char *charset = NULL;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
104 char *ret = NULL;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
105
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
106 if (od->icq)
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
107 charset = purple_account_get_string(account, "encoding", NULL);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
108
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
109 if(charset && *charset)
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
110 ret = g_convert(msg, -1, "UTF-8", charset, NULL, NULL, NULL);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
111
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
112 if(!ret)
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
113 ret = purple_utf8_try_convert(msg);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
114
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
115 return ret;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
116 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
117
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
118 static gchar *
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
119 oscar_convert_to_utf8(const gchar *data, gsize datalen, const char *charsetstr, gboolean fallback)
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
120 {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
121 gchar *ret = NULL;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
122 GError *err = NULL;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
123
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
124 if ((charsetstr == NULL) || (*charsetstr == '\0'))
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
125 return NULL;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
126
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
127 if (g_ascii_strcasecmp("UTF-8", charsetstr)) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
128 if (fallback)
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
129 ret = g_convert_with_fallback(data, datalen, "UTF-8", charsetstr, "?", NULL, NULL, &err);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
130 else
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
131 ret = g_convert(data, datalen, "UTF-8", charsetstr, NULL, NULL, &err);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
132 if (err != NULL) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
133 purple_debug_warning("oscar", "Conversion from %s failed: %s.\n",
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
134 charsetstr, err->message);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
135 g_error_free(err);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
136 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
137 } else {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
138 if (g_utf8_validate(data, datalen, NULL))
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
139 ret = g_strndup(data, datalen);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
140 else
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
141 purple_debug_warning("oscar", "String is not valid UTF-8.\n");
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
142 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
143
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
144 return ret;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
145 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
146
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
147 gchar *
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
148 oscar_decode_im_part(PurpleAccount *account, const char *sourcebn, guint16 charset, guint16 charsubset, const gchar *data, gsize datalen)
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
149 {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
150 gchar *ret = NULL;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
151 /* charsetstr1 is always set to what the correct encoding should be. */
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
152 const gchar *charsetstr1, *charsetstr2, *charsetstr3 = NULL;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
153
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
154 if ((datalen == 0) || (data == NULL))
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
155 return NULL;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
156
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
157 if (charset == AIM_CHARSET_UNICODE) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
158 charsetstr1 = "UTF-16BE";
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
159 charsetstr2 = "UTF-8";
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
160 } else if (charset == AIM_CHARSET_LATIN_1) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
161 if ((sourcebn != NULL) && oscar_util_valid_name_icq(sourcebn))
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
162 charsetstr1 = purple_account_get_string(account, "encoding", OSCAR_DEFAULT_CUSTOM_ENCODING);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
163 else
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
164 charsetstr1 = "ISO-8859-1";
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
165 charsetstr2 = "UTF-8";
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
166 } else if (charset == AIM_CHARSET_ASCII) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
167 /* Should just be "ASCII" */
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
168 charsetstr1 = "ASCII";
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
169 charsetstr2 = purple_account_get_string(account, "encoding", OSCAR_DEFAULT_CUSTOM_ENCODING);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
170 } else if (charset == 0x000d) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
171 /* iChat sending unicode over a Direct IM connection = UTF-8 */
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
172 /* Mobile AIM client on multiple devices (including Blackberry Tour, Nokia 3100, and LG VX6000) = ISO-8859-1 */
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
173 charsetstr1 = "UTF-8";
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
174 charsetstr2 = "ISO-8859-1";
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
175 charsetstr3 = purple_account_get_string(account, "encoding", OSCAR_DEFAULT_CUSTOM_ENCODING);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
176 } else {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
177 /* Unknown, hope for valid UTF-8... */
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
178 charsetstr1 = "UTF-8";
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
179 charsetstr2 = purple_account_get_string(account, "encoding", OSCAR_DEFAULT_CUSTOM_ENCODING);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
180 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
181
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
182 purple_debug_info("oscar", "Parsing IM part, charset=0x%04hx, charsubset=0x%04hx, datalen=%" G_GSIZE_FORMAT ", choice1=%s, choice2=%s, choice3=%s\n",
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
183 charset, charsubset, datalen, charsetstr1, charsetstr2, (charsetstr3 ? charsetstr3 : ""));
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
184
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
185 ret = oscar_convert_to_utf8(data, datalen, charsetstr1, FALSE);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
186 if (ret == NULL) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
187 if (charsetstr3 != NULL) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
188 /* Try charsetstr2 without allowing substitutions, then fall through to charsetstr3 if needed */
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
189 ret = oscar_convert_to_utf8(data, datalen, charsetstr2, FALSE);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
190 if (ret == NULL)
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
191 ret = oscar_convert_to_utf8(data, datalen, charsetstr3, TRUE);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
192 } else {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
193 /* Try charsetstr2, allowing substitutions */
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
194 ret = oscar_convert_to_utf8(data, datalen, charsetstr2, TRUE);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
195 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
196 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
197 if (ret == NULL) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
198 char *str, *salvage, *tmp;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
199
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
200 str = g_malloc(datalen + 1);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
201 strncpy(str, data, datalen);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
202 str[datalen] = '\0';
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
203 salvage = purple_utf8_salvage(str);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
204 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.)"),
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
205 sourcebn, sourcebn);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
206 ret = g_strdup_printf("%s %s", salvage, tmp);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
207 g_free(tmp);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
208 g_free(str);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
209 g_free(salvage);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
210 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
211
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
212 return ret;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
213 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
214
30385
9d386bf63eab Stop using custom encodings (and LATIN-1, for that matter) for sending
ivan.komarov@soc.pidgin.im
parents: 30383
diff changeset
215 gchar *
9d386bf63eab Stop using custom encodings (and LATIN-1, for that matter) for sending
ivan.komarov@soc.pidgin.im
parents: 30383
diff changeset
216 oscar_convert_to_best_encoding(const gchar *msg, gsize *result_len, guint16 *charset, gchar **charsetstr)
30383
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
217 {
30385
9d386bf63eab Stop using custom encodings (and LATIN-1, for that matter) for sending
ivan.komarov@soc.pidgin.im
parents: 30383
diff changeset
218 guint16 msg_charset = oscar_charset_check(msg);
9d386bf63eab Stop using custom encodings (and LATIN-1, for that matter) for sending
ivan.komarov@soc.pidgin.im
parents: 30383
diff changeset
219 if (charset != NULL) {
9d386bf63eab Stop using custom encodings (and LATIN-1, for that matter) for sending
ivan.komarov@soc.pidgin.im
parents: 30383
diff changeset
220 *charset = msg_charset;
30383
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
221 }
30385
9d386bf63eab Stop using custom encodings (and LATIN-1, for that matter) for sending
ivan.komarov@soc.pidgin.im
parents: 30383
diff changeset
222 if (charsetstr != NULL) {
9d386bf63eab Stop using custom encodings (and LATIN-1, for that matter) for sending
ivan.komarov@soc.pidgin.im
parents: 30383
diff changeset
223 *charsetstr = msg_charset == AIM_CHARSET_ASCII ? "us-ascii" : "unicode-2-0";
30383
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
224 }
30385
9d386bf63eab Stop using custom encodings (and LATIN-1, for that matter) for sending
ivan.komarov@soc.pidgin.im
parents: 30383
diff changeset
225 return g_convert(msg, -1, msg_charset == AIM_CHARSET_ASCII ? "ASCII" : "UTF-16BE", "UTF-8", NULL, result_len, NULL);
30383
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
226 }