123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489 |
- #include "xs.h"
- #include "xs_encdec.h"
- #include "xs_json.h"
- #include "xs_curl.h"
- #include "xs_mime.h"
- #include "snac.h"
- const char *public_address = "https:/" "/www.w3.org/ns/activitystreams#Public";
- int activitypub_request(snac *snac, char *url, d_char **data)
- {
- int status;
- xs *response = NULL;
- xs *payload = NULL;
- int p_size;
- char *ctype;
-
-
-
- response = http_signed_request(snac, "GET", url,
- NULL, NULL, 0, &status, &payload, &p_size);
- if (valid_status(status)) {
-
- ctype = xs_dict_get(response, "content-type");
- if (xs_str_in(ctype, "application/activity+json") != -1)
- *data = xs_json_loads(payload);
- else
- status = 500;
- }
- if (!valid_status(status))
- *data = NULL;
- return status;
- }
- int actor_request(snac *snac, char *actor, d_char **data)
- {
- int status, status2;
- xs *payload = NULL;
-
- status = actor_get(snac, actor, data);
- if (status == 200)
- return status;
-
- status2 = activitypub_request(snac, actor, &payload);
- if (valid_status(status2)) {
-
- status = actor_add(snac, actor, payload);
- *data = payload;
- payload = NULL;
- }
- return status;
- }
- void timeline_request(snac *snac, char *id)
- {
- if (!xs_is_null(id)) {
-
- if (!timeline_here(snac, id)) {
- int status;
- xs *object = NULL;
-
- status = activitypub_request(snac, id, &object);
- if (valid_status(status)) {
-
- char *in_reply_to = xs_dict_get(object, "inReplyTo");
-
- timeline_request(snac, in_reply_to);
-
- timeline_add(snac, id, object, in_reply_to);
- }
- }
- }
- }
- int send_to_inbox(snac *snac, char *inbox, char *msg, d_char **payload, int *p_size)
- {
- int status;
- d_char *response;
- xs *j_msg = xs_json_dumps_pp(msg, 4);
- response = http_signed_request(snac, "POST", inbox,
- NULL, j_msg, strlen(j_msg), &status, payload, p_size);
- free(response);
- return status;
- }
- int send_to_actor(snac *snac, char *actor, char *msg, d_char **payload, int *p_size)
- {
- int status;
- xs *data = NULL;
-
- status = actor_request(snac, actor, &data);
- if (valid_status(status)) {
- char *inbox = xs_dict_get(data, "inbox");
- if (inbox != NULL)
- status = send_to_inbox(snac, inbox, msg, payload, p_size);
- else
- status = 400;
- }
- snac_log(snac, xs_fmt("send_to_actor %s %d", actor, status));
- return status;
- }
- d_char *msg_base(snac *snac, char *type, char *id, char *actor, char *date)
- {
- d_char *msg = xs_dict_new();
- msg = xs_dict_append(msg, "@context", "https:/" "/www.w3.org/ns/activitystreams");
- msg = xs_dict_append(msg, "type", type);
- if (id != NULL)
- msg = xs_dict_append(msg, "id", id);
- if (actor != NULL)
- msg = xs_dict_append(msg, "actor", actor);
- if (date != NULL) {
- xs *published = xs_utc_time("%Y-%m-%dT%H:%M:%SZ");
- msg = xs_dict_append(msg, "published", published);
- }
- return msg;
- }
- d_char *msg_collection(snac *snac, char *id)
- {
- d_char *msg = msg_base(snac, "OrderedCollection", id, NULL, NULL);
- xs *ol = xs_list_new();
- xs *nz = xs_number_new(0);
- msg = xs_dict_append(msg, "attributedTo", snac->actor);
- msg = xs_dict_append(msg, "orderedItems", ol);
- msg = xs_dict_append(msg, "totalItems", nz);
- return msg;
- }
- d_char *msg_update(snac *snac, char *object)
- {
- xs *id = xs_fmt("%s/Update", xs_dict_get(object, "id"));
- d_char *msg = msg_base(snac, "Update", id, snac->actor, "");
- msg = xs_dict_append(msg, "to", public_address);
- msg = xs_dict_append(msg, "object", object);
- return msg;
- }
- d_char *msg_actor(snac *snac)
- {
- xs *ctxt = xs_list_new();
- xs *icon = xs_dict_new();
- xs *keys = xs_dict_new();
- xs *avtr = NULL;
- xs *kid = NULL;
- d_char *msg = msg_base(snac, "Person", snac->actor, NULL, NULL);
- char *p;
- int n;
-
- ctxt = xs_list_append(ctxt, "https:/" "/www.w3.org/ns/activitystreams");
- ctxt = xs_list_append(ctxt, "https:/" "/w3id.org/security/v1");
- msg = xs_dict_set(msg, "@context", ctxt);
- msg = xs_dict_set(msg, "url", snac->actor);
- msg = xs_dict_set(msg, "name", xs_dict_get(snac->config, "name"));
- msg = xs_dict_set(msg, "preferredUsername", snac->uid);
- msg = xs_dict_set(msg, "published", xs_dict_get(snac->config, "published"));
- msg = xs_dict_set(msg, "summary", xs_dict_get(snac->config, "bio"));
- char *folders[] = { "inbox", "outbox", "followers", "following", NULL };
- for (n = 0; folders[n]; n++) {
- xs *f = xs_fmt("%s/%s", snac->actor, folders[n]);
- msg = xs_dict_set(msg, folders[n], f);
- }
- p = xs_dict_get(snac->config, "avatar");
- if (*p == '\0')
- avtr = xs_fmt("%s/susie.png", srv_baseurl);
- else
- avtr = xs_dup(p);
- icon = xs_dict_append(icon, "type", "Image");
- icon = xs_dict_append(icon, "mediaType", xs_mime_by_ext(avtr));
- icon = xs_dict_append(icon, "url", avtr);
- msg = xs_dict_set(msg, "icon", icon);
- kid = xs_fmt("%s#main-key", snac->actor);
- keys = xs_dict_append(keys, "id", kid);
- keys = xs_dict_append(keys, "owner", snac->actor);
- keys = xs_dict_append(keys, "publicKeyPem", xs_dict_get(snac->key, "public"));
- msg = xs_dict_set(msg, "publicKey", keys);
- return msg;
- }
- void process_message(snac *snac, char *msg, char *req)
- {
-
- char *actor = xs_dict_get(msg, "actor");
- char *type = xs_dict_get(msg, "type");
- char *object, *utype;
- object = xs_dict_get(msg, "object");
- if (object != NULL && xs_type(object) == XSTYPE_SOD)
- utype = xs_dict_get(object, "type");
- else
- utype = "(null)";
-
-
- if (strcmp(type, "Create") == 0) {
- if (strcmp(utype, "Note") == 0) {
- if (is_muted(snac, actor))
- snac_log(snac, xs_fmt("ignored 'Note' from muted actor %s", actor));
- else {
- char *id = xs_dict_get(object, "id");
- char *in_reply_to = xs_dict_get(object, "inReplyTo");
- timeline_request(snac, in_reply_to);
- if (timeline_add(snac, id, msg, in_reply_to))
- snac_log(snac, xs_fmt("new 'Note' %s %s", actor, id));
- }
- }
- else
- snac_debug(snac, 1, xs_fmt("ignored 'Create' for object type '%s'", utype));
- }
- else
- if (strcmp(type, "Like") == 0) {
- if (xs_type(object) == XSTYPE_STRING) {
- timeline_admire(snac, object, actor, 1);
- snac_log(snac, xs_fmt("new 'Like' %s %s", actor, object));
- }
- else
- snac_debug(snac, 2, xs_fmt("xs_type for 'Like' object not string"));
- }
- else
- if (strcmp(type, "Announce") == 0) {
- if (xs_type(object) == XSTYPE_STRING) {
- timeline_request(snac, object);
- timeline_admire(snac, object, actor, 0);
- snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object));
- }
- else
- snac_debug(snac, 2, xs_fmt("xs_type for 'Announce' object not string"));
- }
- else
- snac_debug(snac, 1, xs_fmt("process_message type '%s' ignored", type));
- }
- void process_queue(snac *snac)
- {
- xs *list;
- char *p, *fn;
- int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
- list = queue(snac);
- p = list;
- while (xs_list_iter(&p, &fn)) {
- xs *q_item = dequeue(snac, fn);
- char *type;
- if ((type = xs_dict_get(q_item, "type")) == NULL)
- type = "output";
- if (strcmp(type, "output") == 0) {
- int status;
- char *actor = xs_dict_get(q_item, "actor");
- char *msg = xs_dict_get(q_item, "object");
- int retries = xs_number_get(xs_dict_get(q_item, "retries"));
-
- status = send_to_actor(snac, actor, msg, NULL, 0);
- if (!valid_status(status)) {
-
- if (retries > queue_retry_max)
- snac_log(snac, xs_fmt("process_queue giving up %s %d", actor, status));
- else {
-
- enqueue_output(snac, msg, actor, retries + 1);
- snac_log(snac, xs_fmt("process_queue requeue %s %d", actor, retries + 1));
- }
- }
- }
- else
- if (strcmp(type, "input") == 0) {
-
- char *msg = xs_dict_get(q_item, "object");
- char *req = xs_dict_get(q_item, "req");
- process_message(snac, msg, req);
- }
- }
- }
- int activitypub_get_handler(d_char *req, char *q_path,
- char **body, int *b_size, char **ctype)
- {
- int status = 200;
- char *accept = xs_dict_get(req, "accept");
- snac snac;
- xs *msg = NULL;
- if (accept == NULL)
- return 400;
- if (xs_str_in(accept, "application/activity+json") == -1 &&
- xs_str_in(accept, "application/ld+json") == -1)
- return 0;
- xs *l = xs_split_n(q_path, "/", 2);
- char *uid, *p_path;
- uid = xs_list_get(l, 1);
- if (!user_open(&snac, uid)) {
-
- srv_log(xs_fmt("activitypub_get_handler bad user %s", uid));
- return 404;
- }
- p_path = xs_list_get(l, 2);
- if (p_path == NULL) {
-
- msg = msg_actor(&snac);
- }
- else
- if (strcmp(p_path, "outbox") == 0) {
- xs *id = xs_fmt("%s/outbox", snac.actor);
- msg = msg_collection(&snac, id);
-
-
- }
- else
- if (strcmp(p_path, "followers") == 0 || strcmp(p_path, "following") == 0) {
- xs *id = xs_fmt("%s/%s", snac.actor, p_path);
- msg = msg_collection(&snac, id);
- }
- else
- if (xs_startswith(p_path, "p/")) {
- }
- else
- status = 404;
- if (status == 200 && msg != NULL) {
- *body = xs_json_dumps_pp(msg, 4);
- *b_size = strlen(*body);
- }
- user_free(&snac);
- return status;
- }
- int activitypub_post_handler(d_char *req, char *q_path,
- d_char *payload, int p_size,
- char **body, int *b_size, char **ctype)
- {
- int status = 202;
- char *i_ctype = xs_dict_get(req, "content-type");
- snac snac;
- if (i_ctype == NULL)
- return 400;
- if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
- xs_str_in(i_ctype, "application/ld+json") == -1)
- return 0;
-
- xs *msg = xs_json_loads(payload);
- if (msg == NULL) {
- srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
- status = 400;
- }
-
- xs *l = xs_split_n(q_path, "/", 2);
- char *uid;
- if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
-
- srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
- return 404;
- }
- uid = xs_list_get(l, 1);
- if (!user_open(&snac, uid)) {
-
- srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid));
- return 404;
- }
- enqueue_input(&snac, msg, req);
- user_free(&snac);
- if (valid_status(status))
- *ctype = "application/activity+json";
- return status;
- }
|