author | Mikael Berthe <mikael@lilotux.net> |
Sun, 21 Jan 2007 15:13:36 +0100 | |
changeset 1130 | b2040a7fb7fc |
parent 417 | c3ae9251c197 |
permissions | -rw-r--r-- |
25 | 1 |
/* |
2 |
* This program is free software; you can redistribute it and/or modify |
|
3 |
* it under the terms of the GNU General Public License as published by |
|
4 |
* the Free Software Foundation; either version 2 of the License, or |
|
5 |
* (at your option) any later version. |
|
6 |
* |
|
7 |
* This program is distributed in the hope that it will be useful, |
|
8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
10 |
* GNU General Public License for more details. |
|
11 |
* |
|
12 |
* You should have received a copy of the GNU General Public License |
|
13 |
* along with this program; if not, write to the Free Software |
|
14 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
15 |
* |
|
417
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
16 |
* Copyrights |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
17 |
* |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
18 |
* Portions created by or assigned to Jabber.com, Inc. are |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
19 |
* Copyright (c) 1999-2002 Jabber.com, Inc. All Rights Reserved. Contact |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
20 |
* information for Jabber.com, Inc. is available at http://www.jabber.com/. |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
21 |
* |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
22 |
* Portions Copyright (c) 1998-1999 Jeremie Miller. |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
23 |
* |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
24 |
* Acknowledgements |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
25 |
* |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
26 |
* Special thanks to the Jabber Open Source Contributors for their |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
27 |
* suggestions and support of Jabber. |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
28 |
* |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
29 |
* |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
30 |
*/ |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
31 |
|
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
32 |
/** |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
33 |
* @file pproxy.c |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
34 |
* @brief presence proxy database - DEPRECATED |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
35 |
* |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
36 |
* @deprecated these functions are not used by jabberd itself (but aim-t uses them), they will be removed from jabberd |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
37 |
* |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
38 |
* The presence proxy database is used to store presences for different resources of a JID. |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
39 |
* |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
40 |
* these aren't the most efficient things in the world, a hash optimized for tiny spaces would be far better |
25 | 41 |
*/ |
42 |
||
43 |
#include "jabber.h" |
|
44 |
||
45 |
/* these aren't the most efficient things in the world, a hash optimized for tiny spaces would be far better */ |
|
46 |
||
47 |
ppdb _ppdb_new(pool p, jid id) |
|
48 |
{ |
|
49 |
ppdb ret; |
|
50 |
ret = pmalloc(p,sizeof(_ppdb)); |
|
51 |
ret->p = p; |
|
52 |
ret->pri = -1; |
|
53 |
ret->next = NULL; |
|
54 |
ret->user = NULL; |
|
55 |
ret->x = NULL; |
|
56 |
ret->id = jid_new(p,jid_full(id)); |
|
57 |
||
58 |
return ret; |
|
59 |
} |
|
60 |
||
61 |
ppdb _ppdb_get(ppdb db, jid id) |
|
62 |
{ |
|
63 |
ppdb cur; |
|
64 |
||
65 |
if(db == NULL || id == NULL) return NULL; |
|
66 |
||
67 |
for(cur = db->next; cur != NULL; cur = cur->next) |
|
417
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
68 |
if(jid_cmp(cur->id,id) == 0) return cur; |
25 | 69 |
|
70 |
return NULL; |
|
71 |
} |
|
72 |
||
73 |
ppdb ppdb_insert(ppdb db, jid id, xmlnode x) |
|
74 |
{ |
|
75 |
ppdb cur, curu; |
|
76 |
pool p; |
|
77 |
||
78 |
if(id == NULL || id->server == NULL || x == NULL) |
|
417
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
79 |
return db; |
25 | 80 |
|
81 |
/* new ppdb list dummy holder */ |
|
82 |
if(db == NULL) |
|
83 |
{ |
|
417
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
84 |
p = pool_heap(1024); |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
85 |
db = _ppdb_new(p,NULL); |
25 | 86 |
} |
87 |
||
88 |
cur = _ppdb_get(db,id); |
|
89 |
||
90 |
/* just update it */ |
|
91 |
if(cur != NULL) |
|
92 |
{ |
|
417
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
93 |
xmlnode_free(cur->x); |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
94 |
cur->x = xmlnode_dup(x); |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
95 |
cur->pri = jutil_priority(x); |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
96 |
return db; |
25 | 97 |
} |
98 |
||
99 |
/* make an entry for it */ |
|
100 |
cur = _ppdb_new(db->p,id); |
|
101 |
cur->x = xmlnode_dup(x); |
|
102 |
cur->pri = jutil_priority(x); |
|
103 |
cur->next = db->next; |
|
104 |
db->next = cur; |
|
105 |
||
417
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
106 |
/* if this is a user's resource presence, get the the user entry */ |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
107 |
if(id->user != NULL && (curu = _ppdb_get(db,jid_user(id))) != cur) |
25 | 108 |
{ |
417
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
109 |
/* no user entry, make one */ |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
110 |
if(curu == NULL) |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
111 |
{ |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
112 |
curu = _ppdb_new(db->p,jid_user(id)); |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
113 |
curu->next = db->next; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
114 |
db->next = curu; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
115 |
} |
25 | 116 |
|
417
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
117 |
/* insert this resource into the user list */ |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
118 |
cur->user = curu->user; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
119 |
curu->user = cur; |
25 | 120 |
} |
121 |
||
122 |
return db; |
|
123 |
} |
|
124 |
||
125 |
xmlnode ppdb_primary(ppdb db, jid id) |
|
126 |
{ |
|
127 |
ppdb cur, top; |
|
128 |
||
129 |
if(db == NULL || id == NULL) return NULL; |
|
130 |
||
131 |
cur = _ppdb_get(db,id); |
|
132 |
||
133 |
if(cur == NULL) return NULL; |
|
134 |
||
135 |
/* not user@host check, just return */ |
|
136 |
if(id->user == NULL || id->resource != NULL) return cur->x; |
|
137 |
||
138 |
top = cur; |
|
139 |
for(cur = cur->user; cur != NULL; cur = cur->user) |
|
417
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
140 |
if(cur->pri >= top->pri) top = cur; |
25 | 141 |
|
142 |
if(top != NULL && top->pri >= 0) return top->x; |
|
143 |
||
144 |
return NULL; |
|
145 |
} |
|
146 |
||
147 |
/* return the presence for the id, successive calls return all of the known resources for a user@host address */ |
|
148 |
xmlnode ppdb_get(ppdb db, jid id) |
|
149 |
{ |
|
150 |
static ppdb last = NULL; |
|
151 |
ppdb cur; |
|
152 |
||
153 |
if(db == NULL || id == NULL) return NULL; |
|
154 |
||
155 |
/* MODE: if this is NOT just user@host addy, return just the single entry */ |
|
156 |
if(id->user == NULL || id->resource != NULL) |
|
157 |
{ |
|
417
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
158 |
/* we were just here, return now */ |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
159 |
if(last != NULL) |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
160 |
{ |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
161 |
last = NULL; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
162 |
return NULL; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
163 |
} |
25 | 164 |
|
417
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
165 |
last = _ppdb_get(db,id); |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
166 |
if(last != NULL) |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
167 |
return last->x; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
168 |
else |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
169 |
return NULL; |
25 | 170 |
} |
171 |
||
172 |
/* handle looping for user@host */ |
|
173 |
||
174 |
/* we're already in the loop */ |
|
175 |
if(last != NULL) |
|
176 |
{ |
|
417
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
177 |
/* this is the last entry in the list */ |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
178 |
if(last->user == NULL) |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
179 |
{ |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
180 |
last = NULL; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
181 |
return NULL; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
182 |
} |
25 | 183 |
|
417
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
184 |
last = last->user; |
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
185 |
return last->x; |
25 | 186 |
} |
187 |
||
188 |
/* start a new loop */ |
|
189 |
cur = _ppdb_get(db,id); |
|
190 |
||
191 |
if(cur == NULL) return NULL; |
|
192 |
||
193 |
last = cur->user; |
|
194 |
if(last != NULL) |
|
417
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
195 |
return last->x; |
25 | 196 |
else |
417
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
197 |
return NULL; |
25 | 198 |
} |
199 |
||
200 |
||
201 |
void ppdb_free(ppdb db) |
|
202 |
{ |
|
203 |
ppdb cur; |
|
204 |
||
205 |
if(db == NULL) return; |
|
206 |
||
207 |
for(cur = db; cur != NULL; cur = cur->next) |
|
417
c3ae9251c197
Sync libjabber with upstream
Mikael Berthe <mikael@lilotux.net>
parents:
25
diff
changeset
|
208 |
xmlnode_free(cur->x); |
25 | 209 |
|
210 |
pool_free(db->p); |
|
211 |
} |
|
212 |