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 { |
207
|
30 const gchar *name; |
|
31 const gchar *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 |
192
|
77 if (!jidname) return NULL; |
72
|
78 |
89
|
79 if (!roster_type) |
|
80 roster_type = ROSTER_TYPE_USER|ROSTER_TYPE_AGENT|ROSTER_TYPE_GROUP; |
|
81 |
72
|
82 sample.type = roster_type; |
|
83 if (type == jidsearch) { |
|
84 sample.jid = jidname; |
|
85 comp = (GCompareFunc)&roster_compare_jid_type; |
|
86 } else if (type == namesearch) { |
|
87 sample.name = jidname; |
|
88 comp = (GCompareFunc)&roster_compare_name; |
|
89 } else |
|
90 return NULL; // should not happen |
|
91 |
|
92 while (sl_roster_elt) { |
|
93 roster *roster_elt = (roster*)sl_roster_elt->data; |
|
94 if (roster_type & ROSTER_TYPE_GROUP) { |
|
95 if ((type == namesearch) && !strcasecmp(jidname, roster_elt->name)) |
|
96 return sl_roster_elt; |
|
97 } else { |
|
98 res = g_slist_find_custom(roster_elt->list, &sample, comp); |
|
99 if (res) |
|
100 return res; |
|
101 } |
|
102 sl_roster_elt = g_slist_next(sl_roster_elt); |
|
103 } |
|
104 return NULL; |
|
105 } |
|
106 |
|
107 // Returns pointer to new group, or existing group with that name |
78
|
108 GSList *roster_add_group(const char *name) |
72
|
109 { |
|
110 roster *roster_grp; |
|
111 // #1 Check name doesn't already exist |
|
112 if (!roster_find(name, namesearch, ROSTER_TYPE_GROUP)) { |
|
113 // #2 Create the group node |
|
114 roster_grp = g_new0(roster, 1); |
|
115 roster_grp->name = g_strdup(name); |
|
116 roster_grp->type = ROSTER_TYPE_GROUP; |
|
117 // #3 Insert (sorted) |
|
118 groups = g_slist_insert_sorted(groups, roster_grp, |
|
119 (GCompareFunc)&roster_compare_name); |
|
120 } |
|
121 return roster_find(name, namesearch, ROSTER_TYPE_GROUP); |
|
122 } |
|
123 |
|
124 // Returns a pointer to the new user, or existing user with that name |
78
|
125 GSList *roster_add_user(const char *jid, const char *name, const char *group, |
|
126 guint type) |
72
|
127 { |
|
128 roster *roster_usr; |
|
129 roster *my_group; |
|
130 GSList *slist; |
|
131 |
|
132 if ((type != ROSTER_TYPE_USER) && (type != ROSTER_TYPE_AGENT)) { |
|
133 // XXX Error message? |
|
134 return NULL; |
|
135 } |
|
136 |
78
|
137 // Let's be arbitrary: default group has an empty name (""). |
|
138 if (!group) group = ""; |
|
139 |
72
|
140 // #1 Check this user doesn't already exist |
192
|
141 slist = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT); |
|
142 if (slist) return slist; |
72
|
143 // #2 add group if necessary |
|
144 slist = roster_add_group(group); |
|
145 if (!slist) return NULL; |
|
146 my_group = (roster*)slist->data; |
|
147 // #3 Create user node |
|
148 roster_usr = g_new0(roster, 1); |
|
149 roster_usr->jid = g_strdup(jid); |
78
|
150 if (name) { |
|
151 roster_usr->name = g_strdup(name); |
|
152 } else { |
|
153 gchar *p, *str = g_strdup(jid); |
80
|
154 p = strstr(str, "/"); |
78
|
155 if (p) *p = '\0'; |
|
156 roster_usr->name = g_strdup(str); |
|
157 g_free(str); |
|
158 } |
72
|
159 roster_usr->type = type; //ROSTER_TYPE_USER; |
|
160 roster_usr->list = slist; // (my_group SList element) |
|
161 // #4 Insert node (sorted) |
|
162 my_group->list = g_slist_insert_sorted(my_group->list, roster_usr, |
|
163 (GCompareFunc)&roster_compare_name); |
|
164 return roster_find(jid, jidsearch, type); |
|
165 } |
|
166 |
|
167 // Removes user (jid) from roster, frees allocated memory |
78
|
168 void roster_del_user(const char *jid) |
72
|
169 { |
|
170 GSList *sl_user, *sl_group; |
|
171 GSList **sl_group_listptr; |
|
172 roster *roster_usr; |
|
173 |
89
|
174 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT); |
|
175 if (sl_user == NULL) |
72
|
176 return; |
|
177 // Let's free memory (jid, name) |
|
178 roster_usr = (roster*)sl_user->data; |
|
179 if (roster_usr->jid) |
78
|
180 g_free((gchar*)roster_usr->jid); |
72
|
181 if (roster_usr->name) |
78
|
182 g_free((gchar*)roster_usr->name); |
72
|
183 |
|
184 // That's a little complex, we need to dereference twice |
|
185 sl_group = ((roster*)sl_user->data)->list; |
|
186 sl_group_listptr = &((roster*)(sl_group->data))->list; |
|
187 *sl_group_listptr = g_slist_delete_link(*sl_group_listptr, sl_user); |
78
|
188 |
|
189 // We need to rebuild the list |
|
190 if (current_buddy) |
|
191 buddylist_build(); |
|
192 // TODO What we should do, too, is to check if the deleted node is |
|
193 // current_buddy, in which case we could move current_buddy to the |
|
194 // previous (or next) node. |
72
|
195 } |
|
196 |
164
|
197 // Free all roster data. Call buddylist_build() to free the buddylist. |
|
198 void roster_free(void) |
|
199 { |
|
200 GSList *sl_grp = groups; |
|
201 |
|
202 // Walk through groups |
|
203 while (sl_grp) { |
|
204 roster *roster_grp = (roster*)sl_grp->data; |
|
205 GSList *sl_usr = roster_grp->list; |
|
206 // Walk through this group users |
|
207 while (sl_usr) { |
|
208 roster *roster_usr = (roster*)sl_usr->data; |
|
209 // Free name and jid |
|
210 if (roster_usr->jid) |
|
211 g_free((gchar*)roster_usr->jid); |
|
212 if (roster_usr->name) |
|
213 g_free((gchar*)roster_usr->name); |
|
214 sl_usr = g_slist_next(sl_usr); |
|
215 } |
|
216 // Free group's users list |
|
217 if (roster_grp->list) |
|
218 g_slist_free(roster_grp->list); |
|
219 // Free group's name and jid |
|
220 if (roster_grp->jid) |
|
221 g_free((gchar*)roster_grp->jid); |
|
222 if (roster_grp->name) |
|
223 g_free((gchar*)roster_grp->name); |
|
224 sl_grp = g_slist_next(sl_grp); |
|
225 } |
|
226 // Free groups list |
|
227 if (groups) { |
|
228 g_slist_free(groups); |
|
229 groups = NULL; |
|
230 // Update (i.e. free) buddylist |
|
231 if (buddylist) |
|
232 buddylist_build(); |
|
233 } |
|
234 } |
|
235 |
78
|
236 void roster_setstatus(const char *jid, enum imstatus bstat) |
72
|
237 { |
|
238 GSList *sl_user; |
|
239 roster *roster_usr; |
|
240 |
89
|
241 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT); |
92
|
242 // If we can't find it, we add it |
89
|
243 if (sl_user == NULL) |
92
|
244 sl_user = roster_add_user(jid, NULL, NULL, ROSTER_TYPE_USER); |
72
|
245 |
|
246 roster_usr = (roster*)sl_user->data; |
|
247 roster_usr->status = bstat; |
|
248 } |
|
249 |
78
|
250 // roster_setflags() |
|
251 // Set one or several flags to value (TRUE/FALSE) |
80
|
252 void roster_setflags(const char *jid, guint flags, guint value) |
78
|
253 { |
|
254 GSList *sl_user; |
|
255 roster *roster_usr; |
|
256 |
89
|
257 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT); |
|
258 if (sl_user == NULL) |
78
|
259 return; |
|
260 |
|
261 roster_usr = (roster*)sl_user->data; |
|
262 if (value) |
|
263 roster_usr->flags |= flags; |
|
264 else |
|
265 roster_usr->flags &= ~flags; |
|
266 } |
148
|
267 |
|
268 // roster_msg_setflag() |
|
269 // Set the ROSTER_FLAG_MSG to the given value for the given jid. |
|
270 // It will update the buddy's group message flag. |
|
271 void roster_msg_setflag(const char *jid, guint value) |
|
272 { |
|
273 GSList *sl_user; |
|
274 roster *roster_usr, *roster_grp; |
|
275 |
|
276 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT); |
|
277 if (sl_user == NULL) |
|
278 return; |
|
279 |
|
280 roster_usr = (roster*)sl_user->data; |
|
281 roster_grp = (roster*)roster_usr->list->data; |
|
282 if (value) { |
|
283 // Message flag is TRUE. This is easy, we just have to set both flags |
|
284 // to TRUE... |
|
285 roster_usr->flags |= ROSTER_FLAG_MSG; |
|
286 roster_grp->flags |= ROSTER_FLAG_MSG; // group |
|
287 } else { |
|
288 // Message flag is FALSE. |
|
289 guint msg = FALSE; |
|
290 roster_usr->flags &= ~ROSTER_FLAG_MSG; |
|
291 // For the group value we need to watch all buddies in this group; |
|
292 // if one is flagged, then the group will be flagged. |
|
293 // I will re-use sl_user and roster_usr here, as they aren't used |
|
294 // anymore. |
|
295 sl_user = roster_grp->list; |
|
296 while (sl_user) { |
|
297 roster_usr = (roster*)sl_user->data; |
|
298 if (roster_usr->flags & ROSTER_FLAG_MSG) { |
|
299 msg = TRUE; |
|
300 break; |
|
301 } |
|
302 sl_user = g_slist_next(sl_user); |
|
303 } |
|
304 if (!msg) |
|
305 roster_grp->flags &= ~ROSTER_FLAG_MSG; |
|
306 else |
|
307 roster_grp->flags |= ROSTER_FLAG_MSG; |
|
308 // Actually the "else" part is useless, because the group |
|
309 // ROSTER_FLAG_MSG should already be set... |
|
310 } |
|
311 } |
|
312 |
89
|
313 void roster_settype(const char *jid, guint type) |
|
314 { |
|
315 GSList *sl_user; |
|
316 roster *roster_usr; |
|
317 |
|
318 if ((sl_user = roster_find(jid, jidsearch, 0)) == NULL) |
|
319 return; |
|
320 |
|
321 roster_usr = (roster*)sl_user->data; |
|
322 roster_usr->type = type; |
|
323 } |
|
324 |
90
|
325 enum imstatus roster_getstatus(const char *jid) |
|
326 { |
|
327 GSList *sl_user; |
|
328 roster *roster_usr; |
|
329 |
|
330 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT); |
|
331 if (sl_user == NULL) |
|
332 return offline; // Not in the roster, anyway... |
|
333 |
|
334 roster_usr = (roster*)sl_user->data; |
|
335 return roster_usr->status; |
|
336 } |
|
337 |
89
|
338 guint roster_gettype(const char *jid) |
|
339 { |
|
340 GSList *sl_user; |
|
341 roster *roster_usr; |
|
342 |
|
343 if ((sl_user = roster_find(jid, jidsearch, 0)) == NULL) |
|
344 return 0; |
|
345 |
|
346 roster_usr = (roster*)sl_user->data; |
|
347 return roster_usr->type; |
|
348 } |
|
349 |
141
|
350 inline guint roster_exists(const char *jidname, enum findwhat type, |
|
351 guint roster_type) |
|
352 { |
|
353 if (roster_find(jidname, type, roster_type)) |
|
354 return TRUE; |
|
355 return FALSE; |
|
356 } |
|
357 |
72
|
358 |
|
359 /* ### BuddyList functions ### */ |
|
360 |
120
|
361 // buddylist_set_hide_offline_buddies(hide) |
72
|
362 // "hide" values: 1=hide 0=show_all -1=invert |
120
|
363 void buddylist_set_hide_offline_buddies(int hide) |
72
|
364 { |
|
365 if (hide < 0) // NEG (invert) |
|
366 hide_offline_buddies = !hide_offline_buddies; |
|
367 else if (hide == 0) // FALSE (don't hide) |
|
368 hide_offline_buddies = 0; |
|
369 else // TRUE (hide) |
|
370 hide_offline_buddies = 1; |
|
371 } |
|
372 |
120
|
373 inline int buddylist_get_hide_offline_buddies(void) |
|
374 { |
|
375 return hide_offline_buddies; |
|
376 } |
|
377 |
72
|
378 // buddylist_build() |
|
379 // Creates the buddylist from the roster entries. |
|
380 void buddylist_build(void) |
|
381 { |
|
382 GSList *sl_roster_elt = groups; |
|
383 roster *roster_elt; |
81
|
384 roster *roster_current_buddy = NULL; |
128
|
385 int shrunk_group; |
72
|
386 |
81
|
387 // We need to remember which buddy is selected. |
|
388 if (current_buddy) |
|
389 roster_current_buddy = BUDDATA(current_buddy); |
|
390 current_buddy = NULL; |
|
391 |
72
|
392 // Destroy old buddylist |
|
393 if (buddylist) { |
|
394 g_list_free(buddylist); |
|
395 buddylist = NULL; |
|
396 } |
|
397 |
|
398 // Create the new list |
|
399 while (sl_roster_elt) { |
|
400 GSList *sl_roster_usrelt; |
|
401 roster *roster_usrelt; |
147
|
402 guint pending_group = FALSE; |
72
|
403 roster_elt = (roster*) sl_roster_elt->data; |
|
404 |
|
405 // Add the group now unless hide_offline_buddies is set, |
|
406 // in which case we'll add it only if an online buddy belongs to it. |
147
|
407 // We take care to keep the current_buddy in the list, too. |
|
408 if (!hide_offline_buddies || roster_elt == roster_current_buddy) |
72
|
409 buddylist = g_list_append(buddylist, roster_elt); |
|
410 else |
147
|
411 pending_group = TRUE; |
72
|
412 |
128
|
413 shrunk_group = roster_elt->flags & ROSTER_FLAG_HIDE; |
|
414 |
72
|
415 sl_roster_usrelt = roster_elt->list; |
|
416 while (sl_roster_usrelt) { |
|
417 roster_usrelt = (roster*) sl_roster_usrelt->data; |
|
418 |
|
419 // Buddy will be added if either: |
|
420 // - hide_offline_buddies is FALSE |
|
421 // - buddy is not offline |
|
422 // - buddy has a lock (for example the buddy window is currently open) |
|
423 // - buddy has a pending (non-read) message |
128
|
424 // - group isn't hidden (shrunk) |
147
|
425 // - this is the current_buddy |
|
426 if (!hide_offline_buddies || roster_usrelt == roster_current_buddy || |
72
|
427 (buddy_getstatus((gpointer)roster_usrelt) != offline) || |
|
428 (buddy_getflags((gpointer)roster_usrelt) & |
|
429 (ROSTER_FLAG_LOCK | ROSTER_FLAG_MSG))) { |
|
430 // This user should be added. Maybe the group hasn't been added yet? |
147
|
431 if (pending_group && |
|
432 (hide_offline_buddies || roster_usrelt == roster_current_buddy)) { |
72
|
433 // It hasn't been done yet |
|
434 buddylist = g_list_append(buddylist, roster_elt); |
|
435 pending_group = FALSE; |
|
436 } |
|
437 // Add user |
128
|
438 // XXX Should we add the user if there is a message and |
|
439 // the group is shrunk? If so, we'd need to check LOCK flag too, |
|
440 // perhaps... |
|
441 if (!shrunk_group) |
|
442 buddylist = g_list_append(buddylist, roster_usrelt); |
72
|
443 } |
|
444 |
|
445 sl_roster_usrelt = g_slist_next(sl_roster_usrelt); |
|
446 } |
|
447 sl_roster_elt = g_slist_next(sl_roster_elt); |
|
448 } |
78
|
449 |
81
|
450 // Check if we can find our saved current_buddy... |
|
451 if (roster_current_buddy) |
|
452 current_buddy = g_list_find(buddylist, roster_current_buddy); |
78
|
453 // current_buddy initialization |
|
454 if (!current_buddy || (g_list_position(buddylist, current_buddy) == -1)) |
81
|
455 current_buddy = g_list_first(buddylist); |
128
|
456 // XXX Maybe we should set update_roster to TRUE there? |
72
|
457 } |
|
458 |
|
459 // buddy_hide_group(roster, hide) |
|
460 // "hide" values: 1=hide 0=show_all -1=invert |
|
461 void buddy_hide_group(gpointer rosterdata, int hide) |
|
462 { |
207
|
463 roster *roster_usr = rosterdata; |
72
|
464 if (hide > 0) // TRUE (hide) |
207
|
465 roster_usr->flags |= ROSTER_FLAG_HIDE; |
72
|
466 else if (hide < 0) // NEG (invert) |
207
|
467 roster_usr->flags ^= ROSTER_FLAG_HIDE; |
72
|
468 else // FALSE (don't hide) |
207
|
469 roster_usr->flags &= ~ROSTER_FLAG_HIDE; |
72
|
470 } |
|
471 |
|
472 const char *buddy_getjid(gpointer rosterdata) |
|
473 { |
207
|
474 roster *roster_usr = rosterdata; |
|
475 return roster_usr->jid; |
|
476 } |
|
477 |
|
478 void buddy_setname(gpointer rosterdata, char *newname) |
|
479 { |
|
480 roster *roster_usr = rosterdata; |
209
|
481 GSList **sl_group; |
207
|
482 |
|
483 // TODO For groups, we need to check for unicity |
|
484 // However, renaming a group boils down to moving all its buddies to |
|
485 // another group, so calling this function is not really necessary... |
|
486 if (roster_usr->type & ROSTER_TYPE_GROUP) return; |
|
487 |
|
488 if (roster_usr->name) { |
|
489 g_free((gchar*)roster_usr->name); |
|
490 roster_usr->name = NULL; |
|
491 } |
|
492 if (newname) |
|
493 roster_usr->name = g_strdup(newname); |
209
|
494 |
|
495 // We need to resort the group list |
|
496 sl_group = &((roster*)((GSList*)roster_usr->list)->data)->list; |
|
497 *sl_group = g_slist_sort(*sl_group, (GCompareFunc)&roster_compare_name); |
|
498 |
|
499 buddylist_build(); |
72
|
500 } |
|
501 |
|
502 const char *buddy_getname(gpointer rosterdata) |
|
503 { |
207
|
504 roster *roster_usr = rosterdata; |
|
505 return roster_usr->name; |
|
506 } |
|
507 |
|
508 // buddy_getgroupname() |
|
509 // Returns a pointer on buddy's group name. |
|
510 const char *buddy_getgroupname(gpointer rosterdata) |
|
511 { |
|
512 roster *roster_usr = rosterdata; |
|
513 |
|
514 if (roster_usr->type & ROSTER_TYPE_GROUP) |
|
515 return roster_usr->name; |
|
516 |
|
517 // This is a user |
|
518 return ((roster*)((GSList*)roster_usr->list)->data)->name; |
72
|
519 } |
|
520 |
166
|
521 // buddy_getgroup() |
|
522 // Returns a pointer on buddy's group. |
|
523 gpointer buddy_getgroup(gpointer rosterdata) |
|
524 { |
207
|
525 roster *roster_usr = rosterdata; |
166
|
526 |
207
|
527 if (roster_usr->type & ROSTER_TYPE_GROUP) |
166
|
528 return rosterdata; |
|
529 |
|
530 // This is a user |
207
|
531 return (gpointer)((GSList*)roster_usr->list)->data; |
166
|
532 } |
|
533 |
72
|
534 guint buddy_gettype(gpointer rosterdata) |
|
535 { |
207
|
536 roster *roster_usr = rosterdata; |
|
537 return roster_usr->type; |
72
|
538 } |
|
539 |
|
540 enum imstatus buddy_getstatus(gpointer rosterdata) |
|
541 { |
207
|
542 roster *roster_usr = rosterdata; |
|
543 return roster_usr->status; |
72
|
544 } |
|
545 |
119
|
546 // buddy_setflags() |
|
547 // Set one or several flags to value (TRUE/FALSE) |
|
548 void buddy_setflags(gpointer rosterdata, guint flags, guint value) |
|
549 { |
207
|
550 roster *roster_usr = rosterdata; |
119
|
551 if (value) |
207
|
552 roster_usr->flags |= flags; |
119
|
553 else |
207
|
554 roster_usr->flags &= ~flags; |
119
|
555 } |
|
556 |
72
|
557 guint buddy_getflags(gpointer rosterdata) |
|
558 { |
207
|
559 roster *roster_usr = rosterdata; |
|
560 return roster_usr->flags; |
72
|
561 } |
|
562 |