aboutsummaryrefslogtreecommitdiffstats
path: root/src/post.c
blob: 1896ed9dce02faa8488a3709533b1b003de20b1a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <curl/curl.h>
#include <string.h>
#include <stdlib.h>

#include "util.h"
#include "asprintf.h"

/* This function post "status" with the visibility "visibility" */


int
post_status(const char *status, const char *scope)
{
	char instance[50];
	char client_id[50];
	char client_secret[50];
	char access_token[50];
	
	get_tokens_from_file(".demiurgerc",instance,client_id,client_secret,access_token);
	CURL *curl = curl_easy_init();
	if(curl == NULL) {
		fprintf(stderr,"Error creating libcurl thing\n");
		return -1;
	}
	
	char *api_url = "/api/v1/statuses";
	char *url = NULL;
	
	asprintf(&url,"%s%s",instance,api_url);
	puts(url);
	
	curl_easy_setopt(curl,CURLOPT_URL,url);

	char *header_fmt = "Authorization: Bearer %s";
	char *authorization_header = NULL;
	struct curl_slist *header_list = NULL;
	
	asprintf(&authorization_header,header_fmt,access_token);
	header_list = curl_slist_append(header_list, authorization_header);

	curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);

	char *params_fmt = "status=%s&visibility=%s";

	char *status_to_post = NULL;
	char *visibility_to_post = NULL;
	
	char *post_params = NULL;

	asprintf(&post_params,params_fmt,status,scope);
	
	curl_easy_setopt(curl,CURLOPT_POSTFIELDS,post_params);
	
	curl_easy_perform(curl);

	free(url);
	free(post_params);
	free(status_to_post);
	free(visibility_to_post);
	free(authorization_header);
	
	return 0;

}