static void twitter_get_users_lookup(struct im_connection *ic) { struct twitter_data *td = ic->proto_data; char *args[2] = { "user_id", NULL, }; GString *ids = g_string_new(""); int i; /* We can request up to 100 users at a time. */ for (i = 0; i < 100 && td->follow_ids; i ++) { g_string_append_printf(ids, ",%s", (char*) td->follow_ids->data); g_free(td->follow_ids->data); td->follow_ids = g_slist_remove(td->follow_ids, td->follow_ids->data); } if (ids->len > 0) { args[1] = ids->str + 1; /* POST, because I think ids can be up to 1KB long. */ twitter_http(ic, TWITTER_USERS_LOOKUP_URL, twitter_http_get_users_lookup, ic, 1, args, 2); } else { /* We have all users. Continue with login. (Get statuses.) */ td->flags |= TWITTER_HAVE_FRIENDS; twitter_login_finish(ic); } g_string_free(ids, TRUE); }
static gboolean twitter_oauth_callback(struct oauth_info *info) { struct im_connection *ic = info->data; struct twitter_data *td; if (!g_slist_find(twitter_connections, ic)) return FALSE; td = ic->proto_data; if (info->stage == OAUTH_REQUEST_TOKEN) { char *name, *msg; if (info->request_token == NULL) { imcb_error(ic, "OAuth error: %s", twitter_parse_error(info->http)); imc_logout(ic, TRUE); return FALSE; } name = g_strdup_printf("%s_%s", td->prefix, ic->acc->user); msg = g_strdup_printf("To finish OAuth authentication, please visit " "%s and respond with the resulting PIN code.", info->auth_url); imcb_buddy_msg(ic, name, msg, 0, 0); g_free(name); g_free(msg); } else if (info->stage == OAUTH_ACCESS_TOKEN) { const char *sn; if (info->token == NULL || info->token_secret == NULL) { imcb_error(ic, "OAuth error: %s", twitter_parse_error(info->http)); imc_logout(ic, TRUE); return FALSE; } if ((sn = oauth_params_get(&info->params, "screen_name"))) { if (ic->acc->prpl->handle_cmp(sn, ic->acc->user) != 0) imcb_log(ic, "Warning: You logged in via OAuth as %s " "instead of %s.", sn, ic->acc->user); g_free(td->user); td->user = g_strdup(sn); } /* IM mods didn't do this so far and it's ugly but I should be able to get away with it... */ g_free(ic->acc->pass); ic->acc->pass = oauth_to_string(info); twitter_login_finish(ic); } return TRUE; }
/** * Login method. Since the twitter API works with separate HTTP request we * only save the user and pass to the twitter_data object. */ static void twitter_login(account_t * acc) { struct im_connection *ic = imcb_new(acc); struct twitter_data *td; char name[strlen(acc->user) + 9]; url_t url; char *s; if (!url_set(&url, set_getstr(&ic->acc->set, "base_url")) || (url.proto != PROTO_HTTP && url.proto != PROTO_HTTPS)) { imcb_error(ic, "Incorrect API base URL: %s", set_getstr(&ic->acc->set, "base_url")); imc_logout(ic, FALSE); return; } if (!strstr(url.host, "twitter.com") && set_getbool(&ic->acc->set, "stream")) { imcb_error(ic, "Warning: The streaming API is only supported by Twitter, " "and you seem to be connecting to a different service."); } imcb_log(ic, "Connecting"); twitter_connections = g_slist_append(twitter_connections, ic); td = g_new0(struct twitter_data, 1); ic->proto_data = td; td->user = g_strdup(acc->user); td->url_ssl = url.proto == PROTO_HTTPS; td->url_port = url.port; td->url_host = g_strdup(url.host); if (strcmp(url.file, "/") != 0) td->url_path = g_strdup(url.file); else { td->url_path = g_strdup(""); if (g_str_has_suffix(url.host, "twitter.com")) /* May fire for people who turned on HTTPS. */ imcb_error(ic, "Warning: Twitter requires a version number in API calls " "now. Try resetting the base_url account setting."); } /* Hacky string mangling: Turn identi.ca into identi.ca and api.twitter.com into twitter, and try to be sensible if we get anything else. */ td->prefix = g_strdup(url.host); if (g_str_has_suffix(td->prefix, ".com")) td->prefix[strlen(url.host) - 4] = '\0'; if ((s = strrchr(td->prefix, '.')) && strlen(s) > 4) { /* If we have at least 3 chars after the last dot, cut off the rest. (mostly a www/api prefix or sth) */ s = g_strdup(s + 1); g_free(td->prefix); td->prefix = s; } if (strstr(acc->pass, "oauth_token=")) td->oauth_info = oauth_from_string(acc->pass, get_oauth_service(ic)); sprintf(name, "%s_%s", td->prefix, acc->user); imcb_add_buddy(ic, name, NULL); imcb_buddy_status(ic, name, OPT_LOGGED_IN, NULL, NULL); td->log = g_new0(struct twitter_log_data, TWITTER_LOG_LENGTH); td->log_id = -1; s = set_getstr(&ic->acc->set, "mode"); if (g_strcasecmp(s, "one") == 0) td->flags |= TWITTER_MODE_ONE; else if (g_strcasecmp(s, "many") == 0) td->flags |= TWITTER_MODE_MANY; else td->flags |= TWITTER_MODE_CHAT; twitter_login_finish(ic); }