2022-09-19 23:41:30 +03: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 10:39:28 +03:00
|
|
|
#include "xs_openssl.h"
|
2022-10-03 12:18:49 +03:00
|
|
|
#include "xs_glob.h"
|
2022-09-19 23:41:30 +03:00
|
|
|
|
|
|
|
#include "snac.h"
|
|
|
|
|
2022-09-20 13:56:21 +03:00
|
|
|
#include <time.h>
|
|
|
|
#include <sys/stat.h>
|
2022-11-23 17:34:07 +03:00
|
|
|
#include <sys/file.h>
|
|
|
|
#include <fcntl.h>
|
2022-09-20 00:33:11 +03:00
|
|
|
|
2022-11-24 11:49:54 +03:00
|
|
|
double db_layout = 2.3;
|
2022-11-23 15:32:23 +03:00
|
|
|
|
|
|
|
|
|
|
|
int db_upgrade(d_char **error);
|
2022-09-20 00:33:11 +03:00
|
|
|
|
2022-09-19 23:41:30 +03:00
|
|
|
int srv_open(char *basedir)
|
|
|
|
/* opens a server */
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
xs *cfg_file = NULL;
|
|
|
|
FILE *f;
|
2022-09-20 10:39:28 +03:00
|
|
|
d_char *error = NULL;
|
2022-09-19 23:41:30 +03:00
|
|
|
|
|
|
|
srv_basedir = xs_str_new(basedir);
|
|
|
|
|
2022-09-20 10:39:28 +03:00
|
|
|
if (xs_endswith(srv_basedir, "/"))
|
|
|
|
srv_basedir = xs_crop(srv_basedir, 0, -1);
|
|
|
|
|
2022-09-19 23:41:30 +03:00
|
|
|
cfg_file = xs_fmt("%s/server.json", basedir);
|
|
|
|
|
|
|
|
if ((f = fopen(cfg_file, "r")) == NULL)
|
2022-10-09 18:22:18 +03:00
|
|
|
error = xs_fmt("ERROR: cannot opening '%s'", cfg_file);
|
2022-09-19 23:41:30 +03:00
|
|
|
else {
|
|
|
|
xs *cfg_data;
|
|
|
|
|
|
|
|
/* read full config file */
|
|
|
|
cfg_data = xs_readall(f);
|
2022-10-17 21:25:42 +03:00
|
|
|
fclose(f);
|
2022-09-19 23:41:30 +03:00
|
|
|
|
|
|
|
/* parse */
|
|
|
|
srv_config = xs_json_loads(cfg_data);
|
|
|
|
|
|
|
|
if (srv_config == NULL)
|
2022-10-09 18:22:18 +03:00
|
|
|
error = xs_fmt("ERROR: cannot parse '%s'", cfg_file);
|
2022-09-19 23:41:30 +03: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 18:22:18 +03:00
|
|
|
error = xs_str_new("ERROR: cannot get server data");
|
2022-09-19 23:41:30 +03: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 10:39:28 +03:00
|
|
|
error = xs_fmt("DEBUG level set to %d from environment", dbglevel);
|
2022-09-19 23:41:30 +03:00
|
|
|
}
|
|
|
|
|
2022-11-23 15:32:23 +03:00
|
|
|
ret = db_upgrade(&error);
|
2022-09-19 23:41:30 +03:00
|
|
|
}
|
2022-10-09 18:22:18 +03:00
|
|
|
|
2022-09-19 23:41:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-26 09:39:23 +03:00
|
|
|
if (error != NULL)
|
2022-09-20 10:39:28 +03:00
|
|
|
srv_log(error);
|
|
|
|
|
2022-11-16 13:42:16 +03:00
|
|
|
/* disabled temporarily; messages can't be sent (libcurl issue?) */
|
2022-11-15 10:24:03 +03:00
|
|
|
#if 0
|
2022-11-14 19:40:31 +03:00
|
|
|
#ifdef __OpenBSD__
|
|
|
|
srv_debug(2, xs_fmt("Calling unveil()"));
|
2022-11-15 10:24:03 +03:00
|
|
|
unveil(basedir, "rwc");
|
|
|
|
unveil("/usr/sbin", "x");
|
|
|
|
unveil(NULL, NULL);
|
2022-11-14 19:40:31 +03:00
|
|
|
#endif /* __OpenBSD__ */
|
2022-11-15 10:24:03 +03:00
|
|
|
#endif
|
2022-11-14 19:40:31 +03:00
|
|
|
|
2022-09-19 23:41:30 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-25 11:38:12 +03:00
|
|
|
void srv_free(void)
|
|
|
|
{
|
|
|
|
xs_free(srv_basedir);
|
|
|
|
xs_free(srv_config);
|
|
|
|
xs_free(srv_baseurl);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-20 00:33:11 +03:00
|
|
|
void user_free(snac *snac)
|
2022-09-19 23:41:30 +03:00
|
|
|
/* frees a user snac */
|
|
|
|
{
|
2022-10-25 11:20:23 +03:00
|
|
|
xs_free(snac->uid);
|
|
|
|
xs_free(snac->basedir);
|
|
|
|
xs_free(snac->config);
|
|
|
|
xs_free(snac->key);
|
|
|
|
xs_free(snac->actor);
|
2022-09-19 23:41:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-20 00:03:18 +03:00
|
|
|
int user_open(snac *snac, char *uid)
|
2022-09-19 23:41:30 +03: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);
|
|
|
|
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 10:55:33 +03:00
|
|
|
srv_debug(2, xs_fmt("error opening '%s'", cfg_file));
|
2022-09-19 23:41:30 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
srv_log(xs_fmt("invalid user '%s'", uid));
|
|
|
|
|
|
|
|
if (!ret)
|
2022-09-20 00:33:11 +03:00
|
|
|
user_free(snac);
|
2022-09-19 23:41:30 +03:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-03 11:51:52 +03: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-20 00:33:11 +03:00
|
|
|
|
2022-09-20 10:39:28 +03:00
|
|
|
|
2022-09-30 10:59:13 +03:00
|
|
|
double mtime(char *fn)
|
2022-09-20 10:39:28 +03:00
|
|
|
/* returns the mtime of a file or directory, or 0.0 */
|
|
|
|
{
|
|
|
|
struct stat st;
|
2022-09-30 10:59:13 +03:00
|
|
|
double r = 0.0;
|
2022-09-20 10:39:28 +03:00
|
|
|
|
2022-09-30 10:56:29 +03:00
|
|
|
if (fn && stat(fn, &st) != -1)
|
2022-09-30 10:59:13 +03:00
|
|
|
r = (double)st.st_mtim.tv_sec;
|
2022-09-20 10:39:28 +03:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-24 10:00:35 +03:00
|
|
|
/** database 2.1+ **/
|
2022-11-23 16:38:37 +03:00
|
|
|
|
2022-11-24 10:09:57 +03:00
|
|
|
int index_add_md5(const char *fn, const char *md5)
|
2022-11-24 10:00:35 +03:00
|
|
|
/* adds an md5 to an index */
|
2022-11-23 20:58:07 +03:00
|
|
|
{
|
|
|
|
int status = 200;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "a")) != NULL) {
|
|
|
|
flock(fileno(f), LOCK_EX);
|
|
|
|
|
|
|
|
fprintf(f, "%s\n", md5);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
status = 500;
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-24 10:09:57 +03: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-11-24 10:00:35 +03:00
|
|
|
int index_del(const char *fn, const char *md5)
|
|
|
|
/* deletes an md5 from an index */
|
2022-11-23 21:14:55 +03: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 22:25:57 +03:00
|
|
|
char line[256];
|
2022-11-23 21:14:55 +03:00
|
|
|
|
|
|
|
if ((o = fopen(nfn, "w")) != NULL) {
|
|
|
|
while (fgets(line, sizeof(line), i) != NULL) {
|
2022-11-23 22:25:57 +03:00
|
|
|
line[32] = '\0';
|
2022-11-23 21:14:55 +03:00
|
|
|
if (memcmp(line, md5, 32) != 0)
|
2022-11-23 22:25:57 +03:00
|
|
|
fprintf(o, "%s\n", line);
|
2022-11-23 21:14:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(o);
|
|
|
|
|
2022-11-23 22:25:57 +03:00
|
|
|
xs *ofn = xs_fmt("%s.bak", fn);
|
2022-11-23 21:14:55 +03:00
|
|
|
|
|
|
|
link(fn, ofn);
|
|
|
|
rename(nfn, fn);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
status = 500;
|
|
|
|
|
|
|
|
fclose(i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
status = 500;
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-24 10:00:35 +03:00
|
|
|
d_char *index_list(const char *fn, int max)
|
|
|
|
/* returns an index as a list */
|
2022-11-23 21:32:26 +03:00
|
|
|
{
|
2022-11-23 22:25:57 +03:00
|
|
|
d_char *list = NULL;
|
2022-11-23 21:32:26 +03:00
|
|
|
FILE *f;
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
|
|
|
flock(fileno(f), LOCK_SH);
|
|
|
|
|
2022-11-23 22:25:57 +03:00
|
|
|
char line[256];
|
2022-11-23 21:32:26 +03: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-11-24 10:00:35 +03:00
|
|
|
d_char *index_list_desc(const char *fn, int max)
|
|
|
|
/* returns an index as a list, in reverse order */
|
2022-11-23 21:32:26 +03:00
|
|
|
{
|
2022-11-23 22:25:57 +03:00
|
|
|
d_char *list = NULL;
|
2022-11-23 21:32:26 +03:00
|
|
|
FILE *f;
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
|
|
|
flock(fileno(f), LOCK_SH);
|
|
|
|
|
2022-11-23 22:25:57 +03:00
|
|
|
char line[256];
|
2022-11-23 21:32:26 +03:00
|
|
|
list = xs_list_new();
|
|
|
|
|
|
|
|
/* move to the end minus one entry */
|
2022-11-23 22:25:57 +03:00
|
|
|
if (!fseek(f, 0, SEEK_END) && !fseek(f, -33, SEEK_CUR)) {
|
2022-11-23 21:32:26 +03:00
|
|
|
while (n < max && fgets(line, sizeof(line), f) != NULL) {
|
|
|
|
line[32] = '\0';
|
|
|
|
list = xs_list_append(list, line);
|
|
|
|
n++;
|
|
|
|
|
|
|
|
/* move backwards 2 entries */
|
2022-11-23 22:25:57 +03:00
|
|
|
if (fseek(f, -66, SEEK_CUR) == -1)
|
2022-11-23 21:32:26 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-24 10:00:35 +03: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int object_get_by_md5(const char *md5, d_char **obj, const char *type)
|
|
|
|
/* returns a loaded object, optionally of the requested type */
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
/* returns a loaded object, optionally of the requested type */
|
|
|
|
{
|
|
|
|
xs *md5 = xs_md5_hex(id, strlen(id));
|
|
|
|
|
|
|
|
return object_get_by_md5(md5, obj, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int object_add(const char *id, d_char *obj)
|
|
|
|
/* stores an object */
|
|
|
|
{
|
|
|
|
int status = 201; /* Created */
|
|
|
|
xs *fn = _object_fn(id);
|
|
|
|
FILE *f;
|
|
|
|
|
2022-11-24 10:39:30 +03:00
|
|
|
if (mtime(fn) > 0.0) {
|
|
|
|
/* object already here */
|
|
|
|
srv_debug(0, xs_fmt("object_add object already here %s", id));
|
|
|
|
return 204; /* No content */
|
|
|
|
}
|
|
|
|
|
2022-11-24 10:00:35 +03: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 10:09:57 +03:00
|
|
|
|
|
|
|
/* does this object has a parent? */
|
|
|
|
char *in_reply_to = xs_dict_get(obj, "inReplyTo");
|
|
|
|
|
2022-11-24 10:18:51 +03:00
|
|
|
if (!xs_is_null(in_reply_to) && *in_reply_to) {
|
2022-11-24 10:09:57 +03:00
|
|
|
/* update the children index of the parent */
|
|
|
|
xs *pfn = _object_fn(in_reply_to);
|
|
|
|
|
2022-11-24 11:06:41 +03:00
|
|
|
pfn = xs_replace_i(pfn, ".json", "_c.idx");
|
|
|
|
index_add(pfn, id);
|
2022-11-24 11:15:06 +03:00
|
|
|
|
|
|
|
srv_debug(0, xs_fmt("object_add added child %s to %s", id, pfn));
|
2022-11-24 10:09:57 +03:00
|
|
|
}
|
2022-11-24 10:00:35 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
status = 500;
|
|
|
|
|
2022-11-24 10:18:51 +03:00
|
|
|
srv_debug(0, xs_fmt("object_add %s %s %d", id, fn, status));
|
2022-11-24 10:00:35 +03:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int object_del(const char *id)
|
|
|
|
/* deletes an object */
|
|
|
|
{
|
|
|
|
int status = 404;
|
|
|
|
xs *fn = _object_fn(id);
|
|
|
|
|
2022-11-24 10:36:01 +03:00
|
|
|
if (fn != NULL && unlink(fn) != -1) {
|
2022-11-24 10:00:35 +03:00
|
|
|
status = 200;
|
|
|
|
|
2022-11-24 10:36:01 +03:00
|
|
|
/* also delete associated indexes */
|
|
|
|
xs *spec = _object_fn(id);
|
|
|
|
spec = xs_replace_i(spec, ".json", "*.idx");
|
|
|
|
xs *files = xs_glob(spec, 0, 0);
|
|
|
|
char *p, *v;
|
|
|
|
|
|
|
|
p = files;
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
srv_debug(0, xs_fmt("object_del index %s", v));
|
|
|
|
unlink(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
srv_debug(0, xs_fmt("object_del %s %d", id, status));
|
2022-11-24 10:00:35 +03:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-24 11:58:37 +03:00
|
|
|
d_char *object_children(const char *id)
|
|
|
|
/* returns the list of an object's children */
|
|
|
|
{
|
|
|
|
xs *fn = _object_fn(id);
|
|
|
|
|
|
|
|
fn = xs_replace_i(fn, ".json", "_c.idx");
|
|
|
|
|
|
|
|
return index_list(fn, XS_ALL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-20 10:39:28 +03:00
|
|
|
d_char *_follower_fn(snac *snac, char *actor)
|
|
|
|
{
|
|
|
|
xs *md5 = xs_md5_hex(actor, strlen(actor));
|
|
|
|
return xs_fmt("%s/followers/%s.json", snac->basedir, md5);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int follower_add(snac *snac, char *actor, char *msg)
|
|
|
|
/* adds a follower */
|
|
|
|
{
|
|
|
|
int ret = 201; /* created */
|
|
|
|
xs *fn = _follower_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("follower_add %s %s", actor, fn));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int follower_del(snac *snac, char *actor)
|
|
|
|
/* deletes a follower */
|
|
|
|
{
|
2022-09-28 20:59:19 +03:00
|
|
|
int status = 200;
|
2022-09-20 10:39:28 +03:00
|
|
|
xs *fn = _follower_fn(snac, actor);
|
|
|
|
|
2022-09-28 20:59:19 +03:00
|
|
|
if (fn != NULL)
|
|
|
|
unlink(fn);
|
|
|
|
else
|
|
|
|
status = 404;
|
2022-09-20 10:39:28 +03:00
|
|
|
|
|
|
|
snac_debug(snac, 2, xs_fmt("follower_del %s %s", actor, fn));
|
|
|
|
|
2022-09-28 20:59:19 +03:00
|
|
|
return status;
|
2022-09-20 10:39:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int follower_check(snac *snac, char *actor)
|
|
|
|
/* checks if someone is a follower */
|
|
|
|
{
|
|
|
|
xs *fn = _follower_fn(snac, actor);
|
|
|
|
|
|
|
|
return !!(mtime(fn) != 0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
d_char *follower_list(snac *snac)
|
|
|
|
/* returns the list of followers */
|
|
|
|
{
|
2022-10-03 11:56:15 +03:00
|
|
|
xs *spec = xs_fmt("%s/followers/" "*.json", snac->basedir);
|
|
|
|
xs *glist = xs_glob(spec, 0, 0);
|
|
|
|
char *p, *v;
|
|
|
|
d_char *list = xs_list_new();
|
2022-09-20 10:39:28 +03:00
|
|
|
|
2022-10-03 11:56:15 +03:00
|
|
|
/* iterate the list of files */
|
|
|
|
p = glist;
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
FILE *f;
|
2022-09-20 10:39:28 +03:00
|
|
|
|
2022-10-03 11:56:15 +03:00
|
|
|
/* load the follower data */
|
|
|
|
if ((f = fopen(v, "r")) != NULL) {
|
|
|
|
xs *j = xs_readall(f);
|
|
|
|
fclose(f);
|
2022-09-20 10:39:28 +03:00
|
|
|
|
2022-10-03 11:56:15 +03:00
|
|
|
if (j != NULL) {
|
2022-09-20 10:48:13 +03:00
|
|
|
xs *o = xs_json_loads(j);
|
|
|
|
|
|
|
|
if (o != NULL)
|
|
|
|
list = xs_list_append(list, o);
|
2022-09-20 10:39:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
2022-09-20 11:02:00 +03:00
|
|
|
|
|
|
|
|
2022-09-30 10:59:13 +03:00
|
|
|
double timeline_mtime(snac *snac)
|
2022-09-30 10:56:29 +03:00
|
|
|
{
|
|
|
|
xs *fn = xs_fmt("%s/timeline", snac->basedir);
|
|
|
|
return mtime(fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-20 11:49:24 +03:00
|
|
|
d_char *_timeline_find_fn(snac *snac, char *id)
|
2022-09-20 11:02:00 +03:00
|
|
|
/* returns the file name of a timeline entry by its id */
|
|
|
|
{
|
|
|
|
xs *md5 = xs_md5_hex(id, strlen(id));
|
|
|
|
xs *spec = xs_fmt("%s/timeline/" "*-%s.json", snac->basedir, md5);
|
2022-10-03 11:59:08 +03:00
|
|
|
xs *list = NULL;
|
2022-09-20 11:02:00 +03:00
|
|
|
d_char *fn = NULL;
|
2022-10-05 13:08:17 +03:00
|
|
|
int l;
|
2022-09-20 11:02:00 +03:00
|
|
|
|
2022-10-03 11:59:08 +03:00
|
|
|
list = xs_glob(spec, 0, 0);
|
2022-10-05 13:08:17 +03:00
|
|
|
l = xs_list_len(list);
|
2022-09-20 11:02:00 +03:00
|
|
|
|
2022-10-03 11:59:08 +03:00
|
|
|
/* if there is something, get the first one */
|
2022-10-05 13:08:17 +03:00
|
|
|
if (l > 0) {
|
2022-10-03 11:59:08 +03:00
|
|
|
fn = xs_str_new(xs_list_get(list, 0));
|
2022-09-20 11:02:00 +03:00
|
|
|
|
2022-10-05 13:08:17 +03:00
|
|
|
if (l > 1)
|
|
|
|
snac_log(snac, xs_fmt("**ALERT** _timeline_find_fn %d > 1", l));
|
|
|
|
}
|
|
|
|
|
2022-09-20 11:02:00 +03:00
|
|
|
return fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-25 19:28:15 +03:00
|
|
|
int timeline_here(snac *snac, char *id)
|
|
|
|
/* checks if an object is already downloaded */
|
|
|
|
{
|
|
|
|
xs *fn = _timeline_find_fn(snac, id);
|
|
|
|
|
|
|
|
return fn != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-20 11:49:24 +03:00
|
|
|
d_char *timeline_find(snac *snac, char *id)
|
|
|
|
/* gets a message from the timeline by id */
|
2022-09-20 11:02:00 +03:00
|
|
|
{
|
2022-09-26 12:19:45 +03:00
|
|
|
xs *fn = _timeline_find_fn(snac, id);
|
|
|
|
d_char *msg = NULL;
|
2022-09-20 11:02:00 +03:00
|
|
|
|
|
|
|
if (fn != NULL) {
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
|
|
|
xs *j = xs_readall(f);
|
|
|
|
|
|
|
|
msg = xs_json_loads(j);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-20 11:34:32 +03:00
|
|
|
int timeline_del(snac *snac, char *id)
|
2022-09-20 11:02:00 +03:00
|
|
|
/* deletes a message from the timeline */
|
|
|
|
{
|
2022-10-20 11:34:32 +03:00
|
|
|
int ret = 404;
|
|
|
|
xs *fn = _timeline_find_fn(snac, id);
|
2022-09-20 11:02:00 +03:00
|
|
|
|
|
|
|
if (fn != NULL) {
|
|
|
|
xs *lfn = NULL;
|
|
|
|
|
|
|
|
unlink(fn);
|
|
|
|
snac_debug(snac, 1, xs_fmt("timeline_del %s", id));
|
|
|
|
|
|
|
|
/* try to delete also from the local timeline */
|
|
|
|
lfn = xs_replace(fn, "/timeline/", "/local/");
|
|
|
|
|
|
|
|
if (unlink(lfn) != -1)
|
|
|
|
snac_debug(snac, 1, xs_fmt("timeline_del (local) %s", id));
|
2022-10-20 11:34:32 +03:00
|
|
|
|
|
|
|
ret = 200;
|
2022-09-20 11:02:00 +03:00
|
|
|
}
|
2022-10-20 11:34:32 +03:00
|
|
|
|
2022-11-24 10:36:01 +03:00
|
|
|
object_del(id);
|
|
|
|
|
2022-10-20 11:34:32 +03:00
|
|
|
return ret;
|
2022-09-20 11:02:00 +03:00
|
|
|
}
|
2022-09-20 11:49:24 +03:00
|
|
|
|
|
|
|
|
|
|
|
d_char *timeline_get(snac *snac, char *fn)
|
|
|
|
/* gets a timeline entry by file name */
|
|
|
|
{
|
|
|
|
d_char *d = NULL;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
|
|
|
xs *j = xs_readall(f);
|
|
|
|
|
|
|
|
d = xs_json_loads(j);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-28 05:48:23 +03:00
|
|
|
d_char *_timeline_list(snac *snac, char *directory, int max)
|
2022-09-20 11:49:24 +03:00
|
|
|
/* returns a list of the timeline filenames */
|
|
|
|
{
|
2022-09-28 05:48:23 +03:00
|
|
|
xs *spec = xs_fmt("%s/%s/" "*.json", snac->basedir, directory);
|
|
|
|
int c_max;
|
2022-09-20 11:49:24 +03:00
|
|
|
|
|
|
|
/* maximum number of items in the timeline */
|
2022-09-28 05:48:23 +03:00
|
|
|
c_max = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
|
|
|
|
|
2022-10-03 12:08:11 +03:00
|
|
|
/* never more timeline entries than the configured maximum */
|
2022-09-28 05:48:23 +03:00
|
|
|
if (max > c_max)
|
|
|
|
max = c_max;
|
2022-09-20 11:49:24 +03:00
|
|
|
|
2022-10-03 12:08:11 +03:00
|
|
|
return xs_glob_n(spec, 0, 1, max);
|
2022-09-20 11:49:24 +03:00
|
|
|
}
|
2022-09-20 12:16:24 +03:00
|
|
|
|
|
|
|
|
2022-09-28 05:48:23 +03:00
|
|
|
d_char *timeline_list(snac *snac, int max)
|
|
|
|
{
|
|
|
|
return _timeline_list(snac, "timeline", max);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
d_char *local_list(snac *snac, int max)
|
|
|
|
{
|
|
|
|
return _timeline_list(snac, "local", max);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-23 10:50:38 +03:00
|
|
|
d_char *_timeline_new_fn(snac *snac, char *id)
|
|
|
|
/* creates a new filename */
|
2022-09-22 15:46:23 +03:00
|
|
|
{
|
2022-09-23 10:50:38 +03:00
|
|
|
xs *ntid = tid(0);
|
|
|
|
xs *md5 = xs_md5_hex(id, strlen(id));
|
2022-09-22 15:46:23 +03:00
|
|
|
|
2022-09-23 10:50:38 +03:00
|
|
|
return xs_fmt("%s/timeline/%s-%s.json", snac->basedir, ntid, md5);
|
2022-09-22 15:46:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-16 19:28:20 +03:00
|
|
|
int _timeline_write(snac *snac, char *id, char *msg, char *parent, char *referrer)
|
2022-09-25 18:42:39 +03:00
|
|
|
/* writes a timeline entry and refreshes the ancestors */
|
2022-09-20 12:16:24 +03:00
|
|
|
{
|
2022-11-16 19:28:20 +03:00
|
|
|
xs *fn = _timeline_new_fn(snac, id);
|
|
|
|
xs *pfn = NULL;
|
|
|
|
xs *p_msg = NULL;
|
2022-09-25 18:42:39 +03:00
|
|
|
FILE *f;
|
2022-09-20 12:16:24 +03:00
|
|
|
|
2022-11-16 19:28:20 +03:00
|
|
|
if (!xs_is_null(parent)) {
|
|
|
|
/* get the parent */
|
|
|
|
pfn = _timeline_find_fn(snac, parent);
|
|
|
|
|
|
|
|
if (pfn != NULL && (f = fopen(pfn, "r")) != NULL) {
|
|
|
|
xs *j;
|
|
|
|
|
|
|
|
j = xs_readall(f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
p_msg = xs_json_loads(j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write the message */
|
2022-09-20 12:16:24 +03:00
|
|
|
if ((f = fopen(fn, "w")) != NULL) {
|
|
|
|
xs *j = xs_json_dumps_pp(msg, 4);
|
|
|
|
|
|
|
|
fwrite(j, strlen(j), 1, f);
|
|
|
|
fclose(f);
|
|
|
|
|
2022-09-25 18:42:39 +03:00
|
|
|
snac_debug(snac, 1, xs_fmt("_timeline_write %s %s", id, fn));
|
2022-09-20 12:16:24 +03:00
|
|
|
}
|
|
|
|
|
2022-09-23 10:50:38 +03:00
|
|
|
/* related to this user? link to local timeline */
|
|
|
|
if (xs_startswith(id, snac->actor) ||
|
2022-09-26 08:32:44 +03:00
|
|
|
(!xs_is_null(parent) && xs_startswith(parent, snac->actor)) ||
|
|
|
|
(!xs_is_null(referrer) && xs_startswith(referrer, snac->actor))) {
|
2022-09-20 12:16:24 +03:00
|
|
|
xs *lfn = xs_replace(fn, "/timeline/", "/local/");
|
|
|
|
link(fn, lfn);
|
|
|
|
|
2022-09-25 18:42:39 +03:00
|
|
|
snac_debug(snac, 1, xs_fmt("_timeline_write (local) %s %s", id, lfn));
|
2022-09-20 12:16:24 +03:00
|
|
|
}
|
2022-09-22 15:46:23 +03:00
|
|
|
|
2022-11-16 19:28:20 +03:00
|
|
|
if (p_msg != NULL) {
|
2022-09-23 10:50:38 +03:00
|
|
|
/* update the parent, adding this id to its children list */
|
|
|
|
xs *meta = xs_dup(xs_dict_get(p_msg, "_snac"));
|
|
|
|
xs *children = xs_dup(xs_dict_get(meta, "children"));
|
|
|
|
|
2022-09-25 18:42:39 +03:00
|
|
|
/* add the child if it's not already there */
|
|
|
|
if (xs_list_in(children, id) == -1)
|
|
|
|
children = xs_list_append(children, id);
|
2022-09-23 10:50:38 +03:00
|
|
|
|
|
|
|
/* re-store */
|
|
|
|
meta = xs_dict_set(meta, "children", children);
|
|
|
|
p_msg = xs_dict_set(p_msg, "_snac", meta);
|
|
|
|
|
|
|
|
xs *nfn = _timeline_new_fn(snac, parent);
|
|
|
|
|
|
|
|
if ((f = fopen(nfn, "w")) != NULL) {
|
|
|
|
xs *j = xs_json_dumps_pp(p_msg, 4);
|
|
|
|
|
|
|
|
fwrite(j, strlen(j), 1, f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
unlink(pfn);
|
|
|
|
|
2022-09-25 18:42:39 +03:00
|
|
|
snac_debug(snac, 1,
|
|
|
|
xs_fmt("_timeline_write updated parent %s %s", parent, nfn));
|
2022-09-23 10:50:38 +03:00
|
|
|
|
|
|
|
/* try to do the same with the local */
|
|
|
|
xs *olfn = xs_replace(pfn, "/timeline/", "/local/");
|
|
|
|
|
2022-09-26 10:08:03 +03:00
|
|
|
if (unlink(olfn) != -1 || xs_startswith(id, snac->actor)) {
|
2022-09-23 10:50:38 +03:00
|
|
|
xs *nlfn = xs_replace(nfn, "/timeline/", "/local/");
|
|
|
|
|
|
|
|
link(nfn, nlfn);
|
|
|
|
|
2022-09-25 18:42:39 +03:00
|
|
|
snac_debug(snac, 1,
|
|
|
|
xs_fmt("_timeline_write updated parent (local) %s %s", parent, nlfn));
|
2022-09-23 10:50:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2022-11-16 19:28:20 +03:00
|
|
|
return 0;
|
2022-09-23 10:50:38 +03:00
|
|
|
|
|
|
|
/* now iterate all parents up, just renaming the files */
|
|
|
|
xs *grampa = xs_dup(xs_dict_get(meta, "parent"));
|
|
|
|
|
2022-11-22 22:04:08 +03:00
|
|
|
int max_levels = 20;
|
2022-11-21 10:04:22 +03:00
|
|
|
|
2022-09-25 10:07:43 +03:00
|
|
|
while (!xs_is_null(grampa)) {
|
2022-09-23 10:50:38 +03:00
|
|
|
xs *gofn = _timeline_find_fn(snac, grampa);
|
|
|
|
|
|
|
|
if (gofn == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* create the new filename */
|
|
|
|
xs *gnfn = _timeline_new_fn(snac, grampa);
|
|
|
|
|
|
|
|
rename(gofn, gnfn);
|
|
|
|
|
2022-09-25 23:40:31 +03:00
|
|
|
snac_debug(snac, 1,
|
2022-09-25 18:42:39 +03:00
|
|
|
xs_fmt("_timeline_write updated grampa %s %s", grampa, gnfn));
|
2022-09-23 10:50:38 +03:00
|
|
|
|
|
|
|
/* try to do the same with the local */
|
|
|
|
xs *golfn = xs_replace(gofn, "/timeline/", "/local/");
|
|
|
|
|
|
|
|
if (unlink(golfn) != -1) {
|
|
|
|
xs *gnlfn = xs_replace(gnfn, "/timeline/", "/local/");
|
|
|
|
|
|
|
|
link(gnfn, gnlfn);
|
|
|
|
|
2022-09-25 23:40:31 +03:00
|
|
|
snac_debug(snac, 1,
|
2022-09-25 18:42:39 +03:00
|
|
|
xs_fmt("_timeline_write updated grampa (local) %s %s", parent, gnlfn));
|
2022-09-23 10:50:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* now open it and get its own parent */
|
|
|
|
if ((f = fopen(gnfn, "r")) != NULL) {
|
|
|
|
xs *j = xs_readall(f);
|
|
|
|
fclose(f);
|
|
|
|
|
2022-09-25 23:40:31 +03:00
|
|
|
xs *g_msg = xs_json_loads(j);
|
2022-10-25 14:59:15 +03:00
|
|
|
char *meta = xs_dict_get(g_msg, "_snac");
|
|
|
|
char *p = xs_dict_get(meta, "parent");
|
2022-09-23 10:50:38 +03:00
|
|
|
|
2022-10-25 14:59:15 +03:00
|
|
|
xs_free(grampa);
|
2022-09-25 23:40:31 +03:00
|
|
|
grampa = xs_dup(p);
|
2022-09-23 10:50:38 +03:00
|
|
|
}
|
2022-11-21 10:04:22 +03:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (--max_levels == 0) {
|
2022-11-21 11:08:36 +03:00
|
|
|
snac_debug(snac, 1, xs_dup("_timeline_write maximum grampa levels reached"));
|
2022-11-21 10:04:22 +03:00
|
|
|
break;
|
|
|
|
}
|
2022-09-23 10:50:38 +03:00
|
|
|
}
|
|
|
|
}
|
2022-11-16 19:28:20 +03:00
|
|
|
|
|
|
|
return 1;
|
2022-09-20 12:16:24 +03:00
|
|
|
}
|
2022-09-20 12:31:56 +03:00
|
|
|
|
|
|
|
|
2022-09-26 08:13:39 +03:00
|
|
|
int timeline_add(snac *snac, char *id, char *o_msg, char *parent, char *referrer)
|
2022-09-25 18:42:39 +03:00
|
|
|
/* adds a message to the timeline */
|
|
|
|
{
|
|
|
|
xs *pfn = _timeline_find_fn(snac, id);
|
2022-11-16 19:28:20 +03:00
|
|
|
int ret = 0;
|
2022-09-25 18:42:39 +03:00
|
|
|
|
|
|
|
if (pfn != NULL) {
|
|
|
|
snac_log(snac, xs_fmt("timeline_add refusing rewrite %s %s", id, pfn));
|
2022-09-25 19:50:53 +03:00
|
|
|
return 0;
|
2022-09-25 18:42:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
xs *msg = xs_dup(o_msg);
|
|
|
|
xs *md;
|
|
|
|
|
|
|
|
/* add new metadata */
|
|
|
|
md = xs_json_loads("{"
|
|
|
|
"\"children\": [],"
|
|
|
|
"\"liked_by\": [],"
|
|
|
|
"\"announced_by\": [],"
|
2022-09-27 19:01:51 +03:00
|
|
|
"\"version\": \"" USER_AGENT "\","
|
2022-09-26 08:13:39 +03:00
|
|
|
"\"referrer\": null,"
|
2022-09-25 18:42:39 +03:00
|
|
|
"\"parent\": null"
|
|
|
|
"}");
|
|
|
|
|
|
|
|
if (!xs_is_null(parent))
|
|
|
|
md = xs_dict_set(md, "parent", parent);
|
|
|
|
|
2022-09-26 08:13:39 +03:00
|
|
|
if (!xs_is_null(referrer))
|
2022-09-26 11:08:14 +03:00
|
|
|
md = xs_dict_set(md, "referrer", referrer);
|
2022-09-26 08:13:39 +03:00
|
|
|
|
2022-09-25 18:42:39 +03:00
|
|
|
msg = xs_dict_set(msg, "_snac", md);
|
|
|
|
|
2022-11-24 10:18:51 +03:00
|
|
|
if ((ret = _timeline_write(snac, id, msg, parent, referrer))) {
|
2022-11-18 20:08:09 +03:00
|
|
|
snac_debug(snac, 1, xs_fmt("timeline_add %s", id));
|
2022-11-24 10:18:51 +03:00
|
|
|
object_add(id, o_msg);
|
|
|
|
}
|
2022-09-25 19:50:53 +03:00
|
|
|
|
2022-11-16 19:28:20 +03:00
|
|
|
return ret;
|
2022-09-25 18:42:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void timeline_admire(snac *snac, char *id, char *admirer, int like)
|
|
|
|
/* updates a timeline entry with a new admiration */
|
|
|
|
{
|
|
|
|
xs *ofn = _timeline_find_fn(snac, id);
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if (ofn != NULL && (f = fopen(ofn, "r")) != NULL) {
|
|
|
|
xs *j1 = xs_readall(f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
xs *msg = xs_json_loads(j1);
|
|
|
|
xs *meta = xs_dup(xs_dict_get(msg, "_snac"));
|
|
|
|
xs *list;
|
|
|
|
|
|
|
|
if (like)
|
|
|
|
list = xs_dup(xs_dict_get(meta, "liked_by"));
|
|
|
|
else
|
|
|
|
list = xs_dup(xs_dict_get(meta, "announced_by"));
|
|
|
|
|
|
|
|
/* add the admirer if it's not already there */
|
2022-09-26 08:13:39 +03:00
|
|
|
if (xs_list_in(list, admirer) == -1)
|
2022-09-25 18:42:39 +03:00
|
|
|
list = xs_list_append(list, admirer);
|
|
|
|
|
2022-10-31 13:56:20 +03:00
|
|
|
/* set the admirer as the referrer (if not already set or it's us) */
|
|
|
|
if (!like && (xs_is_null(xs_dict_get(meta, "referrer")) ||
|
|
|
|
strcmp(admirer, snac->actor) == 0))
|
2022-09-28 16:41:07 +03:00
|
|
|
meta = xs_dict_set(meta, "referrer", admirer);
|
2022-09-26 08:13:39 +03:00
|
|
|
|
|
|
|
/* re-store */
|
|
|
|
if (like)
|
|
|
|
meta = xs_dict_set(meta, "liked_by", list);
|
|
|
|
else
|
|
|
|
meta = xs_dict_set(meta, "announced_by", list);
|
2022-09-25 18:42:39 +03:00
|
|
|
|
2022-09-26 08:13:39 +03:00
|
|
|
msg = xs_dict_set(msg, "_snac", meta);
|
2022-09-25 18:42:39 +03:00
|
|
|
|
2022-09-28 08:40:01 +03:00
|
|
|
unlink(ofn);
|
|
|
|
ofn = xs_replace_i(ofn, "/timeline/", "/local/");
|
2022-09-26 08:13:39 +03:00
|
|
|
unlink(ofn);
|
2022-09-25 18:42:39 +03:00
|
|
|
|
2022-10-16 01:29:49 +03:00
|
|
|
_timeline_write(snac, id, msg, xs_dict_get(meta, "parent"), like ? NULL : admirer);
|
2022-09-25 19:28:15 +03:00
|
|
|
|
2022-10-11 19:54:59 +03:00
|
|
|
snac_debug(snac, 1, xs_fmt("timeline_admire (%s) %s %s",
|
2022-09-26 08:13:39 +03:00
|
|
|
like ? "Like" : "Announce", id, admirer));
|
2022-09-25 18:42:39 +03:00
|
|
|
}
|
2022-09-26 08:32:44 +03:00
|
|
|
else
|
|
|
|
snac_log(snac, xs_fmt("timeline_admire ignored for unknown object %s", id));
|
2022-09-25 18:42:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-20 12:31:56 +03: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)
|
|
|
|
/* someone is no longer following us */
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
/* checks if someone is following us */
|
|
|
|
{
|
|
|
|
xs *fn = _following_fn(snac, actor);
|
|
|
|
|
|
|
|
return !!(mtime(fn) != 0.0);
|
|
|
|
}
|
2022-09-20 12:38:18 +03:00
|
|
|
|
|
|
|
|
2022-10-01 10:12:33 +03: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 12:49:16 +03: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");
|
|
|
|
|
|
|
|
if (!xs_is_null(type) && strcmp(type, "Accept") == 0)
|
|
|
|
list = xs_list_append(list, o);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-20 12:38:18 +03:00
|
|
|
d_char *_muted_fn(snac *snac, char *actor)
|
|
|
|
{
|
|
|
|
xs *md5 = xs_md5_hex(actor, strlen(actor));
|
2022-11-24 12:06:24 +03:00
|
|
|
return xs_fmt("%s/muted/%s", snac->basedir, md5);
|
2022-09-20 12:38:18 +03: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 13:00:13 +03:00
|
|
|
|
|
|
|
|
2022-11-24 11:39:16 +03:00
|
|
|
d_char *_hidden_fn(snac *snac, const char *id)
|
|
|
|
{
|
|
|
|
xs *md5 = xs_md5_hex(id, strlen(id));
|
2022-11-24 11:49:54 +03:00
|
|
|
return xs_fmt("%s/hidden/%s", snac->basedir, md5);
|
2022-11-24 11:39:16 +03: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 11:58:37 +03:00
|
|
|
|
|
|
|
/* hide all the children */
|
|
|
|
xs *chld = object_children(id);
|
|
|
|
char *p, *v;
|
|
|
|
|
|
|
|
p = chld;
|
2022-11-24 14:21:54 +03: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 11:39:16 +03: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 18:13:51 +03:00
|
|
|
int actor_add(snac *snac, const char *actor, d_char *msg)
|
2022-09-28 21:08:02 +03:00
|
|
|
/* adds an actor */
|
2022-09-22 19:50:39 +03:00
|
|
|
{
|
2022-11-23 18:13:51 +03:00
|
|
|
return object_add(actor, msg);
|
2022-09-22 19:50:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-23 18:13:51 +03:00
|
|
|
int actor_get(snac *snac, const char *actor, d_char **data)
|
2022-09-22 19:50:39 +03:00
|
|
|
/* returns an already downloaded actor */
|
|
|
|
{
|
2022-11-23 18:13:51 +03:00
|
|
|
int status = 200;
|
|
|
|
char *d;
|
2022-09-22 19:50:39 +03:00
|
|
|
|
2022-10-25 19:44:29 +03:00
|
|
|
if (strcmp(actor, snac->actor) == 0) {
|
2022-11-23 18:13:51 +03:00
|
|
|
/* this actor */
|
2022-10-25 19:44:29 +03:00
|
|
|
if (data)
|
|
|
|
*data = msg_actor(snac);
|
|
|
|
|
2022-11-23 18:13:51 +03:00
|
|
|
return status;
|
2022-10-25 19:44:29 +03:00
|
|
|
}
|
|
|
|
|
2022-11-23 18:13:51 +03:00
|
|
|
/* read the object */
|
2022-11-23 23:43:00 +03:00
|
|
|
if (!valid_status(status = object_get(actor, &d, NULL)))
|
2022-11-23 18:13:51 +03:00
|
|
|
return status;
|
2022-09-22 19:50:39 +03:00
|
|
|
|
2022-11-23 18:13:51 +03:00
|
|
|
if (data)
|
|
|
|
*data = d;
|
|
|
|
|
2022-11-23 20:59:18 +03:00
|
|
|
xs *fn = _object_fn(actor);
|
2022-11-23 18:13:51 +03:00
|
|
|
double max_time;
|
2022-09-22 19:50:39 +03:00
|
|
|
|
|
|
|
/* maximum time for the actor data to be considered stale */
|
|
|
|
max_time = 3600.0 * 36.0;
|
|
|
|
|
2022-11-23 18:13:51 +03:00
|
|
|
if (mtime(fn) + max_time < (double) time(NULL)) {
|
2022-09-22 19:50:39 +03:00
|
|
|
/* actor data exists but also stinks */
|
2022-11-23 18:13:51 +03:00
|
|
|
FILE *f;
|
2022-09-22 19:50:39 +03: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 19:56:50 +03:00
|
|
|
|
2022-09-23 20:07:45 +03:00
|
|
|
status = 205; /* "205: Reset Content" "110: Response Is Stale" */
|
2022-09-22 19:50:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-16 19:03:28 +03:00
|
|
|
d_char *_static_fn(snac *snac, const char *id)
|
2022-09-28 10:29:09 +03:00
|
|
|
/* gets the filename for a static file */
|
|
|
|
{
|
|
|
|
return xs_fmt("%s/static/%s", snac->basedir, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-16 19:03:28 +03:00
|
|
|
int static_get(snac *snac, const char *id, d_char **data, int *size)
|
2022-09-28 10:29:09 +03:00
|
|
|
/* returns static content */
|
|
|
|
{
|
|
|
|
xs *fn = _static_fn(snac, id);
|
|
|
|
FILE *f;
|
|
|
|
int status = 404;
|
|
|
|
|
|
|
|
*size = 0xfffffff;
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "rb")) != NULL) {
|
|
|
|
*data = xs_read(f, size);
|
2022-10-17 21:25:42 +03:00
|
|
|
fclose(f);
|
|
|
|
|
2022-09-28 10:29:09 +03:00
|
|
|
status = 200;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-16 19:03:28 +03: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 10:56:29 +03: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 10:59:13 +03:00
|
|
|
double history_mtime(snac *snac, char * id)
|
2022-09-30 10:56:29 +03:00
|
|
|
{
|
2022-09-30 10:59:13 +03:00
|
|
|
double t = 0.0;
|
2022-09-30 10:56:29 +03: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 08:45:36 +03:00
|
|
|
int history_del(snac *snac, char *id)
|
|
|
|
{
|
|
|
|
xs *fn = _history_fn(snac, id);
|
|
|
|
return unlink(fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-02 19:16:58 +03:00
|
|
|
d_char *history_list(snac *snac)
|
|
|
|
{
|
2022-10-03 12:14:16 +03:00
|
|
|
xs *spec = xs_fmt("%s/history/" "*.html", snac->basedir);
|
2022-10-02 19:16:58 +03:00
|
|
|
|
2022-10-03 12:14:16 +03:00
|
|
|
return xs_glob(spec, 1, 0);
|
2022-10-02 19:16:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-11 09:16:58 +03:00
|
|
|
static int _enqueue_put(char *fn, char *msg)
|
|
|
|
/* writes safely to the queue */
|
2022-09-24 00:09:09 +03:00
|
|
|
{
|
2022-10-11 09:16:58 +03:00
|
|
|
int ret = 1;
|
|
|
|
xs *tfn = xs_fmt("%s.tmp", fn);
|
2022-09-24 00:09:09 +03:00
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(tfn, "w")) != NULL) {
|
2022-10-11 09:16:58 +03:00
|
|
|
xs *j = xs_json_dumps_pp(msg, 4);
|
2022-09-24 00:09:09 +03:00
|
|
|
|
|
|
|
fwrite(j, strlen(j), 1, f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
rename(tfn, fn);
|
|
|
|
}
|
2022-10-11 09:16:58 +03: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-24 00:09:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-17 20:33:54 +03:00
|
|
|
void enqueue_output(snac *snac, char *msg, char *inbox, int retries)
|
2022-11-18 10:21:40 +03:00
|
|
|
/* enqueues an output message to an inbox */
|
2022-09-23 20:37:01 +03:00
|
|
|
{
|
2022-11-17 20:33:54 +03:00
|
|
|
if (xs_startswith(inbox, snac->actor)) {
|
|
|
|
snac_debug(snac, 1, xs_str_new("refusing enqueue to myself"));
|
2022-09-23 20:37:01 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-26 12:19:45 +03:00
|
|
|
int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
|
2022-09-23 20:37:01 +03:00
|
|
|
xs *ntid = tid(retries * 60 * qrt);
|
|
|
|
xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
|
2022-10-11 09:16:58 +03:00
|
|
|
xs *qmsg = xs_dict_new();
|
|
|
|
xs *rn = xs_number_new(retries);
|
2022-09-23 20:37:01 +03:00
|
|
|
|
2022-10-11 09:16:58 +03:00
|
|
|
qmsg = xs_dict_append(qmsg, "type", "output");
|
2022-11-17 20:33:54 +03:00
|
|
|
qmsg = xs_dict_append(qmsg, "inbox", inbox);
|
2022-10-11 09:16:58 +03:00
|
|
|
qmsg = xs_dict_append(qmsg, "object", msg);
|
|
|
|
qmsg = xs_dict_append(qmsg, "retries", rn);
|
2022-09-23 20:37:01 +03:00
|
|
|
|
2022-10-11 09:16:58 +03:00
|
|
|
_enqueue_put(fn, qmsg);
|
2022-09-23 20:37:01 +03:00
|
|
|
|
2022-11-17 20:33:54 +03:00
|
|
|
snac_debug(snac, 1, xs_fmt("enqueue_output %s %s %d", inbox, fn, retries));
|
2022-09-23 20:37:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-18 10:21:40 +03: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 20:07:20 +03: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-09-20 13:43:49 +03:00
|
|
|
d_char *queue(snac *snac)
|
|
|
|
/* returns a list with filenames that can be dequeued */
|
|
|
|
{
|
2022-10-18 12:40:10 +03:00
|
|
|
xs *spec = xs_fmt("%s/queue/" "*.json", snac->basedir);
|
2022-09-20 13:43:49 +03:00
|
|
|
d_char *list = xs_list_new();
|
2022-10-18 12:40:10 +03:00
|
|
|
time_t t = time(NULL);
|
|
|
|
char *p, *v;
|
2022-09-20 13:43:49 +03:00
|
|
|
|
2022-10-18 12:40:10 +03:00
|
|
|
xs *fns = xs_glob(spec, 0, 0);
|
2022-09-20 13:43:49 +03:00
|
|
|
|
2022-10-18 12:40:10 +03: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 13:43:49 +03:00
|
|
|
|
2022-10-18 12:40:10 +03:00
|
|
|
if (t2 > t)
|
2022-10-21 20:07:20 +03:00
|
|
|
snac_debug(snac, 2, xs_fmt("queue not yet time for %s [%ld]", v, t));
|
2022-10-18 12:40:10 +03:00
|
|
|
else {
|
|
|
|
list = xs_list_append(list, v);
|
|
|
|
snac_debug(snac, 2, xs_fmt("queue ready for %s", v));
|
2022-09-20 13:43:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
2022-09-20 13:50:37 +03: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 19:46:12 +03:00
|
|
|
|
|
|
|
|
2022-11-12 10:26:26 +03:00
|
|
|
static void _purge_subdir(snac *snac, const char *subdir, int days)
|
|
|
|
/* purges all files in subdir older than days */
|
2022-10-04 19:46:12 +03:00
|
|
|
{
|
2022-11-12 10:26:26 +03:00
|
|
|
if (days) {
|
|
|
|
time_t mt = time(NULL) - days * 24 * 3600;
|
|
|
|
xs *spec = xs_fmt("%s/%s/" "*.json", snac->basedir, subdir);
|
|
|
|
xs *list = xs_glob(spec, 0, 0);
|
|
|
|
char *p, *v;
|
|
|
|
|
|
|
|
p = list;
|
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
if (mtime(v) < mt) {
|
|
|
|
/* older than the minimum time: delete it */
|
|
|
|
unlink(v);
|
|
|
|
snac_debug(snac, 1, xs_fmt("purged %s", v));
|
|
|
|
}
|
|
|
|
}
|
2022-10-17 12:26:36 +03:00
|
|
|
}
|
2022-11-12 10:26:26 +03:00
|
|
|
}
|
2022-10-17 12:26:36 +03:00
|
|
|
|
2022-10-04 19:46:12 +03:00
|
|
|
|
2022-11-12 10:26:26 +03:00
|
|
|
void purge(snac *snac)
|
|
|
|
/* do the purge */
|
|
|
|
{
|
|
|
|
int days;
|
2022-10-04 19:46:12 +03:00
|
|
|
|
2022-11-12 10:26:26 +03:00
|
|
|
days = xs_number_get(xs_dict_get(srv_config, "timeline_purge_days"));
|
|
|
|
_purge_subdir(snac, "timeline", days);
|
2022-10-04 19:46:12 +03:00
|
|
|
|
2022-11-12 10:26:26 +03:00
|
|
|
days = xs_number_get(xs_dict_get(srv_config, "local_purge_days"));
|
|
|
|
_purge_subdir(snac, "local", days);
|
2022-10-04 19:46:12 +03:00
|
|
|
}
|
2022-10-17 12:00:34 +03: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)) {
|
|
|
|
purge(&snac);
|
|
|
|
user_free(&snac);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|