core cleanup: move out email & user_states field
[siplcs.git] / src / core / sipe-utils.c
blobc976660f5720c9d364ffd11d2933c917daf9185e
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"
36 /* Generate 16 random bits */
37 #define RANDOM16BITS (rand() & 0xFFFF)
39 gchar *gencallid(void)
41 return g_strdup_printf("%04Xg%04Xa%04Xi%04Xm%04Xt%04Xb%04Xx%04Xx",
42 RANDOM16BITS, RANDOM16BITS, RANDOM16BITS,
43 RANDOM16BITS, RANDOM16BITS, RANDOM16BITS,
44 RANDOM16BITS, RANDOM16BITS);
47 gchar *gentag(void)
49 return g_strdup_printf("%04d%04d", RANDOM16BITS, RANDOM16BITS);
52 gchar *genconfid(void)
54 return g_strdup_printf("%04X%04X%04X%04X%04X%04X%04X%04X",
55 RANDOM16BITS, RANDOM16BITS, RANDOM16BITS,
56 RANDOM16BITS, RANDOM16BITS, RANDOM16BITS,
57 RANDOM16BITS, RANDOM16BITS);
60 gchar *get_contact(const struct sipe_core_private *sipe_private)
62 return g_strdup(sipe_private->contact);
65 gchar *parse_from(const gchar *hdr)
67 gchar *from;
68 const gchar *tmp, *tmp2 = hdr;
70 if (!hdr) return NULL;
71 SIPE_DEBUG_INFO("parsing address out of %s", hdr);
72 tmp = strchr(hdr, '<');
74 /* i hate the different SIP UA behaviours... */
75 if (tmp) { /* sip address in <...> */
76 tmp2 = tmp + 1;
77 tmp = strchr(tmp2, '>');
78 if (tmp) {
79 from = g_strndup(tmp2, tmp - tmp2);
80 } else {
81 SIPE_DEBUG_INFO_NOFORMAT("found < without > in From");
82 return NULL;
84 } else {
85 tmp = strchr(tmp2, ';');
86 if (tmp) {
87 from = g_strndup(tmp2, tmp - tmp2);
88 } else {
89 from = g_strdup(tmp2);
92 SIPE_DEBUG_INFO("got %s", from);
93 return from;
96 gchar *sip_uri_from_name(const gchar *name)
98 return(g_strdup_printf("sip:%s", name));
101 gchar *sip_uri(const gchar *string)
103 return(strstr(string, "sip:") ? g_strdup(string) : sip_uri_from_name(string));
106 const gchar *sipe_get_no_sip_uri(const gchar *sip_uri)
108 #define SIP_PREFIX "sip:"
110 if (!sip_uri) return NULL;
112 if (g_str_has_prefix(sip_uri, SIP_PREFIX)) {
113 return(sip_uri + strlen(SIP_PREFIX));
114 } else {
115 return sip_uri;
119 gchar *
120 get_epid(struct sipe_core_private *sipe_private)
122 if (!sipe_private->epid) {
123 gchar *self_sip_uri = sip_uri_self(sipe_private);
124 sipe_private->epid = sipe_get_epid(self_sip_uri,
125 g_get_host_name(),
126 sipe_backend_network_ip_address());
127 g_free(self_sip_uri);
129 return g_strdup(sipe_private->epid);
132 gchar *get_uuid(struct sipe_core_private *sipe_private)
134 gchar *epid = get_epid(sipe_private);
135 gchar *uuid = generateUUIDfromEPID(epid);
136 g_free(epid);
137 return(uuid);
141 guint
142 sipe_get_pub_instance(struct sipe_core_private *sipe_private,
143 int publication_key)
145 unsigned res = 0;
146 gchar *epid = get_epid(sipe_private);
148 sscanf(epid, "%08x", &res);
149 g_free(epid);
151 if (publication_key == SIPE_PUB_DEVICE) {
152 /* as is */
153 } else if (publication_key == SIPE_PUB_STATE_MACHINE) { /* First hexadecimal digit is 0x3 */
154 res = (res >> 4) | 0x30000000;
155 } else if (publication_key == SIPE_PUB_STATE_USER) {
156 res = 0x20000000; /* fixed */
157 } else if (publication_key == SIPE_PUB_STATE_CALENDAR) { /* First hexadecimal digit is 0x4 */
158 res = (res >> 4) | 0x40000000;
159 } else if (publication_key == SIPE_PUB_STATE_CALENDAR_OOF) { /* First hexadecimal digit is 0x5 */
160 res = (res >> 4) | 0x50000000;
161 } else if (publication_key == SIPE_PUB_CALENDAR_DATA ||
162 publication_key == SIPE_PUB_NOTE_OOF)
163 { /* First hexadecimal digit is 0x4 */
164 unsigned calendar_id = 0;
165 char *mail_hash = sipe_get_epid(sipe_private->email, "", "");
167 sscanf(mail_hash, "%08x", &calendar_id);
168 g_free(mail_hash);
169 res = (calendar_id >> 4) | 0x40000000;
172 return res;
175 gboolean
176 sipe_is_bad_alias(const char *uri,
177 const char *alias)
179 char *uri_alias;
180 gboolean result = FALSE;
182 if (!uri) return FALSE;
183 if (!alias) return TRUE;
185 if (g_str_has_prefix(alias, "sip:") || g_str_has_prefix(alias, "sips:")) return TRUE;
187 /* check if alias is just SIP URI but without 'sip:' prefix */
188 uri_alias = sip_uri_from_name(alias);
189 if (sipe_strcase_equal(uri, uri_alias)) {
190 result = TRUE;
192 g_free(uri_alias);
194 return result;
197 gboolean
198 is_empty(const char *st)
200 if (!st || strlen(st) == 0)
202 return TRUE;
204 /* suspecious leading or trailing staces */
205 else if (isspace((unsigned char) *st) ||
206 isspace((unsigned char) *(st + strlen(st) - 1)))
208 /* to not modify original string */
209 char *dup = g_strdup(st);
210 if (strlen(g_strstrip(dup)) == 0) {
211 g_free(dup);
212 return TRUE;
214 g_free(dup);
216 return FALSE;
219 /** Returns newly allocated string. Must be g_free()'d */
220 char *
221 replace(const char *st,
222 const char *search,
223 const char *replace)
225 char **tmp;
226 char *res;
228 if (!st) return NULL;
230 res = g_strjoinv(replace, tmp = g_strsplit(st, search, -1));
231 g_strfreev(tmp);
232 return res;
235 void sipe_utils_message_debug(const gchar *type,
236 const gchar *header,
237 const gchar *body,
238 gboolean sending)
240 if (sipe_backend_debug_enabled()) {
241 GString *str = g_string_new("");
242 GTimeVal currtime;
243 gchar *time_str;
244 const char *marker = sending ?
245 ">>>>>>>>>>" :
246 "<<<<<<<<<<";
247 gchar *tmp;
249 g_get_current_time(&currtime);
250 time_str = g_time_val_to_iso8601(&currtime);
251 g_string_append_printf(str, "\nMESSAGE START %s %s - %s\n", marker, type, time_str);
252 g_string_append(str, tmp = replace(header, "\r\n", "\n"));
253 g_free(tmp);
254 g_string_append(str, "\n");
255 if (body) {
256 g_string_append(str, tmp = replace(body, "\r\n", "\n"));
257 g_free(tmp);
258 g_string_append(str, "\n");
260 g_string_append_printf(str, "MESSAGE END %s %s - %s", marker, type, time_str);
261 g_free(time_str);
262 SIPE_DEBUG_INFO_NOFORMAT(str->str);
263 g_string_free(str, TRUE);
267 gboolean
268 sipe_strequal(const gchar *left, const gchar *right)
270 #if GLIB_CHECK_VERSION(2,16,0)
271 return (g_strcmp0(left, right) == 0);
272 #else
273 return ((left == NULL && right == NULL) ||
274 (left != NULL && right != NULL && strcmp(left, right) == 0));
275 #endif
278 gboolean
279 sipe_strcase_equal(const gchar *left, const gchar *right)
281 return ((left == NULL && right == NULL) ||
282 (left != NULL && right != NULL && g_ascii_strcasecmp(left, right) == 0));
285 gint sipe_strcompare(gconstpointer a, gconstpointer b)
287 #if GLIB_CHECK_VERSION(2,16,0)
288 return (g_strcmp0(a, b));
289 #else
290 if (!a)
291 return -(a != b);
292 if (!b)
293 return a != b;
294 return strcmp(a, b);
295 #endif
298 time_t
299 sipe_utils_str_to_time(const gchar *timestamp)
301 GTimeVal time;
302 guint len;
304 /* We have to make sure that the ISO8601 contains a time zone offset,
305 otherwise the time is interpreted as local time, not UTC!
306 @TODO: is there a better way to check this? */
307 if (timestamp && (len = strlen(timestamp) > 0) &&
308 isdigit(timestamp[len - 1])) {
309 gchar *tmp = g_strdup_printf("%sZ", timestamp);
310 g_time_val_from_iso8601(tmp, &time);
311 g_free(tmp);
312 } else {
313 g_time_val_from_iso8601(timestamp, &time);
315 return time.tv_sec;
318 gchar *
319 sipe_utils_time_to_str(time_t timestamp)
321 GTimeVal time = { timestamp, 0 };
322 return g_time_val_to_iso8601(&time);
325 size_t
326 hex_str_to_buff(const char *hex_str, guint8 **buff)
328 char two_digits[3];
329 size_t length;
330 size_t i;
332 if (!buff) return 0;
333 if (!hex_str) return 0;
335 length = strlen(hex_str)/2;
336 *buff = (unsigned char *)g_malloc(length);
337 for (i = 0; i < length; i++) {
338 two_digits[0] = hex_str[i * 2];
339 two_digits[1] = hex_str[i * 2 + 1];
340 two_digits[2] = '\0';
341 (*buff)[i] = (unsigned char)strtoul(two_digits, NULL, 16);
344 return length;
347 char *
348 buff_to_hex_str(const guint8 *buff, const size_t buff_len)
350 char *res;
351 size_t i, j;
353 if (!buff) return NULL;
355 res = g_malloc(buff_len * 2 + 1);
356 for (i = 0, j = 0; i < buff_len; i++, j+=2) {
357 sprintf(&res[j], "%02X", buff[i]);
359 res[j] = '\0';
360 return res;
363 gboolean
364 sipe_utils_parse_lines(GSList **list, gchar **lines, gchar *delimiter)
366 int i;
367 gchar **parts;
368 gchar *dummy;
369 gchar *dummy2;
370 gchar *tmp;
372 for(i = 0; lines[i] && strlen(lines[i]) > 2; i++) {
373 parts = g_strsplit(lines[i], delimiter, 2);
374 if(!parts[0] || !parts[1]) {
375 g_strfreev(parts);
376 return FALSE;
378 dummy = parts[1];
379 dummy2 = 0;
380 while(*dummy==' ' || *dummy=='\t') dummy++;
381 dummy2 = g_strdup(dummy);
382 while(lines[i+1] && (lines[i+1][0]==' ' || lines[i+1][0]=='\t')) {
383 i++;
384 dummy = lines[i];
385 while(*dummy==' ' || *dummy=='\t') dummy++;
386 tmp = g_strdup_printf("%s %s",dummy2, dummy);
387 g_free(dummy2);
388 dummy2 = tmp;
390 *list = sipe_utils_nameval_add(*list, parts[0], dummy2);
391 g_free(dummy2);
392 g_strfreev(parts);
395 return TRUE;
398 GSList*
399 sipe_utils_nameval_add(GSList* list, const gchar *name, const gchar *value)
401 struct sipnameval *element = g_new0(struct sipnameval,1);
403 /* SANITY CHECK: the calling code must be fixed if this happens! */
404 if (!value) {
405 SIPE_DEBUG_ERROR("sipe_utils_nameval_add: NULL value for %s",
406 name);
407 value = "";
410 element->name = g_strdup(name);
411 element->value = g_strdup(value);
412 return g_slist_append(list, element);
415 void
416 sipe_utils_nameval_free(GSList *list) {
417 struct sipnameval *elem;
418 while(list) {
419 elem = list->data;
420 list = g_slist_remove(list,elem);
421 g_free(elem->name);
422 g_free(elem->value);
423 g_free(elem);
427 const gchar *
428 sipe_utils_nameval_find(const GSList *list, const gchar *name)
430 return sipe_utils_nameval_find_instance (list, name, 0);
433 const gchar *
434 sipe_utils_nameval_find_instance(const GSList *list, const gchar *name, int which)
436 const GSList *tmp;
437 struct sipnameval *elem;
438 int i = 0;
439 tmp = list;
440 while(tmp) {
441 elem = tmp->data;
442 // OCS2005 can send the same header in either all caps or mixed case
443 if (sipe_strcase_equal(elem->name, name)) {
444 if (i == which) {
445 return elem->value;
447 i++;
449 tmp = g_slist_next(tmp);
451 return NULL;
454 gchar *sipe_utils_str_replace(const gchar *string,
455 const gchar *delimiter,
456 const gchar *replacement)
458 gchar **split;
459 gchar *result;
461 if (!string || !delimiter || !replacement) return NULL;
463 split = g_strsplit(string, delimiter, 0);
464 result = g_strjoinv(replacement, split);
465 g_strfreev(split);
467 return result;
470 void sipe_utils_shrink_buffer(struct sipe_transport_connection *conn,
471 const gchar *unread)
473 conn->buffer_used -= unread - conn->buffer;
474 /* string terminator is not included in buffer_used */
475 memmove(conn->buffer, unread, conn->buffer_used + 1);
478 gboolean sipe_utils_ip_is_private(const char *ip)
480 return g_str_has_prefix(ip, "10.") ||
481 g_str_has_prefix(ip, "172.16.") ||
482 g_str_has_prefix(ip, "192.168.");
485 gchar *sipe_utils_presence_key(const gchar *uri)
487 return g_strdup_printf("<presence><%s>", uri);
490 gchar *sipe_utils_subscription_key(const gchar *event,
491 const gchar *uri)
493 gchar *key = NULL;
495 if (!is_empty(event)) {
496 if (!g_ascii_strcasecmp(event, "presence")) {
497 /* Subscription is identified by <presence><uri> key */
498 key = sipe_utils_presence_key(uri);
499 } else {
500 /* Subscription is identified by <event> key */
501 key = g_strdup_printf("<%s>", event);
505 return key;
508 gchar *
509 sipe_utils_uri_unescape(const gchar *string)
511 gchar *unescaped;
512 gchar *tmp;
514 if (!string)
515 return NULL;
517 #if GLIB_CHECK_VERSION(2,16,0)
518 unescaped = g_uri_unescape_string(string, NULL);
519 #else
520 // based on libpurple/util.c:purple_url_decode()
522 GString *buf = g_string_new(NULL);
523 size_t len = strlen(string);
524 char hex[3];
526 hex[2] = '\0';
528 while (len--) {
529 gchar c = *string++;
531 if ((len >= 2) && (c == '%')) {
532 strncpy(hex, string, 2);
533 c = strtol(hex, NULL, 16);
535 string += 2;
536 len -= 2;
539 g_string_append_c(buf, c);
542 unescaped = g_string_free(buf, FALSE);
544 #endif
545 if (unescaped && !g_utf8_validate(unescaped, -1, (const gchar **)&tmp))
546 *tmp = '\0';
548 return unescaped;
551 gboolean
552 sipe_utils_is_avconf_uri(const gchar *uri)
554 return g_strstr_len(uri, -1, "app:conf:audio-video:") != NULL;
558 * Only appends if no such value already stored.
559 * Like Set in Java.
561 GSList *
562 slist_insert_unique_sorted(GSList *list, gpointer data, GCompareFunc func) {
563 GSList * res = list;
564 if (!g_slist_find_custom(list, data, func)) {
565 res = g_slist_insert_sorted(list, data, func);
567 return res;
571 Local Variables:
572 mode: c
573 c-file-style: "bsd"
574 indent-tabs-mode: t
575 tab-width: 8
576 End: