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