http: refactor connection data structure handling
[siplcs.git] / src / core / sipmsg.c
blob189b7b94d43147010eb8b8eb693aa3635492b4d9
1 /**
2 * @file sipmsg.c
4 * pidgin-sipe
6 * Copyright (C) 2010-11 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 at specified position
176 void sipmsg_add_header_now_pos(struct sipmsg *msg, const gchar *name, const gchar *value, int pos) {
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_pos: NULL value for %s (%d)",
182 name, pos);
183 value = "";
186 element->name = g_strdup(name);
187 element->value = g_strdup(value);
188 msg->headers = g_slist_insert(msg->headers, element,pos);
192 * Adds header to current message headers
194 void sipmsg_add_header_now(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_now: NULL value for %s",
200 name);
201 value = "";
204 element->name = g_strdup(name);
205 element->value = g_strdup(value);
206 msg->headers = g_slist_append(msg->headers, element);
210 * Adds header to separate storage for future merge
212 void sipmsg_add_header(struct sipmsg *msg, const gchar *name, const gchar *value) {
213 struct sipnameval *element = g_new0(struct sipnameval,1);
215 /* SANITY CHECK: the calling code must be fixed if this happens! */
216 if (!value) {
217 SIPE_DEBUG_ERROR("sipmsg_add_header: NULL value for %s", name);
218 value = "";
221 element->name = g_strdup(name);
222 element->value = g_strdup(value);
223 msg->new_headers = g_slist_append(msg->new_headers, element);
227 * Removes header if it's not in keepers array
229 void sipmsg_strip_headers(struct sipmsg *msg, const gchar *keepers[]) {
230 GSList *entry;
231 struct sipnameval *elem;
233 entry = msg->headers;
234 while(entry) {
235 int i = 0;
236 gboolean keeper = FALSE;
238 elem = entry->data;
239 while (keepers[i]) {
240 if (!g_ascii_strcasecmp(elem->name, keepers[i])) {
241 keeper = TRUE;
242 break;
244 i++;
247 if (!keeper) {
248 GSList *to_delete = entry;
249 SIPE_DEBUG_INFO("sipmsg_strip_headers: removing %s", elem->name);
250 entry = g_slist_next(entry);
251 msg->headers = g_slist_delete_link(msg->headers, to_delete);
252 g_free(elem->name);
253 g_free(elem->value);
254 g_free(elem);
255 } else {
256 entry = g_slist_next(entry);
262 * Merges newly added headers to message
264 void sipmsg_merge_new_headers(struct sipmsg *msg) {
265 while(msg->new_headers) {
266 msg->headers = g_slist_append(msg->headers, msg->new_headers->data);
267 msg->new_headers = g_slist_remove(msg->new_headers, msg->new_headers->data);
271 void sipmsg_free(struct sipmsg *msg) {
272 if (msg) {
273 sipe_utils_nameval_free(msg->headers);
274 sipe_utils_nameval_free(msg->new_headers);
275 g_free(msg->signature);
276 g_free(msg->rand);
277 g_free(msg->num);
278 g_free(msg->responsestr);
279 g_free(msg->method);
280 g_free(msg->target);
281 g_free(msg->body);
282 g_free(msg);
286 void sipmsg_remove_header_now(struct sipmsg *msg, const gchar *name) {
287 struct sipnameval *elem;
288 GSList *tmp = msg->headers;
289 while(tmp) {
290 elem = tmp->data;
291 // OCS2005 can send the same header in either all caps or mixed case
292 if (sipe_strcase_equal(elem->name, name)) {
293 msg->headers = g_slist_remove(msg->headers, elem);
294 g_free(elem->name);
295 g_free(elem->value);
296 g_free(elem);
297 return;
299 tmp = g_slist_next(tmp);
301 return;
304 const gchar *sipmsg_find_header(const struct sipmsg *msg, const gchar *name) {
305 return sipe_utils_nameval_find_instance (msg->headers, name, 0);
308 const gchar *sipmsg_find_header_instance(const struct sipmsg *msg, const gchar *name, int which) {
309 return sipe_utils_nameval_find_instance(msg->headers, name, which);
312 gchar *sipmsg_find_part_of_header(const char *hdr, const char * before, const char * after, const char * def) {
313 const char *tmp;
314 const char *tmp2;
315 gchar *res2;
316 if (!hdr) {
317 return NULL;
320 //printf("partof %s w/ %s before and %s after\n", hdr, before, after);
322 tmp = before == NULL ? hdr : strstr(hdr, before);
323 if (!tmp) {
324 //printf ("not found, returning null\n");
325 return (gchar *)def;
328 if (before != NULL) {
329 tmp += strlen(before);
330 //printf ("tmp now %s\n", tmp);
333 if (after != NULL && (tmp2 = strstr(tmp, after))) {
334 gchar * res = g_strndup(tmp, tmp2 - tmp);
335 //printf("returning %s\n", res);
336 return res;
338 res2 = g_strdup(tmp);
339 //printf("returning %s\n", res2);
340 return res2;
343 int sipmsg_parse_cseq(struct sipmsg *msg)
345 int res = -1;
346 gchar **items;
347 items = g_strsplit(sipmsg_find_header(msg, "CSeq"), " ", 1);
348 if (items[0]) {
349 res = atoi(items[0]);
351 g_strfreev(items);
352 return res;
356 * Parse EndPoints header from INVITE request
357 * Returns a list of end points: contact URI plus optional epid.
358 * You must free the values and the list.
360 * Example headers:
361 * EndPoints: "alice alisson" <sip:alice@atlanta.local>, <sip:bob@atlanta.local>;epid=ebca82d94d, <sip:carol@atlanta.local>
362 * EndPoints: "alice, alisson" <sip:alice@atlanta.local>, <sip:bob@atlanta.local>
363 * EndPoints: "alice alisson" <sip:alice@atlanta.local>, "Super, Man" <sip:super@atlanta.local>
365 * @param header (in) EndPoints header contents
367 * @return GSList with struct sipendpoint as elements
369 GSList *sipmsg_parse_endpoints_header(const gchar *header)
371 GSList *list = NULL;
372 gchar **parts = g_strsplit(header, ",", 0);
373 gchar *part;
374 int i;
376 for (i = 0; (part = parts[i]) != NULL; i++) {
377 /* Does the part contain a URI? */
378 gchar *contact = sipmsg_find_part_of_header(part, "<", ">", NULL);
379 if (contact) {
380 struct sipendpoint *end_point = g_new(struct sipendpoint, 1);
381 end_point->contact = contact;
382 end_point->epid = sipmsg_find_part_of_header(part, "epid=", NULL, NULL);
383 list = g_slist_append(list, end_point);
386 g_strfreev(parts);
388 return(list);
391 void sipmsg_parse_p_asserted_identity(const gchar *header, gchar **sip_uri,
392 gchar **tel_uri) {
393 gchar **parts, **p;
395 *sip_uri = NULL;
396 *tel_uri = NULL;
398 if (g_ascii_strncasecmp(header, "tel:", 4) == 0) {
399 *tel_uri = g_strdup(header);
400 return;
403 parts = g_strsplit(header, ",", 0);
405 for (p = parts; *p; p++) {
406 gchar *uri = sipmsg_find_part_of_header(*p, "<", ">", NULL);
407 if (!uri)
408 continue;
410 if (g_ascii_strncasecmp(uri, "sip:", 4) == 0) {
411 if (*sip_uri) {
412 SIPE_DEBUG_WARNING_NOFORMAT("More than one "
413 "sip: URI found in P-Asserted-Identity!");
414 } else {
415 *sip_uri = uri;
416 uri = NULL;
418 } else if (g_ascii_strncasecmp(uri, "tel:", 4) == 0){
419 if (*tel_uri) {
420 SIPE_DEBUG_WARNING_NOFORMAT("More than one "
421 "tel: URI found in P-Asserted-Identity!");
422 } else {
423 *tel_uri = uri;
424 uri = NULL;
428 g_free(uri);
431 g_strfreev(parts);
435 * sipmsg_find_auth_header will return the particular WWW-Authenticate
436 * header specified by *name.
438 * Use this function when you want to look for a specific authentication
439 * method such as NTLM or Kerberos
442 const gchar *sipmsg_find_auth_header(struct sipmsg *msg, const gchar *name) {
443 GSList *tmp;
444 struct sipnameval *elem;
445 int name_len = strlen(name);
446 tmp = msg->headers;
447 while(tmp) {
448 elem = tmp->data;
449 /* SIPE_DEBUG_INFO("Current header: %s", elem->value); */
450 if (elem && elem->name &&
451 (sipe_strcase_equal(elem->name,"WWW-Authenticate") ||
452 sipe_strcase_equal(elem->name,"Authentication-Info")) ) {
453 if (!g_ascii_strncasecmp((gchar *)elem->value, name, name_len)) {
454 /* SIPE_DEBUG_INFO("elem->value: %s", elem->value); */
455 return elem->value;
458 /* SIPE_DEBUG_INFO_NOFORMAT("moving to next header"); */
459 tmp = g_slist_next(tmp);
461 SIPE_DEBUG_INFO("auth header '%s' not found.", name);
462 return NULL;
466 * Parses headers-like 'msgr' attribute of INVITE's 'ms_text_format' header.
467 * Then retrieves value of 'X-MMS-IM-Format'.
469 * 'msgr' typically looks like:
470 * X-MMS-IM-Format: FN=Microsoft%20Sans%20Serif; EF=BI; CO=800000; CS=0; PF=22
472 static gchar *sipmsg_get_x_mms_im_format(gchar *msgr) {
473 gchar *msgr2;
474 gsize msgr_dec64_len;
475 guchar *msgr_dec64;
476 gchar *msgr_utf8;
477 gchar **lines;
478 gchar **parts;
479 gchar *x_mms_im_format;
480 gchar *tmp;
482 if (!msgr) return NULL;
483 msgr2 = g_strdup(msgr);
484 while (strlen(msgr2) % 4 != 0) {
485 gchar *tmp_msgr2 = msgr2;
486 msgr2 = g_strdup_printf("%s=", msgr2);
487 g_free(tmp_msgr2);
489 msgr_dec64 = g_base64_decode(msgr2, &msgr_dec64_len);
490 msgr_utf8 = g_convert((gchar *) msgr_dec64, msgr_dec64_len, "UTF-8", "UTF-16LE", NULL, NULL, NULL);
491 g_free(msgr_dec64);
492 g_free(msgr2);
493 lines = g_strsplit(msgr_utf8,"\r\n\r\n",0);
494 g_free(msgr_utf8);
495 //@TODO: make extraction like parsing of message headers.
496 parts = g_strsplit(lines[0],"X-MMS-IM-Format:",0);
497 x_mms_im_format = g_strdup(parts[1]);
498 g_strfreev(parts);
499 g_strfreev(lines);
500 tmp = x_mms_im_format;
501 if (x_mms_im_format) {
502 while(*x_mms_im_format==' ' || *x_mms_im_format=='\t') x_mms_im_format++;
504 x_mms_im_format = g_strdup(x_mms_im_format);
505 g_free(tmp);
506 return x_mms_im_format;
509 gchar *sipmsg_get_msgr_string(gchar *x_mms_im_format) {
510 gchar *msgr_orig;
511 gsize msgr_utf16_len;
512 gchar *msgr_utf16;
513 gchar *msgr_enc;
514 gchar *res;
515 int len;
517 if (!x_mms_im_format) return NULL;
518 msgr_orig = g_strdup_printf("X-MMS-IM-Format: %s\r\n\r\n", x_mms_im_format);
519 msgr_utf16 = g_convert(msgr_orig, -1, "UTF-16LE", "UTF-8", NULL, &msgr_utf16_len, NULL);
520 g_free(msgr_orig);
521 msgr_enc = g_base64_encode((guchar *) msgr_utf16, msgr_utf16_len);
522 g_free(msgr_utf16);
523 len = strlen(msgr_enc);
524 while (msgr_enc[len - 1] == '=') len--;
525 res = g_strndup(msgr_enc, len);
526 g_free(msgr_enc);
527 return res;
530 static void msn_parse_format(const char *mime, char **pre_ret, char **post_ret);
533 * Translates X-MMS-IM format to HTML presentation.
535 static gchar *sipmsg_apply_x_mms_im_format(const char *x_mms_im_format, gchar *body) {
536 char *pre, *post;
537 gchar *res;
539 if (!x_mms_im_format) {
540 return body ? g_strdup(body) : NULL;
542 msn_parse_format(x_mms_im_format, &pre, &post);
543 res = g_strdup_printf("%s%s%s", pre ? pre : "", body ? body : "", post ? post : "");
544 g_free(pre);
545 g_free(post);
546 return res;
549 struct html_message_data {
550 gchar *ms_text_format;
551 gchar *body;
552 gboolean preferred;
555 static void get_html_message_mime_cb(gpointer user_data,
556 const GSList *fields,
557 const gchar *body,
558 gsize length)
560 const gchar *type = sipe_utils_nameval_find(fields, "Content-Type");
561 struct html_message_data *data = user_data;
563 if (!data->preferred) {
564 gboolean copy = FALSE;
566 /* preferred format */
567 if (g_str_has_prefix(type, "text/html")) {
568 copy = TRUE;
569 data->preferred = TRUE;
571 /* fallback format */
572 } else if (g_str_has_prefix(type, "text/plain")) {
573 copy = TRUE;
576 if (copy) {
577 g_free(data->ms_text_format);
578 g_free(data->body);
579 data->ms_text_format = g_strdup(type);
580 data->body = g_strndup(body, length);
585 /* ms-text-format: text/plain; charset=UTF-8;msgr=WAAtAE0...DIADQAKAA0ACgA;ms-body=SGk= */
586 gchar *get_html_message(const gchar *ms_text_format_in, const gchar *body_in)
588 gchar *msgr;
589 gchar *res;
590 gchar *ms_text_format = NULL;
591 gchar *body = NULL;
593 if (g_str_has_prefix(ms_text_format_in, "multipart/related") ||
594 g_str_has_prefix(ms_text_format_in, "multipart/alternative")) {
595 struct html_message_data data = { NULL, NULL, FALSE };
597 sipe_mime_parts_foreach(ms_text_format_in, body_in,
598 get_html_message_mime_cb, &data);
600 ms_text_format = data.ms_text_format;
601 body = data.body;
603 } else {
604 ms_text_format = g_strdup(ms_text_format_in);
605 body = g_strdup(body_in);
608 if (body) {
609 res = body;
610 } else {
611 gchar *tmp = sipmsg_find_part_of_header(ms_text_format, "ms-body=", NULL, NULL);
612 gsize len;
613 if (!tmp) {
614 g_free(ms_text_format);
615 return NULL;
617 res = (gchar *) g_base64_decode(tmp, &len);
618 g_free(tmp);
619 if (!res) {
620 g_free(ms_text_format);
621 return NULL;
625 if (g_str_has_prefix(ms_text_format, "text/html")) {
627 * HTML uses tags for formatting, not line breaks. But
628 * clients still might render them, so we need to remove
629 * them to avoid incorrect text rendering.
631 gchar *d = res;
632 const gchar *s = res;
633 gchar c;
635 /* No ANSI C nor glib function seems to exist for this :-( */
636 while ((c = *s++))
637 if ((c != '\n') && (c != '\r'))
638 *d++ = c;
639 *d = c;
641 } else {
642 char *tmp = res;
643 res = g_markup_escape_text(res, -1); // as this is not html
644 g_free(tmp);
647 msgr = sipmsg_find_part_of_header(ms_text_format, "msgr=", ";", NULL);
648 if (msgr) {
649 gchar *x_mms_im_format = sipmsg_get_x_mms_im_format(msgr);
650 gchar *tmp = res;
651 g_free(msgr);
652 res = sipmsg_apply_x_mms_im_format(x_mms_im_format, res);
653 g_free(tmp);
654 g_free(x_mms_im_format);
657 g_free(ms_text_format);
659 return res;
662 static gchar *
663 get_reason(struct sipmsg *msg, const gchar *header)
665 const gchar *diagnostics = sipmsg_find_header(msg, header);
666 if (diagnostics)
667 return sipmsg_find_part_of_header(diagnostics, "reason=\"", "\"", NULL);
669 return NULL;
672 gchar *
673 sipmsg_get_ms_diagnostics_reason(struct sipmsg *msg)
675 return get_reason(msg, "ms-diagnostics");
678 gchar *
679 sipmsg_get_ms_diagnostics_public_reason(struct sipmsg *msg)
681 return get_reason(msg, "ms-diagnostics-public");
685 sipmsg_parse_warning(struct sipmsg *msg, gchar **reason)
688 * Example header:
689 * Warning: 310 lcs.microsoft.com "You are currently not using the recommended version of the client"
691 const gchar *hdr = sipmsg_find_header(msg, "Warning");
692 int code = -1;
694 if (reason)
695 *reason = NULL;
697 if (hdr) {
698 gchar **parts = g_strsplit(hdr, " ", 3);
700 if (parts[0]) {
701 code = atoi(parts[0]);
703 if (reason && parts[1] && parts[2]) {
704 size_t len = strlen(parts[2]);
705 if (len > 2 && parts[2][0] == '"' && parts[2][len - 1] == '"')
706 *reason = g_strndup(parts[2] + 1, len - 2);
710 g_strfreev(parts);
713 return code;
718 //------------------------------------------------------------------------------------------
719 //TEMP solution to include it here (copy from purple's msn protocol
720 //How to reuse msn's util methods from sipe?
722 /* from internal.h */
723 #define MSG_LEN 2048
724 #define BUF_LEN MSG_LEN
726 void
727 msn_parse_format(const char *mime, char **pre_ret, char **post_ret)
729 char *cur;
730 GString *pre = g_string_new(NULL);
731 GString *post = g_string_new(NULL);
732 unsigned int colors[3];
734 if (pre_ret != NULL) *pre_ret = NULL;
735 if (post_ret != NULL) *post_ret = NULL;
737 cur = strstr(mime, "FN=");
739 if (cur && (*(cur = cur + 3) != ';'))
741 pre = g_string_append(pre, "<FONT FACE=\"");
743 while (*cur && *cur != ';')
745 pre = g_string_append_c(pre, *cur);
746 cur++;
749 pre = g_string_append(pre, "\">");
750 post = g_string_prepend(post, "</FONT>");
753 cur = strstr(mime, "EF=");
755 if (cur && (*(cur = cur + 3) != ';'))
757 while (*cur && *cur != ';')
759 pre = g_string_append_c(pre, '<');
760 pre = g_string_append_c(pre, *cur);
761 pre = g_string_append_c(pre, '>');
762 post = g_string_prepend_c(post, '>');
763 post = g_string_prepend_c(post, *cur);
764 post = g_string_prepend_c(post, '/');
765 post = g_string_prepend_c(post, '<');
766 cur++;
770 cur = strstr(mime, "CO=");
772 if (cur && (*(cur = cur + 3) != ';'))
774 int i;
776 i = sscanf(cur, "%02x%02x%02x;", &colors[0], &colors[1], &colors[2]);
778 if (i > 0)
780 char tag[64];
782 if (i == 1)
784 colors[1] = 0;
785 colors[2] = 0;
787 else if (i == 2)
789 unsigned int temp = colors[0];
791 colors[0] = colors[1];
792 colors[1] = temp;
793 colors[2] = 0;
795 else if (i == 3)
797 unsigned int temp = colors[2];
799 colors[2] = colors[0];
800 colors[0] = temp;
803 /* hh is undefined in mingw's gcc 4.4
804 * https://sourceforge.net/tracker/index.php?func=detail&aid=2818436&group_id=2435&atid=102435
806 g_snprintf(tag, sizeof(tag),
807 "<FONT COLOR=\"#%02x%02x%02x\">",
808 (unsigned char)colors[0], (unsigned char)colors[1], (unsigned char)colors[2]);
810 pre = g_string_append(pre, tag);
811 post = g_string_prepend(post, "</FONT>");
815 cur = strstr(mime, "RL=");
817 if (cur && (*(cur = cur + 3) != ';'))
819 if (*cur == '1')
821 /* RTL text was received */
822 pre = g_string_append(pre, "<SPAN style=\"direction:rtl;text-align:right;\">");
823 post = g_string_prepend(post, "</SPAN>");
827 cur = sipe_utils_uri_unescape(pre->str);
828 g_string_free(pre, TRUE);
830 if (pre_ret != NULL)
831 *pre_ret = cur;
832 else
833 g_free(cur);
835 cur = sipe_utils_uri_unescape(post->str);
836 g_string_free(post, TRUE);
838 if (post_ret != NULL)
839 *post_ret = cur;
840 else
841 g_free(cur);
844 static const char *
845 encode_spaces(const char *str)
847 static char buf[BUF_LEN];
848 const char *c;
849 char *d;
851 g_return_val_if_fail(str != NULL, NULL);
853 for (c = str, d = buf; *c != '\0'; c++)
855 if (*c == ' ')
857 *d++ = '%';
858 *d++ = '2';
859 *d++ = '0';
861 else
862 *d++ = *c;
864 *d = '\0';
866 return buf;
869 void
870 sipe_parse_html(const char *html, char **attributes, char **message)
872 int len, retcount = 0;
873 const char *c;
874 char *msg;
875 char *fontface = NULL;
876 char fonteffect[4];
877 char fontcolor[7];
878 char direction = '0';
880 gboolean has_bold = FALSE;
881 gboolean has_italic = FALSE;
882 gboolean has_underline = FALSE;
883 gboolean has_strikethrough = FALSE;
885 g_return_if_fail(html != NULL);
886 g_return_if_fail(attributes != NULL);
887 g_return_if_fail(message != NULL);
889 len = strlen(html);
890 msg = g_malloc0(len + 1);
892 memset(fontcolor, 0, sizeof(fontcolor));
893 strcat(fontcolor, "0");
894 memset(fonteffect, 0, sizeof(fonteffect));
896 for (c = html; *c != '\0';)
898 if (*c == '<')
900 if (!g_ascii_strncasecmp(c + 1, "br>", 3))
902 msg[retcount++] = '\r';
903 msg[retcount++] = '\n';
904 c += 4;
906 else if (!g_ascii_strncasecmp(c + 1, "i>", 2))
908 if (!has_italic)
910 strcat(fonteffect, "I");
911 has_italic = TRUE;
913 c += 3;
915 else if (!g_ascii_strncasecmp(c + 1, "b>", 2))
917 if (!has_bold)
919 strcat(fonteffect, "B");
920 has_bold = TRUE;
922 c += 3;
924 else if (!g_ascii_strncasecmp(c + 1, "u>", 2))
926 if (!has_underline)
928 strcat(fonteffect, "U");
929 has_underline = TRUE;
931 c += 3;
933 else if (!g_ascii_strncasecmp(c + 1, "s>", 2))
935 if (!has_strikethrough)
937 strcat(fonteffect, "S");
938 has_strikethrough = TRUE;
940 c += 3;
942 else if (!g_ascii_strncasecmp(c + 1, "a href=\"", 8))
944 c += 9;
946 if (!g_ascii_strncasecmp(c, "mailto:", 7))
947 c += 7;
949 while ((*c != '\0') && g_ascii_strncasecmp(c, "\">", 2))
950 msg[retcount++] = *c++;
952 if (*c != '\0')
953 c += 2;
955 /* ignore descriptive string */
956 while ((*c != '\0') && g_ascii_strncasecmp(c, "</a>", 4))
957 c++;
959 if (*c != '\0')
960 c += 4;
962 else if (!g_ascii_strncasecmp(c + 1, "span", 4))
964 /* Bi-directional text support using CSS properties in span tags */
965 c += 5;
967 while (*c != '\0' && *c != '>')
969 while (*c == ' ')
970 c++;
971 if (!g_ascii_strncasecmp(c, "dir=\"rtl\"", 9))
973 c += 9;
974 direction = '1';
976 else if (!g_ascii_strncasecmp(c, "style=\"", 7))
978 /* Parse inline CSS attributes */
979 int attr_len = 0;
980 c += 7;
981 while (*(c + attr_len) != '\0' && *(c + attr_len) != '"')
982 attr_len++;
983 if (*(c + attr_len) == '"')
985 char *css_attributes;
986 char *attr_dir;
987 css_attributes = g_strndup(c, attr_len);
988 attr_dir = sipe_backend_markup_css_property(css_attributes, "direction");
989 g_free(css_attributes);
990 if (attr_dir && (!g_ascii_strncasecmp(attr_dir, "RTL", 3)))
991 direction = '1';
992 g_free(attr_dir);
996 else
998 c++;
1001 if (*c == '>')
1002 c++;
1004 else if (!g_ascii_strncasecmp(c + 1, "font", 4))
1006 c += 5;
1008 while ((*c != '\0') && !g_ascii_strncasecmp(c, " ", 1))
1009 c++;
1011 if (!g_ascii_strncasecmp(c, "color=\"#", 7))
1013 c += 8;
1015 fontcolor[0] = *(c + 4);
1016 fontcolor[1] = *(c + 5);
1017 fontcolor[2] = *(c + 2);
1018 fontcolor[3] = *(c + 3);
1019 fontcolor[4] = *c;
1020 fontcolor[5] = *(c + 1);
1022 c += 8;
1024 else if (!g_ascii_strncasecmp(c, "face=\"", 6))
1026 const char *end = NULL;
1027 const char *comma = NULL;
1028 unsigned int namelen = 0;
1030 c += 6;
1031 end = strchr(c, '\"');
1032 comma = strchr(c, ',');
1034 if (comma == NULL || comma > end)
1035 namelen = (unsigned int)(end - c);
1036 else
1037 namelen = (unsigned int)(comma - c);
1039 g_free(fontface);
1040 fontface = g_strndup(c, namelen);
1041 c = end + 2;
1043 else
1045 /* Drop all unrecognized/misparsed font tags */
1046 while ((*c != '\0') && g_ascii_strncasecmp(c, "\">", 2))
1047 c++;
1049 if (*c != '\0')
1050 c += 2;
1053 else
1055 while ((*c != '\0') && (*c != '>'))
1056 c++;
1057 if (*c != '\0')
1058 c++;
1061 else if (*c == '&')
1063 if (!g_ascii_strncasecmp(c, "&lt;", 4))
1065 msg[retcount++] = '<';
1066 c += 4;
1068 else if (!g_ascii_strncasecmp(c, "&gt;", 4))
1070 msg[retcount++] = '>';
1071 c += 4;
1073 else if (!g_ascii_strncasecmp(c, "&nbsp;", 6))
1075 msg[retcount++] = ' ';
1076 c += 6;
1078 else if (!g_ascii_strncasecmp(c, "&quot;", 6))
1080 msg[retcount++] = '"';
1081 c += 6;
1083 else if (!g_ascii_strncasecmp(c, "&amp;", 5))
1085 msg[retcount++] = '&';
1086 c += 5;
1088 else if (!g_ascii_strncasecmp(c, "&apos;", 6))
1090 msg[retcount++] = '\'';
1091 c += 6;
1093 else
1094 msg[retcount++] = *c++;
1096 else
1097 msg[retcount++] = *c++;
1100 if (fontface == NULL)
1101 fontface = g_strdup("MS Sans Serif");
1103 *attributes = g_strdup_printf("FN=%s; EF=%s; CO=%s; PF=0; RL=%c",
1104 encode_spaces(fontface),
1105 fonteffect, fontcolor, direction);
1106 *message = msg;
1108 g_free(fontface);
1110 // End of TEMP
1113 Local Variables:
1114 mode: c
1115 c-file-style: "bsd"
1116 indent-tabs-mode: t
1117 tab-width: 8
1118 End: