3127
|
1 /* --------------------------------------------------------------------------
|
|
2 *
|
|
3 * License
|
|
4 *
|
|
5 * The contents of this file are subject to the Jabber Open Source License
|
|
6 * Version 1.0 (the "JOSL"). You may not copy or use this file, in either
|
|
7 * source code or executable form, except in compliance with the JOSL. You
|
|
8 * may obtain a copy of the JOSL at http://www.jabber.org/ or at
|
|
9 * http://www.opensource.org/.
|
|
10 *
|
|
11 * Software distributed under the JOSL is distributed on an "AS IS" basis,
|
|
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the JOSL
|
|
13 * for the specific language governing rights and limitations under the
|
|
14 * JOSL.
|
|
15 *
|
|
16 * Copyrights
|
|
17 *
|
|
18 * Portions created by or assigned to Jabber.com, Inc. are
|
|
19 * Copyright (c) 1999-2002 Jabber.com, Inc. All Rights Reserved. Contact
|
|
20 * information for Jabber.com, Inc. is available at http://www.jabber.com/.
|
2086
|
21 *
|
3127
|
22 * Portions Copyright (c) 1998-1999 Jeremie Miller.
|
|
23 *
|
|
24 * Acknowledgements
|
|
25 *
|
|
26 * Special thanks to the Jabber Open Source Contributors for their
|
|
27 * suggestions and support of Jabber.
|
|
28 *
|
|
29 * Alternatively, the contents of this file may be used under the terms of the
|
|
30 * GNU General Public License Version 2 or later (the "GPL"), in which case
|
|
31 * the provisions of the GPL are applicable instead of those above. If you
|
|
32 * wish to allow use of your version of this file only under the terms of the
|
|
33 * GPL and not to allow others to use your version of this file under the JOSL,
|
|
34 * indicate your decision by deleting the provisions above and replace them
|
|
35 * with the notice and other provisions required by the GPL. If you do not
|
|
36 * delete the provisions above, a recipient may use your version of this file
|
|
37 * under either the JOSL or the GPL.
|
|
38 *
|
|
39 *
|
|
40 * --------------------------------------------------------------------------*/
|
2086
|
41
|
3127
|
42 #include "lib.h"
|
2086
|
43
|
|
44 jid jid_safe(jid id)
|
|
45 {
|
3127
|
46 unsigned char *str;
|
2086
|
47
|
|
48 if(strlen(id->server) == 0 || strlen(id->server) > 255)
|
|
49 return NULL;
|
|
50
|
|
51 /* lowercase the hostname, make sure it's valid characters */
|
|
52 for(str = id->server; *str != '\0'; str++)
|
|
53 {
|
|
54 *str = tolower(*str);
|
|
55 if(!(isalnum(*str) || *str == '.' || *str == '-' || *str == '_')) return NULL;
|
|
56 }
|
|
57
|
|
58 /* cut off the user */
|
|
59 if(id->user != NULL && strlen(id->user) > 64)
|
|
60 id->user[64] = '\0';
|
|
61
|
|
62 /* check for low and invalid ascii characters in the username */
|
|
63 if(id->user != NULL)
|
|
64 for(str = id->user; *str != '\0'; str++)
|
|
65 if(*str <= 32 || *str == ':' || *str == '@' || *str == '<' || *str == '>' || *str == '\'' || *str == '"' || *str == '&') return NULL;
|
|
66
|
|
67 return id;
|
|
68 }
|
|
69
|
|
70 jid jid_new(pool p, char *idstr)
|
|
71 {
|
|
72 char *server, *resource, *type, *str;
|
|
73 jid id;
|
|
74
|
|
75 if(p == NULL || idstr == NULL || strlen(idstr) == 0)
|
|
76 return NULL;
|
|
77
|
|
78 /* user@server/resource */
|
|
79
|
|
80 str = pstrdup(p, idstr);
|
|
81
|
3127
|
82 id = pmalloco(p,sizeof(struct jid_struct));
|
2086
|
83 id->p = p;
|
|
84
|
|
85 resource = strstr(str,"/");
|
|
86 if(resource != NULL)
|
|
87 {
|
|
88 *resource = '\0';
|
|
89 ++resource;
|
|
90 if(strlen(resource) > 0)
|
|
91 id->resource = resource;
|
|
92 }else{
|
|
93 resource = str + strlen(str); /* point to end */
|
|
94 }
|
|
95
|
|
96 type = strstr(str,":");
|
|
97 if(type != NULL && type < resource)
|
|
98 {
|
|
99 *type = '\0';
|
|
100 ++type;
|
|
101 str = type; /* ignore the type: prefix */
|
|
102 }
|
|
103
|
|
104 server = strstr(str,"@");
|
|
105 if(server == NULL || server > resource)
|
|
106 { /* if there's no @, it's just the server address */
|
|
107 id->server = str;
|
|
108 }else{
|
|
109 *server = '\0';
|
|
110 ++server;
|
|
111 id->server = server;
|
|
112 if(strlen(str) > 0)
|
|
113 id->user = str;
|
|
114 }
|
|
115
|
|
116 return jid_safe(id);
|
|
117 }
|
|
118
|
|
119 void jid_set(jid id, char *str, int item)
|
|
120 {
|
|
121 char *old;
|
|
122
|
|
123 if(id == NULL)
|
|
124 return;
|
|
125
|
|
126 /* invalidate the cached copy */
|
|
127 id->full = NULL;
|
|
128
|
|
129 switch(item)
|
|
130 {
|
|
131 case JID_RESOURCE:
|
|
132 if(str != NULL && strlen(str) != 0)
|
|
133 id->resource = pstrdup(id->p, str);
|
|
134 else
|
|
135 id->resource = NULL;
|
|
136 break;
|
|
137 case JID_USER:
|
|
138 old = id->user;
|
|
139 if(str != NULL && strlen(str) != 0)
|
|
140 id->user = pstrdup(id->p, str);
|
|
141 else
|
|
142 id->user = NULL;
|
|
143 if(jid_safe(id) == NULL)
|
|
144 id->user = old; /* revert if invalid */
|
|
145 break;
|
|
146 case JID_SERVER:
|
|
147 old = id->server;
|
|
148 id->server = pstrdup(id->p, str);
|
|
149 if(jid_safe(id) == NULL)
|
|
150 id->server = old; /* revert if invalid */
|
|
151 break;
|
|
152 }
|
|
153
|
|
154 }
|
|
155
|
|
156 char *jid_full(jid id)
|
|
157 {
|
|
158 spool s;
|
|
159
|
|
160 if(id == NULL)
|
|
161 return NULL;
|
|
162
|
|
163 /* use cached copy */
|
|
164 if(id->full != NULL)
|
|
165 return id->full;
|
|
166
|
|
167 s = spool_new(id->p);
|
|
168
|
|
169 if(id->user != NULL)
|
|
170 spooler(s, id->user,"@",s);
|
|
171
|
|
172 spool_add(s, id->server);
|
|
173
|
|
174 if(id->resource != NULL)
|
|
175 spooler(s, "/",id->resource,s);
|
|
176
|
|
177 id->full = spool_print(s);
|
|
178 return id->full;
|
|
179 }
|
|
180
|
|
181 /* parses a /resource?name=value&foo=bar into an xmlnode representing <resource name="value" foo="bar"/> */
|
|
182 xmlnode jid_xres(jid id)
|
|
183 {
|
|
184 char *cur, *qmark, *amp, *eq;
|
|
185 xmlnode x;
|
|
186
|
|
187 if(id == NULL || id->resource == NULL) return NULL;
|
|
188
|
|
189 cur = pstrdup(id->p, id->resource);
|
|
190 qmark = strstr(cur, "?");
|
|
191 if(qmark == NULL) return NULL;
|
|
192 *qmark = '\0';
|
|
193 qmark++;
|
|
194
|
|
195 x = _xmlnode_new(id->p, cur, NTYPE_TAG);
|
|
196
|
|
197 cur = qmark;
|
|
198 while(cur != '\0')
|
|
199 {
|
|
200 eq = strstr(cur, "=");
|
|
201 if(eq == NULL) break;
|
|
202 *eq = '\0';
|
|
203 eq++;
|
|
204
|
|
205 amp = strstr(eq, "&");
|
|
206 if(amp != NULL)
|
|
207 {
|
|
208 *amp = '\0';
|
|
209 amp++;
|
|
210 }
|
|
211
|
|
212 xmlnode_put_attrib(x,cur,eq);
|
|
213
|
|
214 if(amp != NULL)
|
|
215 cur = amp;
|
|
216 else
|
|
217 break;
|
|
218 }
|
|
219
|
|
220 return x;
|
|
221 }
|
|
222
|
|
223 /* local utils */
|
|
224 int _jid_nullstrcmp(char *a, char *b)
|
|
225 {
|
|
226 if(a == NULL && b == NULL) return 0;
|
|
227 if(a == NULL || b == NULL) return -1;
|
|
228 return strcmp(a,b);
|
|
229 }
|
|
230 int _jid_nullstrcasecmp(char *a, char *b)
|
|
231 {
|
|
232 if(a == NULL && b == NULL) return 0;
|
|
233 if(a == NULL || b == NULL) return -1;
|
|
234 return strcasecmp(a,b);
|
|
235 }
|
|
236
|
|
237 int jid_cmp(jid a, jid b)
|
|
238 {
|
|
239 if(a == NULL || b == NULL)
|
|
240 return -1;
|
|
241
|
|
242 if(_jid_nullstrcmp(a->resource, b->resource) != 0) return -1;
|
|
243 if(_jid_nullstrcasecmp(a->user, b->user) != 0) return -1;
|
|
244 if(_jid_nullstrcmp(a->server, b->server) != 0) return -1;
|
|
245
|
|
246 return 0;
|
|
247 }
|
|
248
|
|
249 /* suggested by Anders Qvist <quest@valdez.netg.se> */
|
|
250 int jid_cmpx(jid a, jid b, int parts)
|
|
251 {
|
|
252 if(a == NULL || b == NULL)
|
|
253 return -1;
|
|
254
|
|
255 if(parts & JID_RESOURCE && _jid_nullstrcmp(a->resource, b->resource) != 0) return -1;
|
|
256 if(parts & JID_USER && _jid_nullstrcasecmp(a->user, b->user) != 0) return -1;
|
|
257 if(parts & JID_SERVER && _jid_nullstrcmp(a->server, b->server) != 0) return -1;
|
|
258
|
|
259 return 0;
|
|
260 }
|
|
261
|
|
262 /* makes a copy of b in a's pool, requires a valid a first! */
|
|
263 jid jid_append(jid a, jid b)
|
|
264 {
|
|
265 jid next;
|
|
266
|
|
267 if(a == NULL)
|
|
268 return NULL;
|
|
269
|
|
270 if(b == NULL)
|
|
271 return a;
|
|
272
|
|
273 next = a;
|
|
274 while(next != NULL)
|
|
275 {
|
|
276 /* check for dups */
|
|
277 if(jid_cmp(next,b) == 0)
|
|
278 break;
|
|
279 if(next->next == NULL)
|
|
280 next->next = jid_new(a->p,jid_full(b));
|
|
281 next = next->next;
|
|
282 }
|
|
283 return a;
|
|
284 }
|
|
285
|
|
286 xmlnode jid_nodescan(jid id, xmlnode x)
|
|
287 {
|
|
288 xmlnode cur;
|
|
289 pool p;
|
|
290 jid tmp;
|
|
291
|
|
292 if(id == NULL || xmlnode_get_firstchild(x) == NULL) return NULL;
|
|
293
|
|
294 p = pool_new();
|
|
295 for(cur = xmlnode_get_firstchild(x); cur != NULL; cur = xmlnode_get_nextsibling(cur))
|
|
296 {
|
|
297 if(xmlnode_get_type(cur) != NTYPE_TAG) continue;
|
|
298
|
|
299 tmp = jid_new(p,xmlnode_get_attrib(cur,"jid"));
|
|
300 if(tmp == NULL) continue;
|
|
301
|
|
302 if(jid_cmp(tmp,id) == 0) break;
|
|
303 }
|
|
304 pool_free(p);
|
|
305
|
|
306 return cur;
|
|
307 }
|
3127
|
308
|
|
309 jid jid_user(jid a)
|
|
310 {
|
|
311 jid ret;
|
|
312
|
|
313 if(a == NULL || a->resource == NULL) return a;
|
|
314
|
|
315 ret = pmalloco(a->p,sizeof(struct jid_struct));
|
|
316 ret->p = a->p;
|
|
317 ret->user = a->user;
|
|
318 ret->server = a->server;
|
|
319
|
|
320 return ret;
|
|
321 }
|