diff options
| author | Wilmer van der Gaast <wilmer@gaast.net> | 2010-04-26 22:50:48 +0100 | 
|---|---|---|
| committer | Wilmer van der Gaast <wilmer@gaast.net> | 2010-04-26 22:50:48 +0100 | 
| commit | 713d6111cd754ff9eae4cfa1e6de82c9824d16db (patch) | |
| tree | 8e0cc81718d29abe2dc8caadf63f76c7f1b1f5c4 | |
| parent | acba168df90f28fdaee26bc09d621364878dd099 (diff) | |
Twitter module now generates authorize URLs.
| -rw-r--r-- | lib/oauth.c | 8 | ||||
| -rw-r--r-- | lib/oauth.h | 2 | ||||
| -rw-r--r-- | protocols/twitter/twitter.c | 64 | ||||
| -rw-r--r-- | protocols/twitter/twitter.h | 4 | 
4 files changed, 59 insertions, 19 deletions
| diff --git a/lib/oauth.c b/lib/oauth.c index 7897b760..828d713d 100644 --- a/lib/oauth.c +++ b/lib/oauth.c @@ -297,11 +297,11 @@ static void oauth_request_token_done( struct http_request *req )  		st->auth_params = g_strdup( req->reply_body );  		oauth_params_parse( ¶ms, st->auth_params ); -		st->token = g_strdup( oauth_params_get( ¶ms, "oauth_token" ) ); +		st->request_token = g_strdup( oauth_params_get( ¶ms, "oauth_token" ) );  		oauth_params_free( ¶ms );  	} -	//st->func( st ); +	st->func( st );  }  static void oauth_access_token_done( struct http_request *req ); @@ -310,7 +310,7 @@ void *oauth_access_token( const char *url, const char *pin, struct oauth_info *s  {  	GSList *params = NULL; -	oauth_params_add( ¶ms, "oauth_token", st->token ); +	oauth_params_add( ¶ms, "oauth_token", st->request_token );  	oauth_params_add( ¶ms, "oauth_verifier", pin );  	return oauth_post_request( url, ¶ms, oauth_access_token_done, st ); @@ -323,7 +323,7 @@ static void oauth_access_token_done( struct http_request *req )  	if( req->status_code == 200 )  		st->access_token = g_strdup( req->reply_body ); -	//st->func( st ); +	st->func( st );  }  char *oauth_http_header( char *access_token, const char *method, const char *url, char *args ) diff --git a/lib/oauth.h b/lib/oauth.h index 91696d83..4151d73f 100644 --- a/lib/oauth.h +++ b/lib/oauth.h @@ -34,7 +34,7 @@ struct oauth_info  	struct http_request *http;  	char *auth_params; -	char *token; +	char *request_token;  	char *access_token;  }; diff --git a/protocols/twitter/twitter.c b/protocols/twitter/twitter.c index ead03c56..e2a5986b 100644 --- a/protocols/twitter/twitter.c +++ b/protocols/twitter/twitter.c @@ -22,6 +22,7 @@  ****************************************************************************/  #include "nogaim.h" +#include "oauth.h"  #include "twitter.h"  #include "twitter_http.h"  #include "twitter_lib.h" @@ -50,6 +51,44 @@ gboolean twitter_main_loop(gpointer data, gint fd, b_input_condition cond)  	return (ic->flags & OPT_LOGGED_IN) == OPT_LOGGED_IN;  } +static void twitter_main_loop_start( struct im_connection *ic ) +{ +	struct twitter_data *td = ic->proto_data; +	 +	imcb_log( ic, "Connecting to Twitter" ); + +	// Run this once. After this queue the main loop function. +	twitter_main_loop(ic, -1, 0); + +	// Queue the main_loop +	// Save the return value, so we can remove the timeout on logout. +	td->main_loop_id = b_timeout_add(60000, twitter_main_loop, ic); +} + +static void twitter_oauth_callback( struct oauth_info *info ); + +static void twitter_oauth_start( struct im_connection *ic ) +{ +	oauth_request_token( TWITTER_OAUTH_REQUEST_TOKEN, twitter_oauth_callback, ic ); +} + +static void twitter_oauth_callback( struct oauth_info *info ) +{ +	struct im_connection *ic = info->data; +	 +	if( info->request_token && info->access_token == NULL ) +	{ +		char name[strlen(ic->acc->user)+9], *msg; +		 +		sprintf( name, "twitter_%s", ic->acc->user ); +		msg = g_strdup_printf( "To finish OAuth authentication, please visit " +		                       "%s?%s and respond with the resulting PIN code.", +		                       TWITTER_OAUTH_AUTHORIZE, info->auth_params ); +		imcb_buddy_msg( ic, name, msg, 0, 0 ); +		g_free( msg ); +	} +} +  static char *set_eval_mode( set_t *set, char *value )  {  	if( g_strcasecmp( value, "one" ) == 0 || @@ -66,6 +105,8 @@ static void twitter_init( account_t *acc )  	s = set_add( &acc->set, "mode", "one", set_eval_mode, acc );  	s->flags |= ACC_SET_OFFLINE_ONLY; +	 +	s = set_add( &acc->set, "oauth", "true", set_eval_bool, acc );  }  /** @@ -79,28 +120,23 @@ static void twitter_login( account_t *acc )  	char name[strlen(acc->user)+9];  	twitter_connections = g_slist_append( twitter_connections, ic ); - +	ic->proto_data = td; +	  	td->user = acc->user; -	if( strstr( acc->pass, "oauth_token=" ) == NULL ) +	if( !set_getbool( &acc->set, "oauth" ) )  		td->pass = g_strdup( acc->pass ); -	else +	else if( strstr( acc->pass, "oauth_token=" ) )  		td->oauth = g_strdup( acc->pass );  	td->home_timeline_id = 0; - -	ic->proto_data = td; - -	imcb_log( ic, "Connecting to Twitter" ); - -	// Run this once. After this queue the main loop function. -	twitter_main_loop(ic, -1, 0); - -	// Queue the main_loop -	// Save the return value, so we can remove the timeout on logout. -	td->main_loop_id = b_timeout_add(60000, twitter_main_loop, ic);  	sprintf( name, "twitter_%s", acc->user );  	imcb_add_buddy( ic, name, NULL );  	imcb_buddy_status( ic, name, OPT_LOGGED_IN, NULL, NULL ); +	 +	if( td->pass || td->oauth ) +		twitter_main_loop_start( ic ); +	else +		twitter_oauth_start( ic );  }  /** diff --git a/protocols/twitter/twitter.h b/protocols/twitter/twitter.h index 16620e1d..f1937bd0 100644 --- a/protocols/twitter/twitter.h +++ b/protocols/twitter/twitter.h @@ -50,4 +50,8 @@ struct twitter_data   */  GSList *twitter_connections; +#define TWITTER_OAUTH_REQUEST_TOKEN "http://api.twitter.com/oauth/request_token" +#define TWITTER_OAUTH_ACCESS_TOKEN  "http://api.twitter.com/oauth/access_token" +#define TWITTER_OAUTH_AUTHORIZE     "http://api.twitter.com/oauth/authorize" +  #endif //_TWITTER_H | 
