html.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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_id, 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_user_header(snac *snac, d_char *s, int local)
  167. /* creates the HTML header */
  168. {
  169. char *p, *v;
  170. s = xs_str_cat(s, "<!DOCTYPE html>\n<html>\n<head>\n");
  171. s = xs_str_cat(s, "<meta name=\"viewport\" "
  172. "content=\"width=device-width, initial-scale=1\"/>\n");
  173. s = xs_str_cat(s, "<meta name=\"generator\" "
  174. "content=\"" USER_AGENT "\"/>\n");
  175. /* add server CSS */
  176. p = xs_dict_get(srv_config, "cssurls");
  177. while (xs_list_iter(&p, &v)) {
  178. xs *s1 = xs_fmt("<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\"/>\n", v);
  179. s = xs_str_cat(s, s1);
  180. }
  181. /* add the user CSS */
  182. {
  183. xs *css = NULL;
  184. int size;
  185. if (valid_status(static_get(snac, "style.css", &css, &size))) {
  186. xs *s1 = xs_fmt("<style>%s</style>\n", css);
  187. s = xs_str_cat(s, s1);
  188. }
  189. }
  190. {
  191. xs *s1 = xs_fmt("<title>%s</title>\n", xs_dict_get(snac->config, "name"));
  192. s = xs_str_cat(s, s1);
  193. }
  194. s = xs_str_cat(s, "</head>\n<body>\n");
  195. /* top nav */
  196. s = xs_str_cat(s, "<nav style=\"snac-top-nav\">");
  197. {
  198. xs *s1;
  199. if (local)
  200. s1 = xs_fmt("<a href=\"%s/admin\">%s</a></nav>", snac->actor, L("admin"));
  201. else
  202. s1 = xs_fmt("<a href=\"%s\">%s</a></nav>", snac->actor, L("public"));
  203. s = xs_str_cat(s, s1);
  204. }
  205. /* user info */
  206. {
  207. s = xs_str_cat(s, "<div class=\"h-card snac-top-user\">\n");
  208. xs *s1 = xs_fmt("<p class=\"p-name snac-top-user-name\">%s</p>\n",
  209. xs_dict_get(snac->config, "name"));
  210. s = xs_str_cat(s, s1);
  211. xs *s2 = xs_fmt("<p class=\"snac-top-user-id\">@%s@%s</p>\n",
  212. xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"));
  213. s = xs_str_cat(s, s2);
  214. xs *bio = NULL;
  215. not_really_markdown(xs_dict_get(snac->config, "bio"), &bio);
  216. xs *s3 = xs_fmt("<div class=\"p-note snac-top-user-bio\">%s</div>\n", bio);
  217. s = xs_str_cat(s, s3);
  218. s = xs_str_cat(s, "</div>\n");
  219. }
  220. return s;
  221. }
  222. d_char *html_timeline(snac *snac, char *list, int local)
  223. /* returns the HTML for the timeline */
  224. {
  225. d_char *s = xs_str_new(NULL);
  226. s = html_user_header(snac, s, local);
  227. s = xs_str_cat(s, "<h1>HI</h1>\n");
  228. s = xs_str_cat(s, xs_fmt("len() == %d\n", xs_list_len(list)));
  229. {
  230. char *i = xs_list_get(list, 0);
  231. xs *msg = timeline_get(snac, i);
  232. s = html_msg_icon(snac, s, msg);
  233. }
  234. s = xs_str_cat(s, "</html>\n");
  235. return s;
  236. }
  237. int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char **ctype)
  238. {
  239. int status = 404;
  240. snac snac;
  241. char *uid, *p_path;
  242. xs *l = xs_split_n(q_path, "/", 2);
  243. uid = xs_list_get(l, 1);
  244. if (!uid || !user_open(&snac, uid)) {
  245. /* invalid user */
  246. srv_log(xs_fmt("html_get_handler bad user %s", uid));
  247. return 404;
  248. }
  249. p_path = xs_list_get(l, 2);
  250. if (p_path == NULL) {
  251. /* public timeline */
  252. xs *list = local_list(&snac, 0xfffffff);
  253. *body = html_timeline(&snac, list, 1);
  254. *b_size = strlen(*body);
  255. status = 200;
  256. }
  257. else
  258. if (strcmp(p_path, "admin") == 0) {
  259. /* private timeline */
  260. if (!login(&snac, req))
  261. status = 401;
  262. else {
  263. xs *list = timeline_list(&snac, 0xfffffff);
  264. *body = html_timeline(&snac, list, 0);
  265. *b_size = strlen(*body);
  266. status = 200;
  267. }
  268. }
  269. else
  270. if (xs_startswith(p_path, "p/") == 0) {
  271. /* a timeline with just one entry */
  272. }
  273. else
  274. if (xs_startswith(p_path, "s/") == 0) {
  275. /* a static file */
  276. }
  277. else
  278. if (xs_startswith(p_path, "h/") == 0) {
  279. /* an entry from the history */
  280. }
  281. else
  282. status = 404;
  283. user_free(&snac);
  284. if (valid_status(status)) {
  285. *ctype = "text/html; charset=utf-8";
  286. }
  287. return status;
  288. }
  289. int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size,
  290. char **body, int *b_size, char **ctype)
  291. {
  292. int status = 0;
  293. return status;
  294. }