Marco Trevisan (Treviño) wrote:
> Unfortunately testing it with the msn-purple plugin (I've tried also
> with Facebook, but I've some issues with the certs) the test client
> attached segfaults. As you can easily try (but I could attach also some
> logs, if you need) the client runs correctly and initializes the
> connection, however after getting and sending some data (wireshark
> confirms it too :P), it crashes. :(

Ok, I've made some work on this and I've found that the connection
didn't continue because if ecore_purple_input_add was asked for a
previously used fd, it create a new fd_handler instead of using the
existing one.
After this modification (the patch and the fixed client are attached
here), I was able to get Msn connected too without getting anymore the
errors I posted initially.

However, unfortunately, the bugs are not over! :(

I continued my tests with msn, using the "prpl-msn" protocol. Now, if I
enable the "http_method" option I can connect to it (and receive
messages) correctly with no experienced segfaults.

By the way, if use the standard connection, the client connects but as
soon as the connection is established it segfaults.
Gdb doesn't help me so much (also if I've installed libe* and libpurple
with debugging symbols, so it must come from some other place) giving me
this:
 0xb7ce2c23 in strlen () from /lib/tls/i686/cmov/libc.so.6

It seems like when you strlen a null pointer, but I can't understand why
and where it happens. :|

The only thing that the libpurple debug says me before the crash is
something like:
 (18:52:40) dns: Got response for '(null)'
 (18:52:55) dns: Got response for '\x21\x55\x94'
And this seems all a little strange, since they doesn't seem right dns
requests. Maybe the bugs stays there... Mhmhmh....

Testing again the client with Google talk (so a xmpp server using the
"prpl-jabber" protocol), it continues working like before (i.e.:
connects, gets IMs), but I always get random crashes after that the
connection has been established with ecore that reports:
    max_fd: 98
    cr: 2, cw: 0, ce: 0
    Aborted
I don't really know why this happens (and neither what it means exactly,
I guess something like "too many fd handlers resources requested"?!),
however it never happens after a first fresh compilation.
It uses to happen after that I kill an instance and I re-run it many
times... It's all so weird for me :|.

Those are the tests I can do currently, please could you report your
experiences too? Thanks!

-- 
Treviño's World - Life and Linux
http://www.3v1n0.net/
/*
 * nullclient-ecore.c - an ecore libpurple wrapper test.
 * Copyright (C) 2009, Marco Trevisan (Treviño) <m...@3v1n0.net>
 *
 * Pidgin is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 *
 */


#include "purple.h"

#include <Ecore.h>
#include <eina_list.h>

#include <signal.h>
#include <string.h>
#include <unistd.h>

// Client related settings
#define CUSTOM_USER_DIRECTORY  "/dev/null"
#define CUSTOM_PLUGIN_PATH     ""
#define PLUGIN_SAVE_PREF       "/purple/nullclient-ecore/plugins/saved"
#define UI_ID                  "nullclient-ecore"


/*** Event uiops ***/
static guint loopID = 0;
Eina_List *loopsList = NULL;

typedef struct _timerRef {
	Ecore_Timer *timer;
	guint id;
} timerRef;

typedef struct _fdRef {
	PurpleInputFunction function;
	Ecore_Fd_Handler_Flags condition;
	Ecore_Fd_Handler *fdh;
	guint id;
	void *data;
} fdRef;

static guint ecore_purple_timeout_add(guint interval, GSourceFunc function, gpointer data) {
	timerRef *ref = calloc(1, sizeof(timerRef));
	double timer_interval = ((double) interval)/1000;
	
	ref->timer = ecore_timer_add(timer_interval, function, data);
	ref->id = ++loopID;
	loopsList = eina_list_append(loopsList, ref);
	
	return ref->id;
}

static gboolean ecore_purple_timeout_remove(guint handle) {
	timerRef *ref;
	Eina_List *tlist;
	
	EINA_LIST_FOREACH(loopsList, tlist, ref) {
		if (ref->id == handle) {
			if (ref->timer)
				ecore_timer_del(ref->timer);
			loopsList = eina_list_remove(loopsList, ref);
			free(ref);
			return TRUE;
		}
	}
	
	return FALSE;
}

static int ecore_purple_input_cb(void *data, Ecore_Fd_Handler *fd_handler) {
	fdRef *ref = data;
	PurpleInputCondition purple_cond = 0;

	if (ref->condition & ECORE_FD_READ)
		purple_cond |= PURPLE_INPUT_READ;

	if (ref->condition & ECORE_FD_WRITE)
		purple_cond |= PURPLE_INPUT_WRITE;

	if (ref->function) {
		ref->function(ref->data, ecore_main_fd_handler_fd_get(fd_handler), purple_cond);
		return 1;
	} else
		return 0;
}

static guint ecore_purple_input_add(int fd, PurpleInputCondition condition,
				    PurpleInputFunction func, gpointer user_data) {
	Ecore_Fd_Handler_Flags fdflags = 0;
	Eina_List *tlist;
	fdRef *ref;
	
	if (fd < 0)
		return ++loopID;

	if (condition & PURPLE_INPUT_READ) {
		fdflags |= ECORE_FD_READ;
		fdflags |= ECORE_FD_ERROR;
	}

	if (condition & PURPLE_INPUT_WRITE) {
		fdflags |= ECORE_FD_WRITE;
		fdflags |= ECORE_FD_ERROR;
	}

	EINA_LIST_FOREACH(loopsList, tlist, ref) {
		if (ref && ref->fdh) {
			if (ecore_main_fd_handler_fd_get(ref->fdh) == fd) {
				break;
			}
		}
	}

	if (!ref) {
		ref = calloc(1, sizeof(fdRef));
		ref->fdh = ecore_main_fd_handler_add(fd, fdflags, ecore_purple_input_cb, ref, NULL, NULL);
		loopsList = eina_list_append(loopsList, ref);
	} else {
		ecore_main_fd_handler_active_set(ref->fdh, fdflags);
	}

	ref->id = ++loopID;
	ref->data = user_data;
	ref->function = func;

	return ref->id;
}

static gboolean ecore_purple_input_remove(guint handle) {
	fdRef *ref;
	Eina_List *tlist;
	
	EINA_LIST_FOREACH(loopsList, tlist, ref) {
		if (ref && ref->id == handle) {
			if (ref->fdh)
				ecore_main_fd_handler_del(ref->fdh);
			loopsList = eina_list_remove(loopsList, ref);
			free(ref);
			return TRUE;
		}
	}
	
	return FALSE;
}

static PurpleEventLoopUiOps ecore_eventloops =
{
	ecore_purple_timeout_add,
	ecore_purple_timeout_remove,
	ecore_purple_input_add,
	ecore_purple_input_remove,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL
};
/*** End of the eventloop functions. ***/

/*** Conversation uiops ***/
static void
null_write_conv(PurpleConversation *conv, const char *who, const char *alias,
			const char *message, PurpleMessageFlags flags, time_t mtime)
{
	const char *name;
	if (alias && *alias)
		name = alias;
	else if (who && *who)
		name = who;
	else
		name = NULL;

	printf("(%s) %s %s: %s\n", purple_conversation_get_name(conv),
			purple_utf8_strftime("(%H:%M:%S)", localtime(&mtime)),
			name, message);
}

static PurpleConversationUiOps null_conv_uiops = 
{
	NULL,                      /* create_conversation  */
	NULL,                      /* destroy_conversation */
	NULL,                      /* write_chat           */
	NULL,                      /* write_im             */
	null_write_conv,           /* write_conv           */
	NULL,                      /* chat_add_users       */
	NULL,                      /* chat_rename_user     */
	NULL,                      /* chat_remove_users    */
	NULL,                      /* chat_update_user     */
	NULL,                      /* present              */
	NULL,                      /* has_focus            */
	NULL,                      /* custom_smiley_add    */
	NULL,                      /* custom_smiley_write  */
	NULL,                      /* custom_smiley_close  */
	NULL,                      /* send_confirm         */
	NULL,
	NULL,
	NULL,
	NULL
};

static void
null_ui_init(void)
{
	/**
	 * This should initialize the UI components for all the modules. Here we
	 * just initialize the UI for conversations.
	 */
	purple_conversations_set_ui_ops(&null_conv_uiops);

}

static PurpleCoreUiOps null_core_uiops =
{
	NULL,
	NULL,
	null_ui_init,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL
};

static void
init_libpurple(void)
{
	/* 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(TRUE);

	/* 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(&null_core_uiops);

	/* Set the uiops for the eventloop. */
	purple_eventloop_set_ui_ops(&ecore_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();
}

static void
signed_on(PurpleConnection *gc, gpointer null)
{
	PurpleBlistNode *node;
	
	PurpleAccount *account = purple_connection_get_account(gc);
	printf("Account connected: %s %s\n", account->username, account->protocol_id);
	
	for (node = purple_blist_get_root(); node;
		node = purple_blist_node_next(node, FALSE)) {
		if (node)
			printf("Contact: %s\n", node);
	}
}

static void
connect_to_signals_for_demonstration_purposes_only(void)
{
	static int handle;
	purple_signal_connect(purple_connections_get_handle(), "signed-on", &handle,
				PURPLE_CALLBACK(signed_on), NULL);
}

int main(int argc, char *argv[])
{
	const char *prpl;
	char name[128];
	char *password;
	PurpleAccount *account;
	PurpleSavedStatus *status;
	char *res;

	/* libpurple's built-in DNS resolution forks processes to perform
	 * blocking lookups without blocking the main process.  It does not
	 * handle SIGCHLD itself, so if the UI does not you quickly get an army
	 * of zombie subprocesses marching around.
	 */
	signal(SIGCHLD, SIG_IGN);
	
	ecore_init();
	eina_list_init();

	init_libpurple();

	printf("libpurple initialized.\n");

	int i, num;
	GList *iter;
	Eina_List *names = NULL;
	iter = purple_plugins_get_protocols();
	for (i = 0; iter; iter = iter->next) {
		PurplePlugin *plugin = iter->data;
		PurplePluginInfo *info = plugin->info;
		if (info && info->name) {
			printf("\t%d: %s\n", i++, info->name);
			names = eina_list_append(names, info->id);
		}
	}
	
	printf("Select the protocol [0-%d]: ", i-1);
	res = fgets(name, sizeof(name), stdin);
	if (!res) {
		fprintf(stderr, "Failed to gets protocol selection.");
		abort();
	}
	sscanf(name, "%d", &num);
	prpl = eina_list_nth(names, num);

	printf("Username: ");
	res = fgets(name, sizeof(name), stdin);
	if (!res) {
		fprintf(stderr, "Failed to read user name.");
		abort();
	}
	name[strlen(name) - 1] = 0;  /* strip the \n at the end */

	/* Create the account */
	account = purple_account_new(name, prpl);

	/* Get the password for the account */
	password = getpass("Password: ");
	purple_account_set_password(account, password);

	/* It's necessary to enable the account first. */
	purple_account_set_enabled(account, UI_ID, TRUE);

	/* Now, to connect the account(s), create a status and activate it. */
	status = purple_savedstatus_new(NULL, PURPLE_STATUS_INVISIBLE);
	purple_savedstatus_activate(status);

	connect_to_signals_for_demonstration_purposes_only();

	ecore_main_loop_begin();

	ecore_main_loop_quit();
	
	eina_list_shutdown();
	
	return 0;
}

--- a/nullclient-ecore.c	2009-04-01 00:51:42.000000000 +0200
+++ b/nullclient-ecore.c	2009-04-02 17:32:52.000000000 +0200
@@ -94,7 +98,7 @@
 	if (ref->condition & ECORE_FD_WRITE)
 		purple_cond |= PURPLE_INPUT_WRITE;
 
-	if (ref->function && ecore_main_fd_handler_active_get(fd_handler, ref->condition)) {
+	if (ref->function) {
 		ref->function(ref->data, ecore_main_fd_handler_fd_get(fd_handler), purple_cond);
 		return 1;
 	} else
@@ -103,24 +107,42 @@
 
 static guint ecore_purple_input_add(int fd, PurpleInputCondition condition,
 				    PurpleInputFunction func, gpointer user_data) {
-	fdRef *ref = calloc(1, sizeof(fdRef));
+	Ecore_Fd_Handler_Flags fdflags = 0;
+	Eina_List *tlist;
+	fdRef *ref;
 	
 	if (fd < 0)
 		return ++loopID;
-		
-	ref->condition = 0;
-	
-	if (condition & PURPLE_INPUT_READ)
-		ref->condition |= ECORE_FD_READ;
 
-	if (condition & PURPLE_INPUT_WRITE)
-		ref->condition |= ECORE_FD_WRITE;
-		
+	if (condition & PURPLE_INPUT_READ) {
+		fdflags |= ECORE_FD_READ;
+		fdflags |= ECORE_FD_ERROR;
+	}
+
+	if (condition & PURPLE_INPUT_WRITE) {
+		fdflags |= ECORE_FD_WRITE;
+		fdflags |= ECORE_FD_ERROR;
+	}
+
+	EINA_LIST_FOREACH(loopsList, tlist, ref) {
+		if (ref && ref->fdh) {
+			if (ecore_main_fd_handler_fd_get(ref->fdh) == fd) {
+				break;
+			}
+		}
+	}
+
+	if (!ref) {
+		ref = calloc(1, sizeof(fdRef));
+		ref->fdh = ecore_main_fd_handler_add(fd, fdflags, ecore_purple_input_cb, ref, NULL, NULL);
+		loopsList = eina_list_append(loopsList, ref);
+	} else {
+		ecore_main_fd_handler_active_set(ref->fdh, fdflags);
+	}
+
 	ref->id = ++loopID;
 	ref->data = user_data;
 	ref->function = func;
-	ref->fdh = ecore_main_fd_handler_add(fd, ref->condition, ecore_purple_input_cb, ref, NULL, NULL);
-	loopsList = eina_list_append(loopsList, ref);
 
 	return ref->id;
 }
@@ -130,7 +152,7 @@
 	Eina_List *tlist;
 	
 	EINA_LIST_FOREACH(loopsList, tlist, ref) {
-		if (ref->id == handle) {
+		if (ref && ref->id == handle) {
 			if (ref->fdh)
 				ecore_main_fd_handler_del(ref->fdh);
 			loopsList = eina_list_remove(loopsList, ref);
------------------------------------------------------------------------------
_______________________________________________
enlightenment-devel mailing list
enlightenment-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/enlightenment-devel

Reply via email to