commit 7e743e8

shtrophic  ·  2024-12-05 16:16:57 +0000 UTC
parents cbd89bd, 221eeab
Merge remote-tracking branch 'upstream/master'
11 files changed,  +543, -76
M data.c
M html.c
M main.c
M snac.h
+16, -0
 1@@ -1,5 +1,21 @@
 2 # Release Notes
 3 
 4+## UNRELEASED
 5+
 6+As many users have asked for it, there is now an option to make the number of followed and following accounts public (still disabled by default). These are only the numbers; the lists themselves are never published.
 7+
 8+Some fixes to blocked instances code (posts from them were sometimes shown).
 9+
10+## 2.65
11+
12+Added a new user option to disable automatic follow confirmations (follow requests must be manually approved from the people page).
13+
14+The search box also searches for accounts (via webfinger).
15+
16+New command-line action `import_list`, to import a Mastodon list in CSV format (so that [Mastodon Follow Packs](https://mastodonmigration.wordpress.com/?p=995) can be directly used).
17+
18+New command-line action `import_block_list`, to import a Mastodon list of accounts to be blocked in CSV format.
19+
20 ## 2.64
21 
22 Some tweaks for better integration with https://bsky.brid.gy (the BlueSky bridge by brid.gy).
+80, -21
  1@@ -1038,15 +1038,14 @@ xs_dict *msg_base(snac *snac, const char *type, const char *id,
  2 }
  3 
  4 
  5-xs_dict *msg_collection(snac *snac, const char *id)
  6+xs_dict *msg_collection(snac *snac, const char *id, int items)
  7 /* creates an empty OrderedCollection message */
  8 {
  9     xs_dict *msg = msg_base(snac, "OrderedCollection", id, NULL, NULL, NULL);
 10-    xs *ol = xs_list_new();
 11+    xs *n = xs_number_new(items);
 12 
 13     msg = xs_dict_append(msg, "attributedTo", snac->actor);
 14-    msg = xs_dict_append(msg, "orderedItems", ol);
 15-    msg = xs_dict_append(msg, "totalItems",   xs_stock(0));
 16+    msg = xs_dict_append(msg, "totalItems",   n);
 17 
 18     return msg;
 19 }
 20@@ -1218,7 +1217,30 @@ xs_dict *msg_actor(snac *snac)
 21     }
 22 
 23     /* add the metadata as attachments of PropertyValue */
 24-    const xs_dict *metadata = xs_dict_get(snac->config, "metadata");
 25+    xs *metadata = NULL;
 26+    const xs_dict *md = xs_dict_get(snac->config, "metadata");
 27+
 28+    if (xs_type(md) == XSTYPE_DICT)
 29+        metadata = xs_dup(md);
 30+    else
 31+    if (xs_type(md) == XSTYPE_STRING) {
 32+        metadata = xs_dict_new();
 33+        xs *l = xs_split(md, "\n");
 34+        const char *ll;
 35+
 36+        xs_list_foreach(l, ll) {
 37+            xs *kv = xs_split_n(ll, "=", 1);
 38+            const char *k = xs_list_get(kv, 0);
 39+            const char *v = xs_list_get(kv, 1);
 40+
 41+            if (k && v) {
 42+                xs *kk = xs_strip_i(xs_dup(k));
 43+                xs *vv = xs_strip_i(xs_dup(v));
 44+                metadata = xs_dict_set(metadata, kk, vv);
 45+            }
 46+        }
 47+    }
 48+
 49     if (xs_type(metadata) == XSTYPE_DICT) {
 50         xs *attach = xs_list_new();
 51         const xs_str *k;
 52@@ -1264,6 +1286,10 @@ xs_dict *msg_actor(snac *snac)
 53         msg = xs_dict_set(msg, "alsoKnownAs", loaka);
 54     }
 55 
 56+    const xs_val *manually = xs_dict_get(snac->config, "approve_followers");
 57+    msg = xs_dict_set(msg, "manuallyApprovesFollowers",
 58+        xs_stock(xs_is_true(manually) ? XSTYPE_TRUE : XSTYPE_FALSE));
 59+
 60     return msg;
 61 }
 62 
 63@@ -1900,22 +1926,31 @@ int process_input_message(snac *snac, const xs_dict *msg, const xs_dict *req)
 64                 object_add(actor, actor_obj);
 65             }
 66 
 67-            xs *f_msg = xs_dup(msg);
 68-            xs *reply = msg_accept(snac, f_msg, actor);
 69+            if (xs_is_true(xs_dict_get(snac->config, "approve_followers"))) {
 70+                pending_add(snac, actor, msg);
 71 
 72-            post_message(snac, actor, reply);
 73-
 74-            if (xs_is_null(xs_dict_get(f_msg, "published"))) {
 75-                /* add a date if it doesn't include one (Mastodon) */
 76-                xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
 77-                f_msg = xs_dict_set(f_msg, "published", date);
 78+                snac_log(snac, xs_fmt("new pending follower approval %s", actor));
 79             }
 80+            else {
 81+                /* automatic following */
 82+                xs *f_msg = xs_dup(msg);
 83+                xs *reply = msg_accept(snac, f_msg, actor);
 84+
 85+                post_message(snac, actor, reply);
 86+
 87+                if (xs_is_null(xs_dict_get(f_msg, "published"))) {
 88+                    /* add a date if it doesn't include one (Mastodon) */
 89+                    xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
 90+                    f_msg = xs_dict_set(f_msg, "published", date);
 91+                }
 92+
 93+                timeline_add(snac, id, f_msg);
 94 
 95-            timeline_add(snac, id, f_msg);
 96+                follower_add(snac, actor);
 97 
 98-            follower_add(snac, actor);
 99+                snac_log(snac, xs_fmt("new follower %s", actor));
100+            }
101 
102-            snac_log(snac, xs_fmt("new follower %s", actor));
103             do_notify = 1;
104         }
105         else
106@@ -1936,6 +1971,11 @@ int process_input_message(snac *snac, const xs_dict *msg, const xs_dict *req)
107                     snac_log(snac, xs_fmt("no longer following us %s", actor));
108                     do_notify = 1;
109                 }
110+                else
111+                if (pending_check(snac, actor)) {
112+                    pending_del(snac, actor);
113+                    snac_log(snac, xs_fmt("cancelled pending follow from %s", actor));
114+                }
115                 else
116                     snac_log(snac, xs_fmt("error deleting follower %s", actor));
117             }
118@@ -2796,6 +2836,8 @@ int activitypub_get_handler(const xs_dict *req, const char *q_path,
119 
120     *ctype  = "application/activity+json";
121 
122+    int show_contact_metrics = xs_is_true(xs_dict_get(snac.config, "show_contact_metrics"));
123+
124     if (p_path == NULL) {
125         /* if there was no component after the user, it's an actor request */
126         msg = msg_actor(&snac);
127@@ -2809,7 +2851,6 @@ int activitypub_get_handler(const xs_dict *req, const char *q_path,
128     if (strcmp(p_path, "outbox") == 0 || strcmp(p_path, "featured") == 0) {
129         xs *id = xs_fmt("%s/%s", snac.actor, p_path);
130         xs *list = xs_list_new();
131-        msg = msg_collection(&snac, id);
132         const char *v;
133         int tc = 0;
134 
135@@ -2831,14 +2872,32 @@ int activitypub_get_handler(const xs_dict *req, const char *q_path,
136         }
137 
138         /* replace the 'orderedItems' with the latest posts */
139-        xs *items = xs_number_new(xs_list_len(list));
140+        msg = msg_collection(&snac, id, xs_list_len(list));
141         msg = xs_dict_set(msg, "orderedItems", list);
142-        msg = xs_dict_set(msg, "totalItems",   items);
143     }
144     else
145-    if (strcmp(p_path, "followers") == 0 || strcmp(p_path, "following") == 0) {
146+    if (strcmp(p_path, "followers") == 0) {
147+        int total = 0;
148+
149+        if (show_contact_metrics) {
150+            xs *l = follower_list(&snac);
151+            total = xs_list_len(l);
152+        }
153+
154+        xs *id = xs_fmt("%s/%s", snac.actor, p_path);
155+        msg = msg_collection(&snac, id, total);
156+    }
157+    else
158+    if (strcmp(p_path, "following") == 0) {
159+        int total = 0;
160+
161+        if (show_contact_metrics) {
162+            xs *l = following_list(&snac);
163+            total = xs_list_len(l);
164+        }
165+
166         xs *id = xs_fmt("%s/%s", snac.actor, p_path);
167-        msg = msg_collection(&snac, id);
168+        msg = msg_collection(&snac, id, total);
169     }
170     else
171     if (xs_startswith(p_path, "p/")) {
M data.c
+119, -0
  1@@ -299,6 +299,35 @@ int user_persist(snac *snac, int publish)
  2     xs *bfn = xs_fmt("%s.bak", fn);
  3     FILE *f;
  4 
  5+    if (publish) {
  6+        /* check if any of the relevant fields have really changed */
  7+        if ((f = fopen(fn, "r")) != NULL) {
  8+            xs *old = xs_json_load(f);
  9+            fclose(f);
 10+
 11+            if (old != NULL) {
 12+                int nw = 0;
 13+                const char *fields[] = { "header", "avatar", "name", "bio", "metadata", NULL };
 14+
 15+                for (int n = 0; fields[n]; n++) {
 16+                    const char *of = xs_dict_get(old, fields[n]);
 17+                    const char *nf = xs_dict_get(snac->config, fields[n]);
 18+
 19+                    if (of == NULL && nf == NULL)
 20+                        continue;
 21+
 22+                    if (xs_type(of) != XSTYPE_STRING || xs_type(nf) != XSTYPE_STRING || strcmp(of, nf)) {
 23+                        nw = 1;
 24+                        break;
 25+                    }
 26+                }
 27+
 28+                if (!nw)
 29+                    publish = 0;
 30+            }
 31+        }
 32+    }
 33+
 34     rename(fn, bfn);
 35 
 36     if ((f = fopen(fn, "w")) != NULL) {
 37@@ -1139,6 +1168,96 @@ xs_list *follower_list(snac *snac)
 38 }
 39 
 40 
 41+/** pending followers **/
 42+
 43+int pending_add(snac *user, const char *actor, const xs_dict *msg)
 44+/* stores the follow message for later confirmation */
 45+{
 46+    xs *dir = xs_fmt("%s/pending", user->basedir);
 47+    xs *md5 = xs_md5_hex(actor, strlen(actor));
 48+    xs *fn  = xs_fmt("%s/%s.json", dir, md5);
 49+    FILE *f;
 50+
 51+    mkdirx(dir);
 52+
 53+    if ((f = fopen(fn, "w")) == NULL)
 54+        return -1;
 55+
 56+    xs_json_dump(msg, 4, f);
 57+    fclose(f);
 58+
 59+    return 0;
 60+}
 61+
 62+
 63+int pending_check(snac *user, const char *actor)
 64+/* checks if there is a pending follow confirmation for the actor */
 65+{
 66+    xs *md5 = xs_md5_hex(actor, strlen(actor));
 67+    xs *fn = xs_fmt("%s/pending/%s.json", user->basedir, md5);
 68+
 69+    return mtime(fn) != 0;
 70+}
 71+
 72+
 73+xs_dict *pending_get(snac *user, const char *actor)
 74+/* returns the pending follow confirmation for the actor */
 75+{
 76+    xs *md5 = xs_md5_hex(actor, strlen(actor));
 77+    xs *fn = xs_fmt("%s/pending/%s.json", user->basedir, md5);
 78+    xs_dict *msg = NULL;
 79+    FILE *f;
 80+
 81+    if ((f = fopen(fn, "r")) != NULL) {
 82+        msg = xs_json_load(f);
 83+        fclose(f);
 84+    }
 85+
 86+    return msg;
 87+}
 88+
 89+
 90+void pending_del(snac *user, const char *actor)
 91+/* deletes a pending follow confirmation for the actor */
 92+{
 93+    xs *md5 = xs_md5_hex(actor, strlen(actor));
 94+    xs *fn = xs_fmt("%s/pending/%s.json", user->basedir, md5);
 95+
 96+    unlink(fn);
 97+}
 98+
 99+
100+xs_list *pending_list(snac *user)
101+/* returns a list of pending follow confirmations */
102+{
103+    xs *spec = xs_fmt("%s/pending/""*.json", user->basedir);
104+    xs *l = xs_glob(spec, 0, 0);
105+    xs_list *r = xs_list_new();
106+    const char *v;
107+
108+    xs_list_foreach(l, v) {
109+        FILE *f;
110+        xs *msg = NULL;
111+
112+        if ((f = fopen(v, "r")) == NULL)
113+            continue;
114+
115+        msg = xs_json_load(f);
116+        fclose(f);
117+
118+        if (msg == NULL)
119+            continue;
120+
121+        const char *actor = xs_dict_get(msg, "actor");
122+
123+        if (xs_type(actor) == XSTYPE_STRING)
124+            r = xs_list_append(r, actor);
125+    }
126+
127+    return r;
128+}
129+
130+
131 /** timeline **/
132 
133 double timeline_mtime(snac *snac)
+35, -1
 1@@ -129,6 +129,28 @@ Just what it says in the tin. This is to mitigate spammers
 2 coming from Fediverse instances with lax / open registration
 3 processes. Please take note that this also avoids possibly
 4 legitimate people trying to contact you.
 5+.It This account is a bot
 6+Set this checkbox if this account behaves like a bot (i.e.
 7+posts are automatically generated).
 8+.It Auto-boost all mentions to this account
 9+If this toggle is set, all mentions to this account are boosted
10+to all followers. This can be used to create groups.
11+.It This account is private
12+If this toggle is set, posts are not published via the public
13+web interface, only via the ActivityPub protocol.
14+.It Collapse top threads by default
15+If this toggle is set, the private timeline will always show
16+conversations collapsed by default. This allows easier navigation
17+through long threads.
18+.It Follow requests must be approved
19+If this toggle is set, follow requests are not automatically
20+accepted, but notified and stored for later review. Pending
21+follow requests will be shown in the people page to be
22+approved or discarded.
23+.It Publish follower and following metrics
24+If this toggle is set, the number of followers and following
25+accounts are made public (this is only the number; the specific
26+lists of accounts are never published).
27 .It Password
28 Write the same string in these two fields to change your
29 password. Don't write anything if you don't want to do this.
30@@ -262,6 +284,13 @@ section 'Migrating from snac to Mastodon').
31 Starts a migration from this account to the one set as an alias (see
32 .Xr snac 8 ,
33 section 'Migrating from snac to Mastodon').
34+.It Cm import_csv Ar basedir Ar uid
35+Imports CSV data files from a Mastodon export. This command expects the
36+following files to be in the current directory:
37+.Pa bookmarks.csv ,
38+.Pa blocked_accounts.csv ,
39+.Pa lists.csv , and
40+.Pa following_accounts.csv .
41 .It Cm state Ar basedir
42 Dumps the current state of the server and its threads. For example:
43 .Bd -literal -offset indent
44@@ -284,6 +313,11 @@ in-memory job queue. The thread state can be: waiting (idle waiting
45 for a job to be assigned), input or output (processing I/O packets)
46 or stopped (not running, only to be seen while starting or stopping
47 the server).
48+.It Cm import_list Ar basedir Ar uid Ar file
49+Imports a Mastodon list in CSV format. This option can be used to
50+import "Mastodon Follow Packs".
51+.It Cm import_block_list Ar basedir Ar uid Ar file
52+Imports a Mastodon list of accounts to be blocked in CSV format.
53 .El
54 .Ss Migrating an account to/from Mastodon
55 See 
56@@ -349,4 +383,4 @@ See the LICENSE file for details.
57 .Sh CAVEATS
58 Use the Fediverse sparingly. Don't fear the MUTE button.
59 .Sh BUGS
60-Probably plenty. Some issues may be even documented in the TODO.md file.
61+Probably many. Some issues may be even documented in the TODO.md file.
M html.c
+190, -40
  1@@ -770,7 +770,7 @@ static xs_html *html_user_body(snac *user, int read_only)
  2                     xs_html_sctag("input",
  3                         xs_html_attr("type", "text"),
  4                         xs_html_attr("name", "q"),
  5-                        xs_html_attr("title", L("Search posts by content (regular expression) or #tag")),
  6+                        xs_html_attr("title", L("Search posts by content (regular expression), @user@host accounts, or #tag")),
  7                         xs_html_attr("placeholder", L("Content search")))));
  8     }
  9 
 10@@ -829,21 +829,45 @@ static xs_html *html_user_body(snac *user, int read_only)
 11     }
 12 
 13     if (read_only) {
 14-        xs *es1  = encode_html(xs_dict_get(user->config, "bio"));
 15         xs *tags = xs_list_new();
 16-        xs *bio1 = not_really_markdown(es1, NULL, &tags);
 17+        xs *bio1 = not_really_markdown(xs_dict_get(user->config, "bio"), NULL, &tags);
 18         xs *bio2 = process_tags(user, bio1, &tags);
 19+        xs *bio3 = sanitize(bio2);
 20 
 21-        bio2 = replace_shortnames(bio2, tags, 2, proxy);
 22+        bio3 = replace_shortnames(bio3, tags, 2, proxy);
 23 
 24         xs_html *top_user_bio = xs_html_tag("div",
 25             xs_html_attr("class", "p-note snac-top-user-bio"),
 26-            xs_html_raw(bio2)); /* already sanitized */
 27+            xs_html_raw(bio3)); /* already sanitized */
 28 
 29         xs_html_add(top_user,
 30             top_user_bio);
 31 
 32-        const xs_dict *metadata = xs_dict_get(user->config, "metadata");
 33+        xs *metadata = NULL;
 34+        const xs_dict *md = xs_dict_get(user->config, "metadata");
 35+
 36+        if (xs_type(md) == XSTYPE_DICT)
 37+            metadata = xs_dup(md);
 38+        else
 39+        if (xs_type(md) == XSTYPE_STRING) {
 40+            /* convert to dict for easier iteration */
 41+            metadata = xs_dict_new();
 42+            xs *l = xs_split(md, "\n");
 43+            const char *ll;
 44+
 45+            xs_list_foreach(l, ll) {
 46+                xs *kv = xs_split_n(ll, "=", 1);
 47+                const char *k = xs_list_get(kv, 0);
 48+                const char *v = xs_list_get(kv, 1);
 49+
 50+                if (k && v) {
 51+                    xs *kk = xs_strip_i(xs_dup(k));
 52+                    xs *vv = xs_strip_i(xs_dup(v));
 53+                    metadata = xs_dict_set(metadata, kk, vv);
 54+                }
 55+            }
 56+        }
 57+
 58         if (xs_type(metadata) == XSTYPE_DICT) {
 59             const xs_str *k;
 60             const xs_str *v;
 61@@ -914,6 +938,18 @@ static xs_html *html_user_body(snac *user, int read_only)
 62             xs_html_add(top_user,
 63                 snac_metadata);
 64         }
 65+
 66+        if (xs_is_true(xs_dict_get(user->config, "show_contact_metrics"))) {
 67+            xs *fwers = follower_list(user);
 68+            xs *fwing = following_list(user);
 69+
 70+            xs *s1 = xs_fmt(L("%d following %d followers"),
 71+                xs_list_len(fwing), xs_list_len(fwers));
 72+
 73+            xs_html_add(top_user,
 74+                xs_html_tag("p",
 75+                    xs_html_text(s1)));
 76+        }
 77     }
 78 
 79     xs_html_add(body,
 80@@ -1025,20 +1061,31 @@ xs_html *html_top_controls(snac *snac)
 81     const xs_val *a_private = xs_dict_get(snac->config, "private");
 82     const xs_val *auto_boost = xs_dict_get(snac->config, "auto_boost");
 83     const xs_val *coll_thrds = xs_dict_get(snac->config, "collapse_threads");
 84+    const xs_val *pending    = xs_dict_get(snac->config, "approve_followers");
 85+    const xs_val *show_foll  = xs_dict_get(snac->config, "show_contact_metrics");
 86 
 87-    xs *metadata = xs_str_new(NULL);
 88+    xs *metadata = NULL;
 89     const xs_dict *md = xs_dict_get(snac->config, "metadata");
 90-    const xs_str *k;
 91-    const xs_str *v;
 92 
 93-    int c = 0;
 94-    while (xs_dict_next(md, &k, &v, &c)) {
 95-        xs *kp = xs_fmt("%s=%s", k, v);
 96+    if (xs_type(md) == XSTYPE_DICT) {
 97+        const xs_str *k;
 98+        const xs_str *v;
 99 
100-        if (*metadata)
101-            metadata = xs_str_cat(metadata, "\n");
102-        metadata = xs_str_cat(metadata, kp);
103+        metadata = xs_str_new(NULL);
104+
105+        xs_dict_foreach(md, k, v) {
106+            xs *kp = xs_fmt("%s=%s", k, v);
107+
108+            if (*metadata)
109+                metadata = xs_str_cat(metadata, "\n");
110+            metadata = xs_str_cat(metadata, kp);
111+        }
112     }
113+    else
114+    if (xs_type(md) == XSTYPE_STRING)
115+        metadata = xs_dup(md);
116+    else
117+        metadata = xs_str_new(NULL);
118 
119     xs *user_setup_action = xs_fmt("%s/admin/user-setup", snac->actor);
120 
121@@ -1187,6 +1234,24 @@ xs_html *html_top_controls(snac *snac)
122                     xs_html_tag("label",
123                         xs_html_attr("for", "collapse_threads"),
124                         xs_html_text(L("Collapse top threads by default")))),
125+                xs_html_tag("p",
126+                    xs_html_sctag("input",
127+                        xs_html_attr("type", "checkbox"),
128+                        xs_html_attr("name", "approve_followers"),
129+                        xs_html_attr("id",   "approve_followers"),
130+                        xs_html_attr(xs_is_true(pending) ? "checked" : "", NULL)),
131+                    xs_html_tag("label",
132+                        xs_html_attr("for", "approve_followers"),
133+                        xs_html_text(L("Follow requests must be approved")))),
134+                xs_html_tag("p",
135+                    xs_html_sctag("input",
136+                        xs_html_attr("type", "checkbox"),
137+                        xs_html_attr("name", "show_contact_metrics"),
138+                        xs_html_attr("id",   "show_contact_metrics"),
139+                        xs_html_attr(xs_is_true(show_foll) ? "checked" : "", NULL)),
140+                    xs_html_tag("label",
141+                        xs_html_attr("for", "show_contact_metrics"),
142+                        xs_html_text(L("Publish follower and following metrics")))),
143                 xs_html_tag("p",
144                     xs_html_text(L("Profile metadata (key=value pairs in each line):")),
145                     xs_html_sctag("br", NULL),
146@@ -1481,6 +1546,9 @@ xs_html *html_entry(snac *user, xs_dict *msg, int read_only,
147     if ((read_only || !user) && !is_msg_public(msg))
148         return NULL;
149 
150+    if (id && is_instance_blocked(id))
151+        return NULL;
152+
153     if (user && level == 0 && xs_is_true(xs_dict_get(user->config, "collapse_threads")))
154         collapse_threads = 1;
155 
156@@ -2437,10 +2505,9 @@ xs_html *html_people_list(snac *snac, xs_list *list, char *header, char *t, cons
157                 xs_html_tag("summary",
158                     xs_html_text("..."))));
159 
160-    xs_list *p = list;
161     const char *actor_id;
162 
163-    while (xs_list_iter(&p, &actor_id)) {
164+    xs_list_foreach(list, actor_id) {
165         xs *md5 = xs_md5_hex(actor_id, strlen(actor_id));
166         xs *actor = NULL;
167 
168@@ -2509,6 +2576,15 @@ xs_html *html_people_list(snac *snac, xs_list *list, char *header, char *t, cons
169                         html_button("limit", L("Limit"),
170                                 L("Block announces (boosts) from this user")));
171             }
172+            else
173+            if (pending_check(snac, actor_id)) {
174+                xs_html_add(form,
175+                    html_button("approve", L("Approve"),
176+                                L("Approve this follow request")));
177+
178+                xs_html_add(form,
179+                    html_button("discard", L("Discard"), L("Discard this follow request")));
180+            }
181             else {
182                 xs_html_add(form,
183                     html_button("follow", L("Follow"),
184@@ -2563,13 +2639,23 @@ xs_str *html_people(snac *user)
185     xs *wing = following_list(user);
186     xs *wers = follower_list(user);
187 
188+    xs_html *lists = xs_html_tag("div",
189+        xs_html_attr("class", "snac-posts"));
190+
191+    if (xs_is_true(xs_dict_get(user->config, "approve_followers"))) {
192+        xs *pending = pending_list(user);
193+        xs_html_add(lists,
194+            html_people_list(user, pending, L("Pending follow confirmations"), "p", proxy));
195+    }
196+
197+    xs_html_add(lists,
198+        html_people_list(user, wing, L("People you follow"), "i", proxy),
199+        html_people_list(user, wers, L("People that follow you"), "e", proxy));
200+
201     xs_html *html = xs_html_tag("html",
202         html_user_head(user, NULL, NULL),
203         xs_html_add(html_user_body(user, 0),
204-            xs_html_tag("div",
205-                xs_html_attr("class", "snac-posts"),
206-                html_people_list(user, wing, L("People you follow"), "i", proxy),
207-                html_people_list(user, wers, L("People that follow you"), "e", proxy)),
208+            lists,
209             html_footer()));
210 
211     return xs_html_render_s(html, "<!DOCTYPE html>\n");
212@@ -2661,6 +2747,9 @@ xs_str *html_notifications(snac *user, int skip, int show)
213                 label = wrk;
214             }
215         }
216+        else
217+        if (strcmp(type, "Follow") == 0 && pending_check(user, actor_id))
218+            label = L("Follow Request");
219 
220         xs *s_date = xs_crop_i(xs_dup(date), 0, 10);
221 
222@@ -2909,6 +2998,48 @@ int html_get_handler(const xs_dict *req, const char *q_path,
223             const char *q = xs_dict_get(q_vars, "q");
224 
225             if (q && *q) {
226+                if (xs_regex_match(q, "^@?[a-zA-Z0-9_]+@[a-zA-Z0-9-]+\\.")) {
227+                    /** search account **/
228+                    xs *actor = NULL;
229+                    xs *acct = NULL;
230+                    xs *l = xs_list_new();
231+                    xs_html *page = NULL;
232+
233+                    if (valid_status(webfinger_request(q, &actor, &acct))) {
234+                        xs *actor_obj = NULL;
235+
236+                        if (valid_status(actor_request(&snac, actor, &actor_obj))) {
237+                            actor_add(actor, actor_obj);
238+
239+                            /* create a people list with only one element */
240+                            l = xs_list_append(xs_list_new(), actor);
241+
242+                            xs *title = xs_fmt(L("Search results for account %s"), q);
243+
244+                            page = html_people_list(&snac, l, title, "wf", NULL);
245+                        }
246+                    }
247+
248+                    if (page == NULL) {
249+                        xs *title = xs_fmt(L("Account %s not found"), q);
250+
251+                        page = xs_html_tag("div",
252+                            xs_html_tag("h2",
253+                                xs_html_attr("class", "snac-header"),
254+                                xs_html_text(title)));
255+                    }
256+
257+                    xs_html *html = xs_html_tag("html",
258+                        html_user_head(&snac, NULL, NULL),
259+                        xs_html_add(html_user_body(&snac, 0),
260+                        page,
261+                        html_footer()));
262+
263+                    *body = xs_html_render_s(html, "<!DOCTYPE html>\n");
264+                    *b_size = strlen(*body);
265+                    status = HTTP_STATUS_OK;
266+                }
267+                else
268                 if (*q == '#') {
269                     /** search by tag **/
270                     xs *tl = tag_search(q, skip, show + 1);
271@@ -3646,6 +3777,34 @@ int html_post_handler(const xs_dict *req, const char *q_path,
272             unbookmark(&snac, id);
273             timeline_touch(&snac);
274         }
275+        else
276+        if (strcmp(action, L("Approve")) == 0) { /** **/
277+            xs *fwreq = pending_get(&snac, actor);
278+
279+            if (fwreq != NULL) {
280+                xs *reply = msg_accept(&snac, fwreq, actor);
281+
282+                enqueue_message(&snac, reply);
283+
284+                if (xs_is_null(xs_dict_get(fwreq, "published"))) {
285+                    /* add a date if it doesn't include one (Mastodon) */
286+                    xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
287+                    fwreq = xs_dict_set(fwreq, "published", date);
288+                }
289+
290+                timeline_add(&snac, xs_dict_get(fwreq, "id"), fwreq);
291+
292+                follower_add(&snac, actor);
293+
294+                pending_del(&snac, actor);
295+
296+                snac_log(&snac, xs_fmt("new follower %s", actor));
297+            }
298+        }
299+        else
300+        if (strcmp(action, L("Discard")) == 0) { /** **/
301+            pending_del(&snac, actor);
302+        }
303         else
304             status = HTTP_STATUS_NOT_FOUND;
305 
306@@ -3705,26 +3864,17 @@ int html_post_handler(const xs_dict *req, const char *q_path,
307             snac.config = xs_dict_set(snac.config, "collapse_threads", xs_stock(XSTYPE_TRUE));
308         else
309             snac.config = xs_dict_set(snac.config, "collapse_threads", xs_stock(XSTYPE_FALSE));
310+        if ((v = xs_dict_get(p_vars, "approve_followers")) != NULL && strcmp(v, "on") == 0)
311+            snac.config = xs_dict_set(snac.config, "approve_followers", xs_stock(XSTYPE_TRUE));
312+        else
313+            snac.config = xs_dict_set(snac.config, "approve_followers", xs_stock(XSTYPE_FALSE));
314+        if ((v = xs_dict_get(p_vars, "show_contact_metrics")) != NULL && strcmp(v, "on") == 0)
315+            snac.config = xs_dict_set(snac.config, "show_contact_metrics", xs_stock(XSTYPE_TRUE));
316+        else
317+            snac.config = xs_dict_set(snac.config, "show_contact_metrics", xs_stock(XSTYPE_FALSE));
318 
319-        if ((v = xs_dict_get(p_vars, "metadata")) != NULL) {
320-            /* split the metadata and store it as a dict */
321-            xs_dict *md = xs_dict_new();
322-            xs *l = xs_split(v, "\n");
323-            xs_list *p = l;
324-            const xs_str *kp;
325-
326-            while (xs_list_iter(&p, &kp)) {
327-                xs *kpl = xs_split_n(kp, "=", 1);
328-                if (xs_list_len(kpl) == 2) {
329-                    xs *k2 = xs_strip_i(xs_dup(xs_list_get(kpl, 0)));
330-                    xs *v2 = xs_strip_i(xs_dup(xs_list_get(kpl, 1)));
331-
332-                    md = xs_dict_set(md, k2, v2);
333-                }
334-            }
335-
336-            snac.config = xs_dict_set(snac.config, "metadata", md);
337-        }
338+        if ((v = xs_dict_get(p_vars, "metadata")) != NULL)
339+            snac.config = xs_dict_set(snac.config, "metadata", v);
340 
341         /* uploads */
342         const char *uploads[] = { "avatar", "header", NULL };
M main.c
+15, -1
 1@@ -51,7 +51,9 @@ int usage(void)
 2     printf("export_csv {basedir} {uid}           Exports data as CSV files into current directory\n");
 3     printf("alias {basedir} {uid} {account}      Sets account (@user@host or actor url) as an alias\n");
 4     printf("migrate {basedir} {uid}              Migrates to the account defined as the alias\n");
 5-    printf("import_csv {basedir} {uid}           Imports data from CSV files into current directory\n");
 6+    printf("import_csv {basedir} {uid}           Imports data from CSV files in the current directory\n");
 7+    printf("import_list {basedir} {uid} {file}   Imports a Mastodon CSV list file\n");
 8+    printf("import_block_list {basedir} {uid} {file} Imports a Mastodon CSV block list file\n");
 9 
10     return 1;
11 }
12@@ -589,6 +591,18 @@ int main(int argc, char *argv[])
13         return 0;
14     }
15 
16+    if (strcmp(cmd, "import_list") == 0) { /** **/
17+        import_list_csv(&snac, url);
18+
19+        return 0;
20+    }
21+
22+    if (strcmp(cmd, "import_block_list") == 0) { /** **/
23+        import_blocked_accounts_csv(&snac, url);
24+
25+        return 0;
26+    }
27+
28     if (strcmp(cmd, "note") == 0) { /** **/
29         xs *content = NULL;
30         xs *msg = NULL;
+25, -0
 1@@ -663,6 +663,17 @@ xs_dict *mastoapi_account(snac *logged, const xs_dict *actor)
 2         if (user_open(&user, prefu)) {
 3             val_links = user.links;
 4             metadata  = xs_dict_get_def(user.config, "metadata", xs_stock(XSTYPE_DICT));
 5+
 6+            /* does this user want to publish their contact metrics? */
 7+            if (xs_is_true(xs_dict_get(user.config, "show_contact_metrics"))) {
 8+                xs *fwing = following_list(&user);
 9+                xs *fwers = follower_list(&user);
10+                xs *ni = xs_number_new(xs_list_len(fwing));
11+                xs *ne = xs_number_new(xs_list_len(fwers));
12+
13+                acct = xs_dict_append(acct, "followers_count", ne);
14+                acct = xs_dict_append(acct, "following_count", ni);
15+            }
16         }
17     }
18 
19@@ -1275,6 +1286,17 @@ void credentials_get(char **body, char **ctype, int *status, snac snac)
20     acct = xs_dict_append(acct, "following_count", xs_stock(0));
21     acct = xs_dict_append(acct, "statuses_count", xs_stock(0));
22 
23+    /* does this user want to publish their contact metrics? */
24+    if (xs_is_true(xs_dict_get(snac.config, "show_contact_metrics"))) {
25+        xs *fwing = following_list(&snac);
26+        xs *fwers = follower_list(&snac);
27+        xs *ni = xs_number_new(xs_list_len(fwing));
28+        xs *ne = xs_number_new(xs_list_len(fwers));
29+
30+        acct = xs_dict_append(acct, "followers_count", ne);
31+        acct = xs_dict_append(acct, "following_count", ni);
32+    }
33+
34     *body = xs_json_dumps(acct, 4);
35     *ctype = "application/json";
36     *status = HTTP_STATUS_OK;
37@@ -1349,6 +1371,9 @@ xs_list *mastoapi_timeline(snac *user, const xs_dict *args, const char *index_fn
38             if (!xs_match(type, POSTLIKE_OBJECT_TYPE))
39                 continue;
40 
41+            if (id && is_instance_blocked(id))
42+                continue;
43+
44             const char *from = NULL;
45             if (strcmp(type, "Page") == 0)
46                 from = xs_dict_get(msg, "audience");
M snac.h
+12, -1
 1@@ -1,7 +1,7 @@
 2 /* snac - A simple, minimalistic ActivityPub instance */
 3 /* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
 4 
 5-#define VERSION "2.64"
 6+#define VERSION "2.66-dev"
 7 
 8 #define USER_AGENT "snac/" VERSION
 9 
10@@ -143,6 +143,12 @@ int follower_del(snac *snac, const char *actor);
11 int follower_check(snac *snac, const char *actor);
12 xs_list *follower_list(snac *snac);
13 
14+int pending_add(snac *user, const char *actor, const xs_dict *msg);
15+int pending_check(snac *user, const char *actor);
16+xs_dict *pending_get(snac *user, const char *actor);
17+void pending_del(snac *user, const char *actor);
18+xs_list *pending_list(snac *user);
19+
20 double timeline_mtime(snac *snac);
21 int timeline_touch(snac *snac);
22 int timeline_here(snac *snac, const char *md5);
23@@ -316,6 +322,7 @@ xs_dict *msg_update(snac *snac, const xs_dict *object);
24 xs_dict *msg_ping(snac *user, const char *rcpt);
25 xs_dict *msg_pong(snac *user, const char *rcpt, const char *object);
26 xs_dict *msg_move(snac *user, const char *new_account);
27+xs_dict *msg_accept(snac *snac, const xs_val *object, const char *to);
28 xs_dict *msg_question(snac *user, const char *content, xs_list *attach,
29                       const xs_list *opts, int multiple, int end_secs);
30 
31@@ -399,6 +406,10 @@ void verify_links(snac *user);
32 
33 void export_csv(snac *user);
34 int migrate_account(snac *user);
35+
36+void import_blocked_accounts_csv(snac *user, const char *fn);
37+void import_following_accounts_csv(snac *user, const char *fn);
38+void import_list_csv(snac *user, const char *fn);
39 void import_csv(snac *user);
40 
41 typedef enum {
+46, -7
 1@@ -670,20 +670,18 @@ void export_csv(snac *user)
 2 }
 3 
 4 
 5-void import_csv(snac *user)
 6-/* import CSV files from Mastodon */
 7+void import_blocked_accounts_csv(snac *user, const char *fn)
 8+/* imports a Mastodon CSV file of blocked accounts */
 9 {
10     FILE *f;
11-    const char *fn;
12 
13-    fn = "blocked_accounts.csv";
14     if ((f = fopen(fn, "r")) != NULL) {
15         snac_log(user, xs_fmt("Importing from %s...", fn));
16 
17         while (!feof(f)) {
18             xs *l = xs_strip_i(xs_readline(f));
19 
20-            if (*l) {
21+            if (*l && strchr(l, '@') != NULL) {
22                 xs *url = NULL;
23                 xs *uid = NULL;
24 
25@@ -704,8 +702,14 @@ void import_csv(snac *user)
26     }
27     else
28         snac_log(user, xs_fmt("Cannot open file %s", fn));
29+}
30+
31+
32+void import_following_accounts_csv(snac *user, const char *fn)
33+/* imports a Mastodon CSV file of accounts to follow */
34+{
35+    FILE *f;
36 
37-    fn = "following_accounts.csv";
38     if ((f = fopen(fn, "r")) != NULL) {
39         snac_log(user, xs_fmt("Importing from %s...", fn));
40 
41@@ -757,8 +761,14 @@ void import_csv(snac *user)
42     }
43     else
44         snac_log(user, xs_fmt("Cannot open file %s", fn));
45+}
46+
47+
48+void import_list_csv(snac *user, const char *fn)
49+/* imports a Mastodon CSV file list */
50+{
51+    FILE *f;
52 
53-    fn = "lists.csv";
54     if ((f = fopen(fn, "r")) != NULL) {
55         snac_log(user, xs_fmt("Importing from %s...", fn));
56 
57@@ -782,6 +792,21 @@ void import_csv(snac *user)
58 
59                         list_content(user, list_id, actor_md5, 1);
60                         snac_log(user, xs_fmt("Added %s to list %s", url, lname));
61+
62+                        if (!following_check(user, url)) {
63+                            xs *msg = msg_follow(user, url);
64+
65+                            if (msg == NULL) {
66+                                snac_log(user, xs_fmt("Cannot follow %s -- server down?", acct));
67+                                continue;
68+                            }
69+
70+                            following_add(user, url, msg);
71+
72+                            enqueue_output_by_actor(user, msg, url, 0);
73+
74+                            snac_log(user, xs_fmt("Following %s", url));
75+                        }
76                     }
77                     else
78                         snac_log(user, xs_fmt("Webfinger error while adding %s to list %s", acct, lname));
79@@ -793,6 +818,20 @@ void import_csv(snac *user)
80     }
81     else
82         snac_log(user, xs_fmt("Cannot open file %s", fn));
83+}
84+
85+
86+void import_csv(snac *user)
87+/* import CSV files from Mastodon */
88+{
89+    FILE *f;
90+    const char *fn;
91+
92+    import_blocked_accounts_csv(user, "blocked_accounts.csv");
93+
94+    import_following_accounts_csv(user, "following_accounts.csv");
95+
96+    import_list_csv(user, "lists.csv");
97 
98     fn = "bookmarks.csv";
99     if ((f = fopen(fn, "r")) != NULL) {
+4, -4
 1@@ -106,13 +106,13 @@ xs_dict *xs_multipart_form_data(const char *payload, int p_size, const char *hea
 2         if (xs_list_len(l1) != 2)
 3             return NULL;
 4 
 5-        boundary = xs_dup(xs_list_get(l1, 1));
 6+        xs *t_boundary = xs_dup(xs_list_get(l1, 1));
 7 
 8         /* Tokodon sends the boundary header with double quotes surrounded */
 9-        if (xs_between("\"", boundary, "\"") != 0)
10-            boundary = xs_strip_chars_i(boundary, "\"");
11+        if (xs_between("\"", t_boundary, "\"") != 0)
12+            t_boundary = xs_strip_chars_i(t_boundary, "\"");
13 
14-        boundary = xs_fmt("--%s", boundary);
15+        boundary = xs_fmt("--%s", t_boundary);
16     }
17 
18     bsz = strlen(boundary);
+1, -1
1@@ -1 +1 @@
2-/* ab0749f821f1c98d16cbec53201bdf2ba2a24a43 2024-11-20T17:02:42+01:00 */
3+/* 297f71e198be7819213e9122e1e78c3b963111bc 2024-11-24T18:48:42+01:00 */