[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" |
---|
| 29 | |
---|
| 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 | |
---|
[18dbb20] | 68 | static gboolean twitter_oauth_callback( struct oauth_info *info ); |
---|
[713d611] | 69 | |
---|
| 70 | static void twitter_oauth_start( struct im_connection *ic ) |
---|
| 71 | { |
---|
[18dbb20] | 72 | struct twitter_data *td = ic->proto_data; |
---|
| 73 | |
---|
[c42e8b9] | 74 | imcb_log( ic, "Requesting OAuth request token" ); |
---|
| 75 | |
---|
[18dbb20] | 76 | td->oauth_info = oauth_request_token( |
---|
| 77 | TWITTER_OAUTH_REQUEST_TOKEN, twitter_oauth_callback, ic ); |
---|
[713d611] | 78 | } |
---|
| 79 | |
---|
[18dbb20] | 80 | static gboolean twitter_oauth_callback( struct oauth_info *info ) |
---|
[713d611] | 81 | { |
---|
| 82 | struct im_connection *ic = info->data; |
---|
[18dbb20] | 83 | struct twitter_data *td; |
---|
[713d611] | 84 | |
---|
[18dbb20] | 85 | if( !g_slist_find( twitter_connections, ic ) ) |
---|
| 86 | return FALSE; |
---|
| 87 | |
---|
| 88 | td = ic->proto_data; |
---|
[c42e8b9] | 89 | if( info->stage == OAUTH_REQUEST_TOKEN ) |
---|
[713d611] | 90 | { |
---|
| 91 | char name[strlen(ic->acc->user)+9], *msg; |
---|
| 92 | |
---|
[c42e8b9] | 93 | if( info->request_token == NULL ) |
---|
| 94 | { |
---|
| 95 | imcb_error( ic, "OAuth error: %s", info->http->status_string ); |
---|
| 96 | imc_logout( ic, TRUE ); |
---|
[18dbb20] | 97 | return FALSE; |
---|
[c42e8b9] | 98 | } |
---|
| 99 | |
---|
[713d611] | 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 | } |
---|
[c42e8b9] | 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 ); |
---|
[18dbb20] | 113 | return FALSE; |
---|
[c42e8b9] | 114 | } |
---|
| 115 | |
---|
| 116 | td->oauth = g_strdup( info->access_token ); |
---|
| 117 | |
---|
[288b215] | 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 | |
---|
[c42e8b9] | 123 | twitter_main_loop_start( ic ); |
---|
| 124 | } |
---|
[18dbb20] | 125 | |
---|
| 126 | return TRUE; |
---|
[713d611] | 127 | } |
---|
| 128 | |
---|
[e88fbe27] | 129 | static char *set_eval_mode( set_t *set, char *value ) |
---|
| 130 | { |
---|
| 131 | if( g_strcasecmp( value, "one" ) == 0 || |
---|
| 132 | g_strcasecmp( value, "many" ) == 0 || |
---|
[db57e7c] | 133 | g_strcasecmp( value, "chat" ) == 0 ) |
---|
[e88fbe27] | 134 | return value; |
---|
| 135 | else |
---|
| 136 | return NULL; |
---|
| 137 | } |
---|
[1b221e0] | 138 | |
---|
| 139 | static void twitter_init( account_t *acc ) |
---|
| 140 | { |
---|
[62d2cfb] | 141 | set_t *s; |
---|
[e88fbe27] | 142 | |
---|
| 143 | s = set_add( &acc->set, "mode", "one", set_eval_mode, acc ); |
---|
[2abceca] | 144 | s->flags |= ACC_SET_OFFLINE_ONLY; |
---|
[713d611] | 145 | |
---|
| 146 | s = set_add( &acc->set, "oauth", "true", set_eval_bool, acc ); |
---|
[1b221e0] | 147 | } |
---|
| 148 | |
---|
| 149 | /** |
---|
| 150 | * Login method. Since the twitter API works with seperate HTTP request we |
---|
| 151 | * only save the user and pass to the twitter_data object. |
---|
| 152 | */ |
---|
| 153 | static void twitter_login( account_t *acc ) |
---|
| 154 | { |
---|
| 155 | struct im_connection *ic = imcb_new( acc ); |
---|
| 156 | struct twitter_data *td = g_new0( struct twitter_data, 1 ); |
---|
[e88fbe27] | 157 | char name[strlen(acc->user)+9]; |
---|
[62d2cfb] | 158 | |
---|
[d569019] | 159 | twitter_connections = g_slist_append( twitter_connections, ic ); |
---|
[713d611] | 160 | ic->proto_data = td; |
---|
[a7c6d0e] | 161 | ic->flags |= OPT_DOES_HTML; |
---|
[713d611] | 162 | |
---|
[1b221e0] | 163 | td->user = acc->user; |
---|
[713d611] | 164 | if( !set_getbool( &acc->set, "oauth" ) ) |
---|
[508c340] | 165 | td->pass = g_strdup( acc->pass ); |
---|
[713d611] | 166 | else if( strstr( acc->pass, "oauth_token=" ) ) |
---|
[508c340] | 167 | td->oauth = g_strdup( acc->pass ); |
---|
[1b221e0] | 168 | td->home_timeline_id = 0; |
---|
[e88fbe27] | 169 | |
---|
| 170 | sprintf( name, "twitter_%s", acc->user ); |
---|
| 171 | imcb_add_buddy( ic, name, NULL ); |
---|
| 172 | imcb_buddy_status( ic, name, OPT_LOGGED_IN, NULL, NULL ); |
---|
[713d611] | 173 | |
---|
| 174 | if( td->pass || td->oauth ) |
---|
| 175 | twitter_main_loop_start( ic ); |
---|
| 176 | else |
---|
| 177 | twitter_oauth_start( ic ); |
---|
[1b221e0] | 178 | } |
---|
| 179 | |
---|
| 180 | /** |
---|
| 181 | * Logout method. Just free the twitter_data. |
---|
| 182 | */ |
---|
| 183 | static void twitter_logout( struct im_connection *ic ) |
---|
| 184 | { |
---|
| 185 | struct twitter_data *td = ic->proto_data; |
---|
| 186 | |
---|
| 187 | // Set the status to logged out. |
---|
| 188 | ic->flags = 0; |
---|
| 189 | |
---|
[2abceca] | 190 | // Remove the main_loop function from the function queue. |
---|
| 191 | b_event_remove(td->main_loop_id); |
---|
| 192 | |
---|
[37d84b3] | 193 | if(td->home_timeline_gc) |
---|
| 194 | imcb_chat_free(td->home_timeline_gc); |
---|
[1014cab] | 195 | |
---|
[1b221e0] | 196 | if( td ) |
---|
| 197 | { |
---|
[18dbb20] | 198 | oauth_info_free( td->oauth_info ); |
---|
| 199 | |
---|
[508c340] | 200 | g_free( td->pass ); |
---|
| 201 | g_free( td->oauth ); |
---|
[1b221e0] | 202 | g_free( td ); |
---|
| 203 | } |
---|
[62d2cfb] | 204 | |
---|
| 205 | twitter_connections = g_slist_remove( twitter_connections, ic ); |
---|
[1b221e0] | 206 | } |
---|
| 207 | |
---|
| 208 | /** |
---|
| 209 | * |
---|
| 210 | */ |
---|
| 211 | static int twitter_buddy_msg( struct im_connection *ic, char *who, char *message, int away ) |
---|
| 212 | { |
---|
[c42e8b9] | 213 | struct twitter_data *td = ic->proto_data; |
---|
| 214 | |
---|
[e88fbe27] | 215 | if (g_strncasecmp(who, "twitter_", 8) == 0 && |
---|
| 216 | g_strcasecmp(who + 8, ic->acc->user) == 0) |
---|
[c42e8b9] | 217 | { |
---|
[18dbb20] | 218 | if( set_getbool( &ic->acc->set, "oauth" ) && td->oauth_info ) |
---|
| 219 | { |
---|
[c42e8b9] | 220 | oauth_access_token( TWITTER_OAUTH_ACCESS_TOKEN, message, td->oauth_info ); |
---|
[18dbb20] | 221 | td->oauth_info = NULL; |
---|
| 222 | } |
---|
[c42e8b9] | 223 | else |
---|
| 224 | twitter_post_status(ic, message); |
---|
| 225 | } |
---|
[e88fbe27] | 226 | else |
---|
[c42e8b9] | 227 | { |
---|
[e88fbe27] | 228 | twitter_direct_messages_new(ic, who, message); |
---|
[c42e8b9] | 229 | } |
---|
[1b221e0] | 230 | return( 0 ); |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | /** |
---|
| 234 | * |
---|
| 235 | */ |
---|
| 236 | static void twitter_set_my_name( struct im_connection *ic, char *info ) |
---|
| 237 | { |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | static void twitter_get_info(struct im_connection *ic, char *who) |
---|
| 241 | { |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | static void twitter_add_buddy( struct im_connection *ic, char *who, char *group ) |
---|
| 245 | { |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | static void twitter_remove_buddy( struct im_connection *ic, char *who, char *group ) |
---|
| 249 | { |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | static void twitter_chat_msg( struct groupchat *c, char *message, int flags ) |
---|
| 253 | { |
---|
[2abceca] | 254 | if( c && message ) |
---|
| 255 | twitter_post_status(c->ic, message); |
---|
[1b221e0] | 256 | } |
---|
| 257 | |
---|
| 258 | static void twitter_chat_invite( struct groupchat *c, char *who, char *message ) |
---|
| 259 | { |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | static void twitter_chat_leave( struct groupchat *c ) |
---|
| 263 | { |
---|
[16592d8] | 264 | struct twitter_data *td = c->ic->proto_data; |
---|
| 265 | |
---|
| 266 | if( c != td->home_timeline_gc ) |
---|
| 267 | return; /* WTF? */ |
---|
| 268 | |
---|
| 269 | /* If the user leaves the channel: Fine. Rejoin him/her once new |
---|
| 270 | tweets come in. */ |
---|
| 271 | imcb_chat_free(td->home_timeline_gc); |
---|
| 272 | td->home_timeline_gc = NULL; |
---|
[1b221e0] | 273 | } |
---|
| 274 | |
---|
| 275 | static struct groupchat *twitter_chat_with( struct im_connection *ic, char *who ) |
---|
| 276 | { |
---|
| 277 | return NULL; |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | static void twitter_keepalive( struct im_connection *ic ) |
---|
| 281 | { |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | static void twitter_add_permit( struct im_connection *ic, char *who ) |
---|
| 285 | { |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | static void twitter_rem_permit( struct im_connection *ic, char *who ) |
---|
| 289 | { |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | static void twitter_add_deny( struct im_connection *ic, char *who ) |
---|
| 293 | { |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | static void twitter_rem_deny( struct im_connection *ic, char *who ) |
---|
| 297 | { |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | static int twitter_send_typing( struct im_connection *ic, char *who, int typing ) |
---|
| 301 | { |
---|
| 302 | return( 1 ); |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | //static char *twitter_set_display_name( set_t *set, char *value ) |
---|
| 306 | //{ |
---|
| 307 | // return value; |
---|
| 308 | //} |
---|
| 309 | |
---|
| 310 | void twitter_initmodule() |
---|
| 311 | { |
---|
| 312 | struct prpl *ret = g_new0(struct prpl, 1); |
---|
| 313 | |
---|
| 314 | ret->name = "twitter"; |
---|
| 315 | ret->login = twitter_login; |
---|
| 316 | ret->init = twitter_init; |
---|
| 317 | ret->logout = twitter_logout; |
---|
| 318 | ret->buddy_msg = twitter_buddy_msg; |
---|
| 319 | ret->get_info = twitter_get_info; |
---|
| 320 | ret->set_my_name = twitter_set_my_name; |
---|
| 321 | ret->add_buddy = twitter_add_buddy; |
---|
| 322 | ret->remove_buddy = twitter_remove_buddy; |
---|
| 323 | ret->chat_msg = twitter_chat_msg; |
---|
| 324 | ret->chat_invite = twitter_chat_invite; |
---|
| 325 | ret->chat_leave = twitter_chat_leave; |
---|
| 326 | ret->chat_with = twitter_chat_with; |
---|
| 327 | ret->keepalive = twitter_keepalive; |
---|
| 328 | ret->add_permit = twitter_add_permit; |
---|
| 329 | ret->rem_permit = twitter_rem_permit; |
---|
| 330 | ret->add_deny = twitter_add_deny; |
---|
| 331 | ret->rem_deny = twitter_rem_deny; |
---|
| 332 | ret->send_typing = twitter_send_typing; |
---|
| 333 | ret->handle_cmp = g_strcasecmp; |
---|
| 334 | |
---|
| 335 | register_protocol(ret); |
---|
[62d2cfb] | 336 | |
---|
| 337 | // Initialise the twitter_connections GSList. |
---|
| 338 | twitter_connections = NULL; |
---|
[1b221e0] | 339 | } |
---|
| 340 | |
---|