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
27 #include "thpresence.h"
28 #include "thconversations.h"
37 /* Internal prototypes */
38 int func_call(gpointer func
);
39 void destroy_record_key (gpointer key
);
40 void validate_callback (SV
*cb
);
41 void thrasher_wrapper_destructor (void);
42 SV
*clean_newSVpv(const char *inp
);
43 void thrasher_remove_account(const char *jid
);
45 perl_callbacks callback
;
46 perl_ft_callbacks ft_callback
;
49 char *trigger_timeout_func_name
= "THPPW::thrasher_wrapper_trigger_timeout_func";
50 char *trigger_input_func_name
= "THPPW::thrasher_wrapper_trigger_input_func";
53 GHashTable
*accts
= NULL
;
54 GHashTable
*callback_funcs
= NULL
;
55 GHashTable
*perl_key_lookup
= NULL
;
57 void thrasher_purple_debug (int tf
)
59 /* Flip on libpurple debugging info */
61 purple_debug_set_enabled(1);
64 purple_debug_set_verbose(1);
69 void thrasher_perl_init()
71 accts
= g_hash_table_new(g_str_hash
, g_str_equal
);
75 * @brief create a PurpleAccount and start connecting it.
76 * @param hash_args of arguments for thrasher_login().
77 * @return 0 for success. 1 if thrasher_login() fails. 2 if the PurpleAccount already exists.
79 int thrasher_action_login (GHashTable
*hash_args
)
84 purple_debug_info("thrasher",
86 GList
*keys
= g_hash_table_get_keys(hash_args
);
87 GList
*values
= g_hash_table_get_values(hash_args
);
90 purple_debug_info("thrasher",
93 (char *)values
->data
);
95 values
= values
->next
;
100 #endif /* TH_DEBUG */
102 // some parameters we always have
103 char *jid
= (char *)g_strdup(g_hash_table_lookup(hash_args
, "jid"));
104 char *username
= (char *)g_strdup(g_hash_table_lookup(hash_args
, "username"));
105 char *password
= (char *)g_strdup(g_hash_table_lookup(hash_args
, "password"));
106 char *proto
= (char *)g_strdup(g_hash_table_lookup(hash_args
, "proto"));
108 g_return_val_if_fail(jid
, 0);
109 g_return_val_if_fail(username
, 0);
110 g_return_val_if_fail(password
, 0);
111 g_return_val_if_fail(proto
, 0);
113 g_hash_table_remove(hash_args
, "jid");
114 g_hash_table_remove(hash_args
, "username");
115 g_hash_table_remove(hash_args
, "password");
116 g_hash_table_remove(hash_args
, "proto");
118 /* Verify user is not already logged in */
119 if (g_hash_table_lookup(accts
, jid
) == NULL
)
121 PurpleAccount
*account
= thrasher_login(proto
, username
, password
,
124 // In the event of an error, connection_error has already been
126 if (account
== NULL
) {
130 char *new_jid
= g_strdup(jid
);
132 /* Add user to hash */
135 new_jid
, /* Should eventually be the protocol + username */
144 // At least indicate something bad has happened.
145 purple_debug_info("thrasher",
146 "login request for [%s] fail because they are apparently already logged in\n",
155 g_hash_table_destroy(hash_args
);
162 * @brief Get a list of all JIDs with active accounts.
165 thrasher_action_get_logged_in_jids() {
166 GList
* jids
= g_hash_table_get_keys(accts
);
173 * @brief add buddy (@p buddy_name) for given @p jid buddy list
174 * @param jid releational key to bound to account
175 * @param buddy_name name of buddy
176 * @return 1 if there is a problem, 0 otherwise (C)
178 int thrasher_action_buddy_add (char *jid
, char *buddy_name
)
180 PurpleAccount
*account
= NULL
;
181 PurpleBuddy
*buddy
= NULL
;
182 PurpleGroup
*group
= NULL
;
184 purple_debug_info("thrasher", "Buddy_add request\n");
186 account
= g_hash_table_lookup(accts
, jid
);
187 g_return_val_if_fail(account
!= NULL
, 1);
189 buddy
= purple_find_buddy(account
, buddy_name
);
191 /* Re-requesting authorization addresses cases in which a buddy is
192 * implicitly added somewhere else but never actually sent an
193 * authorization request. Such a state was sometimes possible with
194 * Yahoo but consistently reproducible with as follows in ICQ:
196 * 1. Register user1 and user2 with the ICQ transport, each on the
198 * 2. Remove user1's ICQ contact from user2's roster.
199 * 3. Remove user2's ICQ contact from user1's roster.
200 * 4. Add user2's ICQ contact to user1's roster.
201 * 5. thrasher_buddy_request_authorize() receives and handles an
202 * authorization request from user1 on behalf of user2. It
203 * adds user1 to user2's buddy list.
204 * 6. user1 is now fully authorized with user2.
205 * 7. user2, however, had user1 added as an *un*authorized buddy.
207 * This also means Thrasher tries to do something sensible with
208 * XMPP client operations like "re-request authorization from
209 * contact". Otherwise, they'd always be handled as no-ops because
210 * the libpurple buddy is already present.
214 /* Re-add: check if (re-)auth is needed. */
215 need_auth
= ! thrasher_account_buddy_is_authorized(account
, buddy
);
218 /* Not yet on list: assume an auth request is needed. */
221 /* FIXME: There's no cross-prpl calls for checking/sending
222 * authorization like oscar.c:purple_auth_request()? Could try to
223 * parse for the menu items added by each prpl we know requires
227 purple_debug_info("thrasher",
228 "%s needs authorization from %s\n",
232 // FIXME: Figure out if we need this buddy list stuff
233 // at all. We don't actually care, as long as we get
234 // the messages and presence to send out.
236 /* We're done if buddy already exists and is authorized.
238 * libpurple says prpl should interpret a re-add of a existent
239 * buddy as an attempt to (re)send an authorization request if
242 if (buddy
== NULL
|| need_auth
)
244 char *default_group
= "General";
246 /* Check to see if default group exists */
247 group
= purple_find_group(default_group
);
249 /* If not, create default group */
251 group
= purple_group_new(default_group
);
253 purple_blist_add_group(group
, NULL
);
255 /* Add buddy to default group (NULL should be alias...) */
256 buddy
= purple_buddy_new(account
, buddy_name
, NULL
);
257 purple_blist_add_buddy(buddy
, NULL
, group
, NULL
);
258 purple_account_add_buddy(account
, buddy
);
260 purple_debug_info("thrasher",
261 "finished adding %s to %s's buddy list\n",
266 purple_debug_info("thrasher",
267 "%s already on %s's buddy list\n",
271 /* Buddy already exists */
276 * @brief remove buddy (@p buddy_name) from given @p jid buddy list
277 * @param jid releational key to bound to account
278 * @param buddy_name name of buddy
279 * @return 1 if there is a problem, 0 otherwise (C)
281 int thrasher_action_buddy_remove (char *jid
, char *buddy_name
)
283 PurpleGroup
*group
= NULL
;
284 PurpleBuddy
*buddy
= NULL
;
285 PurpleAccount
*account
= NULL
;
287 purple_debug_info("thrasher", "Buddy remove\n");
289 account
= g_hash_table_lookup(accts
, jid
);
290 g_return_val_if_fail(account
!= NULL
, 1);
292 // Don't try and remove a buddy if the account
294 if (purple_account_is_connected(account
)) {
296 purple_debug_info("thrasher", "find_buddy...\n");
297 buddy
= purple_find_buddy(account
, buddy_name
);
299 /* Only remove a buddy if they exist */
302 purple_debug_info("thrasher", "get_group...\n");
303 /* Give up us the group */
304 group
= purple_buddy_get_group(buddy
);
306 purple_debug_info("thrasher", "account_remove_buddy...\n");
307 /* Remove server-side buddy entry */
308 purple_account_remove_buddy(account
, buddy
, group
);
310 purple_debug_info("thrasher", "blist_remove_buddy...\n");
311 /* Also need to remove buddy entry from memory */
312 purple_blist_remove_buddy(buddy
);
318 /* Account isn't connected or buddy is not found */
322 void thrasher_action_buddy_authorize(char *jid
, char *legacy_username
)
324 thrasher_action_buddy_add(jid
, legacy_username
);
327 void thrasher_action_buddy_deauthorize(char *jid
, char *legacy_username
) {
328 thrasher_action_buddy_remove(jid
, legacy_username
);
332 * @brief update presence status for given @p jid
333 * @param jid releational key to bound to account
334 * @param status integer representation of PurpleStatusPrimitive
335 * @param message a status message (may be NULL)
336 * @return 1 if there is a problem, 0 otherwise (C)
338 int thrasher_action_presence (char *jid
, int status
, char *message
)
340 PurpleAccount
*account
;
342 g_return_val_if_fail(jid
!= NULL
, 0);
344 account
= g_hash_table_lookup(accts
, jid
);
349 thrasher_set_presence(account
, status
, message
);
356 * @brief Send @p message from the user given by @p jid to the user @p recipient
360 * @return 1 on success 0 on failure
362 int thrasher_action_outgoing_msg (char *jid
, char *recipient
, char *message
)
365 PurpleAccount
*account
= NULL
;
367 g_return_val_if_fail(jid
!= NULL
, 0);
368 g_return_val_if_fail(recipient
!= NULL
, 0);
369 g_return_val_if_fail(message
!= NULL
, 0);
371 purple_debug_info("thrasher", "Send message request\n");
372 /* Pull in user if they exist */
373 account
= g_hash_table_lookup(accts
, jid
);
375 /* User does not exist (may not be logged in) */
376 if (account
== NULL
) {
380 /* Try to send message (message might not be sent) */
381 if (thrasher_send_msg(account
,recipient
,message
) == TRUE
)
389 * @brief Send @p chatstate from the user given by @p jid to the @p recipient
395 thrasher_action_outgoing_chatstate(char *jid
,
397 PurpleTypingState chatstate
) {
398 g_return_if_fail(jid
!= NULL
);
399 g_return_if_fail(recipient
!= NULL
);
401 PurpleAccount
*pa
= NULL
;
402 pa
= g_hash_table_lookup(accts
, jid
);
404 /* User does not exist (may not be logged in) */
408 /* Try to send chatstate */
409 thrasher_outgoing_chatstate(pa
, recipient
, chatstate
);
414 * @brief Log the user out with the given jid
415 * @param jid releational key to bind to account
416 * @return 1 if logout was successful, 0 if it was not
418 * NOTE: As we're going to be running a process for each protocol,
419 * there's no need to worry about the protocol used.
421 int thrasher_action_logout (char *jid
)
424 PurpleAccount
*account
;
426 g_return_val_if_fail(jid
!= NULL
, 0);
428 purple_debug_info("thrasher", "Logout requested for %s\n", jid
);
430 /* Verify user is logged in */
431 account
=g_hash_table_lookup(accts
, jid
);
434 thrasher_logout(account
);
435 thrasher_remove_account(jid
);
443 void _thrasher_action_debug_logged_in_accts_printer(gpointer key
, gpointer value
, gpointer user_data
);
446 * @brief Dump active account name lists to STDERR from libpurple and thperl.
449 thrasher_action_debug_logged_in() {
450 fprintf(stderr
, "purple_accounts_get_all_active:\n");
451 GList
* list
= purple_accounts_get_all_active();
455 PurpleAccount
*pa
= (PurpleAccount
*)l
->data
;
456 fprintf(stderr
, "\t%s = %p\n", purple_account_get_username(pa
), pa
);
463 fprintf(stderr
, "thperl accts:\n");
464 g_hash_table_foreach(accts
,
465 _thrasher_action_debug_logged_in_accts_printer
,
469 fprintf(stderr
, "No thperl accts?!!\n");
474 _thrasher_action_debug_logged_in_accts_printer(gpointer key
,
476 gpointer user_data
) {
477 const char *jid
= (char*)key
;
478 PurpleAccount
*pa
= (PurpleAccount
*)value
;
479 fprintf(stderr
, "\t%s = %s = %p\n",
480 jid
, purple_account_get_username(pa
), pa
);
485 * @brief Destruction function for our internal function hash
486 * @param key data element
488 void destroy_record_key (gpointer key
)
495 * @brief Bizarro passthrough to allow perl calling of dynamically-set C events
496 * @param key a guint value corresponding to the pointer of a callback.
497 * @return boolean as int (0 || 1) on success. Returns 0 on failure as well.
499 int thrasher_wrapper_trigger_timeout_func(long key
)
501 /* @exception key cannot be NULL */
502 g_return_val_if_fail(key
!= TNULL
,TFALSE
);
504 char *real_key
= LONG_TO_PTR(key
);
506 return func_call(real_key
);
510 * @brief Bizarro passthrough (two) to allow perl calling of dynamically-set C events
511 * @param file descriptor
512 * @param condition trigger was called on
513 * @param key a long value corresponding to the pointer of a callback.
514 * @return boolean as int (0 || 1) on success. Returns 0 on failure as well.
516 int thrasher_wrapper_trigger_input_func(long fd
, SV
*cond
, long key
)
518 /* @exception fd cannot be zero */
519 g_return_val_if_fail(fd
!= 0, 0);
520 /* @exception cond cannot be zero */
521 g_return_val_if_fail(cond
!= 0, 0);
522 /* @exception key cannot be zero */
523 g_return_val_if_fail(key
!= 0, 0);
525 // GIOCondition real_cond = cond;
526 char *real_key
= LONG_TO_PTR(key
);
528 /* FIXME: We need to pass cond and key to this call... */
529 return func_call(real_key
);
533 * @brief Simple verifcation for callbacks
534 * @param subref to callback
537 void validate_callback (SV
*cb
)
539 if (!SvROK(cb
) || SvTYPE(SvRV(cb
)) != SVt_PVCV
)
541 croak("Not a callback!");
548 * @brief Initialize our wrapper
549 * @param subref (SV *) to call for timeout_add
550 * @param subref (SV *) to call for input_add
551 * @param subref (SV *) to call for source_remove
552 * @param subref (SV *) to call on incoming messages
553 * @param subref (SV *) to call on incoming presence information
554 * @param subref (SV *) to call on subscription requests
555 * @param subref (SV *) to call on subscription requests from legacy users
556 * @param subref (SV *) to call on connection errors
557 * @param subref (SV *) to call when connections have succeeded
558 * @param subref (SV *) to call on incoming chatstate (optional)
560 void thrasher_wrapper_init (SV
*timeout_add_cb
,
562 SV
*source_remove_cb
,
565 SV
*subscription_add_cb
,
566 SV
*legacy_user_add_user_cb
,
567 SV
*connection_error_cb
,
572 va_start(argp
, connection_cb
);
573 SV
* incoming_chatstate_cb
= va_arg(argp
, SV
*);
575 g_return_if_fail(timeout_add_cb
);
576 g_return_if_fail(input_add_cb
);
577 g_return_if_fail(source_remove_cb
);
578 g_return_if_fail(incoming_msg_cb
);
579 g_return_if_fail(presence_in_cb
);
580 g_return_if_fail(subscription_add_cb
);
581 g_return_if_fail(legacy_user_add_user_cb
);
582 g_return_if_fail(connection_error_cb
);
583 g_return_if_fail(connection_cb
);
585 /* Set the bindings for our function table */
586 callback_funcs
= g_hash_table_new_full(g_direct_hash
,
591 /* Create the lookup table so we know what to do when Perl triggers a remove */
592 perl_key_lookup
= g_hash_table_new(g_int_hash
, g_int_equal
);
594 /* Validate callback, this burns up if it fails so no
595 * need to check for errors!
597 validate_callback(timeout_add_cb
);
598 validate_callback(input_add_cb
);
599 validate_callback(source_remove_cb
);
600 validate_callback(incoming_msg_cb
);
601 validate_callback(presence_in_cb
);
602 validate_callback(subscription_add_cb
);
603 validate_callback(legacy_user_add_user_cb
);
604 validate_callback(connection_error_cb
);
605 validate_callback(connection_cb
);
606 if (incoming_chatstate_cb
) {
607 validate_callback(incoming_chatstate_cb
);
618 callback
.timeout_add
= newSVsv(timeout_add_cb
);
619 callback
.input_add
= newSVsv(input_add_cb
);
620 callback
.source_remove
= newSVsv(source_remove_cb
);
621 callback
.incoming_msg
= newSVsv(incoming_msg_cb
);
622 callback
.presence_in
= newSVsv(presence_in_cb
);
623 callback
.subscription_add
= newSVsv(subscription_add_cb
);
624 callback
.legacy_user_add_user
= newSVsv(legacy_user_add_user_cb
);
625 callback
.connection_error
= newSVsv(connection_error_cb
);
626 callback
.connection
= newSVsv(connection_cb
);
627 if (incoming_chatstate_cb
) {
628 callback
.incoming_chatstate
= newSVsv(incoming_chatstate_cb
);
631 callback
.incoming_chatstate
= 0;
642 * @brief Initialize our file transfer wrapper
643 * @param subref (SV *) to call for recv_accept
644 * @param subref (SV *) to call for recv_start
645 * @param subref (SV *) to call for recv_cancel
646 * @param subref (SV *) to call for recv_complete
647 * @param subref (SV *) to call for send_accept
648 * @param subref (SV *) to call for send_start
649 * @param subref (SV *) to call for send_cancel
650 * @param subref (SV *) to call for send_complete
651 * @param subref (SV *) to call for ui_read. Must return (buffer, size).
652 * @param subref (SV *) to call for ui_write
653 * @param subref (SV *) to call for ui_data_not_sent
655 void thrasher_wrapper_ft_init(SV
*ft_recv_request_cb
,
656 SV
*ft_recv_accept_cb
,
657 SV
*ft_recv_start_cb
,
658 SV
*ft_recv_cancel_cb
,
659 SV
*ft_recv_complete_cb
,
660 SV
*ft_send_accept_cb
,
661 SV
*ft_send_start_cb
,
662 SV
*ft_send_cancel_cb
,
663 SV
*ft_send_complete_cb
,
666 SV
*ft_data_not_sent_cb
)
668 g_return_if_fail(ft_recv_request_cb
);
669 g_return_if_fail(ft_recv_accept_cb
);
670 g_return_if_fail(ft_recv_start_cb
);
671 g_return_if_fail(ft_recv_cancel_cb
);
672 g_return_if_fail(ft_recv_complete_cb
);
673 g_return_if_fail(ft_send_accept_cb
);
674 g_return_if_fail(ft_send_start_cb
);
675 g_return_if_fail(ft_send_cancel_cb
);
676 g_return_if_fail(ft_send_complete_cb
);
677 g_return_if_fail(ft_read_cb
);
678 g_return_if_fail(ft_write_cb
);
679 g_return_if_fail(ft_data_not_sent_cb
);
681 // This assumes thrasher_wrapper_init was already called.
682 g_return_if_fail(callback_funcs
);
684 validate_callback(ft_recv_request_cb
);
685 validate_callback(ft_recv_accept_cb
);
686 validate_callback(ft_recv_start_cb
);
687 validate_callback(ft_recv_cancel_cb
);
688 validate_callback(ft_recv_complete_cb
);
689 validate_callback(ft_send_accept_cb
);
690 validate_callback(ft_send_start_cb
);
691 validate_callback(ft_send_cancel_cb
);
692 validate_callback(ft_send_complete_cb
);
693 validate_callback(ft_read_cb
);
694 validate_callback(ft_write_cb
);
695 validate_callback(ft_data_not_sent_cb
);
705 ft_callback
.ft_recv_request_cb
= newSVsv(ft_recv_request_cb
);
706 ft_callback
.ft_recv_accept_cb
= newSVsv(ft_recv_accept_cb
);
707 ft_callback
.ft_recv_start_cb
= newSVsv(ft_recv_start_cb
);
708 ft_callback
.ft_recv_cancel_cb
= newSVsv(ft_recv_cancel_cb
);
709 ft_callback
.ft_recv_complete_cb
= newSVsv(ft_recv_complete_cb
);
710 ft_callback
.ft_send_accept_cb
= newSVsv(ft_send_accept_cb
);
711 ft_callback
.ft_send_start_cb
= newSVsv(ft_send_start_cb
);
712 ft_callback
.ft_send_cancel_cb
= newSVsv(ft_send_cancel_cb
);
713 ft_callback
.ft_send_complete_cb
= newSVsv(ft_send_complete_cb
);
714 ft_callback
.ft_read_cb
= newSVsv(ft_read_cb
);
715 ft_callback
.ft_write_cb
= newSVsv(ft_write_cb
);
716 ft_callback
.ft_data_not_sent_cb
= newSVsv(ft_data_not_sent_cb
);
728 * @brief Validate then call the given callback pointer.
729 * @param key a hash key for the local function callback table.
730 * @return boolean as int (0 || 1) on success
732 int func_call(gpointer key
)
734 /* @exception key cannot be NULL */
735 g_return_val_if_fail(key
!= NULL
,TFALSE
);
737 gpointer orig_key
= NULL
;
738 hash_record
*record
= NULL
;
739 int (*timeout_add_caller
)(gpointer
);
740 int (*input_add_caller
)(gpointer
, gint
, PurpleInputCondition
);
743 /* Crazy voodoo madness!
744 * We're using direct pointers as keys and a struct at the
745 * end of the pointer, so no need to get any values.
747 if (g_hash_table_lookup_extended(callback_funcs
, key
, &orig_key
, NULL
))
751 if (record
->type
== TIMEOUT_ADD
)
753 timeout_add_caller
= record
->function
;
754 ret
= (timeout_add_caller
)(record
->data
);
757 else if (record
->type
== INPUT_ADD
)
759 input_add_caller
= record
->function
;
760 (input_add_caller
)(record
->data
, record
->fd
,
762 // these functions are void
768 /* We're never gonna be calling this again, so get rid of it */
769 g_hash_table_remove(callback_funcs
, record
);
777 purple_debug_info("thrasher", "Callback for [%p] does not exist!\n", key
);
785 * @brief Wraps Glib::Source->remove
786 * @param key to return to the Perl end
787 * @return Success of call, including the success of the callback
789 gboolean
thrasher_wrapper_call_source_remove (long key
)
793 /* @exception key cannot be NULL */
794 g_return_val_if_fail(callback
.source_remove
, 0);
796 /* @exception callback.source_remove cannot be NULL */
797 g_return_val_if_fail(key
!= TNULL
, 0);
806 /* Push the key onto the stack */
807 XPUSHs(sv_2mortal(newSViv(key
)));
811 call_sv(callback
.source_remove
, G_EVAL
| G_SCALAR
);
822 /* Remove call from lookup table */
823 g_hash_table_remove(perl_key_lookup
, (gpointer
)&key
);
830 * PurpleInputCondition is a bitwise OR of read/write events
831 * PurpleInputFunction is a callback to a guint function with the args gpointer, gint, and PurpleInoutCondition
834 * @brief Wraps Glib::IO->add_watch
835 * @param File descriptor
836 * @param Condition to trigger on
837 * @param Nested callback
838 * @param Data to pass to callback
841 long thrasher_wrapper_set_input_add (guint fd
, PurpleInputCondition cond
, PurpleInputFunction function
, gpointer data
)
843 g_return_val_if_fail(callback
.input_add
, 0);
844 g_return_val_if_fail(function
!= NULL
, 0);
848 hash_record
*record
= (hash_record
*)malloc(sizeof(hash_record
));
850 record
->type
= INPUT_ADD
;
851 record
->function
= function
;
856 g_hash_table_insert(callback_funcs
, record
, NULL
);
858 key
= PTR_TO_LONG(record
);
859 g_return_val_if_fail(key
!= 0, 0);
872 svkey
= newSViv(key
);
873 temp_cv
= get_cv(trigger_input_func_name
, FALSE
);
877 pc
= newRV((SV
*)temp_cv
);
879 /* Ok, this is hairy, first we push the vars we want to send to the
880 * timeout_add Perl callback. Next we push the var we want to give
881 * back to ourselves via trigger_input_func.
886 XPUSHs(sv_2mortal(newSViv(fd
)));
887 XPUSHs(sv_2mortal(newSViv(cond
)));
888 XPUSHs(sv_2mortal(pc
));
889 XPUSHs(sv_2mortal(svkey
));
893 call_sv(callback
.input_add
, G_EVAL
| G_SCALAR
);
896 /* Tweak our internal key to an external key */
901 croak("We've got serious problems, cannot create CV* to [%s]", trigger_input_func_name
);
904 g_hash_table_insert(perl_key_lookup
, (gpointer
)&key
, record
);
911 * @brief Wraps Glib::Timeout->add
912 * @param interval in milliseconds
914 * @param data to pass to the callback
915 * @return Key for the internal timeout_add function hash. (Should this really be returning anything?!)
917 long thrasher_wrapper_set_timeout_add (guint interval
, GSourceFunc function
, gpointer data
)
919 /* @exception callback.timeout_add cannot be NULL */
920 g_return_val_if_fail(callback
.timeout_add
, 0);
921 g_return_val_if_fail(function
!= NULL
, TFALSE
);
925 hash_record
*record
= (hash_record
*)malloc(sizeof(hash_record
*));
927 record
->type
= TIMEOUT_ADD
;
928 record
->function
= function
;
931 g_hash_table_insert(callback_funcs
, record
, NULL
);
933 key
= PTR_TO_LONG(record
);
934 g_return_val_if_fail(key
!= 0, 0);
946 svkey
= newSViv(key
);
947 temp_cv
= get_cv(trigger_timeout_func_name
, FALSE
);
951 pc
= newRV((SV
*)temp_cv
);
953 /* Ok, this is hairy, first we push the vars we want to send to the
954 * timeout_add Perl callback. Next we push the var we want to give
955 * back to ourselves via trigger_input_func.
960 XPUSHs(sv_2mortal(newSViv(interval
)));
961 XPUSHs(sv_2mortal(pc
));
962 XPUSHs(sv_2mortal(svkey
));
966 call_sv(callback
.timeout_add
, G_EVAL
| G_SCALAR
);
970 /* Tweak our internal key to an external key */
975 croak("We've got serious problems, cannot create CV* to [%s]", trigger_input_func_name
);
989 * @brief Send a @p message out via callback.incoming_msg
990 * @param jid user the @p message is being sent to
991 * @param sender user name of the @p message sender
992 * @param alias alternate user name of the @p sender (may be NULL)
993 * @param message string being sent to @p jid
994 * @return 1 on success, 0 on failure
996 int thrasher_wrapper_incoming_msg (const char *jid
,
1000 PurpleMessageFlags flags
)
1004 /* @exception jid, sender, and message cannot be NULL */
1005 g_return_val_if_fail(jid
!= NULL
, 0);
1006 g_return_val_if_fail(sender
!= NULL
, 0);
1007 g_return_val_if_fail(message
!= NULL
, 0);
1009 /* @exception bail if our callback doesn't exist */
1010 g_return_val_if_fail(callback
.incoming_msg
!= NULL
, 0);
1019 /* Push the args onto the stack */
1020 XPUSHs(sv_2mortal(newSVpvn(jid
, strlen(jid
))));
1021 XPUSHs(sv_2mortal(newSVpvn(sender
, strlen(sender
))));
1022 XPUSHs(sv_2mortal(newSVpvn(alias
, strlen(alias
))));
1023 XPUSHs(sv_2mortal(newSVpvn(message
, strlen(message
))));
1024 XPUSHs(sv_2mortal(newSViv(flags
)));
1028 call_sv(callback
.incoming_msg
, G_EVAL
| G_SCALAR
);
1043 * @brief Forwards the 'connection' event, that authentication has
1045 * @param jid user who has successfully connected
1047 int thrasher_wrapper_connection(const char *jid
)
1051 /* @exception jid, sender, and message cannot be NULL */
1052 g_return_val_if_fail(jid
!= NULL
, 0);
1054 /* @exception bail if our callback doesn't exist */
1055 g_return_val_if_fail(callback
.connection
!= NULL
, 0);
1064 /* Push the args onto the stack */
1065 XPUSHs(sv_2mortal(newSVpvn(jid
, strlen(jid
))));
1069 call_sv(callback
.connection
, G_EVAL
| G_SCALAR
);
1083 SV
*clean_newSVpv(const char *inp
)
1086 return sv_2mortal(newSVpv(inp
, 0));
1088 return sv_2mortal(&PL_sv_undef
);
1093 * @brief Forward presence information via callback.presence_in
1094 * @param jid user the @p status is being sent to
1095 * @param sender user name of the @p status sender
1096 * @param alias alternate user name of the @p sender (may be NULL)
1097 * @param group which the @p sender belongs to
1098 * @param status PurpleStatusPrimitive (guint) being sent to @p jid
1099 * @param message string being sent to @p jid (may be NULL)
1100 * @return 1 on success, 0 on failure (wrapped Perl)
1102 int thrasher_wrapper_presence_in(const char *jid
,
1106 const PurpleStatusPrimitive status
,
1107 const char *message
)
1111 /* @exception jid, sender, and group cannot be NULL */
1112 g_return_val_if_fail(jid
!= NULL
, 0);
1113 g_return_val_if_fail(sender
!= NULL
, 0);
1114 g_return_val_if_fail(group
!= NULL
, 0);
1116 /* @exception bail if our callback doesn't exist */
1117 g_return_val_if_fail(callback
.presence_in
!= NULL
, 0);
1126 /* Push the args onto the stack */
1127 XPUSHs(clean_newSVpv(jid
));
1128 XPUSHs(clean_newSVpv(sender
));
1129 XPUSHs(clean_newSVpv(alias
));
1130 XPUSHs(clean_newSVpv(group
));
1131 XPUSHs(sv_2mortal(newSViv(status
)));
1132 XPUSHs(clean_newSVpv(message
));
1136 call_sv(callback
.presence_in
, G_EVAL
| G_SCALAR
);
1150 int thrasher_wrapper_legacy_user_add_user(const char *jid
,
1151 const char *sender
) {
1154 g_return_val_if_fail(jid
, 0);
1155 g_return_val_if_fail(sender
, 0);
1157 g_return_val_if_fail(callback
.legacy_user_add_user
, 0);
1166 XPUSHs(clean_newSVpv(jid
));
1167 XPUSHs(clean_newSVpv(sender
));
1171 call_sv(callback
.legacy_user_add_user
, G_EVAL
| G_SCALAR
);
1186 * @brief Forward subscription request information via callback.subscription_add
1187 * @param jid user the @p status is being sent to
1188 * @param sender user name of the requester
1189 * @param status PurpleStatusPrimitive (guint) being sent to @p jid
1190 * @return 1 on success, 0 on failure (wrapped Perl)
1192 int thrasher_wrapper_subscription_add(const char *jid
,
1198 /* @exception jid, sender, and group cannot be NULL */
1199 g_return_val_if_fail(jid
!= NULL
, 0);
1200 g_return_val_if_fail(sender
!= NULL
, 0);
1202 /* @exception bail if our callback doesn't exist */
1203 g_return_val_if_fail(callback
.subscription_add
!= NULL
, 0);
1212 /* Push the args onto the stack */
1213 XPUSHs(clean_newSVpv(jid
));
1214 XPUSHs(clean_newSVpv(sender
));
1215 XPUSHs(sv_2mortal(newSViv(status
)));
1219 call_sv(callback
.subscription_add
, G_EVAL
| G_SCALAR
);
1234 * @brief Forward connection error messages via callback.connection_error
1235 * @param jid user the @p error_code is being sent to
1236 * @param error_code PurpleConnectionError (guint) being sent to @p jid
1237 * @param message a error message (may be NULL)
1238 * @return 1 on success, 0 on failure (wrapped Perl)
1240 int thrasher_wrapper_connection_error(const char *jid
,
1241 const guint error_code
,
1242 const char *message
)
1246 // If the JID is null, pick it up off of
1247 // thrasher.c:current_login_jid, per the comment in that file.
1249 purple_debug_info("thrasher", "had to retrieve the jid from mem\n");
1250 jid
= get_current_login_jid();
1253 // If we're in the middle of a connection, pick up that we have an error.
1256 g_return_val_if_fail(jid
, 0);
1259 /* @exception error_code cannot be less than zero or greater than PURPLE_CONNECTION_ERROR_OTHER_ERROR */
1260 g_return_val_if_fail(error_code
>= 0, ret
);
1261 g_return_val_if_fail(error_code
<= PURPLE_CONNECTION_ERROR_OTHER_ERROR
, ret
);
1270 /* Push the args onto the stack */
1271 XPUSHs(clean_newSVpv(jid
));
1272 XPUSHs(sv_2mortal(newSViv(error_code
)));
1273 XPUSHs(clean_newSVpv(message
));
1277 call_sv(callback
.connection_error
, G_EVAL
| G_SCALAR
);
1288 // Remove the account from our current active set
1289 thrasher_remove_account(jid
);
1294 void thrasher_remove_account (const char *jid
)
1296 PurpleAccount
*account
;
1298 g_return_if_fail(jid
!= NULL
);
1300 purple_debug_info("thrasher", "Removing active account for %s\n",
1303 account
= g_hash_table_lookup(accts
, jid
);
1304 if (account
!= NULL
)
1306 g_hash_table_remove(accts
, jid
);
1311 * @brief Forwards typing notification state changes from libpurple buddies.
1312 * @param pa Account receiving the notification
1313 * @param who Buddy name sending the notification
1316 void thrasher_wrapper_incoming_chatstate(PurpleAccount
* pa
,
1318 PurpleTypingState state
) {
1319 /* @exception pa and who cannot be NULL */
1320 g_return_if_fail(pa
!= NULL
);
1321 g_return_if_fail(who
!= NULL
);
1323 /* bail if Perl-side doesn't implement this */
1324 if (! callback
.incoming_chatstate
) {
1328 gchar
* jid
= thrasher_account_get_jid(pa
);
1329 /* @exception bail if account has no JID */
1330 g_return_if_fail(jid
!= NULL
);
1339 /* Push the args onto the stack */
1340 XPUSHs(clean_newSVpv(jid
));
1341 XPUSHs(clean_newSVpv(who
));
1342 XPUSHs(sv_2mortal(newSViv(state
)));
1346 call_sv(callback
.incoming_chatstate
, G_EVAL
| G_SCALAR
);
1357 * @begin External trigger to flush the internal function tables
1359 void thrasher_wrapper_destructor ()
1361 g_hash_table_destroy(callback_funcs
);
1362 g_hash_table_destroy(perl_key_lookup
);
1366 thrasher_wrapper_send_file(const char *jid
,
1371 g_return_val_if_fail(jid
, 0);
1372 g_return_val_if_fail(who
, 0);
1374 purple_debug_info("thrasher ft",
1375 "Sending file %s -> %s\n",
1378 PurpleAccount
*account
= g_hash_table_lookup(accts
, jid
);
1380 g_return_val_if_fail(account
, 0);
1382 return thrasher_send_file(account
, who
, filename
, size
, desc
);
1386 thrasher_action_ft_ui_ready(size_t id
) {
1387 PurpleXfer
* xfer
= get_xfer_by_id(id
);
1388 g_return_if_fail(xfer
);
1389 purple_xfer_ui_ready(xfer
);
1390 /* HACK: READY_PRPL is usually not set (or was cleared) right now,
1391 * and some prpls don't appear to set it correctly on repeats.
1392 * Spam prpl_ready to force a do_transfer anyway.
1394 * Using a separate watch on the remote xfer->fd would avoid some
1395 * EAGAINs. I'd be happy to be proven wrong, but testing found no
1396 * significant benefit to Thrasher in that.
1398 purple_xfer_prpl_ready(xfer
);
1402 * @brief Called on libpurple file-send-start signal
1403 * @param id of Thrasher file transfer structure.
1406 thrasher_wrapper_ft_send_start(guint id
) {
1409 /* @exception bail if our callback doesn't exist */
1410 g_return_val_if_fail(ft_callback
.ft_send_start_cb
!= NULL
, 0);
1419 /* Push the args onto the stack */
1420 XPUSHs(sv_2mortal(newSViv(id
)));
1424 call_sv(ft_callback
.ft_send_start_cb
, G_EVAL
| G_SCALAR
);
1439 * @brief Called on libpurple file-send-cancel signal
1440 * @param id of Thrasher file transfer structure.
1443 thrasher_wrapper_ft_send_cancel(guint id
) {
1444 /* @exception bail if our callback doesn't exist */
1445 g_return_if_fail(ft_callback
.ft_send_cancel_cb
!= NULL
);
1454 /* Push the args onto the stack */
1455 XPUSHs(sv_2mortal(newSViv(id
)));
1459 call_sv(ft_callback
.ft_send_cancel_cb
, G_EVAL
| G_SCALAR
);
1472 * @brief Called on libpurple file-send-complete signal
1473 * @param id of Thrasher file transfer structure.
1476 thrasher_wrapper_ft_send_complete(guint id
) {
1477 /* @exception bail if our callback doesn't exist */
1478 g_return_if_fail(ft_callback
.ft_send_complete_cb
!= NULL
);
1487 /* Push the args onto the stack */
1488 XPUSHs(sv_2mortal(newSViv(id
)));
1492 call_sv(ft_callback
.ft_send_complete_cb
, G_EVAL
| G_SCALAR
);
1505 thrasher_wrapper_ft_read(guint id
,
1511 /* @exception bail if our callback doesn't exist */
1512 g_return_val_if_fail(ft_callback
.ft_read_cb
!= NULL
, 0);
1521 /* Push the args onto the stack */
1522 XPUSHs(sv_2mortal(newSViv(id
)));
1523 XPUSHs(sv_2mortal(newSViv(size
)));
1527 count
= call_sv(ft_callback
.ft_read_cb
, G_EVAL
| G_ARRAY
);
1533 void* read
= POPpbytex
;
1534 *buffer
= malloc(read_sz
);
1535 /* This buffer is free'd by libpurple. */
1536 memcpy(*buffer
, read
, read_sz
);
1552 thrasher_wrapper_ft_data_not_sent(guint id
,
1555 /* @exception buffer cannot be NULL */
1556 g_return_if_fail(buffer
!= NULL
);
1558 /* @exception bail if our callback doesn't exist */
1559 g_return_if_fail(ft_callback
.ft_data_not_sent_cb
!= NULL
);
1568 /* Push the args onto the stack */
1569 XPUSHs(sv_2mortal(newSViv(id
)));
1570 /* buffer is not null-terminated. */
1571 XPUSHs(sv_2mortal(newSVpvn(buffer
, size
)));
1575 call_sv(ft_callback
.ft_data_not_sent_cb
, G_EVAL
| G_SCALAR
);
1588 * @brief ask user's permission for ft and set up bytestream
1589 * @param xfer a filled-in PURPLE_XFER_RECEIVE PurpleXfer.
1590 * @param filename suggested local filename
1591 * @return nothing; use thrasher_action_ft_recv_request_respond when ready
1594 thrasher_wrapper_ft_recv_request(PurpleXfer
*xfer
,
1595 const char* filename
) {
1596 /* @exception xfer cannot be NULL */
1597 g_return_if_fail(xfer
!= NULL
);
1599 /* @exception bail if our callback doesn't exist */
1600 g_return_if_fail(ft_callback
.ft_recv_request_cb
!= NULL
);
1609 /* Push the args onto the stack */
1610 guint id
= get_id_by_xfer(xfer
);
1611 XPUSHs(sv_2mortal(newSViv(id
)));
1612 PurpleAccount
* pa
= purple_xfer_get_account(xfer
);
1613 gchar
* jid
= thrasher_account_get_jid(pa
);
1614 XPUSHs(clean_newSVpv(jid
));
1615 const char* who
= purple_xfer_get_remote_user(xfer
);
1616 XPUSHs(clean_newSVpv(who
));
1617 XPUSHs(clean_newSVpv(filename
));
1618 size_t size
= purple_xfer_get_size(xfer
);
1619 XPUSHs(sv_2mortal(newSViv(size
)));
1623 call_sv(ft_callback
.ft_recv_request_cb
, G_EVAL
| G_SCALAR
);
1636 thrasher_action_ft_recv_request_respond(size_t id
,
1637 unsigned int accept
) {
1638 thrasher_xfer_recv_request_responder(id
, accept
);
1642 thrasher_wrapper_ft_write(guint id
,
1645 int written_sz
= -1;
1647 /* @exception bail if our callback doesn't exist */
1648 g_return_val_if_fail(ft_callback
.ft_write_cb
!= NULL
, 0);
1657 /* Push the args onto the stack */
1658 XPUSHs(sv_2mortal(newSViv(id
)));
1659 /* buffer is not null-terminated. */
1660 XPUSHs(sv_2mortal(newSVpvn(buffer
, size
)));
1664 call_sv(ft_callback
.ft_write_cb
, G_EVAL
| G_ARRAY
);
1679 * @brief Called on libpurple file-recv-cancel signal
1680 * @param id of Thrasher file transfer structure.
1683 thrasher_wrapper_ft_recv_cancel(guint id
) {
1684 /* @exception bail if our callback doesn't exist */
1685 g_return_if_fail(ft_callback
.ft_recv_cancel_cb
!= NULL
);
1694 /* Push the args onto the stack */
1695 XPUSHs(sv_2mortal(newSViv(id
)));
1699 call_sv(ft_callback
.ft_recv_cancel_cb
, G_EVAL
| G_SCALAR
);
1712 * @brief Called on libpurple file-recv-complete signal
1713 * @param id of Thrasher file transfer structure.
1716 thrasher_wrapper_ft_recv_complete(guint id
) {
1717 /* @exception bail if our callback doesn't exist */
1718 g_return_if_fail(ft_callback
.ft_recv_complete_cb
!= NULL
);
1727 /* Push the args onto the stack */
1728 XPUSHs(sv_2mortal(newSViv(id
)));
1732 call_sv(ft_callback
.ft_recv_complete_cb
, G_EVAL
| G_SCALAR
);
1745 thrasher_action_ft_cancel_local(size_t id
) {
1746 PurpleXfer
* xfer
= get_xfer_by_id(id
);
1747 purple_xfer_cancel_local(xfer
);
1751 /*********************************************************
1753 * Only debugging stuff beyond this point!
1755 *********************************************************/
1756 gboolean
foo_callback (void *data
);
1757 gboolean
foo_callback2 (void *data
);
1758 void foo_callback3 (gpointer data
, gint fd
, PurpleInputCondition cond
);
1759 long thrasher_wrapper_trigger_timeout_add (void);
1760 long thrasher_wrapper_trigger_timeout_add2 (void);
1761 long thrasher_wrapper_trigger_timeout_add3 (void);
1762 long thrasher_wrapper_trigger_input_add (void);
1763 void thrasher_wrapper_dump_calls (void);
1764 void spit_func_info (gpointer key
, gpointer value
, gpointer user_data
);
1768 foo_callback (void *data
)
1770 printf("!!!!!!Calling a callback w/ data [%s]!!!!\n", (char *)data
);
1779 foo_callback2 (void *data
)
1781 printf("will die in [%d] cycles\n", cntr
);
1789 void foo_callback3 (gpointer data
, gint fd
, PurpleInputCondition cond
)
1791 printf("data [%p]\tfd [%d]\tcond [%d]\n", data
, fd
, cond
);
1794 /* Test functions to allow an external caller to trigger internal actions */
1795 long thrasher_wrapper_trigger_timeout_add ()
1797 printf("trigger_timeout_add called\n");
1798 return thrasher_wrapper_set_timeout_add(1234, foo_callback
, "YAY! test data");
1802 long thrasher_wrapper_trigger_timeout_add2 ()
1804 printf("trigger_timeout_add2 called\n");
1805 return thrasher_wrapper_set_timeout_add(1867, foo_callback
, "test data says what?");
1808 long thrasher_wrapper_trigger_timeout_add3 ()
1810 printf("trigger_timeout_add3 called\n");
1811 return thrasher_wrapper_set_timeout_add(200, foo_callback2
, "this shoudl fail shortly");
1814 long thrasher_wrapper_trigger_input_add ()
1816 printf("trigger_input_add called\n");
1818 int fd
= open("/tmp/testfoo", O_RDWR
);
1819 PurpleInputCondition cond
;
1820 cond
= PURPLE_INPUT_WRITE
;
1822 printf("HAMMERTIME\n");
1823 face
= thrasher_wrapper_set_input_add(fd
, cond
, foo_callback3
, "double plus win ungood");
1828 int thrasher_wrapper_trigger_timeout_remove (long key
)
1830 printf("trigger_timeout_remove called\n");
1831 /* We currently have no way to correlate external keys to internal keys... */
1832 return !thrasher_wrapper_call_source_remove(key
);
1836 void spit_func_info (gpointer key
, gpointer value
, gpointer user_data
)
1838 hash_record
*record
;
1839 printf("key [%p]\tvalue [%p]\n", key
, value
);
1843 if (record
->type
== INPUT_ADD
)
1845 printf("\tINPUT_ADD\n\tfunction [%p]\n\tdata [%p]\n\tcond [%d]\n\tfd [%d]\n", record
->function
, record
->data
, record
->cond
, record
->fd
);
1848 else if (record
->type
== TIMEOUT_ADD
)
1851 printf("\tTIMEOUT_ADD\n\tfunction [%p]\n\tdata [%p]\n", record
->function
, record
->data
);
1856 printf("record type [%d] is unrecognized!\n", record
->type
);
1860 void thrasher_wrapper_dump_calls ()
1862 g_hash_table_foreach(callback_funcs
, spit_func_info
, NULL
);
1865 #endif /* TH_DEBUG */