diff options
Diffstat (limited to 'data.c')
-rw-r--r-- | data.c | 130 |
1 files changed, 121 insertions, 9 deletions
@@ -9,6 +9,7 @@ #include "xs_glob.h" #include "xs_set.h" #include "xs_time.h" +#include "xs_regex.h" #include "snac.h" @@ -116,21 +117,33 @@ int srv_open(char *basedir, int auto_upgrade) srv_debug(1, xs_dup("OpenBSD security disabled by admin")); } else { + int smail = xs_type(xs_dict_get(srv_config, "disable_email_notifications")) != XSTYPE_TRUE; + srv_debug(1, xs_fmt("Calling unveil()")); unveil(basedir, "rwc"); unveil("/tmp", "rwc"); - unveil("/usr/sbin/sendmail", "x"); unveil("/etc/resolv.conf", "r"); unveil("/etc/hosts", "r"); unveil("/etc/ssl/openssl.cnf", "r"); unveil("/etc/ssl/cert.pem", "r"); unveil("/usr/share/zoneinfo", "r"); + + if (smail) + unveil("/usr/sbin/sendmail", "x"); + unveil(NULL, NULL); srv_debug(1, xs_fmt("Calling pledge()")); - pledge("stdio rpath wpath cpath flock inet proc exec dns fattr", NULL); + + if (smail) + pledge("stdio rpath wpath cpath flock inet proc exec dns fattr", NULL); + else + pledge("stdio rpath wpath cpath flock inet proc dns fattr", NULL); } #endif /* __OpenBSD__ */ + /* read (and drop) emojis.json, possibly creating it */ + xs_free(emojis()); + return ret; } @@ -393,7 +406,7 @@ int index_del_md5(const char *fn, const char *md5) fclose(f); } else - status = 500; + status = 410; pthread_mutex_unlock(&data_mutex); @@ -796,6 +809,30 @@ double object_ctime(const char *id) } +double object_mtime_by_md5(const char *md5) +{ + xs *fn = _object_fn_by_md5(md5, "object_mtime_by_md5"); + return mtime(fn); +} + + +double object_mtime(const char *id) +{ + xs *md5 = xs_md5_hex(id, strlen(id)); + return object_mtime_by_md5(md5); +} + + +void object_touch(const char *id) +{ + xs *md5 = xs_md5_hex(id, strlen(id)); + xs *fn = _object_fn_by_md5(md5, "object_touch"); + + if (mtime(fn)) + utimes(fn, NULL); +} + + xs_str *_object_index_fn(const char *id, const char *idxsfx) /* returns the filename of an object's index */ { @@ -880,6 +917,9 @@ int object_unadmire(const char *id, const char *actor, int like) status = index_del(fn, actor); + if (valid_status(status)) + index_gc(fn); + srv_debug(0, xs_fmt("object_unadmire (%s) %s %s %d", like ? "Like" : "Announce", actor, fn, status)); @@ -1551,7 +1591,6 @@ int actor_get(const char *actor, xs_dict **data) else d = xs_free(d); -#ifdef STALE_ACTORS xs *fn = _object_fn(actor); double max_time; @@ -1560,13 +1599,20 @@ int actor_get(const char *actor, xs_dict **data) if (mtime(fn) + max_time < (double) time(NULL)) { /* actor data exists but also stinks */ - - /* touch the file */ - utimes(fn, NULL); - status = 205; /* "205: Reset Content" "110: Response Is Stale" */ } -#endif /* STALE_ACTORS */ + + return status; +} + + +int actor_get_refresh(snac *user, const char *actor, xs_dict **data) +/* gets an actor and requests a refresh if it's stale */ +{ + int status = actor_get(actor, data); + + if (status == 205 && user && !xs_startswith(actor, srv_baseurl)) + enqueue_actor_refresh(user, actor); return status; } @@ -2007,6 +2053,47 @@ int instance_unblock(const char *instance) } +/** content filtering **/ + +int content_check(const char *file, const xs_dict *msg) +/* checks if a message's content matches any of the regexes in file */ +/* file format: one regex per line */ +{ + xs *fn = xs_fmt("%s/%s", srv_basedir, file); + FILE *f; + int r = 0; + char *v = xs_dict_get(msg, "content"); + + if (xs_type(v) == XSTYPE_STRING && *v) { + if ((f = fopen(fn, "r")) != NULL) { + srv_debug(1, xs_fmt("content_check: loading regexes from %s", fn)); + + /* massage content (strip HTML tags, etc.) */ + xs *c = xs_regex_replace(v, "<[^>]+>", " "); + c = xs_regex_replace_i(c, " {2,}", " "); + c = xs_tolower_i(c); + + while (!r && !feof(f)) { + xs *rx = xs_strip_i(xs_readline(f)); + + if (*rx) { + xs *l = xs_regex_select_n(c, rx, 1); + + if (xs_list_len(l)) { + srv_debug(1, xs_fmt("content_check: match for '%s'", rx)); + r = 1; + } + } + } + + fclose(f); + } + } + + return r; +} + + /** notifications **/ xs_str *notify_check_time(snac *snac, int reset) @@ -2388,6 +2475,21 @@ void enqueue_verify_links(snac *user) } +void enqueue_actor_refresh(snac *user, const char *actor) +/* enqueues an actor refresh */ +{ + xs *qmsg = _new_qmsg("actor_refresh", "", 0); + char *ntid = xs_dict_get(qmsg, "ntid"); + xs *fn = xs_fmt("%s/queue/%s.json", user->basedir, ntid); + + qmsg = xs_dict_append(qmsg, "actor", actor); + + qmsg = _enqueue_put(fn, qmsg); + + snac_debug(user, 1, xs_fmt("enqueue_actor_refresh %s", actor)); +} + + void enqueue_request_replies(snac *user, const char *id) /* enqueues a request for the replies of a message */ { @@ -2645,6 +2747,16 @@ void purge_server(void) } } } + + /* delete index backups */ + xs *specb = xs_fmt("%s/" "*.bak", v); + xs *bakfs = xs_glob(specb, 0, 0); + + p2 = bakfs; + while (xs_list_iter(&p2, &v2)) { + unlink(v2); + srv_debug(1, xs_fmt("purged %s", v2)); + } } } |