using libpurple

dimasabyrov dimasabyrov at rambler.ru
Thu Jul 14 09:45:45 EDT 2011


Hello I'm trying to use libpurple by windows.
I have this problem. I cannot connect jabber account
In what may be the problem?

#include <Windows.h>
#include "util.h"
#include "debug.h"
#include "eventloop.h"
#include "plugin.h"
#include "core.h"
#include "connection.h"
#include "win32dep.h"
#include "blist.h"
#include "pref.h"
#include "pounce.h"
#include <stdio.h>
#include "signals.h"
#include "conversation.h"
#include "savedstatus.h"

#define CUSTOM_USER_DIRECTORY  "/dev/null"
#define PIDGIN_READ_COND  (G_IO_IN | G_IO_HUP | G_IO_ERR)
#define PIDGIN_WRITE_COND (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL)
#define CUSTOM_PLUGIN_PATH     "plugins/"
#define UI_ID                  "user"
#define PLUGIN_SAVE_PREF       "/purple/user/plugins/saved"

static void report_disconnect_reason(PurpleConnection *gc,  
PurpleConnectionError reason, const char *text)
{
	HINSTANCE dllpurple= LoadLibraryA("libpurple.dll");
	Purple_connection_get_account purple_connection_get_account  
=(Purple_connection_get_account)  
GetProcAddress(dllpurple,"purple_connection_get_account");
	Purple_account_get_username purple_account_get_username =  
(Purple_account_get_username)  
GetProcAddress(dllpurple,"purple_account_get_username");
	Purple_account_get_protocol_id purple_account_get_protocol_id =  
(Purple_account_get_protocol_id)  
GetProcAddress(dllpurple,"purple_account_get_protocol_id");
	PurpleAccount *account = purple_connection_get_account(gc);
	printf("Connection disconnected: \"%s\" (%s)\n  >Error: %d\n  >Reason:  
%s\n", purple_account_get_username(account),  
purple_account_get_protocol_id(account), reason, text);
	FreeLibrary(dllpurple);
}

void network_disconnected(void)
{

	printf("This machine has been disconnected from the internet\n");

}

void connect_progress (PurpleConnection *gc,const char *text,	size_t  
step,	size_t step_count)
{

}

void connected (PurpleConnection *gc)
{

}

void disconnected(PurpleConnection *gc)
{

}

void network_connected ()
{

}

void notice(PurpleConnection *gc, const char *text)
{

}

void report_disconnect(PurpleConnection *gc, const  char *text)
{

}
PurpleConnectionUiOps connection_uiops =
{
	connect_progress,                      /* connect_progress         */
	connected,                      /* connected                */
	disconnected,                      /* disconnected             */
	notice,                      /* notice                   */
	report_disconnect,                      /* report_disconnect        */
	network_connected,                      /* network_connected        */
	network_disconnected,      /* network_disconnected     */
	report_disconnect_reason,  /* report_disconnect_reason */
	NULL,
	NULL,
	NULL
};

void *request_authorize(PurpleAccount *account, const char *remote_user,  
const char *id, const char *alias, const char *message, gboolean on_list,  
PurpleAccountRequestAuthorizationCb authorize_cb,  
PurpleAccountRequestAuthorizationCb deny_cb, void *user_data)
{
     authorize_cb(user_data);
	return NULL;
}

PurpleAccountUiOps account_uiops =
{
	NULL,                      /* notify_added          */
	NULL,                      /* status_changed        */
	NULL,                      /* request_add           */
	request_authorize,         /* request_authorize     */
	NULL,                      /* close_account_request */
	NULL,
	NULL,
	NULL,
	NULL
};

void ui_init(void)
{
	HINSTANCE dllPurple = LoadLibraryA("libpurple.dll");

	Purple_connections_set_ui_ops purple_connections_set_ui_ops  
=(Purple_connections_set_ui_ops)  
GetProcAddress(dllPurple,"purple_connections_set_ui_ops");
	/**
	 * This should initialize the UI components for all the modules.
	 */

	Purple_accounts_set_ui_ops purple_accounts_set_ui_ops  
=(Purple_accounts_set_ui_ops)  
GetProcAddress(dllPurple,"purple_accounts_set_ui_ops");

	purple_accounts_set_ui_ops(&account_uiops);
	purple_connections_set_ui_ops(&connection_uiops);

	Purple_accounts_init purple_accounts_init =(Purple_accounts_init)  
GetProcAddress(dllPurple,"purple_accounts_init");
	purple_accounts_init();

	Purple_connections_init purple_connections_init =  
(Purple_connections_init)  
GetProcAddress(dllPurple,"purple_connections_init");
	purple_connections_init();

	FreeLibrary(dllPurple);

}

