diff options
| author | 2022-09-25 21:02:47 +0200 | |
|---|---|---|
| committer | 2022-09-25 21:02:47 +0200 | |
| commit | e132ce5076db459642b07b7e85af80f4c353edf7 (patch) | |
| tree | 63c823f0d3b023647031a2e02c058a6a9bbc7564 | |
| parent | Minor tweak in http signatures. (diff) | |
| download | penes-snac2-e132ce5076db459642b07b7e85af80f4c353edf7.tar.gz penes-snac2-e132ce5076db459642b07b7e85af80f4c353edf7.tar.xz penes-snac2-e132ce5076db459642b07b7e85af80f4c353edf7.zip | |
New function msg_actor().
| -rw-r--r-- | activitypub.c | 55 | ||||
| -rw-r--r-- | data.c | 2 | ||||
| -rw-r--r-- | http.c | 1 | ||||
| -rw-r--r-- | main.c | 1 | ||||
| -rw-r--r-- | snac.c | 1 | ||||
| -rw-r--r-- | xs_mime.h | 52 |
6 files changed, 110 insertions, 2 deletions
diff --git a/activitypub.c b/activitypub.c index 8924f1c..7cb0f37 100644 --- a/activitypub.c +++ b/activitypub.c | |||
| @@ -5,6 +5,7 @@ | |||
| 5 | #include "xs_encdec.h" | 5 | #include "xs_encdec.h" |
| 6 | #include "xs_json.h" | 6 | #include "xs_json.h" |
| 7 | #include "xs_curl.h" | 7 | #include "xs_curl.h" |
| 8 | #include "xs_mime.h" | ||
| 8 | 9 | ||
| 9 | #include "snac.h" | 10 | #include "snac.h" |
| 10 | 11 | ||
| @@ -195,6 +196,59 @@ d_char *msg_update(snac *snac, char *object) | |||
| 195 | } | 196 | } |
| 196 | 197 | ||
| 197 | 198 | ||
| 199 | d_char *msg_actor(snac *snac) | ||
| 200 | /* create a Person message for this actor */ | ||
| 201 | { | ||
| 202 | xs *ctxt = xs_list_new(); | ||
| 203 | xs *icon = xs_dict_new(); | ||
| 204 | xs *keys = xs_dict_new(); | ||
| 205 | xs *avtr = NULL; | ||
| 206 | xs *kid = NULL; | ||
| 207 | d_char *msg = msg_base(snac, "Person", snac->actor, NULL, NULL); | ||
| 208 | char *p; | ||
| 209 | int n; | ||
| 210 | |||
| 211 | /* change the @context (is this really necessary?) */ | ||
| 212 | ctxt = xs_list_append(ctxt, "https:/" "/www.w3.org/ns/activitystreams"); | ||
| 213 | ctxt = xs_list_append(ctxt, "https:/" "/w3id.org/security/v1"); | ||
| 214 | msg = xs_dict_set(msg, "@context", ctxt); | ||
| 215 | |||
| 216 | msg = xs_dict_set(msg, "url", snac->actor); | ||
| 217 | msg = xs_dict_set(msg, "name", xs_dict_get(snac->config, "name")); | ||
| 218 | msg = xs_dict_set(msg, "preferredUsername", snac->uid); | ||
| 219 | msg = xs_dict_set(msg, "published", xs_dict_get(snac->config, "published")); | ||
| 220 | msg = xs_dict_set(msg, "summary", xs_dict_get(snac->config, "bio")); | ||
| 221 | |||
| 222 | char *folders[] = { "inbox", "outbox", "followers", "following", NULL }; | ||
| 223 | |||
| 224 | for (n = 0; folders[n]; n++) { | ||
| 225 | xs *f = xs_fmt("%s/%s", snac->actor, folders[n]); | ||
| 226 | msg = xs_dict_set(msg, folders[n], f); | ||
| 227 | } | ||
| 228 | |||
| 229 | p = xs_dict_get(snac->config, "avatar"); | ||
| 230 | |||
| 231 | if (*p == '\0') | ||
| 232 | avtr = xs_fmt("%s/susie.png", srv_baseurl); | ||
| 233 | else | ||
| 234 | avtr = xs_dup(p); | ||
| 235 | |||
| 236 | icon = xs_dict_append(icon, "type", "Image"); | ||
| 237 | icon = xs_dict_append(icon, "mediaType", xs_mime_by_ext(avtr)); | ||
| 238 | icon = xs_dict_append(icon, "url", avtr); | ||
| 239 | msg = xs_dict_set(msg, "icon", icon); | ||
| 240 | |||
| 241 | kid = xs_fmt("%s#main-key", snac->actor); | ||
| 242 | |||
| 243 | keys = xs_dict_append(keys, "id", kid); | ||
| 244 | keys = xs_dict_append(keys, "owner", snac->actor); | ||
| 245 | keys = xs_dict_append(keys, "publicKeyPem", xs_dict_get(snac->key, "public")); | ||
| 246 | msg = xs_dict_set(msg, "publicKey", keys); | ||
| 247 | |||
| 248 | return msg; | ||
| 249 | } | ||
| 250 | |||
| 251 | |||
| 198 | /** queues **/ | 252 | /** queues **/ |
| 199 | 253 | ||
| 200 | void process_message(snac *snac, char *msg, char *req) | 254 | void process_message(snac *snac, char *msg, char *req) |
| @@ -356,6 +410,7 @@ int activitypub_get_handler(d_char *req, char *q_path, | |||
| 356 | 410 | ||
| 357 | if (p_path == NULL) { | 411 | if (p_path == NULL) { |
| 358 | /* if there was no component after the user, it's an actor request */ | 412 | /* if there was no component after the user, it's an actor request */ |
| 413 | msg = msg_actor(&snac); | ||
| 359 | } | 414 | } |
| 360 | else | 415 | else |
| 361 | if (strcmp(p_path, "outbox") == 0) { | 416 | if (strcmp(p_path, "outbox") == 0) { |
| @@ -601,8 +601,6 @@ void timeline_admire(snac *snac, char *id, char *admirer, int like) | |||
| 601 | 601 | ||
| 602 | msg = xs_dict_set(msg, "_snac", meta); | 602 | msg = xs_dict_set(msg, "_snac", meta); |
| 603 | 603 | ||
| 604 | xs *j1 = xs_json_dumps_pp(msg, 4); | ||
| 605 | |||
| 606 | unlink(ofn); | 604 | unlink(ofn); |
| 607 | 605 | ||
| 608 | _timeline_write(snac, id, msg, xs_dict_get(meta, "parent")); | 606 | _timeline_write(snac, id, msg, xs_dict_get(meta, "parent")); |
| @@ -86,6 +86,7 @@ d_char *http_signed_request(snac *snac, char *method, char *url, | |||
| 86 | hdrs = xs_dict_append(hdrs, "date", date); | 86 | hdrs = xs_dict_append(hdrs, "date", date); |
| 87 | hdrs = xs_dict_append(hdrs, "signature", signature); | 87 | hdrs = xs_dict_append(hdrs, "signature", signature); |
| 88 | hdrs = xs_dict_append(hdrs, "digest", digest); | 88 | hdrs = xs_dict_append(hdrs, "digest", digest); |
| 89 | hdrs = xs_dict_append(hdrs, "host", xs_dict_get(srv_config, "host")); | ||
| 89 | hdrs = xs_dict_append(hdrs, "user-agent", "snac/2.x"); | 90 | hdrs = xs_dict_append(hdrs, "user-agent", "snac/2.x"); |
| 90 | 91 | ||
| 91 | response = xs_http_request(method, url, hdrs, | 92 | response = xs_http_request(method, url, hdrs, |
| @@ -50,6 +50,7 @@ char *get_argv(int *argi, int argc, char *argv[]) | |||
| 50 | 50 | ||
| 51 | #define GET_ARGV() get_argv(&argi, argc, argv) | 51 | #define GET_ARGV() get_argv(&argi, argc, argv) |
| 52 | 52 | ||
| 53 | |||
| 53 | int main(int argc, char *argv[]) | 54 | int main(int argc, char *argv[]) |
| 54 | { | 55 | { |
| 55 | char *cmd; | 56 | char *cmd; |
| @@ -11,6 +11,7 @@ | |||
| 11 | #include "xs_openssl.h" | 11 | #include "xs_openssl.h" |
| 12 | #include "xs_socket.h" | 12 | #include "xs_socket.h" |
| 13 | #include "xs_httpd.h" | 13 | #include "xs_httpd.h" |
| 14 | #include "xs_mime.h" | ||
| 14 | 15 | ||
| 15 | #include "snac.h" | 16 | #include "snac.h" |
| 16 | 17 | ||
diff --git a/xs_mime.h b/xs_mime.h new file mode 100644 index 0000000..699ab39 --- /dev/null +++ b/xs_mime.h | |||
| @@ -0,0 +1,52 @@ | |||
| 1 | /* copyright (c) 2022 grunfink - MIT license */ | ||
| 2 | |||
| 3 | #ifndef _XS_MIME | ||
| 4 | |||
| 5 | #define _XS_MIME | ||
| 6 | |||
| 7 | char *xs_mime_by_ext(char *file); | ||
| 8 | |||
| 9 | #ifdef XS_IMPLEMENTATION | ||
| 10 | |||
| 11 | /* intentionally brain-dead simple */ | ||
| 12 | struct _mime_info { | ||
| 13 | char *type; | ||
| 14 | char *ext; | ||
| 15 | } mime_info[] = { | ||
| 16 | { "application/json", ".json" }, | ||
| 17 | { "image/gif", ".gif" }, | ||
| 18 | { "image/jpeg", ".jpeg" }, | ||
| 19 | { "image/jpeg", ".jpg" }, | ||
| 20 | { "image/png", ".png" }, | ||
| 21 | { "image/webp", ".webp" }, | ||
| 22 | { "text/css", ".css" }, | ||
| 23 | { "text/html", ".html" }, | ||
| 24 | { "text/plain", ".txt" }, | ||
| 25 | { "text/xml", ".xml" }, | ||
| 26 | { NULL, NULL } | ||
| 27 | }; | ||
| 28 | |||
| 29 | |||
| 30 | char *xs_mime_by_ext(char *file) | ||
| 31 | /* returns the MIME type by file extension */ | ||
| 32 | { | ||
| 33 | struct _mime_info *mi = mime_info; | ||
| 34 | char *p = NULL; | ||
| 35 | |||
| 36 | while (p == NULL && mi->type != NULL) { | ||
| 37 | if (xs_endswith(file, mi->ext)) | ||
| 38 | p = mi->type; | ||
| 39 | |||
| 40 | mi++; | ||
| 41 | } | ||
| 42 | |||
| 43 | if (p == NULL) | ||
| 44 | p = "application/octet-stream"; | ||
| 45 | |||
| 46 | return p; | ||
| 47 | } | ||
| 48 | |||
| 49 | |||
| 50 | #endif /* XS_IMPLEMENTATION */ | ||
| 51 | |||
| 52 | #endif /* XS_MIME */ | ||