示例#1
0
文件: t4c.c 项目: alphaKAI/t4c
void stream(T4C* t4c, sds url, Parameters* paramsArgument, size_t (*callback)(void*, size_t, size_t, void*)) {
  Parameters* oauthParams = new_parameters();
  genOAuthParams(t4c, oauthParams);
  Parameters* params = new_parameters();
  buildParams(params, oauthParams, paramsArgument);

  sds oauthSignature   = signature(t4c->consumerSecret, t4c->accessTokenSecret, GET, url, params);
  sds encodedSignature = url_encode(oauthSignature);

  add_parameter(oauthParams, sdsnew("oauth_signature"), encodedSignature);
  add_parameter(params,      sdsnew("oauth_signature"), encodedSignature);

  sds authorizeChild = join_parameters(oauthParams, ",");
  sds authorize      = sdscatprintf(sdsempty(), "Authorization: OAuth %s", authorizeChild);

  sds path = join_parameters(params, "&");

  if (DEBUG) {
    printf("----------------------------\n");
    printf("STREAMING API");
    printf("URL: %s\n", url);
    printf("path: %s\n", path);
    printf("authorize: %s\n", authorize);
    printf("----------------------------\n");
  }

  CURL* curl;
  curl = curl_easy_init();

  sds reqURL = sdscatprintf(sdsempty(), "%s?%s", url, path);

  curl_easy_setopt(curl, CURLOPT_URL, reqURL);

  struct curl_slist *headers = NULL;
  headers = curl_slist_append(headers, authorize);

  curl_easy_setopt(curl, CURLOPT_HEADER, headers);
  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
  curl_easy_setopt(curl, CURLOPT_TIMEOUT, 0);

  curl_easy_perform(curl);
  curl_easy_cleanup(curl);

  sdsfree(oauthSignature);
  sdsfree(encodedSignature);
  sdsfree(reqURL);
  sdsfree(url);
  sdsfree(path);
  sdsfree(authorize);
  sdsfree(authorizeChild);

  free_parameters(oauthParams);
}
示例#2
0
bool CommandParser::LoopCall(User* user, Command* handler, const std::vector<std::string>& parameters, unsigned int splithere, int extra, bool usemax)
{
	if (splithere >= parameters.size())
		return false;

	/* First check if we have more than one item in the list, if we don't we return false here and the handler
	 * which called us just carries on as it was.
	 */
	if (parameters[splithere].find(',') == std::string::npos)
		return false;

	/** Some lame ircds will weed out dupes using some shitty O(n^2) algorithm.
	 * By using std::set (thanks for the idea w00t) we can cut this down a ton.
	 * ...VOOODOOOO!
	 *
	 * Only check for duplicates if there is one list (allow them in JOIN).
	 */
	std::set<irc::string> dupes;
	bool check_dupes = (extra < 0);

	/* Create two sepstreams, if we have only one list, then initialize the second sepstream with
	 * an empty string. The second parameter of the constructor of the sepstream tells whether
	 * or not to allow empty tokens.
	 * We allow empty keys, so "JOIN #a,#b ,bkey" will be interpreted as "JOIN #a", "JOIN #b bkey"
	 */
	irc::commasepstream items1(parameters[splithere]);
	irc::commasepstream items2(extra >= 0 ? parameters[extra] : "", true);
	std::string item;
	unsigned int max = 0;
	LocalUser* localuser = IS_LOCAL(user);

	/* Attempt to iterate these lists and call the command handler
	 * for every parameter or parameter pair until there are no more
	 * left to parse.
	 */
	while (items1.GetToken(item) && (!usemax || max++ < ServerInstance->Config->MaxTargets))
	{
		if ((!check_dupes) || (dupes.insert(item.c_str()).second))
		{
			std::vector<std::string> new_parameters(parameters);
			new_parameters[splithere] = item;

			if (extra >= 0)
			{
				// If we have two lists then get the next item from the second list.
				// In case it runs out of elements then 'item' will be an empty string.
				items2.GetToken(item);
				new_parameters[extra] = item;
			}

			CmdResult result = handler->Handle(new_parameters, user);
			if (localuser)
			{
				// Run the OnPostCommand hook with the last parameter (original line) being empty
				// to indicate that the command had more targets in its original form.
				item.clear();
				FOREACH_MOD(OnPostCommand, (handler, new_parameters, localuser, result, item));
			}
		}
	}

	return true;
}
示例#3
0
文件: t4c.c 项目: alphaKAI/t4c
sds request(T4C* t4c, METHOD method, sds endPoint, Parameters* paramsArgument) {
  sds result;

  Parameters* oauthParams = new_parameters();
  genOAuthParams(t4c, oauthParams);
  Parameters* params = new_parameters();
  buildParams(params, oauthParams, paramsArgument);

  sds url = sdscatprintf(sdsempty(), "%s%s", baseUrl, endPoint);
  sds oauthSignature   = signature(t4c->consumerSecret, t4c->accessTokenSecret, method, url, params);
  sds encodedSignature = url_encode(oauthSignature);

  add_parameter(oauthParams, sdsnew("oauth_signature"), encodedSignature);
  add_parameter(params,     sdsnew("oauth_signature"),  encodedSignature);

  sds authorizeChild = join_parameters(oauthParams, ",");
  sds authorize      = sdscatprintf(sdsempty(), "Authorization: OAuth %s", authorizeChild);

  sds path = join_parameters(params, "&");

  if (DEBUG) {
    printf("----------------------------\n");
    printf("URL: %s\n", url);
    printf("endPoint: %s\n", endPoint);
    printf("path: %s\n", path);
    printf("authorize: %s\n", authorize);
    printf("----------------------------\n");
  }

  CURL* curl;
  curl = curl_easy_init();

  sds reqURL;

  if (method == GET) {
    reqURL = sdscatprintf(sdsempty(), "%s?%s", url, path);
    curl_easy_setopt(curl, CURLOPT_URL, reqURL);
  } else if (method == POST) {
    curl_easy_setopt(curl, CURLOPT_POST, 1);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, path);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, sdslen(path));
    curl_easy_setopt(curl, CURLOPT_URL, url);
  }

  struct curl_slist *headers = NULL;
  headers = curl_slist_append(headers, authorize);

  curl_easy_setopt(curl, CURLOPT_HEADER, headers);
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_result_stringlize_callback);
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&result);

  curl_easy_perform(curl);
  curl_easy_cleanup(curl);

  sdsfree(oauthSignature);
  sdsfree(encodedSignature);
  sdsfree(reqURL);
  sdsfree(url);
  sdsfree(path);
  sdsfree(authorize);
  sdsfree(authorizeChild);
  free_parameters(oauthParams);

  return result;
}
示例#4
0
文件: test.c 项目: alphaKAI/t4c
    fprintf(stderr, "[USER STREAM] received -> %s\n", str->value);
  }

  return realsize;
}

int main() {
  T4C t4c = {
    .consumerKey       = make_string("Your Consumer Key"),
    .consumerSecret    = make_string("Your Consumer Secret"),
    .accessToken       = make_string("Your AccessToken"),
    .accessTokenSecret = make_string("Your AccessTokenSecret")
  };

  // POST REQUEST
  Parameters* params = new_parameters();

  add_parameter(params, make_string("status"), make_string("Hello World!"));

  string result  = request(&t4c, POST, make_string("/statuses/update.json"), params);
  printf("RESULT for Tweet: %s\n", string_get_value(result));

  free_parameters(params);

  // GET REQUEST
  string result2 = request(&t4c, GET, make_string("/account/verify_credentials.json"), NULL);
  printf("RESULT for /account/verify_credentials.json: %s\n", string_get_value(result2));

  //STREAM API TEST
  stream(&t4c, make_string("https://userstream.twitter.com/1.1/user.json"), NULL, streaming_callback_sample);