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