[1b221e0] | 1 | /***************************************************************************\ |
---|
| 2 | * * |
---|
| 3 | * BitlBee - An IRC to IM gateway * |
---|
| 4 | * Simple module to facilitate twitter functionality. * |
---|
| 5 | * * |
---|
| 6 | * Copyright 2009 Geert Mulders <g.c.w.m.mulders@gmail.com> * |
---|
| 7 | * * |
---|
| 8 | * This library is free software; you can redistribute it and/or * |
---|
| 9 | * modify it under the terms of the GNU Lesser General Public * |
---|
| 10 | * License as published by the Free Software Foundation, version * |
---|
| 11 | * 2.1. * |
---|
| 12 | * * |
---|
| 13 | * This library is distributed in the hope that it will be useful, * |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
---|
| 16 | * Lesser General Public License for more details. * |
---|
| 17 | * * |
---|
| 18 | * You should have received a copy of the GNU Lesser General Public License * |
---|
| 19 | * along with this library; if not, write to the Free Software Foundation, * |
---|
| 20 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * |
---|
| 21 | * * |
---|
| 22 | ****************************************************************************/ |
---|
| 23 | |
---|
| 24 | #include "nogaim.h" |
---|
[713d611] | 25 | #include "oauth.h" |
---|
[1b221e0] | 26 | #include "twitter.h" |
---|
| 27 | #include "twitter_http.h" |
---|
| 28 | #include "twitter_lib.h" |
---|
[bb5ce4d1] | 29 | #include "url.h" |
---|
[1b221e0] | 30 | |
---|
| 31 | /** |
---|
[62d2cfb] | 32 | * Main loop function |
---|
| 33 | */ |
---|
[1b221e0] | 34 | gboolean twitter_main_loop(gpointer data, gint fd, b_input_condition cond) |
---|
| 35 | { |
---|
| 36 | struct im_connection *ic = data; |
---|
[3e69802] | 37 | |
---|
[1b221e0] | 38 | // Check if we are still logged in... |
---|
[3bd4a93] | 39 | if (!g_slist_find( twitter_connections, ic )) |
---|
[1b221e0] | 40 | return 0; |
---|
| 41 | |
---|
[62d2cfb] | 42 | // If the user uses multiple private message windows we need to get the |
---|
| 43 | // users buddies. |
---|
[e88fbe27] | 44 | if (g_strcasecmp(set_getstr(&ic->acc->set, "mode"), "many") == 0) |
---|
[62d2cfb] | 45 | twitter_get_statuses_friends(ic, -1); |
---|
| 46 | |
---|
[1b221e0] | 47 | // Do stuff.. |
---|
| 48 | twitter_get_home_timeline(ic, -1); |
---|
| 49 | |
---|
| 50 | // If we are still logged in run this function again after timeout. |
---|
| 51 | return (ic->flags & OPT_LOGGED_IN) == OPT_LOGGED_IN; |
---|
| 52 | } |
---|
| 53 | |
---|
[713d611] | 54 | static 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 | |
---|
[c2ecadc] | 68 | |
---|
[3b878a1] | 69 | static const struct oauth_service twitter_oauth = |
---|
[c2ecadc] | 70 | { |
---|
| 71 | "http://api.twitter.com/oauth/request_token", |
---|
| 72 | "http://api.twitter.com/oauth/access_token", |
---|
[c01bbd1] | 73 | "https://api.twitter.com/oauth/authorize", |
---|
[c2ecadc] | 74 | .consumer_key = "xsDNKJuNZYkZyMcu914uEA", |
---|
| 75 | .consumer_secret = "FCxqcr0pXKzsF9ajmP57S3VQ8V6Drk4o2QYtqMcOszo", |
---|
| 76 | }; |
---|
| 77 | |
---|
[18dbb20] | 78 | static gboolean twitter_oauth_callback( struct oauth_info *info ); |
---|
[713d611] | 79 | |
---|
| 80 | static void twitter_oauth_start( struct im_connection *ic ) |
---|
| 81 | { |
---|
[18dbb20] | 82 | struct twitter_data *td = ic->proto_data; |
---|
| 83 | |
---|
[c42e8b9] | 84 | imcb_log( ic, "Requesting OAuth request token" ); |
---|
| 85 | |
---|
[c2ecadc] | 86 | td->oauth_info = oauth_request_token( &twitter_oauth, twitter_oauth_callback, ic ); |
---|
[713d611] | 87 | } |
---|
| 88 | |
---|
[18dbb20] | 89 | static gboolean twitter_oauth_callback( struct oauth_info *info ) |
---|
[713d611] | 90 | { |
---|
| 91 | struct im_connection *ic = info->data; |
---|
[18dbb20] | 92 | struct twitter_data *td; |
---|
[713d611] | 93 | |
---|
[18dbb20] | 94 | if( !g_slist_find( twitter_connections, ic ) ) |
---|
| 95 | return FALSE; |
---|
| 96 | |
---|
| 97 | td = ic->proto_data; |
---|
[c42e8b9] | 98 | if( info->stage == OAUTH_REQUEST_TOKEN ) |
---|
[713d611] | 99 | { |
---|
| 100 | char name[strlen(ic->acc->user)+9], *msg; |
---|
| 101 | |
---|
[c42e8b9] | 102 | if( info->request_token == NULL ) |
---|
| 103 | { |
---|
| 104 | imcb_error( ic, "OAuth error: %s", info->http->status_string ); |
---|
| 105 | imc_logout( ic, TRUE ); |
---|
[18dbb20] | 106 | return FALSE; |
---|
[c42e8b9] | 107 | } |
---|
| 108 | |
---|
[713d611] | 109 | sprintf( name, "twitter_%s", ic->acc->user ); |
---|
| 110 | msg = g_strdup_printf( "To finish OAuth authentication, please visit " |
---|
[c2ecadc] | 111 | "%s and respond with the resulting PIN code.", |
---|
| 112 | info->auth_url ); |
---|
[713d611] | 113 | imcb_buddy_msg( ic, name, msg, 0, 0 ); |
---|
| 114 | g_free( msg ); |
---|
| 115 | } |
---|
[c42e8b9] | 116 | else if( info->stage == OAUTH_ACCESS_TOKEN ) |
---|
| 117 | { |
---|
[3b878a1] | 118 | if( info->token == NULL || info->token_secret == NULL ) |
---|
[c42e8b9] | 119 | { |
---|
| 120 | imcb_error( ic, "OAuth error: %s", info->http->status_string ); |
---|
| 121 | imc_logout( ic, TRUE ); |
---|
[18dbb20] | 122 | return FALSE; |
---|
[c42e8b9] | 123 | } |
---|
| 124 | |
---|
[288b215] | 125 | /* IM mods didn't do this so far and it's ugly but I should |
---|
| 126 | be able to get away with it... */ |
---|
| 127 | g_free( ic->acc->pass ); |
---|
[f4b0911] | 128 | ic->acc->pass = oauth_to_string( info ); |
---|
[288b215] | 129 | |
---|
[c42e8b9] | 130 | twitter_main_loop_start( ic ); |
---|
| 131 | } |
---|
[18dbb20] | 132 | |
---|
| 133 | return TRUE; |
---|
[713d611] | 134 | } |
---|
| 135 | |
---|
[c2ecadc] | 136 | |
---|
[e88fbe27] | 137 | static char *set_eval_mode( set_t *set, char *value ) |
---|
| 138 | { |
---|
| 139 | if( g_strcasecmp( value, "one" ) == 0 || |
---|
| 140 | g_strcasecmp( value, "many" ) == 0 || |
---|
[db57e7c] | 141 | g_strcasecmp( value, "chat" ) == 0 ) |
---|
[e88fbe27] | 142 | return value; |
---|
| 143 | else |
---|
| 144 | return NULL; |
---|
| 145 | } |
---|
[1b221e0] | 146 | |
---|
[9997691] | 147 | static gboolean twitter_length_check( struct im_connection *ic, gchar *msg ) |
---|
| 148 | { |
---|
| 149 | int max = set_getint( &ic->acc->set, "message_length" ), len; |
---|
| 150 | |
---|
| 151 | if( max == 0 || ( len = g_utf8_strlen( msg, -1 ) ) <= max ) |
---|
| 152 | return TRUE; |
---|
| 153 | |
---|
| 154 | imcb_error( ic, "Maximum message length exceeded: %d > %d", len, max ); |
---|
| 155 | |
---|
| 156 | return FALSE; |
---|
| 157 | } |
---|
| 158 | |
---|
[1b221e0] | 159 | static void twitter_init( account_t *acc ) |
---|
| 160 | { |
---|
[62d2cfb] | 161 | set_t *s; |
---|
[e88fbe27] | 162 | |
---|
[bb5ce4d1] | 163 | s = set_add( &acc->set, "base_url", TWITTER_API_URL, NULL, acc ); |
---|
| 164 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
| 165 | |
---|
[9997691] | 166 | s = set_add( &acc->set, "message_length", "140", set_eval_int, acc ); |
---|
| 167 | |
---|
[e88fbe27] | 168 | s = set_add( &acc->set, "mode", "one", set_eval_mode, acc ); |
---|
[2abceca] | 169 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[713d611] | 170 | |
---|
| 171 | s = set_add( &acc->set, "oauth", "true", set_eval_bool, acc ); |
---|
[1b221e0] | 172 | } |
---|
| 173 | |
---|
| 174 | /** |
---|
| 175 | * Login method. Since the twitter API works with seperate HTTP request we |
---|
| 176 | * only save the user and pass to the twitter_data object. |
---|
| 177 | */ |
---|
| 178 | static void twitter_login( account_t *acc ) |
---|
| 179 | { |
---|
| 180 | struct im_connection *ic = imcb_new( acc ); |
---|
[bb5ce4d1] | 181 | struct twitter_data *td; |
---|
[e88fbe27] | 182 | char name[strlen(acc->user)+9]; |
---|
[bb5ce4d1] | 183 | url_t url; |
---|
[62d2cfb] | 184 | |
---|
[bb5ce4d1] | 185 | if( !url_set( &url, set_getstr( &ic->acc->set, "base_url" ) ) || |
---|
| 186 | ( url.proto != PROTO_HTTP && url.proto != PROTO_HTTPS ) ) |
---|
| 187 | { |
---|
| 188 | imcb_error( ic, "Incorrect API base URL: %s", set_getstr( &ic->acc->set, "base_url" ) ); |
---|
| 189 | imc_logout( ic, FALSE ); |
---|
| 190 | return; |
---|
| 191 | } |
---|
| 192 | |
---|
[d569019] | 193 | twitter_connections = g_slist_append( twitter_connections, ic ); |
---|
[bb5ce4d1] | 194 | td = g_new0( struct twitter_data, 1 ); |
---|
[713d611] | 195 | ic->proto_data = td; |
---|
| 196 | |
---|
[bb5ce4d1] | 197 | td->url_ssl = url.proto == PROTO_HTTPS; |
---|
| 198 | td->url_port = url.port; |
---|
| 199 | td->url_host = g_strdup( url.host ); |
---|
| 200 | if( strcmp( url.file, "/" ) != 0 ) |
---|
| 201 | td->url_path = g_strdup( url.file ); |
---|
| 202 | else |
---|
| 203 | td->url_path = g_strdup( "" ); |
---|
| 204 | |
---|
[1b221e0] | 205 | td->user = acc->user; |
---|
[bb5ce4d1] | 206 | if( strstr( acc->pass, "oauth_token=" ) ) |
---|
[f4b0911] | 207 | td->oauth_info = oauth_from_string( acc->pass, &twitter_oauth ); |
---|
[e88fbe27] | 208 | |
---|
| 209 | sprintf( name, "twitter_%s", acc->user ); |
---|
| 210 | imcb_add_buddy( ic, name, NULL ); |
---|
| 211 | imcb_buddy_status( ic, name, OPT_LOGGED_IN, NULL, NULL ); |
---|
[713d611] | 212 | |
---|
[bb5ce4d1] | 213 | if( td->oauth_info || !set_getbool( &acc->set, "oauth" ) ) |
---|
[713d611] | 214 | twitter_main_loop_start( ic ); |
---|
| 215 | else |
---|
| 216 | twitter_oauth_start( ic ); |
---|
[1b221e0] | 217 | } |
---|
| 218 | |
---|
| 219 | /** |
---|
| 220 | * Logout method. Just free the twitter_data. |
---|
| 221 | */ |
---|
| 222 | static void twitter_logout( struct im_connection *ic ) |
---|
| 223 | { |
---|
| 224 | struct twitter_data *td = ic->proto_data; |
---|
| 225 | |
---|
| 226 | // Set the status to logged out. |
---|
| 227 | ic->flags = 0; |
---|
| 228 | |
---|
[2abceca] | 229 | // Remove the main_loop function from the function queue. |
---|
| 230 | b_event_remove(td->main_loop_id); |
---|
| 231 | |
---|
[37d84b3] | 232 | if(td->home_timeline_gc) |
---|
| 233 | imcb_chat_free(td->home_timeline_gc); |
---|
[1014cab] | 234 | |
---|
[1b221e0] | 235 | if( td ) |
---|
| 236 | { |
---|
[18dbb20] | 237 | oauth_info_free( td->oauth_info ); |
---|
[04a927c] | 238 | g_free( td->url_host ); |
---|
| 239 | g_free( td->url_path ); |
---|
[508c340] | 240 | g_free( td->pass ); |
---|
[1b221e0] | 241 | g_free( td ); |
---|
| 242 | } |
---|
[62d2cfb] | 243 | |
---|
| 244 | twitter_connections = g_slist_remove( twitter_connections, ic ); |
---|
[1b221e0] | 245 | } |
---|
| 246 | |
---|
| 247 | /** |
---|
| 248 | * |
---|
| 249 | */ |
---|
| 250 | static int twitter_buddy_msg( struct im_connection *ic, char *who, char *message, int away ) |
---|
| 251 | { |
---|
[c42e8b9] | 252 | struct twitter_data *td = ic->proto_data; |
---|
| 253 | |
---|
[e88fbe27] | 254 | if (g_strncasecmp(who, "twitter_", 8) == 0 && |
---|
| 255 | g_strcasecmp(who + 8, ic->acc->user) == 0) |
---|
[c42e8b9] | 256 | { |
---|
[c2ecadc] | 257 | if( set_getbool( &ic->acc->set, "oauth" ) && |
---|
| 258 | td->oauth_info && td->oauth_info->token == NULL ) |
---|
[18dbb20] | 259 | { |
---|
[c2ecadc] | 260 | if( !oauth_access_token( message, td->oauth_info ) ) |
---|
| 261 | { |
---|
| 262 | imcb_error( ic, "OAuth error: %s", "Failed to send access token request" ); |
---|
| 263 | imc_logout( ic, TRUE ); |
---|
| 264 | return FALSE; |
---|
| 265 | } |
---|
[18dbb20] | 266 | } |
---|
[9997691] | 267 | else if( twitter_length_check(ic, message) ) |
---|
[c42e8b9] | 268 | twitter_post_status(ic, message); |
---|
| 269 | } |
---|
[e88fbe27] | 270 | else |
---|
[c42e8b9] | 271 | { |
---|
[e88fbe27] | 272 | twitter_direct_messages_new(ic, who, message); |
---|
[c42e8b9] | 273 | } |
---|
[1b221e0] | 274 | return( 0 ); |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | /** |
---|
| 278 | * |
---|
| 279 | */ |
---|
| 280 | static void twitter_set_my_name( struct im_connection *ic, char *info ) |
---|
| 281 | { |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | static void twitter_get_info(struct im_connection *ic, char *who) |
---|
| 285 | { |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | static void twitter_add_buddy( struct im_connection *ic, char *who, char *group ) |
---|
| 289 | { |
---|
[7d53efb] | 290 | twitter_friendships_create_destroy(ic, who, 1); |
---|
[1b221e0] | 291 | } |
---|
| 292 | |
---|
| 293 | static void twitter_remove_buddy( struct im_connection *ic, char *who, char *group ) |
---|
| 294 | { |
---|
[7d53efb] | 295 | twitter_friendships_create_destroy(ic, who, 0); |
---|
[1b221e0] | 296 | } |
---|
| 297 | |
---|
| 298 | static void twitter_chat_msg( struct groupchat *c, char *message, int flags ) |
---|
| 299 | { |
---|
[9997691] | 300 | if( c && message && twitter_length_check(c->ic, message)) |
---|
[2abceca] | 301 | twitter_post_status(c->ic, message); |
---|
[1b221e0] | 302 | } |
---|
| 303 | |
---|
| 304 | static void twitter_chat_invite( struct groupchat *c, char *who, char *message ) |
---|
| 305 | { |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | static void twitter_chat_leave( struct groupchat *c ) |
---|
| 309 | { |
---|
[16592d8] | 310 | struct twitter_data *td = c->ic->proto_data; |
---|
| 311 | |
---|
| 312 | if( c != td->home_timeline_gc ) |
---|
| 313 | return; /* WTF? */ |
---|
| 314 | |
---|
| 315 | /* If the user leaves the channel: Fine. Rejoin him/her once new |
---|
| 316 | tweets come in. */ |
---|
| 317 | imcb_chat_free(td->home_timeline_gc); |
---|
| 318 | td->home_timeline_gc = NULL; |
---|
[1b221e0] | 319 | } |
---|
| 320 | |
---|
| 321 | static struct groupchat *twitter_chat_with( struct im_connection *ic, char *who ) |
---|
| 322 | { |
---|
| 323 | return NULL; |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | static void twitter_keepalive( struct im_connection *ic ) |
---|
| 327 | { |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | static void twitter_add_permit( struct im_connection *ic, char *who ) |
---|
| 331 | { |
---|
| 332 | } |
---|
| 333 | |
---|
| 334 | static void twitter_rem_permit( struct im_connection *ic, char *who ) |
---|
| 335 | { |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | static void twitter_add_deny( struct im_connection *ic, char *who ) |
---|
| 339 | { |
---|
| 340 | } |
---|
| 341 | |
---|
| 342 | static void twitter_rem_deny( struct im_connection *ic, char *who ) |
---|
| 343 | { |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | static int twitter_send_typing( struct im_connection *ic, char *who, int typing ) |
---|
| 347 | { |
---|
| 348 | return( 1 ); |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | //static char *twitter_set_display_name( set_t *set, char *value ) |
---|
| 352 | //{ |
---|
| 353 | // return value; |
---|
| 354 | //} |
---|
| 355 | |
---|
| 356 | void twitter_initmodule() |
---|
| 357 | { |
---|
| 358 | struct prpl *ret = g_new0(struct prpl, 1); |
---|
| 359 | |
---|
| 360 | ret->name = "twitter"; |
---|
| 361 | ret->login = twitter_login; |
---|
| 362 | ret->init = twitter_init; |
---|
| 363 | ret->logout = twitter_logout; |
---|
| 364 | ret->buddy_msg = twitter_buddy_msg; |
---|
| 365 | ret->get_info = twitter_get_info; |
---|
| 366 | ret->set_my_name = twitter_set_my_name; |
---|
| 367 | ret->add_buddy = twitter_add_buddy; |
---|
| 368 | ret->remove_buddy = twitter_remove_buddy; |
---|
| 369 | ret->chat_msg = twitter_chat_msg; |
---|
| 370 | ret->chat_invite = twitter_chat_invite; |
---|
| 371 | ret->chat_leave = twitter_chat_leave; |
---|
| 372 | ret->chat_with = twitter_chat_with; |
---|
| 373 | ret->keepalive = twitter_keepalive; |
---|
| 374 | ret->add_permit = twitter_add_permit; |
---|
| 375 | ret->rem_permit = twitter_rem_permit; |
---|
| 376 | ret->add_deny = twitter_add_deny; |
---|
| 377 | ret->rem_deny = twitter_rem_deny; |
---|
| 378 | ret->send_typing = twitter_send_typing; |
---|
| 379 | ret->handle_cmp = g_strcasecmp; |
---|
| 380 | |
---|
| 381 | register_protocol(ret); |
---|
[62d2cfb] | 382 | |
---|
| 383 | // Initialise the twitter_connections GSList. |
---|
| 384 | twitter_connections = NULL; |
---|
[1b221e0] | 385 | } |
---|
| 386 | |
---|