transport: clean up 407 response handling
[siplcs.git] / src / core / sipmsg.c
blob5e6cdff9e828058918b060f57316c6587761b314
1 /**
2 * @file sipmsg.c
4 * pidgin-sipe
6 * Copyright (C) 2010-2013 SIPE Project <http://sipe.sourceforge.net/>
7 * Copyright (C) 2008 Novell, Inc.
8 * Copyright (C) 2005 Thomas Butter <butter@uni-mannheim.de>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <time.h>
30 #include <glib.h>
32 #include "sipmsg.h"
33 #include "sipe-backend.h"
34 #include "sipe-mime.h"
35 #include "sipe-utils.h"
37 struct sipmsg *sipmsg_parse_msg(const gchar *msg) {
38 const char *tmp = strstr(msg, "\r\n\r\n");
39 char *line;
40 struct sipmsg *smsg;
42 if(!tmp) return NULL;
44 line = g_strndup(msg, tmp - msg);
46 smsg = sipmsg_parse_header(line);
47 smsg->body = g_strdup(tmp + 4);
49 g_free(line);
50 return smsg;
53 struct sipmsg *sipmsg_parse_header(const gchar *header) {
54 struct sipmsg *msg = g_new0(struct sipmsg,1);
55 gchar **lines = g_strsplit(header,"\r\n",0);
56 gchar **parts;
57 const gchar *contentlength;
58 if(!lines[0]) {
59 g_strfreev(lines);
60 g_free(msg);
61 return NULL;
63 parts = g_strsplit(lines[0], " ", 3);
64 if(!parts[0] || !parts[1] || !parts[2]) {
65 g_strfreev(parts);
66 g_strfreev(lines);
67 g_free(msg);
68 return NULL;
70 if(strstr(parts[0],"SIP") || strstr(parts[0],"HTTP")) { /* numeric response */
71 msg->responsestr = g_strdup(parts[2]);
72 msg->response = strtol(parts[1],NULL,10);
73 } else { /* request */
74 msg->method = g_strdup(parts[0]);
75 msg->target = g_strdup(parts[1]);
76 msg->response = 0;
78 g_strfreev(parts);
79 if (sipe_utils_parse_lines(&msg->headers, lines + 1, ":") == FALSE) {
80 g_strfreev(lines);
81 sipmsg_free(msg);
82 return NULL;
84 g_strfreev(lines);
85 contentlength = sipmsg_find_header(msg, "Content-Length");
86 if (contentlength) {
87 msg->bodylen = strtol(contentlength,NULL,10);
88 } else {
89 const gchar *tmp = sipmsg_find_header(msg, "Transfer-Encoding");
90 if (tmp && sipe_strcase_equal(tmp, "chunked")) {
91 msg->bodylen = SIPMSG_BODYLEN_CHUNKED;
92 } else {
93 SIPE_DEBUG_FATAL_NOFORMAT("sipmsg_parse_header(): Content-Length header not found");
96 if(msg->response) {
97 const gchar *tmp;
98 tmp = sipmsg_find_header(msg, "CSeq");
99 if(!tmp) {
100 /* SHOULD NOT HAPPEN */
101 msg->method = 0;
102 } else {
103 parts = g_strsplit(tmp, " ", 2);
104 msg->method = g_strdup(parts[1]);
105 g_strfreev(parts);
108 return msg;
111 struct sipmsg *sipmsg_copy(const struct sipmsg *other) {
112 struct sipmsg *msg = g_new0(struct sipmsg, 1);
113 GSList *list;
115 msg->response = other->response;
116 msg->responsestr = g_strdup(other->responsestr);
117 msg->method = g_strdup(other->method);
118 msg->target = g_strdup(other->target);
120 list = other->headers;
121 while(list) {
122 struct sipnameval *elem = list->data;
123 sipmsg_add_header_now(msg, elem->name, elem->value);
124 list = list->next;
127 list = other->new_headers;
128 while(list) {
129 struct sipnameval *elem = list->data;
130 sipmsg_add_header(msg, elem->name, elem->value);
131 list = list->next;
134 msg->bodylen = other->bodylen;
135 msg->body = g_strdup(other->body);
136 msg->signature = g_strdup(other->signature);
137 msg->rand = g_strdup(other->rand);
138 msg->num = g_strdup(other->num);
140 return msg;
143 char *sipmsg_to_string(const struct sipmsg *msg) {
144 GSList *cur;
145 GString *outstr = g_string_new("");
146 struct sipnameval *elem;
148 if(msg->response)
149 g_string_append_printf(outstr, "SIP/2.0 %d Unknown\r\n",
150 msg->response);
151 else
152 g_string_append_printf(outstr, "%s %s SIP/2.0\r\n",
153 msg->method, msg->target);
155 cur = msg->headers;
156 while(cur) {
157 elem = cur->data;
158 /*Todo: remove the LFCR in a good way*/
159 /*if(sipe_strequal(elem->name,"Proxy-Authorization"))
160 g_string_append_printf(outstr, "%s: %s", elem->name,
161 elem->value);
162 else */
163 g_string_append_printf(outstr, "%s: %s\r\n", elem->name,
164 elem->value);
165 cur = g_slist_next(cur);
168 g_string_append_printf(outstr, "\r\n%s", msg->bodylen ? msg->body : "");
170 return g_string_free(outstr, FALSE);
174 * Adds header to current message headers
176 void sipmsg_add_header_now(struct sipmsg *msg, const gchar *name, const gchar *value) {
177 struct sipnameval *element = g_new0(struct sipnameval,1);
179 /* SANITY CHECK: the calling code must be fixed if this happens! */
180 if (!value) {
181 SIPE_DEBUG_ERROR("sipmsg_add_header_now: NULL value for %s",
182 name);
183 value = "";
186 element->name = g_strdup(name);
187 element->value = g_strdup(value);
188 msg->headers = g_slist_append(msg->headers, element);
192 * Adds header to separate storage for future merge
194 void sipmsg_add_header(struct sipmsg *msg, const gchar *name, const gchar *value) {
195 struct sipnameval *element = g_new0(struct sipnameval,1);
197 /* SANITY CHECK: the calling code must be fixed if this happens! */
198 if (!value) {
199 SIPE_DEBUG_ERROR("sipmsg_add_header: NULL value for %s", name);
200 value = "";
203 element->name = g_strdup(name);
204 element->value = g_strdup(value);
205 msg->new_headers = g_slist_append(msg->new_headers, element);
209 * Removes header if it's not in keepers array
211 void sipmsg_strip_headers(struct sipmsg *msg, const gchar *keepers[]) {
212 GSList *entry;
213 struct sipnameval *elem;
215 entry = msg->headers;
216 while(entry) {
217 int i = 0;
218 gboolean keeper = FALSE;
220 elem = entry->data;
221 while (keepers[i]) {
222 if (!g_ascii_strcasecmp(elem->name, keepers[i])) {
223 keeper = TRUE;
224 break;
226 i++;
229 if (!keeper) {
230 GSList *to_delete = entry;
231 SIPE_DEBUG_INFO("sipmsg_strip_headers: removing %s", elem->name);
232 entry = g_slist_next(entry);
233 msg->headers = g_slist_delete_link(msg->headers, to_delete);
234 g_free(elem->name);
235 g_free(elem->value);
236 g_free(elem);
237 } else {
238 entry = g_slist_next(entry);
244 * Merges newly added headers to message
246 void sipmsg_merge_new_headers(struct sipmsg *msg) {
247 while(msg->new_headers) {
248 msg->headers = g_slist_append(msg->headers, msg->new_headers->data);
249 msg->new_headers = g_slist_remove(msg->new_headers, msg->new_headers->data);
253 void sipmsg_free(struct sipmsg *msg) {
254 if (msg) {
255 sipe_utils_nameval_free(msg->headers);
256 sipe_utils_nameval_free(msg->new_headers);
257 g_free(msg->signature);
258 g_free(msg->rand);
259 g_free(msg->num);
260 g_free(msg->responsestr);
261 g_free(msg->method);
262 g_free(msg->target);
263 g_free(msg->body);
264 g_free(msg);
268 void sipmsg_remove_header_now(struct sipmsg *msg, const gchar *name) {
269 struct sipnameval *elem;
270 GSList *tmp = msg->headers;
271 while(tmp) {
272 elem = tmp->data;
273 // OCS2005 can send the same header in either all caps or mixed case
274 if (sipe_strcase_equal(elem->name, name)) {
275 msg->headers = g_slist_remove(msg->headers, elem);
276 g_free(elem->name);
277 g_free(elem->value);
278 g_free(elem);
279 return;
281 tmp = g_slist_next(tmp);
283 return;
286 const gchar *sipmsg_find_header(const struct sipmsg *msg, const gchar *name) {
287 return sipe_utils_nameval_find_instance (msg->headers, name, 0);
290 const gchar *sipmsg_find_header_instance(const struct sipmsg *msg, const gchar *name, int which) {
291 return sipe_utils_nameval_find_instance(msg->headers, name, which);
294 gchar *sipmsg_find_part_of_header(const char *hdr, const char * before, const char * after, const char * def) {
295 const char *tmp;
296 const char *tmp2;
297 gchar *res2;
298 if (!hdr) {
299 return NULL;
302 //printf("partof %s w/ %s before and %s after\n", hdr, before, after);
304 tmp = before == NULL ? hdr : strstr(hdr, before);
305 if (!tmp) {
306 //printf ("not found, returning null\n");
307 return (gchar *)def;
310 if (before != NULL) {
311 tmp += strlen(before);
312 //printf ("tmp now %s\n", tmp);
315 if (after != NULL && (tmp2 = strstr(tmp, after))) {
316 gchar * res = g_strndup(tmp, tmp2 - tmp);
317 //printf("returning %s\n", res);
318 return res;
320 res2 = g_strdup(tmp);
321 //printf("returning %s\n", res2);
322 return res2;
325 int sipmsg_parse_cseq(struct sipmsg *msg)
327 int res = -1;
328 gchar **items;
329 items = g_strsplit(sipmsg_find_header(msg, "CSeq"), " ", 1);
330 if (items[0]) {
331 res = atoi(items[0]);
333 g_strfreev(items);
334 return res;
338 * Parse EndPoints header from INVITE request
339 * Returns a list of end points: contact URI plus optional epid.
340 * You must free the values and the list.
342 * Example headers:
343 * EndPoints: "alice alisson" <sip:alice@atlanta.local>, <sip:bob@atlanta.local>;epid=ebca82d94d, <sip:carol@atlanta.local>
344 * EndPoints: "alice, alisson" <sip:alice@atlanta.local>, <sip:bob@atlanta.local>
345 * EndPoints: "alice alisson" <sip:alice@atlanta.local>, "Super, Man" <sip:super@atlanta.local>
347 * @param header (in) EndPoints header contents
349 * @return GSList with struct sipendpoint as elements
351 GSList *sipmsg_parse_endpoints_header(const gchar *header)
353 GSList *list = NULL;
354 gchar **parts = g_strsplit(header, ",", 0);
355 gchar *part;
356 int i;
358 for (i = 0; (part = parts[i]) != NULL; i++) {
359 /* Does the part contain a URI? */
360 gchar *contact = sipmsg_find_part_of_header(part, "<", ">", NULL);
361 if (contact) {
362 struct sipendpoint *end_point = g_new(struct sipendpoint, 1);
363 end_point->contact = contact;
364 end_point->epid = sipmsg_find_part_of_header(part, "epid=", NULL, NULL);
365 list = g_slist_append(list, end_point);
368 g_strfreev(parts);
370 return(list);
373 void sipmsg_parse_p_asserted_identity(const gchar *header, gchar **sip_uri,
374 gchar **tel_uri) {
375 gchar **parts, **p;
377 *sip_uri = NULL;
378 *tel_uri = NULL;
380 if (g_ascii_strncasecmp(header, "tel:", 4) == 0) {
381 *tel_uri = g_strdup(header);
382 return;
385 parts = g_strsplit(header, ",", 0);
387 for (p = parts; *p; p++) {
388 gchar *uri = sipmsg_find_part_of_header(*p, "<", ">", NULL);
389 if (!uri)
390 continue;
392 if (g_ascii_strncasecmp(uri, "sip:", 4) == 0) {
393 if (*sip_uri) {
394 SIPE_DEBUG_WARNING_NOFORMAT("More than one "
395 "sip: URI found in P-Asserted-Identity!");
396 } else {
397 *sip_uri = uri;
398 uri = NULL;
400 } else if (g_ascii_strncasecmp(uri, "tel:", 4) == 0){
401 if (*tel_uri) {
402 SIPE_DEBUG_WARNING_NOFORMAT("More than one "
403 "tel: URI found in P-Asserted-Identity!");
404 } else {
405 *tel_uri = uri;
406 uri = NULL;
410 g_free(uri);
413 g_strfreev(parts);
417 * sipmsg_find_auth_header will return the particular WWW-Authenticate
418 * header specified by *name.
420 * Use this function when you want to look for a specific authentication
421 * method such as NTLM or Kerberos
424 const gchar *sipmsg_find_auth_header(struct sipmsg *msg, const gchar *name) {
425 GSList *tmp;
426 struct sipnameval *elem;
427 int name_len = strlen(name);
428 tmp = msg->headers;
429 while(tmp) {
430 elem = tmp->data;
431 /* SIPE_DEBUG_INFO("Current header: %s", elem->value); */
432 if (elem && elem->name &&
433 (sipe_strcase_equal(elem->name,"WWW-Authenticate") ||
434 sipe_strcase_equal(elem->name,"Authentication-Info")) ) {
435 if (!g_ascii_strncasecmp((gchar *)elem->value, name, name_len)) {
436 /* SIPE_DEBUG_INFO("elem->value: %s", elem->value); */
437 return elem->value;
440 /* SIPE_DEBUG_INFO_NOFORMAT("moving to next header"); */
441 tmp = g_slist_next(tmp);
443 SIPE_DEBUG_INFO("auth header '%s' not found.", name);
444 return NULL;
448 * Parses headers-like 'msgr' attribute of INVITE's 'ms_text_format' header.
449 * Then retrieves value of 'X-MMS-IM-Format'.
451 * 'msgr' typically looks like:
452 * X-MMS-IM-Format: FN=Microsoft%20Sans%20Serif; EF=BI; CO=800000; CS=0; PF=22
454 static gchar *sipmsg_get_x_mms_im_format(gchar *msgr) {
455 gchar *msgr2;
456 gsize msgr_dec64_len;
457 guchar *msgr_dec64;
458 gchar *msgr_utf8;
459 gchar **lines;
460 gchar **parts;
461 gchar *x_mms_im_format;
462 gchar *tmp;
464 if (!msgr) return NULL;
465 msgr2 = g_strdup(msgr);
466 while (strlen(msgr2) % 4 != 0) {
467 gchar *tmp_msgr2 = msgr2;
468 msgr2 = g_strdup_printf("%s=", msgr2);
469 g_free(tmp_msgr2);
471 msgr_dec64 = g_base64_decode(msgr2, &msgr_dec64_len);
472 msgr_utf8 = g_convert((gchar *) msgr_dec64, msgr_dec64_len, "UTF-8", "UTF-16LE", NULL, NULL, NULL);
473 g_free(msgr_dec64);
474 g_free(msgr2);
475 lines = g_strsplit(msgr_utf8,"\r\n\r\n",0);
476 g_free(msgr_utf8);
477 //@TODO: make extraction like parsing of message headers.
478 parts = g_strsplit(lines[0],"X-MMS-IM-Format:",0);
479 x_mms_im_format = g_strdup(parts[1]);
480 g_strfreev(parts);
481 g_strfreev(lines);
482 tmp = x_mms_im_format;
483 if (x_mms_im_format) {
484 while(*x_mms_im_format==' ' || *x_mms_im_format=='\t') x_mms_im_format++;
486 x_mms_im_format = g_strdup(x_mms_im_format);
487 g_free(tmp);
488 return x_mms_im_format;
491 gchar *sipmsg_get_msgr_string(gchar *x_mms_im_format) {
492 gchar *msgr_orig;
493 gsize msgr_utf16_len;
494 gchar *msgr_utf16;
495 gchar *msgr_enc;
496 gchar *res;
497 int len;
499 if (!x_mms_im_format) return NULL;
500 msgr_orig = g_strdup_printf("X-MMS-IM-Format: %s\r\n\r\n", x_mms_im_format);
501 msgr_utf16 = g_convert(msgr_orig, -1, "UTF-16LE", "UTF-8", NULL, &msgr_utf16_len, NULL);
502 g_free(msgr_orig);
503 msgr_enc = g_base64_encode((guchar *) msgr_utf16, msgr_utf16_len);
504 g_free(msgr_utf16);
505 len = strlen(msgr_enc);
506 while (msgr_enc[len - 1] == '=') len--;
507 res = g_strndup(msgr_enc, len);
508 g_free(msgr_enc);
509 return res;
512 static void msn_parse_format(const char *mime, char **pre_ret, char **post_ret);
515 * Translates X-MMS-IM format to HTML presentation.
517 static gchar *sipmsg_apply_x_mms_im_format(const char *x_mms_im_format, gchar *body) {
518 char *pre, *post;
519 gchar *res;
521 if (!x_mms_im_format) {
522 return body ? g_strdup(body) : NULL;
524 msn_parse_format(x_mms_im_format, &pre, &post);
525 res = g_strdup_printf("%s%s%s", pre ? pre : "", body ? body : "", post ? post : "");
526 g_free(pre);
527 g_free(post);
528 return res;
531 struct html_message_data {
532 gchar *ms_text_format;
533 gchar *body;
534 gboolean preferred;
537 static void get_html_message_mime_cb(gpointer user_data,
538 const GSList *fields,
539 const gchar *body,
540 gsize length)
542 const gchar *type = sipe_utils_nameval_find(fields, "Content-Type");
543 struct html_message_data *data = user_data;
545 if (!data->preferred) {
546 gboolean copy = FALSE;
548 /* preferred format */
549 if (g_str_has_prefix(type, "text/html")) {
550 copy = TRUE;
551 data->preferred = TRUE;
553 /* fallback format */
554 } else if (g_str_has_prefix(type, "text/plain")) {
555 copy = TRUE;
558 if (copy) {
559 g_free(data->ms_text_format);
560 g_free(data->body);
561 data->ms_text_format = g_strdup(type);
562 data->body = g_strndup(body, length);
567 /* ms-text-format: text/plain; charset=UTF-8;msgr=WAAtAE0...DIADQAKAA0ACgA;ms-body=SGk= */
568 gchar *get_html_message(const gchar *ms_text_format_in, const gchar *body_in)
570 gchar *msgr;
571 gchar *res;
572 gchar *ms_text_format = NULL;
573 gchar *body = NULL;
575 if (g_str_has_prefix(ms_text_format_in, "multipart/related") ||
576 g_str_has_prefix(ms_text_format_in, "multipart/alternative")) {
577 struct html_message_data data = { NULL, NULL, FALSE };
579 sipe_mime_parts_foreach(ms_text_format_in, body_in,
580 get_html_message_mime_cb, &data);
582 ms_text_format = data.ms_text_format;
583 body = data.body;
585 } else {
586 ms_text_format = g_strdup(ms_text_format_in);
587 body = g_strdup(body_in);
590 if (body) {
591 res = body;
592 } else {
593 gchar *tmp = sipmsg_find_part_of_header(ms_text_format, "ms-body=", NULL, NULL);
594 gsize len;
595 if (!tmp) {
596 g_free(ms_text_format);
597 return NULL;
599 res = (gchar *) g_base64_decode(tmp, &len);
600 g_free(tmp);
601 if (!res) {
602 g_free(ms_text_format);
603 return NULL;
607 if (g_str_has_prefix(ms_text_format, "text/html")) {
609 * HTML uses tags for formatting, not line breaks. But
610 * clients still might render them, so we need to remove
611 * them to avoid incorrect text rendering.
613 gchar *d = res;
614 const gchar *s = res;
615 gchar c;
617 /* No ANSI C nor glib function seems to exist for this :-( */
618 while ((c = *s++))
619 if ((c != '\n') && (c != '\r'))
620 *d++ = c;
621 *d = c;
623 } else {
624 char *tmp = res;
625 res = g_markup_escape_text(res, -1); // as this is not html
626 g_free(tmp);
629 msgr = sipmsg_find_part_of_header(ms_text_format, "msgr=", ";", NULL);
630 if (msgr) {
631 gchar *x_mms_im_format = sipmsg_get_x_mms_im_format(msgr);
632 gchar *tmp = res;
633 g_free(msgr);
634 res = sipmsg_apply_x_mms_im_format(x_mms_im_format, res);
635 g_free(tmp);
636 g_free(x_mms_im_format);
639 g_free(ms_text_format);
641 return res;
644 static gchar *
645 get_reason(struct sipmsg *msg, const gchar *header)
647 const gchar *diagnostics = sipmsg_find_header(msg, header);
648 if (diagnostics)
649 return sipmsg_find_part_of_header(diagnostics, "reason=\"", "\"", NULL);
651 return NULL;
654 gchar *
655 sipmsg_get_ms_diagnostics_reason(struct sipmsg *msg)
657 return get_reason(msg, "ms-diagnostics");
660 gchar *
661 sipmsg_get_ms_diagnostics_public_reason(struct sipmsg *msg)
663 return get_reason(msg, "ms-diagnostics-public");
667 sipmsg_parse_warning(struct sipmsg *msg, gchar **reason)
670 * Example header:
671 * Warning: 310 lcs.microsoft.com "You are currently not using the recommended version of the client"
673 const gchar *hdr = sipmsg_find_header(msg, "Warning");
674 int code = -1;
676 if (reason)
677 *reason = NULL;
679 if (hdr) {
680 gchar **parts = g_strsplit(hdr, " ", 3);
682 if (parts[0]) {
683 code = atoi(parts[0]);
685 if (reason && parts[1] && parts[2]) {
686 size_t len = strlen(parts[2]);
687 if (len > 2 && parts[2][0] == '"' && parts[2][len - 1] == '"')
688 *reason = g_strndup(parts[2] + 1, len - 2);
692 g_strfreev(parts);
695 return code;
700 //------------------------------------------------------------------------------------------
701 //TEMP solution to include it here (copy from purple's msn protocol
702 //How to reuse msn's util methods from sipe?
704 /* from internal.h */
705 #define MSG_LEN 2048
706 #define BUF_LEN MSG_LEN
708 void
709 msn_parse_format(const char *mime, char **pre_ret, char **post_ret)
711 char *cur;
712 GString *pre = g_string_new(NULL);
713 GString *post = g_string_new(NULL);
714 unsigned int colors[3];
716 if (pre_ret != NULL) *pre_ret = NULL;
717 if (post_ret != NULL) *post_ret = NULL;
719 cur = strstr(mime, "FN=");
721 if (cur && (*(cur = cur + 3) != ';'))
723 pre = g_string_append(pre, "<FONT FACE=\"");
725 while (*cur && *cur != ';')
727 pre = g_string_append_c(pre, *cur);
728 cur++;
731 pre = g_string_append(pre, "\">");
732 post = g_string_prepend(post, "</FONT>");
735 cur = strstr(mime, "EF=");
737 if (cur && (*(cur = cur + 3) != ';'))
739 while (*cur && *cur != ';')
741 pre = g_string_append_c(pre, '<');
742 pre = g_string_append_c(pre, *cur);
743 pre = g_string_append_c(pre, '>');
744 post = g_string_prepend_c(post, '>');
745 post = g_string_prepend_c(post, *cur);
746 post = g_string_prepend_c(post, '/');
747 post = g_string_prepend_c(post, '<');
748 cur++;
752 cur = strstr(mime, "CO=");
754 if (cur && (*(cur = cur + 3) != ';'))
756 int i;
758 i = sscanf(cur, "%02x%02x%02x;", &colors[0], &colors[1], &colors[2]);
760 if (i > 0)
762 char tag[64];
764 if (i == 1)
766 colors[1] = 0;
767 colors[2] = 0;
769 else if (i == 2)
771 unsigned int temp = colors[0];
773 colors[0] = colors[1];
774 colors[1] = temp;
775 colors[2] = 0;
777 else if (i == 3)
779 unsigned int temp = colors[2];
781 colors[2] = colors[0];
782 colors[0] = temp;
785 /* hh is undefined in mingw's gcc 4.4
786 * https://sourceforge.net/tracker/index.php?func=detail&aid=2818436&group_id=2435&atid=102435
788 g_snprintf(tag, sizeof(tag),
789 "<FONT COLOR=\"#%02x%02x%02x\">",
790 (unsigned char)colors[0], (unsigned char)colors[1], (unsigned char)colors[2]);
792 pre = g_string_append(pre, tag);
793 post = g_string_prepend(post, "</FONT>");
797 cur = strstr(mime, "RL=");
799 if (cur && (*(cur = cur + 3) != ';'))
801 if (*cur == '1')
803 /* RTL text was received */
804 pre = g_string_append(pre, "<SPAN style=\"direction:rtl;text-align:right;\">");
805 post = g_string_prepend(post, "</SPAN>");
809 cur = sipe_utils_uri_unescape(pre->str);
810 g_string_free(pre, TRUE);
812 if (pre_ret != NULL)
813 *pre_ret = cur;
814 else
815 g_free(cur);
817 cur = sipe_utils_uri_unescape(post->str);
818 g_string_free(post, TRUE);
820 if (post_ret != NULL)
821 *post_ret = cur;
822 else
823 g_free(cur);
826 static const char *
827 encode_spaces(const char *str)
829 static char buf[BUF_LEN];
830 const char *c;
831 char *d;
833 g_return_val_if_fail(str != NULL, NULL);
835 for (c = str, d = buf; *c != '\0'; c++)
837 if (*c == ' ')
839 *d++ = '%';
840 *d++ = '2';
841 *d++ = '0';
843 else
844 *d++ = *c;
846 *d = '\0';
848 return buf;
851 void
852 sipe_parse_html(const char *html, char **attributes, char **message)
854 int len, retcount = 0;
855 const char *c;
856 char *msg;
857 char *fontface = NULL;
858 char fonteffect[4];
859 char fontcolor[7];
860 char direction = '0';
862 gboolean has_bold = FALSE;
863 gboolean has_italic = FALSE;
864 gboolean has_underline = FALSE;
865 gboolean has_strikethrough = FALSE;
867 g_return_if_fail(html != NULL);
868 g_return_if_fail(attributes != NULL);
869 g_return_if_fail(message != NULL);
871 len = strlen(html);
872 msg = g_malloc0(len + 1);
874 memset(fontcolor, 0, sizeof(fontcolor));
875 strcat(fontcolor, "0");
876 memset(fonteffect, 0, sizeof(fonteffect));
878 for (c = html; *c != '\0';)
880 if (*c == '<')
882 if (!g_ascii_strncasecmp(c + 1, "br>", 3))
884 msg[retcount++] = '\r';
885 msg[retcount++] = '\n';
886 c += 4;
888 else if (!g_ascii_strncasecmp(c + 1, "i>", 2))
890 if (!has_italic)
892 strcat(fonteffect, "I");
893 has_italic = TRUE;
895 c += 3;
897 else if (!g_ascii_strncasecmp(c + 1, "b>", 2))
899 if (!has_bold)
901 strcat(fonteffect, "B");
902 has_bold = TRUE;
904 c += 3;
906 else if (!g_ascii_strncasecmp(c + 1, "u>", 2))
908 if (!has_underline)
910 strcat(fonteffect, "U");
911 has_underline = TRUE;
913 c += 3;
915 else if (!g_ascii_strncasecmp(c + 1, "s>", 2))
917 if (!has_strikethrough)
919 strcat(fonteffect, "S");
920 has_strikethrough = TRUE;
922 c += 3;
924 else if (!g_ascii_strncasecmp(c + 1, "a href=\"", 8))
926 c += 9;
928 if (!g_ascii_strncasecmp(c, "mailto:", 7))
929 c += 7;
931 while ((*c != '\0') && g_ascii_strncasecmp(c, "\">", 2))
932 msg[retcount++] = *c++;
934 if (*c != '\0')
935 c += 2;
937 /* ignore descriptive string */
938 while ((*c != '\0') && g_ascii_strncasecmp(c, "</a>", 4))
939 c++;
941 if (*c != '\0')
942 c += 4;
944 else if (!g_ascii_strncasecmp(c + 1, "span", 4))
946 /* Bi-directional text support using CSS properties in span tags */
947 c += 5;
949 while (*c != '\0' && *c != '>')
951 while (*c == ' ')
952 c++;
953 if (!g_ascii_strncasecmp(c, "dir=\"rtl\"", 9))
955 c += 9;
956 direction = '1';
958 else if (!g_ascii_strncasecmp(c, "style=\"", 7))
960 /* Parse inline CSS attributes */
961 int attr_len = 0;
962 c += 7;
963 while (*(c + attr_len) != '\0' && *(c + attr_len) != '"')
964 attr_len++;
965 if (*(c + attr_len) == '"')
967 char *css_attributes;
968 char *attr_dir;
969 css_attributes = g_strndup(c, attr_len);
970 attr_dir = sipe_backend_markup_css_property(css_attributes, "direction");
971 g_free(css_attributes);
972 if (attr_dir && (!g_ascii_strncasecmp(attr_dir, "RTL", 3)))
973 direction = '1';
974 g_free(attr_dir);
978 else
980 c++;
983 if (*c == '>')
984 c++;
986 else if (!g_ascii_strncasecmp(c + 1, "font", 4))
988 c += 5;
990 while ((*c != '\0') && !g_ascii_strncasecmp(c, " ", 1))
991 c++;
993 if (!g_ascii_strncasecmp(c, "color=\"#", 7))
995 c += 8;
997 fontcolor[0] = *(c + 4);
998 fontcolor[1] = *(c + 5);
999 fontcolor[2] = *(c + 2);
1000 fontcolor[3] = *(c + 3);
1001 fontcolor[4] = *c;
1002 fontcolor[5] = *(c + 1);
1004 c += 8;
1006 else if (!g_ascii_strncasecmp(c, "face=\"", 6))
1008 const char *end = NULL;
1009 const char *comma = NULL;
1010 unsigned int namelen = 0;
1012 c += 6;
1013 end = strchr(c, '\"');
1014 comma = strchr(c, ',');
1016 if (comma == NULL || comma > end)
1017 namelen = (unsigned int)(end - c);
1018 else
1019 namelen = (unsigned int)(comma - c);
1021 g_free(fontface);
1022 fontface = g_strndup(c, namelen);
1023 c = end + 2;
1025 else
1027 /* Drop all unrecognized/misparsed font tags */
1028 while ((*c != '\0') && g_ascii_strncasecmp(c, "\">", 2))
1029 c++;
1031 if (*c != '\0')
1032 c += 2;
1035 else
1037 while ((*c != '\0') && (*c != '>'))
1038 c++;
1039 if (*c != '\0')
1040 c++;
1043 else if (*c == '&')
1045 if (!g_ascii_strncasecmp(c, "&lt;", 4))
1047 msg[retcount++] = '<';
1048 c += 4;
1050 else if (!g_ascii_strncasecmp(c, "&gt;", 4))
1052 msg[retcount++] = '>';
1053 c += 4;
1055 else if (!g_ascii_strncasecmp(c, "&nbsp;", 6))
1057 msg[retcount++] = ' ';
1058 c += 6;
1060 else if (!g_ascii_strncasecmp(c, "&quot;", 6))
1062 msg[retcount++] = '"';
1063 c += 6;
1065 else if (!g_ascii_strncasecmp(c, "&amp;", 5))
1067 msg[retcount++] = '&';
1068 c += 5;
1070 else if (!g_ascii_strncasecmp(c, "&apos;", 6))
1072 msg[retcount++] = '\'';
1073 c += 6;
1075 else
1076 msg[retcount++] = *c++;
1078 else
1079 msg[retcount++] = *c++;
1082 if (fontface == NULL)
1083 fontface = g_strdup("MS Sans Serif");
1085 *attributes = g_strdup_printf("FN=%s; EF=%s; CO=%s; PF=0; RL=%c",
1086 encode_spaces(fontface),
1087 fonteffect, fontcolor, direction);
1088 *message = msg;
1090 g_free(fontface);
1092 // End of TEMP
1095 Local Variables:
1096 mode: c
1097 c-file-style: "bsd"
1098 indent-tabs-mode: t
1099 tab-width: 8
1100 End: