html.c 37 KB

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