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);
|
92
|
204 // If we can't find it, we add it
|
89
|
205 if (sl_user == NULL)
|
92
|
206 sl_user = roster_add_user(jid, NULL, NULL, ROSTER_TYPE_USER);
|
72
|
207
|
|
208 roster_usr = (roster*)sl_user->data;
|
|
209 roster_usr->status = bstat;
|
|
210 }
|
|
211
|
78
|
212 // roster_setflags()
|
|
213 // Set one or several flags to value (TRUE/FALSE)
|
80
|
214 void roster_setflags(const char *jid, guint flags, guint value)
|
78
|
215 {
|
|
216 GSList *sl_user;
|
|
217 roster *roster_usr;
|
|
218
|
89
|
219 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT);
|
|
220 if (sl_user == NULL)
|
78
|
221 return;
|
|
222
|
|
223 roster_usr = (roster*)sl_user->data;
|
|
224 if (value)
|
|
225 roster_usr->flags |= flags;
|
|
226 else
|
|
227 roster_usr->flags &= ~flags;
|
|
228 }
|
|
229
|
89
|
230 void roster_settype(const char *jid, guint type)
|
|
231 {
|
|
232 GSList *sl_user;
|
|
233 roster *roster_usr;
|
|
234
|
|
235 if ((sl_user = roster_find(jid, jidsearch, 0)) == NULL)
|
|
236 return;
|
|
237
|
|
238 roster_usr = (roster*)sl_user->data;
|
|
239 roster_usr->type = type;
|
|
240 }
|
|
241
|
90
|
242 enum imstatus roster_getstatus(const char *jid)
|
|
243 {
|
|
244 GSList *sl_user;
|
|
245 roster *roster_usr;
|
|
246
|
|
247 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT);
|
|
248 if (sl_user == NULL)
|
|
249 return offline; // Not in the roster, anyway...
|
|
250
|
|
251 roster_usr = (roster*)sl_user->data;
|
|
252 return roster_usr->status;
|
|
253 }
|
|
254
|
89
|
255 guint roster_gettype(const char *jid)
|
|
256 {
|
|
257 GSList *sl_user;
|
|
258 roster *roster_usr;
|
|
259
|
|
260 if ((sl_user = roster_find(jid, jidsearch, 0)) == NULL)
|
|
261 return 0;
|
|
262
|
|
263 roster_usr = (roster*)sl_user->data;
|
|
264 return roster_usr->type;
|
|
265 }
|
|
266
|
72
|
267 // char *roster_getgroup(...) / Or *GSList? Which use??
|
|
268 // ... setgroup(char*) ??
|
78
|
269 // guchar roster_getflags(...)
|
72
|
270 // guchar roster_getname(...) / setname ??
|
|
271 // roster_del_group?
|
|
272
|
|
273
|
|
274 /* ### BuddyList functions ### */
|
|
275
|
|
276 // buddylist_hide_offline_buddies(hide)
|
|
277 // "hide" values: 1=hide 0=show_all -1=invert
|
|
278 void buddylist_hide_offline_buddies(int hide)
|
|
279 {
|
|
280 if (hide < 0) // NEG (invert)
|
|
281 hide_offline_buddies = !hide_offline_buddies;
|
|
282 else if (hide == 0) // FALSE (don't hide)
|
|
283 hide_offline_buddies = 0;
|
|
284 else // TRUE (hide)
|
|
285 hide_offline_buddies = 1;
|
|
286 }
|
|
287
|
|
288 // buddylist_build()
|
|
289 // Creates the buddylist from the roster entries.
|
|
290 void buddylist_build(void)
|
|
291 {
|
|
292 GSList *sl_roster_elt = groups;
|
|
293 roster *roster_elt;
|
81
|
294 roster *roster_current_buddy = NULL;
|
72
|
295 int pending_group;
|
|
296
|
81
|
297 // We need to remember which buddy is selected.
|
|
298 if (current_buddy)
|
|
299 roster_current_buddy = BUDDATA(current_buddy);
|
|
300 current_buddy = NULL;
|
|
301
|
72
|
302 // Destroy old buddylist
|
|
303 if (buddylist) {
|
|
304 g_list_free(buddylist);
|
|
305 buddylist = NULL;
|
|
306 }
|
|
307
|
|
308 // Create the new list
|
|
309 while (sl_roster_elt) {
|
|
310 GSList *sl_roster_usrelt;
|
|
311 roster *roster_usrelt;
|
|
312 roster_elt = (roster*) sl_roster_elt->data;
|
|
313
|
|
314 // Add the group now unless hide_offline_buddies is set,
|
|
315 // in which case we'll add it only if an online buddy belongs to it.
|
|
316 if (!hide_offline_buddies)
|
|
317 buddylist = g_list_append(buddylist, roster_elt);
|
|
318 else
|
|
319 pending_group = TRUE;
|
|
320
|
|
321 sl_roster_usrelt = roster_elt->list;
|
|
322 while (sl_roster_usrelt) {
|
|
323 roster_usrelt = (roster*) sl_roster_usrelt->data;
|
|
324
|
|
325 // Buddy will be added if either:
|
|
326 // - hide_offline_buddies is FALSE
|
|
327 // - buddy is not offline
|
|
328 // - buddy has a lock (for example the buddy window is currently open)
|
|
329 // - buddy has a pending (non-read) message
|
|
330 if (!hide_offline_buddies ||
|
|
331 (buddy_getstatus((gpointer)roster_usrelt) != offline) ||
|
|
332 (buddy_getflags((gpointer)roster_usrelt) &
|
|
333 (ROSTER_FLAG_LOCK | ROSTER_FLAG_MSG))) {
|
|
334 // This user should be added. Maybe the group hasn't been added yet?
|
|
335 if (hide_offline_buddies && pending_group) {
|
|
336 // It hasn't been done yet
|
|
337 buddylist = g_list_append(buddylist, roster_elt);
|
|
338 pending_group = FALSE;
|
|
339 }
|
|
340 // Add user
|
|
341 buddylist = g_list_append(buddylist, roster_usrelt);
|
|
342 }
|
|
343
|
|
344 sl_roster_usrelt = g_slist_next(sl_roster_usrelt);
|
|
345 }
|
|
346 sl_roster_elt = g_slist_next(sl_roster_elt);
|
|
347 }
|
78
|
348
|
81
|
349 // Check if we can find our saved current_buddy...
|
|
350 if (roster_current_buddy)
|
|
351 current_buddy = g_list_find(buddylist, roster_current_buddy);
|
78
|
352 // current_buddy initialization
|
|
353 if (!current_buddy || (g_list_position(buddylist, current_buddy) == -1))
|
81
|
354 current_buddy = g_list_first(buddylist);
|
72
|
355 }
|
|
356
|
|
357 // buddy_hide_group(roster, hide)
|
|
358 // "hide" values: 1=hide 0=show_all -1=invert
|
|
359 void buddy_hide_group(gpointer rosterdata, int hide)
|
|
360 {
|
|
361 roster *roster = rosterdata;
|
|
362 if (hide > 0) // TRUE (hide)
|
|
363 roster->flags |= ROSTER_FLAG_HIDE;
|
|
364 else if (hide < 0) // NEG (invert)
|
|
365 roster->flags ^= ROSTER_FLAG_HIDE;
|
|
366 else // FALSE (don't hide)
|
|
367 roster->flags &= ~ROSTER_FLAG_HIDE;
|
|
368 }
|
|
369
|
|
370 const char *buddy_getjid(gpointer rosterdata)
|
|
371 {
|
|
372 roster *roster = rosterdata;
|
|
373 return roster->jid;
|
|
374 }
|
|
375
|
|
376 const char *buddy_getname(gpointer rosterdata)
|
|
377 {
|
|
378 roster *roster = rosterdata;
|
|
379 return roster->name;
|
|
380 }
|
|
381
|
|
382 guint buddy_gettype(gpointer rosterdata)
|
|
383 {
|
|
384 roster *roster = rosterdata;
|
|
385 return roster->type;
|
|
386 }
|
|
387
|
|
388 enum imstatus buddy_getstatus(gpointer rosterdata)
|
|
389 {
|
|
390 roster *roster = rosterdata;
|
|
391 return roster->status;
|
|
392 }
|
|
393
|
|
394 guint buddy_getflags(gpointer rosterdata)
|
|
395 {
|
|
396 roster *roster = rosterdata;
|
|
397 return roster->flags;
|
|
398 }
|
|
399
|