Mercurial > hg
annotate mcabber/src/roster.c @ 446:9f4e9e9aaf08
Minor changes & cleanup
See diff for details.
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Sun, 25 Sep 2005 00:44:11 +0200 |
parents | 63562fd409a1 |
children | 03bb57383cea |
rev | line source |
---|---|
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 | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
28 /* Resource structure */ |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
29 |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
30 typedef struct { |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
31 gchar *name; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
32 gchar prio; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
33 enum imstatus status; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
34 gchar *status_msg; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
35 enum imrole role; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
36 gchar *realjid; /* for chatrooms, if buddy's real jid is known */ |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
37 } res; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
38 |
72 | 39 /* This is a private structure type for the roster */ |
40 | |
41 typedef struct { | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
42 gchar *name; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
43 gchar *jid; |
72 | 44 guint type; |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
45 enum subscr subscription; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
46 GSList *resource; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
47 res *cur_res; |
72 | 48 guint flags; |
49 // list: user -> points to his group; group -> points to its users list | |
50 GSList *list; | |
51 } roster; | |
52 | |
53 | |
54 /* ### Variables ### */ | |
55 | |
56 static int hide_offline_buddies; | |
57 static GSList *groups; | |
236 | 58 static GSList *unread_list; |
72 | 59 GList *buddylist; |
78 | 60 GList *current_buddy; |
330 | 61 GList *alternate_buddy; |
72 | 62 |
63 | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
64 /* ### Resources functions ### */ |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
65 |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
66 static void free_all_resources(GSList **reslist) |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
67 { |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
68 GSList *lip; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
69 res *p_res; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
70 |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
71 for ( lip = *reslist; lip ; lip = g_slist_next(lip)) { |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
72 p_res = (res*)lip->data; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
73 if (p_res->status_msg) { |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
74 g_free((gchar*)p_res->status_msg); |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
75 } |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
76 if (p_res->name) { |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
77 g_free((gchar*)p_res->name); |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
78 } |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
79 if (p_res->realjid) { |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
80 g_free((gchar*)p_res->realjid); |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
81 } |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
82 } |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
83 // Free all nodes but the first (which is static) |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
84 g_slist_free(*reslist); |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
85 *reslist = NULL; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
86 } |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
87 |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
88 // Resources are sorted in ascending order |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
89 static gint resource_compare_prio(res *a, res *b) { |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
90 //return (a->prio - b->prio); |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
91 if (a->prio < b->prio) return -1; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
92 else return 1; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
93 } |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
94 |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
95 // get_resource(rost, resname) |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
96 // Return a pointer to the resource with name resname, in rost's resources list |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
97 // - if rost has no resources, return NULL |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
98 // - if resname is defined, return the match or NULL |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
99 // - if resname is NULL, the last resource is returned, currently |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
100 // This could change in the future, because we should return the best one |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
101 // (priority? last used? and fall back to the first resource) |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
102 // |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
103 static res *get_resource(roster *rost, const char *resname) |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
104 { |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
105 GSList *p; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
106 res *r = NULL; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
107 |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
108 for (p = rost->resource; p; p = g_slist_next(p)) { |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
109 r = p->data; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
110 if (resname && !strcmp(r->name, resname)) |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
111 return r; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
112 } |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
113 |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
114 // The last resource is one of the resources with the highest priority, |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
115 // however, we don't know if it is the more-recently-used. |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
116 if (!resname) return r; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
117 return NULL; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
118 } |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
119 |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
120 // get_or_add_resource(rost, resname, priority) |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
121 // - if there is a "resname" resource in rost's resources, return a pointer |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
122 // on this resource |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
123 // - if not, add the resource, set the name, and return a pointer on this |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
124 // new resource |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
125 static res *get_or_add_resource(roster *rost, const char *resname, gchar prio) |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
126 { |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
127 GSList *p; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
128 res *nres; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
129 |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
130 if (!resname) return NULL; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
131 |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
132 for (p = rost->resource; p; p = g_slist_next(p)) { |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
133 res *r = p->data; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
134 if (!strcmp(r->name, resname)) |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
135 return r; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
136 } |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
137 |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
138 // Resource not found |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
139 nres = g_new0(res, 1); |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
140 nres->name = g_strdup(resname); |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
141 nres->prio = prio; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
142 rost->resource = g_slist_insert_sorted(rost->resource, nres, |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
143 (GCompareFunc)&resource_compare_prio); |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
144 return nres; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
145 } |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
146 |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
147 static void del_resource(roster *rost, const char *resname) |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
148 { |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
149 GSList *p; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
150 GSList *p_res_elt = NULL; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
151 res *p_res; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
152 |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
153 if (!resname) return; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
154 |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
155 for (p = rost->resource; p; p = g_slist_next(p)) { |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
156 res *r = p->data; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
157 if (!strcmp(r->name, resname)) |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
158 p_res_elt = p; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
159 } |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
160 |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
161 if (!p_res_elt) return; // Resource not found |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
162 |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
163 p_res = p_res_elt->data; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
164 // Free allocations and delete resource node |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
165 if (p_res->name) g_free(p_res->name); |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
166 if (p_res->status_msg) g_free(p_res->status_msg); |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
167 if (p_res->realjid) g_free(p_res->realjid); |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
168 rost->resource = g_slist_delete_link(rost->resource, p_res_elt); |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
169 return; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
170 } |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
171 |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
172 |
72 | 173 /* ### Roster functions ### */ |
174 | |
175 // 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
|
176 static gint roster_compare_jid_type(roster *a, roster *b) { |
89 | 177 if (! (a->type & b->type)) |
72 | 178 return -1; // arbitrary (but should be != , of course) |
179 return strcasecmp(a->jid, b->jid); | |
180 } | |
181 | |
182 // Comparison function used to sort the roster (by name) | |
336
eb994ee40029
Make some functions static
Mikael Berthe <mikael@lilotux.net>
parents:
331
diff
changeset
|
183 static gint roster_compare_name(roster *a, roster *b) { |
72 | 184 return strcasecmp(a->name, b->name); |
185 } | |
186 | |
187 // Finds a roster element (user, group, agent...), by jid or name | |
89 | 188 // If roster_type is 0, returns match of any type. |
72 | 189 // Returns the roster GSList element, or NULL if jid/name not found |
78 | 190 GSList *roster_find(const char *jidname, enum findwhat type, guint roster_type) |
72 | 191 { |
192 GSList *sl_roster_elt = groups; | |
193 GSList *res; | |
194 roster sample; | |
195 GCompareFunc comp; | |
196 | |
192 | 197 if (!jidname) return NULL; |
72 | 198 |
89 | 199 if (!roster_type) |
200 roster_type = ROSTER_TYPE_USER|ROSTER_TYPE_AGENT|ROSTER_TYPE_GROUP; | |
201 | |
72 | 202 sample.type = roster_type; |
203 if (type == jidsearch) { | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
204 sample.jid = (gchar*)jidname; |
72 | 205 comp = (GCompareFunc)&roster_compare_jid_type; |
206 } else if (type == namesearch) { | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
207 sample.name = (gchar*)jidname; |
72 | 208 comp = (GCompareFunc)&roster_compare_name; |
209 } else | |
210 return NULL; // should not happen | |
211 | |
212 while (sl_roster_elt) { | |
213 roster *roster_elt = (roster*)sl_roster_elt->data; | |
214 if (roster_type & ROSTER_TYPE_GROUP) { | |
215 if ((type == namesearch) && !strcasecmp(jidname, roster_elt->name)) | |
216 return sl_roster_elt; | |
217 } else { | |
218 res = g_slist_find_custom(roster_elt->list, &sample, comp); | |
219 if (res) | |
220 return res; | |
221 } | |
222 sl_roster_elt = g_slist_next(sl_roster_elt); | |
223 } | |
224 return NULL; | |
225 } | |
226 | |
227 // Returns pointer to new group, or existing group with that name | |
78 | 228 GSList *roster_add_group(const char *name) |
72 | 229 { |
230 roster *roster_grp; | |
231 // #1 Check name doesn't already exist | |
232 if (!roster_find(name, namesearch, ROSTER_TYPE_GROUP)) { | |
233 // #2 Create the group node | |
234 roster_grp = g_new0(roster, 1); | |
235 roster_grp->name = g_strdup(name); | |
236 roster_grp->type = ROSTER_TYPE_GROUP; | |
237 // #3 Insert (sorted) | |
238 groups = g_slist_insert_sorted(groups, roster_grp, | |
239 (GCompareFunc)&roster_compare_name); | |
240 } | |
241 return roster_find(name, namesearch, ROSTER_TYPE_GROUP); | |
242 } | |
243 | |
244 // Returns a pointer to the new user, or existing user with that name | |
78 | 245 GSList *roster_add_user(const char *jid, const char *name, const char *group, |
246 guint type) | |
72 | 247 { |
248 roster *roster_usr; | |
249 roster *my_group; | |
250 GSList *slist; | |
251 | |
252 if ((type != ROSTER_TYPE_USER) && (type != ROSTER_TYPE_AGENT)) { | |
253 // XXX Error message? | |
254 return NULL; | |
255 } | |
256 | |
78 | 257 // Let's be arbitrary: default group has an empty name (""). |
258 if (!group) group = ""; | |
259 | |
72 | 260 // #1 Check this user doesn't already exist |
192 | 261 slist = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT); |
262 if (slist) return slist; | |
72 | 263 // #2 add group if necessary |
264 slist = roster_add_group(group); | |
265 if (!slist) return NULL; | |
266 my_group = (roster*)slist->data; | |
267 // #3 Create user node | |
268 roster_usr = g_new0(roster, 1); | |
269 roster_usr->jid = g_strdup(jid); | |
78 | 270 if (name) { |
271 roster_usr->name = g_strdup(name); | |
272 } else { | |
273 gchar *p, *str = g_strdup(jid); | |
80 | 274 p = strstr(str, "/"); |
78 | 275 if (p) *p = '\0'; |
276 roster_usr->name = g_strdup(str); | |
277 g_free(str); | |
278 } | |
72 | 279 roster_usr->type = type; //ROSTER_TYPE_USER; |
280 roster_usr->list = slist; // (my_group SList element) | |
281 // #4 Insert node (sorted) | |
282 my_group->list = g_slist_insert_sorted(my_group->list, roster_usr, | |
283 (GCompareFunc)&roster_compare_name); | |
284 return roster_find(jid, jidsearch, type); | |
285 } | |
286 | |
287 // Removes user (jid) from roster, frees allocated memory | |
78 | 288 void roster_del_user(const char *jid) |
72 | 289 { |
290 GSList *sl_user, *sl_group; | |
291 GSList **sl_group_listptr; | |
292 roster *roster_usr; | |
246 | 293 GSList *node; |
72 | 294 |
89 | 295 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT); |
296 if (sl_user == NULL) | |
72 | 297 return; |
298 roster_usr = (roster*)sl_user->data; | |
246 | 299 |
300 // Remove (if present) from unread messages list | |
301 node = g_slist_find(unread_list, roster_usr); | |
302 if (node) unread_list = g_slist_delete_link(unread_list, node); | |
303 | |
304 // Let's free memory (jid, name, status message) | |
221 | 305 if (roster_usr->jid) g_free((gchar*)roster_usr->jid); |
306 if (roster_usr->name) g_free((gchar*)roster_usr->name); | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
307 free_all_resources(&roster_usr->resource); |
210 | 308 g_free(roster_usr); |
72 | 309 |
310 // That's a little complex, we need to dereference twice | |
311 sl_group = ((roster*)sl_user->data)->list; | |
312 sl_group_listptr = &((roster*)(sl_group->data))->list; | |
313 *sl_group_listptr = g_slist_delete_link(*sl_group_listptr, sl_user); | |
78 | 314 |
315 // We need to rebuild the list | |
316 if (current_buddy) | |
317 buddylist_build(); | |
210 | 318 // TODO What we could do, too, is to check if the deleted node is |
78 | 319 // current_buddy, in which case we could move current_buddy to the |
320 // previous (or next) node. | |
72 | 321 } |
322 | |
210 | 323 // Free all roster data and call buddylist_build() to free the buddylist. |
164 | 324 void roster_free(void) |
325 { | |
326 GSList *sl_grp = groups; | |
327 | |
301
efcbd3c1ad15
Free unread_list when the roster is freed
Mikael Berthe <mikael@lilotux.net>
parents:
265
diff
changeset
|
328 // Free unread_list |
efcbd3c1ad15
Free unread_list when the roster is freed
Mikael Berthe <mikael@lilotux.net>
parents:
265
diff
changeset
|
329 if (unread_list) { |
efcbd3c1ad15
Free unread_list when the roster is freed
Mikael Berthe <mikael@lilotux.net>
parents:
265
diff
changeset
|
330 g_slist_free(unread_list); |
efcbd3c1ad15
Free unread_list when the roster is freed
Mikael Berthe <mikael@lilotux.net>
parents:
265
diff
changeset
|
331 unread_list = NULL; |
efcbd3c1ad15
Free unread_list when the roster is freed
Mikael Berthe <mikael@lilotux.net>
parents:
265
diff
changeset
|
332 } |
efcbd3c1ad15
Free unread_list when the roster is freed
Mikael Berthe <mikael@lilotux.net>
parents:
265
diff
changeset
|
333 |
164 | 334 // Walk through groups |
335 while (sl_grp) { | |
336 roster *roster_grp = (roster*)sl_grp->data; | |
337 GSList *sl_usr = roster_grp->list; | |
338 // Walk through this group users | |
339 while (sl_usr) { | |
340 roster *roster_usr = (roster*)sl_usr->data; | |
341 // Free name and jid | |
221 | 342 if (roster_usr->jid) g_free((gchar*)roster_usr->jid); |
343 if (roster_usr->name) g_free((gchar*)roster_usr->name); | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
344 free_all_resources(&roster_usr->resource); |
214 | 345 g_free(roster_usr); |
164 | 346 sl_usr = g_slist_next(sl_usr); |
347 } | |
348 // Free group's users list | |
349 if (roster_grp->list) | |
350 g_slist_free(roster_grp->list); | |
351 // Free group's name and jid | |
221 | 352 if (roster_grp->jid) g_free((gchar*)roster_grp->jid); |
353 if (roster_grp->name) g_free((gchar*)roster_grp->name); | |
214 | 354 g_free(roster_grp); |
164 | 355 sl_grp = g_slist_next(sl_grp); |
356 } | |
357 // Free groups list | |
358 if (groups) { | |
359 g_slist_free(groups); | |
360 groups = NULL; | |
361 // Update (i.e. free) buddylist | |
362 if (buddylist) | |
363 buddylist_build(); | |
364 } | |
365 } | |
366 | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
367 void roster_setstatus(const char *jid, const char *resname, gchar prio, |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
368 enum imstatus bstat, const char *status_msg) |
72 | 369 { |
370 GSList *sl_user; | |
371 roster *roster_usr; | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
372 res *p_res; |
72 | 373 |
89 | 374 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT); |
92 | 375 // If we can't find it, we add it |
89 | 376 if (sl_user == NULL) |
92 | 377 sl_user = roster_add_user(jid, NULL, NULL, ROSTER_TYPE_USER); |
72 | 378 |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
379 // If there is no resource name, we can leave now |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
380 if (!resname) return; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
381 |
72 | 382 roster_usr = (roster*)sl_user->data; |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
383 |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
384 // If bstat is offline, we MUST delete the resource, actually |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
385 if (bstat == offline) { |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
386 del_resource(roster_usr, resname); |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
387 return; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
388 } |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
389 |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
390 // New or updated resource |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
391 p_res = get_or_add_resource(roster_usr, resname, prio); |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
392 p_res->status = bstat; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
393 if (p_res->status_msg) { |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
394 g_free((gchar*)p_res->status_msg); |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
395 p_res->status_msg = NULL; |
221 | 396 } |
397 if (status_msg) | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
398 p_res->status_msg = g_strdup(status_msg); |
72 | 399 } |
400 | |
78 | 401 // roster_setflags() |
402 // Set one or several flags to value (TRUE/FALSE) | |
80 | 403 void roster_setflags(const char *jid, guint flags, guint value) |
78 | 404 { |
405 GSList *sl_user; | |
406 roster *roster_usr; | |
407 | |
89 | 408 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT); |
409 if (sl_user == NULL) | |
78 | 410 return; |
411 | |
412 roster_usr = (roster*)sl_user->data; | |
413 if (value) | |
414 roster_usr->flags |= flags; | |
415 else | |
416 roster_usr->flags &= ~flags; | |
417 } | |
148 | 418 |
419 // roster_msg_setflag() | |
420 // Set the ROSTER_FLAG_MSG to the given value for the given jid. | |
421 // It will update the buddy's group message flag. | |
236 | 422 // Update the unread messages list too. |
148 | 423 void roster_msg_setflag(const char *jid, guint value) |
424 { | |
425 GSList *sl_user; | |
426 roster *roster_usr, *roster_grp; | |
427 | |
428 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT); | |
429 if (sl_user == NULL) | |
430 return; | |
431 | |
432 roster_usr = (roster*)sl_user->data; | |
433 roster_grp = (roster*)roster_usr->list->data; | |
434 if (value) { | |
435 // Message flag is TRUE. This is easy, we just have to set both flags | |
436 // to TRUE... | |
437 roster_usr->flags |= ROSTER_FLAG_MSG; | |
438 roster_grp->flags |= ROSTER_FLAG_MSG; // group | |
236 | 439 // Append the roster_usr to unread_list, but avoid duplicates |
440 if (!g_slist_find(unread_list, roster_usr)) | |
441 unread_list = g_slist_append(unread_list, roster_usr); | |
148 | 442 } else { |
443 // Message flag is FALSE. | |
444 guint msg = FALSE; | |
445 roster_usr->flags &= ~ROSTER_FLAG_MSG; | |
236 | 446 if (unread_list) { |
447 GSList *node = g_slist_find(unread_list, roster_usr); | |
448 if (node) unread_list = g_slist_delete_link(unread_list, node); | |
449 } | |
148 | 450 // For the group value we need to watch all buddies in this group; |
451 // if one is flagged, then the group will be flagged. | |
452 // I will re-use sl_user and roster_usr here, as they aren't used | |
453 // anymore. | |
454 sl_user = roster_grp->list; | |
455 while (sl_user) { | |
456 roster_usr = (roster*)sl_user->data; | |
457 if (roster_usr->flags & ROSTER_FLAG_MSG) { | |
458 msg = TRUE; | |
459 break; | |
460 } | |
461 sl_user = g_slist_next(sl_user); | |
462 } | |
463 if (!msg) | |
464 roster_grp->flags &= ~ROSTER_FLAG_MSG; | |
465 else | |
466 roster_grp->flags |= ROSTER_FLAG_MSG; | |
467 // Actually the "else" part is useless, because the group | |
468 // ROSTER_FLAG_MSG should already be set... | |
469 } | |
470 } | |
471 | |
89 | 472 void roster_settype(const char *jid, guint type) |
473 { | |
474 GSList *sl_user; | |
475 roster *roster_usr; | |
476 | |
477 if ((sl_user = roster_find(jid, jidsearch, 0)) == NULL) | |
478 return; | |
479 | |
480 roster_usr = (roster*)sl_user->data; | |
481 roster_usr->type = type; | |
482 } | |
483 | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
484 enum imstatus roster_getstatus(const char *jid, const char *resname) |
90 | 485 { |
486 GSList *sl_user; | |
487 roster *roster_usr; | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
488 res *p_res; |
90 | 489 |
490 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT); | |
491 if (sl_user == NULL) | |
492 return offline; // Not in the roster, anyway... | |
493 | |
494 roster_usr = (roster*)sl_user->data; | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
495 p_res = get_resource(roster_usr, resname); |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
496 if (p_res) |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
497 return p_res->status; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
498 return offline; |
90 | 499 } |
500 | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
501 const char *roster_getstatusmsg(const char *jid, const char *resname) |
318
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
502 { |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
503 GSList *sl_user; |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
504 roster *roster_usr; |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
505 res *p_res; |
318
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
506 |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
507 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
|
508 if (sl_user == NULL) |
425
03f1e37759a6
roster_getstatusmsg() should return a pointer
Mikael Berthe <mikael@lilotux.net>
parents:
393
diff
changeset
|
509 return NULL; // Not in the roster, anyway... |
318
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
510 |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
511 roster_usr = (roster*)sl_user->data; |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
512 p_res = get_resource(roster_usr, resname); |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
513 if (p_res) |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
514 return p_res->status_msg; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
515 return NULL; |
318
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
516 } |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
517 |
89 | 518 guint roster_gettype(const char *jid) |
519 { | |
520 GSList *sl_user; | |
521 roster *roster_usr; | |
522 | |
523 if ((sl_user = roster_find(jid, jidsearch, 0)) == NULL) | |
524 return 0; | |
525 | |
526 roster_usr = (roster*)sl_user->data; | |
527 return roster_usr->type; | |
528 } | |
529 | |
141 | 530 inline guint roster_exists(const char *jidname, enum findwhat type, |
531 guint roster_type) | |
532 { | |
533 if (roster_find(jidname, type, roster_type)) | |
534 return TRUE; | |
535 return FALSE; | |
536 } | |
537 | |
72 | 538 |
539 /* ### BuddyList functions ### */ | |
540 | |
120 | 541 // buddylist_set_hide_offline_buddies(hide) |
72 | 542 // "hide" values: 1=hide 0=show_all -1=invert |
120 | 543 void buddylist_set_hide_offline_buddies(int hide) |
72 | 544 { |
545 if (hide < 0) // NEG (invert) | |
546 hide_offline_buddies = !hide_offline_buddies; | |
547 else if (hide == 0) // FALSE (don't hide) | |
548 hide_offline_buddies = 0; | |
549 else // TRUE (hide) | |
550 hide_offline_buddies = 1; | |
551 } | |
552 | |
120 | 553 inline int buddylist_get_hide_offline_buddies(void) |
554 { | |
555 return hide_offline_buddies; | |
556 } | |
557 | |
72 | 558 // buddylist_build() |
559 // Creates the buddylist from the roster entries. | |
560 void buddylist_build(void) | |
561 { | |
562 GSList *sl_roster_elt = groups; | |
563 roster *roster_elt; | |
81 | 564 roster *roster_current_buddy = NULL; |
330 | 565 roster *roster_alternate_buddy = NULL; |
128 | 566 int shrunk_group; |
72 | 567 |
81 | 568 // We need to remember which buddy is selected. |
569 if (current_buddy) | |
570 roster_current_buddy = BUDDATA(current_buddy); | |
571 current_buddy = NULL; | |
330 | 572 if (alternate_buddy) |
573 roster_alternate_buddy = BUDDATA(alternate_buddy); | |
574 alternate_buddy = NULL; | |
81 | 575 |
72 | 576 // Destroy old buddylist |
577 if (buddylist) { | |
578 g_list_free(buddylist); | |
579 buddylist = NULL; | |
580 } | |
581 | |
582 // Create the new list | |
583 while (sl_roster_elt) { | |
584 GSList *sl_roster_usrelt; | |
585 roster *roster_usrelt; | |
147 | 586 guint pending_group = FALSE; |
72 | 587 roster_elt = (roster*) sl_roster_elt->data; |
588 | |
589 // Add the group now unless hide_offline_buddies is set, | |
590 // in which case we'll add it only if an online buddy belongs to it. | |
147 | 591 // We take care to keep the current_buddy in the list, too. |
592 if (!hide_offline_buddies || roster_elt == roster_current_buddy) | |
72 | 593 buddylist = g_list_append(buddylist, roster_elt); |
594 else | |
147 | 595 pending_group = TRUE; |
72 | 596 |
128 | 597 shrunk_group = roster_elt->flags & ROSTER_FLAG_HIDE; |
598 | |
72 | 599 sl_roster_usrelt = roster_elt->list; |
600 while (sl_roster_usrelt) { | |
601 roster_usrelt = (roster*) sl_roster_usrelt->data; | |
602 | |
603 // Buddy will be added if either: | |
604 // - hide_offline_buddies is FALSE | |
605 // - buddy is not offline | |
606 // - buddy has a lock (for example the buddy window is currently open) | |
607 // - buddy has a pending (non-read) message | |
128 | 608 // - group isn't hidden (shrunk) |
147 | 609 // - this is the current_buddy |
610 if (!hide_offline_buddies || roster_usrelt == roster_current_buddy || | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
611 (buddy_getstatus((gpointer)roster_usrelt, NULL) != offline) || |
72 | 612 (buddy_getflags((gpointer)roster_usrelt) & |
613 (ROSTER_FLAG_LOCK | ROSTER_FLAG_MSG))) { | |
614 // This user should be added. Maybe the group hasn't been added yet? | |
147 | 615 if (pending_group && |
616 (hide_offline_buddies || roster_usrelt == roster_current_buddy)) { | |
72 | 617 // It hasn't been done yet |
618 buddylist = g_list_append(buddylist, roster_elt); | |
619 pending_group = FALSE; | |
620 } | |
621 // Add user | |
128 | 622 // XXX Should we add the user if there is a message and |
623 // the group is shrunk? If so, we'd need to check LOCK flag too, | |
624 // perhaps... | |
625 if (!shrunk_group) | |
626 buddylist = g_list_append(buddylist, roster_usrelt); | |
72 | 627 } |
628 | |
629 sl_roster_usrelt = g_slist_next(sl_roster_usrelt); | |
630 } | |
631 sl_roster_elt = g_slist_next(sl_roster_elt); | |
632 } | |
78 | 633 |
81 | 634 // Check if we can find our saved current_buddy... |
635 if (roster_current_buddy) | |
636 current_buddy = g_list_find(buddylist, roster_current_buddy); | |
330 | 637 if (roster_alternate_buddy) |
638 alternate_buddy = g_list_find(buddylist, roster_alternate_buddy); | |
78 | 639 // current_buddy initialization |
640 if (!current_buddy || (g_list_position(buddylist, current_buddy) == -1)) | |
81 | 641 current_buddy = g_list_first(buddylist); |
128 | 642 // XXX Maybe we should set update_roster to TRUE there? |
72 | 643 } |
644 | |
645 // buddy_hide_group(roster, hide) | |
646 // "hide" values: 1=hide 0=show_all -1=invert | |
647 void buddy_hide_group(gpointer rosterdata, int hide) | |
648 { | |
207 | 649 roster *roster_usr = rosterdata; |
72 | 650 if (hide > 0) // TRUE (hide) |
207 | 651 roster_usr->flags |= ROSTER_FLAG_HIDE; |
72 | 652 else if (hide < 0) // NEG (invert) |
207 | 653 roster_usr->flags ^= ROSTER_FLAG_HIDE; |
72 | 654 else // FALSE (don't hide) |
207 | 655 roster_usr->flags &= ~ROSTER_FLAG_HIDE; |
72 | 656 } |
657 | |
658 const char *buddy_getjid(gpointer rosterdata) | |
659 { | |
207 | 660 roster *roster_usr = rosterdata; |
661 return roster_usr->jid; | |
662 } | |
663 | |
210 | 664 // buddy_setgroup() |
665 // Change the group of current buddy | |
666 // | |
667 // Warning! This function changes current_buddy! | |
668 // Warning! Old buddy is deleted, so you can't acces to its jid/name after | |
669 // calling this function. | |
670 void buddy_setgroup(gpointer rosterdata, char *newgroupname) | |
671 { | |
672 roster *roster_usr = rosterdata; | |
673 GSList **sl_group; | |
674 GSList *sl_clone; | |
675 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
|
676 int is_alternate; |
210 | 677 |
678 // A group has no group :) | |
679 if (roster_usr->type & ROSTER_TYPE_GROUP) return; | |
680 | |
681 // Remove the buddy from current group | |
682 sl_group = &((roster*)((GSList*)roster_usr->list)->data)->list; | |
683 *sl_group = g_slist_remove(*sl_group, rosterdata); | |
393 | 684 |
210 | 685 // Add the buddy to its new group; actually we "clone" this buddy... |
686 sl_clone = roster_add_user(roster_usr->jid, roster_usr->name, | |
687 newgroupname, roster_usr->type); | |
688 roster_clone = (roster*)sl_clone->data; | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
689 roster_clone->flags = roster_usr->flags; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
690 roster_clone->resource = roster_usr->resource; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
691 roster_usr->resource = NULL; |
210 | 692 |
693 // Free old buddy | |
221 | 694 if (roster_usr->jid) g_free((gchar*)roster_usr->jid); |
695 if (roster_usr->name) g_free((gchar*)roster_usr->name); | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
696 free_all_resources(&roster_usr->resource); |
210 | 697 g_free(roster_usr); |
698 | |
212 | 699 // If new new group is folded, the curren_buddy will be lost, and the |
700 // chat window won't be correctly refreshed. So we make sure it isn't... | |
701 ((roster*)((GSList*)roster_clone->list)->data)->flags &= ~ROSTER_FLAG_HIDE; | |
702 | |
703 // 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
|
704 is_alternate = (alternate_buddy == current_buddy); |
212 | 705 buddylist = g_list_append(buddylist, roster_clone); |
706 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
|
707 if (is_alternate) |
1510dda4ad71
Don't loose alternate_buddy when the buddy is moved ("/move")
Mikael Berthe <mikael@lilotux.net>
parents:
330
diff
changeset
|
708 alternate_buddy = current_buddy; |
212 | 709 |
210 | 710 buddylist_build(); |
711 } | |
712 | |
207 | 713 void buddy_setname(gpointer rosterdata, char *newname) |
714 { | |
715 roster *roster_usr = rosterdata; | |
209 | 716 GSList **sl_group; |
207 | 717 |
718 // TODO For groups, we need to check for unicity | |
719 // However, renaming a group boils down to moving all its buddies to | |
720 // another group, so calling this function is not really necessary... | |
721 if (roster_usr->type & ROSTER_TYPE_GROUP) return; | |
722 | |
723 if (roster_usr->name) { | |
724 g_free((gchar*)roster_usr->name); | |
725 roster_usr->name = NULL; | |
726 } | |
727 if (newname) | |
728 roster_usr->name = g_strdup(newname); | |
209 | 729 |
730 // We need to resort the group list | |
731 sl_group = &((roster*)((GSList*)roster_usr->list)->data)->list; | |
732 *sl_group = g_slist_sort(*sl_group, (GCompareFunc)&roster_compare_name); | |
733 | |
734 buddylist_build(); | |
72 | 735 } |
736 | |
737 const char *buddy_getname(gpointer rosterdata) | |
738 { | |
207 | 739 roster *roster_usr = rosterdata; |
740 return roster_usr->name; | |
741 } | |
742 | |
743 // buddy_getgroupname() | |
744 // Returns a pointer on buddy's group name. | |
745 const char *buddy_getgroupname(gpointer rosterdata) | |
746 { | |
747 roster *roster_usr = rosterdata; | |
748 | |
749 if (roster_usr->type & ROSTER_TYPE_GROUP) | |
750 return roster_usr->name; | |
751 | |
752 // This is a user | |
753 return ((roster*)((GSList*)roster_usr->list)->data)->name; | |
72 | 754 } |
755 | |
166 | 756 // buddy_getgroup() |
757 // Returns a pointer on buddy's group. | |
758 gpointer buddy_getgroup(gpointer rosterdata) | |
759 { | |
207 | 760 roster *roster_usr = rosterdata; |
166 | 761 |
207 | 762 if (roster_usr->type & ROSTER_TYPE_GROUP) |
166 | 763 return rosterdata; |
764 | |
765 // This is a user | |
207 | 766 return (gpointer)((GSList*)roster_usr->list)->data; |
166 | 767 } |
768 | |
72 | 769 guint buddy_gettype(gpointer rosterdata) |
770 { | |
207 | 771 roster *roster_usr = rosterdata; |
772 return roster_usr->type; | |
72 | 773 } |
774 | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
775 enum imstatus buddy_getstatus(gpointer rosterdata, const char *resname) |
72 | 776 { |
207 | 777 roster *roster_usr = rosterdata; |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
778 res *p_res = get_resource(roster_usr, resname); |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
779 if (p_res) |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
780 return p_res->status; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
781 return offline; |
72 | 782 } |
783 | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
784 const char *buddy_getstatusmsg(gpointer rosterdata, const char *resname) |
221 | 785 { |
786 roster *roster_usr = rosterdata; | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
787 res *p_res = get_resource(roster_usr, resname); |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
788 if (p_res) |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
789 return p_res->status_msg; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
790 return NULL; |
221 | 791 } |
792 | |
439
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
793 gchar buddy_getresourceprio(gpointer rosterdata, const char *resname) |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
794 { |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
795 roster *roster_usr = rosterdata; |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
796 res *p_res = get_resource(roster_usr, resname); |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
797 if (p_res) |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
798 return p_res->prio; |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
799 return 0; |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
800 } |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
801 |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
802 // buddy_getresources(roster_data) |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
803 // Return a singly-linked-list of resource names |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
804 // Note: the caller should free the list (and data) after use |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
805 GSList *buddy_getresources(gpointer rosterdata) |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
806 { |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
807 roster *roster_usr = rosterdata; |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
808 GSList *reslist = NULL, *lp; |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
809 |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
810 for (lp = roster_usr->resource; lp; lp = g_slist_next(lp)) |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
811 reslist = g_slist_append(reslist, g_strdup(((res*)lp->data)->name)); |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
812 |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
813 return reslist; |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
814 } |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
815 |
119 | 816 // buddy_setflags() |
817 // Set one or several flags to value (TRUE/FALSE) | |
818 void buddy_setflags(gpointer rosterdata, guint flags, guint value) | |
819 { | |
207 | 820 roster *roster_usr = rosterdata; |
119 | 821 if (value) |
207 | 822 roster_usr->flags |= flags; |
119 | 823 else |
207 | 824 roster_usr->flags &= ~flags; |
119 | 825 } |
826 | |
72 | 827 guint buddy_getflags(gpointer rosterdata) |
828 { | |
207 | 829 roster *roster_usr = rosterdata; |
830 return roster_usr->flags; | |
72 | 831 } |
832 | |
265 | 833 // buddy_search(string) |
834 // Look for a buddy whose name or jid contains string. | |
835 // Search begins at current_buddy; if no match is found in the the buddylist, | |
836 // return NULL; | |
837 GList *buddy_search(char *string) | |
838 { | |
839 GList *buddy = current_buddy; | |
840 roster *roster_usr; | |
841 if (!buddylist || !current_buddy) return NULL; | |
842 for (;;) { | |
843 buddy = g_list_next(buddy); | |
844 if (!buddy) | |
845 buddy = buddylist; | |
846 | |
847 roster_usr = (roster*)buddy->data; | |
848 if (roster_usr->jid && strcasestr(roster_usr->jid, string)) | |
849 return buddy; | |
850 if (roster_usr->name && strcasestr(roster_usr->name, string)) | |
851 return buddy; | |
852 | |
853 if (buddy == current_buddy) | |
854 return NULL; // Back to the beginning, and no match found | |
855 } | |
856 } | |
857 | |
225 | 858 // compl_list(type) |
859 // Returns a list of jid's or groups. (For commands completion) | |
860 // type: ROSTER_TYPE_USER (jid's) or ROSTER_TYPE_GROUP (group names) | |
861 // The list should be freed by the caller after use. | |
862 GSList *compl_list(guint type) | |
863 { | |
864 GSList *list = NULL; | |
865 GList *buddy = buddylist; | |
866 | |
867 for ( ; buddy ; buddy = g_list_next(buddy)) { | |
868 guint btype = buddy_gettype(BUDDATA(buddy)); | |
869 | |
870 if (type == ROSTER_TYPE_GROUP) { // (group names) | |
871 if (btype == ROSTER_TYPE_GROUP) { | |
872 const char *bname = buddy_getname(BUDDATA(buddy)); | |
873 if ((bname) && (*bname)) | |
874 list = g_slist_append(list, g_strdup(bname)); | |
875 } | |
876 } else { // ROSTER_TYPE_USER (jid) | |
877 const char *bjid = buddy_getjid(BUDDATA(buddy)); | |
878 if (bjid) | |
879 list = g_slist_append(list, g_strdup(bjid)); | |
880 } | |
881 } | |
882 | |
883 return list; | |
884 } | |
885 | |
236 | 886 // unread_msg(rosterdata) |
887 // Return the next buddy with an unread message. If the parameter is NULL, | |
888 // return the first buddy with an unread message. | |
889 gpointer unread_msg(gpointer rosterdata) | |
890 { | |
891 GSList *unread, *next_unread; | |
892 | |
893 if (!unread_list) return NULL; | |
894 // First unread message | |
895 if (!rosterdata) return unread_list->data; | |
896 | |
897 unread = g_slist_find(unread_list, rosterdata); | |
898 if (!unread) return unread_list->data; | |
899 | |
900 next_unread = g_slist_next(unread); | |
901 if (next_unread) return next_unread->data; | |
902 | |
903 return unread_list->data; | |
904 } | |
905 |