diff options
Diffstat (limited to 'format.c')
-rw-r--r-- | format.c | 79 |
1 files changed, 73 insertions, 6 deletions
@@ -5,6 +5,8 @@ #include "xs_regex.h" #include "xs_mime.h" #include "xs_html.h" +#include "xs_json.h" +#include "xs_time.h" #include "snac.h" @@ -36,6 +38,46 @@ 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); + } + else + srv_log(xs_fmt("Error creating '%s'", fn)); + } + + xs_dict *d = NULL; + + if ((f = fopen(fn, "r")) != NULL) { + d = xs_json_load(f); + fclose(f); + + if (d == NULL) + srv_log(xs_fmt("JSON parse error in '%s'", fn)); + } + else + srv_log(xs_fmt("Error opening '%s'", fn)); + + return d; +} + + static xs_str *format_line(const char *line, xs_list **attach) /* formats a line */ { @@ -106,7 +148,7 @@ static xs_str *format_line(const char *line, xs_list **attach) } -xs_str *not_really_markdown(const char *content, xs_list **attach) +xs_str *not_really_markdown(const char *content, xs_list **attach, xs_list **tag) /* formats a content using some Markdown rules */ { xs_str *s = xs_str_new(NULL); @@ -190,11 +232,36 @@ xs_str *not_really_markdown(const char *content, xs_list **attach) { /* traditional emoticons */ - const char **emo = smileys; - - while (*emo) { - s = xs_replace_i(s, emo[0], emo[1]); - emo += 2; + xs *d = emojis(); + int c = 0; + char *k, *v; + + while (xs_dict_next(d, &k, &v, &c)) { + const char *t = NULL; + + /* is it an URL to an image? */ + if (xs_startswith(v, "https:/" "/") && xs_startswith((t = xs_mime_by_ext(v)), "image/")) { + if (tag) { + /* add the emoji to the tag list */ + xs *e = xs_dict_new(); + xs *i = xs_dict_new(); + xs *u = xs_str_utctime(0, ISO_DATE_SPEC); + + e = xs_dict_append(e, "id", v); + e = xs_dict_append(e, "type", "Emoji"); + e = xs_dict_append(e, "name", k); + e = xs_dict_append(e, "updated", u); + + i = xs_dict_append(i, "type", "Image"); + i = xs_dict_append(i, "mediaType", t); + i = xs_dict_append(i, "url", v); + e = xs_dict_append(e, "icon", i); + + *tag = xs_list_append(*tag, e); + } + } + else + s = xs_replace_i(s, k, v); } } |