6 * Copyright (C) 2009-2010 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #include "sipe-backend.h"
35 #include "sipe-utils.h"
39 /* Generate 32 random bits */
40 #define RANDOM32BITS (rand() & 0xFFFF)
42 gchar
*gencallid(void)
44 return g_strdup_printf("%04Xg%04Xa%04Xi%04Xm%04Xt%04Xb%04Xx%04Xx",
45 RANDOM32BITS
, RANDOM32BITS
, RANDOM32BITS
,
46 RANDOM32BITS
, RANDOM32BITS
, RANDOM32BITS
,
47 RANDOM32BITS
, RANDOM32BITS
);
52 return g_strdup_printf("%04d%04d", RANDOM32BITS
, RANDOM32BITS
);
55 gchar
*genconfid(void)
57 return g_strdup_printf("%04X%04X%04X%04X%04X%04X%04X%04X",
58 RANDOM32BITS
, RANDOM32BITS
, RANDOM32BITS
,
59 RANDOM32BITS
, RANDOM32BITS
, RANDOM32BITS
,
60 RANDOM32BITS
, RANDOM32BITS
);
63 gchar
*get_contact(const struct sipe_account_data
*sip
)
65 return g_strdup(sip
->contact
);
68 gchar
*parse_from(const gchar
*hdr
)
71 const gchar
*tmp
, *tmp2
= hdr
;
73 if (!hdr
) return NULL
;
74 SIPE_DEBUG_INFO("parsing address out of %s", hdr
);
75 tmp
= strchr(hdr
, '<');
77 /* i hate the different SIP UA behaviours... */
78 if (tmp
) { /* sip address in <...> */
80 tmp
= strchr(tmp2
, '>');
82 from
= g_strndup(tmp2
, tmp
- tmp2
);
84 SIPE_DEBUG_INFO_NOFORMAT("found < without > in From");
88 tmp
= strchr(tmp2
, ';');
90 from
= g_strndup(tmp2
, tmp
- tmp2
);
92 from
= g_strdup(tmp2
);
95 SIPE_DEBUG_INFO("got %s", from
);
99 int parse_cseq(const gchar
*hdr
)
103 items
= g_strsplit(hdr
, " ", 1);
105 res
= atoi(items
[0]);
111 gchar
*sip_uri_from_name(const gchar
*name
)
113 return(g_strdup_printf("sip:%s", name
));
116 gchar
*sip_uri(const gchar
*string
)
118 return(strstr(string
, "sip:") ? g_strdup(string
) : sip_uri_from_name(string
));
122 get_epid(struct sipe_account_data
*sip
)
125 gchar
*self_sip_uri
= sip_uri_self(sip
);
126 sip
->epid
= sipe_get_epid(self_sip_uri
,
128 purple_network_get_my_ip(-1));
129 g_free(self_sip_uri
);
131 return g_strdup(sip
->epid
);
135 sipe_get_pub_instance(struct sipe_account_data
*sip
,
139 gchar
*epid
= get_epid(sip
);
141 sscanf(epid
, "%08x", &res
);
144 if (publication_key
== SIPE_PUB_DEVICE
) {
146 } else if (publication_key
== SIPE_PUB_STATE_MACHINE
) { /* First hexadecimal digit is 0x3 */
147 res
= (res
>> 4) | 0x30000000;
148 } else if (publication_key
== SIPE_PUB_STATE_USER
) {
149 res
= 0x20000000; /* fixed */
150 } else if (publication_key
== SIPE_PUB_STATE_CALENDAR
) { /* First hexadecimal digit is 0x4 */
151 res
= (res
>> 4) | 0x40000000;
152 } else if (publication_key
== SIPE_PUB_STATE_CALENDAR_OOF
) { /* First hexadecimal digit is 0x5 */
153 res
= (res
>> 4) | 0x50000000;
154 } else if (publication_key
== SIPE_PUB_CALENDAR_DATA
||
155 publication_key
== SIPE_PUB_NOTE_OOF
)
156 { /* First hexadecimal digit is 0x4 */
157 unsigned calendar_id
= 0;
158 char *mail_hash
= sipe_get_epid(sip
->email
, "", "");
160 sscanf(mail_hash
, "%08x", &calendar_id
);
162 res
= (calendar_id
>> 4) | 0x40000000;
169 sipe_get_pub_instance_(struct sipe_account_data *sip,
170 const char *publication_key)
174 gchar *epid = get_epid(sip);
175 sscanf(epid, "%08x", &part_1);
177 sscanf(publication_key, "%uh", &part_2);
178 return part_1 + part_2;
182 sipe_is_bad_alias(const char *uri
,
186 gboolean result
= FALSE
;
188 if (!uri
) return FALSE
;
189 if (!alias
) return TRUE
;
191 if (g_str_has_prefix(alias
, "sip:") || g_str_has_prefix(alias
, "sips:")) return TRUE
;
193 /* check if alias is just SIP URI but without 'sip:' prefix */
194 uri_alias
= sip_uri_from_name(alias
);
195 if (sipe_strcase_equal(uri
, uri_alias
)) {
204 is_empty(const char *st
)
206 if (!st
|| strlen(st
) == 0)
210 /* suspecious leading or trailing staces */
211 else if (isspace((unsigned char) *st
) ||
212 isspace((unsigned char) *(st
+ strlen(st
) - 1)))
214 /* to not modify original string */
215 char *dup
= g_strdup(st
);
216 if (strlen(g_strstrip(dup
)) == 0) {
225 /** Returns newly allocated string. Must be g_free()'d */
227 replace(const char *st
,
234 if (!st
) return NULL
;
236 res
= g_strjoinv(replace
, tmp
= g_strsplit(st
, search
, -1));
242 fix_newlines(const char *st
)
244 return replace(st
, "\r\n", "\n");
248 sipe_strequal(const gchar
*left
, const gchar
*right
)
250 #if GLIB_CHECK_VERSION(2,16,0)
251 return (g_strcmp0(left
, right
) == 0);
253 return ((left
== NULL
&& right
== NULL
) ||
254 (left
!= NULL
&& right
!= NULL
&& strcmp(left
, right
) == 0));
259 sipe_strcase_equal(const gchar
*left
, const gchar
*right
)
261 return ((left
== NULL
&& right
== NULL
) ||
262 (left
!= NULL
&& right
!= NULL
&& g_ascii_strcasecmp(left
, right
) == 0));
266 sipe_utils_str_to_time(const gchar
*timestamp
)
269 g_time_val_from_iso8601(timestamp
, &time
);
274 sipe_utils_time_to_str(time_t timestamp
)
276 GTimeVal time
= { timestamp
, 0 };
277 return g_time_val_to_iso8601(&time
);
281 hex_str_to_buff(const char *hex_str
, guint8
**buff
)
288 if (!hex_str
) return 0;
290 length
= strlen(hex_str
)/2;
291 *buff
= (unsigned char *)g_malloc(length
);
292 for (i
= 0; i
< length
; i
++) {
293 two_digits
[0] = hex_str
[i
* 2];
294 two_digits
[1] = hex_str
[i
* 2 + 1];
295 two_digits
[2] = '\0';
296 (*buff
)[i
] = (unsigned char)strtoul(two_digits
, NULL
, 16);
303 buff_to_hex_str(const guint8
*buff
, const size_t buff_len
)
308 if (!buff
) return NULL
;
310 res
= g_malloc(buff_len
* 2 + 1);
311 for (i
= 0, j
= 0; i
< buff_len
; i
++, j
+=2) {
312 sprintf(&res
[j
], "%02X", buff
[i
]);
319 sipe_utils_parse_lines(GSList
**list
, gchar
**lines
)
327 for(i
= 0; lines
[i
] && strlen(lines
[i
]) > 2; i
++) {
328 parts
= g_strsplit(lines
[i
], ":", 2);
329 if(!parts
[0] || !parts
[1]) {
335 while(*dummy
==' ' || *dummy
=='\t') dummy
++;
336 dummy2
= g_strdup(dummy
);
337 while(lines
[i
+1] && (lines
[i
+1][0]==' ' || lines
[i
+1][0]=='\t')) {
340 while(*dummy
==' ' || *dummy
=='\t') dummy
++;
341 tmp
= g_strdup_printf("%s %s",dummy2
, dummy
);
345 *list
= sipe_utils_nameval_add(*list
, parts
[0], dummy2
);
354 sipe_utils_nameval_add(GSList
* list
, const gchar
*name
, const gchar
*value
)
356 struct sipnameval
*element
= g_new0(struct sipnameval
,1);
358 /* SANITY CHECK: the calling code must be fixed if this happens! */
360 SIPE_DEBUG_ERROR("sipe_utils_nameval_add: NULL value for %s",
365 element
->name
= g_strdup(name
);
366 element
->value
= g_strdup(value
);
367 return g_slist_append(list
, element
);
371 sipe_utils_nameval_free(GSList
*list
) {
372 struct sipnameval
*elem
;
375 list
= g_slist_remove(list
,elem
);
383 sipe_utils_nameval_find(const GSList
*list
, const gchar
*name
)
385 return sipe_utils_nameval_find_instance (list
, name
, 0);
389 sipe_utils_nameval_find_instance(const GSList
*list
, const gchar
*name
, int which
)
392 struct sipnameval
*elem
;
397 // OCS2005 can send the same header in either all caps or mixed case
398 if (sipe_strcase_equal(elem
->name
, name
)) {
404 tmp
= g_slist_next(tmp
);
409 gchar
*sipe_utils_str_replace(const gchar
*string
,
410 const gchar
*delimiter
,
411 const gchar
*replacement
)
416 if (!string
|| !delimiter
|| !replacement
) return NULL
;
418 split
= g_strsplit(string
, delimiter
, 0);
419 result
= g_strjoinv(replacement
, split
);