Mercurial > ~mikael > mcabber > hg
annotate mcabber/src/roster.c @ 318:45076d02eeef
The /status command can specify a status message
We can now use: "/status dnd On the phone..."
Related change: Display the buddy status message when a buddy connects/changes
his/her status.
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Fri, 15 Jul 2005 18:45:41 +0100 |
parents | efcbd3c1ad15 |
children | a9013124ede6 |
rev | line source |
---|---|
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 | |
265 | 22 #define _GNU_SOURCE /* for strcasestr() */ |
72 | 23 #include <string.h> |
24 | |
25 #include "roster.h" | |
26 | |
27 | |
28 /* This is a private structure type for the roster */ | |
29 | |
30 typedef struct { | |
207 | 31 const gchar *name; |
32 const gchar *jid; | |
221 | 33 const gchar *status_msg; |
72 | 34 guint type; |
35 enum imstatus status; | |
36 guint flags; | |
37 // list: user -> points to his group; group -> points to its users list | |
38 GSList *list; | |
39 } roster; | |
40 | |
41 | |
42 /* ### Variables ### */ | |
43 | |
44 static int hide_offline_buddies; | |
45 static GSList *groups; | |
236 | 46 static GSList *unread_list; |
72 | 47 GList *buddylist; |
78 | 48 GList *current_buddy; |
72 | 49 |
50 | |
51 /* ### Roster functions ### */ | |
52 | |
53 // Comparison function used to search in the roster (compares jids and types) | |
54 gint roster_compare_jid_type(roster *a, roster *b) { | |
89 | 55 if (! (a->type & b->type)) |
72 | 56 return -1; // arbitrary (but should be != , of course) |
57 return strcasecmp(a->jid, b->jid); | |
58 } | |
59 | |
60 // Comparison function used to sort the roster (by name) | |
61 gint roster_compare_name(roster *a, roster *b) { | |
62 return strcasecmp(a->name, b->name); | |
63 } | |
64 | |
65 // Finds a roster element (user, group, agent...), by jid or name | |
89 | 66 // If roster_type is 0, returns match of any type. |
72 | 67 // Returns the roster GSList element, or NULL if jid/name not found |
78 | 68 GSList *roster_find(const char *jidname, enum findwhat type, guint roster_type) |
72 | 69 { |
70 GSList *sl_roster_elt = groups; | |
71 GSList *res; | |
72 roster sample; | |
73 GCompareFunc comp; | |
74 | |
192 | 75 if (!jidname) return NULL; |
72 | 76 |
89 | 77 if (!roster_type) |
78 roster_type = ROSTER_TYPE_USER|ROSTER_TYPE_AGENT|ROSTER_TYPE_GROUP; | |
79 | |
72 | 80 sample.type = roster_type; |
81 if (type == jidsearch) { | |
82 sample.jid = jidname; | |
83 comp = (GCompareFunc)&roster_compare_jid_type; | |
84 } else if (type == namesearch) { | |
85 sample.name = jidname; | |
86 comp = (GCompareFunc)&roster_compare_name; | |
87 } else | |
88 return NULL; // should not happen | |
89 | |
90 while (sl_roster_elt) { | |
91 roster *roster_elt = (roster*)sl_roster_elt->data; | |
92 if (roster_type & ROSTER_TYPE_GROUP) { | |
93 if ((type == namesearch) && !strcasecmp(jidname, roster_elt->name)) | |
94 return sl_roster_elt; | |
95 } else { | |
96 res = g_slist_find_custom(roster_elt->list, &sample, comp); | |
97 if (res) | |
98 return res; | |
99 } | |
100 sl_roster_elt = g_slist_next(sl_roster_elt); | |
101 } | |
102 return NULL; | |
103 } | |
104 | |
105 // Returns pointer to new group, or existing group with that name | |
78 | 106 GSList *roster_add_group(const char *name) |
72 | 107 { |
108 roster *roster_grp; | |
109 // #1 Check name doesn't already exist | |
110 if (!roster_find(name, namesearch, ROSTER_TYPE_GROUP)) { | |
111 // #2 Create the group node | |
112 roster_grp = g_new0(roster, 1); | |
113 roster_grp->name = g_strdup(name); | |
114 roster_grp->type = ROSTER_TYPE_GROUP; | |
115 // #3 Insert (sorted) | |
116 groups = g_slist_insert_sorted(groups, roster_grp, | |
117 (GCompareFunc)&roster_compare_name); | |
118 } | |
119 return roster_find(name, namesearch, ROSTER_TYPE_GROUP); | |
120 } | |
121 | |
122 // Returns a pointer to the new user, or existing user with that name | |
78 | 123 GSList *roster_add_user(const char *jid, const char *name, const char *group, |
124 guint type) | |
72 | 125 { |
126 roster *roster_usr; | |
127 roster *my_group; | |
128 GSList *slist; | |
129 | |
130 if ((type != ROSTER_TYPE_USER) && (type != ROSTER_TYPE_AGENT)) { | |
131 // XXX Error message? | |
132 return NULL; | |
133 } | |
134 | |
78 | 135 // Let's be arbitrary: default group has an empty name (""). |
136 if (!group) group = ""; | |
137 | |
72 | 138 // #1 Check this user doesn't already exist |
192 | 139 slist = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT); |
140 if (slist) return slist; | |
72 | 141 // #2 add group if necessary |
142 slist = roster_add_group(group); | |
143 if (!slist) return NULL; | |
144 my_group = (roster*)slist->data; | |
145 // #3 Create user node | |
146 roster_usr = g_new0(roster, 1); | |
147 roster_usr->jid = g_strdup(jid); | |
78 | 148 if (name) { |
149 roster_usr->name = g_strdup(name); | |
150 } else { | |
151 gchar *p, *str = g_strdup(jid); | |
80 | 152 p = strstr(str, "/"); |
78 | 153 if (p) *p = '\0'; |
154 roster_usr->name = g_strdup(str); | |
155 g_free(str); | |
156 } | |
72 | 157 roster_usr->type = type; //ROSTER_TYPE_USER; |
158 roster_usr->list = slist; // (my_group SList element) | |
159 // #4 Insert node (sorted) | |
160 my_group->list = g_slist_insert_sorted(my_group->list, roster_usr, | |
161 (GCompareFunc)&roster_compare_name); | |
162 return roster_find(jid, jidsearch, type); | |
163 } | |
164 | |
165 // Removes user (jid) from roster, frees allocated memory | |
78 | 166 void roster_del_user(const char *jid) |
72 | 167 { |
168 GSList *sl_user, *sl_group; | |
169 GSList **sl_group_listptr; | |
170 roster *roster_usr; | |
246 | 171 GSList *node; |
72 | 172 |
89 | 173 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT); |
174 if (sl_user == NULL) | |
72 | 175 return; |
176 roster_usr = (roster*)sl_user->data; | |
246 | 177 |
178 // Remove (if present) from unread messages list | |
179 node = g_slist_find(unread_list, roster_usr); | |
180 if (node) unread_list = g_slist_delete_link(unread_list, node); | |
181 | |
182 // Let's free memory (jid, name, status message) | |
221 | 183 if (roster_usr->jid) g_free((gchar*)roster_usr->jid); |
184 if (roster_usr->name) g_free((gchar*)roster_usr->name); | |
185 if (roster_usr->status_msg) g_free((gchar*)roster_usr->status_msg); | |
210 | 186 g_free(roster_usr); |
72 | 187 |
188 // That's a little complex, we need to dereference twice | |
189 sl_group = ((roster*)sl_user->data)->list; | |
190 sl_group_listptr = &((roster*)(sl_group->data))->list; | |
191 *sl_group_listptr = g_slist_delete_link(*sl_group_listptr, sl_user); | |
78 | 192 |
193 // We need to rebuild the list | |
194 if (current_buddy) | |
195 buddylist_build(); | |
210 | 196 // TODO What we could do, too, is to check if the deleted node is |
78 | 197 // current_buddy, in which case we could move current_buddy to the |
198 // previous (or next) node. | |
72 | 199 } |
200 | |
210 | 201 // Free all roster data and call buddylist_build() to free the buddylist. |
164 | 202 void roster_free(void) |
203 { | |
204 GSList *sl_grp = groups; | |
205 | |
301
efcbd3c1ad15
Free unread_list when the roster is freed
Mikael Berthe <mikael@lilotux.net>
parents:
265
diff
changeset
|
206 // Free unread_list |
efcbd3c1ad15
Free unread_list when the roster is freed
Mikael Berthe <mikael@lilotux.net>
parents:
265
diff
changeset
|
207 if (unread_list) { |
efcbd3c1ad15
Free unread_list when the roster is freed
Mikael Berthe <mikael@lilotux.net>
parents:
265
diff
changeset
|
208 g_slist_free(unread_list); |
efcbd3c1ad15
Free unread_list when the roster is freed
Mikael Berthe <mikael@lilotux.net>
parents:
265
diff
changeset
|
209 unread_list = NULL; |
efcbd3c1ad15
Free unread_list when the roster is freed
Mikael Berthe <mikael@lilotux.net>
parents:
265
diff
changeset
|
210 } |
efcbd3c1ad15
Free unread_list when the roster is freed
Mikael Berthe <mikael@lilotux.net>
parents:
265
diff
changeset
|
211 |
164 | 212 // Walk through groups |
213 while (sl_grp) { | |
214 roster *roster_grp = (roster*)sl_grp->data; | |
215 GSList *sl_usr = roster_grp->list; | |
216 // Walk through this group users | |
217 while (sl_usr) { | |
218 roster *roster_usr = (roster*)sl_usr->data; | |
219 // Free name and jid | |
221 | 220 if (roster_usr->jid) g_free((gchar*)roster_usr->jid); |
221 if (roster_usr->name) g_free((gchar*)roster_usr->name); | |
222 if (roster_usr->status_msg) g_free((gchar*)roster_usr->status_msg); | |
214 | 223 g_free(roster_usr); |
164 | 224 sl_usr = g_slist_next(sl_usr); |
225 } | |
226 // Free group's users list | |
227 if (roster_grp->list) | |
228 g_slist_free(roster_grp->list); | |
229 // Free group's name and jid | |
221 | 230 if (roster_grp->jid) g_free((gchar*)roster_grp->jid); |
231 if (roster_grp->name) g_free((gchar*)roster_grp->name); | |
214 | 232 g_free(roster_grp); |
164 | 233 sl_grp = g_slist_next(sl_grp); |
234 } | |
235 // Free groups list | |
236 if (groups) { | |
237 g_slist_free(groups); | |
238 groups = NULL; | |
239 // Update (i.e. free) buddylist | |
240 if (buddylist) | |
241 buddylist_build(); | |
242 } | |
243 } | |
244 | |
221 | 245 void roster_setstatus(const char *jid, enum imstatus bstat, |
246 const char *status_msg) | |
72 | 247 { |
248 GSList *sl_user; | |
249 roster *roster_usr; | |
250 | |
89 | 251 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT); |
92 | 252 // If we can't find it, we add it |
89 | 253 if (sl_user == NULL) |
92 | 254 sl_user = roster_add_user(jid, NULL, NULL, ROSTER_TYPE_USER); |
72 | 255 |
256 roster_usr = (roster*)sl_user->data; | |
257 roster_usr->status = bstat; | |
221 | 258 if (roster_usr->status_msg) { |
259 g_free((gchar*)roster_usr->status_msg); | |
260 roster_usr->status_msg = NULL; | |
261 } | |
262 if (status_msg) | |
263 roster_usr->status_msg = g_strdup(status_msg); | |
72 | 264 } |
265 | |
78 | 266 // roster_setflags() |
267 // Set one or several flags to value (TRUE/FALSE) | |
80 | 268 void roster_setflags(const char *jid, guint flags, guint value) |
78 | 269 { |
270 GSList *sl_user; | |
271 roster *roster_usr; | |
272 | |
89 | 273 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT); |
274 if (sl_user == NULL) | |
78 | 275 return; |
276 | |
277 roster_usr = (roster*)sl_user->data; | |
278 if (value) | |
279 roster_usr->flags |= flags; | |
280 else | |
281 roster_usr->flags &= ~flags; | |
282 } | |
148 | 283 |
284 // roster_msg_setflag() | |
285 // Set the ROSTER_FLAG_MSG to the given value for the given jid. | |
286 // It will update the buddy's group message flag. | |
236 | 287 // Update the unread messages list too. |
148 | 288 void roster_msg_setflag(const char *jid, guint value) |
289 { | |
290 GSList *sl_user; | |
291 roster *roster_usr, *roster_grp; | |
292 | |
293 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT); | |
294 if (sl_user == NULL) | |
295 return; | |
296 | |
297 roster_usr = (roster*)sl_user->data; | |
298 roster_grp = (roster*)roster_usr->list->data; | |
299 if (value) { | |
300 // Message flag is TRUE. This is easy, we just have to set both flags | |
301 // to TRUE... | |
302 roster_usr->flags |= ROSTER_FLAG_MSG; | |
303 roster_grp->flags |= ROSTER_FLAG_MSG; // group | |
236 | 304 // Append the roster_usr to unread_list, but avoid duplicates |
305 if (!g_slist_find(unread_list, roster_usr)) | |
306 unread_list = g_slist_append(unread_list, roster_usr); | |
148 | 307 } else { |
308 // Message flag is FALSE. | |
309 guint msg = FALSE; | |
310 roster_usr->flags &= ~ROSTER_FLAG_MSG; | |
236 | 311 if (unread_list) { |
312 GSList *node = g_slist_find(unread_list, roster_usr); | |
313 if (node) unread_list = g_slist_delete_link(unread_list, node); | |
314 } | |
148 | 315 // For the group value we need to watch all buddies in this group; |
316 // if one is flagged, then the group will be flagged. | |
317 // I will re-use sl_user and roster_usr here, as they aren't used | |
318 // anymore. | |
319 sl_user = roster_grp->list; | |
320 while (sl_user) { | |
321 roster_usr = (roster*)sl_user->data; | |
322 if (roster_usr->flags & ROSTER_FLAG_MSG) { | |
323 msg = TRUE; | |
324 break; | |
325 } | |
326 sl_user = g_slist_next(sl_user); | |
327 } | |
328 if (!msg) | |
329 roster_grp->flags &= ~ROSTER_FLAG_MSG; | |
330 else | |
331 roster_grp->flags |= ROSTER_FLAG_MSG; | |
332 // Actually the "else" part is useless, because the group | |
333 // ROSTER_FLAG_MSG should already be set... | |
334 } | |
335 } | |
336 | |
89 | 337 void roster_settype(const char *jid, guint type) |
338 { | |
339 GSList *sl_user; | |
340 roster *roster_usr; | |
341 | |
342 if ((sl_user = roster_find(jid, jidsearch, 0)) == NULL) | |
343 return; | |
344 | |
345 roster_usr = (roster*)sl_user->data; | |
346 roster_usr->type = type; | |
347 } | |
348 | |
90 | 349 enum imstatus roster_getstatus(const char *jid) |
350 { | |
351 GSList *sl_user; | |
352 roster *roster_usr; | |
353 | |
354 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT); | |
355 if (sl_user == NULL) | |
356 return offline; // Not in the roster, anyway... | |
357 | |
358 roster_usr = (roster*)sl_user->data; | |
359 return roster_usr->status; | |
360 } | |
361 | |
318
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
362 const char *roster_getstatusmsg(const char *jid) |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
363 { |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
364 GSList *sl_user; |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
365 roster *roster_usr; |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
366 |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
367 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT); |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
368 if (sl_user == NULL) |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
369 return offline; // Not in the roster, anyway... |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
370 |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
371 roster_usr = (roster*)sl_user->data; |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
372 return roster_usr->status_msg; |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
373 } |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
374 |
89 | 375 guint roster_gettype(const char *jid) |
376 { | |
377 GSList *sl_user; | |
378 roster *roster_usr; | |
379 | |
380 if ((sl_user = roster_find(jid, jidsearch, 0)) == NULL) | |
381 return 0; | |
382 | |
383 roster_usr = (roster*)sl_user->data; | |
384 return roster_usr->type; | |
385 } | |
386 | |
141 | 387 inline guint roster_exists(const char *jidname, enum findwhat type, |
388 guint roster_type) | |
389 { | |
390 if (roster_find(jidname, type, roster_type)) | |
391 return TRUE; | |
392 return FALSE; | |
393 } | |
394 | |
72 | 395 |
396 /* ### BuddyList functions ### */ | |
397 | |
120 | 398 // buddylist_set_hide_offline_buddies(hide) |
72 | 399 // "hide" values: 1=hide 0=show_all -1=invert |
120 | 400 void buddylist_set_hide_offline_buddies(int hide) |
72 | 401 { |
402 if (hide < 0) // NEG (invert) | |
403 hide_offline_buddies = !hide_offline_buddies; | |
404 else if (hide == 0) // FALSE (don't hide) | |
405 hide_offline_buddies = 0; | |
406 else // TRUE (hide) | |
407 hide_offline_buddies = 1; | |
408 } | |
409 | |
120 | 410 inline int buddylist_get_hide_offline_buddies(void) |
411 { | |
412 return hide_offline_buddies; | |
413 } | |
414 | |
72 | 415 // buddylist_build() |
416 // Creates the buddylist from the roster entries. | |
417 void buddylist_build(void) | |
418 { | |
419 GSList *sl_roster_elt = groups; | |
420 roster *roster_elt; | |
81 | 421 roster *roster_current_buddy = NULL; |
128 | 422 int shrunk_group; |
72 | 423 |
81 | 424 // We need to remember which buddy is selected. |
425 if (current_buddy) | |
426 roster_current_buddy = BUDDATA(current_buddy); | |
427 current_buddy = NULL; | |
428 | |
72 | 429 // Destroy old buddylist |
430 if (buddylist) { | |
431 g_list_free(buddylist); | |
432 buddylist = NULL; | |
433 } | |
434 | |
435 // Create the new list | |
436 while (sl_roster_elt) { | |
437 GSList *sl_roster_usrelt; | |
438 roster *roster_usrelt; | |
147 | 439 guint pending_group = FALSE; |
72 | 440 roster_elt = (roster*) sl_roster_elt->data; |
441 | |
442 // Add the group now unless hide_offline_buddies is set, | |
443 // in which case we'll add it only if an online buddy belongs to it. | |
147 | 444 // We take care to keep the current_buddy in the list, too. |
445 if (!hide_offline_buddies || roster_elt == roster_current_buddy) | |
72 | 446 buddylist = g_list_append(buddylist, roster_elt); |
447 else | |
147 | 448 pending_group = TRUE; |
72 | 449 |
128 | 450 shrunk_group = roster_elt->flags & ROSTER_FLAG_HIDE; |
451 | |
72 | 452 sl_roster_usrelt = roster_elt->list; |
453 while (sl_roster_usrelt) { | |
454 roster_usrelt = (roster*) sl_roster_usrelt->data; | |
455 | |
456 // Buddy will be added if either: | |
457 // - hide_offline_buddies is FALSE | |
458 // - buddy is not offline | |
459 // - buddy has a lock (for example the buddy window is currently open) | |
460 // - buddy has a pending (non-read) message | |
128 | 461 // - group isn't hidden (shrunk) |
147 | 462 // - this is the current_buddy |
463 if (!hide_offline_buddies || roster_usrelt == roster_current_buddy || | |
72 | 464 (buddy_getstatus((gpointer)roster_usrelt) != offline) || |
465 (buddy_getflags((gpointer)roster_usrelt) & | |
466 (ROSTER_FLAG_LOCK | ROSTER_FLAG_MSG))) { | |
467 // This user should be added. Maybe the group hasn't been added yet? | |
147 | 468 if (pending_group && |
469 (hide_offline_buddies || roster_usrelt == roster_current_buddy)) { | |
72 | 470 // It hasn't been done yet |
471 buddylist = g_list_append(buddylist, roster_elt); | |
472 pending_group = FALSE; | |
473 } | |
474 // Add user | |
128 | 475 // XXX Should we add the user if there is a message and |
476 // the group is shrunk? If so, we'd need to check LOCK flag too, | |
477 // perhaps... | |
478 if (!shrunk_group) | |
479 buddylist = g_list_append(buddylist, roster_usrelt); | |
72 | 480 } |
481 | |
482 sl_roster_usrelt = g_slist_next(sl_roster_usrelt); | |
483 } | |
484 sl_roster_elt = g_slist_next(sl_roster_elt); | |
485 } | |
78 | 486 |
81 | 487 // Check if we can find our saved current_buddy... |
488 if (roster_current_buddy) | |
489 current_buddy = g_list_find(buddylist, roster_current_buddy); | |
78 | 490 // current_buddy initialization |
491 if (!current_buddy || (g_list_position(buddylist, current_buddy) == -1)) | |
81 | 492 current_buddy = g_list_first(buddylist); |
128 | 493 // XXX Maybe we should set update_roster to TRUE there? |
72 | 494 } |
495 | |
496 // buddy_hide_group(roster, hide) | |
497 // "hide" values: 1=hide 0=show_all -1=invert | |
498 void buddy_hide_group(gpointer rosterdata, int hide) | |
499 { | |
207 | 500 roster *roster_usr = rosterdata; |
72 | 501 if (hide > 0) // TRUE (hide) |
207 | 502 roster_usr->flags |= ROSTER_FLAG_HIDE; |
72 | 503 else if (hide < 0) // NEG (invert) |
207 | 504 roster_usr->flags ^= ROSTER_FLAG_HIDE; |
72 | 505 else // FALSE (don't hide) |
207 | 506 roster_usr->flags &= ~ROSTER_FLAG_HIDE; |
72 | 507 } |
508 | |
509 const char *buddy_getjid(gpointer rosterdata) | |
510 { | |
207 | 511 roster *roster_usr = rosterdata; |
512 return roster_usr->jid; | |
513 } | |
514 | |
210 | 515 // buddy_setgroup() |
516 // Change the group of current buddy | |
517 // | |
518 // Warning! This function changes current_buddy! | |
519 // Warning! Old buddy is deleted, so you can't acces to its jid/name after | |
520 // calling this function. | |
521 void buddy_setgroup(gpointer rosterdata, char *newgroupname) | |
522 { | |
523 roster *roster_usr = rosterdata; | |
524 GSList **sl_group; | |
525 GSList *sl_clone; | |
526 roster *roster_clone; | |
527 | |
528 // A group has no group :) | |
529 if (roster_usr->type & ROSTER_TYPE_GROUP) return; | |
530 | |
531 // Remove the buddy from current group | |
532 sl_group = &((roster*)((GSList*)roster_usr->list)->data)->list; | |
533 *sl_group = g_slist_remove(*sl_group, rosterdata); | |
534 | |
535 // Add the buddy to its new group; actually we "clone" this buddy... | |
536 sl_clone = roster_add_user(roster_usr->jid, roster_usr->name, | |
537 newgroupname, roster_usr->type); | |
538 roster_clone = (roster*)sl_clone->data; | |
539 roster_clone->status = roster_usr->status; | |
540 roster_clone->flags = roster_usr->flags; | |
541 | |
542 // Free old buddy | |
221 | 543 if (roster_usr->jid) g_free((gchar*)roster_usr->jid); |
544 if (roster_usr->name) g_free((gchar*)roster_usr->name); | |
545 if (roster_usr->status_msg) g_free((gchar*)roster_usr->status_msg); | |
210 | 546 g_free(roster_usr); |
547 | |
212 | 548 // If new new group is folded, the curren_buddy will be lost, and the |
549 // chat window won't be correctly refreshed. So we make sure it isn't... | |
550 ((roster*)((GSList*)roster_clone->list)->data)->flags &= ~ROSTER_FLAG_HIDE; | |
551 | |
552 // Little trick to have current_body pointing to the cloned buddy | |
553 buddylist = g_list_append(buddylist, roster_clone); | |
554 current_buddy = g_list_find(buddylist, roster_clone); | |
555 | |
210 | 556 buddylist_build(); |
557 } | |
558 | |
207 | 559 void buddy_setname(gpointer rosterdata, char *newname) |
560 { | |
561 roster *roster_usr = rosterdata; | |
209 | 562 GSList **sl_group; |
207 | 563 |
564 // TODO For groups, we need to check for unicity | |
565 // However, renaming a group boils down to moving all its buddies to | |
566 // another group, so calling this function is not really necessary... | |
567 if (roster_usr->type & ROSTER_TYPE_GROUP) return; | |
568 | |
569 if (roster_usr->name) { | |
570 g_free((gchar*)roster_usr->name); | |
571 roster_usr->name = NULL; | |
572 } | |
573 if (newname) | |
574 roster_usr->name = g_strdup(newname); | |
209 | 575 |
576 // We need to resort the group list | |
577 sl_group = &((roster*)((GSList*)roster_usr->list)->data)->list; | |
578 *sl_group = g_slist_sort(*sl_group, (GCompareFunc)&roster_compare_name); | |
579 | |
580 buddylist_build(); | |
72 | 581 } |
582 | |
583 const char *buddy_getname(gpointer rosterdata) | |
584 { | |
207 | 585 roster *roster_usr = rosterdata; |
586 return roster_usr->name; | |
587 } | |
588 | |
589 // buddy_getgroupname() | |
590 // Returns a pointer on buddy's group name. | |
591 const char *buddy_getgroupname(gpointer rosterdata) | |
592 { | |
593 roster *roster_usr = rosterdata; | |
594 | |
595 if (roster_usr->type & ROSTER_TYPE_GROUP) | |
596 return roster_usr->name; | |
597 | |
598 // This is a user | |
599 return ((roster*)((GSList*)roster_usr->list)->data)->name; | |
72 | 600 } |
601 | |
166 | 602 // buddy_getgroup() |
603 // Returns a pointer on buddy's group. | |
604 gpointer buddy_getgroup(gpointer rosterdata) | |
605 { | |
207 | 606 roster *roster_usr = rosterdata; |
166 | 607 |
207 | 608 if (roster_usr->type & ROSTER_TYPE_GROUP) |
166 | 609 return rosterdata; |
610 | |
611 // This is a user | |
207 | 612 return (gpointer)((GSList*)roster_usr->list)->data; |
166 | 613 } |
614 | |
72 | 615 guint buddy_gettype(gpointer rosterdata) |
616 { | |
207 | 617 roster *roster_usr = rosterdata; |
618 return roster_usr->type; | |
72 | 619 } |
620 | |
621 enum imstatus buddy_getstatus(gpointer rosterdata) | |
622 { | |
207 | 623 roster *roster_usr = rosterdata; |
624 return roster_usr->status; | |
72 | 625 } |
626 | |
221 | 627 const char *buddy_getstatusmsg(gpointer rosterdata) |
628 { | |
629 roster *roster_usr = rosterdata; | |
630 return roster_usr->status_msg; | |
631 } | |
632 | |
119 | 633 // buddy_setflags() |
634 // Set one or several flags to value (TRUE/FALSE) | |
635 void buddy_setflags(gpointer rosterdata, guint flags, guint value) | |
636 { | |
207 | 637 roster *roster_usr = rosterdata; |
119 | 638 if (value) |
207 | 639 roster_usr->flags |= flags; |
119 | 640 else |
207 | 641 roster_usr->flags &= ~flags; |
119 | 642 } |
643 | |
72 | 644 guint buddy_getflags(gpointer rosterdata) |
645 { | |
207 | 646 roster *roster_usr = rosterdata; |
647 return roster_usr->flags; | |
72 | 648 } |
649 | |
265 | 650 // buddy_search(string) |
651 // Look for a buddy whose name or jid contains string. | |
652 // Search begins at current_buddy; if no match is found in the the buddylist, | |
653 // return NULL; | |
654 GList *buddy_search(char *string) | |
655 { | |
656 GList *buddy = current_buddy; | |
657 roster *roster_usr; | |
658 if (!buddylist || !current_buddy) return NULL; | |
659 for (;;) { | |
660 buddy = g_list_next(buddy); | |
661 if (!buddy) | |
662 buddy = buddylist; | |
663 | |
664 roster_usr = (roster*)buddy->data; | |
665 if (roster_usr->jid && strcasestr(roster_usr->jid, string)) | |
666 return buddy; | |
667 if (roster_usr->name && strcasestr(roster_usr->name, string)) | |
668 return buddy; | |
669 | |
670 if (buddy == current_buddy) | |
671 return NULL; // Back to the beginning, and no match found | |
672 } | |
673 } | |
674 | |
225 | 675 // compl_list(type) |
676 // Returns a list of jid's or groups. (For commands completion) | |
677 // type: ROSTER_TYPE_USER (jid's) or ROSTER_TYPE_GROUP (group names) | |
678 // The list should be freed by the caller after use. | |
679 GSList *compl_list(guint type) | |
680 { | |
681 GSList *list = NULL; | |
682 GList *buddy = buddylist; | |
683 | |
684 for ( ; buddy ; buddy = g_list_next(buddy)) { | |
685 guint btype = buddy_gettype(BUDDATA(buddy)); | |
686 | |
687 if (type == ROSTER_TYPE_GROUP) { // (group names) | |
688 if (btype == ROSTER_TYPE_GROUP) { | |
689 const char *bname = buddy_getname(BUDDATA(buddy)); | |
690 if ((bname) && (*bname)) | |
691 list = g_slist_append(list, g_strdup(bname)); | |
692 } | |
693 } else { // ROSTER_TYPE_USER (jid) | |
694 const char *bjid = buddy_getjid(BUDDATA(buddy)); | |
695 if (bjid) | |
696 list = g_slist_append(list, g_strdup(bjid)); | |
697 } | |
698 } | |
699 | |
700 return list; | |
701 } | |
702 | |
236 | 703 // unread_msg(rosterdata) |
704 // Return the next buddy with an unread message. If the parameter is NULL, | |
705 // return the first buddy with an unread message. | |
706 gpointer unread_msg(gpointer rosterdata) | |
707 { | |
708 GSList *unread, *next_unread; | |
709 | |
710 if (!unread_list) return NULL; | |
711 // First unread message | |
712 if (!rosterdata) return unread_list->data; | |
713 | |
714 unread = g_slist_find(unread_list, rosterdata); | |
715 if (!unread) return unread_list->data; | |
716 | |
717 next_unread = g_slist_next(unread); | |
718 if (next_unread) return next_unread->data; | |
719 | |
720 return unread_list->data; | |
721 } | |
722 |