Ignore:
Timestamp:
2010-06-03T21:13:57Z (14 years ago)
Author:
Sven Moritz Hallberg <pesco@…>
Branches:
master
Children:
1dd3470
Parents:
a6b2f13 (diff), df1ae622 (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:

merge in bitlbee 1.2.7

File:
1 edited

Legend:

Unmodified
Added
Removed
  • protocols/twitter/twitter.c

    ra6b2f13 rbb09b3c  
    2323
    2424#include "nogaim.h"
     25#include "oauth.h"
    2526#include "twitter.h"
    2627#include "twitter_http.h"
    2728#include "twitter_lib.h"
    28 
    2929
    3030/**
     
    5151}
    5252
     53static void twitter_main_loop_start( struct im_connection *ic )
     54{
     55        struct twitter_data *td = ic->proto_data;
     56       
     57        imcb_log( ic, "Connecting to Twitter" );
     58
     59        // Run this once. After this queue the main loop function.
     60        twitter_main_loop(ic, -1, 0);
     61
     62        // Queue the main_loop
     63        // Save the return value, so we can remove the timeout on logout.
     64        td->main_loop_id = b_timeout_add(60000, twitter_main_loop, ic);
     65}
     66
     67
     68static const struct oauth_service twitter_oauth =
     69{
     70        "http://api.twitter.com/oauth/request_token",
     71        "http://api.twitter.com/oauth/access_token",
     72        "http://api.twitter.com/oauth/authorize",
     73        .consumer_key = "xsDNKJuNZYkZyMcu914uEA",
     74        .consumer_secret = "FCxqcr0pXKzsF9ajmP57S3VQ8V6Drk4o2QYtqMcOszo",
     75};
     76
     77static gboolean twitter_oauth_callback( struct oauth_info *info );
     78
     79static void twitter_oauth_start( struct im_connection *ic )
     80{
     81        struct twitter_data *td = ic->proto_data;
     82       
     83        imcb_log( ic, "Requesting OAuth request token" );
     84
     85        td->oauth_info = oauth_request_token( &twitter_oauth, twitter_oauth_callback, ic );
     86}
     87
     88static gboolean twitter_oauth_callback( struct oauth_info *info )
     89{
     90        struct im_connection *ic = info->data;
     91        struct twitter_data *td;
     92       
     93        if( !g_slist_find( twitter_connections, ic ) )
     94                return FALSE;
     95       
     96        td = ic->proto_data;
     97        if( info->stage == OAUTH_REQUEST_TOKEN )
     98        {
     99                char name[strlen(ic->acc->user)+9], *msg;
     100               
     101                if( info->request_token == NULL )
     102                {
     103                        imcb_error( ic, "OAuth error: %s", info->http->status_string );
     104                        imc_logout( ic, TRUE );
     105                        return FALSE;
     106                }
     107               
     108                sprintf( name, "twitter_%s", ic->acc->user );
     109                msg = g_strdup_printf( "To finish OAuth authentication, please visit "
     110                                       "%s and respond with the resulting PIN code.",
     111                                       info->auth_url );
     112                imcb_buddy_msg( ic, name, msg, 0, 0 );
     113                g_free( msg );
     114        }
     115        else if( info->stage == OAUTH_ACCESS_TOKEN )
     116        {
     117                if( info->token == NULL || info->token_secret == NULL )
     118                {
     119                        imcb_error( ic, "OAuth error: %s", info->http->status_string );
     120                        imc_logout( ic, TRUE );
     121                        return FALSE;
     122                }
     123               
     124                /* IM mods didn't do this so far and it's ugly but I should
     125                   be able to get away with it... */
     126                g_free( ic->acc->pass );
     127                ic->acc->pass = oauth_to_string( info );
     128               
     129                twitter_main_loop_start( ic );
     130        }
     131       
     132        return TRUE;
     133}
     134
     135
    53136static char *set_eval_mode( set_t *set, char *value )
    54137{
    55138        if( g_strcasecmp( value, "one" ) == 0 ||
    56139            g_strcasecmp( value, "many" ) == 0 ||
    57             g_strcasecmp( value, "char" ) == 0 )
     140            g_strcasecmp( value, "chat" ) == 0 )
    58141                return value;
    59142        else
     
    61144}
    62145
     146static gboolean twitter_length_check( struct im_connection *ic, gchar *msg )
     147{
     148        int max = set_getint( &ic->acc->set, "message_length" ), len;
     149       
     150        if( max == 0 || ( len = g_utf8_strlen( msg, -1 ) ) <= max )
     151                return TRUE;
     152       
     153        imcb_error( ic, "Maximum message length exceeded: %d > %d", len, max );
     154       
     155        return FALSE;
     156}
     157
    63158static void twitter_init( account_t *acc )
    64159{
    65160        set_t *s;
     161       
     162        s = set_add( &acc->set, "message_length", "140", set_eval_int, acc );
    66163       
    67164        s = set_add( &acc->set, "mode", "one", set_eval_mode, acc );
    68165        s->flags |= ACC_SET_OFFLINE_ONLY;
     166       
     167        s = set_add( &acc->set, "oauth", "true", set_eval_bool, acc );
    69168}
    70169
     
    80179
    81180        twitter_connections = g_slist_append( twitter_connections, ic );
    82 
     181        ic->proto_data = td;
     182        ic->flags |= OPT_DOES_HTML;
     183       
    83184        td->user = acc->user;
    84         td->pass = acc->pass;
     185        if( !set_getbool( &acc->set, "oauth" ) )
     186                td->pass = g_strdup( acc->pass );
     187        else if( strstr( acc->pass, "oauth_token=" ) )
     188                td->oauth_info = oauth_from_string( acc->pass, &twitter_oauth );
    85189        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);
    97190       
    98191        sprintf( name, "twitter_%s", acc->user );
    99192        imcb_add_buddy( ic, name, NULL );
    100193        imcb_buddy_status( ic, name, OPT_LOGGED_IN, NULL, NULL );
     194       
     195        if( td->pass || td->oauth_info )
     196                twitter_main_loop_start( ic );
     197        else
     198                twitter_oauth_start( ic );
    101199}
    102200
     
    119217        if( td )
    120218        {
     219                oauth_info_free( td->oauth_info );
     220                g_free( td->pass );
    121221                g_free( td );
    122222        }
     
    130230static int twitter_buddy_msg( struct im_connection *ic, char *who, char *message, int away )
    131231{
     232        struct twitter_data *td = ic->proto_data;
     233       
    132234        if (g_strncasecmp(who, "twitter_", 8) == 0 &&
    133235            g_strcasecmp(who + 8, ic->acc->user) == 0)
    134                 twitter_post_status(ic, message);
     236        {
     237                if( set_getbool( &ic->acc->set, "oauth" ) &&
     238                    td->oauth_info && td->oauth_info->token == NULL )
     239                {
     240                        if( !oauth_access_token( message, td->oauth_info ) )
     241                        {
     242                                imcb_error( ic, "OAuth error: %s", "Failed to send access token request" );
     243                                imc_logout( ic, TRUE );
     244                                return FALSE;
     245                        }
     246                }
     247                else if( twitter_length_check(ic, message) )
     248                        twitter_post_status(ic, message);
     249        }
    135250        else
     251        {
    136252                twitter_direct_messages_new(ic, who, message);
    137        
     253        }
    138254        return( 0 );
    139255}
     
    160276static void twitter_chat_msg( struct groupchat *c, char *message, int flags )
    161277{
    162         if( c && message )
     278        if( c && message && twitter_length_check(c->ic, message))
    163279                twitter_post_status(c->ic, message);
    164280}
Note: See TracChangeset for help on using the changeset viewer.