-
Notifications
You must be signed in to change notification settings - Fork 0
/
accounts.c
138 lines (100 loc) · 3.49 KB
/
accounts.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
/*
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 <curl/curl.h>
#include <json-c/json.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "asprintf.h"
#include "fetch.h"
#include "util.h"
// get the account id from @User@Domain
char *
request_account_id(char *api_parameter, const struct config *config)
{
char *returned_data = NULL;
struct json_object *parsed_json;
struct json_object *accountObj;
struct json_object *account_id;
char *api_url_fmt = "%s/api/v1/accounts/search?q=%s&limit=1";
char *api_url = NULL;
if(api_parameter[0] == '@')
api_parameter++;
dm_asprintf(&api_url, api_url_fmt, config->instance, api_parameter);
returned_data = do_api_request(api_url, config, false);
if(NULL == returned_data) {
return NULL;
}
parsed_json = json_tokener_parse(returned_data);
if(NULL == parsed_json) {
puts("An error occured fetching, invalid data returned");
return NULL;
}
accountObj = json_object_array_get_idx(parsed_json, 0);
if(json_object_object_get_ex(accountObj, "id", &account_id)) {
/* keep freeing */
const char *str = json_object_get_string(account_id);
char *return_str = malloc(strlen(str + 1));
strcpy(return_str, str); // strdup() segfaults for some reason
json_object_put(parsed_json);
// puts(return_str);
return return_str;
}
json_object_put(parsed_json);
return NULL;
}
// follow /unfollow
// We need to do a search, limited to 1 user
// Then use the id from that for the follow/unfollow.
char *
follow_account(char *id, char action, const struct config *config)
{
char *returned_data = NULL;
char *returned_request_data = NULL;
struct json_object *parsed_json;
struct json_object *follow_state;
char *api_url_fmt = "%s/api/v1/accounts/%s/follow";
char *api_url = NULL;
returned_data = request_account_id(id, config);
if(NULL == returned_data) {
return NULL;
}
if(action == 'f')
api_url_fmt = "%s/api/v1/accounts/%s/follow";
else if(action == 'u')
api_url_fmt = "%s/api/v1/accounts/%s/unfollow";
dm_asprintf(&api_url, api_url_fmt, config->instance, returned_data);
returned_request_data = do_api_request(api_url, config, true);
parsed_json = json_tokener_parse(returned_request_data);
if(NULL == parsed_json) {
puts("An error occured fetching, invalid data returned");
return NULL;
}
if(json_object_object_get_ex(parsed_json, "error", &follow_state)) {
const char *str = json_object_get_string(follow_state);
char *return_str = malloc(strlen(str + 1));
strcpy(return_str, str); // strdup() segfaults for some reason
json_object_put(parsed_json);
return return_str;
}
if(json_object_object_get_ex(parsed_json, "following", &follow_state)) {
const char *str = json_object_get_string(follow_state);
char *return_str = malloc(strlen(str + 1));
strcpy(return_str, str); // strdup() segfaults for some reason
json_object_put(parsed_json);
return return_str;
}
json_object_put(parsed_json);
return NULL;
}