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
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
);
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
)
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 <...> */
74 tmp
= strchr(tmp2
, '>');
76 from
= g_strndup(tmp2
, tmp
- tmp2
);
78 purple_debug_info("sipe", "found < without > in From\n");
82 tmp
= strchr(tmp2
, ';');
84 from
= g_strndup(tmp2
, tmp
- tmp2
);
86 from
= g_strdup(tmp2
);
89 purple_debug_info("sipe", "got %s\n", from
);
93 int parse_cseq(const gchar
*hdr
)
97 items
= g_strsplit(hdr
, " ", 1);
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
, ...)
118 xmlnode
*node
= NULL
;
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;
132 gint
xmlnode_get_int_attrib(xmlnode
*node
,
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();
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");
153 strcpy(hostname
, "localhost");
156 /*const gchar * hostname = purple_get_host_name();*/
157 return (char *)hostname
;
161 get_epid(struct sipe_account_data
*sip
)
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
);
174 sipe_get_pub_instance(struct sipe_account_data
*sip
,
178 gchar
*epid
= get_epid(sip
);
180 sscanf(epid
, "%08x", &res
);
183 if (publication_key
== SIPE_PUB_DEVICE
) {
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
);
201 res
= (calendar_id
>> 4) | 0x40000000;
208 sipe_get_pub_instance_(struct sipe_account_data *sip,
209 const char *publication_key)
213 gchar *epid = get_epid(sip);
214 sscanf(epid, "%08x", &part_1);
216 sscanf(publication_key, "%uh", &part_2);
217 return part_1 + part_2;
221 sipe_is_bad_alias(const char *uri
,
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
)) {
243 is_empty(const char *st
)
245 if (!st
|| strlen(st
) == 0)
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) {
264 /** Returns newly allocated string. Must be g_free()'d */
266 replace(const char *st
,
273 if (!st
) return NULL
;
275 res
= g_strjoinv(replace
, tmp
= g_strsplit(st
, search
, -1));
281 fix_newlines(const char *st
)
283 return replace(st
, "\r\n", "\n");
287 sipe_utils_str_to_time(const char *timestamp
)
289 return purple_str_to_time(timestamp
, TRUE
, NULL
, NULL
, NULL
);