diff options
Diffstat (limited to 'protocols')
41 files changed, 6469 insertions, 1006 deletions
| diff --git a/protocols/Makefile b/protocols/Makefile index 18d79e8d..d4aa6e14 100644 --- a/protocols/Makefile +++ b/protocols/Makefile @@ -7,9 +7,13 @@  ### DEFINITIONS  -include ../Makefile.settings +ifdef SRCDIR +SRCDIR := $(SRCDIR)protocols/ +endif  # [SH] Program variables -objects = nogaim.o +objects = account.o bee.o bee_chat.o bee_ft.o bee_user.o nogaim.o +  # [SH] The next two lines should contain the directory name (in $(subdirs))  #      and the name of the object file, which should be linked into @@ -48,6 +52,6 @@ protocols.o: $(objects) $(subdirs)  $(objects): ../Makefile.settings Makefile -$(objects): %.o: %.c +$(objects): %.o: $(SRCDIR)%.c  	@echo '*' Compiling $<  	@$(CC) -c $(CFLAGS) $< -o $@ diff --git a/protocols/account.c b/protocols/account.c new file mode 100644 index 00000000..0bacea74 --- /dev/null +++ b/protocols/account.c @@ -0,0 +1,360 @@ +  /********************************************************************\ +  * BitlBee -- An IRC to other IM-networks gateway                     * +  *                                                                    * +  * Copyright 2002-2010 Wilmer van der Gaast and others                * +  \********************************************************************/ + +/* Account management functions                                         */ + +/* +  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 with +  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; +  if not, write to the Free Software Foundation, Inc., 59 Temple Place, +  Suite 330, Boston, MA  02111-1307  USA +*/ + +#define BITLBEE_CORE +#include "bitlbee.h" +#include "account.h" +#include "chat.h" + +account_t *account_add( bee_t *bee, struct prpl *prpl, char *user, char *pass ) +{ +	account_t *a; +	set_t *s; +	 +	if( bee->accounts ) +	{ +		for( a = bee->accounts; a->next; a = a->next ); +		a = a->next = g_new0( account_t, 1 ); +	} +	else +	{ +		bee->accounts = a = g_new0 ( account_t, 1 ); +	} +	 +	a->prpl = prpl; +	a->user = g_strdup( user ); +	a->pass = g_strdup( pass ); +	a->auto_connect = 1; +	a->bee = bee; +	 +	s = set_add( &a->set, "auto_connect", "true", set_eval_account, a ); +	s->flags |= ACC_SET_NOSAVE; +	 +	s = set_add( &a->set, "auto_reconnect", "true", set_eval_bool, a ); +	 +	s = set_add( &a->set, "nick_source", "handle", NULL, a ); +	 +	s = set_add( &a->set, "password", NULL, set_eval_account, a ); +	s->flags |= ACC_SET_NOSAVE | SET_NULL_OK; +	 +	s = set_add( &a->set, "username", NULL, set_eval_account, a ); +	s->flags |= ACC_SET_NOSAVE | ACC_SET_OFFLINE_ONLY; +	set_setstr( &a->set, "username", user ); +	 +	a->nicks = g_hash_table_new_full( g_str_hash, g_str_equal, g_free, g_free ); +	 +	/* This function adds some more settings (and might want to do more +	   things that have to be done now, although I can't think of anything. */ +	if( prpl->init ) +		prpl->init( a ); +	 +	s = set_add( &a->set, "away", NULL, set_eval_account, a ); +	s->flags |= SET_NULL_OK; +	 +	if( a->flags & ACC_FLAG_STATUS_MESSAGE ) +	{ +		s = set_add( &a->set, "status", NULL, set_eval_account, a ); +		s->flags |= SET_NULL_OK; +	} +	 +	return a; +} + +char *set_eval_account( set_t *set, char *value ) +{ +	account_t *acc = set->data; +	 +	/* Double-check: We refuse to edit on-line accounts. */ +	if( set->flags & ACC_SET_OFFLINE_ONLY && acc->ic ) +		return SET_INVALID; +	 +	if( strcmp( set->key, "server" ) == 0 ) +	{ +		g_free( acc->server ); +		if( value && *value ) +		{ +			acc->server = g_strdup( value ); +			return value; +		} +		else +		{ +			acc->server = g_strdup( set->def ); +			return g_strdup( set->def ); +		} +	} +	else if( strcmp( set->key, "username" ) == 0 ) +	{ +		g_free( acc->user ); +		acc->user = g_strdup( value ); +		return value; +	} +	else if( strcmp( set->key, "password" ) == 0 ) +	{ +		if( value ) +		{ +			g_free( acc->pass ); +			acc->pass = g_strdup( value ); +			return NULL;	/* password shouldn't be visible in plaintext! */ +		} +		else +		{ +			/* NULL can (should) be stored in the set_t +			   variable, but is otherwise not correct. */ +			return SET_INVALID; +		} +	} +	else if( strcmp( set->key, "auto_connect" ) == 0 ) +	{ +		if( !is_bool( value ) ) +			return SET_INVALID; +		 +		acc->auto_connect = bool2int( value ); +		return value; +	} +	else if( strcmp( set->key, "away" ) == 0 || +	         strcmp( set->key, "status" ) == 0 ) +	{ +		if( acc->ic && acc->ic->flags & OPT_LOGGED_IN ) +		{ +			/* If we're currently on-line, set the var now already +			   (bit of a hack) and send an update. */ +			g_free( set->value ); +			set->value = g_strdup( value ); +			 +			imc_away_send_update( acc->ic ); +		} +		 +		return value; +	} +	 +	return SET_INVALID; +} + +account_t *account_get( bee_t *bee, char *id ) +{ +	account_t *a, *ret = NULL; +	char *handle, *s; +	int nr; +	 +	/* This checks if the id string ends with (...) */ +	if( ( handle = strchr( id, '(' ) ) && ( s = strchr( handle, ')' ) ) && s[1] == 0 ) +	{ +		struct prpl *proto; +		 +		*s = *handle = 0; +		handle ++; +		 +		if( ( proto = find_protocol( id ) ) ) +		{ +			for( a = bee->accounts; a; a = a->next ) +				if( a->prpl == proto && +				    a->prpl->handle_cmp( handle, a->user ) == 0 ) +					ret = a; +		} +		 +		/* Restore the string. */ +		handle --; +		*handle = '('; +		*s = ')'; +		 +		if( ret ) +			return ret; +	} +	 +	if( sscanf( id, "%d", &nr ) == 1 && nr < 1000 ) +	{ +		for( a = bee->accounts; a; a = a->next ) +			if( ( nr-- ) == 0 ) +				return( a ); +		 +		return( NULL ); +	} +	 +	for( a = bee->accounts; a; a = a->next ) +	{ +		if( g_strcasecmp( id, a->prpl->name ) == 0 ) +		{ +			if( !ret ) +				ret = a; +			else +				return( NULL ); /* We don't want to match more than one... */ +		} +		else if( strstr( a->user, id ) ) +		{ +			if( !ret ) +				ret = a; +			else +				return( NULL ); +		} +	} +	 +	return( ret ); +} + +void account_del( bee_t *bee, account_t *acc ) +{ +	account_t *a, *l = NULL; +	 +	if( acc->ic ) +		/* Caller should have checked, accounts still in use can't be deleted. */ +		return; +	 +	for( a = bee->accounts; a; a = (l=a)->next ) +		if( a == acc ) +		{ +			if( l ) +				l->next = a->next; +			else +				bee->accounts = a->next; +			 +			/** FIXME +			for( c = bee->chatrooms; c; c = nc ) +			{ +				nc = c->next; +				if( acc == c->acc ) +					chat_del( bee, c ); +			} +			*/ +			 +			while( a->set ) +				set_del( &a->set, a->set->key ); +			 +			g_hash_table_destroy( a->nicks ); +			 +			g_free( a->user ); +			g_free( a->pass ); +			g_free( a->server ); +			if( a->reconnect )	/* This prevents any reconnect still queued to happen */ +				cancel_auto_reconnect( a ); +			g_free( a ); +			 +			break; +		} +} + +void account_on( bee_t *bee, account_t *a ) +{ +	if( a->ic ) +	{ +		/* Trying to enable an already-enabled account */ +		return; +	} +	 +	cancel_auto_reconnect( a ); +	 +	a->reconnect = 0; +	a->prpl->login( a ); +} + +void account_off( bee_t *bee, account_t *a ) +{ +	imc_logout( a->ic, FALSE ); +	a->ic = NULL; +	if( a->reconnect ) +	{ +		/* Shouldn't happen */ +		cancel_auto_reconnect( a ); +	} +} + +struct account_reconnect_delay +{ +	int start; +	char op; +	int step; +	int max; +}; + +int account_reconnect_delay_parse( char *value, struct account_reconnect_delay *p ) +{ +	memset( p, 0, sizeof( *p ) ); +	/* A whole day seems like a sane "maximum maximum". */ +	p->max = 86400; +	 +	/* Format: /[0-9]+([*+][0-9]+(<[0-9+])?)?/ */ +	while( *value && isdigit( *value ) ) +		p->start = p->start * 10 + *value++ - '0'; +	 +	/* Sure, call me evil for implementing my own fscanf here, but it's +	   dead simple and I immediately know where to continue parsing. */ +	 +	if( *value == 0 ) +		/* If the string ends now, the delay is constant. */ +		return 1; +	else if( *value != '+' && *value != '*' ) +		/* Otherwise allow either a + or a * */ +		return 0; +	 +	p->op = *value++; +	 +	/* + or * the delay by this number every time. */ +	while( *value && isdigit( *value ) ) +		p->step = p->step * 10 + *value++ - '0'; +	 +	if( *value == 0 ) +		/* Use the default maximum (one day). */ +		return 1; +	else if( *value != '<' ) +		return 0; +	 +	p->max = 0; +	value ++; +	while( *value && isdigit( *value ) ) +		p->max = p->max * 10 + *value++ - '0'; +	 +	return p->max > 0; +} + +char *set_eval_account_reconnect_delay( set_t *set, char *value ) +{ +	struct account_reconnect_delay p; +	 +	return account_reconnect_delay_parse( value, &p ) ? value : SET_INVALID; +} + +int account_reconnect_delay( account_t *a ) +{ +	char *setting = set_getstr( &a->bee->set, "auto_reconnect_delay" ); +	struct account_reconnect_delay p; +	 +	if( account_reconnect_delay_parse( setting, &p ) ) +	{ +		if( a->auto_reconnect_delay == 0 ) +			a->auto_reconnect_delay = p.start; +		else if( p.op == '+' ) +			a->auto_reconnect_delay += p.step; +		else if( p.op == '*' ) +			a->auto_reconnect_delay *= p.step; +		 +		if( a->auto_reconnect_delay > p.max ) +			a->auto_reconnect_delay = p.max; +	} +	else +	{ +		a->auto_reconnect_delay = 0; +	} +	 +	return a->auto_reconnect_delay; +} diff --git a/protocols/account.h b/protocols/account.h new file mode 100644 index 00000000..be27542e --- /dev/null +++ b/protocols/account.h @@ -0,0 +1,72 @@ +  /********************************************************************\ +  * BitlBee -- An IRC to other IM-networks gateway                     * +  *                                                                    * +  * Copyright 2002-2004 Wilmer van der Gaast and others                * +  \********************************************************************/ + +/* Account management functions                                         */ + +/* +  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 with +  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; +  if not, write to the Free Software Foundation, Inc., 59 Temple Place, +  Suite 330, Boston, MA  02111-1307  USA +*/ + +#ifndef _ACCOUNT_H +#define _ACCOUNT_H + +typedef struct account +{ +	struct prpl *prpl; +	char *user; +	char *pass; +	char *server; +	 +	int auto_connect; +	int auto_reconnect_delay; +	int reconnect; +	int flags; +	 +	set_t *set; +	GHashTable *nicks; +	 +	struct bee *bee; +	struct im_connection *ic; +	struct account *next; +} account_t; + +account_t *account_add( bee_t *bee, struct prpl *prpl, char *user, char *pass ); +account_t *account_get( bee_t *bee, char *id ); +void account_del( bee_t *bee, account_t *acc ); +void account_on( bee_t *bee, account_t *a ); +void account_off( bee_t *bee, account_t *a ); + +char *set_eval_account( set_t *set, char *value ); +char *set_eval_account_reconnect_delay( set_t *set, char *value ); +int account_reconnect_delay( account_t *a ); + +typedef enum +{ +	ACC_SET_NOSAVE = 0x01,          /* Don't save this setting (i.e. stored elsewhere). */ +	ACC_SET_OFFLINE_ONLY = 0x02,    /* Allow changes only if the acct is offline. */ +	ACC_SET_ONLINE_ONLY = 0x04,     /* Allow changes only if the acct is online. */ +} account_set_flag_t; + +typedef enum +{ +	ACC_FLAG_AWAY_MESSAGE = 0x01,   /* Supports away messages instead of just states. */ +	ACC_FLAG_STATUS_MESSAGE = 0x02, /* Supports status messages (without being away). */ +} account_flag_t; + +#endif diff --git a/protocols/bee.c b/protocols/bee.c new file mode 100644 index 00000000..c5eeee17 --- /dev/null +++ b/protocols/bee.c @@ -0,0 +1,95 @@ +  /********************************************************************\ +  * BitlBee -- An IRC to other IM-networks gateway                     * +  *                                                                    * +  * Copyright 2002-2010 Wilmer van der Gaast and others                * +  \********************************************************************/ + +/* Some IM-core stuff                                                   */ + +/* +  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 with +  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; +  if not, write to the Free Software Foundation, Inc., 59 Temple Place, +  Suite 330, Boston, MA  02111-1307  USA +*/ + +#define BITLBEE_CORE +#include "bitlbee.h" + +static char *set_eval_away_status( set_t *set, char *value ); + +bee_t *bee_new() +{ +	bee_t *b = g_new0( bee_t, 1 ); +	set_t *s; +	 +	s = set_add( &b->set, "away", NULL, set_eval_away_status, b ); +	s->flags |= SET_NULL_OK; +	s = set_add( &b->set, "auto_connect", "true", set_eval_bool, b ); +	s = set_add( &b->set, "auto_reconnect", "true", set_eval_bool, b ); +	s = set_add( &b->set, "auto_reconnect_delay", "5*3<900", set_eval_account_reconnect_delay, b ); +	s = set_add( &b->set, "debug", "false", set_eval_bool, b ); +	s = set_add( &b->set, "save_on_quit", "true", set_eval_bool, b ); +	s = set_add( &b->set, "status", NULL, set_eval_away_status, b ); +	s->flags |= SET_NULL_OK; +	s = set_add( &b->set, "strip_html", "true", NULL, b ); +	 +	b->user = g_malloc( 1 ); +	 +	return b; +} + +void bee_free( bee_t *b ) +{ +	while( b->accounts ) +	{ +		if( b->accounts->ic ) +			imc_logout( b->accounts->ic, FALSE ); +		else if( b->accounts->reconnect ) +			cancel_auto_reconnect( b->accounts ); +		 +		if( b->accounts->ic == NULL ) +			account_del( b, b->accounts ); +		else +			/* Nasty hack, but account_del() doesn't work in this +			   case and we don't want infinite loops, do we? ;-) */ +			b->accounts = b->accounts->next; +	} +	 +	while( b->set ) +		set_del( &b->set, b->set->key ); +	 +	bee_group_free( b ); +	 +	g_free( b->user ); +	g_free( b ); +} + +static char *set_eval_away_status( set_t *set, char *value ) +{ +	bee_t *bee = set->data; +	account_t *a; +	 +	g_free( set->value ); +	set->value = g_strdup( value ); +	 +	for( a = bee->accounts; a; a = a->next ) +	{ +		struct im_connection *ic = a->ic; +		 +		if( ic && ic->flags & OPT_LOGGED_IN ) +			imc_away_send_update( ic ); +	} +	 +	return value; +} diff --git a/protocols/bee.h b/protocols/bee.h new file mode 100644 index 00000000..c3230f47 --- /dev/null +++ b/protocols/bee.h @@ -0,0 +1,147 @@ +  /********************************************************************\ +  * BitlBee -- An IRC to other IM-networks gateway                     * +  *                                                                    * +  * Copyright 2002-2010 Wilmer van der Gaast and others                * +  \********************************************************************/ + +/* Stuff to handle, save and search buddies                             */ + +/* +  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 with +  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; +  if not, write to the Free Software Foundation, Inc., 59 Temple Place, +  Suite 330, Boston, MA  02111-1307  USA +*/ + +#ifndef __BEE_H__ +#define __BEE_H__ + +struct bee_ui_funcs; +struct groupchat; + +typedef struct bee +{ +	struct set *set; +	 +	GSList *users; +	GSList *groups; +	struct account *accounts; /* TODO(wilmer): Use GSList here too? */ +	 +	/* Symbolic, to refer to the local user (who has no real bee_user +	   object). Not to be used by anything except so far imcb_chat_add/ +	   remove_buddy(). This seems slightly cleaner than abusing NULL. */ +	struct bee_user *user; +	 +	const struct bee_ui_funcs *ui; +	void *ui_data; +} bee_t; + +bee_t *bee_new(); +void bee_free( bee_t *b ); + +typedef enum +{ +	BEE_USER_ONLINE = 1,    /* Compatibility with old OPT_LOGGED_IN flag */ +	BEE_USER_AWAY = 4,      /* Compatibility with old OPT_AWAY flag */ +	BEE_USER_LOCAL = 256,   /* Locally-added contacts (not in real contact list) */ +} bee_user_flags_t; + +typedef struct bee_user +{ +	struct im_connection *ic; +	char *handle; +	char *fullname; +	struct bee_group *group; + +	bee_user_flags_t flags; +	char *status; +	char *status_msg; +	 +	time_t login_time, idle_time; +	 +	bee_t *bee; +	void *ui_data; +} bee_user_t; + +typedef struct bee_group +{ +	char *key; +	char *name; +} bee_group_t; + +typedef struct bee_ui_funcs +{ +	gboolean (*user_new)( bee_t *bee, struct bee_user *bu ); +	gboolean (*user_free)( bee_t *bee, struct bee_user *bu ); +	gboolean (*user_fullname)( bee_t *bee, bee_user_t *bu ); +	gboolean (*user_nick_hint)( bee_t *bee, bee_user_t *bu, const char *hint ); +	gboolean (*user_group)( bee_t *bee, bee_user_t *bu ); +	gboolean (*user_status)( bee_t *bee, struct bee_user *bu, struct bee_user *old ); +	gboolean (*user_msg)( bee_t *bee, bee_user_t *bu, const char *msg, time_t sent_at ); +	gboolean (*user_typing)( bee_t *bee, bee_user_t *bu, guint32 flags ); +	 +	gboolean (*chat_new)( bee_t *bee, struct groupchat *c ); +	gboolean (*chat_free)( bee_t *bee, struct groupchat *c ); +	gboolean (*chat_log)( bee_t *bee, struct groupchat *c, const char *text ); +	gboolean (*chat_msg)( bee_t *bee, struct groupchat *c, bee_user_t *bu, const char *msg, time_t sent_at ); +	gboolean (*chat_add_user)( bee_t *bee, struct groupchat *c, bee_user_t *bu ); +	gboolean (*chat_remove_user)( bee_t *bee, struct groupchat *c, bee_user_t *bu ); +	gboolean (*chat_topic)( bee_t *bee, struct groupchat *c, const char *new, bee_user_t *bu ); +	gboolean (*chat_name_hint)( bee_t *bee, struct groupchat *c, const char *name ); +	 +	struct file_transfer* (*ft_in_start)( bee_t *bee, bee_user_t *bu, const char *file_name, size_t file_size ); +	gboolean (*ft_out_start)( struct im_connection *ic, struct file_transfer *ft ); +	void (*ft_close)( struct im_connection *ic, struct file_transfer *ft ); +	void (*ft_finished)( struct im_connection *ic, struct file_transfer *ft ); +} bee_ui_funcs_t; + + +/* bee.c */ +bee_t *bee_new(); +void bee_free( bee_t *b ); + +/* bee_user.c */ +bee_user_t *bee_user_new( bee_t *bee, struct im_connection *ic, const char *handle, bee_user_flags_t flags ); +int bee_user_free( bee_t *bee, bee_user_t *bu ); +bee_user_t *bee_user_by_handle( bee_t *bee, struct im_connection *ic, const char *handle ); +int bee_user_msg( bee_t *bee, bee_user_t *bu, const char *msg, int flags ); +bee_group_t *bee_group_by_name( bee_t *bee, const char *name, gboolean creat ); +void bee_group_free( bee_t *bee ); + +/* Callbacks from IM modules to core: */ +/* Buddy activity */ +/* To manipulate the status of a handle. + * - flags can be |='d with OPT_* constants. You will need at least: + *   OPT_LOGGED_IN and OPT_AWAY. + * - 'state' and 'message' can be NULL */ +G_MODULE_EXPORT void imcb_buddy_status( struct im_connection *ic, const char *handle, int flags, const char *state, const char *message ); +G_MODULE_EXPORT void imcb_buddy_times( struct im_connection *ic, const char *handle, time_t login, time_t idle ); +/* Call when a handle says something. 'flags' and 'sent_at may be just 0. */ +G_MODULE_EXPORT void imcb_buddy_msg( struct im_connection *ic, const char *handle, char *msg, guint32 flags, time_t sent_at ); + +/* bee_chat.c */ +#if 0 +struct groupchat *imcb_chat_new( struct im_connection *ic, const char *handle ); +void imcb_chat_name_hint( struct groupchat *c, const char *name ); +void imcb_chat_free( struct groupchat *c ); +void imcb_chat_msg( struct groupchat *c, const char *who, char *msg, uint32_t flags, time_t sent_at ); +void imcb_chat_log( struct groupchat *c, char *format, ... ); +void imcb_chat_topic( struct groupchat *c, char *who, char *topic, time_t set_at ); +void imcb_chat_add_buddy( struct groupchat *b, const char *handle ); +void imcb_chat_remove_buddy( struct groupchat *b, const char *handle, const char *reason ); +static int remove_chat_buddy_silent( struct groupchat *b, const char *handle ); +#endif +int bee_chat_msg( bee_t *bee, struct groupchat *c, const char *msg, int flags ); +struct groupchat *bee_chat_by_title( bee_t *bee, struct im_connection *ic, const char *title ); + +#endif /* __BEE_H__ */ diff --git a/protocols/bee_chat.c b/protocols/bee_chat.c new file mode 100644 index 00000000..3be6f189 --- /dev/null +++ b/protocols/bee_chat.c @@ -0,0 +1,234 @@ +  /********************************************************************\ +  * BitlBee -- An IRC to other IM-networks gateway                     * +  *                                                                    * +  * Copyright 2002-2010 Wilmer van der Gaast and others                * +  \********************************************************************/ + +/* Stuff to handle rooms                                                */ + +/* +  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 with +  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; +  if not, write to the Free Software Foundation, Inc., 59 Temple Place, +  Suite 330, Boston, MA  02111-1307  USA +*/ + +#define BITLBEE_CORE +#include "bitlbee.h" + +struct groupchat *imcb_chat_new( struct im_connection *ic, const char *handle ) +{ +	struct groupchat *c = g_new0( struct groupchat, 1 ); +	bee_t *bee = ic->bee; +	 +	/* This one just creates the conversation structure, user won't see +	   anything yet until s/he is joined to the conversation. (This +	   allows you to add other already present participants first.) */ +	 +	ic->groupchats = g_slist_prepend( ic->groupchats, c ); +	c->ic = ic; +	c->title = g_strdup( handle ); +	c->topic = g_strdup_printf( "BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!", c->title ); +	 +	if( set_getbool( &ic->bee->set, "debug" ) ) +		imcb_log( ic, "Creating new conversation: (id=%p,handle=%s)", c, handle ); +	 +	if( bee->ui->chat_new ) +		bee->ui->chat_new( bee, c ); +	 +	return c; +} + +void imcb_chat_name_hint( struct groupchat *c, const char *name ) +{ +	bee_t *bee = c->ic->bee; +	 +	if( bee->ui->chat_name_hint ) +		bee->ui->chat_name_hint( bee, c, name ); +} + +void imcb_chat_free( struct groupchat *c ) +{ +	struct im_connection *ic = c->ic; +	bee_t *bee = ic->bee; +	GList *ir; +	 +	if( bee->ui->chat_free ) +		bee->ui->chat_free( bee, c ); +	 +	if( set_getbool( &ic->bee->set, "debug" ) ) +		imcb_log( ic, "You were removed from conversation %p", c ); +	 +	ic->groupchats = g_slist_remove( ic->groupchats, c ); +	 +	for( ir = c->in_room; ir; ir = ir->next ) +		g_free( ir->data ); +	g_list_free( c->in_room ); +	g_free( c->title ); +	g_free( c->topic ); +	g_free( c ); +} + +void imcb_chat_msg( struct groupchat *c, const char *who, char *msg, uint32_t flags, time_t sent_at ) +{ +	struct im_connection *ic = c->ic; +	bee_t *bee = ic->bee; +	bee_user_t *bu; +	char *s; +	 +	/* Gaim sends own messages through this too. IRC doesn't want this, so kill them */ +	if( g_strcasecmp( who, ic->acc->user ) == 0 ) +		return; +	 +	bu = bee_user_by_handle( bee, ic, who ); +	 +	s = set_getstr( &ic->bee->set, "strip_html" ); +	if( ( g_strcasecmp( s, "always" ) == 0 ) || +	    ( ( ic->flags & OPT_DOES_HTML ) && s ) ) +		strip_html( msg ); +	 +	if( bu && bee->ui->chat_msg ) +		bee->ui->chat_msg( bee, c, bu, msg, sent_at ); +	else +		imcb_chat_log( c, "Message from unknown participant %s: %s", who, msg ); +} + +void imcb_chat_log( struct groupchat *c, char *format, ... ) +{ +	struct im_connection *ic = c->ic; +	bee_t *bee = ic->bee; +	va_list params; +	char *text; +	 +	if( !bee->ui->chat_log ) +		return; +	 +	va_start( params, format ); +	text = g_strdup_vprintf( format, params ); +	va_end( params ); +	 +	bee->ui->chat_log( bee, c, text ); +	g_free( text ); +} + +void imcb_chat_topic( struct groupchat *c, char *who, char *topic, time_t set_at ) +{ +	struct im_connection *ic = c->ic; +	bee_t *bee = ic->bee; +	bee_user_t *bu; +	 +	if( !bee->ui->chat_topic ) +		return; +	 +	if( who == NULL) +		bu = NULL; +	else if( g_strcasecmp( who, ic->acc->user ) == 0 ) +		bu = bee->user; +	else +		bu = bee_user_by_handle( bee, ic, who ); +	 +	if( ( g_strcasecmp( set_getstr( &ic->bee->set, "strip_html" ), "always" ) == 0 ) || +	    ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->bee->set, "strip_html" ) ) ) +		strip_html( topic ); +	 +	bee->ui->chat_topic( bee, c, topic, bu ); +} + +void imcb_chat_add_buddy( struct groupchat *c, const char *handle ) +{ +	struct im_connection *ic = c->ic; +	bee_t *bee = ic->bee; +	bee_user_t *bu = bee_user_by_handle( bee, ic, handle ); +	gboolean me; +	 +	if( set_getbool( &c->ic->bee->set, "debug" ) ) +		imcb_log( c->ic, "User %s added to conversation %p", handle, c ); +	 +	me = ic->acc->prpl->handle_cmp( handle, ic->acc->user ) == 0; +	 +	/* Most protocols allow people to join, even when they're not in +	   your contact list. Try to handle that here */ +	if( !me && !bu ) +		bu = bee_user_new( bee, ic, handle, BEE_USER_LOCAL ); +	 +	/* Add the handle to the room userlist */ +	/* TODO: Use bu instead of a string */ +	c->in_room = g_list_append( c->in_room, g_strdup( handle ) ); +	 +	if( bee->ui->chat_add_user ) +		bee->ui->chat_add_user( bee, c, me ? bee->user : bu ); +	 +	if( me ) +		c->joined = 1; +} + +void imcb_chat_remove_buddy( struct groupchat *c, const char *handle, const char *reason ) +{ +	struct im_connection *ic = c->ic; +	bee_t *bee = ic->bee; +	bee_user_t *bu = NULL; +	 +	if( set_getbool( &bee->set, "debug" ) ) +		imcb_log( ic, "User %s removed from conversation %p (%s)", handle, c, reason ? reason : "" ); +	 +	/* It might be yourself! */ +	if( g_strcasecmp( handle, ic->acc->user ) == 0 ) +	{ +		if( c->joined == 0 ) +			return; +		 +		bu = bee->user; +		c->joined = 0; +	} +	else +	{ +		bu = bee_user_by_handle( bee, ic, handle ); +	} +	 +	if( bee->ui->chat_remove_user ) +		bee->ui->chat_remove_user( bee, c, bu ); +} + +int bee_chat_msg( bee_t *bee, struct groupchat *c, const char *msg, int flags ) +{ +	struct im_connection *ic = c->ic; +	char *buf = NULL; +	 +	if( ( ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) ) +	{ +		buf = escape_html( msg ); +		msg = buf; +	} +	else +		buf = g_strdup( msg ); +	 +	ic->acc->prpl->chat_msg( c, buf, flags ); +	g_free( buf ); +	 +	return 1; +} + +struct groupchat *bee_chat_by_title( bee_t *bee, struct im_connection *ic, const char *title ) +{ +	struct groupchat *c; +	GSList *l; +	 +	for( l = ic->groupchats; l; l = l->next ) +	{ +		c = l->data; +		if( strcmp( c->title, title ) == 0 ) +			return c; +	} +	 +	return NULL; +} diff --git a/protocols/bee_ft.c b/protocols/bee_ft.c new file mode 100644 index 00000000..1026eab3 --- /dev/null +++ b/protocols/bee_ft.c @@ -0,0 +1,66 @@ +/********************************************************************\ +* BitlBee -- An IRC to other IM-networks gateway                     * +*                                                                    * +* Copyright 2010 Wilmer van der Gaast <wilmer@gaast.net>             * +\********************************************************************/ + +/* +  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 with +  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; +  if not, write to the Free Software Foundation, Inc., 59 Temple Place, +  Suite 330, Boston, MA  02111-1307  USA +*/ + +#define BITLBEE_CORE +#include "bitlbee.h" +#include "ft.h" + +file_transfer_t *imcb_file_send_start( struct im_connection *ic, char *handle, char *file_name, size_t file_size ) +{ +	bee_t *bee = ic->bee;  +	bee_user_t *bu = bee_user_by_handle( bee, ic, handle ); +	 +	if( bee->ui->ft_in_start ) +		return bee->ui->ft_in_start( bee, bu, file_name, file_size ); +	else +		return NULL; +} + +gboolean imcb_file_recv_start( struct im_connection *ic, file_transfer_t *ft ) +{ +	bee_t *bee = ic->bee; +	 +	if( bee->ui->ft_out_start ) +		return bee->ui->ft_out_start( ic, ft ); +	else +		return FALSE; +} + +void imcb_file_canceled( struct im_connection *ic, file_transfer_t *file, char *reason ) +{ +	bee_t *bee = ic->bee; +	 +	if( file->canceled ) +		file->canceled( file, reason ); +	 +	if( bee->ui->ft_close ) +		bee->ui->ft_close( ic, file ); +} + +void imcb_file_finished( struct im_connection *ic, file_transfer_t *file ) +{ +	bee_t *bee = ic->bee; +	 +	if( bee->ui->ft_finished ) +		bee->ui->ft_finished( ic, file ); +} diff --git a/protocols/bee_user.c b/protocols/bee_user.c new file mode 100644 index 00000000..faa2acb7 --- /dev/null +++ b/protocols/bee_user.c @@ -0,0 +1,247 @@ +  /********************************************************************\ +  * BitlBee -- An IRC to other IM-networks gateway                     * +  *                                                                    * +  * Copyright 2002-2010 Wilmer van der Gaast and others                * +  \********************************************************************/ + +/* Stuff to handle, save and search buddies                             */ + +/* +  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 with +  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; +  if not, write to the Free Software Foundation, Inc., 59 Temple Place, +  Suite 330, Boston, MA  02111-1307  USA +*/ + +#define BITLBEE_CORE +#include "bitlbee.h" + +bee_user_t *bee_user_new( bee_t *bee, struct im_connection *ic, const char *handle, bee_user_flags_t flags ) +{ +	bee_user_t *bu; +	 +	if( bee_user_by_handle( bee, ic, handle ) != NULL ) +		return NULL; +	 +	bu = g_new0( bee_user_t, 1 ); +	bu->bee = bee; +	bu->ic = ic; +	bu->flags = flags; +	bu->handle = g_strdup( handle ); +	bee->users = g_slist_prepend( bee->users, bu ); +	 +	if( bee->ui->user_new ) +		bee->ui->user_new( bee, bu ); +	 +	/* Offline by default. This will set the right flags. */ +	imcb_buddy_status( ic, handle, 0, NULL, NULL ); +	 +	return bu; +} + +int bee_user_free( bee_t *bee, bee_user_t *bu ) +{ +	if( !bu ) +		return 0; +	 +	if( bee->ui->user_free ) +		bee->ui->user_free( bee, bu ); +	 +	g_free( bu->handle ); +	g_free( bu->fullname ); +	g_free( bu->status ); +	g_free( bu->status_msg ); +	g_free( bu ); +	 +	bee->users = g_slist_remove( bee->users, bu ); +	 +	return 1; +} + +bee_user_t *bee_user_by_handle( bee_t *bee, struct im_connection *ic, const char *handle ) +{ +	GSList *l; +	 +	for( l = bee->users; l; l = l->next ) +	{ +		bee_user_t *bu = l->data; +		 +		if( bu->ic == ic && ic->acc->prpl->handle_cmp( bu->handle, handle ) == 0 ) +			return bu; +	} +	 +	return NULL; +} + +int bee_user_msg( bee_t *bee, bee_user_t *bu, const char *msg, int flags ) +{ +	char *buf = NULL; +	int st; +	 +	if( ( bu->ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) ) +	{ +		buf = escape_html( msg ); +		msg = buf; +	} +	else +		buf = g_strdup( msg ); +	 +	st = bu->ic->acc->prpl->buddy_msg( bu->ic, bu->handle, buf, flags ); +	g_free( buf ); +	 +	return st; +} + + +/* Groups */ +static bee_group_t *bee_group_new( bee_t *bee, const char *name ) +{ +	bee_group_t *bg = g_new0( bee_group_t, 1 ); +	 +	bg->name = g_strdup( name ); +	bg->key = g_utf8_casefold( name, -1 ); +	bee->groups = g_slist_prepend( bee->groups, bg ); +	 +	return bg; +} + +bee_group_t *bee_group_by_name( bee_t *bee, const char *name, gboolean creat ) +{ +	GSList *l; +	char *key; +	 +	if( name == NULL ) +		return NULL; +	 +	key = g_utf8_casefold( name, -1 ); +	for( l = bee->groups; l; l = l->next ) +	{ +		bee_group_t *bg = l->data; +		if( strcmp( bg->key, key ) == 0 ) +			break; +	} +	g_free( key ); +	 +	if( !l ) +		return creat ? bee_group_new( bee, name ) : NULL; +	else +		return l->data; +} + +void bee_group_free( bee_t *bee ) +{ +	while( bee->groups ) +	{ +		bee_group_t *bg = bee->groups->data; +		g_free( bg->name ); +		g_free( bg->key ); +		g_free( bg ); +		bee->groups = g_slist_remove( bee->groups, bee->groups->data ); +	} +} + + +/* IM->UI callbacks */ +void imcb_buddy_status( struct im_connection *ic, const char *handle, int flags, const char *state, const char *message ) +{ +	bee_t *bee = ic->bee; +	bee_user_t *bu, *old; +	 +	if( !( bu = bee_user_by_handle( bee, ic, handle ) ) ) +	{ +		if( g_strcasecmp( set_getstr( &ic->bee->set, "handle_unknown" ), "add" ) == 0 ) +		{ +			bu = bee_user_new( bee, ic, handle, BEE_USER_LOCAL ); +		} +		else +		{ +			if( g_strcasecmp( set_getstr( &ic->bee->set, "handle_unknown" ), "ignore" ) != 0 ) +			{ +				imcb_log( ic, "imcb_buddy_status() for unknown handle %s:\n" +				              "flags = %d, state = %s, message = %s", handle, flags, +				              state ? state : "NULL", message ? message : "NULL" ); +			} +			 +			return; +		} +	} +	 +	/* May be nice to give the UI something to compare against. */ +	old = g_memdup( bu, sizeof( bee_user_t ) ); +	 +	/* TODO(wilmer): OPT_AWAY, or just state == NULL ? */ +	bu->flags = flags; +	bu->status = g_strdup( ( flags & OPT_AWAY ) && state == NULL ? "Away" : state ); +	bu->status_msg = g_strdup( message ); +	 +	if( bee->ui->user_status ) +		bee->ui->user_status( bee, bu, old ); +	 +	g_free( old->status_msg ); +	g_free( old->status ); +	g_free( old ); +} + +void imcb_buddy_times( struct im_connection *ic, const char *handle, time_t login, time_t idle ) +{ +	bee_t *bee = ic->bee; +	bee_user_t *bu; +	 +	if( !( bu = bee_user_by_handle( bee, ic, handle ) ) ) +		return; +	 +	bu->login_time = login; +	bu->idle_time = idle; +} + +void imcb_buddy_msg( struct im_connection *ic, const char *handle, char *msg, uint32_t flags, time_t sent_at ) +{ +	bee_t *bee = ic->bee; +	bee_user_t *bu; +	 +	bu = bee_user_by_handle( bee, ic, handle ); +	 +	if( !bu ) +	{ +		char *h = set_getstr( &bee->set, "handle_unknown" ); +		 +		if( g_strcasecmp( h, "ignore" ) == 0 ) +		{ +			return; +		} +		else if( g_strncasecmp( h, "add", 3 ) == 0 ) +		{ +			bu = bee_user_new( bee, ic, handle, BEE_USER_LOCAL ); +		} +	} +	 +	if( ( g_strcasecmp( set_getstr( &ic->bee->set, "strip_html" ), "always" ) == 0 ) || +	    ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->bee->set, "strip_html" ) ) ) +		strip_html( msg ); +	 +	if( bee->ui->user_msg && bu ) +		bee->ui->user_msg( bee, bu, msg, sent_at ); +	else +		imcb_log( ic, "Message from unknown handle %s:\n%s", handle, msg ); +} + +void imcb_buddy_typing( struct im_connection *ic, char *handle, uint32_t flags ) +{ +	bee_user_t *bu; +	 +	if( ic->bee->ui->user_typing && +	    ( bu = bee_user_by_handle( ic->bee, ic, handle ) ) ) +	{ +		ic->bee->ui->user_typing( ic->bee, bu, flags ); +	} +} diff --git a/protocols/chat.c b/protocols/chat.c new file mode 100644 index 00000000..8c5ce0bc --- /dev/null +++ b/protocols/chat.c @@ -0,0 +1,192 @@ +  /********************************************************************\ +  * BitlBee -- An IRC to other IM-networks gateway                     * +  *                                                                    * +  * Copyright 2002-2008 Wilmer van der Gaast and others                * +  \********************************************************************/ + +/* Keep track of chatrooms the user is interested in                    */ + +/* +  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 with +  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; +  if not, write to the Free Software Foundation, Inc., 59 Temple Place, +  Suite 330, Boston, MA  02111-1307  USA +*/ + +#include "bitlbee.h" +#include "chat.h" + +struct chat *chat_add( irc_t *irc, account_t *acc, char *handle, char *channel ) +{ +	struct chat *c, *l; +	set_t *s; + +	if( acc->prpl->chat_join == NULL || !chat_chanok( channel ) || +	    chat_chancmp( channel, irc->channel ) == 0 ) +	{ +		return NULL; +	} +	 +	for( c = irc->chatrooms; c; c = c->next ) +	{ +		if( chat_chancmp( channel, c->channel ) == 0 ) +			return NULL; +		 +		if( acc == c->acc && g_strcasecmp( handle, c->handle ) == 0 ) +			return NULL; +		 +		l = c; +	} +	 +	if( irc->chatrooms == NULL ) +		irc->chatrooms = c = g_new0( struct chat, 1 ); +	else +		l->next = c = g_new0( struct chat, 1 ); +	 +	c->acc = acc; +	c->handle = g_strdup( handle ); +	c->channel = g_strdup( channel ); +	 +	s = set_add( &c->set, "auto_join", "false", set_eval_bool, c ); +	/* s = set_add( &c->set, "auto_rejoin", "false", set_eval_bool, c ); */ +	s = set_add( &c->set, "nick", NULL, NULL, c ); +	s->flags |= SET_NULL_OK; +	 +	return c; +} + +struct chat *chat_byhandle( irc_t *irc, account_t *acc, char *handle ) +{ +	struct chat *c; +	 +	for( c = irc->chatrooms; c; c = c->next ) +	{ +		if( acc == c->acc && g_strcasecmp( handle, c->handle ) == 0 ) +			break; +	} +	 +	return c; +} + +struct chat *chat_bychannel( irc_t *irc, char *channel ) +{ +	struct chat *c; +	 +	for( c = irc->chatrooms; c; c = c->next ) +	{ +		if( chat_chancmp( channel, c->channel ) == 0 ) +			break; +	} +	 +	return c; +} + +struct chat *chat_get( irc_t *irc, char *id ) +{ +	struct chat *c, *ret = NULL; +	int nr; +	 +	if( sscanf( id, "%d", &nr ) == 1 && nr < 1000 ) +	{ +		for( c = irc->chatrooms; c; c = c->next ) +			if( ( nr-- ) == 0 ) +				return c; +		 +		return NULL; +	} +	 +	for( c = irc->chatrooms; c; c = c->next ) +	{ +		if( strstr( c->handle, id ) ) +		{ +			if( !ret ) +				ret = c; +			else +				return NULL; +		} +		else if( strstr( c->channel, id ) ) +		{ +			if( !ret ) +				ret = c; +			else +				return NULL; +		} +	} +	 +	return ret; +} + +int chat_del( irc_t *irc, struct chat *chat ) +{ +	struct chat *c, *l = NULL; +	 +	for( c = irc->chatrooms; c; c = (l=c)->next ) +		if( c == chat ) +			break; +	 +	if( c == NULL ) +		return 0; +	else if( l == NULL ) +		irc->chatrooms = c->next; +	else +		l->next = c->next; +	 +	while( c->set ) +		set_del( &c->set, c->set->key ); +	 +	g_free( c->handle ); +	g_free( c->channel ); +	g_free( c ); +	 +	return 1; +} + +int chat_chancmp( char *a, char *b ) +{ +	if( !chat_chanok( a ) || !chat_chanok( b ) ) +		return 0; +	 +	if( a[0] == b[0] ) +		return nick_cmp( a + 1, b + 1 ); +	else +		return -1; +} + +int chat_chanok( char *a ) +{ +	if( strchr( CTYPES, a[0] ) != NULL ) +		return nick_ok( a + 1 ); +	else +		return 0; +} + +int chat_join( irc_t *irc, struct chat *c, const char *password ) +{ +	struct groupchat *gc; +	char *nick = set_getstr( &c->set, "nick" ); + +	if( c->acc->ic == NULL || c->acc->prpl->chat_join == NULL ) +		return 0; +	 +	if( nick == NULL ) +		nick = irc->nick; +	 +	if( ( gc = c->acc->prpl->chat_join( c->acc->ic, c->handle, nick, password ) ) ) +	{ +		g_free( gc->channel ); +		gc->channel = g_strdup( c->channel ); +		return 1; +	} +	 +	return 0; +} diff --git a/protocols/chat.h b/protocols/chat.h new file mode 100644 index 00000000..7196aea8 --- /dev/null +++ b/protocols/chat.h @@ -0,0 +1,51 @@ +  /********************************************************************\ +  * BitlBee -- An IRC to other IM-networks gateway                     * +  *                                                                    * +  * Copyright 2002-2008 Wilmer van der Gaast and others                * +  \********************************************************************/ + +/* Keep track of chatrooms the user is interested in                    */ + +/* +  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 with +  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; +  if not, write to the Free Software Foundation, Inc., 59 Temple Place, +  Suite 330, Boston, MA  02111-1307  USA +*/ + +#ifndef _CHAT_H +#define _CHAT_H + +struct chat +{ +	account_t *acc; +	 +	char *handle; +	char *channel; +	set_t *set; +	 +	struct chat *next; +}; + +struct chat *chat_add( irc_t *irc, account_t *acc, char *handle, char *channel ); +struct chat *chat_byhandle( irc_t *irc, account_t *acc, char *handle ); +struct chat *chat_bychannel( irc_t *irc, char *channel ); +struct chat *chat_get( irc_t *irc, char *id ); +int chat_del( irc_t *irc, struct chat *chat ); + +int chat_chancmp( char *a, char *b ); +int chat_chanok( char *a ); + +int chat_join( irc_t *irc, struct chat *c, const char *password ); + +#endif diff --git a/protocols/ft.h b/protocols/ft.h new file mode 100644 index 00000000..159f16f2 --- /dev/null +++ b/protocols/ft.h @@ -0,0 +1,176 @@ +/********************************************************************\ +* BitlBee -- An IRC to other IM-networks gateway                     * +*                                                                    * +* Copyright 2006 Marijn Kruisselbrink and others                     * +\********************************************************************/ + +/* Generic file transfer header                                     */ + +/* +  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 with +  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; +  if not, write to the Free Software Foundation, Inc., 59 Temple Place, +  Suite 330, Boston, MA  02111-1307  USA +*/ + +#ifndef _FT_H +#define _FT_H + +/* + * One buffer is needed for each transfer. The receiver stores a message + * in it and gives it to the sender. The sender will stall the receiver + * till the buffer has been sent out. + */ +#define FT_BUFFER_SIZE 2048 + +typedef enum { +	FT_STATUS_LISTENING	= 1, +	FT_STATUS_TRANSFERRING	= 2, +	FT_STATUS_FINISHED	= 4, +	FT_STATUS_CANCELED	= 8, +	FT_STATUS_CONNECTING	= 16 +} file_status_t; + +/* + * This structure holds all irc specific information regarding an incoming (from the point of view of + * the irc client) file transfer. New instances of this struct should only be created by calling the + * imcb_file_send_start() method, which will initialize most of the fields. The data field and the various + * methods are zero-initialized. Instances will automatically be deleted once the transfer is completed, + * canceled, or the connection to the irc client has been lost (note that also if only the irc connection + * and not the file transfer connection is lost, the file transfer will still be canceled and freed). + * + * The following (poor ascii-art) diagram illustrates what methods are called for which status-changes: + * + *	                        /-----------\                    /----------\ + *	               -------> | LISTENING | -----------------> | CANCELED | + *	                        \-----------/  [canceled,]free   \----------/ + *	                              | + *	                              | accept + *	                              V + *	               /------ /-------------\                    /------------------------\ + *	   out_of_data |       | TRANSFERING | -----------------> | TRANSFERING | CANCELED | + *	               \-----> \-------------/  [canceled,]free   \------------------------/ + *	                              | + *	                              | finished,free + *	                              V + *	                 /------------------------\ + *	                 | TRANSFERING | FINISHED | + *	                 \------------------------/ + */ +typedef struct file_transfer { + +	/* Are we sending something? */ +	int sending; + +	/* +	 * The current status of this file transfer. +	 */  +	file_status_t status; +	 +	/* +	 * file size +	 */ +	size_t file_size; +	 +	/* +	 * Number of bytes that have been successfully transferred. +	 */ +	size_t bytes_transferred; + +	/* +	 * Time started. Used to calculate kb/s. +	 */ +	time_t started; + +	/* +	 * file name +	 */ +	char *file_name; + +	/* +	 * A unique local ID for this file transfer. +	 */ +	unsigned int local_id; + +	/* +	 * IM-protocol specific data associated with this file transfer. +	 */ +	gpointer data; +	struct im_connection *ic; +	 +	/* +	 * Private data. +	 */ +	gpointer priv; +	 +	/* +	 * If set, called after succesful connection setup. +	 */ +	void (*accept) ( struct file_transfer *file ); +	 +	/* +	 * If set, called when the transfer is canceled or finished. +	 * Subsequently, this structure will be freed. +	 * +	 */ +	void (*free) ( struct file_transfer *file ); +	 +	/* +	 * If set, called when the transfer is finished and successful. +	 */ +	void (*finished) ( struct file_transfer *file ); +	 +	/* +	 * If set, called when the transfer is canceled. +	 * ( canceled either by the transfer implementation or by +	 *  a call to imcb_file_canceled ) +	 */ +	void (*canceled) ( struct file_transfer *file, char *reason ); +	 +	/* +	 * called by the sending side to indicate that it is writable. +	 * The callee should check if data is available and call the  +	 * function(as seen below) if that is the case. +	 */ +	gboolean (*write_request) ( struct file_transfer *file ); + +	/* +	 * When sending files, protocols register this function to receive data. +	 * This should only be called once after write_request is called. The caller +	 * should not read more data until write_request is called again. This technique +	 * avoids buffering. +	 */ +	gboolean (*write) (struct file_transfer *file, char *buffer, unsigned int len ); + +	/* The send buffer associated with this transfer. +	 * Since receivers always wait for a write_request call one is enough. +	 */ +	char buffer[FT_BUFFER_SIZE]; + +} file_transfer_t; + +/* + * This starts a file transfer from bitlbee to the user. + */ +file_transfer_t *imcb_file_send_start( struct im_connection *ic, char *user_nick, char *file_name, size_t file_size ); + +/* + * This should be called by a protocol when the transfer is canceled. Note that + * the canceled() and free() callbacks given in file will be called by this function. + */ +void imcb_file_canceled( struct im_connection *ic, file_transfer_t *file, char *reason ); + +gboolean imcb_file_recv_start( struct im_connection *ic, file_transfer_t *ft ); + +void imcb_file_finished( struct im_connection *ic, file_transfer_t *file ); +#endif diff --git a/protocols/jabber/Makefile b/protocols/jabber/Makefile index e7a505ba..912ea702 100644 --- a/protocols/jabber/Makefile +++ b/protocols/jabber/Makefile @@ -7,9 +7,12 @@  ### DEFINITIONS  -include ../../Makefile.settings +ifdef SRCDIR +SRCDIR := $(SRCDIR)protocols/jabber/ +endif  # [SH] Program variables -objects = conference.o io.o iq.o jabber.o jabber_util.o message.o presence.o sasl.o +objects = conference.o io.o iq.o jabber.o jabber_util.o message.o presence.o s5bytestream.o sasl.o si.o  CFLAGS += -Wall  LFLAGS += -r @@ -32,7 +35,7 @@ distclean: clean  $(objects): ../../Makefile.settings Makefile -$(objects): %.o: %.c +$(objects): %.o: $(SRCDIR)%.c  	@echo '*' Compiling $<  	@$(CC) -c $(CFLAGS) $< -o $@ diff --git a/protocols/jabber/conference.c b/protocols/jabber/conference.c index affe8aef..e04b9792 100644 --- a/protocols/jabber/conference.c +++ b/protocols/jabber/conference.c @@ -91,18 +91,20 @@ static xt_status jabber_chat_join_failed( struct im_connection *ic, struct xt_no  struct groupchat *jabber_chat_by_jid( struct im_connection *ic, const char *name )  {  	char *normalized = jabber_normalize( name ); +	GSList *l;  	struct groupchat *ret;  	struct jabber_chat *jc; -	for( ret = ic->groupchats; ret; ret = ret->next ) +	for( l = ic->groupchats; l; l = l->next )  	{ +		ret = l->data;  		jc = ret->data;  		if( strcmp( normalized, jc->name ) == 0 )  			break;  	}  	g_free( normalized ); -	return ret; +	return l ? ret : NULL;  }  void jabber_chat_free( struct groupchat *c ) diff --git a/protocols/jabber/io.c b/protocols/jabber/io.c index a14ad21c..d6f92a5f 100644 --- a/protocols/jabber/io.c +++ b/protocols/jabber/io.c @@ -63,7 +63,7 @@ int jabber_write( struct im_connection *ic, char *buf, int len )  		   it via the event handler. If not, add the handler. (In  		   most cases it probably won't be necessary.) */  		if( ( ret = jabber_write_queue( ic ) ) && jd->tx_len > 0 ) -			jd->w_inpa = b_input_add( jd->fd, GAIM_INPUT_WRITE, jabber_write_callback, ic ); +			jd->w_inpa = b_input_add( jd->fd, B_EV_IO_WRITE, jabber_write_callback, ic );  	}  	else  	{ @@ -503,7 +503,7 @@ gboolean jabber_start_stream( struct im_connection *ic )  	jd->xt = xt_new( jabber_handlers, ic );  	if( jd->r_inpa <= 0 ) -		jd->r_inpa = b_input_add( jd->fd, GAIM_INPUT_READ, jabber_read_callback, ic ); +		jd->r_inpa = b_input_add( jd->fd, B_EV_IO_READ, jabber_read_callback, ic );  	greet = g_strdup_printf( "%s<stream:stream to=\"%s\" xmlns=\"jabber:client\" "  	                          "xmlns:stream=\"http://etherx.jabber.org/streams\" version=\"1.0\">",  diff --git a/protocols/jabber/iq.c b/protocols/jabber/iq.c index 68777b0b..82c90d39 100644 --- a/protocols/jabber/iq.c +++ b/protocols/jabber/iq.c @@ -90,14 +90,17 @@ xt_status jabber_pkt_iq( struct xt_node *node, gpointer data )  				xt_add_attr( reply, "id", s );  			pack = 0;  		} -		else if( strcmp( s, XMLNS_DISCOVER ) == 0 ) +		else if( strcmp( s, XMLNS_DISCO_INFO ) == 0 )  		{ -			const char *features[] = { XMLNS_DISCOVER, +			const char *features[] = { XMLNS_DISCO_INFO,  			                           XMLNS_VERSION,  			                           XMLNS_TIME,  			                           XMLNS_CHATSTATES,  			                           XMLNS_MUC,  			                           XMLNS_PING, +			                           XMLNS_SI, +			                           XMLNS_BYTESTREAMS, +			                           XMLNS_FILETRANSFER,  			                           NULL };  			const char **f; @@ -117,23 +120,28 @@ xt_status jabber_pkt_iq( struct xt_node *node, gpointer data )  		else  		{  			xt_free_node( reply ); -			reply = jabber_make_error_packet( node, "feature-not-implemented", "cancel" ); +			reply = jabber_make_error_packet( node, "feature-not-implemented", "cancel", NULL );  			pack = 0;  		}  	}  	else if( strcmp( type, "set" ) == 0 )  	{ -		if( !( c = xt_find_node( node->children, "query" ) ) || -		    !( s = xt_find_attr( c, "xmlns" ) ) ) +		if( ( c = xt_find_node( node->children, "si" ) ) && +		    ( s = xt_find_attr( c, "xmlns" ) ) && +		    ( strcmp( s, XMLNS_SI ) == 0 ) ) +		{ +			return jabber_si_handle_request( ic, node, c ); +		} +		else if( !( c = xt_find_node( node->children, "query" ) ) || +		         !( s = xt_find_attr( c, "xmlns" ) ) )  		{  			return XT_HANDLED;  		} -		 +		else if( strcmp( s, XMLNS_ROSTER ) == 0 ) +		{  		/* This is a roster push. XMPP servers send this when someone  		   was added to (or removed from) the buddy list. AFAIK they're  		   sent even if we added this buddy in our own session. */ -		if( strcmp( s, XMLNS_ROSTER ) == 0 ) -		{  			int bare_len = strlen( ic->acc->user );  			if( ( s = xt_find_attr( node, "from" ) ) == NULL || @@ -150,14 +158,19 @@ xt_status jabber_pkt_iq( struct xt_node *node, gpointer data )  				imcb_log( ic, "Warning: %s tried to fake a roster push!", s ? s : "(unknown)" );  				xt_free_node( reply ); -				reply = jabber_make_error_packet( node, "not-allowed", "cancel" ); +				reply = jabber_make_error_packet( node, "not-allowed", "cancel", NULL );  				pack = 0;  			}  		} +		else if( strcmp( s, XMLNS_BYTESTREAMS ) == 0 ) +		{ +			/* Bytestream Request (stage 2 of file transfer) */ +			return jabber_bs_recv_request( ic, node, c ); +		}  		else  		{  			xt_free_node( reply ); -			reply = jabber_make_error_packet( node, "feature-not-implemented", "cancel" ); +			reply = jabber_make_error_packet( node, "feature-not-implemented", "cancel", NULL );  			pack = 0;  		}  	} @@ -368,7 +381,7 @@ static xt_status jabber_parse_roster( struct im_connection *ic, struct xt_node *  	c = query->children;  	while( ( c = xt_find_node( c, "item" ) ) )  	{ -		struct xt_node *group = xt_find_node( node->children, "group" ); +		struct xt_node *group = xt_find_node( c->children, "group" );  		char *jid = xt_find_attr( c, "jid" );  		char *name = xt_find_attr( c, "name" );  		char *sub = xt_find_attr( c, "subscription" ); @@ -377,9 +390,8 @@ static xt_status jabber_parse_roster( struct im_connection *ic, struct xt_node *  		{  			if( ( strcmp( sub, "both" ) == 0 || strcmp( sub, "to" ) == 0 ) )  			{ -				if( initial || imcb_find_buddy( ic, jid ) == NULL ) -					imcb_add_buddy( ic, jid, ( group && group->text_len ) ? -					                           group->text : NULL ); +				imcb_add_buddy( ic, jid, ( group && group->text_len ) ? +				                           group->text : NULL );  				if( name )  					imcb_rename_buddy( ic, jid, name ); @@ -542,7 +554,7 @@ static xt_status jabber_iq_display_vcard( struct im_connection *ic, struct xt_no  static xt_status jabber_add_to_roster_callback( struct im_connection *ic, struct xt_node *node, struct xt_node *orig ); -int jabber_add_to_roster( struct im_connection *ic, char *handle, char *name ) +int jabber_add_to_roster( struct im_connection *ic, const char *handle, const char *name, const char *group )  {  	struct xt_node *node;  	int st; @@ -552,6 +564,8 @@ int jabber_add_to_roster( struct im_connection *ic, char *handle, char *name )  	xt_add_attr( node, "jid", handle );  	if( name )  		xt_add_attr( node, "name", name ); +	if( group ) +		xt_add_child( node, xt_new_node( "group", group, NULL ) );  	/* And pack it into a roster-add packet */  	node = xt_new_node( "query", NULL, node ); @@ -575,7 +589,7 @@ static xt_status jabber_add_to_roster_callback( struct im_connection *ic, struct  	    ( s = xt_find_attr( node, "type" ) ) &&  	    strcmp( s, "result" ) == 0 )  	{ -		if( imcb_find_buddy( ic, jid ) == NULL ) +		if( bee_user_by_handle( ic->bee, ic, jid ) == NULL )  			imcb_add_buddy( ic, jid, NULL );  	}  	else @@ -607,3 +621,175 @@ int jabber_remove_from_roster( struct im_connection *ic, char *handle )  	xt_free_node( node );  	return st;  } + +xt_status jabber_iq_parse_features( struct im_connection *ic, struct xt_node *node, struct xt_node *orig ); + +xt_status jabber_iq_query_features( struct im_connection *ic, char *bare_jid ) +{ +	struct xt_node *node, *query; +	struct jabber_buddy *bud; +	 +	if( ( bud = jabber_buddy_by_jid( ic, bare_jid , 0 ) ) == NULL ) +	{ +		/* Who cares about the unknown... */ +		imcb_log( ic, "Couldn't find buddy: %s", bare_jid); +		return XT_HANDLED; +	} +	 +	if( bud->features ) /* been here already */ +		return XT_HANDLED; +	 +	node = xt_new_node( "query", NULL, NULL ); +	xt_add_attr( node, "xmlns", XMLNS_DISCO_INFO ); +	 +	if( !( query = jabber_make_packet( "iq", "get", bare_jid, node ) ) ) +	{ +		imcb_log( ic, "WARNING: Couldn't generate feature query" ); +		xt_free_node( node ); +		return XT_HANDLED; +	} + +	jabber_cache_add( ic, query, jabber_iq_parse_features ); + +	return jabber_write_packet( ic, query ) ? XT_HANDLED : XT_ABORT; +} + +xt_status jabber_iq_parse_features( struct im_connection *ic, struct xt_node *node, struct xt_node *orig ) +{ +	struct xt_node *c; +	struct jabber_buddy *bud; +	char *feature, *xmlns, *from; + +	if( !( from = xt_find_attr( node, "from" ) ) || +	    !( c = xt_find_node( node->children, "query" ) ) || +	    !( xmlns = xt_find_attr( c, "xmlns" ) ) || +	    !( strcmp( xmlns, XMLNS_DISCO_INFO ) == 0 ) ) +	{ +		imcb_log( ic, "WARNING: Received incomplete IQ-result packet for discover" ); +		return XT_HANDLED; +	} +	if( ( bud = jabber_buddy_by_jid( ic, from, 0 ) ) == NULL ) +	{ +		/* Who cares about the unknown... */ +		imcb_log( ic, "Couldn't find buddy: %s", from ); +		return XT_HANDLED; +	} +	 +	c = c->children; +	while( ( c = xt_find_node( c, "feature" ) ) ) +	{ +		feature = xt_find_attr( c, "var" ); +		if( feature ) +			bud->features = g_slist_append( bud->features, g_strdup( feature ) ); +		c = c->next; +	} + +	return XT_HANDLED; +} + +xt_status jabber_iq_parse_server_features( struct im_connection *ic, struct xt_node *node, struct xt_node *orig ); + +xt_status jabber_iq_query_server( struct im_connection *ic, char *jid, char *xmlns ) +{ +	struct xt_node *node, *query; +	struct jabber_data *jd = ic->proto_data; +	 +	node = xt_new_node( "query", NULL, NULL ); +	xt_add_attr( node, "xmlns", xmlns ); +	 +	if( !( query = jabber_make_packet( "iq", "get", jid, node ) ) ) +	{ +		imcb_log( ic, "WARNING: Couldn't generate server query" ); +		xt_free_node( node ); +	} + +	jd->have_streamhosts--; +	jabber_cache_add( ic, query, jabber_iq_parse_server_features ); + +	return jabber_write_packet( ic, query ) ? XT_HANDLED : XT_ABORT; +} + +/* + * Query the server for "items", query each "item" for identities, query each "item" that's a proxy for it's bytestream info + */ +xt_status jabber_iq_parse_server_features( struct im_connection *ic, struct xt_node *node, struct xt_node *orig ) +{ +	struct xt_node *c; +	struct jabber_data *jd = ic->proto_data; +	char *xmlns, *from; + +	if( !( c = xt_find_node( node->children, "query" ) ) || +	    !( from = xt_find_attr( node, "from" ) ) || +	    !( xmlns = xt_find_attr( c, "xmlns" ) ) ) +	{ +		imcb_log( ic, "WARNING: Received incomplete IQ-result packet for discover" ); +		return XT_HANDLED; +	} + +	jd->have_streamhosts++; + +	if( strcmp( xmlns, XMLNS_DISCO_ITEMS ) == 0 ) +	{ +		char *itemjid; + +		/* answer from server */ +	 +		c = c->children; +		while( ( c = xt_find_node( c, "item" ) ) ) +		{ +			itemjid = xt_find_attr( c, "jid" ); +			 +			if( itemjid ) +				jabber_iq_query_server( ic, itemjid, XMLNS_DISCO_INFO ); + +			c = c->next; +		} +	} +	else if( strcmp( xmlns, XMLNS_DISCO_INFO ) == 0 ) +	{ +		char *category, *type; + +		/* answer from potential proxy */ + +		c = c->children; +		while( ( c = xt_find_node( c, "identity" ) ) ) +		{ +			category = xt_find_attr( c, "category" ); +			type = xt_find_attr( c, "type" ); + +			if( type && ( strcmp( type, "bytestreams" ) == 0 ) && +			    category && ( strcmp( category, "proxy" ) == 0 ) ) +				jabber_iq_query_server( ic, from, XMLNS_BYTESTREAMS ); + +			c = c->next; +		} +	} +	else if( strcmp( xmlns, XMLNS_BYTESTREAMS ) == 0 ) +	{ +		char *host, *jid, *port_s; +		int port; + +		/* answer from proxy */ + +		if( ( c = xt_find_node( c->children, "streamhost" ) ) && +		    ( host = xt_find_attr( c, "host" ) ) && +		    ( port_s = xt_find_attr( c, "port" ) ) && +		    ( sscanf( port_s, "%d", &port ) == 1 ) && +		    ( jid = xt_find_attr( c, "jid" ) ) ) +		{ +			jabber_streamhost_t *sh = g_new0( jabber_streamhost_t, 1 ); +			 +			sh->jid = g_strdup( jid ); +			sh->host = g_strdup( host ); +			g_snprintf( sh->port, sizeof( sh->port ), "%u", port ); + +			imcb_log( ic, "Proxy found: jid %s host %s port %u", jid, host, port ); +			jd->streamhosts = g_slist_append( jd->streamhosts, sh ); +		} +	} + +	if( jd->have_streamhosts == 0 ) +		jd->have_streamhosts++; + +	return XT_HANDLED; +} diff --git a/protocols/jabber/jabber.c b/protocols/jabber/jabber.c index 75351d0d..7147a628 100644 --- a/protocols/jabber/jabber.c +++ b/protocols/jabber/jabber.c @@ -64,6 +64,8 @@ static void jabber_init( account_t *acc )  	s->flags |= ACC_SET_OFFLINE_ONLY;  	s = set_add( &acc->set, "priority", "0", set_eval_priority, acc ); + +	s = set_add( &acc->set, "proxy", "<local>;<auto>", NULL, acc );  	s = set_add( &acc->set, "resource", "BitlBee", NULL, acc );  	s->flags |= ACC_SET_OFFLINE_ONLY; @@ -265,11 +267,23 @@ static void jabber_logout( struct im_connection *ic )  {  	struct jabber_data *jd = ic->proto_data; +	while( jd->filetransfers ) +		imcb_file_canceled( ic, ( ( struct jabber_transfer *) jd->filetransfers->data )->ft, "Logging out" ); + +	while( jd->streamhosts ) +	{ +		jabber_streamhost_t *sh = jd->streamhosts->data; +		jd->streamhosts = g_slist_remove( jd->streamhosts, sh ); +		g_free( sh->jid ); +		g_free( sh->host ); +		g_free( sh ); +	} +  	if( jd->fd >= 0 )  		jabber_end_stream( ic );  	while( ic->groupchats ) -		jabber_chat_free( ic->groupchats ); +		jabber_chat_free( ic->groupchats->data );  	if( jd->r_inpa >= 0 )  		b_event_remove( jd->r_inpa ); @@ -400,7 +414,7 @@ static void jabber_add_buddy( struct im_connection *ic, char *who, char *group )  		return;  	} -	if( jabber_add_to_roster( ic, who, NULL ) ) +	if( jabber_add_to_roster( ic, who, NULL, group ) )  		presence_send_request( ic, who, "subscribe" );  } @@ -548,6 +562,7 @@ void jabber_initmodule()  	ret->keepalive = jabber_keepalive;  	ret->send_typing = jabber_send_typing;  	ret->handle_cmp = g_strcasecmp; +	ret->transfer_request = jabber_si_transfer_request;  	register_protocol( ret );  } diff --git a/protocols/jabber/jabber.h b/protocols/jabber/jabber.h index 3f4144b8..45a1c5c1 100644 --- a/protocols/jabber/jabber.h +++ b/protocols/jabber/jabber.h @@ -60,6 +60,14 @@ typedef enum  	                                   have a real JID. */  } jabber_buddy_flags_t; +/* Stores a streamhost's (a.k.a. proxy) data */ +typedef struct +{ +	char *jid; +	char *host; +	char port[6]; +} jabber_streamhost_t; +  typedef enum  {  	JCFLAG_MESSAGE_SENT = 1,        /* Set this after sending the first message, so @@ -90,6 +98,10 @@ struct jabber_data  	md5_state_t cached_id_prefix;  	GHashTable *node_cache;  	GHashTable *buddies; + +	GSList *filetransfers; +	GSList *streamhosts; +	int have_streamhosts;  };  struct jabber_away_state @@ -126,6 +138,7 @@ struct jabber_buddy  	int priority;  	struct jabber_away_state *away_state;  	char *away_message; +	GSList *features;  	time_t last_msg;  	jabber_buddy_flags_t flags; @@ -141,6 +154,36 @@ struct jabber_chat  	struct jabber_buddy *me;  }; +struct jabber_transfer +{ +	/* bitlbee's handle for this transfer */ +	file_transfer_t *ft; + +	/* the stream's private handle */ +	gpointer streamhandle; + +	/* timeout for discover queries */ +	gint disco_timeout; +	gint disco_timeout_fired; + +	struct im_connection *ic; + +	struct jabber_buddy *bud; + +	int watch_in; +	int watch_out; + +	char *ini_jid; +	char *tgt_jid; +	char *iq_id; +	char *sid; +	int accepted; + +	size_t bytesread, byteswritten; +	int fd; +	struct sockaddr_storage saddr; +}; +  #define JABBER_XMLCONSOLE_HANDLE "xmlconsole"  /* Prefixes to use for packet IDs (mainly for IQ packets ATM). Usually the @@ -166,17 +209,24 @@ struct jabber_chat  #define XMLNS_ROSTER       "jabber:iq:roster"  /* Some supported extensions/legacy stuff */ -#define XMLNS_AUTH         "jabber:iq:auth"                     /* XEP-0078 */ -#define XMLNS_VERSION      "jabber:iq:version"                  /* XEP-0092 */ -#define XMLNS_TIME         "jabber:iq:time"                     /* XEP-0090 */ -#define XMLNS_PING         "urn:xmpp:ping"                      /* XEP-0199 */ -#define XMLNS_VCARD        "vcard-temp"                         /* XEP-0054 */ -#define XMLNS_DELAY        "jabber:x:delay"                     /* XEP-0091 */ -#define XMLNS_CHATSTATES   "http://jabber.org/protocol/chatstates"  /* 0085 */ -#define XMLNS_DISCOVER     "http://jabber.org/protocol/disco#info"  /* 0030 */ -#define XMLNS_MUC          "http://jabber.org/protocol/muc"     /* XEP-0045 */ -#define XMLNS_MUC_USER     "http://jabber.org/protocol/muc#user"/* XEP-0045 */ -#define XMLNS_CAPS         "http://jabber.org/protocol/caps"    /* XEP-0115 */ +#define XMLNS_AUTH         "jabber:iq:auth"                                      /* XEP-0078 */ +#define XMLNS_VERSION      "jabber:iq:version"                                   /* XEP-0092 */ +#define XMLNS_TIME         "jabber:iq:time"                                      /* XEP-0090 */ +#define XMLNS_PING         "urn:xmpp:ping"                                       /* XEP-0199 */ +#define XMLNS_VCARD        "vcard-temp"                                          /* XEP-0054 */ +#define XMLNS_DELAY        "jabber:x:delay"                                      /* XEP-0091 */ +#define XMLNS_XDATA        "jabber:x:data"                                       /* XEP-0004 */ +#define XMLNS_CHATSTATES   "http://jabber.org/protocol/chatstates"               /* XEP-0085 */ +#define XMLNS_DISCO_INFO   "http://jabber.org/protocol/disco#info"               /* XEP-0030 */ +#define XMLNS_DISCO_ITEMS  "http://jabber.org/protocol/disco#items"              /* XEP-0030 */ +#define XMLNS_MUC          "http://jabber.org/protocol/muc"                      /* XEP-0045 */ +#define XMLNS_MUC_USER     "http://jabber.org/protocol/muc#user"                 /* XEP-0045 */ +#define XMLNS_CAPS         "http://jabber.org/protocol/caps"                     /* XEP-0115 */ +#define XMLNS_FEATURE      "http://jabber.org/protocol/feature-neg"              /* XEP-0020 */ +#define XMLNS_SI           "http://jabber.org/protocol/si"                       /* XEP-0095 */ +#define XMLNS_FILETRANSFER "http://jabber.org/protocol/si/profile/file-transfer" /* XEP-0096 */ +#define XMLNS_BYTESTREAMS  "http://jabber.org/protocol/bytestreams"              /* XEP-0065 */ +#define XMLNS_IBB          "http://jabber.org/protocol/ibb"                      /* XEP-0047 */  /* iq.c */  xt_status jabber_pkt_iq( struct xt_node *node, gpointer data ); @@ -184,8 +234,20 @@ int jabber_init_iq_auth( struct im_connection *ic );  xt_status jabber_pkt_bind_sess( struct im_connection *ic, struct xt_node *node, struct xt_node *orig );  int jabber_get_roster( struct im_connection *ic );  int jabber_get_vcard( struct im_connection *ic, char *bare_jid ); -int jabber_add_to_roster( struct im_connection *ic, char *handle, char *name ); +int jabber_add_to_roster( struct im_connection *ic, const char *handle, const char *name, const char *group );  int jabber_remove_from_roster( struct im_connection *ic, char *handle ); +xt_status jabber_iq_query_features( struct im_connection *ic, char *bare_jid ); +xt_status jabber_iq_query_server( struct im_connection *ic, char *jid, char *xmlns ); + +/* si.c */ +int jabber_si_handle_request( struct im_connection *ic, struct xt_node *node, struct xt_node *sinode ); +void jabber_si_transfer_request( struct im_connection *ic, file_transfer_t *ft, char *who ); +void jabber_si_free_transfer( file_transfer_t *ft); + +/* s5bytestream.c */ +int jabber_bs_recv_request( struct im_connection *ic, struct xt_node *node, struct xt_node *qnode); +gboolean jabber_bs_send_start( struct jabber_transfer *tf ); +gboolean jabber_bs_send_write( file_transfer_t *ft, char *buffer, unsigned int len );  /* message.c */  xt_status jabber_pkt_message( struct xt_node *node, gpointer data ); @@ -199,7 +261,7 @@ int presence_send_request( struct im_connection *ic, char *handle, char *request  char *set_eval_priority( set_t *set, char *value );  char *set_eval_tls( set_t *set, char *value );  struct xt_node *jabber_make_packet( char *name, char *type, char *to, struct xt_node *children ); -struct xt_node *jabber_make_error_packet( struct xt_node *orig, char *err_cond, char *err_type ); +struct xt_node *jabber_make_error_packet( struct xt_node *orig, char *err_cond, char *err_type, char *err_code );  void jabber_cache_add( struct im_connection *ic, struct xt_node *node, jabber_cache_event func );  struct xt_node *jabber_cache_get( struct im_connection *ic, char *id );  void jabber_cache_entry_free( gpointer entry ); diff --git a/protocols/jabber/jabber_util.c b/protocols/jabber/jabber_util.c index 651b7068..ab3e6c38 100644 --- a/protocols/jabber/jabber_util.c +++ b/protocols/jabber/jabber_util.c @@ -98,7 +98,7 @@ struct xt_node *jabber_make_packet( char *name, char *type, char *to, struct xt_  	return node;  } -struct xt_node *jabber_make_error_packet( struct xt_node *orig, char *err_cond, char *err_type ) +struct xt_node *jabber_make_error_packet( struct xt_node *orig, char *err_cond, char *err_type, char *err_code )  {  	struct xt_node *node, *c;  	char *to; @@ -111,6 +111,10 @@ struct xt_node *jabber_make_error_packet( struct xt_node *orig, char *err_cond,  	c = xt_new_node( "error", NULL, c );  	xt_add_attr( c, "type", err_type ); +	/* Add the error code, if present */ +	if (err_code) +		xt_add_attr( c, "code", err_code ); +	  	/* To make the actual error packet, we copy the original packet and  	   add our <error>/type="error" tag. Including the original packet  	   is recommended, so let's just do it. */ @@ -274,8 +278,7 @@ static void jabber_buddy_ask_yes( void *data )  	presence_send_request( bla->ic, bla->handle, "subscribed" ); -	if( imcb_find_buddy( bla->ic, bla->handle ) == NULL ) -		imcb_ask_add( bla->ic, bla->handle, NULL ); +	imcb_ask_add( bla->ic, bla->handle, NULL );  	g_free( bla->handle );  	g_free( bla ); @@ -457,7 +460,7 @@ struct jabber_buddy *jabber_buddy_by_jid( struct im_connection *ic, char *jid_,  		}  		if( bud == NULL && ( flags & GET_BUDDY_CREAT ) && -		    ( bare_exists || imcb_find_buddy( ic, jid ) ) ) +		    ( bare_exists || bee_user_by_handle( ic->bee, ic, jid ) ) )  		{  			*s = '/';  			bud = jabber_buddy_add( ic, jid ); @@ -478,7 +481,8 @@ struct jabber_buddy *jabber_buddy_by_jid( struct im_connection *ic, char *jid_,  		if( bud == NULL )  			/* No match. Create it now? */ -			return ( ( flags & GET_BUDDY_CREAT ) && imcb_find_buddy( ic, jid_ ) ) ? +			return ( ( flags & GET_BUDDY_CREAT ) && +			         bee_user_by_handle( ic->bee, ic, jid_ ) ) ?  			           jabber_buddy_add( ic, jid_ ) : NULL;  		else if( bud->resource && ( flags & GET_BUDDY_EXACT ) )  			/* We want an exact match, so in thise case there shouldn't be a /resource. */ diff --git a/protocols/jabber/presence.c b/protocols/jabber/presence.c index 006eeead..2875d23e 100644 --- a/protocols/jabber/presence.c +++ b/protocols/jabber/presence.c @@ -204,7 +204,7 @@ int presence_send_update( struct im_connection *ic )  {  	struct jabber_data *jd = ic->proto_data;  	struct xt_node *node, *cap; -	struct groupchat *c; +	GSList *l;  	int st;  	node = jabber_make_packet( "presence", NULL, NULL, NULL ); @@ -228,8 +228,9 @@ int presence_send_update( struct im_connection *ic )  	/* Have to send this update to all groupchats too, the server won't  	   do this automatically. */ -	for( c = ic->groupchats; c && st; c = c->next ) +	for( l = ic->groupchats; l && st; l = l->next )  	{ +		struct groupchat *c = l->data;  		struct jabber_chat *jc = c->data;  		xt_add_attr( node, "to", jc->my_full_jid ); diff --git a/protocols/jabber/s5bytestream.c b/protocols/jabber/s5bytestream.c new file mode 100644 index 00000000..a8137271 --- /dev/null +++ b/protocols/jabber/s5bytestream.c @@ -0,0 +1,1154 @@ +/***************************************************************************\ +*                                                                           * +*  BitlBee - An IRC to IM gateway                                           * +*  Jabber module - SOCKS5 Bytestreams ( XEP-0065 )                          * +*                                                                           * +*  Copyright 2007 Uli Meis <a.sporto+bee@gmail.com>                         * +*                                                                           * +*  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 02110-1301 USA.              * +*                                                                           * +\***************************************************************************/ + +#include "jabber.h" +#include "sha1.h" +#include "lib/ftutil.h" +#include <poll.h> + +struct bs_transfer { + +	struct jabber_transfer *tf; + +	jabber_streamhost_t *sh; +	GSList *streamhosts; + +	enum  +	{  +		BS_PHASE_CONNECT,  +		BS_PHASE_CONNECTED,  +		BS_PHASE_REQUEST,  +		BS_PHASE_REPLY +	} phase; + +	/* SHA1( SID + Initiator JID + Target JID) */ +	char *pseudoadr; + +	gint connect_timeout; +	 +	char peek_buf[64]; +	int peek_buf_len; +}; + +struct socks5_message +{ +	unsigned char ver; +	union +	{ +		unsigned char cmd; +		unsigned char rep; +	} cmdrep; +	unsigned char rsv; +	unsigned char atyp; +	unsigned char addrlen; +	unsigned char address[40]; +	in_port_t port; +} __attribute__ ((packed));  + +char *socks5_reply_code[] = { +	"succeeded", +	"general SOCKS server failure", +	"connection not allowed by ruleset", +	"Network unreachable", +	"Host unreachable", +	"Connection refused", +	"TTL expired", +	"Command not supported", +	"Address type not supported", +	"unassigned"}; + +/* connect() timeout in seconds. */ +#define JABBER_BS_CONTIMEOUT 15 +/* listen timeout */ +#define JABBER_BS_LISTEN_TIMEOUT  90 + +/* very useful */ +#define ASSERTSOCKOP(op, msg) \ +	if( (op) == -1 ) \ +		return jabber_bs_abort( bt , msg ": %s", strerror( errno ) ); + +gboolean jabber_bs_abort( struct bs_transfer *bt, char *format, ... ); +void jabber_bs_canceled( file_transfer_t *ft , char *reason ); +void jabber_bs_free_transfer( file_transfer_t *ft ); +gboolean jabber_bs_connect_timeout( gpointer data, gint fd, b_input_condition cond ); +gboolean jabber_bs_poll( struct bs_transfer *bt, int fd, short *revents ); +gboolean jabber_bs_peek( struct bs_transfer *bt, void *buffer, int buflen ); + +void jabber_bs_recv_answer_request( struct bs_transfer *bt ); +gboolean jabber_bs_recv_read( gpointer data, gint fd, b_input_condition cond ); +gboolean jabber_bs_recv_write_request( file_transfer_t *ft ); +gboolean jabber_bs_recv_handshake( gpointer data, gint fd, b_input_condition cond ); +gboolean jabber_bs_recv_handshake_abort( struct bs_transfer *bt, char *error ); +int jabber_bs_recv_request( struct im_connection *ic, struct xt_node *node, struct xt_node *qnode ); + +gboolean jabber_bs_send_handshake_abort( struct bs_transfer *bt, char *error ); +gboolean jabber_bs_send_request( struct jabber_transfer *tf, GSList *streamhosts ); +gboolean jabber_bs_send_handshake( gpointer data, gint fd, b_input_condition cond ); +static xt_status jabber_bs_send_handle_activate( struct im_connection *ic, struct xt_node *node, struct xt_node *orig ); +void jabber_bs_send_activate( struct bs_transfer *bt ); + +/* + * Frees a bs_transfer struct and calls the SI free function + */ +void jabber_bs_free_transfer( file_transfer_t *ft) { +	struct jabber_transfer *tf = ft->data; +	struct bs_transfer *bt = tf->streamhandle; +	jabber_streamhost_t *sh; + +	if ( bt->connect_timeout ) +	{ +		b_event_remove( bt->connect_timeout ); +		bt->connect_timeout = 0; +	} + +	if ( tf->watch_in ) +		b_event_remove( tf->watch_in ); +	 +	if( tf->watch_out ) +		b_event_remove( tf->watch_out ); +	 +	g_free( bt->pseudoadr ); + +	while( bt->streamhosts ) +	{ +		sh = bt->streamhosts->data; +		bt->streamhosts = g_slist_remove( bt->streamhosts, sh ); +		g_free( sh->jid ); +		g_free( sh->host ); +		g_free( sh ); +	} +	 +	g_free( bt ); + +	jabber_si_free_transfer( ft ); +} + +/* + * Checks if buflen data is available on the socket and + * writes it to buffer if that's the case. + */ +gboolean jabber_bs_peek( struct bs_transfer *bt, void *buffer, int buflen ) +{ +	int ret; +	int fd = bt->tf->fd; + +	if( buflen > sizeof( bt->peek_buf ) ) +		return jabber_bs_abort( bt, "BUG: %d > sizeof(peek_buf)", buflen ); + +	ASSERTSOCKOP( ret = recv( fd, bt->peek_buf + bt->peek_buf_len, +		buflen - bt->peek_buf_len, 0 ), "recv() on SOCKS5 connection" ); + +	if( ret == 0 ) +		return jabber_bs_abort( bt, "Remote end closed connection" ); +	 +	bt->peek_buf_len += ret; +	memcpy( buffer, bt->peek_buf, bt->peek_buf_len ); +	 +	if( bt->peek_buf_len == buflen ) +	{ +		/* If we have everything the caller wanted, reset the peek buffer. */ +		bt->peek_buf_len = 0; +		return buflen; +	} +	else +		return bt->peek_buf_len; +} + + +/*  + * This function is scheduled in bs_handshake via b_timeout_add after a (non-blocking) connect(). + */ +gboolean jabber_bs_connect_timeout( gpointer data, gint fd, b_input_condition cond ) +{ +	struct bs_transfer *bt = data; + +	bt->connect_timeout = 0; + +	jabber_bs_abort( bt, "no connection after %d seconds", bt->tf->ft->sending ? JABBER_BS_LISTEN_TIMEOUT : JABBER_BS_CONTIMEOUT ); + +	return FALSE; +} + +/*  + * Polls the socket, checks for errors and removes a connect timer + * if there is one. + */ +gboolean jabber_bs_poll( struct bs_transfer *bt, int fd, short *revents ) +{ +	struct pollfd pfd = { .fd = fd, .events = POLLHUP|POLLERR }; +	 +	if ( bt->connect_timeout ) +	{ +		b_event_remove( bt->connect_timeout ); +		bt->connect_timeout = 0; +	} + +	ASSERTSOCKOP( poll( &pfd, 1, 0 ), "poll()" ) + +	if( pfd.revents & POLLERR ) +	{ +		int sockerror; +		socklen_t errlen = sizeof( sockerror ); + +		if ( getsockopt( fd, SOL_SOCKET, SO_ERROR, &sockerror, &errlen ) ) +			return jabber_bs_abort( bt, "getsockopt() failed, unknown socket error during SOCKS5 handshake (weird!)" ); + +		if ( bt->phase == BS_PHASE_CONNECTED ) +			return jabber_bs_abort( bt, "connect failed: %s", strerror( sockerror ) ); + +		return jabber_bs_abort( bt, "Socket error during SOCKS5 handshake(weird!): %s", strerror( sockerror ) ); +	} + +	if( pfd.revents & POLLHUP ) +		return jabber_bs_abort( bt, "Remote end closed connection" ); +	 +	*revents = pfd.revents; +	 +	return TRUE; +} + +/* + * Used for receive and send path. + */ +gboolean jabber_bs_abort( struct bs_transfer *bt, char *format, ... ) +{ +	va_list params; +	va_start( params, format ); +	char error[128]; + +	if( vsnprintf( error, 128, format, params ) < 0 ) +		sprintf( error, "internal error parsing error string (BUG)" ); +	va_end( params ); +	if( bt->tf->ft->sending ) +		return jabber_bs_send_handshake_abort( bt, error ); +	else +		return jabber_bs_recv_handshake_abort( bt, error ); +} + +/* Bad luck */ +void jabber_bs_canceled( file_transfer_t *ft , char *reason ) +{ +	struct jabber_transfer *tf = ft->data; + +	imcb_log( tf->ic, "File transfer aborted: %s", reason ); +} + +/* + * Parses an incoming bytestream request and calls jabber_bs_handshake on success. + */ +int jabber_bs_recv_request( struct im_connection *ic, struct xt_node *node, struct xt_node *qnode) +{ +	char *sid, *ini_jid, *tgt_jid, *mode, *iq_id; +	struct jabber_data *jd = ic->proto_data; +	struct jabber_transfer *tf = NULL; +	GSList *tflist; +	struct bs_transfer *bt; +	GSList *shlist=NULL; +	struct xt_node *shnode; + +	sha1_state_t sha; +	char hash_hex[41]; +	unsigned char hash[20]; +	int i; +	 +	if( !(iq_id   = xt_find_attr( node, "id" ) ) || +	    !(ini_jid = xt_find_attr( node, "from" ) ) || +	    !(tgt_jid = xt_find_attr( node, "to" ) ) || +	    !(sid     = xt_find_attr( qnode, "sid" ) ) ) +	{ +		imcb_log( ic, "WARNING: Received incomplete SI bytestream request"); +		return XT_HANDLED; +	} + +	if( ( mode = xt_find_attr( qnode, "mode" ) ) && +	      ( strcmp( mode, "tcp" ) != 0 ) )  +	{ +		imcb_log( ic, "WARNING: Received SI Request for unsupported bytestream mode %s", xt_find_attr( qnode, "mode" ) ); +		return XT_HANDLED; +	} + +	shnode = qnode->children; +	while( ( shnode = xt_find_node( shnode, "streamhost" ) ) ) +	{ +		char *jid, *host, *port_s; +		int port; +		if( ( jid = xt_find_attr( shnode, "jid" ) ) && +		    ( host = xt_find_attr( shnode, "host" ) ) && +		    ( port_s = xt_find_attr( shnode, "port" ) ) && +		    ( sscanf( port_s, "%d", &port ) == 1 ) ) +		{ +			jabber_streamhost_t *sh = g_new0( jabber_streamhost_t, 1 ); +			sh->jid = g_strdup(jid); +			sh->host = g_strdup(host); +			sprintf( sh->port, "%u", port ); +			shlist = g_slist_append( shlist, sh ); +		} +		shnode = shnode->next; +	} +	 +	if( !shlist ) +	{ +		imcb_log( ic, "WARNING: Received incomplete SI bytestream request, no parseable streamhost entries"); +		return XT_HANDLED; +	} + +	/* Let's see if we can find out what this bytestream should be for... */ + +	for( tflist = jd->filetransfers ; tflist; tflist = g_slist_next(tflist) ) +	{ +		struct jabber_transfer *tft = tflist->data; +		if( ( strcmp( tft->sid, sid ) == 0 ) && +		    ( strcmp( tft->ini_jid, ini_jid ) == 0 ) && +		    ( strcmp( tft->tgt_jid, tgt_jid ) == 0 ) ) +		{ +		    	tf = tft; +			break; +		} +	} + +	if (!tf)  +	{ +		imcb_log( ic, "WARNING: Received bytestream request from %s that doesn't match an SI request", ini_jid ); +		return XT_HANDLED; +	} + +	/* iq_id and canceled can be reused since SI is done */ +	g_free( tf->iq_id ); +	tf->iq_id = g_strdup( iq_id ); + +	tf->ft->canceled = jabber_bs_canceled; + +	/* SHA1( SID + Initiator JID + Target JID ) is given to the streamhost which it will match against the initiator's value */ +	sha1_init( &sha ); +	sha1_append( &sha, (unsigned char*) sid, strlen( sid ) ); +	sha1_append( &sha, (unsigned char*) ini_jid, strlen( ini_jid ) ); +	sha1_append( &sha, (unsigned char*) tgt_jid, strlen( tgt_jid ) ); +	sha1_finish( &sha, hash ); +	 +	for( i = 0; i < 20; i ++ ) +		sprintf( hash_hex + i * 2, "%02x", hash[i] ); +		 +	bt = g_new0( struct bs_transfer, 1 ); +	bt->tf = tf; +	bt->streamhosts = shlist; +	bt->sh = shlist->data; +	bt->phase = BS_PHASE_CONNECT; +	bt->pseudoadr = g_strdup( hash_hex ); +	tf->streamhandle = bt; +	tf->ft->free = jabber_bs_free_transfer; + +	jabber_bs_recv_handshake( bt, -1, 0 );  + +	return XT_HANDLED; +} + +/* + * This is what a protocol handshake can look like in cooperative multitasking :) + * Might be confusing at first because it's called from different places and is recursing. + * (places being the event thread, bs_request, bs_handshake_abort, and itself) + * + * All in all, it turned out quite nice :) + */ +gboolean jabber_bs_recv_handshake( gpointer data, gint fd, b_input_condition cond ) +{ + +	struct bs_transfer *bt = data; +	short revents; +	int gret; + +	if ( ( fd != -1 ) && !jabber_bs_poll( bt, fd, &revents ) ) +		return FALSE; +	 +	switch( bt->phase )  +	{ +	case BS_PHASE_CONNECT: +		{ +			struct addrinfo hints, *rp; + +			memset( &hints, 0, sizeof( struct addrinfo ) ); +			hints.ai_socktype = SOCK_STREAM; + +			if ( ( gret = getaddrinfo( bt->sh->host, bt->sh->port, &hints, &rp ) ) != 0 ) +				return jabber_bs_abort( bt, "getaddrinfo() failed: %s", gai_strerror( gret ) ); + +			ASSERTSOCKOP( bt->tf->fd = fd = socket( rp->ai_family, rp->ai_socktype, 0 ), "Opening socket" ); + +			sock_make_nonblocking( fd ); + +			imcb_log( bt->tf->ic, "File %s: Connecting to streamhost %s:%s", bt->tf->ft->file_name, bt->sh->host, bt->sh->port ); + +			if( ( connect( fd, rp->ai_addr, rp->ai_addrlen ) == -1 ) && +			    ( errno != EINPROGRESS ) ) +				return jabber_bs_abort( bt , "connect() failed: %s", strerror( errno ) ); + +			freeaddrinfo( rp ); + +			bt->phase = BS_PHASE_CONNECTED; +			 +			bt->tf->watch_out = b_input_add( fd, B_EV_IO_WRITE, jabber_bs_recv_handshake, bt ); + +			/* since it takes forever(3mins?) till connect() fails on itself we schedule a timeout */ +			bt->connect_timeout = b_timeout_add( JABBER_BS_CONTIMEOUT * 1000, jabber_bs_connect_timeout, bt ); + +			bt->tf->watch_in = 0; +			return FALSE; +		} +	case BS_PHASE_CONNECTED: +		{ +			struct { +				unsigned char ver; +				unsigned char nmethods; +				unsigned char method; +			} socks5_hello = { +				.ver = 5, +				.nmethods = 1, +				.method = 0x00 /* no auth */ +				/* one could also implement username/password. If you know +				 * a jabber client or proxy that actually does it, tell me. +				 */ +			}; +			 +			ASSERTSOCKOP( send( fd, &socks5_hello, sizeof( socks5_hello ) , 0 ), "Sending auth request" ); + +			bt->phase = BS_PHASE_REQUEST; + +			bt->tf->watch_in = b_input_add( fd, B_EV_IO_READ, jabber_bs_recv_handshake, bt ); + +			bt->tf->watch_out = 0; +			return FALSE; +		} +	case BS_PHASE_REQUEST: +		{ +			struct socks5_message socks5_connect =  +			{ +				.ver = 5, +				.cmdrep.cmd = 0x01, +				.rsv = 0, +				.atyp = 0x03, +				.addrlen = strlen( bt->pseudoadr ), +				.port = 0 +			}; +			int ret; +			char buf[2]; + +			/* If someone's trying to be funny and sends only one byte at a time we'll fail :) */ +			ASSERTSOCKOP( ret = recv( fd, buf, 2, 0 ) , "Receiving auth reply" ); + +			if( !( ret == 2 ) || +			    !( buf[0] == 5 ) || +			    !( buf[1] == 0 ) ) +				return jabber_bs_abort( bt, "Auth not accepted by streamhost (reply: len=%d, ver=%d, status=%d)", +									ret, buf[0], buf[1] ); + +			/* copy hash into connect message */ +			memcpy( socks5_connect.address, bt->pseudoadr, socks5_connect.addrlen ); + +			ASSERTSOCKOP( send( fd, &socks5_connect, sizeof( struct socks5_message ), 0 ) , "Sending SOCKS5 Connect" ); + +			bt->phase = BS_PHASE_REPLY; + +			return TRUE; +		} +	case BS_PHASE_REPLY: +		{ +			struct socks5_message socks5_reply; +			int ret; + +			if ( !( ret = jabber_bs_peek( bt, &socks5_reply, sizeof( struct socks5_message ) ) ) ) +				return FALSE; + +			if ( ret < 5 ) /* header up to address length */ +				return TRUE; +			else if( ret < sizeof( struct socks5_message ) ) +			{ +				/* Either a buggy proxy or just one that doesnt regard +				 * the SHOULD in XEP-0065 saying the reply SHOULD +				 * contain the address. We'll take it, so make sure the +				 * next jabber_bs_peek starts with an empty buffer. */ +				bt->peek_buf_len = 0; +			} + +			if( !( socks5_reply.ver == 5 ) || +			    !( socks5_reply.cmdrep.rep == 0 ) ) { +			    	char errstr[128] = ""; +				if( ( socks5_reply.ver == 5 ) && ( socks5_reply.cmdrep.rep <  +				    ( sizeof( socks5_reply_code ) / sizeof( socks5_reply_code[0] ) ) ) ) { +					sprintf( errstr, "with \"%s\" ", socks5_reply_code[ socks5_reply.cmdrep.rep ] ); +				} +				return jabber_bs_abort( bt, "SOCKS5 CONNECT failed %s(reply: ver=%d, rep=%d, atyp=%d, addrlen=%d)",  +					errstr, +					socks5_reply.ver, +					socks5_reply.cmdrep.rep, +					socks5_reply.atyp, +					socks5_reply.addrlen); +			} +			 +			/* usually a proxy sends back the 40 bytes address but I encountered at least one (of jabber.cz)  +			 * that sends atyp=0 addrlen=0 and only 6 bytes (one less than one would expect). +			 * Therefore I removed the wait for more bytes. Since we don't care about what else the proxy +			 * is sending, it shouldnt matter */ + +			if( bt->tf->ft->sending ) +				jabber_bs_send_activate( bt ); +			else +				jabber_bs_recv_answer_request( bt ); + +			return FALSE; +		} +	default: +		/* BUG */ +		imcb_log( bt->tf->ic, "BUG in file transfer code: undefined handshake phase" ); + +		bt->tf->watch_in = 0; +		return FALSE; +	} +} + +/* + * If the handshake failed we can try the next streamhost, if there is one. + * An intelligent sender would probably specify himself as the first streamhost and + * a proxy as the second (Kopete and PSI are examples here). That way, a (potentially)  + * slow proxy is only used if neccessary. This of course also means, that the timeout + * per streamhost should be kept short. If one or two firewalled adresses are specified, + * they have to timeout first before a proxy is tried. + */ +gboolean jabber_bs_recv_handshake_abort( struct bs_transfer *bt, char *error ) +{ +	struct jabber_transfer *tf = bt->tf; +	struct xt_node *reply, *iqnode; +	GSList *shlist; + +	imcb_log( tf->ic, "Transferring file %s: connection to streamhost %s:%s failed (%s)",  +		  tf->ft->file_name,  +		  bt->sh->host, +		  bt->sh->port, +		  error ); + +	/* Alright, this streamhost failed, let's try the next... */ +	bt->phase = BS_PHASE_CONNECT; +	shlist = g_slist_find( bt->streamhosts, bt->sh ); +	if( shlist && shlist->next ) +	{ +		bt->sh = shlist->next->data; +		return jabber_bs_recv_handshake( bt, -1, 0 ); +	} + + +	/* out of stream hosts */ + +	iqnode = jabber_make_packet( "iq", "result", tf->ini_jid, NULL ); +	reply = jabber_make_error_packet( iqnode, "item-not-found", "cancel" , "404" ); +	xt_free_node( iqnode ); + +	xt_add_attr( reply, "id", tf->iq_id ); +		 +	if( !jabber_write_packet( tf->ic, reply ) ) +		imcb_log( tf->ic, "WARNING: Error transmitting bytestream response" ); +	xt_free_node( reply ); + +	imcb_file_canceled( tf->ic, tf->ft, "couldn't connect to any streamhosts" ); + +	bt->tf->watch_in = 0; +	/* MUST always return FALSE! */ +	return FALSE; +} + +/*  + * After the SOCKS5 handshake succeeds we need to inform the initiator which streamhost we chose. + * If he is the streamhost himself, he might already know that. However, if it's a proxy, + * the initiator will have to make a connection himself. + */ +void jabber_bs_recv_answer_request( struct bs_transfer *bt ) +{ +	struct jabber_transfer *tf = bt->tf; +	struct xt_node *reply; + +	imcb_log( tf->ic, "File %s: established SOCKS5 connection to %s:%s",  +		  tf->ft->file_name,  +		  bt->sh->host, +		  bt->sh->port ); + +	tf->ft->data = tf; +	tf->watch_in = b_input_add( tf->fd, B_EV_IO_READ, jabber_bs_recv_read, bt ); +	tf->ft->write_request = jabber_bs_recv_write_request; + +	reply = xt_new_node( "streamhost-used", NULL, NULL ); +	xt_add_attr( reply, "jid", bt->sh->jid ); + +	reply = xt_new_node( "query", NULL, reply ); +	xt_add_attr( reply, "xmlns", XMLNS_BYTESTREAMS ); + +	reply = jabber_make_packet( "iq", "result", tf->ini_jid, reply ); + +	xt_add_attr( reply, "id", tf->iq_id ); +		 +	if( !jabber_write_packet( tf->ic, reply ) ) +		imcb_file_canceled( tf->ic, tf->ft, "Error transmitting bytestream response" ); +	xt_free_node( reply ); +} + +/*  + * This function is called from write_request directly. If no data is available, it will install itself + * as a watcher for input on fd and once that happens, deliver the data and unschedule itself again. + */ +gboolean jabber_bs_recv_read( gpointer data, gint fd, b_input_condition cond ) +{ +	int ret; +	struct bs_transfer *bt = data; +	struct jabber_transfer *tf = bt->tf; + +	if( fd != -1 ) /* called via event thread */ +	{ +		tf->watch_in = 0; +		ASSERTSOCKOP( ret = recv( fd, tf->ft->buffer, sizeof( tf->ft->buffer ), 0 ) , "Receiving" ); +	} +	else +	{ +		/* called directly. There might not be any data available. */ +		if( ( ( ret = recv( tf->fd, tf->ft->buffer, sizeof( tf->ft->buffer ), 0 ) ) == -1 ) && +		    ( errno != EAGAIN ) ) +		    return jabber_bs_abort( bt, "Receiving: %s", strerror( errno ) ); + +		if( ( ret == -1 ) && ( errno == EAGAIN ) ) +		{ +			tf->watch_in = b_input_add( tf->fd, B_EV_IO_READ, jabber_bs_recv_read, bt ); +			return FALSE; +		} +	} + +	/* shouldn't happen since we know the file size */ +	if( ret == 0 ) +		return jabber_bs_abort( bt, "Remote end closed connection" ); +	 +	tf->bytesread += ret; + +	if( tf->bytesread >= tf->ft->file_size ) +		imcb_file_finished( tf->ic, tf->ft ); + +	tf->ft->write( tf->ft, tf->ft->buffer, ret );	 + +	return FALSE; +} + +/*  + * imc callback that is invoked when it is ready to receive some data. + */ +gboolean jabber_bs_recv_write_request( file_transfer_t *ft ) +{ +	struct jabber_transfer *tf = ft->data; + +	if( tf->watch_in ) +	{ +		imcb_file_canceled( tf->ic, ft, "BUG in jabber file transfer: write_request called when already watching for input" ); +		return FALSE; +	} +	 +	jabber_bs_recv_read( tf->streamhandle, -1 , 0 ); + +	return TRUE; +} + +/*  + * Issues a write_request to imc. + * */ +gboolean jabber_bs_send_can_write( gpointer data, gint fd, b_input_condition cond ) +{ +	struct bs_transfer *bt = data; + +	bt->tf->watch_out = 0; + +	bt->tf->ft->write_request( bt->tf->ft ); + +	return FALSE; +} + +/* + * This should only be called if we can write, so just do it. + * Add a write watch so we can write more during the next cycle (if possible). + */ +gboolean jabber_bs_send_write( file_transfer_t *ft, char *buffer, unsigned int len ) +{ +	struct jabber_transfer *tf = ft->data; +	struct bs_transfer *bt = tf->streamhandle; +	int ret; + +	if( tf->watch_out ) +		return jabber_bs_abort( bt, "BUG: write() called while watching " ); +	 +	/* TODO: catch broken pipe */ +	ASSERTSOCKOP( ret = send( tf->fd, buffer, len, 0 ), "Sending" ); + +	tf->byteswritten += ret; +	 +	/* TODO: this should really not be fatal */ +	if( ret < len ) +		return jabber_bs_abort( bt, "send() sent %d instead of %d (send buffer too big!)", ret, len ); + +	if( tf->byteswritten >= ft->file_size ) +		imcb_file_finished( tf->ic, ft ); +	else +		bt->tf->watch_out = b_input_add( tf->fd, B_EV_IO_WRITE, jabber_bs_send_can_write, bt ); +		 +	return TRUE; +} + +/* + * Handles the reply by the receiver containing the used streamhost. + */ +static xt_status jabber_bs_send_handle_reply(struct im_connection *ic, struct xt_node *node, struct xt_node *orig ) { +	struct jabber_transfer *tf = NULL; +	struct jabber_data *jd = ic->proto_data; +	struct bs_transfer *bt; +	GSList *tflist; +	struct xt_node *c; +	char *sid, *jid; + +	if( !( c = xt_find_node( node->children, "query" ) ) || +	    !( c = xt_find_node( c->children, "streamhost-used" ) ) || +	    !( jid = xt_find_attr( c, "jid" ) ) ) + +	{ +		imcb_log( ic, "WARNING: Received incomplete bytestream reply" ); +		return XT_HANDLED; +	} +	 +	if( !( c = xt_find_node( orig->children, "query" ) ) || +	    !( sid = xt_find_attr( c, "sid" ) ) ) +	{ +		imcb_log( ic, "WARNING: Error parsing request corresponding to the incoming bytestream reply" ); +		return XT_HANDLED; +	} + +	/* Let's see if we can find out what this bytestream should be for... */ + +	for( tflist = jd->filetransfers ; tflist; tflist = g_slist_next(tflist) ) +	{ +		struct jabber_transfer *tft = tflist->data; +		if( ( strcmp( tft->sid, sid ) == 0 ) ) +		{ +		    	tf = tft; +			break; +		} +	} + +	if( !tf ) +	{ +		imcb_log( ic, "WARNING: Received SOCKS5 bytestream reply to unknown request" ); +		return XT_HANDLED; +	} + +	bt = tf->streamhandle; + +	tf->accepted = TRUE; + +	if( strcmp( jid, tf->ini_jid ) == 0 ) +	{ +		/* we're streamhost and target */ +		if( bt->phase == BS_PHASE_REPLY ) +		{ +			/* handshake went through, let's start transferring */ +			tf->ft->write_request( tf->ft ); +		} +	} else +	{ +		/* using a proxy, abort listen */ + +		if( tf->watch_in ) +		{ +			b_event_remove( tf->watch_in ); +			tf->watch_in = 0; +		} +		 +		if( tf->fd != -1 ) { +			closesocket( tf->fd ); +			tf->fd = -1; +		} + +		if ( bt->connect_timeout ) +		{ +			b_event_remove( bt->connect_timeout ); +			bt->connect_timeout = 0; +		} + +		GSList *shlist; +		for( shlist = jd->streamhosts ; shlist ; shlist = g_slist_next( shlist ) ) +		{ +			jabber_streamhost_t *sh = shlist->data; +			if( strcmp( sh->jid, jid ) == 0 ) +			{ +				bt->sh = sh; +				jabber_bs_recv_handshake( bt, -1, 0 ); +				return XT_HANDLED; +			} +		} + +		imcb_log( ic, "WARNING: Received SOCKS5 bytestream reply with unknown streamhost %s", jid ); +	} + +	return XT_HANDLED; +} + +/*  + * Tell the proxy to activate the stream. Looks like this: + * + * <iq type=set> + * 	<query xmlns=bs sid=sid> + * 		<activate>tgt_jid</activate> + * 	</query> + * </iq> + */ +void jabber_bs_send_activate( struct bs_transfer *bt ) +{ +	struct xt_node *node; + +	node = xt_new_node( "activate", bt->tf->tgt_jid, NULL ); +	node = xt_new_node( "query", NULL, node ); +	xt_add_attr( node, "xmlns", XMLNS_BYTESTREAMS ); +	xt_add_attr( node, "sid", bt->tf->sid ); +	node = jabber_make_packet( "iq", "set", bt->sh->jid, node ); + +	jabber_cache_add( bt->tf->ic, node, jabber_bs_send_handle_activate ); + +	jabber_write_packet( bt->tf->ic, node ); +} + +/* + * The proxy has activated the bytestream. + * We can finally start pushing some data out. + */ +static xt_status jabber_bs_send_handle_activate( struct im_connection *ic, struct xt_node *node, struct xt_node *orig ) +{ +	char *sid; +	GSList *tflist; +	struct jabber_transfer *tf = NULL; +	struct xt_node *query; +	struct jabber_data *jd = ic->proto_data; + +	query = xt_find_node( orig->children, "query" ); +	sid = xt_find_attr( query, "sid" ); + +	for( tflist = jd->filetransfers ; tflist; tflist = g_slist_next(tflist) ) +	{ +		struct jabber_transfer *tft = tflist->data; +		if( ( strcmp( tft->sid, sid ) == 0 ) ) +		{ +		    	tf = tft; +			break; +		} +	} + +	if( !tf ) +	{ +		imcb_log( ic, "WARNING: Received SOCKS5 bytestream activation for unknown stream" ); +		return XT_HANDLED; +	} + +	imcb_log( tf->ic, "File %s: SOCKS5 handshake and activation successful! Transfer about to start...", tf->ft->file_name ); + +	/* handshake went through, let's start transferring */ +	tf->ft->write_request( tf->ft ); + +	return XT_HANDLED; +} + +jabber_streamhost_t *jabber_si_parse_proxy( struct im_connection *ic, char *proxy ) +{ +	char *host, *port, *jid; +	jabber_streamhost_t *sh; + +	if( ( ( host = strchr( proxy, ',' ) ) == 0 ) || +	     ( ( port = strchr( host+1, ',' ) ) == 0 ) ) { +		imcb_log( ic, "Error parsing proxy setting: \"%s\" (ignored)", proxy ); +		return NULL; +	} +	 +	jid = proxy; +	*host++ = '\0'; +	*port++ = '\0'; + +	sh = g_new0( jabber_streamhost_t, 1 ); +	sh->jid = g_strdup( jid ); +	sh->host = g_strdup( host ); +	strcpy( sh->port, port ); + +	return sh; +} + +void jabber_si_set_proxies( struct bs_transfer *bt ) +{ +	struct jabber_transfer *tf = bt->tf; +	struct jabber_data *jd = tf->ic->proto_data; +	char *proxysetting = g_strdup ( set_getstr( &tf->ic->acc->set, "proxy" ) ); +	char *proxy, *next, *errmsg = NULL; +	char port[6]; +	char host[HOST_NAME_MAX+1]; +	jabber_streamhost_t *sh, *sh2; +	GSList *streamhosts = jd->streamhosts; + +	proxy = proxysetting; +	while ( proxy && ( *proxy!='\0' ) ) { +		if( ( next = strchr( proxy, ';' ) ) ) +			*next++ = '\0';	 +		 +		if( strcmp( proxy, "<local>" ) == 0 ) { +			if( ( tf->fd = ft_listen( &tf->saddr, host, port, FALSE, &errmsg ) ) != -1 ) { +				sh = g_new0( jabber_streamhost_t, 1 ); +				sh->jid = g_strdup( tf->ini_jid ); +				sh->host = g_strdup( host ); +				strcpy( sh->port, port ); +				bt->streamhosts = g_slist_append( bt->streamhosts, sh ); + +				bt->tf->watch_in = b_input_add( tf->fd, B_EV_IO_READ, jabber_bs_send_handshake, bt ); +				bt->connect_timeout = b_timeout_add( JABBER_BS_LISTEN_TIMEOUT * 1000, jabber_bs_connect_timeout, bt ); +			} else { +				imcb_log( tf->ic, "Transferring file %s: couldn't listen locally(non fatal, check your ft_listen setting in bitlbee.conf): %s", +					  tf->ft->file_name, +					  errmsg ); +			} +		} else if( strcmp( proxy, "<auto>" ) == 0 ) { +			while ( streamhosts ) { +				sh = g_new0( jabber_streamhost_t, 1 ); +				sh2 = streamhosts->data; +				sh->jid = g_strdup( sh2->jid ); +				sh->host = g_strdup( sh2->host ); +				strcpy( sh->port, sh2->port ); +				bt->streamhosts = g_slist_append( bt->streamhosts, sh ); +				streamhosts = g_slist_next( streamhosts ); +			} +		} else if( ( sh = jabber_si_parse_proxy( tf->ic, proxy ) ) ) +			bt->streamhosts = g_slist_append( bt->streamhosts, sh ); +		proxy = next; +	} +} + +/* + * Starts a bytestream. + */ +gboolean jabber_bs_send_start( struct jabber_transfer *tf ) +{ +	struct bs_transfer *bt; +	sha1_state_t sha; +	char hash_hex[41]; +	unsigned char hash[20]; +	int i,ret; + +	/* SHA1( SID + Initiator JID + Target JID ) is given to the streamhost which it will match against the initiator's value */ +	sha1_init( &sha ); +	sha1_append( &sha, (unsigned char*) tf->sid, strlen( tf->sid ) ); +	sha1_append( &sha, (unsigned char*) tf->ini_jid, strlen( tf->ini_jid ) ); +	sha1_append( &sha, (unsigned char*) tf->tgt_jid, strlen( tf->tgt_jid ) ); +	sha1_finish( &sha, hash ); +	 +	for( i = 0; i < 20; i ++ ) +		sprintf( hash_hex + i * 2, "%02x", hash[i] ); +		 +	bt = g_new0( struct bs_transfer, 1 ); +	bt->tf = tf; +	bt->phase = BS_PHASE_CONNECT; +	bt->pseudoadr = g_strdup( hash_hex ); +	tf->streamhandle = bt; +	tf->ft->free = jabber_bs_free_transfer; +	tf->ft->canceled = jabber_bs_canceled; + +	jabber_si_set_proxies( bt ); + +	ret = jabber_bs_send_request( tf, bt->streamhosts); + +	return ret; +} + +gboolean jabber_bs_send_request( struct jabber_transfer *tf, GSList *streamhosts ) +{ +	struct xt_node *shnode, *query, *iq; + +	query = xt_new_node( "query", NULL, NULL ); +	xt_add_attr( query, "xmlns", XMLNS_BYTESTREAMS ); +	xt_add_attr( query, "sid", tf->sid ); +	xt_add_attr( query, "mode", "tcp" ); + +	while( streamhosts ) { +		jabber_streamhost_t *sh = streamhosts->data; +		shnode = xt_new_node( "streamhost", NULL, NULL ); +		xt_add_attr( shnode, "jid", sh->jid ); +		xt_add_attr( shnode, "host", sh->host ); +		xt_add_attr( shnode, "port", sh->port ); + +		xt_add_child( query, shnode ); + +		streamhosts = g_slist_next( streamhosts ); +	} + + +	iq = jabber_make_packet( "iq", "set", tf->tgt_jid, query ); +	xt_add_attr( iq, "from", tf->ini_jid ); + +	jabber_cache_add( tf->ic, iq, jabber_bs_send_handle_reply ); + +	if( !jabber_write_packet( tf->ic, iq ) ) +		imcb_file_canceled( tf->ic, tf->ft, "Error transmitting bytestream request" ); +	return TRUE; +} + +gboolean jabber_bs_send_handshake_abort(struct bs_transfer *bt, char *error ) +{ +	struct jabber_transfer *tf = bt->tf; +	struct jabber_data *jd = tf->ic->proto_data; + +	/* TODO: did the receiver get here somehow??? */ +	imcb_log( tf->ic, "Transferring file %s: SOCKS5 handshake failed: %s",  +		  tf->ft->file_name,  +		  error ); + +	if( jd->streamhosts==NULL ) /* we're done here unless we have a proxy to try */ +		imcb_file_canceled( tf->ic, tf->ft, error ); + +	/* MUST always return FALSE! */ +	return FALSE; +} + +/* + * SOCKS5BYTESTREAM protocol for the sender + */ +gboolean jabber_bs_send_handshake( gpointer data, gint fd, b_input_condition cond ) +{ +	struct bs_transfer *bt = data; +	struct jabber_transfer *tf = bt->tf; +	short revents; + +	if ( !jabber_bs_poll( bt, fd, &revents ) ) +		return FALSE; +	 +	switch( bt->phase )  +	{ +	case BS_PHASE_CONNECT: +		{ +			struct sockaddr_storage clt_addr; +			socklen_t ssize = sizeof( clt_addr ); +			 +			/* Connect */ + +			ASSERTSOCKOP( tf->fd = accept( fd, (struct sockaddr *) &clt_addr, &ssize ), "Accepting connection" ); + +			closesocket( fd ); +			fd = tf->fd; +			sock_make_nonblocking( fd ); +			 +			bt->phase = BS_PHASE_CONNECTED; + +			bt->tf->watch_in = b_input_add( fd, B_EV_IO_READ, jabber_bs_send_handshake, bt ); +			return FALSE; +		} +	case BS_PHASE_CONNECTED: +		{ +			int ret, have_noauth=FALSE; +			struct { +				unsigned char ver; +				unsigned char method; +			} socks5_auth_reply = { .ver = 5, .method = 0 }; +			struct { +				unsigned char ver; +				unsigned char nmethods; +				unsigned char method; +			} socks5_hello; + +			if( !( ret = jabber_bs_peek( bt, &socks5_hello, sizeof( socks5_hello ) ) ) ) +				return FALSE; + +			if( ret < sizeof( socks5_hello ) ) +				return TRUE; + +			if( !( socks5_hello.ver == 5 ) || +			    !( socks5_hello.nmethods >= 1 ) || +			    !( socks5_hello.nmethods < 32 ) ) +				return jabber_bs_abort( bt, "Invalid auth request ver=%d nmethods=%d method=%d", socks5_hello.ver, socks5_hello.nmethods, socks5_hello.method ); + +			have_noauth = socks5_hello.method == 0; + +			if( socks5_hello.nmethods > 1 ) +			{ +				char mbuf[32]; +				int i; +				ASSERTSOCKOP( ret = recv( fd, mbuf, socks5_hello.nmethods - 1, 0 ) , "Receiving auth methods" ); +				if( ret < ( socks5_hello.nmethods - 1 ) ) +					return jabber_bs_abort( bt, "Partial auth request"); +				for( i = 0 ; !have_noauth && ( i < socks5_hello.nmethods - 1 ) ; i ++ ) +					if( mbuf[i] == 0 ) +						have_noauth = TRUE; +			} +			 +			if( !have_noauth ) +				return jabber_bs_abort( bt, "Auth request didn't include no authentication" ); + +			ASSERTSOCKOP( send( fd, &socks5_auth_reply, sizeof( socks5_auth_reply ) , 0 ), "Sending auth reply" ); + +			bt->phase = BS_PHASE_REQUEST; + +			return TRUE; +		} +	case BS_PHASE_REQUEST: +		{ +			struct socks5_message socks5_connect; +			int msgsize = sizeof( struct socks5_message ); +			int ret; + +			if( !( ret = jabber_bs_peek( bt, &socks5_connect, msgsize ) ) ) +				return FALSE; + +			if( ret < msgsize ) +				return TRUE; + +			if( !( socks5_connect.ver == 5) || +			    !( socks5_connect.cmdrep.cmd == 1 ) || +			    !( socks5_connect.atyp == 3 ) || +			    !(socks5_connect.addrlen == 40 ) ) +				return jabber_bs_abort( bt, "Invalid SOCKS5 Connect message (addrlen=%d, ver=%d, cmd=%d, atyp=%d)", socks5_connect.addrlen, socks5_connect.ver, socks5_connect.cmdrep.cmd, socks5_connect.atyp ); +			if( !( memcmp( socks5_connect.address, bt->pseudoadr, 40 ) == 0 ) ) +				return jabber_bs_abort( bt, "SOCKS5 Connect message contained wrong digest"); + +			socks5_connect.cmdrep.rep = 0; + +			ASSERTSOCKOP( send( fd, &socks5_connect, msgsize, 0 ), "Sending connect reply" ); + +			bt->phase = BS_PHASE_REPLY; + +			imcb_log( tf->ic, "File %s: SOCKS5 handshake successful! Transfer about to start...", tf->ft->file_name ); + +			if( tf->accepted ) +			{ +				/* streamhost-used message came already in(possible?), let's start sending */ +				tf->ft->write_request( tf->ft ); +			} + +			tf->watch_in = 0; +			return FALSE; + +		} +	default: +		/* BUG */ +		imcb_log( bt->tf->ic, "BUG in file transfer code: undefined handshake phase" ); + +		bt->tf->watch_in = 0; +		return FALSE; +	} +} +#undef ASSERTSOCKOP diff --git a/protocols/jabber/si.c b/protocols/jabber/si.c new file mode 100644 index 00000000..58c0e17f --- /dev/null +++ b/protocols/jabber/si.c @@ -0,0 +1,529 @@ +/***************************************************************************\ +*                                                                           * +*  BitlBee - An IRC to IM gateway                                           * +*  Jabber module - SI packets                                               * +*                                                                           * +*  Copyright 2007 Uli Meis <a.sporto+bee@gmail.com>                         * +*                                                                           * +*  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 02110-1301 USA.              * +*                                                                           * +\***************************************************************************/ + +#include "jabber.h" +#include "sha1.h" + +void jabber_si_answer_request( file_transfer_t *ft ); +int jabber_si_send_request(struct im_connection *ic, char *who, struct jabber_transfer *tf ); + +/* file_transfer free() callback */ +void jabber_si_free_transfer( file_transfer_t *ft) +{ +	struct jabber_transfer *tf = ft->data; +	struct jabber_data *jd = tf->ic->proto_data; + +	if ( tf->watch_in ) +		b_event_remove( tf->watch_in ); + +	jd->filetransfers = g_slist_remove( jd->filetransfers, tf ); + +	if( tf->fd != -1 ) +	{ +		closesocket( tf->fd ); +		tf->fd = -1; +	} + +	if( tf->disco_timeout ) +		b_event_remove( tf->disco_timeout ); +	 +	g_free( tf->ini_jid ); +	g_free( tf->tgt_jid ); +	g_free( tf->iq_id ); +	g_free( tf->sid ); +	g_free( tf ); +} + +/* file_transfer canceled() callback */ +void jabber_si_canceled( file_transfer_t *ft, char *reason ) +{ +	struct jabber_transfer *tf = ft->data; +	struct xt_node *reply, *iqnode; + +	if( tf->accepted ) +		return; +	 +	iqnode = jabber_make_packet( "iq", "error", tf->ini_jid, NULL ); +	xt_add_attr( iqnode, "id", tf->iq_id ); +	reply = jabber_make_error_packet( iqnode, "forbidden", "cancel", "403" ); +	xt_free_node( iqnode ); +	 +	if( !jabber_write_packet( tf->ic, reply ) ) +		imcb_log( tf->ic, "WARNING: Error generating reply to file transfer request" ); +	xt_free_node( reply ); + +} + +int jabber_si_check_features( struct jabber_transfer *tf, GSList *features ) { +	int foundft = FALSE, foundbt = FALSE, foundsi = FALSE; + +	while ( features ) +	{ +		if( !strcmp( features->data, XMLNS_FILETRANSFER ) ) +			foundft = TRUE; +		if( !strcmp( features->data, XMLNS_BYTESTREAMS ) ) +			foundbt = TRUE; +		if( !strcmp( features->data, XMLNS_SI ) ) +			foundsi = TRUE; + +		features = g_slist_next(features); +	} + +	if( !foundft ) +		imcb_file_canceled( tf->ic, tf->ft, "Buddy's client doesn't feature file transfers" ); +	else if( !foundbt ) +		imcb_file_canceled( tf->ic, tf->ft, "Buddy's client doesn't feature byte streams (required)" ); +	else if( !foundsi ) +		imcb_file_canceled( tf->ic, tf->ft, "Buddy's client doesn't feature stream initiation (required)" ); +		 +	return foundft && foundbt && foundsi; +} + +void jabber_si_transfer_start( struct jabber_transfer *tf ) { + +	if( !jabber_si_check_features( tf, tf->bud->features ) ) +		return; +		 +	/* send the request to our buddy */ +	jabber_si_send_request( tf->ic, tf->bud->full_jid, tf ); + +	/* and start the receive logic */ +	imcb_file_recv_start( tf->ic, tf->ft ); + +} + +gboolean jabber_si_waitfor_disco( gpointer data, gint fd, b_input_condition cond ) +{ +	struct jabber_transfer *tf = data; +	struct jabber_data *jd = tf->ic->proto_data; + +	tf->disco_timeout_fired++; + +	if( tf->bud->features && jd->have_streamhosts==1 ) { +		tf->disco_timeout = 0; +		jabber_si_transfer_start( tf ); +		return FALSE; +	} + +	/* 8 seconds should be enough for server and buddy to respond */ +	if ( tf->disco_timeout_fired < 16 ) +		return TRUE; +	 +	if( !tf->bud->features && jd->have_streamhosts!=1 ) +		imcb_log( tf->ic, "Couldn't get buddy's features nor discover all services of the server" ); +	else if( !tf->bud->features ) +		imcb_log( tf->ic, "Couldn't get buddy's features" ); +	else +		imcb_log( tf->ic, "Couldn't discover some of the server's services" ); +	 +	tf->disco_timeout = 0; +	jabber_si_transfer_start( tf ); +	return FALSE; +} + +void jabber_si_transfer_request( struct im_connection *ic, file_transfer_t *ft, char *who )  +{ +	struct jabber_transfer *tf; +	struct jabber_data *jd = ic->proto_data; +	struct jabber_buddy *bud; +	char *server = jd->server, *s; + +	if( ( s = strchr( who, '=' ) ) && jabber_chat_by_jid( ic, s + 1 ) ) +		bud = jabber_buddy_by_ext_jid( ic, who, 0 ); +	else +		bud = jabber_buddy_by_jid( ic, who, 0 ); + +	if( bud == NULL ) +	{ +		imcb_file_canceled( ic, ft, "Couldn't find buddy (BUG?)" ); +		return; +	} +	 +	imcb_log( ic, "Trying to send %s(%zd bytes) to %s", ft->file_name, ft->file_size, who ); + +	tf = g_new0( struct jabber_transfer, 1 ); + +	tf->ic = ic; +	tf->ft = ft; +	tf->fd = -1; +	tf->ft->data = tf; +	tf->ft->free = jabber_si_free_transfer; +	tf->bud = bud; +	ft->write = jabber_bs_send_write; + +	jd->filetransfers = g_slist_prepend( jd->filetransfers, tf ); + +	/* query buddy's features and server's streaming proxies if neccessary */ + +	if( !tf->bud->features ) +		jabber_iq_query_features( ic, bud->full_jid ); + +	/* If <auto> is not set don't check for proxies */ +	if( ( jd->have_streamhosts!=1 ) && ( jd->streamhosts==NULL ) && +	    ( strstr( set_getstr( &ic->acc->set, "proxy" ), "<auto>" ) != NULL ) ) { +		jd->have_streamhosts = 0; +		jabber_iq_query_server( ic, server, XMLNS_DISCO_ITEMS ); +	} else if ( jd->streamhosts!=NULL ) +		jd->have_streamhosts = 1; + +	/* if we had to do a query, wait for the result.  +	 * Otherwise fire away. */ +	if( !tf->bud->features || jd->have_streamhosts!=1 ) +		tf->disco_timeout = b_timeout_add( 500, jabber_si_waitfor_disco, tf ); +	else +		jabber_si_transfer_start( tf ); +} + +/* + * First function that gets called when a file transfer request comes in. + * A lot to parse. + * + * We choose a stream type from the options given by the initiator. + * Then we wait for imcb to call the accept or cancel callbacks. + */ +int jabber_si_handle_request( struct im_connection *ic, struct xt_node *node, struct xt_node *sinode) +{ +	struct xt_node *c, *d, *reply; +	char *sid, *ini_jid, *tgt_jid, *iq_id, *s, *ext_jid, *size_s; +	struct jabber_buddy *bud; +	int requestok = FALSE; +	char *name, *cmp; +	size_t size; +	struct jabber_transfer *tf; +	struct jabber_data *jd = ic->proto_data; +	file_transfer_t *ft; +	 +	/* All this means we expect something like this: ( I think ) +	 * <iq from=... to=... id=...> +	 * 	<si id=id xmlns=si profile=ft> +	 * 		<file xmlns=ft/> +	 * 		<feature xmlns=feature> +	 * 			<x xmlns=xdata type=submit> +	 * 				<field var=stream-method> +	 * +	 */ +	if( !( ini_jid 		= xt_find_attr(   node, "from" ) 			) || +	    !( tgt_jid 		= xt_find_attr(   node, "to" ) 				) || +	    !( iq_id 		= xt_find_attr(   node, "id" ) 				) || +	    !( sid 		= xt_find_attr( sinode, "id" ) 				) || +	    !( cmp              = xt_find_attr( sinode, "profile" )                     ) || +	    !( 0               == strcmp( cmp, XMLNS_FILETRANSFER )			) || +	    !( d 		= xt_find_node( sinode->children, "file" ) 		) || +	    !( cmp = xt_find_attr( d, "xmlns" )						) || +	    !( 0               == strcmp( cmp, XMLNS_FILETRANSFER )			) || +	    !( name 		= xt_find_attr( d, "name" ) 				) || +	    !( size_s           = xt_find_attr( d, "size" )                             ) || +	    !( 1               == sscanf( size_s, "%zd", &size )                        ) || +	    !( d 		= xt_find_node( sinode->children, "feature" ) 		) || +	    !( cmp              = xt_find_attr( d, "xmlns" )				) || +	    !( 0               == strcmp( cmp, XMLNS_FEATURE )				) || +	    !( d 		= xt_find_node( d->children, "x" ) 			) || +	    !( cmp              = xt_find_attr( d, "xmlns" )				) || +	    !( 0               == strcmp( cmp, XMLNS_XDATA )				) || +	    !( cmp              = xt_find_attr( d, "type" )				) || +	    !( 0               == strcmp( cmp, "form" )					) || +	    !( d 		= xt_find_node( d->children, "field" ) 			) || +	    !( cmp              = xt_find_attr( d, "var" )				) || +	    !( 0               == strcmp( cmp, "stream-method" )			) ) +	{ +		imcb_log( ic, "WARNING: Received incomplete Stream Initiation request" ); +	} +	else +	{ +		/* Check if we support one of the options */ + +		c = d->children; +		while( ( c = xt_find_node( c, "option" ) ) ) +			if( ( d = xt_find_node( c->children, "value" ) ) && +			    ( d->text != NULL ) && +			    ( strcmp( d->text, XMLNS_BYTESTREAMS ) == 0 ) ) +			{ +				requestok = TRUE; +				break; +			} + +		if ( !requestok ) +			imcb_log( ic, "WARNING: Unsupported file transfer request from %s", ini_jid); +	} +	 +	if( requestok ) +	{ +		/* Figure out who the transfer should come frome... */ + +		ext_jid = ini_jid; +		if( ( s = strchr( ini_jid, '/' ) ) ) +		{ +			if( ( bud = jabber_buddy_by_jid( ic, ini_jid, GET_BUDDY_EXACT ) ) ) +			{ +				bud->last_msg = time( NULL ); +				ext_jid = bud->ext_jid ? : bud->bare_jid; +			} +			else +				*s = 0; /* We need to generate a bare JID now. */ +		} + +		if( !( ft = imcb_file_send_start( ic, ext_jid, name, size ) ) ) +		{  +			imcb_log( ic, "WARNING: Error handling transfer request from %s", ini_jid); +			requestok = FALSE; +		} + +		*s = '/'; +	} +	else +	{  +		reply = jabber_make_error_packet( node, "item-not-found", "cancel", NULL ); +		if (!jabber_write_packet( ic, reply )) +			imcb_log( ic, "WARNING: Error generating reply to file transfer request" ); +		xt_free_node( reply ); +		return XT_HANDLED; +	} + +	/* Request is fine. */ + +	tf = g_new0( struct jabber_transfer, 1 ); + +	tf->ini_jid = g_strdup( ini_jid ); +	tf->tgt_jid = g_strdup( tgt_jid ); +	tf->iq_id = g_strdup( iq_id ); +	tf->sid = g_strdup( sid ); +	tf->ic = ic; +	tf->ft = ft; +	tf->fd = -1; +	tf->ft->data = tf; +	tf->ft->accept = jabber_si_answer_request; +	tf->ft->free = jabber_si_free_transfer; +	tf->ft->canceled = jabber_si_canceled; + +	jd->filetransfers = g_slist_prepend( jd->filetransfers, tf ); + +	return XT_HANDLED; +} + +/* + * imc called the accept callback which probably means that the user accepted this file transfer. + * We send our response to the initiator. + * In the next step, the initiator will send us a request for the given stream type. + * (currently that can only be a SOCKS5 bytestream) + */ +void jabber_si_answer_request( file_transfer_t *ft ) { +	struct jabber_transfer *tf = ft->data; +	struct xt_node *node, *sinode, *reply; + +	/* generate response, start with the SI tag */ +	sinode = xt_new_node( "si", NULL, NULL ); +	xt_add_attr( sinode, "xmlns", XMLNS_SI ); +	xt_add_attr( sinode, "profile", XMLNS_FILETRANSFER ); +	xt_add_attr( sinode, "id", tf->sid ); + +	/* now the file tag */ +	node = xt_new_node( "file", NULL, NULL ); +	xt_add_attr( node, "xmlns", XMLNS_FILETRANSFER ); + +	xt_add_child( sinode, node ); + +	/* and finally the feature tag */ +	node = xt_new_node( "field", NULL, NULL ); +	xt_add_attr( node, "var", "stream-method" ); +	xt_add_attr( node, "type", "list-single" ); + +	/* Currently all we can do. One could also implement in-band (IBB) */ +	xt_add_child( node, xt_new_node( "value", XMLNS_BYTESTREAMS, NULL ) ); + +	node = xt_new_node( "x", NULL, node ); +	xt_add_attr( node, "xmlns", XMLNS_XDATA ); +	xt_add_attr( node, "type", "submit" ); + +	node = xt_new_node( "feature", NULL, node ); +	xt_add_attr( node, "xmlns", XMLNS_FEATURE ); + +	xt_add_child( sinode, node ); + +	reply = jabber_make_packet( "iq", "result", tf->ini_jid, sinode ); +	xt_add_attr( reply, "id", tf->iq_id ); +	 +	if( !jabber_write_packet( tf->ic, reply ) ) +		imcb_log( tf->ic, "WARNING: Error generating reply to file transfer request" ); +	else +		tf->accepted = TRUE; +	xt_free_node( reply ); +} + +static xt_status jabber_si_handle_response(struct im_connection *ic, struct xt_node *node, struct xt_node *orig ) +{ +	struct xt_node *c, *d; +	char *ini_jid, *tgt_jid, *iq_id, *cmp; +	GSList *tflist; +	struct jabber_transfer *tf=NULL; +	struct jabber_data *jd = ic->proto_data; + +	if( !( tgt_jid = xt_find_attr( node, "from" ) ) || +	    !( ini_jid = xt_find_attr( node, "to" ) ) ) +	{ +		imcb_log( ic, "Invalid SI response from=%s to=%s", tgt_jid, ini_jid ); +		return XT_HANDLED; +	} +	 +	/* All this means we expect something like this: ( I think ) +	 * <iq from=... to=... id=...> +	 * 	<si xmlns=si> +	 * 	[	<file xmlns=ft/>    ] <-- not neccessary +	 * 		<feature xmlns=feature> +	 * 			<x xmlns=xdata type=submit> +	 * 				<field var=stream-method> +	 * 					<value> +	 */ +	if( !( tgt_jid = xt_find_attr( node, "from" ) ) || +	    !( ini_jid = xt_find_attr( node, "to" ) ) || +	    !( iq_id   = xt_find_attr( node, "id" ) ) || +	    !( c = xt_find_node( node->children, "si" ) ) || +	    !( cmp = xt_find_attr( c, "xmlns" ) ) || +	    !( strcmp( cmp, XMLNS_SI ) == 0 ) || +	    !( d = xt_find_node( c->children, "feature" ) ) || +	    !( cmp = xt_find_attr( d, "xmlns" ) ) || +	    !( strcmp( cmp, XMLNS_FEATURE ) == 0 ) || +	    !( d = xt_find_node( d->children, "x" ) ) || +	    !( cmp = xt_find_attr( d, "xmlns" ) ) || +	    !( strcmp( cmp, XMLNS_XDATA ) == 0 ) || +	    !( cmp = xt_find_attr( d, "type" ) ) || +	    !( strcmp( cmp, "submit" ) == 0 ) || +	    !( d = xt_find_node( d->children, "field" ) ) || +	    !( cmp = xt_find_attr( d, "var" ) ) || +	    !( strcmp( cmp, "stream-method" ) == 0 ) || +	    !( d = xt_find_node( d->children, "value" ) ) ) +	{ +		imcb_log( ic, "WARNING: Received incomplete Stream Initiation response" ); +		return XT_HANDLED; +	} + +	if( !( strcmp( d->text, XMLNS_BYTESTREAMS ) == 0 ) ) {  +		/* since we should only have advertised what we can do and the peer should +		 * only have chosen what we offered, this should never happen */ +		imcb_log( ic, "WARNING: Received invalid Stream Initiation response, method %s", d->text ); +			 +		return XT_HANDLED; +	} +	 +	/* Let's see if we can find out what this bytestream should be for... */ + +	for( tflist = jd->filetransfers ; tflist; tflist = g_slist_next(tflist) ) +	{ +		struct jabber_transfer *tft = tflist->data; +		if( ( strcmp( tft->iq_id, iq_id ) == 0 ) ) +		{ +		    	tf = tft; +			break; +		} +	} + +	if (!tf)  +	{ +		imcb_log( ic, "WARNING: Received bytestream request from %s that doesn't match an SI request", ini_jid ); +		return XT_HANDLED; +	} + +	tf->ini_jid = g_strdup( ini_jid ); +	tf->tgt_jid = g_strdup( tgt_jid ); + +	imcb_log( ic, "File %s: %s accepted the transfer!", tf->ft->file_name, tgt_jid ); + +	jabber_bs_send_start( tf ); + +	return XT_HANDLED; +} + +int jabber_si_send_request(struct im_connection *ic, char *who, struct jabber_transfer *tf ) +{ +	struct xt_node *node, *sinode; +	struct jabber_buddy *bud; + +	/* who knows how many bits the future holds :) */ +	char filesizestr[ 1 + ( int ) ( 0.301029995663981198f * sizeof( size_t ) * 8 ) ]; + +	const char *methods[] =  +	{  	 +		XMLNS_BYTESTREAMS, +		//XMLNS_IBB, +		NULL  +	}; +	const char **m; +	char *s; + +	/* Maybe we should hash this? */ +	tf->sid = g_strdup_printf( "BitlBeeJabberSID%d", tf->ft->local_id ); +	 +	if( ( s = strchr( who, '=' ) ) && jabber_chat_by_jid( ic, s + 1 ) ) +		bud = jabber_buddy_by_ext_jid( ic, who, 0 ); +	else +		bud = jabber_buddy_by_jid( ic, who, 0 ); + +	/* start with the SI tag */ +	sinode = xt_new_node( "si", NULL, NULL ); +	xt_add_attr( sinode, "xmlns", XMLNS_SI ); +	xt_add_attr( sinode, "profile", XMLNS_FILETRANSFER ); +	xt_add_attr( sinode, "id", tf->sid ); + +/*	if( mimetype )  +		xt_add_attr( node, "mime-type", mimetype ); */ + +	/* now the file tag */ +/*	if( desc ) + 		node = xt_new_node( "desc", descr, NULL ); */ +	node = xt_new_node( "range", NULL, NULL ); + +	sprintf( filesizestr, "%zd", tf->ft->file_size ); +	node = xt_new_node( "file", NULL, node ); +	xt_add_attr( node, "xmlns", XMLNS_FILETRANSFER ); +	xt_add_attr( node, "name", tf->ft->file_name ); +	xt_add_attr( node, "size", filesizestr ); +/*	if (hash) +		xt_add_attr( node, "hash", hash ); +	if (date) +		xt_add_attr( node, "date", date ); */ + +	xt_add_child( sinode, node ); + +	/* and finally the feature tag */ +	node = xt_new_node( "field", NULL, NULL ); +	xt_add_attr( node, "var", "stream-method" ); +	xt_add_attr( node, "type", "list-single" ); + +	for ( m = methods ; *m ; m ++ ) +		xt_add_child( node, xt_new_node( "option", NULL, xt_new_node( "value", (char *)*m, NULL ) ) ); + +	node = xt_new_node( "x", NULL, node ); +	xt_add_attr( node, "xmlns", XMLNS_XDATA ); +	xt_add_attr( node, "type", "form" ); + +	node = xt_new_node( "feature", NULL, node ); +	xt_add_attr( node, "xmlns", XMLNS_FEATURE ); + +	xt_add_child( sinode, node ); + +	/* and we are there... */ +	node = jabber_make_packet( "iq", "set", bud ? bud->full_jid : who, sinode ); +	jabber_cache_add( ic, node, jabber_si_handle_response ); +	tf->iq_id = g_strdup( xt_find_attr( node, "id" ) ); +	 +	return jabber_write_packet( ic, node ); +} diff --git a/protocols/msn/Makefile b/protocols/msn/Makefile index 6a588613..6c59aedb 100644 --- a/protocols/msn/Makefile +++ b/protocols/msn/Makefile @@ -7,6 +7,9 @@  ### DEFINITIONS  -include ../../Makefile.settings +ifdef SRCDIR +SRCDIR := $(SRCDIR)protocols/msn/ +endif  # [SH] Program variables  objects = msn.o msn_util.o ns.o passport.o sb.o tables.o @@ -32,7 +35,7 @@ distclean: clean  $(objects): ../../Makefile.settings Makefile -$(objects): %.o: %.c +$(objects): %.o: $(SRCDIR)%.c  	@echo '*' Compiling $<  	@$(CC) -c $(CFLAGS) $< -o $@ diff --git a/protocols/msn/invitation.c b/protocols/msn/invitation.c new file mode 100644 index 00000000..9f8b9a6e --- /dev/null +++ b/protocols/msn/invitation.c @@ -0,0 +1,622 @@ +/********************************************************************\ +* BitlBee -- An IRC to other IM-networks gateway                     * +*                                                                    * +* Copyright 2008 Uli Meis					     * +* Copyright 2006 Marijn Kruisselbrink and others                     * +\********************************************************************/ + +/* MSN module - File transfer support             */ + +/* + 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 with + the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; + if not, write to the Free Software Foundation, Inc., 59 Temple Place, + Suite 330, Boston, MA  02111-1307  USA + */ + +#include "bitlbee.h" +#include "invitation.h" +#include "msn.h" +#include "lib/ftutil.h" + +#ifdef debug +#undef debug +#endif +#define debug(msg...) log_message( LOGLVL_INFO, msg ) + +static void msn_ftp_free( file_transfer_t *file ); +static void msn_ftpr_accept( file_transfer_t *file ); +static void msn_ftp_finished( file_transfer_t *file ); +static void msn_ftp_canceled( file_transfer_t *file, char *reason ); +static gboolean msn_ftpr_write_request( file_transfer_t *file ); + +static gboolean msn_ftp_connected( gpointer data, gint fd, b_input_condition cond ); +static gboolean msn_ftp_read( gpointer data, gint fd, b_input_condition cond ); +gboolean msn_ftps_write( file_transfer_t *file, char *buffer, unsigned int len ); + +/* + * Vararg wrapper for imcb_file_canceled(). + */ +gboolean msn_ftp_abort( file_transfer_t *file, char *format, ... ) +{ +        va_list params; +        va_start( params, format ); +        char error[128]; + +        if( vsnprintf( error, 128, format, params ) < 0 ) +                sprintf( error, "internal error parsing error string (BUG)" ); +        va_end( params ); +	imcb_file_canceled( file, error ); +	return FALSE; +} + +/* very useful */ +#define ASSERTSOCKOP(op, msg) \ +	if( (op) == -1 ) \ +		return msn_ftp_abort( file , msg ": %s", strerror( errno ) ); + +void msn_ftp_invitation_cmd( struct im_connection *ic, char *who, int cookie, char *icmd, +			     char *trailer ) +{ +	struct msn_message *m = g_new0( struct msn_message, 1 ); +	 +	m->text = g_strdup_printf( "%s" +		    "Invitation-Command: %s\r\n" +		    "Invitation-Cookie: %u\r\n" +		    "%s", +		    MSN_INVITE_HEADERS, +		    icmd, +		    cookie, +		    trailer); +	 +	m->who = g_strdup( who ); + +	msn_sb_write_msg( ic, m ); +} + +void msn_ftp_cancel_invite( struct im_connection *ic, char *who,  int cookie, char *code ) +{ +	char buf[64]; + +	g_snprintf( buf, sizeof( buf ), "Cancel-Code: %s\r\n", code ); +	msn_ftp_invitation_cmd( ic, who, cookie, "CANCEL", buf ); +} + +void msn_ftp_transfer_request( struct im_connection *ic, file_transfer_t *file, char *who ) +{ +	unsigned int cookie = time( NULL ); /* TODO: randomize */ +	char buf[2048]; + +	msn_filetransfer_t *msn_file = g_new0( msn_filetransfer_t, 1 ); +	file->data = msn_file; +	file->free = msn_ftp_free; +	file->canceled = msn_ftp_canceled; +	file->write = msn_ftps_write; +	msn_file->md = ic->proto_data; +	msn_file->invite_cookie = cookie; +	msn_file->handle = g_strdup( who ); +	msn_file->dcc = file; +	msn_file->md->filetransfers = g_slist_prepend( msn_file->md->filetransfers, msn_file->dcc ); +	msn_file->fd = -1; +	msn_file->sbufpos = 3; + +	g_snprintf( buf, sizeof( buf ),  +		"Application-Name: File Transfer\r\n" +		"Application-GUID: {5D3E02AB-6190-11d3-BBBB-00C04F795683}\r\n" +		"Application-File: %s\r\n" +		"Application-FileSize: %zd\r\n", +		file->file_name, +		file->file_size); + +	msn_ftp_invitation_cmd( msn_file->md->ic, msn_file->handle, cookie, "INVITE", buf ); + +	imcb_file_recv_start( file ); +} + +void msn_invitation_invite( struct msn_switchboard *sb, char *handle, unsigned int cookie, char *body, int blen ) +{ +	char *itype = msn_findheader( body, "Application-GUID:", blen ); +	char *name, *size, *invitecookie, *reject = NULL; +	user_t *u; +	size_t isize; +	file_transfer_t *file; +	 +	if( !itype || strcmp( itype, "{5D3E02AB-6190-11d3-BBBB-00C04F795683}" ) != 0 ) { +		/* Don't know what that is - don't care */ +		char *iname = msn_findheader( body, "Application-Name:", blen ); +		imcb_log( sb->ic, "Received unknown MSN invitation %s (%s) from %s", +			  itype ? : "with no GUID", iname ? iname : "no application name", handle ); +		g_free( iname ); +		reject = "REJECT_NOT_INSTALLED"; +	} else if (  +		!( name = msn_findheader( body, "Application-File:", blen )) ||  +		!( size = msn_findheader( body, "Application-FileSize:", blen )) ||  +		!( invitecookie = msn_findheader( body, "Invitation-Cookie:", blen)) || +		!( isize = atoll( size ) ) ) {  +		imcb_log( sb->ic, "Received corrupted transfer request from %s" +			  "(name=%s, size=%s, invitecookie=%s)", +			  handle, name, size, invitecookie ); +		reject = "REJECT"; +	} else if ( !( u = user_findhandle( sb->ic, handle ) ) ) { +		imcb_log( sb->ic, "Error in parsing transfer request, User '%s'" +			  "is not in contact list", handle ); +		reject = "REJECT"; +	} else if ( !( file = imcb_file_send_start( sb->ic, handle, name, isize ) ) ) { +		imcb_log( sb->ic, "Error initiating transfer for request from %s for %s", +			  handle, name ); +		reject = "REJECT"; +	} else { +		msn_filetransfer_t *msn_file = g_new0( msn_filetransfer_t, 1 ); +		file->data = msn_file; +		file->accept = msn_ftpr_accept; +		file->free = msn_ftp_free; +		file->finished = msn_ftp_finished; +		file->canceled = msn_ftp_canceled; +		file->write_request = msn_ftpr_write_request; +		msn_file->md = sb->ic->proto_data; +		msn_file->invite_cookie = cookie; +		msn_file->handle = g_strdup( handle ); +		msn_file->dcc = file; +		msn_file->md->filetransfers = g_slist_prepend( msn_file->md->filetransfers, msn_file->dcc ); +		msn_file->fd = -1; +	} + +	if( reject ) +		msn_ftp_cancel_invite( sb->ic, sb->who, cookie, reject ); + +	g_free( name ); +	g_free( size ); +	g_free( invitecookie ); +	g_free( itype ); +} + +msn_filetransfer_t* msn_find_filetransfer( struct msn_data *md, unsigned int cookie, char *handle ) +{ +	GSList *l; +	 +	for( l = md->filetransfers; l; l = l->next ) { +		msn_filetransfer_t *file = ( (file_transfer_t*) l->data )->data; +		if( file->invite_cookie == cookie && strcmp( handle, file->handle ) == 0 ) { +			return file; +		} +	} +	return NULL; +} + +gboolean msn_ftps_connected( gpointer data, gint fd, b_input_condition cond ) +{ +	file_transfer_t *file = data; +	msn_filetransfer_t *msn_file = file->data; +	struct sockaddr_storage clt_addr; +	socklen_t ssize = sizeof( clt_addr ); +	 +	debug( "Connected to MSNFTP client" ); +	 +	ASSERTSOCKOP( msn_file->fd = accept( fd, (struct sockaddr *) &clt_addr, &ssize ), "Accepting connection" ); + +	closesocket( fd ); +	fd = msn_file->fd; +	sock_make_nonblocking( fd ); + +	msn_file->r_event_id = b_input_add( fd, B_EV_IO_READ, msn_ftp_read, file ); + +	return FALSE; +} + +void msn_invitations_accept( msn_filetransfer_t *msn_file, struct msn_switchboard *sb, char *handle, unsigned int cookie, char *body, int blen ) +{ +	file_transfer_t *file = msn_file->dcc; +	char buf[1024]; +	unsigned int acookie = time ( NULL ); +	char host[HOST_NAME_MAX+1]; +	char port[6]; +	char *errmsg; + +	msn_file->auth_cookie = acookie; + +	if( ( msn_file->fd = ft_listen( NULL, host, port, FALSE, &errmsg ) ) == -1 ) { +		msn_ftp_abort( file, "Failed to listen locally, check your ft_listen setting in bitlbee.conf: %s", errmsg ); +		return; +	} + +	msn_file->r_event_id = b_input_add( msn_file->fd, B_EV_IO_READ, msn_ftps_connected, file ); + +	g_snprintf( buf, sizeof( buf ), +		    "IP-Address: %s\r\n" +		    "Port: %s\r\n" +		    "AuthCookie: %d\r\n" +		    "Launch-Application: FALSE\r\n" +		    "Request-Data: IP-Address:\r\n\r\n", +		    host, +		    port, +		    msn_file->auth_cookie ); + +	msn_ftp_invitation_cmd( msn_file->md->ic, handle, msn_file->invite_cookie, "ACCEPT", buf ); +} + +void msn_invitationr_accept( msn_filetransfer_t *msn_file, struct msn_switchboard *sb, char *handle, unsigned int cookie, char *body, int blen ) { +	file_transfer_t *file = msn_file->dcc; +	char *authcookie, *ip, *port; + +	if( !( authcookie = msn_findheader( body, "AuthCookie:", blen ) ) || +	    !( ip = msn_findheader( body, "IP-Address:", blen ) ) || +	    !( port = msn_findheader( body, "Port:", blen ) ) ) { +		msn_ftp_abort( file, "Received invalid accept reply" ); +	} else if(  +		( msn_file->fd = proxy_connect( ip, atoi( port ), msn_ftp_connected, file ) ) +		< 0 ) { +			msn_ftp_abort( file, "Error connecting to MSN client" ); +	} else +		msn_file->auth_cookie = strtoul( authcookie, NULL, 10 ); + +	g_free( authcookie ); +	g_free( ip ); +	g_free( port ); +} + +void msn_invitation_accept( struct msn_switchboard *sb, char *handle, unsigned int cookie, char *body, int blen ) +{ +	msn_filetransfer_t *msn_file = msn_find_filetransfer( sb->ic->proto_data, cookie, handle ); +	file_transfer_t *file = msn_file ? msn_file->dcc : NULL; +	 +	if( !msn_file  ) +		imcb_log( sb->ic, "Received invitation ACCEPT message for unknown invitation (already aborted?)" ); +	else if( file->sending )  +		msn_invitations_accept( msn_file, sb, handle, cookie, body, blen ); +	else +		msn_invitationr_accept( msn_file, sb, handle, cookie, body, blen ); +} + +void msn_invitation_cancel( struct msn_switchboard *sb, char *handle, unsigned int cookie, char *body, int blen ) +{ +	msn_filetransfer_t *msn_file = msn_find_filetransfer( sb->ic->proto_data, cookie, handle ); +	 +	if( !msn_file ) +		imcb_log( sb->ic, "Received invitation CANCEL message for unknown invitation (already aborted?)" ); +	else +		msn_ftp_abort( msn_file->dcc, msn_findheader( body, "Cancel-Code:", blen ) ); +} + +int msn_ftp_write( file_transfer_t *file, char *format, ... ) +{ +	msn_filetransfer_t *msn_file = file->data; +	va_list params; +	int st; +	char *s; +	 +	va_start( params, format ); +	s = g_strdup_vprintf( format, params ); +	va_end( params ); +	 +	st = write( msn_file->fd, s, strlen( s ) ); +	if( st != strlen( s ) ) +		return msn_ftp_abort( file, "Error sending data over MSNFTP connection: %s", +				strerror( errno ) ); +	 +	g_free( s ); +	return 1; +} + +gboolean msn_ftp_connected( gpointer data, gint fd, b_input_condition cond ) +{ +	file_transfer_t *file = data; +	msn_filetransfer_t *msn_file = file->data; +	 +	debug( "Connected to MSNFTP server, starting authentication" ); +	if( !msn_ftp_write( file, "VER MSNFTP\r\n" ) )  +		return FALSE; +	 +	sock_make_nonblocking( msn_file->fd ); +	msn_file->r_event_id = b_input_add( msn_file->fd, B_EV_IO_READ, msn_ftp_read, file ); +	 +	return FALSE; +} + +gboolean msn_ftp_handle_command( file_transfer_t *file, char* line ) +{ +	msn_filetransfer_t *msn_file = file->data; +	char **cmd = msn_linesplit( line ); +	int count = 0; +	if( cmd[0] ) while( cmd[++count] ); +	 +	if( count < 1 ) +		return msn_ftp_abort( file, "Missing command in MSNFTP communication" ); +	 +	if( strcmp( cmd[0], "VER" ) == 0 ) { +		if( strcmp( cmd[1], "MSNFTP" ) != 0 ) +			return msn_ftp_abort( file, "Unsupported filetransfer protocol: %s", cmd[1] ); +		if( file->sending ) +			msn_ftp_write( file, "VER MSNFTP\r\n" ); +		else  +			msn_ftp_write( file, "USR %s %u\r\n", msn_file->md->ic->acc->user, msn_file->auth_cookie ); +	} else if( strcmp( cmd[0], "FIL" ) == 0 ) { +		if( strtoul( cmd[1], NULL, 10 ) != file->file_size ) +			return msn_ftp_abort( file, "FIL reply contains a different file size than the size in the invitation" ); +		msn_ftp_write( file, "TFR\r\n" ); +		msn_file->status |= MSN_TRANSFER_RECEIVING; +	} else if( strcmp( cmd[0], "USR" ) == 0 ) { +		if( ( strcmp( cmd[1], msn_file->handle ) != 0 ) || +		    ( strtoul( cmd[2], NULL, 10 ) != msn_file->auth_cookie ) ) +			msn_ftp_abort( file, "Authentication failed. " +				"Expected handle: %s (got %s), cookie: %u (got %s)", +				msn_file->handle, cmd[1], +				msn_file->auth_cookie, cmd[2] ); +		msn_ftp_write( file, "FIL %zu\r\n", file->file_size); +	} else if( strcmp( cmd[0], "TFR" ) == 0 ) { +		file->write_request( file ); +	} else if( strcmp( cmd[0], "BYE" ) == 0 ) { +		unsigned int retcode = count > 1 ? atoi(cmd[1]) : 1; + +		if( ( retcode==16777989 ) || ( retcode==16777987 ) ) +			imcb_file_finished( file ); +		else if( retcode==2147942405 ) +			imcb_file_canceled( file, "Failure: receiver is out of disk space" ); +		else if( retcode==2164261682 ) +			imcb_file_canceled( file, "Failure: receiver cancelled the transfer" ); +		else if( retcode==2164261683 ) +			imcb_file_canceled( file, "Failure: sender has cancelled the transfer" ); +		else if( retcode==2164261694 ) +			imcb_file_canceled( file, "Failure: connection is blocked" ); +		else { +			char buf[128]; + +			sprintf( buf, "Failure: unknown BYE code: %d", retcode); +			imcb_file_canceled( file, buf ); +		} +	} else if( strcmp( cmd[0], "CCL" ) == 0 ) { +		imcb_file_canceled( file, "Failure: receiver cancelled the transfer" ); +	} else { +		msn_ftp_abort( file, "Received invalid command %s from msn client", cmd[0] ); +	} +	return TRUE; +} + +gboolean msn_ftp_send( gpointer data, gint fd, b_input_condition cond ) +{ +	file_transfer_t *file = data; +	msn_filetransfer_t *msn_file = file->data; + +	msn_file->w_event_id = 0; + +	file->write_request( file ); + +	return FALSE; +} + +/* + * This should only be called if we can write, so just do it. + * Add a write watch so we can write more during the next cycle (if possible). + * This got a bit complicated because (at least) amsn expects packets of size 2045. + */ +gboolean msn_ftps_write( file_transfer_t *file, char *buffer, unsigned int len ) +{ +	msn_filetransfer_t *msn_file = file->data; +        int ret, overflow; + +	/* what we can't send now */ +	overflow = msn_file->sbufpos + len - MSNFTP_PSIZE; + +	/* append what we can do the send buffer */ +	memcpy( msn_file->sbuf + msn_file->sbufpos, buffer, MIN( len, MSNFTP_PSIZE - msn_file->sbufpos ) ); +	msn_file->sbufpos += MIN( len, MSNFTP_PSIZE - msn_file->sbufpos ); + +	/* if we don't have enough for a full packet and there's more wait for it */ +	if( ( msn_file->sbufpos < MSNFTP_PSIZE ) &&  +	    ( msn_file->data_sent + msn_file->sbufpos - 3 < file->file_size ) ) { +		if( !msn_file->w_event_id ) +			msn_file->w_event_id = b_input_add( msn_file->fd, B_EV_IO_WRITE, msn_ftp_send, file ); +		return TRUE; +	} + +	/* Accumulated enough data, lets send something out */ + +	msn_file->sbuf[0] = 0; +	msn_file->sbuf[1] = ( msn_file->sbufpos - 3 ) & 0xff; +	msn_file->sbuf[2] = ( ( msn_file->sbufpos - 3 ) >> 8 ) & 0xff; + +        ASSERTSOCKOP( ret = send( msn_file->fd, msn_file->sbuf, msn_file->sbufpos, 0 ), "Sending" ); + +        msn_file->data_sent += ret - 3; + +        /* TODO: this should really not be fatal */ +        if( ret < msn_file->sbufpos ) +                return msn_ftp_abort( file, "send() sent %d instead of %d (send buffer full!)", ret, msn_file->sbufpos ); + +	msn_file->sbufpos = 3; + +	if( overflow > 0 ) { +		while( overflow > ( MSNFTP_PSIZE - 3 ) ) { +			if( !msn_ftps_write( file, buffer + len - overflow, MSNFTP_PSIZE - 3 ) ) +				return FALSE; +			overflow -= MSNFTP_PSIZE - 3; +		} +		return msn_ftps_write( file, buffer + len - overflow, overflow ); +	} + +	if( msn_file->data_sent == file->file_size ) { +		if( msn_file->w_event_id ) { +			b_event_remove( msn_file->w_event_id ); +			msn_file->w_event_id = 0; +		} +	} else { +		/* we might already be listening if this is data from an overflow */ +		if( !msn_file->w_event_id ) +			msn_file->w_event_id = b_input_add( msn_file->fd, B_EV_IO_WRITE, msn_ftp_send, file ); +	} + +        return TRUE; +} + +/* Binary part of the file transfer protocol */ +gboolean msn_ftpr_read( file_transfer_t *file )  +{ +	msn_filetransfer_t *msn_file = file->data; +	int st; +	unsigned char buf[3]; + +	if( msn_file->data_remaining ) { +		msn_file->r_event_id = 0; + +		ASSERTSOCKOP( st = read( msn_file->fd, file->buffer, MIN( sizeof( file->buffer ), msn_file->data_remaining ) ), "Receiving" ); + +		if( st == 0 ) +			return msn_ftp_abort( file, "Remote end closed connection"); + +		msn_file->data_sent += st; + +		msn_file->data_remaining -= st; + +		file->write( file, file->buffer, st ); + +		if( msn_file->data_sent >= file->file_size ) +			imcb_file_finished( file ); + +		return FALSE; +	} else { +		ASSERTSOCKOP( st = read( msn_file->fd, buf, 1 ), "Receiving" ); +		if( st == 0 ) { +			return msn_ftp_abort( file, "read returned EOF while reading data header from msn client" ); +		} else if( buf[0] == '\r' || buf[0] == '\n' ) { +			debug( "Discarding extraneous newline" ); +		} else if( buf[0] != 0 ) { +			msn_ftp_abort( file, "Remote end canceled the transfer"); +			/* don't really care about these last 2 (should be 0,0) */ +			read( msn_file->fd, buf, 2 ); +			return FALSE; +		} else { +			unsigned int size; +			ASSERTSOCKOP( st = read( msn_file->fd, buf, 2 ), "Receiving" ); +			if( st < 2 ) +				return msn_ftp_abort( file, "read returned EOF while reading data header from msn client" ); + +			size = buf[0] + ((unsigned int) buf[1] << 8); +			msn_file->data_remaining = size; +		} +	} +	return TRUE; +} + +/* Text mode part of the file transfer protocol */ +gboolean msn_ftp_txtproto( file_transfer_t *file ) +{ +	msn_filetransfer_t *msn_file = file->data; +	int i = msn_file->tbufpos, st; +	char *tbuf = msn_file->tbuf; + +	ASSERTSOCKOP( st = read( msn_file->fd,  +				 tbuf + msn_file->tbufpos,  +				 sizeof( msn_file->tbuf ) - msn_file->tbufpos ), +				 "Receiving" ); + +	if( st == 0 ) +		return msn_ftp_abort( file, "read returned EOF while reading text from msn client" ); + +	msn_file->tbufpos += st; + +	do { +		for( ;i < msn_file->tbufpos; i++ ) { +			if( tbuf[i] == '\n' || tbuf[i] == '\r' ) { +				tbuf[i] = '\0'; +				if( i > 0 ) +					msn_ftp_handle_command( file, tbuf ); +				else +					while( tbuf[i] == '\n' || tbuf[i] == '\r' ) i++; +				memmove( tbuf, tbuf + i + 1, msn_file->tbufpos - i - 1 ); +				msn_file->tbufpos -= i + 1; +				i = 0; +				break; +			} +		} +	} while ( i < msn_file->tbufpos ); + +	if( msn_file->tbufpos == sizeof( msn_file->tbuf ) ) +		return msn_ftp_abort( file,  +				      "Line exceeded %d bytes in text protocol",  +				      sizeof( msn_file->tbuf ) ); +	return TRUE; +} + +gboolean msn_ftp_read( gpointer data, gint fd, b_input_condition cond ) +{ +	file_transfer_t *file = data; +	msn_filetransfer_t *msn_file = file->data; +	 +	if( msn_file->status & MSN_TRANSFER_RECEIVING ) +		return msn_ftpr_read( file ); +	else +		return msn_ftp_txtproto( file ); +} + +void msn_ftp_free( file_transfer_t *file ) +{ +	msn_filetransfer_t *msn_file = file->data; +	 +	if( msn_file->r_event_id ) +		b_event_remove( msn_file->r_event_id ); + +	if( msn_file->w_event_id ) +		b_event_remove( msn_file->w_event_id ); + +	if( msn_file->fd != -1 ) +		closesocket( msn_file->fd ); + +	msn_file->md->filetransfers = g_slist_remove( msn_file->md->filetransfers, msn_file->dcc ); +	 +	g_free( msn_file->handle ); +	 +	g_free( msn_file ); +} + +void msn_ftpr_accept( file_transfer_t *file ) +{ +	msn_filetransfer_t *msn_file = file->data; + +	msn_ftp_invitation_cmd( msn_file->md->ic, msn_file->handle, msn_file->invite_cookie, "ACCEPT",  +				"Launch-Application: FALSE\r\n"  +				"Request-Data: IP-Address:\r\n"); +} + +void msn_ftp_finished( file_transfer_t *file ) +{ +	msn_ftp_write( file, "BYE 16777989\r\n" ); +} + +void msn_ftp_canceled( file_transfer_t *file, char *reason ) +{ +	msn_filetransfer_t *msn_file = file->data; + +	msn_ftp_cancel_invite( msn_file->md->ic, msn_file->handle,  +			       msn_file->invite_cookie,  +			       file->status & FT_STATUS_TRANSFERRING ?  +					"FTTIMEOUT" :  +					"FAIL" ); + +	imcb_log( msn_file->md->ic, "File transfer aborted: %s", reason ); +} + +gboolean msn_ftpr_write_request( file_transfer_t *file ) +{ +	msn_filetransfer_t *msn_file = file->data; +	if( msn_file->r_event_id != 0 ) { +		msn_ftp_abort( file,  +					"BUG in MSN file transfer:" +					"write_request called when" +					"already watching for input" ); +		return FALSE; +	} + +	msn_file->r_event_id =  +		b_input_add( msn_file->fd, B_EV_IO_READ, msn_ftp_read, file ); + +	return TRUE; +} diff --git a/protocols/msn/invitation.h b/protocols/msn/invitation.h new file mode 100644 index 00000000..289efd7b --- /dev/null +++ b/protocols/msn/invitation.h @@ -0,0 +1,82 @@ +/********************************************************************\ +* BitlBee -- An IRC to other IM-networks gateway                     * +*                                                                    * +* Copyright 2006 Marijn Kruisselbrink and others                     * +\********************************************************************/ + +/* MSN module - File transfer support             */ + +/* + 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 with + the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; + if not, write to the Free Software Foundation, Inc., 59 Temple Place, + Suite 330, Boston, MA  02111-1307  USA + */ + +#ifndef _MSN_INVITATION_H +#define _MSN_INVITATION_H + +#include "msn.h" + +#define MSN_INVITE_HEADERS	"MIME-Version: 1.0\r\n" \ +				"Content-Type: text/x-msmsgsinvite; charset=UTF-8\r\n" \ +				"\r\n" + +#define MSNFTP_PSIZE 2048 + +typedef enum { +	MSN_TRANSFER_RECEIVING	= 1, +	MSN_TRANSFER_SENDING	= 2 +} msn_filetransfer_status_t; + +typedef struct msn_filetransfer +{ +/* Generic invitation data */	 +	/* msn_data instance this invitation was received with. */ +	struct msn_data *md; +	/* Cookie specifying this invitation. */ +	unsigned int invite_cookie; +	/* Handle of user that started this invitation. */ +	char *handle; + +/* File transfer specific data */ +	/* Current status of the file transfer. */ +	msn_filetransfer_status_t status; +	/* Pointer to the dcc structure for this transfer. */ +	file_transfer_t *dcc; +	/* Socket the transfer is taking place over. */ +	int fd; +	/* Cookie received in the original invitation, this must be sent as soon as +	   a connection has been established. */ +	unsigned int auth_cookie; +	/* Data remaining to be received in the current packet. */ +	unsigned int data_remaining; +	/* Buffer containing received, but unprocessed text. */ +	char tbuf[256]; +	unsigned int tbufpos; +	 +	unsigned int data_sent; + +	gint r_event_id; +	gint w_event_id; +	 +	unsigned char sbuf[2048]; +	int sbufpos; + +} msn_filetransfer_t; + +void msn_invitation_invite( struct msn_switchboard *sb, char *handle, unsigned int cookie, char *body, int blen ); +void msn_invitation_accept( struct msn_switchboard *sb, char *handle, unsigned int cookie, char *body, int blen ); +void msn_invitation_cancel( struct msn_switchboard *sb, char *handle, unsigned int cookie, char *body, int blen ); + +#endif diff --git a/protocols/msn/msn.c b/protocols/msn/msn.c index 7dbdb9d6..8b4f1a81 100644 --- a/protocols/msn/msn.c +++ b/protocols/msn/msn.c @@ -78,6 +78,12 @@ static void msn_logout( struct im_connection *ic )  	if( md )  	{ +		/** Disabling MSN ft support for now. +		while( md->filetransfers ) { +			imcb_file_canceled( md->filetransfers->data, "Closing connection" ); +		} +		*/ +		  		if( md->fd >= 0 )  			closesocket( md->fd ); @@ -97,6 +103,15 @@ static void msn_logout( struct im_connection *ic )  			g_free( md->grouplist[--md->groupcount] );  		g_free( md->grouplist ); +		while( md->grpq ) +		{ +			struct msn_groupadd *ga = md->grpq->data; +			g_free( ga->group ); +			g_free( ga->who ); +			g_free( ga ); +			md->grpq = g_slist_remove( md->grpq, ga ); +		} +		  		g_free( md );  	} @@ -115,6 +130,14 @@ static int msn_buddy_msg( struct im_connection *ic, char *who, char *message, in  {  	struct msn_switchboard *sb; +#ifdef DEBUG +	if( strcmp( who, "raw" ) == 0 ) +	{ +		msn_write( ic, message, strlen( message ) ); +		msn_write( ic, "\r\n", 2 ); +	} +	else +#endif  	if( ( sb = msn_sb_by_handle( ic, who ) ) )  	{  		return( msn_sb_sendmessage( sb, message ) ); @@ -175,7 +198,7 @@ static void msn_get_info(struct im_connection *ic, char *who)  static void msn_add_buddy( struct im_connection *ic, char *who, char *group )  { -	msn_buddy_list_add( ic, "FL", who, who ); +	msn_buddy_list_add( ic, "FL", who, who, group );  }  static void msn_remove_buddy( struct im_connection *ic, char *who, char *group ) @@ -216,6 +239,7 @@ static void msn_chat_leave( struct groupchat *c )  static struct groupchat *msn_chat_with( struct im_connection *ic, char *who )  {  	struct msn_switchboard *sb; +	struct groupchat *c = imcb_chat_new( ic, who );  	if( ( sb = msn_sb_by_handle( ic, who ) ) )  	{ @@ -233,10 +257,8 @@ static struct groupchat *msn_chat_with( struct im_connection *ic, char *who )  		msn_sb_write_msg( ic, m ); -		return NULL; +		return c;  	} -	 -	return NULL;  }  static void msn_keepalive( struct im_connection *ic ) @@ -246,7 +268,7 @@ static void msn_keepalive( struct im_connection *ic )  static void msn_add_permit( struct im_connection *ic, char *who )  { -	msn_buddy_list_add( ic, "AL", who, who ); +	msn_buddy_list_add( ic, "AL", who, who, NULL );  }  static void msn_rem_permit( struct im_connection *ic, char *who ) @@ -258,7 +280,7 @@ static void msn_add_deny( struct im_connection *ic, char *who )  {  	struct msn_switchboard *sb; -	msn_buddy_list_add( ic, "BL", who, who ); +	msn_buddy_list_add( ic, "BL", who, who, NULL );  	/* If there's still a conversation with this person, close it. */  	if( ( sb = msn_sb_by_handle( ic, who ) ) ) @@ -327,6 +349,7 @@ void msn_initmodule()  	ret->rem_deny = msn_rem_deny;  	ret->send_typing = msn_send_typing;  	ret->handle_cmp = g_strcasecmp; +	//ret->transfer_request = msn_ftp_transfer_request;  	register_protocol(ret);  } diff --git a/protocols/msn/msn.h b/protocols/msn/msn.h index 83a080a3..59036e37 100644 --- a/protocols/msn/msn.h +++ b/protocols/msn/msn.h @@ -69,10 +69,11 @@ struct msn_data  	int trId; -	GSList *msgq; +	GSList *msgq, *grpq;  	GSList *switchboards;  	int sb_failures;  	time_t first_sb_failure; +	GSList *filetransfers;  	const struct msn_away_state *away_state;  	int buddycount; @@ -119,6 +120,12 @@ struct msn_message  	char *text;  }; +struct msn_groupadd +{ +	char *who; +	char *group; +}; +  struct msn_handler_data  {  	int fd; @@ -158,7 +165,7 @@ gboolean msn_ns_connected( gpointer data, gint source, b_input_condition cond );  /* msn_util.c */  int msn_write( struct im_connection *ic, char *s, int len );  int msn_logged_in( struct im_connection *ic ); -int msn_buddy_list_add( struct im_connection *ic, char *list, char *who, char *realname ); +int msn_buddy_list_add( struct im_connection *ic, const char *list, const char *who, const char *realname_, const char *group );  int msn_buddy_list_remove( struct im_connection *ic, char *list, char *who );  void msn_buddy_ask( struct im_connection *ic, char *handle, char *realname );  char *msn_findheader( char *text, char *header, int len ); @@ -188,4 +195,7 @@ int msn_sb_write_msg( struct im_connection *ic, struct msn_message *m );  void msn_sb_start_keepalives( struct msn_switchboard *sb, gboolean initial );  void msn_sb_stop_keepalives( struct msn_switchboard *sb ); +/* invitation.c */ +void msn_ftp_transfer_request( struct im_connection *ic, file_transfer_t *ft, char *who ); +  #endif //_MSN_H diff --git a/protocols/msn/msn_util.c b/protocols/msn/msn_util.c index 9c9d2720..24f7e10e 100644 --- a/protocols/msn/msn_util.c +++ b/protocols/msn/msn_util.c @@ -50,24 +50,62 @@ int msn_logged_in( struct im_connection *ic )  	return( 0 );  } -int msn_buddy_list_add( struct im_connection *ic, char *list, char *who, char *realname_ ) +int msn_buddy_list_add( struct im_connection *ic, const char *list, const char *who, const char *realname_, const char *group )  {  	struct msn_data *md = ic->proto_data; -	char buf[1024], *realname; +	char buf[1024], *realname, groupid[8]; -	realname = msn_http_encode( realname_ ); -	 -	g_snprintf( buf, sizeof( buf ), "ADD %d %s %s %s\r\n", ++md->trId, list, who, realname ); -	if( msn_write( ic, buf, strlen( buf ) ) ) +	*groupid = '\0'; +	if( group )  	{ -		g_free( realname ); +		int i; +		for( i = 0; i < md->groupcount; i ++ ) +			if( g_strcasecmp( md->grouplist[i], group ) == 0 ) +			{ +				g_snprintf( groupid, sizeof( groupid ), " %d", i ); +				break; +			} -		return( 1 ); +		if( *groupid == '\0' ) +		{ +			/* Have to create this group, it doesn't exist yet. */ +			struct msn_groupadd *ga; +			GSList *l; +			 +			for( l = md->grpq; l; l = l->next ) +			{ +				ga = l->data; +				if( g_strcasecmp( ga->group, group ) == 0 ) +					break; +			} +			 +			ga = g_new0( struct msn_groupadd, 1 ); +			ga->who = g_strdup( who ); +			ga->group = g_strdup( group ); +			md->grpq = g_slist_prepend( md->grpq, ga ); +			 +			if( l == NULL ) +			{ +				char *groupname = msn_http_encode( group ); +				g_snprintf( buf, sizeof( buf ), "ADG %d %s %d\r\n", ++md->trId, groupname, 0 ); +				g_free( groupname ); +				return msn_write( ic, buf, strlen( buf ) ); +			} +			else +			{ +				/* This can happen if the user's doing lots of adds to a +				   new group at once; we're still waiting for the server +				   to confirm group creation. */ +				return 1; +			} +		}  	} +	realname = msn_http_encode( realname_ ); +	g_snprintf( buf, sizeof( buf ), "ADD %d %s %s %s%s\r\n", ++md->trId, list, who, realname, groupid );  	g_free( realname ); -	return( 0 ); +	return msn_write( ic, buf, strlen( buf ) );  }  int msn_buddy_list_remove( struct im_connection *ic, char *list, char *who ) @@ -93,10 +131,9 @@ static void msn_buddy_ask_yes( void *data )  {  	struct msn_buddy_ask_data *bla = data; -	msn_buddy_list_add( bla->ic, "AL", bla->handle, bla->realname ); +	msn_buddy_list_add( bla->ic, "AL", bla->handle, bla->realname, NULL ); -	if( imcb_find_buddy( bla->ic, bla->handle ) == NULL ) -		imcb_ask_add( bla->ic, bla->handle, NULL ); +	imcb_ask_add( bla->ic, bla->handle, NULL );  	g_free( bla->handle );  	g_free( bla->realname ); @@ -107,7 +144,7 @@ static void msn_buddy_ask_no( void *data )  {  	struct msn_buddy_ask_data *bla = data; -	msn_buddy_list_add( bla->ic, "BL", bla->handle, bla->realname ); +	msn_buddy_list_add( bla->ic, "BL", bla->handle, bla->realname, NULL );  	g_free( bla->handle );  	g_free( bla->realname ); diff --git a/protocols/msn/ns.c b/protocols/msn/ns.c index 2f656ea5..0be9e727 100644 --- a/protocols/msn/ns.c +++ b/protocols/msn/ns.c @@ -75,7 +75,7 @@ gboolean msn_ns_connected( gpointer data, gint source, b_input_condition cond )  	g_snprintf( s, sizeof( s ), "VER %d MSNP8 CVR0\r\n", ++md->trId );  	if( msn_write( ic, s, strlen( s ) ) )  	{ -		ic->inpa = b_input_add( md->fd, GAIM_INPUT_READ, msn_ns_callback, ic ); +		ic->inpa = b_input_add( md->fd, B_EV_IO_READ, msn_ns_callback, ic );  		imcb_log( ic, "Connected to server, waiting for reply" );  	} @@ -532,8 +532,14 @@ static int msn_ns_command( gpointer data, char **cmd, int num_parts )  		}  		else if( num_parts >= 6 && strcmp( cmd[2], "FL" ) == 0 )  		{ +			const char *group = NULL; +			int num; +			 +			if( cmd[6] != NULL && sscanf( cmd[6], "%d", &num ) == 1 && num < md->groupcount ) +				group = md->grouplist[num]; +			  			http_decode( cmd[5] ); -			imcb_add_buddy( ic, cmd[4], NULL ); +			imcb_add_buddy( ic, cmd[4], group );  			imcb_rename_buddy( ic, cmd[4], cmd[5] );  		}  	} @@ -605,6 +611,50 @@ static int msn_ns_command( gpointer data, char **cmd, int num_parts )  			return( 0 );  		}  	} +	else if( strcmp( cmd[0], "ADG" ) == 0 ) +	{ +		char *group = g_strdup( cmd[3] ); +		int groupnum, i; +		GSList *l, *next; +		 +		http_decode( group ); +		if( sscanf( cmd[4], "%d", &groupnum ) == 1 ) +		{ +			if( groupnum >= md->groupcount ) +			{ +				md->grouplist = g_renew( char *, md->grouplist, groupnum + 1 ); +				for( i = md->groupcount; i <= groupnum; i ++ ) +					md->grouplist[i] = NULL; +				md->groupcount = groupnum + 1; +			} +			g_free( md->grouplist[groupnum] ); +			md->grouplist[groupnum] = group; +		} +		else +		{ +			/* Shouldn't happen, but if it does, give up on the group. */ +			g_free( group ); +			imcb_error( ic, "Syntax error" ); +			imc_logout( ic, TRUE ); +			return 0; +		} +		 +		for( l = md->grpq; l; l = next ) +		{ +			struct msn_groupadd *ga = l->data; +			next = l->next; +			if( g_strcasecmp( ga->group, group ) == 0 ) +			{ +				if( !msn_buddy_list_add( ic, "FL", ga->who, ga->who, group ) ) +					return 0; +				 +				g_free( ga->group ); +				g_free( ga->who ); +				g_free( ga ); +				md->grpq = g_slist_remove( md->grpq, ga ); +			} +		} +	}  	else if( isdigit( cmd[0][0] ) )  	{  		int num = atoi( cmd[0] ); diff --git a/protocols/msn/sb.c b/protocols/msn/sb.c index 49eed601..cb5789b8 100644 --- a/protocols/msn/sb.c +++ b/protocols/msn/sb.c @@ -28,6 +28,7 @@  #include "msn.h"  #include "passport.h"  #include "md5.h" +#include "invitation.h"  static gboolean msn_sb_callback( gpointer data, gint source, b_input_condition cond );  static int msn_sb_command( gpointer data, char **cmd, int num_parts ); @@ -178,6 +179,11 @@ int msn_sb_sendmessage( struct msn_switchboard *sb, char *text )  			buf = g_strdup( SB_KEEPALIVE_HEADERS );  			i = strlen( buf );  		} +		else if( strncmp( text, MSN_INVITE_HEADERS, sizeof( MSN_INVITE_HEADERS ) - 1 ) == 0 )  +		{ +			buf = g_strdup( text ); +			i = strlen( buf ); +		}  		else  		{  			buf = g_new0( char, sizeof( MSN_MESSAGE_HEADERS ) + strlen( text ) * 2 + 1 ); @@ -226,11 +232,17 @@ int msn_sb_sendmessage( struct msn_switchboard *sb, char *text )  struct groupchat *msn_sb_to_chat( struct msn_switchboard *sb )  {  	struct im_connection *ic = sb->ic; +	struct groupchat *c = NULL;  	char buf[1024];  	/* Create the groupchat structure. */  	g_snprintf( buf, sizeof( buf ), "MSN groupchat session %d", sb->session ); -	sb->chat = imcb_chat_new( ic, buf ); +	if( sb->who ) +		c = bee_chat_by_title( ic->bee, ic, sb->who ); +	if( c && !msn_sb_by_chat( c ) ) +		sb->chat = c; +	else +		sb->chat = imcb_chat_new( ic, buf );  	/* Populate the channel. */  	if( sb->who ) imcb_chat_add_buddy( sb->chat, sb->who ); @@ -314,7 +326,7 @@ gboolean msn_sb_connected( gpointer data, gint source, b_input_condition cond )  		g_snprintf( buf, sizeof( buf ), "ANS %d %s %s %d\r\n", ++sb->trId, ic->acc->user, sb->key, sb->session );  	if( msn_sb_write( sb, buf, strlen( buf ) ) ) -		sb->inp = b_input_add( sb->fd, GAIM_INPUT_READ, msn_sb_callback, sb ); +		sb->inp = b_input_add( sb->fd, B_EV_IO_READ, msn_sb_callback, sb );  	else  		debug( "Error %d while connecting to switchboard server", 2 ); @@ -691,64 +703,46 @@ static int msn_sb_message( gpointer data, char *msg, int msglen, char **cmd, int  				/* PANIC! */  			}  		} +#if 0 +		// Disable MSN ft support for now.  		else if( g_strncasecmp( ct, "text/x-msmsgsinvite", 19 ) == 0 )  		{ -			char *itype = msn_findheader( body, "Application-GUID:", blen ); -			char buf[1024]; +			char *command = msn_findheader( body, "Invitation-Command:", blen ); +			char *cookie = msn_findheader( body, "Invitation-Cookie:", blen ); +			unsigned int icookie;  			g_free( ct ); -			*buf = 0; -			 -			if( !itype ) -				return( 1 ); -			 -			/* File transfer. */ -			if( strcmp( itype, "{5D3E02AB-6190-11d3-BBBB-00C04F795683}" ) == 0 ) -			{ -				char *name = msn_findheader( body, "Application-File:", blen ); -				char *size = msn_findheader( body, "Application-FileSize:", blen ); -				 -				if( name && size ) -				{ -					g_snprintf( buf, sizeof( buf ), "<< \x02""BitlBee\x02"" - Filetransfer: `%s', %s bytes >>\n" -					            "Filetransfers are not supported by BitlBee for now...", name, size ); -				} -				else -				{ -					strcpy( buf, "<< \x02""BitlBee\x02"" - Corrupted MSN filetransfer invitation message >>" ); -				} -				 -				if( name ) g_free( name ); -				if( size ) g_free( size ); -			} -			else -			{ -				char *iname = msn_findheader( body, "Application-Name:", blen ); -				 -				g_snprintf( buf, sizeof( buf ), "<< \x02""BitlBee\x02"" - Unknown MSN invitation - %s (%s) >>", -				                                itype, iname ? iname : "no name" ); -				 -				if( iname ) g_free( iname ); +			/* Every invite should have both a Command and Cookie header */ +			if( !command || !cookie ) { +				g_free( command ); +				g_free( cookie ); +				imcb_log( ic, "Warning: No command or cookie from %s", sb->who ); +				return 1;  			} -			g_free( itype ); -			 -			if( !*buf ) -				return( 1 ); +			icookie = strtoul( cookie, NULL, 10 ); +			g_free( cookie ); -			if( sb->who ) -			{ -				imcb_buddy_msg( ic, cmd[1], buf, 0, 0 ); -			} -			else if( sb->chat ) -			{ -				imcb_chat_msg( sb->chat, cmd[1], buf, 0, 0 ); -			} -			else -			{ -				/* PANIC! */ +			if( g_strncasecmp( command, "INVITE", 6 ) == 0 ) { +				msn_invitation_invite( sb, cmd[1], icookie, body, blen ); +			} else if( g_strncasecmp( command, "ACCEPT", 6 ) == 0 ) { +				msn_invitation_accept( sb, cmd[1], icookie, body, blen ); +			} else if( g_strncasecmp( command, "CANCEL", 6 ) == 0 ) { +				msn_invitation_cancel( sb, cmd[1], icookie, body, blen ); +			} else { +				imcb_log( ic, "Warning: Received invalid invitation with " +						"command %s from %s", command, sb->who );  			} +			 +			g_free( command ); +		} +#endif +		else if( g_strncasecmp( ct, "application/x-msnmsgrp2p", 24 ) == 0 )  +		{ +			imcb_error( sb->ic, "Cannot receive file from %s: BitlBee does not " +					"support msnmsgrp2p yet.", sb->who ); +			g_free( ct );  		}  		else if( g_strncasecmp( ct, "text/x-msmsgscontrol", 20 ) == 0 )  		{ @@ -779,10 +773,11 @@ static gboolean msn_sb_keepalive( gpointer data, gint source, b_input_condition  void msn_sb_start_keepalives( struct msn_switchboard *sb, gboolean initial )  { -	struct buddy *b; +	bee_user_t *bu;  	if( sb && sb->who && sb->keepalive == 0 && -	    ( b = imcb_find_buddy( sb->ic, sb->who ) ) && !b->present && +	    ( bu = bee_user_by_handle( sb->ic->bee, sb->ic, sb->who ) ) && +	    !( bu->flags & BEE_USER_ONLINE ) &&  	    set_getbool( &sb->ic->acc->set, "switchboard_keepalives" ) )  	{  		if( initial ) diff --git a/protocols/nogaim.c b/protocols/nogaim.c index 2248d11e..6ecdfe12 100644 --- a/protocols/nogaim.c +++ b/protocols/nogaim.c @@ -37,9 +37,6 @@  #include "nogaim.h"  #include "chat.h" -static int remove_chat_buddy_silent( struct groupchat *b, const char *handle ); -static char *format_timestamp( irc_t *irc, time_t msg_ts ); -  GSList *connections;  #ifdef WITH_PLUGINS @@ -92,8 +89,6 @@ void load_plugins(void)  }  #endif -/* nogaim.c */ -  GList *protocols = NULL;  void register_protocol (struct prpl *p) @@ -116,16 +111,18 @@ void register_protocol (struct prpl *p)  struct prpl *find_protocol(const char *name)  {  	GList *gl; -	for (gl = protocols; gl; gl = gl->next)  +	 +	for( gl = protocols; gl; gl = gl->next )   	{   		struct prpl *proto = gl->data; - 		if(!g_strcasecmp(proto->name, name))  + 		 + 		if( g_strcasecmp( proto->name, name ) == 0 )  			return proto;   	} + 	   	return NULL;  } -/* nogaim.c */  void nogaim_init()  {  	extern void msn_initmodule(); @@ -133,6 +130,7 @@ void nogaim_init()  	extern void byahoo_initmodule();  	extern void jabber_initmodule();  	extern void twitter_initmodule(); +	extern void purple_initmodule();  #ifdef WITH_MSN  	msn_initmodule(); @@ -154,6 +152,10 @@ void nogaim_init()  	twitter_initmodule();  #endif +#ifdef WITH_PURPLE +	purple_initmodule(); +#endif +  #ifdef WITH_PLUGINS  	load_plugins();  #endif @@ -161,15 +163,13 @@ void nogaim_init()  GSList *get_connections() { return connections; } -/* multi.c */ -  struct im_connection *imcb_new( account_t *acc )  {  	struct im_connection *ic;  	ic = g_new0( struct im_connection, 1 ); -	ic->irc = acc->irc; +	ic->bee = acc->bee;  	ic->acc = acc;  	acc->ic = ic; @@ -183,7 +183,7 @@ void imc_free( struct im_connection *ic )  	account_t *a;  	/* Destroy the pointer to this connection from the account list */ -	for( a = ic->irc->accounts; a; a = a->next ) +	for( a = ic->bee->accounts; a; a = a->next )  		if( a->ic == ic )  		{  			a->ic = NULL; @@ -204,20 +204,21 @@ static void serv_got_crap( struct im_connection *ic, char *format, ... )  	text = g_strdup_vprintf( format, params );  	va_end( params ); -	if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) || -	    ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) ) +	if( ( g_strcasecmp( set_getstr( &ic->bee->set, "strip_html" ), "always" ) == 0 ) || +	    ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->bee->set, "strip_html" ) ) )  		strip_html( text );  	/* Try to find a different connection on the same protocol. */ -	for( a = ic->irc->accounts; a; a = a->next ) +	for( a = ic->bee->accounts; a; a = a->next )  		if( a->prpl == ic->acc->prpl && a->ic != ic )  			break;  	/* If we found one, include the screenname in the message. */  	if( a ) -		irc_usermsg( ic->irc, "%s(%s) - %s", ic->acc->prpl->name, ic->acc->user, text ); +		/* FIXME(wilmer): ui_log callback or so */ +		irc_usermsg( ic->bee->ui_data, "%s(%s) - %s", ic->acc->prpl->name, ic->acc->user, text );  	else -		irc_usermsg( ic->irc, "%s - %s", ic->acc->prpl->name, text ); +		irc_usermsg( ic->bee->ui_data, "%s - %s", ic->acc->prpl->name, text );  	g_free( text );  } @@ -268,18 +269,12 @@ static gboolean send_keepalive( gpointer d, gint fd, b_input_condition cond )  void imcb_connected( struct im_connection *ic )  { -	irc_t *irc = ic->irc; -	struct chat *c; -	user_t *u; -	  	/* MSN servers sometimes redirect you to a different server and do  	   the whole login sequence again, so these "late" calls to this  	   function should be handled correctly. (IOW, ignored) */  	if( ic->flags & OPT_LOGGED_IN )  		return; -	u = user_find( ic->irc, ic->irc->nick ); -	  	imcb_log( ic, "Logged in" );  	ic->keepalive = b_timeout_add( 60000, send_keepalive, ic ); @@ -292,6 +287,7 @@ void imcb_connected( struct im_connection *ic )  	   exponential backoff timer. */  	ic->acc->auto_reconnect_delay = 0; +	/*  	for( c = irc->chatrooms; c; c = c->next )  	{  		if( c->acc != ic->acc ) @@ -300,6 +296,7 @@ void imcb_connected( struct im_connection *ic )  		if( set_getbool( &c->set, "auto_join" ) )  			chat_join( irc, c, NULL );  	} +	*/  }  gboolean auto_reconnect( gpointer data, gint fd, b_input_condition cond ) @@ -307,7 +304,7 @@ gboolean auto_reconnect( gpointer data, gint fd, b_input_condition cond )  	account_t *a = data;  	a->reconnect = 0; -	account_on( a->irc, a ); +	account_on( a->bee, a );  	return( FALSE );	/* Only have to run the timeout once */  } @@ -320,9 +317,9 @@ void cancel_auto_reconnect( account_t *a )  void imc_logout( struct im_connection *ic, int allow_reconnect )  { -	irc_t *irc = ic->irc; -	user_t *t, *u; +	bee_t *bee = ic->bee;  	account_t *a; +	GSList *l;  	int delay;  	/* Nested calls might happen sometimes, this is probably the best @@ -342,22 +339,20 @@ void imc_logout( struct im_connection *ic, int allow_reconnect )  	g_free( ic->away );  	ic->away = NULL; -	u = irc->users; -	while( u ) +	for( l = bee->users; l; )  	{ -		if( u->ic == ic ) -		{ -			t = u->next; -			user_del( irc, u->nick ); -			u = t; -		} -		else -			u = u->next; +		bee_user_t *bu = l->data; +		GSList *next = l->next; +		 +		if( bu->ic == ic ) +			bee_user_free( bee, bu ); +		 +		l = next;  	} -	query_del_by_conn( ic->irc, ic ); +	query_del_by_conn( (irc_t*) ic->bee->ui_data, ic ); -	for( a = irc->accounts; a; a = a->next ) +	for( a = bee->accounts; a; a = a->next )  		if( a->ic == ic )  			break; @@ -365,7 +360,7 @@ void imc_logout( struct im_connection *ic, int allow_reconnect )  	{  		/* Uhm... This is very sick. */  	} -	else if( allow_reconnect && set_getbool( &irc->set, "auto_reconnect" ) && +	else if( allow_reconnect && set_getbool( &bee->set, "auto_reconnect" ) &&  	         set_getbool( &a->set, "auto_reconnect" ) &&  	         ( delay = account_reconnect_delay( a ) ) > 0 )  	{ @@ -376,170 +371,59 @@ void imc_logout( struct im_connection *ic, int allow_reconnect )  	imc_free( ic );  } - -/* dialogs.c */ -  void imcb_ask( struct im_connection *ic, char *msg, void *data,                 query_callback doit, query_callback dont )  { -	query_add( ic->irc, ic, msg, doit, dont, data ); +	query_add( (irc_t *) ic->bee->ui_data, ic, msg, doit, dont, data );  } - -/* list.c */ -  void imcb_add_buddy( struct im_connection *ic, const char *handle, const char *group )  { -	user_t *u; -	char nick[MAX_NICK_LENGTH+1], *s; -	irc_t *irc = ic->irc; -	 -	if( user_findhandle( ic, handle ) ) -	{ -		if( set_getbool( &irc->set, "debug" ) ) -			imcb_log( ic, "User already exists, ignoring add request: %s", handle ); -		 -		return; -		 -		/* Buddy seems to exist already. Let's ignore this request then... -		   Eventually subsequent calls to this function *should* be possible -		   when a buddy is in multiple groups. But for now BitlBee doesn't -		   even support groups so let's silently ignore this for now. */ -	} -	 -	memset( nick, 0, MAX_NICK_LENGTH + 1 ); -	strcpy( nick, nick_get( ic->acc, handle ) ); -	 -	u = user_add( ic->irc, nick ); -	 -//	if( !realname || !*realname ) realname = nick; -//	u->realname = g_strdup( realname ); -	 -	if( ( s = strchr( handle, '@' ) ) ) -	{ -		u->host = g_strdup( s + 1 ); -		u->user = g_strndup( handle, s - handle ); -	} -	else if( ic->acc->server ) -	{ -		u->host = g_strdup( ic->acc->server ); -		u->user = g_strdup( handle ); -		 -		/* s/ /_/ ... important for AOL screennames */ -		for( s = u->user; *s; s ++ ) -			if( *s == ' ' ) -				*s = '_'; -	} -	else -	{ -		u->host = g_strdup( ic->acc->prpl->name ); -		u->user = g_strdup( handle ); -	} -	 -	u->ic = ic; -	u->handle = g_strdup( handle ); -	if( group ) u->group = g_strdup( group ); -	u->send_handler = buddy_send_handler; -	u->last_typing_notice = 0; -} - -struct buddy *imcb_find_buddy( struct im_connection *ic, char *handle ) -{ -	static struct buddy b[1]; -	user_t *u; -	 -	u = user_findhandle( ic, handle ); +	bee_user_t *bu; +	bee_t *bee = ic->bee; -	if( !u ) -		return( NULL ); +	if( !( bu = bee_user_by_handle( bee, ic, handle ) ) ) +		bu = bee_user_new( bee, ic, handle, 0 ); -	memset( b, 0, sizeof( b ) ); -	strncpy( b->name, handle, 80 ); -	strncpy( b->show, u->realname, BUDDY_ALIAS_MAXLEN ); -	b->present = u->online; -	b->ic = u->ic; +	bu->group = bee_group_by_name( bee, group, TRUE ); -	return( b ); +	if( bee->ui->user_group ) +		bee->ui->user_group( bee, bu );  } -void imcb_rename_buddy( struct im_connection *ic, const char *handle, const char *realname ) +void imcb_rename_buddy( struct im_connection *ic, const char *handle, const char *fullname )  { -	user_t *u = user_findhandle( ic, handle ); -	char *set; +	bee_t *bee = ic->bee; +	bee_user_t *bu = bee_user_by_handle( bee, ic, handle ); -	if( !u || !realname ) return; -	 -	if( g_strcasecmp( u->realname, realname ) != 0 ) -	{ -		if( u->realname != u->nick ) g_free( u->realname ); -		 -		u->realname = g_strdup( realname ); -		 -		if( ( ic->flags & OPT_LOGGED_IN ) && set_getbool( &ic->irc->set, "display_namechanges" ) ) -			imcb_log( ic, "User `%s' changed name to `%s'", u->nick, u->realname ); -	} +	if( !bu || !fullname ) return; -	set = set_getstr( &ic->acc->set, "nick_source" ); -	if( strcmp( set, "handle" ) != 0 ) +	if( !bu->fullname || strcmp( bu->fullname, fullname ) != 0 )  	{ -		char *name = g_strdup( realname ); -		 -		if( strcmp( set, "first_name" ) == 0 ) -		{ -			int i; -			for( i = 0; name[i] && !isspace( name[i] ); i ++ ) {} -			name[i] = '\0'; -		} +		g_free( bu->fullname ); +		bu->fullname = g_strdup( fullname ); -		imcb_buddy_nick_hint( ic, handle, name ); -		 -		g_free( name ); +		if( bee->ui->user_fullname ) +			bee->ui->user_fullname( bee, bu );  	}  }  void imcb_remove_buddy( struct im_connection *ic, const char *handle, char *group )  { -	user_t *u; -	 -	if( ( u = user_findhandle( ic, handle ) ) ) -		user_del( ic->irc, u->nick ); +	bee_user_free( ic->bee, bee_user_by_handle( ic->bee, ic, handle ) );  }  /* Mainly meant for ICQ (and now also for Jabber conferences) to allow IM     modules to suggest a nickname for a handle. */  void imcb_buddy_nick_hint( struct im_connection *ic, const char *handle, const char *nick )  { -	user_t *u = user_findhandle( ic, handle ); -	char newnick[MAX_NICK_LENGTH+1], *orig_nick; +	bee_t *bee = ic->bee; +	bee_user_t *bu = bee_user_by_handle( bee, ic, handle ); -	if( u && !u->online && !nick_saved( ic->acc, handle ) ) -	{ -		/* Only do this if the person isn't online yet (which should -		   be the case if we just added it) and if the user hasn't -		   assigned a nickname to this buddy already. */ -		 -		strncpy( newnick, nick, MAX_NICK_LENGTH ); -		newnick[MAX_NICK_LENGTH] = 0; -		 -		/* Some processing to make sure this string is a valid IRC nickname. */ -		nick_strip( newnick ); -		if( set_getbool( &ic->irc->set, "lcnicks" ) ) -			nick_lc( newnick ); -		 -		if( strcmp( u->nick, newnick ) != 0 ) -		{ -			/* Only do this if newnick is different from the current one. -			   If rejoining a channel, maybe we got this nick already -			   (and dedupe would only add an underscore. */ -			nick_dedupe( ic->acc, handle, newnick ); -			 -			/* u->nick will be freed halfway the process, so it can't be -			   passed as an argument. */ -			orig_nick = g_strdup( u->nick ); -			user_rename( ic->irc, orig_nick, newnick ); -			g_free( orig_nick ); -		} -	} +	if( !bu || !nick ) return; +	 +	if( bee->ui->user_nick_hint ) +		bee->ui->user_nick_hint( bee, bu, nick );  } @@ -584,7 +468,8 @@ void imcb_ask_auth( struct im_connection *ic, const char *handle, const char *re  	data->ic = ic;  	data->handle = g_strdup( handle ); -	query_add( ic->irc, ic, s, imcb_ask_auth_cb_yes, imcb_ask_auth_cb_no, data ); +	query_add( (irc_t *) ic->bee->ui_data, ic, s, +	           imcb_ask_auth_cb_yes, imcb_ask_auth_cb_no, data );  } @@ -609,672 +494,25 @@ void imcb_ask_add( struct im_connection *ic, const char *handle, const char *rea  	char *s;  	/* TODO: Make a setting for this! */ -	if( user_findhandle( ic, handle ) != NULL ) +	if( bee_user_by_handle( ic->bee, ic, handle ) != NULL )  		return;  	s = g_strdup_printf( "The user %s is not in your buddy list yet. Do you want to add him/her now?", handle );  	data->ic = ic;  	data->handle = g_strdup( handle ); -	query_add( ic->irc, ic, s, imcb_ask_add_cb_yes, imcb_ask_add_cb_no, data ); -} - - -/* server.c */                     - -void imcb_buddy_status( struct im_connection *ic, const char *handle, int flags, const char *state, const char *message ) -{ -	user_t *u; -	int oa, oo; -	 -	u = user_findhandle( ic, (char*) handle ); -	 -	if( !u ) -	{ -		if( g_strcasecmp( set_getstr( &ic->irc->set, "handle_unknown" ), "add" ) == 0 ) -		{ -			imcb_add_buddy( ic, (char*) handle, NULL ); -			u = user_findhandle( ic, (char*) handle ); -		} -		else -		{ -			if( set_getbool( &ic->irc->set, "debug" ) || g_strcasecmp( set_getstr( &ic->irc->set, "handle_unknown" ), "ignore" ) != 0 ) -			{ -				imcb_log( ic, "imcb_buddy_status() for unknown handle %s:", handle ); -				imcb_log( ic, "flags = %d, state = %s, message = %s", flags, -				          state ? state : "NULL", message ? message : "NULL" ); -			} -			 -			return; -		} -	} -	 -	oa = u->away != NULL; -	oo = u->online; -	 -	g_free( u->away ); -	g_free( u->status_msg ); -	u->away = u->status_msg = NULL; -	 -	if( set_getbool( &ic->irc->set, "show_offline" ) && !u->online ) -	{ -		/* always set users as online */ -		irc_spawn( ic->irc, u ); -		u->online = 1; -		if( !( flags & OPT_LOGGED_IN ) ) -		{ -			/* set away message if user isn't really online */ -			u->away = g_strdup( "User is offline" ); -		} -	} -	else if( ( flags & OPT_LOGGED_IN ) && !u->online ) -	{ -		irc_spawn( ic->irc, u ); -		u->online = 1; -	} -	else if( !( flags & OPT_LOGGED_IN ) && u->online ) -	{ -		struct groupchat *c; -		 -		if( set_getbool( &ic->irc->set, "show_offline" ) ) -		{ -			/* keep offline users in channel and set away message to "offline" */ -			u->away = g_strdup( "User is offline" ); - -			/* Keep showing him/her in the control channel but not in groupchats. */ -			for( c = ic->groupchats; c; c = c->next ) -			{ -				if( remove_chat_buddy_silent( c, handle ) && c->joined ) -					irc_part( c->ic->irc, u, c->channel ); -			} -		} -		else -		{ -			/* kill offline users */ -			irc_kill( ic->irc, u ); -			u->online = 0; - -			/* Remove him/her from the groupchats to prevent PART messages after he/she QUIT already */ -			for( c = ic->groupchats; c; c = c->next ) -				remove_chat_buddy_silent( c, handle ); -		} -	} - -	if( flags & OPT_AWAY ) -	{ -		if( state && message ) -		{ -			u->away = g_strdup_printf( "%s (%s)", state, message ); -		} -		else if( state ) -		{ -			u->away = g_strdup( state ); -		} -		else if( message ) -		{ -			u->away = g_strdup( message ); -		} -		else -		{ -			u->away = g_strdup( "Away" ); -		} -	} -	else -	{ -		u->status_msg = g_strdup( message ); -	} -	 -	/* early if-clause for show_offline even if there is some redundant code here because this isn't LISP but C ;) */ -	if( set_getbool( &ic->irc->set, "show_offline" ) && set_getbool( &ic->irc->set, "away_devoice" ) ) -	{ -		char *from; -		 -		if( set_getbool( &ic->irc->set, "simulate_netsplit" ) ) -		{ -			from = g_strdup( ic->irc->myhost ); -		} -		else -		{ -			from = g_strdup_printf( "%s!%s@%s", ic->irc->mynick, ic->irc->mynick, -			                                    ic->irc->myhost ); -		} - -		/* if we use show_offline, we op online users, voice away users, and devoice/deop offline users */ -		if( flags & OPT_LOGGED_IN ) -		{ -			/* user is "online" (either really online or away) */ -			irc_write( ic->irc, ":%s MODE %s %cv%co %s %s", from, ic->irc->channel, -			                                          u->away?'+':'-', u->away?'-':'+', u->nick, u->nick ); -		} -		else -		{ -			/* user is offline */ -			irc_write( ic->irc, ":%s MODE %s -vo %s %s", from, ic->irc->channel, u->nick, u->nick ); -		} -	} -	else -	{  -		/* LISPy... */ -		if( ( set_getbool( &ic->irc->set, "away_devoice" ) ) &&		/* Don't do a thing when user doesn't want it */ -		    ( u->online ) &&						/* Don't touch offline people */ -		    ( ( ( u->online != oo ) && !u->away ) ||			/* Voice joining people */ -		      ( ( u->online == oo ) && ( oa == !u->away ) ) ) )		/* (De)voice people changing state */ -		{ -			char *from; - -			if( set_getbool( &ic->irc->set, "simulate_netsplit" ) ) -			{ -				from = g_strdup( ic->irc->myhost ); -			} -			else -			{ -				from = g_strdup_printf( "%s!%s@%s", ic->irc->mynick, ic->irc->mynick, -				                                    ic->irc->myhost ); -			} -			irc_write( ic->irc, ":%s MODE %s %cv %s", from, ic->irc->channel, -			                                          u->away?'-':'+', u->nick ); -			g_free( from ); -		} -	} -} - -void imcb_buddy_msg( struct im_connection *ic, const char *handle, char *msg, uint32_t flags, time_t sent_at ) -{ -	irc_t *irc = ic->irc; -	char *wrapped, *ts = NULL; -	user_t *u; -	 -	u = user_findhandle( ic, handle ); -	 -	if( !u ) -	{ -		char *h = set_getstr( &irc->set, "handle_unknown" ); -		 -		if( g_strcasecmp( h, "ignore" ) == 0 ) -		{ -			if( set_getbool( &irc->set, "debug" ) ) -				imcb_log( ic, "Ignoring message from unknown handle %s", handle ); -			 -			return; -		} -		else if( g_strncasecmp( h, "add", 3 ) == 0 ) -		{ -			int private = set_getbool( &irc->set, "private" ); -			 -			if( h[3] ) -			{ -				if( g_strcasecmp( h + 3, "_private" ) == 0 ) -					private = 1; -				else if( g_strcasecmp( h + 3, "_channel" ) == 0 ) -					private = 0; -			} -			 -			imcb_add_buddy( ic, handle, NULL ); -			u = user_findhandle( ic, handle ); -			u->is_private = private; -		} -		else -		{ -			imcb_log( ic, "Message from unknown handle %s:", handle ); -			u = user_find( irc, irc->mynick ); -		} -	} -	 -	if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) || -	    ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) ) -		strip_html( msg ); -	 -	if( set_getbool( &ic->irc->set, "display_timestamps" ) && -	    ( ts = format_timestamp( irc, sent_at ) ) ) -	{ -		char *new = g_strconcat( ts, msg, NULL ); -		g_free( ts ); -		ts = msg = new; -	} -	 -	wrapped = word_wrap( msg, 425 ); -	irc_msgfrom( irc, u->nick, wrapped ); -	g_free( wrapped ); -	g_free( ts ); -} - -void imcb_buddy_typing( struct im_connection *ic, char *handle, uint32_t flags ) -{ -	user_t *u; -	 -	if( !set_getbool( &ic->irc->set, "typing_notice" ) ) -		return; -	 -	if( ( u = user_findhandle( ic, handle ) ) ) -	{ -		char buf[256];  -		 -		g_snprintf( buf, 256, "\1TYPING %d\1", ( flags >> 8 ) & 3 ); -		irc_privmsg( ic->irc, u, "PRIVMSG", ic->irc->nick, NULL, buf ); -	} -} - -struct groupchat *imcb_chat_new( struct im_connection *ic, const char *handle ) -{ -	struct groupchat *c; -	 -	/* This one just creates the conversation structure, user won't see anything yet */ -	 -	if( ic->groupchats ) -	{ -		for( c = ic->groupchats; c->next; c = c->next ); -		c = c->next = g_new0( struct groupchat, 1 ); -	} -	else -		ic->groupchats = c = g_new0( struct groupchat, 1 ); -	 -	c->ic = ic; -	c->title = g_strdup( handle ); -	c->channel = g_strdup_printf( "&chat_%03d", ic->irc->c_id++ ); -	c->topic = g_strdup_printf( "BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!", c->title ); -	 -	if( set_getbool( &ic->irc->set, "debug" ) ) -		imcb_log( ic, "Creating new conversation: (id=%p,handle=%s)", c, handle ); -	 -	return c; -} - -void imcb_chat_name_hint( struct groupchat *c, const char *name ) -{ -	if( !c->joined ) -	{ -		struct im_connection *ic = c->ic; -		char stripped[MAX_NICK_LENGTH+1], *full_name; -		 -		strncpy( stripped, name, MAX_NICK_LENGTH ); -		stripped[MAX_NICK_LENGTH] = '\0'; -		nick_strip( stripped ); -		if( set_getbool( &ic->irc->set, "lcnicks" ) ) -			nick_lc( stripped ); -		 -		full_name = g_strdup_printf( "&%s", stripped ); -		 -		if( stripped[0] && -		    nick_cmp( stripped, ic->irc->channel + 1 ) != 0 && -		    irc_chat_by_channel( ic->irc, full_name ) == NULL ) -		{ -			g_free( c->channel ); -			c->channel = full_name; -		} -		else -		{ -			g_free( full_name ); -		} -	} -} - -void imcb_chat_free( struct groupchat *c ) -{ -	struct im_connection *ic = c->ic; -	struct groupchat *l; -	GList *ir; -	 -	if( set_getbool( &ic->irc->set, "debug" ) ) -		imcb_log( ic, "You were removed from conversation %p", c ); -	 -	if( c ) -	{ -		if( c->joined ) -		{ -			user_t *u, *r; -			 -			r = user_find( ic->irc, ic->irc->mynick ); -			irc_privmsg( ic->irc, r, "PRIVMSG", c->channel, "", "Cleaning up channel, bye!" ); -			 -			u = user_find( ic->irc, ic->irc->nick ); -			irc_kick( ic->irc, u, c->channel, r ); -			/* irc_part( ic->irc, u, c->channel ); */ -		} -		 -		/* Find the previous chat in the linked list. */ -		for( l = ic->groupchats; l && l->next != c; l = l->next ); -		 -		if( l ) -			l->next = c->next; -		else -			ic->groupchats = c->next; -		 -		for( ir = c->in_room; ir; ir = ir->next ) -			g_free( ir->data ); -		g_list_free( c->in_room ); -		g_free( c->channel ); -		g_free( c->title ); -		g_free( c->topic ); -		g_free( c ); -	} -} - -void imcb_chat_msg( struct groupchat *c, const char *who, char *msg, uint32_t flags, time_t sent_at ) -{ -	struct im_connection *ic = c->ic; -	char *wrapped; -	user_t *u; -	 -	/* Gaim sends own messages through this too. IRC doesn't want this, so kill them */ -	if( g_strcasecmp( who, ic->acc->user ) == 0 ) -		return; -	 -	u = user_findhandle( ic, who ); -	 -	if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) || -	    ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) ) -		strip_html( msg ); -	 -	wrapped = word_wrap( msg, 425 ); -	if( c && u ) -	{ -		char *ts = NULL; -		if( set_getbool( &ic->irc->set, "display_timestamps" ) ) -			ts = format_timestamp( ic->irc, sent_at ); -		irc_privmsg( ic->irc, u, "PRIVMSG", c->channel, ts ? : "", wrapped ); -		g_free( ts ); -	} -	else -	{ -		imcb_log( ic, "Message from/to conversation %s@%p (unknown conv/user): %s", who, c, wrapped ); -	} -	g_free( wrapped ); -} - -void imcb_chat_log( struct groupchat *c, char *format, ... ) -{ -	irc_t *irc = c->ic->irc; -	va_list params; -	char *text; -	user_t *u; -	 -	va_start( params, format ); -	text = g_strdup_vprintf( format, params ); -	va_end( params ); -	 -	u = user_find( irc, irc->mynick ); -	 -	irc_privmsg( irc, u, "PRIVMSG", c->channel, "System message: ", text ); -	 -	g_free( text ); -} - -void imcb_chat_topic( struct groupchat *c, char *who, char *topic, time_t set_at ) -{ -	struct im_connection *ic = c->ic; -	user_t *u = NULL; -	 -	if( who == NULL) -		u = user_find( ic->irc, ic->irc->mynick ); -	else if( g_strcasecmp( who, ic->acc->user ) == 0 ) -		u = user_find( ic->irc, ic->irc->nick ); -	else -		u = user_findhandle( ic, who ); -	 -	if( ( g_strcasecmp( set_getstr( &ic->irc->set, "strip_html" ), "always" ) == 0 ) || -	    ( ( ic->flags & OPT_DOES_HTML ) && set_getbool( &ic->irc->set, "strip_html" ) ) ) -		strip_html( topic ); -	 -	g_free( c->topic ); -	c->topic = g_strdup( topic ); -	 -	if( c->joined && u ) -		irc_write( ic->irc, ":%s!%s@%s TOPIC %s :%s", u->nick, u->user, u->host, c->channel, topic ); -} - - -/* buddy_chat.c */ - -void imcb_chat_add_buddy( struct groupchat *b, const char *handle ) -{ -	user_t *u = user_findhandle( b->ic, handle ); -	int me = 0; -	 -	if( set_getbool( &b->ic->irc->set, "debug" ) ) -		imcb_log( b->ic, "User %s added to conversation %p", handle, b ); -	 -	/* It might be yourself! */ -	if( b->ic->acc->prpl->handle_cmp( handle, b->ic->acc->user ) == 0 ) -	{ -		u = user_find( b->ic->irc, b->ic->irc->nick ); -		if( !b->joined ) -			irc_join( b->ic->irc, u, b->channel ); -		b->joined = me = 1; -	} -	 -	/* Most protocols allow people to join, even when they're not in -	   your contact list. Try to handle that here */ -	if( !u ) -	{ -		imcb_add_buddy( b->ic, handle, NULL ); -		u = user_findhandle( b->ic, handle ); -	} -	 -	/* Add the handle to the room userlist, if it's not 'me' */ -	if( !me ) -	{ -		if( b->joined ) -			irc_join( b->ic->irc, u, b->channel ); -		b->in_room = g_list_append( b->in_room, g_strdup( handle ) ); -	} -} - -/* This function is one BIG hack... :-( EREWRITE */ -void imcb_chat_remove_buddy( struct groupchat *b, const char *handle, const char *reason ) -{ -	user_t *u; -	int me = 0; -	 -	if( set_getbool( &b->ic->irc->set, "debug" ) ) -		imcb_log( b->ic, "User %s removed from conversation %p (%s)", handle, b, reason ? reason : "" ); -	 -	/* It might be yourself! */ -	if( g_strcasecmp( handle, b->ic->acc->user ) == 0 ) -	{ -		if( b->joined == 0 ) -			return; -		 -		u = user_find( b->ic->irc, b->ic->irc->nick ); -		b->joined = 0; -		me = 1; -	} -	else -	{ -		u = user_findhandle( b->ic, handle ); -	} -	 -	if( me || ( remove_chat_buddy_silent( b, handle ) && b->joined && u ) ) -		irc_part( b->ic->irc, u, b->channel ); -} - -static int remove_chat_buddy_silent( struct groupchat *b, const char *handle ) -{ -	GList *i; -	 -	/* Find the handle in the room userlist and shoot it */ -	i = b->in_room; -	while( i ) -	{ -		if( g_strcasecmp( handle, i->data ) == 0 ) -		{ -			g_free( i->data ); -			b->in_room = g_list_remove( b->in_room, i->data ); -			return( 1 ); -		} -		 -		i = i->next; -	} -	 -	return( 0 ); -} - - -/* Misc. BitlBee stuff which shouldn't really be here */ - -char *set_eval_away_devoice( set_t *set, char *value ) -{ -	irc_t *irc = set->data; -	int st; -	 -	if( !is_bool( value ) ) -		return SET_INVALID; -	 -	st = bool2int( value ); -	 -	/* Horror.... */ -	 -	if( st != set_getbool( &irc->set, "away_devoice" ) ) -	{ -		char list[80] = ""; -		user_t *u = irc->users; -		int i = 0, count = 0; -		char pm; -		char v[80]; -		 -		if( st ) -			pm = '+'; -		else -			pm = '-'; -		 -		while( u ) -		{ -			if( u->ic && u->online && !u->away ) -			{ -				if( ( strlen( list ) + strlen( u->nick ) ) >= 79 ) -				{ -					for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0; -					irc_write( irc, ":%s MODE %s %c%s%s", -					           irc->myhost, -		        			   irc->channel, pm, v, list ); -					 -					*list = 0; -					count = 0; -				} -				 -				sprintf( list + strlen( list ), " %s", u->nick ); -				count ++; -			} -			u = u->next; -		} -		 -		/* $v = 'v' x $i */ -		for( i = 0; i < count; v[i++] = 'v' ); v[i] = 0; -		irc_write( irc, ":%s MODE %s %c%s%s", irc->myhost, -		                                            irc->channel, pm, v, list ); -	} -	 -	return value; +	query_add( (irc_t *) ic->bee->ui_data, ic, s, +	           imcb_ask_add_cb_yes, imcb_ask_add_cb_no, data );  } -char *set_eval_timezone( set_t *set, char *value ) +struct bee_user *imcb_buddy_by_handle( struct im_connection *ic, const char *handle )  { -	char *s; -	 -	if( strcmp( value, "local" ) == 0 || -	    strcmp( value, "gmt" ) == 0 || strcmp( value, "utc" ) == 0 ) -		return value; -	 -	/* Otherwise: +/- at the beginning optional, then one or more numbers, -	   possibly followed by a colon and more numbers. Don't bother bound- -	   checking them since users are free to shoot themselves in the foot. */ -	s = value; -	if( *s == '+' || *s == '-' ) -		s ++; -	 -	/* \d+ */ -	if( !isdigit( *s ) ) -		return SET_INVALID; -	while( *s && isdigit( *s ) ) s ++; -	 -	/* EOS? */ -	if( *s == '\0' ) -		return value; -	 -	/* Otherwise, colon */ -	if( *s != ':' ) -		return SET_INVALID; -	s ++; -	 -	/* \d+ */ -	if( !isdigit( *s ) ) -		return SET_INVALID; -	while( *s && isdigit( *s ) ) s ++; -	 -	/* EOS */ -	return *s == '\0' ? value : SET_INVALID; -} - -static char *format_timestamp( irc_t *irc, time_t msg_ts ) -{ -	time_t now_ts = time( NULL ); -	struct tm now, msg; -	char *set; -	 -	/* If the timestamp is <= 0 or less than a minute ago, discard it as -	   it doesn't seem to add to much useful info and/or might be noise. */ -	if( msg_ts <= 0 || msg_ts > now_ts - 60 ) -		return NULL; -	 -	set = set_getstr( &irc->set, "timezone" ); -	if( strcmp( set, "local" ) == 0 ) -	{ -		localtime_r( &now_ts, &now ); -		localtime_r( &msg_ts, &msg ); -	} -	else -	{ -		int hr, min = 0, sign = 60; -		 -		if( set[0] == '-' ) -		{ -			sign *= -1; -			set ++; -		} -		else if( set[0] == '+' ) -		{ -			set ++; -		} -		 -		if( sscanf( set, "%d:%d", &hr, &min ) >= 1 ) -		{ -			msg_ts += sign * ( hr * 60 + min ); -			now_ts += sign * ( hr * 60 + min ); -		} -		 -		gmtime_r( &now_ts, &now ); -		gmtime_r( &msg_ts, &msg ); -	} -	 -	if( msg.tm_year == now.tm_year && msg.tm_yday == now.tm_yday ) -		return g_strdup_printf( "\x02[\x02\x02\x02%02d:%02d:%02d\x02]\x02 ", -		                        msg.tm_hour, msg.tm_min, msg.tm_sec ); -	else -		return g_strdup_printf( "\x02[\x02\x02\x02%04d-%02d-%02d " -		                        "%02d:%02d:%02d\x02]\x02 ", -		                        msg.tm_year + 1900, msg.tm_mon + 1, msg.tm_mday, -		                        msg.tm_hour, msg.tm_min, msg.tm_sec ); +	return bee_user_by_handle( ic->bee, ic, handle );  }  /* The plan is to not allow straight calls to prpl functions anymore, but do     them all from some wrappers. We'll start to define some down here: */ -int imc_buddy_msg( struct im_connection *ic, char *handle, char *msg, int flags ) -{ -	char *buf = NULL; -	int st; -	 -	if( ( ic->flags & OPT_DOES_HTML ) && ( g_strncasecmp( msg, "<html>", 6 ) != 0 ) ) -	{ -		buf = escape_html( msg ); -		msg = buf; -	} -	 -	st = ic->acc->prpl->buddy_msg( ic, handle, msg, flags ); -	g_free( buf ); -	 -	return st; -} -  int imc_chat_msg( struct groupchat *c, char *msg, int flags )  {  	char *buf = NULL; @@ -1302,7 +540,7 @@ int imc_away_send_update( struct im_connection *ic )  		return 0;  	away = set_getstr( &ic->acc->set, "away" ) ? -	     : set_getstr( &ic->irc->set, "away" ); +	     : set_getstr( &ic->bee->set, "away" );  	if( away && *away )  	{  		GList *m = ic->acc->prpl->away_states( ic ); @@ -1313,7 +551,7 @@ int imc_away_send_update( struct im_connection *ic )  	{  		away = NULL;  		msg = set_getstr( &ic->acc->set, "status" ) ? -		    : set_getstr( &ic->irc->set, "status" ); +		    : set_getstr( &ic->bee->set, "status" );  	}  	ic->acc->prpl->set_away( ic, away, msg ); diff --git a/protocols/nogaim.h b/protocols/nogaim.h index 48a80413..5ce62742 100644 --- a/protocols/nogaim.h +++ b/protocols/nogaim.h @@ -1,7 +1,7 @@    /********************************************************************\    * BitlBee -- An IRC to other IM-networks gateway                     *    *                                                                    * -  * Copyright 2002-2004 Wilmer van der Gaast and others                * +  * Copyright 2002-2010 Wilmer van der Gaast and others                *    \********************************************************************/  /* @@ -44,6 +44,8 @@  #include "account.h"  #include "proxy.h"  #include "query.h" +#include "md5.h" +#include "ft.h"  #define BUDDY_ALIAS_MAXLEN 388   /* because MSN names can be 387 characters */ @@ -84,9 +86,9 @@ struct im_connection  	int evil;  	/* BitlBee */ -	irc_t *irc; +	bee_t *bee; -	struct groupchat *groupchats; +	GSList *groupchats;  };  struct groupchat { @@ -97,10 +99,9 @@ struct groupchat {  	 * "nick list". This is how you can check who is in the group chat  	 * already, for example to avoid adding somebody two times. */  	GList *in_room; -	GList *ignored; +	//GList *ignored; -	struct groupchat *next; -	char *channel; +	//struct groupchat *next;  	/* The title variable contains the ID you gave when you created the  	 * chat using imcb_chat_new(). */  	char *title; @@ -111,6 +112,7 @@ struct groupchat {  	/* This is for you, you can add your own structure here to extend this  	 * structure for your protocol's needs. */  	void *data; +	void *ui_data;  };  struct buddy { @@ -131,6 +133,7 @@ struct prpl {  	/* You should set this to the name of your protocol.  	 * - The user sees this name ie. when imcb_log() is used. */  	const char *name; +	void *data;  	/* Added this one to be able to add per-account settings, don't think  	 * it should be used for anything else. You are supposed to use the @@ -227,6 +230,9 @@ struct prpl {  	/* Implement these callbacks if you want to use imcb_ask_auth() */  	void (* auth_allow)	(struct im_connection *, const char *who);  	void (* auth_deny)	(struct im_connection *, const char *who); + +	/* Incoming transfer request */ +	void (* transfer_request) (struct im_connection *, file_transfer_t *ft, char *handle );  };  /* im_api core stuff. */ @@ -280,16 +286,8 @@ G_MODULE_EXPORT struct buddy *imcb_find_buddy( struct im_connection *ic, char *h  G_MODULE_EXPORT void imcb_rename_buddy( struct im_connection *ic, const char *handle, const char *realname );  G_MODULE_EXPORT void imcb_buddy_nick_hint( struct im_connection *ic, const char *handle, const char *nick ); -/* Buddy activity */ -/* To manipulate the status of a handle. - * - flags can be |='d with OPT_* constants. You will need at least: - *   OPT_LOGGED_IN and OPT_AWAY. - * - 'state' and 'message' can be NULL */ -G_MODULE_EXPORT void imcb_buddy_status( struct im_connection *ic, const char *handle, int flags, const char *state, const char *message ); -/* Not implemented yet! */ G_MODULE_EXPORT void imcb_buddy_times( struct im_connection *ic, const char *handle, time_t login, time_t idle ); -/* Call when a handle says something. 'flags' and 'sent_at may be just 0. */ -G_MODULE_EXPORT void imcb_buddy_msg( struct im_connection *ic, const char *handle, char *msg, uint32_t flags, time_t sent_at );  G_MODULE_EXPORT void imcb_buddy_typing( struct im_connection *ic, char *handle, uint32_t flags ); +G_MODULE_EXPORT struct bee_user *imcb_buddy_by_handle( struct im_connection *ic, const char *handle );  G_MODULE_EXPORT void imcb_clean_handle( struct im_connection *ic, char *handle );  /* Groupchats */ @@ -315,7 +313,6 @@ G_MODULE_EXPORT void imcb_chat_free( struct groupchat *c );  /* Actions, or whatever. */  int imc_away_send_update( struct im_connection *ic ); -int imc_buddy_msg( struct im_connection *ic, char *handle, char *msg, int flags );  int imc_chat_msg( struct groupchat *c, char *msg, int flags );  void imc_add_allow( struct im_connection *ic, char *handle ); diff --git a/protocols/oscar/Makefile b/protocols/oscar/Makefile index 2792f22a..0ec7436b 100644 --- a/protocols/oscar/Makefile +++ b/protocols/oscar/Makefile @@ -7,6 +7,10 @@  ### DEFINITIONS  -include ../../Makefile.settings +ifdef SRCDIR +SRCDIR := $(SRCDIR)protocols/oscar/ +CFLAGS += -I$(SRCDIR) +endif  # [SH] Program variables  objects = admin.o auth.o bos.o buddylist.o chat.o chatnav.o conn.o icq.o im.o info.o misc.o msgcookie.o rxhandlers.o rxqueue.o search.o service.o snac.o ssi.o stats.o tlv.o txqueue.o oscar_util.o oscar.o @@ -32,7 +36,7 @@ distclean: clean  $(objects): ../../Makefile.settings Makefile -$(objects): %.o: %.c +$(objects): %.o: $(SRCDIR)%.c  	@echo '*' Compiling $<  	@$(CC) -c $(CFLAGS) $< -o $@ diff --git a/protocols/oscar/oscar.c b/protocols/oscar/oscar.c index 9602a496..acae6433 100644 --- a/protocols/oscar/oscar.c +++ b/protocols/oscar/oscar.c @@ -253,8 +253,6 @@ static char *normalize(const char *s)  	g_return_val_if_fail((s != NULL), NULL);  	u = t = g_strdup(s); - -	strcpy(t, s);  	g_strdown(t);  	while (*t && (x < BUF_LEN - 1)) { @@ -289,7 +287,7 @@ static gboolean oscar_callback(gpointer data, gint source,  	odata = (struct oscar_data *)ic->proto_data; -	if (condition & GAIM_INPUT_READ) { +	if (condition & B_EV_IO_READ) {  		if (aim_get_command(odata->sess, conn) >= 0) {  			aim_rxdispatch(odata->sess);                                 if (odata->killme) @@ -361,7 +359,7 @@ static gboolean oscar_login_connect(gpointer data, gint source, b_input_conditio  	}  	aim_conn_completeconnect(sess, conn); -	ic->inpa = b_input_add(conn->fd, GAIM_INPUT_READ, +	ic->inpa = b_input_add(conn->fd, B_EV_IO_READ,  			oscar_callback, conn);  	return FALSE; @@ -492,7 +490,7 @@ static gboolean oscar_bos_connect(gpointer data, gint source, b_input_condition  	}  	aim_conn_completeconnect(sess, bosconn); -	ic->inpa = b_input_add(bosconn->fd, GAIM_INPUT_READ, +	ic->inpa = b_input_add(bosconn->fd, B_EV_IO_READ,  			oscar_callback, bosconn);  	imcb_log(ic, _("Connection established, cookie sent")); @@ -651,6 +649,7 @@ static int gaim_parse_logout(aim_session_t *sess, aim_frame_t *fr, ...) {  static int conninitdone_chat(aim_session_t *sess, aim_frame_t *fr, ...) {  	struct im_connection *ic = sess->aux_data;  	struct chat_connection *chatcon; +	struct groupchat *c = NULL;  	static int id = 1;  	aim_conn_addhandler(sess, fr->conn, 0x000e, 0x0001, gaim_parse_genericerr, 0); @@ -663,7 +662,12 @@ static int conninitdone_chat(aim_session_t *sess, aim_frame_t *fr, ...) {  	chatcon = find_oscar_chat_by_conn(ic, fr->conn);  	chatcon->id = id; -	chatcon->cnv = imcb_chat_new(ic, chatcon->show); +	 +	c = bee_chat_by_title(ic->bee, ic, chatcon->show); +	if (c && !c->data) +		chatcon->cnv = c; +	else +		chatcon->cnv = imcb_chat_new(ic, chatcon->show);  	chatcon->cnv->data = chatcon;  	return 1; @@ -702,7 +706,7 @@ static gboolean oscar_chatnav_connect(gpointer data, gint source, b_input_condit  	}  	aim_conn_completeconnect(sess, tstconn); -	odata->cnpa = b_input_add(tstconn->fd, GAIM_INPUT_READ, +	odata->cnpa = b_input_add(tstconn->fd, B_EV_IO_READ,  					oscar_callback, tstconn);  	return FALSE; @@ -730,7 +734,7 @@ static gboolean oscar_auth_connect(gpointer data, gint source, b_input_condition  	}  	aim_conn_completeconnect(sess, tstconn); -	odata->paspa = b_input_add(tstconn->fd, GAIM_INPUT_READ, +	odata->paspa = b_input_add(tstconn->fd, B_EV_IO_READ,  				oscar_callback, tstconn);  	return FALSE; @@ -766,7 +770,7 @@ static gboolean oscar_chat_connect(gpointer data, gint source, b_input_condition  	aim_conn_completeconnect(sess, ccon->conn);  	ccon->inpa = b_input_add(tstconn->fd, -			GAIM_INPUT_READ, +			B_EV_IO_READ,  			oscar_callback, tstconn);  	odata->oscar_chats = g_slist_append(odata->oscar_chats, ccon); @@ -933,7 +937,7 @@ static int gaim_parse_oncoming(aim_session_t *sess, aim_frame_t *fr, ...) {  	tmp = normalize(info->sn);  	imcb_buddy_status(ic, tmp, flags, state_string, NULL); -	/* imcb_buddy_times(ic, tmp, signon, time_idle); */ +	imcb_buddy_times(ic, tmp, signon, time_idle);  	return 1; @@ -1059,8 +1063,7 @@ static void gaim_icq_authgrant(void *data_) {  	message = 0;  	aim_ssi_auth_reply(od->sess, od->conn, uin, 1, "");  	// aim_send_im_ch4(od->sess, uin, AIM_ICQMSG_AUTHGRANTED, &message); -	if(imcb_find_buddy(data->ic, uin) == NULL) -		imcb_ask_add(data->ic, uin, NULL); +	imcb_ask_add(data->ic, uin, NULL);  	g_free(uin);  	g_free(data); @@ -1821,11 +1824,13 @@ static void oscar_get_info(struct im_connection *g, char *name) {  static void oscar_get_away(struct im_connection *g, char *who) {  	struct oscar_data *odata = (struct oscar_data *)g->proto_data;  	if (odata->icq) { +		/** FIXME(wilmer): Hmm, lost the ability to get away msgs here, do we care to get that back?  		struct buddy *budlight = imcb_find_buddy(g, who);  		if (budlight)  			if ((budlight->uc & 0xff80) >> 7)  				if (budlight->caps & AIM_CAPS_ICQSERVERRELAY)  					aim_send_im_ch2_geticqmessage(odata->sess, who, (budlight->uc & 0xff80) >> 7); +		*/  	} else  		aim_getinfo(odata->sess, odata->conn, who, AIM_GETINFO_AWAYMESSAGE);  } @@ -1952,7 +1957,7 @@ static int gaim_ssi_parserights(aim_session_t *sess, aim_frame_t *fr, ...) {  static int gaim_ssi_parselist(aim_session_t *sess, aim_frame_t *fr, ...) {  	struct im_connection *ic = sess->aux_data; -	struct aim_ssi_item *curitem; +	struct aim_ssi_item *curitem, *curgroup;  	int tmp;  	char *nrm; @@ -1963,13 +1968,13 @@ static int gaim_ssi_parselist(aim_session_t *sess, aim_frame_t *fr, ...) {  		switch (curitem->type) {  			case 0x0000: /* Buddy */ -				if ((curitem->name) && (!imcb_find_buddy(ic, nrm))) { +				if ((curitem->name) && (!imcb_buddy_by_handle(ic, nrm))) {  					char *realname = NULL;  					if (curitem->data && aim_gettlv(curitem->data, 0x0131, 1))  						    realname = aim_gettlv_str(curitem->data, 0x0131, 1); -						 -					imcb_add_buddy(ic, nrm, NULL); +					 +					imcb_add_buddy(ic, nrm, curgroup->gid == curitem->gid ? curgroup->name : NULL);  					if (realname) {  						imcb_buddy_nick_hint(ic, nrm, realname); @@ -1979,6 +1984,10 @@ static int gaim_ssi_parselist(aim_session_t *sess, aim_frame_t *fr, ...) {  				}  				break; +			case 0x0001: /* Group */ +				curgroup = curitem; +				break; +  			case 0x0002: /* Permit buddy */  				if (curitem->name) {  					GSList *list; @@ -2519,12 +2528,13 @@ struct groupchat *oscar_chat_with(struct im_connection * ic, char *who)  	struct groupchat *ret;  	static int chat_id = 0;  	char * chatname; +	struct groupchat *c;  	chatname = g_strdup_printf("%s%s_%d", isdigit(*ic->acc->user) ? "icq_" : "",  	                           ic->acc->user, chat_id++); -   +	 +	c = imcb_chat_new(ic, chatname);  	ret = oscar_chat_join(ic, chatname, NULL, NULL); -  	aim_chat_invite(od->sess, od->conn, who, "", 4, chatname, 0x0);  	g_free(chatname); diff --git a/protocols/purple/Makefile b/protocols/purple/Makefile new file mode 100644 index 00000000..97a5bb6a --- /dev/null +++ b/protocols/purple/Makefile @@ -0,0 +1,44 @@ +########################### +## Makefile for BitlBee  ## +##                       ## +## Copyright 2002 Lintux ## +########################### + +### DEFINITIONS + +-include ../../Makefile.settings +ifdef SRCDIR +SRCDIR := $(SRCDIR)protocols/purple/ +endif + +# [SH] Program variables +objects = ft.o purple.o + +CFLAGS += -Wall $(PURPLE_CFLAGS) +LFLAGS += -r + +# [SH] Phony targets +all: purple_mod.o +check: all +lcov: check +gcov:  +	gcov *.c + +.PHONY: all clean distclean + +clean: +	rm -f *.o core + +distclean: clean + +### MAIN PROGRAM + +$(objects): ../../Makefile.settings Makefile + +$(objects): %.o: $(SRCDIR)%.c +	@echo '*' Compiling $< +	@$(CC) -c $(CFLAGS) $< -o $@ + +purple_mod.o: $(objects) +	@echo '*' Linking purple_mod.o +	$(LD) $(LFLAGS) $(objects) -o purple_mod.o diff --git a/protocols/purple/ft-direct.c b/protocols/purple/ft-direct.c new file mode 100644 index 00000000..98a16d75 --- /dev/null +++ b/protocols/purple/ft-direct.c @@ -0,0 +1,239 @@ +/***************************************************************************\ +*                                                                           * +*  BitlBee - An IRC to IM gateway                                           * +*  libpurple module - File transfer stuff                                   * +*                                                                           * +*  Copyright 2009-2010 Wilmer van der Gaast <wilmer@gaast.net>              * +*                                                                           * +*  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 02110-1301 USA.              * +*                                                                           * +\***************************************************************************/ + +/* This code tries to do direct file transfers, i.e. without caching the file +   locally on disk first. Since libpurple can only do this since version 2.6.0 +   and even then very unreliably (and not with all IM modules), I'm canning +   this code for now. */ + +#include "bitlbee.h" + +#include <stdarg.h> + +#include <glib.h> +#include <purple.h> + +struct prpl_xfer_data +{ +	PurpleXfer *xfer; +	file_transfer_t *ft; +	gint ready_timer; +	char *buf; +	int buf_len; +}; + +static file_transfer_t *next_ft; + +struct im_connection *purple_ic_by_pa( PurpleAccount *pa ); + +/* Glorious hack: We seem to have to remind at least some libpurple plugins +   that we're ready because this info may get lost if we give it too early. +   So just do it ten times a second. :-/ */ +static gboolean prplcb_xfer_write_request_cb( gpointer data, gint fd, b_input_condition cond ) +{ +	struct prpl_xfer_data *px = data; +	 +	purple_xfer_ui_ready( px->xfer ); +	 +	return purple_xfer_get_type( px->xfer ) == PURPLE_XFER_RECEIVE; +} + +static gboolean prpl_xfer_write_request( struct file_transfer *ft ) +{ +	struct prpl_xfer_data *px = ft->data; +	px->ready_timer = b_timeout_add( 100, prplcb_xfer_write_request_cb, px ); +	return TRUE; +} + +static gboolean prpl_xfer_write( struct file_transfer *ft, char *buffer, unsigned int len ) +{ +	struct prpl_xfer_data *px = ft->data; +	 +	px->buf = g_memdup( buffer, len ); +	px->buf_len = len; + +	//purple_xfer_ui_ready( px->xfer ); +	px->ready_timer = b_timeout_add( 0, prplcb_xfer_write_request_cb, px ); +	 +	return TRUE; +} + +static void prpl_xfer_accept( struct file_transfer *ft ) +{ +	struct prpl_xfer_data *px = ft->data; +	purple_xfer_request_accepted( px->xfer, NULL ); +	prpl_xfer_write_request( ft ); +} + +static void prpl_xfer_canceled( struct file_transfer *ft, char *reason ) +{ +	struct prpl_xfer_data *px = ft->data; +	purple_xfer_request_denied( px->xfer ); +} + +static gboolean prplcb_xfer_new_send_cb( gpointer data, gint fd, b_input_condition cond ) +{ +	PurpleXfer *xfer = data; +	struct im_connection *ic = purple_ic_by_pa( xfer->account ); +	struct prpl_xfer_data *px = g_new0( struct prpl_xfer_data, 1 ); +	PurpleBuddy *buddy; +	const char *who; +	 +	buddy = purple_find_buddy( xfer->account, xfer->who ); +	who = buddy ? purple_buddy_get_name( buddy ) : xfer->who; +	 +	/* TODO(wilmer): After spreading some more const goodness in BitlBee, +	   remove the evil cast below. */ +	px->ft = imcb_file_send_start( ic, (char*) who, xfer->filename, xfer->size ); +	px->ft->data = px; +	px->xfer = data; +	px->xfer->ui_data = px; +	 +	px->ft->accept = prpl_xfer_accept; +	px->ft->canceled = prpl_xfer_canceled; +	px->ft->write_request = prpl_xfer_write_request; +	 +	return FALSE; +} + +static void prplcb_xfer_new( PurpleXfer *xfer ) +{ +	if( purple_xfer_get_type( xfer ) == PURPLE_XFER_RECEIVE ) +	{ +		/* This should suppress the stupid file dialog. */ +		purple_xfer_set_local_filename( xfer, "/tmp/wtf123" ); +		 +		/* Sadly the xfer struct is still empty ATM so come back after +		   the caller is done. */ +		b_timeout_add( 0, prplcb_xfer_new_send_cb, xfer ); +	} +	else +	{ +		struct prpl_xfer_data *px = g_new0( struct prpl_xfer_data, 1 ); +		 +		px->ft = next_ft; +		px->ft->data = px; +		px->xfer = xfer; +		px->xfer->ui_data = px; +		 +		purple_xfer_set_filename( xfer, px->ft->file_name ); +		purple_xfer_set_size( xfer, px->ft->file_size ); +		 +		next_ft = NULL; +	} +} + +static void prplcb_xfer_progress( PurpleXfer *xfer, double percent ) +{ +	fprintf( stderr, "prplcb_xfer_dbg 0x%p %f\n", xfer, percent ); +} + +static void prplcb_xfer_dbg( PurpleXfer *xfer ) +{ +	fprintf( stderr, "prplcb_xfer_dbg 0x%p\n", xfer ); +} + +static gssize prplcb_xfer_write( PurpleXfer *xfer, const guchar *buffer, gssize size ) +{ +	struct prpl_xfer_data *px = xfer->ui_data; +	gboolean st; +	 +	fprintf( stderr, "xfer_write %d %d\n", size, px->buf_len ); +	 +	b_event_remove( px->ready_timer ); +	px->ready_timer = 0; +	 +	st = px->ft->write( px->ft, (char*) buffer, size ); +	 +	if( st && xfer->bytes_remaining == size ) +		imcb_file_finished( px->ft ); +	 +	return st ? size : 0; +} + +gssize prplcb_xfer_read( PurpleXfer *xfer, guchar **buffer, gssize size ) +{ +	struct prpl_xfer_data *px = xfer->ui_data; +	 +	fprintf( stderr, "xfer_read %d %d\n", size, px->buf_len ); + +	if( px->buf ) +	{ +		*buffer = px->buf; +		px->buf = NULL; +		 +		px->ft->write_request( px->ft ); +		 +		return px->buf_len; +	} +	 +	return 0; +} + +PurpleXferUiOps bee_xfer_uiops = +{ +	prplcb_xfer_new, +	prplcb_xfer_dbg, +	prplcb_xfer_dbg, +	prplcb_xfer_progress, +	prplcb_xfer_dbg, +	prplcb_xfer_dbg, +	prplcb_xfer_write, +	prplcb_xfer_read, +	prplcb_xfer_dbg, +}; + +static gboolean prplcb_xfer_send_cb( gpointer data, gint fd, b_input_condition cond ); + +void purple_transfer_request( struct im_connection *ic, file_transfer_t *ft, char *handle ) +{ +	PurpleAccount *pa = ic->proto_data; +	struct prpl_xfer_data *px; +	 +	/* xfer_new() will pick up this variable. It's a hack but we're not +	   multi-threaded anyway. */ +	next_ft = ft; +	serv_send_file( purple_account_get_connection( pa ), handle, ft->file_name ); +	 +	ft->write = prpl_xfer_write; +	 +	px = ft->data; +	imcb_file_recv_start( ft ); +	 +	px->ready_timer = b_timeout_add( 100, prplcb_xfer_send_cb, px ); +} + +static gboolean prplcb_xfer_send_cb( gpointer data, gint fd, b_input_condition cond ) +{ +	struct prpl_xfer_data *px = data; +	 +	if( px->ft->status & FT_STATUS_TRANSFERRING ) +	{ +		fprintf( stderr, "The ft, it is ready...\n" ); +		px->ft->write_request( px->ft ); +		 +		return FALSE; +	} +	 +	return TRUE; +} diff --git a/protocols/purple/ft.c b/protocols/purple/ft.c new file mode 100644 index 00000000..c4efc657 --- /dev/null +++ b/protocols/purple/ft.c @@ -0,0 +1,355 @@ +/***************************************************************************\ +*                                                                           * +*  BitlBee - An IRC to IM gateway                                           * +*  libpurple module - File transfer stuff                                   * +*                                                                           * +*  Copyright 2009-2010 Wilmer van der Gaast <wilmer@gaast.net>              * +*                                                                           * +*  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 02110-1301 USA.              * +*                                                                           * +\***************************************************************************/ + +/* Do file transfers via disk for now, since libpurple was really designed +   for straight-to/from disk fts and is only just learning how to pass the +   file contents the the UI instead (2.6.0 and higher it seems, and with +   varying levels of success). */ + +#include "bitlbee.h" + +#include <stdarg.h> + +#include <glib.h> +#include <purple.h> + +struct prpl_xfer_data +{ +	PurpleXfer *xfer; +	file_transfer_t *ft; +	struct im_connection *ic; +	int fd; +	char *fn, *handle; +	gboolean ui_wants_data; +}; + +static file_transfer_t *next_ft; + +struct im_connection *purple_ic_by_pa( PurpleAccount *pa ); +static gboolean prplcb_xfer_new_send_cb( gpointer data, gint fd, b_input_condition cond ); +static gboolean prpl_xfer_write_request( struct file_transfer *ft ); + + +/* Receiving files (IM->UI): */ +static void prpl_xfer_accept( struct file_transfer *ft ) +{ +	struct prpl_xfer_data *px = ft->data; +	purple_xfer_request_accepted( px->xfer, NULL ); +	prpl_xfer_write_request( ft ); +} + +static void prpl_xfer_canceled( struct file_transfer *ft, char *reason ) +{ +	struct prpl_xfer_data *px = ft->data; +	purple_xfer_request_denied( px->xfer ); +} + +static void prplcb_xfer_new( PurpleXfer *xfer ) +{ +	if( purple_xfer_get_type( xfer ) == PURPLE_XFER_RECEIVE ) +	{ +		struct prpl_xfer_data *px = g_new0( struct prpl_xfer_data, 1 ); +		 +		xfer->ui_data = px; +		px->xfer = xfer; +		px->fn = mktemp( g_strdup( "/tmp/bitlbee-purple-ft.XXXXXX" ) ); +		px->fd = -1; +		px->ic = purple_ic_by_pa( xfer->account ); +		 +		purple_xfer_set_local_filename( xfer, px->fn ); +		 +		/* Sadly the xfer struct is still empty ATM so come back after +		   the caller is done. */ +		b_timeout_add( 0, prplcb_xfer_new_send_cb, xfer ); +	} +	else +	{ +		struct file_transfer *ft = next_ft; +		struct prpl_xfer_data *px = ft->data; +		 +		xfer->ui_data = px; +		px->xfer = xfer; +		 +		next_ft = NULL; +	} +} + +static gboolean prplcb_xfer_new_send_cb( gpointer data, gint fd, b_input_condition cond ) +{ +	PurpleXfer *xfer = data; +	struct im_connection *ic = purple_ic_by_pa( xfer->account ); +	struct prpl_xfer_data *px = xfer->ui_data; +	PurpleBuddy *buddy; +	const char *who; +	 +	buddy = purple_find_buddy( xfer->account, xfer->who ); +	who = buddy ? purple_buddy_get_name( buddy ) : xfer->who; +	 +	/* TODO(wilmer): After spreading some more const goodness in BitlBee, +	   remove the evil cast below. */ +	px->ft = imcb_file_send_start( ic, (char*) who, xfer->filename, xfer->size ); +	px->ft->data = px; +	 +	px->ft->accept = prpl_xfer_accept; +	px->ft->canceled = prpl_xfer_canceled; +	px->ft->write_request = prpl_xfer_write_request; +	 +	return FALSE; +} + +gboolean try_write_to_ui( gpointer data, gint fd, b_input_condition cond ) +{ +	struct file_transfer *ft = data; +	struct prpl_xfer_data *px = ft->data; +	struct stat fs; +	off_t tx_bytes; +	 +	/* If we don't have the file opened yet, there's no data so wait. */ +	if( px->fd < 0 || !px->ui_wants_data ) +		return FALSE; +	 +	tx_bytes = lseek( px->fd, 0, SEEK_CUR ); +	fstat( px->fd, &fs ); +	 +	if( fs.st_size > tx_bytes ) +	{ +		char buf[1024]; +		size_t n = MIN( fs.st_size - tx_bytes, sizeof( buf ) ); +		 +		if( read( px->fd, buf, n ) == n && ft->write( ft, buf, n ) ) +		{ +			px->ui_wants_data = FALSE; +		} +		else +		{ +			purple_xfer_cancel_local( px->xfer ); +			imcb_file_canceled( px->ic, ft, "Read error" ); +		} +	} +	 +	if( lseek( px->fd, 0, SEEK_CUR ) == px->xfer->size ) +	{ +		/*purple_xfer_end( px->xfer );*/ +		imcb_file_finished( px->ic, ft ); +	} +	 +	return FALSE; +} + +/* UI calls this when its buffer is empty and wants more data to send to the user. */ +static gboolean prpl_xfer_write_request( struct file_transfer *ft ) +{ +	struct prpl_xfer_data *px = ft->data; +	 +	px->ui_wants_data = TRUE; +	try_write_to_ui( ft, 0, 0 ); +	 +	return FALSE; +} + + +/* Generic (IM<>UI): */ +static void prplcb_xfer_destroy( PurpleXfer *xfer ) +{ +	struct prpl_xfer_data *px = xfer->ui_data; +	 +	g_free( px->fn ); +	g_free( px->handle ); +	if( px->fd >= 0 ) +		close( px->fd ); +	g_free( px ); +} + +static void prplcb_xfer_progress( PurpleXfer *xfer, double percent ) +{ +	struct prpl_xfer_data *px = xfer->ui_data; +	 +	if( px == NULL ) +		return; +	 +	if( purple_xfer_get_type( xfer ) == PURPLE_XFER_SEND ) +	{ +		if( *px->fn ) +		{ +			char *slash; +			 +			unlink( px->fn ); +			if( ( slash = strrchr( px->fn, '/' ) ) ) +			{ +				*slash = '\0'; +				rmdir( px->fn ); +			} +			*px->fn = '\0'; +		} +		 +		return; +	} +	 +	if( px->fd == -1 && percent > 0 ) +	{ +		/* Weeeeeeeee, we're getting data! That means the file exists +		   by now so open it and start sending to the UI. */ +		px->fd = open( px->fn, O_RDONLY ); +		 +		/* Unlink it now, because we don't need it after this. */ +		unlink( px->fn ); +	} +	 +	if( percent < 1 ) +		try_write_to_ui( px->ft, 0, 0 ); +	else +		/* Another nice problem: If we have the whole file, it only +		   gets closed when we return. Problem: There may still be +		   stuff buffered and not written, we'll only see it after +		   the caller close()s the file. So poll the file after that. */ +		b_timeout_add( 0, try_write_to_ui, px->ft ); +} + +static void prplcb_xfer_cancel_remote( PurpleXfer *xfer ) +{ +	struct prpl_xfer_data *px = xfer->ui_data; +	 +	if( px->ft ) +		imcb_file_canceled( px->ic, px->ft, "Canceled by remote end" ); +	else +		/* px->ft == NULL for sends, because of the two stages. :-/ */ +		imcb_error( px->ic, "File transfer cancelled by remote end" ); +} + +static void prplcb_xfer_dbg( PurpleXfer *xfer ) +{ +	fprintf( stderr, "prplcb_xfer_dbg 0x%p\n", xfer ); +} + + +/* Sending files (UI->IM): */ +static gboolean prpl_xfer_write( struct file_transfer *ft, char *buffer, unsigned int len ); +static gboolean purple_transfer_request_cb( gpointer data, gint fd, b_input_condition cond ); + +void purple_transfer_request( struct im_connection *ic, file_transfer_t *ft, char *handle ) +{ +	struct prpl_xfer_data *px = g_new0( struct prpl_xfer_data, 1 ); +	char *dir, *basename; +	 +	ft->data = px; +	px->ft = ft; +	 +	dir = g_strdup( "/tmp/bitlbee-purple-ft.XXXXXX" ); +	if( !mkdtemp( dir ) ) +	{ +		imcb_error( ic, "Could not create temporary file for file transfer" ); +		g_free( px ); +		g_free( dir ); +		return; +	} +	 +	if( ( basename = strrchr( ft->file_name, '/' ) ) ) +		basename++; +	else +		basename = ft->file_name; +	px->fn = g_strdup_printf( "%s/%s", dir, basename ); +	px->fd = open( px->fn, O_WRONLY | O_CREAT, 0600 ); +	g_free( dir ); +	 +	if( px->fd < 0 ) +	{ +		imcb_error( ic, "Could not create temporary file for file transfer" ); +		g_free( px ); +		g_free( px->fn ); +		return; +	} +	 +	px->ic = ic; +	px->handle = g_strdup( handle ); +	 +	imcb_log( ic, "Due to libpurple limitations, the file has to be cached locally before proceeding with the actual file transfer. Please wait..." ); +	 +	b_timeout_add( 0, purple_transfer_request_cb, ft ); +} + +static void purple_transfer_forward( struct file_transfer *ft ) +{ +	struct prpl_xfer_data *px = ft->data; +	PurpleAccount *pa = px->ic->proto_data; +	 +	/* xfer_new() will pick up this variable. It's a hack but we're not +	   multi-threaded anyway. */ +	next_ft = ft; +	serv_send_file( purple_account_get_connection( pa ), px->handle, px->fn ); +} + +static gboolean purple_transfer_request_cb( gpointer data, gint fd, b_input_condition cond ) +{ +	file_transfer_t *ft = data; +	struct prpl_xfer_data *px = ft->data; +	 +	if( ft->write == NULL ) +	{ +		ft->write = prpl_xfer_write; +		imcb_file_recv_start( px->ic, ft ); +	} +	 +	ft->write_request( ft ); +	 +	return FALSE; +} + +static gboolean prpl_xfer_write( struct file_transfer *ft, char *buffer, unsigned int len ) +{ +	struct prpl_xfer_data *px = ft->data; +	 +	if( write( px->fd, buffer, len ) != len ) +	{ +		imcb_file_canceled( px->ic, ft, "Error while writing temporary file" ); +		return FALSE; +	} +	 +	if( lseek( px->fd, 0, SEEK_CUR ) >= ft->file_size ) +	{ +		close( px->fd ); +		px->fd = -1; +		 +		purple_transfer_forward( ft ); +		imcb_file_finished( px->ic, ft ); +		px->ft = NULL; +	} +	else +		b_timeout_add( 0, purple_transfer_request_cb, ft ); +	 +	return TRUE; +} + + + +PurpleXferUiOps bee_xfer_uiops = +{ +	prplcb_xfer_new, +	prplcb_xfer_destroy, +	NULL, /* prplcb_xfer_add, */ +	prplcb_xfer_progress, +	prplcb_xfer_dbg, +	prplcb_xfer_cancel_remote, +	NULL, +	NULL, +	prplcb_xfer_dbg, +}; diff --git a/protocols/purple/purple.c b/protocols/purple/purple.c new file mode 100644 index 00000000..2804fdf3 --- /dev/null +++ b/protocols/purple/purple.c @@ -0,0 +1,1152 @@ +/***************************************************************************\ +*                                                                           * +*  BitlBee - An IRC to IM gateway                                           * +*  libpurple module - Main file                                             * +*                                                                           * +*  Copyright 2009-2010 Wilmer van der Gaast <wilmer@gaast.net>              * +*                                                                           * +*  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 02110-1301 USA.              * +*                                                                           * +\***************************************************************************/ + +#include "bitlbee.h" +#include "help.h" + +#include <stdarg.h> + +#include <glib.h> +#include <purple.h> + +GSList *purple_connections; + +/* This makes me VERY sad... :-( But some libpurple callbacks come in without +   any context so this is the only way to get that. Don't want to support +   libpurple in daemon mode anyway. */ +static bee_t *local_bee; + +static char *set_eval_display_name( set_t *set, char *value ); + +struct im_connection *purple_ic_by_pa( PurpleAccount *pa ) +{ +	GSList *i; +	 +	for( i = purple_connections; i; i = i->next ) +		if( ((struct im_connection *)i->data)->proto_data == pa ) +			return i->data; +	 +	return NULL; +} + +static struct im_connection *purple_ic_by_gc( PurpleConnection *gc ) +{ +	return purple_ic_by_pa( purple_connection_get_account( gc ) ); +} + +static gboolean purple_menu_cmp( const char *a, const char *b ) +{ +	while( *a && *b ) +	{ +		while( *a == '_' ) a ++; +		while( *b == '_' ) b ++; +		if( tolower( *a ) != tolower( *b ) ) +			return FALSE; +		 +		a ++; +		b ++; +	} +	 +	return ( *a == '\0' && *b == '\0' ); +} + +static void purple_init( account_t *acc ) +{ +	PurplePlugin *prpl = purple_plugins_find_with_id( (char*) acc->prpl->data ); +	PurplePluginProtocolInfo *pi = prpl->info->extra_info; +	PurpleAccount *pa; +	GList *i, *st; +	set_t *s; +	char help_title[64]; +	GString *help; +	 +	help = g_string_new( "" ); +	g_string_printf( help, "BitlBee libpurple module %s (%s).\n\nSupported settings:", +	                        (char*) acc->prpl->name, prpl->info->name ); +	 +	/* Convert all protocol_options into per-account setting variables. */ +	for( i = pi->protocol_options; i; i = i->next ) +	{ +		PurpleAccountOption *o = i->data; +		const char *name; +		char *def = NULL; +		set_eval eval = NULL; +		void *eval_data = NULL; +		GList *io = NULL; +		GSList *opts = NULL; +		 +		name = purple_account_option_get_setting( o ); +		 +		switch( purple_account_option_get_type( o ) ) +		{ +		case PURPLE_PREF_STRING: +			def = g_strdup( purple_account_option_get_default_string( o ) ); +			 +			g_string_append_printf( help, "\n* %s (%s), %s, default: %s", +			                        name, purple_account_option_get_text( o ), +			                        "string", def ); +			 +			break; +		 +		case PURPLE_PREF_INT: +			def = g_strdup_printf( "%d", purple_account_option_get_default_int( o ) ); +			eval = set_eval_int; +			 +			g_string_append_printf( help, "\n* %s (%s), %s, default: %s", +			                        name, purple_account_option_get_text( o ), +			                        "integer", def ); +			 +			break; +		 +		case PURPLE_PREF_BOOLEAN: +			if( purple_account_option_get_default_bool( o ) ) +				def = g_strdup( "true" ); +			else +				def = g_strdup( "false" ); +			eval = set_eval_bool; +			 +			g_string_append_printf( help, "\n* %s (%s), %s, default: %s", +			                        name, purple_account_option_get_text( o ), +			                        "boolean", def ); +			 +			break; +		 +		case PURPLE_PREF_STRING_LIST: +			def = g_strdup( purple_account_option_get_default_list_value( o ) ); +			 +			g_string_append_printf( help, "\n* %s (%s), %s, default: %s", +			                        name, purple_account_option_get_text( o ), +			                        "list", def ); +			g_string_append( help, "\n  Possible values: " ); +			 +			for( io = purple_account_option_get_list( o ); io; io = io->next ) +			{ +				PurpleKeyValuePair *kv = io->data; +				opts = g_slist_append( opts, kv->value ); +				/* TODO: kv->value is not a char*, WTF? */ +				if( strcmp( kv->value, kv->key ) != 0 ) +					g_string_append_printf( help, "%s (%s), ", (char*) kv->value, kv->key ); +				else +					g_string_append_printf( help, "%s, ", (char*) kv->value ); +			} +			g_string_truncate( help, help->len - 2 ); +			eval = set_eval_list; +			eval_data = opts; +			 +			break; +			 +		default: +			/** No way to talk to the user right now, invent one when +			this becomes important. +			irc_usermsg( acc->irc, "Setting with unknown type: %s (%d) Expect stuff to break..\n", +			             name, purple_account_option_get_type( o ) ); +			*/ +			name = NULL; +		} +		 +		if( name != NULL ) +		{ +			s = set_add( &acc->set, name, def, eval, acc ); +			s->flags |= ACC_SET_OFFLINE_ONLY; +			s->eval_data = eval_data; +			g_free( def ); +		} +	} +	 +	g_snprintf( help_title, sizeof( help_title ), "purple %s", (char*) acc->prpl->name ); +	help_add_mem( &global.help, help_title, help->str ); +	g_string_free( help, TRUE ); +	 +	s = set_add( &acc->set, "display_name", NULL, set_eval_display_name, acc ); +	s->flags |= ACC_SET_ONLINE_ONLY; +	 +	if( pi->options & OPT_PROTO_MAIL_CHECK ) +	{ +		s = set_add( &acc->set, "mail_notifications", "false", set_eval_bool, acc ); +		s->flags |= ACC_SET_OFFLINE_ONLY; +	} +	 +	/* Go through all away states to figure out if away/status messages +	   are possible. */ +	pa = purple_account_new( acc->user, (char*) acc->prpl->data ); +	for( st = purple_account_get_status_types( pa ); st; st = st->next ) +	{ +		PurpleStatusPrimitive prim = purple_status_type_get_primitive( st->data ); +		 +		if( prim == PURPLE_STATUS_AVAILABLE ) +		{ +			if( purple_status_type_get_attr( st->data, "message" ) ) +				acc->flags |= ACC_FLAG_STATUS_MESSAGE; +		} +		else if( prim != PURPLE_STATUS_OFFLINE ) +		{ +			if( purple_status_type_get_attr( st->data, "message" ) ) +				acc->flags |= ACC_FLAG_AWAY_MESSAGE; +		} +	} +	purple_accounts_remove( pa ); +} + +static void purple_sync_settings( account_t *acc, PurpleAccount *pa ) +{ +	PurplePlugin *prpl = purple_plugins_find_with_id( pa->protocol_id ); +	PurplePluginProtocolInfo *pi = prpl->info->extra_info; +	GList *i; +	 +	for( i = pi->protocol_options; i; i = i->next ) +	{ +		PurpleAccountOption *o = i->data; +		const char *name; +		set_t *s; +		 +		name = purple_account_option_get_setting( o ); +		s = set_find( &acc->set, name ); +		if( s->value == NULL ) +			continue; +		 +		switch( purple_account_option_get_type( o ) ) +		{ +		case PURPLE_PREF_STRING: +		case PURPLE_PREF_STRING_LIST: +			purple_account_set_string( pa, name, set_getstr( &acc->set, name ) ); +			break; +		 +		case PURPLE_PREF_INT: +			purple_account_set_int( pa, name, set_getint( &acc->set, name ) ); +			break; +		 +		case PURPLE_PREF_BOOLEAN: +			purple_account_set_bool( pa, name, set_getbool( &acc->set, name ) ); +			break; +		 +		default: +			break; +		} +	} +	 +	if( pi->options & OPT_PROTO_MAIL_CHECK ) +		purple_account_set_check_mail( pa, set_getbool( &acc->set, "mail_notifications" ) ); +} + +static void purple_login( account_t *acc ) +{ +	struct im_connection *ic = imcb_new( acc ); +	PurpleAccount *pa; +	 +	if( local_bee != NULL && local_bee != acc->bee ) +	{ +		imcb_error( ic,  "Daemon mode detected. Do *not* try to use libpurple in daemon mode! " +		                 "Please use inetd or ForkDaemon mode instead." ); +		imc_logout( ic, FALSE ); +		return; +	} +	local_bee = acc->bee; +	 +	/* For now this is needed in the _connected() handlers if using +	   GLib event handling, to make sure we're not handling events +	   on dead connections. */ +	purple_connections = g_slist_prepend( purple_connections, ic ); +	 +	ic->proto_data = pa = purple_account_new( acc->user, (char*) acc->prpl->data ); +	purple_account_set_password( pa, acc->pass ); +	purple_sync_settings( acc, pa ); +	 +	purple_account_set_enabled( pa, "BitlBee", TRUE ); +} + +static void purple_logout( struct im_connection *ic ) +{ +	PurpleAccount *pa = ic->proto_data; +	 +	purple_account_set_enabled( pa, "BitlBee", FALSE ); +	purple_connections = g_slist_remove( purple_connections, ic ); +	purple_accounts_remove( pa ); +} + +static int purple_buddy_msg( struct im_connection *ic, char *who, char *message, int flags ) +{ +	PurpleConversation *conv; +	 +	if( ( conv = purple_find_conversation_with_account( PURPLE_CONV_TYPE_IM, +	                                                    who, ic->proto_data ) ) == NULL ) +	{ +		conv = purple_conversation_new( PURPLE_CONV_TYPE_IM, +		                                ic->proto_data, who ); +	} +	 +	purple_conv_im_send( purple_conversation_get_im_data( conv ), message ); +	 +	return 1; +} + +static GList *purple_away_states( struct im_connection *ic ) +{ +	PurpleAccount *pa = ic->proto_data; +	GList *st, *ret = NULL; +	 +	for( st = purple_account_get_status_types( pa ); st; st = st->next ) +	{ +		PurpleStatusPrimitive prim = purple_status_type_get_primitive( st->data ); +		if( prim != PURPLE_STATUS_AVAILABLE && prim != PURPLE_STATUS_OFFLINE ) +			ret = g_list_append( ret, (void*) purple_status_type_get_name( st->data ) ); +	} +	 +	return ret; +} + +static void purple_set_away( struct im_connection *ic, char *state_txt, char *message ) +{ +	PurpleAccount *pa = ic->proto_data; +	GList *status_types = purple_account_get_status_types( pa ), *st; +	PurpleStatusType *pst = NULL; +	GList *args = NULL; +	 +	for( st = status_types; st; st = st->next ) +	{ +		pst = st->data; +		 +		if( state_txt == NULL && +		    purple_status_type_get_primitive( pst ) == PURPLE_STATUS_AVAILABLE ) +			break; + +		if( state_txt != NULL && +		    g_strcasecmp( state_txt, purple_status_type_get_name( pst ) ) == 0 ) +			break; +	} +	 +	if( message && purple_status_type_get_attr( pst, "message" ) ) +	{ +		args = g_list_append( args, "message" ); +		args = g_list_append( args, message ); +	} +	 +	purple_account_set_status_list( pa, st ? purple_status_type_get_id( pst ) : "away", +		                        TRUE, args ); + +	g_list_free( args ); +} + +static char *set_eval_display_name( set_t *set, char *value ) +{ +	account_t *acc = set->data; +	struct im_connection *ic = acc->ic; +	 +	return NULL; +} + +static void purple_add_buddy( struct im_connection *ic, char *who, char *group ) +{ +	PurpleBuddy *pb; +	 +	pb = purple_buddy_new( (PurpleAccount*) ic->proto_data, who, NULL ); +	purple_blist_add_buddy( pb, NULL, NULL, NULL ); +	purple_account_add_buddy( (PurpleAccount*) ic->proto_data, pb ); +} + +static void purple_remove_buddy( struct im_connection *ic, char *who, char *group ) +{ +	PurpleBuddy *pb; +	 +	pb = purple_find_buddy( (PurpleAccount*) ic->proto_data, who ); +	if( pb != NULL ) +	{ +		purple_account_remove_buddy( (PurpleAccount*) ic->proto_data, pb, NULL ); +		purple_blist_remove_buddy( pb ); +	} +} + +static void purple_add_permit( struct im_connection *ic, char *who ) +{ +	PurpleAccount *pa = ic->proto_data; +	 +	purple_privacy_permit_add( pa, who, FALSE ); +} + +static void purple_add_deny( struct im_connection *ic, char *who ) +{ +	PurpleAccount *pa = ic->proto_data; +	 +	purple_privacy_deny_add( pa, who, FALSE ); +} + +static void purple_rem_permit( struct im_connection *ic, char *who ) +{ +	PurpleAccount *pa = ic->proto_data; +	 +	purple_privacy_permit_remove( pa, who, FALSE ); +} + +static void purple_rem_deny( struct im_connection *ic, char *who ) +{ +	PurpleAccount *pa = ic->proto_data; +	 +	purple_privacy_deny_remove( pa, who, FALSE ); +} + +static void purple_get_info( struct im_connection *ic, char *who ) +{ +	serv_get_info( purple_account_get_connection( ic->proto_data ), who ); +} + +static void purple_keepalive( struct im_connection *ic ) +{ +} + +static int purple_send_typing( struct im_connection *ic, char *who, int flags ) +{ +	PurpleTypingState state = PURPLE_NOT_TYPING; +	PurpleConversation *conv; +	 +	if( flags & OPT_TYPING ) +		state = PURPLE_TYPING; +	else if( flags & OPT_THINKING ) +		state = PURPLE_TYPED; +	 +	if( ( conv = purple_find_conversation_with_account( PURPLE_CONV_TYPE_IM, +	                                                    who, ic->proto_data ) ) == NULL ) +	{ +		purple_conv_im_set_typing_state( purple_conversation_get_im_data( conv ), state ); +		return 1; +	} +	else +	{ +		return 0; +	} +} + +static void purple_chat_msg( struct groupchat *gc, char *message, int flags ) +{ +	PurpleConversation *pc = gc->data; +	 +	purple_conv_chat_send( purple_conversation_get_chat_data( pc ), message ); +} + +struct groupchat *purple_chat_with( struct im_connection *ic, char *who ) +{ +	/* No, "of course" this won't work this way. Or in fact, it almost +	   does, but it only lets you send msgs to it, you won't receive +	   any. Instead, we have to click the virtual menu item. +	PurpleAccount *pa = ic->proto_data; +	PurpleConversation *pc; +	PurpleConvChat *pcc; +	struct groupchat *gc; +	 +	gc = imcb_chat_new( ic, "BitlBee-libpurple groupchat" ); +	gc->data = pc = purple_conversation_new( PURPLE_CONV_TYPE_CHAT, pa, "BitlBee-libpurple groupchat" ); +	pc->ui_data = gc; +	 +	pcc = PURPLE_CONV_CHAT( pc ); +	purple_conv_chat_add_user( pcc, ic->acc->user, "", 0, TRUE ); +	purple_conv_chat_invite_user( pcc, who, "Please join my chat", FALSE ); +	//purple_conv_chat_add_user( pcc, who, "", 0, TRUE ); +	*/ +	 +	/* There went my nice afternoon. :-( */ +	 +	PurpleAccount *pa = ic->proto_data; +	PurplePlugin *prpl = purple_plugins_find_with_id( pa->protocol_id ); +	PurplePluginProtocolInfo *pi = prpl->info->extra_info; +	PurpleBuddy *pb = purple_find_buddy( (PurpleAccount*) ic->proto_data, who ); +	PurpleMenuAction *mi; +	GList *menu; +	void (*callback)(PurpleBlistNode *, gpointer); /* FFFFFFFFFFFFFUUUUUUUUUUUUUU */ +	 +	if( !pb || !pi || !pi->blist_node_menu ) +		return NULL; +	 +	menu = pi->blist_node_menu( &pb->node ); +	while( menu ) +	{ +		mi = menu->data; +		if( purple_menu_cmp( mi->label, "initiate chat" ) || +		    purple_menu_cmp( mi->label, "initiate conference" ) ) +			break; +		menu = menu->next; +	} +	 +	if( menu == NULL ) +		return NULL; +	 +	/* Call the fucker. */ +	callback = (void*) mi->callback; +	callback( &pb->node, menu->data ); +	 +	return NULL; +} + +void purple_chat_invite( struct groupchat *gc, char *who, char *message ) +{ +	PurpleConversation *pc = gc->data; +	PurpleConvChat *pcc = PURPLE_CONV_CHAT( pc ); +	 +	serv_chat_invite( purple_account_get_connection( gc->ic->proto_data ), +	                  purple_conv_chat_get_id( pcc ),  +	                  message && *message ? message : "Please join my chat", +	                  who ); +} + +void purple_chat_leave( struct groupchat *gc ) +{ +	PurpleConversation *pc = gc->data; +	 +	purple_conversation_destroy( pc ); +} + +struct groupchat *purple_chat_join( struct im_connection *ic, const char *room, const char *nick, const char *password ) +{ +	PurpleAccount *pa = ic->proto_data; +	PurplePlugin *prpl = purple_plugins_find_with_id( pa->protocol_id ); +	PurplePluginProtocolInfo *pi = prpl->info->extra_info; +	GHashTable *chat_hash; +	PurpleConversation *conv; +	GList *info, *l; +	 +	if( !pi->chat_info || !pi->chat_info_defaults || +	    !( info = pi->chat_info( purple_account_get_connection( pa ) ) ) ) +	{ +		imcb_error( ic, "Joining chatrooms not supported by this protocol" ); +		return NULL; +	} +	 +	if( ( conv = purple_find_conversation_with_account( PURPLE_CONV_TYPE_CHAT, room, pa ) ) ) +		purple_conversation_destroy( conv ); +	 +	chat_hash = pi->chat_info_defaults( purple_account_get_connection( pa ), room ); +	 +	for( l = info; l; l = l->next ) +	{ +		struct proto_chat_entry *pce = l->data; +		 +		if( strcmp( pce->identifier, "handle" ) == 0 ) +			g_hash_table_replace( chat_hash, "handle", g_strdup( nick ) ); +		else if( strcmp( pce->identifier, "password" ) == 0 ) +			g_hash_table_replace( chat_hash, "password", g_strdup( password ) ); +		else if( strcmp( pce->identifier, "passwd" ) == 0 ) +			g_hash_table_replace( chat_hash, "passwd", g_strdup( password ) ); +	} +	 +	serv_join_chat( purple_account_get_connection( pa ), chat_hash ); +	 +	return NULL; +} + +void purple_transfer_request( struct im_connection *ic, file_transfer_t *ft, char *handle ); + +static void purple_ui_init(); + +GHashTable *prplcb_ui_info() +{ +	static GHashTable *ret; +	 +	if( ret == NULL ) +	{ +		ret = g_hash_table_new(g_str_hash, g_str_equal); +		g_hash_table_insert( ret, "name", "BitlBee" ); +		g_hash_table_insert( ret, "version", BITLBEE_VERSION ); +	} +	 +	return ret; +} + +static PurpleCoreUiOps bee_core_uiops =  +{ +	NULL, +	NULL, +	purple_ui_init, +	NULL, +	prplcb_ui_info, +}; + +static void prplcb_conn_progress( PurpleConnection *gc, const char *text, size_t step, size_t step_count ) +{ +	struct im_connection *ic = purple_ic_by_gc( gc ); +	 +	imcb_log( ic, "%s", text ); +} + +static void prplcb_conn_connected( PurpleConnection *gc ) +{ +	struct im_connection *ic = purple_ic_by_gc( gc ); +	const char *dn; +	set_t *s; +	 +	imcb_connected( ic ); +	 +	if( ( dn = purple_connection_get_display_name( gc ) ) && +	    ( s = set_find( &ic->acc->set, "display_name" ) ) ) +	{ +		g_free( s->value ); +		s->value = g_strdup( dn ); +	} +	 +	if( gc->flags & PURPLE_CONNECTION_HTML ) +		ic->flags |= OPT_DOES_HTML; +} + +static void prplcb_conn_disconnected( PurpleConnection *gc ) +{ +	struct im_connection *ic = purple_ic_by_gc( gc ); +	 +	if( ic != NULL ) +	{ +		imc_logout( ic, !gc->wants_to_die ); +	} +} + +static void prplcb_conn_notice( PurpleConnection *gc, const char *text ) +{ +	struct im_connection *ic = purple_ic_by_gc( gc ); +	 +	if( ic != NULL ) +		imcb_log( ic, "%s", text ); +} + +static void prplcb_conn_report_disconnect_reason( PurpleConnection *gc, PurpleConnectionError reason, const char *text ) +{ +	struct im_connection *ic = purple_ic_by_gc( gc ); +	 +	/* PURPLE_CONNECTION_ERROR_NAME_IN_USE means concurrent login, +	   should probably handle that. */ +	if( ic != NULL ) +		imcb_error( ic, "%s", text ); +} + +static PurpleConnectionUiOps bee_conn_uiops = +{ +	prplcb_conn_progress, +	prplcb_conn_connected, +	prplcb_conn_disconnected, +	prplcb_conn_notice, +	NULL, +	NULL, +	NULL, +	prplcb_conn_report_disconnect_reason, +}; + +static void prplcb_blist_new( PurpleBlistNode *node ) +{ +	PurpleBuddy *bud = (PurpleBuddy*) node; +	 +	if( node->type == PURPLE_BLIST_BUDDY_NODE ) +	{ +		struct im_connection *ic = purple_ic_by_pa( bud->account ); +		 +		if( ic == NULL ) +			return; +		 +		imcb_add_buddy( ic, bud->name, NULL ); +		if( bud->server_alias ) +		{ +			imcb_rename_buddy( ic, bud->name, bud->server_alias ); +			imcb_buddy_nick_hint( ic, bud->name, bud->server_alias ); +		} +	} +} + +static void prplcb_blist_update( PurpleBuddyList *list, PurpleBlistNode *node ) +{ +	PurpleBuddy *bud = (PurpleBuddy*) node; +	 +	if( node->type == PURPLE_BLIST_BUDDY_NODE ) +	{ +		struct im_connection *ic = purple_ic_by_pa( bud->account ); +		PurpleStatus *as; +		int flags = 0; +		 +		if( ic == NULL ) +			return; +		 +		if( bud->server_alias ) +			imcb_rename_buddy( ic, bud->name, bud->server_alias ); +		 +		flags |= purple_presence_is_online( bud->presence ) ? OPT_LOGGED_IN : 0; +		flags |= purple_presence_is_available( bud->presence ) ? 0 : OPT_AWAY; +		 +		as = purple_presence_get_active_status( bud->presence ); +		 +		imcb_buddy_status( ic, bud->name, flags, purple_status_get_name( as ), +		                   purple_status_get_attr_string( as, "message" ) ); +		 +		imcb_buddy_times( ic, bud->name, +		                  purple_presence_get_login_time( bud->presence ), +		                  purple_presence_get_idle_time( bud->presence ) ); +	} +} + +static void prplcb_blist_remove( PurpleBuddyList *list, PurpleBlistNode *node ) +{ +	/* +	PurpleBuddy *bud = (PurpleBuddy*) node; +	 +	if( node->type == PURPLE_BLIST_BUDDY_NODE ) +	{ +		struct im_connection *ic = purple_ic_by_pa( bud->account ); +		 +		if( ic == NULL ) +			return; +		 +		imcb_remove_buddy( ic, bud->name, NULL ); +	} +	*/ +} + +static PurpleBlistUiOps bee_blist_uiops = +{ +	NULL, +	prplcb_blist_new, +	NULL, +	prplcb_blist_update, +	prplcb_blist_remove, +}; + +void prplcb_conv_new( PurpleConversation *conv ) +{ +	if( conv->type == PURPLE_CONV_TYPE_CHAT ) +	{ +		struct im_connection *ic = purple_ic_by_pa( conv->account ); +		struct groupchat *gc; +		 +		gc = imcb_chat_new( ic, conv->name ); +		conv->ui_data = gc; +		gc->data = conv; +		 +		/* libpurple brokenness: Whatever. Show that we join right away, +		   there's no clear "This is you!" signaling in _add_users so +		   don't even try. */ +		imcb_chat_add_buddy( gc, gc->ic->acc->user ); +	} +} + +void prplcb_conv_free( PurpleConversation *conv ) +{ +	struct groupchat *gc = conv->ui_data; +	 +	imcb_chat_free( gc ); +} + +void prplcb_conv_add_users( PurpleConversation *conv, GList *cbuddies, gboolean new_arrivals ) +{ +	struct groupchat *gc = conv->ui_data; +	GList *b; +	 +	for( b = cbuddies; b; b = b->next ) +	{ +		PurpleConvChatBuddy *pcb = b->data; +		 +		imcb_chat_add_buddy( gc, pcb->name ); +	} +} + +void prplcb_conv_del_users( PurpleConversation *conv, GList *cbuddies ) +{ +	struct groupchat *gc = conv->ui_data; +	GList *b; +	 +	for( b = cbuddies; b; b = b->next ) +		imcb_chat_remove_buddy( gc, b->data, "" ); +} + +void prplcb_conv_chat_msg( PurpleConversation *conv, const char *who, const char *message, PurpleMessageFlags flags, time_t mtime ) +{ +	struct groupchat *gc = conv->ui_data; +	PurpleBuddy *buddy; +	 +	/* ..._SEND means it's an outgoing message, no need to echo those. */ +	if( flags & PURPLE_MESSAGE_SEND ) +		return; +	 +	buddy = purple_find_buddy( conv->account, who ); +	if( buddy != NULL ) +		who = purple_buddy_get_name( buddy ); +	 +	imcb_chat_msg( gc, who, (char*) message, 0, mtime ); +} + +static void prplcb_conv_im( PurpleConversation *conv, const char *who, const char *message, PurpleMessageFlags flags, time_t mtime ) +{ +	struct im_connection *ic = purple_ic_by_pa( conv->account ); +	PurpleBuddy *buddy; +	 +	/* ..._SEND means it's an outgoing message, no need to echo those. */ +	if( flags & PURPLE_MESSAGE_SEND ) +		return; +	 +	buddy = purple_find_buddy( conv->account, who ); +	if( buddy != NULL ) +		who = purple_buddy_get_name( buddy ); +	 +	imcb_buddy_msg( ic, (char*) who, (char*) message, 0, mtime ); +} + +static PurpleConversationUiOps bee_conv_uiops =  +{ +	prplcb_conv_new,           /* create_conversation  */ +	prplcb_conv_free,          /* destroy_conversation */ +	prplcb_conv_chat_msg,      /* write_chat           */ +	prplcb_conv_im,            /* write_im             */ +	NULL,                      /* write_conv           */ +	prplcb_conv_add_users,     /* chat_add_users       */ +	NULL,                      /* chat_rename_user     */ +	prplcb_conv_del_users,     /* 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         */ +}; + +struct prplcb_request_action_data +{ +	void *user_data, *bee_data; +	PurpleRequestActionCb yes, no; +	int yes_i, no_i; +}; + +static void prplcb_request_action_yes( void *data ) +{ +	struct prplcb_request_action_data *pqad = data; +	 +	pqad->yes( pqad->user_data, pqad->yes_i ); +	g_free( pqad ); +} + +static void prplcb_request_action_no( void *data ) +{ +	struct prplcb_request_action_data *pqad = data; +	 +	pqad->no( pqad->user_data, pqad->no_i ); +	g_free( pqad ); +} + +static void *prplcb_request_action( const char *title, const char *primary, const char *secondary, +                                    int default_action, PurpleAccount *account, const char *who, +                                    PurpleConversation *conv, void *user_data, size_t action_count, +                                    va_list actions ) +{ +	struct prplcb_request_action_data *pqad;  +	int i; +	char *q; +	 +	pqad = g_new0( struct prplcb_request_action_data, 1 ); +	 +	for( i = 0; i < action_count; i ++ ) +	{ +		char *caption; +		void *fn; +		 +		caption = va_arg( actions, char* ); +		fn = va_arg( actions, void* ); +		 +		if( strstr( caption, "Accept" ) ) +		{ +			pqad->yes = fn; +			pqad->yes_i = i; +		} +		else if( strstr( caption, "Reject" ) || strstr( caption, "Cancel" ) ) +		{ +			pqad->no = fn; +			pqad->no_i = i; +		} +	} +	 +	pqad->user_data = user_data; +	 +	/* TODO: IRC stuff here :-( */ +	q = g_strdup_printf( "Request: %s\n\n%s\n\n%s", title, primary, secondary ); +	pqad->bee_data = query_add( local_bee->ui_data, purple_ic_by_pa( account ), q, +		prplcb_request_action_yes, prplcb_request_action_no, pqad ); +	 +	g_free( q ); +	 +	return pqad; +} + +static PurpleRequestUiOps bee_request_uiops = +{ +	NULL, +	NULL, +	prplcb_request_action, +	NULL, +	NULL, +	NULL, +	NULL, +}; + +static void prplcb_privacy_permit_added( PurpleAccount *account, const char *name ) +{ +	struct im_connection *ic = purple_ic_by_pa( account ); +	 +	if( !g_slist_find_custom( ic->permit, name, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) +		ic->permit = g_slist_prepend( ic->permit, g_strdup( name ) ); +} + +static void prplcb_privacy_permit_removed( PurpleAccount *account, const char *name ) +{ +	struct im_connection *ic = purple_ic_by_pa( account ); +	void *n; +	 +	n = g_slist_find_custom( ic->permit, name, (GCompareFunc) ic->acc->prpl->handle_cmp ); +	ic->permit = g_slist_remove( ic->permit, n ); +} + +static void prplcb_privacy_deny_added( PurpleAccount *account, const char *name ) +{ +	struct im_connection *ic = purple_ic_by_pa( account ); +	 +	if( !g_slist_find_custom( ic->deny, name, (GCompareFunc) ic->acc->prpl->handle_cmp ) ) +		ic->deny = g_slist_prepend( ic->deny, g_strdup( name ) ); +} + +static void prplcb_privacy_deny_removed( PurpleAccount *account, const char *name ) +{ +	struct im_connection *ic = purple_ic_by_pa( account ); +	void *n; +	 +	n = g_slist_find_custom( ic->deny, name, (GCompareFunc) ic->acc->prpl->handle_cmp ); +	ic->deny = g_slist_remove( ic->deny, n ); +} + +static PurplePrivacyUiOps bee_privacy_uiops = +{ +	prplcb_privacy_permit_added, +	prplcb_privacy_permit_removed, +	prplcb_privacy_deny_added, +	prplcb_privacy_deny_removed, +}; + +static void prplcb_debug_print( PurpleDebugLevel level, const char *category, const char *arg_s ) +{ +	fprintf( stderr, "DEBUG %s: %s", category, arg_s ); +} + +static PurpleDebugUiOps bee_debug_uiops = +{ +	prplcb_debug_print, +}; + +static guint prplcb_ev_timeout_add( guint interval, GSourceFunc func, gpointer udata ) +{ +	return b_timeout_add( interval, (b_event_handler) func, udata ); +} + +static guint prplcb_ev_input_add( int fd, PurpleInputCondition cond, PurpleInputFunction func, gpointer udata ) +{ +	return b_input_add( fd, cond | B_EV_FLAG_FORCE_REPEAT, (b_event_handler) func, udata ); +} + +static gboolean prplcb_ev_remove( guint id ) +{ +	b_event_remove( (gint) id ); +	return TRUE; +} + +static PurpleEventLoopUiOps glib_eventloops =  +{ +	prplcb_ev_timeout_add, +	prplcb_ev_remove, +	prplcb_ev_input_add, +	prplcb_ev_remove, +}; + +static void *prplcb_notify_email( PurpleConnection *gc, const char *subject, const char *from, +                                  const char *to, const char *url ) +{ +	struct im_connection *ic = purple_ic_by_gc( gc ); +	 +	imcb_log( ic, "Received e-mail from %s for %s: %s <%s>", from, to, subject, url ); +	 +	return NULL; +} + +static void *prplcb_notify_userinfo( PurpleConnection *gc, const char *who, PurpleNotifyUserInfo *user_info ) +{ +	struct im_connection *ic = purple_ic_by_gc( gc ); +	GString *info = g_string_new( "" ); +	GList *l = purple_notify_user_info_get_entries( user_info ); +	char *key; +	const char *value; +	int n; +	 +	while( l ) +	{ +		PurpleNotifyUserInfoEntry *e = l->data; +		 +		switch( purple_notify_user_info_entry_get_type( e ) ) +		{ +		case PURPLE_NOTIFY_USER_INFO_ENTRY_PAIR: +		case PURPLE_NOTIFY_USER_INFO_ENTRY_SECTION_HEADER: +			key = g_strdup( purple_notify_user_info_entry_get_label( e ) ); +			value = purple_notify_user_info_entry_get_value( e ); +			 +			if( key ) +			{ +				strip_html( key ); +				g_string_append_printf( info, "%s: ", key ); +				 +				if( value ) +				{ +					n = strlen( value ) - 1; +					while( isspace( value[n] ) ) +						n --; +					g_string_append_len( info, value, n + 1 ); +				} +				g_string_append_c( info, '\n' ); +				g_free( key ); +			} +			 +			break; +		case PURPLE_NOTIFY_USER_INFO_ENTRY_SECTION_BREAK: +			g_string_append( info, "------------------------\n" ); +			break; +		} +		 +		l = l->next; +	} +	 +	imcb_log( ic, "User %s info:\n%s", who, info->str ); +	g_string_free( info, TRUE ); +	 +	return NULL; +} + +static PurpleNotifyUiOps bee_notify_uiops = +{ +        NULL, +        prplcb_notify_email, +        NULL, +        NULL, +        NULL, +        NULL, +        prplcb_notify_userinfo, +}; + +extern PurpleXferUiOps bee_xfer_uiops; + +static void purple_ui_init() +{ +	purple_blist_set_ui_ops( &bee_blist_uiops ); +	purple_connections_set_ui_ops( &bee_conn_uiops ); +	purple_conversations_set_ui_ops( &bee_conv_uiops ); +	purple_request_set_ui_ops( &bee_request_uiops ); +	purple_notify_set_ui_ops( &bee_notify_uiops ); +	purple_xfers_set_ui_ops( &bee_xfer_uiops ); +	purple_privacy_set_ui_ops( &bee_privacy_uiops ); +	 +	if( getenv( "BITLBEE_DEBUG" ) ) +		purple_debug_set_ui_ops( &bee_debug_uiops ); +} + +void purple_initmodule() +{ +	struct prpl funcs; +	GList *prots; +	GString *help; +	 +	if( B_EV_IO_READ != PURPLE_INPUT_READ || +	    B_EV_IO_WRITE != PURPLE_INPUT_WRITE ) +	{ +		/* FIXME FIXME FIXME FIXME FIXME :-) */ +		exit( 1 ); +	} +	 +	purple_util_set_user_dir("/tmp"); +	purple_debug_set_enabled(FALSE); +	purple_core_set_ui_ops(&bee_core_uiops); +	purple_eventloop_set_ui_ops(&glib_eventloops); +	if( !purple_core_init( "BitlBee") ) +	{ +		/* Initializing the core failed. Terminate. */ +		fprintf( stderr, "libpurple initialization failed.\n" ); +		abort(); +	} +	 +	/* This seems like stateful shit we don't want... */ +	purple_set_blist(purple_blist_new()); +	purple_blist_load(); +	 +	/* Meh? */ +	purple_prefs_load(); +	 +	memset( &funcs, 0, sizeof( funcs ) ); +	funcs.login = purple_login; +	funcs.init = purple_init; +	funcs.logout = purple_logout; +	funcs.buddy_msg = purple_buddy_msg; +	funcs.away_states = purple_away_states; +	funcs.set_away = purple_set_away; +	funcs.add_buddy = purple_add_buddy; +	funcs.remove_buddy = purple_remove_buddy; +	funcs.add_permit = purple_add_permit; +	funcs.add_deny = purple_add_deny; +	funcs.rem_permit = purple_rem_permit; +	funcs.rem_deny = purple_rem_deny; +	funcs.get_info = purple_get_info; +	funcs.keepalive = purple_keepalive; +	funcs.send_typing = purple_send_typing; +	funcs.handle_cmp = g_strcasecmp; +	/* TODO(wilmer): Set these only for protocols that support them? */ +	funcs.chat_msg = purple_chat_msg; +	funcs.chat_with = purple_chat_with; +	funcs.chat_invite = purple_chat_invite; +	funcs.chat_leave = purple_chat_leave; +	funcs.chat_join = purple_chat_join; +	funcs.transfer_request = purple_transfer_request; +	 +	help = g_string_new("BitlBee libpurple module supports the following IM protocols:\n"); +	 +	/* Add a protocol entry to BitlBee's structures for every protocol +	   supported by this libpurple instance. */	 +	for( prots = purple_plugins_get_protocols(); prots; prots = prots->next ) +	{ +		PurplePlugin *prot = prots->data; +		struct prpl *ret; +		 +		ret = g_memdup( &funcs, sizeof( funcs ) ); +		ret->name = ret->data = prot->info->id; +		if( strncmp( ret->name, "prpl-", 5 ) == 0 ) +			ret->name += 5; +		register_protocol( ret ); +		 +		g_string_append_printf( help, "\n* %s (%s)", ret->name, prot->info->name ); +		 +		/* libpurple doesn't define a protocol called OSCAR, but we +		   need it to be compatible with normal BitlBee. */ +		if( g_strcasecmp( prot->info->id, "prpl-aim" ) == 0 ) +		{ +			ret = g_memdup( &funcs, sizeof( funcs ) ); +			ret->name = "oscar"; +			ret->data = prot->info->id; +			register_protocol( ret ); +		} +	} +	 +	g_string_append( help, "\n\nFor used protocols, more information about available " +	                 "settings can be found using \x02help purple <protocol name>\x02" ); +	 +	/* Add a simple dynamically-generated help item listing all +	   the supported protocols. */ +	help_add_mem( &global.help, "purple", help->str ); +	g_string_free( help, TRUE ); +} diff --git a/protocols/twitter/Makefile b/protocols/twitter/Makefile index ca1e4695..8a4b97f9 100644 --- a/protocols/twitter/Makefile +++ b/protocols/twitter/Makefile @@ -7,6 +7,9 @@  ### DEFINITIONS  -include ../../Makefile.settings +ifdef SRCDIR +SRCDIR := $(SRCDIR)protocols/twitter/ +endif  # [SH] Program variables  objects = twitter.o twitter_http.o twitter_lib.o @@ -32,7 +35,7 @@ distclean: clean  $(objects): ../../Makefile.settings Makefile -$(objects): %.o: %.c +$(objects): %.o: $(SRCDIR)%.c  	@echo '*' Compiling $<  	@$(CC) -c $(CFLAGS) $< -o $@ diff --git a/protocols/twitter/twitter_lib.c b/protocols/twitter/twitter_lib.c index fb7a6af9..53928287 100644 --- a/protocols/twitter/twitter_lib.c +++ b/protocols/twitter/twitter_lib.c @@ -104,7 +104,7 @@ static void twitter_add_buddy(struct im_connection *ic, char *name, const char *  	struct twitter_data *td = ic->proto_data;  	// Check if the buddy is allready in the buddy list. -	if (!imcb_find_buddy( ic, name )) +	if (!bee_user_by_handle( ic->bee, ic, name ))  	{  		char *mode = set_getstr(&ic->acc->set, "mode"); diff --git a/protocols/yahoo/Makefile b/protocols/yahoo/Makefile index b4fe56e2..20ecce71 100644 --- a/protocols/yahoo/Makefile +++ b/protocols/yahoo/Makefile @@ -7,6 +7,9 @@  ### DEFINITIONS  -include ../../Makefile.settings +ifdef SRCDIR +SRCDIR := $(SRCDIR)protocols/yahoo/ +endif  # [SH] Program variables  objects = yahoo.o crypt.o libyahoo2.o yahoo_fn.o yahoo_httplib.o yahoo_util.o @@ -32,7 +35,7 @@ distclean: clean  $(objects): ../../Makefile.settings Makefile -$(objects): %.o: %.c +$(objects): %.o: $(SRCDIR)%.c  	@echo '*' Compiling $<  	@$(CC) -c $(CFLAGS) $< -o $@ diff --git a/protocols/yahoo/yahoo.c b/protocols/yahoo/yahoo.c index e4d541d5..bf577496 100644 --- a/protocols/yahoo/yahoo.c +++ b/protocols/yahoo/yahoo.c @@ -157,7 +157,7 @@ static void byahoo_logout( struct im_connection *ic )  	GSList *l;  	while( ic->groupchats ) -		imcb_chat_free( ic->groupchats ); +		imcb_chat_free( ic->groupchats->data );  	for( l = yd->buddygroups; l; l = l->next )  	{ @@ -612,10 +612,8 @@ void ext_yahoo_status_changed( int id, const char *who, int stat, const char *ms  	imcb_buddy_status( ic, who, flags, state_string, msg ); -	/* Not implemented yet...  	if( stat == YAHOO_STATUS_IDLE ) -		imcb_buddy_times( ic, who, 0, away ); -	*/ +		imcb_buddy_times( ic, who, 0, idle );  }  void ext_yahoo_got_im( int id, const char *me, const char *who, const char *msg, long tm, int stat, int utf8 ) @@ -685,7 +683,7 @@ int ext_yahoo_add_handler( int id, int fd, yahoo_input_condition cond, void *dat  		d->data = data;  		inp->d = d; -		d->tag = inp->h = b_input_add( fd, GAIM_INPUT_READ, (b_event_handler) byahoo_read_ready_callback, (gpointer) d ); +		d->tag = inp->h = b_input_add( fd, B_EV_IO_READ, (b_event_handler) byahoo_read_ready_callback, (gpointer) d );  	}  	else if( cond == YAHOO_INPUT_WRITE )  	{ @@ -696,7 +694,7 @@ int ext_yahoo_add_handler( int id, int fd, yahoo_input_condition cond, void *dat  		d->data = data;  		inp->d = d; -		d->tag = inp->h = b_input_add( fd, GAIM_INPUT_WRITE, (b_event_handler) byahoo_write_ready_callback, (gpointer) d ); +		d->tag = inp->h = b_input_add( fd, B_EV_IO_WRITE, (b_event_handler) byahoo_write_ready_callback, (gpointer) d );  	}  	else  	{ @@ -795,10 +793,14 @@ static void byahoo_accept_conf( void *data )  {  	struct byahoo_conf_invitation *inv = data;  	struct groupchat *b; +	GSList *l; -	for( b = inv->ic->groupchats; b; b = b->next ) +	for( l = inv->ic->groupchats; l; l = l->next ) +	{ +		b = l->data;  		if( b == inv->c )  			break; +	}  	if( b != NULL )  	{ @@ -864,9 +866,7 @@ void ext_yahoo_conf_userdecline( int id, const char *ignored, const char *who, c  void ext_yahoo_conf_userjoin( int id, const char *ignored, const char *who, const char *room )  {  	struct im_connection *ic = byahoo_get_ic_by_id( id ); -	struct groupchat *c; -	 -	for( c = ic->groupchats; c && strcmp( c->title, room ) != 0; c = c->next ); +	struct groupchat *c = bee_chat_by_title( ic->bee, ic, room );  	if( c )  		imcb_chat_add_buddy( c, (char*) who ); @@ -876,9 +876,7 @@ void ext_yahoo_conf_userleave( int id, const char *ignored, const char *who, con  {  	struct im_connection *ic = byahoo_get_ic_by_id( id ); -	struct groupchat *c; -	 -	for( c = ic->groupchats; c && strcmp( c->title, room ) != 0; c = c->next ); +	struct groupchat *c = bee_chat_by_title( ic->bee, ic, room );  	if( c )  		imcb_chat_remove_buddy( c, (char*) who, "" ); @@ -888,9 +886,7 @@ void ext_yahoo_conf_message( int id, const char *ignored, const char *who, const  {  	struct im_connection *ic = byahoo_get_ic_by_id( id );  	char *m = byahoo_strip( msg ); -	struct groupchat *c; -	 -	for( c = ic->groupchats; c && strcmp( c->title, room ) != 0; c = c->next ); +	struct groupchat *c = bee_chat_by_title( ic->bee, ic, room );  	if( c )  		imcb_chat_msg( c, (char*) who, (char*) m, 0, 0 ); | 
