Update documentation of thrasher_account_uiops.
[thrasher.git] / thblist.c
blobcca98e466c5f4fb121fe4981ba03db5ee7f6a985
1 /*
2 * Thrasher Bird - XMPP transport via libpurple
3 * Copyright (C) 2008 Barracuda Networks, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with Thrasher Bird; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
19 #include <stdlib.h>
20 #include "blist.h"
21 #include "thperl.h"
22 #include "thrasher.h"
23 #include "thblist.h"
24 #include "status.h"
26 #include <server.h>
27 #include <account.h>
29 void thrasher_buddy_status_changed (PurpleBuddy *buddy, PurpleStatus *old,
30 PurpleStatus *new);
31 void thrasher_buddy_signed_on (PurpleBuddy *buddy);
32 void thrasher_buddy_signed_off (PurpleBuddy *buddy);
33 void thrasher_buddy_added (PurpleBuddy *buddy);
34 void thrasher_buddy_add_request (PurpleAccount *account, const char *remote_user,
35 const char *id, const char *alias,
36 const char *message);
37 static void * thrasher_buddy_request_authorize
38 (PurpleAccount *account,
39 const char *remote_user,
40 const char *id,
41 const char *alias,
42 const char *message,
43 gboolean on_list,
44 PurpleAccountRequestAuthorizationCb authorize_cb,
45 PurpleAccountRequestAuthorizationCb deny_cb,
46 void *user_data);
47 void thrasher_buddy_removed (PurpleBuddy *buddy);
48 static gpointer thrasher_blist_get_handle (void);
50 static PurpleAccountUiOps thrasher_account_uiops =
52 // notify added
53 thrasher_buddy_add_request,
55 // status changed
56 NULL,
58 // request add
59 thrasher_buddy_add_request,
61 // request_authorize
62 thrasher_buddy_request_authorize,
64 // close_account_request
65 NULL,
66 // save_node
67 NULL,
68 // remove_node
69 NULL,
70 // save_account
71 NULL,
72 // Reserved
73 NULL
76 struct _PurpleStatusType
78 PurpleStatusPrimitive primitive;
80 char *id;
81 char *name;
82 char *primary_attr_id;
84 gboolean saveable;
85 gboolean user_settable;
86 gboolean independent;
88 GList *attrs;
92 struct _PurpleStatus
94 struct _PurpleStatusType *type;
95 PurplePresence *presence;
97 const char *title;
99 gboolean active;
101 GHashTable *attr_values;
105 * @brief This is the callback for the buddy-status-changed signal
106 * @param buddy PurpleBuddy struct
107 * @param old (unused)
108 * @param new @buddy new PurpleStatus
110 void thrasher_buddy_status_changed (PurpleBuddy *buddy,
111 PurpleStatus *old,
112 PurpleStatus *new)
114 PurpleGroup *group;
115 const char *group_name = NULL;
116 const char *message = NULL;
117 const char *alias = NULL;
119 g_return_if_fail(buddy != NULL);
120 g_return_if_fail(new != NULL);
122 group = purple_buddy_get_group(buddy);
124 if (group)
125 group_name = purple_group_get_name(group);
127 alias = purple_buddy_get_alias_only(buddy);
129 message = purple_status_get_attr_string(new, "message");
131 thrasher_wrapper_presence_in(thrasher_account_get_jid(buddy->account),
132 purple_buddy_get_name(buddy),
133 alias,
134 group_name,
135 purple_status_type_get_primitive(new->type),
136 message);
139 void thrasher_buddy_authorize (const char *jid,
140 const char *legacy_username) {
141 g_return_if_fail(jid);
142 g_return_if_fail(legacy_username);
144 PurpleAccount *account = thrasher_get_account_by_jid(jid);
145 g_return_if_fail(account);
147 PurpleConnection *gc =
148 thrasher_get_connection_by_account(account);
149 g_return_if_fail(gc);
151 purple_debug_info("thrasher", "Authorizing %s for jid %s\n",
152 legacy_username, jid);
154 // defensive; probably only add_permit is necesary
155 serv_rem_deny(gc, legacy_username);
157 serv_add_permit(gc, legacy_username);
160 void thrasher_buddy_deauthorize (const char *jid,
161 const char *legacy_username) {
162 g_return_if_fail(jid);
163 g_return_if_fail(legacy_username);
165 PurpleAccount *account = thrasher_get_account_by_jid(jid);
166 g_return_if_fail(account);
168 PurpleConnection *gc =
169 thrasher_get_connection_by_account(account);
170 g_return_if_fail(gc);
172 purple_debug_info("thrasher", "Deauthorizing %s for jid %s\n",
173 legacy_username, jid);
175 // no corresponding deny, we don't have a reperesentation for that
176 serv_rem_permit(gc, legacy_username);
179 void thrasher_buddy_signed_on (PurpleBuddy *buddy)
181 PurplePresence *presence;
183 presence = purple_buddy_get_presence(buddy);
185 if (!presence)
186 return;
188 // libpurple uses this to populate some stuff about the
189 // buddy, particularly useful for XMPP file transfers but
190 // probably the cause of random other errors if we don't fill this
191 // out.
192 PurpleAccount* account = buddy->account;
193 g_return_if_fail(account);
194 PurpleConnection* connection = thrasher_connection_for_account(account);
195 g_return_if_fail(connection);
197 purple_debug_info("thblist",
198 "getting server info for %s\n\n---------\n\n",
199 buddy->name);
200 serv_get_info(connection, buddy->name);
202 /* Currently active status may be PURPLE_STATUS_UNAVAILABLE
203 * (translates XMPP dnd) instead of just "available".
205 PurpleStatus *status = purple_presence_get_active_status(presence);
207 thrasher_buddy_status_changed(buddy,
208 NULL,
209 status);
212 void thrasher_buddy_signed_off (PurpleBuddy *buddy)
214 PurplePresence *presence;
216 presence = purple_buddy_get_presence(buddy);
218 if (!presence)
219 return;
221 thrasher_buddy_status_changed(buddy,
222 NULL,
223 purple_presence_get_status(presence, "offline"));
226 static void * thrasher_buddy_request_authorize
227 (PurpleAccount *account,
228 const char *remote_user,
229 const char *id,
230 const char *alias,
231 const char *message,
232 gboolean on_list,
233 PurpleAccountRequestAuthorizationCb authorize_cb,
234 PurpleAccountRequestAuthorizationCb deny_cb,
235 void *user_data) {
236 // authorize_cb sometimes NULL?!?
237 purple_debug_info("thrasher", "request authorize, empty cb: %s\n",
238 authorize_cb ? "FALSE" : "TRUE");
239 thrasher_buddy_add_request(account, remote_user, "", alias, message);
241 if (authorize_cb) {
242 authorize_cb(user_data);
245 void *uihandle = NULL;
246 return uihandle;
249 /* thrasher_buddy_add_request
251 * Triggered on a buddy-added signal, this allows us to push new subscriptions.
254 void thrasher_buddy_added (PurpleBuddy *buddy) {
255 PurplePresence *presence = purple_buddy_get_presence(buddy);
256 PurpleStatus *status = purple_presence_get_active_status(presence);
257 PurpleStatusType *type = purple_status_get_type(status);
259 const char *jid = thrasher_account_get_jid(buddy->account);
260 const char *sender = purple_buddy_get_name(buddy);
261 const guint status_int = purple_status_type_get_primitive(type);
263 purple_debug_info("thrasher",
264 "buddy added to %s: %s\n",
265 jid, sender);
267 thrasher_wrapper_subscription_add(jid, sender, status_int);
271 void thrasher_buddy_add_request (PurpleAccount *account, const char *remote_user,
272 const char *id, const char *alias,
273 const char *message)
275 const char *jid = thrasher_account_get_jid(account);
276 purple_debug_info("thrasher",
277 "legacy user %s aka %s added %s to roster\n",
278 remote_user, alias, jid);
279 thrasher_wrapper_legacy_user_add_user(jid, remote_user);
283 void thrasher_buddy_removed (PurpleBuddy *buddy)
285 /* printf("Removed buddy:\t[%s]\t[%s]\n", purple_buddy_get_name(buddy),
286 * purple_status_get_name( purple_presence_get_active_status( purple_buddy_get_presence(buddy) ) )
287 * );
289 PurplePresence *presence = purple_buddy_get_presence(buddy);
290 PurpleStatus *status = purple_presence_get_active_status(presence);
291 PurpleStatusType *type = purple_status_get_type(status);
292 PurpleGroup *group = purple_buddy_get_group(buddy);
294 * Need to fire jid, sender, group_name, alias, and status back to perl */
296 // jid = thrasher_account_get_jid(buddy->account)
297 // sender = purple_buddy_get_name(buddy)
298 // group_name = purple_group_get_name(group)
299 // alias = purple_buddy_get_alias_only(buddy)
300 // status = purple_status_type_get_primitive(type)
304 static gpointer
305 thrasher_blist_get_handle ()
307 static int handle;
308 return &handle;
312 void thrasher_blist_init()
314 purple_accounts_set_ui_ops(&thrasher_account_uiops);
316 purple_signal_connect(purple_blist_get_handle(),
317 "buddy-status-changed",
318 thrasher_blist_get_handle(),
319 PURPLE_CALLBACK(thrasher_buddy_status_changed),
320 NULL);
322 purple_signal_connect(purple_blist_get_handle(),
323 "buddy-signed-on",
324 thrasher_blist_get_handle(),
325 PURPLE_CALLBACK(thrasher_buddy_signed_on),
326 NULL);
328 purple_signal_connect(purple_blist_get_handle(),
329 "buddy-signed-off",
330 thrasher_blist_get_handle(),
331 PURPLE_CALLBACK(thrasher_buddy_signed_off),
332 NULL);
334 // It looks like this only comes in for things already on our roster.
335 purple_signal_connect(purple_blist_get_handle(),
336 "buddy-added",
337 thrasher_blist_get_handle(),
338 PURPLE_CALLBACK(thrasher_buddy_added),
339 NULL);
342 purple_signal_connect(purple_blist_get_handle(),
343 "buddy-removed",
344 thrasher_blist_get_handle(),
345 PURPLE_CALLBACK(thrasher_buddy_removed),
346 NULL);