-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.c
160 lines (120 loc) · 4.19 KB
/
post.c
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
This file is part of Toot.
Toot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Toot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Toot. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdbool.h>
#include <curl/curl.h>
#include <string.h>
#include <stdlib.h>
#include <json-c/json.h>
#include "util.h"
#include "asprintf.h"
/* This function post "status" with the visibility "visibility" */
int
post_status(const char *status,
const char *scope,
const char *topic,
const bool sensitive,
char *media_ptr[])
{
struct config config;
if(load_config(&config) < 0) {
fprintf(stderr, "Error loading config");
return -1;
}
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;
dm_asprintf(&url, "%s%s", config.instance, api_url);
char *header_fmt = "Authorization: Bearer %s";
char *authorization_header = NULL;
struct curl_slist *header_list = NULL;
dm_asprintf(&authorization_header, header_fmt, config.access_token);
header_list = curl_slist_append(header_list, authorization_header);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);
/* Write the thing */
struct memory chunk = { 0 };
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
curl_mime *mime;
mime = curl_mime_init(curl);
curl_mimepart *status_part, *scope_part, *topic_part, *source_part,
*sensitive_part;
curl_mimepart *media_curlparts[__MAX_UPLOADS__];
/* Is there a better way to do this? */
status_part = curl_mime_addpart(mime);
topic_part = curl_mime_addpart(mime);
sensitive_part = curl_mime_addpart(mime);
source_part = curl_mime_addpart(mime);
scope_part = curl_mime_addpart(mime);
/* Status */
curl_mime_data(status_part, status, CURL_ZERO_TERMINATED);
curl_mime_name(status_part, "status");
/* Topic / content warning */
curl_mime_data(topic_part, topic, CURL_ZERO_TERMINATED);
curl_mime_name(topic_part, "spoiler_text");
/* Mark it sensitive if we have a topic */
if(sensitive)
curl_mime_data(sensitive_part, "true", CURL_ZERO_TERMINATED);
else
curl_mime_data(sensitive_part, "false", CURL_ZERO_TERMINATED);
curl_mime_name(sensitive_part, "sensitive");
/* Set souce of post - application */
curl_mime_data(source_part, __VERSION__, CURL_ZERO_TERMINATED);
curl_mime_name(source_part, "source");
/* Visibility */
curl_mime_data(scope_part, scope, CURL_ZERO_TERMINATED);
curl_mime_name(scope_part, "visibility");
/* Media */
if(media_ptr != NULL) {
for(int mediaItem = 0; mediaItem < __MAX_UPLOADS__; mediaItem++) {
if(media_ptr[mediaItem] != NULL) {
media_curlparts[mediaItem] = curl_mime_addpart(mime);
curl_mime_data(media_curlparts[mediaItem],
media_ptr[mediaItem],
CURL_ZERO_TERMINATED);
curl_mime_name(media_curlparts[mediaItem], "media_ids[]");
}
}
}
/* post */
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
curl_easy_perform(curl);
/* free */
curl_easy_cleanup(curl);
curl_slist_free_all(header_list);
curl_mime_free(mime);
/* The the url */
struct json_object *parsed_json;
struct json_object *json_url;
struct json_object *server_response;
parsed_json = json_tokener_parse(chunk.response);
json_object_object_get_ex(parsed_json, "url", &json_url);
json_object_object_get_ex(parsed_json, "error", &server_response);
const char *error = json_object_get_string(server_response);
if(json_url == NULL) {
fprintf(stderr, "Error posting, server said: \"%s\"\n", error);
return -1;
}
puts(json_object_get_string(json_url));
free(url);
free(authorization_header);
free_response(&chunk);
json_object_put(parsed_json);
return 0;
}