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