summaryrefslogtreecommitdiff
path: root/webfinger.c
diff options
context:
space:
mode:
Diffstat (limited to 'webfinger.c')
-rw-r--r--webfinger.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/webfinger.c b/webfinger.c
new file mode 100644
index 0000000..be89ea9
--- /dev/null
+++ b/webfinger.c
@@ -0,0 +1,94 @@
1/* snac - A simple, minimalistic ActivityPub instance */
2/* copyright (c) 2022 grunfink - MIT license */
3
4#include "xs.h"
5#include "xs_encdec.h"
6#include "xs_json.h"
7
8#include "snac.h"
9
10void webfinger_get_handler(d_char *req, char *q_path, int *status,
11 char **body, int *b_size, char **ctype)
12/* serves webfinger queries */
13{
14 if (strcmp(q_path, "/.well-known/webfinger") != 0)
15 return;
16
17 char *q_vars = xs_dict_get(req, "q_vars");
18 char *resource = xs_dict_get(q_vars, "resource");
19
20 if (resource == NULL) {
21 *status = 400;
22 return;
23 }
24
25 snac snac;
26 int found = 0;
27
28 if (xs_startswith(resource, "https:/" "/")) {
29 /* actor search: find a user with this actor */
30 xs *list = user_list();
31 char *p, *uid;
32
33 p = list;
34 while (xs_list_iter(&p, &uid)) {
35 if (user_open(&snac, uid)) {
36 if (strcmp(snac.actor, resource) == 0) {
37 found = 1;
38 break;
39 }
40
41 user_free(&snac);
42 }
43 }
44 }
45 else
46 if (xs_startswith(resource, "acct:")) {
47 /* it's an account name */
48 xs *an = xs_replace(resource, "acct:", "");
49 xs *l = NULL;
50
51 /* strip a possible leading @ */
52 if (xs_startswith(an, "@"))
53 an = xs_crop(an, 1, 0);
54
55 l = xs_split_n(an, "@", 1);
56
57 if (xs_list_len(l) == 2) {
58 char *uid = xs_list_get(l, 0);
59 char *host = xs_list_get(l, 1);
60
61 if (strcmp(host, xs_dict_get(srv_config, "host")) == 0)
62 found = user_open(&snac, uid);
63 }
64 }
65
66 if (found) {
67 /* build the object */
68 xs *acct;
69 xs *aaj = xs_dict_new();
70 xs *links = xs_list_new();
71 xs *obj = xs_dict_new();
72 d_char *j;
73
74 acct = xs_fmt("acct:%s@%s",
75 xs_dict_get(snac.config, "uid"), xs_dict_get(srv_config, "host"));
76
77 aaj = xs_dict_append(aaj, "rel", "self");
78 aaj = xs_dict_append(aaj, "type", "application/activity+json");
79 aaj = xs_dict_append(aaj, "href", snac.actor);
80
81 links = xs_list_append(links, aaj);
82
83 obj = xs_dict_append(obj, "subject", acct);
84 obj = xs_dict_append(obj, "links", links);
85
86 j = xs_json_dumps_pp(obj, 4);
87
88 user_free(&snac);
89
90 *status = 200;
91 *body = j;
92 *ctype = "application/json";
93 }
94}