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