Updated to release 1.7.1
[siplcs.git] / src / sipe-utils.c
blob7aa2e5343f250a48c5b6d7377ba3109785f0dd33
1 /**
2 * @file sipe-utils.c
4 * pidgin-sipe
6 * Copyright (C) 2009 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 <string.h>
24 #include <ctype.h>
25 #include <glib.h>
27 #include "debug.h"
28 #include "xmlnode.h"
30 #include "sipe.h"
31 #include "sipe-utils.h"
33 /* Generate 32 random bits */
34 #define RANDOM32BITS (rand() & 0xFFFF)
36 gchar *gencallid(void)
38 return g_strdup_printf("%04Xg%04Xa%04Xi%04Xm%04Xt%04Xb%04Xx%04Xx",
39 RANDOM32BITS, RANDOM32BITS, RANDOM32BITS,
40 RANDOM32BITS, RANDOM32BITS, RANDOM32BITS,
41 RANDOM32BITS, RANDOM32BITS);
44 gchar *gentag(void)
46 return g_strdup_printf("%04d%04d", RANDOM32BITS, RANDOM32BITS);
49 gchar *genconfid(void)
51 return g_strdup_printf("%04X%04X%04X%04X%04X%04X%04X%04X",
52 RANDOM32BITS, RANDOM32BITS, RANDOM32BITS,
53 RANDOM32BITS, RANDOM32BITS, RANDOM32BITS,
54 RANDOM32BITS, RANDOM32BITS);
57 gchar *get_contact(const struct sipe_account_data *sip)
59 return g_strdup(sip->contact);
62 gchar *parse_from(const gchar *hdr)
64 gchar *from;
65 const gchar *tmp, *tmp2 = hdr;
67 if (!hdr) return NULL;
68 purple_debug_info("sipe", "parsing address out of %s\n", hdr);
69 tmp = strchr(hdr, '<');
71 /* i hate the different SIP UA behaviours... */
72 if (tmp) { /* sip address in <...> */
73 tmp2 = tmp + 1;
74 tmp = strchr(tmp2, '>');
75 if (tmp) {
76 from = g_strndup(tmp2, tmp - tmp2);
77 } else {
78 purple_debug_info("sipe", "found < without > in From\n");
79 return NULL;
81 } else {
82 tmp = strchr(tmp2, ';');
83 if (tmp) {
84 from = g_strndup(tmp2, tmp - tmp2);
85 } else {
86 from = g_strdup(tmp2);
89 purple_debug_info("sipe", "got %s\n", from);
90 return from;
93 int parse_cseq(const gchar *hdr)
95 int res = -1;
96 gchar **items;
97 items = g_strsplit(hdr, " ", 1);
98 if (items[0]) {
99 res = atoi(items[0]);
101 g_strfreev(items);
102 return res;
105 gchar *sip_uri_from_name(const gchar *name)
107 return(g_strdup_printf("sip:%s", name));
110 gchar *sip_uri(const gchar *string)
112 return(strstr(string, "sip:") ? g_strdup(string) : sip_uri_from_name(string));
115 xmlnode *xmlnode_get_descendant(const xmlnode *parent, ...)
117 va_list args;
118 xmlnode *node = NULL;
119 const gchar *name;
121 va_start(args, parent);
122 while ((name = va_arg(args, const char *)) != NULL) {
123 node = xmlnode_get_child(parent, name);
124 if (node == NULL) return NULL;
125 parent = node;
127 va_end(args);
129 return node;
132 //* @TODO Do we need compat with glib < 2.8 ? */
133 char *sipe_get_host_name(void)
135 #if GLIB_CHECK_VERSION(2,8,0)
136 const gchar * hostname = g_get_host_name();
137 #else
138 static char hostname[256];
139 int ret = gethostname(hostname, sizeof(hostname));
140 hostname[sizeof(hostname) - 1] = '\0';
141 if (ret == -1 || hostname[0] == '\0') {
142 purple_debug(PURPLE_DEBUG_MISC, "sipe", "Error when getting host name. Using \"localhost.\"\n");
143 g_strerror(errno);
144 strcpy(hostname, "localhost");
146 #endif
147 /*const gchar * hostname = purple_get_host_name();*/
148 return (char *)hostname;
151 gchar *
152 get_epid(struct sipe_account_data *sip)
154 if (!sip->epid) {
155 gchar *self_sip_uri = sip_uri_self(sip);
156 sip->epid = sipe_get_epid(self_sip_uri,
157 sipe_get_host_name(),
158 purple_network_get_my_ip(-1));
159 g_free(self_sip_uri);
161 return g_strdup(sip->epid);
164 guint
165 sipe_get_pub_instance(struct sipe_account_data *sip,
166 const char *publication_key)
168 unsigned part_1;
169 unsigned part_2;
170 gchar *epid = get_epid(sip);
171 sscanf(epid, "%08x", &part_1);
172 g_free(epid);
173 sscanf(publication_key, "%uh", &part_2);
174 return part_1 + part_2;
177 gboolean
178 sipe_is_bad_alias(const char *uri,
179 const char *alias)
181 char *uri_alias;
182 gboolean result = FALSE;
184 if (!uri) return FALSE;
185 if (!alias) return TRUE;
187 if (g_str_has_prefix(alias, "sip:") || g_str_has_prefix(alias, "sips:")) return TRUE;
189 /* check if alias is just SIP URI but without 'sip:' prefix */
190 uri_alias = sip_uri_from_name(alias);
191 if (!g_ascii_strcasecmp(uri, uri_alias)) {
192 result = TRUE;
194 g_free(uri_alias);
196 return result;
199 gboolean
200 is_empty(const char *st)
202 if (!st || strlen(st) == 0)
204 return TRUE;
206 /* suspecious leading or trailing staces */
207 else if (isspace((unsigned char) *st) ||
208 isspace((unsigned char) *(st + strlen(st) - 1)))
210 /* to not modify original string */
211 char *dup = g_strdup(st);
212 if (strlen(g_strstrip(dup)) == 0) {
213 g_free(dup);
214 return TRUE;
216 g_free(dup);
218 return FALSE;
221 /** Returns newly allocated string. Must be g_free()'d */
222 char *
223 replace(const char *st,
224 const char *search,
225 const char *replace)
227 char **tmp;
228 char *res;
230 if (!st) return NULL;
232 res = g_strjoinv(replace, tmp = g_strsplit(st, search, -1));
233 g_strfreev(tmp);
234 return res;
237 char *
238 fix_newlines(const char *st)
240 return replace(st, "\r\n", "\n");
244 Local Variables:
245 mode: c
246 c-file-style: "bsd"
247 indent-tabs-mode: t
248 tab-width: 8
249 End: