Mercurial > pidgin.yaz
annotate libpurple/protocols/oscar/encoding.c @ 30834:a6511abec788
A couple of refactorings related to oscar_utf8_try_convert().
author | ivan.komarov@soc.pidgin.im |
---|---|
date | Thu, 05 Aug 2010 23:43:24 +0000 |
parents | 1f3ef11a9690 |
children | 2eacc8595967 a2e2d351f6fb |
rev | line source |
---|---|
30816 | 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 | |
30819
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30818
diff
changeset
|
23 static gchar * |
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30818
diff
changeset
|
24 encoding_extract(const char *encoding) |
30816 | 25 { |
26 char *begin, *end; | |
27 | |
30825
a4f579485ce6
encoding can be NULL in encoding_extract(); this is not an error and
ivan.komarov@soc.pidgin.im
parents:
30824
diff
changeset
|
28 if (encoding == NULL) { |
a4f579485ce6
encoding can be NULL in encoding_extract(); this is not an error and
ivan.komarov@soc.pidgin.im
parents:
30824
diff
changeset
|
29 return NULL; |
a4f579485ce6
encoding can be NULL in encoding_extract(); this is not an error and
ivan.komarov@soc.pidgin.im
parents:
30824
diff
changeset
|
30 } |
30816 | 31 |
30819
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30818
diff
changeset
|
32 if (!g_str_has_prefix(encoding, "text/aolrtf; charset=") && |
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30818
diff
changeset
|
33 !g_str_has_prefix(encoding, "text/x-aolrtf; charset=") && |
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30818
diff
changeset
|
34 !g_str_has_prefix(encoding, "text/plain; charset=")) { |
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30818
diff
changeset
|
35 return g_strdup(encoding); |
30816 | 36 } |
37 | |
38 begin = strchr(encoding, '"'); | |
39 end = strrchr(encoding, '"'); | |
40 | |
30819
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30818
diff
changeset
|
41 if ((begin == NULL) || (end == NULL) || (begin >= end)) { |
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30818
diff
changeset
|
42 return g_strdup(encoding); |
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30818
diff
changeset
|
43 } |
30816 | 44 |
30819
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30818
diff
changeset
|
45 return g_strndup(begin+1, (end-1) - begin); |
30816 | 46 } |
47 | |
48 gchar * | |
30819
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30818
diff
changeset
|
49 oscar_encoding_to_utf8(const char *encoding, const char *text, int textlen) |
30816 | 50 { |
51 gchar *utf8 = NULL; | |
30819
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30818
diff
changeset
|
52 const gchar *glib_encoding = NULL; |
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30818
diff
changeset
|
53 gchar *extracted_encoding = encoding_extract(encoding); |
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30818
diff
changeset
|
54 |
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30818
diff
changeset
|
55 if (extracted_encoding == NULL || *extracted_encoding == '\0') { |
30816 | 56 purple_debug_info("oscar", "Empty encoding, assuming UTF-8\n"); |
30819
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30818
diff
changeset
|
57 } else if (!g_ascii_strcasecmp(extracted_encoding, "iso-8859-1")) { |
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30818
diff
changeset
|
58 glib_encoding = "iso-8859-1"; |
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30818
diff
changeset
|
59 } 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:
30818
diff
changeset
|
60 glib_encoding = "Windows-1252"; |
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30818
diff
changeset
|
61 } else if (!g_ascii_strcasecmp(extracted_encoding, "unicode-2-0")) { |
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30818
diff
changeset
|
62 glib_encoding = "UTF-16BE"; |
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30818
diff
changeset
|
63 } else if (g_ascii_strcasecmp(extracted_encoding, "utf-8")) { |
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30818
diff
changeset
|
64 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:
30818
diff
changeset
|
65 glib_encoding = extracted_encoding; |
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30818
diff
changeset
|
66 } |
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30818
diff
changeset
|
67 |
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30818
diff
changeset
|
68 if (glib_encoding != NULL) { |
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30818
diff
changeset
|
69 utf8 = g_convert(text, textlen, "UTF-8", glib_encoding, NULL, NULL, NULL); |
30816 | 70 } |
71 | |
72 /* | |
73 * If utf8 is still NULL then either the encoding is utf-8 or | |
74 * we have been unable to convert the text to utf-8 from the encoding | |
75 * that was specified. So we check if the text is valid utf-8 then | |
76 * just copy it. | |
77 */ | |
78 if (utf8 == NULL) { | |
30819
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30818
diff
changeset
|
79 if (textlen != 0 && *text != '\0' && !g_utf8_validate(text, textlen, NULL)) |
30816 | 80 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.)")); |
81 else | |
82 utf8 = g_strndup(text, textlen); | |
83 } | |
84 | |
30819
ca90b6c27eb8
Refactored oscar_encoding_to_utf8().
ivan.komarov@soc.pidgin.im
parents:
30818
diff
changeset
|
85 g_free(extracted_encoding); |
30816 | 86 return utf8; |
87 } | |
88 | |
89 gchar * | |
90 oscar_utf8_try_convert(PurpleAccount *account, OscarData *od, const gchar *msg) | |
91 { | |
92 const char *charset = NULL; | |
93 char *ret = NULL; | |
94 | |
30834
a6511abec788
A couple of refactorings related to oscar_utf8_try_convert().
ivan.komarov@soc.pidgin.im
parents:
30830
diff
changeset
|
95 if (msg == NULL) |
a6511abec788
A couple of refactorings related to oscar_utf8_try_convert().
ivan.komarov@soc.pidgin.im
parents:
30830
diff
changeset
|
96 return NULL; |
a6511abec788
A couple of refactorings related to oscar_utf8_try_convert().
ivan.komarov@soc.pidgin.im
parents:
30830
diff
changeset
|
97 |
a6511abec788
A couple of refactorings related to oscar_utf8_try_convert().
ivan.komarov@soc.pidgin.im
parents:
30830
diff
changeset
|
98 if (g_utf8_validate(msg, -1, NULL)) |
a6511abec788
A couple of refactorings related to oscar_utf8_try_convert().
ivan.komarov@soc.pidgin.im
parents:
30830
diff
changeset
|
99 return g_strdup(msg); |
a6511abec788
A couple of refactorings related to oscar_utf8_try_convert().
ivan.komarov@soc.pidgin.im
parents:
30830
diff
changeset
|
100 |
30816 | 101 if (od->icq) |
102 charset = purple_account_get_string(account, "encoding", NULL); | |
103 | |
104 if(charset && *charset) | |
105 ret = g_convert(msg, -1, "UTF-8", charset, NULL, NULL, NULL); | |
106 | |
107 if(!ret) | |
108 ret = purple_utf8_try_convert(msg); | |
109 | |
110 return ret; | |
111 } | |
112 | |
113 static gchar * | |
114 oscar_convert_to_utf8(const gchar *data, gsize datalen, const char *charsetstr, gboolean fallback) | |
115 { | |
116 gchar *ret = NULL; | |
117 GError *err = NULL; | |
118 | |
119 if ((charsetstr == NULL) || (*charsetstr == '\0')) | |
120 return NULL; | |
121 | |
122 if (g_ascii_strcasecmp("UTF-8", charsetstr)) { | |
123 if (fallback) | |
124 ret = g_convert_with_fallback(data, datalen, "UTF-8", charsetstr, "?", NULL, NULL, &err); | |
125 else | |
126 ret = g_convert(data, datalen, "UTF-8", charsetstr, NULL, NULL, &err); | |
127 if (err != NULL) { | |
128 purple_debug_warning("oscar", "Conversion from %s failed: %s.\n", | |
129 charsetstr, err->message); | |
130 g_error_free(err); | |
131 } | |
132 } else { | |
133 if (g_utf8_validate(data, datalen, NULL)) | |
134 ret = g_strndup(data, datalen); | |
135 else | |
136 purple_debug_warning("oscar", "String is not valid UTF-8.\n"); | |
137 } | |
138 | |
139 return ret; | |
140 } | |
141 | |
142 gchar * | |
30824
5661f30d1b8e
Got rid of receiving multipart messages over channel 1, which simplified the code
ivan.komarov@soc.pidgin.im
parents:
30819
diff
changeset
|
143 oscar_decode_im(PurpleAccount *account, const char *sourcebn, guint16 charset, const gchar *data, gsize datalen) |
30816 | 144 { |
145 gchar *ret = NULL; | |
146 /* charsetstr1 is always set to what the correct encoding should be. */ | |
147 const gchar *charsetstr1, *charsetstr2, *charsetstr3 = NULL; | |
148 | |
149 if ((datalen == 0) || (data == NULL)) | |
150 return NULL; | |
151 | |
152 if (charset == AIM_CHARSET_UNICODE) { | |
153 charsetstr1 = "UTF-16BE"; | |
154 charsetstr2 = "UTF-8"; | |
155 } else if (charset == AIM_CHARSET_LATIN_1) { | |
156 if ((sourcebn != NULL) && oscar_util_valid_name_icq(sourcebn)) | |
157 charsetstr1 = purple_account_get_string(account, "encoding", OSCAR_DEFAULT_CUSTOM_ENCODING); | |
158 else | |
159 charsetstr1 = "ISO-8859-1"; | |
160 charsetstr2 = "UTF-8"; | |
161 } else if (charset == AIM_CHARSET_ASCII) { | |
162 /* Should just be "ASCII" */ | |
163 charsetstr1 = "ASCII"; | |
164 charsetstr2 = purple_account_get_string(account, "encoding", OSCAR_DEFAULT_CUSTOM_ENCODING); | |
165 } else if (charset == 0x000d) { | |
166 /* iChat sending unicode over a Direct IM connection = UTF-8 */ | |
167 /* Mobile AIM client on multiple devices (including Blackberry Tour, Nokia 3100, and LG VX6000) = ISO-8859-1 */ | |
168 charsetstr1 = "UTF-8"; | |
169 charsetstr2 = "ISO-8859-1"; | |
170 charsetstr3 = purple_account_get_string(account, "encoding", OSCAR_DEFAULT_CUSTOM_ENCODING); | |
171 } else { | |
172 /* Unknown, hope for valid UTF-8... */ | |
173 charsetstr1 = "UTF-8"; | |
174 charsetstr2 = purple_account_get_string(account, "encoding", OSCAR_DEFAULT_CUSTOM_ENCODING); | |
175 } | |
176 | |
30824
5661f30d1b8e
Got rid of receiving multipart messages over channel 1, which simplified the code
ivan.komarov@soc.pidgin.im
parents:
30819
diff
changeset
|
177 purple_debug_info("oscar", "Parsing IM, charset=0x%04hx, datalen=%" G_GSIZE_FORMAT ", choice1=%s, choice2=%s, choice3=%s\n", |
5661f30d1b8e
Got rid of receiving multipart messages over channel 1, which simplified the code
ivan.komarov@soc.pidgin.im
parents:
30819
diff
changeset
|
178 charset, datalen, charsetstr1, charsetstr2, (charsetstr3 ? charsetstr3 : "")); |
30816 | 179 |
180 ret = oscar_convert_to_utf8(data, datalen, charsetstr1, FALSE); | |
181 if (ret == NULL) { | |
182 if (charsetstr3 != NULL) { | |
183 /* Try charsetstr2 without allowing substitutions, then fall through to charsetstr3 if needed */ | |
184 ret = oscar_convert_to_utf8(data, datalen, charsetstr2, FALSE); | |
185 if (ret == NULL) | |
186 ret = oscar_convert_to_utf8(data, datalen, charsetstr3, TRUE); | |
187 } else { | |
188 /* Try charsetstr2, allowing substitutions */ | |
189 ret = oscar_convert_to_utf8(data, datalen, charsetstr2, TRUE); | |
190 } | |
191 } | |
192 if (ret == NULL) { | |
193 char *str, *salvage, *tmp; | |
194 | |
195 str = g_malloc(datalen + 1); | |
196 strncpy(str, data, datalen); | |
197 str[datalen] = '\0'; | |
198 salvage = purple_utf8_salvage(str); | |
199 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.)"), | |
200 sourcebn, sourcebn); | |
201 ret = g_strdup_printf("%s %s", salvage, tmp); | |
202 g_free(tmp); | |
203 g_free(str); | |
204 g_free(salvage); | |
205 } | |
206 | |
207 return ret; | |
208 } | |
209 | |
30827 | 210 static guint16 |
211 get_simplest_charset(const char *utf8) | |
212 { | |
30830
1f3ef11a9690
My esteemed mentor caught me red-handed. Thanks Mark!
ivan.komarov@soc.pidgin.im
parents:
30827
diff
changeset
|
213 while (*utf8) |
30827 | 214 { |
215 if ((unsigned char)(*utf8) > 0x7f) { | |
216 /* not ASCII! */ | |
217 return AIM_CHARSET_UNICODE; | |
218 } | |
30830
1f3ef11a9690
My esteemed mentor caught me red-handed. Thanks Mark!
ivan.komarov@soc.pidgin.im
parents:
30827
diff
changeset
|
219 utf8++; |
30827 | 220 } |
221 return AIM_CHARSET_ASCII; | |
222 } | |
223 | |
30818
9d386bf63eab
Stop using custom encodings (and LATIN-1, for that matter) for sending
ivan.komarov@soc.pidgin.im
parents:
30816
diff
changeset
|
224 gchar * |
30827 | 225 oscar_encode_im(const gchar *msg, gsize *result_len, guint16 *charset, gchar **charsetstr) |
30816 | 226 { |
30827 | 227 guint16 msg_charset = get_simplest_charset(msg); |
30818
9d386bf63eab
Stop using custom encodings (and LATIN-1, for that matter) for sending
ivan.komarov@soc.pidgin.im
parents:
30816
diff
changeset
|
228 if (charset != NULL) { |
9d386bf63eab
Stop using custom encodings (and LATIN-1, for that matter) for sending
ivan.komarov@soc.pidgin.im
parents:
30816
diff
changeset
|
229 *charset = msg_charset; |
30816 | 230 } |
30818
9d386bf63eab
Stop using custom encodings (and LATIN-1, for that matter) for sending
ivan.komarov@soc.pidgin.im
parents:
30816
diff
changeset
|
231 if (charsetstr != NULL) { |
9d386bf63eab
Stop using custom encodings (and LATIN-1, for that matter) for sending
ivan.komarov@soc.pidgin.im
parents:
30816
diff
changeset
|
232 *charsetstr = msg_charset == AIM_CHARSET_ASCII ? "us-ascii" : "unicode-2-0"; |
30816 | 233 } |
30818
9d386bf63eab
Stop using custom encodings (and LATIN-1, for that matter) for sending
ivan.komarov@soc.pidgin.im
parents:
30816
diff
changeset
|
234 return g_convert(msg, -1, msg_charset == AIM_CHARSET_ASCII ? "ASCII" : "UTF-16BE", "UTF-8", NULL, result_len, NULL); |
30816 | 235 } |