PurpleCoreUiOps core_uiops =
{
	NULL,
	NULL,
	ui_init,
	NULL,

	NULL,
	NULL,
	NULL,
	NULL
};

typedef struct _PurpleGLibIOClosure {
	PurpleInputFunction function;
	guint result;
	gpointer data;
} PurpleGLibIOClosure;

gboolean pidgin_io_invoke(GIOChannel *source, GIOCondition condition,  
gpointer data)
{
	PurpleGLibIOClosure *closure2 = static_cast<PurpleGLibIOClosure*>(data);
	PurpleInputCondition purple_cond =PurpleInputCondition( 0);

	if (condition & PIDGIN_READ_COND)
		purple_cond = PURPLE_INPUT_READ;
	if (condition & PIDGIN_WRITE_COND)
		purple_cond = PURPLE_INPUT_WRITE;

	if(! purple_cond) {

		return TRUE;
	}

	closure2->function(closure2->data, g_io_channel_unix_get_fd(source),
		purple_cond);

	return TRUE;
}

guint glib_input_add(gint fd, PurpleInputCondition condition,  
PurpleInputFunction function,
	gpointer data)
{

	PurpleGLibIOClosure *closure = g_new0(PurpleGLibIOClosure, 1);
	GIOCondition cond = GIOCondition(0);
	GIOChannel *channel;
	int use_glib_io_channel = -1;

	if (use_glib_io_channel == -1)
		use_glib_io_channel = (g_getenv("PIDGIN_GLIB_IO_CHANNEL") != NULL) ? 1 :  
0;

	closure->function = function;
	closure->data = data;

	if (condition & PURPLE_INPUT_READ)
		cond = GIOCondition(PIDGIN_READ_COND);
	if (condition & PURPLE_INPUT_WRITE)
		cond = GIOCondition(PIDGIN_WRITE_COND);

	HINSTANCE dllPurple = LoadLibraryA("libpurple.dll");

	Wpurple_g_io_channel_win32_new_socket  
wpurple_g_io_channel_win32_new_socket  
=(Wpurple_g_io_channel_win32_new_socket)  
GetProcAddress(dllPurple,"wpurple_g_io_channel_win32_new_socket");
	channel = wpurple_g_io_channel_win32_new_socket(fd);
	closure->result = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond,
		pidgin_io_invoke, closure, g_free);

	g_io_channel_unref(channel);
	return closure->result;
}

PurpleEventLoopUiOps glib_eventloops =
{
	g_timeout_add,
	g_source_remove,
	glib_input_add,
	g_source_remove,
	NULL,
#if GLIB_CHECK_VERSION(2,14,0)
	g_timeout_add_seconds,
#else
	NULL,
#endif

	NULL,
	NULL,
	NULL
};

