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.
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.
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.
17 * Copyright (C) 1998-1999 The Jabber Team http://jabber.org/
22 /* these aren't the most efficient things in the world, a hash optimized for tiny spaces would be far better */
24 ppdb
_ppdb_new(pool p
, jid id
)
27 ret
= pmalloc(p
,sizeof(_ppdb
));
33 ret
->id
= jid_new(p
,jid_full(id
));
38 ppdb
_ppdb_get(ppdb db
, jid id
)
42 if(db
== NULL
|| id
== NULL
) return NULL
;
44 for(cur
= db
->next
; cur
!= NULL
; cur
= cur
->next
)
45 if(jid_cmp(cur
->id
,id
) == 0) return cur
;
50 ppdb
ppdb_insert(ppdb db
, jid id
, xmlnode x
)
56 if(id
== NULL
|| id
->server
== NULL
|| x
== NULL
)
59 /* new ppdb list dummy holder */
66 cur
= _ppdb_get(db
,id
);
72 cur
->x
= xmlnode_dup(x
);
73 cur
->pri
= jutil_priority(x
);
77 /* make an entry for it */
78 cur
= _ppdb_new(db
->p
,id
);
79 cur
->x
= xmlnode_dup(x
);
80 cur
->pri
= jutil_priority(x
);
84 /* this is a presence from a resource, make an entry for just the user */
85 if(id
->user
!= NULL
&& id
->resource
!= NULL
)
87 /* modify the id to just user@host */
89 jid_set(id
,NULL
,JID_RESOURCE
);
90 curu
= _ppdb_get(db
,id
);
92 /* no user entry, make one */
95 curu
= _ppdb_new(db
->p
,id
);
96 curu
->next
= db
->next
;
101 jid_set(id
,res
,JID_RESOURCE
);
103 /* insert this resource into the user list */
104 cur
->user
= curu
->user
;
111 xmlnode
ppdb_primary(ppdb db
, jid id
)
115 if(db
== NULL
|| id
== NULL
) return NULL
;
117 cur
= _ppdb_get(db
,id
);
119 if(cur
== NULL
) return NULL
;
121 /* not user@host check, just return */
122 if(id
->user
== NULL
|| id
->resource
!= NULL
) return cur
->x
;
125 for(cur
= cur
->user
; cur
!= NULL
; cur
= cur
->user
)
126 if(cur
->pri
>= top
->pri
) top
= cur
;
128 if(top
!= NULL
&& top
->pri
>= 0) return top
->x
;
133 /* return the presence for the id, successive calls return all of the known resources for a user@host address */
134 xmlnode
ppdb_get(ppdb db
, jid id
)
136 static ppdb last
= NULL
;
139 if(db
== NULL
|| id
== NULL
) return NULL
;
141 /* MODE: if this is NOT just user@host addy, return just the single entry */
142 if(id
->user
== NULL
|| id
->resource
!= NULL
)
144 /* we were just here, return now */
151 last
= _ppdb_get(db
,id
);
158 /* handle looping for user@host */
160 /* we're already in the loop */
163 /* this is the last entry in the list */
164 if(last
->user
== NULL
)
174 /* start a new loop */
175 cur
= _ppdb_get(db
,id
);
177 if(cur
== NULL
) return NULL
;
187 void ppdb_free(ppdb db
)
191 if(db
== NULL
) return;
193 for(cur
= db
; cur
!= NULL
; cur
= cur
->next
)
194 xmlnode_free(cur
->x
);