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) char *id = xs_dict_get(object, "id"); char *in_reply_to = xs_dict_get(object, "inReplyTo"); - if (in_reply_to != NULL) { + if (xs_is_null(in_reply_to)) { /* recursively download ancestors */ /* ... */ } diff --git a/data.c b/data.c index c3576cc..0b87c6a 100644 --- a/data.c +++ b/data.c @@ -391,7 +391,7 @@ d_char *_timeline_new_fn(snac *snac, char *id) } -void timeline_add(snac *snac, char *id, char *msg, char *parent) +void timeline_add(snac *snac, char *id, char *o_msg, char *parent) /* adds a message to the timeline */ { xs *pfn = _timeline_find_fn(snac, id); @@ -404,6 +404,7 @@ void timeline_add(snac *snac, char *id, char *msg, char *parent) /* build the new filename */ xs *fn = _timeline_new_fn(snac, id); + xs *msg = xs_dup(o_msg); xs *md; /* add metadata */ @@ -414,7 +415,7 @@ void timeline_add(snac *snac, char *id, char *msg, char *parent) "\"parent\": null" "}"); - if (parent != NULL) + if (!xs_is_null(parent)) md = xs_dict_set(md, "parent", parent); msg = xs_dict_set(msg, "_snac", md); @@ -430,14 +431,14 @@ void timeline_add(snac *snac, char *id, char *msg, char *parent) /* related to this user? link to local timeline */ if (xs_startswith(id, snac->actor) || - (parent != NULL && xs_startswith(parent, snac->actor))) { + (!xs_is_null(parent) && xs_startswith(parent, snac->actor))) { xs *lfn = xs_replace(fn, "/timeline/", "/local/"); link(fn, lfn); snac_debug(snac, 1, xs_fmt("timeline_add (local) %s %s", id, lfn)); } - if (parent != NULL) { + if (!xs_is_null(parent)) { /* update the parent, adding this id to its children list */ xs *pfn = _timeline_find_fn(snac, parent); xs *p_msg = NULL; @@ -493,7 +494,7 @@ void timeline_add(snac *snac, char *id, char *msg, char *parent) /* now iterate all parents up, just renaming the files */ xs *grampa = xs_dup(xs_dict_get(meta, "parent")); - while (grampa != NULL) { + while (!xs_is_null(grampa)) { xs *gofn = _timeline_find_fn(snac, grampa); if (gofn == NULL) @@ -528,7 +529,7 @@ void timeline_add(snac *snac, char *id, char *msg, char *parent) free(grampa); - if (p != NULL) + if (!xs_is_null(p)) p = xs_dup(p); grampa = p; diff --git a/main.c b/main.c index 90fd5f9..d7d408f 100644 --- a/main.c +++ b/main.c @@ -9,11 +9,47 @@ int usage(void) { - printf("usage:\n"); + printf("snac - A simple, minimalistic ActivityPub instance\n"); + printf("Copyright (c) 2022 grunfink - MIT license\n"); + printf("\n"); + printf("Commands:\n"); + printf("\n"); + printf("init [{basedir}] Initializes the database\n"); + printf("httpd {basedir} Starts the HTTPD daemon\n"); + printf("webfinger {basedir} {user} Queries about a @user@host or actor\n"); + printf("queue {basedir} {uid} Processes a user queue\n"); +// printf("check {basedir} [{uid}] Checks the database\n"); +// printf("purge {basedir} [{uid}] Purges old data\n"); +// printf("adduser {basedir} [{uid}] Adds a new user\n"); + +// printf("update {basedir} {uid} Sends a user update to followers\n"); +// printf("passwd {basedir} {uid} Sets the password for {uid}\n"); +// printf("follow {basedir} {uid} {actor} Follows an actor\n"); +// printf("unfollow {basedir} {uid} {actor} Unfollows an actor\n"); +// printf("mute {basedir} {uid} {actor} Mutes an actor\n"); +// printf("unmute {basedir} {uid} {actor} Unmutes an actor\n"); +// printf("like {basedir} {uid} {url} Likes an url\n"); +// printf("announce {basedir} {uid} {url} Announces (boosts) an url\n"); +// printf("note {basedir} {uid} {'text'} Sends a note to followers\n"); + + printf("request {basedir} {uid} {url} Requests an object\n"); + printf("actor {basedir} {uid} {url} Requests an actor\n"); + return 1; } +char *get_argv(int *argi, int argc, char *argv[]) +{ + if (*argi < argc) + return argv[(*argi)++]; + else + return NULL; +} + + +#define GET_ARGV() get_argv(&argi, argc, argv) + int main(int argc, char *argv[]) { char *cmd; @@ -23,21 +59,20 @@ int main(int argc, char *argv[]) int argi = 1; snac snac; - argc--; - if (argc < argi) + if ((cmd = GET_ARGV()) == NULL) return usage(); - cmd = argv[argi++]; - if (strcmp(cmd, "init") == 0) { + /* initialize the database */ + /* ... */ + basedir = GET_ARGV(); + return 0; } - if (argc < argi) + if ((basedir = GET_ARGV()) == NULL) return usage(); - basedir = argv[argi++]; - if (!srv_open(basedir)) { srv_log(xs_fmt("error opening database at %s", basedir)); return 1; @@ -48,11 +83,9 @@ int main(int argc, char *argv[]) return 0; } - if (argc < argi) + if ((user = GET_ARGV()) == NULL) return usage(); - user = argv[argi++]; - if (strcmp(cmd, "webfinger") == 0) { xs *actor = NULL; xs *uid = NULL; @@ -69,16 +102,19 @@ int main(int argc, char *argv[]) return 0; } - if (argc < argi) - return usage(); - - url = argv[argi++]; - if (!user_open(&snac, user)) { printf("error in user '%s'\n", user); return 1; } + if (strcmp(cmd, "queue") == 0) { + process_queue(&snac); + return 0; + } + + if ((url = GET_ARGV()) == NULL) + return usage(); + if (strcmp(cmd, "request") == 0) { int status; xs *data = NULL; diff --git a/xs.h b/xs.h index fe8d78b..3562070 100644 --- a/xs.h +++ b/xs.h @@ -41,6 +41,7 @@ void _xs_destroy(char **var); #define xs_debug() kill(getpid(), 5) xstype xs_type(const char *data); int xs_size(const char *data); +int xs_is_null(char *data); d_char *xs_dup(const char *data); d_char *xs_expand(d_char *data, int offset, int size); d_char *xs_collapse(d_char *data, int offset, int size); @@ -185,6 +186,13 @@ int xs_size(const char *data) } +int xs_is_null(char *data) +/* checks for null */ +{ + return !!(data == NULL || xs_type(data) == XSTYPE_NULL); +} + + d_char *xs_dup(const char *data) /* creates a duplicate of data */ {