diff options
| author | Wilmer van der Gaast <wilmer@gaast.net> | 2010-04-26 23:40:11 +0100 | 
|---|---|---|
| committer | Wilmer van der Gaast <wilmer@gaast.net> | 2010-04-26 23:40:11 +0100 | 
| commit | c42e8b907817fc76df4dc3b48d85858555102654 (patch) | |
| tree | 25cc5dd76a35ec74045b643d566b6efd663cb365 | |
| parent | 713d6111cd754ff9eae4cfa1e6de82c9824d16db (diff) | |
OAuth, it lives!
| -rw-r--r-- | lib/oauth.c | 14 | ||||
| -rw-r--r-- | lib/oauth.h | 9 | ||||
| -rw-r--r-- | protocols/twitter/twitter.c | 39 | ||||
| -rw-r--r-- | protocols/twitter/twitter.h | 1 | 
4 files changed, 59 insertions, 4 deletions
| diff --git a/lib/oauth.c b/lib/oauth.c index 828d713d..7f60f4f5 100644 --- a/lib/oauth.c +++ b/lib/oauth.c @@ -301,6 +301,7 @@ static void oauth_request_token_done( struct http_request *req )  		oauth_params_free( ¶ms );  	} +	st->stage = OAUTH_REQUEST_TOKEN;  	st->func( st );  } @@ -321,8 +322,19 @@ static void oauth_access_token_done( struct http_request *req )  	struct oauth_info *st = req->data;  	if( req->status_code == 200 ) -		st->access_token = g_strdup( req->reply_body ); +	{ +		GSList *params; +		const char *token, *token_secret; +		 +		oauth_params_parse( ¶ms, req->reply_body ); +		token = oauth_params_get( ¶ms, "oauth_token" ); +		token_secret = oauth_params_get( ¶ms, "oauth_token_secret" ); +		st->access_token = g_strdup_printf( +			"oauth_token=%s&oauth_token_secret=%s", token, token_secret ); +		oauth_params_free( ¶ms ); +	} +	st->stage = OAUTH_ACCESS_TOKEN;  	st->func( st );  } diff --git a/lib/oauth.h b/lib/oauth.h index 4151d73f..30486b98 100644 --- a/lib/oauth.h +++ b/lib/oauth.h @@ -26,8 +26,17 @@  struct oauth_info;  typedef void (*oauth_cb)( struct oauth_info * ); +typedef enum +{ +	OAUTH_INIT, +	OAUTH_REQUEST_TOKEN, +	OAUTH_ACCESS_TOKEN, +} oauth_stage_t; +  struct oauth_info  { +	oauth_stage_t stage; +	  	oauth_cb func;  	void *data; diff --git a/protocols/twitter/twitter.c b/protocols/twitter/twitter.c index e2a5986b..eb05d9d5 100644 --- a/protocols/twitter/twitter.c +++ b/protocols/twitter/twitter.c @@ -69,17 +69,29 @@ static void twitter_oauth_callback( struct oauth_info *info );  static void twitter_oauth_start( struct im_connection *ic )  { +	imcb_log( ic, "Requesting OAuth request token" ); +  	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; +	struct twitter_data *td = ic->proto_data; -	if( info->request_token && info->access_token == NULL ) +	if( info->stage == OAUTH_REQUEST_TOKEN )  	{  		char name[strlen(ic->acc->user)+9], *msg; +		if( info->request_token == NULL ) +		{ +			imcb_error( ic, "OAuth error: %s", info->http->status_string ); +			imc_logout( ic, TRUE ); +			return; +		} +		 +		td->oauth_info = info; +		  		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.", @@ -87,6 +99,19 @@ static void twitter_oauth_callback( struct oauth_info *info )  		imcb_buddy_msg( ic, name, msg, 0, 0 );  		g_free( msg );  	} +	else if( info->stage == OAUTH_ACCESS_TOKEN ) +	{ +		if( info->access_token == NULL ) +		{ +			imcb_error( ic, "OAuth error: %s", info->http->status_string ); +			imc_logout( ic, TRUE ); +			return; +		} +		 +		td->oauth = g_strdup( info->access_token ); +		 +		twitter_main_loop_start( ic ); +	}  }  static char *set_eval_mode( set_t *set, char *value ) @@ -170,12 +195,20 @@ static void twitter_logout( struct im_connection *ic )   */  static int twitter_buddy_msg( struct im_connection *ic, char *who, char *message, int away )  { +	struct twitter_data *td = ic->proto_data; +	  	if (g_strncasecmp(who, "twitter_", 8) == 0 &&  	    g_strcasecmp(who + 8, ic->acc->user) == 0) -		twitter_post_status(ic, message); +	{ +		if( set_getbool( &ic->acc->set, "oauth" ) && td->oauth == NULL ) +			oauth_access_token( TWITTER_OAUTH_ACCESS_TOKEN, message, td->oauth_info ); +		else +			twitter_post_status(ic, message); +	}  	else +	{  		twitter_direct_messages_new(ic, who, message); -	 +	}  	return( 0 );  } diff --git a/protocols/twitter/twitter.h b/protocols/twitter/twitter.h index f1937bd0..9c046b66 100644 --- a/protocols/twitter/twitter.h +++ b/protocols/twitter/twitter.h @@ -37,6 +37,7 @@ struct twitter_data  	char* user;  	char* pass;  	char* oauth; +	struct oauth_info *oauth_info;  	guint64 home_timeline_id;  	gint main_loop_id;  	struct groupchat *home_timeline_gc; | 
