annotate libpurple/protocols/oscar/encoding.c @ 30383:502f25fd81b8

Forgot to add encoding.c.
author ivan.komarov@soc.pidgin.im
date Sat, 24 Jul 2010 22:00:35 +0000
parents
children 9d386bf63eab
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
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
23 guint32
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 {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
26 int i = 0;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
27 int charset = AIM_CHARSET_ASCII;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
28
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
29 /*
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
30 * Can we get away with using our custom encoding?
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 while (utf8[i])
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
33 {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
34 if ((unsigned char)utf8[i] > 0x7f) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
35 /* not ASCII! */
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
36 charset = AIM_CHARSET_LATIN_1;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
37 break;
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 i++;
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
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
42 /*
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
43 * Must we send this message as UNICODE (in the UTF-16BE encoding)?
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
44 */
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
45 while (utf8[i])
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
46 {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
47 /* ISO-8859-1 is 0x00-0xbf in the first byte
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
48 * followed by 0xc0-0xc3 in the second */
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
49 if ((unsigned char)utf8[i] < 0x80) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
50 i++;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
51 continue;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
52 } else if (((unsigned char)utf8[i] & 0xfc) == 0xc0 &&
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
53 ((unsigned char)utf8[i + 1] & 0xc0) == 0x80) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
54 i += 2;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
55 continue;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
56 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
57 charset = AIM_CHARSET_UNICODE;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
58 break;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
59 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
60
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
61 return charset;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
62 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
63
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
64 gchar *
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
65 oscar_encoding_extract(const char *encoding)
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
66 {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
67 gchar *ret = NULL;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
68 char *begin, *end;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
69
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
70 g_return_val_if_fail(encoding != NULL, NULL);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
71
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
72 /* Make sure encoding begins with charset= */
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
73 if (strncmp(encoding, "text/aolrtf; charset=", 21) &&
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
74 strncmp(encoding, "text/x-aolrtf; charset=", 23) &&
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
75 strncmp(encoding, "text/plain; charset=", 20))
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
76 {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
77 return NULL;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
78 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
79
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
80 begin = strchr(encoding, '"');
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
81 end = strrchr(encoding, '"');
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 if ((begin == NULL) || (end == NULL) || (begin >= end))
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
84 return NULL;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
85
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
86 ret = g_strndup(begin+1, (end-1) - begin);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
87
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
88 return ret;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
89 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
90
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
91 gchar *
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
92 oscar_encoding_to_utf8(PurpleAccount *account, const char *encoding, const char *text, int textlen)
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
93 {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
94 gchar *utf8 = NULL;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
95
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
96 if ((encoding == NULL) || encoding[0] == '\0') {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
97 purple_debug_info("oscar", "Empty encoding, assuming UTF-8\n");
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
98 } else if (!g_ascii_strcasecmp(encoding, "iso-8859-1")) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
99 utf8 = g_convert(text, textlen, "UTF-8", "iso-8859-1", NULL, NULL, NULL);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
100 } else if (!g_ascii_strcasecmp(encoding, "ISO-8859-1-Windows-3.1-Latin-1") ||
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
101 !g_ascii_strcasecmp(encoding, "us-ascii"))
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 utf8 = g_convert(text, textlen, "UTF-8", "Windows-1252", NULL, NULL, NULL);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
104 } else if (!g_ascii_strcasecmp(encoding, "unicode-2-0")) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
105 /* Some official ICQ clients are apparently total crack,
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
106 * and have been known to save a UTF-8 string converted
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
107 * from the locale character set to UTF-16 (not from UTF-8
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
108 * to UTF-16!) in the away message. This hack should find
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
109 * and do something (un)reasonable with that, and not
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
110 * mess up too much else. */
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
111 const gchar *charset = purple_account_get_string(account, "encoding", NULL);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
112 if (charset) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
113 gsize len;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
114 utf8 = g_convert(text, textlen, charset, "UTF-16BE", &len, NULL, NULL);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
115 if (!utf8 || len != textlen || !g_utf8_validate(utf8, -1, NULL)) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
116 g_free(utf8);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
117 utf8 = NULL;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
118 } else {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
119 purple_debug_info("oscar", "Used broken ICQ fallback encoding\n");
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 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
122 if (!utf8)
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
123 utf8 = g_convert(text, textlen, "UTF-8", "UTF-16BE", NULL, NULL, NULL);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
124 } else if (g_ascii_strcasecmp(encoding, "utf-8")) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
125 purple_debug_warning("oscar", "Unrecognized character encoding \"%s\", "
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
126 "attempting to convert to UTF-8 anyway\n", encoding);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
127 utf8 = g_convert(text, textlen, "UTF-8", encoding, NULL, NULL, NULL);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
128 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
129
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
130 /*
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
131 * 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
132 * 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
133 * 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
134 * just copy it.
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
135 */
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
136 if (utf8 == NULL) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
137 if (textlen != 0 && *text != '\0'
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
138 && !g_utf8_validate(text, textlen, NULL))
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
139 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
140 else
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
141 utf8 = g_strndup(text, textlen);
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 utf8;
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_utf8_try_convert(PurpleAccount *account, OscarData *od, const gchar *msg)
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 const char *charset = NULL;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
151 char *ret = NULL;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
152
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
153 if (od->icq)
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
154 charset = purple_account_get_string(account, "encoding", NULL);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
155
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
156 if(charset && *charset)
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
157 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
158
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
159 if(!ret)
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
160 ret = purple_utf8_try_convert(msg);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
161
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
162 return ret;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
163 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
164
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
165 static gchar *
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
166 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
167 {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
168 gchar *ret = NULL;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
169 GError *err = NULL;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
170
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
171 if ((charsetstr == NULL) || (*charsetstr == '\0'))
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
172 return NULL;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
173
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
174 if (g_ascii_strcasecmp("UTF-8", charsetstr)) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
175 if (fallback)
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
176 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
177 else
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
178 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
179 if (err != NULL) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
180 purple_debug_warning("oscar", "Conversion from %s failed: %s.\n",
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
181 charsetstr, err->message);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
182 g_error_free(err);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
183 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
184 } else {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
185 if (g_utf8_validate(data, datalen, NULL))
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
186 ret = g_strndup(data, datalen);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
187 else
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
188 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
189 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
190
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
191 return ret;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
192 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
193
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
194 gchar *
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
195 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
196 {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
197 gchar *ret = NULL;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
198 /* 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
199 const gchar *charsetstr1, *charsetstr2, *charsetstr3 = NULL;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
200
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
201 if ((datalen == 0) || (data == NULL))
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
202 return NULL;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
203
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
204 if (charset == AIM_CHARSET_UNICODE) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
205 charsetstr1 = "UTF-16BE";
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
206 charsetstr2 = "UTF-8";
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
207 } else if (charset == AIM_CHARSET_LATIN_1) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
208 if ((sourcebn != NULL) && oscar_util_valid_name_icq(sourcebn))
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
209 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
210 else
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
211 charsetstr1 = "ISO-8859-1";
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
212 charsetstr2 = "UTF-8";
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
213 } else if (charset == AIM_CHARSET_ASCII) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
214 /* Should just be "ASCII" */
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
215 charsetstr1 = "ASCII";
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
216 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
217 } else if (charset == 0x000d) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
218 /* iChat sending unicode over a Direct IM connection = UTF-8 */
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
219 /* 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
220 charsetstr1 = "UTF-8";
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
221 charsetstr2 = "ISO-8859-1";
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
222 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
223 } else {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
224 /* Unknown, hope for valid UTF-8... */
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
225 charsetstr1 = "UTF-8";
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
226 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
227 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
228
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
229 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
230 charset, charsubset, datalen, charsetstr1, charsetstr2, (charsetstr3 ? charsetstr3 : ""));
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
231
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
232 ret = oscar_convert_to_utf8(data, datalen, charsetstr1, FALSE);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
233 if (ret == NULL) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
234 if (charsetstr3 != NULL) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
235 /* 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
236 ret = oscar_convert_to_utf8(data, datalen, charsetstr2, FALSE);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
237 if (ret == NULL)
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
238 ret = oscar_convert_to_utf8(data, datalen, charsetstr3, TRUE);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
239 } else {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
240 /* Try charsetstr2, allowing substitutions */
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
241 ret = oscar_convert_to_utf8(data, datalen, charsetstr2, TRUE);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
242 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
243 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
244 if (ret == NULL) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
245 char *str, *salvage, *tmp;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
246
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
247 str = g_malloc(datalen + 1);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
248 strncpy(str, data, datalen);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
249 str[datalen] = '\0';
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
250 salvage = purple_utf8_salvage(str);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
251 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
252 sourcebn, sourcebn);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
253 ret = g_strdup_printf("%s %s", salvage, tmp);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
254 g_free(tmp);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
255 g_free(str);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
256 g_free(salvage);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
257 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
258
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
259 return ret;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
260 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
261
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
262 void
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
263 oscar_convert_to_best_encoding(PurpleConnection *gc,
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
264 const char *destbn, const gchar *from,
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
265 gchar **msg, int *msglen_int,
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
266 guint16 *charset, guint16 *charsubset)
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
267 {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
268 OscarData *od = purple_connection_get_protocol_data(gc);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
269 PurpleAccount *account = purple_connection_get_account(gc);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
270 GError *err = NULL;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
271 aim_userinfo_t *userinfo = NULL;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
272 const gchar *charsetstr;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
273 gsize msglen;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
274
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
275 /* Attempt to send as ASCII */
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
276 if (oscar_charset_check(from) == AIM_CHARSET_ASCII) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
277 *msg = g_convert(from, -1, "ASCII", "UTF-8", NULL, &msglen, NULL);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
278 *charset = AIM_CHARSET_ASCII;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
279 *charsubset = 0x0000;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
280 *msglen_int = msglen;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
281 return;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
282 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
283
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
284 /*
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
285 * If we're sending to an ICQ user, and they are in our
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
286 * buddy list, and they are advertising the Unicode
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
287 * capability, and they are online, then attempt to send
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
288 * as UTF-16BE.
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
289 */
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
290 if ((destbn != NULL) && oscar_util_valid_name_icq(destbn))
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
291 userinfo = aim_locate_finduserinfo(od, destbn);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
292
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
293 if ((userinfo != NULL) && (userinfo->capabilities & OSCAR_CAPABILITY_UNICODE))
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
294 {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
295 PurpleBuddy *b;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
296 b = purple_find_buddy(account, destbn);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
297 if ((b != NULL) && (PURPLE_BUDDY_IS_ONLINE(b)))
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
298 {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
299 *msg = g_convert(from, -1, "UTF-16BE", "UTF-8", NULL, &msglen, &err);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
300 if (*msg != NULL)
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
301 {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
302 *charset = AIM_CHARSET_UNICODE;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
303 *charsubset = 0x0000;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
304 *msglen_int = msglen;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
305 return;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
306 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
307
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
308 purple_debug_error("oscar", "Conversion from UTF-8 to UTF-16BE failed: %s.\n",
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
309 err->message);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
310 g_error_free(err);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
311 err = NULL;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
312 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
313 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
314
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
315 /*
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
316 * If this is AIM then attempt to send as ISO-8859-1. If this is
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
317 * ICQ then attempt to send as the user specified character encoding.
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
318 */
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
319 charsetstr = "ISO-8859-1";
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
320 if ((destbn != NULL) && oscar_util_valid_name_icq(destbn))
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
321 charsetstr = purple_account_get_string(account, "encoding", OSCAR_DEFAULT_CUSTOM_ENCODING);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
322
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
323 /*
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
324 * XXX - We need a way to only attempt to convert if we KNOW "from"
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
325 * can be converted to "charsetstr"
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
326 */
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
327 *msg = g_convert(from, -1, charsetstr, "UTF-8", NULL, &msglen, &err);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
328 if (*msg != NULL) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
329 *charset = AIM_CHARSET_LATIN_1;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
330 *charsubset = 0x0000;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
331 *msglen_int = msglen;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
332 return;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
333 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
334
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
335 purple_debug_info("oscar", "Conversion from UTF-8 to %s failed (%s). Falling back to unicode.\n",
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
336 charsetstr, err->message);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
337 g_error_free(err);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
338 err = NULL;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
339
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
340 /*
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
341 * Nothing else worked, so send as UTF-16BE.
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
342 */
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
343 *msg = g_convert(from, -1, "UTF-16BE", "UTF-8", NULL, &msglen, &err);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
344 if (*msg != NULL) {
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
345 *charset = AIM_CHARSET_UNICODE;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
346 *charsubset = 0x0000;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
347 *msglen_int = msglen;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
348 return;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
349 }
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
350
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
351 purple_debug_error("oscar", "Error converting a Unicode message: %s\n", err->message);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
352 g_error_free(err);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
353 err = NULL;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
354
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
355 purple_debug_error("oscar", "This should NEVER happen! Sending UTF-8 text flagged as ASCII.\n");
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
356 *msg = g_strdup(from);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
357 *msglen_int = strlen(*msg);
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
358 *charset = AIM_CHARSET_ASCII;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
359 *charsubset = 0x0000;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
360 return;
502f25fd81b8 Forgot to add encoding.c.
ivan.komarov@soc.pidgin.im
parents:
diff changeset
361 }