Mercurial > pidgin
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 |
rev | line source |
---|---|
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 | |
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 | 24 oscar_charset_check(const char *utf8) |
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 | 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 | 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 | 31 } |
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 | 34 } |
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 | 38 { |
39 char *begin, *end; | |
40 | |
41 g_return_val_if_fail(encoding != NULL, NULL); | |
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 | 47 } |
48 | |
49 begin = strchr(encoding, '"'); | |
50 end = strrchr(encoding, '"'); | |
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 | 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 | 57 } |
58 | |
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 | 61 { |
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 | 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 | 81 } |
82 | |
83 /* | |
84 * If utf8 is still NULL then either the encoding is utf-8 or | |
85 * we have been unable to convert the text to utf-8 from the encoding | |
86 * that was specified. So we check if the text is valid utf-8 then | |
87 * just copy it. | |
88 */ | |
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 | 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.)")); |
92 else | |
93 utf8 = g_strndup(text, textlen); | |
94 } | |
95 | |
30386
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30385
diff
changeset
|
96 g_free(extracted_encoding); |
30383 | 97 return utf8; |
98 } | |
99 | |
100 gchar * | |
101 oscar_utf8_try_convert(PurpleAccount *account, OscarData *od, const gchar *msg) | |
102 { | |
103 const char *charset = NULL; | |
104 char *ret = NULL; | |
105 | |
106 if (od->icq) | |
107 charset = purple_account_get_string(account, "encoding", NULL); | |
108 | |
109 if(charset && *charset) | |
110 ret = g_convert(msg, -1, "UTF-8", charset, NULL, NULL, NULL); | |
111 | |
112 if(!ret) | |
113 ret = purple_utf8_try_convert(msg); | |
114 | |
115 return ret; | |
116 } | |
117 | |
118 static gchar * | |
119 oscar_convert_to_utf8(const gchar *data, gsize datalen, const char *charsetstr, gboolean fallback) | |
120 { | |
121 gchar *ret = NULL; | |
122 GError *err = NULL; | |
123 | |
124 if ((charsetstr == NULL) || (*charsetstr == '\0')) | |
125 return NULL; | |
126 | |
127 if (g_ascii_strcasecmp("UTF-8", charsetstr)) { | |
128 if (fallback) | |
129 ret = g_convert_with_fallback(data, datalen, "UTF-8", charsetstr, "?", NULL, NULL, &err); | |
130 else | |
131 ret = g_convert(data, datalen, "UTF-8", charsetstr, NULL, NULL, &err); | |
132 if (err != NULL) { | |
133 purple_debug_warning("oscar", "Conversion from %s failed: %s.\n", | |
134 charsetstr, err->message); | |
135 g_error_free(err); | |
136 } | |
137 } else { | |
138 if (g_utf8_validate(data, datalen, NULL)) | |
139 ret = g_strndup(data, datalen); | |
140 else | |
141 purple_debug_warning("oscar", "String is not valid UTF-8.\n"); | |
142 } | |
143 | |
144 return ret; | |
145 } | |
146 | |
147 gchar * | |
148 oscar_decode_im_part(PurpleAccount *account, const char *sourcebn, guint16 charset, guint16 charsubset, const gchar *data, gsize datalen) | |
149 { | |
150 gchar *ret = NULL; | |
151 /* charsetstr1 is always set to what the correct encoding should be. */ | |
152 const gchar *charsetstr1, *charsetstr2, *charsetstr3 = NULL; | |
153 | |
154 if ((datalen == 0) || (data == NULL)) | |
155 return NULL; | |
156 | |
157 if (charset == AIM_CHARSET_UNICODE) { | |
158 charsetstr1 = "UTF-16BE"; | |
159 charsetstr2 = "UTF-8"; | |
160 } else if (charset == AIM_CHARSET_LATIN_1) { | |
161 if ((sourcebn != NULL) && oscar_util_valid_name_icq(sourcebn)) | |
162 charsetstr1 = purple_account_get_string(account, "encoding", OSCAR_DEFAULT_CUSTOM_ENCODING); | |
163 else | |
164 charsetstr1 = "ISO-8859-1"; | |
165 charsetstr2 = "UTF-8"; | |
166 } else if (charset == AIM_CHARSET_ASCII) { | |
167 /* Should just be "ASCII" */ | |
168 charsetstr1 = "ASCII"; | |
169 charsetstr2 = purple_account_get_string(account, "encoding", OSCAR_DEFAULT_CUSTOM_ENCODING); | |
170 } else if (charset == 0x000d) { | |
171 /* iChat sending unicode over a Direct IM connection = UTF-8 */ | |
172 /* Mobile AIM client on multiple devices (including Blackberry Tour, Nokia 3100, and LG VX6000) = ISO-8859-1 */ | |
173 charsetstr1 = "UTF-8"; | |
174 charsetstr2 = "ISO-8859-1"; | |
175 charsetstr3 = purple_account_get_string(account, "encoding", OSCAR_DEFAULT_CUSTOM_ENCODING); | |
176 } else { | |
177 /* Unknown, hope for valid UTF-8... */ | |
178 charsetstr1 = "UTF-8"; | |
179 charsetstr2 = purple_account_get_string(account, "encoding", OSCAR_DEFAULT_CUSTOM_ENCODING); | |
180 } | |
181 | |
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", | |
183 charset, charsubset, datalen, charsetstr1, charsetstr2, (charsetstr3 ? charsetstr3 : "")); | |
184 | |
185 ret = oscar_convert_to_utf8(data, datalen, charsetstr1, FALSE); | |
186 if (ret == NULL) { | |
187 if (charsetstr3 != NULL) { | |
188 /* Try charsetstr2 without allowing substitutions, then fall through to charsetstr3 if needed */ | |
189 ret = oscar_convert_to_utf8(data, datalen, charsetstr2, FALSE); | |
190 if (ret == NULL) | |
191 ret = oscar_convert_to_utf8(data, datalen, charsetstr3, TRUE); | |
192 } else { | |
193 /* Try charsetstr2, allowing substitutions */ | |
194 ret = oscar_convert_to_utf8(data, datalen, charsetstr2, TRUE); | |
195 } | |
196 } | |
197 if (ret == NULL) { | |
198 char *str, *salvage, *tmp; | |
199 | |
200 str = g_malloc(datalen + 1); | |
201 strncpy(str, data, datalen); | |
202 str[datalen] = '\0'; | |
203 salvage = purple_utf8_salvage(str); | |
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.)"), | |
205 sourcebn, sourcebn); | |
206 ret = g_strdup_printf("%s %s", salvage, tmp); | |
207 g_free(tmp); | |
208 g_free(str); | |
209 g_free(salvage); | |
210 } | |
211 | |
212 return ret; | |
213 } | |
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 | 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 | 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 | 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 | 226 } |