Mercurial > hg
annotate mcabber/src/roster.c @ 480:882e1acae422
Add /say_to command
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Tue, 04 Oct 2005 19:47:50 +0200 |
parents | a926523d2392 |
children | 4a10c04ac2fb |
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; |
447
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
47 gchar *nickname; // For groupchats |
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)) |
480 | 178 return -1; // arbitrary (but should be != 0, of course) |
72 | 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) |
480 | 200 roster_type = ROSTER_TYPE_USER | ROSTER_TYPE_ROOM | |
447
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
201 ROSTER_TYPE_AGENT | ROSTER_TYPE_GROUP; |
89 | 202 |
72 | 203 sample.type = roster_type; |
204 if (type == jidsearch) { | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
205 sample.jid = (gchar*)jidname; |
72 | 206 comp = (GCompareFunc)&roster_compare_jid_type; |
207 } else if (type == namesearch) { | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
208 sample.name = (gchar*)jidname; |
72 | 209 comp = (GCompareFunc)&roster_compare_name; |
210 } else | |
211 return NULL; // should not happen | |
212 | |
213 while (sl_roster_elt) { | |
214 roster *roster_elt = (roster*)sl_roster_elt->data; | |
215 if (roster_type & ROSTER_TYPE_GROUP) { | |
216 if ((type == namesearch) && !strcasecmp(jidname, roster_elt->name)) | |
217 return sl_roster_elt; | |
218 } else { | |
219 res = g_slist_find_custom(roster_elt->list, &sample, comp); | |
220 if (res) | |
221 return res; | |
222 } | |
223 sl_roster_elt = g_slist_next(sl_roster_elt); | |
224 } | |
225 return NULL; | |
226 } | |
227 | |
228 // Returns pointer to new group, or existing group with that name | |
78 | 229 GSList *roster_add_group(const char *name) |
72 | 230 { |
231 roster *roster_grp; | |
232 // #1 Check name doesn't already exist | |
233 if (!roster_find(name, namesearch, ROSTER_TYPE_GROUP)) { | |
234 // #2 Create the group node | |
235 roster_grp = g_new0(roster, 1); | |
236 roster_grp->name = g_strdup(name); | |
237 roster_grp->type = ROSTER_TYPE_GROUP; | |
238 // #3 Insert (sorted) | |
239 groups = g_slist_insert_sorted(groups, roster_grp, | |
240 (GCompareFunc)&roster_compare_name); | |
241 } | |
242 return roster_find(name, namesearch, ROSTER_TYPE_GROUP); | |
243 } | |
244 | |
245 // Returns a pointer to the new user, or existing user with that name | |
78 | 246 GSList *roster_add_user(const char *jid, const char *name, const char *group, |
247 guint type) | |
72 | 248 { |
249 roster *roster_usr; | |
250 roster *my_group; | |
251 GSList *slist; | |
252 | |
447
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
253 if ((type != ROSTER_TYPE_USER) && |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
254 (type != ROSTER_TYPE_ROOM) && |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
255 (type != ROSTER_TYPE_AGENT)) { |
72 | 256 // XXX Error message? |
257 return NULL; | |
258 } | |
259 | |
78 | 260 // Let's be arbitrary: default group has an empty name (""). |
261 if (!group) group = ""; | |
262 | |
72 | 263 // #1 Check this user doesn't already exist |
447
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
264 slist = roster_find(jid, jidsearch, type|ROSTER_TYPE_AGENT); |
192 | 265 if (slist) return slist; |
72 | 266 // #2 add group if necessary |
267 slist = roster_add_group(group); | |
268 if (!slist) return NULL; | |
269 my_group = (roster*)slist->data; | |
270 // #3 Create user node | |
271 roster_usr = g_new0(roster, 1); | |
272 roster_usr->jid = g_strdup(jid); | |
78 | 273 if (name) { |
274 roster_usr->name = g_strdup(name); | |
275 } else { | |
276 gchar *p, *str = g_strdup(jid); | |
80 | 277 p = strstr(str, "/"); |
78 | 278 if (p) *p = '\0'; |
279 roster_usr->name = g_strdup(str); | |
280 g_free(str); | |
281 } | |
447
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
282 roster_usr->type = type; |
72 | 283 roster_usr->list = slist; // (my_group SList element) |
284 // #4 Insert node (sorted) | |
285 my_group->list = g_slist_insert_sorted(my_group->list, roster_usr, | |
286 (GCompareFunc)&roster_compare_name); | |
287 return roster_find(jid, jidsearch, type); | |
288 } | |
289 | |
290 // Removes user (jid) from roster, frees allocated memory | |
78 | 291 void roster_del_user(const char *jid) |
72 | 292 { |
293 GSList *sl_user, *sl_group; | |
294 GSList **sl_group_listptr; | |
295 roster *roster_usr; | |
246 | 296 GSList *node; |
72 | 297 |
447
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
298 sl_user = roster_find(jid, jidsearch, |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
299 ROSTER_TYPE_USER|ROSTER_TYPE_AGENT|ROSTER_TYPE_ROOM); |
89 | 300 if (sl_user == NULL) |
72 | 301 return; |
302 roster_usr = (roster*)sl_user->data; | |
246 | 303 |
304 // Remove (if present) from unread messages list | |
305 node = g_slist_find(unread_list, roster_usr); | |
306 if (node) unread_list = g_slist_delete_link(unread_list, node); | |
307 | |
308 // Let's free memory (jid, name, status message) | |
221 | 309 if (roster_usr->jid) g_free((gchar*)roster_usr->jid); |
310 if (roster_usr->name) g_free((gchar*)roster_usr->name); | |
447
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
311 if (roster_usr->nickname) g_free((gchar*)roster_usr->nickname); |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
312 free_all_resources(&roster_usr->resource); |
210 | 313 g_free(roster_usr); |
72 | 314 |
315 // That's a little complex, we need to dereference twice | |
316 sl_group = ((roster*)sl_user->data)->list; | |
317 sl_group_listptr = &((roster*)(sl_group->data))->list; | |
318 *sl_group_listptr = g_slist_delete_link(*sl_group_listptr, sl_user); | |
78 | 319 |
320 // We need to rebuild the list | |
321 if (current_buddy) | |
322 buddylist_build(); | |
210 | 323 // TODO What we could do, too, is to check if the deleted node is |
78 | 324 // current_buddy, in which case we could move current_buddy to the |
325 // previous (or next) node. | |
72 | 326 } |
327 | |
210 | 328 // Free all roster data and call buddylist_build() to free the buddylist. |
164 | 329 void roster_free(void) |
330 { | |
331 GSList *sl_grp = groups; | |
332 | |
301
efcbd3c1ad15
Free unread_list when the roster is freed
Mikael Berthe <mikael@lilotux.net>
parents:
265
diff
changeset
|
333 // Free unread_list |
efcbd3c1ad15
Free unread_list when the roster is freed
Mikael Berthe <mikael@lilotux.net>
parents:
265
diff
changeset
|
334 if (unread_list) { |
efcbd3c1ad15
Free unread_list when the roster is freed
Mikael Berthe <mikael@lilotux.net>
parents:
265
diff
changeset
|
335 g_slist_free(unread_list); |
efcbd3c1ad15
Free unread_list when the roster is freed
Mikael Berthe <mikael@lilotux.net>
parents:
265
diff
changeset
|
336 unread_list = NULL; |
efcbd3c1ad15
Free unread_list when the roster is freed
Mikael Berthe <mikael@lilotux.net>
parents:
265
diff
changeset
|
337 } |
efcbd3c1ad15
Free unread_list when the roster is freed
Mikael Berthe <mikael@lilotux.net>
parents:
265
diff
changeset
|
338 |
164 | 339 // Walk through groups |
340 while (sl_grp) { | |
341 roster *roster_grp = (roster*)sl_grp->data; | |
342 GSList *sl_usr = roster_grp->list; | |
343 // Walk through this group users | |
344 while (sl_usr) { | |
345 roster *roster_usr = (roster*)sl_usr->data; | |
346 // Free name and jid | |
221 | 347 if (roster_usr->jid) g_free((gchar*)roster_usr->jid); |
348 if (roster_usr->name) g_free((gchar*)roster_usr->name); | |
447
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
349 if (roster_usr->nickname) g_free((gchar*)roster_usr->nickname); |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
350 free_all_resources(&roster_usr->resource); |
214 | 351 g_free(roster_usr); |
164 | 352 sl_usr = g_slist_next(sl_usr); |
353 } | |
354 // Free group's users list | |
355 if (roster_grp->list) | |
356 g_slist_free(roster_grp->list); | |
357 // Free group's name and jid | |
221 | 358 if (roster_grp->jid) g_free((gchar*)roster_grp->jid); |
359 if (roster_grp->name) g_free((gchar*)roster_grp->name); | |
214 | 360 g_free(roster_grp); |
164 | 361 sl_grp = g_slist_next(sl_grp); |
362 } | |
363 // Free groups list | |
364 if (groups) { | |
365 g_slist_free(groups); | |
366 groups = NULL; | |
367 // Update (i.e. free) buddylist | |
368 if (buddylist) | |
369 buddylist_build(); | |
370 } | |
371 } | |
372 | |
468
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
373 // roster_setstatus() |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
374 // Note: resname, role and realjid are for room members only |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
375 void roster_setstatus(const char *jid, const char *resname, gchar prio, |
468
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
376 enum imstatus bstat, const char *status_msg, |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
377 enum imrole role, const char *realjid) |
72 | 378 { |
379 GSList *sl_user; | |
380 roster *roster_usr; | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
381 res *p_res; |
72 | 382 |
447
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
383 sl_user = roster_find(jid, jidsearch, |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
384 ROSTER_TYPE_USER|ROSTER_TYPE_ROOM|ROSTER_TYPE_AGENT); |
92 | 385 // If we can't find it, we add it |
89 | 386 if (sl_user == NULL) |
92 | 387 sl_user = roster_add_user(jid, NULL, NULL, ROSTER_TYPE_USER); |
72 | 388 |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
389 // 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
|
390 if (!resname) return; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
391 |
72 | 392 roster_usr = (roster*)sl_user->data; |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
393 |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
394 // 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
|
395 if (bstat == offline) { |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
396 del_resource(roster_usr, resname); |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
397 return; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
398 } |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
399 |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
400 // New or updated resource |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
401 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
|
402 p_res->status = bstat; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
403 if (p_res->status_msg) { |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
404 g_free((gchar*)p_res->status_msg); |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
405 p_res->status_msg = NULL; |
221 | 406 } |
407 if (status_msg) | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
408 p_res->status_msg = g_strdup(status_msg); |
468
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
409 |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
410 p_res->role = role; |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
411 |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
412 if (p_res->realjid) { |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
413 g_free((gchar*)p_res->realjid); |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
414 p_res->realjid = NULL; |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
415 } |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
416 if (realjid) |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
417 p_res->realjid = g_strdup(realjid); |
72 | 418 } |
419 | |
78 | 420 // roster_setflags() |
421 // Set one or several flags to value (TRUE/FALSE) | |
80 | 422 void roster_setflags(const char *jid, guint flags, guint value) |
78 | 423 { |
424 GSList *sl_user; | |
425 roster *roster_usr; | |
426 | |
447
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
427 sl_user = roster_find(jid, jidsearch, |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
428 ROSTER_TYPE_USER|ROSTER_TYPE_ROOM|ROSTER_TYPE_AGENT); |
89 | 429 if (sl_user == NULL) |
78 | 430 return; |
431 | |
432 roster_usr = (roster*)sl_user->data; | |
433 if (value) | |
434 roster_usr->flags |= flags; | |
435 else | |
436 roster_usr->flags &= ~flags; | |
437 } | |
148 | 438 |
439 // roster_msg_setflag() | |
440 // Set the ROSTER_FLAG_MSG to the given value for the given jid. | |
441 // It will update the buddy's group message flag. | |
236 | 442 // Update the unread messages list too. |
148 | 443 void roster_msg_setflag(const char *jid, guint value) |
444 { | |
445 GSList *sl_user; | |
446 roster *roster_usr, *roster_grp; | |
447 | |
447
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
448 sl_user = roster_find(jid, jidsearch, |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
449 ROSTER_TYPE_USER|ROSTER_TYPE_ROOM|ROSTER_TYPE_AGENT); |
148 | 450 if (sl_user == NULL) |
451 return; | |
452 | |
453 roster_usr = (roster*)sl_user->data; | |
454 roster_grp = (roster*)roster_usr->list->data; | |
455 if (value) { | |
456 // Message flag is TRUE. This is easy, we just have to set both flags | |
457 // to TRUE... | |
458 roster_usr->flags |= ROSTER_FLAG_MSG; | |
459 roster_grp->flags |= ROSTER_FLAG_MSG; // group | |
236 | 460 // Append the roster_usr to unread_list, but avoid duplicates |
461 if (!g_slist_find(unread_list, roster_usr)) | |
462 unread_list = g_slist_append(unread_list, roster_usr); | |
148 | 463 } else { |
464 // Message flag is FALSE. | |
465 guint msg = FALSE; | |
466 roster_usr->flags &= ~ROSTER_FLAG_MSG; | |
236 | 467 if (unread_list) { |
468 GSList *node = g_slist_find(unread_list, roster_usr); | |
469 if (node) unread_list = g_slist_delete_link(unread_list, node); | |
470 } | |
148 | 471 // For the group value we need to watch all buddies in this group; |
472 // if one is flagged, then the group will be flagged. | |
473 // I will re-use sl_user and roster_usr here, as they aren't used | |
474 // anymore. | |
475 sl_user = roster_grp->list; | |
476 while (sl_user) { | |
477 roster_usr = (roster*)sl_user->data; | |
478 if (roster_usr->flags & ROSTER_FLAG_MSG) { | |
479 msg = TRUE; | |
480 break; | |
481 } | |
482 sl_user = g_slist_next(sl_user); | |
483 } | |
484 if (!msg) | |
485 roster_grp->flags &= ~ROSTER_FLAG_MSG; | |
486 else | |
487 roster_grp->flags |= ROSTER_FLAG_MSG; | |
488 // Actually the "else" part is useless, because the group | |
489 // ROSTER_FLAG_MSG should already be set... | |
490 } | |
491 } | |
492 | |
89 | 493 void roster_settype(const char *jid, guint type) |
494 { | |
495 GSList *sl_user; | |
496 roster *roster_usr; | |
497 | |
498 if ((sl_user = roster_find(jid, jidsearch, 0)) == NULL) | |
499 return; | |
500 | |
501 roster_usr = (roster*)sl_user->data; | |
502 roster_usr->type = type; | |
503 } | |
504 | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
505 enum imstatus roster_getstatus(const char *jid, const char *resname) |
90 | 506 { |
507 GSList *sl_user; | |
508 roster *roster_usr; | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
509 res *p_res; |
90 | 510 |
511 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT); | |
512 if (sl_user == NULL) | |
513 return offline; // Not in the roster, anyway... | |
514 | |
515 roster_usr = (roster*)sl_user->data; | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
516 p_res = get_resource(roster_usr, resname); |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
517 if (p_res) |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
518 return p_res->status; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
519 return offline; |
90 | 520 } |
521 | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
522 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
|
523 { |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
524 GSList *sl_user; |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
525 roster *roster_usr; |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
526 res *p_res; |
318
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
527 |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
528 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
|
529 if (sl_user == NULL) |
425
03f1e37759a6
roster_getstatusmsg() should return a pointer
Mikael Berthe <mikael@lilotux.net>
parents:
393
diff
changeset
|
530 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
|
531 |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
532 roster_usr = (roster*)sl_user->data; |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
533 p_res = get_resource(roster_usr, resname); |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
534 if (p_res) |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
535 return p_res->status_msg; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
536 return NULL; |
318
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
537 } |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
538 |
89 | 539 guint roster_gettype(const char *jid) |
540 { | |
541 GSList *sl_user; | |
542 roster *roster_usr; | |
543 | |
544 if ((sl_user = roster_find(jid, jidsearch, 0)) == NULL) | |
545 return 0; | |
546 | |
547 roster_usr = (roster*)sl_user->data; | |
548 return roster_usr->type; | |
549 } | |
550 | |
72 | 551 |
552 /* ### BuddyList functions ### */ | |
553 | |
120 | 554 // buddylist_set_hide_offline_buddies(hide) |
72 | 555 // "hide" values: 1=hide 0=show_all -1=invert |
120 | 556 void buddylist_set_hide_offline_buddies(int hide) |
72 | 557 { |
558 if (hide < 0) // NEG (invert) | |
559 hide_offline_buddies = !hide_offline_buddies; | |
560 else if (hide == 0) // FALSE (don't hide) | |
561 hide_offline_buddies = 0; | |
562 else // TRUE (hide) | |
563 hide_offline_buddies = 1; | |
564 } | |
565 | |
120 | 566 inline int buddylist_get_hide_offline_buddies(void) |
567 { | |
568 return hide_offline_buddies; | |
569 } | |
570 | |
72 | 571 // buddylist_build() |
572 // Creates the buddylist from the roster entries. | |
573 void buddylist_build(void) | |
574 { | |
575 GSList *sl_roster_elt = groups; | |
576 roster *roster_elt; | |
81 | 577 roster *roster_current_buddy = NULL; |
330 | 578 roster *roster_alternate_buddy = NULL; |
128 | 579 int shrunk_group; |
72 | 580 |
81 | 581 // We need to remember which buddy is selected. |
582 if (current_buddy) | |
583 roster_current_buddy = BUDDATA(current_buddy); | |
584 current_buddy = NULL; | |
330 | 585 if (alternate_buddy) |
586 roster_alternate_buddy = BUDDATA(alternate_buddy); | |
587 alternate_buddy = NULL; | |
81 | 588 |
72 | 589 // Destroy old buddylist |
590 if (buddylist) { | |
591 g_list_free(buddylist); | |
592 buddylist = NULL; | |
593 } | |
594 | |
595 // Create the new list | |
596 while (sl_roster_elt) { | |
597 GSList *sl_roster_usrelt; | |
598 roster *roster_usrelt; | |
147 | 599 guint pending_group = FALSE; |
72 | 600 roster_elt = (roster*) sl_roster_elt->data; |
601 | |
602 // Add the group now unless hide_offline_buddies is set, | |
603 // in which case we'll add it only if an online buddy belongs to it. | |
147 | 604 // We take care to keep the current_buddy in the list, too. |
605 if (!hide_offline_buddies || roster_elt == roster_current_buddy) | |
72 | 606 buddylist = g_list_append(buddylist, roster_elt); |
607 else | |
147 | 608 pending_group = TRUE; |
72 | 609 |
128 | 610 shrunk_group = roster_elt->flags & ROSTER_FLAG_HIDE; |
611 | |
72 | 612 sl_roster_usrelt = roster_elt->list; |
613 while (sl_roster_usrelt) { | |
614 roster_usrelt = (roster*) sl_roster_usrelt->data; | |
615 | |
616 // Buddy will be added if either: | |
617 // - hide_offline_buddies is FALSE | |
618 // - buddy is not offline | |
619 // - buddy has a lock (for example the buddy window is currently open) | |
620 // - buddy has a pending (non-read) message | |
128 | 621 // - group isn't hidden (shrunk) |
147 | 622 // - this is the current_buddy |
623 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
|
624 (buddy_getstatus((gpointer)roster_usrelt, NULL) != offline) || |
72 | 625 (buddy_getflags((gpointer)roster_usrelt) & |
626 (ROSTER_FLAG_LOCK | ROSTER_FLAG_MSG))) { | |
627 // This user should be added. Maybe the group hasn't been added yet? | |
147 | 628 if (pending_group && |
629 (hide_offline_buddies || roster_usrelt == roster_current_buddy)) { | |
72 | 630 // It hasn't been done yet |
631 buddylist = g_list_append(buddylist, roster_elt); | |
632 pending_group = FALSE; | |
633 } | |
634 // Add user | |
128 | 635 // XXX Should we add the user if there is a message and |
636 // the group is shrunk? If so, we'd need to check LOCK flag too, | |
637 // perhaps... | |
638 if (!shrunk_group) | |
639 buddylist = g_list_append(buddylist, roster_usrelt); | |
72 | 640 } |
641 | |
642 sl_roster_usrelt = g_slist_next(sl_roster_usrelt); | |
643 } | |
644 sl_roster_elt = g_slist_next(sl_roster_elt); | |
645 } | |
78 | 646 |
81 | 647 // Check if we can find our saved current_buddy... |
648 if (roster_current_buddy) | |
649 current_buddy = g_list_find(buddylist, roster_current_buddy); | |
330 | 650 if (roster_alternate_buddy) |
651 alternate_buddy = g_list_find(buddylist, roster_alternate_buddy); | |
78 | 652 // current_buddy initialization |
653 if (!current_buddy || (g_list_position(buddylist, current_buddy) == -1)) | |
81 | 654 current_buddy = g_list_first(buddylist); |
128 | 655 // XXX Maybe we should set update_roster to TRUE there? |
72 | 656 } |
657 | |
658 // buddy_hide_group(roster, hide) | |
659 // "hide" values: 1=hide 0=show_all -1=invert | |
660 void buddy_hide_group(gpointer rosterdata, int hide) | |
661 { | |
207 | 662 roster *roster_usr = rosterdata; |
72 | 663 if (hide > 0) // TRUE (hide) |
207 | 664 roster_usr->flags |= ROSTER_FLAG_HIDE; |
72 | 665 else if (hide < 0) // NEG (invert) |
207 | 666 roster_usr->flags ^= ROSTER_FLAG_HIDE; |
72 | 667 else // FALSE (don't hide) |
207 | 668 roster_usr->flags &= ~ROSTER_FLAG_HIDE; |
72 | 669 } |
670 | |
671 const char *buddy_getjid(gpointer rosterdata) | |
672 { | |
207 | 673 roster *roster_usr = rosterdata; |
674 return roster_usr->jid; | |
675 } | |
676 | |
210 | 677 // buddy_setgroup() |
678 // Change the group of current buddy | |
679 // | |
680 // Warning! This function changes current_buddy! | |
681 // Warning! Old buddy is deleted, so you can't acces to its jid/name after | |
682 // calling this function. | |
683 void buddy_setgroup(gpointer rosterdata, char *newgroupname) | |
684 { | |
685 roster *roster_usr = rosterdata; | |
686 GSList **sl_group; | |
687 GSList *sl_clone; | |
688 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
|
689 int is_alternate; |
210 | 690 |
691 // A group has no group :) | |
692 if (roster_usr->type & ROSTER_TYPE_GROUP) return; | |
693 | |
694 // Remove the buddy from current group | |
695 sl_group = &((roster*)((GSList*)roster_usr->list)->data)->list; | |
696 *sl_group = g_slist_remove(*sl_group, rosterdata); | |
393 | 697 |
210 | 698 // Add the buddy to its new group; actually we "clone" this buddy... |
699 sl_clone = roster_add_user(roster_usr->jid, roster_usr->name, | |
700 newgroupname, roster_usr->type); | |
701 roster_clone = (roster*)sl_clone->data; | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
702 roster_clone->flags = roster_usr->flags; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
703 roster_clone->resource = roster_usr->resource; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
704 roster_usr->resource = NULL; |
210 | 705 |
706 // Free old buddy | |
221 | 707 if (roster_usr->jid) g_free((gchar*)roster_usr->jid); |
708 if (roster_usr->name) g_free((gchar*)roster_usr->name); | |
447
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
709 if (roster_usr->nickname) g_free((gchar*)roster_usr->nickname); |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
710 free_all_resources(&roster_usr->resource); |
210 | 711 g_free(roster_usr); |
712 | |
212 | 713 // If new new group is folded, the curren_buddy will be lost, and the |
714 // chat window won't be correctly refreshed. So we make sure it isn't... | |
715 ((roster*)((GSList*)roster_clone->list)->data)->flags &= ~ROSTER_FLAG_HIDE; | |
716 | |
717 // 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
|
718 is_alternate = (alternate_buddy == current_buddy); |
212 | 719 buddylist = g_list_append(buddylist, roster_clone); |
720 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
|
721 if (is_alternate) |
1510dda4ad71
Don't loose alternate_buddy when the buddy is moved ("/move")
Mikael Berthe <mikael@lilotux.net>
parents:
330
diff
changeset
|
722 alternate_buddy = current_buddy; |
212 | 723 |
210 | 724 buddylist_build(); |
725 } | |
726 | |
207 | 727 void buddy_setname(gpointer rosterdata, char *newname) |
728 { | |
729 roster *roster_usr = rosterdata; | |
209 | 730 GSList **sl_group; |
207 | 731 |
732 // TODO For groups, we need to check for unicity | |
733 // However, renaming a group boils down to moving all its buddies to | |
734 // another group, so calling this function is not really necessary... | |
735 if (roster_usr->type & ROSTER_TYPE_GROUP) return; | |
736 | |
737 if (roster_usr->name) { | |
738 g_free((gchar*)roster_usr->name); | |
739 roster_usr->name = NULL; | |
740 } | |
741 if (newname) | |
742 roster_usr->name = g_strdup(newname); | |
209 | 743 |
744 // We need to resort the group list | |
745 sl_group = &((roster*)((GSList*)roster_usr->list)->data)->list; | |
746 *sl_group = g_slist_sort(*sl_group, (GCompareFunc)&roster_compare_name); | |
747 | |
748 buddylist_build(); | |
72 | 749 } |
750 | |
751 const char *buddy_getname(gpointer rosterdata) | |
752 { | |
207 | 753 roster *roster_usr = rosterdata; |
754 return roster_usr->name; | |
755 } | |
756 | |
469
a926523d2392
Use UTF8 to handle resources and room nicknames
Mikael Berthe <mikael@lilotux.net>
parents:
468
diff
changeset
|
757 void buddy_setnickname(gpointer rosterdata, const char *newname) |
447
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
758 { |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
759 roster *roster_usr = rosterdata; |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
760 |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
761 if (!roster_usr->type & ROSTER_TYPE_ROOM) return; |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
762 |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
763 if (roster_usr->nickname) { |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
764 g_free((gchar*)roster_usr->nickname); |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
765 roster_usr->nickname = NULL; |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
766 } |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
767 if (newname) |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
768 roster_usr->nickname = g_strdup(newname); |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
769 } |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
770 |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
771 const char *buddy_getnickname(gpointer rosterdata) |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
772 { |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
773 roster *roster_usr = rosterdata; |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
774 return roster_usr->nickname; |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
775 } |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
776 |
207 | 777 // buddy_getgroupname() |
778 // Returns a pointer on buddy's group name. | |
779 const char *buddy_getgroupname(gpointer rosterdata) | |
780 { | |
781 roster *roster_usr = rosterdata; | |
782 | |
783 if (roster_usr->type & ROSTER_TYPE_GROUP) | |
784 return roster_usr->name; | |
785 | |
786 // This is a user | |
787 return ((roster*)((GSList*)roster_usr->list)->data)->name; | |
72 | 788 } |
789 | |
166 | 790 // buddy_getgroup() |
791 // Returns a pointer on buddy's group. | |
792 gpointer buddy_getgroup(gpointer rosterdata) | |
793 { | |
207 | 794 roster *roster_usr = rosterdata; |
166 | 795 |
207 | 796 if (roster_usr->type & ROSTER_TYPE_GROUP) |
166 | 797 return rosterdata; |
798 | |
799 // This is a user | |
207 | 800 return (gpointer)((GSList*)roster_usr->list)->data; |
166 | 801 } |
802 | |
72 | 803 guint buddy_gettype(gpointer rosterdata) |
804 { | |
207 | 805 roster *roster_usr = rosterdata; |
806 return roster_usr->type; | |
72 | 807 } |
808 | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
809 enum imstatus buddy_getstatus(gpointer rosterdata, const char *resname) |
72 | 810 { |
207 | 811 roster *roster_usr = rosterdata; |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
812 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
|
813 if (p_res) |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
814 return p_res->status; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
815 return offline; |
72 | 816 } |
817 | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
818 const char *buddy_getstatusmsg(gpointer rosterdata, const char *resname) |
221 | 819 { |
820 roster *roster_usr = rosterdata; | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
821 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
|
822 if (p_res) |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
823 return p_res->status_msg; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
824 return NULL; |
221 | 825 } |
826 | |
439
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
827 gchar buddy_getresourceprio(gpointer rosterdata, const char *resname) |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
828 { |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
829 roster *roster_usr = rosterdata; |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
830 res *p_res = get_resource(roster_usr, resname); |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
831 if (p_res) |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
832 return p_res->prio; |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
833 return 0; |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
834 } |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
835 |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
836 // buddy_getresources(roster_data) |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
837 // Return a singly-linked-list of resource names |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
838 // 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
|
839 GSList *buddy_getresources(gpointer rosterdata) |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
840 { |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
841 roster *roster_usr = rosterdata; |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
842 GSList *reslist = NULL, *lp; |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
843 |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
844 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
|
845 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
|
846 |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
847 return reslist; |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
848 } |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
849 |
468
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
850 // buddy_resource_setname(roster_data, oldname, newname) |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
851 // Useful for nickname change in a MUC room |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
852 void buddy_resource_setname(gpointer rosterdata, const char *resname, |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
853 const char *newname) |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
854 { |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
855 roster *roster_usr = rosterdata; |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
856 res *p_res = get_resource(roster_usr, resname); |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
857 if (p_res) { |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
858 if (p_res->name) { |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
859 g_free((gchar*)p_res->name); |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
860 p_res->name = NULL; |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
861 } |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
862 if (newname) |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
863 p_res->name = g_strdup(newname); |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
864 } |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
865 } |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
866 |
447
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
867 // buddy_del_all_resources() |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
868 // Remove all resources from the specified buddy |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
869 void buddy_del_all_resources(gpointer rosterdata) |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
870 { |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
871 roster *roster_usr = rosterdata; |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
872 |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
873 while (roster_usr->resource) { |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
874 res *r = roster_usr->resource->data; |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
875 del_resource(roster_usr, r->name); |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
876 } |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
877 } |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
878 |
119 | 879 // buddy_setflags() |
880 // Set one or several flags to value (TRUE/FALSE) | |
881 void buddy_setflags(gpointer rosterdata, guint flags, guint value) | |
882 { | |
207 | 883 roster *roster_usr = rosterdata; |
119 | 884 if (value) |
207 | 885 roster_usr->flags |= flags; |
119 | 886 else |
207 | 887 roster_usr->flags &= ~flags; |
119 | 888 } |
889 | |
72 | 890 guint buddy_getflags(gpointer rosterdata) |
891 { | |
207 | 892 roster *roster_usr = rosterdata; |
893 return roster_usr->flags; | |
72 | 894 } |
895 | |
480 | 896 // buddy_search_jid(jid) |
897 // Look for a buddy with specified jid. | |
898 // Search begins at buddylist; if no match is found in the the buddylist, | |
899 // return NULL; | |
900 GList *buddy_search_jid(char *jid) | |
901 { | |
902 GList *buddy; | |
903 roster *roster_usr; | |
904 | |
905 if (!buddylist) return NULL; | |
906 | |
907 for (buddy = buddylist; buddy; buddy = g_list_next(buddy)) { | |
908 roster_usr = (roster*)buddy->data; | |
909 if (roster_usr->jid && !strcasecmp(roster_usr->jid, jid)) | |
910 return buddy; | |
911 } | |
912 return NULL; | |
913 } | |
914 | |
265 | 915 // buddy_search(string) |
916 // Look for a buddy whose name or jid contains string. | |
917 // Search begins at current_buddy; if no match is found in the the buddylist, | |
918 // return NULL; | |
919 GList *buddy_search(char *string) | |
920 { | |
921 GList *buddy = current_buddy; | |
922 roster *roster_usr; | |
923 if (!buddylist || !current_buddy) return NULL; | |
924 for (;;) { | |
925 buddy = g_list_next(buddy); | |
926 if (!buddy) | |
927 buddy = buddylist; | |
928 | |
929 roster_usr = (roster*)buddy->data; | |
930 if (roster_usr->jid && strcasestr(roster_usr->jid, string)) | |
931 return buddy; | |
932 if (roster_usr->name && strcasestr(roster_usr->name, string)) | |
933 return buddy; | |
934 | |
935 if (buddy == current_buddy) | |
936 return NULL; // Back to the beginning, and no match found | |
937 } | |
938 } | |
939 | |
225 | 940 // compl_list(type) |
941 // Returns a list of jid's or groups. (For commands completion) | |
942 // type: ROSTER_TYPE_USER (jid's) or ROSTER_TYPE_GROUP (group names) | |
943 // The list should be freed by the caller after use. | |
944 GSList *compl_list(guint type) | |
945 { | |
946 GSList *list = NULL; | |
947 GList *buddy = buddylist; | |
948 | |
949 for ( ; buddy ; buddy = g_list_next(buddy)) { | |
950 guint btype = buddy_gettype(BUDDATA(buddy)); | |
951 | |
952 if (type == ROSTER_TYPE_GROUP) { // (group names) | |
953 if (btype == ROSTER_TYPE_GROUP) { | |
954 const char *bname = buddy_getname(BUDDATA(buddy)); | |
955 if ((bname) && (*bname)) | |
956 list = g_slist_append(list, g_strdup(bname)); | |
957 } | |
447
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
958 } else { // ROSTER_TYPE_USER (jid) (or agent, or chatroom...) |
225 | 959 const char *bjid = buddy_getjid(BUDDATA(buddy)); |
960 if (bjid) | |
961 list = g_slist_append(list, g_strdup(bjid)); | |
962 } | |
963 } | |
964 | |
965 return list; | |
966 } | |
967 | |
236 | 968 // unread_msg(rosterdata) |
969 // Return the next buddy with an unread message. If the parameter is NULL, | |
970 // return the first buddy with an unread message. | |
971 gpointer unread_msg(gpointer rosterdata) | |
972 { | |
973 GSList *unread, *next_unread; | |
974 | |
975 if (!unread_list) return NULL; | |
976 // First unread message | |
977 if (!rosterdata) return unread_list->data; | |
978 | |
979 unread = g_slist_find(unread_list, rosterdata); | |
980 if (!unread) return unread_list->data; | |
981 | |
982 next_unread = g_slist_next(unread); | |
983 if (next_unread) return next_unread->data; | |
984 | |
985 return unread_list->data; | |
986 } | |
987 |