html.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 "xs_set.h"
  9. #include "snac.h"
  10. d_char *not_really_markdown(char *content, d_char **f_content)
  11. /* formats a content using some Markdown rules */
  12. {
  13. d_char *s = NULL;
  14. int in_pre = 0;
  15. int in_blq = 0;
  16. xs *list;
  17. char *p, *v;
  18. xs *wrk = xs_str_new(NULL);
  19. {
  20. /* split by special markup */
  21. xs *sm = xs_regex_split(content,
  22. "(`[^`]+`|\\*\\*?[^\\*]+\\*?\\*|https?:/" "/[^ ]*)");
  23. int n = 0;
  24. p = sm;
  25. while (xs_list_iter(&p, &v)) {
  26. if ((n & 0x1)) {
  27. /* markup */
  28. if (xs_startswith(v, "`")) {
  29. xs *s1 = xs_crop(xs_dup(v), 1, -1);
  30. xs *s2 = xs_fmt("<code>%s</code>", s1);
  31. wrk = xs_str_cat(wrk, s2);
  32. }
  33. else
  34. if (xs_startswith(v, "**")) {
  35. xs *s1 = xs_crop(xs_dup(v), 2, -2);
  36. xs *s2 = xs_fmt("<b>%s</b>", s1);
  37. wrk = xs_str_cat(wrk, s2);
  38. }
  39. else
  40. if (xs_startswith(v, "*")) {
  41. xs *s1 = xs_crop(xs_dup(v), 1, -1);
  42. xs *s2 = xs_fmt("<i>%s</i>", s1);
  43. wrk = xs_str_cat(wrk, s2);
  44. }
  45. else
  46. if (xs_startswith(v, "http")) {
  47. xs *s1 = xs_fmt("<a href=\"%s\">%s</a>", v, v);
  48. wrk = xs_str_cat(wrk, s1);
  49. }
  50. else
  51. /* what the hell is this */
  52. wrk = xs_str_cat(wrk, v);
  53. }
  54. else
  55. /* surrounded text, copy directly */
  56. wrk = xs_str_cat(wrk, v);
  57. n++;
  58. }
  59. }
  60. /* now work by lines */
  61. p = list = xs_split(wrk, "\n");
  62. s = xs_str_new(NULL);
  63. while (xs_list_iter(&p, &v)) {
  64. xs *ss = xs_strip(xs_dup(v));
  65. if (xs_startswith(ss, "```")) {
  66. if (!in_pre)
  67. s = xs_str_cat(s, "<pre>");
  68. else
  69. s = xs_str_cat(s, "</pre>");
  70. in_pre = !in_pre;
  71. continue;
  72. }
  73. if (xs_startswith(ss, ">")) {
  74. /* delete the > and subsequent spaces */
  75. ss = xs_strip(xs_crop(ss, 1, 0));
  76. if (!in_blq) {
  77. s = xs_str_cat(s, "<blockquote>");
  78. in_blq = 1;
  79. }
  80. s = xs_str_cat(s, ss);
  81. s = xs_str_cat(s, "<br>");
  82. continue;
  83. }
  84. if (in_blq) {
  85. s = xs_str_cat(s, "</blockquote>");
  86. in_blq = 0;
  87. }
  88. s = xs_str_cat(s, ss);
  89. s = xs_str_cat(s, "<br>");
  90. }
  91. if (in_blq)
  92. s = xs_str_cat(s, "</blockquote>");
  93. if (in_pre)
  94. s = xs_str_cat(s, "</pre>");
  95. /* some beauty fixes */
  96. s = xs_replace_i(s, "</blockquote><br>", "</blockquote>");
  97. *f_content = s;
  98. return *f_content;
  99. }
  100. int login(snac *snac, char *headers)
  101. /* tries a login */
  102. {
  103. int logged_in = 0;
  104. char *auth = xs_dict_get(headers, "authorization");
  105. if (auth && xs_startswith(auth, "Basic ")) {
  106. int sz;
  107. xs *s1 = xs_crop(xs_dup(auth), 6, 0);
  108. xs *s2 = xs_base64_dec(s1, &sz);
  109. xs *l1 = xs_split_n(s2, ":", 1);
  110. if (xs_list_len(l1) == 2) {
  111. logged_in = check_password(
  112. xs_list_get(l1, 0), xs_list_get(l1, 1),
  113. xs_dict_get(snac->config, "passwd"));
  114. }
  115. }
  116. return logged_in;
  117. }
  118. d_char *html_msg_icon(snac *snac, d_char *s, char *msg)
  119. {
  120. char *actor_id;
  121. xs *actor = NULL;
  122. if ((actor_id = xs_dict_get(msg, "attributedTo")) == NULL)
  123. actor_id = xs_dict_get(msg, "actor");
  124. if (actor_id && valid_status(actor_get(snac, actor_id, &actor))) {
  125. xs *name = NULL;
  126. xs *avatar = NULL;
  127. char *v;
  128. /* get the name */
  129. if ((v = xs_dict_get(actor, "name")) == NULL) {
  130. if ((v = xs_dict_get(actor, "preferredUsername")) == NULL) {
  131. v = "user";
  132. }
  133. }
  134. name = xs_dup(v);
  135. /* get the avatar */
  136. if ((v = xs_dict_get(actor, "icon")) != NULL &&
  137. (v = xs_dict_get(v, "url")) != NULL) {
  138. avatar = xs_dup(v);
  139. }
  140. if (avatar == NULL)
  141. avatar = xs_fmt("data:image/png;base64, %s", susie);
  142. {
  143. xs *s1 = xs_fmt("<p><img class=\"snac-avatar\" src=\"%s\"/>\n", avatar);
  144. s = xs_str_cat(s, s1);
  145. }
  146. {
  147. xs *s1 = xs_fmt("<a href=\"%s\" class=\"p-author h-card snac-author\">%s</a>",
  148. actor_id, name);
  149. s = xs_str_cat(s, s1);
  150. }
  151. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0) {
  152. xs *s1 = xs_fmt(" <a href=\"%s\">»</a>", xs_dict_get(msg, "id"));
  153. s = xs_str_cat(s, s1);
  154. }
  155. if (!is_msg_public(snac, msg))
  156. s = xs_str_cat(s, " <span title=\"private\">&#128274;</span>");
  157. if ((v = xs_dict_get(msg, "published")) == NULL)
  158. v = "&nbsp;";
  159. {
  160. xs *s1 = xs_fmt("<br>\n<time class=\"dt-published snac-pubdate\">%s</time>\n", v);
  161. s = xs_str_cat(s, s1);
  162. }
  163. s = xs_str_cat(s, "</div>\n");
  164. }
  165. return s;
  166. }
  167. d_char *html_user_header(snac *snac, d_char *s, int local)
  168. /* creates the HTML header */
  169. {
  170. char *p, *v;
  171. s = xs_str_cat(s, "<!DOCTYPE html>\n<html>\n<head>\n");
  172. s = xs_str_cat(s, "<meta name=\"viewport\" "
  173. "content=\"width=device-width, initial-scale=1\"/>\n");
  174. s = xs_str_cat(s, "<meta name=\"generator\" "
  175. "content=\"" USER_AGENT "\"/>\n");
  176. /* add server CSS */
  177. p = xs_dict_get(srv_config, "cssurls");
  178. while (xs_list_iter(&p, &v)) {
  179. xs *s1 = xs_fmt("<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\"/>\n", v);
  180. s = xs_str_cat(s, s1);
  181. }
  182. /* add the user CSS */
  183. {
  184. xs *css = NULL;
  185. int size;
  186. if (valid_status(static_get(snac, "style.css", &css, &size))) {
  187. xs *s1 = xs_fmt("<style>%s</style>\n", css);
  188. s = xs_str_cat(s, s1);
  189. }
  190. }
  191. {
  192. xs *s1 = xs_fmt("<title>%s</title>\n", xs_dict_get(snac->config, "name"));
  193. s = xs_str_cat(s, s1);
  194. }
  195. s = xs_str_cat(s, "</head>\n<body>\n");
  196. /* top nav */
  197. s = xs_str_cat(s, "<nav style=\"snac-top-nav\">");
  198. {
  199. xs *s1;
  200. if (local)
  201. s1 = xs_fmt("<a href=\"%s/admin\">%s</a></nav>", snac->actor, L("admin"));
  202. else
  203. s1 = xs_fmt("<a href=\"%s\">%s</a></nav>", snac->actor, L("public"));
  204. s = xs_str_cat(s, s1);
  205. }
  206. /* user info */
  207. {
  208. xs *bio = NULL;
  209. char *_tmpl =
  210. "<div class=\"h-card snac-top-user\">\n"
  211. "<p class=\"p-name snac-top-user-name\">%s</p>\n"
  212. "<p class=\"snac-top-user-id\">@%s@%s</p>\n"
  213. "<div class=\"p-note snac-top-user-bio\">%s</div>\n"
  214. "</div>\n";
  215. not_really_markdown(xs_dict_get(snac->config, "bio"), &bio);
  216. xs *s1 = xs_fmt(_tmpl,
  217. xs_dict_get(snac->config, "name"),
  218. xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"),
  219. bio
  220. );
  221. s = xs_str_cat(s, s1);
  222. }
  223. return s;
  224. }
  225. d_char *html_top_controls(snac *snac, d_char *s)
  226. /* generates the top controls */
  227. {
  228. char *_tmpl =
  229. "<div class=\"snac-top-controls\">\n"
  230. "<div class=\"snac-note\">\n"
  231. "<form method=\"post\" action=\"%s/admin/note\">\n"
  232. "<textarea class=\"snac-textarea\" name=\"content\" "
  233. "rows=\"8\" wrap=\"virtual\" required=\"required\"></textarea>\n"
  234. "<input type=\"hidden\" name=\"in_reply_to\" value=\"\">\n"
  235. "<input type=\"submit\" class=\"button\" value=\"%s\">\n"
  236. "</form><p>\n"
  237. "</div>\n"
  238. "<div class=\"snac-top-controls-more\">\n"
  239. "<details><summary>%s</summary>\n"
  240. "<form method=\"post\" action=\"%s/admin/action\">\n"
  241. "<input type=\"text\" name=\"actor\" required=\"required\">\n"
  242. "<input type=\"submit\" name=\"action\" value=\"%s\"> %s\n"
  243. "</form></p>\n"
  244. "<form method=\"post\" action=\"%s\">\n"
  245. "<input type=\"text\" name=\"id\" required=\"required\">\n"
  246. "<input type=\"submit\" name=\"action\" value=\"%s\"> %s\n"
  247. "</form></p>\n"
  248. "<details><summary>%s</summary>\n"
  249. "<div class=\"snac-user-setup\">\n"
  250. "<form method=\"post\" action=\"%s/admin/user-setup\">\n"
  251. "<p>%s:<br>\n"
  252. "<input type=\"text\" name=\"name\" value=\"%s\"></p>\n"
  253. "<p>%s:<br>\n"
  254. "<input type=\"text\" name=\"avatar\" value=\"%s\"></p>\n"
  255. "<p>%s:<br>\n"
  256. "<textarea name=\"bio\" cols=60 rows=4>%s</textarea></p>\n"
  257. "<p>%s:<br>\n"
  258. "<input type=\"password\" name=\"passwd1\" value=\"\"></p>\n"
  259. "<p>%s:<br>\n"
  260. "<input type=\"password\" name=\"passwd2\" value=\"\"></p>\n"
  261. "<input type=\"submit\" class=\"button\" value=\"%s\">\n"
  262. "</form>\n"
  263. "</div>\n"
  264. "</details>\n"
  265. "</details>\n"
  266. "</div>\n"
  267. "</div>\n";
  268. xs *s1 = xs_fmt(_tmpl,
  269. snac->actor,
  270. L("Post"),
  271. L("More options..."),
  272. snac->actor,
  273. L("Follow"), L("(by URL or user@host)"),
  274. snac->actor,
  275. L("Boost"), L("(by URL)"),
  276. L("User setup..."),
  277. snac->actor,
  278. L("User name"),
  279. xs_dict_get(snac->config, "name"),
  280. L("Avatar URL"),
  281. xs_dict_get(snac->config, "avatar"),
  282. L("Bio"),
  283. xs_dict_get(snac->config, "bio"),
  284. L("Password (only to change it)"),
  285. L("Repeat Password"),
  286. L("Update user info")
  287. );
  288. s = xs_str_cat(s, s1);
  289. return s;
  290. }
  291. d_char *html_timeline(snac *snac, char *list, int local)
  292. /* returns the HTML for the timeline */
  293. {
  294. d_char *s = xs_str_new(NULL);
  295. s = html_user_header(snac, s, local);
  296. if (!local)
  297. s = html_top_controls(snac, s);
  298. s = xs_str_cat(s, "<h1>HI</h1>\n");
  299. s = xs_str_cat(s, xs_fmt("len() == %d\n", xs_list_len(list)));
  300. {
  301. char *i = xs_list_get(list, 0);
  302. xs *msg = timeline_get(snac, i);
  303. s = html_msg_icon(snac, s, msg);
  304. }
  305. s = xs_str_cat(s, "</html>\n");
  306. return s;
  307. }
  308. int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char **ctype)
  309. {
  310. int status = 404;
  311. snac snac;
  312. char *uid, *p_path;
  313. xs *l = xs_split_n(q_path, "/", 2);
  314. uid = xs_list_get(l, 1);
  315. if (!uid || !user_open(&snac, uid)) {
  316. /* invalid user */
  317. srv_log(xs_fmt("html_get_handler bad user %s", uid));
  318. return 404;
  319. }
  320. p_path = xs_list_get(l, 2);
  321. if (p_path == NULL) {
  322. /* public timeline */
  323. xs *list = local_list(&snac, 0xfffffff);
  324. *body = html_timeline(&snac, list, 1);
  325. *b_size = strlen(*body);
  326. status = 200;
  327. }
  328. else
  329. if (strcmp(p_path, "admin") == 0) {
  330. /* private timeline */
  331. if (!login(&snac, req))
  332. status = 401;
  333. else {
  334. xs *list = timeline_list(&snac, 0xfffffff);
  335. *body = html_timeline(&snac, list, 0);
  336. *b_size = strlen(*body);
  337. status = 200;
  338. }
  339. }
  340. else
  341. if (xs_startswith(p_path, "p/") == 0) {
  342. /* a timeline with just one entry */
  343. }
  344. else
  345. if (xs_startswith(p_path, "s/") == 0) {
  346. /* a static file */
  347. }
  348. else
  349. if (xs_startswith(p_path, "h/") == 0) {
  350. /* an entry from the history */
  351. }
  352. else
  353. status = 404;
  354. user_free(&snac);
  355. if (valid_status(status)) {
  356. *ctype = "text/html; charset=utf-8";
  357. }
  358. return status;
  359. }
  360. int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size,
  361. char **body, int *b_size, char **ctype)
  362. {
  363. int status = 0;
  364. return status;
  365. }