comparison src/protocols/msn/utils.c @ 8595:1d5e31e518fc

[gaim-migrate @ 9346] This brings back MSN formatting support, for now at least. Tim is working on a patch to make per-message formatting (for MSN and other protocols that decided to be crappy in design) a reality, so this should let him test it out. committer: Tailor Script <tailor@pidgin.im>
author Christian Hammond <chipx86@chipx86.com>
date Tue, 06 Apr 2004 05:41:12 +0000
parents 45e8c6cbd4a5
children 1e211dde3cae
comparison
equal deleted inserted replaced
8594:f3b928825a72 8595:1d5e31e518fc
148 } 148 }
149 149
150 return buf; 150 return buf;
151 } 151 }
152 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 }