html.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 grunfink - MIT license */
  3. #include "xs.h"
  4. #include "xs_io.h"
  5. #include "xs_encdec.h"
  6. #include "xs_json.h"
  7. #include "xs_regex.h"
  8. #include "snac.h"
  9. d_char *not_really_markdown(char *content, d_char **f_content)
  10. /* formats a content using some Markdown rules */
  11. {
  12. d_char *s = NULL;
  13. int in_pre = 0;
  14. int in_blq = 0;
  15. xs *list;
  16. char *p, *v;
  17. xs *wrk = xs_str_new(NULL);
  18. {
  19. /* split by special markup */
  20. xs *sm = xs_regex_split(content,
  21. "(`[^`]+`|\\*\\*?[^\\*]+\\*?\\*|https?:/" "/[^ ]*)");
  22. int n = 0;
  23. p = sm;
  24. while (xs_list_iter(&p, &v)) {
  25. if ((n & 0x1)) {
  26. /* markup */
  27. if (xs_startswith(v, "`")) {
  28. xs *s1 = xs_crop(xs_dup(v), 1, -1);
  29. xs *s2 = xs_fmt("<code>%s</code>", s1);
  30. wrk = xs_str_cat(wrk, s2);
  31. }
  32. else
  33. if (xs_startswith(v, "**")) {
  34. xs *s1 = xs_crop(xs_dup(v), 2, -2);
  35. xs *s2 = xs_fmt("<b>%s</b>", s1);
  36. wrk = xs_str_cat(wrk, s2);
  37. }
  38. else
  39. if (xs_startswith(v, "*")) {
  40. xs *s1 = xs_crop(xs_dup(v), 1, -1);
  41. xs *s2 = xs_fmt("<i>%s</i>", s1);
  42. wrk = xs_str_cat(wrk, s2);
  43. }
  44. else
  45. if (xs_startswith(v, "http")) {
  46. xs *s1 = xs_fmt("<a href=\"%s\">%s</a>", v, v);
  47. wrk = xs_str_cat(wrk, s1);
  48. }
  49. else
  50. /* what the hell is this */
  51. wrk = xs_str_cat(wrk, v);
  52. }
  53. else
  54. /* surrounded text, copy directly */
  55. wrk = xs_str_cat(wrk, v);
  56. n++;
  57. }
  58. }
  59. /* now work by lines */
  60. p = list = xs_split(wrk, "\n");
  61. s = xs_str_new(NULL);
  62. while (xs_list_iter(&p, &v)) {
  63. xs *ss = xs_strip(xs_dup(v));
  64. if (xs_startswith(ss, "```")) {
  65. if (!in_pre)
  66. s = xs_str_cat(s, "<pre>");
  67. else
  68. s = xs_str_cat(s, "</pre>");
  69. in_pre = !in_pre;
  70. continue;
  71. }
  72. if (xs_startswith(ss, ">")) {
  73. /* delete the > and subsequent spaces */
  74. ss = xs_strip(xs_crop(ss, 1, 0));
  75. if (!in_blq) {
  76. s = xs_str_cat(s, "<blockquote>");
  77. in_blq = 1;
  78. }
  79. s = xs_str_cat(s, ss);
  80. s = xs_str_cat(s, "<br>");
  81. continue;
  82. }
  83. if (in_blq) {
  84. s = xs_str_cat(s, "</blockquote>");
  85. in_blq = 0;
  86. }
  87. s = xs_str_cat(s, ss);
  88. s = xs_str_cat(s, "<br>");
  89. }
  90. if (in_blq)
  91. s = xs_str_cat(s, "</blockquote>");
  92. if (in_pre)
  93. s = xs_str_cat(s, "</pre>");
  94. /* some beauty fixes */
  95. s = xs_replace_i(s, "</blockquote><br>", "</blockquote>");
  96. *f_content = s;
  97. return *f_content;
  98. }
  99. int login(snac *snac, char *headers)
  100. /* tries a login */
  101. {
  102. int logged_in = 0;
  103. char *auth = xs_dict_get(headers, "authorization");
  104. if (auth && xs_startswith(auth, "Basic ")) {
  105. int sz;
  106. xs *s1 = xs_crop(xs_dup(auth), 6, 0);
  107. xs *s2 = xs_base64_dec(s1, &sz);
  108. xs *l1 = xs_split_n(s2, ":", 1);
  109. if (xs_list_len(l1) == 2) {
  110. logged_in = check_password(
  111. xs_list_get(l1, 0), xs_list_get(l1, 1),
  112. xs_dict_get(snac->config, "passwd"));
  113. }
  114. }
  115. return logged_in;
  116. }
  117. d_char *html_msg_icon(snac *snac, d_char *s, char *msg)
  118. {
  119. char *actor_id;
  120. xs *actor = NULL;
  121. if ((actor_id = xs_dict_get(msg, "attributedTo")) == NULL)
  122. actor_id = xs_dict_get(msg, "actor");
  123. if (actor_id && valid_status(actor_get(snac, actor_id, &actor))) {
  124. xs *name = NULL;
  125. xs *avatar = NULL;
  126. char *v;
  127. /* get the name */
  128. if ((v = xs_dict_get(actor, "name")) == NULL) {
  129. if ((v = xs_dict_get(actor, "preferredUsername")) == NULL) {
  130. v = "user";
  131. }
  132. }
  133. name = xs_dup(v);
  134. /* get the avatar */
  135. if ((v = xs_dict_get(actor, "icon")) != NULL &&
  136. (v = xs_dict_get(v, "url")) != NULL) {
  137. avatar = xs_dup(v);
  138. }
  139. if (avatar == NULL)
  140. avatar = xs_fmt("data:image/png;base64, %s", susie);
  141. {
  142. xs *s1 = xs_fmt("<p><img class=\"snac-avatar\" src=\"%s\"/>\n", avatar);
  143. s = xs_str_cat(s, s1);
  144. }
  145. {
  146. xs *s1 = xs_fmt("<a href=\"%s\" class=\"p-author h-card snac-author\">%s</a>",
  147. actor, name);
  148. s = xs_str_cat(s, s1);
  149. }
  150. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0) {
  151. xs *s1 = xs_fmt(" <a href=\"%s\">»</a>", xs_dict_get(msg, "id"));
  152. s = xs_str_cat(s, s1);
  153. }
  154. if (!is_msg_public(snac, msg))
  155. s = xs_str_cat(s, " <span title=\"private\">&#128274;</span>");
  156. if ((v = xs_dict_get(msg, "published")) == NULL)
  157. v = "&nbsp;";
  158. {
  159. xs *s1 = xs_fmt("<br>\n<time class=\"dt-published snac-pubdate\">%s</time>\n", v);
  160. s = xs_str_cat(s, s1);
  161. }
  162. s = xs_str_cat(s, "</div>\n");
  163. }
  164. return s;
  165. }
  166. d_char *html_timeline(snac *snac, char *list, int local)
  167. /* returns the HTML for the timeline */
  168. {
  169. d_char *s = xs_str_new(NULL);
  170. s = xs_str_cat(s, "<!DOCTYPE html>\n<html>\n");
  171. s = xs_str_cat(s, "<h1>HI</h1>\n");
  172. s = xs_str_cat(s, xs_fmt("len() == %d\n", xs_list_len(list)));
  173. {
  174. char *i = xs_list_get(list, 0);
  175. xs *msg = timeline_get(snac, i);
  176. s = html_msg_icon(snac, s, msg);
  177. }
  178. s = xs_str_cat(s, "</html>\n");
  179. return s;
  180. }
  181. int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char **ctype)
  182. {
  183. int status = 404;
  184. snac snac;
  185. char *uid, *p_path;
  186. xs *l = xs_split_n(q_path, "/", 2);
  187. uid = xs_list_get(l, 1);
  188. if (!uid || !user_open(&snac, uid)) {
  189. /* invalid user */
  190. srv_log(xs_fmt("html_get_handler bad user %s", uid));
  191. return 404;
  192. }
  193. p_path = xs_list_get(l, 2);
  194. if (p_path == NULL) {
  195. /* public timeline */
  196. xs *list = local_list(&snac, 0xfffffff);
  197. *body = html_timeline(&snac, list, 1);
  198. *b_size = strlen(*body);
  199. status = 200;
  200. }
  201. else
  202. if (strcmp(p_path, "admin") == 0) {
  203. /* private timeline */
  204. if (!login(&snac, req))
  205. status = 401;
  206. else {
  207. xs *list = timeline_list(&snac, 0xfffffff);
  208. *body = html_timeline(&snac, list, 0);
  209. *b_size = strlen(*body);
  210. status = 200;
  211. }
  212. }
  213. else
  214. if (xs_startswith(p_path, "p/") == 0) {
  215. /* a timeline with just one entry */
  216. }
  217. else
  218. if (xs_startswith(p_path, "s/") == 0) {
  219. /* a static file */
  220. }
  221. else
  222. if (xs_startswith(p_path, "h/") == 0) {
  223. /* an entry from the history */
  224. }
  225. else
  226. status = 404;
  227. user_free(&snac);
  228. if (valid_status(status)) {
  229. *ctype = "text/html; charset=utf-8";
  230. }
  231. return status;
  232. }
  233. int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size,
  234. char **body, int *b_size, char **ctype)
  235. {
  236. int status = 0;
  237. return status;
  238. }