-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneocities.c
408 lines (333 loc) · 12.1 KB
/
neocities.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include "neocities.h"
#include <curl/curl.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
// Structure to hold response data
struct ResponseData {
char* data;
size_t size;
};
// Callback function for writing response data
static size_t write_callback(void* contents, size_t size, size_t nmemb, void* userp) {
size_t realsize = size * nmemb;
struct ResponseData* resp = (struct ResponseData*)userp;
char* ptr = realloc(resp->data, resp->size + realsize + 1);
if (!ptr) {
return 0;
}
resp->data = ptr;
memcpy(&(resp->data[resp->size]), contents, realsize);
resp->size += realsize;
resp->data[resp->size] = 0;
return realsize;
}
// Helper function for CURL requests
static bool perform_curl_request(CURL* curl, const char* url, curl_mime* form,
struct ResponseData* resp, const char* userpwd) {
CURLcode res;
bool success = false;
curl_easy_setopt(curl, CURLOPT_URL, url);
if (form) {
curl_easy_setopt(curl, CURLOPT_MIMEPOST, form);
}
curl_easy_setopt(curl, CURLOPT_USERPWD, userpwd);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, resp);
// SSL Options
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); // Don't verify peer
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); // Don't verify hostname
// Optional: Enable verbose output for debugging
// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "cURL Error: %s\n", curl_easy_strerror(res));
} else {
long response_code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
if (strstr(resp->data, "invalid_auth") != NULL) {
fprintf(stderr, "Authentication error: %s\n", resp->data);
} else {
success = true;
}
}
return success;
}
struct Neocities* neocities_init(const char* username, const char* password) {
struct Neocities* neo = malloc(sizeof(struct Neocities));
if (neo) {
neo->username = strdup(username);
neo->password = strdup(password);
}
return neo;
}
void neocities_cleanup(struct Neocities* neo) {
if (neo) {
free(neo->username);
free(neo->password);
free(neo);
}
}
bool neocities_upload(struct Neocities* neo, struct UploadFile* files, size_t file_count) {
CURL* curl;
curl_mime* form = NULL;
bool success = false;
struct ResponseData resp = {0};
// Check if files exist before proceeding
for (size_t i = 0; i < file_count; i++) {
FILE* test_file = fopen(files[i].filepath, "rb");
if (!test_file) {
fprintf(stderr, "Error: File not found: %s\n", files[i].filepath);
return false;
}
fclose(test_file);
}
curl = curl_easy_init();
if (curl) {
form = curl_mime_init(curl);
for (size_t i = 0; i < file_count; i++) {
curl_mimepart* field = curl_mime_addpart(form);
curl_mime_name(field, files[i].filename);
curl_mime_filedata(field, files[i].filepath);
}
char auth[256];
snprintf(auth, sizeof(auth), "%s:%s", neo->username, neo->password);
success = perform_curl_request(curl, "https://neocities.org/api/upload",
form, &resp, auth);
// Check the response for specific error messages
if (success && resp.data) {
if (strstr(resp.data, "error") != NULL) {
fprintf(stderr, "Upload error: %s\n", resp.data);
success = false;
}
}
curl_mime_free(form);
curl_easy_cleanup(curl);
}
free(resp.data);
return success;
}
bool neocities_delete_files(struct Neocities* neo, char** filenames, size_t filename_count) {
CURL* curl;
curl_mime* form = NULL;
bool success = false;
struct ResponseData resp = {0};
curl = curl_easy_init();
if (curl) {
form = curl_mime_init(curl);
// Add each filename to the form
for (size_t i = 0; i < filename_count; i++) {
curl_mimepart* field = curl_mime_addpart(form);
curl_mime_name(field, "filenames[]");
curl_mime_data(field, filenames[i], CURL_ZERO_TERMINATED);
}
char auth[256];
snprintf(auth, sizeof(auth), "%s:%s", neo->username, neo->password);
success = perform_curl_request(curl, "https://neocities.org/api/delete",
form, &resp, auth);
curl_mime_free(form);
curl_easy_cleanup(curl);
}
free(resp.data);
return success;
}
char* neocities_get_info(struct Neocities* neo, const char* sitename) {
CURL* curl;
struct ResponseData resp = {0};
char* response = NULL;
curl = curl_easy_init();
if (curl) {
char* encoded_sitename = curl_easy_escape(curl, sitename, strlen(sitename));
char url[512];
snprintf(url, sizeof(url), "https://neocities.org/api/info?sitename=%s", encoded_sitename);
char auth[256];
snprintf(auth, sizeof(auth), "%s:%s", neo->username, neo->password);
if (perform_curl_request(curl, url, NULL, &resp, auth)) {
response = strdup(resp.data);
}
curl_free(encoded_sitename);
curl_easy_cleanup(curl);
}
free(resp.data);
return response;
}
char* neocities_list_files(struct Neocities* neo, const char* path) {
CURL* curl;
struct ResponseData resp = {0};
char* response = NULL;
curl = curl_easy_init();
if (curl) {
char url[512] = "https://neocities.org/api/list";
if (path && *path) {
char* encoded_path = curl_easy_escape(curl, path, strlen(path));
snprintf(url, sizeof(url), "https://neocities.org/api/list?path=%s", encoded_path);
curl_free(encoded_path);
}
char auth[256];
snprintf(auth, sizeof(auth), "%s:%s", neo->username, neo->password);
if (perform_curl_request(curl, url, NULL, &resp, auth)) {
response = strdup(resp.data);
}
curl_easy_cleanup(curl);
}
free(resp.data);
return response;
}
// Helper function to find a JSON field and extract its value
static const char* find_json_field(const char* json, const char* field) {
char search_str[256];
snprintf(search_str, sizeof(search_str), "\"%s\":", field);
const char* pos = strstr(json, search_str);
if (!pos) return NULL;
return pos + strlen(search_str);
}
// Helper function to parse integer from JSON value
static int parse_json_int(const char* str) {
while (*str && (*str == ' ' || *str == '\t')) str++; // Skip whitespace
if (*str == ':') str++; // Skip colon if present
while (*str && (*str == ' ' || *str == '\t')) str++; // Skip whitespace
return atoi(str);
}
int neocities_get_hits(struct Neocities* neo, const char* sitename) {
char* info_json = neocities_get_info(neo, sitename);
int hits = -1;
if (info_json) {
// First check if the request was successful
const char* success = find_json_field(info_json, "result");
if (success && strstr(success, "\"success\"")) {
// Find the info object
const char* info = find_json_field(info_json, "info");
if (info) {
// Find the hits field within info
const char* hits_str = find_json_field(info, "hits");
if (hits_str) {
hits = parse_json_int(hits_str);
}
}
}
free(info_json);
}
return hits;
}
// Helper function to parse JSON array
static char** parse_json_array(const char* str, size_t* count) {
*count = 0;
char** result = NULL;
const char* p = str;
// Skip to opening bracket
while (*p && *p != '[') p++;
if (*p != '[') return NULL;
p++;
// Count elements and allocate array
const char* tmp = p;
while (*tmp && *tmp != ']') {
if (*tmp == '"') (*count)++;
tmp++;
}
if (*count == 0) return NULL;
result = malloc(sizeof(char*) * (*count));
if (!result) return NULL;
// Parse each string
size_t idx = 0;
while (*p && *p != ']' && idx < *count) {
// Skip to opening quote
while (*p && *p != '"') p++;
if (*p != '"') break;
p++;
// Find closing quote
const char* end = p;
while (*end && *end != '"') end++;
if (*end != '"') break;
// Copy string
size_t len = end - p;
result[idx] = malloc(len + 1);
if (result[idx]) {
strncpy(result[idx], p, len);
result[idx][len] = '\0';
idx++;
}
p = end + 1;
}
*count = idx;
return result;
}
// Helper function to parse ISO 8601 date string
static time_t parse_iso8601_date(const char* date_str) {
struct tm tm = {0};
int year, month, day;
// Skip the opening quote if present
if (*date_str == '"') date_str++;
// Parse YYYY-MM-DD format
if (sscanf(date_str, "%d-%d-%d", &year, &month, &day) == 3) {
tm.tm_year = year - 1900; // Years since 1900
tm.tm_mon = month - 1; // Months are 0-11
tm.tm_mday = day;
tm.tm_hour = 12; // Set to noon to avoid DST issues
return mktime(&tm);
}
return 0;
}
int neocities_get_views(struct Neocities* neo, const char* sitename) {
char* info_json = neocities_get_info(neo, sitename);
int views = -1;
if (info_json) {
const char* success = find_json_field(info_json, "result");
if (success && strstr(success, "\"success\"")) {
const char* info = find_json_field(info_json, "info");
if (info) {
const char* views_str = find_json_field(info, "views");
if (views_str) {
views = parse_json_int(views_str);
}
}
}
free(info_json);
}
return views;
}
char** neocities_get_tags(struct Neocities* neo, const char* sitename, size_t* tag_count) {
char* info_json = neocities_get_info(neo, sitename);
char** tags = NULL;
*tag_count = 0;
if (info_json) {
const char* success = find_json_field(info_json, "result");
if (success && strstr(success, "\"success\"")) {
const char* info = find_json_field(info_json, "info");
if (info) {
const char* tags_str = find_json_field(info, "tags");
if (tags_str) {
tags = parse_json_array(tags_str, tag_count);
}
}
}
free(info_json);
}
return tags;
}
time_t neocities_get_created_at(struct Neocities* neo, const char* sitename) {
char* info_json = neocities_get_info(neo, sitename);
time_t created_at = 0;
if (info_json) {
const char* success = find_json_field(info_json, "result");
if (success && strstr(success, "\"success\"")) {
const char* info = find_json_field(info_json, "info");
if (info) {
const char* created_at_str = find_json_field(info, "created_at");
if (created_at_str) {
created_at = parse_iso8601_date(created_at_str);
}
}
}
free(info_json);
}
return created_at;
}
void neocities_free_tags(char** tags, size_t tag_count) {
if (tags) {
for (size_t i = 0; i < tag_count; i++) {
free(tags[i]);
}
free(tags);
}
}