utils: factor out device UUID generation
[siplcs.git] / src / core / sipe-utils.c
blob0aac97c21c4d4f9bc0e9fba4695473902111047c
1 /**
2 * @file sipe-utils.c
4 * pidgin-sipe
6 * Copyright (C) 2009-2011 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
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <ctype.h>
28 #include <glib.h>
30 #include "sipe-backend.h"
31 #include "sipe-core.h" /* to ensure same API for backends */
32 #include "sipe-core-private.h"
33 #include "sipe-utils.h"
34 #include "uuid.h"
35 #include "sipe.h"
37 /* Generate 32 random bits */
38 #define RANDOM32BITS (rand() & 0xFFFF)
40 gchar *gencallid(void)
42 return g_strdup_printf("%04Xg%04Xa%04Xi%04Xm%04Xt%04Xb%04Xx%04Xx",
43 RANDOM32BITS, RANDOM32BITS, RANDOM32BITS,
44 RANDOM32BITS, RANDOM32BITS, RANDOM32BITS,
45 RANDOM32BITS, RANDOM32BITS);
48 gchar *gentag(void)
50 return g_strdup_printf("%04d%04d", RANDOM32BITS, RANDOM32BITS);
53 gchar *genconfid(void)
55 return g_strdup_printf("%04X%04X%04X%04X%04X%04X%04X%04X",
56 RANDOM32BITS, RANDOM32BITS, RANDOM32BITS,
57 RANDOM32BITS, RANDOM32BITS, RANDOM32BITS,
58 RANDOM32BITS, RANDOM32BITS);
61 gchar *get_contact(const struct sipe_core_private *sipe_private)
63 return g_strdup(sipe_private->contact);
66 gchar *parse_from(const gchar *hdr)
68 gchar *from;
69 const gchar *tmp, *tmp2 = hdr;
71 if (!hdr) return NULL;
72 SIPE_DEBUG_INFO("parsing address out of %s", hdr);
73 tmp = strchr(hdr, '<');
75 /* i hate the different SIP UA behaviours... */
76 if (tmp) { /* sip address in <...> */
77 tmp2 = tmp + 1;
78 tmp = strchr(tmp2, '>');
79 if (tmp) {
80 from = g_strndup(tmp2, tmp - tmp2);
81 } else {
82 SIPE_DEBUG_INFO_NOFORMAT("found < without > in From");
83 return NULL;
85 } else {
86 tmp = strchr(tmp2, ';');
87 if (tmp) {
88 from = g_strndup(tmp2, tmp - tmp2);
89 } else {
90 from = g_strdup(tmp2);
93 SIPE_DEBUG_INFO("got %s", from);
94 return from;
97 gchar *sip_uri_from_name(const gchar *name)
99 return(g_strdup_printf("sip:%s", name));
102 gchar *sip_uri(const gchar *string)
104 return(strstr(string, "sip:") ? g_strdup(string) : sip_uri_from_name(string));
107 gchar *
108 get_epid(struct sipe_core_private *sipe_private)
110 if (!sipe_private->epid) {
111 gchar *self_sip_uri = sip_uri_self(sipe_private);
112 sipe_private->epid = sipe_get_epid(self_sip_uri,
113 g_get_host_name(),
114 sipe_backend_network_ip_address());
115 g_free(self_sip_uri);
117 return g_strdup(sipe_private->epid);
120 gchar *get_uuid(struct sipe_core_private *sipe_private)
122 gchar *epid = get_epid(sipe_private);
123 gchar *uuid = generateUUIDfromEPID(epid);
124 g_free(epid);
125 return(uuid);
129 guint
130 sipe_get_pub_instance(struct sipe_core_private *sipe_private,
131 int publication_key)
133 unsigned res = 0;
134 gchar *epid = get_epid(sipe_private);
136 sscanf(epid, "%08x", &res);
137 g_free(epid);
139 if (publication_key == SIPE_PUB_DEVICE) {
140 /* as is */
141 } else if (publication_key == SIPE_PUB_STATE_MACHINE) { /* First hexadecimal digit is 0x3 */
142 res = (res >> 4) | 0x30000000;
143 } else if (publication_key == SIPE_PUB_STATE_USER) {
144 res = 0x20000000; /* fixed */
145 } else if (publication_key == SIPE_PUB_STATE_CALENDAR) { /* First hexadecimal digit is 0x4 */
146 res = (res >> 4) | 0x40000000;
147 } else if (publication_key == SIPE_PUB_STATE_CALENDAR_OOF) { /* First hexadecimal digit is 0x5 */
148 res = (res >> 4) | 0x50000000;
149 } else if (publication_key == SIPE_PUB_CALENDAR_DATA ||
150 publication_key == SIPE_PUB_NOTE_OOF)
151 { /* First hexadecimal digit is 0x4 */
152 unsigned calendar_id = 0;
153 char *mail_hash = sipe_get_epid(SIPE_ACCOUNT_DATA_PRIVATE->email, "", "");
155 sscanf(mail_hash, "%08x", &calendar_id);
156 g_free(mail_hash);
157 res = (calendar_id >> 4) | 0x40000000;
160 return res;
163 gboolean
164 sipe_is_bad_alias(const char *uri,
165 const char *alias)
167 char *uri_alias;
168 gboolean result = FALSE;
170 if (!uri) return FALSE;
171 if (!alias) return TRUE;
173 if (g_str_has_prefix(alias, "sip:") || g_str_has_prefix(alias, "sips:")) return TRUE;
175 /* check if alias is just SIP URI but without 'sip:' prefix */
176 uri_alias = sip_uri_from_name(alias);
177 if (sipe_strcase_equal(uri, uri_alias)) {
178 result = TRUE;
180 g_free(uri_alias);
182 return result;
185 gboolean
186 is_empty(const char *st)
188 if (!st || strlen(st) == 0)
190 return TRUE;
192 /* suspecious leading or trailing staces */
193 else if (isspace((unsigned char) *st) ||
194 isspace((unsigned char) *(st + strlen(st) - 1)))
196 /* to not modify original string */
197 char *dup = g_strdup(st);
198 if (strlen(g_strstrip(dup)) == 0) {
199 g_free(dup);
200 return TRUE;
202 g_free(dup);
204 return FALSE;
207 /** Returns newly allocated string. Must be g_free()'d */
208 char *
209 replace(const char *st,
210 const char *search,
211 const char *replace)
213 char **tmp;
214 char *res;
216 if (!st) return NULL;
218 res = g_strjoinv(replace, tmp = g_strsplit(st, search, -1));
219 g_strfreev(tmp);
220 return res;
223 void sipe_utils_message_debug(const gchar *type,
224 const gchar *header,
225 const gchar *body,
226 gboolean sending)
228 if (sipe_backend_debug_enabled()) {
229 GString *str = g_string_new("");
230 GTimeVal currtime;
231 gchar *time_str;
232 const char *marker = sending ?
233 ">>>>>>>>>>" :
234 "<<<<<<<<<<";
235 gchar *tmp;
237 g_get_current_time(&currtime);
238 time_str = g_time_val_to_iso8601(&currtime);
239 g_string_append_printf(str, "\nMESSAGE START %s %s - %s\n", marker, type, time_str);
240 g_string_append(str, tmp = replace(header, "\r\n", "\n"));
241 g_free(tmp);
242 g_string_append(str, "\n");
243 if (body) {
244 g_string_append(str, tmp = replace(body, "\r\n", "\n"));
245 g_free(tmp);
246 g_string_append(str, "\n");
248 g_string_append_printf(str, "MESSAGE END %s %s - %s", marker, type, time_str);
249 g_free(time_str);
250 SIPE_DEBUG_INFO_NOFORMAT(str->str);
251 g_string_free(str, TRUE);
255 gboolean
256 sipe_strequal(const gchar *left, const gchar *right)
258 #if GLIB_CHECK_VERSION(2,16,0)
259 return (g_strcmp0(left, right) == 0);
260 #else
261 return ((left == NULL && right == NULL) ||
262 (left != NULL && right != NULL && strcmp(left, right) == 0));
263 #endif
266 gboolean
267 sipe_strcase_equal(const gchar *left, const gchar *right)
269 return ((left == NULL && right == NULL) ||
270 (left != NULL && right != NULL && g_ascii_strcasecmp(left, right) == 0));
273 gint sipe_strcompare(gconstpointer a, gconstpointer b)
275 #if GLIB_CHECK_VERSION(2,16,0)
276 return (g_strcmp0(a, b));
277 #else
278 if (!a)
279 return -(a != b);
280 if (!b)
281 return a != b;
282 return strcmp(a, b);
283 #endif
286 time_t
287 sipe_utils_str_to_time(const gchar *timestamp)
289 GTimeVal time;
290 guint len;
292 /* We have to make sure that the ISO8601 contains a time zone offset,
293 otherwise the time is interpreted as local time, not UTC!
294 @TODO: is there a better way to check this? */
295 if (timestamp && (len = strlen(timestamp) > 0) &&
296 isdigit(timestamp[len - 1])) {
297 gchar *tmp = g_strdup_printf("%sZ", timestamp);
298 g_time_val_from_iso8601(tmp, &time);
299 g_free(tmp);
300 } else {
301 g_time_val_from_iso8601(timestamp, &time);
303 return time.tv_sec;
306 gchar *
307 sipe_utils_time_to_str(time_t timestamp)
309 GTimeVal time = { timestamp, 0 };
310 return g_time_val_to_iso8601(&time);
313 size_t
314 hex_str_to_buff(const char *hex_str, guint8 **buff)
316 char two_digits[3];
317 size_t length;
318 size_t i;
320 if (!buff) return 0;
321 if (!hex_str) return 0;
323 length = strlen(hex_str)/2;
324 *buff = (unsigned char *)g_malloc(length);
325 for (i = 0; i < length; i++) {
326 two_digits[0] = hex_str[i * 2];
327 two_digits[1] = hex_str[i * 2 + 1];
328 two_digits[2] = '\0';
329 (*buff)[i] = (unsigned char)strtoul(two_digits, NULL, 16);
332 return length;
335 char *
336 buff_to_hex_str(const guint8 *buff, const size_t buff_len)
338 char *res;
339 size_t i, j;
341 if (!buff) return NULL;
343 res = g_malloc(buff_len * 2 + 1);
344 for (i = 0, j = 0; i < buff_len; i++, j+=2) {
345 sprintf(&res[j], "%02X", buff[i]);
347 res[j] = '\0';
348 return res;
351 gboolean
352 sipe_utils_parse_lines(GSList **list, gchar **lines, gchar *delimiter)
354 int i;
355 gchar **parts;
356 gchar *dummy;
357 gchar *dummy2;
358 gchar *tmp;
360 for(i = 0; lines[i] && strlen(lines[i]) > 2; i++) {
361 parts = g_strsplit(lines[i], delimiter, 2);
362 if(!parts[0] || !parts[1]) {
363 g_strfreev(parts);
364 return FALSE;
366 dummy = parts[1];
367 dummy2 = 0;
368 while(*dummy==' ' || *dummy=='\t') dummy++;
369 dummy2 = g_strdup(dummy);
370 while(lines[i+1] && (lines[i+1][0]==' ' || lines[i+1][0]=='\t')) {
371 i++;
372 dummy = lines[i];
373 while(*dummy==' ' || *dummy=='\t') dummy++;
374 tmp = g_strdup_printf("%s %s",dummy2, dummy);
375 g_free(dummy2);
376 dummy2 = tmp;
378 *list = sipe_utils_nameval_add(*list, parts[0], dummy2);
379 g_free(dummy2);
380 g_strfreev(parts);
383 return TRUE;
386 GSList*
387 sipe_utils_nameval_add(GSList* list, const gchar *name, const gchar *value)
389 struct sipnameval *element = g_new0(struct sipnameval,1);
391 /* SANITY CHECK: the calling code must be fixed if this happens! */
392 if (!value) {
393 SIPE_DEBUG_ERROR("sipe_utils_nameval_add: NULL value for %s",
394 name);
395 value = "";
398 element->name = g_strdup(name);
399 element->value = g_strdup(value);
400 return g_slist_append(list, element);
403 void
404 sipe_utils_nameval_free(GSList *list) {
405 struct sipnameval *elem;
406 while(list) {
407 elem = list->data;
408 list = g_slist_remove(list,elem);
409 g_free(elem->name);
410 g_free(elem->value);
411 g_free(elem);
415 const gchar *
416 sipe_utils_nameval_find(const GSList *list, const gchar *name)
418 return sipe_utils_nameval_find_instance (list, name, 0);
421 const gchar *
422 sipe_utils_nameval_find_instance(const GSList *list, const gchar *name, int which)
424 const GSList *tmp;
425 struct sipnameval *elem;
426 int i = 0;
427 tmp = list;
428 while(tmp) {
429 elem = tmp->data;
430 // OCS2005 can send the same header in either all caps or mixed case
431 if (sipe_strcase_equal(elem->name, name)) {
432 if (i == which) {
433 return elem->value;
435 i++;
437 tmp = g_slist_next(tmp);
439 return NULL;
442 gchar *sipe_utils_str_replace(const gchar *string,
443 const gchar *delimiter,
444 const gchar *replacement)
446 gchar **split;
447 gchar *result;
449 if (!string || !delimiter || !replacement) return NULL;
451 split = g_strsplit(string, delimiter, 0);
452 result = g_strjoinv(replacement, split);
453 g_strfreev(split);
455 return result;
458 void sipe_utils_shrink_buffer(struct sipe_transport_connection *conn,
459 const gchar *unread)
461 conn->buffer_used -= unread - conn->buffer;
462 /* string terminator is not included in buffer_used */
463 memmove(conn->buffer, unread, conn->buffer_used + 1);
466 gboolean sipe_utils_ip_is_private(const char *ip)
468 return g_str_has_prefix(ip, "10.") ||
469 g_str_has_prefix(ip, "172.16.") ||
470 g_str_has_prefix(ip, "192.168.");
473 gchar *sipe_utils_presence_key(const gchar *uri)
475 return g_strdup_printf("<presence><%s>", uri);
478 gchar *sipe_utils_subscription_key(const gchar *event,
479 const gchar *uri)
481 gchar *key = NULL;
483 if (!is_empty(event)) {
484 if (!g_ascii_strcasecmp(event, "presence")) {
485 /* Subscription is identified by <presence><uri> key */
486 key = sipe_utils_presence_key(uri);
487 } else {
488 /* Subscription is identified by <event> key */
489 key = g_strdup_printf("<%s>", event);
493 return key;
496 gchar *
497 sipe_utils_uri_unescape(const gchar *string)
499 gchar *unescaped;
500 gchar *tmp;
502 if (!string)
503 return NULL;
505 #if GLIB_CHECK_VERSION(2,16,0)
506 unescaped = g_uri_unescape_string(string, NULL);
507 #else
508 // based on libpurple/util.c:purple_url_decode()
510 GString *buf = g_string_new(NULL);
511 size_t len = strlen(string);
512 char hex[3];
514 hex[2] = '\0';
516 while (len--) {
517 gchar c = *string++;
519 if ((len >= 2) && (c == '%')) {
520 strncpy(hex, string, 2);
521 c = strtol(hex, NULL, 16);
523 string += 2;
524 len -= 2;
527 g_string_append_c(buf, c);
530 unescaped = g_string_free(buf, FALSE);
532 #endif
533 if (unescaped && !g_utf8_validate(unescaped, -1, (const gchar **)&tmp))
534 *tmp = '\0';
536 return unescaped;
539 gboolean
540 sipe_utils_is_avconf_uri(const gchar *uri)
542 return g_strstr_len(uri, -1, "app:conf:audio-video:") != NULL;
546 * Only appends if no such value already stored.
547 * Like Set in Java.
549 GSList *
550 slist_insert_unique_sorted(GSList *list, gpointer data, GCompareFunc func) {
551 GSList * res = list;
552 if (!g_slist_find_custom(list, data, func)) {
553 res = g_slist_insert_sorted(list, data, func);
555 return res;
559 Local Variables:
560 mode: c
561 c-file-style: "bsd"
562 indent-tabs-mode: t
563 tab-width: 8
564 End: