html.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. xs *bio = NULL;
  208. char *_tmpl =
  209. "<div class=\"h-card snac-top-user\">\n"
  210. "<p class=\"p-name snac-top-user-name\">%s</p>\n"
  211. "<p class=\"snac-top-user-id\">@%s@%s</p>\n"
  212. "<div class=\"p-note snac-top-user-bio\">%s</div>\n"
  213. "</div>\n";
  214. not_really_markdown(xs_dict_get(snac->config, "bio"), &bio);
  215. xs *s1 = xs_fmt(_tmpl,
  216. xs_dict_get(snac->config, "name"),
  217. xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"),
  218. bio
  219. );
  220. s = xs_str_cat(s, s1);
  221. }
  222. return s;
  223. }
  224. d_char *html_top_controls(snac *snac, d_char *s)
  225. /* generates the top controls */
  226. {
  227. char *_tmpl =
  228. "<div class=\"snac-top-controls\">\n"
  229. "<div class=\"snac-note\">\n"
  230. "<form method=\"post\" action=\"%s/admin/note\">\n"
  231. "<textarea class=\"snac-textarea\" name=\"content\" "
  232. "rows=\"8\" wrap=\"virtual\" required=\"required\"></textarea>\n"
  233. "<input type=\"hidden\" name=\"in_reply_to\" value=\"\">\n"
  234. "<input type=\"submit\" class=\"button\" value=\"%s\">\n"
  235. "</form><p>\n"
  236. "</div>\n"
  237. "<div class=\"snac-top-controls-more\">\n"
  238. "<details><summary>%s</summary>\n"
  239. "<form method=\"post\" action=\"%s/admin/action\">\n"
  240. "<input type=\"text\" name=\"actor\" required=\"required\">\n"
  241. "<input type=\"submit\" name=\"action\" value=\"%s\"> %s\n"
  242. "</form></p>\n"
  243. "<form method=\"post\" action=\"%s\">\n"
  244. "<input type=\"text\" name=\"id\" required=\"required\">\n"
  245. "<input type=\"submit\" name=\"action\" value=\"%s\"> %s\n"
  246. "</form></p>\n"
  247. "<details><summary>%s</summary>\n"
  248. "<div class=\"snac-user-setup\">\n"
  249. "<form method=\"post\" action=\"%s/admin/user-setup\">\n"
  250. "<p>%s:<br>\n"
  251. "<input type=\"text\" name=\"name\" value=\"%s\"></p>\n"
  252. "<p>%s:<br>\n"
  253. "<input type=\"text\" name=\"avatar\" value=\"%s\"></p>\n"
  254. "<p>%s:<br>\n"
  255. "<textarea name=\"bio\" cols=60 rows=4>%s</textarea></p>\n"
  256. "<p>%s:<br>\n"
  257. "<input type=\"password\" name=\"passwd1\" value=\"\"></p>\n"
  258. "<p>%s:<br>\n"
  259. "<input type=\"password\" name=\"passwd2\" value=\"\"></p>\n"
  260. "<input type=\"submit\" class=\"button\" value=\"%s\">\n"
  261. "</form>\n"
  262. "</div>\n"
  263. "</details>\n"
  264. "</details>\n"
  265. "</div>\n"
  266. "</div>\n";
  267. xs *s1 = xs_fmt(_tmpl,
  268. snac->actor,
  269. L("Post"),
  270. L("More options..."),
  271. snac->actor,
  272. L("Follow"), L("(by URL or user@host)"),
  273. snac->actor,
  274. L("Boost"), L("(by URL)"),
  275. L("User setup..."),
  276. snac->actor,
  277. L("User name"),
  278. xs_dict_get(snac->config, "name"),
  279. L("Avatar URL"),
  280. xs_dict_get(snac->config, "avatar"),
  281. L("Bio"),
  282. xs_dict_get(snac->config, "bio"),
  283. L("Password (only to change it)"),
  284. L("Repeat Password"),
  285. L("Update user info")
  286. );
  287. s = xs_str_cat(s, s1);
  288. return s;
  289. }
  290. d_char *html_timeline(snac *snac, char *list, int local)
  291. /* returns the HTML for the timeline */
  292. {
  293. d_char *s = xs_str_new(NULL);
  294. s = html_user_header(snac, s, local);
  295. if (!local)
  296. s = html_top_controls(snac, s);
  297. s = xs_str_cat(s, "<h1>HI</h1>\n");
  298. s = xs_str_cat(s, xs_fmt("len() == %d\n", xs_list_len(list)));
  299. {
  300. char *i = xs_list_get(list, 0);
  301. xs *msg = timeline_get(snac, i);
  302. s = html_msg_icon(snac, s, msg);
  303. }
  304. s = xs_str_cat(s, "</html>\n");
  305. return s;
  306. }
  307. int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char **ctype)
  308. {
  309. int status = 404;
  310. snac snac;
  311. char *uid, *p_path;
  312. xs *l = xs_split_n(q_path, "/", 2);
  313. uid = xs_list_get(l, 1);
  314. if (!uid || !user_open(&snac, uid)) {
  315. /* invalid user */
  316. srv_log(xs_fmt("html_get_handler bad user %s", uid));
  317. return 404;
  318. }
  319. p_path = xs_list_get(l, 2);
  320. if (p_path == NULL) {
  321. /* public timeline */
  322. xs *list = local_list(&snac, 0xfffffff);
  323. *body = html_timeline(&snac, list, 1);
  324. *b_size = strlen(*body);
  325. status = 200;
  326. }
  327. else
  328. if (strcmp(p_path, "admin") == 0) {
  329. /* private timeline */
  330. if (!login(&snac, req))
  331. status = 401;
  332. else {
  333. xs *list = timeline_list(&snac, 0xfffffff);
  334. *body = html_timeline(&snac, list, 0);
  335. *b_size = strlen(*body);
  336. status = 200;
  337. }
  338. }
  339. else
  340. if (xs_startswith(p_path, "p/") == 0) {
  341. /* a timeline with just one entry */
  342. }
  343. else
  344. if (xs_startswith(p_path, "s/") == 0) {
  345. /* a static file */
  346. }
  347. else
  348. if (xs_startswith(p_path, "h/") == 0) {
  349. /* an entry from the history */
  350. }
  351. else
  352. status = 404;
  353. user_free(&snac);
  354. if (valid_status(status)) {
  355. *ctype = "text/html; charset=utf-8";
  356. }
  357. return status;
  358. }
  359. int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size,
  360. char **body, int *b_size, char **ctype)
  361. {
  362. int status = 0;
  363. return status;
  364. }