static int send_request(struct session *session) { char endpoint[500]; char user_password[500]; char data[500]; struct bti_curl_buffer *curl_buf; CURL *curl = NULL; CURLcode res; struct curl_httppost *formpost = NULL; struct curl_httppost *lastptr = NULL; struct curl_slist *slist = NULL; char *req_url = NULL; char *reply = NULL; char *postarg = NULL; char *escaped_tweet = NULL; int is_post = 0; if (!session) return -EINVAL; if (!session->hosturl) session->hosturl = strdup(twitter_host); if (session->no_oauth || session->guest) { curl_buf = bti_curl_buffer_alloc(session->action); if (!curl_buf) return -ENOMEM; curl_buf->session = session; curl = curl_init(); if (!curl) return -EINVAL; if (!session->hosturl) session->hosturl = strdup(twitter_host); switch (session->action) { case ACTION_UPDATE: snprintf(user_password, sizeof(user_password), "%s:%s", session->account, session->password); snprintf(data, sizeof(data), "status=\"%s\"", session->tweet); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "status", CURLFORM_COPYCONTENTS, session->tweet, CURLFORM_END); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "source", CURLFORM_COPYCONTENTS, "bti", CURLFORM_END); if (session->replyto) curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "in_reply_to_status_id", CURLFORM_COPYCONTENTS, session->replyto, CURLFORM_END); curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); slist = curl_slist_append(slist, "Expect:"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist); sprintf(endpoint, "%s%s", session->hosturl, update_uri); curl_easy_setopt(curl, CURLOPT_URL, endpoint); curl_easy_setopt(curl, CURLOPT_USERPWD, user_password); break; case ACTION_FRIENDS: snprintf(user_password, sizeof(user_password), "%s:%s", session->account, session->password); sprintf(endpoint, "%s%s?page=%d", session->hosturl, friends_uri, session->page); curl_easy_setopt(curl, CURLOPT_URL, endpoint); curl_easy_setopt(curl, CURLOPT_USERPWD, user_password); break; case ACTION_USER: sprintf(endpoint, "%s%s%s.xml?page=%d", session->hosturl, user_uri, session->user, session->page); curl_easy_setopt(curl, CURLOPT_URL, endpoint); break; case ACTION_REPLIES: snprintf(user_password, sizeof(user_password), "%s:%s", session->account, session->password); sprintf(endpoint, "%s%s?page=%d", session->hosturl, replies_uri, session->page); curl_easy_setopt(curl, CURLOPT_URL, endpoint); curl_easy_setopt(curl, CURLOPT_USERPWD, user_password); break; case ACTION_PUBLIC: sprintf(endpoint, "%s%s?page=%d", session->hosturl, public_uri, session->page); curl_easy_setopt(curl, CURLOPT_URL, endpoint); break; case ACTION_GROUP: sprintf(endpoint, "%s%s%s.xml?page=%d", session->hosturl, group_uri, session->group, session->page); curl_easy_setopt(curl, CURLOPT_URL, endpoint); break; default: break; } if (session->proxy) curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy); if (debug) curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); dbg("user_password = %s\n", user_password); dbg("data = %s\n", data); dbg("proxy = %s\n", session->proxy); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf); if (!session->dry_run) { res = curl_easy_perform(curl); if (!session->background) { xmlDocPtr doc; xmlNodePtr current; if (res) { fprintf(stderr, "error(%d) trying to " "perform operation\n", res); return -EINVAL; } doc = xmlReadMemory(curl_buf->data, curl_buf->length, "response.xml", NULL, XML_PARSE_NOERROR); if (doc == NULL) return -EINVAL; current = xmlDocGetRootElement(doc); if (current == NULL) { fprintf(stderr, "empty document\n"); xmlFreeDoc(doc); return -EINVAL; } if (xmlStrcmp(current->name, (const xmlChar *)"status")) { fprintf(stderr, "unexpected document type\n"); xmlFreeDoc(doc); return -EINVAL; } xmlFreeDoc(doc); } } curl_easy_cleanup(curl); if (session->action == ACTION_UPDATE) curl_formfree(formpost); bti_curl_buffer_free(curl_buf); } else { switch (session->action) { case ACTION_UPDATE: escaped_tweet = oauth_url_escape(session->tweet); if (session->replyto) { sprintf(endpoint, "%s%s?status=%s&in_reply_to_status_id=%s", session->hosturl, update_uri, escaped_tweet, session->replyto); } else { sprintf(endpoint, "%s%s?status=%s", session->hosturl, update_uri, escaped_tweet); } is_post = 1; break; case ACTION_USER: sprintf(endpoint, "%s%s%s.xml?page=%d", session->hosturl, user_uri, session->user, session->page); break; case ACTION_REPLIES: sprintf(endpoint, "%s%s?page=%d", session->hosturl, mentions_uri, session->page); break; case ACTION_PUBLIC: sprintf(endpoint, "%s%s?page=%d", session->hosturl, public_uri, session->page); break; case ACTION_GROUP: sprintf(endpoint, "%s%s%s.xml?page=%d", session->hosturl, group_uri, session->group, session->page); break; case ACTION_FRIENDS: sprintf(endpoint, "%s%s?page=%d", session->hosturl, friends_uri, session->page); break; case ACTION_RETWEET: sprintf(endpoint, "%s%s%s.xml", session->hosturl, retweet_uri, session->retweet); is_post = 1; break; default: break; } dbg("%s\n", endpoint); if (!session->dry_run) { if (is_post) { req_url = oauth_sign_url2(endpoint, &postarg, OA_HMAC, NULL, session->consumer_key, session->consumer_secret, session->access_token_key, session->access_token_secret); reply = oauth_http_post(req_url, postarg); } else { req_url = oauth_sign_url2(endpoint, NULL, OA_HMAC, NULL, session->consumer_key, session->consumer_secret, session->access_token_key, session->access_token_secret); reply = oauth_http_get(req_url, postarg); } dbg("%s\n", req_url); dbg("%s\n", reply); if (req_url) free(req_url); } if (!reply) { fprintf(stderr, "Error retrieving from URL (%s)\n", endpoint); return -EIO; } if ((session->action != ACTION_UPDATE) && (session->action != ACTION_RETWEET)) parse_timeline(reply, session); } return 0; }
static int send_request(struct session *session) { char endpoint[500]; char user_password[500]; char data[500]; struct bti_curl_buffer *curl_buf; CURL *curl = NULL; CURLcode res; struct curl_httppost *formpost = NULL; struct curl_httppost *lastptr = NULL; struct curl_slist *slist = NULL; char *req_url = NULL; char *reply = NULL; char *postarg = NULL; char *escaped_tweet = NULL; int is_post = 0; if (!session) return -EINVAL; if (!session->hosturl) session->hosturl = strdup(twitter_host); if (session->no_oauth) { curl_buf = bti_curl_buffer_alloc(session->action); if (!curl_buf) return -ENOMEM; curl = curl_init(); if (!curl) return -EINVAL; if (!session->hosturl) session->hosturl = strdup(twitter_host); switch (session->action) { case ACTION_UPDATE: snprintf(user_password, sizeof(user_password), "%s:%s", session->account, session->password); snprintf(data, sizeof(data), "status=\"%s\"", session->tweet); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "status", CURLFORM_COPYCONTENTS, session->tweet, CURLFORM_END); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "source", CURLFORM_COPYCONTENTS, "bti", CURLFORM_END); if (session->replyto) curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "in_reply_to_status_id", CURLFORM_COPYCONTENTS, session->replyto, CURLFORM_END); curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); slist = curl_slist_append(slist, "Expect:"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist); sprintf(endpoint, "%s%s", session->hosturl, update_uri); curl_easy_setopt(curl, CURLOPT_URL, endpoint); curl_easy_setopt(curl, CURLOPT_USERPWD, user_password); break; case ACTION_FRIENDS: snprintf(user_password, sizeof(user_password), "%s:%s", session->account, session->password); sprintf(endpoint, "%s%s?page=%d", session->hosturl, friends_uri, session->page); curl_easy_setopt(curl, CURLOPT_URL, endpoint); curl_easy_setopt(curl, CURLOPT_USERPWD, user_password); break; case ACTION_USER: snprintf(user_password, sizeof(user_password), "%s:%s", session->account, session->password); sprintf(endpoint, "%s%s?screen_name=%s&page=%d", session->hosturl, user_uri, session->user, session->page); curl_easy_setopt(curl, CURLOPT_URL, endpoint); curl_easy_setopt(curl, CURLOPT_USERPWD, user_password); break; case ACTION_REPLIES: snprintf(user_password, sizeof(user_password), "%s:%s", session->account, session->password); sprintf(endpoint, "%s%s?page=%d", session->hosturl, replies_uri, session->page); curl_easy_setopt(curl, CURLOPT_URL, endpoint); curl_easy_setopt(curl, CURLOPT_USERPWD, user_password); break; case ACTION_PUBLIC: snprintf(user_password, sizeof(user_password), "%s:%s", session->account, session->password); sprintf(endpoint, "%s%s?page=%d", session->hosturl, public_uri, session->page); curl_easy_setopt(curl, CURLOPT_URL, endpoint); curl_easy_setopt(curl, CURLOPT_USERPWD, user_password); break; case ACTION_GROUP: snprintf(user_password, sizeof(user_password), "%s:%s", session->account, session->password); sprintf(endpoint, "%s%s%s.xml?page=%d", session->hosturl, group_uri, session->group, session->page); curl_easy_setopt(curl, CURLOPT_URL, endpoint); curl_easy_setopt(curl, CURLOPT_USERPWD, user_password); break; default: break; } if (session->proxy) curl_easy_setopt(curl, CURLOPT_PROXY, session->proxy); if (debug) curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); dbg("user_password = %s\n", user_password); dbg("data = %s\n", data); dbg("proxy = %s\n", session->proxy); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, curl_buf); if (!session->dry_run) { res = curl_easy_perform(curl); if (res && !session->background) { fprintf(stderr, "error(%d) trying to perform " "operation\n", res); return -EINVAL; } } curl_easy_cleanup(curl); if (session->action == ACTION_UPDATE) curl_formfree(formpost); bti_curl_buffer_free(curl_buf); } else { switch (session->action) { case ACTION_UPDATE: escaped_tweet = oauth_url_escape(session->tweet); sprintf(endpoint, "%s%s?status=%s", session->hosturl, update_uri, escaped_tweet); is_post = 1; break; case ACTION_USER: sprintf(endpoint, "%s%s%s.xml?page=%d", session->hosturl, user_uri, session->user, session->page); break; case ACTION_REPLIES: sprintf(endpoint, "%s%s?page=%d", session->hosturl, mentions_uri, session->page); break; case ACTION_PUBLIC: sprintf(endpoint, "%s%s?page=%d", session->hosturl, public_uri, session->page); break; case ACTION_GROUP: sprintf(endpoint, "%s%s%s.xml?page=%d", session->hosturl, group_uri, session->group, session->page); break; case ACTION_FRIENDS: sprintf(endpoint, "%s%s?page=%d", session->hosturl, friends_uri, session->page); break; default: break; } if (is_post) { req_url = oauth_sign_url2(endpoint, &postarg, OA_HMAC, NULL, session->consumer_key, session->consumer_secret, session->access_token_key, session->access_token_secret); reply = oauth_http_post(req_url, postarg); } else { req_url = oauth_sign_url2(endpoint, NULL, OA_HMAC, NULL, session->consumer_key, session->consumer_secret, session->access_token_key, session->access_token_secret); reply = oauth_http_get(req_url, postarg); } dbg("%s\n", req_url); dbg("%s\n", reply); if (req_url) free(req_url); if (session->action != ACTION_UPDATE) parse_timeline(reply); } return 0; }