From 05bf4b2c3c9888ab8830e8f4bbe73e8ad4538760 Mon Sep 17 00:00:00 2001 From: default Date: Tue, 27 Sep 2022 15:28:08 +0200 Subject: [PATCH] New function msg_follow(). --- activitypub.c | 28 ++++++++++++++++++++++++++++ main.c | 31 ++++++++++++++++++++++++++----- snac.h | 1 + 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/activitypub.c b/activitypub.c index d54e5ba..3ea05f5 100644 --- a/activitypub.c +++ b/activitypub.c @@ -380,6 +380,34 @@ d_char *msg_create(snac *snac, char *object) } +d_char *msg_follow(snac *snac, char *actor) +/* creates a 'Follow' message */ +{ + d_char *actor_o = NULL; + d_char *msg = NULL; + int status; + + /* request the actor */ + status = actor_request(snac, actor, &actor_o); + + if (valid_status(status)) { + /* check if the actor is an alias */ + char *r_actor = xs_dict_get(actor_o, "id"); + + if (r_actor && strcmp(actor, r_actor) != 0) { + snac_log(snac, xs_fmt("actor to follow is an alias %s -> %s", actor, r_actor)); + actor = r_actor; + } + + msg = msg_base(snac, "Follow", "@dummy", snac->actor, NULL, actor); + } + else + snac_log(snac, xs_fmt("cannot get actor to follow %s %d", actor, status)); + + return msg; +} + + d_char *msg_note(snac *snac, char *content, char *rcpts, char *in_reply_to) /* creates a 'Note' message */ { diff --git a/main.c b/main.c index c8cba6f..39164d1 100644 --- a/main.c +++ b/main.c @@ -19,13 +19,14 @@ int usage(void) 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("follow {basedir} {uid} {actor} Follows an actor\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"); @@ -124,8 +125,29 @@ int main(int argc, char *argv[]) if (msg != NULL) { post(&snac, msg); - xs *j = xs_json_dumps_pp(msg, 4); - printf("%s\n", j); + if (dbglevel) { + xs *j = xs_json_dumps_pp(msg, 4); + printf("%s\n", j); + } + } + + return 0; + } + + if (strcmp(cmd, "follow") == 0) { + xs *msg = msg_follow(&snac, url); + + if (msg != NULL) { + char *actor = xs_dict_get(msg, "object"); + + following_add(&snac, actor, msg); + + enqueue_output(&snac, msg, actor, 0); + + if (dbglevel) { + xs *j = xs_json_dumps_pp(msg, 4); + printf("%s\n", j); + } } return 0; @@ -164,7 +186,6 @@ int main(int argc, char *argv[]) } if (strcmp(cmd, "note") == 0) { - int status; xs *content = NULL; xs *msg = NULL; xs *c_msg = NULL; @@ -194,7 +215,7 @@ int main(int argc, char *argv[]) c_msg = msg_create(&snac, msg); - { + if (dbglevel) { xs *j = xs_json_dumps_pp(c_msg, 4); printf("%s\n", j); } diff --git a/snac.h b/snac.h index 7d671db..3e83be0 100644 --- a/snac.h +++ b/snac.h @@ -89,6 +89,7 @@ int webfinger_get_handler(d_char *req, char *q_path, d_char *msg_admiration(snac *snac, char *object, char *type); d_char *msg_create(snac *snac, char *object); +d_char *msg_follow(snac *snac, char *actor); d_char *msg_note(snac *snac, char *content, char *rcpts, char *in_reply_to); int activitypub_request(snac *snac, char *url, d_char **data);