Mercurial > hg
annotate mcabber/src/roster.c @ 520:c60175268eb5
Remove old comment
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Mon, 14 Nov 2005 22:15:25 +0100 |
parents | 6c3cd3aa4a91 |
children | 2ac8d8e49e81 |
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 | |
510
6c3cd3aa4a91
New 'buddy_format' option
Mikael Berthe <mikael@lilotux.net>
parents:
509
diff
changeset
|
492 const char *roster_getname(const char *jid) |
6c3cd3aa4a91
New 'buddy_format' option
Mikael Berthe <mikael@lilotux.net>
parents:
509
diff
changeset
|
493 { |
6c3cd3aa4a91
New 'buddy_format' option
Mikael Berthe <mikael@lilotux.net>
parents:
509
diff
changeset
|
494 GSList *sl_user; |
6c3cd3aa4a91
New 'buddy_format' option
Mikael Berthe <mikael@lilotux.net>
parents:
509
diff
changeset
|
495 roster *roster_usr; |
6c3cd3aa4a91
New 'buddy_format' option
Mikael Berthe <mikael@lilotux.net>
parents:
509
diff
changeset
|
496 |
6c3cd3aa4a91
New 'buddy_format' option
Mikael Berthe <mikael@lilotux.net>
parents:
509
diff
changeset
|
497 sl_user = roster_find(jid, jidsearch, |
6c3cd3aa4a91
New 'buddy_format' option
Mikael Berthe <mikael@lilotux.net>
parents:
509
diff
changeset
|
498 ROSTER_TYPE_USER|ROSTER_TYPE_ROOM|ROSTER_TYPE_AGENT); |
6c3cd3aa4a91
New 'buddy_format' option
Mikael Berthe <mikael@lilotux.net>
parents:
509
diff
changeset
|
499 if (sl_user == NULL) |
6c3cd3aa4a91
New 'buddy_format' option
Mikael Berthe <mikael@lilotux.net>
parents:
509
diff
changeset
|
500 return NULL; // Not in the roster... |
6c3cd3aa4a91
New 'buddy_format' option
Mikael Berthe <mikael@lilotux.net>
parents:
509
diff
changeset
|
501 |
6c3cd3aa4a91
New 'buddy_format' option
Mikael Berthe <mikael@lilotux.net>
parents:
509
diff
changeset
|
502 roster_usr = (roster*)sl_user->data; |
6c3cd3aa4a91
New 'buddy_format' option
Mikael Berthe <mikael@lilotux.net>
parents:
509
diff
changeset
|
503 return roster_usr->name; |
6c3cd3aa4a91
New 'buddy_format' option
Mikael Berthe <mikael@lilotux.net>
parents:
509
diff
changeset
|
504 } |
6c3cd3aa4a91
New 'buddy_format' option
Mikael Berthe <mikael@lilotux.net>
parents:
509
diff
changeset
|
505 |
89 | 506 void roster_settype(const char *jid, guint type) |
507 { | |
508 GSList *sl_user; | |
509 roster *roster_usr; | |
510 | |
511 if ((sl_user = roster_find(jid, jidsearch, 0)) == NULL) | |
512 return; | |
513 | |
514 roster_usr = (roster*)sl_user->data; | |
515 roster_usr->type = type; | |
516 } | |
517 | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
518 enum imstatus roster_getstatus(const char *jid, const char *resname) |
90 | 519 { |
520 GSList *sl_user; | |
521 roster *roster_usr; | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
522 res *p_res; |
90 | 523 |
524 sl_user = roster_find(jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT); | |
525 if (sl_user == NULL) | |
526 return offline; // Not in the roster, anyway... | |
527 | |
528 roster_usr = (roster*)sl_user->data; | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
529 p_res = get_resource(roster_usr, resname); |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
530 if (p_res) |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
531 return p_res->status; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
532 return offline; |
90 | 533 } |
534 | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
535 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
|
536 { |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
537 GSList *sl_user; |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
538 roster *roster_usr; |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
539 res *p_res; |
318
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
540 |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
541 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
|
542 if (sl_user == NULL) |
425
03f1e37759a6
roster_getstatusmsg() should return a pointer
Mikael Berthe <mikael@lilotux.net>
parents:
393
diff
changeset
|
543 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
|
544 |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
545 roster_usr = (roster*)sl_user->data; |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
546 p_res = get_resource(roster_usr, resname); |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
547 if (p_res) |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
548 return p_res->status_msg; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
549 return NULL; |
318
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
550 } |
45076d02eeef
The /status command can specify a status message
Mikael Berthe <mikael@lilotux.net>
parents:
301
diff
changeset
|
551 |
89 | 552 guint roster_gettype(const char *jid) |
553 { | |
554 GSList *sl_user; | |
555 roster *roster_usr; | |
556 | |
557 if ((sl_user = roster_find(jid, jidsearch, 0)) == NULL) | |
558 return 0; | |
559 | |
560 roster_usr = (roster*)sl_user->data; | |
561 return roster_usr->type; | |
562 } | |
563 | |
72 | 564 |
565 /* ### BuddyList functions ### */ | |
566 | |
120 | 567 // buddylist_set_hide_offline_buddies(hide) |
72 | 568 // "hide" values: 1=hide 0=show_all -1=invert |
120 | 569 void buddylist_set_hide_offline_buddies(int hide) |
72 | 570 { |
571 if (hide < 0) // NEG (invert) | |
572 hide_offline_buddies = !hide_offline_buddies; | |
573 else if (hide == 0) // FALSE (don't hide) | |
574 hide_offline_buddies = 0; | |
575 else // TRUE (hide) | |
576 hide_offline_buddies = 1; | |
577 } | |
578 | |
120 | 579 inline int buddylist_get_hide_offline_buddies(void) |
580 { | |
581 return hide_offline_buddies; | |
582 } | |
583 | |
72 | 584 // buddylist_build() |
585 // Creates the buddylist from the roster entries. | |
586 void buddylist_build(void) | |
587 { | |
588 GSList *sl_roster_elt = groups; | |
589 roster *roster_elt; | |
81 | 590 roster *roster_current_buddy = NULL; |
330 | 591 roster *roster_alternate_buddy = NULL; |
128 | 592 int shrunk_group; |
72 | 593 |
81 | 594 // We need to remember which buddy is selected. |
595 if (current_buddy) | |
596 roster_current_buddy = BUDDATA(current_buddy); | |
597 current_buddy = NULL; | |
330 | 598 if (alternate_buddy) |
599 roster_alternate_buddy = BUDDATA(alternate_buddy); | |
600 alternate_buddy = NULL; | |
81 | 601 |
72 | 602 // Destroy old buddylist |
603 if (buddylist) { | |
604 g_list_free(buddylist); | |
605 buddylist = NULL; | |
606 } | |
607 | |
608 // Create the new list | |
609 while (sl_roster_elt) { | |
610 GSList *sl_roster_usrelt; | |
611 roster *roster_usrelt; | |
147 | 612 guint pending_group = FALSE; |
72 | 613 roster_elt = (roster*) sl_roster_elt->data; |
614 | |
615 // Add the group now unless hide_offline_buddies is set, | |
616 // in which case we'll add it only if an online buddy belongs to it. | |
147 | 617 // We take care to keep the current_buddy in the list, too. |
618 if (!hide_offline_buddies || roster_elt == roster_current_buddy) | |
72 | 619 buddylist = g_list_append(buddylist, roster_elt); |
620 else | |
147 | 621 pending_group = TRUE; |
72 | 622 |
128 | 623 shrunk_group = roster_elt->flags & ROSTER_FLAG_HIDE; |
624 | |
72 | 625 sl_roster_usrelt = roster_elt->list; |
626 while (sl_roster_usrelt) { | |
627 roster_usrelt = (roster*) sl_roster_usrelt->data; | |
628 | |
629 // Buddy will be added if either: | |
630 // - hide_offline_buddies is FALSE | |
631 // - buddy is not offline | |
632 // - buddy has a lock (for example the buddy window is currently open) | |
633 // - buddy has a pending (non-read) message | |
128 | 634 // - group isn't hidden (shrunk) |
147 | 635 // - this is the current_buddy |
636 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
|
637 (buddy_getstatus((gpointer)roster_usrelt, NULL) != offline) || |
72 | 638 (buddy_getflags((gpointer)roster_usrelt) & |
639 (ROSTER_FLAG_LOCK | ROSTER_FLAG_MSG))) { | |
640 // This user should be added. Maybe the group hasn't been added yet? | |
147 | 641 if (pending_group && |
642 (hide_offline_buddies || roster_usrelt == roster_current_buddy)) { | |
72 | 643 // It hasn't been done yet |
644 buddylist = g_list_append(buddylist, roster_elt); | |
645 pending_group = FALSE; | |
646 } | |
647 // Add user | |
128 | 648 // XXX Should we add the user if there is a message and |
649 // the group is shrunk? If so, we'd need to check LOCK flag too, | |
650 // perhaps... | |
651 if (!shrunk_group) | |
652 buddylist = g_list_append(buddylist, roster_usrelt); | |
72 | 653 } |
654 | |
655 sl_roster_usrelt = g_slist_next(sl_roster_usrelt); | |
656 } | |
657 sl_roster_elt = g_slist_next(sl_roster_elt); | |
658 } | |
78 | 659 |
81 | 660 // Check if we can find our saved current_buddy... |
661 if (roster_current_buddy) | |
662 current_buddy = g_list_find(buddylist, roster_current_buddy); | |
330 | 663 if (roster_alternate_buddy) |
664 alternate_buddy = g_list_find(buddylist, roster_alternate_buddy); | |
78 | 665 // current_buddy initialization |
666 if (!current_buddy || (g_list_position(buddylist, current_buddy) == -1)) | |
81 | 667 current_buddy = g_list_first(buddylist); |
72 | 668 } |
669 | |
670 // buddy_hide_group(roster, hide) | |
671 // "hide" values: 1=hide 0=show_all -1=invert | |
672 void buddy_hide_group(gpointer rosterdata, int hide) | |
673 { | |
207 | 674 roster *roster_usr = rosterdata; |
72 | 675 if (hide > 0) // TRUE (hide) |
207 | 676 roster_usr->flags |= ROSTER_FLAG_HIDE; |
72 | 677 else if (hide < 0) // NEG (invert) |
207 | 678 roster_usr->flags ^= ROSTER_FLAG_HIDE; |
72 | 679 else // FALSE (don't hide) |
207 | 680 roster_usr->flags &= ~ROSTER_FLAG_HIDE; |
72 | 681 } |
682 | |
683 const char *buddy_getjid(gpointer rosterdata) | |
684 { | |
207 | 685 roster *roster_usr = rosterdata; |
686 return roster_usr->jid; | |
687 } | |
688 | |
210 | 689 // buddy_setgroup() |
690 // Change the group of current buddy | |
691 // | |
506
74dcd0df532e
"/move" does not jump to the buddy in the new group
Mikael Berthe <mikael@lilotux.net>
parents:
501
diff
changeset
|
692 // Warning! This function changes the specified buddy! |
74dcd0df532e
"/move" does not jump to the buddy in the new group
Mikael Berthe <mikael@lilotux.net>
parents:
501
diff
changeset
|
693 // Warning! Old buddy is deleted, so you can't access to its jid/name after |
74dcd0df532e
"/move" does not jump to the buddy in the new group
Mikael Berthe <mikael@lilotux.net>
parents:
501
diff
changeset
|
694 // calling this function (they are free'd). |
210 | 695 void buddy_setgroup(gpointer rosterdata, char *newgroupname) |
696 { | |
697 roster *roster_usr = rosterdata; | |
698 GSList **sl_group; | |
699 GSList *sl_clone; | |
700 roster *roster_clone; | |
701 | |
702 // A group has no group :) | |
703 if (roster_usr->type & ROSTER_TYPE_GROUP) return; | |
704 | |
705 // Remove the buddy from current group | |
706 sl_group = &((roster*)((GSList*)roster_usr->list)->data)->list; | |
707 *sl_group = g_slist_remove(*sl_group, rosterdata); | |
393 | 708 |
210 | 709 // Add the buddy to its new group; actually we "clone" this buddy... |
710 sl_clone = roster_add_user(roster_usr->jid, roster_usr->name, | |
711 newgroupname, roster_usr->type); | |
712 roster_clone = (roster*)sl_clone->data; | |
509
cd52ddef5ce1
Fix a bug when moving a chatroom to a new group
Mikael Berthe <mikael@lilotux.net>
parents:
506
diff
changeset
|
713 roster_clone->subscription = roster_usr->subscription; |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
714 roster_clone->flags = roster_usr->flags; |
509
cd52ddef5ce1
Fix a bug when moving a chatroom to a new group
Mikael Berthe <mikael@lilotux.net>
parents:
506
diff
changeset
|
715 |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
716 roster_clone->resource = roster_usr->resource; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
717 roster_usr->resource = NULL; |
509
cd52ddef5ce1
Fix a bug when moving a chatroom to a new group
Mikael Berthe <mikael@lilotux.net>
parents:
506
diff
changeset
|
718 roster_clone->nickname = roster_usr->nickname; |
cd52ddef5ce1
Fix a bug when moving a chatroom to a new group
Mikael Berthe <mikael@lilotux.net>
parents:
506
diff
changeset
|
719 roster_usr->nickname = NULL; |
210 | 720 |
721 // Free old buddy | |
221 | 722 if (roster_usr->jid) g_free((gchar*)roster_usr->jid); |
723 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
|
724 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
|
725 free_all_resources(&roster_usr->resource); |
210 | 726 g_free(roster_usr); |
727 | |
212 | 728 buddylist = g_list_append(buddylist, roster_clone); |
506
74dcd0df532e
"/move" does not jump to the buddy in the new group
Mikael Berthe <mikael@lilotux.net>
parents:
501
diff
changeset
|
729 // We must have current_buddy pointing to the cloned buddy, if this is |
74dcd0df532e
"/move" does not jump to the buddy in the new group
Mikael Berthe <mikael@lilotux.net>
parents:
501
diff
changeset
|
730 // the one we have moved. Same for alternate_buddy. |
74dcd0df532e
"/move" does not jump to the buddy in the new group
Mikael Berthe <mikael@lilotux.net>
parents:
501
diff
changeset
|
731 if (rosterdata == BUDDATA(current_buddy)) { |
74dcd0df532e
"/move" does not jump to the buddy in the new group
Mikael Berthe <mikael@lilotux.net>
parents:
501
diff
changeset
|
732 current_buddy = g_list_find(buddylist, roster_clone); |
74dcd0df532e
"/move" does not jump to the buddy in the new group
Mikael Berthe <mikael@lilotux.net>
parents:
501
diff
changeset
|
733 // If new new group is folded, the current_buddy will be lost, and the |
74dcd0df532e
"/move" does not jump to the buddy in the new group
Mikael Berthe <mikael@lilotux.net>
parents:
501
diff
changeset
|
734 // chat window won't be correctly refreshed. So we make sure it isn't... |
74dcd0df532e
"/move" does not jump to the buddy in the new group
Mikael Berthe <mikael@lilotux.net>
parents:
501
diff
changeset
|
735 ((roster*)((GSList*)roster_clone->list)->data)->flags &= ~ROSTER_FLAG_HIDE; |
74dcd0df532e
"/move" does not jump to the buddy in the new group
Mikael Berthe <mikael@lilotux.net>
parents:
501
diff
changeset
|
736 } |
74dcd0df532e
"/move" does not jump to the buddy in the new group
Mikael Berthe <mikael@lilotux.net>
parents:
501
diff
changeset
|
737 if (alternate_buddy && BUDDATA(alternate_buddy) == rosterdata) |
74dcd0df532e
"/move" does not jump to the buddy in the new group
Mikael Berthe <mikael@lilotux.net>
parents:
501
diff
changeset
|
738 alternate_buddy = g_list_find(buddylist, roster_clone); |
212 | 739 |
210 | 740 buddylist_build(); |
741 } | |
742 | |
207 | 743 void buddy_setname(gpointer rosterdata, char *newname) |
744 { | |
745 roster *roster_usr = rosterdata; | |
209 | 746 GSList **sl_group; |
207 | 747 |
748 // TODO For groups, we need to check for unicity | |
749 // However, renaming a group boils down to moving all its buddies to | |
750 // another group, so calling this function is not really necessary... | |
751 if (roster_usr->type & ROSTER_TYPE_GROUP) return; | |
752 | |
753 if (roster_usr->name) { | |
754 g_free((gchar*)roster_usr->name); | |
755 roster_usr->name = NULL; | |
756 } | |
757 if (newname) | |
758 roster_usr->name = g_strdup(newname); | |
209 | 759 |
760 // We need to resort the group list | |
761 sl_group = &((roster*)((GSList*)roster_usr->list)->data)->list; | |
762 *sl_group = g_slist_sort(*sl_group, (GCompareFunc)&roster_compare_name); | |
763 | |
764 buddylist_build(); | |
72 | 765 } |
766 | |
767 const char *buddy_getname(gpointer rosterdata) | |
768 { | |
207 | 769 roster *roster_usr = rosterdata; |
770 return roster_usr->name; | |
771 } | |
772 | |
469
a926523d2392
Use UTF8 to handle resources and room nicknames
Mikael Berthe <mikael@lilotux.net>
parents:
468
diff
changeset
|
773 void buddy_setnickname(gpointer rosterdata, const char *newname) |
447
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 roster *roster_usr = rosterdata; |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
776 |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
777 if (!roster_usr->type & ROSTER_TYPE_ROOM) return; |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
778 |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
779 if (roster_usr->nickname) { |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
780 g_free((gchar*)roster_usr->nickname); |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
781 roster_usr->nickname = NULL; |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
782 } |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
783 if (newname) |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
784 roster_usr->nickname = g_strdup(newname); |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
785 } |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
786 |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
787 const char *buddy_getnickname(gpointer rosterdata) |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
788 { |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
789 roster *roster_usr = rosterdata; |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
790 return roster_usr->nickname; |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
791 } |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
792 |
207 | 793 // buddy_getgroupname() |
794 // Returns a pointer on buddy's group name. | |
795 const char *buddy_getgroupname(gpointer rosterdata) | |
796 { | |
797 roster *roster_usr = rosterdata; | |
798 | |
799 if (roster_usr->type & ROSTER_TYPE_GROUP) | |
800 return roster_usr->name; | |
801 | |
802 // This is a user | |
803 return ((roster*)((GSList*)roster_usr->list)->data)->name; | |
72 | 804 } |
805 | |
166 | 806 // buddy_getgroup() |
807 // Returns a pointer on buddy's group. | |
808 gpointer buddy_getgroup(gpointer rosterdata) | |
809 { | |
207 | 810 roster *roster_usr = rosterdata; |
166 | 811 |
207 | 812 if (roster_usr->type & ROSTER_TYPE_GROUP) |
166 | 813 return rosterdata; |
814 | |
815 // This is a user | |
207 | 816 return (gpointer)((GSList*)roster_usr->list)->data; |
166 | 817 } |
818 | |
489
21ab22a60bcb
Auto-convert a roster item to "room" type when receiving a groupchat message
Mikael Berthe <mikael@lilotux.net>
parents:
483
diff
changeset
|
819 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
|
820 { |
21ab22a60bcb
Auto-convert a roster item to "room" type when receiving a groupchat message
Mikael Berthe <mikael@lilotux.net>
parents:
483
diff
changeset
|
821 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
|
822 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
|
823 } |
21ab22a60bcb
Auto-convert a roster item to "room" type when receiving a groupchat message
Mikael Berthe <mikael@lilotux.net>
parents:
483
diff
changeset
|
824 |
72 | 825 guint buddy_gettype(gpointer rosterdata) |
826 { | |
207 | 827 roster *roster_usr = rosterdata; |
828 return roster_usr->type; | |
72 | 829 } |
830 | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
831 enum imstatus buddy_getstatus(gpointer rosterdata, const char *resname) |
72 | 832 { |
207 | 833 roster *roster_usr = rosterdata; |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
834 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
|
835 if (p_res) |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
836 return p_res->status; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
837 return offline; |
72 | 838 } |
839 | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
840 const char *buddy_getstatusmsg(gpointer rosterdata, const char *resname) |
221 | 841 { |
842 roster *roster_usr = rosterdata; | |
438
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
843 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
|
844 if (p_res) |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
845 return p_res->status_msg; |
b44be19d6229
Handle multiple resources for the same buddy
Mikael Berthe <mikael@lilotux.net>
parents:
425
diff
changeset
|
846 return NULL; |
221 | 847 } |
848 | |
439
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
849 gchar buddy_getresourceprio(gpointer rosterdata, const char *resname) |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
850 { |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
851 roster *roster_usr = rosterdata; |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
852 res *p_res = get_resource(roster_usr, resname); |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
853 if (p_res) |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
854 return p_res->prio; |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
855 return 0; |
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 |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
858 // buddy_getresources(roster_data) |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
859 // Return a singly-linked-list of resource names |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
860 // 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
|
861 // 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
|
862 GSList *buddy_getresources(gpointer rosterdata) |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
863 { |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
864 roster *roster_usr = rosterdata; |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
865 GSList *reslist = NULL, *lp; |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
866 |
501
7c1ca00070e8
Add COMPL_RESOURCE (resource completion category)
Mikael Berthe <mikael@lilotux.net>
parents:
494
diff
changeset
|
867 if (!roster_usr) { |
7c1ca00070e8
Add COMPL_RESOURCE (resource completion category)
Mikael Berthe <mikael@lilotux.net>
parents:
494
diff
changeset
|
868 if (!current_buddy) return NULL; |
7c1ca00070e8
Add COMPL_RESOURCE (resource completion category)
Mikael Berthe <mikael@lilotux.net>
parents:
494
diff
changeset
|
869 roster_usr = BUDDATA(current_buddy); |
7c1ca00070e8
Add COMPL_RESOURCE (resource completion category)
Mikael Berthe <mikael@lilotux.net>
parents:
494
diff
changeset
|
870 } |
439
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
871 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
|
872 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
|
873 |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
874 return reslist; |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
875 } |
63562fd409a1
/info can show multiple resources
Mikael Berthe <mikael@lilotux.net>
parents:
438
diff
changeset
|
876 |
468
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
877 // buddy_resource_setname(roster_data, oldname, newname) |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
878 // Useful for nickname change in a MUC room |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
879 void buddy_resource_setname(gpointer rosterdata, const char *resname, |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
880 const char *newname) |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
881 { |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
882 roster *roster_usr = rosterdata; |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
883 res *p_res = get_resource(roster_usr, resname); |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
884 if (p_res) { |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
885 if (p_res->name) { |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
886 g_free((gchar*)p_res->name); |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
887 p_res->name = NULL; |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
888 } |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
889 if (newname) |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
890 p_res->name = g_strdup(newname); |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
891 } |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
892 } |
644b8bf9ca4d
Improve MUC presence messages handling
Mikael Berthe <mikael@lilotux.net>
parents:
447
diff
changeset
|
893 |
447
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
894 // buddy_del_all_resources() |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
895 // Remove all resources from the specified buddy |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
896 void buddy_del_all_resources(gpointer rosterdata) |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
897 { |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
898 roster *roster_usr = rosterdata; |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
899 |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
900 while (roster_usr->resource) { |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
901 res *r = roster_usr->resource->data; |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
902 del_resource(roster_usr, r->name); |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
903 } |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
904 } |
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
905 |
119 | 906 // buddy_setflags() |
907 // Set one or several flags to value (TRUE/FALSE) | |
908 void buddy_setflags(gpointer rosterdata, guint flags, guint value) | |
909 { | |
207 | 910 roster *roster_usr = rosterdata; |
119 | 911 if (value) |
207 | 912 roster_usr->flags |= flags; |
119 | 913 else |
207 | 914 roster_usr->flags &= ~flags; |
119 | 915 } |
916 | |
72 | 917 guint buddy_getflags(gpointer rosterdata) |
918 { | |
207 | 919 roster *roster_usr = rosterdata; |
920 return roster_usr->flags; | |
72 | 921 } |
922 | |
480 | 923 // buddy_search_jid(jid) |
924 // Look for a buddy with specified jid. | |
925 // Search begins at buddylist; if no match is found in the the buddylist, | |
926 // return NULL; | |
927 GList *buddy_search_jid(char *jid) | |
928 { | |
929 GList *buddy; | |
930 roster *roster_usr; | |
931 | |
932 if (!buddylist) return NULL; | |
933 | |
934 for (buddy = buddylist; buddy; buddy = g_list_next(buddy)) { | |
935 roster_usr = (roster*)buddy->data; | |
936 if (roster_usr->jid && !strcasecmp(roster_usr->jid, jid)) | |
937 return buddy; | |
938 } | |
939 return NULL; | |
940 } | |
941 | |
265 | 942 // buddy_search(string) |
943 // Look for a buddy whose name or jid contains string. | |
944 // Search begins at current_buddy; if no match is found in the the buddylist, | |
945 // return NULL; | |
946 GList *buddy_search(char *string) | |
947 { | |
948 GList *buddy = current_buddy; | |
949 roster *roster_usr; | |
950 if (!buddylist || !current_buddy) return NULL; | |
951 for (;;) { | |
952 buddy = g_list_next(buddy); | |
953 if (!buddy) | |
954 buddy = buddylist; | |
955 | |
956 roster_usr = (roster*)buddy->data; | |
957 if (roster_usr->jid && strcasestr(roster_usr->jid, string)) | |
958 return buddy; | |
959 if (roster_usr->name && strcasestr(roster_usr->name, string)) | |
960 return buddy; | |
961 | |
962 if (buddy == current_buddy) | |
963 return NULL; // Back to the beginning, and no match found | |
964 } | |
965 } | |
966 | |
225 | 967 // compl_list(type) |
968 // Returns a list of jid's or groups. (For commands completion) | |
969 // type: ROSTER_TYPE_USER (jid's) or ROSTER_TYPE_GROUP (group names) | |
970 // The list should be freed by the caller after use. | |
971 GSList *compl_list(guint type) | |
972 { | |
973 GSList *list = NULL; | |
974 GList *buddy = buddylist; | |
975 | |
976 for ( ; buddy ; buddy = g_list_next(buddy)) { | |
977 guint btype = buddy_gettype(BUDDATA(buddy)); | |
978 | |
979 if (type == ROSTER_TYPE_GROUP) { // (group names) | |
980 if (btype == ROSTER_TYPE_GROUP) { | |
981 const char *bname = buddy_getname(BUDDATA(buddy)); | |
982 if ((bname) && (*bname)) | |
983 list = g_slist_append(list, g_strdup(bname)); | |
984 } | |
447
03bb57383cea
Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents:
439
diff
changeset
|
985 } else { // ROSTER_TYPE_USER (jid) (or agent, or chatroom...) |
225 | 986 const char *bjid = buddy_getjid(BUDDATA(buddy)); |
987 if (bjid) | |
988 list = g_slist_append(list, g_strdup(bjid)); | |
989 } | |
990 } | |
991 | |
992 return list; | |
993 } | |
994 | |
236 | 995 // unread_msg(rosterdata) |
996 // Return the next buddy with an unread message. If the parameter is NULL, | |
997 // return the first buddy with an unread message. | |
998 gpointer unread_msg(gpointer rosterdata) | |
999 { | |
1000 GSList *unread, *next_unread; | |
1001 | |
1002 if (!unread_list) return NULL; | |
1003 // First unread message | |
1004 if (!rosterdata) return unread_list->data; | |
1005 | |
1006 unread = g_slist_find(unread_list, rosterdata); | |
1007 if (!unread) return unread_list->data; | |
1008 | |
1009 next_unread = g_slist_next(unread); | |
1010 if (next_unread) return next_unread->data; | |
1011 | |
1012 return unread_list->data; | |
1013 } | |
1014 |