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
29 void thrasher_buddy_status_changed (PurpleBuddy
*buddy
, PurpleStatus
*old
,
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
,
37 static void * thrasher_buddy_request_authorize
38 (PurpleAccount
*account
,
39 const char *remote_user
,
44 PurpleAccountRequestAuthorizationCb authorize_cb
,
45 PurpleAccountRequestAuthorizationCb deny_cb
,
47 void thrasher_buddy_removed (PurpleBuddy
*buddy
);
48 static gpointer
thrasher_blist_get_handle (void);
49 static char* status_text_for_buddy(PurpleBuddy
*buddy
);
51 static PurpleAccountUiOps thrasher_account_uiops
=
54 thrasher_buddy_add_request
,
60 thrasher_buddy_add_request
,
63 thrasher_buddy_request_authorize
,
65 // close_account_request
77 struct _PurpleStatusType
79 PurpleStatusPrimitive primitive
;
83 char *primary_attr_id
;
86 gboolean user_settable
;
95 struct _PurpleStatusType
*type
;
96 PurplePresence
*presence
;
102 GHashTable
*attr_values
;
106 * @brief This is the callback for the buddy-status-changed signal
107 * @param buddy PurpleBuddy struct
108 * @param old (unused)
109 * @param new @buddy new PurpleStatus
111 void thrasher_buddy_status_changed (PurpleBuddy
*buddy
,
116 const char *group_name
= 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
);
125 group_name
= purple_group_get_name(group
);
127 alias
= purple_buddy_get_alias_only(buddy
);
129 char* message
= status_text_for_buddy(buddy
);
131 message
= g_strdup(purple_status_get_attr_string(new, "message"));
134 thrasher_wrapper_presence_in(thrasher_account_get_jid(buddy
->account
),
135 purple_buddy_get_name(buddy
),
138 purple_status_type_get_primitive(new->type
),
143 /* Returns a string that becomes owned by the caller or NULL. */
145 status_text_for_buddy(PurpleBuddy
*buddy
) {
149 if (! buddy
->account
->gc
) {
153 const char *prpl_id
= purple_account_get_protocol_id(buddy
->account
);
155 PurplePlugin
*prpl
= purple_find_prpl(prpl_id
);
157 PurplePluginProtocolInfo
*prpl_info
158 = PURPLE_PLUGIN_PROTOCOL_INFO(prpl
);
159 if (prpl_info
&& prpl_info
->status_text
) {
160 return prpl_info
->status_text(buddy
);
166 void thrasher_buddy_authorize (const char *jid
,
167 const char *legacy_username
) {
168 g_return_if_fail(jid
);
169 g_return_if_fail(legacy_username
);
171 PurpleAccount
*account
= thrasher_get_account_by_jid(jid
);
172 g_return_if_fail(account
);
174 PurpleConnection
*gc
=
175 thrasher_get_connection_by_account(account
);
176 g_return_if_fail(gc
);
178 purple_debug_info("thrasher", "Authorizing %s for jid %s\n",
179 legacy_username
, jid
);
181 // defensive; probably only add_permit is necesary
182 serv_rem_deny(gc
, legacy_username
);
184 serv_add_permit(gc
, legacy_username
);
187 void thrasher_buddy_deauthorize (const char *jid
,
188 const char *legacy_username
) {
189 g_return_if_fail(jid
);
190 g_return_if_fail(legacy_username
);
192 PurpleAccount
*account
= thrasher_get_account_by_jid(jid
);
193 g_return_if_fail(account
);
195 PurpleConnection
*gc
=
196 thrasher_get_connection_by_account(account
);
197 g_return_if_fail(gc
);
199 purple_debug_info("thrasher", "Deauthorizing %s for jid %s\n",
200 legacy_username
, jid
);
202 // no corresponding deny, we don't have a reperesentation for that
203 serv_rem_permit(gc
, legacy_username
);
206 void thrasher_buddy_signed_on (PurpleBuddy
*buddy
)
208 PurplePresence
*presence
;
210 presence
= purple_buddy_get_presence(buddy
);
215 // libpurple uses this to populate some stuff about the
216 // buddy, particularly useful for XMPP file transfers but
217 // probably the cause of random other errors if we don't fill this
219 PurpleAccount
* account
= buddy
->account
;
220 g_return_if_fail(account
);
221 PurpleConnection
* connection
= thrasher_connection_for_account(account
);
222 g_return_if_fail(connection
);
224 purple_debug_info("thblist",
225 "getting server info for %s\n\n---------\n\n",
227 serv_get_info(connection
, buddy
->name
);
229 /* Currently active status may be PURPLE_STATUS_UNAVAILABLE
230 * (translates XMPP dnd) instead of just "available".
232 PurpleStatus
*status
= purple_presence_get_active_status(presence
);
234 thrasher_buddy_status_changed(buddy
,
239 void thrasher_buddy_signed_off (PurpleBuddy
*buddy
)
241 PurplePresence
*presence
;
243 presence
= purple_buddy_get_presence(buddy
);
248 thrasher_buddy_status_changed(buddy
,
250 purple_presence_get_status(presence
, "offline"));
253 static void * thrasher_buddy_request_authorize
254 (PurpleAccount
*account
,
255 const char *remote_user
,
260 PurpleAccountRequestAuthorizationCb authorize_cb
,
261 PurpleAccountRequestAuthorizationCb deny_cb
,
263 // authorize_cb sometimes NULL?!?
264 purple_debug_info("thrasher", "request authorize, empty cb: %s\n",
265 authorize_cb
? "FALSE" : "TRUE");
266 thrasher_buddy_add_request(account
, remote_user
, "", alias
, message
);
269 authorize_cb(user_data
);
272 void *uihandle
= NULL
;
276 /* thrasher_buddy_add_request
278 * Triggered on a buddy-added signal, this allows us to push new subscriptions.
281 void thrasher_buddy_added (PurpleBuddy
*buddy
) {
282 PurplePresence
*presence
= purple_buddy_get_presence(buddy
);
283 PurpleStatus
*status
= purple_presence_get_active_status(presence
);
284 PurpleStatusType
*type
= purple_status_get_type(status
);
286 const char *jid
= thrasher_account_get_jid(buddy
->account
);
287 const char *sender
= purple_buddy_get_name(buddy
);
288 const guint status_int
= purple_status_type_get_primitive(type
);
290 purple_debug_info("thrasher",
291 "buddy added to %s: %s\n",
294 thrasher_wrapper_subscription_add(jid
, sender
, status_int
);
298 void thrasher_buddy_add_request (PurpleAccount
*account
, const char *remote_user
,
299 const char *id
, const char *alias
,
302 const char *jid
= thrasher_account_get_jid(account
);
303 purple_debug_info("thrasher",
304 "legacy user %s aka %s added %s to roster\n",
305 remote_user
, alias
, jid
);
306 thrasher_wrapper_legacy_user_add_user(jid
, remote_user
);
310 void thrasher_buddy_removed (PurpleBuddy
*buddy
)
312 /* printf("Removed buddy:\t[%s]\t[%s]\n", purple_buddy_get_name(buddy),
313 * purple_status_get_name( purple_presence_get_active_status( purple_buddy_get_presence(buddy) ) )
316 PurplePresence *presence = purple_buddy_get_presence(buddy);
317 PurpleStatus *status = purple_presence_get_active_status(presence);
318 PurpleStatusType *type = purple_status_get_type(status);
319 PurpleGroup *group = purple_buddy_get_group(buddy);
321 * Need to fire jid, sender, group_name, alias, and status back to perl */
323 // jid = thrasher_account_get_jid(buddy->account)
324 // sender = purple_buddy_get_name(buddy)
325 // group_name = purple_group_get_name(group)
326 // alias = purple_buddy_get_alias_only(buddy)
327 // status = purple_status_type_get_primitive(type)
332 thrasher_blist_get_handle ()
339 void thrasher_blist_init()
341 purple_accounts_set_ui_ops(&thrasher_account_uiops
);
343 purple_signal_connect(purple_blist_get_handle(),
344 "buddy-status-changed",
345 thrasher_blist_get_handle(),
346 PURPLE_CALLBACK(thrasher_buddy_status_changed
),
349 purple_signal_connect(purple_blist_get_handle(),
351 thrasher_blist_get_handle(),
352 PURPLE_CALLBACK(thrasher_buddy_signed_on
),
355 purple_signal_connect(purple_blist_get_handle(),
357 thrasher_blist_get_handle(),
358 PURPLE_CALLBACK(thrasher_buddy_signed_off
),
361 // It looks like this only comes in for things already on our roster.
362 purple_signal_connect(purple_blist_get_handle(),
364 thrasher_blist_get_handle(),
365 PURPLE_CALLBACK(thrasher_buddy_added
),
369 purple_signal_connect(purple_blist_get_handle(),
371 thrasher_blist_get_handle(),
372 PURPLE_CALLBACK(thrasher_buddy_removed),