Net::REPL::Client: Allow read/print portions to be overridden separately.
[thrasher.git] / thblist.c
blob68be7af9a1138105519d7ee85641c4448bbb5222
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,
67 // Reserved 1 - 4
68 NULL,
69 NULL,
70 NULL,
71 NULL
74 struct _PurpleStatusType
76 PurpleStatusPrimitive primitive;
78 char *id;
79 char *name;
80 char *primary_attr_id;
82 gboolean saveable;
83 gboolean user_settable;
84 gboolean independent;
86 GList *attrs;
90 struct _PurpleStatus
92 struct _PurpleStatusType *type;
93 PurplePresence *presence;
95 const char *title;
97 gboolean active;
99 GHashTable *attr_values;
103 * @brief This is the callback for the buddy-status-changed signal
104 * @param buddy PurpleBuddy struct
105 * @param old (unused)
106 * @param new @buddy new PurpleStatus
108 void thrasher_buddy_status_changed (PurpleBuddy *buddy,
109 PurpleStatus *old,
110 PurpleStatus *new)
112 PurpleGroup *group;
113 const char *group_name = NULL;
114 const char *message = NULL;
115 const char *alias = NULL;
117 g_return_if_fail(buddy != NULL);
118 g_return_if_fail(new != NULL);
120 group = purple_buddy_get_group(buddy);
122 if (group)
123 group_name = purple_group_get_name(group);
125 alias = purple_buddy_get_alias_only(buddy);
127 message = purple_status_get_attr_string(new, "message");
129 thrasher_wrapper_presence_in(thrasher_account_get_jid(buddy->account),
130 purple_buddy_get_name(buddy),
131 alias,
132 group_name,
133 purple_status_type_get_primitive(new->type),
134 message);
137 void thrasher_buddy_authorize (const char *jid,
138 const char *legacy_username) {
139 g_return_if_fail(jid);
140 g_return_if_fail(legacy_username);
142 PurpleAccount *account = thrasher_get_account_by_jid(jid);
143 g_return_if_fail(account);
145 PurpleConnection *gc =
146 thrasher_get_connection_by_account(account);
147 g_return_if_fail(gc);
149 purple_debug_info("thrasher", "Authorizing %s for jid %s\n",
150 legacy_username, jid);
152 // defensive; probably only add_permit is necesary
153 serv_rem_deny(gc, legacy_username);
155 serv_add_permit(gc, legacy_username);
158 void thrasher_buddy_deauthorize (const char *jid,
159 const char *legacy_username) {
160 g_return_if_fail(jid);
161 g_return_if_fail(legacy_username);
163 PurpleAccount *account = thrasher_get_account_by_jid(jid);
164 g_return_if_fail(account);
166 PurpleConnection *gc =
167 thrasher_get_connection_by_account(account);
168 g_return_if_fail(gc);
170 purple_debug_info("thrasher", "Deauthorizing %s for jid %s\n",
171 legacy_username, jid);
173 // no corresponding deny, we don't have a reperesentation for that
174 serv_rem_permit(gc, legacy_username);
177 void thrasher_buddy_signed_on (PurpleBuddy *buddy)
179 PurplePresence *presence;
181 presence = purple_buddy_get_presence(buddy);
183 if (!presence)
184 return;
186 // libpurple uses this to populate some stuff about the
187 // buddy, particularly useful for XMPP file transfers but
188 // probably the cause of random other errors if we don't fill this
189 // out.
190 PurpleAccount* account = buddy->account;
191 g_return_if_fail(account);
192 PurpleConnection* connection = thrasher_connection_for_account(account);
193 g_return_if_fail(connection);
195 purple_debug_info("thblist",
196 "getting server info for %s\n\n---------\n\n",
197 buddy->name);
198 serv_get_info(connection, buddy->name);
200 /* Currently active status may be PURPLE_STATUS_UNAVAILABLE
201 * (translates XMPP dnd) instead of just "available".
203 PurpleStatus *status = purple_presence_get_active_status(presence);
205 thrasher_buddy_status_changed(buddy,
206 NULL,
207 status);
210 void thrasher_buddy_signed_off (PurpleBuddy *buddy)
212 PurplePresence *presence;
214 presence = purple_buddy_get_presence(buddy);
216 if (!presence)
217 return;
219 thrasher_buddy_status_changed(buddy,
220 NULL,
221 purple_presence_get_status(presence, "offline"));
224 static void * thrasher_buddy_request_authorize
225 (PurpleAccount *account,
226 const char *remote_user,
227 const char *id,
228 const char *alias,
229 const char *message,
230 gboolean on_list,
231 PurpleAccountRequestAuthorizationCb authorize_cb,
232 PurpleAccountRequestAuthorizationCb deny_cb,
233 void *user_data) {
234 // authorize_cb sometimes NULL?!?
235 purple_debug_info("thrasher", "request authorize, empty cb: %s\n",
236 authorize_cb ? "FALSE" : "TRUE");
237 thrasher_buddy_add_request(account, remote_user, "", alias, message);
239 if (authorize_cb) {
240 authorize_cb(user_data);
243 void *uihandle = NULL;
244 return uihandle;
247 /* thrasher_buddy_add_request
249 * Triggered on a buddy-added signal, this allows us to push new subscriptions.
252 void thrasher_buddy_added (PurpleBuddy *buddy) {
253 PurplePresence *presence = purple_buddy_get_presence(buddy);
254 PurpleStatus *status = purple_presence_get_active_status(presence);
255 PurpleStatusType *type = purple_status_get_type(status);
257 const char *jid = thrasher_account_get_jid(buddy->account);
258 const char *sender = purple_buddy_get_name(buddy);
259 const guint status_int = purple_status_type_get_primitive(type);
261 purple_debug_info("thrasher",
262 "buddy added to %s: %s\n",
263 jid, sender);
265 thrasher_wrapper_subscription_add(jid, sender, status_int);
269 void thrasher_buddy_add_request (PurpleAccount *account, const char *remote_user,
270 const char *id, const char *alias,
271 const char *message)
273 purple_debug_info("thrasher",
274 "legacy user %s adding %s to roster\n",
275 remote_user, alias);
276 const char *jid = thrasher_account_get_jid(account);
277 thrasher_wrapper_legacy_user_add_user(jid, remote_user);
281 void thrasher_buddy_removed (PurpleBuddy *buddy)
283 /* printf("Removed buddy:\t[%s]\t[%s]\n", purple_buddy_get_name(buddy),
284 * purple_status_get_name( purple_presence_get_active_status( purple_buddy_get_presence(buddy) ) )
285 * );
287 PurplePresence *presence = purple_buddy_get_presence(buddy);
288 PurpleStatus *status = purple_presence_get_active_status(presence);
289 PurpleStatusType *type = purple_status_get_type(status);
290 PurpleGroup *group = purple_buddy_get_group(buddy);
292 * Need to fire jid, sender, group_name, alias, and status back to perl */
294 // jid = thrasher_account_get_jid(buddy->account)
295 // sender = purple_buddy_get_name(buddy)
296 // group_name = purple_group_get_name(group)
297 // alias = purple_buddy_get_alias_only(buddy)
298 // status = purple_status_type_get_primitive(type)
302 static gpointer
303 thrasher_blist_get_handle ()
305 static int handle;
306 return &handle;
310 void thrasher_blist_init()
312 purple_accounts_set_ui_ops(&thrasher_account_uiops);
314 purple_signal_connect(purple_blist_get_handle(),
315 "buddy-status-changed",
316 thrasher_blist_get_handle(),
317 PURPLE_CALLBACK(thrasher_buddy_status_changed),
318 NULL);
320 purple_signal_connect(purple_blist_get_handle(),
321 "buddy-signed-on",
322 thrasher_blist_get_handle(),
323 PURPLE_CALLBACK(thrasher_buddy_signed_on),
324 NULL);
326 purple_signal_connect(purple_blist_get_handle(),
327 "buddy-signed-off",
328 thrasher_blist_get_handle(),
329 PURPLE_CALLBACK(thrasher_buddy_signed_off),
330 NULL);
332 // It looks like this only comes in for things already on our roster.
333 purple_signal_connect(purple_blist_get_handle(),
334 "buddy-added",
335 thrasher_blist_get_handle(),
336 PURPLE_CALLBACK(thrasher_buddy_added),
337 NULL);
340 purple_signal_connect(purple_blist_get_handle(),
341 "buddy-removed",
342 thrasher_blist_get_handle(),
343 PURPLE_CALLBACK(thrasher_buddy_removed),
344 NULL);