Move slist_insert_unique_sorted to sipe-utils
[siplcs.git] / src / core / sipe-utils.c
blob42261e50d36bf4536f727ae65c2a2ac8321332ac
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 guint
121 sipe_get_pub_instance(struct sipe_core_private *sipe_private,
122 int publication_key)
124 unsigned res = 0;
125 gchar *epid = get_epid(sipe_private);
127 sscanf(epid, "%08x", &res);
128 g_free(epid);
130 if (publication_key == SIPE_PUB_DEVICE) {
131 /* as is */
132 } else if (publication_key == SIPE_PUB_STATE_MACHINE) { /* First hexadecimal digit is 0x3 */
133 res = (res >> 4) | 0x30000000;
134 } else if (publication_key == SIPE_PUB_STATE_USER) {
135 res = 0x20000000; /* fixed */
136 } else if (publication_key == SIPE_PUB_STATE_CALENDAR) { /* First hexadecimal digit is 0x4 */
137 res = (res >> 4) | 0x40000000;
138 } else if (publication_key == SIPE_PUB_STATE_CALENDAR_OOF) { /* First hexadecimal digit is 0x5 */
139 res = (res >> 4) | 0x50000000;
140 } else if (publication_key == SIPE_PUB_CALENDAR_DATA ||
141 publication_key == SIPE_PUB_NOTE_OOF)
142 { /* First hexadecimal digit is 0x4 */
143 unsigned calendar_id = 0;
144 char *mail_hash = sipe_get_epid(SIPE_ACCOUNT_DATA_PRIVATE->email, "", "");
146 sscanf(mail_hash, "%08x", &calendar_id);
147 g_free(mail_hash);
148 res = (calendar_id >> 4) | 0x40000000;
151 return res;
154 gboolean
155 sipe_is_bad_alias(const char *uri,
156 const char *alias)
158 char *uri_alias;
159 gboolean result = FALSE;
161 if (!uri) return FALSE;
162 if (!alias) return TRUE;
164 if (g_str_has_prefix(alias, "sip:") || g_str_has_prefix(alias, "sips:")) return TRUE;
166 /* check if alias is just SIP URI but without 'sip:' prefix */
167 uri_alias = sip_uri_from_name(alias);
168 if (sipe_strcase_equal(uri, uri_alias)) {
169 result = TRUE;
171 g_free(uri_alias);
173 return result;
176 gboolean
177 is_empty(const char *st)
179 if (!st || strlen(st) == 0)
181 return TRUE;
183 /* suspecious leading or trailing staces */
184 else if (isspace((unsigned char) *st) ||
185 isspace((unsigned char) *(st + strlen(st) - 1)))
187 /* to not modify original string */
188 char *dup = g_strdup(st);
189 if (strlen(g_strstrip(dup)) == 0) {
190 g_free(dup);
191 return TRUE;
193 g_free(dup);
195 return FALSE;
198 /** Returns newly allocated string. Must be g_free()'d */
199 char *
200 replace(const char *st,
201 const char *search,
202 const char *replace)
204 char **tmp;
205 char *res;
207 if (!st) return NULL;
209 res = g_strjoinv(replace, tmp = g_strsplit(st, search, -1));
210 g_strfreev(tmp);
211 return res;
214 void sipe_utils_message_debug(const gchar *type,
215 const gchar *header,
216 const gchar *body,
217 gboolean sending)
219 if (sipe_backend_debug_enabled()) {
220 GString *str = g_string_new("");
221 GTimeVal currtime;
222 gchar *time_str;
223 const char *marker = sending ?
224 ">>>>>>>>>>" :
225 "<<<<<<<<<<";
226 gchar *tmp;
228 g_get_current_time(&currtime);
229 time_str = g_time_val_to_iso8601(&currtime);
230 g_string_append_printf(str, "\nMESSAGE START %s %s - %s\n", marker, type, time_str);
231 g_string_append(str, tmp = replace(header, "\r\n", "\n"));
232 g_free(tmp);
233 g_string_append(str, "\n");
234 if (body) {
235 g_string_append(str, tmp = replace(body, "\r\n", "\n"));
236 g_free(tmp);
237 g_string_append(str, "\n");
239 g_string_append_printf(str, "MESSAGE END %s %s - %s", marker, type, time_str);
240 g_free(time_str);
241 SIPE_DEBUG_INFO_NOFORMAT(str->str);
242 g_string_free(str, TRUE);
246 gboolean
247 sipe_strequal(const gchar *left, const gchar *right)
249 #if GLIB_CHECK_VERSION(2,16,0)
250 return (g_strcmp0(left, right) == 0);
251 #else
252 return ((left == NULL && right == NULL) ||
253 (left != NULL && right != NULL && strcmp(left, right) == 0));
254 #endif
257 gboolean
258 sipe_strcase_equal(const gchar *left, const gchar *right)
260 return ((left == NULL && right == NULL) ||
261 (left != NULL && right != NULL && g_ascii_strcasecmp(left, right) == 0));
264 gint sipe_strcompare(gconstpointer a, gconstpointer b)
266 #if GLIB_CHECK_VERSION(2,16,0)
267 return (g_strcmp0(a, b));
268 #else
269 if (!a)
270 return -(a != b);
271 if (!b)
272 return a != b;
273 return strcmp(a, b);
274 #endif
277 time_t
278 sipe_utils_str_to_time(const gchar *timestamp)
280 GTimeVal time;
281 guint len;
283 /* We have to make sure that the ISO8601 contains a time zone offset,
284 otherwise the time is interpreted as local time, not UTC!
285 @TODO: is there a better way to check this? */
286 if (timestamp && (len = strlen(timestamp) > 0) &&
287 isdigit(timestamp[len - 1])) {
288 gchar *tmp = g_strdup_printf("%sZ", timestamp);
289 g_time_val_from_iso8601(tmp, &time);
290 g_free(tmp);
291 } else {
292 g_time_val_from_iso8601(timestamp, &time);
294 return time.tv_sec;
297 gchar *
298 sipe_utils_time_to_str(time_t timestamp)
300 GTimeVal time = { timestamp, 0 };
301 return g_time_val_to_iso8601(&time);
304 size_t
305 hex_str_to_buff(const char *hex_str, guint8 **buff)
307 char two_digits[3];
308 size_t length;
309 size_t i;
311 if (!buff) return 0;
312 if (!hex_str) return 0;
314 length = strlen(hex_str)/2;
315 *buff = (unsigned char *)g_malloc(length);
316 for (i = 0; i < length; i++) {
317 two_digits[0] = hex_str[i * 2];
318 two_digits[1] = hex_str[i * 2 + 1];
319 two_digits[2] = '\0';
320 (*buff)[i] = (unsigned char)strtoul(two_digits, NULL, 16);
323 return length;
326 char *
327 buff_to_hex_str(const guint8 *buff, const size_t buff_len)
329 char *res;
330 size_t i, j;
332 if (!buff) return NULL;
334 res = g_malloc(buff_len * 2 + 1);
335 for (i = 0, j = 0; i < buff_len; i++, j+=2) {
336 sprintf(&res[j], "%02X", buff[i]);
338 res[j] = '\0';
339 return res;
342 gboolean
343 sipe_utils_parse_lines(GSList **list, gchar **lines, gchar *delimiter)
345 int i;
346 gchar **parts;
347 gchar *dummy;
348 gchar *dummy2;
349 gchar *tmp;
351 for(i = 0; lines[i] && strlen(lines[i]) > 2; i++) {
352 parts = g_strsplit(lines[i], delimiter, 2);
353 if(!parts[0] || !parts[1]) {
354 g_strfreev(parts);
355 return FALSE;
357 dummy = parts[1];
358 dummy2 = 0;
359 while(*dummy==' ' || *dummy=='\t') dummy++;
360 dummy2 = g_strdup(dummy);
361 while(lines[i+1] && (lines[i+1][0]==' ' || lines[i+1][0]=='\t')) {
362 i++;
363 dummy = lines[i];
364 while(*dummy==' ' || *dummy=='\t') dummy++;
365 tmp = g_strdup_printf("%s %s",dummy2, dummy);
366 g_free(dummy2);
367 dummy2 = tmp;
369 *list = sipe_utils_nameval_add(*list, parts[0], dummy2);
370 g_free(dummy2);
371 g_strfreev(parts);
374 return TRUE;
377 GSList*
378 sipe_utils_nameval_add(GSList* list, const gchar *name, const gchar *value)
380 struct sipnameval *element = g_new0(struct sipnameval,1);
382 /* SANITY CHECK: the calling code must be fixed if this happens! */
383 if (!value) {
384 SIPE_DEBUG_ERROR("sipe_utils_nameval_add: NULL value for %s",
385 name);
386 value = "";
389 element->name = g_strdup(name);
390 element->value = g_strdup(value);
391 return g_slist_append(list, element);
394 void
395 sipe_utils_nameval_free(GSList *list) {
396 struct sipnameval *elem;
397 while(list) {
398 elem = list->data;
399 list = g_slist_remove(list,elem);
400 g_free(elem->name);
401 g_free(elem->value);
402 g_free(elem);
406 const gchar *
407 sipe_utils_nameval_find(const GSList *list, const gchar *name)
409 return sipe_utils_nameval_find_instance (list, name, 0);
412 const gchar *
413 sipe_utils_nameval_find_instance(const GSList *list, const gchar *name, int which)
415 const GSList *tmp;
416 struct sipnameval *elem;
417 int i = 0;
418 tmp = list;
419 while(tmp) {
420 elem = tmp->data;
421 // OCS2005 can send the same header in either all caps or mixed case
422 if (sipe_strcase_equal(elem->name, name)) {
423 if (i == which) {
424 return elem->value;
426 i++;
428 tmp = g_slist_next(tmp);
430 return NULL;
433 gchar *sipe_utils_str_replace(const gchar *string,
434 const gchar *delimiter,
435 const gchar *replacement)
437 gchar **split;
438 gchar *result;
440 if (!string || !delimiter || !replacement) return NULL;
442 split = g_strsplit(string, delimiter, 0);
443 result = g_strjoinv(replacement, split);
444 g_strfreev(split);
446 return result;
449 void sipe_utils_shrink_buffer(struct sipe_transport_connection *conn,
450 const gchar *unread)
452 conn->buffer_used -= unread - conn->buffer;
453 /* string terminator is not included in buffer_used */
454 memmove(conn->buffer, unread, conn->buffer_used + 1);
457 gboolean sipe_utils_ip_is_private(const char *ip)
459 return g_str_has_prefix(ip, "10.") ||
460 g_str_has_prefix(ip, "172.16.") ||
461 g_str_has_prefix(ip, "192.168.");
464 gchar *sipe_utils_presence_key(const gchar *uri)
466 return g_strdup_printf("<presence><%s>", uri);
469 gchar *sipe_utils_subscription_key(const gchar *event,
470 const gchar *uri)
472 gchar *key = NULL;
474 if (!is_empty(event)) {
475 if (!g_ascii_strcasecmp(event, "presence")) {
476 /* Subscription is identified by <presence><uri> key */
477 key = sipe_utils_presence_key(uri);
478 } else {
479 /* Subscription is identified by <event> key */
480 key = g_strdup_printf("<%s>", event);
484 return key;
487 gchar *
488 sipe_utils_uri_unescape(const gchar *string)
490 gchar *unescaped;
491 gchar *tmp;
493 if (!string)
494 return NULL;
496 #if GLIB_CHECK_VERSION(2,16,0)
497 unescaped = g_uri_unescape_string(string, NULL);
498 #else
499 // based on libpurple/util.c:purple_url_decode()
501 GString *buf = g_string_new(NULL);
502 size_t len = strlen(string);
503 char hex[3];
505 hex[2] = '\0';
507 while (len--) {
508 gchar c = *string++;
510 if ((len >= 2) && (c == '%')) {
511 strncpy(hex, string, 2);
512 c = strtol(hex, NULL, 16);
514 string += 2;
515 len -= 2;
518 g_string_append_c(buf, c);
521 unescaped = g_string_free(buf, FALSE);
523 #endif
524 if (unescaped && !g_utf8_validate(unescaped, -1, (const gchar **)&tmp))
525 *tmp = '\0';
527 return unescaped;
530 gboolean
531 sipe_utils_is_avconf_uri(const gchar *uri)
533 return g_strstr_len(uri, -1, "app:conf:audio-video:") != NULL;
537 * Only appends if no such value already stored.
538 * Like Set in Java.
540 GSList *
541 slist_insert_unique_sorted(GSList *list, gpointer data, GCompareFunc func) {
542 GSList * res = list;
543 if (!g_slist_find_custom(list, data, func)) {
544 res = g_slist_insert_sorted(list, data, func);
546 return res;
550 Local Variables:
551 mode: c
552 c-file-style: "bsd"
553 indent-tabs-mode: t
554 tab-width: 8
555 End: