html.c 36 KB

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