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
25 #include <sys/socket.h>
26 #include <sys/ioctl.h>
27 #include <sys/types.h>
28 #include <netinet/in.h>
34 #define _LIBC_INTERNAL_
42 #include "accountopt.h"
44 #include "conversation.h"
55 struct sipmsg
*sipmsg_parse_msg(const gchar
*msg
) {
56 const char *tmp
= strstr(msg
, "\r\n\r\n");
62 line
= g_strndup(msg
, tmp
- msg
);
64 smsg
= sipmsg_parse_header(line
);
65 smsg
->body
= g_strdup(tmp
+ 4);
71 struct sipmsg
*sipmsg_parse_header(const gchar
*header
) {
72 struct sipmsg
*msg
= g_new0(struct sipmsg
,1);
73 gchar
**lines
= g_strsplit(header
,"\r\n",0);
80 if(!lines
[0]) return NULL
;
81 parts
= g_strsplit(lines
[0], " ", 3);
82 if(!parts
[0] || !parts
[1] || !parts
[2]) {
88 if(strstr(parts
[0],"SIP")) { /* numeric response */
89 msg
->method
= g_strdup(parts
[2]);
90 msg
->response
= strtol(parts
[1],NULL
,10);
91 } else { /* request */
92 msg
->method
= g_strdup(parts
[0]);
93 msg
->target
= g_strdup(parts
[1]);
97 for(i
=1; lines
[i
] && strlen(lines
[i
])>2; i
++) {
98 parts
= g_strsplit(lines
[i
], ":", 2);
99 if(!parts
[0] || !parts
[1]) {
107 while(*dummy
==' ' || *dummy
=='\t') dummy
++;
108 dummy2
= g_strdup(dummy
);
109 while(lines
[i
+1] && (lines
[i
+1][0]==' ' || lines
[i
+1][0]=='\t')) {
112 while(*dummy
==' ' || *dummy
=='\t') dummy
++;
113 tmp
= g_strdup_printf("%s %s",dummy2
, dummy
);
117 sipmsg_add_header_now(msg
, parts
[0], dummy2
);
122 contentlength
= sipmsg_find_header(msg
, "Content-Length");
124 msg
->bodylen
= strtol(contentlength
,NULL
,10);
126 purple_debug_fatal("sipe", "sipmsg_parse_header(): Content-Length header not found\n");
130 tmp
= sipmsg_find_header(msg
, "CSeq");
132 /* SHOULD NOT HAPPEN */
135 parts
= g_strsplit(tmp
, " ", 2);
136 msg
->method
= g_strdup(parts
[1]);
143 void sipmsg_print(const struct sipmsg
*msg
) {
145 struct siphdrelement
*elem
;
146 purple_debug(PURPLE_DEBUG_MISC
, "sipe", "SIP MSG\n");
147 purple_debug(PURPLE_DEBUG_MISC
, "sipe", "response: %d\nmethod: %s\nbodylen: %d\n",msg
->response
,msg
->method
,msg
->bodylen
);
148 if(msg
->target
) purple_debug(PURPLE_DEBUG_MISC
, "sipe", "target: %s\n",msg
->target
);
152 purple_debug(PURPLE_DEBUG_MISC
, "sipe", "name: %s value: %s\n",elem
->name
, elem
->value
);
153 cur
= g_slist_next(cur
);
157 char *sipmsg_to_string(const struct sipmsg
*msg
) {
159 GString
*outstr
= g_string_new("");
160 struct siphdrelement
*elem
;
163 g_string_append_printf(outstr
, "SIP/2.0 %d Unknown\r\n",
166 g_string_append_printf(outstr
, "%s %s SIP/2.0\r\n",
167 msg
->method
, msg
->target
);
172 /*Todo: remove the LFCR in a good way*/
173 /*if(!strcmp(elem->name,"Proxy-Authorization"))
174 g_string_append_printf(outstr, "%s: %s", elem->name,
177 g_string_append_printf(outstr
, "%s: %s\r\n", elem
->name
,
179 cur
= g_slist_next(cur
);
182 g_string_append_printf(outstr
, "\r\n%s", msg
->bodylen
? msg
->body
: "");
184 return g_string_free(outstr
, FALSE
);
188 * Adds header to current message headers at specified position
190 void sipmsg_add_header_now_pos(struct sipmsg
*msg
, const gchar
*name
, const gchar
*value
, int pos
) {
191 struct siphdrelement
*element
= g_new0(struct siphdrelement
,1);
192 element
->name
= g_strdup(name
);
193 element
->value
= g_strdup(value
);
194 msg
->headers
= g_slist_insert(msg
->headers
, element
,pos
);
198 * Adds header to current message headers
200 void sipmsg_add_header_now(struct sipmsg
*msg
, const gchar
*name
, const gchar
*value
) {
201 struct siphdrelement
*element
= g_new0(struct siphdrelement
,1);
202 element
->name
= g_strdup(name
);
203 element
->value
= g_strdup(value
);
204 msg
->headers
= g_slist_append(msg
->headers
, element
);
208 * Adds header to separate storage for future merge
210 void sipmsg_add_header(struct sipmsg
*msg
, const gchar
*name
, const gchar
*value
) {
211 struct siphdrelement
*element
= g_new0(struct siphdrelement
,1);
212 element
->name
= g_strdup(name
);
213 element
->value
= g_strdup(value
);
214 msg
->new_headers
= g_slist_append(msg
->new_headers
, element
);
218 * Removes header if it's not in keepers array
220 void sipmsg_strip_headers(struct sipmsg
*msg
, const gchar
*keepers
[]) {
222 struct siphdrelement
*elem
;
224 entry
= msg
->headers
;
227 gboolean keeper
= FALSE
;
231 if (!g_strcasecmp(elem
->name
, keepers
[i
])) {
239 GSList
*to_delete
= entry
;
240 purple_debug_info("sipe", "sipmsg_strip_headers: removing %s\n", elem
->name
);
241 entry
= g_slist_next(entry
);
242 msg
->headers
= g_slist_delete_link(msg
->headers
, to_delete
);
247 entry
= g_slist_next(entry
);
253 * Merges newly added headers to message
255 void sipmsg_merge_new_headers(struct sipmsg
*msg
) {
256 while(msg
->new_headers
) {
257 msg
->headers
= g_slist_append(msg
->headers
, msg
->new_headers
->data
);
258 msg
->new_headers
= g_slist_remove(msg
->new_headers
, msg
->new_headers
->data
);
262 void sipmsg_free(struct sipmsg
*msg
) {
263 struct siphdrelement
*elem
;
264 while(msg
->headers
) {
265 elem
= msg
->headers
->data
;
266 msg
->headers
= g_slist_remove(msg
->headers
,elem
);
271 while(msg
->new_headers
) {
272 elem
= msg
->new_headers
->data
;
273 msg
->new_headers
= g_slist_remove(msg
->new_headers
,elem
);
278 g_free(msg
->signature
);
287 void sipmsg_remove_header_now(struct sipmsg
*msg
, const gchar
*name
) {
288 struct siphdrelement
*elem
;
289 GSList
*tmp
= msg
->headers
;
292 // OCS2005 can send the same header in either all caps or mixed case
293 if (g_ascii_strcasecmp(elem
->name
, name
)==0) {
294 msg
->headers
= g_slist_remove(msg
->headers
, elem
);
300 tmp
= g_slist_next(tmp
);
305 gchar
*sipmsg_find_header(const struct sipmsg
*msg
, const gchar
*name
) {
306 return sipmsg_find_header_instance (msg
, name
, 0);
309 gchar
*sipmsg_find_header_instance(const struct sipmsg
*msg
, const gchar
*name
, int which
) {
311 struct siphdrelement
*elem
;
316 // OCS2005 can send the same header in either all caps or mixed case
317 if (g_ascii_strcasecmp(elem
->name
,name
)==0) {
323 tmp
= g_slist_next(tmp
);
328 gchar
*sipmsg_find_part_of_header(const char *hdr
, const char * before
, const char * after
, const char * def
) {
336 //printf("partof %s w/ %s before and %s after\n", hdr, before, after);
338 tmp
= before
== NULL
? hdr
: strstr(hdr
, before
);
340 //printf ("not found, returning null\n");
344 if (before
!= NULL
) {
345 tmp
+= strlen(before
);
346 //printf ("tmp now %s\n", tmp);
349 if (after
!= NULL
&& (tmp2
= strstr(tmp
, after
))) {
350 gchar
* res
= g_strndup(tmp
, tmp2
- tmp
);
351 //printf("returning %s\n", res);
354 res2
= g_strdup(tmp
);
355 //printf("returning %s\n", res2);
360 * Parse EndPoints header from INVITE request
361 * Returns a list of end points: contact URI plus optional epid.
362 * You must free the values and the list.
365 * EndPoints: "alice alisson" <sip:alice@atlanta.local>, <sip:bob@atlanta.local>;epid=ebca82d94d, <sip:carol@atlanta.local>
366 * EndPoints: "alice, alisson" <sip:alice@atlanta.local>, <sip:bob@atlanta.local>
367 * EndPoints: "alice alisson" <sip:alice@atlanta.local>, "Super, Man" <sip:super@atlanta.local>
369 * @param header (in) EndPoints header contents
371 * @return GSList with struct sipendpoint as elements
373 GSList
*sipmsg_parse_endpoints_header(const gchar
*header
)
376 gchar
**parts
= g_strsplit(header
, ",", 0);
380 for (i
= 0; (part
= parts
[i
]) != NULL
; i
++) {
381 /* Does the part contain a URI? */
382 gchar
*contact
= sipmsg_find_part_of_header(part
, "<", ">", NULL
);
384 struct sipendpoint
*end_point
= g_new(struct sipendpoint
, 1);
385 end_point
->contact
= contact
;
386 end_point
->epid
= sipmsg_find_part_of_header(part
, "epid=", NULL
, NULL
);
387 list
= g_slist_append(list
, end_point
);
396 * sipmsg_find_auth_header will return the particular WWW-Authenticate
397 * header specified by *name.
399 * Use this function when you want to look for a specific authentication
400 * method such as NTLM or Kerberos
403 gchar
*sipmsg_find_auth_header(struct sipmsg
*msg
, const gchar
*name
) {
405 struct siphdrelement
*elem
;
406 int name_len
= strlen(name
);
410 //purple_debug(PURPLE_DEBUG_MISC, "sipmsg", "Current header: %s\r\n", elem->value);
411 if (elem
&& elem
->name
&&
412 (!g_ascii_strcasecmp(elem
->name
,"WWW-Authenticate")
413 || !g_ascii_strcasecmp(elem
->name
,"Authentication-Info")) ) {
414 if (!g_strncasecmp((gchar
*)elem
->value
, name
, name_len
)) {
415 //purple_debug(PURPLE_DEBUG_MISC, "sipmsg", "elem->value: %s\r\n", elem->value);
419 //purple_debug(PURPLE_DEBUG_MISC, "sipmsg", "moving to next header\r\n");
420 tmp
= g_slist_next(tmp
);
422 purple_debug(PURPLE_DEBUG_MISC
, "sipmsg", "Did not found auth header %s\r\n", name
);
426 gchar
*sipmsg_get_x_mms_im_format(gchar
*msgr
) {
428 gsize msgr_dec64_len
;
433 gchar
*x_mms_im_format
;
436 if (!msgr
) return NULL
;
437 msgr2
= g_strdup(msgr
);
438 while (strlen(msgr2
) % 4 != 0) {
439 gchar
*tmp_msgr2
= msgr2
;
440 msgr2
= g_strdup_printf("%s=", msgr2
);
443 msgr_dec64
= purple_base64_decode(msgr2
, &msgr_dec64_len
);
444 msgr_utf8
= g_convert((gchar
*) msgr_dec64
, msgr_dec64_len
, "UTF-8", "UTF-16LE", NULL
, NULL
, NULL
);
447 lines
= g_strsplit(msgr_utf8
,"\r\n\r\n",0);
449 //@TODO: make extraction like parsing of message headers.
450 parts
= g_strsplit(lines
[0],"X-MMS-IM-Format:",0);
451 x_mms_im_format
= g_strdup(parts
[1]);
454 tmp
= x_mms_im_format
;
455 if (x_mms_im_format
) {
456 while(*x_mms_im_format
==' ' || *x_mms_im_format
=='\t') x_mms_im_format
++;
458 x_mms_im_format
= g_strdup(x_mms_im_format
);
460 return x_mms_im_format
;
463 gchar
*sipmsg_get_msgr_string(gchar
*x_mms_im_format
) {
465 gsize msgr_utf16_len
;
471 if (!x_mms_im_format
) return NULL
;
472 msgr_orig
= g_strdup_printf("X-MMS-IM-Format: %s\r\n\r\n", x_mms_im_format
);
473 msgr_utf16
= g_convert(msgr_orig
, -1, "UTF-16LE", "UTF-8", NULL
, &msgr_utf16_len
, NULL
);
475 msgr_enc
= purple_base64_encode((guchar
*) msgr_utf16
, msgr_utf16_len
);
477 len
= strlen(msgr_enc
);
478 while (msgr_enc
[len
- 1] == '=') len
--;
479 res
= g_strndup(msgr_enc
, len
);
484 gchar
*sipmsg_apply_x_mms_im_format(const char *x_mms_im_format
, gchar
*body
) {
488 if (!x_mms_im_format
) {
489 return body
? g_strdup(body
) : NULL
;
491 msn_parse_format(x_mms_im_format
, &pre
, &post
);
492 res
= g_strdup_printf("%s%s%s", pre
? pre
: "", body
? body
: "", post
? post
: "");
498 //ms-text-format: text/plain; charset=UTF-8;msgr=WAAtAE0...DIADQAKAA0ACgA;ms-body=SGk=
499 gchar
*get_html_message(const gchar
*ms_text_format
, const gchar
*body
)
506 res
= g_strdup(body
);
508 res
= sipmsg_find_part_of_header(ms_text_format
, "ms-body=", NULL
, NULL
);
509 if (!res
) return NULL
;
511 res
= (gchar
*) purple_base64_decode(res
, NULL
);
519 if (strncmp(ms_text_format
, "text/html", 9)) { // NOT html
521 res
= g_markup_escape_text(res
, -1); // as this is not html
525 msgr
= sipmsg_find_part_of_header(ms_text_format
, "msgr=", ";", NULL
);
527 gchar
*x_mms_im_format
= sipmsg_get_x_mms_im_format(msgr
);
530 res
= sipmsg_apply_x_mms_im_format(x_mms_im_format
, res
);
532 g_free(x_mms_im_format
);
542 //------------------------------------------------------------------------------------------
543 //TEMP solution to include it here (copy from purple's msn protocol
544 //How to reuse msn's util methods from sipe?
546 // from internal.h for linux compilation
549 #define BUF_LEN MSG_LEN
552 msn_parse_format(const char *mime
, char **pre_ret
, char **post_ret
)
555 GString
*pre
= g_string_new(NULL
);
556 GString
*post
= g_string_new(NULL
);
557 unsigned int colors
[3];
559 if (pre_ret
!= NULL
) *pre_ret
= NULL
;
560 if (post_ret
!= NULL
) *post_ret
= NULL
;
562 cur
= strstr(mime
, "FN=");
564 if (cur
&& (*(cur
= cur
+ 3) != ';'))
566 pre
= g_string_append(pre
, "<FONT FACE=\"");
568 while (*cur
&& *cur
!= ';')
570 pre
= g_string_append_c(pre
, *cur
);
574 pre
= g_string_append(pre
, "\">");
575 post
= g_string_prepend(post
, "</FONT>");
578 cur
= strstr(mime
, "EF=");
580 if (cur
&& (*(cur
= cur
+ 3) != ';'))
582 while (*cur
&& *cur
!= ';')
584 pre
= g_string_append_c(pre
, '<');
585 pre
= g_string_append_c(pre
, *cur
);
586 pre
= g_string_append_c(pre
, '>');
587 post
= g_string_prepend_c(post
, '>');
588 post
= g_string_prepend_c(post
, *cur
);
589 post
= g_string_prepend_c(post
, '/');
590 post
= g_string_prepend_c(post
, '<');
595 cur
= strstr(mime
, "CO=");
597 if (cur
&& (*(cur
= cur
+ 3) != ';'))
601 i
= sscanf(cur
, "%02x%02x%02x;", &colors
[0], &colors
[1], &colors
[2]);
614 unsigned int temp
= colors
[0];
616 colors
[0] = colors
[1];
622 unsigned int temp
= colors
[2];
624 colors
[2] = colors
[0];
628 g_snprintf(tag
, sizeof(tag
),
629 "<FONT COLOR=\"#%02hhx%02hhx%02hhx\">",
630 colors
[0], colors
[1], colors
[2]);
632 pre
= g_string_append(pre
, tag
);
633 post
= g_string_prepend(post
, "</FONT>");
637 cur
= strstr(mime
, "RL=");
639 if (cur
&& (*(cur
= cur
+ 3) != ';'))
643 /* RTL text was received */
644 pre
= g_string_append(pre
, "<SPAN style=\"direction:rtl;text-align:right;\">");
645 post
= g_string_prepend(post
, "</SPAN>");
649 cur
= g_strdup(purple_url_decode(pre
->str
));
650 g_string_free(pre
, TRUE
);
657 cur
= g_strdup(purple_url_decode(post
->str
));
658 g_string_free(post
, TRUE
);
660 if (post_ret
!= NULL
)
667 encode_spaces(const char *str
)
669 static char buf
[BUF_LEN
];
673 g_return_val_if_fail(str
!= NULL
, NULL
);
675 for (c
= str
, d
= buf
; *c
!= '\0'; c
++)
692 msn_import_html(const char *html
, char **attributes
, char **message
)
694 int len
, retcount
= 0;
697 char *fontface
= NULL
;
700 char direction
= '0';
702 gboolean has_bold
= FALSE
;
703 gboolean has_italic
= FALSE
;
704 gboolean has_underline
= FALSE
;
705 gboolean has_strikethrough
= FALSE
;
707 g_return_if_fail(html
!= NULL
);
708 g_return_if_fail(attributes
!= NULL
);
709 g_return_if_fail(message
!= NULL
);
712 msg
= g_malloc0(len
+ 1);
714 memset(fontcolor
, 0, sizeof(fontcolor
));
715 strcat(fontcolor
, "0");
716 memset(fonteffect
, 0, sizeof(fonteffect
));
718 for (c
= html
; *c
!= '\0';)
722 if (!g_ascii_strncasecmp(c
+ 1, "br>", 3))
724 msg
[retcount
++] = '\r';
725 msg
[retcount
++] = '\n';
728 else if (!g_ascii_strncasecmp(c
+ 1, "i>", 2))
732 strcat(fonteffect
, "I");
737 else if (!g_ascii_strncasecmp(c
+ 1, "b>", 2))
741 strcat(fonteffect
, "B");
746 else if (!g_ascii_strncasecmp(c
+ 1, "u>", 2))
750 strcat(fonteffect
, "U");
751 has_underline
= TRUE
;
755 else if (!g_ascii_strncasecmp(c
+ 1, "s>", 2))
757 if (!has_strikethrough
)
759 strcat(fonteffect
, "S");
760 has_strikethrough
= TRUE
;
764 else if (!g_ascii_strncasecmp(c
+ 1, "a href=\"", 8))
768 if (!g_ascii_strncasecmp(c
, "mailto:", 7))
771 while ((*c
!= '\0') && g_ascii_strncasecmp(c
, "\">", 2))
772 msg
[retcount
++] = *c
++;
777 /* ignore descriptive string */
778 while ((*c
!= '\0') && g_ascii_strncasecmp(c
, "</a>", 4))
784 else if (!g_ascii_strncasecmp(c
+ 1, "span", 4))
786 /* Bi-directional text support using CSS properties in span tags */
789 while (*c
!= '\0' && *c
!= '>')
793 if (!g_ascii_strncasecmp(c
, "dir=\"rtl\"", 9))
798 else if (!g_ascii_strncasecmp(c
, "style=\"", 7))
800 /* Parse inline CSS attributes */
804 while (*(c
+ attr_len
) != '\0' && *(c
+ attr_len
) != '"')
806 if (*(c
+ attr_len
) == '"')
809 attributes
= g_strndup(c
, attr_len
);
810 attr_dir
= purple_markup_get_css_property(attributes
, "direction");
811 if (attr_dir
&& (!g_ascii_strncasecmp(attr_dir
, "RTL", 3)))
826 else if (!g_ascii_strncasecmp(c
+ 1, "font", 4))
830 while ((*c
!= '\0') && !g_ascii_strncasecmp(c
, " ", 1))
833 if (!g_ascii_strncasecmp(c
, "color=\"#", 7))
837 fontcolor
[0] = *(c
+ 4);
838 fontcolor
[1] = *(c
+ 5);
839 fontcolor
[2] = *(c
+ 2);
840 fontcolor
[3] = *(c
+ 3);
842 fontcolor
[5] = *(c
+ 1);
846 else if (!g_ascii_strncasecmp(c
, "face=\"", 6))
848 const char *end
= NULL
;
849 const char *comma
= NULL
;
850 unsigned int namelen
= 0;
853 end
= strchr(c
, '\"');
854 comma
= strchr(c
, ',');
856 if (comma
== NULL
|| comma
> end
)
857 namelen
= (unsigned int)(end
- c
);
859 namelen
= (unsigned int)(comma
- c
);
861 fontface
= g_strndup(c
, namelen
);
866 /* Drop all unrecognized/misparsed font tags */
867 while ((*c
!= '\0') && g_ascii_strncasecmp(c
, "\">", 2))
876 while ((*c
!= '\0') && (*c
!= '>'))
884 if (!g_ascii_strncasecmp(c
, "<", 4))
886 msg
[retcount
++] = '<';
889 else if (!g_ascii_strncasecmp(c
, ">", 4))
891 msg
[retcount
++] = '>';
894 else if (!g_ascii_strncasecmp(c
, " ", 6))
896 msg
[retcount
++] = ' ';
899 else if (!g_ascii_strncasecmp(c
, """, 6))
901 msg
[retcount
++] = '"';
904 else if (!g_ascii_strncasecmp(c
, "&", 5))
906 msg
[retcount
++] = '&';
909 else if (!g_ascii_strncasecmp(c
, "'", 6))
911 msg
[retcount
++] = '\'';
915 msg
[retcount
++] = *c
++;
918 msg
[retcount
++] = *c
++;
921 if (fontface
== NULL
)
922 fontface
= g_strdup("MS Sans Serif");
924 *attributes
= g_strdup_printf("FN=%s; EF=%s; CO=%s; PF=0; RL=%c",
925 encode_spaces(fontface
),
926 fonteffect
, fontcolor
, direction
);