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