void init_libpurple()
{
	HINSTANCE dllpPurple = LoadLibraryA("libpurple.dll");
	Purple_util_set_user_dir purple_util_set_user_dir  
=(Purple_util_set_user_dir)  
GetProcAddress(dllpPurple,"purple_util_set_user_dir");
	Purple_debug_set_enabled purple_debug_set_enabled =  
(Purple_debug_set_enabled)  
GetProcAddress(dllpPurple,"purple_debug_set_enabled");
	Purple_core_set_ui_ops purple_core_set_ui_ops = (Purple_core_set_ui_ops)  
GetProcAddress(dllpPurple,"purple_core_set_ui_ops");
	Purple_eventloop_set_ui_ops purple_eventloop_set_ui_ops  
=(Purple_eventloop_set_ui_ops)  
GetProcAddress(dllpPurple,"purple_eventloop_set_ui_ops");
	Purple_plugins_add_search_path purple_plugins_add_search_path =  
(Purple_plugins_add_search_path)  
GetProcAddress(dllpPurple,"purple_plugins_add_search_path");
	Purple_core_init purple_core_init =(Purple_core_init)  
GetProcAddress(dllpPurple,"purple_core_init");
	Purple_set_blist purple_set_blist =(Purple_set_blist)  
GetProcAddress(dllpPurple,"purple_set_blist");
	Purple_blist_new purple_blist_new =(Purple_blist_new)  
GetProcAddress(dllpPurple,"purple_blist_new");
	Purple_blist_load purple_blist_load = (Purple_blist_load)  
GetProcAddress(dllpPurple,"purple_blist_load");
	Purple_prefs_load purple_prefs_load = (Purple_prefs_load)  
GetProcAddress(dllpPurple,"purple_prefs_load");
	Purple_plugins_load_saved purple_plugins_load_saved =  
(Purple_plugins_load_saved)  
GetProcAddress(dllpPurple,"purple_plugins_load_saved");
	Purple_pounces_load purple_pounces_load = (Purple_pounces_load)  
GetProcAddress(dllpPurple,"purple_pounces_load");

	/* Set a custom user directory (optional) */
	purple_util_set_user_dir(CUSTOM_USER_DIRECTORY);

	/* We do not want any debugging for now to keep the noise to a minimum. */
	purple_debug_set_enabled(FALSE);

	/* Set the core-uiops, which is used to
	 * 	- initialize the ui specific preferences.
	 * 	- initialize the debug ui.
	 * 	- initialize the ui components for all the modules.
	 * 	- uninitialize the ui components for all the modules when the core  
terminates.
	 */
	purple_core_set_ui_ops(&core_uiops);

	/* Set the uiops for the eventloop. If your client is glib-based, you can  
safely
	 * copy this verbatim. */
	purple_eventloop_set_ui_ops(&glib_eventloops);

	/* Set path to search for plugins. The core (libpurple) takes care of  
loading the
	 * core-plugins, which includes the protocol-plugins. So it is not  
essential to add
	 * any path here, but it might be desired, especially for ui-specific  
plugins. */
	purple_plugins_add_search_path(CUSTOM_PLUGIN_PATH);

	/* Now that all the essential stuff has been set, let's try to init the  
core. It's
	 * necessary to provide a non-NULL name for the current ui to the core.  
This name
	 * is used by stuff that depends on this ui, for example the ui-specific  
plugins. */
	if (!purple_core_init(UI_ID))
	{
		/* Initializing the core failed. Terminate. */
		fprintf(stderr,
				"libpurple initialization failed. Dumping core.\n"
				"Please report this!\n");
		abort();
	}

	/* Create and load the buddylist. */
	purple_set_blist(purple_blist_new());
	purple_blist_load();

	/* Load the preferences. */
	purple_prefs_load();

	/* Load the desired plugins. The client should save the list of loaded  
plugins in
	 * the preferences using purple_plugins_save_loaded(PLUGIN_SAVE_PREF) */
	purple_plugins_load_saved(PLUGIN_SAVE_PREF);

	/* Load the pounces. */
	purple_pounces_load();
	FreeLibrary(dllpPurple);
}

void signed_on(PurpleConnection *gc)
{
	HINSTANCE dllpurple= LoadLibraryA("libpurple.dll");

	Purple_connection_get_account purple_connection_get_account =  
(Purple_connection_get_account)  
GetProcAddress(dllpurple,"purple_connection_get_account");
	Purple_account_get_username purple_account_get_username =  
(Purple_account_get_username)  
GetProcAddress(dllpurple,"purple_account_get_username");
	Purple_account_get_protocol_id purple_account_get_protocol_id =  
(Purple_account_get_protocol_id)  
GetProcAddress(dllpurple,"purple_account_get_protocol_id");

	PurpleAccount *account = purple_connection_get_account(gc);
	printf("Account connected: \"%s\" (%s)\n",  
purple_account_get_username(account),  
purple_account_get_protocol_id(account));

	FreeLibrary(dllpurple);
}

void received_im_msg(PurpleAccount *account, char *sender, char *message,  
PurpleConversation *conv, PurpleMessageFlags flags)
{
	HINSTANCE dllpurple = LoadLibraryA("libpurple.dll");
	Purple_conversation_new purple_conversation_new  
=(Purple_conversation_new)  
GetProcAddress(dllpurple,"purple_conversation_new");
	Purple_utf8_strftime purple_utf8_strftime = (Purple_utf8_strftime)  
GetProcAddress(dllpurple,"purple_utf8_strftime");
	Purple_conversation_get_name purple_conversation_get_name =  
(Purple_conversation_get_name)  
GetProcAddress(dllpurple,"purple_conversation_get_name");

	if (conv==NULL)
	{
		conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, account, sender);
	}

	printf("(%s) %s (%s): %s\n", purple_utf8_strftime("%H:%M:%S", NULL),  
sender, purple_conversation_get_name(conv), message);

}

static void buddy_signed_off(PurpleBuddy *buddy)
{
}

