2022-09-19 20:41:30 +00:00
|
|
|
/* snac - A simple, minimalistic ActivityPub instance */
|
|
|
|
/* copyright (c) 2022 grunfink - MIT license */
|
|
|
|
|
|
|
|
#include "xs.h"
|
|
|
|
#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"
|
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>
|
|
|
|
#include <fcntl.h>
|
2022-09-19 21:33:11 +00:00
|
|
|
|
2022-12-03 17:41:44 +00:00
|
|
|
double db_layout = 2.7;
|
2022-11-23 12:32:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
int db_upgrade(d_char **error);
|
2022-09-19 21:33:11 +00:00
|
|
|
|
2022-11-25 09:53:16 +00:00
|
|
|
int srv_open(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;
|
2022-09-20 07:39:28 +00:00
|
|
|
d_char *error = 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)
|
2022-10-09 15:22:18 +00:00
|
|
|
error = xs_fmt("ERROR: cannot opening '%s'", cfg_file);
|
2022-09-19 20:41:30 +00:00
|
|
|
else {
|
|
|
|
xs *cfg_data;
|
|
|
|
|
|
|
|
/* read full config file */
|
|
|
|
cfg_data = xs_readall(f);
|
2022-10-17 18:25:42 +00:00
|
|
|
fclose(f);
|
2022-09-19 20:41:30 +00:00
|
|
|
|
|
|
|
/* parse */
|
|
|
|
srv_config = xs_json_loads(cfg_data);
|
|
|
|
|
|
|
|
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 {
|
|
|
|
char *host;
|
|
|
|
char *prefix;
|
|
|
|
char *dbglvl;
|
|
|
|
|
|
|
|
host = xs_dict_get(srv_config, "host");
|
|
|
|
prefix = xs_dict_get(srv_config, "prefix");
|
|
|
|
dbglvl = xs_dict_get(srv_config, "dbglevel");
|
|
|
|
|
|
|
|
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 {
|
|
|
|
srv_baseurl = xs_fmt("https://%s%s", host, prefix);
|
|
|
|
|
|
|
|
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)
|
|
|
|
ret = db_upgrade(&error);
|
|
|
|
else {
|
2022-11-25 09:56:34 +00:00
|
|
|
if (xs_number_get(xs_dict_get(srv_config, "layout")) < db_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);
|
|
|
|
|
2022-11-16 10:42:16 +00:00
|
|
|
/* disabled temporarily; messages can't be sent (libcurl issue?) */
|
2022-11-15 07:24:03 +00:00
|
|
|
#if 0
|
2022-11-14 16:40:31 +00:00
|
|
|
#ifdef __OpenBSD__
|
|
|
|
srv_debug(2, xs_fmt("Calling unveil()"));
|
2022-11-15 07:24:03 +00:00
|
|
|
unveil(basedir, "rwc");
|
|
|
|
unveil("/usr/sbin", "x");
|
|
|
|
unveil(NULL, NULL);
|
2022-11-14 16:40:31 +00:00
|
|
|
#endif /* __OpenBSD__ */
|
2022-11-15 07:24:03 +00:00
|
|
|
#endif
|
2022-11-14 16:40:31 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
xs_free(snac->key);
|
|
|
|
xs_free(snac->actor);
|
2022-12-02 18:14:59 +00:00
|
|
|
xs_free(snac->md5);
|
2022-09-19 20:41:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-04 20:14:18 +00:00
|
|
|
int user_open(snac *snac, const char *uid)
|
2022-09-19 20:41:30 +00:00
|
|
|
/* opens a user */
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
memset(snac, '\0', sizeof(struct _snac));
|
|
|
|
|
|
|
|
if (validate_uid(uid)) {
|
|
|
|
xs *cfg_file;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
snac->uid = xs_str_new(uid);
|
|
|
|
|
|
|
|
snac->basedir = xs_fmt("%s/user/%s", srv_basedir, uid);
|
|
|
|
|
|
|
|
cfg_file = xs_fmt("%s/user.json", snac->basedir);
|
|
|
|
|
|
|
|
if ((f = fopen(cfg_file, "r")) != NULL) {
|
|
|
|
xs *cfg_data;
|
|
|
|
|
|
|
|
/* read full config file */
|
|
|
|
cfg_data = xs_readall(f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
if ((snac->config = xs_json_loads(cfg_data)) != NULL) {
|
|
|
|
xs *key_file = xs_fmt("%s/key.json", snac->basedir);
|
|
|
|
|
|
|
|
if ((f = fopen(key_file, "r")) != NULL) {
|
|
|
|
xs *key_data;
|
|
|
|
|
|
|
|
key_data = xs_readall(f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
if ((snac->key = xs_json_loads(key_data)) != NULL) {
|
|
|
|
snac->actor = xs_fmt("%s/%s", srv_baseurl, uid);
|
2022-12-02 18:14:59 +00:00
|
|
|
snac->md5 = xs_md5_hex(snac->actor, strlen(snac->actor));
|
2022-09-19 20:41:30 +00:00
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
srv_log(xs_fmt("cannot parse '%s'", key_file));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
srv_log(xs_fmt("error opening '%s'", key_file));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
srv_log(xs_fmt("cannot parse '%s'", cfg_file));
|
|
|
|
}
|
|
|
|
else
|
2022-09-25 07:55:33 +00:00
|
|
|
srv_debug(2, xs_fmt("error opening '%s'", cfg_file));
|
2022-09-19 20:41:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
srv_log(xs_fmt("invalid user '%s'", uid));
|
|
|
|
|
|
|
|
if (!ret)
|
2022-09-19 21:33:11 +00:00
|
|
|
user_free(snac);
|
2022-09-19 20:41:30 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-03 08:51:52 +00:00
|
|
|
d_char *user_list(void)
|
|
|
|
/* 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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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 **/
|
|
|
|
|
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
|
|
|
{
|
2022-11-24 12:10:03 +00:00
|
|
|
int status = 201; /* Created */
|
2022-11-23 17:58:07 +00:00
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
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
|
|
|
|
status = 500;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-14 04:36:30 +00:00
|
|
|
int index_del_md5(const char *fn, const char *md5)
|
2022-11-24 07:00:35 +00:00
|
|
|
/* deletes an md5 from an index */
|
2022-11-23 18:14:55 +00:00
|
|
|
{
|
|
|
|
int status = 404;
|
|
|
|
FILE *i, *o;
|
|
|
|
|
|
|
|
if ((i = fopen(fn, "r")) != NULL) {
|
|
|
|
flock(fileno(i), LOCK_EX);
|
|
|
|
|
|
|
|
xs *nfn = xs_fmt("%s.new", fn);
|
2022-11-23 19:25:57 +00:00
|
|
|
char line[256];
|
2022-11-23 18:14:55 +00:00
|
|
|
|
|
|
|
if ((o = fopen(nfn, "w")) != NULL) {
|
|
|
|
while (fgets(line, sizeof(line), i) != NULL) {
|
2022-11-23 19:25:57 +00:00
|
|
|
line[32] = '\0';
|
2022-11-23 18:14:55 +00:00
|
|
|
if (memcmp(line, md5, 32) != 0)
|
2022-11-23 19:25:57 +00:00
|
|
|
fprintf(o, "%s\n", line);
|
2022-11-23 18:14:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(o);
|
|
|
|
|
2022-11-23 19:25:57 +00:00
|
|
|
xs *ofn = xs_fmt("%s.bak", fn);
|
2022-11-23 18:14:55 +00:00
|
|
|
|
|
|
|
link(fn, ofn);
|
|
|
|
rename(nfn, fn);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
status = 500;
|
|
|
|
|
|
|
|
fclose(i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
status = 500;
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-14 04:36:30 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
line[32] = '\0';
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-26 16:35:18 +00:00
|
|
|
int index_first(const char *fn, char *line, int size)
|
|
|
|
/* reads the first entry of an index */
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
2022-11-26 18:19:52 +00:00
|
|
|
flock(fileno(f), LOCK_SH);
|
|
|
|
|
2022-11-26 16:35:18 +00:00
|
|
|
if (fgets(line, size, f) != NULL) {
|
|
|
|
line[32] = '\0';
|
|
|
|
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)
|
|
|
|
len = st.st_size / 33;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-24 07:00:35 +00:00
|
|
|
d_char *index_list(const char *fn, int max)
|
|
|
|
/* returns an index as a list */
|
2022-11-23 18:32:26 +00:00
|
|
|
{
|
2022-11-23 19:25:57 +00:00
|
|
|
d_char *list = NULL;
|
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
|
|
|
list = xs_list_new();
|
|
|
|
|
|
|
|
while (n < max && fgets(line, sizeof(line), f) != NULL) {
|
|
|
|
line[32] = '\0';
|
|
|
|
list = xs_list_append(list, line);
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-04 20:28:29 +00:00
|
|
|
d_char *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
|
|
|
{
|
2022-11-23 19:25:57 +00:00
|
|
|
d_char *list = NULL;
|
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
|
|
|
list = xs_list_new();
|
|
|
|
|
2022-12-04 20:28:29 +00:00
|
|
|
/* move to the end minus one entry (or more, if skipping entries) */
|
|
|
|
if (!fseek(f, 0, SEEK_END) && !fseek(f, (skip + 1) * -33, SEEK_CUR)) {
|
|
|
|
while (n < show && fgets(line, sizeof(line), f) != NULL) {
|
2022-11-23 18:32:26 +00:00
|
|
|
line[32] = '\0';
|
|
|
|
list = xs_list_append(list, line);
|
|
|
|
n++;
|
|
|
|
|
|
|
|
/* move backwards 2 entries */
|
2022-11-23 19:25:57 +00:00
|
|
|
if (fseek(f, -66, SEEK_CUR) == -1)
|
2022-11-23 18:32:26 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-25 16:48:43 +00:00
|
|
|
/** objects **/
|
|
|
|
|
2022-11-24 07:00:35 +00:00
|
|
|
d_char *_object_fn_by_md5(const char *md5)
|
|
|
|
{
|
|
|
|
xs *bfn = xs_fmt("%s/object/%c%c", srv_basedir, md5[0], md5[1]);
|
|
|
|
|
|
|
|
mkdir(bfn, 0755);
|
|
|
|
|
|
|
|
return xs_fmt("%s/%s.json", bfn, md5);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
d_char *_object_fn(const char *id)
|
|
|
|
{
|
|
|
|
xs *md5 = xs_md5_hex(id, strlen(id));
|
|
|
|
return _object_fn_by_md5(md5);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-02 20:19:46 +00:00
|
|
|
int object_here_by_md5(char *id)
|
|
|
|
/* checks if an object is already downloaded */
|
|
|
|
{
|
|
|
|
xs *fn = _object_fn_by_md5(id);
|
|
|
|
return mtime(fn) > 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-02 19:44:51 +00:00
|
|
|
int object_here(char *id)
|
|
|
|
/* checks if an object is already downloaded */
|
|
|
|
{
|
|
|
|
xs *fn = _object_fn(id);
|
|
|
|
return mtime(fn) > 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-24 07:00:35 +00:00
|
|
|
int object_get_by_md5(const char *md5, d_char **obj, const char *type)
|
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
|
|
|
{
|
|
|
|
int status = 404;
|
|
|
|
xs *fn = _object_fn_by_md5(md5);
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
|
|
|
flock(fileno(f), LOCK_SH);
|
|
|
|
|
|
|
|
xs *j = xs_readall(f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
*obj = xs_json_loads(j);
|
|
|
|
|
|
|
|
if (*obj) {
|
|
|
|
status = 200;
|
|
|
|
|
|
|
|
/* specific type requested? */
|
|
|
|
if (!xs_is_null(type)) {
|
|
|
|
char *v = xs_dict_get(*obj, "type");
|
|
|
|
|
|
|
|
if (xs_is_null(v) || strcmp(v, type) != 0) {
|
|
|
|
status = 404;
|
|
|
|
*obj = xs_free(*obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*obj = NULL;
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int object_get(const char *id, d_char **obj, const char *type)
|
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));
|
|
|
|
return object_get_by_md5(md5, obj, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-26 04:18:48 +00:00
|
|
|
int _object_add(const char *id, d_char *obj, int ow)
|
2022-11-24 07:00:35 +00:00
|
|
|
/* stores an object */
|
|
|
|
{
|
|
|
|
int status = 201; /* Created */
|
|
|
|
xs *fn = _object_fn(id);
|
|
|
|
FILE *f;
|
|
|
|
|
2022-11-26 04:18:48 +00:00
|
|
|
if (!ow && mtime(fn) > 0.0) {
|
2022-11-24 07:39:30 +00:00
|
|
|
/* object already here */
|
2022-12-04 05:40:17 +00:00
|
|
|
srv_debug(1, xs_fmt("object_add object already here %s", id));
|
2022-11-24 07:39:30 +00:00
|
|
|
return 204; /* No content */
|
|
|
|
}
|
|
|
|
|
2022-11-24 07:00:35 +00:00
|
|
|
if ((f = fopen(fn, "w")) != NULL) {
|
|
|
|
flock(fileno(f), LOCK_EX);
|
|
|
|
|
|
|
|
xs *j = xs_json_dumps_pp(obj, 4);
|
|
|
|
|
|
|
|
fwrite(j, strlen(j), 1, f);
|
|
|
|
fclose(f);
|
2022-11-24 07:09:57 +00:00
|
|
|
|
|
|
|
/* does this object has a parent? */
|
|
|
|
char *in_reply_to = xs_dict_get(obj, "inReplyTo");
|
|
|
|
|
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 */
|
2022-11-26 04:29:09 +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));
|
2022-11-24 07:00:35 +00:00
|
|
|
status = 500;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-26 04:18:48 +00:00
|
|
|
int object_add(const char *id, d_char *obj)
|
|
|
|
/* stores an object */
|
|
|
|
{
|
|
|
|
return _object_add(id, obj, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int object_add_ow(const char *id, d_char *obj)
|
|
|
|
/* 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
|
|
|
{
|
|
|
|
int status = 404;
|
2022-11-26 04:05:57 +00:00
|
|
|
xs *fn = _object_fn_by_md5(md5);
|
2022-11-24 07:00:35 +00:00
|
|
|
|
2022-11-24 07:36:01 +00:00
|
|
|
if (fn != NULL && unlink(fn) != -1) {
|
2022-11-24 07:00:35 +00:00
|
|
|
status = 200;
|
|
|
|
|
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);
|
|
|
|
char *p, *v;
|
|
|
|
|
|
|
|
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 */
|
|
|
|
{
|
|
|
|
xs *fn = _object_fn(id);
|
|
|
|
int n_links;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (mtime_nl(fn, &n_links) > 0.0 && n_links < 2)
|
|
|
|
ret = object_del(id);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-10 10:19:26 +00:00
|
|
|
d_char *_object_index_fn(const char *id, const char *idxsfx)
|
|
|
|
/* returns the filename of an object's index */
|
2022-12-02 18:14:59 +00:00
|
|
|
{
|
2022-12-10 10:19:26 +00:00
|
|
|
d_char *fn = _object_fn(id);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-24 08:58:37 +00:00
|
|
|
d_char *object_children(const char *id)
|
|
|
|
/* 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
|
|
|
|
|
|
|
|
2022-12-02 18:14:59 +00:00
|
|
|
d_char *object_likes(const char *id)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
d_char *object_announces(const char *id)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-02 20:16:34 +00:00
|
|
|
int object_parent(const char *id, char *buf, int size)
|
|
|
|
/* returns the object parent, if any */
|
|
|
|
{
|
|
|
|
xs *fn = _object_fn_by_md5(id);
|
|
|
|
fn = xs_replace_i(fn, ".json", "_p.idx");
|
|
|
|
return index_first(fn, buf, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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 */
|
|
|
|
{
|
2022-11-24 12:10:03 +00:00
|
|
|
int status = 200;
|
|
|
|
xs *fn = _object_fn(id);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-26 04:51:48 +00:00
|
|
|
int _object_user_cache(snac *snac, const char *id, const char *cachedir, int del)
|
|
|
|
/* adds or deletes from a user cache */
|
2022-11-25 16:48:43 +00:00
|
|
|
{
|
|
|
|
xs *ofn = _object_fn(id);
|
|
|
|
xs *l = xs_split(ofn, "/");
|
|
|
|
xs *cfn = xs_fmt("%s/%s/%s", snac->basedir, cachedir, xs_list_get(l, -1));
|
2022-11-26 04:58:13 +00:00
|
|
|
xs *idx = xs_fmt("%s/%s.idx", snac->basedir, cachedir);
|
|
|
|
int ret;
|
2022-11-25 16:48:43 +00:00
|
|
|
|
2022-11-26 04:58:13 +00:00
|
|
|
if (del) {
|
2022-11-26 05:20:41 +00:00
|
|
|
if ((ret = unlink(cfn)) != -1)
|
|
|
|
index_del(idx, id);
|
2022-11-26 04:58:13 +00:00
|
|
|
}
|
|
|
|
else {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int object_user_cache_add(snac *snac, const char *id, const char *cachedir)
|
|
|
|
/* caches an object into a user cache */
|
|
|
|
{
|
|
|
|
return _object_user_cache(snac, id, cachedir, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int object_user_cache_del(snac *snac, const char *id, const char *cachedir)
|
|
|
|
/* deletes an object from a user cache */
|
|
|
|
{
|
|
|
|
return _object_user_cache(snac, id, cachedir, 1);
|
2022-11-25 16:48:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-28 07:34:19 +00:00
|
|
|
int object_user_cache_in(snac *snac, const char *id, const char *cachedir)
|
|
|
|
/* checks if an object is stored in a cache */
|
|
|
|
{
|
|
|
|
xs *md5 = xs_md5_hex(id, strlen(id));
|
|
|
|
xs *cfn = xs_fmt("%s/%s/%s.json", snac->basedir, cachedir, md5);
|
|
|
|
|
|
|
|
return !!(mtime(cfn) != 0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-28 09:46:42 +00:00
|
|
|
d_char *object_user_cache_list(snac *snac, const char *cachedir, int max)
|
|
|
|
/* returns the objects in a cache as a list */
|
2022-09-20 07:39:28 +00:00
|
|
|
{
|
2022-11-28 09:46:42 +00:00
|
|
|
xs *idx = xs_fmt("%s/%s.idx", snac->basedir, cachedir);
|
|
|
|
return 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
|
|
|
|
2022-11-28 10:24:19 +00:00
|
|
|
return ret == -1 ? 500 : 200;
|
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
|
|
|
|
2022-11-28 10:24:19 +00:00
|
|
|
return ret == -1 ? 404 : 200;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
d_char *follower_list(snac *snac)
|
|
|
|
/* returns the list of followers */
|
|
|
|
{
|
2022-11-28 09:46:42 +00:00
|
|
|
xs *list = object_user_cache_list(snac, "followers", XS_ALL);
|
|
|
|
d_char *fwers = xs_list_new();
|
2022-10-03 08:56:15 +00:00
|
|
|
char *p, *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
|
|
|
|
2022-11-28 09:46:42 +00:00
|
|
|
if (valid_status(object_get_by_md5(v, &a_obj, NULL))) {
|
|
|
|
char *actor = xs_dict_get(a_obj, "id");
|
2022-09-20 07:48:13 +00:00
|
|
|
|
2022-11-28 09:46:42 +00:00
|
|
|
if (!xs_is_null(actor))
|
|
|
|
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
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-20 08:34:32 +00:00
|
|
|
int timeline_del(snac *snac, 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
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (valid_status(object_get(id, &msg, NULL))) {
|
|
|
|
/* if its ours and is public, also store in public */
|
|
|
|
if (is_msg_public(snac, msg))
|
|
|
|
object_user_cache_add(snac, id, "public");
|
|
|
|
}
|
|
|
|
}
|
2022-11-25 13:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-11 19:40:13 +00:00
|
|
|
int timeline_add(snac *snac, char *id, char *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
|
|
|
|
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
|
|
|
|
2023-01-11 19:47:36 +00:00
|
|
|
void timeline_admire(snac *snac, char *id, 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
|
|
|
|
2022-12-02 19:28:27 +00:00
|
|
|
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));
|
2022-09-25 15:42:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-02 18:14:59 +00:00
|
|
|
d_char *timeline_top_level(d_char *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;
|
|
|
|
char *p, *v;
|
|
|
|
|
|
|
|
xs_set_init(&seen);
|
|
|
|
|
|
|
|
p = list;
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
char line[256] = "";
|
|
|
|
|
|
|
|
strcpy(line, v);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
char line2[256];
|
|
|
|
|
2022-11-26 17:52:51 +00:00
|
|
|
/* if it doesn't have a parent, use this */
|
2022-12-02 20:16:34 +00:00
|
|
|
if (!object_parent(line, line2, sizeof(line2)))
|
2022-11-26 17:04:05 +00:00
|
|
|
break;
|
|
|
|
|
2022-11-26 17:52:51 +00:00
|
|
|
/* well, there is a parent... but if it's not there, use this */
|
2022-12-02 20:19:46 +00:00
|
|
|
if (!object_here_by_md5(line2))
|
2022-11-26 17:04:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* it's here! try again with its own parent */
|
|
|
|
strcpy(line, line2);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-04 20:28:29 +00:00
|
|
|
d_char *timeline_simple_list(snac *snac, 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
|
|
|
{
|
|
|
|
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 */
|
2022-12-04 20:28:29 +00:00
|
|
|
if (show > c_max)
|
|
|
|
show = c_max;
|
2022-12-02 18:14:59 +00:00
|
|
|
|
2022-12-03 06:27:26 +00:00
|
|
|
xs *idx = xs_fmt("%s/%s.idx", snac->basedir, idx_name);
|
|
|
|
|
2022-12-04 20:28:29 +00:00
|
|
|
return index_list_desc(idx, skip, show);
|
2022-12-03 06:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-04 20:28:29 +00:00
|
|
|
d_char *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) */
|
|
|
|
{
|
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
|
|
|
|
|
|
|
return timeline_top_level(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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) */
|
|
|
|
|
2022-09-20 09:31:56 +00:00
|
|
|
d_char *_following_fn(snac *snac, char *actor)
|
|
|
|
{
|
|
|
|
xs *md5 = xs_md5_hex(actor, strlen(actor));
|
|
|
|
return xs_fmt("%s/following/%s.json", snac->basedir, md5);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int following_add(snac *snac, char *actor, char *msg)
|
|
|
|
/* adds to the following list */
|
|
|
|
{
|
|
|
|
int ret = 201; /* created */
|
|
|
|
xs *fn = _following_fn(snac, actor);
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "w")) != NULL) {
|
|
|
|
xs *j = xs_json_dumps_pp(msg, 4);
|
|
|
|
|
|
|
|
fwrite(j, 1, strlen(j), f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = 500;
|
|
|
|
|
|
|
|
snac_debug(snac, 2, xs_fmt("following_add %s %s", actor, fn));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int following_del(snac *snac, 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);
|
|
|
|
|
|
|
|
unlink(fn);
|
|
|
|
|
|
|
|
snac_debug(snac, 2, xs_fmt("following_del %s %s", actor, fn));
|
|
|
|
|
|
|
|
return 200;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int following_check(snac *snac, 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
|
|
|
|
|
|
|
|
2022-10-01 07:12:33 +00:00
|
|
|
int following_get(snac *snac, char *actor, d_char **data)
|
|
|
|
/* returns the 'Follow' object */
|
|
|
|
{
|
|
|
|
xs *fn = _following_fn(snac, actor);
|
|
|
|
FILE *f;
|
|
|
|
int status = 200;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
|
|
|
xs *j = xs_readall(f);
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
*data = xs_json_loads(j);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
status = 404;
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-02 09:49:16 +00:00
|
|
|
d_char *following_list(snac *snac)
|
|
|
|
/* returns the list of people being followed */
|
|
|
|
{
|
|
|
|
xs *spec = xs_fmt("%s/following/" "*.json", snac->basedir);
|
|
|
|
xs *glist = xs_glob(spec, 0, 0);
|
|
|
|
char *p, *v;
|
|
|
|
d_char *list = xs_list_new();
|
|
|
|
|
|
|
|
/* 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) {
|
|
|
|
xs *j = xs_readall(f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
if (j != NULL) {
|
|
|
|
xs *o = xs_json_loads(j);
|
|
|
|
|
|
|
|
if (o != NULL) {
|
|
|
|
char *type = xs_dict_get(o, "type");
|
|
|
|
|
2022-11-28 10:06:46 +00:00
|
|
|
if (!xs_is_null(type) && strcmp(type, "Accept") == 0) {
|
|
|
|
char *actor = xs_dict_get(o, "actor");
|
|
|
|
|
|
|
|
if (!xs_is_null(actor))
|
|
|
|
list = xs_list_append(list, actor);
|
|
|
|
}
|
2022-11-02 09:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-20 09:38:18 +00:00
|
|
|
d_char *_muted_fn(snac *snac, char *actor)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void mute(snac *snac, char *actor)
|
|
|
|
/* 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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void unmute(snac *snac, char *actor)
|
|
|
|
/* 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));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int is_muted(snac *snac, char *actor)
|
|
|
|
/* check if someone is muted */
|
|
|
|
{
|
|
|
|
xs *fn = _muted_fn(snac, actor);
|
|
|
|
|
|
|
|
return !!(mtime(fn) != 0.0);
|
|
|
|
}
|
2022-09-20 10:00:13 +00:00
|
|
|
|
|
|
|
|
2022-11-24 08:39:16 +00:00
|
|
|
d_char *_hidden_fn(snac *snac, const char *id)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
char *p, *v;
|
|
|
|
|
|
|
|
p = chld;
|
2022-11-24 11:21:54 +00:00
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
xs *co = NULL;
|
|
|
|
|
|
|
|
/* resolve to get the id */
|
|
|
|
if (valid_status(object_get_by_md5(v, &co, NULL))) {
|
|
|
|
if ((v = xs_dict_get(co, "id")) != NULL)
|
|
|
|
hide(snac, v);
|
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-23 15:13:51 +00:00
|
|
|
int actor_add(snac *snac, const char *actor, d_char *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
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-23 15:13:51 +00:00
|
|
|
int actor_get(snac *snac, const char *actor, d_char **data)
|
2022-09-22 16:50:39 +00:00
|
|
|
/* returns an already downloaded actor */
|
|
|
|
{
|
2022-11-23 15:13:51 +00:00
|
|
|
int status = 200;
|
2022-12-06 18:28:29 +00:00
|
|
|
d_char *d;
|
2022-09-22 16:50:39 +00:00
|
|
|
|
2022-10-25 16:44:29 +00:00
|
|
|
if (strcmp(actor, snac->actor) == 0) {
|
2022-11-23 15:13:51 +00:00
|
|
|
/* this actor */
|
2022-10-25 16:44:29 +00:00
|
|
|
if (data)
|
|
|
|
*data = msg_actor(snac);
|
|
|
|
|
2022-11-23 15:13:51 +00:00
|
|
|
return status;
|
2022-10-25 16:44:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 15:13:51 +00:00
|
|
|
/* read the object */
|
2022-11-23 20:43:00 +00:00
|
|
|
if (!valid_status(status = object_get(actor, &d, NULL)))
|
2022-11-23 15:13:51 +00:00
|
|
|
return status;
|
2022-09-22 16:50:39 +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
|
|
|
|
2022-11-23 17:59:18 +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 */
|
2022-11-23 15:13:51 +00:00
|
|
|
FILE *f;
|
2022-09-22 16:50:39 +00:00
|
|
|
|
|
|
|
if ((f = fopen(fn, "a")) != NULL) {
|
|
|
|
/* write a blank at the end to 'touch' the file */
|
|
|
|
fwrite(" ", 1, 1, f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
2022-09-22 16:56:50 +00:00
|
|
|
|
2022-09-23 17:07:45 +00:00
|
|
|
status = 205; /* "205: Reset Content" "110: Response Is Stale" */
|
2022-09-22 16:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-16 16:03:28 +00:00
|
|
|
d_char *_static_fn(snac *snac, const char *id)
|
2022-09-28 07:29:09 +00:00
|
|
|
/* gets the filename for a static file */
|
|
|
|
{
|
|
|
|
return xs_fmt("%s/static/%s", snac->basedir, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-16 16:03:28 +00:00
|
|
|
int static_get(snac *snac, const char *id, d_char **data, int *size)
|
2022-09-28 07:29:09 +00:00
|
|
|
/* returns static content */
|
|
|
|
{
|
|
|
|
xs *fn = _static_fn(snac, id);
|
|
|
|
FILE *f;
|
|
|
|
int status = 404;
|
|
|
|
|
2022-11-25 11:21:26 +00:00
|
|
|
*size = XS_ALL;
|
2022-09-28 07:29:09 +00:00
|
|
|
|
|
|
|
if ((f = fopen(fn, "rb")) != NULL) {
|
|
|
|
*data = xs_read(f, size);
|
2022-10-17 18:25:42 +00:00
|
|
|
fclose(f);
|
|
|
|
|
2022-09-28 07:29:09 +00:00
|
|
|
status = 200;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "wb")) != NULL) {
|
|
|
|
fwrite(data, size, 1, f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-30 07:56:29 +00:00
|
|
|
d_char *_history_fn(snac *snac, char *id)
|
|
|
|
/* gets the filename for the history */
|
|
|
|
{
|
|
|
|
return xs_fmt("%s/history/%s", snac->basedir, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-30 07:59:13 +00:00
|
|
|
double history_mtime(snac *snac, 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void history_add(snac *snac, char *id, char *content, int size)
|
|
|
|
/* adds something to the history */
|
|
|
|
{
|
|
|
|
xs *fn = _history_fn(snac, id);
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "w")) != NULL) {
|
|
|
|
fwrite(content, size, 1, f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
d_char *history_get(snac *snac, char *id)
|
|
|
|
{
|
|
|
|
d_char *content = NULL;
|
|
|
|
xs *fn = _history_fn(snac, id);
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
|
|
|
content = xs_readall(f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-01 05:45:36 +00:00
|
|
|
int history_del(snac *snac, char *id)
|
|
|
|
{
|
|
|
|
xs *fn = _history_fn(snac, id);
|
|
|
|
return unlink(fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-02 16:16:58 +00:00
|
|
|
d_char *history_list(snac *snac)
|
|
|
|
{
|
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
|
|
|
|
2022-10-03 09:14:16 +00:00
|
|
|
return xs_glob(spec, 1, 0);
|
2022-10-02 16:16:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-26 04:18:48 +00:00
|
|
|
/** the queue **/
|
|
|
|
|
2022-10-11 06:16:58 +00:00
|
|
|
static int _enqueue_put(char *fn, char *msg)
|
|
|
|
/* writes safely to the queue */
|
2022-09-23 21:09:09 +00:00
|
|
|
{
|
2022-10-11 06:16:58 +00:00
|
|
|
int ret = 1;
|
|
|
|
xs *tfn = xs_fmt("%s.tmp", fn);
|
2022-09-23 21:09:09 +00:00
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(tfn, "w")) != NULL) {
|
2022-10-11 06:16:58 +00:00
|
|
|
xs *j = xs_json_dumps_pp(msg, 4);
|
2022-09-23 21:09:09 +00:00
|
|
|
|
|
|
|
fwrite(j, strlen(j), 1, f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
rename(tfn, fn);
|
|
|
|
}
|
2022-10-11 06:16:58 +00:00
|
|
|
else
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void enqueue_input(snac *snac, char *msg, char *req, int retries)
|
|
|
|
/* enqueues an input message */
|
|
|
|
{
|
|
|
|
int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
|
|
|
|
xs *ntid = tid(retries * 60 * qrt);
|
|
|
|
xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
|
|
|
|
xs *qmsg = xs_dict_new();
|
|
|
|
xs *rn = xs_number_new(retries);
|
|
|
|
|
|
|
|
qmsg = xs_dict_append(qmsg, "type", "input");
|
|
|
|
qmsg = xs_dict_append(qmsg, "object", msg);
|
|
|
|
qmsg = xs_dict_append(qmsg, "req", req);
|
|
|
|
qmsg = xs_dict_append(qmsg, "retries", rn);
|
|
|
|
|
|
|
|
_enqueue_put(fn, qmsg);
|
|
|
|
|
|
|
|
snac_debug(snac, 1, xs_fmt("enqueue_input %s", fn));
|
2022-09-23 21:09:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-17 17:33:54 +00:00
|
|
|
void enqueue_output(snac *snac, char *msg, char *inbox, int retries)
|
2022-11-18 07:21:40 +00:00
|
|
|
/* enqueues an output message to an inbox */
|
2022-09-23 17:37:01 +00:00
|
|
|
{
|
2022-11-17 17:33:54 +00:00
|
|
|
if (xs_startswith(inbox, snac->actor)) {
|
|
|
|
snac_debug(snac, 1, xs_str_new("refusing enqueue to myself"));
|
2022-09-23 17:37:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-26 09:19:45 +00:00
|
|
|
int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
|
2022-09-23 17:37:01 +00:00
|
|
|
xs *ntid = tid(retries * 60 * qrt);
|
|
|
|
xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
|
2022-10-11 06:16:58 +00:00
|
|
|
xs *qmsg = xs_dict_new();
|
|
|
|
xs *rn = xs_number_new(retries);
|
2022-09-23 17:37:01 +00:00
|
|
|
|
2022-10-11 06:16:58 +00:00
|
|
|
qmsg = xs_dict_append(qmsg, "type", "output");
|
2022-11-17 17:33:54 +00:00
|
|
|
qmsg = xs_dict_append(qmsg, "inbox", inbox);
|
2022-10-11 06:16:58 +00:00
|
|
|
qmsg = xs_dict_append(qmsg, "object", msg);
|
|
|
|
qmsg = xs_dict_append(qmsg, "retries", rn);
|
2022-09-23 17:37:01 +00:00
|
|
|
|
2022-10-11 06:16:58 +00:00
|
|
|
_enqueue_put(fn, qmsg);
|
2022-09-23 17:37:01 +00:00
|
|
|
|
2022-11-17 17:33:54 +00:00
|
|
|
snac_debug(snac, 1, xs_fmt("enqueue_output %s %s %d", inbox, fn, retries));
|
2022-09-23 17:37:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-18 07:21:40 +00:00
|
|
|
void enqueue_output_by_actor(snac *snac, char *msg, char *actor, int retries)
|
|
|
|
/* enqueues an output message for an actor */
|
|
|
|
{
|
|
|
|
xs *inbox = get_actor_inbox(snac, actor);
|
|
|
|
|
|
|
|
if (!xs_is_null(inbox))
|
|
|
|
enqueue_output(snac, msg, inbox, retries);
|
|
|
|
else
|
|
|
|
snac_log(snac, xs_fmt("enqueue_output_by_actor cannot get inbox %s", actor));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-21 17:07:20 +00:00
|
|
|
void enqueue_email(snac *snac, char *msg, int retries)
|
|
|
|
/* enqueues an email message to be sent */
|
|
|
|
{
|
|
|
|
int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
|
|
|
|
xs *ntid = tid(retries * 60 * qrt);
|
|
|
|
xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
|
|
|
|
xs *qmsg = xs_dict_new();
|
|
|
|
xs *rn = xs_number_new(retries);
|
|
|
|
|
|
|
|
qmsg = xs_dict_append(qmsg, "type", "email");
|
|
|
|
qmsg = xs_dict_append(qmsg, "message", msg);
|
|
|
|
qmsg = xs_dict_append(qmsg, "retries", rn);
|
|
|
|
|
|
|
|
_enqueue_put(fn, qmsg);
|
|
|
|
|
|
|
|
snac_debug(snac, 1, xs_fmt("enqueue_email %d", retries));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-16 06:09:25 +00:00
|
|
|
void enqueue_message(snac *snac, char *msg)
|
|
|
|
/* enqueues an output message */
|
|
|
|
{
|
|
|
|
char *id = xs_dict_get(msg, "id");
|
|
|
|
xs *ntid = tid(0);
|
|
|
|
xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
|
|
|
|
xs *qmsg = xs_dict_new();
|
|
|
|
|
|
|
|
qmsg = xs_dict_append(qmsg, "type", "message");
|
|
|
|
qmsg = xs_dict_append(qmsg, "message", msg);
|
|
|
|
|
|
|
|
_enqueue_put(fn, qmsg);
|
|
|
|
|
2022-12-16 06:16:00 +00:00
|
|
|
snac_debug(snac, 0, xs_fmt("enqueue_message %s", id));
|
2022-12-16 06:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-20 10:43:49 +00:00
|
|
|
d_char *queue(snac *snac)
|
|
|
|
/* returns a list with filenames that can be dequeued */
|
|
|
|
{
|
2022-10-18 09:40:10 +00:00
|
|
|
xs *spec = xs_fmt("%s/queue/" "*.json", snac->basedir);
|
2022-09-20 10:43:49 +00:00
|
|
|
d_char *list = xs_list_new();
|
2022-10-18 09:40:10 +00:00
|
|
|
time_t t = time(NULL);
|
|
|
|
char *p, *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)
|
2022-10-21 17:07:20 +00:00
|
|
|
snac_debug(snac, 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);
|
|
|
|
snac_debug(snac, 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
|
|
|
|
|
|
|
|
|
|
|
d_char *dequeue(snac *snac, char *fn)
|
|
|
|
/* dequeues a message */
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
d_char *obj = NULL;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
|
|
|
/* delete right now */
|
|
|
|
unlink(fn);
|
|
|
|
|
|
|
|
xs *j = xs_readall(f);
|
|
|
|
obj = xs_json_loads(j);
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
2022-10-04 16:46:12 +00:00
|
|
|
|
|
|
|
|
2022-11-26 04:18:48 +00:00
|
|
|
/** the purge **/
|
|
|
|
|
2022-11-26 04:08:56 +00:00
|
|
|
static void _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
|
|
|
{
|
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);
|
|
|
|
srv_debug(1, xs_fmt("purged %s", fn));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-12 07:26:26 +00:00
|
|
|
static void _purge_subdir(snac *snac, const char *subdir, int days)
|
|
|
|
/* purges all files in subdir older than days */
|
2022-10-04 16:46:12 +00:00
|
|
|
{
|
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;
|
2022-11-25 10:15:09 +00:00
|
|
|
xs *spec = xs_fmt("%s/%s/" "*", snac->basedir, subdir);
|
2022-11-12 07:26:26 +00:00
|
|
|
xs *list = xs_glob(spec, 0, 0);
|
|
|
|
char *p, *v;
|
|
|
|
|
|
|
|
p = list;
|
2022-11-25 10:37:05 +00:00
|
|
|
while (xs_list_iter(&p, &v))
|
2022-11-26 04:08:56 +00:00
|
|
|
_purge_file(v, mt);
|
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);
|
|
|
|
char *p, *v;
|
|
|
|
|
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)) {
|
2022-11-26 04:05:57 +00:00
|
|
|
xs *spec2 = xs_fmt("%s/" "*.json", v);
|
2022-11-25 10:37:05 +00:00
|
|
|
xs *files = xs_glob(spec2, 0, 0);
|
|
|
|
char *p2, *v2;
|
|
|
|
|
|
|
|
p2 = files;
|
|
|
|
while (xs_list_iter(&p2, &v2)) {
|
2022-11-26 04:05:57 +00:00
|
|
|
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, "/");
|
|
|
|
char *md5 = xs_list_get(l, -1);
|
|
|
|
|
|
|
|
object_del_by_md5(md5);
|
|
|
|
}
|
2022-11-12 07:26:26 +00:00
|
|
|
}
|
2022-10-17 09:26:36 +00:00
|
|
|
}
|
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
|
|
|
{
|
|
|
|
int days;
|
2022-10-04 16:46:12 +00:00
|
|
|
|
2022-11-12 07:26:26 +00:00
|
|
|
days = xs_number_get(xs_dict_get(srv_config, "timeline_purge_days"));
|
|
|
|
_purge_subdir(snac, "timeline", days);
|
2022-11-25 10:15:09 +00:00
|
|
|
_purge_subdir(snac, "hidden", days);
|
2022-11-25 16:42:01 +00:00
|
|
|
_purge_subdir(snac, "private", days);
|
2022-10-04 16:46:12 +00:00
|
|
|
|
2022-11-12 07:26:26 +00:00
|
|
|
days = xs_number_get(xs_dict_get(srv_config, "local_purge_days"));
|
|
|
|
_purge_subdir(snac, "local", days);
|
2022-11-25 16:42:01 +00:00
|
|
|
_purge_subdir(snac, "public", days);
|
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();
|
|
|
|
char *p, *uid;
|
|
|
|
|
|
|
|
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();
|
2022-10-17 09:00:34 +00:00
|
|
|
}
|