commit bc752b7

default  ·  2024-03-25 14:33:14 +0000 UTC
parent 5a8c4ca
Emojis are now read from ~/emojis.json.
3 files changed,  +43, -4
M data.c
M snac.h
M data.c
+3, -0
 1@@ -132,6 +132,9 @@ int srv_open(char *basedir, int auto_upgrade)
 2     }
 3 #endif /* __OpenBSD__ */
 4 
 5+    /* read (and drop) emojis.json, possibly creating it */
 6+    xs_free(emojis());
 7+
 8     return ret;
 9 }
10 
+39, -4
 1@@ -5,6 +5,7 @@
 2 #include "xs_regex.h"
 3 #include "xs_mime.h"
 4 #include "xs_html.h"
 5+#include "xs_json.h"
 6 
 7 #include "snac.h"
 8 
 9@@ -36,6 +37,39 @@ const char *smileys[] = {
10 };
11 
12 
13+xs_dict *emojis(void)
14+/* returns a dict with the emojis */
15+{
16+    xs *fn = xs_fmt("%s/emojis.json", srv_basedir);
17+    FILE *f;
18+
19+    if (mtime(fn) == 0) {
20+        /* file does not exist; create it with the defaults */
21+        xs *d = xs_dict_new();
22+        const char **emo = smileys;
23+
24+        while (*emo) {
25+            d = xs_dict_append(d, emo[0], emo[1]);
26+            emo += 2;
27+        }
28+
29+        if ((f = fopen(fn, "w")) != NULL) {
30+            xs_json_dump(d, 4, f);
31+            fclose(f);
32+        }
33+    }
34+
35+    xs_dict *d = NULL;
36+
37+    if ((f = fopen(fn, "r")) != NULL) {
38+        d = xs_json_load(f);
39+        fclose(f);
40+    }
41+
42+    return d;
43+}
44+
45+
46 static xs_str *format_line(const char *line, xs_list **attach)
47 /* formats a line */
48 {
49@@ -190,11 +224,12 @@ xs_str *not_really_markdown(const char *content, xs_list **attach)
50 
51     {
52         /* traditional emoticons */
53-        const char **emo = smileys;
54+        xs *d = emojis();
55+        int c = 0;
56+        char *k, *v;
57 
58-        while (*emo) {
59-            s = xs_replace_i(s, emo[0], emo[1]);
60-            emo += 2;
61+        while (xs_dict_next(d, &k, &v, &c)) {
62+            s = xs_replace_i(s, k, v);
63         }
64     }
65 
M snac.h
+1, -0
1@@ -304,6 +304,7 @@ int activitypub_post_handler(const xs_dict *req, const char *q_path,
2                              char *payload, int p_size,
3                              char **body, int *b_size, char **ctype);
4 
5+xs_dict *emojis(void);
6 xs_str *not_really_markdown(const char *content, xs_list **attach);
7 xs_str *sanitize(const char *content);
8 xs_str *encode_html(const char *str);