core cleanup: xml replacement in sipe.c
[siplcs.git] / src / core / sipe-utils.c
blobc0248ed81008de52833fe2ccd8d4eacc929b8417
1 /**
2 * @file sipe-utils.c
4 * pidgin-sipe
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
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <time.h>
29 #include <glib.h>
31 #include "network.h"
33 #include "sip-sec.h"
34 #include "sipe-backend.h"
35 #include "sipe-utils.h"
36 #include "uuid.h"
37 #include "sipe.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);
50 gchar *gentag(void)
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)
70 gchar *from;
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 <...> */
79 tmp2 = tmp + 1;
80 tmp = strchr(tmp2, '>');
81 if (tmp) {
82 from = g_strndup(tmp2, tmp - tmp2);
83 } else {
84 SIPE_DEBUG_INFO_NOFORMAT("found < without > in From");
85 return NULL;
87 } else {
88 tmp = strchr(tmp2, ';');
89 if (tmp) {
90 from = g_strndup(tmp2, tmp - tmp2);
91 } else {
92 from = g_strdup(tmp2);
95 SIPE_DEBUG_INFO("got %s", from);
96 return from;
99 int parse_cseq(const gchar *hdr)
101 int res = -1;
102 gchar **items;
103 items = g_strsplit(hdr, " ", 1);
104 if (items[0]) {
105 res = atoi(items[0]);
107 g_strfreev(items);
108 return res;
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));
121 gchar *
122 get_epid(struct sipe_account_data *sip)
124 if (!sip->epid) {
125 gchar *self_sip_uri = sip_uri_self(sip);
126 sip->epid = sipe_get_epid(self_sip_uri,
127 g_get_host_name(),
128 purple_network_get_my_ip(-1));
129 g_free(self_sip_uri);
131 return g_strdup(sip->epid);
134 guint
135 sipe_get_pub_instance(struct sipe_account_data *sip,
136 int publication_key)
138 unsigned res = 0;
139 gchar *epid = get_epid(sip);
141 sscanf(epid, "%08x", &res);
142 g_free(epid);
144 if (publication_key == SIPE_PUB_DEVICE) {
145 /* as is */
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);
161 g_free(mail_hash);
162 res = (calendar_id >> 4) | 0x40000000;
165 return res;
167 /* an old version
168 guint
169 sipe_get_pub_instance_(struct sipe_account_data *sip,
170 const char *publication_key)
172 unsigned part_1;
173 unsigned part_2;
174 gchar *epid = get_epid(sip);
175 sscanf(epid, "%08x", &part_1);
176 g_free(epid);
177 sscanf(publication_key, "%uh", &part_2);
178 return part_1 + part_2;
181 gboolean
182 sipe_is_bad_alias(const char *uri,
183 const char *alias)
185 char *uri_alias;
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)) {
196 result = TRUE;
198 g_free(uri_alias);
200 return result;
203 gboolean
204 is_empty(const char *st)
206 if (!st || strlen(st) == 0)
208 return TRUE;
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) {
217 g_free(dup);
218 return TRUE;
220 g_free(dup);
222 return FALSE;
225 /** Returns newly allocated string. Must be g_free()'d */
226 char *
227 replace(const char *st,
228 const char *search,
229 const char *replace)
231 char **tmp;
232 char *res;
234 if (!st) return NULL;
236 res = g_strjoinv(replace, tmp = g_strsplit(st, search, -1));
237 g_strfreev(tmp);
238 return res;
241 char *
242 fix_newlines(const char *st)
244 return replace(st, "\r\n", "\n");
247 gboolean
248 sipe_strequal(const gchar *left, const gchar *right)
250 #if GLIB_CHECK_VERSION(2,16,0)
251 return (g_strcmp0(left, right) == 0);
252 #else
253 return ((left == NULL && right == NULL) ||
254 (left != NULL && right != NULL && strcmp(left, right) == 0));
255 #endif
258 gboolean
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));
265 time_t
266 sipe_utils_str_to_time(const gchar *timestamp)
268 GTimeVal time;
269 g_time_val_from_iso8601(timestamp, &time);
270 return time.tv_sec;
273 gchar *
274 sipe_utils_time_to_str(time_t timestamp)
276 GTimeVal time = { timestamp, 0 };
277 return g_time_val_to_iso8601(&time);
280 size_t
281 hex_str_to_buff(const char *hex_str, guint8 **buff)
283 char two_digits[3];
284 size_t length;
285 size_t i;
287 if (!buff) return 0;
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);
299 return length;
302 char *
303 buff_to_hex_str(const guint8 *buff, const size_t buff_len)
305 char *res;
306 size_t i, j;
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]);
314 res[j] = '\0';
315 return res;
318 gboolean
319 sipe_utils_parse_lines(GSList **list, gchar **lines)
321 int i;
322 gchar **parts;
323 gchar *dummy;
324 gchar *dummy2;
325 gchar *tmp;
327 for(i = 0; lines[i] && strlen(lines[i]) > 2; i++) {
328 parts = g_strsplit(lines[i], ":", 2);
329 if(!parts[0] || !parts[1]) {
330 g_strfreev(parts);
331 return FALSE;
333 dummy = parts[1];
334 dummy2 = 0;
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')) {
338 i++;
339 dummy = lines[i];
340 while(*dummy==' ' || *dummy=='\t') dummy++;
341 tmp = g_strdup_printf("%s %s",dummy2, dummy);
342 g_free(dummy2);
343 dummy2 = tmp;
345 *list = sipe_utils_nameval_add(*list, parts[0], dummy2);
346 g_free(dummy2);
347 g_strfreev(parts);
350 return TRUE;
353 GSList*
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! */
359 if (!value) {
360 SIPE_DEBUG_ERROR("sipe_utils_nameval_add: NULL value for %s",
361 name);
362 value = "";
365 element->name = g_strdup(name);
366 element->value = g_strdup(value);
367 return g_slist_append(list, element);
370 void
371 sipe_utils_nameval_free(GSList *list) {
372 struct sipnameval *elem;
373 while(list) {
374 elem = list->data;
375 list = g_slist_remove(list,elem);
376 g_free(elem->name);
377 g_free(elem->value);
378 g_free(elem);
382 const gchar *
383 sipe_utils_nameval_find(const GSList *list, const gchar *name)
385 return sipe_utils_nameval_find_instance (list, name, 0);
388 const gchar *
389 sipe_utils_nameval_find_instance(const GSList *list, const gchar *name, int which)
391 const GSList *tmp;
392 struct sipnameval *elem;
393 int i = 0;
394 tmp = list;
395 while(tmp) {
396 elem = tmp->data;
397 // OCS2005 can send the same header in either all caps or mixed case
398 if (sipe_strcase_equal(elem->name, name)) {
399 if (i == which) {
400 return elem->value;
402 i++;
404 tmp = g_slist_next(tmp);
406 return NULL;
409 gchar *sipe_utils_str_replace(const gchar *string,
410 const gchar *delimiter,
411 const gchar *replacement)
413 gchar **split;
414 gchar *result;
416 if (!string || !delimiter || !replacement) return NULL;
418 split = g_strsplit(string, delimiter, 0);
419 result = g_strjoinv(replacement, split);
420 g_strfreev(split);
422 return result;
426 Local Variables:
427 mode: c
428 c-file-style: "bsd"
429 indent-tabs-mode: t
430 tab-width: 8
431 End: