6 * Copyright (C) 2008 Novell, Inc.
7 * Copyright (C) 2005 Thomas Butter <butter@uni-mannheim.de>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include "sipe-utils.h"
34 struct sipmsg
*sipmsg_parse_msg(const gchar
*msg
) {
35 const char *tmp
= strstr(msg
, "\r\n\r\n");
41 line
= g_strndup(msg
, tmp
- msg
);
43 smsg
= sipmsg_parse_header(line
);
44 smsg
->body
= g_strdup(tmp
+ 4);
50 struct sipmsg
*sipmsg_parse_header(const gchar
*header
) {
51 struct sipmsg
*msg
= g_new0(struct sipmsg
,1);
52 gchar
**lines
= g_strsplit(header
,"\r\n",0);
54 const gchar
*contentlength
;
60 parts
= g_strsplit(lines
[0], " ", 3);
61 if(!parts
[0] || !parts
[1] || !parts
[2]) {
67 if(strstr(parts
[0],"SIP") || strstr(parts
[0],"HTTP")) { /* numeric response */
68 msg
->method
= g_strdup(parts
[2]);
69 msg
->response
= strtol(parts
[1],NULL
,10);
70 } else { /* request */
71 msg
->method
= g_strdup(parts
[0]);
72 msg
->target
= g_strdup(parts
[1]);
76 if (sipe_utils_parse_lines(&msg
->headers
,lines
+ 1) == FALSE
) {
82 contentlength
= sipmsg_find_header(msg
, "Content-Length");
84 msg
->bodylen
= strtol(contentlength
,NULL
,10);
86 purple_debug_fatal("sipe", "sipmsg_parse_header(): Content-Length header not found\n");
91 tmp
= sipmsg_find_header(msg
, "CSeq");
93 /* SHOULD NOT HAPPEN */
96 parts
= g_strsplit(tmp
, " ", 2);
97 msg
->method
= g_strdup(parts
[1]);
104 void sipmsg_print(const struct sipmsg
*msg
) {
106 struct sipnameval
*elem
;
107 purple_debug(PURPLE_DEBUG_MISC
, "sipe", "SIP MSG\n");
108 purple_debug(PURPLE_DEBUG_MISC
, "sipe", "response: %d\nmethod: %s\nbodylen: %d\n",msg
->response
,msg
->method
,msg
->bodylen
);
109 if(msg
->target
) purple_debug(PURPLE_DEBUG_MISC
, "sipe", "target: %s\n",msg
->target
);
113 purple_debug(PURPLE_DEBUG_MISC
, "sipe", "name: %s value: %s\n",elem
->name
, elem
->value
);
114 cur
= g_slist_next(cur
);
118 char *sipmsg_to_string(const struct sipmsg
*msg
) {
120 GString
*outstr
= g_string_new("");
121 struct sipnameval
*elem
;
124 g_string_append_printf(outstr
, "SIP/2.0 %d Unknown\r\n",
127 g_string_append_printf(outstr
, "%s %s SIP/2.0\r\n",
128 msg
->method
, msg
->target
);
133 /*Todo: remove the LFCR in a good way*/
134 /*if(sipe_strequal(elem->name,"Proxy-Authorization"))
135 g_string_append_printf(outstr, "%s: %s", elem->name,
138 g_string_append_printf(outstr
, "%s: %s\r\n", elem
->name
,
140 cur
= g_slist_next(cur
);
143 g_string_append_printf(outstr
, "\r\n%s", msg
->bodylen
? msg
->body
: "");
145 return g_string_free(outstr
, FALSE
);
149 * Adds header to current message headers at specified position
151 void sipmsg_add_header_now_pos(struct sipmsg
*msg
, const gchar
*name
, const gchar
*value
, int pos
) {
152 struct sipnameval
*element
= g_new0(struct sipnameval
,1);
154 /* SANITY CHECK: the calling code must be fixed if this happens! */
156 purple_debug(PURPLE_DEBUG_ERROR
, "sipe", "sipmsg_add_header_now_pos: NULL value for %s (%d)\n",
161 element
->name
= g_strdup(name
);
162 element
->value
= g_strdup(value
);
163 msg
->headers
= g_slist_insert(msg
->headers
, element
,pos
);
167 * Adds header to current message headers
169 void sipmsg_add_header_now(struct sipmsg
*msg
, const gchar
*name
, const gchar
*value
) {
170 struct sipnameval
*element
= g_new0(struct sipnameval
,1);
172 /* SANITY CHECK: the calling code must be fixed if this happens! */
174 purple_debug(PURPLE_DEBUG_ERROR
, "sipe", "sipmsg_add_header_now: NULL value for %s\n",
179 element
->name
= g_strdup(name
);
180 element
->value
= g_strdup(value
);
181 msg
->headers
= g_slist_append(msg
->headers
, element
);
185 * Adds header to separate storage for future merge
187 void sipmsg_add_header(struct sipmsg
*msg
, const gchar
*name
, const gchar
*value
) {
188 struct sipnameval
*element
= g_new0(struct sipnameval
,1);
190 /* SANITY CHECK: the calling code must be fixed if this happens! */
192 purple_debug(PURPLE_DEBUG_ERROR
, "sipe", "sipmsg_add_header: NULL value for %s\n",
197 element
->name
= g_strdup(name
);
198 element
->value
= g_strdup(value
);
199 msg
->new_headers
= g_slist_append(msg
->new_headers
, element
);
203 * Removes header if it's not in keepers array
205 void sipmsg_strip_headers(struct sipmsg
*msg
, const gchar
*keepers
[]) {
207 struct sipnameval
*elem
;
209 entry
= msg
->headers
;
212 gboolean keeper
= FALSE
;
216 if (!g_strcasecmp(elem
->name
, keepers
[i
])) {
224 GSList
*to_delete
= entry
;
225 purple_debug_info("sipe", "sipmsg_strip_headers: removing %s\n", elem
->name
);
226 entry
= g_slist_next(entry
);
227 msg
->headers
= g_slist_delete_link(msg
->headers
, to_delete
);
232 entry
= g_slist_next(entry
);
238 * Merges newly added headers to message
240 void sipmsg_merge_new_headers(struct sipmsg
*msg
) {
241 while(msg
->new_headers
) {
242 msg
->headers
= g_slist_append(msg
->headers
, msg
->new_headers
->data
);
243 msg
->new_headers
= g_slist_remove(msg
->new_headers
, msg
->new_headers
->data
);
247 void sipmsg_free(struct sipmsg
*msg
) {
248 sipe_utils_nameval_free(msg
->headers
);
249 sipe_utils_nameval_free(msg
->new_headers
);
250 g_free(msg
->signature
);
259 void sipmsg_remove_header_now(struct sipmsg
*msg
, const gchar
*name
) {
260 struct sipnameval
*elem
;
261 GSList
*tmp
= msg
->headers
;
264 // OCS2005 can send the same header in either all caps or mixed case
265 if (g_ascii_strcasecmp(elem
->name
, name
)==0) {
266 msg
->headers
= g_slist_remove(msg
->headers
, elem
);
272 tmp
= g_slist_next(tmp
);
277 const gchar
*sipmsg_find_header(const struct sipmsg
*msg
, const gchar
*name
) {
278 return sipe_utils_nameval_find_instance (msg
->headers
, name
, 0);
281 const gchar
*sipmsg_find_header_instance(const struct sipmsg
*msg
, const gchar
*name
, int which
) {
282 return sipe_utils_nameval_find_instance(msg
->headers
, name
, which
);
285 gchar
*sipmsg_find_part_of_header(const char *hdr
, const char * before
, const char * after
, const char * def
) {
293 //printf("partof %s w/ %s before and %s after\n", hdr, before, after);
295 tmp
= before
== NULL
? hdr
: strstr(hdr
, before
);
297 //printf ("not found, returning null\n");
301 if (before
!= NULL
) {
302 tmp
+= strlen(before
);
303 //printf ("tmp now %s\n", tmp);
306 if (after
!= NULL
&& (tmp2
= strstr(tmp
, after
))) {
307 gchar
* res
= g_strndup(tmp
, tmp2
- tmp
);
308 //printf("returning %s\n", res);
311 res2
= g_strdup(tmp
);
312 //printf("returning %s\n", res2);
317 * Parse EndPoints header from INVITE request
318 * Returns a list of end points: contact URI plus optional epid.
319 * You must free the values and the list.
322 * EndPoints: "alice alisson" <sip:alice@atlanta.local>, <sip:bob@atlanta.local>;epid=ebca82d94d, <sip:carol@atlanta.local>
323 * EndPoints: "alice, alisson" <sip:alice@atlanta.local>, <sip:bob@atlanta.local>
324 * EndPoints: "alice alisson" <sip:alice@atlanta.local>, "Super, Man" <sip:super@atlanta.local>
326 * @param header (in) EndPoints header contents
328 * @return GSList with struct sipendpoint as elements
330 GSList
*sipmsg_parse_endpoints_header(const gchar
*header
)
333 gchar
**parts
= g_strsplit(header
, ",", 0);
337 for (i
= 0; (part
= parts
[i
]) != NULL
; i
++) {
338 /* Does the part contain a URI? */
339 gchar
*contact
= sipmsg_find_part_of_header(part
, "<", ">", NULL
);
341 struct sipendpoint
*end_point
= g_new(struct sipendpoint
, 1);
342 end_point
->contact
= contact
;
343 end_point
->epid
= sipmsg_find_part_of_header(part
, "epid=", NULL
, NULL
);
344 list
= g_slist_append(list
, end_point
);
353 * sipmsg_find_auth_header will return the particular WWW-Authenticate
354 * header specified by *name.
356 * Use this function when you want to look for a specific authentication
357 * method such as NTLM or Kerberos
360 gchar
*sipmsg_find_auth_header(struct sipmsg
*msg
, const gchar
*name
) {
362 struct sipnameval
*elem
;
363 int name_len
= strlen(name
);
367 //purple_debug(PURPLE_DEBUG_MISC, "sipmsg", "Current header: %s\r\n", elem->value);
368 if (elem
&& elem
->name
&&
369 (!g_ascii_strcasecmp(elem
->name
,"WWW-Authenticate")
370 || !g_ascii_strcasecmp(elem
->name
,"Authentication-Info")) ) {
371 if (!g_strncasecmp((gchar
*)elem
->value
, name
, name_len
)) {
372 //purple_debug(PURPLE_DEBUG_MISC, "sipmsg", "elem->value: %s\r\n", elem->value);
376 //purple_debug(PURPLE_DEBUG_MISC, "sipmsg", "moving to next header\r\n");
377 tmp
= g_slist_next(tmp
);
379 purple_debug(PURPLE_DEBUG_MISC
, "sipmsg", "Did not found auth header %s\r\n", name
);
383 gchar
*sipmsg_get_x_mms_im_format(gchar
*msgr
) {
385 gsize msgr_dec64_len
;
390 gchar
*x_mms_im_format
;
393 if (!msgr
) return NULL
;
394 msgr2
= g_strdup(msgr
);
395 while (strlen(msgr2
) % 4 != 0) {
396 gchar
*tmp_msgr2
= msgr2
;
397 msgr2
= g_strdup_printf("%s=", msgr2
);
400 msgr_dec64
= purple_base64_decode(msgr2
, &msgr_dec64_len
);
401 msgr_utf8
= g_convert((gchar
*) msgr_dec64
, msgr_dec64_len
, "UTF-8", "UTF-16LE", NULL
, NULL
, NULL
);
404 lines
= g_strsplit(msgr_utf8
,"\r\n\r\n",0);
406 //@TODO: make extraction like parsing of message headers.
407 parts
= g_strsplit(lines
[0],"X-MMS-IM-Format:",0);
408 x_mms_im_format
= g_strdup(parts
[1]);
411 tmp
= x_mms_im_format
;
412 if (x_mms_im_format
) {
413 while(*x_mms_im_format
==' ' || *x_mms_im_format
=='\t') x_mms_im_format
++;
415 x_mms_im_format
= g_strdup(x_mms_im_format
);
417 return x_mms_im_format
;
420 gchar
*sipmsg_get_msgr_string(gchar
*x_mms_im_format
) {
422 gsize msgr_utf16_len
;
428 if (!x_mms_im_format
) return NULL
;
429 msgr_orig
= g_strdup_printf("X-MMS-IM-Format: %s\r\n\r\n", x_mms_im_format
);
430 msgr_utf16
= g_convert(msgr_orig
, -1, "UTF-16LE", "UTF-8", NULL
, &msgr_utf16_len
, NULL
);
432 msgr_enc
= purple_base64_encode((guchar
*) msgr_utf16
, msgr_utf16_len
);
434 len
= strlen(msgr_enc
);
435 while (msgr_enc
[len
- 1] == '=') len
--;
436 res
= g_strndup(msgr_enc
, len
);
441 gchar
*sipmsg_apply_x_mms_im_format(const char *x_mms_im_format
, gchar
*body
) {
445 if (!x_mms_im_format
) {
446 return body
? g_strdup(body
) : NULL
;
448 msn_parse_format(x_mms_im_format
, &pre
, &post
);
449 res
= g_strdup_printf("%s%s%s", pre
? pre
: "", body
? body
: "", post
? post
: "");
455 /* ms-text-format: text/plain; charset=UTF-8;msgr=WAAtAE0...DIADQAKAA0ACgA;ms-body=SGk= */
456 gchar
*get_html_message(const gchar
*ms_text_format_in
, const gchar
*body_in
)
460 gchar
*ms_text_format
= NULL
;
463 if (g_str_has_prefix(ms_text_format_in
, "multipart/related") ||
464 g_str_has_prefix(ms_text_format_in
, "multipart/alternative")) {
465 char *doc
= g_strdup_printf("Content-Type: %s\r\n\r\n%s", ms_text_format_in
, body_in
);
466 PurpleMimeDocument
*mime
;
469 mime
= purple_mime_document_parse(doc
);
470 parts
= purple_mime_document_get_parts(mime
);
472 const gchar
*content_type
= purple_mime_part_get_field(parts
->data
, "Content-Type");
474 const gchar
*content
= purple_mime_part_get_data(parts
->data
);
475 guint length
= purple_mime_part_get_length(parts
->data
);
477 /* if no other format has stored */
478 if (!ms_text_format
&& g_str_has_prefix(content_type
, "text/plain")) {
479 ms_text_format
= g_strdup(content_type
);
480 body
= g_strndup(content
, length
);
481 /* preferred format */
482 } else if (g_str_has_prefix(ms_text_format
, "text/html")) {
483 g_free(ms_text_format
);
485 ms_text_format
= g_strdup(content_type
);
486 body
= g_strndup(content
, length
);
494 purple_mime_document_free(mime
);
496 ms_text_format
= g_strdup(ms_text_format_in
);
497 body
= g_strdup(body_in
);
503 gchar
*tmp
= sipmsg_find_part_of_header(ms_text_format
, "ms-body=", NULL
, NULL
);
505 g_free(ms_text_format
);
508 res
= (gchar
*) purple_base64_decode(tmp
, NULL
);
511 g_free(ms_text_format
);
516 if (!g_str_has_prefix(ms_text_format
, "text/html")) { // NOT html
518 res
= g_markup_escape_text(res
, -1); // as this is not html
522 msgr
= sipmsg_find_part_of_header(ms_text_format
, "msgr=", ";", NULL
);
524 gchar
*x_mms_im_format
= sipmsg_get_x_mms_im_format(msgr
);
527 res
= sipmsg_apply_x_mms_im_format(x_mms_im_format
, res
);
529 g_free(x_mms_im_format
);
532 g_free(ms_text_format
);
541 //------------------------------------------------------------------------------------------
542 //TEMP solution to include it here (copy from purple's msn protocol
543 //How to reuse msn's util methods from sipe?
545 /* from internal.h */
547 #define BUF_LEN MSG_LEN
550 msn_parse_format(const char *mime
, char **pre_ret
, char **post_ret
)
553 GString
*pre
= g_string_new(NULL
);
554 GString
*post
= g_string_new(NULL
);
555 unsigned int colors
[3];
557 if (pre_ret
!= NULL
) *pre_ret
= NULL
;
558 if (post_ret
!= NULL
) *post_ret
= NULL
;
560 cur
= strstr(mime
, "FN=");
562 if (cur
&& (*(cur
= cur
+ 3) != ';'))
564 pre
= g_string_append(pre
, "<FONT FACE=\"");
566 while (*cur
&& *cur
!= ';')
568 pre
= g_string_append_c(pre
, *cur
);
572 pre
= g_string_append(pre
, "\">");
573 post
= g_string_prepend(post
, "</FONT>");
576 cur
= strstr(mime
, "EF=");
578 if (cur
&& (*(cur
= cur
+ 3) != ';'))
580 while (*cur
&& *cur
!= ';')
582 pre
= g_string_append_c(pre
, '<');
583 pre
= g_string_append_c(pre
, *cur
);
584 pre
= g_string_append_c(pre
, '>');
585 post
= g_string_prepend_c(post
, '>');
586 post
= g_string_prepend_c(post
, *cur
);
587 post
= g_string_prepend_c(post
, '/');
588 post
= g_string_prepend_c(post
, '<');
593 cur
= strstr(mime
, "CO=");
595 if (cur
&& (*(cur
= cur
+ 3) != ';'))
599 i
= sscanf(cur
, "%02x%02x%02x;", &colors
[0], &colors
[1], &colors
[2]);
612 unsigned int temp
= colors
[0];
614 colors
[0] = colors
[1];
620 unsigned int temp
= colors
[2];
622 colors
[2] = colors
[0];
626 g_snprintf(tag
, sizeof(tag
),
627 "<FONT COLOR=\"#%02hhx%02hhx%02hhx\">",
628 colors
[0], colors
[1], colors
[2]);
630 pre
= g_string_append(pre
, tag
);
631 post
= g_string_prepend(post
, "</FONT>");
635 cur
= strstr(mime
, "RL=");
637 if (cur
&& (*(cur
= cur
+ 3) != ';'))
641 /* RTL text was received */
642 pre
= g_string_append(pre
, "<SPAN style=\"direction:rtl;text-align:right;\">");
643 post
= g_string_prepend(post
, "</SPAN>");
647 cur
= g_strdup(purple_url_decode(pre
->str
));
648 g_string_free(pre
, TRUE
);
655 cur
= g_strdup(purple_url_decode(post
->str
));
656 g_string_free(post
, TRUE
);
658 if (post_ret
!= NULL
)
665 encode_spaces(const char *str
)
667 static char buf
[BUF_LEN
];
671 g_return_val_if_fail(str
!= NULL
, NULL
);
673 for (c
= str
, d
= buf
; *c
!= '\0'; c
++)
690 msn_import_html(const char *html
, char **attributes
, char **message
)
692 int len
, retcount
= 0;
695 char *fontface
= NULL
;
698 char direction
= '0';
700 gboolean has_bold
= FALSE
;
701 gboolean has_italic
= FALSE
;
702 gboolean has_underline
= FALSE
;
703 gboolean has_strikethrough
= FALSE
;
705 g_return_if_fail(html
!= NULL
);
706 g_return_if_fail(attributes
!= NULL
);
707 g_return_if_fail(message
!= NULL
);
710 msg
= g_malloc0(len
+ 1);
712 memset(fontcolor
, 0, sizeof(fontcolor
));
713 strcat(fontcolor
, "0");
714 memset(fonteffect
, 0, sizeof(fonteffect
));
716 for (c
= html
; *c
!= '\0';)
720 if (!g_ascii_strncasecmp(c
+ 1, "br>", 3))
722 msg
[retcount
++] = '\r';
723 msg
[retcount
++] = '\n';
726 else if (!g_ascii_strncasecmp(c
+ 1, "i>", 2))
730 strcat(fonteffect
, "I");
735 else if (!g_ascii_strncasecmp(c
+ 1, "b>", 2))
739 strcat(fonteffect
, "B");
744 else if (!g_ascii_strncasecmp(c
+ 1, "u>", 2))
748 strcat(fonteffect
, "U");
749 has_underline
= TRUE
;
753 else if (!g_ascii_strncasecmp(c
+ 1, "s>", 2))
755 if (!has_strikethrough
)
757 strcat(fonteffect
, "S");
758 has_strikethrough
= TRUE
;
762 else if (!g_ascii_strncasecmp(c
+ 1, "a href=\"", 8))
766 if (!g_ascii_strncasecmp(c
, "mailto:", 7))
769 while ((*c
!= '\0') && g_ascii_strncasecmp(c
, "\">", 2))
770 msg
[retcount
++] = *c
++;
775 /* ignore descriptive string */
776 while ((*c
!= '\0') && g_ascii_strncasecmp(c
, "</a>", 4))
782 else if (!g_ascii_strncasecmp(c
+ 1, "span", 4))
784 /* Bi-directional text support using CSS properties in span tags */
787 while (*c
!= '\0' && *c
!= '>')
791 if (!g_ascii_strncasecmp(c
, "dir=\"rtl\"", 9))
796 else if (!g_ascii_strncasecmp(c
, "style=\"", 7))
798 /* Parse inline CSS attributes */
801 while (*(c
+ attr_len
) != '\0' && *(c
+ attr_len
) != '"')
803 if (*(c
+ attr_len
) == '"')
805 char *css_attributes
;
807 css_attributes
= g_strndup(c
, attr_len
);
808 attr_dir
= purple_markup_get_css_property(css_attributes
, "direction");
809 g_free(css_attributes
);
810 if (attr_dir
&& (!g_ascii_strncasecmp(attr_dir
, "RTL", 3)))
824 else if (!g_ascii_strncasecmp(c
+ 1, "font", 4))
828 while ((*c
!= '\0') && !g_ascii_strncasecmp(c
, " ", 1))
831 if (!g_ascii_strncasecmp(c
, "color=\"#", 7))
835 fontcolor
[0] = *(c
+ 4);
836 fontcolor
[1] = *(c
+ 5);
837 fontcolor
[2] = *(c
+ 2);
838 fontcolor
[3] = *(c
+ 3);
840 fontcolor
[5] = *(c
+ 1);
844 else if (!g_ascii_strncasecmp(c
, "face=\"", 6))
846 const char *end
= NULL
;
847 const char *comma
= NULL
;
848 unsigned int namelen
= 0;
851 end
= strchr(c
, '\"');
852 comma
= strchr(c
, ',');
854 if (comma
== NULL
|| comma
> end
)
855 namelen
= (unsigned int)(end
- c
);
857 namelen
= (unsigned int)(comma
- c
);
860 fontface
= g_strndup(c
, namelen
);
865 /* Drop all unrecognized/misparsed font tags */
866 while ((*c
!= '\0') && g_ascii_strncasecmp(c
, "\">", 2))
875 while ((*c
!= '\0') && (*c
!= '>'))
883 if (!g_ascii_strncasecmp(c
, "<", 4))
885 msg
[retcount
++] = '<';
888 else if (!g_ascii_strncasecmp(c
, ">", 4))
890 msg
[retcount
++] = '>';
893 else if (!g_ascii_strncasecmp(c
, " ", 6))
895 msg
[retcount
++] = ' ';
898 else if (!g_ascii_strncasecmp(c
, """, 6))
900 msg
[retcount
++] = '"';
903 else if (!g_ascii_strncasecmp(c
, "&", 5))
905 msg
[retcount
++] = '&';
908 else if (!g_ascii_strncasecmp(c
, "'", 6))
910 msg
[retcount
++] = '\'';
914 msg
[retcount
++] = *c
++;
917 msg
[retcount
++] = *c
++;
920 if (fontface
== NULL
)
921 fontface
= g_strdup("MS Sans Serif");
923 *attributes
= g_strdup_printf("FN=%s; EF=%s; CO=%s; PF=0; RL=%c",
924 encode_spaces(fontface
),
925 fonteffect
, fontcolor
, direction
);