72
|
1 /*
|
|
2 * roster.c -- Local roster implementation
|
|
3 *
|
|
4 * Copyright (C) 2005 Mikael Berthe <bmikael@lists.lilotux.net>
|
|
5 *
|
|
6 * This program is free software; you can redistribute it and/or modify
|
|
7 * it under the terms of the GNU General Public License as published by
|
|
8 * the Free Software Foundation; either version 2 of the License, or (at
|
|
9 * your option) any later version.
|
|
10 *
|
|
11 * This program is distributed in the hope that it will be useful, but
|
|
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
14 * General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU General Public License
|
|
17 * along with this program; if not, write to the Free Software
|
|
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
19 * USA
|
|
20 */
|
|
21
|
|
22 #include <string.h>
|
|
23
|
|
24 #include "roster.h"
|
|
25
|
|
26
|
|
27 /* This is a private structure type for the roster */
|
|
28
|
|
29 typedef struct {
|
78
|
30 const char *name;
|
|
31 const char *jid;
|
72
|
32 guint type;
|
|
33 enum imstatus status;
|
|
34 guint flags;
|
|
35 // list: user -> points to his group; group -> points to its users list
|
|
36 GSList *list;
|
|
37 } roster;
|
|
38
|
|
39
|
|
40 /* ### Variables ### */
|
|
41
|
|
42 static int hide_offline_buddies;
|
|
43 static GSList *groups;
|
|
44 GList *buddylist;
|
78
|
45 GList *current_buddy;
|
72
|
46
|
|
47 #ifdef MCABBER_TESTUNIT
|
|
48 // Export groups for testing routines
|
|
49 GSList **pgroups = &groups;
|
|
50 #endif
|
|
51
|
|
52
|
|
53 /* ### Roster functions ### */
|
|
54
|
|
55 // Comparison function used to search in the roster (compares jids and types)
|
|
56 gint roster_compare_jid_type(roster *a, roster *b) {
|
89
|
57 if (! (a->type & b->type))
|
72
|
58 return -1; // arbitrary (but should be != , of course)
|
|
59 return strcasecmp(a->jid, b->jid);
|
|
60 }
|
|
61
|
|
62 // Comparison function used to sort the roster (by name)
|
|
63 gint roster_compare_name(roster *a, roster *b) {
|
|
64 return strcasecmp(a->name, b->name);
|
|
65 }
|
|
66
|
|
67 // Finds a roster element (user, group, agent...), by jid or name
|
89
|
68 // If roster_type is 0, returns match of any type.
|
72
|
69 // Returns the roster GSList element, or NULL if jid/name not found
|
78
|
70 GSList *roster_find(const char *jidname, enum findwhat type, guint roster_type)
|
72
|
71 {
|
|
72 GSList *sl_roster_elt = groups;
|
|
73 GSList *res;
|
|
74 roster sample;
|
|
75 GCompareFunc comp;
|
|
76
|
|
77 if (!jidname)
|
|
78 return NULL; // should not happen
|
|
79
|
89
|
80 if (!roster_type)
|
|
81 roster_type = ROSTER_TYPE_USER|ROSTER_TYPE_AGENT|ROSTER_TYPE_GROUP;
|
|
82
|
72
|
83 sample.type = roster_type;
|
|
84 if (type == jidsearch) {
|
|
85 sample.jid = jidname;
|
|
86 comp = (GCompareFunc)&roster_compare_jid_type;
|
|
87 } else if (type == namesearch) {
|
|
88 sample.name = jidname;
|
|
89 comp = (GCompareFunc)&roster_compare_name;
|
|
90 } else
|
|
91 return NULL; // should not happen
|
|
92
|
|
93 while (sl_roster_elt) {
|
|
94 roster *roster_elt = (roster*)sl_roster_elt->data;
|
|
95 if (roster_type & ROSTER_TYPE_GROUP) {
|
|
96 if ((type == namesearch) && !strcasecmp(jidname, roster_elt->name))
|
|
97 return sl_roster_elt;
|
|
98 } else {
|
|
99 res = g_slist_find_custom(roster_elt->list, &sample, comp);
|
|
100 if (res)
|
|
101 return res;
|
|
102 }
|
|
103 sl_roster_elt = g_slist_next(sl_roster_elt);
|
|
104 }
|
|
105 return NULL;
|
|
106 }
|
|
107
|
|
108 // Returns pointer to new group, or existing group with that name
|
78
|
109 GSList *roster_add_group(const char *name)
|
72
|
110 {
|
|
111 roster *roster_grp;
|
|
112 // #1 Check name doesn't already exist
|
|
113 if (!roster_find(name, namesearch, ROSTER_TYPE_GROUP)) {
|
|
114 // #2 Create the group node
|
|
115 roster_grp = g_new0(roster, 1);
|
|
116 roster_grp->name = g_strdup(name);
|
|
117 roster_grp->type = ROSTER_TYPE_GROUP;
|
|
118 // #3 Insert (sorted)
|
|
119 groups = g_slist_insert_sorted(groups, roster_grp,
|
|
120 (GCompareFunc)&roster_compare_name);
|
|
121 }
|
|
122 return roster_find(name, namesearch, ROSTER_TYPE_GROUP);
|
|
123 }
|
|
124
|
|
125 // Returns a pointer to the new user, or existing user with that name
|
78
|
126 GSList *roster_add_user(const char *jid, const char *name, const char *group,
|
|
127 guint type)
|
72
|
128 {
|
|
129 roster *roster_usr;
|
|
130 roster *my_group;
|
|
131 GSList *slist;
|
|
132
|
|
133 if ((type != ROSTER_TYPE_USER) && (type != ROSTER_TYPE_AGENT)) {
|
|
134 // XXX Error message?
|
|
135 return NULL;
|
|
136 }
|
|
137
|
78
|
138 // Let's be arbitrary: default group has an empty name ("").
|
|
139 if (!group) group = "";
|
|
140
|
72
|
141 // #1 Check this user doesn't already exist
|
|
142 if ((slist = roster_find(jid, jidsearch, type)) != NULL)
|
|
143 return slist;
|
|
144 // #2 add group if necessary
|
|
145 slist = roster_add_group(group);
|
|
146 if (!slist) return NULL;
|
|
147 my_group = (roster*)slist->data;
|
|
148 // #3 Create user node
|
|
149 roster_usr = g_new0(roster, 1);
|
|
150 roster_usr->jid = g_strdup(jid);
|
78
|
151 if (name) {
|
|
152 roster_usr->name = g_strdup(name);
|
|
153 } else {
|
|
154 gchar *p, *str = g_strdup(jid);
|
80
|
155 p = strstr(str, "/");
|
78
|
156 if (p) *p = '\0';
|
|
157 roster_usr->name = g_strdup(str);
|
|
158 g_free(str);
|
|
159 }
|
72
|
160 roster_usr->type = type; //ROSTER_TYPE_USER;
|
|
161 roster_usr->list = slist; // (my_group SList element)
|
|
162 // #4 Insert node (sorted)
|
|
163 my_group->list = g_slist_insert_sorted(my_group->list, roster_usr,
|
|
164 (GCompareFunc)&roster_compare_name);
|
|
165 return roster_find(jid, jidsearch, type);
|
|
166 }
|
|
167
|
|
168 // Removes user (jid) from roster, frees allocated memory
|
78
|
169 void roster_del_user(const char *jid)
|
72
|
170 {
|
|
171 GSList *sl_user, *sl_group;
|
|
172 GSList **sl_group_listptr;
|
|
173 roster *roster_usr;
|
|
174
|
89
|
175 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT);
|
|
176 if (sl_user == NULL)
|
72
|
177 return;
|
|
178 // Let's free memory (jid, name)
|
|
179 roster_usr = (roster*)sl_user->data;
|
|
180 if (roster_usr->jid)
|
78
|
181 g_free((gchar*)roster_usr->jid);
|
72
|
182 if (roster_usr->name)
|
78
|
183 g_free((gchar*)roster_usr->name);
|
72
|
184
|
|
185 // That's a little complex, we need to dereference twice
|
|
186 sl_group = ((roster*)sl_user->data)->list;
|
|
187 sl_group_listptr = &((roster*)(sl_group->data))->list;
|
|
188 *sl_group_listptr = g_slist_delete_link(*sl_group_listptr, sl_user);
|
78
|
189
|
|
190 // We need to rebuild the list
|
|
191 if (current_buddy)
|
|
192 buddylist_build();
|
|
193 // TODO What we should do, too, is to check if the deleted node is
|
|
194 // current_buddy, in which case we could move current_buddy to the
|
|
195 // previous (or next) node.
|
72
|
196 }
|
|
197
|
78
|
198 void roster_setstatus(const char *jid, enum imstatus bstat)
|
72
|
199 {
|
|
200 GSList *sl_user;
|
|
201 roster *roster_usr;
|
|
202
|
89
|
203 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT);
|
|
204 if (sl_user == NULL)
|
72
|
205 return;
|
|
206
|
|
207 roster_usr = (roster*)sl_user->data;
|
|
208 roster_usr->status = bstat;
|
|
209 }
|
|
210
|
78
|
211 // roster_setflags()
|
|
212 // Set one or several flags to value (TRUE/FALSE)
|
80
|
213 void roster_setflags(const char *jid, guint flags, guint value)
|
78
|
214 {
|
|
215 GSList *sl_user;
|
|
216 roster *roster_usr;
|
|
217
|
89
|
218 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT);
|
|
219 if (sl_user == NULL)
|
78
|
220 return;
|
|
221
|
|
222 roster_usr = (roster*)sl_user->data;
|
|
223 if (value)
|
|
224 roster_usr->flags |= flags;
|
|
225 else
|
|
226 roster_usr->flags &= ~flags;
|
|
227 }
|
|
228
|
89
|
229 void roster_settype(const char *jid, guint type)
|
|
230 {
|
|
231 GSList *sl_user;
|
|
232 roster *roster_usr;
|
|
233
|
|
234 if ((sl_user = roster_find(jid, jidsearch, 0)) == NULL)
|
|
235 return;
|
|
236
|
|
237 roster_usr = (roster*)sl_user->data;
|
|
238 roster_usr->type = type;
|
|
239 }
|
|
240
|
90
|
241 enum imstatus roster_getstatus(const char *jid)
|
|
242 {
|
|
243 GSList *sl_user;
|
|
244 roster *roster_usr;
|
|
245
|
|
246 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT);
|
|
247 if (sl_user == NULL)
|
|
248 return offline; // Not in the roster, anyway...
|
|
249
|
|
250 roster_usr = (roster*)sl_user->data;
|
|
251 return roster_usr->status;
|
|
252 }
|
|
253
|
89
|
254 guint roster_gettype(const char *jid)
|
|
255 {
|
|
256 GSList *sl_user;
|
|
257 roster *roster_usr;
|
|
258
|
|
259 if ((sl_user = roster_find(jid, jidsearch, 0)) == NULL)
|
|
260 return 0;
|
|
261
|
|
262 roster_usr = (roster*)sl_user->data;
|
|
263 return roster_usr->type;
|
|
264 }
|
|
265
|
72
|
266 // char *roster_getgroup(...) / Or *GSList? Which use??
|
|
267 // ... setgroup(char*) ??
|
78
|
268 // guchar roster_getflags(...)
|
72
|
269 // guchar roster_getname(...) / setname ??
|
|
270 // roster_del_group?
|
|
271
|
|
272
|
|
273 /* ### BuddyList functions ### */
|
|
274
|
|
275 // buddylist_hide_offline_buddies(hide)
|
|
276 // "hide" values: 1=hide 0=show_all -1=invert
|
|
277 void buddylist_hide_offline_buddies(int hide)
|
|
278 {
|
|
279 if (hide < 0) // NEG (invert)
|
|
280 hide_offline_buddies = !hide_offline_buddies;
|
|
281 else if (hide == 0) // FALSE (don't hide)
|
|
282 hide_offline_buddies = 0;
|
|
283 else // TRUE (hide)
|
|
284 hide_offline_buddies = 1;
|
|
285 }
|
|
286
|
|
287 // buddylist_build()
|
|
288 // Creates the buddylist from the roster entries.
|
|
289 void buddylist_build(void)
|
|
290 {
|
|
291 GSList *sl_roster_elt = groups;
|
|
292 roster *roster_elt;
|
81
|
293 roster *roster_current_buddy = NULL;
|
72
|
294 int pending_group;
|
|
295
|
81
|
296 // We need to remember which buddy is selected.
|
|
297 if (current_buddy)
|
|
298 roster_current_buddy = BUDDATA(current_buddy);
|
|
299 current_buddy = NULL;
|
|
300
|
72
|
301 // Destroy old buddylist
|
|
302 if (buddylist) {
|
|
303 g_list_free(buddylist);
|
|
304 buddylist = NULL;
|
|
305 }
|
|
306
|
|
307 // Create the new list
|
|
308 while (sl_roster_elt) {
|
|
309 GSList *sl_roster_usrelt;
|
|
310 roster *roster_usrelt;
|
|
311 roster_elt = (roster*) sl_roster_elt->data;
|
|
312
|
|
313 // Add the group now unless hide_offline_buddies is set,
|
|
314 // in which case we'll add it only if an online buddy belongs to it.
|
|
315 if (!hide_offline_buddies)
|
|
316 buddylist = g_list_append(buddylist, roster_elt);
|
|
317 else
|
|
318 pending_group = TRUE;
|
|
319
|
|
320 sl_roster_usrelt = roster_elt->list;
|
|
321 while (sl_roster_usrelt) {
|
|
322 roster_usrelt = (roster*) sl_roster_usrelt->data;
|
|
323
|
|
324 // Buddy will be added if either:
|
|
325 // - hide_offline_buddies is FALSE
|
|
326 // - buddy is not offline
|
|
327 // - buddy has a lock (for example the buddy window is currently open)
|
|
328 // - buddy has a pending (non-read) message
|
|
329 if (!hide_offline_buddies ||
|
|
330 (buddy_getstatus((gpointer)roster_usrelt) != offline) ||
|
|
331 (buddy_getflags((gpointer)roster_usrelt) &
|
|
332 (ROSTER_FLAG_LOCK | ROSTER_FLAG_MSG))) {
|
|
333 // This user should be added. Maybe the group hasn't been added yet?
|
|
334 if (hide_offline_buddies && pending_group) {
|
|
335 // It hasn't been done yet
|
|
336 buddylist = g_list_append(buddylist, roster_elt);
|
|
337 pending_group = FALSE;
|
|
338 }
|
|
339 // Add user
|
|
340 buddylist = g_list_append(buddylist, roster_usrelt);
|
|
341 }
|
|
342
|
|
343 sl_roster_usrelt = g_slist_next(sl_roster_usrelt);
|
|
344 }
|
|
345 sl_roster_elt = g_slist_next(sl_roster_elt);
|
|
346 }
|
78
|
347
|
81
|
348 // Check if we can find our saved current_buddy...
|
|
349 if (roster_current_buddy)
|
|
350 current_buddy = g_list_find(buddylist, roster_current_buddy);
|
78
|
351 // current_buddy initialization
|
|
352 if (!current_buddy || (g_list_position(buddylist, current_buddy) == -1))
|
81
|
353 current_buddy = g_list_first(buddylist);
|
72
|
354 }
|
|
355
|
|
356 // buddy_hide_group(roster, hide)
|
|
357 // "hide" values: 1=hide 0=show_all -1=invert
|
|
358 void buddy_hide_group(gpointer rosterdata, int hide)
|
|
359 {
|
|
360 roster *roster = rosterdata;
|
|
361 if (hide > 0) // TRUE (hide)
|
|
362 roster->flags |= ROSTER_FLAG_HIDE;
|
|
363 else if (hide < 0) // NEG (invert)
|
|
364 roster->flags ^= ROSTER_FLAG_HIDE;
|
|
365 else // FALSE (don't hide)
|
|
366 roster->flags &= ~ROSTER_FLAG_HIDE;
|
|
367 }
|
|
368
|
|
369 const char *buddy_getjid(gpointer rosterdata)
|
|
370 {
|
|
371 roster *roster = rosterdata;
|
|
372 return roster->jid;
|
|
373 }
|
|
374
|
|
375 const char *buddy_getname(gpointer rosterdata)
|
|
376 {
|
|
377 roster *roster = rosterdata;
|
|
378 return roster->name;
|
|
379 }
|
|
380
|
|
381 guint buddy_gettype(gpointer rosterdata)
|
|
382 {
|
|
383 roster *roster = rosterdata;
|
|
384 return roster->type;
|
|
385 }
|
|
386
|
|
387 enum imstatus buddy_getstatus(gpointer rosterdata)
|
|
388 {
|
|
389 roster *roster = rosterdata;
|
|
390 return roster->status;
|
|
391 }
|
|
392
|
|
393 guint buddy_getflags(gpointer rosterdata)
|
|
394 {
|
|
395 roster *roster = rosterdata;
|
|
396 return roster->flags;
|
|
397 }
|
|
398
|