Ignore:
Timestamp:
2010-04-28T07:44:45Z (14 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
a7c6d0e
Parents:
f1b7711 (diff), 3f668e47 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merging OAuth branch. It should be stable now and is documented.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • protocols/twitter/twitter.c

    rf1b7711 r23784065  
    2323
    2424#include "nogaim.h"
     25#include "oauth.h"
    2526#include "twitter.h"
    2627#include "twitter_http.h"
     
    4950        // If we are still logged in run this function again after timeout.
    5051        return (ic->flags & OPT_LOGGED_IN) == OPT_LOGGED_IN;
     52}
     53
     54static void twitter_main_loop_start( struct im_connection *ic )
     55{
     56        struct twitter_data *td = ic->proto_data;
     57       
     58        imcb_log( ic, "Connecting to Twitter" );
     59
     60        // Run this once. After this queue the main loop function.
     61        twitter_main_loop(ic, -1, 0);
     62
     63        // Queue the main_loop
     64        // Save the return value, so we can remove the timeout on logout.
     65        td->main_loop_id = b_timeout_add(60000, twitter_main_loop, ic);
     66}
     67
     68static gboolean twitter_oauth_callback( struct oauth_info *info );
     69
     70static void twitter_oauth_start( struct im_connection *ic )
     71{
     72        struct twitter_data *td = ic->proto_data;
     73       
     74        imcb_log( ic, "Requesting OAuth request token" );
     75
     76        td->oauth_info = oauth_request_token(
     77                TWITTER_OAUTH_REQUEST_TOKEN, twitter_oauth_callback, ic );
     78}
     79
     80static gboolean twitter_oauth_callback( struct oauth_info *info )
     81{
     82        struct im_connection *ic = info->data;
     83        struct twitter_data *td;
     84       
     85        if( !g_slist_find( twitter_connections, ic ) )
     86                return FALSE;
     87       
     88        td = ic->proto_data;
     89        if( info->stage == OAUTH_REQUEST_TOKEN )
     90        {
     91                char name[strlen(ic->acc->user)+9], *msg;
     92               
     93                if( info->request_token == NULL )
     94                {
     95                        imcb_error( ic, "OAuth error: %s", info->http->status_string );
     96                        imc_logout( ic, TRUE );
     97                        return FALSE;
     98                }
     99               
     100                sprintf( name, "twitter_%s", ic->acc->user );
     101                msg = g_strdup_printf( "To finish OAuth authentication, please visit "
     102                                       "%s?%s and respond with the resulting PIN code.",
     103                                       TWITTER_OAUTH_AUTHORIZE, info->auth_params );
     104                imcb_buddy_msg( ic, name, msg, 0, 0 );
     105                g_free( msg );
     106        }
     107        else if( info->stage == OAUTH_ACCESS_TOKEN )
     108        {
     109                if( info->access_token == NULL )
     110                {
     111                        imcb_error( ic, "OAuth error: %s", info->http->status_string );
     112                        imc_logout( ic, TRUE );
     113                        return FALSE;
     114                }
     115               
     116                td->oauth = g_strdup( info->access_token );
     117               
     118                /* IM mods didn't do this so far and it's ugly but I should
     119                   be able to get away with it... */
     120                g_free( ic->acc->pass );
     121                ic->acc->pass = g_strdup( info->access_token );
     122               
     123                twitter_main_loop_start( ic );
     124        }
     125       
     126        return TRUE;
    51127}
    52128
     
    67143        s = set_add( &acc->set, "mode", "one", set_eval_mode, acc );
    68144        s->flags |= ACC_SET_OFFLINE_ONLY;
     145       
     146        s = set_add( &acc->set, "oauth", "true", set_eval_bool, acc );
    69147}
    70148
     
    80158
    81159        twitter_connections = g_slist_append( twitter_connections, ic );
    82 
     160        ic->proto_data = td;
     161       
    83162        td->user = acc->user;
    84         td->pass = acc->pass;
     163        if( !set_getbool( &acc->set, "oauth" ) )
     164                td->pass = g_strdup( acc->pass );
     165        else if( strstr( acc->pass, "oauth_token=" ) )
     166                td->oauth = g_strdup( acc->pass );
    85167        td->home_timeline_id = 0;
    86 
    87         ic->proto_data = td;
    88 
    89         imcb_log( ic, "Connecting to Twitter" );
    90 
    91         // Run this once. After this queue the main loop function.
    92         twitter_main_loop(ic, -1, 0);
    93 
    94         // Queue the main_loop
    95         // Save the return value, so we can remove the timeout on logout.
    96         td->main_loop_id = b_timeout_add(60000, twitter_main_loop, ic);
    97168       
    98169        sprintf( name, "twitter_%s", acc->user );
    99170        imcb_add_buddy( ic, name, NULL );
    100171        imcb_buddy_status( ic, name, OPT_LOGGED_IN, NULL, NULL );
     172       
     173        if( td->pass || td->oauth )
     174                twitter_main_loop_start( ic );
     175        else
     176                twitter_oauth_start( ic );
    101177}
    102178
     
    119195        if( td )
    120196        {
     197                oauth_info_free( td->oauth_info );
     198               
     199                g_free( td->pass );
     200                g_free( td->oauth );
    121201                g_free( td );
    122202        }
     
    130210static int twitter_buddy_msg( struct im_connection *ic, char *who, char *message, int away )
    131211{
     212        struct twitter_data *td = ic->proto_data;
     213       
    132214        if (g_strncasecmp(who, "twitter_", 8) == 0 &&
    133215            g_strcasecmp(who + 8, ic->acc->user) == 0)
    134                 twitter_post_status(ic, message);
     216        {
     217                if( set_getbool( &ic->acc->set, "oauth" ) && td->oauth_info )
     218                {
     219                        oauth_access_token( TWITTER_OAUTH_ACCESS_TOKEN, message, td->oauth_info );
     220                        td->oauth_info = NULL;
     221                }
     222                else
     223                        twitter_post_status(ic, message);
     224        }
    135225        else
     226        {
    136227                twitter_direct_messages_new(ic, who, message);
    137        
     228        }
    138229        return( 0 );
    139230}
Note: See TracChangeset for help on using the changeset viewer.