[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 | |
---|
[665c24f] | 31 | #define twitter_msg( ic, fmt... ) \ |
---|
| 32 | do { \ |
---|
| 33 | struct twitter_data *td = ic->proto_data; \ |
---|
| 34 | if( td->home_timeline_gc ) \ |
---|
| 35 | imcb_chat_log( td->home_timeline_gc, fmt ); \ |
---|
| 36 | else \ |
---|
| 37 | imcb_log( ic, fmt ); \ |
---|
| 38 | } while( 0 ); |
---|
| 39 | |
---|
[9c9a29c] | 40 | GSList *twitter_connections = NULL; |
---|
[665c24f] | 41 | |
---|
[1b221e0] | 42 | /** |
---|
[62d2cfb] | 43 | * Main loop function |
---|
| 44 | */ |
---|
[1b221e0] | 45 | gboolean twitter_main_loop(gpointer data, gint fd, b_input_condition cond) |
---|
| 46 | { |
---|
| 47 | struct im_connection *ic = data; |
---|
[3e69802] | 48 | |
---|
[1b221e0] | 49 | // Check if we are still logged in... |
---|
[3bd4a93] | 50 | if (!g_slist_find( twitter_connections, ic )) |
---|
[1b221e0] | 51 | return 0; |
---|
| 52 | |
---|
| 53 | // Do stuff.. |
---|
| 54 | twitter_get_home_timeline(ic, -1); |
---|
| 55 | |
---|
| 56 | // If we are still logged in run this function again after timeout. |
---|
| 57 | return (ic->flags & OPT_LOGGED_IN) == OPT_LOGGED_IN; |
---|
| 58 | } |
---|
| 59 | |
---|
[713d611] | 60 | static void twitter_main_loop_start( struct im_connection *ic ) |
---|
| 61 | { |
---|
| 62 | struct twitter_data *td = ic->proto_data; |
---|
| 63 | |
---|
[d6aa6dd] | 64 | imcb_log( ic, "Getting initial statuses" ); |
---|
[713d611] | 65 | |
---|
| 66 | // Run this once. After this queue the main loop function. |
---|
| 67 | twitter_main_loop(ic, -1, 0); |
---|
| 68 | |
---|
| 69 | // Queue the main_loop |
---|
| 70 | // Save the return value, so we can remove the timeout on logout. |
---|
| 71 | td->main_loop_id = b_timeout_add(60000, twitter_main_loop, ic); |
---|
| 72 | } |
---|
| 73 | |
---|
[d6aa6dd] | 74 | static void twitter_oauth_start( struct im_connection *ic ); |
---|
| 75 | |
---|
| 76 | void twitter_login_finish( struct im_connection *ic ) |
---|
| 77 | { |
---|
| 78 | struct twitter_data *td = ic->proto_data; |
---|
| 79 | |
---|
| 80 | if( set_getbool( &ic->acc->set, "oauth" ) && !td->oauth_info ) |
---|
| 81 | twitter_oauth_start( ic ); |
---|
| 82 | else if( g_strcasecmp( set_getstr( &ic->acc->set, "mode" ), "one" ) != 0 && |
---|
| 83 | !( td->flags & TWITTER_HAVE_FRIENDS ) ) |
---|
| 84 | { |
---|
| 85 | imcb_log( ic, "Getting contact list" ); |
---|
| 86 | twitter_get_statuses_friends( ic, -1 ); |
---|
| 87 | } |
---|
| 88 | else |
---|
| 89 | twitter_main_loop_start( ic ); |
---|
| 90 | } |
---|
[c2ecadc] | 91 | |
---|
[3b878a1] | 92 | static const struct oauth_service twitter_oauth = |
---|
[c2ecadc] | 93 | { |
---|
| 94 | "http://api.twitter.com/oauth/request_token", |
---|
| 95 | "http://api.twitter.com/oauth/access_token", |
---|
[c01bbd1] | 96 | "https://api.twitter.com/oauth/authorize", |
---|
[c2ecadc] | 97 | .consumer_key = "xsDNKJuNZYkZyMcu914uEA", |
---|
| 98 | .consumer_secret = "FCxqcr0pXKzsF9ajmP57S3VQ8V6Drk4o2QYtqMcOszo", |
---|
| 99 | }; |
---|
| 100 | |
---|
[18dbb20] | 101 | static gboolean twitter_oauth_callback( struct oauth_info *info ); |
---|
[713d611] | 102 | |
---|
| 103 | static void twitter_oauth_start( struct im_connection *ic ) |
---|
| 104 | { |
---|
[18dbb20] | 105 | struct twitter_data *td = ic->proto_data; |
---|
| 106 | |
---|
[c42e8b9] | 107 | imcb_log( ic, "Requesting OAuth request token" ); |
---|
| 108 | |
---|
[c2ecadc] | 109 | td->oauth_info = oauth_request_token( &twitter_oauth, twitter_oauth_callback, ic ); |
---|
[713d611] | 110 | } |
---|
| 111 | |
---|
[18dbb20] | 112 | static gboolean twitter_oauth_callback( struct oauth_info *info ) |
---|
[713d611] | 113 | { |
---|
| 114 | struct im_connection *ic = info->data; |
---|
[18dbb20] | 115 | struct twitter_data *td; |
---|
[713d611] | 116 | |
---|
[18dbb20] | 117 | if( !g_slist_find( twitter_connections, ic ) ) |
---|
| 118 | return FALSE; |
---|
| 119 | |
---|
| 120 | td = ic->proto_data; |
---|
[c42e8b9] | 121 | if( info->stage == OAUTH_REQUEST_TOKEN ) |
---|
[713d611] | 122 | { |
---|
| 123 | char name[strlen(ic->acc->user)+9], *msg; |
---|
| 124 | |
---|
[c42e8b9] | 125 | if( info->request_token == NULL ) |
---|
| 126 | { |
---|
| 127 | imcb_error( ic, "OAuth error: %s", info->http->status_string ); |
---|
| 128 | imc_logout( ic, TRUE ); |
---|
[18dbb20] | 129 | return FALSE; |
---|
[c42e8b9] | 130 | } |
---|
| 131 | |
---|
[ffcdf13] | 132 | sprintf( name, "%s_%s", td->prefix, ic->acc->user ); |
---|
[713d611] | 133 | msg = g_strdup_printf( "To finish OAuth authentication, please visit " |
---|
[c2ecadc] | 134 | "%s and respond with the resulting PIN code.", |
---|
| 135 | info->auth_url ); |
---|
[713d611] | 136 | imcb_buddy_msg( ic, name, msg, 0, 0 ); |
---|
| 137 | g_free( msg ); |
---|
| 138 | } |
---|
[c42e8b9] | 139 | else if( info->stage == OAUTH_ACCESS_TOKEN ) |
---|
| 140 | { |
---|
[3b878a1] | 141 | if( info->token == NULL || info->token_secret == NULL ) |
---|
[c42e8b9] | 142 | { |
---|
| 143 | imcb_error( ic, "OAuth error: %s", info->http->status_string ); |
---|
| 144 | imc_logout( ic, TRUE ); |
---|
[18dbb20] | 145 | return FALSE; |
---|
[c42e8b9] | 146 | } |
---|
| 147 | |
---|
[288b215] | 148 | /* IM mods didn't do this so far and it's ugly but I should |
---|
| 149 | be able to get away with it... */ |
---|
| 150 | g_free( ic->acc->pass ); |
---|
[f4b0911] | 151 | ic->acc->pass = oauth_to_string( info ); |
---|
[288b215] | 152 | |
---|
[d6aa6dd] | 153 | twitter_login_finish( ic ); |
---|
[c42e8b9] | 154 | } |
---|
[18dbb20] | 155 | |
---|
| 156 | return TRUE; |
---|
[713d611] | 157 | } |
---|
| 158 | |
---|
[c2ecadc] | 159 | |
---|
[e88fbe27] | 160 | static char *set_eval_mode( set_t *set, char *value ) |
---|
| 161 | { |
---|
| 162 | if( g_strcasecmp( value, "one" ) == 0 || |
---|
| 163 | g_strcasecmp( value, "many" ) == 0 || |
---|
[db57e7c] | 164 | g_strcasecmp( value, "chat" ) == 0 ) |
---|
[e88fbe27] | 165 | return value; |
---|
| 166 | else |
---|
| 167 | return NULL; |
---|
| 168 | } |
---|
[1b221e0] | 169 | |
---|
[9997691] | 170 | static gboolean twitter_length_check( struct im_connection *ic, gchar *msg ) |
---|
| 171 | { |
---|
| 172 | int max = set_getint( &ic->acc->set, "message_length" ), len; |
---|
| 173 | |
---|
| 174 | if( max == 0 || ( len = g_utf8_strlen( msg, -1 ) ) <= max ) |
---|
| 175 | return TRUE; |
---|
| 176 | |
---|
| 177 | imcb_error( ic, "Maximum message length exceeded: %d > %d", len, max ); |
---|
| 178 | |
---|
| 179 | return FALSE; |
---|
| 180 | } |
---|
| 181 | |
---|
[1b221e0] | 182 | static void twitter_init( account_t *acc ) |
---|
| 183 | { |
---|
[62d2cfb] | 184 | set_t *s; |
---|
[ffcdf13] | 185 | char *def_url; |
---|
| 186 | char *def_oauth; |
---|
[e88fbe27] | 187 | |
---|
[ffcdf13] | 188 | if( strcmp( acc->prpl->name, "twitter" ) == 0 ) |
---|
| 189 | { |
---|
| 190 | def_url = TWITTER_API_URL; |
---|
| 191 | def_oauth = "true"; |
---|
| 192 | } |
---|
| 193 | else /* if( strcmp( acc->prpl->name, "identica" ) == 0 ) */ |
---|
| 194 | { |
---|
| 195 | def_url = IDENTICA_API_URL; |
---|
| 196 | def_oauth = "false"; |
---|
| 197 | } |
---|
| 198 | |
---|
[b890626] | 199 | s = set_add( &acc->set, "auto_reply_timeout", "10800", set_eval_int, acc ); |
---|
| 200 | |
---|
[ffcdf13] | 201 | s = set_add( &acc->set, "base_url", def_url, NULL, acc ); |
---|
[bb5ce4d1] | 202 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
| 203 | |
---|
[7b87539] | 204 | s = set_add( &acc->set, "commands", "true", set_eval_bool, acc ); |
---|
| 205 | |
---|
[9997691] | 206 | s = set_add( &acc->set, "message_length", "140", set_eval_int, acc ); |
---|
| 207 | |
---|
[c55701e] | 208 | s = set_add( &acc->set, "mode", "chat", set_eval_mode, acc ); |
---|
[2abceca] | 209 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[713d611] | 210 | |
---|
[ffcdf13] | 211 | s = set_add( &acc->set, "oauth", def_oauth, set_eval_bool, acc ); |
---|
[1b221e0] | 212 | } |
---|
| 213 | |
---|
| 214 | /** |
---|
| 215 | * Login method. Since the twitter API works with seperate HTTP request we |
---|
| 216 | * only save the user and pass to the twitter_data object. |
---|
| 217 | */ |
---|
| 218 | static void twitter_login( account_t *acc ) |
---|
| 219 | { |
---|
| 220 | struct im_connection *ic = imcb_new( acc ); |
---|
[bb5ce4d1] | 221 | struct twitter_data *td; |
---|
[e88fbe27] | 222 | char name[strlen(acc->user)+9]; |
---|
[bb5ce4d1] | 223 | url_t url; |
---|
[62d2cfb] | 224 | |
---|
[bb5ce4d1] | 225 | if( !url_set( &url, set_getstr( &ic->acc->set, "base_url" ) ) || |
---|
| 226 | ( url.proto != PROTO_HTTP && url.proto != PROTO_HTTPS ) ) |
---|
| 227 | { |
---|
| 228 | imcb_error( ic, "Incorrect API base URL: %s", set_getstr( &ic->acc->set, "base_url" ) ); |
---|
| 229 | imc_logout( ic, FALSE ); |
---|
| 230 | return; |
---|
| 231 | } |
---|
| 232 | |
---|
[d569019] | 233 | twitter_connections = g_slist_append( twitter_connections, ic ); |
---|
[bb5ce4d1] | 234 | td = g_new0( struct twitter_data, 1 ); |
---|
[713d611] | 235 | ic->proto_data = td; |
---|
| 236 | |
---|
[bb5ce4d1] | 237 | td->url_ssl = url.proto == PROTO_HTTPS; |
---|
| 238 | td->url_port = url.port; |
---|
| 239 | td->url_host = g_strdup( url.host ); |
---|
| 240 | if( strcmp( url.file, "/" ) != 0 ) |
---|
| 241 | td->url_path = g_strdup( url.file ); |
---|
| 242 | else |
---|
| 243 | td->url_path = g_strdup( "" ); |
---|
[ffcdf13] | 244 | if( g_str_has_suffix( url.host, ".com" ) ) |
---|
| 245 | td->prefix = g_strndup( url.host, strlen( url.host ) - 4 ); |
---|
| 246 | else |
---|
| 247 | td->prefix = g_strdup( url.host ); |
---|
[bb5ce4d1] | 248 | |
---|
[1b221e0] | 249 | td->user = acc->user; |
---|
[bb5ce4d1] | 250 | if( strstr( acc->pass, "oauth_token=" ) ) |
---|
[f4b0911] | 251 | td->oauth_info = oauth_from_string( acc->pass, &twitter_oauth ); |
---|
[e88fbe27] | 252 | |
---|
[ffcdf13] | 253 | sprintf( name, "%s_%s", td->prefix, acc->user ); |
---|
[e88fbe27] | 254 | imcb_add_buddy( ic, name, NULL ); |
---|
| 255 | imcb_buddy_status( ic, name, OPT_LOGGED_IN, NULL, NULL ); |
---|
[713d611] | 256 | |
---|
[d6aa6dd] | 257 | imcb_log( ic, "Connecting" ); |
---|
| 258 | |
---|
| 259 | twitter_login_finish( ic ); |
---|
[1b221e0] | 260 | } |
---|
| 261 | |
---|
| 262 | /** |
---|
| 263 | * Logout method. Just free the twitter_data. |
---|
| 264 | */ |
---|
| 265 | static void twitter_logout( struct im_connection *ic ) |
---|
| 266 | { |
---|
| 267 | struct twitter_data *td = ic->proto_data; |
---|
| 268 | |
---|
| 269 | // Set the status to logged out. |
---|
[4ffd757] | 270 | ic->flags &= ~ OPT_LOGGED_IN; |
---|
[1b221e0] | 271 | |
---|
[2abceca] | 272 | // Remove the main_loop function from the function queue. |
---|
| 273 | b_event_remove(td->main_loop_id); |
---|
| 274 | |
---|
[37d84b3] | 275 | if(td->home_timeline_gc) |
---|
| 276 | imcb_chat_free(td->home_timeline_gc); |
---|
[1014cab] | 277 | |
---|
[1b221e0] | 278 | if( td ) |
---|
| 279 | { |
---|
[18dbb20] | 280 | oauth_info_free( td->oauth_info ); |
---|
[ffcdf13] | 281 | g_free( td->prefix ); |
---|
[04a927c] | 282 | g_free( td->url_host ); |
---|
| 283 | g_free( td->url_path ); |
---|
[508c340] | 284 | g_free( td->pass ); |
---|
[1b221e0] | 285 | g_free( td ); |
---|
| 286 | } |
---|
[62d2cfb] | 287 | |
---|
| 288 | twitter_connections = g_slist_remove( twitter_connections, ic ); |
---|
[1b221e0] | 289 | } |
---|
| 290 | |
---|
[7b87539] | 291 | static void twitter_handle_command( struct im_connection *ic, char *message ); |
---|
| 292 | |
---|
[1b221e0] | 293 | /** |
---|
| 294 | * |
---|
| 295 | */ |
---|
| 296 | static int twitter_buddy_msg( struct im_connection *ic, char *who, char *message, int away ) |
---|
| 297 | { |
---|
[c42e8b9] | 298 | struct twitter_data *td = ic->proto_data; |
---|
[ffcdf13] | 299 | int plen = strlen( td->prefix ); |
---|
[c42e8b9] | 300 | |
---|
[ffcdf13] | 301 | if (g_strncasecmp(who, td->prefix, plen) == 0 && who[plen] == '_' && |
---|
| 302 | g_strcasecmp(who + plen + 1, ic->acc->user) == 0) |
---|
[c42e8b9] | 303 | { |
---|
[c2ecadc] | 304 | if( set_getbool( &ic->acc->set, "oauth" ) && |
---|
| 305 | td->oauth_info && td->oauth_info->token == NULL ) |
---|
[18dbb20] | 306 | { |
---|
[64f8c425] | 307 | char pin[strlen(message)+1], *s; |
---|
| 308 | |
---|
| 309 | strcpy( pin, message ); |
---|
| 310 | for( s = pin + sizeof( pin ) - 2; s > pin && isspace( *s ); s -- ) |
---|
| 311 | *s = '\0'; |
---|
| 312 | for( s = pin; *s && isspace( *s ); s ++ ) {} |
---|
| 313 | |
---|
| 314 | if( !oauth_access_token( s, td->oauth_info ) ) |
---|
[c2ecadc] | 315 | { |
---|
| 316 | imcb_error( ic, "OAuth error: %s", "Failed to send access token request" ); |
---|
| 317 | imc_logout( ic, TRUE ); |
---|
| 318 | return FALSE; |
---|
| 319 | } |
---|
[18dbb20] | 320 | } |
---|
[7b87539] | 321 | else |
---|
| 322 | twitter_handle_command(ic, message); |
---|
[c42e8b9] | 323 | } |
---|
[e88fbe27] | 324 | else |
---|
[c42e8b9] | 325 | { |
---|
[e88fbe27] | 326 | twitter_direct_messages_new(ic, who, message); |
---|
[c42e8b9] | 327 | } |
---|
[1b221e0] | 328 | return( 0 ); |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | /** |
---|
| 332 | * |
---|
| 333 | */ |
---|
| 334 | static void twitter_set_my_name( struct im_connection *ic, char *info ) |
---|
| 335 | { |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | static void twitter_get_info(struct im_connection *ic, char *who) |
---|
| 339 | { |
---|
| 340 | } |
---|
| 341 | |
---|
| 342 | static void twitter_add_buddy( struct im_connection *ic, char *who, char *group ) |
---|
| 343 | { |
---|
[7d53efb] | 344 | twitter_friendships_create_destroy(ic, who, 1); |
---|
[1b221e0] | 345 | } |
---|
| 346 | |
---|
| 347 | static void twitter_remove_buddy( struct im_connection *ic, char *who, char *group ) |
---|
| 348 | { |
---|
[7d53efb] | 349 | twitter_friendships_create_destroy(ic, who, 0); |
---|
[1b221e0] | 350 | } |
---|
| 351 | |
---|
| 352 | static void twitter_chat_msg( struct groupchat *c, char *message, int flags ) |
---|
| 353 | { |
---|
[7b87539] | 354 | if( c && message ) |
---|
| 355 | twitter_handle_command( c->ic, message ); |
---|
[1b221e0] | 356 | } |
---|
| 357 | |
---|
| 358 | static void twitter_chat_invite( struct groupchat *c, char *who, char *message ) |
---|
| 359 | { |
---|
| 360 | } |
---|
| 361 | |
---|
| 362 | static void twitter_chat_leave( struct groupchat *c ) |
---|
| 363 | { |
---|
[16592d8] | 364 | struct twitter_data *td = c->ic->proto_data; |
---|
| 365 | |
---|
| 366 | if( c != td->home_timeline_gc ) |
---|
| 367 | return; /* WTF? */ |
---|
| 368 | |
---|
| 369 | /* If the user leaves the channel: Fine. Rejoin him/her once new |
---|
| 370 | tweets come in. */ |
---|
| 371 | imcb_chat_free(td->home_timeline_gc); |
---|
| 372 | td->home_timeline_gc = NULL; |
---|
[1b221e0] | 373 | } |
---|
| 374 | |
---|
| 375 | static struct groupchat *twitter_chat_with( struct im_connection *ic, char *who ) |
---|
| 376 | { |
---|
| 377 | return NULL; |
---|
| 378 | } |
---|
| 379 | |
---|
| 380 | static void twitter_keepalive( struct im_connection *ic ) |
---|
| 381 | { |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | static void twitter_add_permit( struct im_connection *ic, char *who ) |
---|
| 385 | { |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | static void twitter_rem_permit( struct im_connection *ic, char *who ) |
---|
| 389 | { |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | static void twitter_add_deny( struct im_connection *ic, char *who ) |
---|
| 393 | { |
---|
| 394 | } |
---|
| 395 | |
---|
| 396 | static void twitter_rem_deny( struct im_connection *ic, char *who ) |
---|
| 397 | { |
---|
| 398 | } |
---|
| 399 | |
---|
| 400 | static int twitter_send_typing( struct im_connection *ic, char *who, int typing ) |
---|
| 401 | { |
---|
| 402 | return( 1 ); |
---|
| 403 | } |
---|
| 404 | |
---|
| 405 | //static char *twitter_set_display_name( set_t *set, char *value ) |
---|
| 406 | //{ |
---|
| 407 | // return value; |
---|
| 408 | //} |
---|
[7b87539] | 409 | |
---|
[203a2d2] | 410 | static void twitter_buddy_data_add( struct bee_user *bu ) |
---|
| 411 | { |
---|
| 412 | bu->data = g_new0( struct twitter_user_data, 1 ); |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | static void twitter_buddy_data_free( struct bee_user *bu ) |
---|
| 416 | { |
---|
| 417 | g_free( bu->data ); |
---|
| 418 | } |
---|
| 419 | |
---|
[7b87539] | 420 | static void twitter_handle_command( struct im_connection *ic, char *message ) |
---|
| 421 | { |
---|
| 422 | struct twitter_data *td = ic->proto_data; |
---|
| 423 | char *cmds, **cmd; |
---|
| 424 | |
---|
| 425 | cmds = g_strdup( message ); |
---|
| 426 | cmd = split_command_parts( cmds ); |
---|
| 427 | |
---|
| 428 | if( cmd[0] == NULL ) |
---|
| 429 | { |
---|
| 430 | g_free( cmds ); |
---|
| 431 | return; |
---|
| 432 | } |
---|
[203a2d2] | 433 | else if( !set_getbool( &ic->acc->set, "commands" ) ) |
---|
[7b87539] | 434 | { |
---|
| 435 | /* Not supporting commands. */ |
---|
| 436 | } |
---|
| 437 | else if( g_strcasecmp( cmd[0], "undo" ) == 0 ) |
---|
| 438 | { |
---|
| 439 | guint64 id; |
---|
| 440 | |
---|
| 441 | if( cmd[1] ) |
---|
[b890626] | 442 | id = g_ascii_strtoull( cmd[1], NULL, 10 ); |
---|
[7b87539] | 443 | else |
---|
| 444 | id = td->last_status_id; |
---|
| 445 | |
---|
| 446 | /* TODO: User feedback. */ |
---|
| 447 | if( id ) |
---|
| 448 | twitter_status_destroy( ic, id ); |
---|
[665c24f] | 449 | else |
---|
| 450 | twitter_msg( ic, "Could not undo last action" ); |
---|
[7b87539] | 451 | |
---|
| 452 | g_free( cmds ); |
---|
| 453 | return; |
---|
| 454 | } |
---|
[b890626] | 455 | else if( g_strcasecmp( cmd[0], "follow" ) == 0 && cmd[1] ) |
---|
| 456 | { |
---|
| 457 | twitter_add_buddy( ic, cmd[1], NULL ); |
---|
| 458 | g_free( cmds ); |
---|
| 459 | return; |
---|
| 460 | } |
---|
| 461 | else if( g_strcasecmp( cmd[0], "unfollow" ) == 0 && cmd[1] ) |
---|
| 462 | { |
---|
| 463 | twitter_remove_buddy( ic, cmd[1], NULL ); |
---|
| 464 | g_free( cmds ); |
---|
| 465 | return; |
---|
| 466 | } |
---|
| 467 | else if( g_strcasecmp( cmd[0], "rt" ) == 0 && cmd[1] ) |
---|
| 468 | { |
---|
| 469 | struct twitter_user_data *tud; |
---|
| 470 | bee_user_t *bu; |
---|
| 471 | guint64 id; |
---|
| 472 | |
---|
| 473 | if( ( bu = bee_user_by_handle( ic->bee, ic, cmd[1] ) ) && |
---|
| 474 | ( tud = bu->data ) && tud->last_id ) |
---|
| 475 | id = tud->last_id; |
---|
| 476 | else |
---|
| 477 | id = g_ascii_strtoull( cmd[1], NULL, 10 ); |
---|
| 478 | |
---|
| 479 | td->last_status_id = 0; |
---|
| 480 | if( id ) |
---|
| 481 | twitter_status_retweet( ic, id ); |
---|
[665c24f] | 482 | else |
---|
| 483 | twitter_msg( ic, "User `%s' does not exist or didn't " |
---|
| 484 | "post any statuses recently", cmd[1] ); |
---|
[b890626] | 485 | |
---|
| 486 | g_free( cmds ); |
---|
| 487 | return; |
---|
| 488 | } |
---|
[7b87539] | 489 | else if( g_strcasecmp( cmd[0], "post" ) == 0 ) |
---|
| 490 | { |
---|
| 491 | message += 5; |
---|
| 492 | } |
---|
| 493 | |
---|
| 494 | { |
---|
[b890626] | 495 | guint64 in_reply_to = 0; |
---|
[7b87539] | 496 | char *s, *new = NULL; |
---|
| 497 | bee_user_t *bu; |
---|
| 498 | |
---|
| 499 | if( !twitter_length_check( ic, message ) ) |
---|
| 500 | { |
---|
| 501 | g_free( cmds ); |
---|
| 502 | return; |
---|
| 503 | } |
---|
| 504 | |
---|
| 505 | s = cmd[0] + strlen( cmd[0] ) - 1; |
---|
| 506 | if( s > cmd[0] && ( *s == ':' || *s == ',' ) ) |
---|
| 507 | { |
---|
| 508 | *s = '\0'; |
---|
| 509 | |
---|
| 510 | if( ( bu = bee_user_by_handle( ic->bee, ic, cmd[0] ) ) ) |
---|
| 511 | { |
---|
[b890626] | 512 | struct twitter_user_data *tud = bu->data; |
---|
| 513 | |
---|
[7b87539] | 514 | new = g_strdup_printf( "@%s %s", bu->handle, |
---|
| 515 | message + ( s - cmd[0] ) + 2 ); |
---|
| 516 | message = new; |
---|
[b890626] | 517 | |
---|
| 518 | if( time( NULL ) < tud->last_time + |
---|
| 519 | set_getint( &ic->acc->set, "auto_reply_timeout" ) ) |
---|
| 520 | in_reply_to = tud->last_id; |
---|
[7b87539] | 521 | } |
---|
| 522 | } |
---|
| 523 | |
---|
[b890626] | 524 | /* If the user runs undo between this request and its response |
---|
| 525 | this would delete the second-last Tweet. Prevent that. */ |
---|
| 526 | td->last_status_id = 0; |
---|
| 527 | twitter_post_status( ic, message, in_reply_to ); |
---|
[7b87539] | 528 | g_free( new ); |
---|
| 529 | } |
---|
| 530 | g_free( cmds ); |
---|
| 531 | } |
---|
[1b221e0] | 532 | |
---|
| 533 | void twitter_initmodule() |
---|
| 534 | { |
---|
| 535 | struct prpl *ret = g_new0(struct prpl, 1); |
---|
| 536 | |
---|
[1dd3470] | 537 | ret->options = OPT_NOOTR; |
---|
[1b221e0] | 538 | ret->name = "twitter"; |
---|
| 539 | ret->login = twitter_login; |
---|
| 540 | ret->init = twitter_init; |
---|
| 541 | ret->logout = twitter_logout; |
---|
| 542 | ret->buddy_msg = twitter_buddy_msg; |
---|
| 543 | ret->get_info = twitter_get_info; |
---|
| 544 | ret->set_my_name = twitter_set_my_name; |
---|
| 545 | ret->add_buddy = twitter_add_buddy; |
---|
| 546 | ret->remove_buddy = twitter_remove_buddy; |
---|
| 547 | ret->chat_msg = twitter_chat_msg; |
---|
| 548 | ret->chat_invite = twitter_chat_invite; |
---|
| 549 | ret->chat_leave = twitter_chat_leave; |
---|
| 550 | ret->chat_with = twitter_chat_with; |
---|
| 551 | ret->keepalive = twitter_keepalive; |
---|
| 552 | ret->add_permit = twitter_add_permit; |
---|
| 553 | ret->rem_permit = twitter_rem_permit; |
---|
| 554 | ret->add_deny = twitter_add_deny; |
---|
| 555 | ret->rem_deny = twitter_rem_deny; |
---|
| 556 | ret->send_typing = twitter_send_typing; |
---|
[203a2d2] | 557 | ret->buddy_data_add = twitter_buddy_data_add; |
---|
| 558 | ret->buddy_data_free = twitter_buddy_data_free; |
---|
[1b221e0] | 559 | ret->handle_cmp = g_strcasecmp; |
---|
[ffcdf13] | 560 | |
---|
| 561 | register_protocol(ret); |
---|
[1b221e0] | 562 | |
---|
[ffcdf13] | 563 | /* And an identi.ca variant: */ |
---|
| 564 | ret = g_memdup(ret, sizeof(struct prpl)); |
---|
| 565 | ret->name = "identica"; |
---|
[1b221e0] | 566 | register_protocol(ret); |
---|
| 567 | } |
---|