diff options
| -rw-r--r-- | activitypub.c | 2 | ||||
| -rw-r--r-- | data.c | 13 | ||||
| -rw-r--r-- | main.c | 68 | ||||
| -rw-r--r-- | xs.h | 8 |
4 files changed, 68 insertions, 23 deletions
diff --git a/activitypub.c b/activitypub.c index a6fa645..ba45395 100644 --- a/activitypub.c +++ b/activitypub.c | |||
| @@ -204,7 +204,7 @@ void process_message(snac *snac, char *msg, char *req) | |||
| 204 | char *id = xs_dict_get(object, "id"); | 204 | char *id = xs_dict_get(object, "id"); |
| 205 | char *in_reply_to = xs_dict_get(object, "inReplyTo"); | 205 | char *in_reply_to = xs_dict_get(object, "inReplyTo"); |
| 206 | 206 | ||
| 207 | if (in_reply_to != NULL) { | 207 | if (xs_is_null(in_reply_to)) { |
| 208 | /* recursively download ancestors */ | 208 | /* recursively download ancestors */ |
| 209 | /* ... */ | 209 | /* ... */ |
| 210 | } | 210 | } |
| @@ -391,7 +391,7 @@ d_char *_timeline_new_fn(snac *snac, char *id) | |||
| 391 | } | 391 | } |
| 392 | 392 | ||
| 393 | 393 | ||
| 394 | void timeline_add(snac *snac, char *id, char *msg, char *parent) | 394 | void timeline_add(snac *snac, char *id, char *o_msg, char *parent) |
| 395 | /* adds a message to the timeline */ | 395 | /* adds a message to the timeline */ |
| 396 | { | 396 | { |
| 397 | xs *pfn = _timeline_find_fn(snac, id); | 397 | xs *pfn = _timeline_find_fn(snac, id); |
| @@ -404,6 +404,7 @@ void timeline_add(snac *snac, char *id, char *msg, char *parent) | |||
| 404 | 404 | ||
| 405 | /* build the new filename */ | 405 | /* build the new filename */ |
| 406 | xs *fn = _timeline_new_fn(snac, id); | 406 | xs *fn = _timeline_new_fn(snac, id); |
| 407 | xs *msg = xs_dup(o_msg); | ||
| 407 | xs *md; | 408 | xs *md; |
| 408 | 409 | ||
| 409 | /* add metadata */ | 410 | /* add metadata */ |
| @@ -414,7 +415,7 @@ void timeline_add(snac *snac, char *id, char *msg, char *parent) | |||
| 414 | "\"parent\": null" | 415 | "\"parent\": null" |
| 415 | "}"); | 416 | "}"); |
| 416 | 417 | ||
| 417 | if (parent != NULL) | 418 | if (!xs_is_null(parent)) |
| 418 | md = xs_dict_set(md, "parent", parent); | 419 | md = xs_dict_set(md, "parent", parent); |
| 419 | 420 | ||
| 420 | msg = xs_dict_set(msg, "_snac", md); | 421 | msg = xs_dict_set(msg, "_snac", md); |
| @@ -430,14 +431,14 @@ void timeline_add(snac *snac, char *id, char *msg, char *parent) | |||
| 430 | 431 | ||
| 431 | /* related to this user? link to local timeline */ | 432 | /* related to this user? link to local timeline */ |
| 432 | if (xs_startswith(id, snac->actor) || | 433 | if (xs_startswith(id, snac->actor) || |
| 433 | (parent != NULL && xs_startswith(parent, snac->actor))) { | 434 | (!xs_is_null(parent) && xs_startswith(parent, snac->actor))) { |
| 434 | xs *lfn = xs_replace(fn, "/timeline/", "/local/"); | 435 | xs *lfn = xs_replace(fn, "/timeline/", "/local/"); |
| 435 | link(fn, lfn); | 436 | link(fn, lfn); |
| 436 | 437 | ||
| 437 | snac_debug(snac, 1, xs_fmt("timeline_add (local) %s %s", id, lfn)); | 438 | snac_debug(snac, 1, xs_fmt("timeline_add (local) %s %s", id, lfn)); |
| 438 | } | 439 | } |
| 439 | 440 | ||
| 440 | if (parent != NULL) { | 441 | if (!xs_is_null(parent)) { |
| 441 | /* update the parent, adding this id to its children list */ | 442 | /* update the parent, adding this id to its children list */ |
| 442 | xs *pfn = _timeline_find_fn(snac, parent); | 443 | xs *pfn = _timeline_find_fn(snac, parent); |
| 443 | xs *p_msg = NULL; | 444 | xs *p_msg = NULL; |
| @@ -493,7 +494,7 @@ void timeline_add(snac *snac, char *id, char *msg, char *parent) | |||
| 493 | /* now iterate all parents up, just renaming the files */ | 494 | /* now iterate all parents up, just renaming the files */ |
| 494 | xs *grampa = xs_dup(xs_dict_get(meta, "parent")); | 495 | xs *grampa = xs_dup(xs_dict_get(meta, "parent")); |
| 495 | 496 | ||
| 496 | while (grampa != NULL) { | 497 | while (!xs_is_null(grampa)) { |
| 497 | xs *gofn = _timeline_find_fn(snac, grampa); | 498 | xs *gofn = _timeline_find_fn(snac, grampa); |
| 498 | 499 | ||
| 499 | if (gofn == NULL) | 500 | if (gofn == NULL) |
| @@ -528,7 +529,7 @@ void timeline_add(snac *snac, char *id, char *msg, char *parent) | |||
| 528 | 529 | ||
| 529 | free(grampa); | 530 | free(grampa); |
| 530 | 531 | ||
| 531 | if (p != NULL) | 532 | if (!xs_is_null(p)) |
| 532 | p = xs_dup(p); | 533 | p = xs_dup(p); |
| 533 | 534 | ||
| 534 | grampa = p; | 535 | grampa = p; |
| @@ -9,11 +9,47 @@ | |||
| 9 | 9 | ||
| 10 | int usage(void) | 10 | int usage(void) |
| 11 | { | 11 | { |
| 12 | printf("usage:\n"); | 12 | printf("snac - A simple, minimalistic ActivityPub instance\n"); |
| 13 | printf("Copyright (c) 2022 grunfink - MIT license\n"); | ||
| 14 | printf("\n"); | ||
| 15 | printf("Commands:\n"); | ||
| 16 | printf("\n"); | ||
| 17 | printf("init [{basedir}] Initializes the database\n"); | ||
| 18 | printf("httpd {basedir} Starts the HTTPD daemon\n"); | ||
| 19 | printf("webfinger {basedir} {user} Queries about a @user@host or actor\n"); | ||
| 20 | printf("queue {basedir} {uid} Processes a user queue\n"); | ||
| 21 | // printf("check {basedir} [{uid}] Checks the database\n"); | ||
| 22 | // printf("purge {basedir} [{uid}] Purges old data\n"); | ||
| 23 | // printf("adduser {basedir} [{uid}] Adds a new user\n"); | ||
| 24 | |||
| 25 | // printf("update {basedir} {uid} Sends a user update to followers\n"); | ||
| 26 | // printf("passwd {basedir} {uid} Sets the password for {uid}\n"); | ||
| 27 | // printf("follow {basedir} {uid} {actor} Follows an actor\n"); | ||
| 28 | // printf("unfollow {basedir} {uid} {actor} Unfollows an actor\n"); | ||
| 29 | // printf("mute {basedir} {uid} {actor} Mutes an actor\n"); | ||
| 30 | // printf("unmute {basedir} {uid} {actor} Unmutes an actor\n"); | ||
| 31 | // printf("like {basedir} {uid} {url} Likes an url\n"); | ||
| 32 | // printf("announce {basedir} {uid} {url} Announces (boosts) an url\n"); | ||
| 33 | // printf("note {basedir} {uid} {'text'} Sends a note to followers\n"); | ||
| 34 | |||
| 35 | printf("request {basedir} {uid} {url} Requests an object\n"); | ||
| 36 | printf("actor {basedir} {uid} {url} Requests an actor\n"); | ||
| 37 | |||
| 13 | return 1; | 38 | return 1; |
| 14 | } | 39 | } |
| 15 | 40 | ||
| 16 | 41 | ||
| 42 | char *get_argv(int *argi, int argc, char *argv[]) | ||
| 43 | { | ||
| 44 | if (*argi < argc) | ||
| 45 | return argv[(*argi)++]; | ||
| 46 | else | ||
| 47 | return NULL; | ||
| 48 | } | ||
| 49 | |||
| 50 | |||
| 51 | #define GET_ARGV() get_argv(&argi, argc, argv) | ||
| 52 | |||
| 17 | int main(int argc, char *argv[]) | 53 | int main(int argc, char *argv[]) |
| 18 | { | 54 | { |
| 19 | char *cmd; | 55 | char *cmd; |
| @@ -23,21 +59,20 @@ int main(int argc, char *argv[]) | |||
| 23 | int argi = 1; | 59 | int argi = 1; |
| 24 | snac snac; | 60 | snac snac; |
| 25 | 61 | ||
| 26 | argc--; | 62 | if ((cmd = GET_ARGV()) == NULL) |
| 27 | if (argc < argi) | ||
| 28 | return usage(); | 63 | return usage(); |
| 29 | 64 | ||
| 30 | cmd = argv[argi++]; | ||
| 31 | |||
| 32 | if (strcmp(cmd, "init") == 0) { | 65 | if (strcmp(cmd, "init") == 0) { |
| 66 | /* initialize the database */ | ||
| 67 | /* ... */ | ||
| 68 | basedir = GET_ARGV(); | ||
| 69 | |||
| 33 | return 0; | 70 | return 0; |
| 34 | } | 71 | } |
| 35 | 72 | ||
| 36 | if (argc < argi) | 73 | if ((basedir = GET_ARGV()) == NULL) |
| 37 | return usage(); | 74 | return usage(); |
| 38 | 75 | ||
| 39 | basedir = argv[argi++]; | ||
| 40 | |||
| 41 | if (!srv_open(basedir)) { | 76 | if (!srv_open(basedir)) { |
| 42 | srv_log(xs_fmt("error opening database at %s", basedir)); | 77 | srv_log(xs_fmt("error opening database at %s", basedir)); |
| 43 | return 1; | 78 | return 1; |
| @@ -48,11 +83,9 @@ int main(int argc, char *argv[]) | |||
| 48 | return 0; | 83 | return 0; |
| 49 | } | 84 | } |
| 50 | 85 | ||
| 51 | if (argc < argi) | 86 | if ((user = GET_ARGV()) == NULL) |
| 52 | return usage(); | 87 | return usage(); |
| 53 | 88 | ||
| 54 | user = argv[argi++]; | ||
| 55 | |||
| 56 | if (strcmp(cmd, "webfinger") == 0) { | 89 | if (strcmp(cmd, "webfinger") == 0) { |
| 57 | xs *actor = NULL; | 90 | xs *actor = NULL; |
| 58 | xs *uid = NULL; | 91 | xs *uid = NULL; |
| @@ -69,16 +102,19 @@ int main(int argc, char *argv[]) | |||
| 69 | return 0; | 102 | return 0; |
| 70 | } | 103 | } |
| 71 | 104 | ||
| 72 | if (argc < argi) | ||
| 73 | return usage(); | ||
| 74 | |||
| 75 | url = argv[argi++]; | ||
| 76 | |||
| 77 | if (!user_open(&snac, user)) { | 105 | if (!user_open(&snac, user)) { |
| 78 | printf("error in user '%s'\n", user); | 106 | printf("error in user '%s'\n", user); |
| 79 | return 1; | 107 | return 1; |
| 80 | } | 108 | } |
| 81 | 109 | ||
| 110 | if (strcmp(cmd, "queue") == 0) { | ||
| 111 | process_queue(&snac); | ||
| 112 | return 0; | ||
| 113 | } | ||
| 114 | |||
| 115 | if ((url = GET_ARGV()) == NULL) | ||
| 116 | return usage(); | ||
| 117 | |||
| 82 | if (strcmp(cmd, "request") == 0) { | 118 | if (strcmp(cmd, "request") == 0) { |
| 83 | int status; | 119 | int status; |
| 84 | xs *data = NULL; | 120 | xs *data = NULL; |
| @@ -41,6 +41,7 @@ void _xs_destroy(char **var); | |||
| 41 | #define xs_debug() kill(getpid(), 5) | 41 | #define xs_debug() kill(getpid(), 5) |
| 42 | xstype xs_type(const char *data); | 42 | xstype xs_type(const char *data); |
| 43 | int xs_size(const char *data); | 43 | int xs_size(const char *data); |
| 44 | int xs_is_null(char *data); | ||
| 44 | d_char *xs_dup(const char *data); | 45 | d_char *xs_dup(const char *data); |
| 45 | d_char *xs_expand(d_char *data, int offset, int size); | 46 | d_char *xs_expand(d_char *data, int offset, int size); |
| 46 | d_char *xs_collapse(d_char *data, int offset, int size); | 47 | d_char *xs_collapse(d_char *data, int offset, int size); |
| @@ -185,6 +186,13 @@ int xs_size(const char *data) | |||
| 185 | } | 186 | } |
| 186 | 187 | ||
| 187 | 188 | ||
| 189 | int xs_is_null(char *data) | ||
| 190 | /* checks for null */ | ||
| 191 | { | ||
| 192 | return !!(data == NULL || xs_type(data) == XSTYPE_NULL); | ||
| 193 | } | ||
| 194 | |||
| 195 | |||
| 188 | d_char *xs_dup(const char *data) | 196 | d_char *xs_dup(const char *data) |
| 189 | /* creates a duplicate of data */ | 197 | /* creates a duplicate of data */ |
| 190 | { | 198 | { |