diff options
Diffstat (limited to 'protocols/jabber/jabber.c')
| -rw-r--r-- | protocols/jabber/jabber.c | 99 | 
1 files changed, 44 insertions, 55 deletions
diff --git a/protocols/jabber/jabber.c b/protocols/jabber/jabber.c index 7d9547ab..9b94b21d 100644 --- a/protocols/jabber/jabber.c +++ b/protocols/jabber/jabber.c @@ -59,6 +59,8 @@ static void jabber_init( account_t *acc )  	s = set_add( &acc->set, "activity_timeout", "600", set_eval_int, acc ); +	s = set_add( &acc->set, "oauth", "false", set_eval_bool, acc ); +  	g_snprintf( str, sizeof( str ), "%d", jabber_port_list[0] );  	s = set_add( &acc->set, "port", str, set_eval_int, acc );  	s->flags |= ACC_SET_OFFLINE_ONLY; @@ -98,9 +100,7 @@ static void jabber_login( account_t *acc )  {  	struct im_connection *ic = imcb_new( acc );  	struct jabber_data *jd = g_new0( struct jabber_data, 1 ); -	struct ns_srv_reply **srvl = NULL, *srv = NULL; -	char *connect_to, *s; -	int i; +	char *s;  	/* For now this is needed in the _connected() handlers if using  	   GLib event handling, to make sure we're not handling events @@ -137,63 +137,37 @@ static void jabber_login( account_t *acc )  		*s = 0;  	} -	/* This code isn't really pretty. Backwards compatibility never is... */ -	s = acc->server; -	while( s ) +	jd->node_cache = g_hash_table_new_full( g_str_hash, g_str_equal, NULL, jabber_cache_entry_free ); +	jd->buddies = g_hash_table_new( g_str_hash, g_str_equal ); +	 +	if( set_getbool( &acc->set, "oauth" ) )  	{ -		static int had_port = 0; -		 -		if( strncmp( s, "ssl", 3 ) == 0 ) -		{ -			set_setstr( &acc->set, "ssl", "true" ); -			 -			/* Flush this part so that (if this was the first -			   part of the server string) acc->server gets -			   flushed. We don't want to have to do this another -			   time. :-) */ -			*s = 0; -			s ++; -			 -			/* Only set this if the user didn't specify a custom -			   port number already... */ -			if( !had_port ) -				set_setint( &acc->set, "port", 5223 ); -		} -		else if( isdigit( *s ) ) -		{ -			int i; -			 -			/* The first character is a digit. It could be an -			   IP address though. Only accept this as a port# -			   if there are only digits. */ -			for( i = 0; isdigit( s[i] ); i ++ ); -			 -			/* If the first non-digit character is a colon or -			   the end of the string, save the port number -			   where it should be. */ -			if( s[i] == ':' || s[i] == 0 ) -			{ -				sscanf( s, "%d", &i ); -				set_setint( &acc->set, "port", i ); -				 -				/* See above. */ -				*s = 0; -				s ++; -			} -			 -			had_port = 1; -		} +		jd->fd = jd->r_inpa = jd->w_inpa = -1; -		s = strchr( s, ':' ); -		if( s ) +		/* For the first login with OAuth, we have to authenticate via the browser. +		   For subsequent logins, exchange the refresh token for a valid access +		   token (even though the last one maybe didn't expire yet). */ +		if( strncmp( acc->pass, "refresh_token=", 14 ) != 0 )  		{ -			*s = 0; -			s ++; +			sasl_oauth2_init( ic ); +			ic->flags |= OPT_SLOW_LOGIN;  		} +		else +			sasl_oauth2_refresh( ic, acc->pass + 14 );  	} -	 -	jd->node_cache = g_hash_table_new_full( g_str_hash, g_str_equal, NULL, jabber_cache_entry_free ); -	jd->buddies = g_hash_table_new( g_str_hash, g_str_equal ); +	else +		jabber_connect( ic ); +} + +/* Separate this from jabber_login() so we can do OAuth first if necessary. +   Putting this in io.c would probably be more correct. */ +void jabber_connect( struct im_connection *ic ) +{ +	account_t *acc = ic->acc; +	struct jabber_data *jd = ic->proto_data; +	int i; +	char *connect_to; +	struct ns_srv_reply **srvl = NULL, *srv = NULL;  	/* Figure out the hostname to connect to. */  	if( acc->server && *acc->server ) @@ -318,6 +292,7 @@ static void jabber_logout( struct im_connection *ic )  	xt_free( jd->xt ); +	g_free( jd->oauth2_access_token );  	g_free( jd->away_message );  	g_free( jd->username );  	g_free( jd ); @@ -336,6 +311,20 @@ static int jabber_buddy_msg( struct im_connection *ic, char *who, char *message,  	if( g_strcasecmp( who, JABBER_XMLCONSOLE_HANDLE ) == 0 )  		return jabber_write( ic, message, strlen( message ) ); +	if( g_strcasecmp( who, "jabber_oauth" ) == 0 ) +	{ +		if( sasl_oauth2_get_refresh_token( ic, message ) ) +		{ +			return 1; +		} +		else +		{ +			imcb_error( ic, "OAuth failure" ); +			imc_logout( ic, TRUE ); +			return 0; +		} +	} +	  	if( ( s = strchr( who, '=' ) ) && jabber_chat_by_jid( ic, s + 1 ) )  		bud = jabber_buddy_by_ext_jid( ic, who, 0 );  	else  | 
