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-09-19 23:41:30 +03:00
|
|
|
|
|
|
|
#include "snac.h"
|
|
|
|
|
2022-09-20 13:56:21 +03:00
|
|
|
#include <time.h>
|
2022-09-20 00:33:11 +03:00
|
|
|
#include <glob.h>
|
2022-09-20 13:56:21 +03:00
|
|
|
#include <sys/stat.h>
|
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-09-20 10:39:28 +03:00
|
|
|
error = xs_fmt("error 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);
|
|
|
|
|
|
|
|
/* parse */
|
|
|
|
srv_config = xs_json_loads(cfg_data);
|
|
|
|
|
|
|
|
if (srv_config == NULL)
|
2022-09-20 10:39:28 +03:00
|
|
|
error = xs_fmt("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-09-20 10:39:28 +03:00
|
|
|
error = xs_str_new("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
|
|
|
}
|
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 10:39:28 +03:00
|
|
|
if (ret == 0 && error != NULL)
|
|
|
|
srv_log(error);
|
|
|
|
|
2022-09-19 23:41:30 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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 */
|
|
|
|
{
|
|
|
|
free(snac->uid);
|
|
|
|
free(snac->basedir);
|
|
|
|
free(snac->config);
|
|
|
|
free(snac->key);
|
|
|
|
free(snac->actor);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
srv_log(xs_fmt("error opening '%s'", cfg_file));
|
|
|
|
}
|
|
|
|
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-09-20 00:33:11 +03:00
|
|
|
d_char *user_list(void)
|
|
|
|
/* returns the list of user ids */
|
|
|
|
{
|
|
|
|
d_char *list;
|
|
|
|
xs *spec;
|
|
|
|
glob_t globbuf;
|
|
|
|
|
|
|
|
globbuf.gl_offs = 1;
|
|
|
|
|
|
|
|
list = xs_list_new();
|
2022-09-20 10:39:28 +03:00
|
|
|
spec = xs_fmt("%s/user/" "*", srv_basedir);
|
2022-09-20 00:33:11 +03:00
|
|
|
|
|
|
|
if (glob(spec, 0, NULL, &globbuf) == 0) {
|
|
|
|
int n;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
for (n = 0; (p = globbuf.gl_pathv[n]) != NULL; n++) {
|
|
|
|
if ((p = strrchr(p, '/')) != NULL)
|
|
|
|
list = xs_list_append(list, p + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
globfree(&globbuf);
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2022-09-20 10:39:28 +03:00
|
|
|
|
|
|
|
float mtime(char *fn)
|
|
|
|
/* returns the mtime of a file or directory, or 0.0 */
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
float r = 0.0;
|
|
|
|
|
|
|
|
if (stat(fn, &st) != -1)
|
|
|
|
r = (float)st.st_mtim.tv_sec;
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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 */
|
|
|
|
{
|
|
|
|
xs *fn = _follower_fn(snac, actor);
|
|
|
|
|
|
|
|
unlink(fn);
|
|
|
|
|
|
|
|
snac_debug(snac, 2, xs_fmt("follower_del %s %s", actor, fn));
|
|
|
|
|
|
|
|
return 200;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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 */
|
|
|
|
{
|
|
|
|
d_char *list;
|
|
|
|
xs *spec;
|
|
|
|
glob_t globbuf;
|
|
|
|
|
|
|
|
list = xs_list_new();
|
|
|
|
spec = xs_fmt("%s/followers/" "*.json", snac->basedir);
|
|
|
|
|
|
|
|
if (glob(spec, 0, NULL, &globbuf) == 0) {
|
|
|
|
int n;
|
2022-09-20 11:49:24 +03:00
|
|
|
char *fn;
|
2022-09-20 10:39:28 +03:00
|
|
|
|
2022-09-20 11:49:24 +03:00
|
|
|
for (n = 0; (fn = globbuf.gl_pathv[n]) != NULL; n++) {
|
2022-09-20 10:39:28 +03:00
|
|
|
FILE *f;
|
|
|
|
|
2022-09-20 11:49:24 +03:00
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
2022-09-20 10:48:13 +03:00
|
|
|
xs *j = xs_readall(f);
|
|
|
|
xs *o = xs_json_loads(j);
|
|
|
|
|
|
|
|
if (o != NULL)
|
|
|
|
list = xs_list_append(list, o);
|
|
|
|
|
|
|
|
fclose(f);
|
2022-09-20 10:39:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
globfree(&globbuf);
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
2022-09-20 11:02:00 +03:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
glob_t globbuf;
|
|
|
|
d_char *fn = NULL;
|
|
|
|
|
2022-09-20 13:56:21 +03:00
|
|
|
if (glob(spec, 0, NULL, &globbuf) == 0 && globbuf.gl_pathc) {
|
2022-09-20 11:02:00 +03:00
|
|
|
/* get just the first file */
|
|
|
|
fn = xs_str_new(globbuf.gl_pathv[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
globfree(&globbuf);
|
|
|
|
|
|
|
|
return fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-20 11:49:24 +03:00
|
|
|
xs *fn = _timeline_find_fn(snac, id);
|
2022-09-20 11:02:00 +03:00
|
|
|
xs *msg = NULL;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void timeline_del(snac *snac, char *id)
|
|
|
|
/* deletes a message from the timeline */
|
|
|
|
{
|
2022-09-20 11:49:24 +03:00
|
|
|
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-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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
d_char *timeline_list(snac *snac)
|
|
|
|
/* returns a list of the timeline filenames */
|
|
|
|
{
|
|
|
|
d_char *list;
|
|
|
|
xs *spec = xs_fmt("%s/timeline/" "*.json", snac->basedir);
|
|
|
|
glob_t globbuf;
|
|
|
|
int max;
|
|
|
|
|
|
|
|
/* maximum number of items in the timeline */
|
|
|
|
max = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
|
|
|
|
|
|
|
|
list = xs_list_new();
|
|
|
|
|
|
|
|
/* get the list in reverse order */
|
|
|
|
if (glob(spec, 0, NULL, &globbuf) == 0) {
|
|
|
|
int n;
|
|
|
|
|
2022-09-20 13:56:21 +03:00
|
|
|
if (max > globbuf.gl_pathc)
|
|
|
|
max = globbuf.gl_pathc;
|
2022-09-20 11:49:24 +03:00
|
|
|
|
|
|
|
for (n = 0; n < max; n++) {
|
2022-09-20 13:56:21 +03:00
|
|
|
char *fn = globbuf.gl_pathv[globbuf.gl_pathc - n - 1];
|
2022-09-20 11:49:24 +03:00
|
|
|
|
|
|
|
list = xs_list_append(list, fn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
globfree(&globbuf);
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
2022-09-20 12:16:24 +03:00
|
|
|
|
|
|
|
|
2022-09-22 15:46:23 +03:00
|
|
|
void _timeline_parent(snac *snac, char *parent, char *child)
|
|
|
|
/* add child to parent's children list */
|
|
|
|
{
|
2022-09-23 01:03:45 +03:00
|
|
|
/* FIXME: this must be recursive, but *ONLY* for file renaming */
|
|
|
|
|
2022-09-22 15:46:23 +03:00
|
|
|
if (parent != NULL) {
|
|
|
|
xs *pfn = _timeline_find_fn(snac, parent);
|
|
|
|
|
|
|
|
if (pfn != NULL) {
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(pfn, "r")) != NULL) {
|
|
|
|
xs *ji, *msg;
|
|
|
|
|
|
|
|
ji = xs_readall(f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
msg = xs_json_loads(ji);
|
|
|
|
|
|
|
|
if (msg != NULL) {
|
|
|
|
xs *meta;
|
|
|
|
xs *children;
|
|
|
|
|
|
|
|
/* get the children list */
|
2022-09-23 01:00:25 +03:00
|
|
|
meta = xs_dup(xs_dict_get(msg, "_snac"));
|
|
|
|
children = xs_dup(xs_dict_get(meta, "children"));
|
2022-09-22 15:46:23 +03:00
|
|
|
|
|
|
|
/* add */
|
|
|
|
children = xs_list_append(children, child);
|
|
|
|
|
|
|
|
/* re-store */
|
|
|
|
meta = xs_dict_set(meta, "children", children);
|
|
|
|
msg = xs_dict_set(msg, "_snac", meta);
|
|
|
|
|
|
|
|
xs *jo = xs_json_dumps_pp(msg, 4);
|
|
|
|
xs *ntid = tid(0);
|
|
|
|
xs *md5 = xs_md5_hex(parent, strlen(parent));
|
|
|
|
xs *nfn = xs_fmt("%s/timeline/%s-%s.json", snac->basedir, ntid, md5);
|
|
|
|
|
|
|
|
if ((f = fopen(nfn, "w")) != NULL) {
|
|
|
|
fwrite(jo, strlen(jo), 1, f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
snac_debug(snac, 1,
|
|
|
|
xs_fmt("_timeline_parent updated %s %s", parent, nfn));
|
|
|
|
|
|
|
|
unlink(pfn);
|
|
|
|
|
|
|
|
/* generated by this user? link to local timeline */
|
|
|
|
if (xs_startswith(parent, snac->actor)) {
|
|
|
|
xs *lfn = xs_replace(nfn, "/timeline/", "/local/");
|
|
|
|
xs *olfn = xs_replace(pfn, "/timeline/", "/local/");
|
|
|
|
|
|
|
|
link(nfn, lfn);
|
|
|
|
unlink(olfn);
|
|
|
|
|
|
|
|
snac_debug(snac, 1,
|
|
|
|
xs_fmt("_timeline_parent (local) updated %s %s", parent, lfn));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
snac_log(snac, xs_fmt("_timeline_parent error writing %s %s", parent, nfn));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
snac_log(snac, xs_fmt("_timeline_parent error reading %s %s", parent, pfn));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
snac_log(snac, xs_fmt("_timeline_parent error opening %s %s", parent, pfn));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-20 12:16:24 +03:00
|
|
|
void timeline_add(snac *snac, char *id, char *msg, char *parent)
|
|
|
|
/* adds a message to the timeline */
|
|
|
|
{
|
|
|
|
xs *pfn = _timeline_find_fn(snac, id);
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if (pfn != NULL) {
|
|
|
|
snac_log(snac, xs_fmt("timeline_add refusing rewrite %s %s", id, pfn));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* build the new filename */
|
2022-09-20 13:00:13 +03:00
|
|
|
xs *ntid = tid(0);
|
2022-09-20 12:16:24 +03:00
|
|
|
xs *md5 = xs_md5_hex(id, strlen(id));
|
|
|
|
xs *fn = xs_fmt("%s/timeline/%s-%s.json", snac->basedir, ntid, md5);
|
|
|
|
xs *md;
|
|
|
|
|
|
|
|
/* add metadata */
|
|
|
|
md = xs_json_loads("{"
|
|
|
|
"\"children\": [],"
|
|
|
|
"\"liked_by\": [],"
|
|
|
|
"\"announced_by\": [],"
|
|
|
|
"\"parent\": null"
|
|
|
|
"}");
|
|
|
|
|
|
|
|
if (parent != NULL)
|
|
|
|
md = xs_dict_set(md, "parent", parent);
|
|
|
|
|
|
|
|
msg = xs_dict_set(msg, "_snac", md);
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "w")) != NULL) {
|
|
|
|
xs *j = xs_json_dumps_pp(msg, 4);
|
|
|
|
|
|
|
|
fwrite(j, strlen(j), 1, f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
snac_debug(snac, 1, xs_fmt("timeline_add %s %s", id, fn));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generated by this user? link to local timeline */
|
|
|
|
if (xs_startswith(id, snac->actor)) {
|
|
|
|
xs *lfn = xs_replace(fn, "/timeline/", "/local/");
|
|
|
|
link(fn, lfn);
|
|
|
|
|
|
|
|
snac_debug(snac, 1, xs_fmt("timeline_add (local) %s %s", id, lfn));
|
|
|
|
}
|
2022-09-22 15:46:23 +03:00
|
|
|
|
|
|
|
/* relink the parent */
|
|
|
|
_timeline_parent(snac, parent, id);
|
2022-09-20 12:16:24 +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
|
|
|
|
|
|
|
|
|
|
|
d_char *_muted_fn(snac *snac, char *actor)
|
|
|
|
{
|
|
|
|
xs *md5 = xs_md5_hex(actor, strlen(actor));
|
|
|
|
return xs_fmt("%s/muted/%s.json", snac->basedir, md5);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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-09-22 18:12:46 +03:00
|
|
|
void enqueue_output(snac *snac, char *actor, char *msg, int retries)
|
|
|
|
/* enqueues an output message for an actor */
|
2022-09-20 13:00:13 +03:00
|
|
|
{
|
|
|
|
if (strcmp(actor, snac->actor) == 0) {
|
|
|
|
snac_debug(snac, 1, xs_str_new("enqueue refused to myself"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int qrt = xs_number_get(xs_dict_get(srv_config, "query_retry_minutes"));
|
|
|
|
xs *ntid = tid(retries * 60 * qrt);
|
|
|
|
xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
|
|
|
|
xs *tfn = xs_str_cat(fn, ".tmp");
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(tfn, "w")) != NULL) {
|
|
|
|
xs *qmsg = xs_dict_new();
|
|
|
|
xs *rn = xs_number_new(retries);
|
|
|
|
xs *j;
|
|
|
|
|
2022-09-22 18:12:46 +03:00
|
|
|
qmsg = xs_dict_append(qmsg, "type", "output");
|
2022-09-20 13:00:13 +03:00
|
|
|
qmsg = xs_dict_append(qmsg, "actor", actor);
|
|
|
|
qmsg = xs_dict_append(qmsg, "object", msg);
|
|
|
|
qmsg = xs_dict_append(qmsg, "retries", rn);
|
|
|
|
|
|
|
|
j = xs_json_dumps_pp(qmsg, 4);
|
|
|
|
|
|
|
|
fwrite(j, strlen(j), 1, f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
rename(tfn, fn);
|
|
|
|
|
|
|
|
snac_debug(snac, 2, xs_fmt("enqueue %s %s %d", actor, fn, retries));
|
|
|
|
}
|
|
|
|
}
|
2022-09-20 13:43:49 +03:00
|
|
|
|
|
|
|
|
2022-09-22 19:50:39 +03:00
|
|
|
d_char *_actor_fn(snac *snac, char *actor)
|
|
|
|
/* returns the file name for an actor */
|
|
|
|
{
|
|
|
|
xs *md5 = xs_md5_hex(actor, strlen(actor));
|
|
|
|
return xs_fmt("%s/actors/%s.json", snac->basedir, md5);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int actor_add(snac *snac, char *actor, char *msg)
|
|
|
|
/* adds a follower */
|
|
|
|
{
|
|
|
|
int ret = 201; /* created */
|
|
|
|
xs *fn = _actor_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("actor_add %s %s", actor, fn));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-22 19:52:42 +03:00
|
|
|
int actor_get(snac *snac, char *actor, d_char **data)
|
2022-09-22 19:50:39 +03:00
|
|
|
/* returns an already downloaded actor */
|
|
|
|
{
|
|
|
|
xs *fn = _actor_fn(snac, actor);
|
|
|
|
float t;
|
|
|
|
float max_time;
|
|
|
|
int status;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
t = mtime(fn);
|
|
|
|
|
|
|
|
/* no mtime? there is nothing here */
|
|
|
|
if (t == 0.0)
|
|
|
|
return 404;
|
|
|
|
|
|
|
|
/* maximum time for the actor data to be considered stale */
|
|
|
|
max_time = 3600.0 * 36.0;
|
|
|
|
|
|
|
|
if (t + max_time < (float) time(NULL)) {
|
|
|
|
/* actor data exists but also stinks */
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
status = 110; /* "Response Is Stale" */
|
2022-09-22 19:50:39 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* it's still valid */
|
|
|
|
status = 200;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
|
|
|
xs *j = xs_readall(f);
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
*data = xs_json_loads(j);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
status = 500;
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-20 13:43:49 +03:00
|
|
|
d_char *queue(snac *snac)
|
|
|
|
/* returns a list with filenames that can be dequeued */
|
|
|
|
{
|
|
|
|
xs *spec = xs_fmt("%s/queue/" "*.json", snac->basedir);
|
|
|
|
d_char *list = xs_list_new();
|
|
|
|
glob_t globbuf;
|
|
|
|
time_t t = time(NULL);
|
|
|
|
|
|
|
|
if (glob(spec, 0, NULL, &globbuf) == 0) {
|
|
|
|
int n;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
for (n = 0; (p = globbuf.gl_pathv[n]) != NULL; n++) {
|
|
|
|
/* get the retry time from the basename */
|
|
|
|
char *bn = strrchr(p, '/');
|
|
|
|
time_t t2 = atol(bn + 1);
|
|
|
|
|
|
|
|
if (t2 > t)
|
|
|
|
snac_debug(snac, 2, xs_fmt("queue not yet time for %s", p));
|
|
|
|
else {
|
|
|
|
list = xs_list_append(list, p);
|
|
|
|
snac_debug(snac, 2, xs_fmt("queue ready for %s", p));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
globfree(&globbuf);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|