From 049818a9042ce1da23e6fe3c41f776b1470e7cc4 Mon Sep 17 00:00:00 2001 From: default Date: Sat, 29 Apr 2023 07:36:18 +0200 Subject: Public posts are also added to an instance public timeline index. --- data.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/data.c b/data.c index c2d29c1..db42ece 100644 --- a/data.c +++ b/data.c @@ -969,8 +969,13 @@ void timeline_update_indexes(snac *snac, const char *id) if (valid_status(object_get(id, &msg))) { /* if its ours and is public, also store in public */ - if (is_msg_public(snac, msg)) + if (is_msg_public(snac, msg)) { object_user_cache_add(snac, id, "public"); + + /* also add it to the instance public timeline */ + xs *ipt = xs_fmt("%s/public.idx", srv_basedir); + index_add(ipt, id); + } } } } -- cgit v1.2.3 From 6b632e1ee9450f12c51442264e402753f8dfe325 Mon Sep 17 00:00:00 2001 From: default Date: Sun, 30 Apr 2023 06:01:08 +0200 Subject: Bumped version. --- snac.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snac.h b/snac.h index fe7adfb..a17d33f 100644 --- a/snac.h +++ b/snac.h @@ -1,7 +1,7 @@ /* snac - A simple, minimalistic ActivityPub instance */ /* copyright (c) 2022 - 2023 grunfink / MIT license */ -#define VERSION "2.29" +#define VERSION "2.30-dev" #define USER_AGENT "snac/" VERSION -- cgit v1.2.3 From ede4d6f2dc8f862337724054dcfeb31cbaa89bcc Mon Sep 17 00:00:00 2001 From: default Date: Sun, 30 Apr 2023 06:39:55 +0200 Subject: Some instance timeline work. --- data.c | 13 +++++++++++-- mastoapi.c | 52 +++++++++++++++++++++++----------------------------- snac.h | 8 ++++---- 3 files changed, 38 insertions(+), 35 deletions(-) diff --git a/data.c b/data.c index db42ece..60c4b26 100644 --- a/data.c +++ b/data.c @@ -1046,7 +1046,7 @@ xs_list *timeline_top_level(snac *snac, xs_list *list) } -d_char *timeline_simple_list(snac *snac, const char *idx_name, int skip, int show) +xs_list *timeline_simple_list(snac *snac, const char *idx_name, int skip, int show) /* returns a timeline (with all entries) */ { int c_max; @@ -1064,7 +1064,7 @@ d_char *timeline_simple_list(snac *snac, const char *idx_name, int skip, int sho } -d_char *timeline_list(snac *snac, const char *idx_name, int skip, int show) +xs_list *timeline_list(snac *snac, const char *idx_name, int skip, int show) /* returns a timeline (only top level entries) */ { xs *list = timeline_simple_list(snac, idx_name, skip, show); @@ -1073,6 +1073,15 @@ d_char *timeline_list(snac *snac, const char *idx_name, int skip, int show) } +xs_list *timeline_instance_list(int skip, int show) +/* returns the timeline for the full instance */ +{ + xs *idx = xs_fmt("%s/public.idx", srv_basedir); + + return index_list_desc(idx, skip, show); +} + + /** following **/ /* this needs special treatment and cannot use the object db as is, diff --git a/mastoapi.c b/mastoapi.c index 001c0bc..92acd41 100644 --- a/mastoapi.c +++ b/mastoapi.c @@ -1038,9 +1038,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, if (strcmp(cmd, "/v1/timelines/public") == 0) { /* the public timeline (public timelines for all users) */ - /* this is an ugly kludge: first users in the list get all the fame */ - - const char *limit_s = xs_dict_get(args, "limit"); + const char *limit_s = xs_dict_get(args, "limit"); int limit = 0; int cnt = 0; @@ -1050,44 +1048,40 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, if (limit == 0) limit = 20; - xs *out = xs_list_new(); - xs *users = user_list(); - xs_list *p = users; - xs_str *uid; - - while (xs_list_iter(&p, &uid) && cnt < limit) { - snac user; + xs *timeline = timeline_instance_list(0, limit); + xs *out = xs_list_new(); + xs_list *p = timeline; + xs_str *md5; - if (user_open(&user, uid)) { - xs *timeline = timeline_simple_list(&user, "public", 0, 4); - xs_list *p2 = timeline; - xs_str *v; + while (xs_list_iter(&p, &md5) && cnt < limit) { + xs *msg = NULL; - while (xs_list_iter(&p2, &v) && cnt < limit) { - xs *msg = NULL; + /* get the entry */ + if (!valid_status(object_get_by_md5(md5, &msg))) + continue; - /* get the entry */ - if (!valid_status(timeline_get_by_md5(&user, v, &msg))) - continue; + /* discard non-Notes */ + if (strcmp(xs_dict_get(msg, "type"), "Note") != 0) + continue; - /* discard non-Notes */ - if (strcmp(xs_dict_get(msg, "type"), "Note") != 0) - continue; + /* get the uid */ + xs *l = xs_split(xs_dict_get(msg, "attributedTo"), "/"); + const char *uid = xs_list_get(l, -1); - /* discard entries not by this user */ - if (!xs_startswith(xs_dict_get(msg, "id"), user.actor)) - continue; + if (!xs_is_null(uid)) { + snac user; + if (user_open(&user, uid)) { /* convert the Note into a Mastodon status */ xs *st = mastoapi_status(&user, msg); - if (st != NULL) { + if (st != NULL) out = xs_list_append(out, st); - cnt++; - } + + user_free(&user); } - user_free(&user); + cnt++; } } diff --git a/snac.h b/snac.h index a17d33f..5478933 100644 --- a/snac.h +++ b/snac.h @@ -105,14 +105,14 @@ int timeline_touch(snac *snac); int timeline_here(snac *snac, const char *md5); int timeline_get_by_md5(snac *snac, const char *md5, xs_dict **msg); int timeline_del(snac *snac, char *id); -d_char *timeline_simple_list(snac *snac, const char *idx_name, int skip, int show); -d_char *timeline_list(snac *snac, const char *idx_name, int skip, int show); +xs_list *timeline_simple_list(snac *snac, const char *idx_name, int skip, int show); +xs_list *timeline_list(snac *snac, const char *idx_name, int skip, int show); int timeline_add(snac *snac, char *id, char *o_msg); void timeline_admire(snac *snac, char *id, char *admirer, int like); xs_list *timeline_top_level(snac *snac, xs_list *list); - -d_char *local_list(snac *snac, int max); +xs_list *local_list(snac *snac, int max); +xs_list *timeline_instance_list(int skip, int show); int following_add(snac *snac, const char *actor, const xs_dict *msg); int following_del(snac *snac, const char *actor); -- cgit v1.2.3 From e31c4810a83c42c0298d4832fb4f2291dec0e84d Mon Sep 17 00:00:00 2001 From: default Date: Sun, 30 Apr 2023 06:49:51 +0200 Subject: actor_get() returns an actor message for local users. --- data.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/data.c b/data.c index 60c4b26..187d9b9 100644 --- a/data.c +++ b/data.c @@ -1287,27 +1287,43 @@ int is_hidden(snac *snac, const char *id) } -int actor_add(snac *snac, const char *actor, d_char *msg) +int actor_add(snac *snac, const char *actor, xs_dict *msg) /* adds an actor */ { return object_add_ow(actor, msg); } -int actor_get(snac *snac, const char *actor, d_char **data) +int actor_get(snac *snac1, const char *actor, xs_dict **data) /* returns an already downloaded actor */ { int status = 200; - d_char *d; + xs_dict *d; - if (strcmp(actor, snac->actor) == 0) { + if (strcmp(actor, snac1->actor) == 0) { /* this actor */ if (data) - *data = msg_actor(snac); + *data = msg_actor(snac1); return status; } + if (xs_startswith(actor, srv_baseurl)) { + /* it's a (possible) local user */ + xs *l = xs_split(actor, "/"); + const char *uid = xs_list_get(l, -1); + snac user; + + if (!xs_is_null(uid) && user_open(&user, uid)) { + if (data) + *data = msg_actor(&user); + + user_free(&user); + } + else + return 404; + } + /* read the object */ if (!valid_status(status = object_get(actor, &d))) return status; -- cgit v1.2.3 From cfa0df3ac5a8b62539a3d1830414ec3d81d24f5d Mon Sep 17 00:00:00 2001 From: default Date: Sun, 30 Apr 2023 07:00:49 +0200 Subject: The instance timeline now works. --- mastoapi.c | 29 +++++++++++------------------ snac.h | 4 ++-- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/mastoapi.c b/mastoapi.c index 92acd41..5c4e27f 100644 --- a/mastoapi.c +++ b/mastoapi.c @@ -1036,7 +1036,12 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, } else if (strcmp(cmd, "/v1/timelines/public") == 0) { - /* the public timeline (public timelines for all users) */ + /* the instance public timeline (public timelines for all users) */ + + /* NOTE: this api call needs no authorization; but, + I need a logged-in user in mastoapi_status() for + is_msg_public() and the liked/boosted flags, + so it will silently fail for pure public access */ const char *limit_s = xs_dict_get(args, "limit"); int limit = 0; @@ -1053,7 +1058,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, xs_list *p = timeline; xs_str *md5; - while (xs_list_iter(&p, &md5) && cnt < limit) { + while (logged_in && xs_list_iter(&p, &md5) && cnt < limit) { xs *msg = NULL; /* get the entry */ @@ -1064,23 +1069,11 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, if (strcmp(xs_dict_get(msg, "type"), "Note") != 0) continue; - /* get the uid */ - xs *l = xs_split(xs_dict_get(msg, "attributedTo"), "/"); - const char *uid = xs_list_get(l, -1); - - if (!xs_is_null(uid)) { - snac user; - - if (user_open(&user, uid)) { - /* convert the Note into a Mastodon status */ - xs *st = mastoapi_status(&user, msg); - - if (st != NULL) - out = xs_list_append(out, st); - - user_free(&user); - } + /* convert the Note into a Mastodon status */ + xs *st = mastoapi_status(&snac1, msg); + if (st != NULL) { + out = xs_list_append(out, st); cnt++; } } diff --git a/snac.h b/snac.h index 5478933..10e8c4c 100644 --- a/snac.h +++ b/snac.h @@ -127,8 +127,8 @@ int is_muted(snac *snac, const char *actor); void hide(snac *snac, const char *id); int is_hidden(snac *snac, const char *id); -int actor_add(snac *snac, const char *actor, d_char *msg); -int actor_get(snac *snac, const char *actor, d_char **data); +int actor_add(snac *snac, const char *actor, xs_dict *msg); +int actor_get(snac *snac, const char *actor, xs_dict **data); int static_get(snac *snac, const char *id, d_char **data, int *size); void static_put(snac *snac, const char *id, const char *data, int size); -- cgit v1.2.3 From 6917f0ab2684a126986dc4169b56bbdac985aa15 Mon Sep 17 00:00:00 2001 From: default Date: Sun, 30 Apr 2023 07:01:44 +0200 Subject: Updated RELEASE_NOTES. --- RELEASE_NOTES.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 8a153e9..033916f 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,5 +1,9 @@ # Release Notes +## 2.30 + +New Mastodon API features: the instance public timeline is now a real one. + ## 2.29 New Mastodon API features: account search, relationships (so the Follow/Unfollow buttons now appear for each account), follow and unfollow accounts, an instance-level timeline (very kludgy), custom emojis for accounts and statuses, many bug fixes (sadly, the Mastodon official app still does not work). -- cgit v1.2.3 From 21a2a01937211543216add23a8a4eaca20bf5f67 Mon Sep 17 00:00:00 2001 From: default Date: Sun, 30 Apr 2023 07:37:39 +0200 Subject: Return immediately from actor_get() if it's a local user. --- data.c | 1 + 1 file changed, 1 insertion(+) diff --git a/data.c b/data.c index 187d9b9..ee36103 100644 --- a/data.c +++ b/data.c @@ -1319,6 +1319,7 @@ int actor_get(snac *snac1, const char *actor, xs_dict **data) *data = msg_actor(&user); user_free(&user); + return 200; } else return 404; -- cgit v1.2.3 From a7d4513f776f831c36058dfb9b86e80438d16d26 Mon Sep 17 00:00:00 2001 From: default Date: Mon, 1 May 2023 07:35:26 +0200 Subject: In /api/v1/statuses, get the object from the storage instead of from the timeline. This was affecting clicking on posts from the instance timeline, that were not in the logged-in user timeline. --- mastoapi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mastoapi.c b/mastoapi.c index 5c4e27f..3d0a939 100644 --- a/mastoapi.c +++ b/mastoapi.c @@ -1313,7 +1313,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, /* skip the 'fake' part of the id */ id = MID_TO_MD5(id); - if (valid_status(timeline_get_by_md5(&snac1, id, &msg))) { + if (valid_status(object_get_by_md5(id, &msg))) { if (op == NULL) { if (!is_muted(&snac1, xs_dict_get(msg, "attributedTo"))) { /* return the status itself */ -- cgit v1.2.3 From 84902d8dcca0ff06e55698432cca69e542be685f Mon Sep 17 00:00:00 2001 From: default Date: Mon, 1 May 2023 08:32:48 +0200 Subject: Purge / gc the instance timeline index. --- data.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/data.c b/data.c index ee36103..50d88eb 100644 --- a/data.c +++ b/data.c @@ -2051,7 +2051,11 @@ void purge_server(void) xs *ib_dir = xs_fmt("%s/inbox", srv_basedir); _purge_dir(ib_dir, 7); - srv_debug(1, xs_fmt("purge: global (obj: %d, idx: %d)", cnt, icnt)); + /* purge the instance timeline */ + xs *itl_fn = xs_fmt("%s/public.idx", srv_basedir); + int itl_gc = index_gc(itl_fn); + + srv_debug(1, xs_fmt("purge: global (obj: %d, idx: %d, itl: %d)", cnt, icnt, itl_gc)); } -- cgit v1.2.3 From c21bbd5f7ca814d65a3759d429c1d673bee5081b Mon Sep 17 00:00:00 2001 From: default Date: Mon, 1 May 2023 17:02:44 +0200 Subject: New functions index_del_md5(), index_del() and object_unadmire(). --- data.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/data.c b/data.c index 50d88eb..e038f81 100644 --- a/data.c +++ b/data.c @@ -328,6 +328,51 @@ int index_add(const char *fn, const char *id) } +int index_del_md5(const char *fn, const char *md5) +/* deletes an md5 from an index */ +{ + int status = 404; + FILE *f; + + pthread_mutex_lock(&data_mutex); + + if ((f = fopen(fn, "r+")) != NULL) { + char line[256]; + + while (fgets(line, sizeof(line), f) != NULL) { + line[32] = '\0'; + + if (strcmp(line, md5) == 0) { + /* found! just rewind, overwrite it with garbage + and an eventual call to index_gc() will clean it + [yes: this breaks index_len()] */ + fseek(f, -33, SEEK_CUR); + fwrite("-", 1, 1, f); + status = 200; + + break; + } + } + + fclose(f); + } + else + status = 500; + + pthread_mutex_unlock(&data_mutex); + + return status; +} + + +int index_del(const char *fn, const char *id) +/* deletes an id from an index */ +{ + xs *md5 = xs_md5_hex(id, strlen(id)); + return index_del_md5(fn, md5); +} + + int index_gc(const char *fn) /* garbage-collects an index, deleting objects that are not here */ { @@ -772,6 +817,23 @@ int object_admire(const char *id, const char *actor, int like) } +int object_unadmire(const char *id, const char *actor, int like) +/* actor no longer likes or announces this object */ +{ + int status; + xs *fn = _object_fn(id); + + fn = xs_replace_i(fn, ".json", like ? "_l.idx" : "_a.idx"); + + status = index_del(fn, actor); + + srv_debug(1, + xs_fmt("object_unadmire (%s) %s %s %d", like ? "Like" : "Announce", actor, fn, status)); + + return status; +} + + int _object_user_cache(snac *snac, const char *id, const char *cachedir, int del) /* adds or deletes from a user cache */ { -- cgit v1.2.3 From 4595a3685992a8f31b86cca0ecf10e286dec52eb Mon Sep 17 00:00:00 2001 From: default Date: Mon, 1 May 2023 17:20:49 +0200 Subject: Partial support for mastoapi unfavourite / unreblog. --- data.c | 2 +- mastoapi.c | 9 +++++++-- snac.h | 1 + 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/data.c b/data.c index e038f81..67f5751 100644 --- a/data.c +++ b/data.c @@ -827,7 +827,7 @@ int object_unadmire(const char *id, const char *actor, int like) status = index_del(fn, actor); - srv_debug(1, + srv_debug(0, xs_fmt("object_unadmire (%s) %s %s %d", like ? "Like" : "Announce", actor, fn, status)); return status; diff --git a/mastoapi.c b/mastoapi.c index 3d0a939..72d9579 100644 --- a/mastoapi.c +++ b/mastoapi.c @@ -1670,7 +1670,11 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path, } else if (strcmp(op, "unfavourite") == 0) { - /* snac does not support Undo+Like */ + /* partial support: as the original Like message + is not stored anywhere here, it's not possible + to send an Undo + Like; the only thing done here + is to delete the actor from the list of likes */ + object_unadmire(id, snac.actor, 1); } else if (strcmp(op, "reblog") == 0) { @@ -1685,7 +1689,8 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path, } else if (strcmp(op, "unreblog") == 0) { - /* snac does not support Undo+Announce */ + /* partial support: see comment in 'unfavourite' */ + object_unadmire(id, snac.actor, 0); } else if (strcmp(op, "bookmark") == 0) { diff --git a/snac.h b/snac.h index 10e8c4c..a162618 100644 --- a/snac.h +++ b/snac.h @@ -83,6 +83,7 @@ int object_del_if_unref(const char *id); double object_ctime_by_md5(const char *md5); double object_ctime(const char *id); int object_admire(const char *id, const char *actor, int like); +int object_unadmire(const char *id, const char *actor, int like); int object_likes_len(const char *id); int object_announces_len(const char *id); -- cgit v1.2.3 From f1756a53d1c2f3308fefda669ae4d372daf88f8d Mon Sep 17 00:00:00 2001 From: default Date: Mon, 1 May 2023 17:21:56 +0200 Subject: Updated RELEASE_NOTES. --- RELEASE_NOTES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 033916f..78a3603 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -2,7 +2,7 @@ ## 2.30 -New Mastodon API features: the instance public timeline is now a real one. +New Mastodon API features: the instance public timeline is now a real one, unfavourite / unreblog is supported (somewhat). ## 2.29 -- cgit v1.2.3 From 29815a38736666e2226454d96ba444843853dca5 Mon Sep 17 00:00:00 2001 From: default Date: Tue, 2 May 2023 06:41:45 +0200 Subject: Backport from xs. --- xs.h | 12 ++-- xs_encdec.h | 221 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- xs_version.h | 2 +- 3 files changed, 220 insertions(+), 15 deletions(-) diff --git a/xs.h b/xs.h index fef91b7..c4c961b 100644 --- a/xs.h +++ b/xs.h @@ -65,8 +65,10 @@ xs_str *xs_str_new(const char *str); xs_str *xs_str_wrap_i(const char *prefix, xs_str *str, const char *suffix); #define xs_str_prepend_i(str, prefix) xs_str_wrap_i(prefix, str, NULL) #define xs_str_cat(str, suffix) xs_str_wrap_i(NULL, str, suffix) -xs_str *xs_replace_i(xs_str *str, const char *sfrom, const char *sto); -#define xs_replace(str, sfrom, sto) xs_replace_i(xs_dup(str), sfrom, sto) +xs_str *xs_replace_in(xs_str *str, const char *sfrom, const char *sto, int times); +#define xs_replace_i(str, sfrom, sto) xs_replace_in(str, sfrom, sto, XS_ALL) +#define xs_replace(str, sfrom, sto) xs_replace_in(xs_dup(str), sfrom, sto, XS_ALL) +#define xs_replace_n(str, sfrom, sto, times) xs_replace_in(xs_dup(str), sfrom, sto, times) xs_str *xs_fmt(const char *fmt, ...); int xs_str_in(const char *haystack, const char *needle); int _xs_startsorends(const char *str, const char *xfix, int ends); @@ -416,7 +418,7 @@ xs_str *xs_str_wrap_i(const char *prefix, xs_str *str, const char *suffix) } -xs_str *xs_replace_i(xs_str *str, const char *sfrom, const char *sto) +xs_str *xs_replace_in(xs_str *str, const char *sfrom, const char *sto, int times) /* replaces inline all sfrom with sto */ { XS_ASSERT_TYPE(str, XSTYPE_STRING); @@ -426,7 +428,7 @@ xs_str *xs_replace_i(xs_str *str, const char *sfrom, const char *sto) char *ss; int offset = 0; - while ((ss = strstr(str + offset, sfrom)) != NULL) { + while (times > 0 && (ss = strstr(str + offset, sfrom)) != NULL) { int n_offset = ss - str; str = xs_collapse(str, n_offset, sfsz); @@ -434,6 +436,8 @@ xs_str *xs_replace_i(xs_str *str, const char *sfrom, const char *sto) memcpy(str + n_offset, sto, stsz); offset = n_offset + stsz; + + times--; } return str; diff --git a/xs_encdec.h b/xs_encdec.h index 12f40ef..2502520 100644 --- a/xs_encdec.h +++ b/xs_encdec.h @@ -7,13 +7,20 @@ xs_str *xs_hex_enc(const xs_val *data, int size); xs_val *xs_hex_dec(const xs_str *hex, int *size); int xs_is_hex(const char *str); + xs_str *xs_base32_enc(const xs_val *data, int sz); + xs_str *xs_base32hex_enc(const xs_val *data, int sz); + xs_val *xs_base32_dec(const xs_str *data, int *size); + xs_val *xs_base32hex_dec(const xs_str *data, int *size); xs_str *xs_base64_enc(const xs_val *data, int sz); xs_val *xs_base64_dec(const xs_str *data, int *size); + int xs_is_base64(const char *str); xs_str *xs_utf8_enc(xs_str *str, unsigned int cpoint); #ifdef XS_IMPLEMENTATION +/** hex **/ + xs_str *xs_hex_enc(const xs_val *data, int size) /* returns an hexdump of data */ { @@ -78,16 +85,178 @@ int xs_is_hex(const char *str) } -xs_str *xs_base64_enc(const xs_val *data, int sz) -/* encodes data to base64 */ +/** base32 */ + +static char *xs_b32_tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "234567="; + +static char *xs_b32hex_tbl = "0123456789" + "ABCDEFGHIJKLMNOPQRSTUV="; + +/* + 00000|00011|11111|12222|22223|33333|33444|44444 +*/ + +xs_str *xs_base32_enc_tbl(const xs_val *data, int sz, const char *b32_tbl) +/* encodes data to base32 using a table */ +{ + xs_str *s = xs_str_new(NULL); + unsigned char *p; + int n; + + p = (unsigned char *)data; + + for (n = 0; n < sz; n += 5) { + int l = sz - n; + char enc[9] = "========"; + + enc[0] = b32_tbl[(p[n] >> 3) & 0x1f]; + + if (l > 1) { + enc[1] = b32_tbl[(p[n] << 2 | p[n + 1] >> 6) & 0x1f]; + enc[2] = b32_tbl[(p[n + 1] >> 1) & 0x1f]; + + if (l > 2) { + enc[3] = b32_tbl[(p[n + 1] << 4 | p[n + 2] >> 4) & 0x1f]; + + if (l > 3) { + enc[4] = b32_tbl[(p[n + 2] << 1 | p[n + 3] >> 7) & 0x1f]; + enc[5] = b32_tbl[(p[n + 3] >> 2) & 0x1f]; + + if (l > 4) { + enc[6] = b32_tbl[(p[n + 3] << 3 | p[n + 4] >> 5) & 0x1f]; + enc[7] = b32_tbl[(p[n + 4]) & 0x1f]; + } + else + enc[6] = b32_tbl[(p[n + 3] << 3) & 0x1f]; + } + else + enc[4] = b32_tbl[(p[n + 2] << 1) & 0x1f]; + } + else + enc[3] = b32_tbl[(p[n + 1] << 4) & 0x1f]; + } + else + enc[1] = b32_tbl[(p[n] << 2) & 0x1f]; + + s = xs_str_cat(s, enc); + } + + return s; +} + + +xs_str *xs_base32_enc(const xs_val *data, int sz) +/* encodes data to base32 */ +{ + return xs_base32_enc_tbl(data, sz, xs_b32_tbl); +} + + +xs_str *xs_base32hex_enc(const xs_val *data, int sz) +/* encodes data to base32 with HEX alphabet (RFC4648) */ +{ + return xs_base32_enc_tbl(data, sz, xs_b32hex_tbl); +} + + +xs_val *xs_base32_dec_tbl(const xs_str *data, int *size, const char *b32_tbl) +/* decodes data from base32 using a table */ +{ + xs_val *s = NULL; + int sz = 0; + char *p; + + p = (char *)data; + + /* size of data must be a multiple of 8 */ + if (strlen(p) % 8) + return NULL; + + for (p = (char *)data; *p; p += 8) { + int cs[8]; + int n; + unsigned char tmp[5]; + + for (n = 0; n < 8; n++) { + char *ss = strchr(b32_tbl, p[n]); + + if (ss == NULL) { + /* not a base32 char */ + return xs_free(s); + } + + cs[n] = ss - b32_tbl; + } + + n = 0; + + /* #0 byte */ + tmp[n++] = cs[0] << 3 | cs[1] >> 2; + + if (cs[2] != 32) { + /* #1 byte */ + tmp[n++] = (cs[1] & 0x3) << 6 | cs[2] << 1 | (cs[3] & 0x10) >> 4; + + if (cs[4] != 32) { + /* #2 byte */ + tmp[n++] = (cs[3] & 0xf) << 4 | cs[4] >> 1; + + if (cs[5] != 32) { + /* #3 byte */ + tmp[n++] = (cs[4] & 0x1) << 7 | cs[5] << 2 | cs[6] >> 3; + + if (cs[7] != 32) { + /* #4 byte */ + tmp[n++] = (cs[6] & 0x7) << 5 | cs[7]; + } + } + } + } + + /* must be done manually because data can be pure binary */ + s = xs_realloc(s, _xs_blk_size(sz + n)); + memcpy(s + sz, tmp, n); + sz += n; + } + + /* asciiz it to use it as a string */ + s = xs_realloc(s, _xs_blk_size(sz + 1)); + s[sz] = '\0'; + + *size = sz; + + return s; +} + + +xs_val *xs_base32_dec(const xs_str *data, int *size) +/* decodes data from base32 */ +{ + return xs_base32_dec_tbl(data, size, xs_b32_tbl); +} + + +xs_val *xs_base32hex_dec(const xs_str *data, int *size) +/* decodes data from base32 with HEX alphabet (RFC4648) */ +{ + return xs_base32_dec_tbl(data, size, xs_b32hex_tbl); +} + + +/** base64 */ + +static char *xs_b64_tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/="; + +xs_str *xs_base64_enc_tbl(const xs_val *data, int sz, const char *b64_tbl) +/* encodes data to base64 using a table */ { xs_str *s; unsigned char *p; char *i; int bsz, n; - static char *b64_tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - "0123456789+/"; bsz = ((sz + 3 - 1) / 3) * 4; i = s = xs_realloc(NULL, _xs_blk_size(bsz + 1)); @@ -123,15 +292,19 @@ xs_str *xs_base64_enc(const xs_val *data, int sz) } -xs_val *xs_base64_dec(const xs_str *data, int *size) -/* decodes data from base64 */ +xs_str *xs_base64_enc(const xs_val *data, int sz) +/* encodes data to base64 */ +{ + return xs_base64_enc_tbl(data, sz, xs_b64_tbl); +} + + +xs_val *xs_base64_dec_tbl(const xs_str *data, int *size, const char *b64_tbl) +/* decodes data from base64 using a table */ { xs_val *s = NULL; int sz = 0; char *p; - static char *b64_tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - "0123456789+/="; p = (char *)data; @@ -184,6 +357,34 @@ xs_val *xs_base64_dec(const xs_str *data, int *size) } +xs_val *xs_base64_dec(const xs_str *data, int *size) +/* decodes data from base64 */ +{ + return xs_base64_dec_tbl(data, size, xs_b64_tbl); +} + + +int xs_is_base64_tbl(const char *str, const char *b64_tbl) +/* returns 1 if str is a base64 string, with table */ +{ + while (*str) { + if (strchr(b64_tbl, *str++) == NULL) + return 0; + } + + return 1; +} + + +int xs_is_base64(const char *str) +/* returns 1 if str is a base64 string */ +{ + return xs_is_base64_tbl(str, xs_b64_tbl); +} + + +/** utf-8 **/ + xs_str *xs_utf8_enc(xs_str *str, unsigned int cpoint) /* encodes an Unicode codepoint to utf8 */ { diff --git a/xs_version.h b/xs_version.h index 1d59f5e..6117ce9 100644 --- a/xs_version.h +++ b/xs_version.h @@ -1 +1 @@ -/* a885c7cc4c8e6384ae23125ed05f434471ccc6fb */ +/* dfdd729248d7169b80cb6a7462fe6c0ba6efeb16 */ -- cgit v1.2.3 From be5f08e6c3d605fb2beb1fdd1c2f10818b1e1812 Mon Sep 17 00:00:00 2001 From: default Date: Tue, 2 May 2023 06:49:00 +0200 Subject: Use xs_replace_n() where it suits. --- http.c | 2 +- mastoapi.c | 14 +++++++------- webfinger.c | 6 +++--- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/http.c b/http.c index ae46001..58d188c 100644 --- a/http.c +++ b/http.c @@ -33,7 +33,7 @@ xs_dict *http_signed_request_raw(const char *keyid, const char *seckey, date = xs_str_utctime(0, "%a, %d %b %Y %H:%M:%S GMT"); { - xs *s = xs_replace(url, "https:/" "/", ""); + xs *s = xs_replace_n(url, "https:/" "/", "", 1); l1 = xs_split_n(s, "/", 1); } diff --git a/mastoapi.c b/mastoapi.c index 72d9579..f7e101d 100644 --- a/mastoapi.c +++ b/mastoapi.c @@ -185,7 +185,7 @@ int oauth_get_handler(const xs_dict *req, const char *q_path, int status = 404; xs_dict *msg = xs_dict_get(req, "q_vars"); - xs *cmd = xs_replace(q_path, "/oauth", ""); + xs *cmd = xs_replace_n(q_path, "/oauth", "", 1); srv_debug(1, xs_fmt("oauth_get_handler %s", q_path)); @@ -245,7 +245,7 @@ int oauth_post_handler(const xs_dict *req, const char *q_path, else args = xs_dup(xs_dict_get(req, "p_vars")); - xs *cmd = xs_replace(q_path, "/oauth", ""); + xs *cmd = xs_replace_n(q_path, "/oauth", "", 1); srv_debug(1, xs_fmt("oauth_post_handler %s", q_path)); @@ -328,7 +328,7 @@ int oauth_post_handler(const xs_dict *req, const char *q_path, const char *auhdr = xs_dict_get(req, "authorization"); if (!xs_is_null(auhdr) && xs_startswith(auhdr, "Basic ")) { - xs *s1 = xs_replace(auhdr, "Basic ", ""); + xs *s1 = xs_replace_n(auhdr, "Basic ", "", 1); int size; xs *s2 = xs_base64_dec(s1, &size); @@ -787,7 +787,7 @@ int process_auth_token(snac *snac, const xs_dict *req) /* if there is an authorization field, try to validate it */ if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) { - xs *tokid = xs_replace(v, "Bearer ", ""); + xs *tokid = xs_replace_n(v, "Bearer ", "", 1); xs *token = token_get(tokid); if (token != NULL) { @@ -826,7 +826,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, int status = 404; xs_dict *args = xs_dict_get(req, "q_vars"); - xs *cmd = xs_replace(q_path, "/api", ""); + xs *cmd = xs_replace_n(q_path, "/api", "", 1); snac snac1 = {0}; int logged_in = process_auth_token(&snac1, req); @@ -1500,7 +1500,7 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path, printf("%s\n", j); }*/ - xs *cmd = xs_replace(q_path, "/api", ""); + xs *cmd = xs_replace_n(q_path, "/api", "", 1); snac snac = {0}; int logged_in = process_auth_token(&snac, req); @@ -1916,7 +1916,7 @@ int mastoapi_put_handler(const xs_dict *req, const char *q_path, if (args == NULL) return 400; - xs *cmd = xs_replace(q_path, "/api", ""); + xs *cmd = xs_replace_n(q_path, "/api", "", 1); snac snac = {0}; int logged_in = process_auth_token(&snac, req); diff --git a/webfinger.c b/webfinger.c index eb6b2ad..f56b6f2 100644 --- a/webfinger.c +++ b/webfinger.c @@ -21,7 +21,7 @@ int webfinger_request(const char *qs, char **actor, char **user) if (xs_startswith(qs, "https:/" "/")) { /* actor query: pick the host */ - xs *s = xs_replace(qs, "https:/" "/", ""); + xs *s = xs_replace_n(qs, "https:/" "/", "", 1); l = xs_split_n(s, "/", 1); @@ -74,7 +74,7 @@ int webfinger_request(const char *qs, char **actor, char **user) char *subject = xs_dict_get(obj, "subject"); if (subject) - *user = xs_replace(subject, "acct:", ""); + *user = xs_replace_n(subject, "acct:", "", 1); } if (actor != NULL) { @@ -136,7 +136,7 @@ int webfinger_get_handler(d_char *req, char *q_path, else if (xs_startswith(resource, "acct:")) { /* it's an account name */ - xs *an = xs_replace(resource, "acct:", ""); + xs *an = xs_replace_n(resource, "acct:", "", 1); xs *l = NULL; /* strip a possible leading @ */ -- cgit v1.2.3 From 0bd609f5be9aa24bced629273ff2428058388ac3 Mon Sep 17 00:00:00 2001 From: default Date: Wed, 3 May 2023 07:57:10 +0200 Subject: Fixed missing notifications in certain circunstancies. --- html.c | 2 +- mastoapi.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/html.c b/html.c index 5f57262..9d6fbc6 100644 --- a/html.c +++ b/html.c @@ -1226,7 +1226,7 @@ xs_str *html_notifications(snac *snac) const char *actor_id = xs_dict_get(noti, "actor"); xs *actor = NULL; - if (!valid_status(object_get(actor_id, &actor))) + if (!valid_status(actor_get(snac, actor_id, &actor))) continue; xs *a_name = actor_name(actor); diff --git a/mastoapi.c b/mastoapi.c index f7e101d..7324bed 100644 --- a/mastoapi.c +++ b/mastoapi.c @@ -1108,7 +1108,7 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, xs *actor = NULL; xs *entry = NULL; - if (!valid_status(object_get(xs_dict_get(noti, "actor"), &actor))) + if (!valid_status(actor_get(&snac1, xs_dict_get(noti, "actor"), &actor))) continue; if (objid != NULL && !valid_status(object_get(objid, &entry))) -- cgit v1.2.3 From 18f799a58fc7516012751abb0b371ba5420ac12b Mon Sep 17 00:00:00 2001 From: default Date: Wed, 3 May 2023 07:58:08 +0200 Subject: Updated RELEASE_NOTES. --- RELEASE_NOTES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 78a3603..7453ec3 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -2,6 +2,8 @@ ## 2.30 +Fixed a bug that made some notifications to be missed. + New Mastodon API features: the instance public timeline is now a real one, unfavourite / unreblog is supported (somewhat). ## 2.29 -- cgit v1.2.3 From cf2b334d86e8b4ccd0525fbb741fbbaf86a2c618 Mon Sep 17 00:00:00 2001 From: default Date: Wed, 3 May 2023 08:15:38 +0200 Subject: Ensure no actor data is left in actor_get(). --- data.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/data.c b/data.c index 67f5751..edeb676 100644 --- a/data.c +++ b/data.c @@ -1360,7 +1360,7 @@ int actor_get(snac *snac1, const char *actor, xs_dict **data) /* returns an already downloaded actor */ { int status = 200; - xs_dict *d; + xs_dict *d = NULL; if (strcmp(actor, snac1->actor) == 0) { /* this actor */ @@ -1388,8 +1388,10 @@ int actor_get(snac *snac1, const char *actor, xs_dict **data) } /* read the object */ - if (!valid_status(status = object_get(actor, &d))) + if (!valid_status(status = object_get(actor, &d))) { + d = xs_free(d); return status; + } if (data) *data = d; -- cgit v1.2.3 From cf8d91147b1a0233ba5d3d98a8806bd2a87c222d Mon Sep 17 00:00:00 2001 From: default Date: Thu, 4 May 2023 05:52:35 +0200 Subject: Updated documentation. --- doc/snac.1 | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/doc/snac.1 b/doc/snac.1 index ec8df64..91d7e85 100644 --- a/doc/snac.1 +++ b/doc/snac.1 @@ -220,6 +220,20 @@ Please take note that they will show your timeline in a 'Mastodon fashion' post display with the most active threads at the top that the web interface of .Nm provides. +.Ss Implementing post bots +.Nm +makes very easy to post messages in a non-interactive manner. This example +posts a string: +.Bd -literal -offset indent +uptime | snac note $SNAC_BASEDIR $SNAC_USER - +.Ed +.Pp +You can setup a line like this from a +.Xr crontab 5 +or similar. Take note that you need a) command-line access to the same machine +that hosts the +.Nm +instance, and b) write permissions to the storage directories and files. .Sh ENVIRONMENT .Bl -tag -width Ds .It Ev DEBUG -- cgit v1.2.3 From a9f0f2f695d653a2ca58057837fae6b8bc4dc13f Mon Sep 17 00:00:00 2001 From: default Date: Thu, 4 May 2023 06:27:13 +0200 Subject: Avoid crash in optional mastoapi argument. --- mastoapi.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mastoapi.c b/mastoapi.c index 7324bed..c51c933 100644 --- a/mastoapi.c +++ b/mastoapi.c @@ -1569,6 +1569,9 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path, if (xs_is_null(media_ids)) media_ids = xs_dict_get(args, "media_ids[]"); + if (xs_is_null(visibility)) + visibility = "public"; + xs *attach_list = xs_list_new(); xs *irt = NULL; -- cgit v1.2.3 From dd290c8e226c7d78dc74695d26e28ec8bf4eab26 Mon Sep 17 00:00:00 2001 From: default Date: Thu, 4 May 2023 06:38:24 +0200 Subject: Updated documentation. --- doc/snac.1 | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/doc/snac.1 b/doc/snac.1 index 91d7e85..c52eba9 100644 --- a/doc/snac.1 +++ b/doc/snac.1 @@ -234,6 +234,17 @@ or similar. Take note that you need a) command-line access to the same machine that hosts the .Nm instance, and b) write permissions to the storage directories and files. +.Pp +You can also post non-interactively using the Mastodon API and a command-line +http tool like +.Xr curl 1 +or similar. This has the advantage that you can do it remotely from any host, +anywhere; the only thing you need is an API Token. This is an example: +.Bd -literal -offset indent +curl -X POST https://snac.example.com/api/v1/statuses \ +--header "Authorization: Bearer ${TOKEN}" -d "status=$(uptime)" +.Ed +.Pp .Sh ENVIRONMENT .Bl -tag -width Ds .It Ev DEBUG -- cgit v1.2.3 From d46bf8837c1c6654868170a293352d12f6d84962 Mon Sep 17 00:00:00 2001 From: default Date: Thu, 4 May 2023 06:40:51 +0200 Subject: Updated documentation. --- doc/snac.1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/snac.1 b/doc/snac.1 index c52eba9..0d36f5d 100644 --- a/doc/snac.1 +++ b/doc/snac.1 @@ -241,10 +241,11 @@ http tool like or similar. This has the advantage that you can do it remotely from any host, anywhere; the only thing you need is an API Token. This is an example: .Bd -literal -offset indent -curl -X POST https://snac.example.com/api/v1/statuses \ +curl -X POST https://snac.example.com/api/v1/statuses \\ --header "Authorization: Bearer ${TOKEN}" -d "status=$(uptime)" .Ed .Pp +You can obtain an API Token from a mobile app. .Sh ENVIRONMENT .Bl -tag -width Ds .It Ev DEBUG -- cgit v1.2.3 From cd19efcae29d64171e9f2eb852799cc0fe5642ea Mon Sep 17 00:00:00 2001 From: default Date: Thu, 4 May 2023 09:17:50 +0200 Subject: Updated dependencies. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index c0e4170..710d61f 100644 --- a/Makefile +++ b/Makefile @@ -40,7 +40,7 @@ httpd.o: httpd.c xs.h xs_io.h xs_encdec.h xs_json.h xs_socket.h \ xs_httpd.h xs_mime.h snac.h main.o: main.c xs.h xs_io.h xs_encdec.h xs_json.h snac.h mastoapi.o: mastoapi.c xs.h xs_encdec.h xs_openssl.h xs_json.h xs_io.h \ - xs_time.h snac.h + xs_time.h xs_glob.h snac.h snac.o: snac.c xs.h xs_io.h xs_encdec.h xs_json.h xs_curl.h xs_openssl.h \ xs_socket.h xs_httpd.h xs_mime.h xs_regex.h xs_set.h xs_time.h xs_glob.h \ snac.h -- cgit v1.2.3 From 511f5062b7df26c47409c88649f24d68bbd43ccb Mon Sep 17 00:00:00 2001 From: default Date: Thu, 4 May 2023 09:19:26 +0200 Subject: Deleted real unused parameters. --- activitypub.c | 4 ++-- data.c | 2 +- html.c | 15 +++++++-------- snac.h | 2 +- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/activitypub.c b/activitypub.c index b20ae0f..13b1ce2 100644 --- a/activitypub.c +++ b/activitypub.c @@ -109,7 +109,7 @@ int actor_request(snac *snac, const char *actor, xs_dict **data) if (valid_status(status2)) { /* renew data */ - status = actor_add(snac, actor, payload); + status = actor_add(actor, payload); if (data != NULL) { *data = payload; @@ -1121,7 +1121,7 @@ int process_input_message(snac *snac, xs_dict *msg, xs_dict *req) else if (strcmp(type, "Update") == 0) { if (strcmp(utype, "Person") == 0) { - actor_add(snac, actor, xs_dict_get(msg, "object")); + actor_add(actor, xs_dict_get(msg, "object")); snac_log(snac, xs_fmt("updated actor %s", actor)); } diff --git a/data.c b/data.c index edeb676..f03fd71 100644 --- a/data.c +++ b/data.c @@ -1349,7 +1349,7 @@ int is_hidden(snac *snac, const char *id) } -int actor_add(snac *snac, const char *actor, xs_dict *msg) +int actor_add(const char *actor, xs_dict *msg) /* adds an actor */ { return object_add_ow(actor, msg); diff --git a/html.c b/html.c index 9d6fbc6..2aa6514 100644 --- a/html.c +++ b/html.c @@ -79,7 +79,7 @@ xs_str *actor_name(xs_dict *actor) } -d_char *html_actor_icon(snac *snac, d_char *os, char *actor, +xs_str *html_actor_icon(xs_str *os, char *actor, const char *date, const char *udate, const char *url, int priv) { xs *s = xs_str_new(NULL); @@ -168,7 +168,7 @@ d_char *html_msg_icon(snac *snac, d_char *os, char *msg) date = xs_dict_get(msg, "published"); udate = xs_dict_get(msg, "updated"); - os = html_actor_icon(snac, os, actor, date, udate, url, priv); + os = html_actor_icon(os, actor, date, udate, url, priv); } return os; @@ -983,7 +983,7 @@ d_char *html_entry(snac *snac, d_char *os, char *msg, int local, } -d_char *html_user_footer(snac *snac, d_char *s) +xs_str *html_user_footer(xs_str *s) { xs *s1 = xs_fmt( "