refactoring str_to_time
[siplcs.git] / src / core / sipe-utils.c
blob3e31fbac63a03759ac9211c5e72cc1b2649869be
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) break;
125 parent = node;
127 va_end(args);
129 return node;
132 gint xmlnode_get_int_attrib(xmlnode *node,
133 const char *attr,
134 gint fallback)
136 const char *value = xmlnode_get_attrib(node, attr);
137 return(value ? atoi(value) : fallback);
141 //* @TODO Do we need compat with glib < 2.8 ? */
142 char *sipe_get_host_name(void)
144 #if GLIB_CHECK_VERSION(2,8,0)
145 const gchar * hostname = g_get_host_name();
146 #else
147 static char hostname[256];
148 int ret = gethostname(hostname, sizeof(hostname));
149 hostname[sizeof(hostname) - 1] = '\0';
150 if (ret == -1 || hostname[0] == '\0') {
151 purple_debug(PURPLE_DEBUG_MISC, "sipe", "Error when getting host name. Using \"localhost.\"\n");
152 g_strerror(errno);
153 strcpy(hostname, "localhost");
155 #endif
156 /*const gchar * hostname = purple_get_host_name();*/
157 return (char *)hostname;
160 gchar *
161 get_epid(struct sipe_account_data *sip)
163 if (!sip->epid) {
164 gchar *self_sip_uri = sip_uri_self(sip);
165 sip->epid = sipe_get_epid(self_sip_uri,
166 sipe_get_host_name(),
167 purple_network_get_my_ip(-1));
168 g_free(self_sip_uri);
170 return g_strdup(sip->epid);
173 guint
174 sipe_get_pub_instance(struct sipe_account_data *sip,
175 int publication_key)
177 unsigned res = 0;
178 gchar *epid = get_epid(sip);
180 sscanf(epid, "%08x", &res);
181 g_free(epid);
183 if (publication_key == SIPE_PUB_DEVICE) {
184 /* as is */
185 } else if (publication_key == SIPE_PUB_STATE_MACHINE) { /* First hexadecimal digit is 0x3 */
186 res = (res >> 4) | 0x30000000;
187 } else if (publication_key == SIPE_PUB_STATE_USER) {
188 res = 0x20000000; /* fixed */
189 } else if (publication_key == SIPE_PUB_STATE_CALENDAR) { /* First hexadecimal digit is 0x4 */
190 res = (res >> 4) | 0x40000000;
191 } else if (publication_key == SIPE_PUB_STATE_CALENDAR_OOF) { /* First hexadecimal digit is 0x5 */
192 res = (res >> 4) | 0x50000000;
193 } else if (publication_key == SIPE_PUB_CALENDAR_DATA ||
194 publication_key == SIPE_PUB_NOTE_OOF)
195 { /* First hexadecimal digit is 0x4 */
196 unsigned calendar_id = 0;
197 char *mail_hash = sipe_get_epid(sip->email, "", "");
199 sscanf(mail_hash, "%08x", &calendar_id);
200 g_free(mail_hash);
201 res = (calendar_id >> 4) | 0x40000000;
204 return res;
206 /* an old version
207 guint
208 sipe_get_pub_instance_(struct sipe_account_data *sip,
209 const char *publication_key)
211 unsigned part_1;
212 unsigned part_2;
213 gchar *epid = get_epid(sip);
214 sscanf(epid, "%08x", &part_1);
215 g_free(epid);
216 sscanf(publication_key, "%uh", &part_2);
217 return part_1 + part_2;
220 gboolean
221 sipe_is_bad_alias(const char *uri,
222 const char *alias)
224 char *uri_alias;
225 gboolean result = FALSE;
227 if (!uri) return FALSE;
228 if (!alias) return TRUE;
230 if (g_str_has_prefix(alias, "sip:") || g_str_has_prefix(alias, "sips:")) return TRUE;
232 /* check if alias is just SIP URI but without 'sip:' prefix */
233 uri_alias = sip_uri_from_name(alias);
234 if (!g_ascii_strcasecmp(uri, uri_alias)) {
235 result = TRUE;
237 g_free(uri_alias);
239 return result;
242 gboolean
243 is_empty(const char *st)
245 if (!st || strlen(st) == 0)
247 return TRUE;
249 /* suspecious leading or trailing staces */
250 else if (isspace((unsigned char) *st) ||
251 isspace((unsigned char) *(st + strlen(st) - 1)))
253 /* to not modify original string */
254 char *dup = g_strdup(st);
255 if (strlen(g_strstrip(dup)) == 0) {
256 g_free(dup);
257 return TRUE;
259 g_free(dup);
261 return FALSE;
264 /** Returns newly allocated string. Must be g_free()'d */
265 char *
266 replace(const char *st,
267 const char *search,
268 const char *replace)
270 char **tmp;
271 char *res;
273 if (!st) return NULL;
275 res = g_strjoinv(replace, tmp = g_strsplit(st, search, -1));
276 g_strfreev(tmp);
277 return res;
280 char *
281 fix_newlines(const char *st)
283 return replace(st, "\r\n", "\n");
286 time_t
287 sipe_utils_str_to_time(const char *timestamp)
289 return purple_str_to_time(timestamp, TRUE, NULL, NULL, NULL);
293 Local Variables:
294 mode: c
295 c-file-style: "bsd"
296 indent-tabs-mode: t
297 tab-width: 8
298 End: