Exemplo n.º 1
0
static void gsocial_send_request(Session *request)
{
    request->exit_code = 0;
    char *escaped_tweet = NULL;
    int is_post = 0;
    char endpoint[500];
    char *req_url;
    char *reply;
    char *postarg = NULL;
    switch(request->action) {
        case ACTION_HOME_TIMELINE:
            if(request->since_id != NULL) {
                sprintf(endpoint, "%s%s?%s%s", twitter_statuses, "/home_timeline.xml", 
                        "since_id=", request->since_id);
                printf ( "%s\n", request->since_id );
            }
            else {
                sprintf(endpoint, "%s%s", twitter_statuses, "/home_timeline.xml");
            }
            break;
        case ACTION_UPDATE:
            escaped_tweet = oauth_url_escape(request->tweet);
            sprintf(endpoint, "%s%s?status=%s","http://api.twitter.com/1/statuses",
                    "/update.xml", escaped_tweet);
            is_post = 1;
            break;
        case ACTION_MESSAGES:
            if(request->since_id != NULL) {
                sprintf(endpoint, "%s?%s%s", twitter_dm_uri, "since_id=", request->since_id);
            }
            else {
                sprintf(endpoint, "%s", twitter_dm_uri);
            }
            break;

    }
    if(is_post){
        req_url = oauth_sign_url2(endpoint, &postarg, 
                OA_HMAC, NULL, consumer_key, consumer_secret, access_key, access_secret);

        reply = oauth_http_post(req_url, postarg);

    } else{
        req_url = oauth_sign_url2(endpoint, NULL, OA_HMAC, NULL,
                consumer_key, consumer_secret, access_key, access_secret);

        reply = oauth_http_get(req_url, postarg);
    }


    if (request->action != ACTION_UPDATE)
        gsocial_parse(reply, request, request->action);

    if(reply)
        request->exit_code = 1;

    free(postarg);
    free(req_url);
    free(reply);
}
Exemplo n.º 2
0
int oauthrequest (int mode, oauthparam *op) {
    if (mode&2==0) { // GET
        char *geturl = NULL;
        geturl = oauth_sign_url(op->url, NULL, OA_HMAC, op->c_key, op->c_secret, op->t_key, op->t_secret);
        if(!geturl) {
            return (1);
        }
        char *reply = oauth_http_get(geturl, NULL);
        if(reply) {
            //write(STDOUT, reply, strlen(reply))
            printf("%s\n", reply);
            free(reply);
        }
        free(geturl);
    } else { // POST
        char *postargs = NULL, *post = NULL;
        post = oauth_sign_url(op->url, &postargs, OA_HMAC, op->c_key, op->c_secret, op->t_key, op->t_secret);
        if (!post || !postargs) {
            return (1);
        }
        char *reply = oauth_http_post(post,postargs);
        if(reply) {
            //write(STDOUT, reply, strlen(reply))
            printf("%s\n", reply);
            free(reply);
        }
        if(post) free(post);
        if(postargs) free(postargs);
    }
    return (0);
}
Exemplo n.º 3
0
int oauthsign_alt (int mode, oauthparam *op) {
    if (mode==1) { // GET
        char *geturl = NULL;
        geturl = oauth_sign_url(op->url, NULL, OA_HMAC, op->c_key, op->c_secret, op->t_key, op->t_secret);
        if(geturl) {
            printf("%s\n", geturl);
            free(geturl);
        }
    } else { // POST
        char *postargs = NULL, *post = NULL;
        post = oauth_sign_url(op->url, &postargs, OA_HMAC, op->c_key, op->c_secret, op->t_key, op->t_secret);
        if (!post || !postargs) {
            return (1);
        }
        if (mode==2) { // print postargs only
            if (postargs) printf("%s\n", postargs);
        } else if (mode==3) { // print url and postargs
            if (post && postargs) printf("%s\n%s\n", post, postargs);
        } else if (post && postargs) {
            char *reply = oauth_http_post(post,postargs);
            if(reply) {
                //write(STDOUT, reply, strlen(reply))
                printf("%s\n", reply);
                free(reply);
            }
        }
        if(post) free(post);
        if(postargs) free(postargs);
    }
    return (0);
}
Exemplo n.º 4
0
// basically oauth_sign_url() from liboauth in steps..
char *oauthrequest_ext (int mode, oauthparam *op, int oauthargc, char **oauthargv, char *sign) {
    int argc=0;
    char **argv = NULL;
    char *request=NULL;

    append_parameters(&argc, &argv, oauthargc, oauthargv);

    if (sign) {
        add_kv_to_array(&argc, &argv, "oauth_signature", sign);
    }

    // build URL params
    request = oauth_serialize_url(argc, (mode&2?1:0), argv);

    char *reply = NULL;
    if(request) {
        if (mode&2) { // POST
            reply = oauth_http_post(argv[0],request);
        } else { // GET
            reply= oauth_http_get(request, NULL);
        }
        free(request);
    }
    free_array(argc,argv);
    return reply;
}
Exemplo n.º 5
0
int weibo_statuses_update(struct unim_login_info *login_info,
				struct unim_api_call_info *api_call_info)
{
	char *req_param, *reply;
	int rc = -1;
	int argc = 0;
	char **argv = NULL;
	int i;

	argc = oauth_split_url_parameters(api_call_info->uri, &argv);
	if (login_info->request_added_argc) {
		for (i = 0; i < login_info->request_added_argc; i++) {
			oauth_add_param_to_array(&argc, &argv,
					login_info->request_added_argv[i]);
		}
	}
	req_param = oauth_serialize_url_parameters(argc, argv);
	reply = oauth_http_post(api_call_info->uri, req_param);
	if (reply) {
		/* TODO: check replay! */
		api_call_info->result = reply;
		rc = 0;
	}

	if (req_param)
		free(req_param);

	return rc;
}
Exemplo n.º 6
0
// 取得したキーでつぶやく
int post_tweet(_TCHAR *consumer_key, _TCHAR *consumer_secret, _TCHAR *access_key, _TCHAR *access_secret, _TCHAR *tweet)
{
    std::string c_key; //< consumer key
    std::string c_secret; //< consumer secret

    std::string t_key; //< access key
    std::string t_secret; //< access secret

	std::string message; //< tweetする内容

    std::string req_url;
	std::string postarg;
    std::string reply;

	// アプリケーションのキーを代入
    c_key = consumer_key;
    c_secret = consumer_secret;

    t_key = access_key;
    t_secret = access_secret;

	message = tweet;

	printf("Tweet Message..\n");

	printf("consumer_key: %s\n", c_key.c_str());
	printf("consumer_secret: %s\n", c_secret.c_str());
	printf("access_key: %s\n", t_key.c_str());
	printf("access_secret: %s\n", t_secret.c_str());
	printf("message: %s\n", message.c_str());

	if (!message.empty()) {
		{
			int n;
			wchar_t ucs2[1000];
			char utf8[1000];
			n = MultiByteToWideChar(CP_ACP, 0, message.c_str(), message.size(), ucs2, 1000);
			n = WideCharToMultiByte(CP_UTF8, 0, ucs2, n, utf8, 1000, 0, 0);
			message.assign(utf8, n);
		}
		
		std::string uri = "http://api.twitter.com/statuses/update.json";
		uri += "?status=";
		uri += oauth_url_escape(message.c_str());

		req_url = oauth_sign_url2(uri.c_str(), &postarg, OA_HMAC, 0, c_key.c_str(), c_secret.c_str(), t_key.c_str(), t_secret.c_str());
		reply = oauth_http_post(req_url.c_str(), postarg.c_str());

		/* 取得したjsonをファイルに書き出す */
		FILE *fp;
		fp = fopen("post_message.json","w");
		fputs( reply.c_str() , fp );
		fclose( fp );
	}

    return TRUE;
}
Exemplo n.º 7
0
void
TwitterDialog::tweetCurrentRide()
{

    QString strToken = appsettings->cvalue(context->athlete->cyclist, GC_TWITTER_TOKEN).toString();
    QString strSecret = appsettings->cvalue(context->athlete->cyclist, GC_TWITTER_SECRET).toString();

    QString s_token = QString(strToken);
    QString s_secret = QString(strSecret);

    if(s_token.isEmpty() || s_secret.isEmpty()) {
      #ifdef Q_OS_MACX
      #define GC_PREF tr("Golden Cheetah->Preferences")
      #else
      #define GC_PREF tr("Tools->Options")
      #endif
      QString advise = QString(tr("Error fetching OAuth credentials.  Please make sure to complete the twitter authorization procedure found under %1.")).arg(GC_PREF);
      QMessageBox oautherr(QMessageBox::Critical, tr("OAuth Error"), advise);
      oautherr.exec();
      return;
    }

    char *postarg = NULL;

    // This is for API 1.0
    // QString qurl = "http://api.twitter.com/1/statuses/update.json?status=";
    // This is for API 1.1
    QString qurl = "https://api.twitter.com/1.1/statuses/update.json?status=";

    QString twitterMsg = getTwitterMessage();

    if(twitterMsg.length() > 140) {
      QMessageBox tweetlengtherr(QMessageBox::Critical, tr("Tweet Length Error"), tr("Tweet must be 140 characters or fewer."));
      tweetlengtherr.exec();
      return;
    }

    const QString strUrl = QUrl::toPercentEncoding(twitterMsg);
    qurl.append(strUrl);
    const char *req_url = oauth_sign_url2(qurl.toLatin1(), &postarg, OA_HMAC, NULL, GC_TWITTER_CONSUMER_KEY, GC_TWITTER_CONSUMER_SECRET, s_token.toLatin1(), s_secret.toLatin1());
    const char *strreply = oauth_http_post(req_url,postarg);

    QString post_reply = QString(strreply);

    if(!post_reply.contains("created_at", Qt::CaseInsensitive)) {
      QMessageBox oautherr(QMessageBox::Critical, tr("Error Posting Tweet"), tr("There was an error connecting to Twitter.  Check your network connection and try again."));
      oautherr.setDetailedText(post_reply);
      oautherr.exec();
      return;
    }

    if(postarg) free(postarg);

    accept();

}
Exemplo n.º 8
0
// アプリケーション認証にアクセスし、PINコードを取得
int twitter_getpincode( char *tkey_buf , int size )
{
	static const char *request_token_uri = "http://api.twitter.com/oauth/request_token";
	static const char *authorize_uri = "http://api.twitter.com/oauth/authorize";
	static const char *access_token_uri = "http://api.twitter.com/oauth/access_token";

	std::string c_key; //< consumer key
	std::string c_secret; //< consumer secret

	std::string t_key;
	std::string t_secret;

	std::string req_url;
	std::string postarg;
	std::string reply;

	// アプリケーションのキーを代入
	c_key = consumer_key;
	c_secret = consumer_secret;

	// メッセージを表示
	label1->Text="Request token..\n";

    // 認証ページにアクセス
    {
        std::string reply;
        std::string req_url = oauth_sign_url2(request_token_uri, &postarg, OA_HMAC, 0, c_key.c_str(), c_secret.c_str(), 0, 0);
        reply = oauth_http_post(req_url.c_str(),postarg.c_str());

		// 反応がなかった場合
		if (!parse_reply(reply.c_str(), &t_key, &t_secret))
        {
			MessageBox (NULL,"failed to get request token.", "ERROR!",MB_OK);
            return FALSE;
        }
    }

	//

	// メッセージを表示
	label1->Text=label1->Text+"Authorize..\n";

	// PINを表示
	{
		std::string req_url = oauth_sign_url2(authorize_uri, 0, OA_HMAC, 0, c_key.c_str(), c_secret.c_str(), t_key.c_str(), t_secret.c_str());

		label1->Text=label1->Text+"Opening...\n";
		puts(req_url.c_str());
		ShellExecute(0, 0, req_url.c_str(), 0, 0, SW_SHOW); // ウェブブラウザを起動する
	}
	return TRUE;
}
Exemplo n.º 9
0
int weibo_oauth_access(struct unim_login_info *login_info)
{
	char *req_param, *reply;
	int rc = -1;
	int argc = 0;
	char **argv = NULL;
	char *postargs = NULL;
	int i;

	argc = oauth_split_url_parameters(login_info->access_token_uri, &argv);
	if (login_info->request_added_argc) {
		for (i = 0; i < login_info->request_added_argc; i++) {
			oauth_add_param_to_array(&argc, &argv,
					login_info->request_added_argv[i]);
		}
	}
	req_param = oauth_serialize_url_parameters(argc, argv);
	printf("req_param: %s\n", req_param);
	reply = oauth_http_post(login_info->access_token_uri, req_param);
	if (reply) {
		JsonParser *jparser;
		GError *err;
		gboolean rb;

		printf("HTTP-reply:\n\t%s\n", reply);
		jparser = json_parser_new();
		rb = json_parser_load_from_data(jparser, reply, strlen(reply), &err);
		if (!rb) {
			printf("json parse failed! err: %s\n", err->message);
			g_object_unref(jparser);
			goto error_out;
		}
		JsonReader *jreader = json_reader_new(json_parser_get_root(jparser));
		rb = json_reader_read_member(jreader, "access_token");
		if (rb) {
			login_info->access_token_key =
				strdup(json_reader_get_string_value(jreader));
			rc = 0;
		}
		g_object_unref(jreader);
	}

error_out:
	if (req_param)
		free(req_param);
	if (reply)
		free(reply);
	if (postargs)
		free(postargs);

	return rc;
}
Exemplo n.º 10
0
static char* weibo_do_oauth_request(char* szUrl, char* szToken, char* szTokenSecret, gboolean bPost)
{
    POS();
    char *postargs = NULL;
    char* post = NULL;
    post = oauth_sign_url2(
            szUrl
            , &postargs
            , OA_HMAC
            , NULL
            , APP_KEY
            , APP_SECRET
            , szToken
            , szTokenSecret);

    char *reply = oauth_http_post(post,postargs);
    return reply;
    if (bPost) {
        return oauth_http_post(post,postargs);  
    } else {
        return oauth_http_get(post,postargs);       
    }
}
Exemplo n.º 11
0
/*
 * a example requesting and parsing a request-token from an OAuth
 * service-provider, using the oauth-HTTP POST function.
 */
void request_token_example_post()
{
	const char *request_token_uri =
				"http://term.ie/oauth/example/request_token.php";
	const char *req_c_key = "key";
	const char *req_c_secret = "secret";
	char *res_t_key = NULL;
	char *res_t_secret = NULL;
	char *postarg = NULL;
	char *req_url;
	char *reply;

	printf("***** request token example, use method post *****\n");
	req_url = oauth_sign_url2(request_token_uri, &postarg, OA_HMAC, NULL,
						req_c_key, req_c_secret, NULL, NULL);
	printf("request url:\n\t%s -> \n\t%s\n", request_token_uri, req_url);
	reply = oauth_http_post(req_url, postarg);
	printf("post arg:\n\t%s\n", postarg);
	if (!reply)  {
		printf("HTTP request for an oauth request-token failed!\n");
	} else {
		int rc;
		char **rv = NULL;
		printf("HTTP-reply:\n\t%s\n", reply);
		rc = oauth_split_url_parameters(reply, &rv);
		qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
		if (rc == 2 &&
			!strncmp(rv[0], "oauth_token=", 11) &&
			!strncmp(rv[1], "oauth_token_secret=", 18)) {
			res_t_key = strdup(&(rv[0][12]));
			res_t_secret = strdup(&(rv[1][19]));
			printf("key:    '%s'\nsecret: '%s'\n", res_t_key, res_t_secret);
		}
		if (rv)
			free(rv);
	}

	if (req_url)
		free(req_url);
	if (postarg)
		free(postarg);
	if (reply)
		free(reply);
	if (res_t_key)
		free(res_t_key);
	if (res_t_secret)
		free(res_t_secret);
}
Exemplo n.º 12
0
CURLcode submit_weibo_with_url_image1(char* szContent, char* szImageUrl)
{
    POS();
    char *postargs = NULL;
    char* post = NULL;
    post = oauth_sign_url2("http://api.t.sina.com.cn/statuses/update.json?status=teeeest1314353455", &postargs, OA_HMAC, NULL, "3551174478", "c53f4123bdb7b39a0ae03e400b839934", "18d2d221a6bc0aa8743d3f30d6745b7e", "2de591d207b213e4f46a1d1170413367");

    DEBUG("%s\n", post);
    DEBUG("%s\n", postargs);
    char *reply = oauth_http_post(post,postargs);
    if(reply){
        DEBUG("%s\n", reply);
    }

    return 0;
}
Exemplo n.º 13
0
/*
 * a example requesting and parsing a request-token from an OAuth service-provider
 * using the oauth-HTTP POST function.
 */
void request_token_example_post(void) {
#if 0
  const char *request_token_uri = "http://oauth-sandbox.mediamatic.nl/module/OAuth/request_token";
  const char *req_c_key         = "17b09ea4c9a4121145936f0d7d8daa28047583796"; //< consumer key
  const char *req_c_secret      = "942295b08ffce77b399419ee96ac65be"; //< consumer secret
#else
  const char *request_token_uri = "http://term.ie/oauth/example/request_token.php";
  const char *req_c_key         = "key"; //< consumer key
  const char *req_c_secret      = "secret"; //< consumer secret
#endif
  char *res_t_key    = NULL; //< replied key
  char *res_t_secret = NULL; //< replied secret

  char *postarg = NULL;
  char *req_url;
  char *reply;

  req_url = oauth_sign_url2(request_token_uri, &postarg, OA_HMAC, NULL, req_c_key, req_c_secret, NULL, NULL);

  printf("request URL:%s\n\n", req_url);
  reply = oauth_http_post(req_url,postarg);
  if (!reply) 
    printf("HTTP request for an oauth request-token failed.\n");
  else {
    //parse reply - example:
    //"oauth_token=2a71d1c73d2771b00f13ca0acb9836a10477d3c56&oauth_token_secret=a1b5c00c1f3e23fb314a0aa22e990266"
    int rc;
    char **rv = NULL;
    printf("HTTP-reply: %s\n", reply);
    rc = oauth_split_url_parameters(reply, &rv);
    qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
    if( rc==2 
	&& !strncmp(rv[0],"oauth_token=",11)
	&& !strncmp(rv[1],"oauth_token_secret=",18) ){
	  res_t_key=strdup(&(rv[0][12]));
	  res_t_secret=strdup(&(rv[1][19]));
	  printf("key:    '%s'\nsecret: '%s'\n",res_t_key, res_t_secret);
    }
    if(rv) free(rv);
  }

  if(req_url) free(req_url);
  if(postarg) free(postarg);
  if(reply) free(reply);
  if(res_t_key) free(res_t_key);
  if(res_t_secret) free(res_t_secret);
}
Exemplo n.º 14
0
/**
 * @brief Use API key/secret to retrive the resonse key/secret
 *
 * @param req_key API key
 * @param req_secret API secret key
 * @param res_key the address of the response key
 * @param res_secret the address of the response secret key
 *
 * @return error on 1
 */
inline static int get_response_token(char* req_key, 
                                     char* req_secret,
                                     char** res_key, 
                                     char** res_secret){
    int fail = 0;
    char *request_token_uri = s_concate(&(plurk_url), &(plurk_uri_request));

    char *postarg = NULL;
    char *req_url;
    char *reply;

    req_url = oauth_sign_url2(
              request_token_uri,  // url
              &postarg,           // postarg
              OA_HMAC,            // OAuthMethod
              "POST",             // HTTPMethod
              req_key,            // customer key
              req_secret,         // customer secret
              NULL,               // token key
              NULL);              // token secret

    reply = oauth_http_post(req_url,postarg);
    if (!reply){
        printf("HTTP request for an oauth request-token failed.\n");
        return (fail = 1);
    }
    else 
        fail = get_keysecret(reply, res_key, res_secret);
    
#if SAMUEL_DEBUG
    printf("SAMUEL_DEBUG, res_key: '%s'\t res_secret: '%s'\n", *res_key, *res_secret);
#endif

    if(reply) free(reply);
    if(req_url) free(req_url);
    if(postarg) free(postarg);
    if(request_token_uri) free(request_token_uri);

    return fail;
}
Exemplo n.º 15
0
static char *knh_request(Args_t *args, const char *method, AccessToken_t *token)
{
    char *reply = NULL;
    if (knh_strncmp(method, "POST", 5) == 0) {
        char *postargs = NULL;
        char *req_url = oauth_sign_array2(
                            &args->argc, // argcp
                            &args->argv, // argvp
                            &postargs, // postargs
                            OA_HMAC, // method
                            NULL, // http_method
                            token->key, // c_key
                            token->secret, // c_secret
                            token->rtoken, // t_key
                            token->rtoken_secret // t_secret
                        );
        DBG_P("request URL(POST): %s?%s", req_url, postargs);
        reply = oauth_http_post(req_url, postargs);
        if (postargs) free(postargs);
        if (req_url) free(req_url);
    } else {
        char *req_url = oauth_sign_array2(
                            &args->argc, // argcp
                            &args->argv, // argvp
                            NULL, // postargs
                            OA_HMAC, // method
                            NULL, // http_method
                            token->key, // c_key
                            token->secret, // c_secret
                            token->rtoken, // t_key
                            token->rtoken_secret // t_secret
                        );
        DBG_P("request URL(GET): %s", req_url);
        reply = oauth_http_get(req_url, NULL);
        if (req_url) free(req_url);
    }
    return reply;
}
Exemplo n.º 16
0
Arquivo: bti.c Projeto: sunng87/bti
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;
}
Exemplo n.º 17
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 || 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;
}