aboutsummaryrefslogtreecommitdiff
path: root/src/transmission.c
blob: 241b0b1a96be06a3ff3444b4eda86e343a5ecfd5 (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include "transmission.h"
#include "program.h"
#include "buffer.h"
#include "../depends/cJSON.h"
#include "rss_html_common.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

/* TODO: Make port configurable */

int transmission_connect(TransmissionSession *session) {
    int result = 0;
    Buffer buffer;
    buffer_init(&buffer);

    const char *args[] = { "curl", "-s", "http://127.0.0.1:9091/transmission/rpc", NULL };
    result = program_exec(args, program_buffer_write_callback, &buffer);
    if(result != 0) {
        fprintf(stderr, "Failed to retrieve transmission session id\n");
        goto cleanup;
    }

    char *session_id_start = strstr(buffer.data, "X-Transmission-Session-Id: ");
    if(!session_id_start) {
        fprintf(stderr, "Failed to find session id in transmission response\n");
        result = -1;
        goto cleanup;
    }

    char *session_id_end = strchr(session_id_start + 27, '<');
    if(!session_id_end) {
        fprintf(stderr, "Failed to find session id in transmission response\n");
        result = -1;
        goto cleanup;
    }

    const size_t session_id_len = session_id_end - session_id_start;
    if(session_id_len + 1 > sizeof(session->session_header)) {
        fprintf(stderr, "Session id is too long\n");
        result = -1;
        goto cleanup;
    }

    memcpy(session->session_header, session_id_start, session_id_end - session_id_start);
    session->session_header[session_id_end - session_id_start] = '\0';

    cleanup:
    buffer_deinit(&buffer);
    return result;
}

int transmission_is_daemon_running(void) {
    const char *args[] = { "transmission-remote", "-si", NULL };
    return program_exec(args, NULL, NULL);
}

int transmission_start_daemon(const char *download_dir) {
    /* TODO: Make seed ratio configurable */
    const char *args[] = { "transmission-daemon", "--global-seedratio", "2.0", "--download-dir", download_dir, NULL };
    int res = program_exec(args, NULL, NULL);
    if(res != 0)
        return res;

    fprintf(stderr, "Waiting for the transmission daemon to startup...\n");
    int num_tries = 0;
    const int max_tries = 7; /* 7 seconds */

    while(transmission_is_daemon_running() != 0 && num_tries < max_tries) {
        sleep(1);
        ++num_tries;
    }
    
    if(num_tries == max_tries) {
        fprintf(stderr, "Failed to launch transmission daemon in 7 seconds\n");
        return -1;
    }

    fprintf(stderr, "The transmission daemon is now running!\n");
    return 0;
}

static int transmission_response_is_success(cJSON *json_root) {
    cJSON *result_field = cJSON_GetObjectItemCaseSensitive(json_root, "result");
    if(!result_field || !cJSON_IsString(result_field)) {
        fprintf(stderr, "Error: Transmission response is missing result field\n");
        return -1;
    }

    return strcmp(result_field->valuestring, "success");
}

static int transmission_add_torrent_response_get_torrent_name(cJSON *json_root, int *torrent_id, const char **torrent_name) {
    cJSON *arguments_obj = cJSON_GetObjectItemCaseSensitive(json_root, "arguments");
    if(!cJSON_IsObject(arguments_obj)) {
        fprintf(stderr, "Error: transmission add torrent response is missing arguments field or its not an object\n");
        return -1;
    }

    cJSON *torrent_added_obj = cJSON_GetObjectItemCaseSensitive(arguments_obj, "torrent-added");
    if(!cJSON_IsObject(torrent_added_obj))
        torrent_added_obj = cJSON_GetObjectItemCaseSensitive(arguments_obj, "torrent-duplicate");
    
    if(!cJSON_IsObject(torrent_added_obj)) {
        fprintf(stderr, "Error: transmission add torrent response is missing both torrent-added and torrent-duplicate argument\n");
        return -1;
    }

    if(torrent_id) {
        cJSON *torrent_id_field = cJSON_GetObjectItemCaseSensitive(torrent_added_obj, "id");
        if(!cJSON_IsNumber(torrent_id_field)) {
            fprintf(stderr, "Error: transmission add torrent response is missing torrent id\n");
            return -1;
        }

        *torrent_id = torrent_id_field->valuedouble;
        if(*torrent_id == 0) {
            fprintf(stderr, "Error: transmission add torrent response has invalid torrent id\n");
            return -1;
        }
    }

    if(torrent_name) {
        cJSON *torrent_name_field = cJSON_GetObjectItemCaseSensitive(torrent_added_obj, "name");
        if(!cJSON_IsString(torrent_name_field)) {
            fprintf(stderr, "Error: transmission add torrent response is missing torrent name\n");
            return -1;
        }
        *torrent_name = torrent_name_field->valuestring;
    }
    
    return 0;
}

int transmission_add_torrent(TransmissionSession *session, const char *url, int *torrent_id, char **torrent_name) {
    int result = 0;
    Buffer buffer;
    buffer_init(&buffer);
    cJSON *json_response = NULL;

    /* TODO: json escape url */
    char request[4096];
    int written_bytes = snprintf(request, sizeof(request), "{ \"arguments\": { \"filename\": \"%s\" }, \"method\": \"torrent-add\" }", url);
    if(written_bytes >= (int)sizeof(request)) {
        fprintf(stderr, "Failed to write all bytes to request (this is a bug)\n");
        result = -1;
        goto cleanup;
    }

    const char *args[] = { "curl", "-s", "-f", "-d", request, "-H", session->session_header, "--", "http://127.0.0.1:9091/transmission/rpc", NULL };
    result = program_exec(args, program_buffer_write_callback, &buffer);
    if(result != 0) {
        /* Maybe the request failed because the session is no longer valid? retry once with new session id */
        if(transmission_connect(session) != 0) {
            fprintf(stderr, "Failed to add torrent: %s\n", url);
            goto cleanup;
        }

        buffer_clear(&buffer);
        const char *new_args[] = { "curl", "-s", "-f", "-d", request, "-H", session->session_header, "--", "http://127.0.0.1:9091/transmission/rpc", NULL };
        result = program_exec(new_args, program_buffer_write_callback, &buffer);
        if(result != 0) {
            fprintf(stderr, "Failed to add torrent: %s\n", url);
            goto cleanup;
        }
    }

    json_response = cJSON_ParseWithLength(buffer.data, buffer.size);
    if(!json_response || !cJSON_IsObject(json_response)) {
        fprintf(stderr, "Failed to parse torrent add response: %.*s as json\n", (int)buffer.size, (char*)buffer.data);
        result = -1;
        goto cleanup;
    }
    
    if(transmission_response_is_success(json_response) != 0) {
        fprintf(stderr, "Error: transmission torrent add request failed, response: %.*s\n", (int)buffer.size, (char*)buffer.data);
        result = -1;
        goto cleanup;
    }

    int response_torrent_id;
    const char *response_torrent_name;
    result = transmission_add_torrent_response_get_torrent_name(json_response,
        torrent_id ? &response_torrent_id : NULL,
        torrent_name ? &response_torrent_name : NULL);

    if(result != 0)
        goto cleanup;

    if(torrent_id)
        *torrent_id = response_torrent_id;

    if(torrent_name)
        *torrent_name = strdup(response_torrent_name);

    cleanup:
    cJSON_Delete(json_response);
    buffer_deinit(&buffer);
    return result;
}

int transmission_list_torrents(TransmissionSession *session, TorrentListCallback callback, void *userdata) {
    int result = 0;
    Buffer buffer;
    buffer_init(&buffer);
    cJSON *json_response = NULL;

    char request[100];
    int written_bytes = snprintf(request, sizeof(request), "{ \"arguments\": { \"fields\": [ \"id\", \"name\", \"percentDone\" ] }, \"method\": \"torrent-get\" }");
    if(written_bytes >= (int)sizeof(request)) {
        fprintf(stderr, "Failed to write all bytes to request (this is a bug)\n");
        result = -1;
        goto cleanup;
    }

    const char *args[] = { "curl", "-s", "-f", "-d", request, "-H", session->session_header, "--", "http://127.0.0.1:9091/transmission/rpc", NULL };
    result = program_exec(args, program_buffer_write_callback, &buffer);
    if(result != 0) {
        /* Maybe the request failed because the session is no longer valid? retry once with new session id */
        if(transmission_connect(session) != 0) {
            fprintf(stderr, "Failed to list torrents\n");
            goto cleanup;
        }

        buffer_clear(&buffer);
        const char *new_args[] = { "curl", "-s", "-f", "-d", request, "-H", session->session_header, "--", "http://127.0.0.1:9091/transmission/rpc", NULL };
        result = program_exec(new_args, program_buffer_write_callback, &buffer);
        if(result != 0) {
            fprintf(stderr, "Failed to list torrents\n");
            goto cleanup;
        }
    }

    json_response = cJSON_ParseWithLength(buffer.data, buffer.size);
    if(!json_response || !cJSON_IsObject(json_response)) {
        fprintf(stderr, "Failed to parse torrent list response: %.*s as json\n", (int)buffer.size, (char*)buffer.data);
        result = -1;
        goto cleanup;
    }
    
    if(transmission_response_is_success(json_response) != 0) {
        fprintf(stderr, "Error: transmission torrent list request failed, response: %.*s\n", (int)buffer.size, (char*)buffer.data);
        result = -1;
        goto cleanup;
    }

    cJSON *arguments_field = cJSON_GetObjectItemCaseSensitive(json_response, "arguments");
    if(!cJSON_IsObject(arguments_field)) {
        fprintf(stderr, "Error: transmission torrent list response is missing arguments or its not an object\n");
        result = -1;
        goto cleanup;
    }

    cJSON *torrents_field = cJSON_GetObjectItemCaseSensitive(arguments_field, "torrents");
    if(!cJSON_IsArray(torrents_field)) {
        fprintf(stderr, "Error: transmission torrent list response is missing torrents or its not an array\n");
        result = -1;
        goto cleanup;
    }

    cJSON *torrent_item = NULL;
    cJSON_ArrayForEach(torrent_item, torrents_field) {
        if(!cJSON_IsObject(torrent_item))
            continue;

        cJSON *id_field = cJSON_GetObjectItemCaseSensitive(torrent_item, "id");
        cJSON *name_field = cJSON_GetObjectItemCaseSensitive(torrent_item, "name");
        cJSON *percent_done_field = cJSON_GetObjectItemCaseSensitive(torrent_item, "percentDone");
        if(!cJSON_IsNumber(id_field) || !cJSON_IsString(name_field) || !cJSON_IsNumber(percent_done_field))
            continue;

        callback(id_field->valuedouble, name_field->valuestring, percent_done_field->valuedouble, userdata);
    }

    cleanup:
    cJSON_Delete(json_response);
    buffer_deinit(&buffer);
    return result;
}