src/protocols/msn/utils.c

changeset 8518
694a8ad60edd
parent 8475
3b5687726055
child 8530
9a378004cffa
equal deleted inserted replaced
8517:a6647dc5baee 8518:694a8ad60edd
32 if (pre_ret != NULL) *pre_ret = NULL; 32 if (pre_ret != NULL) *pre_ret = NULL;
33 if (post_ret != NULL) *post_ret = NULL; 33 if (post_ret != NULL) *post_ret = NULL;
34 34
35 cur = strstr(mime, "FN="); 35 cur = strstr(mime, "FN=");
36 36
37 if (cur && (*(cur = cur + 3) != ';')) { 37 if (cur && (*(cur = cur + 3) != ';'))
38 {
38 pre = g_string_append(pre, "<FONT FACE=\""); 39 pre = g_string_append(pre, "<FONT FACE=\"");
39 40
40 while (*cur && *cur != ';') { 41 while (*cur && *cur != ';')
42 {
41 pre = g_string_append_c(pre, *cur); 43 pre = g_string_append_c(pre, *cur);
42 cur++; 44 cur++;
43 } 45 }
44 46
45 pre = g_string_append(pre, "\">"); 47 pre = g_string_append(pre, "\">");
46 post = g_string_prepend(post, "</FONT>"); 48 post = g_string_prepend(post, "</FONT>");
47 } 49 }
48 50
49 cur = strstr(mime, "EF="); 51 cur = strstr(mime, "EF=");
50 52
51 if (cur && (*(cur = cur + 3) != ';')) { 53 if (cur && (*(cur = cur + 3) != ';'))
52 while (*cur && *cur != ';') { 54 {
55 while (*cur && *cur != ';')
56 {
53 pre = g_string_append_c(pre, '<'); 57 pre = g_string_append_c(pre, '<');
54 pre = g_string_append_c(pre, *cur); 58 pre = g_string_append_c(pre, *cur);
55 pre = g_string_append_c(pre, '>'); 59 pre = g_string_append_c(pre, '>');
56 cur++; 60 cur++;
57 } 61 }
58 } 62 }
59 63
60 cur = strstr(mime, "CO="); 64 cur = strstr(mime, "CO=");
61 65
62 if (cur && (*(cur = cur + 3) != ';')) { 66 if (cur && (*(cur = cur + 3) != ';'))
67 {
63 int i; 68 int i;
64 69
65 i = sscanf(cur, "%02x%02x%02x;", &colors[0], &colors[1], &colors[2]); 70 i = sscanf(cur, "%02x%02x%02x;", &colors[0], &colors[1], &colors[2]);
66 71
67 if (i > 0) { 72 if (i > 0)
73 {
68 char tag[64]; 74 char tag[64];
69 75
70 if (i == 1) { 76 if (i == 1)
77 {
78 colors[1] = 0;
79 colors[2] = 0;
80 }
81 else if (i == 2)
82 {
83 unsigned int temp = colors[0];
84
85 colors[0] = colors[1];
86 colors[1] = temp;
87 colors[2] = 0;
88 }
89 else if (i == 3)
90 {
91 unsigned int temp = colors[2];
92
71 colors[2] = colors[0]; 93 colors[2] = colors[0];
72 colors[1] = 0; 94 colors[0] = temp;
73 colors[0] = 0;
74 }
75 else if (i == 2) {
76 colors[2] = colors[1];
77 colors[1] = colors[0];
78 colors[0] = 0;
79 } 95 }
80 96
81 g_snprintf(tag, sizeof(tag), 97 g_snprintf(tag, sizeof(tag),
82 "<FONT COLOR=\"#%02hhx%02hhx%02hhx\">", 98 "<FONT COLOR=\"#%02hhx%02hhx%02hhx\">",
83 colors[2], colors[1], colors[0]); 99 colors[0], colors[1], colors[2]);
100
101 gaim_debug_misc("msn", "Got: %s\n", tag);
84 102
85 pre = g_string_append(pre, tag); 103 pre = g_string_append(pre, tag);
86 post = g_string_prepend(post, "</FONT>"); 104 post = g_string_prepend(post, "</FONT>");
87 } 105 }
88 } 106 }
101 if (post_ret != NULL) 119 if (post_ret != NULL)
102 *post_ret = cur; 120 *post_ret = cur;
103 else 121 else
104 g_free(cur); 122 g_free(cur);
105 } 123 }
124
125 /*
126 * We need this because we're only supposed to encode spaces in the font
127 * names. gaim_url_encode() isn't acceptable.
128 */
129 const char *
130 encode_spaces(const char *str)
131 {
132 static char buf[BUF_LEN];
133 const char *c;
134 char *d;
135
136 g_return_val_if_fail(str != NULL, NULL);
137
138 for (c = str, d = buf; *c != '\0'; c++)
139 {
140 if (*c == ' ')
141 {
142 *d++ = '%';
143 *d++ = '2';
144 *d++ = '0';
145 }
146 else
147 *d++ = *c;
148 }
149
150 return buf;
151 }
152
153 /*
154 * Taken from the zephyr plugin.
155 * This parses HTML formatting (put out by one of the gtkimhtml widgets
156 * and converts it to msn formatting. It doesn't deal with the tag closing,
157 * but gtkimhtml widgets give valid html.
158 * It currently deals properly with <b>, <u>, <i>, <font face=...>,
159 * <font color=...>.
160 * It ignores <font back=...> and <font size=...>
161 */
162 void
163 msn_import_html(const char *html, char **attributes, char **message)
164 {
165 int len, retcount = 0;
166 const char *c;
167 char *msg;
168 char *fontface = NULL;
169 char fonteffect[4];
170 char fontcolor[7];
171
172 g_return_if_fail(html != NULL);
173 g_return_if_fail(attributes != NULL);
174 g_return_if_fail(message != NULL);
175
176 len = strlen(html);
177 msg = g_malloc0(len + 1);
178
179 memset(fontcolor, 0, sizeof(fontcolor));
180 memset(fonteffect, 0, sizeof(fontcolor));
181
182 for (c = html; *c != '\0';)
183 {
184 if (*c == '<')
185 {
186 if (!g_ascii_strncasecmp(c + 1, "i>", 2))
187 {
188 strcat(fonteffect, "I");
189 c += 3;
190 }
191 else if (!g_ascii_strncasecmp(c + 1, "b>", 2))
192 {
193 strcat(fonteffect, "B");
194 c += 3;
195 }
196 else if (!g_ascii_strncasecmp(c + 1, "u>", 2))
197 {
198 strcat(fonteffect, "U");
199 c += 3;
200 }
201 else if (!g_ascii_strncasecmp(c + 1, "a href=\"", 8))
202 {
203 c += 9;
204
205 while (g_ascii_strncasecmp(c, "\">", 2))
206 msg[retcount++] = *c++;
207
208 c += 2;
209
210 /* ignore descriptive string */
211 while (g_ascii_strncasecmp(c, "</a>", 4))
212 c++;
213
214 c += 4;
215 }
216 else if (!g_ascii_strncasecmp(c + 1, "font", 4))
217 {
218 c += 5;
219
220 while (!g_ascii_strncasecmp(c, " ", 1))
221 c++;
222
223 if (!g_ascii_strncasecmp(c, "color=\"#", 7))
224 {
225 c += 8;
226
227 fontcolor[0] = *(c + 4);
228 fontcolor[1] = *(c + 5);
229 fontcolor[2] = *(c + 2);
230 fontcolor[3] = *(c + 3);
231 fontcolor[4] = *c;
232 fontcolor[5] = *(c + 1);
233
234 c += 8;
235 }
236 else if (!g_ascii_strncasecmp(c, "face=\"", 6))
237 {
238 const char *end = NULL;
239 unsigned int namelen = 0;
240
241 c += 6;
242 end = strchr(c, '\"');
243 namelen = (unsigned int)(end - c);
244 fontface = g_strndup(c, namelen);
245 c = end + 2;
246 }
247 else
248 {
249 /* Drop all unrecognized/misparsed font tags */
250 while (g_ascii_strncasecmp(c, "\">", 2))
251 c++;
252
253 c += 2;
254 }
255 }
256 else
257 {
258 while (g_ascii_strncasecmp(c, ">", 1))
259 c++;
260
261 c++;
262 }
263 }
264 else
265 msg[retcount++] = *c++;
266 }
267
268 if (fontface == NULL)
269 fontface = g_strdup("MS Sans Serif");
270
271 *attributes = g_strdup_printf("FN=%s; EF=%s; CO=%s; PF=0",
272 encode_spaces(fontface),
273 fonteffect, fontcolor);
274 *message = g_strdup(msg);
275
276 g_free(fontface);
277 g_free(msg);
278 }

mercurial