html.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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 "xs_openssl.h"
  10. #include "snac.h"
  11. d_char *not_really_markdown(char *content, d_char **f_content)
  12. /* formats a content using some Markdown rules */
  13. {
  14. d_char *s = NULL;
  15. int in_pre = 0;
  16. int in_blq = 0;
  17. xs *list;
  18. char *p, *v;
  19. xs *wrk = xs_str_new(NULL);
  20. {
  21. /* split by special markup */
  22. xs *sm = xs_regex_split(content,
  23. "(`[^`]+`|\\*\\*?[^\\*]+\\*?\\*|https?:/" "/[^ ]*)");
  24. int n = 0;
  25. p = sm;
  26. while (xs_list_iter(&p, &v)) {
  27. if ((n & 0x1)) {
  28. /* markup */
  29. if (xs_startswith(v, "`")) {
  30. xs *s1 = xs_crop(xs_dup(v), 1, -1);
  31. xs *s2 = xs_fmt("<code>%s</code>", s1);
  32. wrk = xs_str_cat(wrk, s2);
  33. }
  34. else
  35. if (xs_startswith(v, "**")) {
  36. xs *s1 = xs_crop(xs_dup(v), 2, -2);
  37. xs *s2 = xs_fmt("<b>%s</b>", s1);
  38. wrk = xs_str_cat(wrk, s2);
  39. }
  40. else
  41. if (xs_startswith(v, "*")) {
  42. xs *s1 = xs_crop(xs_dup(v), 1, -1);
  43. xs *s2 = xs_fmt("<i>%s</i>", s1);
  44. wrk = xs_str_cat(wrk, s2);
  45. }
  46. else
  47. if (xs_startswith(v, "http")) {
  48. xs *s1 = xs_fmt("<a href=\"%s\">%s</a>", v, v);
  49. wrk = xs_str_cat(wrk, s1);
  50. }
  51. else
  52. /* what the hell is this */
  53. wrk = xs_str_cat(wrk, v);
  54. }
  55. else
  56. /* surrounded text, copy directly */
  57. wrk = xs_str_cat(wrk, v);
  58. n++;
  59. }
  60. }
  61. /* now work by lines */
  62. p = list = xs_split(wrk, "\n");
  63. s = xs_str_new(NULL);
  64. while (xs_list_iter(&p, &v)) {
  65. xs *ss = xs_strip(xs_dup(v));
  66. if (xs_startswith(ss, "```")) {
  67. if (!in_pre)
  68. s = xs_str_cat(s, "<pre>");
  69. else
  70. s = xs_str_cat(s, "</pre>");
  71. in_pre = !in_pre;
  72. continue;
  73. }
  74. if (xs_startswith(ss, ">")) {
  75. /* delete the > and subsequent spaces */
  76. ss = xs_strip(xs_crop(ss, 1, 0));
  77. if (!in_blq) {
  78. s = xs_str_cat(s, "<blockquote>");
  79. in_blq = 1;
  80. }
  81. s = xs_str_cat(s, ss);
  82. s = xs_str_cat(s, "<br>");
  83. continue;
  84. }
  85. if (in_blq) {
  86. s = xs_str_cat(s, "</blockquote>");
  87. in_blq = 0;
  88. }
  89. s = xs_str_cat(s, ss);
  90. s = xs_str_cat(s, "<br>");
  91. }
  92. if (in_blq)
  93. s = xs_str_cat(s, "</blockquote>");
  94. if (in_pre)
  95. s = xs_str_cat(s, "</pre>");
  96. /* some beauty fixes */
  97. s = xs_replace_i(s, "</blockquote><br>", "</blockquote>");
  98. *f_content = s;
  99. return *f_content;
  100. }
  101. int login(snac *snac, char *headers)
  102. /* tries a login */
  103. {
  104. int logged_in = 0;
  105. char *auth = xs_dict_get(headers, "authorization");
  106. if (auth && xs_startswith(auth, "Basic ")) {
  107. int sz;
  108. xs *s1 = xs_crop(xs_dup(auth), 6, 0);
  109. xs *s2 = xs_base64_dec(s1, &sz);
  110. xs *l1 = xs_split_n(s2, ":", 1);
  111. if (xs_list_len(l1) == 2) {
  112. logged_in = check_password(
  113. xs_list_get(l1, 0), xs_list_get(l1, 1),
  114. xs_dict_get(snac->config, "passwd"));
  115. }
  116. }
  117. return logged_in;
  118. }
  119. d_char *html_msg_icon(snac *snac, d_char *os, char *msg)
  120. {
  121. char *actor_id;
  122. xs *actor = NULL;
  123. xs *s = xs_str_new(NULL);
  124. if ((actor_id = xs_dict_get(msg, "attributedTo")) == NULL)
  125. actor_id = xs_dict_get(msg, "actor");
  126. if (actor_id && valid_status(actor_get(snac, actor_id, &actor))) {
  127. xs *name = NULL;
  128. xs *avatar = NULL;
  129. char *v;
  130. /* get the name */
  131. if ((v = xs_dict_get(actor, "name")) == NULL) {
  132. if ((v = xs_dict_get(actor, "preferredUsername")) == NULL) {
  133. v = "user";
  134. }
  135. }
  136. name = xs_dup(v);
  137. /* get the avatar */
  138. if ((v = xs_dict_get(actor, "icon")) != NULL &&
  139. (v = xs_dict_get(v, "url")) != NULL) {
  140. avatar = xs_dup(v);
  141. }
  142. if (avatar == NULL)
  143. avatar = xs_fmt("data:image/png;base64, %s", susie);
  144. {
  145. xs *s1 = xs_fmt("<p><img class=\"snac-avatar\" src=\"%s\" alt=\"\"/>\n", avatar);
  146. s = xs_str_cat(s, s1);
  147. }
  148. {
  149. xs *s1 = xs_fmt("<a href=\"%s\" class=\"p-author h-card snac-author\">%s</a>",
  150. actor_id, name);
  151. s = xs_str_cat(s, s1);
  152. }
  153. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0) {
  154. xs *s1 = xs_fmt(" <a href=\"%s\">»</a>", xs_dict_get(msg, "id"));
  155. s = xs_str_cat(s, s1);
  156. }
  157. if (!is_msg_public(snac, msg))
  158. s = xs_str_cat(s, " <span title=\"private\">&#128274;</span>");
  159. if ((v = xs_dict_get(msg, "published")) == NULL)
  160. v = "&nbsp;";
  161. {
  162. xs *s1 = xs_fmt("<br>\n<time class=\"dt-published snac-pubdate\">%s</time>\n", v);
  163. s = xs_str_cat(s, s1);
  164. }
  165. }
  166. return xs_str_cat(os, s);
  167. }
  168. d_char *html_user_header(snac *snac, d_char *s, int local)
  169. /* creates the HTML header */
  170. {
  171. char *p, *v;
  172. s = xs_str_cat(s, "<!DOCTYPE html>\n<html>\n<head>\n");
  173. s = xs_str_cat(s, "<meta name=\"viewport\" "
  174. "content=\"width=device-width, initial-scale=1\"/>\n");
  175. s = xs_str_cat(s, "<meta name=\"generator\" "
  176. "content=\"" USER_AGENT "\"/>\n");
  177. /* add server CSS */
  178. p = xs_dict_get(srv_config, "cssurls");
  179. while (xs_list_iter(&p, &v)) {
  180. xs *s1 = xs_fmt("<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\"/>\n", v);
  181. s = xs_str_cat(s, s1);
  182. }
  183. /* add the user CSS */
  184. {
  185. xs *css = NULL;
  186. int size;
  187. if (valid_status(static_get(snac, "style.css", &css, &size))) {
  188. xs *s1 = xs_fmt("<style>%s</style>\n", css);
  189. s = xs_str_cat(s, s1);
  190. }
  191. }
  192. {
  193. xs *s1 = xs_fmt("<title>%s</title>\n", xs_dict_get(snac->config, "name"));
  194. s = xs_str_cat(s, s1);
  195. }
  196. s = xs_str_cat(s, "</head>\n<body>\n");
  197. /* top nav */
  198. s = xs_str_cat(s, "<nav class=\"snac-top-nav\">");
  199. {
  200. xs *s1;
  201. if (local)
  202. s1 = xs_fmt("<a href=\"%s/admin\">%s</a></nav>\n", snac->actor, L("admin"));
  203. else
  204. s1 = xs_fmt("<a href=\"%s\">%s</a></nav>\n", snac->actor, L("public"));
  205. s = xs_str_cat(s, s1);
  206. }
  207. /* user info */
  208. {
  209. xs *bio = NULL;
  210. char *_tmpl =
  211. "<div class=\"h-card snac-top-user\">\n"
  212. "<p class=\"p-name snac-top-user-name\">%s</p>\n"
  213. "<p class=\"snac-top-user-id\">@%s@%s</p>\n"
  214. "<div class=\"p-note snac-top-user-bio\">%s</div>\n"
  215. "</div>\n";
  216. not_really_markdown(xs_dict_get(snac->config, "bio"), &bio);
  217. xs *s1 = xs_fmt(_tmpl,
  218. xs_dict_get(snac->config, "name"),
  219. xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"),
  220. bio
  221. );
  222. s = xs_str_cat(s, s1);
  223. }
  224. return s;
  225. }
  226. d_char *html_top_controls(snac *snac, d_char *s)
  227. /* generates the top controls */
  228. {
  229. char *_tmpl =
  230. "<div class=\"snac-top-controls\">\n"
  231. "<div class=\"snac-note\">\n"
  232. "<form method=\"post\" action=\"%s/admin/note\">\n"
  233. "<textarea class=\"snac-textarea\" name=\"content\" "
  234. "rows=\"8\" wrap=\"virtual\" required=\"required\"></textarea>\n"
  235. "<input type=\"hidden\" name=\"in_reply_to\" value=\"\">\n"
  236. "<input type=\"submit\" class=\"button\" value=\"%s\">\n"
  237. "</form><p>\n"
  238. "</div>\n"
  239. "<div class=\"snac-top-controls-more\">\n"
  240. "<details><summary>%s</summary>\n"
  241. "<form method=\"post\" action=\"%s/admin/action\">\n"
  242. "<input type=\"text\" name=\"actor\" required=\"required\">\n"
  243. "<input type=\"submit\" name=\"action\" value=\"%s\"> %s\n"
  244. "</form></p>\n"
  245. "<form method=\"post\" action=\"%s\">\n"
  246. "<input type=\"text\" name=\"id\" required=\"required\">\n"
  247. "<input type=\"submit\" name=\"action\" value=\"%s\"> %s\n"
  248. "</form></p>\n"
  249. "<details><summary>%s</summary>\n"
  250. "<div class=\"snac-user-setup\">\n"
  251. "<form method=\"post\" action=\"%s/admin/user-setup\">\n"
  252. "<p>%s:<br>\n"
  253. "<input type=\"text\" name=\"name\" value=\"%s\"></p>\n"
  254. "<p>%s:<br>\n"
  255. "<input type=\"text\" name=\"avatar\" value=\"%s\"></p>\n"
  256. "<p>%s:<br>\n"
  257. "<textarea name=\"bio\" cols=60 rows=4>%s</textarea></p>\n"
  258. "<p>%s:<br>\n"
  259. "<input type=\"password\" name=\"passwd1\" value=\"\"></p>\n"
  260. "<p>%s:<br>\n"
  261. "<input type=\"password\" name=\"passwd2\" value=\"\"></p>\n"
  262. "<input type=\"submit\" class=\"button\" value=\"%s\">\n"
  263. "</form>\n"
  264. "</div>\n"
  265. "</details>\n"
  266. "</details>\n"
  267. "</div>\n"
  268. "</div>\n";
  269. xs *s1 = xs_fmt(_tmpl,
  270. snac->actor,
  271. L("Post"),
  272. L("More options..."),
  273. snac->actor,
  274. L("Follow"), L("(by URL or user@host)"),
  275. snac->actor,
  276. L("Boost"), L("(by URL)"),
  277. L("User setup..."),
  278. snac->actor,
  279. L("User name"),
  280. xs_dict_get(snac->config, "name"),
  281. L("Avatar URL"),
  282. xs_dict_get(snac->config, "avatar"),
  283. L("Bio"),
  284. xs_dict_get(snac->config, "bio"),
  285. L("Password (only to change it)"),
  286. L("Repeat Password"),
  287. L("Update user info")
  288. );
  289. s = xs_str_cat(s, s1);
  290. return s;
  291. }
  292. d_char *html_entry_controls(snac *snac, d_char *os, char *msg)
  293. {
  294. char *id = xs_dict_get(msg, "id");
  295. char *actor = xs_dict_get(msg, "attributedTo");
  296. char *meta = xs_dict_get(msg, "_snac");
  297. xs *s = xs_str_new(NULL);
  298. xs *md5 = xs_md5_hex(id, strlen(id));
  299. s = xs_str_cat(s, "<div class=\"snac-controls\">\n");
  300. {
  301. xs *s1 = xs_fmt(
  302. "<form method=\"post\" action=\"%s/admin/action\">\n"
  303. "<input type=\"hidden\" name=\"id\" value=\"%s\">\n"
  304. "<input type=\"hidden\" name=\"actor\" value=\"%s\">\n"
  305. "<input type=\"button\" name=\"action\" "
  306. "value=\"%s\" onclick=\""
  307. "x = document.getElementById('%s_reply'); "
  308. "if (x.style.display == 'block') "
  309. " x.style.display = 'none'; else "
  310. " x.style.display = 'block';"
  311. "\">\n",
  312. snac->actor, id, actor,
  313. L("Reply"),
  314. md5
  315. );
  316. s = xs_str_cat(s, s1);
  317. }
  318. if (strcmp(actor, snac->actor) != 0) {
  319. /* controls for other actors than this one */
  320. char *l;
  321. l = xs_dict_get(meta, "liked_by");
  322. if (xs_list_in(l, snac->actor) == -1) {
  323. /* not already liked; add button */
  324. xs *s1 = xs_fmt(
  325. "<input type=\"submit\" name=\"action\" "
  326. "class=\"snac-btn-like\" value=\"%s\">\n",
  327. L("Like")
  328. );
  329. s = xs_str_cat(s, s1);
  330. }
  331. l = xs_dict_get(meta, "announced_by");
  332. if (xs_list_in(l, snac->actor) == -1) {
  333. /* not already boosted; add button */
  334. xs *s1 = xs_fmt(
  335. "<input type=\"submit\" name=\"action\" "
  336. "class=\"snac-btn-boost\" value=\"%s\">\n",
  337. L("Boost")
  338. );
  339. s = xs_str_cat(s, s1);
  340. }
  341. if (following_check(snac, actor)) {
  342. xs *s1 = xs_fmt(
  343. "<input type=\"submit\" name=\"action\" "
  344. "class=\"snac-btn-unfollow\" value=\"%s\">\n",
  345. L("Unfollow")
  346. );
  347. s = xs_str_cat(s, s1);
  348. }
  349. else {
  350. xs *s1 = xs_fmt(
  351. "<input type=\"submit\" name=\"action\" "
  352. "class=\"snac-btn-follow\" value=\"%s\">\n"
  353. "<input type=\"submit\" name=\"action\" "
  354. "class=\"snac-btn-mute\" value=\"%s\">\n",
  355. L("Follow"),
  356. L("MUTE")
  357. );
  358. s = xs_str_cat(s, s1);
  359. }
  360. }
  361. {
  362. xs *s1 = xs_fmt(
  363. "<input type=\"submit\" name=\"action\" "
  364. "class=\"snac-btn-delete\" value=\"%s\">\n",
  365. L("Delete")
  366. );
  367. s = xs_str_cat(s, s1);
  368. }
  369. s = xs_str_cat(s, "</form>\n");
  370. {
  371. xs *ct = xs_str_new("");
  372. xs *s1 = xs_fmt(
  373. "<p><div class=\"snac-note\" style=\"display: none\" id=\"%s_reply\">\n"
  374. "<form method=\"post\" action=\"%s/admin/note\" id=\"%s_reply_form\">\n"
  375. "<textarea class=\"snac-textarea\" name=\"content\" "
  376. "rows=\"4\" wrap=\"virtual\" required=\"required\">%s</textarea>\n"
  377. "<input type=\"hidden\" name=\"in_reply_to\" value=\"%s\">\n"
  378. "<input type=\"submit\" class=\"button\" value=\"%s\">\n"
  379. "</form><p></div>\n",
  380. md5,
  381. snac->actor, md5,
  382. ct,
  383. id,
  384. L("Post")
  385. );
  386. s = xs_str_cat(s, s1);
  387. }
  388. s = xs_str_cat(s, "</div>\n");
  389. return xs_str_cat(os, s);
  390. }
  391. d_char *html_entry(snac *snac, d_char *os, char *msg, xs_set *seen, int local, int level)
  392. {
  393. char *id = xs_dict_get(msg, "id");
  394. char *type = xs_dict_get(msg, "type");
  395. char *meta = xs_dict_get(msg, "_snac");
  396. xs *actor_o = NULL;
  397. char *actor;
  398. /* return if already seen */
  399. if (xs_set_add(seen, id) == 0)
  400. return os;
  401. if (strcmp(type, "Follow") == 0)
  402. return os;
  403. /* bring the main actor */
  404. if ((actor = xs_dict_get(msg, "attributedTo")) == NULL)
  405. return os;
  406. if (!valid_status(actor_get(snac, actor, &actor_o)))
  407. return os;
  408. xs *s = xs_str_new(NULL);
  409. /* if this is our post, add the score */
  410. if (xs_startswith(id, snac->actor)) {
  411. int likes = xs_list_len(xs_dict_get(meta, "liked_by"));
  412. int boosts = xs_list_len(xs_dict_get(meta, "announced_by"));
  413. xs *s1 = xs_fmt(
  414. "<div class=\"snac-score\">%d &#9733; %d &#8634;</div>\n",
  415. likes, boosts);
  416. s = xs_str_cat(s, s1);
  417. }
  418. if (level == 0) {
  419. char *referrer;
  420. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  421. /* print the origin of the post, if any */
  422. if ((referrer = xs_dict_get(meta, "referrer")) != NULL) {
  423. xs *actor_r = NULL;
  424. if (valid_status(actor_get(snac, referrer, &actor_r))) {
  425. char *name;
  426. if ((name = xs_dict_get(actor_r, "name")) == NULL)
  427. name = xs_dict_get(actor_r, "preferredUsername");
  428. xs *s1 = xs_fmt(
  429. "<div class=\"snac-origin\">"
  430. "<a href=\"%s\">%s</a> %s</div>\n",
  431. xs_dict_get(actor_r, "id"),
  432. name,
  433. L("boosted")
  434. );
  435. s = xs_str_cat(s, s1);
  436. }
  437. }
  438. }
  439. else
  440. s = xs_str_cat(s, "<div class=\"snac-child\">\n");
  441. s = html_msg_icon(snac, s, msg);
  442. /* add the content */
  443. s = xs_str_cat(s, "<div class=\"e-content snac-content\">\n");
  444. {
  445. xs *c = xs_dup(xs_dict_get(msg, "content"));
  446. /* do some tweaks to the content */
  447. c = xs_replace_i(c, "\r", "");
  448. while (xs_endswith(c, "<br><br>"))
  449. c = xs_crop(c, 0, -4);
  450. c = xs_replace_i(c, "<br><br>", "<p>");
  451. if (!xs_startswith(c, "<p>")) {
  452. xs *s1 = c;
  453. c = xs_fmt("<p>%s</p>", s1);
  454. }
  455. s = xs_str_cat(s, c);
  456. }
  457. s = xs_str_cat(s, "\n");
  458. /* add the attachments */
  459. char *attach;
  460. if ((attach = xs_dict_get(msg, "attachment")) != NULL) {
  461. char *v;
  462. while (xs_list_iter(&attach, &v)) {
  463. char *t = xs_dict_get(v, "mediaType");
  464. if (t && xs_startswith(t, "image/")) {
  465. char *url = xs_dict_get(v, "url");
  466. char *name = xs_dict_get(v, "name");
  467. if (url != NULL) {
  468. xs *s1 = xs_fmt("<p><img src=\"%s\" alt=\"%s\"/></p>\n",
  469. url, xs_is_null(name) ? "" : name);
  470. s = xs_str_cat(s, s1);
  471. }
  472. }
  473. }
  474. }
  475. s = xs_str_cat(s, "</div>\n");
  476. /** controls **/
  477. if (!local)
  478. s = html_entry_controls(snac, s, msg);
  479. /** children **/
  480. char *children = xs_dict_get(meta, "children");
  481. if (xs_list_len(children)) {
  482. int left = xs_list_len(children);
  483. char *id;
  484. s = xs_str_cat(s, "<div class=\"snac-children\">\n");
  485. if (left > 3)
  486. s = xs_str_cat(s, "<details><summary>...</summary>\n");
  487. while (xs_list_iter(&children, &id)) {
  488. xs *chd = timeline_find(snac, id);
  489. if (left == 0)
  490. s = xs_str_cat(s, "</details>\n");
  491. if (chd != NULL)
  492. s = html_entry(snac, s, chd, seen, local, level + 1);
  493. else
  494. snac_debug(snac, 1, xs_fmt("cannot read from timeline child %s", id));
  495. left--;
  496. }
  497. s = xs_str_cat(s, "</div>\n");
  498. }
  499. s = xs_str_cat(s, "</div>\n");
  500. return xs_str_cat(os, s);
  501. }
  502. d_char *html_user_footer(snac *snac, d_char *s)
  503. {
  504. xs *s1 = xs_fmt(
  505. "<div class=\"snac-footer\">\n"
  506. "<a href=\"%s\">%s</a> - "
  507. "powered by <abbr title=\"Social Networks Are Crap\">snac</abbr></div>\n",
  508. srv_baseurl,
  509. L("about this site")
  510. );
  511. return xs_str_cat(s, s1);
  512. }
  513. d_char *html_timeline(snac *snac, char *list, int local)
  514. /* returns the HTML for the timeline */
  515. {
  516. d_char *s = xs_str_new(NULL);
  517. xs_set *seen = xs_set_new(4096);
  518. char *v;
  519. double t = ftime();
  520. s = html_user_header(snac, s, local);
  521. if (!local)
  522. s = html_top_controls(snac, s);
  523. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  524. while (xs_list_iter(&list, &v)) {
  525. xs *msg = timeline_get(snac, v);
  526. s = html_entry(snac, s, msg, seen, local, 0);
  527. }
  528. s = xs_str_cat(s, "</div>\n");
  529. s = html_user_footer(snac, s);
  530. {
  531. xs *s1 = xs_fmt("<!-- %lf seconds -->\n", ftime() - t);
  532. s = xs_str_cat(s, s1);
  533. }
  534. s = xs_str_cat(s, "</body>\n</html>\n");
  535. xs_set_free(seen);
  536. return s;
  537. }
  538. int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char **ctype)
  539. {
  540. int status = 404;
  541. snac snac;
  542. char *uid, *p_path;
  543. xs *l = xs_split_n(q_path, "/", 2);
  544. uid = xs_list_get(l, 1);
  545. if (!uid || !user_open(&snac, uid)) {
  546. /* invalid user */
  547. srv_log(xs_fmt("html_get_handler bad user %s", uid));
  548. return 404;
  549. }
  550. p_path = xs_list_get(l, 2);
  551. if (p_path == NULL) {
  552. /* public timeline */
  553. xs *list = local_list(&snac, 0xfffffff);
  554. *body = html_timeline(&snac, list, 1);
  555. *b_size = strlen(*body);
  556. status = 200;
  557. }
  558. else
  559. if (strcmp(p_path, "admin") == 0) {
  560. /* private timeline */
  561. if (!login(&snac, req))
  562. status = 401;
  563. else {
  564. xs *list = timeline_list(&snac, 0xfffffff);
  565. *body = html_timeline(&snac, list, 0);
  566. *b_size = strlen(*body);
  567. status = 200;
  568. }
  569. }
  570. else
  571. if (xs_startswith(p_path, "p/") == 0) {
  572. /* a timeline with just one entry */
  573. }
  574. else
  575. if (xs_startswith(p_path, "s/") == 0) {
  576. /* a static file */
  577. }
  578. else
  579. if (xs_startswith(p_path, "h/") == 0) {
  580. /* an entry from the history */
  581. }
  582. else
  583. status = 404;
  584. user_free(&snac);
  585. if (valid_status(status)) {
  586. *ctype = "text/html; charset=utf-8";
  587. }
  588. return status;
  589. }
  590. int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size,
  591. char **body, int *b_size, char **ctype)
  592. {
  593. int status = 0;
  594. return status;
  595. }