[gaim-migrate @ 3063]
[pidgin-git.git] / src / protocols / jabber / pproxy.c
blob967229995b39f8bbce86677ef7fa1ab326ddf106
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.
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.
16 * Jabber
17 * Copyright (C) 1998-1999 The Jabber Team http://jabber.org/
20 #include "jabber.h"
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)
26 ppdb ret;
27 ret = pmalloc(p,sizeof(_ppdb));
28 ret->p = p;
29 ret->pri = -1;
30 ret->next = NULL;
31 ret->user = NULL;
32 ret->x = NULL;
33 ret->id = jid_new(p,jid_full(id));
35 return ret;
38 ppdb _ppdb_get(ppdb db, jid id)
40 ppdb cur;
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;
47 return NULL;
50 ppdb ppdb_insert(ppdb db, jid id, xmlnode x)
52 char *res;
53 ppdb cur, curu;
54 pool p;
56 if(id == NULL || id->server == NULL || x == NULL)
57 return db;
59 /* new ppdb list dummy holder */
60 if(db == NULL)
62 p = pool_heap(1024);
63 db = _ppdb_new(p,id);
66 cur = _ppdb_get(db,id);
68 /* just update it */
69 if(cur != NULL)
71 xmlnode_free(cur->x);
72 cur->x = xmlnode_dup(x);
73 cur->pri = jutil_priority(x);
74 return db;
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);
81 cur->next = db->next;
82 db->next = cur;
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 */
88 res = id->resource;
89 jid_set(id,NULL,JID_RESOURCE);
90 curu = _ppdb_get(db,id);
92 /* no user entry, make one */
93 if(curu == NULL)
95 curu = _ppdb_new(db->p,id);
96 curu->next = db->next;
97 db->next = curu;
100 /* restore the id */
101 jid_set(id,res,JID_RESOURCE);
103 /* insert this resource into the user list */
104 cur->user = curu->user;
105 curu->user = cur;
108 return db;
111 xmlnode ppdb_primary(ppdb db, jid id)
113 ppdb cur, top;
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;
124 top = cur;
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;
130 return NULL;
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;
137 ppdb cur;
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 */
145 if(last != NULL)
147 last = NULL;
148 return NULL;
151 last = _ppdb_get(db,id);
152 if(last != NULL)
153 return last->x;
154 else
155 return NULL;
158 /* handle looping for user@host */
160 /* we're already in the loop */
161 if(last != NULL)
163 /* this is the last entry in the list */
164 if(last->user == NULL)
166 last = NULL;
167 return NULL;
170 last = last->user;
171 return last->x;
174 /* start a new loop */
175 cur = _ppdb_get(db,id);
177 if(cur == NULL) return NULL;
179 last = cur->user;
180 if(last != NULL)
181 return last->x;
182 else
183 return NULL;
187 void ppdb_free(ppdb db)
189 ppdb cur;
191 if(db == NULL) return;
193 for(cur = db; cur != NULL; cur = cur->next)
194 xmlnode_free(cur->x);
196 pool_free(db->p);