diff --git a/data.c b/data.c index 83e1b15..142fe8c 100644 --- a/data.c +++ b/data.c @@ -132,6 +132,9 @@ int srv_open(char *basedir, int auto_upgrade) } #endif /* __OpenBSD__ */ + /* read (and drop) emojis.json, possibly creating it */ + xs_free(emojis()); + return ret; } diff --git a/format.c b/format.c index 9944822..06e006a 100644 --- a/format.c +++ b/format.c @@ -5,6 +5,7 @@ #include "xs_regex.h" #include "xs_mime.h" #include "xs_html.h" +#include "xs_json.h" #include "snac.h" @@ -36,6 +37,39 @@ const char *smileys[] = { }; +xs_dict *emojis(void) +/* returns a dict with the emojis */ +{ + xs *fn = xs_fmt("%s/emojis.json", srv_basedir); + FILE *f; + + if (mtime(fn) == 0) { + /* file does not exist; create it with the defaults */ + xs *d = xs_dict_new(); + const char **emo = smileys; + + while (*emo) { + d = xs_dict_append(d, emo[0], emo[1]); + emo += 2; + } + + if ((f = fopen(fn, "w")) != NULL) { + xs_json_dump(d, 4, f); + fclose(f); + } + } + + xs_dict *d = NULL; + + if ((f = fopen(fn, "r")) != NULL) { + d = xs_json_load(f); + fclose(f); + } + + return d; +} + + static xs_str *format_line(const char *line, xs_list **attach) /* formats a line */ { @@ -190,11 +224,12 @@ xs_str *not_really_markdown(const char *content, xs_list **attach) { /* traditional emoticons */ - const char **emo = smileys; + xs *d = emojis(); + int c = 0; + char *k, *v; - while (*emo) { - s = xs_replace_i(s, emo[0], emo[1]); - emo += 2; + while (xs_dict_next(d, &k, &v, &c)) { + s = xs_replace_i(s, k, v); } } diff --git a/snac.h b/snac.h index 5a92abc..2953af7 100644 --- a/snac.h +++ b/snac.h @@ -304,6 +304,7 @@ int activitypub_post_handler(const xs_dict *req, const char *q_path, char *payload, int p_size, char **body, int *b_size, char **ctype); +xs_dict *emojis(void); xs_str *not_really_markdown(const char *content, xs_list **attach); xs_str *sanitize(const char *content); xs_str *encode_html(const char *str);