html.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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 "xs_time.h"
  11. #include "xs_mime.h"
  12. #include "snac.h"
  13. int login(snac *snac, char *headers)
  14. /* tries a login */
  15. {
  16. int logged_in = 0;
  17. char *auth = xs_dict_get(headers, "authorization");
  18. if (auth && xs_startswith(auth, "Basic ")) {
  19. int sz;
  20. xs *s1 = xs_crop(xs_dup(auth), 6, 0);
  21. xs *s2 = xs_base64_dec(s1, &sz);
  22. xs *l1 = xs_split_n(s2, ":", 1);
  23. if (xs_list_len(l1) == 2) {
  24. logged_in = check_password(
  25. xs_list_get(l1, 0), xs_list_get(l1, 1),
  26. xs_dict_get(snac->config, "passwd"));
  27. }
  28. }
  29. return logged_in;
  30. }
  31. d_char *html_msg_icon(snac *snac, d_char *os, char *msg)
  32. {
  33. char *actor_id;
  34. xs *actor = NULL;
  35. xs *s = xs_str_new(NULL);
  36. if ((actor_id = xs_dict_get(msg, "attributedTo")) == NULL)
  37. actor_id = xs_dict_get(msg, "actor");
  38. if (actor_id && valid_status(actor_get(snac, actor_id, &actor))) {
  39. xs *name = NULL;
  40. xs *avatar = NULL;
  41. char *p, *v;
  42. /* get the name */
  43. if (xs_is_null((v = xs_dict_get(actor, "name"))) || *v == '\0') {
  44. if (xs_is_null(v = xs_dict_get(actor, "preferredUsername"))) {
  45. v = "user";
  46. }
  47. }
  48. name = xs_dup(v);
  49. /* replace the :shortnames: */
  50. if (!xs_is_null(p = xs_dict_get(actor, "tag"))) {
  51. /* iterate the tags */
  52. while (xs_list_iter(&p, &v)) {
  53. char *t = xs_dict_get(v, "type");
  54. if (t && strcmp(t, "Emoji") == 0) {
  55. char *n = xs_dict_get(v, "name");
  56. char *i = xs_dict_get(v, "icon");
  57. if (n && i) {
  58. char *u = xs_dict_get(i, "url");
  59. xs *img = xs_fmt("<img src=\"%s\" style=\"height: 1em\"/>", u);
  60. name = xs_replace_i(name, n, img);
  61. }
  62. }
  63. }
  64. }
  65. /* get the avatar */
  66. if ((v = xs_dict_get(actor, "icon")) != NULL &&
  67. (v = xs_dict_get(v, "url")) != NULL) {
  68. avatar = xs_dup(v);
  69. }
  70. if (avatar == NULL)
  71. avatar = xs_fmt("data:image/png;base64, %s", susie);
  72. {
  73. xs *s1 = xs_fmt("<p><img class=\"snac-avatar\" src=\"%s\" alt=\"\"/>\n", avatar);
  74. s = xs_str_cat(s, s1);
  75. }
  76. {
  77. xs *s1 = xs_fmt("<a href=\"%s\" class=\"p-author h-card snac-author\">%s</a>",
  78. actor_id, name);
  79. s = xs_str_cat(s, s1);
  80. }
  81. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0) {
  82. xs *s1 = xs_fmt(" <a href=\"%s\">»</a>", xs_dict_get(msg, "id"));
  83. s = xs_str_cat(s, s1);
  84. }
  85. if (!is_msg_public(snac, msg))
  86. s = xs_str_cat(s, " <span title=\"private\">&#128274;</span>");
  87. if ((v = xs_dict_get(msg, "published")) == NULL) {
  88. s = xs_str_cat(s, "<br>\n<time>&nbsp;</time>\n");
  89. }
  90. else {
  91. xs *sd = xs_crop(xs_dup(v), 0, 10);
  92. xs *s1 = xs_fmt(
  93. "<br>\n<time class=\"dt-published snac-pubdate\">%s</time>\n", sd);
  94. s = xs_str_cat(s, s1);
  95. }
  96. }
  97. return xs_str_cat(os, s);
  98. }
  99. d_char *html_user_header(snac *snac, d_char *s, int local)
  100. /* creates the HTML header */
  101. {
  102. char *p, *v;
  103. s = xs_str_cat(s, "<!DOCTYPE html>\n<html>\n<head>\n");
  104. s = xs_str_cat(s, "<meta name=\"viewport\" "
  105. "content=\"width=device-width, initial-scale=1\"/>\n");
  106. s = xs_str_cat(s, "<meta name=\"generator\" "
  107. "content=\"" USER_AGENT "\"/>\n");
  108. /* add server CSS */
  109. p = xs_dict_get(srv_config, "cssurls");
  110. while (xs_list_iter(&p, &v)) {
  111. xs *s1 = xs_fmt("<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\"/>\n", v);
  112. s = xs_str_cat(s, s1);
  113. }
  114. /* add the user CSS */
  115. {
  116. xs *css = NULL;
  117. int size;
  118. if (valid_status(static_get(snac, "style.css", &css, &size))) {
  119. xs *s1 = xs_fmt("<style>%s</style>\n", css);
  120. s = xs_str_cat(s, s1);
  121. }
  122. }
  123. {
  124. xs *s1 = xs_fmt("<title>%s</title>\n", xs_dict_get(snac->config, "name"));
  125. s = xs_str_cat(s, s1);
  126. }
  127. s = xs_str_cat(s, "</head>\n<body>\n");
  128. /* top nav */
  129. s = xs_str_cat(s, "<nav class=\"snac-top-nav\">");
  130. {
  131. xs *s1;
  132. if (local)
  133. s1 = xs_fmt("<a href=\"%s/admin\">%s</a></nav>\n", snac->actor, L("admin"));
  134. else
  135. s1 = xs_fmt("<a href=\"%s\">%s</a></nav>\n", snac->actor, L("public"));
  136. s = xs_str_cat(s, s1);
  137. }
  138. /* user info */
  139. {
  140. xs *bio = NULL;
  141. char *_tmpl =
  142. "<div class=\"h-card snac-top-user\">\n"
  143. "<p class=\"p-name snac-top-user-name\">%s</p>\n"
  144. "<p class=\"snac-top-user-id\">@%s@%s</p>\n"
  145. "<div class=\"p-note snac-top-user-bio\">%s</div>\n"
  146. "</div>\n";
  147. not_really_markdown(xs_dict_get(snac->config, "bio"), &bio);
  148. xs *s1 = xs_fmt(_tmpl,
  149. xs_dict_get(snac->config, "name"),
  150. xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"),
  151. bio
  152. );
  153. s = xs_str_cat(s, s1);
  154. }
  155. return s;
  156. }
  157. d_char *html_top_controls(snac *snac, d_char *s)
  158. /* generates the top controls */
  159. {
  160. char *_tmpl =
  161. "<div class=\"snac-top-controls\">\n"
  162. "<div class=\"snac-note\">\n"
  163. "<form method=\"post\" action=\"%s/admin/note\" enctype=\"multipart/form-data\">\n"
  164. "<textarea class=\"snac-textarea\" name=\"content\" "
  165. "rows=\"8\" wrap=\"virtual\" required=\"required\"></textarea>\n"
  166. "<input type=\"hidden\" name=\"in_reply_to\" value=\"\">\n"
  167. "<p><input type=\"file\" name=\"attach\">\n"
  168. "<p><input type=\"submit\" class=\"button\" value=\"%s\">\n"
  169. "</form><p>\n"
  170. "</div>\n"
  171. "<div class=\"snac-top-controls-more\">\n"
  172. "<details><summary>%s</summary>\n"
  173. "<form method=\"post\" action=\"%s/admin/action\">\n"
  174. "<input type=\"text\" name=\"actor\" required=\"required\">\n"
  175. "<input type=\"submit\" name=\"action\" value=\"%s\"> %s\n"
  176. "</form><p>\n"
  177. "<form method=\"post\" action=\"%s/admin/action\">\n"
  178. "<input type=\"text\" name=\"id\" required=\"required\">\n"
  179. "<input type=\"submit\" name=\"action\" value=\"%s\"> %s\n"
  180. "</form><p>\n"
  181. "<details><summary>%s</summary>\n"
  182. "<div class=\"snac-user-setup\">\n"
  183. "<form method=\"post\" action=\"%s/admin/user-setup\">\n"
  184. "<p>%s:<br>\n"
  185. "<input type=\"text\" name=\"name\" value=\"%s\"></p>\n"
  186. "<p>%s:<br>\n"
  187. "<input type=\"text\" name=\"avatar\" value=\"%s\"></p>\n"
  188. "<p>%s:<br>\n"
  189. "<textarea name=\"bio\" cols=\"40\" rows=\"4\">%s</textarea></p>\n"
  190. "<p>%s:<br>\n"
  191. "<input type=\"text\" name=\"email\" value=\"%s\"></p>\n"
  192. "<p>%s:<br>\n"
  193. "<input type=\"password\" name=\"passwd1\" value=\"\"></p>\n"
  194. "<p>%s:<br>\n"
  195. "<input type=\"password\" name=\"passwd2\" value=\"\"></p>\n"
  196. "<input type=\"submit\" class=\"button\" value=\"%s\">\n"
  197. "</form>\n"
  198. "</div>\n"
  199. "</details>\n"
  200. "</details>\n"
  201. "</div>\n"
  202. "</div>\n";
  203. char *email = xs_dict_get(snac->config, "email");
  204. if (xs_is_null(email))
  205. email = "";
  206. xs *s1 = xs_fmt(_tmpl,
  207. snac->actor,
  208. L("Post"),
  209. L("More options..."),
  210. snac->actor,
  211. L("Follow"), L("(by URL or user@host)"),
  212. snac->actor,
  213. L("Boost"), L("(by URL)"),
  214. L("User setup..."),
  215. snac->actor,
  216. L("User name"),
  217. xs_dict_get(snac->config, "name"),
  218. L("Avatar URL"),
  219. xs_dict_get(snac->config, "avatar"),
  220. L("Bio"),
  221. xs_dict_get(snac->config, "bio"),
  222. L("Email address for notifications"),
  223. email,
  224. L("Password (only to change it)"),
  225. L("Repeat Password"),
  226. L("Update user info")
  227. );
  228. s = xs_str_cat(s, s1);
  229. return s;
  230. }
  231. d_char *html_button(d_char *s, char *clss, char *label)
  232. {
  233. xs *s1 = xs_fmt(
  234. "<input type=\"submit\" name=\"action\" "
  235. "class=\"snac-btn-%s\" value=\"%s\">\n",
  236. clss, label);
  237. return xs_str_cat(s, s1);
  238. }
  239. d_char *build_mentions(snac *snac, char *msg)
  240. /* returns a string with the mentions in msg */
  241. {
  242. d_char *s = xs_str_new(NULL);
  243. char *list = xs_dict_get(msg, "tag");
  244. char *v;
  245. while (xs_list_iter(&list, &v)) {
  246. char *type = xs_dict_get(v, "type");
  247. char *href = xs_dict_get(v, "href");
  248. char *name = xs_dict_get(v, "name");
  249. if (type && strcmp(type, "Mention") == 0 &&
  250. href && strcmp(href, snac->actor) != 0 && name) {
  251. xs *l = xs_split(name, "@");
  252. /* is it a name without a host? */
  253. if (xs_list_len(l) < 3) {
  254. /* split the href and pick the host name LIKE AN ANIMAL */
  255. /* would be better to query the webfinger but *won't do that* here */
  256. xs *l2 = xs_split(href, "/");
  257. if (xs_list_len(l2) >= 3) {
  258. xs *s1 = xs_fmt("%s@%s ", name, xs_list_get(l2, 2));
  259. s = xs_str_cat(s, s1);
  260. }
  261. }
  262. else {
  263. s = xs_str_cat(s, name);
  264. s = xs_str_cat(s, " ");
  265. }
  266. }
  267. }
  268. return s;
  269. }
  270. d_char *html_entry_controls(snac *snac, d_char *os, char *msg, int num)
  271. {
  272. char *id = xs_dict_get(msg, "id");
  273. char *actor = xs_dict_get(msg, "attributedTo");
  274. char *meta = xs_dict_get(msg, "_snac");
  275. xs *s = xs_str_new(NULL);
  276. xs *md5 = xs_md5_hex(id, strlen(id));
  277. s = xs_str_cat(s, "<div class=\"snac-controls\">\n");
  278. {
  279. xs *s1 = xs_fmt(
  280. "<form method=\"post\" action=\"%s/admin/action\">\n"
  281. "<input type=\"hidden\" name=\"id\" value=\"%s\">\n"
  282. "<input type=\"hidden\" name=\"actor\" value=\"%s\">\n"
  283. "<input type=\"hidden\" name=\"redir\" value=\"%d_entry\">\n"
  284. "<input type=\"button\" name=\"action\" "
  285. "value=\"%s\" onclick=\""
  286. "x = document.getElementById('%s_reply'); "
  287. "if (x.style.display == 'block') "
  288. " x.style.display = 'none'; else "
  289. " x.style.display = 'block';"
  290. "\">\n",
  291. snac->actor, id, actor, num,
  292. L("Reply"),
  293. md5
  294. );
  295. s = xs_str_cat(s, s1);
  296. }
  297. if (strcmp(actor, snac->actor) != 0) {
  298. /* controls for other actors than this one */
  299. char *l;
  300. l = xs_dict_get(meta, "liked_by");
  301. if (xs_list_in(l, snac->actor) == -1) {
  302. /* not already liked; add button */
  303. s = html_button(s, "like", L("Like"));
  304. }
  305. l = xs_dict_get(meta, "announced_by");
  306. if (xs_list_in(l, snac->actor) == -1) {
  307. /* not already boosted; add button */
  308. s = html_button(s, "boost", L("Boost"));
  309. }
  310. if (following_check(snac, actor)) {
  311. s = html_button(s, "unfollow", L("Unfollow"));
  312. }
  313. else {
  314. s = html_button(s, "follow", L("Follow"));
  315. }
  316. s = html_button(s, "mute", L("MUTE"));
  317. }
  318. s = html_button(s, "delete", L("Delete"));
  319. s = xs_str_cat(s, "</form>\n");
  320. {
  321. /* the post textarea */
  322. xs *ct = build_mentions(snac, msg);
  323. xs *s1 = xs_fmt(
  324. "<p><div class=\"snac-note\" style=\"display: none\" id=\"%s_reply\">\n"
  325. "<form method=\"post\" action=\"%s/admin/note\" "
  326. "enctype=\"multipart/form-data\" id=\"%s_reply_form\">\n"
  327. "<textarea class=\"snac-textarea\" name=\"content\" "
  328. "rows=\"4\" wrap=\"virtual\" required=\"required\">%s</textarea>\n"
  329. "<input type=\"hidden\" name=\"in_reply_to\" value=\"%s\">\n"
  330. "<p><input type=\"file\" name=\"attach\">\n"
  331. "<p><input type=\"submit\" class=\"button\" value=\"%s\">\n"
  332. "</form><p></div>\n",
  333. md5,
  334. snac->actor, md5,
  335. ct,
  336. id,
  337. L("Post")
  338. );
  339. s = xs_str_cat(s, s1);
  340. }
  341. s = xs_str_cat(s, "</div>\n");
  342. return xs_str_cat(os, s);
  343. }
  344. d_char *html_entry(snac *snac, d_char *os, char *msg, xs_set *seen, int local, int level)
  345. {
  346. char *id = xs_dict_get(msg, "id");
  347. char *type = xs_dict_get(msg, "type");
  348. char *meta = xs_dict_get(msg, "_snac");
  349. char *actor;
  350. int sensitive = 0;
  351. char *v;
  352. int num = xs_list_len(seen->list);
  353. /* do not show non-public messages in the public timeline */
  354. if (local && !is_msg_public(snac, msg))
  355. return os;
  356. /* return if already seen */
  357. if (xs_set_add(seen, id) == 0)
  358. return os;
  359. xs *s = xs_str_new(NULL);
  360. {
  361. xs *s1 = xs_fmt("<a name=\"%d_entry\"></a>\n", num);
  362. s = xs_str_cat(s, s1);
  363. }
  364. if (strcmp(type, "Follow") == 0) {
  365. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  366. xs *s1 = xs_fmt("<div class=\"snac-origin\">%s</div>\n", L("follows you"));
  367. s = xs_str_cat(s, s1);
  368. s = html_msg_icon(snac, s, msg);
  369. s = xs_str_cat(s, "</div>\n");
  370. return xs_str_cat(os, s);
  371. }
  372. /* bring the main actor */
  373. if ((actor = xs_dict_get(msg, "attributedTo")) == NULL)
  374. return os;
  375. /* ignore muted morons immediately */
  376. if (is_muted(snac, actor))
  377. return os;
  378. if (strcmp(actor, snac->actor) != 0 && !valid_status(actor_get(snac, actor, NULL)))
  379. return os;
  380. /* if this is our post, add the score */
  381. if (xs_startswith(id, snac->actor)) {
  382. int likes = xs_list_len(xs_dict_get(meta, "liked_by"));
  383. int boosts = xs_list_len(xs_dict_get(meta, "announced_by"));
  384. /* alternate emojis: %d &#128077; %d &#128257; */
  385. xs *s1 = xs_fmt(
  386. "<div class=\"snac-score\">%d &#9733; %d &#8634;</div>\n",
  387. likes, boosts);
  388. s = xs_str_cat(s, s1);
  389. }
  390. if (level == 0) {
  391. char *p;
  392. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  393. /* print the origin of the post, if any */
  394. if (!xs_is_null(p = xs_dict_get(meta, "referrer"))) {
  395. xs *actor_r = NULL;
  396. if (valid_status(actor_get(snac, p, &actor_r))) {
  397. char *name;
  398. if ((name = xs_dict_get(actor_r, "name")) == NULL)
  399. name = xs_dict_get(actor_r, "preferredUsername");
  400. xs *s1 = xs_fmt(
  401. "<div class=\"snac-origin\">"
  402. "<a href=\"%s\">%s</a> %s</div>\n",
  403. xs_dict_get(actor_r, "id"),
  404. name,
  405. L("boosted")
  406. );
  407. s = xs_str_cat(s, s1);
  408. }
  409. }
  410. else
  411. if (!xs_is_null((p = xs_dict_get(meta, "parent"))) && *p) {
  412. /* this may happen if any of the autor or the parent aren't here */
  413. xs *s1 = xs_fmt(
  414. "<div class=\"snac-origin\">%s "
  415. "<a href=\"%s\">»</a></div>\n",
  416. L("in reply to"), p
  417. );
  418. s = xs_str_cat(s, s1);
  419. }
  420. else
  421. if (!xs_is_null((p = xs_dict_get(meta, "announced_by"))) &&
  422. xs_list_in(p, snac->actor) != -1) {
  423. /* we boosted this */
  424. xs *s1 = xs_fmt(
  425. "<div class=\"snac-origin\">"
  426. "<a href=\"%s\">%s</a> %s</a></div>",
  427. snac->actor, xs_dict_get(snac->config, "name"), L("boosted")
  428. );
  429. s = xs_str_cat(s, s1);
  430. }
  431. else
  432. if (!xs_is_null((p = xs_dict_get(meta, "liked_by"))) &&
  433. xs_list_in(p, snac->actor) != -1) {
  434. /* we liked this */
  435. xs *s1 = xs_fmt(
  436. "<div class=\"snac-origin\">"
  437. "<a href=\"%s\">%s</a> %s</a></div>",
  438. snac->actor, xs_dict_get(snac->config, "name"), L("liked")
  439. );
  440. s = xs_str_cat(s, s1);
  441. }
  442. }
  443. else
  444. s = xs_str_cat(s, "<div class=\"snac-child\">\n");
  445. s = html_msg_icon(snac, s, msg);
  446. /* add the content */
  447. s = xs_str_cat(s, "<div class=\"e-content snac-content\">\n");
  448. /* is it sensitive? */
  449. if (!xs_is_null(v = xs_dict_get(msg, "sensitive")) && xs_type(v) == XSTYPE_TRUE) {
  450. if (xs_is_null(v = xs_dict_get(msg, "summary")) || *v == '\0')
  451. v = "...";
  452. xs *s1 = xs_fmt("<details><summary>%s [%s]</summary>\n", v, L("SENSITIVE CONTENT"));
  453. s = xs_str_cat(s, s1);
  454. sensitive = 1;
  455. }
  456. {
  457. xs *c = xs_dup(xs_dict_get(msg, "content"));
  458. char *p, *v;
  459. /* do some tweaks to the content */
  460. c = xs_replace_i(c, "\r", "");
  461. while (xs_endswith(c, "<br><br>"))
  462. c = xs_crop(c, 0, -4);
  463. c = xs_replace_i(c, "<br><br>", "<p>");
  464. if (!xs_startswith(c, "<p>")) {
  465. xs *s1 = c;
  466. c = xs_fmt("<p>%s</p>", s1);
  467. }
  468. /* replace the :shortnames: */
  469. if (!xs_is_null(p = xs_dict_get(msg, "tag"))) {
  470. /* iterate the tags */
  471. while (xs_list_iter(&p, &v)) {
  472. char *t = xs_dict_get(v, "type");
  473. if (t && strcmp(t, "Emoji") == 0) {
  474. char *n = xs_dict_get(v, "name");
  475. char *i = xs_dict_get(v, "icon");
  476. if (n && i) {
  477. char *u = xs_dict_get(i, "url");
  478. xs *img = xs_fmt("<img src=\"%s\" style=\"height: 1em\"/>", u);
  479. c = xs_replace_i(c, n, img);
  480. }
  481. }
  482. }
  483. }
  484. xs *sc = sanitize(c);
  485. s = xs_str_cat(s, sc);
  486. }
  487. s = xs_str_cat(s, "\n");
  488. /* add the attachments */
  489. char *attach;
  490. if ((attach = xs_dict_get(msg, "attachment")) != NULL) {
  491. char *v;
  492. while (xs_list_iter(&attach, &v)) {
  493. char *t = xs_dict_get(v, "mediaType");
  494. if (xs_is_null(t))
  495. continue;
  496. if (xs_startswith(t, "image/")) {
  497. char *url = xs_dict_get(v, "url");
  498. char *name = xs_dict_get(v, "name");
  499. if (url != NULL) {
  500. xs *s1 = xs_fmt("<p><img src=\"%s\" alt=\"%s\"/></p>\n",
  501. url, xs_is_null(name) ? "" : name);
  502. s = xs_str_cat(s, s1);
  503. }
  504. }
  505. else
  506. if (xs_startswith(t, "video/")) {
  507. char *url = xs_dict_get(v, "url");
  508. if (url != NULL) {
  509. xs *s1 = xs_fmt("<p><object data=\"%s\"></object></p>\n", url);
  510. s = xs_str_cat(s, s1);
  511. }
  512. }
  513. }
  514. }
  515. if (sensitive)
  516. s = xs_str_cat(s, "</details><p>\n");
  517. s = xs_str_cat(s, "</div>\n");
  518. /** controls **/
  519. if (!local)
  520. s = html_entry_controls(snac, s, msg, num + 1);
  521. /** children **/
  522. char *children = xs_dict_get(meta, "children");
  523. int left = xs_list_len(children);
  524. if (left) {
  525. char *id;
  526. if (level < 4)
  527. s = xs_str_cat(s, "<div class=\"snac-children\">\n");
  528. else
  529. s = xs_str_cat(s, "<div>\n");
  530. if (left > 3)
  531. s = xs_str_cat(s, "<details><summary>...</summary>\n");
  532. while (xs_list_iter(&children, &id)) {
  533. xs *chd = timeline_find(snac, id);
  534. if (left == 3)
  535. s = xs_str_cat(s, "</details>\n");
  536. if (chd != NULL)
  537. s = html_entry(snac, s, chd, seen, local, level + 1);
  538. else
  539. snac_debug(snac, 2, xs_fmt("cannot read from timeline child %s", id));
  540. left--;
  541. }
  542. s = xs_str_cat(s, "</div>\n");
  543. }
  544. s = xs_str_cat(s, "</div>\n");
  545. return xs_str_cat(os, s);
  546. }
  547. d_char *html_user_footer(snac *snac, d_char *s)
  548. {
  549. xs *s1 = xs_fmt(
  550. "<div class=\"snac-footer\">\n"
  551. "<a href=\"%s\">%s</a> - "
  552. "powered by <abbr title=\"Social Networks Are Crap\">snac</abbr></div>\n",
  553. srv_baseurl,
  554. L("about this site")
  555. );
  556. return xs_str_cat(s, s1);
  557. }
  558. d_char *html_timeline(snac *snac, char *list, int local)
  559. /* returns the HTML for the timeline */
  560. {
  561. d_char *s = xs_str_new(NULL);
  562. xs_set *seen = xs_set_new(4096);
  563. char *v;
  564. double t = ftime();
  565. s = html_user_header(snac, s, local);
  566. if (!local)
  567. s = html_top_controls(snac, s);
  568. s = xs_str_cat(s, "<a name=\"snac-posts\"></a>\n");
  569. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  570. while (xs_list_iter(&list, &v)) {
  571. xs *msg = timeline_get(snac, v);
  572. s = html_entry(snac, s, msg, seen, local, 0);
  573. }
  574. s = xs_str_cat(s, "</div>\n");
  575. if (local) {
  576. xs *s1 = xs_fmt(
  577. "<div class=\"snac-history\">\n"
  578. "<p class=\"snac-history-title\">%s</p><ul>\n",
  579. L("History")
  580. );
  581. s = xs_str_cat(s, s1);
  582. xs *list = history_list(snac);
  583. char *p, *v;
  584. p = list;
  585. while (xs_list_iter(&p, &v)) {
  586. xs *fn = xs_replace(v, ".html", "");
  587. xs *s1 = xs_fmt(
  588. "<li><a href=\"%s/h/%s\">%s</li>\n",
  589. snac->actor, v, fn);
  590. s = xs_str_cat(s, s1);
  591. }
  592. s = xs_str_cat(s, "</ul></div>\n");
  593. }
  594. s = html_user_footer(snac, s);
  595. {
  596. xs *s1 = xs_fmt("<!-- %lf seconds -->\n", ftime() - t);
  597. s = xs_str_cat(s, s1);
  598. }
  599. s = xs_str_cat(s, "</body>\n</html>\n");
  600. xs_set_free(seen);
  601. return s;
  602. }
  603. int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char **ctype)
  604. {
  605. int status = 404;
  606. snac snac;
  607. char *uid, *p_path;
  608. int cache = 1;
  609. char *v;
  610. xs *l = xs_split_n(q_path, "/", 2);
  611. uid = xs_list_get(l, 1);
  612. if (!uid || !user_open(&snac, uid)) {
  613. /* invalid user */
  614. srv_log(xs_fmt("html_get_handler bad user %s", uid));
  615. return 404;
  616. }
  617. /* check if server config variable 'disable_cache' is set */
  618. if ((v = xs_dict_get(srv_config, "disable_cache")) && xs_type(v) == XSTYPE_TRUE)
  619. cache = 0;
  620. p_path = xs_list_get(l, 2);
  621. if (p_path == NULL) {
  622. /* public timeline */
  623. xs *h = xs_str_localtime(0, "%Y-%m.html");
  624. if (cache && history_mtime(&snac, h) > timeline_mtime(&snac)) {
  625. snac_debug(&snac, 1, xs_fmt("serving cached local timeline"));
  626. *body = history_get(&snac, h);
  627. *b_size = strlen(*body);
  628. status = 200;
  629. }
  630. else {
  631. xs *list = local_list(&snac, 0xfffffff);
  632. *body = html_timeline(&snac, list, 1);
  633. *b_size = strlen(*body);
  634. status = 200;
  635. history_add(&snac, h, *body, *b_size);
  636. }
  637. }
  638. else
  639. if (strcmp(p_path, "admin") == 0) {
  640. /* private timeline */
  641. if (!login(&snac, req))
  642. status = 401;
  643. else {
  644. if (cache && history_mtime(&snac, "timeline.html_") > timeline_mtime(&snac)) {
  645. snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
  646. *body = history_get(&snac, "timeline.html_");
  647. *b_size = strlen(*body);
  648. status = 200;
  649. }
  650. else {
  651. snac_debug(&snac, 1, xs_fmt("building timeline"));
  652. xs *list = timeline_list(&snac, 0xfffffff);
  653. *body = html_timeline(&snac, list, 0);
  654. *b_size = strlen(*body);
  655. status = 200;
  656. history_add(&snac, "timeline.html_", *body, *b_size);
  657. }
  658. }
  659. }
  660. else
  661. if (xs_startswith(p_path, "p/")) {
  662. /* a timeline with just one entry */
  663. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  664. xs *fn = _timeline_find_fn(&snac, id);
  665. if (fn != NULL) {
  666. xs *list = xs_list_new();
  667. list = xs_list_append(list, fn);
  668. *body = html_timeline(&snac, list, 1);
  669. *b_size = strlen(*body);
  670. status = 200;
  671. }
  672. }
  673. else
  674. if (xs_startswith(p_path, "s/")) {
  675. /* a static file */
  676. xs *l = xs_split(p_path, "/");
  677. char *id = xs_list_get(l, 1);
  678. int sz;
  679. if (valid_status(static_get(&snac, id, body, &sz))) {
  680. *b_size = sz;
  681. *ctype = xs_mime_by_ext(id);
  682. status = 200;
  683. }
  684. }
  685. else
  686. if (xs_startswith(p_path, "h/")) {
  687. /* an entry from the history */
  688. xs *l = xs_split(p_path, "/");
  689. char *id = xs_list_get(l, 1);
  690. if ((*body = history_get(&snac, id)) != NULL) {
  691. *b_size = strlen(*body);
  692. status = 200;
  693. }
  694. }
  695. else
  696. status = 404;
  697. user_free(&snac);
  698. if (valid_status(status) && *ctype == NULL) {
  699. *ctype = "text/html; charset=utf-8";
  700. }
  701. return status;
  702. }
  703. int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size,
  704. char **body, int *b_size, char **ctype)
  705. {
  706. int status = 0;
  707. snac snac;
  708. char *uid, *p_path;
  709. char *p_vars;
  710. xs *l = xs_split_n(q_path, "/", 2);
  711. uid = xs_list_get(l, 1);
  712. if (!uid || !user_open(&snac, uid)) {
  713. /* invalid user */
  714. srv_log(xs_fmt("html_get_handler bad user %s", uid));
  715. return 404;
  716. }
  717. p_path = xs_list_get(l, 2);
  718. /* all posts must be authenticated */
  719. if (!login(&snac, req))
  720. return 401;
  721. p_vars = xs_dict_get(req, "p_vars");
  722. #if 0
  723. {
  724. xs *j1 = xs_json_dumps_pp(p_vars, 4);
  725. printf("%s\n", j1);
  726. }
  727. #endif
  728. if (p_path && strcmp(p_path, "admin/note") == 0) {
  729. /* post note */
  730. char *content = xs_dict_get(p_vars, "content");
  731. char *in_reply_to = xs_dict_get(p_vars, "in_reply_to");
  732. char *attach_url = xs_dict_get(p_vars, "attach_url");
  733. char *attach_file = xs_dict_get(p_vars, "attach");
  734. xs *attach_list = xs_list_new();
  735. /* is attach_url set? */
  736. if (!xs_is_null(attach_url) && *attach_url != '\0')
  737. attach_list = xs_list_append(attach_list, attach_url);
  738. /* is attach_file set? */
  739. if (!xs_is_null(attach_file) && xs_type(attach_file) == XSTYPE_LIST) {
  740. char *fn = xs_list_get(attach_file, 0);
  741. if (*fn != '\0') {
  742. char *ext = strrchr(fn, '.');
  743. xs *ntid = tid(0);
  744. xs *id = xs_fmt("%s%s", ntid, ext);
  745. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  746. int fo = xs_number_get(xs_list_get(attach_file, 1));
  747. int fs = xs_number_get(xs_list_get(attach_file, 2));
  748. /* store */
  749. static_put(&snac, id, payload + fo, fs);
  750. attach_list = xs_list_append(attach_list, url);
  751. }
  752. }
  753. if (content != NULL) {
  754. xs *msg = NULL;
  755. xs *c_msg = NULL;
  756. msg = msg_note(&snac, content, NULL, in_reply_to, attach_list);
  757. c_msg = msg_create(&snac, msg);
  758. post(&snac, c_msg);
  759. timeline_add(&snac, xs_dict_get(msg, "id"), msg, in_reply_to, NULL);
  760. }
  761. status = 303;
  762. }
  763. else
  764. if (p_path && strcmp(p_path, "admin/action") == 0) {
  765. /* action on an entry */
  766. char *id = xs_dict_get(p_vars, "id");
  767. char *actor = xs_dict_get(p_vars, "actor");
  768. char *action = xs_dict_get(p_vars, "action");
  769. if (action == NULL)
  770. return 404;
  771. snac_debug(&snac, 1, xs_fmt("web action '%s' received", action));
  772. status = 303;
  773. if (strcmp(action, L("Like")) == 0) {
  774. xs *msg = msg_admiration(&snac, id, "Like");
  775. post(&snac, msg);
  776. timeline_admire(&snac, id, snac.actor, 1);
  777. }
  778. else
  779. if (strcmp(action, L("Boost")) == 0) {
  780. xs *msg = msg_admiration(&snac, id, "Announce");
  781. post(&snac, msg);
  782. timeline_admire(&snac, id, snac.actor, 0);
  783. }
  784. else
  785. if (strcmp(action, L("MUTE")) == 0) {
  786. mute(&snac, actor);
  787. }
  788. else
  789. if (strcmp(action, L("Follow")) == 0) {
  790. xs *msg = msg_follow(&snac, actor);
  791. /* reload the actor from the message, in may be different */
  792. actor = xs_dict_get(msg, "object");
  793. following_add(&snac, actor, msg);
  794. enqueue_output(&snac, msg, actor, 0);
  795. }
  796. else
  797. if (strcmp(action, L("Unfollow")) == 0) {
  798. /* get the following object */
  799. xs *object = NULL;
  800. if (valid_status(following_get(&snac, actor, &object))) {
  801. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  802. following_del(&snac, actor);
  803. enqueue_output(&snac, msg, actor, 0);
  804. snac_log(&snac, xs_fmt("unfollowed actor %s", actor));
  805. }
  806. else
  807. snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
  808. }
  809. else
  810. if (strcmp(action, L("Delete")) == 0) {
  811. /* delete an entry */
  812. if (xs_startswith(id, snac.actor)) {
  813. /* it's a post by us: generate a delete */
  814. xs *msg = msg_delete(&snac, id);
  815. post(&snac, msg);
  816. snac_log(&snac, xs_fmt("posted tombstone for %s", id));
  817. }
  818. timeline_del(&snac, id);
  819. snac_log(&snac, xs_fmt("deleted entry %s", id));
  820. }
  821. else
  822. status = 404;
  823. /* delete the cached timeline */
  824. if (status == 303)
  825. history_del(&snac, "timeline.html_");
  826. }
  827. else
  828. if (p_path && strcmp(p_path, "admin/user-setup") == 0) {
  829. /* change of user data */
  830. char *v;
  831. char *p1, *p2;
  832. if ((v = xs_dict_get(p_vars, "name")) != NULL)
  833. snac.config = xs_dict_set(snac.config, "name", v);
  834. if ((v = xs_dict_get(p_vars, "avatar")) != NULL)
  835. snac.config = xs_dict_set(snac.config, "avatar", v);
  836. if ((v = xs_dict_get(p_vars, "bio")) != NULL)
  837. snac.config = xs_dict_set(snac.config, "bio", v);
  838. if ((v = xs_dict_get(p_vars, "email")) != NULL)
  839. snac.config = xs_dict_set(snac.config, "email", v);
  840. /* password change? */
  841. if ((p1 = xs_dict_get(p_vars, "passwd1")) != NULL &&
  842. (p2 = xs_dict_get(p_vars, "passwd2")) != NULL &&
  843. *p1 && strcmp(p1, p2) == 0) {
  844. xs *pw = hash_password(snac.uid, p1, NULL);
  845. snac.config = xs_dict_set(snac.config, "passwd", pw);
  846. }
  847. xs *fn = xs_fmt("%s/user.json", snac.basedir);
  848. xs *bfn = xs_fmt("%s.bak", fn);
  849. FILE *f;
  850. rename(fn, bfn);
  851. if ((f = fopen(fn, "w")) != NULL) {
  852. xs *j = xs_json_dumps_pp(snac.config, 4);
  853. fwrite(j, strlen(j), 1, f);
  854. fclose(f);
  855. }
  856. else
  857. rename(bfn, fn);
  858. history_del(&snac, "timeline.html_");
  859. xs *a_msg = msg_actor(&snac);
  860. xs *u_msg = msg_update(&snac, a_msg);
  861. post(&snac, u_msg);
  862. status = 303;
  863. }
  864. if (status == 303) {
  865. char *redir = xs_dict_get(p_vars, "redir");
  866. if (xs_is_null(redir))
  867. redir = "snac-posts";
  868. *body = xs_fmt("%s/admin#%s", snac.actor, redir);
  869. *b_size = strlen(*body);
  870. }
  871. user_free(&snac);
  872. return status;
  873. }