diff options
author | default <nobody@localhost> | 2024-05-21 14:12:15 +0200 |
---|---|---|
committer | default <nobody@localhost> | 2024-05-21 14:12:15 +0200 |
commit | 4777fc86cb962917a8f34afb3bfa40f26290815d (patch) | |
tree | 268c078531a018f07c1b6d029f14f87134805f7b /data.c | |
parent | b95fbe4e438a2ab8a8625875e2eedac38dae572f (diff) |
Added const everywhere.
Diffstat (limited to 'data.c')
-rw-r--r-- | data.c | 130 |
1 files changed, 49 insertions, 81 deletions
@@ -29,7 +29,7 @@ pthread_mutex_t data_mutex = {0}; int snac_upgrade(xs_str **error); -int srv_open(char *basedir, int auto_upgrade) +int srv_open(const char *basedir, int auto_upgrade) /* opens a server */ { int ret = 0; @@ -58,10 +58,10 @@ int srv_open(char *basedir, int auto_upgrade) if (srv_config == NULL) error = xs_fmt("ERROR: cannot parse '%s'", cfg_file); else { - char *host; - char *prefix; - char *dbglvl; - char *proto; + const char *host; + const char *prefix; + const char *dbglvl; + const char *proto; host = xs_dict_get(srv_config, "host"); prefix = xs_dict_get(srv_config, "prefix"); @@ -710,7 +710,7 @@ int _object_add(const char *id, const xs_dict *obj, int ow) fclose(f); /* does this object has a parent? */ - char *in_reply_to = xs_dict_get(obj, "inReplyTo"); + const char *in_reply_to = xs_dict_get(obj, "inReplyTo"); if (!xs_is_null(in_reply_to) && *in_reply_to) { /* update the children index of the parent */ @@ -1124,7 +1124,7 @@ int timeline_get_by_md5(snac *snac, const char *md5, xs_dict **msg) } -int timeline_del(snac *snac, char *id) +int timeline_del(snac *snac, const char *id) /* deletes a message from the timeline */ { /* delete from the user's caches */ @@ -1192,17 +1192,16 @@ int timeline_admire(snac *snac, const char *id, const char *admirer, int like) } -xs_list *timeline_top_level(snac *snac, xs_list *list) +xs_list *timeline_top_level(snac *snac, const xs_list *list) /* returns the top level md5 entries from this index */ { xs_set seen; - xs_list *p; xs_str *v; xs_set_init(&seen); - p = list; - while (xs_list_iter(&p, &v)) { + int c = 0; + while (xs_list_next(list, &v, &c)) { char line[256] = ""; strncpy(line, v, sizeof(line)); @@ -1290,7 +1289,7 @@ int following_add(snac *snac, const char *actor, const xs_dict *msg) /* object already exists; if it's of type Accept, the actor is already being followed and confirmed, so do nothing */ - char *type = xs_dict_get(p_object, "type"); + const char *type = xs_dict_get(p_object, "type"); if (!xs_is_null(type) && strcmp(type, "Accept") == 0) { snac_debug(snac, 1, xs_fmt("following_add actor already confirmed %s", actor)); @@ -1546,8 +1545,9 @@ void hide(snac *snac, const char *id) /* resolve to get the id */ if (valid_status(object_get_by_md5(v, &co))) { - if ((v = xs_dict_get(co, "id")) != NULL) - hide(snac, v); + const char *id = xs_dict_get(co, "id"); + if (id != NULL) + hide(snac, id); } } } @@ -1563,7 +1563,7 @@ int is_hidden(snac *snac, const char *id) } -int actor_add(const char *actor, xs_dict *msg) +int actor_add(const char *actor, const xs_dict *msg) /* adds an actor */ { return object_add_ow(actor, msg); @@ -1687,7 +1687,7 @@ int limited(snac *user, const char *id, int cmd) void tag_index(const char *id, const xs_dict *obj) /* update the tag indexes for this object */ { - xs_list *tags = xs_dict_get(obj, "tag"); + const xs_list *tags = xs_dict_get(obj, "tag"); if (is_msg_public(obj) && xs_type(tags) == XSTYPE_LIST && xs_list_len(tags) > 0) { xs *g_tag_dir = xs_fmt("%s/tag", srv_basedir); @@ -1695,9 +1695,10 @@ void tag_index(const char *id, const xs_dict *obj) mkdirx(g_tag_dir); xs_dict *v; - while (xs_list_iter(&tags, &v)) { - char *type = xs_dict_get(v, "type"); - char *name = xs_dict_get(v, "name"); + int ct = 0; + while (xs_list_next(tags, &v, &ct)) { + const char *type = xs_dict_get(v, "type"); + const char *name = xs_dict_get(v, "name"); if (!xs_is_null(type) && !xs_is_null(name) && strcmp(type, "Hashtag") == 0) { while (*name == '#' || *name == '@') @@ -1706,7 +1707,7 @@ void tag_index(const char *id, const xs_dict *obj) if (*name == '\0') continue; - name = xs_tolower_i(name); + name = xs_tolower_i((xs_str *)name); xs *md5_tag = xs_md5_hex(name, strlen(name)); xs *tag_dir = xs_fmt("%s/%c%c", g_tag_dir, md5_tag[0], md5_tag[1]); @@ -1729,7 +1730,7 @@ void tag_index(const char *id, const xs_dict *obj) } -xs_list *tag_search(char *tag, int skip, int show) +xs_list *tag_search(const char *tag, int skip, int show) /* returns the list of posts tagged with tag */ { if (*tag == '#') @@ -1912,7 +1913,7 @@ xs_val *list_content(snac *user, const char *list, const char *actor_md5, int op void list_distribute(snac *user, const char *who, const xs_dict *post) /* distributes the post to all appropriate lists */ { - char *id = xs_dict_get(post, "id"); + const char *id = xs_dict_get(post, "id"); /* if who is not set, use the attributedTo in the message */ if (xs_is_null(who)) @@ -2164,7 +2165,7 @@ void inbox_add(const char *inbox) void inbox_add_by_actor(const xs_dict *actor) /* collects an actor's shared inbox, if it has one */ { - char *v; + const char *v; if (!xs_is_null(v = xs_dict_get(actor, "endpoints")) && !xs_is_null(v = xs_dict_get(v, "sharedInbox"))) { @@ -2210,7 +2211,7 @@ xs_str *_instance_block_fn(const char *instance) xs *s = xs_replace(instance, "http:/" "/", ""); xs *s1 = xs_replace(s, "https:/" "/", ""); xs *l = xs_split(s1, "/"); - char *p = xs_list_get(l, 0); + const char *p = xs_list_get(l, 0); xs *md5 = xs_md5_hex(p, strlen(p)); return xs_fmt("%s/block/%s", srv_basedir, md5); @@ -2279,7 +2280,7 @@ int content_match(const char *file, const xs_dict *msg) xs *fn = xs_fmt("%s/%s", srv_basedir, file); FILE *f; int r = 0; - char *v = xs_dict_get(msg, "content"); + const char *v = xs_dict_get(msg, "content"); if (xs_type(v) == XSTYPE_STRING && *v) { if ((f = fopen(fn, "r")) != NULL) { @@ -2386,7 +2387,7 @@ xs_list *content_search(snac *user, const char *regex, if (id == NULL || is_hidden(user, id)) continue; - char *content = xs_dict_get(post, "content"); + const char *content = xs_dict_get(post, "content"); if (xs_is_null(content)) continue; @@ -2639,7 +2640,7 @@ void enqueue_input(snac *snac, const xs_dict *msg, const xs_dict *req, int retri /* enqueues an input message */ { xs *qmsg = _new_qmsg("input", msg, retries); - char *ntid = xs_dict_get(qmsg, "ntid"); + const char *ntid = xs_dict_get(qmsg, "ntid"); xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid); qmsg = xs_dict_append(qmsg, "req", req); @@ -2654,7 +2655,7 @@ void enqueue_shared_input(const xs_dict *msg, const xs_dict *req, int retries) /* enqueues an input message from the shared input */ { xs *qmsg = _new_qmsg("input", msg, retries); - char *ntid = xs_dict_get(qmsg, "ntid"); + const char *ntid = xs_dict_get(qmsg, "ntid"); xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid); qmsg = xs_dict_append(qmsg, "req", req); @@ -2666,11 +2667,12 @@ void enqueue_shared_input(const xs_dict *msg, const xs_dict *req, int retries) void enqueue_output_raw(const char *keyid, const char *seckey, - xs_dict *msg, xs_str *inbox, int retries, int p_status) + const xs_dict *msg, const xs_str *inbox, + int retries, int p_status) /* enqueues an output message to an inbox */ { xs *qmsg = _new_qmsg("output", msg, retries); - char *ntid = xs_dict_get(qmsg, "ntid"); + const char *ntid = xs_dict_get(qmsg, "ntid"); xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid); xs *ns = xs_number_new(p_status); @@ -2690,7 +2692,8 @@ void enqueue_output_raw(const char *keyid, const char *seckey, } -void enqueue_output(snac *snac, xs_dict *msg, xs_str *inbox, int retries, int p_status) +void enqueue_output(snac *snac, const xs_dict *msg, + const xs_str *inbox, int retries, int p_status) /* enqueues an output message to an inbox */ { if (xs_startswith(inbox, snac->actor)) { @@ -2698,13 +2701,14 @@ void enqueue_output(snac *snac, xs_dict *msg, xs_str *inbox, int retries, int p_ return; } - char *seckey = xs_dict_get(snac->key, "secret"); + const char *seckey = xs_dict_get(snac->key, "secret"); enqueue_output_raw(snac->actor, seckey, msg, inbox, retries, p_status); } -void enqueue_output_by_actor(snac *snac, xs_dict *msg, const xs_str *actor, int retries) +void enqueue_output_by_actor(snac *snac, const xs_dict *msg, + const xs_str *actor, int retries) /* enqueues an output message for an actor */ { xs *inbox = get_actor_inbox(actor); @@ -2716,11 +2720,11 @@ void enqueue_output_by_actor(snac *snac, xs_dict *msg, const xs_str *actor, int } -void enqueue_email(xs_str *msg, int retries) +void enqueue_email(const xs_str *msg, int retries) /* enqueues an email message to be sent */ { xs *qmsg = _new_qmsg("email", msg, retries); - char *ntid = xs_dict_get(qmsg, "ntid"); + const char *ntid = xs_dict_get(qmsg, "ntid"); xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid); qmsg = _enqueue_put(fn, qmsg); @@ -2733,7 +2737,7 @@ void enqueue_telegram(const xs_str *msg, const char *bot, const char *chat_id) /* enqueues a message to be sent via Telegram */ { xs *qmsg = _new_qmsg("telegram", msg, 0); - char *ntid = xs_dict_get(qmsg, "ntid"); + const char *ntid = xs_dict_get(qmsg, "ntid"); xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid); qmsg = xs_dict_append(qmsg, "bot", bot); @@ -2748,7 +2752,7 @@ void enqueue_ntfy(const xs_str *msg, const char *ntfy_server, const char *ntfy_t /* enqueues a message to be sent via ntfy */ { xs *qmsg = _new_qmsg("ntfy", msg, 0); - char *ntid = xs_dict_get(qmsg, "ntid"); + const char *ntid = xs_dict_get(qmsg, "ntid"); xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid); qmsg = xs_dict_append(qmsg, "ntfy_server", ntfy_server); @@ -2764,7 +2768,7 @@ void enqueue_message(snac *snac, const xs_dict *msg) /* enqueues an output message */ { xs *qmsg = _new_qmsg("message", msg, 0); - char *ntid = xs_dict_get(qmsg, "ntid"); + const char *ntid = xs_dict_get(qmsg, "ntid"); xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid); qmsg = _enqueue_put(fn, qmsg); @@ -2807,7 +2811,7 @@ void enqueue_verify_links(snac *user) /* enqueues a link verification */ { xs *qmsg = _new_qmsg("verify_links", "", 0); - char *ntid = xs_dict_get(qmsg, "ntid"); + const char *ntid = xs_dict_get(qmsg, "ntid"); xs *fn = xs_fmt("%s/queue/%s.json", user->basedir, ntid); qmsg = _enqueue_put(fn, qmsg); @@ -2832,42 +2836,6 @@ void enqueue_actor_refresh(snac *user, const char *actor, int forward_secs) } -void enqueue_request_replies(snac *user, const char *id) -/* enqueues a request for the replies of a message */ -{ - /* test first if this precise request is already in the queue */ - xs *queue = user_queue(user); - xs_list *p = queue; - xs_str *v; - - while (xs_list_iter(&p, &v)) { - xs *q_item = queue_get(v); - - if (q_item != NULL) { - const char *type = xs_dict_get(q_item, "type"); - const char *msg = xs_dict_get(q_item, "message"); - - if (type && msg && strcmp(type, "request_replies") == 0 && strcmp(msg, id) == 0) { - /* don't requeue */ - snac_debug(user, 1, xs_fmt("enqueue_request_replies already here %s", id)); - return; - } - } - } - - /* not there; enqueue the request with a small delay */ - xs *qmsg = _new_qmsg("request_replies", id, 0); - xs *ntid = tid(10); - xs *fn = xs_fmt("%s/queue/%s.json", user->basedir, ntid); - - qmsg = xs_dict_set(qmsg, "ntid", ntid); - - qmsg = _enqueue_put(fn, qmsg); - - snac_debug(user, 2, xs_fmt("enqueue_request_replies %s", id)); -} - - int was_question_voted(snac *user, const char *id) /* returns true if the user voted in this poll */ { @@ -2881,7 +2849,7 @@ int was_question_voted(snac *user, const char *id) xs *obj = NULL; if (valid_status(object_get_by_md5(md5, &obj))) { - char *atto = get_atto(obj); + const char *atto = get_atto(obj); if (atto && strcmp(atto, user->actor) == 0 && !xs_is_null(xs_dict_get(obj, "name"))) { voted = 1; @@ -3055,7 +3023,7 @@ void purge_server(void) 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); + const char *md5 = xs_list_get(l, -1); object_del_by_md5(md5); cnt++; @@ -3147,7 +3115,7 @@ void purge_user(snac *snac) /* do the purge for this user */ { int priv_days, pub_days, user_days = 0; - char *v; + const char *v; int n; priv_days = xs_number_get(xs_dict_get(srv_config, "timeline_purge_days")); @@ -3256,7 +3224,7 @@ void srv_archive(const char *direction, const char *url, xs_dict *req, if (p_size && payload) { xs *payload_fn = NULL; xs *payload_fn_raw = NULL; - char *v = xs_dict_get(req, "content-type"); + const char *v = xs_dict_get(req, "content-type"); if (v && xs_str_in(v, "json") != -1) { payload_fn = xs_fmt("%s/payload.json", dir); @@ -3287,7 +3255,7 @@ void srv_archive(const char *direction, const char *url, xs_dict *req, if (b_size && body) { xs *body_fn = NULL; - char *v = xs_dict_get(headers, "content-type"); + const char *v = xs_dict_get(headers, "content-type"); if (v && xs_str_in(v, "json") != -1) { body_fn = xs_fmt("%s/body.json", dir); @@ -3356,7 +3324,7 @@ void srv_archive_error(const char *prefix, const xs_str *err, } -void srv_archive_qitem(char *prefix, xs_dict *q_item) +void srv_archive_qitem(const char *prefix, xs_dict *q_item) /* archives a q_item in the error folder */ { xs *ntid = tid(0); |