html.c 36 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=\"file\" name=\"attach\">\n"
  352. "<p><input type=\"submit\" class=\"button\" value=\"%s\">\n"
  353. "</form><p></div>\n"
  354. "</details><p>"
  355. "\n",
  356. L("Reply..."),
  357. md5,
  358. snac->actor, md5,
  359. ct,
  360. id,
  361. L("Post")
  362. );
  363. s = xs_str_cat(s, s1);
  364. }
  365. s = xs_str_cat(s, "</div>\n");
  366. return xs_str_cat(os, s);
  367. }
  368. d_char *html_entry(snac *snac, d_char *os, char *msg, xs_set *seen, int local, int level, int *num)
  369. {
  370. char *id = xs_dict_get(msg, "id");
  371. char *type = xs_dict_get(msg, "type");
  372. char *meta = xs_dict_get(msg, "_snac");
  373. char *actor;
  374. int sensitive = 0;
  375. char *v;
  376. /* do not show non-public messages in the public timeline */
  377. if (local && !is_msg_public(snac, msg))
  378. return os;
  379. /* return if already seen */
  380. if (xs_set_add(seen, id) == 0)
  381. return os;
  382. xs *s = xs_str_new(NULL);
  383. /* top wrap */
  384. if ((v = xs_dict_get(meta, "hidden")) && xs_type(v) == XSTYPE_TRUE)
  385. s = xs_str_cat(s, "<div style=\"display: none\">\n");
  386. else
  387. s = xs_str_cat(s, "<div>\n");
  388. if (level == 0) {
  389. xs *s1 = xs_fmt("<a name=\"%d_entry\"></a>\n", *num);
  390. *num = *num + 1;
  391. s = xs_str_cat(s, s1);
  392. }
  393. if (strcmp(type, "Follow") == 0) {
  394. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  395. xs *s1 = xs_fmt("<div class=\"snac-origin\">%s</div>\n", L("follows you"));
  396. s = xs_str_cat(s, s1);
  397. s = html_msg_icon(snac, s, msg);
  398. s = xs_str_cat(s, "</div>\n");
  399. return xs_str_cat(os, s);
  400. }
  401. /* bring the main actor */
  402. if ((actor = xs_dict_get(msg, "attributedTo")) == NULL)
  403. return os;
  404. /* ignore muted morons immediately */
  405. if (is_muted(snac, actor))
  406. return os;
  407. if (strcmp(actor, snac->actor) != 0 && !valid_status(actor_get(snac, actor, NULL)))
  408. return os;
  409. /* if this is our post, add the score */
  410. if (xs_startswith(id, snac->actor)) {
  411. int likes = xs_list_len(xs_dict_get(meta, "liked_by"));
  412. int boosts = xs_list_len(xs_dict_get(meta, "announced_by"));
  413. /* alternate emojis: %d &#128077; %d &#128257; */
  414. xs *s1 = xs_fmt(
  415. "<div class=\"snac-score\">%d &#9733; %d &#8634;</div>\n",
  416. likes, boosts);
  417. s = xs_str_cat(s, s1);
  418. }
  419. if (level == 0) {
  420. char *p;
  421. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  422. /* print the origin of the post, if any */
  423. if (!xs_is_null(p = xs_dict_get(meta, "referrer"))) {
  424. xs *actor_r = NULL;
  425. if (valid_status(actor_get(snac, p, &actor_r))) {
  426. char *name;
  427. if ((name = xs_dict_get(actor_r, "name")) == NULL)
  428. name = xs_dict_get(actor_r, "preferredUsername");
  429. xs *s1 = xs_fmt(
  430. "<div class=\"snac-origin\">"
  431. "<a href=\"%s\">%s</a> %s</div>\n",
  432. xs_dict_get(actor_r, "id"),
  433. name,
  434. L("boosted")
  435. );
  436. s = xs_str_cat(s, s1);
  437. }
  438. }
  439. else
  440. if (!xs_is_null((p = xs_dict_get(meta, "parent"))) && *p) {
  441. /* this may happen if any of the autor or the parent aren't here */
  442. xs *s1 = xs_fmt(
  443. "<div class=\"snac-origin\">%s "
  444. "<a href=\"%s\">»</a></div>\n",
  445. L("in reply to"), p
  446. );
  447. s = xs_str_cat(s, s1);
  448. }
  449. else
  450. if (!xs_is_null((p = xs_dict_get(meta, "announced_by"))) &&
  451. xs_list_in(p, snac->actor) != -1) {
  452. /* we boosted this */
  453. xs *s1 = xs_fmt(
  454. "<div class=\"snac-origin\">"
  455. "<a href=\"%s\">%s</a> %s</a></div>",
  456. snac->actor, xs_dict_get(snac->config, "name"), L("boosted")
  457. );
  458. s = xs_str_cat(s, s1);
  459. }
  460. else
  461. if (!xs_is_null((p = xs_dict_get(meta, "liked_by"))) &&
  462. xs_list_in(p, snac->actor) != -1) {
  463. /* we liked this */
  464. xs *s1 = xs_fmt(
  465. "<div class=\"snac-origin\">"
  466. "<a href=\"%s\">%s</a> %s</a></div>",
  467. snac->actor, xs_dict_get(snac->config, "name"), L("liked")
  468. );
  469. s = xs_str_cat(s, s1);
  470. }
  471. }
  472. else
  473. s = xs_str_cat(s, "<div class=\"snac-child\">\n");
  474. s = html_msg_icon(snac, s, msg);
  475. /* add the content */
  476. s = xs_str_cat(s, "<div class=\"e-content snac-content\">\n");
  477. /* is it sensitive? */
  478. if (!xs_is_null(v = xs_dict_get(msg, "sensitive")) && xs_type(v) == XSTYPE_TRUE) {
  479. if (xs_is_null(v = xs_dict_get(msg, "summary")) || *v == '\0')
  480. v = "...";
  481. xs *s1 = xs_fmt("<details><summary>%s [%s]</summary>\n", v, L("SENSITIVE CONTENT"));
  482. s = xs_str_cat(s, s1);
  483. sensitive = 1;
  484. }
  485. {
  486. xs *c = xs_dup(xs_dict_get(msg, "content"));
  487. char *p, *v;
  488. /* do some tweaks to the content */
  489. c = xs_replace_i(c, "\r", "");
  490. while (xs_endswith(c, "<br><br>"))
  491. c = xs_crop(c, 0, -4);
  492. c = xs_replace_i(c, "<br><br>", "<p>");
  493. if (!xs_startswith(c, "<p>")) {
  494. xs *s1 = c;
  495. c = xs_fmt("<p>%s</p>", s1);
  496. }
  497. /* replace the :shortnames: */
  498. if (!xs_is_null(p = xs_dict_get(msg, "tag"))) {
  499. /* iterate the tags */
  500. while (xs_list_iter(&p, &v)) {
  501. char *t = xs_dict_get(v, "type");
  502. if (t && strcmp(t, "Emoji") == 0) {
  503. char *n = xs_dict_get(v, "name");
  504. char *i = xs_dict_get(v, "icon");
  505. if (n && i) {
  506. char *u = xs_dict_get(i, "url");
  507. xs *img = xs_fmt("<img src=\"%s\" style=\"height: 1em\"/>", u);
  508. c = xs_replace_i(c, n, img);
  509. }
  510. }
  511. }
  512. }
  513. xs *sc = sanitize(c);
  514. s = xs_str_cat(s, sc);
  515. }
  516. s = xs_str_cat(s, "\n");
  517. /* add the attachments */
  518. char *attach;
  519. if ((attach = xs_dict_get(msg, "attachment")) != NULL) {
  520. char *v;
  521. while (xs_list_iter(&attach, &v)) {
  522. char *t = xs_dict_get(v, "mediaType");
  523. if (xs_is_null(t))
  524. continue;
  525. if (xs_startswith(t, "image/")) {
  526. char *url = xs_dict_get(v, "url");
  527. char *name = xs_dict_get(v, "name");
  528. if (url != NULL) {
  529. xs *s1 = xs_fmt("<p><img src=\"%s\" alt=\"%s\"/></p>\n",
  530. url, xs_is_null(name) ? "" : name);
  531. s = xs_str_cat(s, s1);
  532. }
  533. }
  534. else
  535. if (xs_startswith(t, "video/")) {
  536. char *url = xs_dict_get(v, "url");
  537. if (url != NULL) {
  538. xs *s1 = xs_fmt("<p><object data=\"%s\"></object></p>\n", url);
  539. s = xs_str_cat(s, s1);
  540. }
  541. }
  542. }
  543. }
  544. if (sensitive)
  545. s = xs_str_cat(s, "</details><p>\n");
  546. s = xs_str_cat(s, "</div>\n");
  547. /** controls **/
  548. if (!local)
  549. s = html_entry_controls(snac, s, msg, *num);
  550. /** children **/
  551. char *children = xs_dict_get(meta, "children");
  552. int left = xs_list_len(children);
  553. if (left) {
  554. char *id;
  555. if (level < 4)
  556. s = xs_str_cat(s, "<div class=\"snac-children\">\n");
  557. else
  558. s = xs_str_cat(s, "<div>\n");
  559. if (left > 3)
  560. s = xs_str_cat(s, "<details><summary>...</summary>\n");
  561. while (xs_list_iter(&children, &id)) {
  562. xs *chd = timeline_find(snac, id);
  563. if (left == 3)
  564. s = xs_str_cat(s, "</details>\n");
  565. if (chd != NULL)
  566. s = html_entry(snac, s, chd, seen, local, level + 1, num);
  567. else
  568. snac_debug(snac, 2, xs_fmt("cannot read from timeline child %s", id));
  569. left--;
  570. }
  571. s = xs_str_cat(s, "</div>\n");
  572. }
  573. s = xs_str_cat(s, "</div>\n</div>\n");
  574. return xs_str_cat(os, s);
  575. }
  576. d_char *html_user_footer(snac *snac, d_char *s)
  577. {
  578. xs *s1 = xs_fmt(
  579. "<div class=\"snac-footer\">\n"
  580. "<a href=\"%s\">%s</a> - "
  581. "powered by <abbr title=\"Social Networks Are Crap\">snac</abbr></div>\n",
  582. srv_baseurl,
  583. L("about this site")
  584. );
  585. return xs_str_cat(s, s1);
  586. }
  587. d_char *html_timeline(snac *snac, char *list, int local)
  588. /* returns the HTML for the timeline */
  589. {
  590. d_char *s = xs_str_new(NULL);
  591. xs_set *seen = xs_set_new(4096);
  592. char *v;
  593. double t = ftime();
  594. int num = 0;
  595. s = html_user_header(snac, s, local);
  596. if (!local)
  597. s = html_top_controls(snac, s);
  598. s = xs_str_cat(s, "<a name=\"snac-posts\"></a>\n");
  599. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  600. while (xs_list_iter(&list, &v)) {
  601. xs *msg = timeline_get(snac, v);
  602. s = html_entry(snac, s, msg, seen, local, 0, &num);
  603. }
  604. s = xs_str_cat(s, "</div>\n");
  605. if (local) {
  606. xs *s1 = xs_fmt(
  607. "<div class=\"snac-history\">\n"
  608. "<p class=\"snac-history-title\">%s</p><ul>\n",
  609. L("History")
  610. );
  611. s = xs_str_cat(s, s1);
  612. xs *list = history_list(snac);
  613. char *p, *v;
  614. p = list;
  615. while (xs_list_iter(&p, &v)) {
  616. xs *fn = xs_replace(v, ".html", "");
  617. xs *s1 = xs_fmt(
  618. "<li><a href=\"%s/h/%s\">%s</li>\n",
  619. snac->actor, v, fn);
  620. s = xs_str_cat(s, s1);
  621. }
  622. s = xs_str_cat(s, "</ul></div>\n");
  623. }
  624. s = html_user_footer(snac, s);
  625. {
  626. xs *s1 = xs_fmt("<!-- %lf seconds -->\n", ftime() - t);
  627. s = xs_str_cat(s, s1);
  628. }
  629. s = xs_str_cat(s, "</body>\n</html>\n");
  630. xs_set_free(seen);
  631. return s;
  632. }
  633. d_char *html_people_list(snac *snac, d_char *os, d_char *list, const char *header, const char *t)
  634. {
  635. xs *s = xs_str_new(NULL);
  636. xs *h = xs_fmt("<h2>%s</h2>\n", header);
  637. char *p, *v;
  638. s = xs_str_cat(s, h);
  639. p = list;
  640. while (xs_list_iter(&p, &v)) {
  641. char *actor_id = xs_dict_get(v, "actor");
  642. xs *md5 = xs_md5_hex(actor_id, strlen(actor_id));
  643. xs *actor = NULL;
  644. if (valid_status(actor_get(snac, actor_id, &actor))) {
  645. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  646. s = html_actor_icon(snac, s, actor, xs_dict_get(actor, "published"), NULL, 0);
  647. /* content (user bio) */
  648. char *c = xs_dict_get(actor, "summary");
  649. if (!xs_is_null(c)) {
  650. s = xs_str_cat(s, "<div class=\"snac-content\">\n");
  651. xs *sc = sanitize(c);
  652. if (xs_startswith(sc, "<p>"))
  653. s = xs_str_cat(s, sc);
  654. else {
  655. xs *s1 = xs_fmt("<p>%s</p>", sc);
  656. s = xs_str_cat(s, s1);
  657. }
  658. s = xs_str_cat(s, "</div>\n");
  659. }
  660. /* buttons */
  661. s = xs_str_cat(s, "<div class=\"snac-controls\">\n");
  662. xs *s1 = xs_fmt(
  663. "<p><form method=\"post\" action=\"%s/admin/action\">\n"
  664. "<input type=\"hidden\" name=\"actor\" value=\"%s\">\n",
  665. snac->actor, actor_id,
  666. md5, t
  667. );
  668. s = xs_str_cat(s, s1);
  669. if (following_check(snac, actor_id))
  670. s = html_button(s, "unfollow", L("Unfollow"));
  671. else
  672. s = html_button(s, "follow", L("Follow"));
  673. if (is_muted(snac, actor_id))
  674. s = html_button(s, "unmute", L("Unmute"));
  675. else
  676. s = html_button(s, "mute", L("MUTE"));
  677. s = xs_str_cat(s, "</form>\n");
  678. /* the post textarea */
  679. xs *s2 = xs_fmt(
  680. "<p><details><summary>%s</summary>\n"
  681. "<p><div class=\"snac-note\" id=\"%s_%s_dm\">\n"
  682. "<form method=\"post\" action=\"%s/admin/note\" "
  683. "enctype=\"multipart/form-data\" id=\"%s_reply_form\">\n"
  684. "<textarea class=\"snac-textarea\" name=\"content\" "
  685. "rows=\"4\" wrap=\"virtual\" required=\"required\"></textarea>\n"
  686. "<input type=\"hidden\" name=\"to\" value=\"%s\">\n"
  687. "<p><input type=\"file\" name=\"attach\">\n"
  688. "<p><input type=\"submit\" class=\"button\" value=\"%s\">\n"
  689. "</form><p></div>\n"
  690. "</details><p>\n",
  691. L("Direct Message..."),
  692. md5, t,
  693. snac->actor, md5,
  694. actor_id,
  695. L("Post")
  696. );
  697. s = xs_str_cat(s, s2);
  698. s = xs_str_cat(s, "</div>\n");
  699. s = xs_str_cat(s, "</div>\n");
  700. }
  701. }
  702. return xs_str_cat(os, s);
  703. }
  704. d_char *html_people(snac *snac)
  705. {
  706. d_char *s = xs_str_new(NULL);
  707. xs *wing = following_list(snac);
  708. xs *wers = follower_list(snac);
  709. s = html_user_header(snac, s, 0);
  710. s = html_people_list(snac, s, wing, L("People you follow"), "i");
  711. s = html_people_list(snac, s, wers, L("People that follows you"), "e");
  712. s = html_user_footer(snac, s);
  713. s = xs_str_cat(s, "</body>\n</html>\n");
  714. return s;
  715. }
  716. int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char **ctype)
  717. {
  718. int status = 404;
  719. snac snac;
  720. char *uid, *p_path;
  721. int cache = 1;
  722. char *v;
  723. xs *l = xs_split_n(q_path, "/", 2);
  724. uid = xs_list_get(l, 1);
  725. if (!uid || !user_open(&snac, uid)) {
  726. /* invalid user */
  727. srv_log(xs_fmt("html_get_handler bad user %s", uid));
  728. return 404;
  729. }
  730. /* check if server config variable 'disable_cache' is set */
  731. if ((v = xs_dict_get(srv_config, "disable_cache")) && xs_type(v) == XSTYPE_TRUE)
  732. cache = 0;
  733. p_path = xs_list_get(l, 2);
  734. if (p_path == NULL) {
  735. /* public timeline */
  736. xs *h = xs_str_localtime(0, "%Y-%m.html");
  737. if (cache && history_mtime(&snac, h) > timeline_mtime(&snac)) {
  738. snac_debug(&snac, 1, xs_fmt("serving cached local timeline"));
  739. *body = history_get(&snac, h);
  740. *b_size = strlen(*body);
  741. status = 200;
  742. }
  743. else {
  744. xs *list = local_list(&snac, 0xfffffff);
  745. *body = html_timeline(&snac, list, 1);
  746. *b_size = strlen(*body);
  747. status = 200;
  748. history_add(&snac, h, *body, *b_size);
  749. }
  750. }
  751. else
  752. if (strcmp(p_path, "admin") == 0) {
  753. /* private timeline */
  754. if (!login(&snac, req))
  755. status = 401;
  756. else {
  757. if (cache && history_mtime(&snac, "timeline.html_") > timeline_mtime(&snac)) {
  758. snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
  759. *body = history_get(&snac, "timeline.html_");
  760. *b_size = strlen(*body);
  761. status = 200;
  762. }
  763. else {
  764. snac_debug(&snac, 1, xs_fmt("building timeline"));
  765. xs *list = timeline_list(&snac, 0xfffffff);
  766. *body = html_timeline(&snac, list, 0);
  767. *b_size = strlen(*body);
  768. status = 200;
  769. history_add(&snac, "timeline.html_", *body, *b_size);
  770. }
  771. }
  772. }
  773. else
  774. if (strcmp(p_path, "people") == 0) {
  775. /* the list of people */
  776. if (!login(&snac, req))
  777. status = 401;
  778. else {
  779. *body = html_people(&snac);
  780. *b_size = strlen(*body);
  781. status = 200;
  782. }
  783. }
  784. else
  785. if (xs_startswith(p_path, "p/")) {
  786. /* a timeline with just one entry */
  787. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  788. xs *fn = _timeline_find_fn(&snac, id);
  789. if (fn != NULL) {
  790. xs *list = xs_list_new();
  791. list = xs_list_append(list, fn);
  792. *body = html_timeline(&snac, list, 1);
  793. *b_size = strlen(*body);
  794. status = 200;
  795. }
  796. }
  797. else
  798. if (xs_startswith(p_path, "s/")) {
  799. /* a static file */
  800. xs *l = xs_split(p_path, "/");
  801. char *id = xs_list_get(l, 1);
  802. int sz;
  803. if (valid_status(static_get(&snac, id, body, &sz))) {
  804. *b_size = sz;
  805. *ctype = xs_mime_by_ext(id);
  806. status = 200;
  807. }
  808. }
  809. else
  810. if (xs_startswith(p_path, "h/")) {
  811. /* an entry from the history */
  812. xs *l = xs_split(p_path, "/");
  813. char *id = xs_list_get(l, 1);
  814. if ((*body = history_get(&snac, id)) != NULL) {
  815. *b_size = strlen(*body);
  816. status = 200;
  817. }
  818. }
  819. else
  820. status = 404;
  821. user_free(&snac);
  822. if (valid_status(status) && *ctype == NULL) {
  823. *ctype = "text/html; charset=utf-8";
  824. }
  825. return status;
  826. }
  827. int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size,
  828. char **body, int *b_size, char **ctype)
  829. {
  830. int status = 0;
  831. snac snac;
  832. char *uid, *p_path;
  833. char *p_vars;
  834. xs *l = xs_split_n(q_path, "/", 2);
  835. uid = xs_list_get(l, 1);
  836. if (!uid || !user_open(&snac, uid)) {
  837. /* invalid user */
  838. srv_log(xs_fmt("html_get_handler bad user %s", uid));
  839. return 404;
  840. }
  841. p_path = xs_list_get(l, 2);
  842. /* all posts must be authenticated */
  843. if (!login(&snac, req)) {
  844. user_free(&snac);
  845. return 401;
  846. }
  847. p_vars = xs_dict_get(req, "p_vars");
  848. #if 0
  849. {
  850. xs *j1 = xs_json_dumps_pp(p_vars, 4);
  851. printf("%s\n", j1);
  852. }
  853. #endif
  854. if (p_path && strcmp(p_path, "admin/note") == 0) {
  855. /* post note */
  856. char *content = xs_dict_get(p_vars, "content");
  857. char *in_reply_to = xs_dict_get(p_vars, "in_reply_to");
  858. char *attach_url = xs_dict_get(p_vars, "attach_url");
  859. char *attach_file = xs_dict_get(p_vars, "attach");
  860. char *to = xs_dict_get(p_vars, "to");
  861. char *sensitive = xs_dict_get(p_vars, "sensitive");
  862. xs *attach_list = xs_list_new();
  863. /* is attach_url set? */
  864. if (!xs_is_null(attach_url) && *attach_url != '\0')
  865. attach_list = xs_list_append(attach_list, attach_url);
  866. /* is attach_file set? */
  867. if (!xs_is_null(attach_file) && xs_type(attach_file) == XSTYPE_LIST) {
  868. char *fn = xs_list_get(attach_file, 0);
  869. if (*fn != '\0') {
  870. char *ext = strrchr(fn, '.');
  871. xs *ntid = tid(0);
  872. xs *id = xs_fmt("%s%s", ntid, ext);
  873. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  874. int fo = xs_number_get(xs_list_get(attach_file, 1));
  875. int fs = xs_number_get(xs_list_get(attach_file, 2));
  876. /* store */
  877. static_put(&snac, id, payload + fo, fs);
  878. attach_list = xs_list_append(attach_list, url);
  879. }
  880. }
  881. if (content != NULL) {
  882. xs *msg = NULL;
  883. xs *c_msg = NULL;
  884. xs *content_2 = xs_replace(content, "\r", "");
  885. msg = msg_note(&snac, content_2, to, in_reply_to, attach_list);
  886. if (sensitive != NULL) {
  887. xs *t = xs_val_new(XSTYPE_TRUE);
  888. msg = xs_dict_set(msg, "sensitive", t);
  889. msg = xs_dict_set(msg, "summary", "...");
  890. }
  891. c_msg = msg_create(&snac, msg);
  892. post(&snac, c_msg);
  893. timeline_add(&snac, xs_dict_get(msg, "id"), msg, in_reply_to, NULL);
  894. }
  895. status = 303;
  896. }
  897. else
  898. if (p_path && strcmp(p_path, "admin/action") == 0) {
  899. /* action on an entry */
  900. char *id = xs_dict_get(p_vars, "id");
  901. char *actor = xs_dict_get(p_vars, "actor");
  902. char *action = xs_dict_get(p_vars, "action");
  903. if (action == NULL)
  904. return 404;
  905. snac_debug(&snac, 1, xs_fmt("web action '%s' received", action));
  906. status = 303;
  907. if (strcmp(action, L("Like")) == 0) {
  908. xs *msg = msg_admiration(&snac, id, "Like");
  909. post(&snac, msg);
  910. timeline_admire(&snac, id, snac.actor, 1);
  911. }
  912. else
  913. if (strcmp(action, L("Boost")) == 0) {
  914. xs *msg = msg_admiration(&snac, id, "Announce");
  915. post(&snac, msg);
  916. timeline_admire(&snac, id, snac.actor, 0);
  917. }
  918. else
  919. if (strcmp(action, L("MUTE")) == 0) {
  920. mute(&snac, actor);
  921. }
  922. else
  923. if (strcmp(action, L("Unmute")) == 0) {
  924. unmute(&snac, actor);
  925. }
  926. else
  927. if (strcmp(action, L("Hide")) == 0) {
  928. timeline_hide(&snac, id, 1);
  929. }
  930. else
  931. if (strcmp(action, L("Follow")) == 0) {
  932. xs *msg = msg_follow(&snac, actor);
  933. if (msg != NULL) {
  934. /* reload the actor from the message, in may be different */
  935. actor = xs_dict_get(msg, "object");
  936. following_add(&snac, actor, msg);
  937. enqueue_output(&snac, msg, actor, 0);
  938. }
  939. }
  940. else
  941. if (strcmp(action, L("Unfollow")) == 0) {
  942. /* get the following object */
  943. xs *object = NULL;
  944. if (valid_status(following_get(&snac, actor, &object))) {
  945. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  946. following_del(&snac, actor);
  947. enqueue_output(&snac, msg, actor, 0);
  948. snac_log(&snac, xs_fmt("unfollowed actor %s", actor));
  949. }
  950. else
  951. snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
  952. }
  953. else
  954. if (strcmp(action, L("Delete")) == 0) {
  955. /* delete an entry */
  956. if (xs_startswith(id, snac.actor)) {
  957. /* it's a post by us: generate a delete */
  958. xs *msg = msg_delete(&snac, id);
  959. post(&snac, msg);
  960. snac_log(&snac, xs_fmt("posted tombstone for %s", id));
  961. }
  962. timeline_del(&snac, id);
  963. snac_log(&snac, xs_fmt("deleted entry %s", id));
  964. }
  965. else
  966. status = 404;
  967. /* delete the cached timeline */
  968. if (status == 303)
  969. history_del(&snac, "timeline.html_");
  970. }
  971. else
  972. if (p_path && strcmp(p_path, "admin/user-setup") == 0) {
  973. /* change of user data */
  974. char *v;
  975. char *p1, *p2;
  976. if ((v = xs_dict_get(p_vars, "name")) != NULL)
  977. snac.config = xs_dict_set(snac.config, "name", v);
  978. if ((v = xs_dict_get(p_vars, "avatar")) != NULL)
  979. snac.config = xs_dict_set(snac.config, "avatar", v);
  980. if ((v = xs_dict_get(p_vars, "bio")) != NULL)
  981. snac.config = xs_dict_set(snac.config, "bio", v);
  982. if ((v = xs_dict_get(p_vars, "email")) != NULL)
  983. snac.config = xs_dict_set(snac.config, "email", v);
  984. /* password change? */
  985. if ((p1 = xs_dict_get(p_vars, "passwd1")) != NULL &&
  986. (p2 = xs_dict_get(p_vars, "passwd2")) != NULL &&
  987. *p1 && strcmp(p1, p2) == 0) {
  988. xs *pw = hash_password(snac.uid, p1, NULL);
  989. snac.config = xs_dict_set(snac.config, "passwd", pw);
  990. }
  991. xs *fn = xs_fmt("%s/user.json", snac.basedir);
  992. xs *bfn = xs_fmt("%s.bak", fn);
  993. FILE *f;
  994. rename(fn, bfn);
  995. if ((f = fopen(fn, "w")) != NULL) {
  996. xs *j = xs_json_dumps_pp(snac.config, 4);
  997. fwrite(j, strlen(j), 1, f);
  998. fclose(f);
  999. }
  1000. else
  1001. rename(bfn, fn);
  1002. history_del(&snac, "timeline.html_");
  1003. xs *a_msg = msg_actor(&snac);
  1004. xs *u_msg = msg_update(&snac, a_msg);
  1005. post(&snac, u_msg);
  1006. status = 303;
  1007. }
  1008. if (status == 303) {
  1009. char *redir = xs_dict_get(p_vars, "redir");
  1010. if (xs_is_null(redir))
  1011. redir = "snac-posts";
  1012. *body = xs_fmt("%s/admin#%s", snac.actor, redir);
  1013. *b_size = strlen(*body);
  1014. }
  1015. user_free(&snac);
  1016. return status;
  1017. }