Changeset bb09b3c for protocols/twitter/twitter.c
- Timestamp:
- 2010-06-03T21:13:57Z (14 years ago)
- 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. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
protocols/twitter/twitter.c
ra6b2f13 rbb09b3c 23 23 24 24 #include "nogaim.h" 25 #include "oauth.h" 25 26 #include "twitter.h" 26 27 #include "twitter_http.h" 27 28 #include "twitter_lib.h" 28 29 29 30 30 /** … … 51 51 } 52 52 53 static 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 68 static 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 77 static gboolean twitter_oauth_callback( struct oauth_info *info ); 78 79 static 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 88 static 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 53 136 static char *set_eval_mode( set_t *set, char *value ) 54 137 { 55 138 if( g_strcasecmp( value, "one" ) == 0 || 56 139 g_strcasecmp( value, "many" ) == 0 || 57 g_strcasecmp( value, "cha r" ) == 0 )140 g_strcasecmp( value, "chat" ) == 0 ) 58 141 return value; 59 142 else … … 61 144 } 62 145 146 static 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 63 158 static void twitter_init( account_t *acc ) 64 159 { 65 160 set_t *s; 161 162 s = set_add( &acc->set, "message_length", "140", set_eval_int, acc ); 66 163 67 164 s = set_add( &acc->set, "mode", "one", set_eval_mode, acc ); 68 165 s->flags |= ACC_SET_OFFLINE_ONLY; 166 167 s = set_add( &acc->set, "oauth", "true", set_eval_bool, acc ); 69 168 } 70 169 … … 80 179 81 180 twitter_connections = g_slist_append( twitter_connections, ic ); 82 181 ic->proto_data = td; 182 ic->flags |= OPT_DOES_HTML; 183 83 184 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 ); 85 189 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_loop95 // 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);97 190 98 191 sprintf( name, "twitter_%s", acc->user ); 99 192 imcb_add_buddy( ic, name, NULL ); 100 193 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 ); 101 199 } 102 200 … … 119 217 if( td ) 120 218 { 219 oauth_info_free( td->oauth_info ); 220 g_free( td->pass ); 121 221 g_free( td ); 122 222 } … … 130 230 static int twitter_buddy_msg( struct im_connection *ic, char *who, char *message, int away ) 131 231 { 232 struct twitter_data *td = ic->proto_data; 233 132 234 if (g_strncasecmp(who, "twitter_", 8) == 0 && 133 235 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 } 135 250 else 251 { 136 252 twitter_direct_messages_new(ic, who, message); 137 253 } 138 254 return( 0 ); 139 255 } … … 160 276 static void twitter_chat_msg( struct groupchat *c, char *message, int flags ) 161 277 { 162 if( c && message )278 if( c && message && twitter_length_check(c->ic, message)) 163 279 twitter_post_status(c->ic, message); 164 280 }
Note: See TracChangeset
for help on using the changeset viewer.