static void buddy_away(PurpleBuddy *buddy, PurpleStatus *old_status,  
PurpleStatus *status)
{
}

static void buddy_idle(PurpleBuddy *buddy, gboolean old_idle, gboolean  
idle)
{
}


static void buddy_typing(PurpleAccount *account, const char *name)
{
}

static void buddy_typed(PurpleAccount *account, const char *name) //not  
supported on all protocols
{
}

void buddy_typing_stopped(PurpleAccount *account, const char *name)
{
}

int account_authorization_requested(PurpleAccount *account, const char  
*user)
{
}

void buddy_signed_on(PurpleBuddy *buddy)
{
}

void connect_to_signals(void)
{
	static int handle;
	HINSTANCE dllpurple = LoadLibraryA("libpurple.dll");
	Purple_signal_connect purple_signal_connect =(Purple_signal_connect)  
GetProcAddress(dllpurple,"purple_signal_connect");
	Purple_connections_get_handle purple_connections_get_handle  
=(Purple_connections_get_handle)  
GetProcAddress(dllpurple,"purple_connections_get_handle");
	Purple_blist_get_handle purple_blist_get_handle  
=(Purple_blist_get_handle)  
GetProcAddress(dllpurple,"purple_blist_get_handle");
	Purple_conversations_get_handle purple_conversations_get_handle  
=(Purple_conversations_get_handle)  
GetProcAddress(dllpurple,"purple_conversations_get_handle");
	Purple_accounts_get_handle purple_accounts_get_handle  
=(Purple_accounts_get_handle)  
GetProcAddress(dllpurple,"purple_accounts_get_handle");


	purple_signal_connect(purple_connections_get_handle(), "signed-on",  
&handle,
		PURPLE_CALLBACK(signed_on), NULL);

	purple_signal_connect(purple_blist_get_handle(), "buddy-signed-on",  
&handle,
		PURPLE_CALLBACK(buddy_signed_on), NULL);

	purple_signal_connect(purple_blist_get_handle(), "buddy-signed-off",  
&handle,
		PURPLE_CALLBACK(buddy_signed_off), NULL);

	purple_signal_connect(purple_blist_get_handle(), "buddy-status-changed",  
&handle,
		PURPLE_CALLBACK(buddy_away), NULL);

	purple_signal_connect(purple_blist_get_handle(), "buddy-idle-changed",  
&handle,
		PURPLE_CALLBACK(buddy_idle), NULL);

	purple_signal_connect(purple_conversations_get_handle(),  
"received-im-msg", &handle,
		PURPLE_CALLBACK(received_im_msg), NULL);

	purple_signal_connect(purple_conversations_get_handle(), "buddy-typing",  
&handle,
		PURPLE_CALLBACK(buddy_typing), NULL);

	purple_signal_connect(purple_conversations_get_handle(), "buddy-typed",  
&handle,
		PURPLE_CALLBACK(buddy_typed), NULL);

	purple_signal_connect(purple_conversations_get_handle(),  
"buddy-typing-stopped", &handle,
		PURPLE_CALLBACK(buddy_typing_stopped), NULL);

	purple_signal_connect(purple_accounts_get_handle(),  
"account-authorization-requested", &handle,
		PURPLE_CALLBACK(account_authorization_requested), NULL);

}


#define JABBER_DEFAULT_FT_PROXIES     "proxy.eu.jabber.org"

