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