2022-09-19 20:41:30 +00:00
|
|
|
/* snac - A simple, minimalistic ActivityPub instance */
|
2024-01-04 08:22:03 +00:00
|
|
|
/* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
|
2022-09-19 20:41:30 +00:00
|
|
|
|
|
|
|
#include "xs.h"
|
2023-11-17 07:51:53 +00:00
|
|
|
#include "xs_hex.h"
|
2022-09-19 20:41:30 +00:00
|
|
|
#include "xs_io.h"
|
|
|
|
#include "xs_json.h"
|
2022-09-20 07:39:28 +00:00
|
|
|
#include "xs_openssl.h"
|
2022-10-03 09:18:49 +00:00
|
|
|
#include "xs_glob.h"
|
2022-11-26 17:04:05 +00:00
|
|
|
#include "xs_set.h"
|
2023-04-13 14:59:17 +00:00
|
|
|
#include "xs_time.h"
|
2024-03-11 05:00:21 +00:00
|
|
|
#include "xs_regex.h"
|
2024-05-09 07:24:39 +00:00
|
|
|
#include "xs_match.h"
|
2024-08-23 18:27:07 +00:00
|
|
|
#include "xs_unicode.h"
|
2024-11-12 04:38:55 +00:00
|
|
|
#include "xs_random.h"
|
2022-09-19 20:41:30 +00:00
|
|
|
|
|
|
|
#include "snac.h"
|
|
|
|
|
2022-09-20 10:56:21 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <sys/stat.h>
|
2022-11-23 14:34:07 +00:00
|
|
|
#include <sys/file.h>
|
2023-04-14 17:17:16 +00:00
|
|
|
#include <sys/time.h>
|
2022-11-23 14:34:07 +00:00
|
|
|
#include <fcntl.h>
|
2023-02-23 08:32:47 +00:00
|
|
|
#include <pthread.h>
|
2023-02-10 12:39:17 +00:00
|
|
|
|
2023-01-31 17:33:45 +00:00
|
|
|
double disk_layout = 2.7;
|
2022-11-23 12:32:23 +00:00
|
|
|
|
2023-02-23 08:32:47 +00:00
|
|
|
/* storage serializer */
|
|
|
|
pthread_mutex_t data_mutex = {0};
|
2022-11-23 12:32:23 +00:00
|
|
|
|
2023-08-31 05:00:15 +00:00
|
|
|
int snac_upgrade(xs_str **error);
|
2022-09-19 21:33:11 +00:00
|
|
|
|
2023-02-23 08:32:47 +00:00
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
int srv_open(const char *basedir, int auto_upgrade)
|
2022-09-19 20:41:30 +00:00
|
|
|
/* opens a server */
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
xs *cfg_file = NULL;
|
|
|
|
FILE *f;
|
2023-06-07 09:08:14 +00:00
|
|
|
xs_str *error = NULL;
|
2022-09-19 20:41:30 +00:00
|
|
|
|
2023-02-23 08:32:47 +00:00
|
|
|
pthread_mutex_init(&data_mutex, NULL);
|
|
|
|
|
2022-09-19 20:41:30 +00:00
|
|
|
srv_basedir = xs_str_new(basedir);
|
|
|
|
|
2022-09-20 07:39:28 +00:00
|
|
|
if (xs_endswith(srv_basedir, "/"))
|
2023-01-12 08:28:02 +00:00
|
|
|
srv_basedir = xs_crop_i(srv_basedir, 0, -1);
|
2022-09-20 07:39:28 +00:00
|
|
|
|
2022-09-19 20:41:30 +00:00
|
|
|
cfg_file = xs_fmt("%s/server.json", basedir);
|
|
|
|
|
|
|
|
if ((f = fopen(cfg_file, "r")) == NULL)
|
2024-08-24 12:33:04 +00:00
|
|
|
error = xs_fmt("ERROR: cannot open '%s'", cfg_file);
|
2022-09-19 20:41:30 +00:00
|
|
|
else {
|
|
|
|
/* read full config file */
|
2023-08-05 12:50:20 +00:00
|
|
|
srv_config = xs_json_load(f);
|
2022-10-17 18:25:42 +00:00
|
|
|
fclose(f);
|
2022-09-19 20:41:30 +00:00
|
|
|
|
|
|
|
/* parse */
|
|
|
|
|
|
|
|
if (srv_config == NULL)
|
2022-10-09 15:22:18 +00:00
|
|
|
error = xs_fmt("ERROR: cannot parse '%s'", cfg_file);
|
2022-09-19 20:41:30 +00:00
|
|
|
else {
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *host;
|
|
|
|
const char *prefix;
|
|
|
|
const char *dbglvl;
|
|
|
|
const char *proto;
|
2022-09-19 20:41:30 +00:00
|
|
|
|
|
|
|
host = xs_dict_get(srv_config, "host");
|
|
|
|
prefix = xs_dict_get(srv_config, "prefix");
|
|
|
|
dbglvl = xs_dict_get(srv_config, "dbglevel");
|
2024-04-18 15:13:31 +00:00
|
|
|
proto = xs_dict_get_def(srv_config, "protocol", "https");
|
2022-09-19 20:41:30 +00:00
|
|
|
|
|
|
|
if (host == NULL || prefix == NULL)
|
2022-10-09 15:22:18 +00:00
|
|
|
error = xs_str_new("ERROR: cannot get server data");
|
2022-09-19 20:41:30 +00:00
|
|
|
else {
|
2024-04-18 15:13:31 +00:00
|
|
|
srv_baseurl = xs_fmt("%s:/" "/%s%s", proto, host, prefix);
|
2022-09-19 20:41:30 +00:00
|
|
|
|
|
|
|
dbglevel = (int) xs_number_get(dbglvl);
|
|
|
|
|
|
|
|
if ((dbglvl = getenv("DEBUG")) != NULL) {
|
|
|
|
dbglevel = atoi(dbglvl);
|
2022-09-20 07:39:28 +00:00
|
|
|
error = xs_fmt("DEBUG level set to %d from environment", dbglevel);
|
2022-09-19 20:41:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 09:53:16 +00:00
|
|
|
if (auto_upgrade)
|
2023-01-31 17:33:45 +00:00
|
|
|
ret = snac_upgrade(&error);
|
2022-11-25 09:53:16 +00:00
|
|
|
else {
|
2023-01-31 17:33:45 +00:00
|
|
|
if (xs_number_get(xs_dict_get(srv_config, "layout")) < disk_layout)
|
2022-11-25 10:04:23 +00:00
|
|
|
error = xs_fmt("ERROR: disk layout changed - execute 'snac upgrade' first");
|
2022-11-25 09:56:34 +00:00
|
|
|
else
|
|
|
|
ret = 1;
|
2022-11-25 09:53:16 +00:00
|
|
|
}
|
2022-09-19 20:41:30 +00:00
|
|
|
}
|
2022-10-09 15:22:18 +00:00
|
|
|
|
2022-09-19 20:41:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-26 06:39:23 +00:00
|
|
|
if (error != NULL)
|
2022-09-20 07:39:28 +00:00
|
|
|
srv_log(error);
|
|
|
|
|
2023-02-02 04:07:20 +00:00
|
|
|
/* create the queue/ subdir, just in case */
|
|
|
|
xs *qdir = xs_fmt("%s/queue", srv_basedir);
|
2023-02-07 08:25:01 +00:00
|
|
|
mkdirx(qdir);
|
2023-02-02 04:07:20 +00:00
|
|
|
|
2023-03-02 08:01:08 +00:00
|
|
|
xs *ibdir = xs_fmt("%s/inbox", srv_basedir);
|
2023-03-02 07:43:50 +00:00
|
|
|
mkdirx(ibdir);
|
|
|
|
|
2023-12-06 13:46:51 +00:00
|
|
|
xs *tmpdir = xs_fmt("%s/tmp", srv_basedir);
|
|
|
|
mkdirx(tmpdir);
|
|
|
|
|
2024-03-06 17:08:01 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
/* Apple uses st_atimespec instead of st_atim etc */
|
|
|
|
#define st_atim st_atimespec
|
|
|
|
#define st_ctim st_ctimespec
|
|
|
|
#define st_mtim st_mtimespec
|
|
|
|
#endif
|
|
|
|
|
2022-11-14 16:40:31 +00:00
|
|
|
#ifdef __OpenBSD__
|
2024-08-05 04:54:47 +00:00
|
|
|
if (xs_is_true(xs_dict_get(srv_config, "disable_openbsd_security"))) {
|
2023-01-13 13:18:23 +00:00
|
|
|
srv_debug(1, xs_dup("OpenBSD security disabled by admin"));
|
|
|
|
}
|
|
|
|
else {
|
2024-08-05 04:54:47 +00:00
|
|
|
int smail = !xs_is_true(xs_dict_get(srv_config, "disable_email_notifications"));
|
|
|
|
const char *address = xs_dict_get(srv_config, "address");
|
2024-04-02 15:44:27 +00:00
|
|
|
|
2023-01-13 13:18:23 +00:00
|
|
|
srv_debug(1, xs_fmt("Calling unveil()"));
|
|
|
|
unveil(basedir, "rwc");
|
2024-01-11 10:07:07 +00:00
|
|
|
unveil("/tmp", "rwc");
|
2023-01-13 13:18:23 +00:00
|
|
|
unveil("/etc/resolv.conf", "r");
|
|
|
|
unveil("/etc/hosts", "r");
|
|
|
|
unveil("/etc/ssl/openssl.cnf", "r");
|
|
|
|
unveil("/etc/ssl/cert.pem", "r");
|
|
|
|
unveil("/usr/share/zoneinfo", "r");
|
2024-04-02 15:44:27 +00:00
|
|
|
|
|
|
|
if (smail)
|
|
|
|
unveil("/usr/sbin/sendmail", "x");
|
|
|
|
|
2024-08-05 04:54:47 +00:00
|
|
|
if (*address == '/')
|
|
|
|
unveil(address, "rwc");
|
|
|
|
|
2023-01-13 13:18:23 +00:00
|
|
|
unveil(NULL, NULL);
|
2024-08-05 04:54:47 +00:00
|
|
|
|
2023-01-13 13:18:23 +00:00
|
|
|
srv_debug(1, xs_fmt("Calling pledge()"));
|
2024-04-02 15:44:27 +00:00
|
|
|
|
2024-08-05 04:54:47 +00:00
|
|
|
xs *p = xs_str_new("stdio rpath wpath cpath flock inet proc dns fattr");
|
|
|
|
|
2024-04-02 15:44:27 +00:00
|
|
|
if (smail)
|
2024-08-05 04:54:47 +00:00
|
|
|
p = xs_str_cat(p, " exec");
|
|
|
|
|
|
|
|
if (*address == '/')
|
|
|
|
p = xs_str_cat(p, " unix");
|
|
|
|
|
|
|
|
pledge(p, NULL);
|
2023-01-13 13:18:23 +00:00
|
|
|
}
|
2022-11-14 16:40:31 +00:00
|
|
|
#endif /* __OpenBSD__ */
|
|
|
|
|
2024-03-25 14:33:14 +00:00
|
|
|
/* read (and drop) emojis.json, possibly creating it */
|
|
|
|
xs_free(emojis());
|
|
|
|
|
2024-09-04 07:48:35 +00:00
|
|
|
/* if style.css does not exist, create it */
|
|
|
|
xs *css_fn = xs_fmt("%s/style.css", srv_basedir);
|
|
|
|
|
|
|
|
if (mtime(css_fn) == 0) {
|
|
|
|
srv_log(xs_fmt("Writing style.css"));
|
|
|
|
write_default_css();
|
|
|
|
}
|
|
|
|
|
2024-11-12 08:01:30 +00:00
|
|
|
/* create the proxy token seed */
|
|
|
|
{
|
2024-11-12 04:38:55 +00:00
|
|
|
char rnd[16];
|
|
|
|
xs_rnd_buf(rnd, sizeof(rnd));
|
|
|
|
|
2024-11-12 08:01:30 +00:00
|
|
|
srv_proxy_token_seed = xs_hex_enc(rnd, sizeof(rnd));
|
2024-11-12 04:38:55 +00:00
|
|
|
}
|
|
|
|
|
2022-09-19 20:41:30 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-25 08:38:12 +00:00
|
|
|
void srv_free(void)
|
|
|
|
{
|
|
|
|
xs_free(srv_basedir);
|
|
|
|
xs_free(srv_config);
|
|
|
|
xs_free(srv_baseurl);
|
2023-02-23 08:32:47 +00:00
|
|
|
|
|
|
|
pthread_mutex_destroy(&data_mutex);
|
2022-10-25 08:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-19 21:33:11 +00:00
|
|
|
void user_free(snac *snac)
|
2022-09-19 20:41:30 +00:00
|
|
|
/* frees a user snac */
|
|
|
|
{
|
2022-10-25 08:20:23 +00:00
|
|
|
xs_free(snac->uid);
|
|
|
|
xs_free(snac->basedir);
|
|
|
|
xs_free(snac->config);
|
2023-02-12 08:17:38 +00:00
|
|
|
xs_free(snac->config_o);
|
2022-10-25 08:20:23 +00:00
|
|
|
xs_free(snac->key);
|
2024-02-15 16:34:46 +00:00
|
|
|
xs_free(snac->links);
|
2022-10-25 08:20:23 +00:00
|
|
|
xs_free(snac->actor);
|
2022-12-02 18:14:59 +00:00
|
|
|
xs_free(snac->md5);
|
2022-09-19 20:41:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-03 10:01:25 +00:00
|
|
|
int user_open(snac *user, const char *uid)
|
2022-09-19 20:41:30 +00:00
|
|
|
/* opens a user */
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
2024-01-03 10:01:25 +00:00
|
|
|
*user = (snac){0};
|
2022-09-19 20:41:30 +00:00
|
|
|
|
|
|
|
if (validate_uid(uid)) {
|
2023-10-13 04:33:12 +00:00
|
|
|
xs *cfg_file = NULL;
|
2022-09-19 20:41:30 +00:00
|
|
|
FILE *f;
|
|
|
|
|
2023-10-13 04:33:12 +00:00
|
|
|
xs *t = xs_fmt("%s/user/%s", srv_basedir, uid);
|
2022-09-19 20:41:30 +00:00
|
|
|
|
2023-10-13 04:33:12 +00:00
|
|
|
if (mtime(t) == 0.0) {
|
|
|
|
/* user folder does not exist; try with a different case */
|
|
|
|
xs *lcuid = xs_tolower_i(xs_dup(uid));
|
|
|
|
xs *ulist = user_list();
|
|
|
|
xs_list *p = ulist;
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_str *v;
|
2023-10-13 04:33:12 +00:00
|
|
|
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
xs *v2 = xs_tolower_i(xs_dup(v));
|
|
|
|
|
|
|
|
if (strcmp(lcuid, v2) == 0) {
|
2024-01-03 10:01:25 +00:00
|
|
|
user->uid = xs_dup(v);
|
2023-10-13 04:33:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2024-01-03 10:01:25 +00:00
|
|
|
user->uid = xs_str_new(uid);
|
2023-10-13 04:33:12 +00:00
|
|
|
|
2024-01-03 10:01:25 +00:00
|
|
|
if (user->uid == NULL)
|
2023-10-13 04:33:12 +00:00
|
|
|
return ret;
|
|
|
|
|
2024-01-03 10:01:25 +00:00
|
|
|
user->basedir = xs_fmt("%s/user/%s", srv_basedir, user->uid);
|
2022-09-19 20:41:30 +00:00
|
|
|
|
2024-01-03 10:01:25 +00:00
|
|
|
cfg_file = xs_fmt("%s/user.json", user->basedir);
|
2022-09-19 20:41:30 +00:00
|
|
|
|
|
|
|
if ((f = fopen(cfg_file, "r")) != NULL) {
|
|
|
|
/* read full config file */
|
2024-01-03 10:01:25 +00:00
|
|
|
user->config = xs_json_load(f);
|
2022-09-19 20:41:30 +00:00
|
|
|
fclose(f);
|
|
|
|
|
2024-01-03 10:01:25 +00:00
|
|
|
if (user->config != NULL) {
|
|
|
|
xs *key_file = xs_fmt("%s/key.json", user->basedir);
|
2022-09-19 20:41:30 +00:00
|
|
|
|
|
|
|
if ((f = fopen(key_file, "r")) != NULL) {
|
2024-01-03 10:01:25 +00:00
|
|
|
user->key = xs_json_load(f);
|
2022-09-19 20:41:30 +00:00
|
|
|
fclose(f);
|
|
|
|
|
2024-01-03 10:01:25 +00:00
|
|
|
if (user->key != NULL) {
|
|
|
|
user->actor = xs_fmt("%s/%s", srv_baseurl, user->uid);
|
|
|
|
user->md5 = xs_md5_hex(user->actor, strlen(user->actor));
|
2023-02-12 08:17:38 +00:00
|
|
|
|
|
|
|
/* everything is ok right now */
|
2022-09-19 20:41:30 +00:00
|
|
|
ret = 1;
|
2023-02-12 08:17:38 +00:00
|
|
|
|
|
|
|
/* does it have a configuration override? */
|
2024-01-03 10:01:25 +00:00
|
|
|
xs *cfg_file_o = xs_fmt("%s/user_o.json", user->basedir);
|
2023-02-12 08:17:38 +00:00
|
|
|
if ((f = fopen(cfg_file_o, "r")) != NULL) {
|
2024-01-03 10:01:25 +00:00
|
|
|
user->config_o = xs_json_load(f);
|
2023-02-12 08:17:38 +00:00
|
|
|
fclose(f);
|
|
|
|
|
2024-01-03 10:01:25 +00:00
|
|
|
if (user->config_o == NULL)
|
2023-02-12 08:26:44 +00:00
|
|
|
srv_log(xs_fmt("error parsing '%s'", cfg_file_o));
|
2023-02-12 08:17:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-03 10:01:25 +00:00
|
|
|
if (user->config_o == NULL)
|
|
|
|
user->config_o = xs_dict_new();
|
2022-09-19 20:41:30 +00:00
|
|
|
}
|
|
|
|
else
|
2023-02-12 08:26:44 +00:00
|
|
|
srv_log(xs_fmt("error parsing '%s'", key_file));
|
2022-09-19 20:41:30 +00:00
|
|
|
}
|
|
|
|
else
|
2023-02-08 14:03:35 +00:00
|
|
|
srv_log(xs_fmt("error opening '%s' %d", key_file, errno));
|
2022-09-19 20:41:30 +00:00
|
|
|
}
|
|
|
|
else
|
2023-02-12 08:26:44 +00:00
|
|
|
srv_log(xs_fmt("error parsing '%s'", cfg_file));
|
2022-09-19 20:41:30 +00:00
|
|
|
}
|
|
|
|
else
|
2023-02-08 14:03:35 +00:00
|
|
|
srv_debug(2, xs_fmt("error opening '%s' %d", cfg_file, errno));
|
2024-02-15 16:34:46 +00:00
|
|
|
|
|
|
|
/* verified links */
|
|
|
|
xs *links_file = xs_fmt("%s/links.json", user->basedir);
|
|
|
|
|
|
|
|
if ((f = fopen(links_file, "r")) != NULL) {
|
|
|
|
user->links = xs_json_load(f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
2022-09-19 20:41:30 +00:00
|
|
|
}
|
|
|
|
else
|
2023-02-03 15:51:59 +00:00
|
|
|
srv_debug(1, xs_fmt("invalid user '%s'", uid));
|
2022-09-19 20:41:30 +00:00
|
|
|
|
|
|
|
if (!ret)
|
2024-01-03 10:01:25 +00:00
|
|
|
user_free(user);
|
2022-09-19 20:41:30 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-15 11:04:30 +00:00
|
|
|
xs_list *user_list(void)
|
2022-10-03 08:51:52 +00:00
|
|
|
/* returns the list of user ids */
|
|
|
|
{
|
|
|
|
xs *spec = xs_fmt("%s/user/" "*", srv_basedir);
|
|
|
|
return xs_glob(spec, 1, 0);
|
|
|
|
}
|
2022-09-19 21:33:11 +00:00
|
|
|
|
2022-09-20 07:39:28 +00:00
|
|
|
|
2023-04-16 05:43:41 +00:00
|
|
|
int user_open_by_md5(snac *snac, const char *md5)
|
|
|
|
/* iterates all users searching by md5 */
|
|
|
|
{
|
|
|
|
xs *ulist = user_list();
|
|
|
|
xs_list *p = ulist;
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_str *v;
|
2023-04-16 05:43:41 +00:00
|
|
|
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
user_open(snac, v);
|
|
|
|
|
|
|
|
if (strcmp(snac->md5, md5) == 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
user_free(snac);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-05-31 15:51:33 +00:00
|
|
|
int user_persist(snac *snac, int publish)
|
2024-05-27 10:24:17 +00:00
|
|
|
/* store user */
|
|
|
|
{
|
|
|
|
xs *fn = xs_fmt("%s/user.json", snac->basedir);
|
|
|
|
xs *bfn = xs_fmt("%s.bak", fn);
|
|
|
|
FILE *f;
|
|
|
|
|
2024-11-23 16:33:49 +00:00
|
|
|
if (publish) {
|
|
|
|
/* check if any of the relevant fields have really changed */
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
|
|
|
xs *old = xs_json_load(f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
if (old != NULL) {
|
|
|
|
int nw = 0;
|
|
|
|
const char *fields[] = { "header", "avatar", "name", "bio", "metadata", NULL };
|
|
|
|
|
|
|
|
for (int n = 0; fields[n]; n++) {
|
|
|
|
const char *of = xs_dict_get(old, fields[n]);
|
|
|
|
const char *nf = xs_dict_get(snac->config, fields[n]);
|
|
|
|
|
2024-11-24 08:37:59 +00:00
|
|
|
if (of == NULL && nf == NULL)
|
|
|
|
continue;
|
|
|
|
|
2024-11-23 16:33:49 +00:00
|
|
|
if (xs_type(of) != XSTYPE_STRING || xs_type(nf) != XSTYPE_STRING || strcmp(of, nf)) {
|
|
|
|
nw = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!nw)
|
|
|
|
publish = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-27 10:24:17 +00:00
|
|
|
rename(fn, bfn);
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "w")) != NULL) {
|
|
|
|
xs_json_dump(snac->config, 4, f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rename(bfn, fn);
|
|
|
|
|
|
|
|
history_del(snac, "timeline.html_");
|
2024-10-13 04:48:29 +00:00
|
|
|
timeline_touch(snac);
|
2024-05-27 10:24:17 +00:00
|
|
|
|
2024-05-31 15:51:33 +00:00
|
|
|
if (publish) {
|
|
|
|
xs *a_msg = msg_actor(snac);
|
|
|
|
xs *u_msg = msg_update(snac, a_msg);
|
|
|
|
|
|
|
|
enqueue_message(snac, u_msg);
|
2024-05-27 10:24:17 +00:00
|
|
|
|
2024-05-31 20:39:10 +00:00
|
|
|
enqueue_verify_links(snac);
|
|
|
|
}
|
2024-05-27 10:24:17 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-04-16 05:43:41 +00:00
|
|
|
|
2022-11-25 12:33:13 +00:00
|
|
|
double mtime_nl(const char *fn, int *n_link)
|
|
|
|
/* returns the mtime and number of links of a file or directory, or 0.0 */
|
2022-09-20 07:39:28 +00:00
|
|
|
{
|
|
|
|
struct stat st;
|
2022-09-30 07:59:13 +00:00
|
|
|
double r = 0.0;
|
2022-11-25 12:33:13 +00:00
|
|
|
int n = 0;
|
2022-09-20 07:39:28 +00:00
|
|
|
|
2022-11-25 12:33:13 +00:00
|
|
|
if (fn && stat(fn, &st) != -1) {
|
|
|
|
r = (double) st.st_mtim.tv_sec;
|
|
|
|
n = st.st_nlink;
|
|
|
|
}
|
|
|
|
|
2022-11-25 12:54:26 +00:00
|
|
|
if (n_link)
|
2022-11-25 12:33:13 +00:00
|
|
|
*n_link = n;
|
2022-09-20 07:39:28 +00:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-13 07:46:05 +00:00
|
|
|
#define MIN(v1, v2) ((v1) < (v2) ? (v1) : (v2))
|
|
|
|
|
2023-04-12 07:43:23 +00:00
|
|
|
double f_ctime(const char *fn)
|
|
|
|
/* returns the ctime of a file or directory, or 0.0 */
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
double r = 0.0;
|
|
|
|
|
2023-04-13 07:46:05 +00:00
|
|
|
if (fn && stat(fn, &st) != -1) {
|
|
|
|
/* return the lowest of ctime and mtime;
|
|
|
|
there are operations that change the ctime, like link() */
|
|
|
|
r = (double) MIN(st.st_ctim.tv_sec, st.st_mtim.tv_sec);
|
|
|
|
}
|
2023-04-12 07:43:23 +00:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-04-30 07:27:33 +00:00
|
|
|
int is_md5_hex(const char *md5)
|
|
|
|
{
|
2024-07-23 07:59:48 +00:00
|
|
|
return xs_is_hex(md5) && strlen(md5) == MD5_HEX_SIZE - 1;
|
2024-04-30 07:27:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-24 07:00:35 +00:00
|
|
|
/** database 2.1+ **/
|
2022-11-23 13:38:37 +00:00
|
|
|
|
2022-11-25 16:48:43 +00:00
|
|
|
/** indexes **/
|
|
|
|
|
2023-02-22 18:09:05 +00:00
|
|
|
|
2022-11-24 07:09:57 +00:00
|
|
|
int index_add_md5(const char *fn, const char *md5)
|
2022-11-24 07:00:35 +00:00
|
|
|
/* adds an md5 to an index */
|
2022-11-23 17:58:07 +00:00
|
|
|
{
|
2024-05-26 19:45:41 +00:00
|
|
|
int status = HTTP_STATUS_CREATED;
|
2023-02-22 20:52:24 +00:00
|
|
|
FILE *f;
|
2022-11-23 17:58:07 +00:00
|
|
|
|
2024-04-30 07:27:33 +00:00
|
|
|
if (!is_md5_hex(md5)) {
|
|
|
|
srv_log(xs_fmt("index_add_md5: bad md5 %s %s", fn, md5));
|
2024-05-26 19:45:41 +00:00
|
|
|
return HTTP_STATUS_BAD_REQUEST;
|
2024-04-30 07:27:33 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 08:32:47 +00:00
|
|
|
pthread_mutex_lock(&data_mutex);
|
|
|
|
|
2022-11-23 17:58:07 +00:00
|
|
|
if ((f = fopen(fn, "a")) != NULL) {
|
|
|
|
flock(fileno(f), LOCK_EX);
|
|
|
|
|
2022-11-26 18:19:52 +00:00
|
|
|
/* ensure the position is at the end after getting the lock */
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
|
|
|
2022-11-23 17:58:07 +00:00
|
|
|
fprintf(f, "%s\n", md5);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
else
|
2024-05-26 19:45:41 +00:00
|
|
|
status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
|
2022-11-23 17:58:07 +00:00
|
|
|
|
2023-02-23 08:32:47 +00:00
|
|
|
pthread_mutex_unlock(&data_mutex);
|
|
|
|
|
2022-11-23 17:58:07 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-24 07:09:57 +00:00
|
|
|
int index_add(const char *fn, const char *id)
|
|
|
|
/* adds an id to an index */
|
|
|
|
{
|
|
|
|
xs *md5 = xs_md5_hex(id, strlen(id));
|
|
|
|
return index_add_md5(fn, md5);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-01 15:02:44 +00:00
|
|
|
int index_del_md5(const char *fn, const char *md5)
|
|
|
|
/* deletes an md5 from an index */
|
|
|
|
{
|
2024-05-26 19:45:41 +00:00
|
|
|
int status = HTTP_STATUS_NOT_FOUND;
|
2023-05-01 15:02:44 +00:00
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&data_mutex);
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "r+")) != NULL) {
|
|
|
|
char line[256];
|
|
|
|
|
|
|
|
while (fgets(line, sizeof(line), f) != NULL) {
|
2024-07-23 07:59:48 +00:00
|
|
|
line[MD5_HEX_SIZE - 1] = '\0';
|
2023-05-01 15:02:44 +00:00
|
|
|
|
|
|
|
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()] */
|
2024-07-23 07:59:48 +00:00
|
|
|
fseek(f, -MD5_HEX_SIZE, SEEK_CUR);
|
2023-05-01 15:02:44 +00:00
|
|
|
fwrite("-", 1, 1, f);
|
2024-05-26 19:45:41 +00:00
|
|
|
status = HTTP_STATUS_OK;
|
2023-05-01 15:02:44 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
else
|
2024-05-26 19:45:41 +00:00
|
|
|
status = HTTP_STATUS_GONE;
|
2023-05-01 15:02:44 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-23 09:42:09 +00:00
|
|
|
int index_gc(const char *fn)
|
|
|
|
/* garbage-collects an index, deleting objects that are not here */
|
|
|
|
{
|
|
|
|
FILE *i, *o;
|
|
|
|
int gc = -1;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&data_mutex);
|
|
|
|
|
|
|
|
if ((i = fopen(fn, "r")) != NULL) {
|
|
|
|
xs *nfn = xs_fmt("%s.new", fn);
|
|
|
|
char line[256];
|
|
|
|
|
|
|
|
if ((o = fopen(nfn, "w")) != NULL) {
|
|
|
|
gc = 0;
|
|
|
|
|
|
|
|
while (fgets(line, sizeof(line), i) != NULL) {
|
2024-07-23 07:59:48 +00:00
|
|
|
line[MD5_HEX_SIZE - 1] = '\0';
|
2023-02-23 09:42:09 +00:00
|
|
|
|
2023-06-18 17:09:37 +00:00
|
|
|
if (line[0] != '-' && object_here_by_md5(line))
|
2023-02-23 09:42:09 +00:00
|
|
|
fprintf(o, "%s\n", line);
|
|
|
|
else
|
|
|
|
gc++;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(o);
|
|
|
|
|
|
|
|
xs *ofn = xs_fmt("%s.bak", fn);
|
|
|
|
|
|
|
|
unlink(ofn);
|
|
|
|
link(fn, ofn);
|
|
|
|
rename(nfn, fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&data_mutex);
|
|
|
|
|
|
|
|
return gc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-24 12:06:28 +00:00
|
|
|
int index_in_md5(const char *fn, const char *md5)
|
|
|
|
/* checks if the md5 is already in the index */
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
|
|
|
flock(fileno(f), LOCK_SH);
|
|
|
|
|
|
|
|
char line[256];
|
|
|
|
|
|
|
|
while (!ret && fgets(line, sizeof(line), f) != NULL) {
|
2024-07-23 07:59:48 +00:00
|
|
|
line[MD5_HEX_SIZE - 1] = '\0';
|
2022-11-24 12:06:28 +00:00
|
|
|
|
|
|
|
if (strcmp(line, md5) == 0)
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int index_in(const char *fn, const char *id)
|
|
|
|
/* checks if the object id is already in the index */
|
|
|
|
{
|
|
|
|
xs *md5 = xs_md5_hex(id, strlen(id));
|
|
|
|
return index_in_md5(fn, md5);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-23 08:09:12 +00:00
|
|
|
int index_first(const char *fn, char md5[MD5_HEX_SIZE])
|
2022-11-26 16:35:18 +00:00
|
|
|
/* reads the first entry of an index */
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
2024-07-23 08:09:12 +00:00
|
|
|
if (fread(md5, MD5_HEX_SIZE, 1, f)) {
|
|
|
|
md5[MD5_HEX_SIZE - 1] = '\0';
|
2022-11-26 16:35:18 +00:00
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-10 10:19:26 +00:00
|
|
|
int index_len(const char *fn)
|
|
|
|
/* returns the number of elements in an index */
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
if (stat(fn, &st) != -1)
|
2024-07-23 07:59:48 +00:00
|
|
|
len = st.st_size / MD5_HEX_SIZE;
|
2022-12-10 10:19:26 +00:00
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-24 11:05:43 +00:00
|
|
|
xs_list *index_list(const char *fn, int max)
|
2022-11-24 07:00:35 +00:00
|
|
|
/* returns an index as a list */
|
2022-11-23 18:32:26 +00:00
|
|
|
{
|
2023-07-05 12:41:47 +00:00
|
|
|
xs_list *list = xs_list_new();
|
2022-11-23 18:32:26 +00:00
|
|
|
FILE *f;
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
|
|
|
flock(fileno(f), LOCK_SH);
|
|
|
|
|
2022-11-23 19:25:57 +00:00
|
|
|
char line[256];
|
2022-11-23 18:32:26 +00:00
|
|
|
|
|
|
|
while (n < max && fgets(line, sizeof(line), f) != NULL) {
|
2023-07-04 14:33:54 +00:00
|
|
|
if (line[0] != '-') {
|
2024-07-23 07:59:48 +00:00
|
|
|
line[MD5_HEX_SIZE - 1] = '\0';
|
2023-07-04 14:33:54 +00:00
|
|
|
list = xs_list_append(list, line);
|
|
|
|
n++;
|
|
|
|
}
|
2022-11-23 18:32:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-23 07:59:48 +00:00
|
|
|
int index_desc_next(FILE *f, char md5[MD5_HEX_SIZE])
|
2024-07-23 07:34:53 +00:00
|
|
|
/* reads the next entry of a desc index */
|
|
|
|
{
|
|
|
|
for (;;) {
|
|
|
|
/* move backwards 2 entries */
|
2024-07-23 07:59:48 +00:00
|
|
|
if (fseek(f, MD5_HEX_SIZE * -2, SEEK_CUR) == -1)
|
2024-07-23 07:34:53 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* read and md5 */
|
2024-07-23 07:59:48 +00:00
|
|
|
if (!fread(md5, MD5_HEX_SIZE, 1, f))
|
2024-07-23 07:34:53 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (md5[0] != '-')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-07-23 07:59:48 +00:00
|
|
|
md5[MD5_HEX_SIZE - 1] = '\0';
|
2024-07-23 07:34:53 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-23 07:59:48 +00:00
|
|
|
int index_desc_first(FILE *f, char md5[MD5_HEX_SIZE], int skip)
|
2024-07-23 07:34:53 +00:00
|
|
|
/* reads the first entry of a desc index */
|
|
|
|
{
|
|
|
|
/* try to position at the end and then back to the first element */
|
2024-07-23 07:59:48 +00:00
|
|
|
if (fseek(f, 0, SEEK_END) || fseek(f, (skip + 1) * -MD5_HEX_SIZE, SEEK_CUR))
|
2024-07-23 07:34:53 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* try to read an md5 */
|
2024-07-23 07:59:48 +00:00
|
|
|
if (!fread(md5, MD5_HEX_SIZE, 1, f))
|
2024-07-23 07:34:53 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* null-terminate */
|
2024-07-23 07:59:48 +00:00
|
|
|
md5[MD5_HEX_SIZE - 1] = '\0';
|
2024-07-23 07:34:53 +00:00
|
|
|
|
|
|
|
/* deleted? retry next */
|
|
|
|
if (md5[0] == '-')
|
|
|
|
return index_desc_next(f, md5);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-24 11:05:43 +00:00
|
|
|
xs_list *index_list_desc(const char *fn, int skip, int show)
|
2022-11-24 07:00:35 +00:00
|
|
|
/* returns an index as a list, in reverse order */
|
2022-11-23 18:32:26 +00:00
|
|
|
{
|
2023-07-05 12:41:47 +00:00
|
|
|
xs_list *list = xs_list_new();
|
2022-11-23 18:32:26 +00:00
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
2024-07-23 07:59:48 +00:00
|
|
|
char md5[MD5_HEX_SIZE];
|
2022-11-23 18:32:26 +00:00
|
|
|
|
2024-07-23 07:50:56 +00:00
|
|
|
if (index_desc_first(f, md5, skip)) {
|
|
|
|
int n = 1;
|
2022-11-23 18:32:26 +00:00
|
|
|
|
2024-07-23 07:50:56 +00:00
|
|
|
do {
|
|
|
|
list = xs_list_append(list, md5);
|
|
|
|
} while (n++ < show && index_desc_next(f, md5));
|
2022-11-23 18:32:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-25 16:48:43 +00:00
|
|
|
/** objects **/
|
|
|
|
|
2023-06-18 17:53:33 +00:00
|
|
|
static xs_str *_object_fn_by_md5(const char *md5, const char *func)
|
2022-11-24 07:00:35 +00:00
|
|
|
{
|
2023-06-18 18:08:51 +00:00
|
|
|
xs *bfn = xs_fmt("%s/object/%c%c", srv_basedir, md5[0], md5[1]);
|
2023-06-18 18:38:35 +00:00
|
|
|
xs_str *ret;
|
2023-06-18 18:08:51 +00:00
|
|
|
int ok = 1;
|
2023-06-18 17:53:33 +00:00
|
|
|
|
2023-06-18 18:08:51 +00:00
|
|
|
/* an object deleted from an index; fail but don't bark */
|
|
|
|
if (md5[0] == '-')
|
|
|
|
ok = 0;
|
|
|
|
else
|
2024-04-30 07:27:33 +00:00
|
|
|
if (!is_md5_hex(md5)) {
|
2023-06-18 17:53:33 +00:00
|
|
|
srv_log(xs_fmt("_object_fn_by_md5() [from %s()]: bad md5 '%s'", func, md5));
|
2023-06-18 18:08:51 +00:00
|
|
|
ok = 0;
|
2023-06-16 08:04:29 +00:00
|
|
|
}
|
2023-06-15 04:15:39 +00:00
|
|
|
|
2023-06-18 18:29:50 +00:00
|
|
|
if (ok) {
|
2023-06-18 18:08:51 +00:00
|
|
|
mkdirx(bfn);
|
2023-06-18 18:29:50 +00:00
|
|
|
ret = xs_fmt("%s/%s.json", bfn, md5);
|
2023-06-18 18:08:51 +00:00
|
|
|
}
|
2023-06-18 18:29:50 +00:00
|
|
|
else
|
|
|
|
ret = xs_fmt("%s/object/invalid/invalid.json", srv_basedir);
|
2022-11-24 07:00:35 +00:00
|
|
|
|
2023-06-18 18:29:50 +00:00
|
|
|
return ret;
|
2022-11-24 07:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-18 18:35:16 +00:00
|
|
|
static xs_str *_object_fn(const char *id)
|
2022-11-24 07:00:35 +00:00
|
|
|
{
|
|
|
|
xs *md5 = xs_md5_hex(id, strlen(id));
|
2023-06-18 18:35:16 +00:00
|
|
|
return _object_fn_by_md5(md5, "_object_fn");
|
2022-11-24 07:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-07 09:08:14 +00:00
|
|
|
int object_here_by_md5(const char *id)
|
2022-12-02 20:19:46 +00:00
|
|
|
/* checks if an object is already downloaded */
|
|
|
|
{
|
2023-06-18 17:53:33 +00:00
|
|
|
xs *fn = _object_fn_by_md5(id, "object_here_by_md5");
|
2023-06-18 18:08:51 +00:00
|
|
|
return mtime(fn) > 0.0;
|
2022-12-02 20:19:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-07 09:08:14 +00:00
|
|
|
int object_here(const char *id)
|
2022-12-02 19:44:51 +00:00
|
|
|
/* checks if an object is already downloaded */
|
|
|
|
{
|
2023-06-18 18:35:16 +00:00
|
|
|
xs *fn = _object_fn(id);
|
2022-12-02 19:44:51 +00:00
|
|
|
return mtime(fn) > 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-05 16:45:00 +00:00
|
|
|
int object_get_by_md5(const char *md5, xs_dict **obj)
|
2022-11-24 11:38:31 +00:00
|
|
|
/* returns a stored object, optionally of the requested type */
|
2022-11-24 07:00:35 +00:00
|
|
|
{
|
2024-05-26 19:45:41 +00:00
|
|
|
int status = HTTP_STATUS_NOT_FOUND;
|
2023-08-14 18:11:03 +00:00
|
|
|
xs *fn = _object_fn_by_md5(md5, "object_get_by_md5");
|
2022-11-24 07:00:35 +00:00
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
2023-08-05 12:56:07 +00:00
|
|
|
*obj = xs_json_load(f);
|
2022-11-24 07:00:35 +00:00
|
|
|
fclose(f);
|
|
|
|
|
2023-02-05 16:45:00 +00:00
|
|
|
if (*obj)
|
2024-05-26 19:45:41 +00:00
|
|
|
status = HTTP_STATUS_OK;
|
2022-11-24 07:00:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
*obj = NULL;
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-05 16:45:00 +00:00
|
|
|
int object_get(const char *id, xs_dict **obj)
|
2022-11-24 11:38:31 +00:00
|
|
|
/* returns a stored object, optionally of the requested type */
|
2022-11-24 07:00:35 +00:00
|
|
|
{
|
|
|
|
xs *md5 = xs_md5_hex(id, strlen(id));
|
2023-02-05 16:45:00 +00:00
|
|
|
return object_get_by_md5(md5, obj);
|
2022-11-24 07:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-07 09:08:14 +00:00
|
|
|
int _object_add(const char *id, const xs_dict *obj, int ow)
|
2022-11-24 07:00:35 +00:00
|
|
|
/* stores an object */
|
|
|
|
{
|
2024-05-26 19:45:41 +00:00
|
|
|
int status = HTTP_STATUS_CREATED; /* Created */
|
2023-06-18 18:35:16 +00:00
|
|
|
xs *fn = _object_fn(id);
|
2022-11-24 07:00:35 +00:00
|
|
|
FILE *f;
|
|
|
|
|
2023-12-11 09:41:41 +00:00
|
|
|
if (mtime(fn) > 0.0) {
|
|
|
|
if (!ow) {
|
|
|
|
/* object already here */
|
|
|
|
srv_debug(1, xs_fmt("object_add object already here %s", id));
|
2024-05-26 19:45:41 +00:00
|
|
|
return HTTP_STATUS_NO_CONTENT;
|
2023-12-11 09:41:41 +00:00
|
|
|
}
|
|
|
|
else
|
2024-05-26 19:45:41 +00:00
|
|
|
status = HTTP_STATUS_OK;
|
2022-11-24 07:39:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 07:00:35 +00:00
|
|
|
if ((f = fopen(fn, "w")) != NULL) {
|
|
|
|
flock(fileno(f), LOCK_EX);
|
|
|
|
|
2023-08-08 17:29:34 +00:00
|
|
|
xs_json_dump(obj, 4, f);
|
2022-11-24 07:00:35 +00:00
|
|
|
fclose(f);
|
2022-11-24 07:09:57 +00:00
|
|
|
|
|
|
|
/* does this object has a parent? */
|
2024-11-19 05:59:53 +00:00
|
|
|
const char *in_reply_to = get_in_reply_to(obj);
|
2022-11-24 07:09:57 +00:00
|
|
|
|
2022-11-24 07:18:51 +00:00
|
|
|
if (!xs_is_null(in_reply_to) && *in_reply_to) {
|
2022-11-24 07:09:57 +00:00
|
|
|
/* update the children index of the parent */
|
2023-06-18 18:35:16 +00:00
|
|
|
xs *c_idx = _object_fn(in_reply_to);
|
2022-11-24 07:09:57 +00:00
|
|
|
|
2022-11-26 04:29:09 +00:00
|
|
|
c_idx = xs_replace_i(c_idx, ".json", "_c.idx");
|
2022-11-24 08:15:06 +00:00
|
|
|
|
2022-12-03 16:58:49 +00:00
|
|
|
if (!index_in(c_idx, id)) {
|
|
|
|
index_add(c_idx, id);
|
2022-12-04 05:40:17 +00:00
|
|
|
srv_debug(1, xs_fmt("object_add added child %s to %s", id, c_idx));
|
2022-12-03 16:58:49 +00:00
|
|
|
}
|
|
|
|
else
|
2022-12-04 05:40:17 +00:00
|
|
|
srv_debug(1, xs_fmt("object_add %s child already in %s", id, c_idx));
|
2022-11-26 04:29:09 +00:00
|
|
|
|
|
|
|
/* create a one-element index with the parent */
|
|
|
|
xs *p_idx = xs_replace(fn, ".json", "_p.idx");
|
2022-11-26 04:46:21 +00:00
|
|
|
|
2022-12-15 09:52:09 +00:00
|
|
|
if (mtime(p_idx) == 0.0) {
|
|
|
|
index_add(p_idx, in_reply_to);
|
|
|
|
srv_debug(1, xs_fmt("object_add added parent %s to %s", in_reply_to, p_idx));
|
|
|
|
}
|
2022-11-24 07:09:57 +00:00
|
|
|
}
|
2022-11-24 07:00:35 +00:00
|
|
|
}
|
2022-12-18 16:51:23 +00:00
|
|
|
else {
|
|
|
|
srv_log(xs_fmt("object_add error writing %s (errno: %d)", fn, errno));
|
2024-05-26 19:45:41 +00:00
|
|
|
status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
|
2022-12-18 16:51:23 +00:00
|
|
|
}
|
2022-11-24 07:00:35 +00:00
|
|
|
|
2022-12-04 05:40:17 +00:00
|
|
|
srv_debug(1, xs_fmt("object_add %s %s %d", id, fn, status));
|
2022-11-24 07:00:35 +00:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-07 09:08:14 +00:00
|
|
|
int object_add(const char *id, const xs_dict *obj)
|
2022-11-26 04:18:48 +00:00
|
|
|
/* stores an object */
|
|
|
|
{
|
|
|
|
return _object_add(id, obj, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-07 09:08:14 +00:00
|
|
|
int object_add_ow(const char *id, const xs_dict *obj)
|
2022-11-26 04:18:48 +00:00
|
|
|
/* stores an object (overwriting allowed) */
|
|
|
|
{
|
|
|
|
return _object_add(id, obj, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-26 04:05:57 +00:00
|
|
|
int object_del_by_md5(const char *md5)
|
|
|
|
/* deletes an object by its md5 */
|
2022-11-24 07:00:35 +00:00
|
|
|
{
|
2024-05-26 19:45:41 +00:00
|
|
|
int status = HTTP_STATUS_NOT_FOUND;
|
2023-06-18 17:53:33 +00:00
|
|
|
xs *fn = _object_fn_by_md5(md5, "object_del_by_md5");
|
2022-11-24 07:00:35 +00:00
|
|
|
|
2023-02-22 17:15:48 +00:00
|
|
|
if (unlink(fn) != -1) {
|
2024-05-26 19:45:41 +00:00
|
|
|
status = HTTP_STATUS_OK;
|
2022-11-24 07:00:35 +00:00
|
|
|
|
2022-11-24 07:36:01 +00:00
|
|
|
/* also delete associated indexes */
|
2022-11-26 04:05:57 +00:00
|
|
|
xs *spec = xs_dup(fn);
|
|
|
|
spec = xs_replace_i(spec, ".json", "*.idx");
|
2022-11-24 07:36:01 +00:00
|
|
|
xs *files = xs_glob(spec, 0, 0);
|
2024-05-23 08:01:37 +00:00
|
|
|
char *p;
|
|
|
|
const char *v;
|
2022-11-24 07:36:01 +00:00
|
|
|
|
|
|
|
p = files;
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
2022-12-04 05:40:17 +00:00
|
|
|
srv_debug(1, xs_fmt("object_del index %s", v));
|
2022-11-24 07:36:01 +00:00
|
|
|
unlink(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 05:40:17 +00:00
|
|
|
srv_debug(1, xs_fmt("object_del %s %d", fn, status));
|
2022-11-24 07:00:35 +00:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-26 04:05:57 +00:00
|
|
|
int object_del(const char *id)
|
|
|
|
/* deletes an object */
|
|
|
|
{
|
|
|
|
xs *md5 = xs_md5_hex(id, strlen(id));
|
|
|
|
return object_del_by_md5(md5);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-27 08:45:06 +00:00
|
|
|
int object_del_if_unref(const char *id)
|
|
|
|
/* deletes an object if its n_links < 2 */
|
|
|
|
{
|
2023-06-18 18:35:16 +00:00
|
|
|
xs *fn = _object_fn(id);
|
2022-11-27 08:45:06 +00:00
|
|
|
int n_links;
|
|
|
|
int ret = 0;
|
|
|
|
|
2023-06-18 18:08:51 +00:00
|
|
|
if (mtime_nl(fn, &n_links) > 0.0 && n_links < 2)
|
2022-11-27 08:45:06 +00:00
|
|
|
ret = object_del(id);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-12 07:46:42 +00:00
|
|
|
double object_ctime_by_md5(const char *md5)
|
|
|
|
{
|
2023-06-18 17:53:33 +00:00
|
|
|
xs *fn = _object_fn_by_md5(md5, "object_ctime_by_md5");
|
2023-06-18 18:08:51 +00:00
|
|
|
return f_ctime(fn);
|
2023-04-12 07:46:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double object_ctime(const char *id)
|
|
|
|
{
|
|
|
|
xs *md5 = xs_md5_hex(id, strlen(id));
|
|
|
|
return object_ctime_by_md5(md5);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-03-12 16:54:54 +00:00
|
|
|
double object_mtime_by_md5(const char *md5)
|
|
|
|
{
|
|
|
|
xs *fn = _object_fn_by_md5(md5, "object_mtime_by_md5");
|
|
|
|
return mtime(fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
double object_mtime(const char *id)
|
|
|
|
{
|
|
|
|
xs *md5 = xs_md5_hex(id, strlen(id));
|
|
|
|
return object_mtime_by_md5(md5);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-03-12 17:04:09 +00:00
|
|
|
void object_touch(const char *id)
|
|
|
|
{
|
|
|
|
xs *md5 = xs_md5_hex(id, strlen(id));
|
|
|
|
xs *fn = _object_fn_by_md5(md5, "object_touch");
|
|
|
|
|
|
|
|
if (mtime(fn))
|
|
|
|
utimes(fn, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-07 09:08:14 +00:00
|
|
|
xs_str *_object_index_fn(const char *id, const char *idxsfx)
|
2022-12-10 10:19:26 +00:00
|
|
|
/* returns the filename of an object's index */
|
2022-12-02 18:14:59 +00:00
|
|
|
{
|
2023-06-18 18:35:16 +00:00
|
|
|
xs_str *fn = _object_fn(id);
|
2022-12-10 10:19:26 +00:00
|
|
|
return xs_replace_i(fn, ".json", idxsfx);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int object_likes_len(const char *id)
|
|
|
|
/* returns the number of likes (without reading the index) */
|
|
|
|
{
|
|
|
|
xs *fn = _object_index_fn(id, "_l.idx");
|
|
|
|
return index_len(fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int object_announces_len(const char *id)
|
|
|
|
/* returns the number of announces (without reading the index) */
|
|
|
|
{
|
|
|
|
xs *fn = _object_index_fn(id, "_a.idx");
|
|
|
|
return index_len(fn);
|
2022-12-02 18:14:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-24 11:05:43 +00:00
|
|
|
xs_list *object_children(const char *id)
|
2022-11-24 08:58:37 +00:00
|
|
|
/* returns the list of an object's children */
|
|
|
|
{
|
2022-12-10 10:19:26 +00:00
|
|
|
xs *fn = _object_index_fn(id, "_c.idx");
|
|
|
|
return index_list(fn, XS_ALL);
|
2022-12-02 18:14:59 +00:00
|
|
|
}
|
2022-11-24 08:58:37 +00:00
|
|
|
|
|
|
|
|
2023-05-24 11:05:43 +00:00
|
|
|
xs_list *object_likes(const char *id)
|
2022-12-02 18:14:59 +00:00
|
|
|
{
|
2022-12-10 10:19:26 +00:00
|
|
|
xs *fn = _object_index_fn(id, "_l.idx");
|
|
|
|
return index_list(fn, XS_ALL);
|
2022-12-02 18:14:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-24 11:05:43 +00:00
|
|
|
xs_list *object_announces(const char *id)
|
2022-12-02 18:14:59 +00:00
|
|
|
{
|
2022-12-10 10:19:26 +00:00
|
|
|
xs *fn = _object_index_fn(id, "_a.idx");
|
|
|
|
return index_list(fn, XS_ALL);
|
2022-11-24 08:58:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-23 08:09:12 +00:00
|
|
|
int object_parent(const char *md5, char parent[MD5_HEX_SIZE])
|
2022-12-02 20:16:34 +00:00
|
|
|
/* returns the object parent, if any */
|
|
|
|
{
|
2023-06-18 17:53:33 +00:00
|
|
|
xs *fn = _object_fn_by_md5(md5, "object_parent");
|
2023-06-16 08:04:29 +00:00
|
|
|
|
2022-12-02 20:16:34 +00:00
|
|
|
fn = xs_replace_i(fn, ".json", "_p.idx");
|
2024-07-23 08:09:12 +00:00
|
|
|
return index_first(fn, parent);
|
2022-12-02 20:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-24 12:00:24 +00:00
|
|
|
int object_admire(const char *id, const char *actor, int like)
|
|
|
|
/* actor likes or announces this object */
|
|
|
|
{
|
2024-05-26 19:45:41 +00:00
|
|
|
int status = HTTP_STATUS_OK;
|
2023-06-18 18:35:16 +00:00
|
|
|
xs *fn = _object_fn(id);
|
2023-06-18 17:53:33 +00:00
|
|
|
|
2022-11-24 12:00:24 +00:00
|
|
|
fn = xs_replace_i(fn, ".json", like ? "_l.idx" : "_a.idx");
|
|
|
|
|
2022-11-24 12:15:32 +00:00
|
|
|
if (!index_in(fn, actor)) {
|
2022-11-24 12:10:03 +00:00
|
|
|
status = index_add(fn, actor);
|
|
|
|
|
2022-12-04 05:40:17 +00:00
|
|
|
srv_debug(1, xs_fmt("object_admire (%s) %s %s", like ? "Like" : "Announce", actor, fn));
|
2022-11-24 12:15:32 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 12:10:03 +00:00
|
|
|
return status;
|
2022-11-24 12:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-01 15:02:44 +00:00
|
|
|
int object_unadmire(const char *id, const char *actor, int like)
|
|
|
|
/* actor no longer likes or announces this object */
|
|
|
|
{
|
|
|
|
int status;
|
2023-06-18 18:35:16 +00:00
|
|
|
xs *fn = _object_fn(id);
|
2023-05-01 15:02:44 +00:00
|
|
|
|
|
|
|
fn = xs_replace_i(fn, ".json", like ? "_l.idx" : "_a.idx");
|
|
|
|
|
|
|
|
status = index_del(fn, actor);
|
2024-04-11 17:55:45 +00:00
|
|
|
|
|
|
|
if (valid_status(status))
|
|
|
|
index_gc(fn);
|
2023-05-01 15:02:44 +00:00
|
|
|
|
2023-05-01 15:20:49 +00:00
|
|
|
srv_debug(0,
|
2023-05-01 15:02:44 +00:00
|
|
|
xs_fmt("object_unadmire (%s) %s %s %d", like ? "Like" : "Announce", actor, fn, status));
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-08-29 04:59:44 +00:00
|
|
|
xs_str *object_user_cache_fn_by_md5(snac *user, const char *md5, const char *cachedir)
|
|
|
|
{
|
|
|
|
return xs_fmt("%s/%s/%s.json", user->basedir, cachedir, md5);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-08-29 04:40:38 +00:00
|
|
|
xs_str *object_user_cache_fn(snac *user, const char *id, const char *cachedir)
|
|
|
|
{
|
|
|
|
xs *md5 = xs_md5_hex(id, strlen(id));
|
2024-08-29 04:59:44 +00:00
|
|
|
return object_user_cache_fn_by_md5(user, md5, cachedir);
|
2024-08-29 04:40:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-08-29 06:30:09 +00:00
|
|
|
xs_str *object_user_cache_index_fn(snac *user, const char *cachedir)
|
|
|
|
{
|
|
|
|
return xs_fmt("%s/%s.idx", user->basedir, cachedir);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-08-29 04:40:38 +00:00
|
|
|
int _object_user_cache(snac *user, const char *id, const char *cachedir, int del)
|
2022-11-26 04:51:48 +00:00
|
|
|
/* adds or deletes from a user cache */
|
2022-11-25 16:48:43 +00:00
|
|
|
{
|
2023-06-18 18:35:16 +00:00
|
|
|
xs *ofn = _object_fn(id);
|
2024-08-29 04:40:38 +00:00
|
|
|
xs *cfn = object_user_cache_fn(user, id, cachedir);
|
2024-08-29 06:30:09 +00:00
|
|
|
xs *idx = object_user_cache_index_fn(user, cachedir);
|
2022-11-26 04:58:13 +00:00
|
|
|
int ret;
|
2022-11-25 16:48:43 +00:00
|
|
|
|
2022-11-26 04:58:13 +00:00
|
|
|
if (del) {
|
2023-02-23 17:43:52 +00:00
|
|
|
ret = unlink(cfn);
|
2023-07-04 12:34:32 +00:00
|
|
|
index_del(idx, id);
|
2022-11-26 04:58:13 +00:00
|
|
|
}
|
|
|
|
else {
|
2024-08-29 04:47:55 +00:00
|
|
|
/* create the subfolder, if it does not exist */
|
|
|
|
xs *dir = xs_fmt("%s/%s/", user->basedir, cachedir);
|
|
|
|
mkdirx(dir);
|
|
|
|
|
2022-12-03 20:59:19 +00:00
|
|
|
if ((ret = link(ofn, cfn)) != -1)
|
|
|
|
index_add(idx, id);
|
2022-11-26 04:58:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2022-11-26 04:51:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-08-29 04:40:38 +00:00
|
|
|
int object_user_cache_add(snac *user, const char *id, const char *cachedir)
|
2022-11-26 04:51:48 +00:00
|
|
|
/* caches an object into a user cache */
|
|
|
|
{
|
2024-08-29 04:40:38 +00:00
|
|
|
return _object_user_cache(user, id, cachedir, 0);
|
2022-11-26 04:51:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-08-29 04:40:38 +00:00
|
|
|
int object_user_cache_del(snac *user, const char *id, const char *cachedir)
|
2022-11-26 04:51:48 +00:00
|
|
|
/* deletes an object from a user cache */
|
|
|
|
{
|
2024-08-29 04:40:38 +00:00
|
|
|
return _object_user_cache(user, id, cachedir, 1);
|
2022-11-25 16:48:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-08-29 04:40:38 +00:00
|
|
|
int object_user_cache_in(snac *user, const char *id, const char *cachedir)
|
2022-11-28 07:34:19 +00:00
|
|
|
/* checks if an object is stored in a cache */
|
|
|
|
{
|
2024-08-29 04:40:38 +00:00
|
|
|
xs *cfn = object_user_cache_fn(user, id, cachedir);
|
2022-11-28 07:34:19 +00:00
|
|
|
return !!(mtime(cfn) != 0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-08-29 04:59:44 +00:00
|
|
|
int object_user_cache_in_by_md5(snac *user, const char *md5, const char *cachedir)
|
|
|
|
/* checks if an object is stored in a cache */
|
|
|
|
{
|
|
|
|
xs *cfn = object_user_cache_fn_by_md5(user, md5, cachedir);
|
|
|
|
return !!(mtime(cfn) != 0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-08-29 04:40:38 +00:00
|
|
|
xs_list *object_user_cache_list(snac *user, const char *cachedir, int max, int inv)
|
2022-11-28 09:46:42 +00:00
|
|
|
/* returns the objects in a cache as a list */
|
2022-09-20 07:39:28 +00:00
|
|
|
{
|
2024-08-29 04:40:38 +00:00
|
|
|
xs *idx = xs_fmt("%s/%s.idx", user->basedir, cachedir);
|
2023-07-05 12:06:21 +00:00
|
|
|
return inv ? index_list_desc(idx, 0, max) : index_list(idx, max);
|
2022-09-20 07:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-28 09:46:42 +00:00
|
|
|
/** specialized functions **/
|
2022-09-20 07:39:28 +00:00
|
|
|
|
2022-11-28 09:46:42 +00:00
|
|
|
/** followers **/
|
2022-09-20 07:39:28 +00:00
|
|
|
|
2022-11-28 09:46:42 +00:00
|
|
|
int follower_add(snac *snac, const char *actor)
|
|
|
|
/* adds a follower */
|
|
|
|
{
|
2022-11-28 10:24:19 +00:00
|
|
|
int ret = object_user_cache_add(snac, actor, "followers");
|
2022-09-20 07:39:28 +00:00
|
|
|
|
2022-12-04 09:23:48 +00:00
|
|
|
snac_debug(snac, 2, xs_fmt("follower_add %s", actor));
|
2022-09-20 07:39:28 +00:00
|
|
|
|
2024-05-26 19:45:41 +00:00
|
|
|
return ret == -1 ? HTTP_STATUS_INTERNAL_SERVER_ERROR : HTTP_STATUS_OK;
|
2022-09-20 07:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-28 09:46:42 +00:00
|
|
|
int follower_del(snac *snac, const char *actor)
|
2022-09-20 07:39:28 +00:00
|
|
|
/* deletes a follower */
|
|
|
|
{
|
2022-11-28 10:24:19 +00:00
|
|
|
int ret = object_user_cache_del(snac, actor, "followers");
|
2022-09-20 07:39:28 +00:00
|
|
|
|
2022-12-13 15:02:41 +00:00
|
|
|
snac_debug(snac, 2, xs_fmt("follower_del %s", actor));
|
2022-09-20 07:39:28 +00:00
|
|
|
|
2024-05-26 19:45:41 +00:00
|
|
|
return ret == -1 ? HTTP_STATUS_NOT_FOUND : HTTP_STATUS_OK;
|
2022-09-20 07:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-28 09:46:42 +00:00
|
|
|
int follower_check(snac *snac, const char *actor)
|
2022-09-20 07:39:28 +00:00
|
|
|
/* checks if someone is a follower */
|
|
|
|
{
|
2022-11-28 09:46:42 +00:00
|
|
|
return object_user_cache_in(snac, actor, "followers");
|
2022-09-20 07:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-07 09:08:14 +00:00
|
|
|
xs_list *follower_list(snac *snac)
|
2022-09-20 07:39:28 +00:00
|
|
|
/* returns the list of followers */
|
|
|
|
{
|
2023-07-05 12:06:21 +00:00
|
|
|
xs *list = object_user_cache_list(snac, "followers", XS_ALL, 0);
|
2023-06-07 09:08:14 +00:00
|
|
|
xs_list *fwers = xs_list_new();
|
2024-05-23 08:01:37 +00:00
|
|
|
char *p;
|
|
|
|
const char *v;
|
2022-09-20 07:39:28 +00:00
|
|
|
|
2022-11-28 09:46:42 +00:00
|
|
|
/* resolve the list of md5 to be a list of actors */
|
|
|
|
p = list;
|
2022-10-03 08:56:15 +00:00
|
|
|
while (xs_list_iter(&p, &v)) {
|
2022-11-28 09:46:42 +00:00
|
|
|
xs *a_obj = NULL;
|
2022-09-20 07:39:28 +00:00
|
|
|
|
2023-02-05 16:45:00 +00:00
|
|
|
if (valid_status(object_get_by_md5(v, &a_obj))) {
|
2023-06-15 02:56:44 +00:00
|
|
|
const char *actor = xs_dict_get(a_obj, "id");
|
2022-09-20 07:48:13 +00:00
|
|
|
|
2023-06-15 02:56:44 +00:00
|
|
|
if (!xs_is_null(actor)) {
|
|
|
|
/* check if the actor is still cached */
|
|
|
|
xs *fn = xs_fmt("%s/followers/%s.json", snac->basedir, v);
|
|
|
|
|
|
|
|
if (mtime(fn) > 0.0)
|
|
|
|
fwers = xs_list_append(fwers, actor);
|
|
|
|
}
|
2022-09-20 07:39:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-28 09:46:42 +00:00
|
|
|
return fwers;
|
2022-09-20 07:39:28 +00:00
|
|
|
}
|
2022-09-20 08:02:00 +00:00
|
|
|
|
|
|
|
|
2024-11-24 07:17:38 +00:00
|
|
|
/** pending followers **/
|
|
|
|
|
|
|
|
int pending_add(snac *user, const char *actor, const xs_dict *msg)
|
|
|
|
/* stores the follow message for later confirmation */
|
|
|
|
{
|
|
|
|
xs *dir = xs_fmt("%s/pending", user->basedir);
|
|
|
|
xs *md5 = xs_md5_hex(actor, strlen(actor));
|
|
|
|
xs *fn = xs_fmt("%s/%s.json", dir, md5);
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
mkdirx(dir);
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "w")) == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
xs_json_dump(msg, 4, f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-11-24 07:37:19 +00:00
|
|
|
int pending_check(snac *user, const char *actor)
|
|
|
|
/* checks if there is a pending follow confirmation for the actor */
|
|
|
|
{
|
|
|
|
xs *md5 = xs_md5_hex(actor, strlen(actor));
|
|
|
|
xs *fn = xs_fmt("%s/pending/%s.json", user->basedir, md5);
|
|
|
|
|
|
|
|
return mtime(fn) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-11-24 07:31:01 +00:00
|
|
|
xs_dict *pending_get(snac *user, const char *actor)
|
|
|
|
/* returns the pending follow confirmation for the actor */
|
|
|
|
{
|
|
|
|
xs *md5 = xs_md5_hex(actor, strlen(actor));
|
|
|
|
xs *fn = xs_fmt("%s/pending/%s.json", user->basedir, md5);
|
|
|
|
xs_dict *msg = NULL;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
|
|
|
msg = xs_json_load(f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-11-24 07:37:19 +00:00
|
|
|
void pending_del(snac *user, const char *actor)
|
|
|
|
/* deletes a pending follow confirmation for the actor */
|
|
|
|
{
|
|
|
|
xs *md5 = xs_md5_hex(actor, strlen(actor));
|
|
|
|
xs *fn = xs_fmt("%s/pending/%s.json", user->basedir, md5);
|
|
|
|
|
|
|
|
unlink(fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-11-24 07:31:01 +00:00
|
|
|
xs_list *pending_list(snac *user)
|
|
|
|
/* returns a list of pending follow confirmations */
|
|
|
|
{
|
|
|
|
xs *spec = xs_fmt("%s/pending/""*.json", user->basedir);
|
|
|
|
xs *l = xs_glob(spec, 0, 0);
|
|
|
|
xs_list *r = xs_list_new();
|
|
|
|
const char *v;
|
|
|
|
|
|
|
|
xs_list_foreach(l, v) {
|
2024-11-24 07:54:43 +00:00
|
|
|
FILE *f;
|
|
|
|
xs *msg = NULL;
|
2024-11-24 07:31:01 +00:00
|
|
|
|
2024-11-24 07:54:43 +00:00
|
|
|
if ((f = fopen(v, "r")) == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
msg = xs_json_load(f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
if (msg == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const char *actor = xs_dict_get(msg, "actor");
|
|
|
|
|
|
|
|
if (xs_type(actor) == XSTYPE_STRING)
|
|
|
|
r = xs_list_append(r, actor);
|
2024-11-24 07:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-28 09:46:42 +00:00
|
|
|
/** timeline **/
|
|
|
|
|
2022-09-30 07:59:13 +00:00
|
|
|
double timeline_mtime(snac *snac)
|
2022-09-30 07:56:29 +00:00
|
|
|
{
|
2022-12-02 19:56:51 +00:00
|
|
|
xs *fn = xs_fmt("%s/private.idx", snac->basedir);
|
2022-09-30 07:56:29 +00:00
|
|
|
return mtime(fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-14 17:17:16 +00:00
|
|
|
int timeline_touch(snac *snac)
|
|
|
|
/* changes the date of the timeline index */
|
|
|
|
{
|
|
|
|
xs *fn = xs_fmt("%s/private.idx", snac->basedir);
|
|
|
|
return utimes(fn, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-08 12:06:49 +00:00
|
|
|
xs_str *timeline_fn_by_md5(snac *snac, const char *md5)
|
|
|
|
/* get the filename of an entry by md5 from any timeline */
|
|
|
|
{
|
2024-04-22 03:46:56 +00:00
|
|
|
xs_str *fn = NULL;
|
2023-02-08 12:06:49 +00:00
|
|
|
|
2024-07-23 07:59:48 +00:00
|
|
|
if (is_md5_hex(md5)) {
|
2024-04-22 03:46:56 +00:00
|
|
|
fn = xs_fmt("%s/private/%s.json", snac->basedir, md5);
|
2023-02-08 12:06:49 +00:00
|
|
|
|
2024-04-22 03:46:56 +00:00
|
|
|
if (mtime(fn) == 0.0) {
|
2023-02-08 12:06:49 +00:00
|
|
|
fn = xs_free(fn);
|
2024-04-22 03:46:56 +00:00
|
|
|
fn = xs_fmt("%s/public/%s.json", snac->basedir, md5);
|
|
|
|
|
|
|
|
if (mtime(fn) == 0.0)
|
|
|
|
fn = xs_free(fn);
|
|
|
|
}
|
2023-02-08 12:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-08 12:28:03 +00:00
|
|
|
int timeline_here(snac *snac, const char *md5)
|
|
|
|
/* checks if an object is in the user cache */
|
|
|
|
{
|
|
|
|
xs *fn = timeline_fn_by_md5(snac, md5);
|
|
|
|
|
|
|
|
return !(fn == NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-05 16:39:40 +00:00
|
|
|
int timeline_get_by_md5(snac *snac, const char *md5, xs_dict **msg)
|
2023-02-05 12:35:50 +00:00
|
|
|
/* gets a message from the timeline */
|
|
|
|
{
|
2024-05-26 19:45:41 +00:00
|
|
|
int status = HTTP_STATUS_NOT_FOUND;
|
2023-02-05 16:56:59 +00:00
|
|
|
FILE *f = NULL;
|
|
|
|
|
2023-02-08 12:06:49 +00:00
|
|
|
xs *fn = timeline_fn_by_md5(snac, md5);
|
2023-02-05 16:56:59 +00:00
|
|
|
|
2023-02-08 12:06:49 +00:00
|
|
|
if (fn != NULL && (f = fopen(fn, "r")) != NULL) {
|
2023-08-05 12:56:07 +00:00
|
|
|
*msg = xs_json_load(f);
|
2023-02-05 16:56:59 +00:00
|
|
|
fclose(f);
|
|
|
|
|
2023-08-05 12:56:07 +00:00
|
|
|
if (*msg != NULL)
|
2024-05-26 19:45:41 +00:00
|
|
|
status = HTTP_STATUS_OK;
|
2023-02-05 16:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
2023-02-05 12:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
int timeline_del(snac *snac, const char *id)
|
2022-09-20 08:02:00 +00:00
|
|
|
/* deletes a message from the timeline */
|
|
|
|
{
|
2022-11-26 12:34:43 +00:00
|
|
|
/* delete from the user's caches */
|
|
|
|
object_user_cache_del(snac, id, "public");
|
|
|
|
object_user_cache_del(snac, id, "private");
|
2022-11-24 07:36:01 +00:00
|
|
|
|
2024-08-29 10:53:49 +00:00
|
|
|
unpin(snac, id);
|
|
|
|
unbookmark(snac, id);
|
|
|
|
|
2022-11-27 08:45:06 +00:00
|
|
|
/* try to delete the object if it's not used elsewhere */
|
2022-12-02 19:39:31 +00:00
|
|
|
return object_del_if_unref(id);
|
2022-09-20 08:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-25 20:21:59 +00:00
|
|
|
void timeline_update_indexes(snac *snac, const char *id)
|
|
|
|
/* updates the indexes */
|
2022-11-25 13:19:00 +00:00
|
|
|
{
|
2022-11-26 04:46:21 +00:00
|
|
|
object_user_cache_add(snac, id, "private");
|
2022-11-25 16:26:12 +00:00
|
|
|
|
2022-12-03 19:28:23 +00:00
|
|
|
if (xs_startswith(id, snac->actor)) {
|
|
|
|
xs *msg = NULL;
|
|
|
|
|
2023-02-05 16:45:00 +00:00
|
|
|
if (valid_status(object_get(id, &msg))) {
|
2022-12-03 19:28:23 +00:00
|
|
|
/* if its ours and is public, also store in public */
|
2023-08-12 07:43:01 +00:00
|
|
|
if (is_msg_public(msg)) {
|
2022-12-03 19:28:23 +00:00
|
|
|
object_user_cache_add(snac, id, "public");
|
2023-04-29 05:36:18 +00:00
|
|
|
|
|
|
|
/* also add it to the instance public timeline */
|
|
|
|
xs *ipt = xs_fmt("%s/public.idx", srv_basedir);
|
|
|
|
index_add(ipt, id);
|
|
|
|
}
|
2022-12-03 19:28:23 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-25 13:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-14 06:47:20 +00:00
|
|
|
int timeline_add(snac *snac, const char *id, const xs_dict *o_msg)
|
2022-09-25 15:42:39 +00:00
|
|
|
/* adds a message to the timeline */
|
|
|
|
{
|
2022-12-02 19:28:27 +00:00
|
|
|
int ret = object_add(id, o_msg);
|
|
|
|
timeline_update_indexes(snac, id);
|
2022-09-25 15:42:39 +00:00
|
|
|
|
2023-11-08 07:14:34 +00:00
|
|
|
tag_index(id, o_msg);
|
|
|
|
|
2024-04-30 17:41:08 +00:00
|
|
|
list_distribute(snac, NULL, o_msg);
|
2024-04-29 07:50:22 +00:00
|
|
|
|
2022-12-02 19:28:27 +00:00
|
|
|
snac_debug(snac, 1, xs_fmt("timeline_add %s", id));
|
2022-09-25 15:42:39 +00:00
|
|
|
|
2022-12-02 19:28:27 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2022-09-25 15:42:39 +00:00
|
|
|
|
2022-11-25 11:42:42 +00:00
|
|
|
|
2024-02-21 08:22:32 +00:00
|
|
|
int timeline_admire(snac *snac, const char *id, const char *admirer, int like)
|
2022-12-02 19:28:27 +00:00
|
|
|
/* updates a timeline entry with a new admiration */
|
|
|
|
{
|
2022-12-03 20:34:10 +00:00
|
|
|
/* if we are admiring this, add to both timelines */
|
|
|
|
if (!like && strcmp(admirer, snac->actor) == 0) {
|
2022-12-03 18:37:52 +00:00
|
|
|
object_user_cache_add(snac, id, "public");
|
2022-12-03 20:34:10 +00:00
|
|
|
object_user_cache_add(snac, id, "private");
|
|
|
|
}
|
2022-12-03 18:37:52 +00:00
|
|
|
|
2024-02-21 08:22:32 +00:00
|
|
|
int ret = object_admire(id, admirer, like);
|
2022-09-25 16:50:53 +00:00
|
|
|
|
2022-12-02 19:28:27 +00:00
|
|
|
snac_debug(snac, 1, xs_fmt("timeline_admire (%s) %s %s",
|
|
|
|
like ? "Like" : "Announce", id, admirer));
|
2024-02-21 08:22:32 +00:00
|
|
|
|
|
|
|
return ret;
|
2022-09-25 15:42:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
xs_list *timeline_top_level(snac *snac, const xs_list *list)
|
2022-11-26 17:52:51 +00:00
|
|
|
/* returns the top level md5 entries from this index */
|
2022-11-26 17:04:05 +00:00
|
|
|
{
|
|
|
|
xs_set seen;
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_str *v;
|
2022-11-26 17:04:05 +00:00
|
|
|
|
|
|
|
xs_set_init(&seen);
|
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
int c = 0;
|
|
|
|
while (xs_list_next(list, &v, &c)) {
|
2024-07-23 08:09:12 +00:00
|
|
|
char line[MD5_HEX_SIZE] = "";
|
2022-11-26 17:04:05 +00:00
|
|
|
|
2023-04-16 18:12:44 +00:00
|
|
|
strncpy(line, v, sizeof(line));
|
2022-11-26 17:04:05 +00:00
|
|
|
|
|
|
|
for (;;) {
|
2024-07-23 08:09:12 +00:00
|
|
|
char line2[MD5_HEX_SIZE];
|
2022-11-26 17:04:05 +00:00
|
|
|
|
2022-11-26 17:52:51 +00:00
|
|
|
/* if it doesn't have a parent, use this */
|
2024-07-23 08:09:12 +00:00
|
|
|
if (!object_parent(line, line2))
|
2022-11-26 17:04:05 +00:00
|
|
|
break;
|
|
|
|
|
2023-02-08 12:21:44 +00:00
|
|
|
/* well, there is a parent... but is it here? */
|
2023-02-08 12:28:03 +00:00
|
|
|
if (!timeline_here(snac, line2))
|
2022-11-26 17:04:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* it's here! try again with its own parent */
|
2023-04-16 18:12:44 +00:00
|
|
|
strncpy(line, line2, sizeof(line));
|
2022-11-26 17:04:05 +00:00
|
|
|
}
|
|
|
|
|
2022-12-02 20:24:17 +00:00
|
|
|
xs_set_add(&seen, line);
|
2022-11-26 17:04:05 +00:00
|
|
|
}
|
|
|
|
|
2022-12-02 20:24:17 +00:00
|
|
|
return xs_set_result(&seen);
|
2022-11-26 17:04:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-08-09 14:28:57 +00:00
|
|
|
xs_str *user_index_fn(snac *user, const char *idx_name)
|
|
|
|
/* returns the filename of a user index */
|
|
|
|
{
|
|
|
|
return xs_fmt("%s/%s.idx", user->basedir, idx_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
xs_list *timeline_simple_list(snac *user, const char *idx_name, int skip, int show)
|
2022-12-03 06:27:26 +00:00
|
|
|
/* returns a timeline (with all entries) */
|
2022-12-02 18:14:59 +00:00
|
|
|
{
|
2024-08-09 14:28:57 +00:00
|
|
|
xs *idx = user_index_fn(user, idx_name);
|
2022-12-03 06:27:26 +00:00
|
|
|
|
2022-12-04 20:28:29 +00:00
|
|
|
return index_list_desc(idx, skip, show);
|
2022-12-03 06:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-30 04:39:55 +00:00
|
|
|
xs_list *timeline_list(snac *snac, const char *idx_name, int skip, int show)
|
2022-12-03 06:27:26 +00:00
|
|
|
/* returns a timeline (only top level entries) */
|
|
|
|
{
|
2023-10-09 09:49:45 +00:00
|
|
|
int c_max;
|
|
|
|
|
|
|
|
/* maximum number of items in the timeline */
|
|
|
|
c_max = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
|
|
|
|
|
|
|
|
/* never more timeline entries than the configured maximum */
|
|
|
|
if (show > c_max)
|
|
|
|
show = c_max;
|
|
|
|
|
2022-12-04 20:28:29 +00:00
|
|
|
xs *list = timeline_simple_list(snac, idx_name, skip, show);
|
2022-12-02 18:14:59 +00:00
|
|
|
|
2023-02-08 12:21:44 +00:00
|
|
|
return timeline_top_level(snac, list);
|
2022-12-02 18:14:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-08-09 14:28:57 +00:00
|
|
|
xs_str *instance_index_fn(void)
|
|
|
|
{
|
|
|
|
return xs_fmt("%s/public.idx", srv_basedir);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-30 04:39:55 +00:00
|
|
|
xs_list *timeline_instance_list(int skip, int show)
|
|
|
|
/* returns the timeline for the full instance */
|
|
|
|
{
|
2024-08-09 14:28:57 +00:00
|
|
|
xs *idx = instance_index_fn();
|
2023-04-30 04:39:55 +00:00
|
|
|
|
|
|
|
return index_list_desc(idx, skip, show);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-28 10:06:46 +00:00
|
|
|
/** following **/
|
|
|
|
|
|
|
|
/* this needs special treatment and cannot use the object db as is,
|
|
|
|
with a link to a cached author, because we need the Follow object
|
|
|
|
in case we need to unfollow (Undo + original Follow) */
|
|
|
|
|
2023-06-07 09:08:14 +00:00
|
|
|
xs_str *_following_fn(snac *snac, const char *actor)
|
2022-09-20 09:31:56 +00:00
|
|
|
{
|
|
|
|
xs *md5 = xs_md5_hex(actor, strlen(actor));
|
|
|
|
return xs_fmt("%s/following/%s.json", snac->basedir, md5);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-23 06:44:26 +00:00
|
|
|
int following_add(snac *snac, const char *actor, const xs_dict *msg)
|
2022-09-20 09:31:56 +00:00
|
|
|
/* adds to the following list */
|
|
|
|
{
|
2024-05-26 19:45:41 +00:00
|
|
|
int ret = HTTP_STATUS_CREATED;
|
2022-09-20 09:31:56 +00:00
|
|
|
xs *fn = _following_fn(snac, actor);
|
|
|
|
FILE *f;
|
2024-01-14 14:08:54 +00:00
|
|
|
xs *p_object = NULL;
|
|
|
|
|
|
|
|
if (valid_status(following_get(snac, actor, &p_object))) {
|
|
|
|
/* object already exists; if it's of type Accept,
|
|
|
|
the actor is already being followed and confirmed,
|
|
|
|
so do nothing */
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *type = xs_dict_get(p_object, "type");
|
2024-01-14 14:08:54 +00:00
|
|
|
|
|
|
|
if (!xs_is_null(type) && strcmp(type, "Accept") == 0) {
|
|
|
|
snac_debug(snac, 1, xs_fmt("following_add actor already confirmed %s", actor));
|
2024-05-26 19:45:41 +00:00
|
|
|
return HTTP_STATUS_OK;
|
2024-01-14 14:08:54 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-20 09:31:56 +00:00
|
|
|
|
|
|
|
if ((f = fopen(fn, "w")) != NULL) {
|
2023-08-08 17:29:34 +00:00
|
|
|
xs_json_dump(msg, 4, f);
|
2022-09-20 09:31:56 +00:00
|
|
|
fclose(f);
|
2023-06-15 02:11:39 +00:00
|
|
|
|
|
|
|
/* get the filename of the actor object */
|
2023-06-18 18:35:16 +00:00
|
|
|
xs *actor_fn = _object_fn(actor);
|
2023-06-15 02:11:39 +00:00
|
|
|
|
|
|
|
/* increase its reference count */
|
2023-06-15 02:29:26 +00:00
|
|
|
fn = xs_replace_i(fn, ".json", "_a.json");
|
2023-06-15 02:11:39 +00:00
|
|
|
link(actor_fn, fn);
|
2022-09-20 09:31:56 +00:00
|
|
|
}
|
|
|
|
else
|
2024-05-26 19:45:41 +00:00
|
|
|
ret = HTTP_STATUS_INTERNAL_SERVER_ERROR;
|
2022-09-20 09:31:56 +00:00
|
|
|
|
|
|
|
snac_debug(snac, 2, xs_fmt("following_add %s %s", actor, fn));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-23 06:59:14 +00:00
|
|
|
int following_del(snac *snac, const char *actor)
|
2022-11-24 11:38:31 +00:00
|
|
|
/* we're not following this actor any longer */
|
2022-09-20 09:31:56 +00:00
|
|
|
{
|
|
|
|
xs *fn = _following_fn(snac, actor);
|
|
|
|
|
2023-06-15 02:11:39 +00:00
|
|
|
snac_debug(snac, 2, xs_fmt("following_del %s %s", actor, fn));
|
|
|
|
|
2022-09-20 09:31:56 +00:00
|
|
|
unlink(fn);
|
|
|
|
|
2023-06-15 02:11:39 +00:00
|
|
|
/* also delete the reference to the author */
|
2023-06-15 02:29:26 +00:00
|
|
|
fn = xs_replace_i(fn, ".json", "_a.json");
|
2023-06-15 02:11:39 +00:00
|
|
|
unlink(fn);
|
2022-09-20 09:31:56 +00:00
|
|
|
|
2024-05-26 19:45:41 +00:00
|
|
|
return HTTP_STATUS_OK;
|
2022-09-20 09:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-23 04:05:35 +00:00
|
|
|
int following_check(snac *snac, const char *actor)
|
2022-11-24 11:38:31 +00:00
|
|
|
/* checks if we are following this actor */
|
2022-09-20 09:31:56 +00:00
|
|
|
{
|
|
|
|
xs *fn = _following_fn(snac, actor);
|
|
|
|
|
|
|
|
return !!(mtime(fn) != 0.0);
|
|
|
|
}
|
2022-09-20 09:38:18 +00:00
|
|
|
|
|
|
|
|
2023-06-07 09:08:14 +00:00
|
|
|
int following_get(snac *snac, const char *actor, xs_dict **data)
|
2022-10-01 07:12:33 +00:00
|
|
|
/* returns the 'Follow' object */
|
|
|
|
{
|
|
|
|
xs *fn = _following_fn(snac, actor);
|
|
|
|
FILE *f;
|
2024-05-26 19:45:41 +00:00
|
|
|
int status = HTTP_STATUS_OK;
|
2022-10-01 07:12:33 +00:00
|
|
|
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
2023-08-05 12:56:07 +00:00
|
|
|
*data = xs_json_load(f);
|
2022-10-01 07:12:33 +00:00
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
else
|
2024-05-26 19:45:41 +00:00
|
|
|
status = HTTP_STATUS_NOT_FOUND;
|
2022-10-01 07:12:33 +00:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-15 09:15:28 +00:00
|
|
|
xs_list *following_list(snac *snac)
|
2022-11-02 09:49:16 +00:00
|
|
|
/* returns the list of people being followed */
|
|
|
|
{
|
|
|
|
xs *spec = xs_fmt("%s/following/" "*.json", snac->basedir);
|
|
|
|
xs *glist = xs_glob(spec, 0, 0);
|
2023-05-15 09:15:28 +00:00
|
|
|
xs_list *p;
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_str *v;
|
2023-05-15 09:15:28 +00:00
|
|
|
xs_list *list = xs_list_new();
|
2022-11-02 09:49:16 +00:00
|
|
|
|
|
|
|
/* iterate the list of files */
|
|
|
|
p = glist;
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
/* load the follower data */
|
|
|
|
if ((f = fopen(v, "r")) != NULL) {
|
2023-08-05 12:56:07 +00:00
|
|
|
xs *o = xs_json_load(f);
|
2022-11-02 09:49:16 +00:00
|
|
|
fclose(f);
|
|
|
|
|
2023-08-05 12:56:07 +00:00
|
|
|
if (o != NULL) {
|
|
|
|
const char *type = xs_dict_get(o, "type");
|
2022-11-02 09:49:16 +00:00
|
|
|
|
2023-08-05 12:56:07 +00:00
|
|
|
if (!xs_is_null(type) && strcmp(type, "Accept") == 0) {
|
|
|
|
const char *actor = xs_dict_get(o, "actor");
|
2022-11-28 10:06:46 +00:00
|
|
|
|
2023-08-05 12:56:07 +00:00
|
|
|
if (!xs_is_null(actor)) {
|
|
|
|
list = xs_list_append(list, actor);
|
2023-06-15 02:19:09 +00:00
|
|
|
|
2023-08-05 12:56:07 +00:00
|
|
|
/* check if there is a link to the actor object */
|
|
|
|
xs *v2 = xs_replace(v, ".json", "_a.json");
|
2023-06-15 02:19:09 +00:00
|
|
|
|
2023-08-05 12:56:07 +00:00
|
|
|
if (mtime(v2) == 0.0) {
|
|
|
|
/* no; add a link to it */
|
|
|
|
xs *actor_fn = _object_fn(actor);
|
|
|
|
link(actor_fn, v2);
|
2023-06-15 02:19:09 +00:00
|
|
|
}
|
2022-11-28 10:06:46 +00:00
|
|
|
}
|
2022-11-02 09:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-07 09:08:14 +00:00
|
|
|
xs_str *_muted_fn(snac *snac, const char *actor)
|
2022-09-20 09:38:18 +00:00
|
|
|
{
|
|
|
|
xs *md5 = xs_md5_hex(actor, strlen(actor));
|
2022-11-24 09:06:24 +00:00
|
|
|
return xs_fmt("%s/muted/%s", snac->basedir, md5);
|
2022-09-20 09:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-23 04:05:35 +00:00
|
|
|
void mute(snac *snac, const char *actor)
|
2022-09-20 09:38:18 +00:00
|
|
|
/* mutes a moron */
|
|
|
|
{
|
|
|
|
xs *fn = _muted_fn(snac, actor);
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "w")) != NULL) {
|
|
|
|
fprintf(f, "%s\n", actor);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
snac_debug(snac, 2, xs_fmt("muted %s %s", actor, fn));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-23 04:05:35 +00:00
|
|
|
void unmute(snac *snac, const char *actor)
|
2022-09-20 09:38:18 +00:00
|
|
|
/* actor is no longer a moron */
|
|
|
|
{
|
|
|
|
xs *fn = _muted_fn(snac, actor);
|
|
|
|
|
|
|
|
unlink(fn);
|
|
|
|
|
|
|
|
snac_debug(snac, 2, xs_fmt("unmuted %s %s", actor, fn));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-23 04:05:35 +00:00
|
|
|
int is_muted(snac *snac, const char *actor)
|
2022-09-20 09:38:18 +00:00
|
|
|
/* check if someone is muted */
|
|
|
|
{
|
|
|
|
xs *fn = _muted_fn(snac, actor);
|
|
|
|
|
|
|
|
return !!(mtime(fn) != 0.0);
|
|
|
|
}
|
2022-09-20 10:00:13 +00:00
|
|
|
|
|
|
|
|
2024-09-18 10:12:58 +00:00
|
|
|
xs_list *muted_list(snac *user)
|
|
|
|
/* returns the list (actor URLs) of the muted morons */
|
|
|
|
{
|
|
|
|
xs_list *l = xs_list_new();
|
|
|
|
xs *spec = xs_fmt("%s/muted/" "*", user->basedir);
|
|
|
|
xs *files = xs_glob(spec, 0, 0);
|
|
|
|
const char *fn;
|
|
|
|
|
|
|
|
xs_list_foreach(files, fn) {
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
|
|
|
xs *actor = xs_strip_i(xs_readline(f));
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
l = xs_list_append(l, actor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-08-29 04:40:38 +00:00
|
|
|
/** bookmarking **/
|
|
|
|
|
|
|
|
int is_bookmarked(snac *user, const char *id)
|
|
|
|
/* returns true if this note is bookmarked */
|
|
|
|
{
|
2024-08-29 05:25:28 +00:00
|
|
|
return object_user_cache_in(user, id, "bookmark");
|
2024-08-29 04:40:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int bookmark(snac *user, const char *id)
|
|
|
|
/* bookmarks a post */
|
|
|
|
{
|
|
|
|
if (is_bookmarked(user, id))
|
|
|
|
return -3;
|
|
|
|
|
|
|
|
return object_user_cache_add(user, id, "bookmark");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int unbookmark(snac *user, const char *id)
|
|
|
|
/* unbookmarks a post */
|
|
|
|
{
|
|
|
|
return object_user_cache_del(user, id, "bookmark");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-08-29 05:45:22 +00:00
|
|
|
xs_list *bookmark_list(snac *user)
|
|
|
|
/* return the lists of bookmarked posts */
|
|
|
|
{
|
|
|
|
return object_user_cache_list(user, "bookmark", XS_ALL, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-08-29 06:30:09 +00:00
|
|
|
xs_str *bookmark_index_fn(snac *user)
|
|
|
|
{
|
|
|
|
return object_user_cache_index_fn(user, "bookmark");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-28 18:26:59 +00:00
|
|
|
/** pinning **/
|
|
|
|
|
|
|
|
int is_pinned(snac *user, const char *id)
|
|
|
|
/* returns true if this note is pinned */
|
|
|
|
{
|
2024-08-29 04:59:44 +00:00
|
|
|
return object_user_cache_in(user, id, "pinned");
|
2023-06-28 18:26:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-18 20:52:27 +00:00
|
|
|
int is_pinned_by_md5(snac *user, const char *md5)
|
|
|
|
{
|
2024-08-29 04:59:44 +00:00
|
|
|
return object_user_cache_in_by_md5(user, md5, "pinned");
|
2023-09-18 20:52:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-28 18:26:59 +00:00
|
|
|
int pin(snac *user, const char *id)
|
|
|
|
/* pins a message */
|
|
|
|
{
|
2023-06-28 18:52:09 +00:00
|
|
|
int ret = -2;
|
2023-06-28 18:26:59 +00:00
|
|
|
|
|
|
|
if (xs_startswith(id, user->actor)) {
|
2023-06-28 18:52:09 +00:00
|
|
|
if (is_pinned(user, id))
|
|
|
|
ret = -3;
|
2024-08-29 04:59:44 +00:00
|
|
|
else
|
2023-06-28 18:52:09 +00:00
|
|
|
ret = object_user_cache_add(user, id, "pinned");
|
2023-06-28 18:26:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-28 18:52:09 +00:00
|
|
|
int unpin(snac *user, const char *id)
|
2023-06-28 18:26:59 +00:00
|
|
|
/* unpin a message */
|
|
|
|
{
|
2024-08-29 04:59:44 +00:00
|
|
|
return object_user_cache_del(user, id, "pinned");
|
2023-06-28 18:26:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
xs_list *pinned_list(snac *user)
|
|
|
|
/* return the lists of pinned posts */
|
|
|
|
{
|
2023-07-05 12:06:21 +00:00
|
|
|
return object_user_cache_list(user, "pinned", XS_ALL, 1);
|
2023-06-28 18:26:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-06 07:54:27 +00:00
|
|
|
/** drafts **/
|
|
|
|
|
|
|
|
int is_draft(snac *user, const char *id)
|
|
|
|
/* returns true if this note is a draft */
|
|
|
|
{
|
|
|
|
return object_user_cache_in(user, id, "draft");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void draft_del(snac *user, const char *id)
|
|
|
|
/* delete a message from the draft cache */
|
|
|
|
{
|
|
|
|
object_user_cache_del(user, id, "draft");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void draft_add(snac *user, const char *id, const xs_dict *msg)
|
|
|
|
/* store the message as a draft */
|
|
|
|
{
|
|
|
|
/* delete from the index, in case it was already there */
|
|
|
|
draft_del(user, id);
|
|
|
|
|
|
|
|
/* overwrite object */
|
|
|
|
object_add_ow(id, msg);
|
|
|
|
|
|
|
|
/* [re]add to the index */
|
|
|
|
object_user_cache_add(user, id, "draft");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-06 08:31:09 +00:00
|
|
|
xs_list *draft_list(snac *user)
|
|
|
|
/* return the lists of drafts */
|
|
|
|
{
|
|
|
|
return object_user_cache_list(user, "draft", XS_ALL, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-08-29 04:40:38 +00:00
|
|
|
/** hiding **/
|
|
|
|
|
2023-06-07 09:08:14 +00:00
|
|
|
xs_str *_hidden_fn(snac *snac, const char *id)
|
2022-11-24 08:39:16 +00:00
|
|
|
{
|
|
|
|
xs *md5 = xs_md5_hex(id, strlen(id));
|
2022-11-24 08:49:54 +00:00
|
|
|
return xs_fmt("%s/hidden/%s", snac->basedir, md5);
|
2022-11-24 08:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void hide(snac *snac, const char *id)
|
|
|
|
/* hides a message tree */
|
|
|
|
{
|
|
|
|
xs *fn = _hidden_fn(snac, id);
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "w")) != NULL) {
|
|
|
|
fprintf(f, "%s\n", id);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
snac_debug(snac, 2, xs_fmt("hidden %s %s", id, fn));
|
2022-11-24 08:58:37 +00:00
|
|
|
|
|
|
|
/* hide all the children */
|
|
|
|
xs *chld = object_children(id);
|
2024-05-23 08:01:37 +00:00
|
|
|
char *p;
|
|
|
|
const char *v;
|
2022-11-24 08:58:37 +00:00
|
|
|
|
|
|
|
p = chld;
|
2022-11-24 11:21:54 +00:00
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
xs *co = NULL;
|
|
|
|
|
|
|
|
/* resolve to get the id */
|
2023-02-05 16:45:00 +00:00
|
|
|
if (valid_status(object_get_by_md5(v, &co))) {
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *id = xs_dict_get(co, "id");
|
|
|
|
if (id != NULL)
|
|
|
|
hide(snac, id);
|
2022-11-24 11:21:54 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-24 08:39:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int is_hidden(snac *snac, const char *id)
|
|
|
|
/* check is id is hidden */
|
|
|
|
{
|
|
|
|
xs *fn = _hidden_fn(snac, id);
|
|
|
|
|
|
|
|
return !!(mtime(fn) != 0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
int actor_add(const char *actor, const xs_dict *msg)
|
2022-09-28 18:08:02 +00:00
|
|
|
/* adds an actor */
|
2022-09-22 16:50:39 +00:00
|
|
|
{
|
2022-11-26 04:18:48 +00:00
|
|
|
return object_add_ow(actor, msg);
|
2022-09-22 16:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-12 09:23:01 +00:00
|
|
|
int actor_get(const char *actor, xs_dict **data)
|
2022-09-22 16:50:39 +00:00
|
|
|
/* returns an already downloaded actor */
|
|
|
|
{
|
2024-05-26 19:45:41 +00:00
|
|
|
int status = HTTP_STATUS_OK;
|
2023-05-03 06:15:38 +00:00
|
|
|
xs_dict *d = NULL;
|
2022-09-22 16:50:39 +00:00
|
|
|
|
2023-04-30 04:49:51 +00:00
|
|
|
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);
|
2024-05-26 19:45:41 +00:00
|
|
|
return HTTP_STATUS_OK;
|
2023-04-30 04:49:51 +00:00
|
|
|
}
|
|
|
|
else
|
2024-05-26 19:45:41 +00:00
|
|
|
return HTTP_STATUS_NOT_FOUND;
|
2023-04-30 04:49:51 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 15:13:51 +00:00
|
|
|
/* read the object */
|
2023-05-03 06:15:38 +00:00
|
|
|
if (!valid_status(status = object_get(actor, &d))) {
|
|
|
|
d = xs_free(d);
|
2022-11-23 15:13:51 +00:00
|
|
|
return status;
|
2023-05-03 06:15:38 +00:00
|
|
|
}
|
2022-09-22 16:50:39 +00:00
|
|
|
|
2024-01-14 11:19:35 +00:00
|
|
|
/* if the object is corrupted, discard it */
|
|
|
|
if (xs_is_null(xs_dict_get(d, "id")) || xs_is_null(xs_dict_get(d, "type"))) {
|
|
|
|
srv_debug(1, xs_fmt("corrupted actor object %s", actor));
|
|
|
|
d = xs_free(d);
|
2024-05-26 19:45:41 +00:00
|
|
|
return HTTP_STATUS_NOT_FOUND;
|
2024-01-14 11:19:35 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 15:13:51 +00:00
|
|
|
if (data)
|
|
|
|
*data = d;
|
2022-12-06 18:28:29 +00:00
|
|
|
else
|
|
|
|
d = xs_free(d);
|
2022-11-23 15:13:51 +00:00
|
|
|
|
2023-06-18 18:35:16 +00:00
|
|
|
xs *fn = _object_fn(actor);
|
2022-11-23 15:13:51 +00:00
|
|
|
double max_time;
|
2022-09-22 16:50:39 +00:00
|
|
|
|
|
|
|
/* maximum time for the actor data to be considered stale */
|
|
|
|
max_time = 3600.0 * 36.0;
|
|
|
|
|
2022-11-23 15:13:51 +00:00
|
|
|
if (mtime(fn) + max_time < (double) time(NULL)) {
|
2022-09-22 16:50:39 +00:00
|
|
|
/* actor data exists but also stinks */
|
2024-05-26 19:45:41 +00:00
|
|
|
status = HTTP_STATUS_RESET_CONTENT; /* "110: Response Is Stale" */
|
2022-09-22 16:50:39 +00:00
|
|
|
}
|
2024-03-12 16:54:54 +00:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int actor_get_refresh(snac *user, const char *actor, xs_dict **data)
|
2024-03-13 05:30:24 +00:00
|
|
|
/* gets an actor and requests a refresh if it's stale */
|
2024-03-12 16:54:54 +00:00
|
|
|
{
|
|
|
|
int status = actor_get(actor, data);
|
|
|
|
|
2024-05-26 19:45:41 +00:00
|
|
|
if (status == HTTP_STATUS_RESET_CONTENT && user && !xs_startswith(actor, srv_baseurl))
|
2024-05-11 17:16:54 +00:00
|
|
|
enqueue_actor_refresh(user, actor, 0);
|
2022-09-22 16:50:39 +00:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-06 16:40:50 +00:00
|
|
|
/** user limiting (announce blocks) **/
|
|
|
|
|
|
|
|
int limited(snac *user, const char *id, int cmd)
|
|
|
|
/* announce messages from a followed (0: check, 1: limit; 2: unlimit) */
|
|
|
|
{
|
|
|
|
int ret = 0;
|
2023-08-07 04:38:19 +00:00
|
|
|
xs *dir = xs_fmt("%s/limited", user->basedir);
|
2023-08-06 16:40:50 +00:00
|
|
|
xs *md5 = xs_md5_hex(id, strlen(id));
|
2023-08-07 04:38:19 +00:00
|
|
|
xs *fn = xs_fmt("%s/%s", dir, md5);
|
2023-08-06 16:40:50 +00:00
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case 0: /** check **/
|
|
|
|
ret = !!(mtime(fn) > 0.0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: /** limit **/
|
2023-08-07 04:38:19 +00:00
|
|
|
mkdirx(dir);
|
|
|
|
|
2023-08-06 16:40:50 +00:00
|
|
|
if (mtime(fn) > 0.0)
|
|
|
|
ret = -1;
|
|
|
|
else {
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "w")) != NULL) {
|
|
|
|
fprintf(f, "%s\n", id);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = -2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: /** unlimit **/
|
|
|
|
if (mtime(fn) > 0.0)
|
|
|
|
ret = unlink(fn);
|
|
|
|
else
|
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-08 07:14:34 +00:00
|
|
|
/** tag indexing **/
|
|
|
|
|
|
|
|
void tag_index(const char *id, const xs_dict *obj)
|
|
|
|
/* update the tag indexes for this object */
|
|
|
|
{
|
2024-05-21 12:12:15 +00:00
|
|
|
const xs_list *tags = xs_dict_get(obj, "tag");
|
2024-10-19 20:34:22 +00:00
|
|
|
xs *md5_id = xs_md5_hex(id, strlen(id));
|
2023-11-08 07:14:34 +00:00
|
|
|
|
|
|
|
if (is_msg_public(obj) && xs_type(tags) == XSTYPE_LIST && xs_list_len(tags) > 0) {
|
|
|
|
xs *g_tag_dir = xs_fmt("%s/tag", srv_basedir);
|
|
|
|
|
|
|
|
mkdirx(g_tag_dir);
|
|
|
|
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_dict *v;
|
2024-05-21 12:12:15 +00:00
|
|
|
int ct = 0;
|
|
|
|
while (xs_list_next(tags, &v, &ct)) {
|
|
|
|
const char *type = xs_dict_get(v, "type");
|
|
|
|
const char *name = xs_dict_get(v, "name");
|
2023-11-08 07:14:34 +00:00
|
|
|
|
|
|
|
if (!xs_is_null(type) && !xs_is_null(name) && strcmp(type, "Hashtag") == 0) {
|
2023-12-06 13:46:51 +00:00
|
|
|
while (*name == '#' || *name == '@')
|
2023-11-08 07:14:34 +00:00
|
|
|
name++;
|
|
|
|
|
2023-12-06 13:46:51 +00:00
|
|
|
if (*name == '\0')
|
|
|
|
continue;
|
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
name = xs_tolower_i((xs_str *)name);
|
2023-11-08 07:14:34 +00:00
|
|
|
|
|
|
|
xs *md5_tag = xs_md5_hex(name, strlen(name));
|
|
|
|
xs *tag_dir = xs_fmt("%s/%c%c", g_tag_dir, md5_tag[0], md5_tag[1]);
|
|
|
|
mkdirx(tag_dir);
|
|
|
|
|
|
|
|
xs *g_tag_idx = xs_fmt("%s/%s.idx", tag_dir, md5_tag);
|
2024-10-19 20:34:22 +00:00
|
|
|
|
|
|
|
if (!index_in_md5(g_tag_idx, md5_id))
|
|
|
|
index_add_md5(g_tag_idx, md5_id);
|
2023-11-08 07:14:34 +00:00
|
|
|
|
|
|
|
FILE *f;
|
|
|
|
xs *g_tag_name = xs_replace(g_tag_idx, ".idx", ".tag");
|
|
|
|
if ((f = fopen(g_tag_name, "w")) != NULL) {
|
|
|
|
fprintf(f, "%s\n", name);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
2023-11-08 08:20:34 +00:00
|
|
|
srv_debug(0, xs_fmt("tagged %s #%s (#%s)", id, name, md5_tag));
|
2023-11-08 07:14:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-08-09 14:28:57 +00:00
|
|
|
xs_str *tag_fn(const char *tag)
|
2023-11-08 07:14:34 +00:00
|
|
|
{
|
|
|
|
if (*tag == '#')
|
|
|
|
tag++;
|
|
|
|
|
|
|
|
xs *lw_tag = xs_tolower_i(xs_dup(tag));
|
|
|
|
xs *md5 = xs_md5_hex(lw_tag, strlen(lw_tag));
|
2024-08-09 14:28:57 +00:00
|
|
|
|
|
|
|
return xs_fmt("%s/tag/%c%c/%s.idx", srv_basedir, md5[0], md5[1], md5);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
xs_list *tag_search(const char *tag, int skip, int show)
|
|
|
|
/* returns the list of posts tagged with tag */
|
|
|
|
{
|
|
|
|
xs *idx = tag_fn(tag);
|
2023-11-08 07:14:34 +00:00
|
|
|
|
|
|
|
return index_list_desc(idx, skip, show);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-04-29 05:43:01 +00:00
|
|
|
/** lists **/
|
|
|
|
|
2024-04-29 06:29:18 +00:00
|
|
|
xs_val *list_maint(snac *user, const char *list, int op)
|
2024-04-29 07:26:37 +00:00
|
|
|
/* list maintenance */
|
2024-04-29 05:43:01 +00:00
|
|
|
{
|
2024-04-29 06:29:18 +00:00
|
|
|
xs_val *l = NULL;
|
2024-04-29 05:43:01 +00:00
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
case 0: /** list of lists **/
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
xs *spec = xs_fmt("%s/list/" "*.id", user->basedir);
|
|
|
|
xs *ls = xs_glob(spec, 0, 0);
|
|
|
|
int c = 0;
|
2024-05-23 08:01:37 +00:00
|
|
|
const char *v;
|
2024-04-29 05:43:01 +00:00
|
|
|
|
|
|
|
l = xs_list_new();
|
|
|
|
|
|
|
|
while (xs_list_next(ls, &v, &c)) {
|
|
|
|
if ((f = fopen(v, "r")) != NULL) {
|
|
|
|
xs *title = xs_readline(f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
title = xs_strip_i(title);
|
2024-04-29 06:29:18 +00:00
|
|
|
|
|
|
|
xs *v2 = xs_replace(v, ".id", "");
|
|
|
|
xs *l2 = xs_split(v2, "/");
|
2024-04-29 05:43:01 +00:00
|
|
|
|
|
|
|
/* return [ list_id, list_title ] */
|
2024-04-29 06:29:18 +00:00
|
|
|
l = xs_list_append(l, xs_list_append(xs_list_new(), xs_list_get(l2, -1), title));
|
2024-04-29 05:43:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: /** create new list (list is the name) **/
|
|
|
|
{
|
2024-04-29 06:29:18 +00:00
|
|
|
xs *lol = list_maint(user, NULL, 0);
|
|
|
|
int c = 0;
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_list *v;
|
2024-04-29 06:29:18 +00:00
|
|
|
int add = 1;
|
2024-04-29 05:43:01 +00:00
|
|
|
|
2024-04-29 06:29:18 +00:00
|
|
|
/* check if this list name already exists */
|
|
|
|
while (xs_list_next(lol, &v, &c)) {
|
|
|
|
if (strcmp(xs_list_get(v, 1), list) == 0) {
|
|
|
|
add = 0;
|
2024-10-19 19:22:52 +00:00
|
|
|
|
|
|
|
l = xs_dup(xs_list_get(v, 0));
|
|
|
|
|
2024-04-29 06:29:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-04-29 05:43:01 +00:00
|
|
|
|
2024-04-29 06:29:18 +00:00
|
|
|
if (add) {
|
|
|
|
FILE *f;
|
|
|
|
xs *dir = xs_fmt("%s/list/", user->basedir);
|
2024-06-08 19:25:02 +00:00
|
|
|
|
|
|
|
l = xs_fmt("%010x", time(NULL));
|
2024-04-29 05:43:01 +00:00
|
|
|
|
2024-04-29 06:29:18 +00:00
|
|
|
mkdirx(dir);
|
|
|
|
|
2024-06-08 19:25:02 +00:00
|
|
|
xs *fn = xs_fmt("%s%s.id", dir, l);
|
2024-04-29 06:29:18 +00:00
|
|
|
|
|
|
|
if ((f = fopen(fn, "w")) != NULL) {
|
|
|
|
fprintf(f, "%s\n", list);
|
|
|
|
fclose(f);
|
|
|
|
}
|
2024-04-29 05:43:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2024-04-29 06:29:18 +00:00
|
|
|
case 2: /** delete list (list is the id) **/
|
|
|
|
{
|
|
|
|
if (xs_is_hex(list)) {
|
|
|
|
xs *fn = xs_fmt("%s/list/%s.id", user->basedir, list);
|
|
|
|
unlink(fn);
|
|
|
|
|
|
|
|
fn = xs_replace_i(fn, ".id", ".lst");
|
|
|
|
unlink(fn);
|
|
|
|
|
2024-04-29 07:26:37 +00:00
|
|
|
fn = xs_replace_i(fn, ".lst", ".idx");
|
2024-04-29 06:29:18 +00:00
|
|
|
unlink(fn);
|
2024-05-24 10:05:31 +00:00
|
|
|
|
2024-05-24 10:12:03 +00:00
|
|
|
fn = xs_str_cat(fn, ".bak");
|
2024-05-24 10:05:31 +00:00
|
|
|
unlink(fn);
|
2024-04-29 06:29:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-07 18:51:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 3: /** get list name **/
|
|
|
|
if (xs_is_hex(list)) {
|
|
|
|
FILE *f;
|
|
|
|
xs *fn = xs_fmt("%s/list/%s.id", user->basedir, list);
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
|
|
|
l = xs_strip_i(xs_readline(f));
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-29 05:43:01 +00:00
|
|
|
break;
|
2024-04-29 07:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-08-09 16:07:05 +00:00
|
|
|
xs_str *list_timeline_fn(snac *user, const char *list)
|
|
|
|
{
|
|
|
|
return xs_fmt("%s/list/%s.idx", user->basedir, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-07 17:40:28 +00:00
|
|
|
xs_list *list_timeline(snac *user, const char *list, int skip, int show)
|
|
|
|
/* returns the timeline of a list */
|
|
|
|
{
|
|
|
|
xs_list *l = NULL;
|
|
|
|
|
|
|
|
if (!xs_is_hex(list))
|
|
|
|
return NULL;
|
|
|
|
|
2024-08-09 16:07:05 +00:00
|
|
|
xs *fn = list_timeline_fn(user, list);
|
2024-05-07 17:40:28 +00:00
|
|
|
|
|
|
|
if (mtime(fn) > 0.0)
|
|
|
|
l = index_list_desc(fn, skip, show);
|
2024-10-19 19:28:11 +00:00
|
|
|
else
|
|
|
|
l = xs_list_new();
|
2024-05-07 17:40:28 +00:00
|
|
|
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-04-29 07:26:37 +00:00
|
|
|
xs_val *list_content(snac *user, const char *list, const char *actor_md5, int op)
|
|
|
|
/* list content management */
|
|
|
|
{
|
|
|
|
xs_val *l = NULL;
|
|
|
|
|
|
|
|
if (!xs_is_hex(list))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (actor_md5 != NULL && !xs_is_hex(actor_md5))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
xs *fn = xs_fmt("%s/list/%s.lst", user->basedir, list);
|
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
case 0: /** list content **/
|
|
|
|
l = index_list(fn, XS_ALL);
|
2024-04-29 05:43:01 +00:00
|
|
|
|
|
|
|
break;
|
2024-04-29 07:26:37 +00:00
|
|
|
|
|
|
|
case 1: /** append actor to list **/
|
|
|
|
if (actor_md5 != NULL) {
|
2024-06-06 22:57:33 +00:00
|
|
|
if (!index_in_md5(fn, actor_md5))
|
2024-04-29 07:26:37 +00:00
|
|
|
index_add_md5(fn, actor_md5);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: /** delete actor from list **/
|
|
|
|
if (actor_md5 != NULL)
|
|
|
|
index_del_md5(fn, actor_md5);
|
2024-04-29 08:15:04 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
2024-05-07 17:40:28 +00:00
|
|
|
default:
|
|
|
|
srv_log(xs_fmt("ERROR: list_content: bad op %d", op));
|
2024-04-29 08:15:04 +00:00
|
|
|
break;
|
2024-04-29 05:43:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-04-30 17:41:08 +00:00
|
|
|
void list_distribute(snac *user, const char *who, const xs_dict *post)
|
2024-04-29 07:50:22 +00:00
|
|
|
/* distributes the post to all appropriate lists */
|
|
|
|
{
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *id = xs_dict_get(post, "id");
|
2024-04-29 07:50:22 +00:00
|
|
|
|
2024-04-30 17:41:08 +00:00
|
|
|
/* if who is not set, use the attributedTo in the message */
|
|
|
|
if (xs_is_null(who))
|
|
|
|
who = get_atto(post);
|
|
|
|
|
|
|
|
if (xs_type(who) == XSTYPE_STRING && xs_type(id) == XSTYPE_STRING) {
|
|
|
|
xs *a_md5 = xs_md5_hex(who, strlen(who));
|
2024-04-29 07:50:22 +00:00
|
|
|
xs *i_md5 = xs_md5_hex(id, strlen(id));
|
|
|
|
xs *spec = xs_fmt("%s/list/" "*.lst", user->basedir);
|
|
|
|
xs *ls = xs_glob(spec, 0, 0);
|
|
|
|
int c = 0;
|
2024-05-23 08:01:37 +00:00
|
|
|
const char *v;
|
2024-04-29 07:50:22 +00:00
|
|
|
|
|
|
|
while (xs_list_next(ls, &v, &c)) {
|
|
|
|
/* is the actor in this list? */
|
|
|
|
if (index_in_md5(v, a_md5)) {
|
|
|
|
/* it is; add post md5 to its timeline */
|
|
|
|
xs *idx = xs_replace(v, ".lst", ".idx");
|
2024-04-29 08:15:04 +00:00
|
|
|
index_add_md5(idx, i_md5);
|
|
|
|
|
|
|
|
snac_debug(user, 1, xs_fmt("listed post %s in %s", id, idx));
|
2024-04-29 07:50:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-22 05:58:31 +00:00
|
|
|
/** static data **/
|
|
|
|
|
2023-08-19 07:52:04 +00:00
|
|
|
static int _load_raw_file(const char *fn, xs_val **data, int *size,
|
|
|
|
const char *inm, xs_str **etag)
|
|
|
|
/* loads a cached file */
|
2022-09-28 07:29:09 +00:00
|
|
|
{
|
2024-05-26 19:45:41 +00:00
|
|
|
int status = HTTP_STATUS_NOT_FOUND;
|
2022-09-28 07:29:09 +00:00
|
|
|
|
2023-07-02 09:11:01 +00:00
|
|
|
if (fn) {
|
|
|
|
double tm = mtime(fn);
|
2022-10-17 18:25:42 +00:00
|
|
|
|
2023-07-02 09:11:01 +00:00
|
|
|
if (tm > 0.0) {
|
|
|
|
/* file exists; build the etag */
|
|
|
|
xs *e = xs_fmt("W/\"snac-%.0lf\"", tm);
|
|
|
|
|
|
|
|
/* if if-none-match is set, check if it's the same */
|
|
|
|
if (!xs_is_null(inm) && strcmp(e, inm) == 0) {
|
|
|
|
/* client has the newest version */
|
2024-05-26 19:45:41 +00:00
|
|
|
status = HTTP_STATUS_NOT_MODIFIED;
|
2023-07-02 09:11:01 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* newer or never downloaded; read the full file */
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "rb")) != NULL) {
|
|
|
|
*size = XS_ALL;
|
|
|
|
*data = xs_read(f, size);
|
|
|
|
fclose(f);
|
|
|
|
|
2024-05-26 19:45:41 +00:00
|
|
|
status = HTTP_STATUS_OK;
|
2023-07-02 09:11:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if caller wants the etag, return it */
|
|
|
|
if (etag != NULL)
|
|
|
|
*etag = xs_dup(e);
|
|
|
|
|
2023-08-19 07:52:04 +00:00
|
|
|
srv_debug(1, xs_fmt("_load_raw_file(): %s %d", fn, status));
|
2023-07-02 09:11:01 +00:00
|
|
|
}
|
2022-09-28 07:29:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-19 07:52:04 +00:00
|
|
|
xs_str *_static_fn(snac *snac, const char *id)
|
|
|
|
/* gets the filename for a static file */
|
|
|
|
{
|
|
|
|
if (strchr(id, '/'))
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return xs_fmt("%s/static/%s", snac->basedir, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int static_get(snac *snac, const char *id, xs_val **data, int *size,
|
|
|
|
const char *inm, xs_str **etag)
|
|
|
|
/* returns static content */
|
|
|
|
{
|
|
|
|
xs *fn = _static_fn(snac, id);
|
|
|
|
|
|
|
|
return _load_raw_file(fn, data, size, inm, etag);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-16 16:03:28 +00:00
|
|
|
void static_put(snac *snac, const char *id, const char *data, int size)
|
|
|
|
/* writes status content */
|
|
|
|
{
|
|
|
|
xs *fn = _static_fn(snac, id);
|
|
|
|
FILE *f;
|
|
|
|
|
2023-04-22 05:58:31 +00:00
|
|
|
if (fn && (f = fopen(fn, "wb")) != NULL) {
|
2022-10-16 16:03:28 +00:00
|
|
|
fwrite(data, size, 1, f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-21 22:51:06 +00:00
|
|
|
void static_put_meta(snac *snac, const char *id, const char *str)
|
2023-04-21 22:24:15 +00:00
|
|
|
/* puts metadata (i.e. a media description string) to id */
|
|
|
|
{
|
|
|
|
xs *fn = _static_fn(snac, id);
|
|
|
|
|
2023-04-22 05:58:31 +00:00
|
|
|
if (fn) {
|
|
|
|
fn = xs_str_cat(fn, ".txt");
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "w")) != NULL) {
|
|
|
|
fprintf(f, "%s\n", str);
|
|
|
|
fclose(f);
|
|
|
|
}
|
2023-04-21 22:24:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-21 22:51:06 +00:00
|
|
|
xs_str *static_get_meta(snac *snac, const char *id)
|
2023-04-21 22:24:15 +00:00
|
|
|
/* gets metadata from a media */
|
|
|
|
{
|
|
|
|
xs *fn = _static_fn(snac, id);
|
|
|
|
xs_str *r = NULL;
|
|
|
|
|
2023-04-22 05:58:31 +00:00
|
|
|
if (fn) {
|
|
|
|
fn = xs_str_cat(fn, ".txt");
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
|
|
|
r = xs_strip_i(xs_readline(f));
|
|
|
|
fclose(f);
|
|
|
|
}
|
2023-04-21 22:24:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
r = xs_str_new("");
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-22 06:02:23 +00:00
|
|
|
/** history **/
|
|
|
|
|
|
|
|
xs_str *_history_fn(snac *snac, const char *id)
|
2022-09-30 07:56:29 +00:00
|
|
|
/* gets the filename for the history */
|
|
|
|
{
|
2023-04-22 06:02:23 +00:00
|
|
|
if (strchr(id, '/'))
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return xs_fmt("%s/history/%s", snac->basedir, id);
|
2022-09-30 07:56:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-22 06:02:23 +00:00
|
|
|
double history_mtime(snac *snac, const char *id)
|
2022-09-30 07:56:29 +00:00
|
|
|
{
|
2022-09-30 07:59:13 +00:00
|
|
|
double t = 0.0;
|
2022-09-30 07:56:29 +00:00
|
|
|
xs *fn = _history_fn(snac, id);
|
|
|
|
|
|
|
|
if (fn != NULL)
|
|
|
|
t = mtime(fn);
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-19 07:59:58 +00:00
|
|
|
void history_add(snac *snac, const char *id, const char *content, int size,
|
|
|
|
xs_str **etag)
|
2022-09-30 07:56:29 +00:00
|
|
|
/* adds something to the history */
|
|
|
|
{
|
|
|
|
xs *fn = _history_fn(snac, id);
|
|
|
|
FILE *f;
|
|
|
|
|
2023-04-22 06:02:23 +00:00
|
|
|
if (fn && (f = fopen(fn, "w")) != NULL) {
|
2022-09-30 07:56:29 +00:00
|
|
|
fwrite(content, size, 1, f);
|
|
|
|
fclose(f);
|
2023-08-19 07:59:58 +00:00
|
|
|
|
|
|
|
if (etag) {
|
|
|
|
double tm = mtime(fn);
|
|
|
|
*etag = xs_fmt("W/\"snac-%.0lf\"", tm);
|
|
|
|
}
|
2022-09-30 07:56:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-19 07:31:13 +00:00
|
|
|
int history_get(snac *snac, const char *id, xs_str **content, int *size,
|
|
|
|
const char *inm, xs_str **etag)
|
2022-09-30 07:56:29 +00:00
|
|
|
{
|
|
|
|
xs *fn = _history_fn(snac, id);
|
2023-08-19 07:31:13 +00:00
|
|
|
|
2023-08-19 07:52:04 +00:00
|
|
|
return _load_raw_file(fn, content, size, inm, etag);
|
2022-09-30 07:56:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-22 06:02:23 +00:00
|
|
|
int history_del(snac *snac, const char *id)
|
2022-10-01 05:45:36 +00:00
|
|
|
{
|
|
|
|
xs *fn = _history_fn(snac, id);
|
2023-04-22 06:02:23 +00:00
|
|
|
|
|
|
|
if (fn)
|
|
|
|
return unlink(fn);
|
|
|
|
else
|
|
|
|
return -1;
|
2022-10-01 05:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-22 06:02:23 +00:00
|
|
|
xs_list *history_list(snac *snac)
|
2022-10-02 16:16:58 +00:00
|
|
|
{
|
2022-10-03 09:14:16 +00:00
|
|
|
xs *spec = xs_fmt("%s/history/" "*.html", snac->basedir);
|
2022-10-02 16:16:58 +00:00
|
|
|
|
2023-08-17 09:37:51 +00:00
|
|
|
return xs_glob(spec, 1, 1);
|
2022-10-02 16:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-08 07:02:45 +00:00
|
|
|
void lastlog_write(snac *snac, const char *source)
|
2023-04-05 22:03:06 +00:00
|
|
|
/* writes the last time the user logged in */
|
2023-04-05 21:46:51 +00:00
|
|
|
{
|
|
|
|
xs *fn = xs_fmt("%s/lastlog.txt", snac->basedir);
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "w")) != NULL) {
|
2023-05-08 07:02:45 +00:00
|
|
|
fprintf(f, "%lf %s\n", ftime(), source);
|
2023-04-05 21:46:51 +00:00
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-02 07:43:50 +00:00
|
|
|
/** inbox collection **/
|
|
|
|
|
|
|
|
void inbox_add(const char *inbox)
|
|
|
|
/* collects a shared inbox */
|
|
|
|
{
|
2023-12-19 11:08:06 +00:00
|
|
|
/* don't collect ourselves */
|
|
|
|
if (xs_startswith(inbox, srv_baseurl))
|
|
|
|
return;
|
|
|
|
|
2023-03-02 07:43:50 +00:00
|
|
|
xs *md5 = xs_md5_hex(inbox, strlen(inbox));
|
2023-03-02 08:01:08 +00:00
|
|
|
xs *fn = xs_fmt("%s/inbox/%s", srv_basedir, md5);
|
2023-03-02 07:43:50 +00:00
|
|
|
FILE *f;
|
|
|
|
|
2023-07-10 16:04:59 +00:00
|
|
|
if ((f = fopen(fn, "w")) != NULL) {
|
2023-03-02 07:43:50 +00:00
|
|
|
fprintf(f, "%s\n", inbox);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void inbox_add_by_actor(const xs_dict *actor)
|
|
|
|
/* collects an actor's shared inbox, if it has one */
|
|
|
|
{
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *v;
|
2023-03-02 07:43:50 +00:00
|
|
|
|
|
|
|
if (!xs_is_null(v = xs_dict_get(actor, "endpoints")) &&
|
2023-09-02 06:41:29 +00:00
|
|
|
!xs_is_null(v = xs_dict_get(v, "sharedInbox"))) {
|
|
|
|
/* only collect this inbox if its instance is not blocked */
|
|
|
|
if (!is_instance_blocked(v))
|
|
|
|
inbox_add(v);
|
|
|
|
}
|
2023-03-02 07:43:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-02 08:01:08 +00:00
|
|
|
xs_list *inbox_list(void)
|
|
|
|
/* returns the collected inboxes as a list */
|
|
|
|
{
|
2023-03-02 08:15:40 +00:00
|
|
|
xs_list *ibl = xs_list_new();
|
|
|
|
xs *spec = xs_fmt("%s/inbox/" "*", srv_basedir);
|
|
|
|
xs *files = xs_glob(spec, 0, 0);
|
|
|
|
xs_list *p = files;
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_val *v;
|
2023-03-02 08:15:40 +00:00
|
|
|
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(v, "r")) != NULL) {
|
2023-07-10 16:04:59 +00:00
|
|
|
xs *line = xs_readline(f);
|
2023-03-02 08:15:40 +00:00
|
|
|
|
2023-07-10 16:04:59 +00:00
|
|
|
if (line) {
|
|
|
|
line = xs_strip_i(line);
|
|
|
|
ibl = xs_list_append(ibl, line);
|
2023-03-02 08:15:40 +00:00
|
|
|
}
|
2023-07-10 16:04:59 +00:00
|
|
|
|
|
|
|
fclose(f);
|
2023-03-02 08:15:40 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-02 08:01:08 +00:00
|
|
|
|
2023-03-02 08:15:40 +00:00
|
|
|
return ibl;
|
2023-03-02 08:01:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-29 06:07:10 +00:00
|
|
|
/** instance-wide operations **/
|
|
|
|
|
|
|
|
xs_str *_instance_block_fn(const char *instance)
|
|
|
|
{
|
2024-04-18 15:13:31 +00:00
|
|
|
xs *s = xs_replace(instance, "http:/" "/", "");
|
2024-03-09 14:37:54 +00:00
|
|
|
xs *s1 = xs_replace(s, "https:/" "/", "");
|
2023-06-29 06:07:10 +00:00
|
|
|
xs *l = xs_split(s1, "/");
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *p = xs_list_get(l, 0);
|
2023-06-29 06:07:10 +00:00
|
|
|
xs *md5 = xs_md5_hex(p, strlen(p));
|
|
|
|
|
|
|
|
return xs_fmt("%s/block/%s", srv_basedir, md5);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int is_instance_blocked(const char *instance)
|
|
|
|
{
|
|
|
|
xs *fn = _instance_block_fn(instance);
|
|
|
|
|
|
|
|
return !!(mtime(fn) != 0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int instance_block(const char *instance)
|
|
|
|
/* blocks a full instance */
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* create the subdir */
|
|
|
|
xs *dir = xs_fmt("%s/block/", srv_basedir);
|
|
|
|
mkdirx(dir);
|
|
|
|
|
|
|
|
if (!is_instance_blocked(instance)) {
|
|
|
|
xs *fn = _instance_block_fn(instance);
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "w")) != NULL) {
|
|
|
|
fprintf(f, "%s\n", instance);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = -2;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int instance_unblock(const char *instance)
|
|
|
|
/* unblocks a full instance */
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (is_instance_blocked(instance)) {
|
|
|
|
xs *fn = _instance_block_fn(instance);
|
|
|
|
ret = unlink(fn);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = -2;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-15 11:29:07 +00:00
|
|
|
/** operations by content **/
|
2024-03-11 05:00:21 +00:00
|
|
|
|
2024-05-15 11:31:53 +00:00
|
|
|
int content_match(const char *file, const xs_dict *msg)
|
2024-03-11 08:53:20 +00:00
|
|
|
/* checks if a message's content matches any of the regexes in file */
|
|
|
|
/* file format: one regex per line */
|
2024-03-11 05:00:21 +00:00
|
|
|
{
|
|
|
|
xs *fn = xs_fmt("%s/%s", srv_basedir, file);
|
|
|
|
FILE *f;
|
|
|
|
int r = 0;
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *v = xs_dict_get(msg, "content");
|
2024-03-11 05:00:21 +00:00
|
|
|
|
|
|
|
if (xs_type(v) == XSTYPE_STRING && *v) {
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
2024-05-15 11:31:53 +00:00
|
|
|
srv_debug(1, xs_fmt("content_match: loading regexes from %s", fn));
|
2024-03-11 05:00:21 +00:00
|
|
|
|
2024-03-11 08:53:20 +00:00
|
|
|
/* massage content (strip HTML tags, etc.) */
|
2024-03-11 05:00:21 +00:00
|
|
|
xs *c = xs_regex_replace(v, "<[^>]+>", " ");
|
|
|
|
c = xs_regex_replace_i(c, " {2,}", " ");
|
|
|
|
c = xs_tolower_i(c);
|
|
|
|
|
|
|
|
while (!r && !feof(f)) {
|
|
|
|
xs *rx = xs_strip_i(xs_readline(f));
|
|
|
|
|
2024-05-15 11:27:23 +00:00
|
|
|
if (*rx && xs_regex_match(c, rx)) {
|
2024-05-15 11:31:53 +00:00
|
|
|
srv_debug(1, xs_fmt("content_match: match for '%s'", rx));
|
2024-05-15 11:27:23 +00:00
|
|
|
r = 1;
|
2024-03-11 05:00:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-15 11:29:07 +00:00
|
|
|
xs_list *content_search(snac *user, const char *regex,
|
|
|
|
int priv, int skip, int show, int max_secs, int *timeout)
|
|
|
|
/* returns a list of posts which content matches the regex */
|
|
|
|
{
|
|
|
|
if (regex == NULL || *regex == '\0')
|
|
|
|
return xs_list_new();
|
|
|
|
|
2024-08-23 18:27:07 +00:00
|
|
|
xs *i_regex = xs_utf8_to_lower(regex);
|
2024-05-15 11:29:07 +00:00
|
|
|
|
|
|
|
xs_set seen;
|
|
|
|
|
|
|
|
xs_set_init(&seen);
|
|
|
|
|
|
|
|
if (max_secs == 0)
|
|
|
|
max_secs = 3;
|
|
|
|
|
|
|
|
time_t t = time(NULL) + max_secs;
|
|
|
|
*timeout = 0;
|
|
|
|
|
|
|
|
/* iterate all timelines simultaneously */
|
|
|
|
xs_list *tls[3] = {0};
|
2024-05-23 08:01:37 +00:00
|
|
|
const char *md5s[3] = {0};
|
|
|
|
int c[3] = {0};
|
2024-05-15 11:29:07 +00:00
|
|
|
|
|
|
|
tls[0] = timeline_simple_list(user, "public", 0, XS_ALL); /* public */
|
|
|
|
tls[1] = timeline_instance_list(0, XS_ALL); /* instance */
|
|
|
|
tls[2] = priv ? timeline_simple_list(user, "private", 0, XS_ALL) : xs_list_new(); /* private or none */
|
|
|
|
|
|
|
|
/* first positioning */
|
|
|
|
for (int n = 0; n < 3; n++)
|
|
|
|
xs_list_next(tls[n], &md5s[n], &c[n]);
|
|
|
|
|
|
|
|
show += skip;
|
|
|
|
|
|
|
|
while (show > 0) {
|
|
|
|
/* timeout? */
|
|
|
|
if (time(NULL) > t) {
|
|
|
|
*timeout = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find the newest post */
|
|
|
|
int newest = -1;
|
|
|
|
double mtime = 0.0;
|
|
|
|
|
|
|
|
for (int n = 0; n < 3; n++) {
|
|
|
|
if (md5s[n] != NULL) {
|
|
|
|
xs *fn = _object_fn_by_md5(md5s[n], "content_search");
|
|
|
|
double mt = mtime(fn);
|
|
|
|
|
|
|
|
if (mt > mtime) {
|
|
|
|
newest = n;
|
|
|
|
mtime = mt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newest == -1)
|
|
|
|
break;
|
|
|
|
|
2024-05-23 08:01:37 +00:00
|
|
|
const char *md5 = md5s[newest];
|
2024-05-15 11:29:07 +00:00
|
|
|
|
|
|
|
/* advance the chosen timeline */
|
|
|
|
if (!xs_list_next(tls[newest], &md5s[newest], &c[newest]))
|
|
|
|
md5s[newest] = NULL;
|
|
|
|
|
|
|
|
xs *post = NULL;
|
|
|
|
|
|
|
|
if (!valid_status(object_get_by_md5(md5, &post)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!xs_match(xs_dict_get_def(post, "type", "-"), POSTLIKE_OBJECT_TYPE))
|
|
|
|
continue;
|
|
|
|
|
2024-05-15 11:36:59 +00:00
|
|
|
const char *id = xs_dict_get(post, "id");
|
|
|
|
|
|
|
|
if (id == NULL || is_hidden(user, id))
|
|
|
|
continue;
|
|
|
|
|
2024-12-19 03:24:25 +00:00
|
|
|
/* test for the post URL */
|
|
|
|
if (strcmp(id, regex) == 0) {
|
|
|
|
if (xs_set_add(&seen, md5) == 1)
|
|
|
|
show--;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-12-19 04:19:04 +00:00
|
|
|
/* test for the alternate post id */
|
|
|
|
const char *url = xs_dict_get(post, "url");
|
2024-12-19 13:37:52 +00:00
|
|
|
if (xs_type(url) == XSTYPE_STRING && strcmp(url, regex) == 0) {
|
2024-12-19 04:19:04 +00:00
|
|
|
if (xs_set_add(&seen, md5) == 1)
|
|
|
|
show--;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-08-11 15:54:48 +00:00
|
|
|
xs *c = xs_str_new(NULL);
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *content = xs_dict_get(post, "content");
|
2024-08-11 15:54:48 +00:00
|
|
|
const char *name = xs_dict_get(post, "name");
|
2024-05-15 11:29:07 +00:00
|
|
|
|
2024-08-11 15:54:48 +00:00
|
|
|
if (!xs_is_null(content))
|
|
|
|
c = xs_str_cat(c, content);
|
|
|
|
if (!xs_is_null(name))
|
|
|
|
c = xs_str_cat(c, " ", name);
|
2024-08-11 15:29:09 +00:00
|
|
|
|
|
|
|
/* add alt-texts from attachments */
|
|
|
|
const xs_list *atts = xs_dict_get(post, "attachment");
|
|
|
|
int tc = 0;
|
|
|
|
const xs_dict *att;
|
|
|
|
|
|
|
|
while (xs_list_next(atts, &att, &tc)) {
|
|
|
|
const char *name = xs_dict_get(att, "name");
|
|
|
|
|
|
|
|
if (name != NULL)
|
|
|
|
c = xs_str_cat(c, " ", name);
|
|
|
|
}
|
|
|
|
|
2024-05-15 11:29:07 +00:00
|
|
|
/* strip HTML */
|
2024-08-11 15:29:09 +00:00
|
|
|
c = xs_regex_replace_i(c, "<[^>]+>", " ");
|
2024-05-15 11:29:07 +00:00
|
|
|
c = xs_regex_replace_i(c, " {2,}", " ");
|
2024-08-23 18:27:07 +00:00
|
|
|
|
|
|
|
/* convert to lowercase */
|
|
|
|
xs *lc = xs_utf8_to_lower(c);
|
2024-05-15 11:29:07 +00:00
|
|
|
|
|
|
|
/* apply regex */
|
2024-08-23 18:27:07 +00:00
|
|
|
if (xs_regex_match(lc, i_regex)) {
|
2024-05-15 11:29:07 +00:00
|
|
|
if (xs_set_add(&seen, md5) == 1)
|
|
|
|
show--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
xs_list *r = xs_set_result(&seen);
|
|
|
|
|
|
|
|
if (skip) {
|
|
|
|
/* BAD */
|
|
|
|
while (skip--) {
|
|
|
|
r = xs_list_del(r, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
xs_free(tls[0]);
|
|
|
|
xs_free(tls[1]);
|
|
|
|
xs_free(tls[2]);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-13 14:59:17 +00:00
|
|
|
/** notifications **/
|
|
|
|
|
2023-04-13 15:12:07 +00:00
|
|
|
xs_str *notify_check_time(snac *snac, int reset)
|
|
|
|
/* gets or resets the latest notification check time */
|
|
|
|
{
|
|
|
|
xs_str *t = NULL;
|
|
|
|
xs *fn = xs_fmt("%s/notifydate.txt", snac->basedir);
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if (reset) {
|
|
|
|
if ((f = fopen(fn, "w")) != NULL) {
|
|
|
|
t = tid(0);
|
|
|
|
fprintf(f, "%s\n", t);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
|
|
|
t = xs_readline(f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* never set before */
|
|
|
|
t = xs_fmt("%16.6f", 0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2024-12-19 19:55:56 +00:00
|
|
|
xs_dict *markers_get(snac *snac, const xs_list *markers)
|
|
|
|
{
|
|
|
|
xs_dict *data = NULL;
|
|
|
|
xs_dict *returns = xs_dict_new();
|
|
|
|
xs *fn = xs_fmt("%s/markers.json", snac->basedir);
|
|
|
|
const xs_str *v = NULL;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
|
|
|
data = xs_json_load(f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xs_is_null(data))
|
|
|
|
data = xs_dict_new();
|
|
|
|
|
|
|
|
xs_list_foreach(markers, v) {
|
|
|
|
const xs_dict *mark = xs_dict_get(data, v);
|
|
|
|
if (!xs_is_null(mark)) {
|
|
|
|
returns = xs_dict_append(returns, v, mark);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return returns;
|
|
|
|
}
|
|
|
|
|
|
|
|
xs_dict *markers_set(snac *snac, const char *home_marker, const char *notify_marker)
|
|
|
|
/* gets or sets notification marker */
|
|
|
|
{
|
|
|
|
xs_dict *data = NULL;
|
|
|
|
xs_dict *written = xs_dict_new();
|
|
|
|
xs *fn = xs_fmt("%s/markers.json", snac->basedir);
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
|
|
|
data = xs_json_load(f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xs_is_null(data))
|
|
|
|
data = xs_dict_new();
|
|
|
|
|
|
|
|
if (!xs_is_null(home_marker)) {
|
|
|
|
xs_dict *home = xs_dict_new();
|
|
|
|
home = xs_dict_append(home, "last_read_id", home_marker);
|
|
|
|
home = xs_dict_append(home, "version", xs_stock(0));
|
|
|
|
home = xs_dict_append(home, "updated_at", tid(0));
|
|
|
|
data = xs_dict_set(data, "home", home);
|
|
|
|
written = xs_dict_append(written, "home", home);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!xs_is_null(notify_marker)) {
|
|
|
|
xs_dict *notify = xs_dict_new();
|
|
|
|
notify = xs_dict_append(notify, "last_read_id", notify_marker);
|
|
|
|
notify = xs_dict_append(notify, "version", xs_stock(0));
|
|
|
|
notify = xs_dict_append(notify, "updated_at", tid(0));
|
|
|
|
data = xs_dict_set(data, "notifications", notify);
|
|
|
|
written = xs_dict_append(written, "notifications", notify);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "w")) != NULL) {
|
|
|
|
xs_json_dump(data, 4, f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return written;
|
|
|
|
}
|
2023-04-13 15:12:07 +00:00
|
|
|
|
2023-04-13 14:59:17 +00:00
|
|
|
void notify_add(snac *snac, const char *type, const char *utype,
|
2024-07-31 04:22:13 +00:00
|
|
|
const char *actor, const char *objid, const xs_dict *msg)
|
2023-04-13 14:59:17 +00:00
|
|
|
/* adds a new notification */
|
|
|
|
{
|
|
|
|
xs *ntid = tid(0);
|
|
|
|
xs *fn = xs_fmt("%s/notify/", snac->basedir);
|
2023-05-29 07:07:27 +00:00
|
|
|
xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
|
2023-04-13 14:59:17 +00:00
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
/* create the directory */
|
|
|
|
mkdirx(fn);
|
|
|
|
fn = xs_str_cat(fn, ntid);
|
|
|
|
fn = xs_str_cat(fn, ".json");
|
|
|
|
|
|
|
|
xs *noti = xs_dict_new();
|
|
|
|
|
|
|
|
noti = xs_dict_append(noti, "id", ntid);
|
|
|
|
noti = xs_dict_append(noti, "type", type);
|
|
|
|
noti = xs_dict_append(noti, "utype", utype);
|
|
|
|
noti = xs_dict_append(noti, "actor", actor);
|
|
|
|
noti = xs_dict_append(noti, "date", date);
|
2024-07-31 04:22:13 +00:00
|
|
|
noti = xs_dict_append(noti, "msg", msg);
|
2023-04-13 14:59:17 +00:00
|
|
|
|
2023-04-13 15:56:00 +00:00
|
|
|
if (!xs_is_null(objid))
|
|
|
|
noti = xs_dict_append(noti, "objid", objid);
|
|
|
|
|
2023-04-13 14:59:17 +00:00
|
|
|
if ((f = fopen(fn, "w")) != NULL) {
|
2023-08-08 17:29:34 +00:00
|
|
|
xs_json_dump(noti, 4, f);
|
2023-04-13 14:59:17 +00:00
|
|
|
fclose(f);
|
|
|
|
}
|
2024-02-05 13:53:45 +00:00
|
|
|
|
|
|
|
/* add it to the index if it already exists */
|
|
|
|
xs *idx = xs_fmt("%s/notify.idx", snac->basedir);
|
|
|
|
|
|
|
|
if (mtime(idx) != 0.0) {
|
|
|
|
pthread_mutex_lock(&data_mutex);
|
|
|
|
|
|
|
|
if ((f = fopen(idx, "a")) != NULL) {
|
|
|
|
fprintf(f, "%-32s\n", ntid);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&data_mutex);
|
|
|
|
}
|
2023-04-13 14:59:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-13 15:34:48 +00:00
|
|
|
xs_dict *notify_get(snac *snac, const char *id)
|
|
|
|
/* gets a notification */
|
|
|
|
{
|
2024-02-05 13:53:45 +00:00
|
|
|
/* base file */
|
|
|
|
xs *fn = xs_fmt("%s/notify/%s", snac->basedir, id);
|
|
|
|
|
|
|
|
/* strip spaces and add extension */
|
|
|
|
fn = xs_strip_i(fn);
|
|
|
|
fn = xs_str_cat(fn, ".json");
|
|
|
|
|
2023-04-13 15:34:48 +00:00
|
|
|
FILE *f;
|
|
|
|
xs_dict *out = NULL;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
2023-08-05 12:56:07 +00:00
|
|
|
out = xs_json_load(f);
|
2023-04-13 15:34:48 +00:00
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-02-05 18:34:27 +00:00
|
|
|
xs_list *notify_list(snac *snac, int skip, int show)
|
2024-02-05 13:53:45 +00:00
|
|
|
/* returns a list of notification ids */
|
|
|
|
{
|
|
|
|
xs *idx = xs_fmt("%s/notify.idx", snac->basedir);
|
|
|
|
|
|
|
|
if (mtime(idx) == 0.0) {
|
|
|
|
/* create the index from scratch */
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&data_mutex);
|
|
|
|
|
|
|
|
if ((f = fopen(idx, "w")) != NULL) {
|
|
|
|
xs *spec = xs_fmt("%s/notify/" "*.json", snac->basedir);
|
|
|
|
xs *lst = xs_glob(spec, 1, 0);
|
|
|
|
xs_list *p = lst;
|
2024-05-23 08:01:37 +00:00
|
|
|
const char *v;
|
2024-02-05 13:53:45 +00:00
|
|
|
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
char *p = strrchr(v, '.');
|
|
|
|
if (p) {
|
|
|
|
*p = '\0';
|
|
|
|
fprintf(f, "%-32s\n", v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&data_mutex);
|
|
|
|
}
|
|
|
|
|
2024-02-05 18:34:27 +00:00
|
|
|
return index_list_desc(idx, skip, show);
|
2024-02-05 13:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-02-05 09:18:38 +00:00
|
|
|
int notify_new_num(snac *snac)
|
|
|
|
/* counts the number of new notifications */
|
2023-04-13 15:34:48 +00:00
|
|
|
{
|
2024-02-05 09:18:38 +00:00
|
|
|
xs *t = notify_check_time(snac, 0);
|
2024-02-05 18:34:27 +00:00
|
|
|
xs *lst = notify_list(snac, 0, XS_ALL);
|
2024-02-05 09:18:38 +00:00
|
|
|
int cnt = 0;
|
|
|
|
|
|
|
|
xs_list *p = lst;
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_str *v;
|
2023-04-13 15:34:48 +00:00
|
|
|
|
2024-02-05 09:18:38 +00:00
|
|
|
while (xs_list_iter(&p, &v)) {
|
2024-02-05 13:53:45 +00:00
|
|
|
xs *id = xs_strip_i(xs_dup(v));
|
2023-04-13 15:34:48 +00:00
|
|
|
|
2024-02-05 09:18:38 +00:00
|
|
|
/* old? count no more */
|
|
|
|
if (strcmp(id, t) < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-14 17:39:31 +00:00
|
|
|
void notify_clear(snac *snac)
|
|
|
|
/* clears all notifications */
|
|
|
|
{
|
|
|
|
xs *spec = xs_fmt("%s/notify/" "*", snac->basedir);
|
|
|
|
xs *lst = xs_glob(spec, 0, 0);
|
|
|
|
xs_list *p = lst;
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_str *v;
|
2023-04-14 17:39:31 +00:00
|
|
|
|
|
|
|
while (xs_list_iter(&p, &v))
|
|
|
|
unlink(v);
|
2024-02-05 13:53:45 +00:00
|
|
|
|
|
|
|
xs *idx = xs_fmt("%s/notify.idx", snac->basedir);
|
|
|
|
|
|
|
|
if (mtime(idx) != 0.0) {
|
|
|
|
pthread_mutex_lock(&data_mutex);
|
|
|
|
truncate(idx, 0);
|
|
|
|
pthread_mutex_unlock(&data_mutex);
|
|
|
|
}
|
2023-04-14 17:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-26 04:18:48 +00:00
|
|
|
/** the queue **/
|
|
|
|
|
2023-01-31 19:22:36 +00:00
|
|
|
static xs_dict *_enqueue_put(const char *fn, xs_dict *msg)
|
2022-10-11 06:16:58 +00:00
|
|
|
/* writes safely to the queue */
|
2022-09-23 21:09:09 +00:00
|
|
|
{
|
2022-10-11 06:16:58 +00:00
|
|
|
xs *tfn = xs_fmt("%s.tmp", fn);
|
2022-09-23 21:09:09 +00:00
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(tfn, "w")) != NULL) {
|
2023-08-08 17:29:34 +00:00
|
|
|
xs_json_dump(msg, 4, f);
|
2022-09-23 21:09:09 +00:00
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
rename(tfn, fn);
|
|
|
|
}
|
2022-10-11 06:16:58 +00:00
|
|
|
|
2023-01-31 19:22:36 +00:00
|
|
|
return msg;
|
2022-10-11 06:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-31 20:49:43 +00:00
|
|
|
static xs_dict *_new_qmsg(const char *type, const xs_val *msg, int retries)
|
2023-01-31 20:03:59 +00:00
|
|
|
/* creates a queue message */
|
2022-10-11 06:16:58 +00:00
|
|
|
{
|
|
|
|
int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
|
|
|
|
xs *ntid = tid(retries * 60 * qrt);
|
|
|
|
xs *rn = xs_number_new(retries);
|
|
|
|
|
2023-01-31 20:03:59 +00:00
|
|
|
xs_dict *qmsg = xs_dict_new();
|
|
|
|
|
|
|
|
qmsg = xs_dict_append(qmsg, "type", type);
|
2023-01-31 19:22:36 +00:00
|
|
|
qmsg = xs_dict_append(qmsg, "message", msg);
|
2022-10-11 06:16:58 +00:00
|
|
|
qmsg = xs_dict_append(qmsg, "retries", rn);
|
2023-01-31 20:03:59 +00:00
|
|
|
qmsg = xs_dict_append(qmsg, "ntid", ntid);
|
|
|
|
|
|
|
|
return qmsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-04 07:25:09 +00:00
|
|
|
void enqueue_input(snac *snac, const xs_dict *msg, const xs_dict *req, int retries)
|
2023-01-31 20:03:59 +00:00
|
|
|
/* enqueues an input message */
|
|
|
|
{
|
|
|
|
xs *qmsg = _new_qmsg("input", msg, retries);
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *ntid = xs_dict_get(qmsg, "ntid");
|
2023-01-31 20:03:59 +00:00
|
|
|
xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
|
|
|
|
|
|
|
|
qmsg = xs_dict_append(qmsg, "req", req);
|
2022-10-11 06:16:58 +00:00
|
|
|
|
2023-01-31 19:22:36 +00:00
|
|
|
qmsg = _enqueue_put(fn, qmsg);
|
2022-10-11 06:16:58 +00:00
|
|
|
|
2024-03-04 08:22:06 +00:00
|
|
|
snac_debug(snac, 1, xs_fmt("enqueue_input %s", xs_dict_get(msg, "id")));
|
2022-09-23 21:09:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-06 13:46:51 +00:00
|
|
|
void enqueue_shared_input(const xs_dict *msg, const xs_dict *req, int retries)
|
|
|
|
/* enqueues an input message from the shared input */
|
|
|
|
{
|
|
|
|
xs *qmsg = _new_qmsg("input", msg, retries);
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *ntid = xs_dict_get(qmsg, "ntid");
|
2023-12-06 13:46:51 +00:00
|
|
|
xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
|
|
|
|
|
|
|
|
qmsg = xs_dict_append(qmsg, "req", req);
|
|
|
|
|
|
|
|
qmsg = _enqueue_put(fn, qmsg);
|
|
|
|
|
2024-03-04 08:22:06 +00:00
|
|
|
srv_debug(1, xs_fmt("enqueue_shared_input %s", xs_dict_get(msg, "id")));
|
2023-12-06 13:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-07 12:31:48 +00:00
|
|
|
void enqueue_output_raw(const char *keyid, const char *seckey,
|
2024-05-21 12:12:15 +00:00
|
|
|
const xs_dict *msg, const xs_str *inbox,
|
|
|
|
int retries, int p_status)
|
2022-11-18 07:21:40 +00:00
|
|
|
/* enqueues an output message to an inbox */
|
2022-09-23 17:37:01 +00:00
|
|
|
{
|
2023-01-31 20:15:16 +00:00
|
|
|
xs *qmsg = _new_qmsg("output", msg, retries);
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *ntid = xs_dict_get(qmsg, "ntid");
|
2023-02-07 12:31:48 +00:00
|
|
|
xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
|
2022-09-23 17:37:01 +00:00
|
|
|
|
2023-09-29 08:34:22 +00:00
|
|
|
xs *ns = xs_number_new(p_status);
|
|
|
|
qmsg = xs_dict_append(qmsg, "p_status", ns);
|
|
|
|
|
2023-02-02 03:23:56 +00:00
|
|
|
qmsg = xs_dict_append(qmsg, "inbox", inbox);
|
2023-02-07 12:31:48 +00:00
|
|
|
qmsg = xs_dict_append(qmsg, "keyid", keyid);
|
|
|
|
qmsg = xs_dict_append(qmsg, "seckey", seckey);
|
2022-09-23 17:37:01 +00:00
|
|
|
|
2023-02-08 12:06:49 +00:00
|
|
|
/* if it's to be sent right now, bypass the disk queue and post the job */
|
2024-01-14 11:19:35 +00:00
|
|
|
if (retries == 0 && p_state != NULL)
|
2023-03-02 11:38:02 +00:00
|
|
|
job_post(qmsg, 0);
|
2023-02-08 12:06:49 +00:00
|
|
|
else {
|
|
|
|
qmsg = _enqueue_put(fn, qmsg);
|
|
|
|
srv_debug(1, xs_fmt("enqueue_output %s %s %d", inbox, fn, retries));
|
|
|
|
}
|
2023-02-07 12:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
void enqueue_output(snac *snac, const xs_dict *msg,
|
|
|
|
const xs_str *inbox, int retries, int p_status)
|
2023-02-07 12:31:48 +00:00
|
|
|
/* enqueues an output message to an inbox */
|
|
|
|
{
|
|
|
|
if (xs_startswith(inbox, snac->actor)) {
|
|
|
|
snac_debug(snac, 1, xs_str_new("refusing enqueue to myself"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *seckey = xs_dict_get(snac->key, "secret");
|
2023-02-07 12:31:48 +00:00
|
|
|
|
2023-09-29 08:34:22 +00:00
|
|
|
enqueue_output_raw(snac->actor, seckey, msg, inbox, retries, p_status);
|
2022-09-23 17:37:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
void enqueue_output_by_actor(snac *snac, const xs_dict *msg,
|
|
|
|
const xs_str *actor, int retries)
|
2022-11-18 07:21:40 +00:00
|
|
|
/* enqueues an output message for an actor */
|
|
|
|
{
|
2024-10-08 15:37:15 +00:00
|
|
|
xs *inbox = get_actor_inbox(actor, 1);
|
2022-11-18 07:21:40 +00:00
|
|
|
|
|
|
|
if (!xs_is_null(inbox))
|
2023-09-29 08:34:22 +00:00
|
|
|
enqueue_output(snac, msg, inbox, retries, 0);
|
2022-11-18 07:21:40 +00:00
|
|
|
else
|
|
|
|
snac_log(snac, xs_fmt("enqueue_output_by_actor cannot get inbox %s", actor));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
void enqueue_email(const xs_str *msg, int retries)
|
2022-10-21 17:07:20 +00:00
|
|
|
/* enqueues an email message to be sent */
|
|
|
|
{
|
2023-01-31 20:49:43 +00:00
|
|
|
xs *qmsg = _new_qmsg("email", msg, retries);
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *ntid = xs_dict_get(qmsg, "ntid");
|
2023-02-02 04:21:16 +00:00
|
|
|
xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
|
2022-10-21 17:07:20 +00:00
|
|
|
|
2023-01-31 19:22:36 +00:00
|
|
|
qmsg = _enqueue_put(fn, qmsg);
|
2022-10-21 17:07:20 +00:00
|
|
|
|
2023-02-02 04:21:16 +00:00
|
|
|
srv_debug(1, xs_fmt("enqueue_email %d", retries));
|
2022-10-21 17:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-07 06:37:23 +00:00
|
|
|
void enqueue_telegram(const xs_str *msg, const char *bot, const char *chat_id)
|
|
|
|
/* enqueues a message to be sent via Telegram */
|
|
|
|
{
|
|
|
|
xs *qmsg = _new_qmsg("telegram", msg, 0);
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *ntid = xs_dict_get(qmsg, "ntid");
|
2023-02-07 06:37:23 +00:00
|
|
|
xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
|
|
|
|
|
|
|
|
qmsg = xs_dict_append(qmsg, "bot", bot);
|
|
|
|
qmsg = xs_dict_append(qmsg, "chat_id", chat_id);
|
|
|
|
|
|
|
|
qmsg = _enqueue_put(fn, qmsg);
|
|
|
|
|
2023-12-16 22:24:32 +00:00
|
|
|
srv_debug(1, xs_fmt("enqueue_telegram %s %s", bot, chat_id));
|
2023-02-07 06:37:23 +00:00
|
|
|
}
|
|
|
|
|
2024-01-12 08:54:14 +00:00
|
|
|
void enqueue_ntfy(const xs_str *msg, const char *ntfy_server, const char *ntfy_token)
|
|
|
|
/* enqueues a message to be sent via ntfy */
|
|
|
|
{
|
|
|
|
xs *qmsg = _new_qmsg("ntfy", msg, 0);
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *ntid = xs_dict_get(qmsg, "ntid");
|
2024-01-12 08:54:14 +00:00
|
|
|
xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
|
|
|
|
|
|
|
|
qmsg = xs_dict_append(qmsg, "ntfy_server", ntfy_server);
|
|
|
|
qmsg = xs_dict_append(qmsg, "ntfy_token", ntfy_token);
|
|
|
|
|
|
|
|
|
|
|
|
qmsg = _enqueue_put(fn, qmsg);
|
|
|
|
|
|
|
|
srv_debug(1, xs_fmt("enqueue_ntfy %s %s", ntfy_server, ntfy_token));
|
|
|
|
}
|
2023-02-07 06:37:23 +00:00
|
|
|
|
2023-07-13 16:18:23 +00:00
|
|
|
void enqueue_message(snac *snac, const xs_dict *msg)
|
2022-12-16 06:09:25 +00:00
|
|
|
/* enqueues an output message */
|
|
|
|
{
|
2023-01-31 20:54:50 +00:00
|
|
|
xs *qmsg = _new_qmsg("message", msg, 0);
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *ntid = xs_dict_get(qmsg, "ntid");
|
2023-01-31 20:54:50 +00:00
|
|
|
xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
|
2022-12-16 06:09:25 +00:00
|
|
|
|
2023-01-31 19:22:36 +00:00
|
|
|
qmsg = _enqueue_put(fn, qmsg);
|
2022-12-16 06:09:25 +00:00
|
|
|
|
2023-01-31 20:54:50 +00:00
|
|
|
snac_debug(snac, 0, xs_fmt("enqueue_message %s", xs_dict_get(msg, "id")));
|
2022-12-16 06:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-29 09:07:38 +00:00
|
|
|
void enqueue_close_question(snac *user, const char *id, int end_secs)
|
|
|
|
/* enqueues the closing of a question */
|
|
|
|
{
|
|
|
|
xs *qmsg = _new_qmsg("close_question", id, 0);
|
|
|
|
xs *ntid = tid(end_secs);
|
|
|
|
xs *fn = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
|
|
|
|
|
|
|
|
qmsg = xs_dict_set(qmsg, "ntid", ntid);
|
|
|
|
|
|
|
|
qmsg = _enqueue_put(fn, qmsg);
|
|
|
|
|
|
|
|
snac_debug(user, 0, xs_fmt("enqueue_close_question %s", id));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-04 23:38:22 +00:00
|
|
|
void enqueue_object_request(snac *user, const char *id, int forward_secs)
|
|
|
|
/* enqueues the request of an object in the future */
|
|
|
|
{
|
|
|
|
xs *qmsg = _new_qmsg("object_request", id, 0);
|
|
|
|
xs *ntid = tid(forward_secs);
|
|
|
|
xs *fn = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
|
|
|
|
|
|
|
|
qmsg = xs_dict_set(qmsg, "ntid", ntid);
|
|
|
|
|
|
|
|
qmsg = _enqueue_put(fn, qmsg);
|
|
|
|
|
2024-05-04 23:58:27 +00:00
|
|
|
snac_debug(user, 0, xs_fmt("enqueue_object_request %s %d", id, forward_secs));
|
2024-05-04 23:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-02-20 04:31:34 +00:00
|
|
|
void enqueue_verify_links(snac *user)
|
|
|
|
/* enqueues a link verification */
|
|
|
|
{
|
|
|
|
xs *qmsg = _new_qmsg("verify_links", "", 0);
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *ntid = xs_dict_get(qmsg, "ntid");
|
2024-02-20 04:31:34 +00:00
|
|
|
xs *fn = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
|
|
|
|
|
|
|
|
qmsg = _enqueue_put(fn, qmsg);
|
|
|
|
|
2024-02-20 05:18:41 +00:00
|
|
|
snac_debug(user, 1, xs_fmt("enqueue_verify_links %s", user->actor));
|
2024-02-20 04:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-11 17:16:54 +00:00
|
|
|
void enqueue_actor_refresh(snac *user, const char *actor, int forward_secs)
|
2024-03-12 18:47:37 +00:00
|
|
|
/* enqueues an actor refresh */
|
2024-03-12 16:54:54 +00:00
|
|
|
{
|
2024-05-11 17:16:54 +00:00
|
|
|
xs *qmsg = _new_qmsg("actor_refresh", "", 0);
|
|
|
|
xs *ntid = tid(forward_secs);
|
|
|
|
xs *fn = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
|
2024-03-12 16:54:54 +00:00
|
|
|
|
2024-05-11 17:16:54 +00:00
|
|
|
qmsg = xs_dict_set(qmsg, "ntid", ntid);
|
2024-03-12 16:54:54 +00:00
|
|
|
qmsg = xs_dict_append(qmsg, "actor", actor);
|
|
|
|
|
|
|
|
qmsg = _enqueue_put(fn, qmsg);
|
|
|
|
|
2024-03-12 18:50:26 +00:00
|
|
|
snac_debug(user, 1, xs_fmt("enqueue_actor_refresh %s", actor));
|
2024-03-12 16:54:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-31 20:06:31 +00:00
|
|
|
int was_question_voted(snac *user, const char *id)
|
|
|
|
/* returns true if the user voted in this poll */
|
|
|
|
{
|
|
|
|
xs *children = object_children(id);
|
|
|
|
int voted = 0;
|
|
|
|
xs_list *p;
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_str *md5;
|
2023-05-31 20:06:31 +00:00
|
|
|
|
|
|
|
p = children;
|
|
|
|
while (xs_list_iter(&p, &md5)) {
|
|
|
|
xs *obj = NULL;
|
|
|
|
|
|
|
|
if (valid_status(object_get_by_md5(md5, &obj))) {
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *atto = get_atto(obj);
|
2024-01-18 22:04:37 +00:00
|
|
|
if (atto && strcmp(atto, user->actor) == 0 &&
|
2023-06-15 15:24:44 +00:00
|
|
|
!xs_is_null(xs_dict_get(obj, "name"))) {
|
2023-05-31 20:06:31 +00:00
|
|
|
voted = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return voted;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-02 03:47:59 +00:00
|
|
|
xs_list *user_queue(snac *snac)
|
2022-09-20 10:43:49 +00:00
|
|
|
/* returns a list with filenames that can be dequeued */
|
|
|
|
{
|
2023-02-02 03:47:59 +00:00
|
|
|
xs *spec = xs_fmt("%s/queue/" "*.json", snac->basedir);
|
|
|
|
xs_list *list = xs_list_new();
|
|
|
|
time_t t = time(NULL);
|
|
|
|
xs_list *p;
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_val *v;
|
2022-09-20 10:43:49 +00:00
|
|
|
|
2022-10-18 09:40:10 +00:00
|
|
|
xs *fns = xs_glob(spec, 0, 0);
|
2022-09-20 10:43:49 +00:00
|
|
|
|
2022-10-18 09:40:10 +00:00
|
|
|
p = fns;
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
/* get the retry time from the basename */
|
|
|
|
char *bn = strrchr(v, '/');
|
|
|
|
time_t t2 = atol(bn + 1);
|
2022-09-20 10:43:49 +00:00
|
|
|
|
2022-10-18 09:40:10 +00:00
|
|
|
if (t2 > t)
|
2023-02-02 04:07:20 +00:00
|
|
|
snac_debug(snac, 2, xs_fmt("user_queue not yet time for %s [%ld]", v, t));
|
|
|
|
else {
|
|
|
|
list = xs_list_append(list, v);
|
|
|
|
snac_debug(snac, 2, xs_fmt("user_queue ready for %s", v));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
xs_list *queue(void)
|
|
|
|
/* returns a list with filenames that can be dequeued */
|
|
|
|
{
|
|
|
|
xs *spec = xs_fmt("%s/queue/" "*.json", srv_basedir);
|
|
|
|
xs_list *list = xs_list_new();
|
|
|
|
time_t t = time(NULL);
|
|
|
|
xs_list *p;
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_val *v;
|
2023-02-02 04:07:20 +00:00
|
|
|
|
|
|
|
xs *fns = xs_glob(spec, 0, 0);
|
|
|
|
|
|
|
|
p = fns;
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
/* get the retry time from the basename */
|
|
|
|
char *bn = strrchr(v, '/');
|
|
|
|
time_t t2 = atol(bn + 1);
|
|
|
|
|
|
|
|
if (t2 > t)
|
|
|
|
srv_debug(2, xs_fmt("queue not yet time for %s [%ld]", v, t));
|
2022-10-18 09:40:10 +00:00
|
|
|
else {
|
|
|
|
list = xs_list_append(list, v);
|
2023-02-02 04:07:20 +00:00
|
|
|
srv_debug(2, xs_fmt("queue ready for %s", v));
|
2022-09-20 10:43:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
2022-09-20 10:50:37 +00:00
|
|
|
|
|
|
|
|
2023-06-07 11:09:19 +00:00
|
|
|
xs_dict *queue_get(const char *fn)
|
|
|
|
/* gets a file from a queue */
|
2022-09-20 10:50:37 +00:00
|
|
|
{
|
|
|
|
FILE *f;
|
2023-02-02 03:44:30 +00:00
|
|
|
xs_dict *obj = NULL;
|
2022-09-20 10:50:37 +00:00
|
|
|
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
2023-08-05 12:56:07 +00:00
|
|
|
obj = xs_json_load(f);
|
2022-09-20 10:50:37 +00:00
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
2022-10-04 16:46:12 +00:00
|
|
|
|
|
|
|
|
2023-06-07 11:09:19 +00:00
|
|
|
xs_dict *dequeue(const char *fn)
|
|
|
|
/* dequeues a message */
|
|
|
|
{
|
|
|
|
xs_dict *obj = queue_get(fn);
|
|
|
|
|
2024-01-04 15:27:07 +00:00
|
|
|
unlink(fn);
|
2023-06-07 11:09:19 +00:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-26 04:18:48 +00:00
|
|
|
/** the purge **/
|
|
|
|
|
2023-02-23 17:42:38 +00:00
|
|
|
static int _purge_file(const char *fn, time_t mt)
|
2022-11-26 04:05:57 +00:00
|
|
|
/* purge fn if it's older than days */
|
2022-11-25 10:37:05 +00:00
|
|
|
{
|
2023-02-23 17:42:38 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
2022-11-26 04:05:57 +00:00
|
|
|
if (mtime(fn) < mt) {
|
2022-11-25 10:37:05 +00:00
|
|
|
/* older than the minimum time: delete it */
|
|
|
|
unlink(fn);
|
2023-03-07 13:33:15 +00:00
|
|
|
srv_debug(2, xs_fmt("purged %s", fn));
|
2023-02-23 17:42:38 +00:00
|
|
|
ret = 1;
|
2022-11-25 10:37:05 +00:00
|
|
|
}
|
2023-02-23 17:42:38 +00:00
|
|
|
|
|
|
|
return ret;
|
2022-11-25 10:37:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-07 13:33:15 +00:00
|
|
|
static void _purge_dir(const char *dir, int days)
|
|
|
|
/* purges all files in a directory older than days */
|
2022-10-04 16:46:12 +00:00
|
|
|
{
|
2023-02-23 17:42:38 +00:00
|
|
|
int cnt = 0;
|
|
|
|
|
2022-11-12 07:26:26 +00:00
|
|
|
if (days) {
|
2022-11-26 04:08:56 +00:00
|
|
|
time_t mt = time(NULL) - days * 24 * 3600;
|
2023-03-07 13:33:15 +00:00
|
|
|
xs *spec = xs_fmt("%s/" "*", dir);
|
2022-11-12 07:26:26 +00:00
|
|
|
xs *list = xs_glob(spec, 0, 0);
|
2023-03-07 13:33:15 +00:00
|
|
|
xs_list *p;
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_str *v;
|
2022-11-12 07:26:26 +00:00
|
|
|
|
|
|
|
p = list;
|
2022-11-25 10:37:05 +00:00
|
|
|
while (xs_list_iter(&p, &v))
|
2023-02-23 17:42:38 +00:00
|
|
|
cnt += _purge_file(v, mt);
|
2023-03-07 13:33:15 +00:00
|
|
|
|
|
|
|
srv_debug(1, xs_fmt("purge: %s %d", dir, cnt));
|
2022-11-25 10:37:05 +00:00
|
|
|
}
|
2023-03-07 13:33:15 +00:00
|
|
|
}
|
|
|
|
|
2023-02-23 17:42:38 +00:00
|
|
|
|
2023-03-07 13:33:15 +00:00
|
|
|
static void _purge_user_subdir(snac *snac, const char *subdir, int days)
|
|
|
|
/* purges all files in a user subdir older than days */
|
|
|
|
{
|
|
|
|
xs *u_subdir = xs_fmt("%s/%s", snac->basedir, subdir);
|
|
|
|
|
|
|
|
_purge_dir(u_subdir, days);
|
2022-11-25 10:37:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void purge_server(void)
|
|
|
|
/* purge global server data */
|
|
|
|
{
|
|
|
|
xs *spec = xs_fmt("%s/object/??", srv_basedir);
|
|
|
|
xs *dirs = xs_glob(spec, 0, 0);
|
2023-02-24 09:07:00 +00:00
|
|
|
xs_list *p;
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_str *v;
|
2023-02-23 17:42:38 +00:00
|
|
|
int cnt = 0;
|
2023-02-24 09:07:00 +00:00
|
|
|
int icnt = 0;
|
2022-11-25 10:37:05 +00:00
|
|
|
|
2023-01-04 08:41:02 +00:00
|
|
|
time_t mt = time(NULL) - 7 * 24 * 3600;
|
2022-11-26 04:05:57 +00:00
|
|
|
|
2022-11-25 10:37:05 +00:00
|
|
|
p = dirs;
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
2023-02-24 09:07:00 +00:00
|
|
|
xs_list *p2;
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_str *v2;
|
2023-02-24 09:07:00 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
xs *spec2 = xs_fmt("%s/" "*.json", v);
|
|
|
|
xs *files = xs_glob(spec2, 0, 0);
|
|
|
|
|
|
|
|
p2 = files;
|
|
|
|
while (xs_list_iter(&p2, &v2)) {
|
|
|
|
int n_link;
|
|
|
|
|
|
|
|
/* old and with no hard links? */
|
|
|
|
if (mtime_nl(v2, &n_link) < mt && n_link < 2) {
|
|
|
|
xs *s1 = xs_replace(v2, ".json", "");
|
|
|
|
xs *l = xs_split(s1, "/");
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *md5 = xs_list_get(l, -1);
|
2023-02-24 09:07:00 +00:00
|
|
|
|
|
|
|
object_del_by_md5(md5);
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
/* look for stray indexes */
|
|
|
|
xs *speci = xs_fmt("%s/" "*_?.idx", v);
|
|
|
|
xs *idxfs = xs_glob(speci, 0, 0);
|
|
|
|
|
|
|
|
p2 = idxfs;
|
|
|
|
while (xs_list_iter(&p2, &v2)) {
|
|
|
|
/* old enough to consider? */
|
|
|
|
if (mtime(v2) < mt) {
|
|
|
|
/* check if the indexed object is here */
|
|
|
|
xs *o = xs_dup(v2);
|
|
|
|
char *ext = strchr(o, '_');
|
|
|
|
|
|
|
|
if (ext) {
|
|
|
|
*ext = '\0';
|
|
|
|
o = xs_str_cat(o, ".json");
|
|
|
|
|
|
|
|
if (mtime(o) == 0.0) {
|
|
|
|
/* delete */
|
|
|
|
unlink(v2);
|
|
|
|
srv_debug(1, xs_fmt("purged %s", v2));
|
|
|
|
icnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-26 04:05:57 +00:00
|
|
|
}
|
2024-04-11 17:39:45 +00:00
|
|
|
|
|
|
|
/* delete index backups */
|
|
|
|
xs *specb = xs_fmt("%s/" "*.bak", v);
|
|
|
|
xs *bakfs = xs_glob(specb, 0, 0);
|
|
|
|
|
|
|
|
p2 = bakfs;
|
|
|
|
while (xs_list_iter(&p2, &v2)) {
|
|
|
|
unlink(v2);
|
|
|
|
srv_debug(1, xs_fmt("purged %s", v2));
|
|
|
|
}
|
2022-11-12 07:26:26 +00:00
|
|
|
}
|
2022-10-17 09:26:36 +00:00
|
|
|
}
|
2023-02-23 17:42:38 +00:00
|
|
|
|
2023-03-07 13:33:15 +00:00
|
|
|
/* purge collected inboxes */
|
|
|
|
xs *ib_dir = xs_fmt("%s/inbox", srv_basedir);
|
|
|
|
_purge_dir(ib_dir, 7);
|
|
|
|
|
2023-05-01 06:32:48 +00:00
|
|
|
/* purge the instance timeline */
|
|
|
|
xs *itl_fn = xs_fmt("%s/public.idx", srv_basedir);
|
|
|
|
int itl_gc = index_gc(itl_fn);
|
|
|
|
|
2023-11-08 17:15:51 +00:00
|
|
|
/* purge tag indexes */
|
|
|
|
xs *tag_spec = xs_fmt("%s/tag/??", srv_basedir);
|
|
|
|
xs *tag_dirs = xs_glob(tag_spec, 0, 0);
|
|
|
|
p = tag_dirs;
|
|
|
|
|
|
|
|
int tag_gc = 0;
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
xs *spec2 = xs_fmt("%s/" "*.idx", v);
|
|
|
|
xs *files = xs_glob(spec2, 0, 0);
|
|
|
|
xs_list *p2;
|
2024-05-23 08:01:37 +00:00
|
|
|
const xs_str *v2;
|
2023-11-08 17:15:51 +00:00
|
|
|
|
|
|
|
p2 = files;
|
2023-11-08 17:37:07 +00:00
|
|
|
while (xs_list_iter(&p2, &v2)) {
|
2023-11-08 17:15:51 +00:00
|
|
|
tag_gc += index_gc(v2);
|
2023-11-08 17:37:07 +00:00
|
|
|
xs *bak = xs_fmt("%s.bak", v2);
|
|
|
|
unlink(bak);
|
2023-12-08 07:34:51 +00:00
|
|
|
|
|
|
|
if (index_len(v2) == 0) {
|
|
|
|
/* there are no longer any entry with this tag;
|
|
|
|
purge it completely */
|
|
|
|
unlink(v2);
|
|
|
|
xs *dottag = xs_replace(v2, ".idx", ".tag");
|
|
|
|
unlink(dottag);
|
|
|
|
}
|
2023-11-08 17:37:07 +00:00
|
|
|
}
|
2023-11-08 17:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
srv_debug(1, xs_fmt("purge: global "
|
|
|
|
"(obj: %d, idx: %d, itl: %d, tag: %d)", cnt, icnt, itl_gc, tag_gc));
|
2022-11-12 07:26:26 +00:00
|
|
|
}
|
2022-10-17 09:26:36 +00:00
|
|
|
|
2022-10-04 16:46:12 +00:00
|
|
|
|
2022-11-25 10:15:09 +00:00
|
|
|
void purge_user(snac *snac)
|
|
|
|
/* do the purge for this user */
|
2022-11-12 07:26:26 +00:00
|
|
|
{
|
2023-02-05 18:09:22 +00:00
|
|
|
int priv_days, pub_days, user_days = 0;
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *v;
|
2023-02-23 17:42:38 +00:00
|
|
|
int n;
|
2022-10-04 16:46:12 +00:00
|
|
|
|
2023-02-05 17:13:46 +00:00
|
|
|
priv_days = xs_number_get(xs_dict_get(srv_config, "timeline_purge_days"));
|
|
|
|
pub_days = xs_number_get(xs_dict_get(srv_config, "local_purge_days"));
|
2022-10-04 16:46:12 +00:00
|
|
|
|
2023-02-12 08:26:44 +00:00
|
|
|
if ((v = xs_dict_get(snac->config_o, "purge_days")) != NULL ||
|
|
|
|
(v = xs_dict_get(snac->config, "purge_days")) != NULL)
|
2023-02-05 18:09:22 +00:00
|
|
|
user_days = xs_number_get(v);
|
|
|
|
|
|
|
|
if (user_days) {
|
|
|
|
/* override admin settings only if they are lesser */
|
|
|
|
if (priv_days == 0 || user_days < priv_days)
|
|
|
|
priv_days = user_days;
|
|
|
|
|
|
|
|
if (pub_days == 0 || user_days < pub_days)
|
|
|
|
pub_days = user_days;
|
|
|
|
}
|
|
|
|
|
2023-03-07 13:10:13 +00:00
|
|
|
_purge_user_subdir(snac, "hidden", priv_days);
|
|
|
|
_purge_user_subdir(snac, "private", priv_days);
|
2023-02-05 17:13:46 +00:00
|
|
|
|
2023-03-07 13:10:13 +00:00
|
|
|
_purge_user_subdir(snac, "public", pub_days);
|
2023-02-23 17:42:38 +00:00
|
|
|
|
2024-09-14 14:55:01 +00:00
|
|
|
const char *idxs[] = { "followers.idx", "private.idx", "public.idx",
|
|
|
|
"pinned.idx", "bookmark.idx", "draft.idx", NULL };
|
2023-02-23 17:42:38 +00:00
|
|
|
|
|
|
|
for (n = 0; idxs[n]; n++) {
|
|
|
|
xs *idx = xs_fmt("%s/%s", snac->basedir, idxs[n]);
|
|
|
|
int gc = index_gc(idx);
|
2023-06-26 07:32:59 +00:00
|
|
|
srv_debug(1, xs_fmt("purge: %s %d", idx, gc));
|
2023-02-23 17:42:38 +00:00
|
|
|
}
|
2024-02-20 05:13:34 +00:00
|
|
|
|
2024-04-30 07:14:03 +00:00
|
|
|
/* purge lists */
|
|
|
|
{
|
|
|
|
xs *spec = xs_fmt("%s/list/" "*.idx", snac->basedir);
|
|
|
|
xs *lol = xs_glob(spec, 0, 0);
|
|
|
|
int c = 0;
|
2024-05-23 08:01:37 +00:00
|
|
|
const char *v;
|
2024-04-30 07:14:03 +00:00
|
|
|
|
|
|
|
while (xs_list_next(lol, &v, &c)) {
|
|
|
|
int gc = index_gc(v);
|
|
|
|
srv_debug(1, xs_fmt("purge: %s %d", v, gc));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-20 05:13:34 +00:00
|
|
|
/* unrelated to purging, but it's a janitorial process, so what the hell */
|
2024-02-20 05:38:58 +00:00
|
|
|
verify_links(snac);
|
2022-10-04 16:46:12 +00:00
|
|
|
}
|
2022-10-17 09:00:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
void purge_all(void)
|
|
|
|
/* purge all users */
|
|
|
|
{
|
|
|
|
snac snac;
|
|
|
|
xs *list = user_list();
|
2024-05-23 08:01:37 +00:00
|
|
|
char *p;
|
|
|
|
const char *uid;
|
2022-10-17 09:00:34 +00:00
|
|
|
|
|
|
|
p = list;
|
|
|
|
while (xs_list_iter(&p, &uid)) {
|
|
|
|
if (user_open(&snac, uid)) {
|
2022-11-25 10:15:09 +00:00
|
|
|
purge_user(&snac);
|
2022-10-17 09:00:34 +00:00
|
|
|
user_free(&snac);
|
|
|
|
}
|
|
|
|
}
|
2022-11-25 12:51:56 +00:00
|
|
|
|
|
|
|
purge_server();
|
2023-04-28 06:33:02 +00:00
|
|
|
|
|
|
|
#ifndef NO_MASTODON_API
|
|
|
|
mastoapi_purge();
|
|
|
|
#endif
|
2022-10-17 09:00:34 +00:00
|
|
|
}
|
2023-03-01 07:09:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
/** archive **/
|
|
|
|
|
2023-03-02 16:13:17 +00:00
|
|
|
void srv_archive(const char *direction, const char *url, xs_dict *req,
|
2023-03-01 07:09:56 +00:00
|
|
|
const char *payload, int p_size,
|
|
|
|
int status, xs_dict *headers,
|
|
|
|
const char *body, int b_size)
|
|
|
|
/* archives a connection */
|
|
|
|
{
|
|
|
|
/* obsessive archiving */
|
|
|
|
xs *date = tid(0);
|
|
|
|
xs *dir = xs_fmt("%s/archive/%s_%s", srv_basedir, date, direction);
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if (mkdirx(dir) != -1) {
|
|
|
|
xs *meta_fn = xs_fmt("%s/_META", dir);
|
|
|
|
|
|
|
|
if ((f = fopen(meta_fn, "w")) != NULL) {
|
2023-08-08 17:29:34 +00:00
|
|
|
xs *j1 = xs_json_dumps(req, 4);
|
|
|
|
xs *j2 = xs_json_dumps(headers, 4);
|
2023-03-01 07:09:56 +00:00
|
|
|
|
|
|
|
fprintf(f, "dir: %s\n", direction);
|
2023-03-02 16:13:17 +00:00
|
|
|
|
|
|
|
if (url)
|
|
|
|
fprintf(f, "url: %s\n", url);
|
|
|
|
|
2023-03-01 07:09:56 +00:00
|
|
|
fprintf(f, "req: %s\n", j1);
|
|
|
|
fprintf(f, "p_size: %d\n", p_size);
|
|
|
|
fprintf(f, "status: %d\n", status);
|
|
|
|
fprintf(f, "response: %s\n", j2);
|
|
|
|
fprintf(f, "b_size: %d\n", b_size);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_size && payload) {
|
|
|
|
xs *payload_fn = NULL;
|
|
|
|
xs *payload_fn_raw = NULL;
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *v = xs_dict_get(req, "content-type");
|
2023-03-01 07:09:56 +00:00
|
|
|
|
|
|
|
if (v && xs_str_in(v, "json") != -1) {
|
|
|
|
payload_fn = xs_fmt("%s/payload.json", dir);
|
|
|
|
|
|
|
|
if ((f = fopen(payload_fn, "w")) != NULL) {
|
|
|
|
xs *v1 = xs_json_loads(payload);
|
|
|
|
xs *j1 = NULL;
|
|
|
|
|
|
|
|
if (v1 != NULL)
|
2023-08-08 17:29:34 +00:00
|
|
|
j1 = xs_json_dumps(v1, 4);
|
2023-03-01 07:09:56 +00:00
|
|
|
|
|
|
|
if (j1 != NULL)
|
|
|
|
fwrite(j1, strlen(j1), 1, f);
|
|
|
|
else
|
|
|
|
fwrite(payload, p_size, 1, f);
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
payload_fn_raw = xs_fmt("%s/payload", dir);
|
|
|
|
|
|
|
|
if ((f = fopen(payload_fn_raw, "w")) != NULL) {
|
|
|
|
fwrite(payload, p_size, 1, f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (b_size && body) {
|
|
|
|
xs *body_fn = NULL;
|
2024-05-21 12:12:15 +00:00
|
|
|
const char *v = xs_dict_get(headers, "content-type");
|
2023-03-01 07:09:56 +00:00
|
|
|
|
|
|
|
if (v && xs_str_in(v, "json") != -1) {
|
|
|
|
body_fn = xs_fmt("%s/body.json", dir);
|
|
|
|
|
|
|
|
if ((f = fopen(body_fn, "w")) != NULL) {
|
|
|
|
xs *v1 = xs_json_loads(body);
|
|
|
|
xs *j1 = NULL;
|
|
|
|
|
|
|
|
if (v1 != NULL)
|
2023-08-08 17:29:34 +00:00
|
|
|
j1 = xs_json_dumps(v1, 4);
|
2023-03-01 07:09:56 +00:00
|
|
|
|
|
|
|
if (j1 != NULL)
|
|
|
|
fwrite(j1, strlen(j1), 1, f);
|
|
|
|
else
|
|
|
|
fwrite(body, b_size, 1, f);
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
body_fn = xs_fmt("%s/body", dir);
|
|
|
|
|
|
|
|
if ((f = fopen(body_fn, "w")) != NULL) {
|
|
|
|
fwrite(body, b_size, 1, f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-01 07:25:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
void srv_archive_error(const char *prefix, const xs_str *err,
|
2023-04-12 08:41:15 +00:00
|
|
|
const xs_dict *req, const xs_val *data)
|
2023-03-01 07:25:36 +00:00
|
|
|
/* archives an error */
|
|
|
|
{
|
|
|
|
xs *ntid = tid(0);
|
2023-08-11 20:05:56 +00:00
|
|
|
xs *fn = xs_fmt("%s/error/%s_%s", srv_basedir, ntid, prefix);
|
2023-03-01 07:25:36 +00:00
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "w")) != NULL) {
|
|
|
|
fprintf(f, "Error: %s\n", err);
|
|
|
|
|
|
|
|
if (req) {
|
|
|
|
fprintf(f, "Request headers:\n");
|
|
|
|
|
2023-08-08 17:29:34 +00:00
|
|
|
xs_json_dump(req, 4, f);
|
2023-03-02 16:13:17 +00:00
|
|
|
|
|
|
|
fprintf(f, "\n");
|
2023-03-01 07:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
fprintf(f, "Data:\n");
|
|
|
|
|
2023-04-12 08:41:15 +00:00
|
|
|
if (xs_type(data) == XSTYPE_LIST || xs_type(data) == XSTYPE_DICT) {
|
2023-08-08 17:29:34 +00:00
|
|
|
xs_json_dump(data, 4, f);
|
2023-04-12 08:41:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
fprintf(f, "%s", data);
|
2023-03-02 16:13:17 +00:00
|
|
|
|
|
|
|
fprintf(f, "\n");
|
2023-03-01 07:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
2023-12-17 14:21:16 +00:00
|
|
|
|
|
|
|
|
2024-05-21 12:12:15 +00:00
|
|
|
void srv_archive_qitem(const char *prefix, xs_dict *q_item)
|
2023-12-17 14:21:16 +00:00
|
|
|
/* archives a q_item in the error folder */
|
|
|
|
{
|
|
|
|
xs *ntid = tid(0);
|
2023-12-17 14:27:39 +00:00
|
|
|
xs *fn = xs_fmt("%s/error/%s_qitem_%s", srv_basedir, ntid, prefix);
|
2023-12-17 14:21:16 +00:00
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "w")) != NULL) {
|
|
|
|
xs_json_dump(q_item, 4, f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
2024-05-30 22:30:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
t_announcement *announcement(const double after)
|
|
|
|
/* returns announcement text or NULL if none exists or it is olde than "after" */
|
|
|
|
{
|
|
|
|
static const long int MAX_SIZE = 2048;
|
|
|
|
static t_announcement a = {
|
|
|
|
.text = NULL,
|
|
|
|
.timestamp = 0.0,
|
|
|
|
};
|
|
|
|
static xs_str *fn = NULL;
|
|
|
|
if (fn == NULL)
|
|
|
|
fn = xs_fmt("%s/announcement.txt", srv_basedir);
|
|
|
|
|
|
|
|
const double ts = mtime(fn);
|
|
|
|
|
|
|
|
/* file does not exist or other than what was requested */
|
|
|
|
if (ts == 0.0 || ts <= after)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* nothing changed, just return the current announcement */
|
|
|
|
if (a.text != NULL && ts <= a.timestamp)
|
|
|
|
return &a;
|
|
|
|
|
|
|
|
/* read and store new announcement */
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
|
|
|
fseek (f, 0, SEEK_END);
|
|
|
|
const long int length = ftell(f);
|
|
|
|
|
|
|
|
if (length > MAX_SIZE) {
|
|
|
|
/* this is probably unintentional */
|
|
|
|
srv_log(xs_fmt("announcement.txt too big: %ld bytes, max is %ld, ignoring.", length, MAX_SIZE));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (length > 0) {
|
|
|
|
fseek (f, 0, SEEK_SET);
|
|
|
|
char *buffer = malloc(length + 1);
|
|
|
|
if (buffer) {
|
|
|
|
fread(buffer, 1, length, f);
|
|
|
|
buffer[length] = '\0';
|
|
|
|
|
|
|
|
free(a.text);
|
|
|
|
a.text = buffer;
|
|
|
|
a.timestamp = ts;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
srv_log("Error allocating memory for announcement");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* an empty file means no announcement */
|
|
|
|
free(a.text);
|
|
|
|
a.text = NULL;
|
|
|
|
a.timestamp = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose (f);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a.text != NULL)
|
|
|
|
return &a;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2024-11-07 21:00:25 +00:00
|
|
|
|
|
|
|
|
2024-11-12 04:38:55 +00:00
|
|
|
xs_str *make_url(const char *href, const char *proxy, int by_token)
|
2024-11-07 21:00:25 +00:00
|
|
|
/* makes an URL, possibly including proxying */
|
|
|
|
{
|
|
|
|
xs_str *url = NULL;
|
|
|
|
|
|
|
|
if (proxy && !xs_startswith(href, srv_baseurl)) {
|
2024-11-12 04:47:11 +00:00
|
|
|
xs *p = NULL;
|
|
|
|
|
|
|
|
if (by_token) {
|
2024-11-12 08:01:30 +00:00
|
|
|
xs *tks = xs_fmt("%s:%s", srv_proxy_token_seed, proxy);
|
2024-11-12 04:47:11 +00:00
|
|
|
xs *tk = xs_md5_hex(tks, strlen(tks));
|
|
|
|
|
2024-11-12 05:11:10 +00:00
|
|
|
p = xs_fmt("%s/y/%s/", proxy, tk);
|
2024-11-12 04:47:11 +00:00
|
|
|
}
|
|
|
|
else
|
2024-11-12 05:11:10 +00:00
|
|
|
p = xs_fmt("%s/x/", proxy);
|
2024-11-12 04:47:11 +00:00
|
|
|
|
2024-11-07 21:00:25 +00:00
|
|
|
url = xs_replace(href, "https:/" "/", p);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
url = xs_dup(href);
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
2024-12-19 17:54:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
/** bad login throttle **/
|
|
|
|
|
|
|
|
xs_str *_badlogin_fn(const char *addr)
|
|
|
|
{
|
|
|
|
xs *md5 = xs_md5_hex(addr, strlen(addr));
|
|
|
|
xs *dir = xs_fmt("%s/badlogin", srv_basedir);
|
|
|
|
|
|
|
|
mkdirx(dir);
|
|
|
|
|
|
|
|
return xs_fmt("%s/%s", dir, md5);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int _badlogin_read(const char *fn, int *failures)
|
|
|
|
/* reads a badlogin file */
|
|
|
|
{
|
|
|
|
int ok = 0;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&data_mutex);
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
|
|
|
xs *l = xs_readline(f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
if (sscanf(l, "%d", failures) == 1)
|
|
|
|
ok = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&data_mutex);
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int badlogin_check(const char *user, const char *addr)
|
|
|
|
/* checks if this address is authorized to try a login */
|
|
|
|
{
|
|
|
|
int valid = 1;
|
|
|
|
|
|
|
|
if (xs_type(addr) == XSTYPE_STRING) {
|
|
|
|
xs *fn = _badlogin_fn(addr);
|
|
|
|
double mt = mtime(fn);
|
|
|
|
|
|
|
|
if (mt > 0) {
|
|
|
|
int badlogin_expire = xs_number_get(xs_dict_get_def(srv_config,
|
|
|
|
"badlogin_expire", "300"));
|
|
|
|
|
|
|
|
mt += badlogin_expire;
|
|
|
|
|
|
|
|
/* if file is expired, delete and give pass */
|
|
|
|
if (mt < time(NULL)) {
|
|
|
|
srv_debug(1, xs_fmt("Login from %s for %s allowed again", addr, user));
|
|
|
|
unlink(fn);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int failures;
|
|
|
|
|
|
|
|
if (_badlogin_read(fn, &failures)) {
|
|
|
|
int badlogin_max = xs_number_get(xs_dict_get_def(srv_config,
|
|
|
|
"badlogin_retries", "5"));
|
|
|
|
|
|
|
|
if (failures >= badlogin_max) {
|
|
|
|
valid = 0;
|
|
|
|
|
|
|
|
xs *d = xs_str_iso_date((time_t) mt);
|
|
|
|
|
|
|
|
srv_debug(1,
|
|
|
|
xs_fmt("Login from %s for %s forbidden until %s", addr, user, d));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void badlogin_inc(const char *user, const char *addr)
|
|
|
|
/* increments a bad login from this address */
|
|
|
|
{
|
|
|
|
if (xs_type(addr) == XSTYPE_STRING) {
|
|
|
|
int failures = 0;
|
|
|
|
xs *fn = _badlogin_fn(addr);
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
_badlogin_read(fn, &failures);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&data_mutex);
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "w")) != NULL) {
|
|
|
|
failures++;
|
|
|
|
|
|
|
|
fprintf(f, "%d %s %s\n", failures, addr, user);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
srv_log(xs_fmt("Registered %d login failure(s) from %s for %s", failures, addr, user));
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&data_mutex);
|
|
|
|
}
|
|
|
|
}
|