summaryrefslogtreecommitdiff
path: root/webfinger.c
blob: bcc5982ea1d095116a5f04d52a1dd45fb14f3a29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* snac - A simple, minimalistic ActivityPub instance */
/* copyright (c) 2022 grunfink - MIT license */

#include "xs.h"
#include "xs_encdec.h"
#include "xs_json.h"
#include "xs_curl.h"

#include "snac.h"

void webfinger_request(char *qs, int *status, char **actor, char **user)
/* queries the webfinger for qs and fills the required fields */
{
    xs *payload = NULL;
    int p_size = 0;
    xs *url = NULL;
    xs *headers = xs_dict_new();

    if (xs_startswith(qs, "https:/" "/")) {
        /* actor query: pick the host */
        xs *s = xs_replace(qs, "https:/" "/", "");
        xs *l = xs_split_n(s, "/", 1);

        url = xs_fmt("https:/" "/%s/.well-known/webfinger?resource=%s",
                xs_list_get(l, 0), qs);
    }
    else {
        /* it's a user */
        xs *s = xs_dup(qs);
        xs *l;

        if (xs_startswith(s, "@"))
            s = xs_crop(s, 1, 0);

        l = xs_split_n(s, "@", 1);

        if (xs_list_len(l) == 2) {
            url = xs_fmt("https:/" "/%s/.well-known/webfinger?resource:acct:%s",
                xs_list_get(l, 1), qs);
        }
    }

    if (url == NULL) {
        *status = 400;
        return;
    }

    headers = xs_dict_append(headers, "accept", "application/json");

    xs_http_request("GET", url, headers, NULL, 0, status, &payload, &p_size);
}


void webfinger_get_handler(d_char *req, char *q_path, int *status,
                           char **body, int *b_size, char **ctype)
/* serves webfinger queries */
{
    if (strcmp(q_path, "/.well-known/webfinger") != 0)
        return;

    char *q_vars   = xs_dict_get(req, "q_vars");
    char *resource = xs_dict_get(q_vars, "resource");

    if (resource == NULL) {
        *status = 400;
        return;
    }

    snac snac;
    int found = 0;

    if (xs_startswith(resource, "https:/" "/")) {
        /* actor search: find a user with this actor */
        xs *list = user_list();
        char *p, *uid;

        p = list;
        while (xs_list_iter(&p, &uid)) {
            if (user_open(&snac, uid)) {
                if (strcmp(snac.actor, resource) == 0) {
                    found = 1;
                    break;
                }

                user_free(&snac);
            }
        }
    }
    else
    if (xs_startswith(resource, "acct:")) {
        /* it's an account name */
        xs *an = xs_replace(resource, "acct:", "");
        xs *l = NULL;

        /* strip a possible leading @ */
        if (xs_startswith(an, "@"))
            an = xs_crop(an, 1, 0);

        l = xs_split_n(an, "@", 1);

        if (xs_list_len(l) == 2) {
            char *uid  = xs_list_get(l, 0);
            char *host = xs_list_get(l, 1);

            if (strcmp(host, xs_dict_get(srv_config, "host")) == 0)
                found = user_open(&snac, uid);
        }
    }

    if (found) {
        /* build the object */
        xs *acct;
        xs *aaj   = xs_dict_new();
        xs *links = xs_list_new();
        xs *obj   = xs_dict_new();
        d_char *j;

        acct = xs_fmt("acct:%s@%s",
            xs_dict_get(snac.config, "uid"), xs_dict_get(srv_config, "host"));

        aaj = xs_dict_append(aaj, "rel",  "self");
        aaj = xs_dict_append(aaj, "type", "application/activity+json");
        aaj = xs_dict_append(aaj, "href", snac.actor);

        links = xs_list_append(links, aaj);

        obj = xs_dict_append(obj, "subject", acct);
        obj = xs_dict_append(obj, "links",   links);

        j = xs_json_dumps_pp(obj, 4);

        user_free(&snac);

        *status = 200;
        *body   = j;
        *ctype  = "application/json";
    }
}