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