int main()
{
	HINSTANCE dllpurple = LoadLibraryA("libpurple.dll");

	g_set_application_name("test_purple");

	Purple_core_get_version purple_core_get_version  
=(Purple_core_get_version)  
GetProcAddress(dllpurple,"purple_core_get_version");
	Purple_account_new purple_account_new =(Purple_account_new)  
GetProcAddress(dllpurple,"purple_account_new");
	Purple_account_set_password purple_account_set_password =  
(Purple_account_set_password)  
GetProcAddress(dllpurple,"purple_account_set_password");
	Purple_accounts_add purple_accounts_add = (Purple_accounts_add)  
GetProcAddress(dllpurple,"purple_accounts_add");
	Purple_account_set_enabled purple_account_set_enabled =  
(Purple_account_set_enabled)  
GetProcAddress(dllpurple,"purple_account_set_enabled");
	Purple_savedstatus_new purple_savedstatus_new =(Purple_savedstatus_new)  
GetProcAddress(dllpurple,"purple_savedstatus_new");
	Purple_savedstatus_activate_for_account  
purple_savedstatus_activate_for_account=(Purple_savedstatus_activate_for_account)  
GetProcAddress(dllpurple,"purple_savedstatus_activate_for_account");

	Purple_account_set_bool purple_account_set_bool  
=(Purple_account_set_bool)  
GetProcAddress(dllpurple,"purple_account_set_bool");
	Purple_account_set_int purple_account_set_int =(Purple_account_set_int)  
GetProcAddress(dllpurple,"purple_account_set_int");
	Purple_account_set_string purple_account_set_string =  
(Purple_account_set_string)  
GetProcAddress(dllpurple,"purple_account_set_string");
	Purple_core_ensure_single_instance purple_core_ensure_single_instance  
=(Purple_core_ensure_single_instance)  
GetProcAddress(dllpurple,"purple_core_ensure_single_instance");

	init_libpurple();
	printf("libpurple initialized. Running version %s.\n",  
purple_core_get_version());

	if(!purple_core_ensure_single_instance())
	{

	}
	
	connect_to_signals();

	PurpleAccount *account = purple_account_new("dimasabyrov at jabber.ru",  
"prpl-jabber");

	Purple_account_set_alias purple_account_set_alias  
=(Purple_account_set_alias)  
GetProcAddress(dllpurple,"purple_account_set_alias");
	purple_account_set_alias (account,NULL);

	Purple_account_set_remember_password purple_account_set_remember_password  
=(Purple_account_set_remember_password) GetProcAddress  
(dllpurple,"purple_account_set_remember_password");
	purple_account_set_remember_password(account, TRUE);
	purple_account_set_password(account, "xxxxxxxxx");

	Purple_account_set_username purple_account_set_username  
=(Purple_account_set_username)  
GetProcAddress(dllpurple,"purple_account_set_username");
	purple_account_set_username(account, "dimasabyrov at jabber.ru");

	purple_account_set_bool(account,"auth_plain_in_clear", FALSE);
         purple_account_set_int(account, "port", 5222);
	purple_account_set_string(account, "connect_server", "jabber.ru");
	purple_account_set_string(account, "ft_proxies",  
JABBER_DEFAULT_FT_PROXIES);


	Purple_account_get_proxy_info purple_account_get_proxy_info  
=(Purple_account_get_proxy_info)  
GetProcAddress(dllpurple,"purple_account_get_proxy_info");

	PurpleProxyInfo *proxy_info = purple_account_get_proxy_info(account);
	if(proxy_info== NULL)
	{
		Purple_proxy_info_new purple_proxy_info_new =(Purple_proxy_info_new)  
GetProcAddress(dllpurple,"purple_proxy_info_new");
		proxy_info = purple_proxy_info_new();
	}

	Purple_proxy_info_set_type purple_proxy_info_set_type  
=(Purple_proxy_info_set_type)  
GetProcAddress(dllpurple,"purple_proxy_info_set_type");
	purple_proxy_info_set_type(proxy_info, PURPLE_PROXY_NONE);

	Purple_proxy_info_set_host purple_proxy_info_set_host =  
(Purple_proxy_info_set_host)  
GetProcAddress(dllpurple,"purple_proxy_info_set_host");
	purple_proxy_info_set_host(proxy_info,NULL);

	Purple_proxy_info_set_port purple_proxy_info_set_port  
=(Purple_proxy_info_set_port)  
GetProcAddress(dllpurple,"purple_proxy_info_set_port");
	purple_proxy_info_set_port(proxy_info, 0);

	Purple_proxy_info_set_username purple_proxy_info_set_username  
=(Purple_proxy_info_set_username)  
GetProcAddress(dllpurple,"purple_proxy_info_set_username");
	purple_proxy_info_set_username(proxy_info,NULL);

	Purple_proxy_info_set_password purple_proxy_info_set_password  
=(Purple_proxy_info_set_password)  
GetProcAddress(dllpurple,"purple_proxy_info_set_password");
	purple_proxy_info_set_password(proxy_info, NULL);

	Purple_account_register purple_account_register  
=(Purple_account_register)  
GetProcAddress(dllpurple,"purple_account_register");

	purple_accounts_add(account);

	PurpleSavedStatus *status = purple_savedstatus_new(NULL,  
PURPLE_STATUS_AVAILABLE);
	purple_savedstatus_activate_for_account(status,account);
	purple_account_set_enabled(account, UI_ID, TRUE);
	
	FreeLibrary(dllpurple);
	
}




More information about the Support mailing list