activitypub.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 grunfink / MIT license */
  3. #include "xs.h"
  4. #include "xs_json.h"
  5. #include "xs_curl.h"
  6. #include "xs_mime.h"
  7. #include "xs_openssl.h"
  8. #include "xs_regex.h"
  9. #include "xs_time.h"
  10. #include "xs_set.h"
  11. #include "snac.h"
  12. #include <sys/wait.h>
  13. const char *public_address = "https:/" "/www.w3.org/ns/activitystreams#Public";
  14. /* susie.png */
  15. const char *susie =
  16. "iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
  17. "CEkxzAAAAUUlEQVQoz43R0QkAMQwCUDdw/y3dwE"
  18. "vsvzlL4X1IoQkAisKmwfAFT3RgJHbQezpSRoXEq"
  19. "eqCL9BJBf7h3QbOCCxV5EVWMEMwG7K1/WODtlvx"
  20. "AYTtEsDU9F34AAAAAElFTkSuQmCC";
  21. const char *susie_cool =
  22. "iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
  23. "CEkxzAAAAV0lEQVQoz43RwQ3AMAwCQDZg/y3ZgN"
  24. "qo3+JaedwDOUQBQFHYaTB8wTM6sGl2cMPu+DFzn"
  25. "+ZcgN7wF7ZVihXkfSlWIVzIA6dbQzaygllpNuTX"
  26. "ZmmFNlvxADX1+o0cUPMbAAAAAElFTkSuQmCC";
  27. const char *susie_muertos =
  28. "iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
  29. "CEkxzAAAAV0lEQVQoz4XQsQ0AMQxCUW/A/lv+DT"
  30. "ic6zGRolekIMyMELNp8PiCEw6Q4w4NoAt53IH5m"
  31. "xXksrZYgZwJrIox+Z8vJAfe2lCxG6AK7eKkWcEb"
  32. "QHbF617xAQatAAD7jJHUAAAAAElFTkSuQmCC";
  33. const char *default_avatar_base64(void)
  34. /* returns the default avatar in base64 */
  35. {
  36. time_t t = time(NULL);
  37. struct tm tm;
  38. const char *p = susie;
  39. gmtime_r(&t, &tm);
  40. if (tm.tm_mon == 10 && tm.tm_mday == 2)
  41. p = susie_muertos;
  42. else
  43. if (tm.tm_wday == 0 || tm.tm_wday == 6)
  44. p = susie_cool;
  45. return p;
  46. }
  47. int activitypub_request(snac *snac, const char *url, xs_dict **data)
  48. /* request an object */
  49. {
  50. int status;
  51. xs *response = NULL;
  52. xs *payload = NULL;
  53. int p_size;
  54. char *ctype;
  55. /* get from the net */
  56. response = http_signed_request(snac, "GET", url,
  57. NULL, NULL, 0, &status, &payload, &p_size, 0);
  58. if (status == 0 || (status >= 500 && status <= 599)) {
  59. /* I found an instance running Misskey that returned
  60. 500 on signed messages but returned the object
  61. perfectly without signing (?), so why not try */
  62. xs_free(response);
  63. xs *hdrs = xs_dict_new();
  64. hdrs = xs_dict_append(hdrs, "accept", "application/activity+json");
  65. hdrs = xs_dict_append(hdrs, "user-agent", USER_AGENT);
  66. response = xs_http_request("GET", url, hdrs,
  67. NULL, 0, &status, &payload, &p_size, 0);
  68. }
  69. if (valid_status(status)) {
  70. /* ensure it's ActivityPub data */
  71. ctype = xs_dict_get(response, "content-type");
  72. if (xs_str_in(ctype, "application/activity+json") != -1 ||
  73. xs_str_in(ctype, "application/ld+json") != -1)
  74. *data = xs_json_loads(payload);
  75. else
  76. status = 500;
  77. }
  78. if (!valid_status(status))
  79. *data = NULL;
  80. return status;
  81. }
  82. int actor_request(snac *snac, const char *actor, xs_dict **data)
  83. /* request an actor */
  84. {
  85. int status, status2;
  86. xs *payload = NULL;
  87. if (data)
  88. *data = NULL;
  89. /* get from disk first */
  90. status = actor_get(snac, actor, data);
  91. if (status != 200) {
  92. /* actor data non-existent or stale: get from the net */
  93. status2 = activitypub_request(snac, actor, &payload);
  94. if (valid_status(status2)) {
  95. /* renew data */
  96. status = actor_add(actor, payload);
  97. if (data != NULL) {
  98. *data = payload;
  99. payload = NULL;
  100. }
  101. }
  102. }
  103. /* collect the (presumed) shared inbox in this actor */
  104. if (xs_type(xs_dict_get(srv_config, "disable_inbox_collection")) != XSTYPE_TRUE) {
  105. if (valid_status(status) && data && *data)
  106. inbox_add_by_actor(*data);
  107. }
  108. else
  109. srv_log(xs_fmt("NOT collected"));
  110. return status;
  111. }
  112. int timeline_request(snac *snac, char **id, xs_str **wrk)
  113. /* ensures that an entry and its ancestors are in the timeline */
  114. {
  115. int status = 0;
  116. if (!xs_is_null(*id)) {
  117. xs *object = NULL;
  118. /* is the object already there? */
  119. if (!valid_status(object_get(*id, &object))) {
  120. /* no; download it */
  121. status = activitypub_request(snac, *id, &object);
  122. if (valid_status(status)) {
  123. char *type = xs_dict_get(object, "type");
  124. /* get the id again from the object, as it may be different */
  125. char *nid = xs_dict_get(object, "id");
  126. if (wrk && strcmp(nid, *id) != 0) {
  127. snac_debug(snac, 1,
  128. xs_fmt("timeline_request canonical id for %s is %s", *id, nid));
  129. *wrk = xs_dup(nid);
  130. *id = *wrk;
  131. }
  132. if (!xs_is_null(type) && strcmp(type, "Note") == 0) {
  133. char *actor = xs_dict_get(object, "attributedTo");
  134. /* request (and drop) the actor for this entry */
  135. if (!xs_is_null(actor))
  136. actor_request(snac, actor, NULL);
  137. /* does it have an ancestor? */
  138. char *in_reply_to = xs_dict_get(object, "inReplyTo");
  139. /* store */
  140. timeline_add(snac, *id, object);
  141. /* recurse! */
  142. timeline_request(snac, &in_reply_to, NULL);
  143. }
  144. }
  145. }
  146. enqueue_request_replies(snac, *id);
  147. }
  148. return status;
  149. }
  150. void timeline_request_replies(snac *user, const char *id)
  151. /* requests all replies of a message */
  152. /* FIXME: experimental -- needs more testing */
  153. {
  154. xs *msg = NULL;
  155. if (!valid_status(object_get(id, &msg)))
  156. return;
  157. /* does it have a replies collection? */
  158. const xs_dict *replies = xs_dict_get(msg, "replies");
  159. if (!xs_is_null(replies)) {
  160. const char *type = xs_dict_get(replies, "type");
  161. const char *first = xs_dict_get(replies, "first");
  162. if (!xs_is_null(type) && !xs_is_null(first) && strcmp(type, "Collection") == 0) {
  163. const char *next = xs_dict_get(first, "next");
  164. if (!xs_is_null(next)) {
  165. xs *rpls = NULL;
  166. int status = activitypub_request(user, next, &rpls);
  167. /* request the Collection of replies */
  168. if (valid_status(status)) {
  169. xs_list *items = xs_dict_get(rpls, "items");
  170. if (xs_type(items) == XSTYPE_LIST) {
  171. xs_val *v;
  172. /* request them all */
  173. while (xs_list_iter(&items, &v)) {
  174. if (xs_type(v) == XSTYPE_DICT) {
  175. /* not an id, but the object itself (!) */
  176. const char *id = xs_dict_get(v, "id");
  177. if (!xs_is_null(id)) {
  178. snac_debug(user, 0, xs_fmt("embedded reply %s", id));
  179. object_add(id, v);
  180. /* get its own children */
  181. timeline_request_replies(user, v);
  182. }
  183. }
  184. else {
  185. snac_debug(user, 0, xs_fmt("request reply %s", v));
  186. timeline_request(user, &v, NULL);
  187. }
  188. }
  189. }
  190. }
  191. else
  192. snac_debug(user, 0, xs_fmt("replies request error %s %d", next, status));
  193. }
  194. }
  195. }
  196. }
  197. int send_to_inbox_raw(const char *keyid, const char *seckey,
  198. const xs_str *inbox, const xs_dict *msg,
  199. xs_val **payload, int *p_size, int timeout)
  200. /* sends a message to an Inbox */
  201. {
  202. int status;
  203. xs_dict *response;
  204. xs *j_msg = xs_json_dumps_pp((xs_dict *)msg, 4);
  205. response = http_signed_request_raw(keyid, seckey, "POST", inbox,
  206. NULL, j_msg, strlen(j_msg), &status, payload, p_size, timeout);
  207. xs_free(response);
  208. return status;
  209. }
  210. int send_to_inbox(snac *snac, const xs_str *inbox, const xs_dict *msg,
  211. xs_val **payload, int *p_size, int timeout)
  212. /* sends a message to an Inbox */
  213. {
  214. char *seckey = xs_dict_get(snac->key, "secret");
  215. return send_to_inbox_raw(snac->actor, seckey, inbox, msg, payload, p_size, timeout);
  216. }
  217. d_char *get_actor_inbox(snac *snac, const char *actor)
  218. /* gets an actor's inbox */
  219. {
  220. xs *data = NULL;
  221. char *v = NULL;
  222. if (valid_status(actor_request(snac, actor, &data))) {
  223. /* try first endpoints/sharedInbox */
  224. if ((v = xs_dict_get(data, "endpoints")))
  225. v = xs_dict_get(v, "sharedInbox");
  226. /* try then the regular inbox */
  227. if (xs_is_null(v))
  228. v = xs_dict_get(data, "inbox");
  229. }
  230. return xs_is_null(v) ? NULL : xs_dup(v);
  231. }
  232. int send_to_actor(snac *snac, char *actor, char *msg, d_char **payload, int *p_size, int timeout)
  233. /* sends a message to an actor */
  234. {
  235. int status = 400;
  236. xs *inbox = get_actor_inbox(snac, actor);
  237. if (!xs_is_null(inbox))
  238. status = send_to_inbox(snac, inbox, msg, payload, p_size, timeout);
  239. return status;
  240. }
  241. xs_list *recipient_list(snac *snac, const xs_dict *msg, int expand_public)
  242. /* returns the list of recipients for a message */
  243. {
  244. char *to = xs_dict_get(msg, "to");
  245. char *cc = xs_dict_get(msg, "cc");
  246. xs_set rcpts;
  247. int n;
  248. xs_set_init(&rcpts);
  249. char *lists[] = { to, cc, NULL };
  250. for (n = 0; lists[n]; n++) {
  251. char *l = lists[n];
  252. char *v;
  253. xs *tl = NULL;
  254. /* if it's a string, create a list with only one element */
  255. if (xs_type(l) == XSTYPE_STRING) {
  256. tl = xs_list_new();
  257. tl = xs_list_append(tl, l);
  258. l = tl;
  259. }
  260. while (xs_list_iter(&l, &v)) {
  261. if (expand_public && strcmp(v, public_address) == 0) {
  262. /* iterate the followers and add them */
  263. xs *fwers = follower_list(snac);
  264. char *actor;
  265. char *p = fwers;
  266. while (xs_list_iter(&p, &actor))
  267. xs_set_add(&rcpts, actor);
  268. }
  269. else
  270. xs_set_add(&rcpts, v);
  271. }
  272. }
  273. return xs_set_result(&rcpts);
  274. }
  275. int is_msg_public(snac *snac, const xs_dict *msg)
  276. /* checks if a message is public */
  277. {
  278. xs *rcpts = recipient_list(snac, msg, 0);
  279. return xs_list_in(rcpts, public_address) != -1;
  280. }
  281. int is_msg_for_me(snac *snac, const xs_dict *c_msg)
  282. /* checks if this message is for me */
  283. {
  284. const char *type = xs_dict_get(c_msg, "type");
  285. if (strcmp(type, "Announce") == 0) {
  286. const char *object = xs_dict_get(c_msg, "object");
  287. if (xs_type(object) == XSTYPE_DICT)
  288. object = xs_dict_get(object, "id");
  289. /* if it's about one of our posts, accept it */
  290. if (xs_startswith(object, snac->actor))
  291. return 2;
  292. /* if it's by someone we don't follow, reject */
  293. if (!following_check(snac, xs_dict_get(c_msg, "actor")))
  294. return 0;
  295. }
  296. /* if it's not a Create, allow */
  297. if (strcmp(type, "Create") != 0)
  298. return 1;
  299. xs_dict *msg = xs_dict_get(c_msg, "object");
  300. xs *rcpts = recipient_list(snac, msg, 0);
  301. xs_list *p = rcpts;
  302. xs_str *v;
  303. while(xs_list_iter(&p, &v)) {
  304. /* explicitly for me? accept */
  305. if (strcmp(v, snac->actor) == 0)
  306. return 2;
  307. /* for someone we follow? (probably cc'ed) accept */
  308. if (following_check(snac, v))
  309. return 5;
  310. }
  311. /* accept if it's by someone we follow */
  312. char *atto = xs_dict_get(msg, "attributedTo");
  313. if (!xs_is_null(atto) && following_check(snac, atto))
  314. return 3;
  315. /* is this message a reply to another? */
  316. char *irt = xs_dict_get(msg, "inReplyTo");
  317. if (!xs_is_null(irt)) {
  318. xs *r_msg = NULL;
  319. /* try to get the replied message */
  320. if (valid_status(object_get(irt, &r_msg))) {
  321. atto = xs_dict_get(r_msg, "attributedTo");
  322. /* accept if the replied message is from someone we follow */
  323. if (!xs_is_null(atto) && following_check(snac, atto))
  324. return 4;
  325. }
  326. }
  327. return 0;
  328. }
  329. void process_tags(snac *snac, const char *content, xs_str **n_content, xs_list **tag)
  330. /* parses mentions and tags from content */
  331. {
  332. xs_str *nc = xs_str_new(NULL);
  333. xs_list *tl = *tag;
  334. xs *split;
  335. xs_list *p;
  336. xs_val *v;
  337. int n = 0;
  338. /* create a default server for incomplete mentions */
  339. xs *def_srv = NULL;
  340. if (xs_list_len(tl)) {
  341. /* if there are any mentions, get the server from
  342. the first one, which is the inReplyTo author */
  343. p = tl;
  344. while (xs_list_iter(&p, &v)) {
  345. const char *type = xs_dict_get(v, "type");
  346. const char *name = xs_dict_get(v, "name");
  347. if (type && name && strcmp(type, "Mention") == 0) {
  348. xs *l = xs_split(name, "@");
  349. def_srv = xs_dup(xs_list_get(l, -1));
  350. break;
  351. }
  352. }
  353. }
  354. if (xs_is_null(def_srv))
  355. /* use this same server */
  356. def_srv = xs_dup(xs_dict_get(srv_config, "host"));
  357. split = xs_regex_split(content, "(@[A-Za-z0-9_]+(@[A-Za-z0-9\\.-]+)?|&#[0-9]+;|#[^ ,\\.:;<]+)");
  358. p = split;
  359. while (xs_list_iter(&p, &v)) {
  360. if ((n & 0x1)) {
  361. if (*v == '@') {
  362. xs *link = NULL;
  363. xs *wuid = NULL;
  364. if (strchr(v + 1, '@') == NULL) {
  365. /* only one @? it's a dumb Mastodon-like mention
  366. without server; add the default one */
  367. wuid = xs_fmt("%s@%s", v, def_srv);
  368. snac_debug(snac, 2, xs_fmt("mention without server '%s' '%s'", v, wuid));
  369. }
  370. else
  371. wuid = xs_dup(v);
  372. /* query the webfinger about this fellow */
  373. xs *actor = NULL;
  374. xs *uid = NULL;
  375. int status;
  376. status = webfinger_request(wuid, &actor, &uid);
  377. if (valid_status(status)) {
  378. xs *d = xs_dict_new();
  379. xs *n = xs_fmt("@%s", uid);
  380. d = xs_dict_append(d, "type", "Mention");
  381. d = xs_dict_append(d, "href", actor);
  382. d = xs_dict_append(d, "name", n);
  383. tl = xs_list_append(tl, d);
  384. link = xs_fmt("<a href=\"%s\" class=\"u-url mention\">%s</a>", actor, n);
  385. }
  386. if (!xs_is_null(link))
  387. nc = xs_str_cat(nc, link);
  388. else
  389. nc = xs_str_cat(nc, v);
  390. }
  391. else
  392. if (*v == '#') {
  393. /* hashtag */
  394. xs *d = xs_dict_new();
  395. xs *n = xs_tolower_i(xs_dup(v));
  396. xs *h = xs_fmt("%s%s", snac->actor, n);
  397. xs *l = xs_fmt("<a href=\"%s\" class=\"mention hashtag\" rel=\"tag\">%s</a>", h, v);
  398. d = xs_dict_append(d, "type", "Hashtag");
  399. d = xs_dict_append(d, "href", h);
  400. d = xs_dict_append(d, "name", n);
  401. tl = xs_list_append(tl, d);
  402. /* add the code */
  403. nc = xs_str_cat(nc, l);
  404. }
  405. else
  406. if (*v == '&') {
  407. /* HTML Unicode entity, probably part of an emoji */
  408. /* write as is */
  409. nc = xs_str_cat(nc, v);
  410. }
  411. }
  412. else
  413. nc = xs_str_cat(nc, v);
  414. n++;
  415. }
  416. *n_content = nc;
  417. *tag = tl;
  418. }
  419. void notify(snac *snac, const char *type, const char *utype, const char *actor, const xs_dict *msg)
  420. /* notifies the user of relevant events */
  421. {
  422. const char *id = xs_dict_get(msg, "id");
  423. if (strcmp(type, "Create") == 0) {
  424. /* only notify of notes specifically for us */
  425. xs *rcpts = recipient_list(snac, msg, 0);
  426. if (xs_list_in(rcpts, snac->actor) == -1)
  427. return;
  428. /* discard votes */
  429. const xs_dict *note = xs_dict_get(msg, "object");
  430. if (note && !xs_is_null(xs_dict_get(note, "name")))
  431. return;
  432. }
  433. if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") != 0)
  434. return;
  435. /* get the object id */
  436. const char *objid = xs_dict_get(msg, "object");
  437. if (xs_type(objid) == XSTYPE_DICT)
  438. objid = xs_dict_get(objid, "id");
  439. if (strcmp(type, "Like") == 0 || strcmp(type, "Announce") == 0) {
  440. /* if it's not an admiration about something by us, done */
  441. if (xs_is_null(objid) || !xs_startswith(objid, snac->actor))
  442. return;
  443. }
  444. /* updated poll? */
  445. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0) {
  446. const xs_dict *poll;
  447. const char *poll_id;
  448. if ((poll = xs_dict_get(msg, "object")) == NULL)
  449. return;
  450. /* if it's not closed, discard */
  451. if (xs_is_null(xs_dict_get(poll, "closed")))
  452. return;
  453. if ((poll_id = xs_dict_get(poll, "id")) == NULL)
  454. return;
  455. /* if it's not ours and we didn't vote, discard */
  456. if (!xs_startswith(poll_id, snac->actor) && !was_question_voted(snac, poll_id))
  457. return;
  458. }
  459. /* user will love to know about this! */
  460. /* prepare message body */
  461. xs *body = xs_fmt("User : @%s@%s\n",
  462. xs_dict_get(snac->config, "uid"),
  463. xs_dict_get(srv_config, "host")
  464. );
  465. if (strcmp(utype, "(null)") != 0) {
  466. xs *s1 = xs_fmt("Type : %s + %s\n", type, utype);
  467. body = xs_str_cat(body, s1);
  468. }
  469. else {
  470. xs *s1 = xs_fmt("Type : %s\n", type);
  471. body = xs_str_cat(body, s1);
  472. }
  473. {
  474. xs *s1 = xs_fmt("Actor : %s\n", actor);
  475. body = xs_str_cat(body, s1);
  476. }
  477. if (objid != NULL) {
  478. xs *s1 = xs_fmt("Object: %s\n", objid);
  479. body = xs_str_cat(body, s1);
  480. }
  481. /* email */
  482. const char *email = "[disabled by admin]";
  483. if (xs_type(xs_dict_get(srv_config, "disable_email_notifications")) != XSTYPE_TRUE) {
  484. email = xs_dict_get(snac->config_o, "email");
  485. if (xs_is_null(email)) {
  486. email = xs_dict_get(snac->config, "email");
  487. if (xs_is_null(email))
  488. email = "[empty]";
  489. }
  490. }
  491. if (*email != '\0' && *email != '[') {
  492. snac_debug(snac, 1, xs_fmt("email notify %s %s %s", type, utype, actor));
  493. xs *subject = xs_fmt("snac notify for @%s@%s",
  494. xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"));
  495. xs *from = xs_fmt("snac-daemon <snac-daemon@%s>", xs_dict_get(srv_config, "host"));
  496. xs *header = xs_fmt(
  497. "From: %s\n"
  498. "To: %s\n"
  499. "Subject: %s\n"
  500. "\n",
  501. from, email, subject);
  502. xs *email_body = xs_fmt("%s%s", header, body);
  503. enqueue_email(email_body, 0);
  504. }
  505. /* telegram */
  506. char *bot = xs_dict_get(snac->config, "telegram_bot");
  507. char *chat_id = xs_dict_get(snac->config, "telegram_chat_id");
  508. if (!xs_is_null(bot) && !xs_is_null(chat_id) && *bot && *chat_id)
  509. enqueue_telegram(body, bot, chat_id);
  510. /* finally, store it in the notification folder */
  511. if (strcmp(type, "Follow") == 0)
  512. objid = id;
  513. notify_add(snac, type, utype, actor, objid != NULL ? objid : id);
  514. }
  515. /** messages **/
  516. xs_dict *msg_base(snac *snac, const char *type, const char *id,
  517. const char *actor, const char *date, const char *object)
  518. /* creates a base ActivityPub message */
  519. {
  520. xs *did = NULL;
  521. xs *published = NULL;
  522. xs *ntid = tid(0);
  523. /* generated values */
  524. if (date && strcmp(date, "@now") == 0) {
  525. published = xs_str_utctime(0, ISO_DATE_SPEC);
  526. date = published;
  527. }
  528. if (id != NULL) {
  529. if (strcmp(id, "@dummy") == 0) {
  530. did = xs_fmt("%s/d/%s/%s", snac->actor, ntid, type);
  531. id = did;
  532. }
  533. else
  534. if (strcmp(id, "@object") == 0) {
  535. if (object != NULL) {
  536. did = xs_fmt("%s/%s_%s", xs_dict_get(object, "id"), type, ntid);
  537. id = did;
  538. }
  539. else
  540. id = NULL;
  541. }
  542. }
  543. xs_dict *msg = xs_dict_new();
  544. msg = xs_dict_append(msg, "@context", "https:/" "/www.w3.org/ns/activitystreams");
  545. msg = xs_dict_append(msg, "type", type);
  546. if (id != NULL)
  547. msg = xs_dict_append(msg, "id", id);
  548. if (actor != NULL)
  549. msg = xs_dict_append(msg, "actor", actor);
  550. if (date != NULL)
  551. msg = xs_dict_append(msg, "published", date);
  552. if (object != NULL)
  553. msg = xs_dict_append(msg, "object", object);
  554. return msg;
  555. }
  556. xs_dict *msg_collection(snac *snac, char *id)
  557. /* creates an empty OrderedCollection message */
  558. {
  559. xs_dict *msg = msg_base(snac, "OrderedCollection", id, NULL, NULL, NULL);
  560. xs *ol = xs_list_new();
  561. xs *nz = xs_number_new(0);
  562. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  563. msg = xs_dict_append(msg, "orderedItems", ol);
  564. msg = xs_dict_append(msg, "totalItems", nz);
  565. return msg;
  566. }
  567. xs_dict *msg_accept(snac *snac, char *object, char *to)
  568. /* creates an Accept message (as a response to a Follow) */
  569. {
  570. xs_dict *msg = msg_base(snac, "Accept", "@dummy", snac->actor, NULL, object);
  571. msg = xs_dict_append(msg, "to", to);
  572. return msg;
  573. }
  574. xs_dict *msg_update(snac *snac, xs_dict *object)
  575. /* creates an Update message */
  576. {
  577. xs_dict *msg = msg_base(snac, "Update", "@object", snac->actor, "@now", object);
  578. char *type = xs_dict_get(object, "type");
  579. if (strcmp(type, "Note") == 0) {
  580. msg = xs_dict_append(msg, "to", xs_dict_get(object, "to"));
  581. msg = xs_dict_append(msg, "cc", xs_dict_get(object, "cc"));
  582. }
  583. else
  584. if (strcmp(type, "Person") == 0) {
  585. msg = xs_dict_append(msg, "to", public_address);
  586. /* also spam the people being followed, so that
  587. they have the newest information about who we are */
  588. xs *cc = following_list(snac);
  589. msg = xs_dict_append(msg, "cc", cc);
  590. }
  591. else
  592. msg = xs_dict_append(msg, "to", public_address);
  593. return msg;
  594. }
  595. xs_dict *msg_admiration(snac *snac, char *object, char *type)
  596. /* creates a Like or Announce message */
  597. {
  598. xs *a_msg = NULL;
  599. xs_dict *msg = NULL;
  600. xs *wrk = NULL;
  601. /* call the object */
  602. timeline_request(snac, &object, &wrk);
  603. if (valid_status(object_get(object, &a_msg))) {
  604. xs *rcpts = xs_list_new();
  605. msg = msg_base(snac, type, "@dummy", snac->actor, "@now", object);
  606. if (is_msg_public(snac, a_msg))
  607. rcpts = xs_list_append(rcpts, public_address);
  608. rcpts = xs_list_append(rcpts, xs_dict_get(a_msg, "attributedTo"));
  609. msg = xs_dict_append(msg, "to", rcpts);
  610. }
  611. else
  612. snac_log(snac, xs_fmt("msg_admiration cannot retrieve object %s", object));
  613. return msg;
  614. }
  615. xs_dict *msg_actor(snac *snac)
  616. /* create a Person message for this actor */
  617. {
  618. xs *ctxt = xs_list_new();
  619. xs *icon = xs_dict_new();
  620. xs *keys = xs_dict_new();
  621. xs *avtr = NULL;
  622. xs *kid = NULL;
  623. xs *f_bio = NULL;
  624. xs_dict *msg = msg_base(snac, "Person", snac->actor, NULL, NULL, NULL);
  625. char *p;
  626. int n;
  627. /* change the @context (is this really necessary?) */
  628. ctxt = xs_list_append(ctxt, "https:/" "/www.w3.org/ns/activitystreams");
  629. ctxt = xs_list_append(ctxt, "https:/" "/w3id.org/security/v1");
  630. msg = xs_dict_set(msg, "@context", ctxt);
  631. msg = xs_dict_set(msg, "url", snac->actor);
  632. msg = xs_dict_set(msg, "name", xs_dict_get(snac->config, "name"));
  633. msg = xs_dict_set(msg, "preferredUsername", snac->uid);
  634. msg = xs_dict_set(msg, "published", xs_dict_get(snac->config, "published"));
  635. f_bio = not_really_markdown(xs_dict_get(snac->config, "bio"), NULL);
  636. msg = xs_dict_set(msg, "summary", f_bio);
  637. char *folders[] = { "inbox", "outbox", "followers", "following", NULL };
  638. for (n = 0; folders[n]; n++) {
  639. xs *f = xs_fmt("%s/%s", snac->actor, folders[n]);
  640. msg = xs_dict_set(msg, folders[n], f);
  641. }
  642. p = xs_dict_get(snac->config, "avatar");
  643. if (*p == '\0')
  644. avtr = xs_fmt("%s/susie.png", srv_baseurl);
  645. else
  646. avtr = xs_dup(p);
  647. icon = xs_dict_append(icon, "type", "Image");
  648. icon = xs_dict_append(icon, "mediaType", xs_mime_by_ext(avtr));
  649. icon = xs_dict_append(icon, "url", avtr);
  650. msg = xs_dict_set(msg, "icon", icon);
  651. kid = xs_fmt("%s#main-key", snac->actor);
  652. keys = xs_dict_append(keys, "id", kid);
  653. keys = xs_dict_append(keys, "owner", snac->actor);
  654. keys = xs_dict_append(keys, "publicKeyPem", xs_dict_get(snac->key, "public"));
  655. msg = xs_dict_set(msg, "publicKey", keys);
  656. /* if the "bot" config field is set to true, change type to "Service" */
  657. if (xs_type(xs_dict_get(snac->config, "bot")) == XSTYPE_TRUE)
  658. msg = xs_dict_set(msg, "type", "Service");
  659. return msg;
  660. }
  661. xs_dict *msg_create(snac *snac, const xs_dict *object)
  662. /* creates a 'Create' message */
  663. {
  664. xs_dict *msg = msg_base(snac, "Create", "@object", snac->actor, "@now", object);
  665. xs_val *v;
  666. if ((v = xs_dict_get(object, "attributedTo")))
  667. msg = xs_dict_append(msg, "attributedTo", v);
  668. if ((v = xs_dict_get(object, "cc")))
  669. msg = xs_dict_append(msg, "cc", v);
  670. if ((v = xs_dict_get(object, "to")))
  671. msg = xs_dict_append(msg, "to", v);
  672. else
  673. msg = xs_dict_append(msg, "to", public_address);
  674. return msg;
  675. }
  676. xs_dict *msg_undo(snac *snac, char *object)
  677. /* creates an 'Undo' message */
  678. {
  679. xs_dict *msg = msg_base(snac, "Undo", "@object", snac->actor, "@now", object);
  680. msg = xs_dict_append(msg, "to", xs_dict_get(object, "object"));
  681. return msg;
  682. }
  683. xs_dict *msg_delete(snac *snac, char *id)
  684. /* creates a 'Delete' + 'Tombstone' for a local entry */
  685. {
  686. xs *tomb = xs_dict_new();
  687. xs_dict *msg = NULL;
  688. /* sculpt the tombstone */
  689. tomb = xs_dict_append(tomb, "type", "Tombstone");
  690. tomb = xs_dict_append(tomb, "id", id);
  691. /* now create the Delete */
  692. msg = msg_base(snac, "Delete", "@object", snac->actor, "@now", tomb);
  693. msg = xs_dict_append(msg, "to", public_address);
  694. return msg;
  695. }
  696. xs_dict *msg_follow(snac *snac, const char *q)
  697. /* creates a 'Follow' message */
  698. {
  699. xs *actor_o = NULL;
  700. xs *actor = NULL;
  701. xs_dict *msg = NULL;
  702. int status;
  703. xs *url_or_uid = xs_strip_i(xs_str_new(q));
  704. if (xs_startswith(url_or_uid, "https:/"))
  705. actor = xs_dup(url_or_uid);
  706. else
  707. if (!valid_status(webfinger_request(url_or_uid, &actor, NULL)) || actor == NULL) {
  708. snac_log(snac, xs_fmt("cannot resolve user %s to follow", url_or_uid));
  709. return NULL;
  710. }
  711. /* request the actor */
  712. status = actor_request(snac, actor, &actor_o);
  713. if (valid_status(status)) {
  714. /* check if the actor is an alias */
  715. char *r_actor = xs_dict_get(actor_o, "id");
  716. if (r_actor && strcmp(actor, r_actor) != 0) {
  717. snac_log(snac, xs_fmt("actor to follow is an alias %s -> %s", actor, r_actor));
  718. }
  719. msg = msg_base(snac, "Follow", "@dummy", snac->actor, NULL, r_actor);
  720. }
  721. else
  722. snac_log(snac, xs_fmt("cannot get actor to follow %s %d", actor, status));
  723. return msg;
  724. }
  725. xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts,
  726. xs_str *in_reply_to, xs_list *attach, int priv)
  727. /* creates a 'Note' message */
  728. {
  729. xs *ntid = tid(0);
  730. xs *id = xs_fmt("%s/p/%s", snac->actor, ntid);
  731. xs *ctxt = NULL;
  732. xs *fc2 = NULL;
  733. xs *fc1 = NULL;
  734. xs *to = NULL;
  735. xs *cc = xs_list_new();
  736. xs *irt = NULL;
  737. xs *tag = xs_list_new();
  738. xs *atls = xs_list_new();
  739. xs_dict *msg = msg_base(snac, "Note", id, NULL, "@now", NULL);
  740. xs_list *p;
  741. xs_val *v;
  742. if (rcpts == NULL)
  743. to = xs_list_new();
  744. else {
  745. if (xs_type(rcpts) == XSTYPE_STRING) {
  746. to = xs_list_new();
  747. to = xs_list_append(to, rcpts);
  748. }
  749. else
  750. to = xs_dup(rcpts);
  751. }
  752. /* format the content */
  753. fc2 = not_really_markdown(content, &atls);
  754. if (in_reply_to != NULL && *in_reply_to) {
  755. xs *p_msg = NULL;
  756. xs *wrk = NULL;
  757. /* demand this thing */
  758. timeline_request(snac, &in_reply_to, &wrk);
  759. if (valid_status(object_get(in_reply_to, &p_msg))) {
  760. /* add this author as recipient */
  761. char *a, *v;
  762. if ((a = xs_dict_get(p_msg, "attributedTo")) && xs_list_in(to, a) == -1)
  763. to = xs_list_append(to, a);
  764. /* add this author to the tag list as a mention */
  765. xs *t_href = NULL;
  766. xs *t_name = NULL;
  767. if (!xs_is_null(a) && valid_status(webfinger_request(a, &t_href, &t_name))) {
  768. xs *t = xs_dict_new();
  769. t = xs_dict_append(t, "type", "Mention");
  770. t = xs_dict_append(t, "href", t_href);
  771. t = xs_dict_append(t, "name", t_name);
  772. tag = xs_list_append(tag, t);
  773. }
  774. /* get the context, if there is one */
  775. if ((v = xs_dict_get(p_msg, "context")))
  776. ctxt = xs_dup(v);
  777. /* if this message is public, ours will also be */
  778. if (!priv && is_msg_public(snac, p_msg) && xs_list_in(to, public_address) == -1)
  779. to = xs_list_append(to, public_address);
  780. }
  781. irt = xs_dup(in_reply_to);
  782. }
  783. else
  784. irt = xs_val_new(XSTYPE_NULL);
  785. /* extract the mentions and hashtags and convert the content */
  786. process_tags(snac, fc2, &fc1, &tag);
  787. /* create the attachment list, if there are any */
  788. if (!xs_is_null(attach)) {
  789. while (xs_list_iter(&attach, &v)) {
  790. xs *d = xs_dict_new();
  791. char *url = xs_list_get(v, 0);
  792. char *alt = xs_list_get(v, 1);
  793. char *mime = xs_mime_by_ext(url);
  794. d = xs_dict_append(d, "mediaType", mime);
  795. d = xs_dict_append(d, "url", url);
  796. d = xs_dict_append(d, "name", alt);
  797. d = xs_dict_append(d, "type",
  798. xs_startswith(mime, "image/") ? "Image" : "Document");
  799. atls = xs_list_append(atls, d);
  800. }
  801. }
  802. if (ctxt == NULL)
  803. ctxt = xs_fmt("%s#ctxt", id);
  804. /* add all mentions to the cc */
  805. p = tag;
  806. while (xs_list_iter(&p, &v)) {
  807. if (xs_type(v) == XSTYPE_DICT) {
  808. char *t;
  809. if ((t = xs_dict_get(v, "type")) != NULL && strcmp(t, "Mention") == 0) {
  810. if ((t = xs_dict_get(v, "href")) != NULL)
  811. cc = xs_list_append(cc, t);
  812. }
  813. }
  814. }
  815. /* no recipients? must be for everybody */
  816. if (!priv && xs_list_len(to) == 0)
  817. to = xs_list_append(to, public_address);
  818. /* delete all cc recipients that also are in the to */
  819. p = to;
  820. while (xs_list_iter(&p, &v)) {
  821. int i;
  822. if ((i = xs_list_in(cc, v)) != -1)
  823. cc = xs_list_del(cc, i);
  824. }
  825. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  826. msg = xs_dict_append(msg, "summary", "");
  827. msg = xs_dict_append(msg, "content", fc1);
  828. msg = xs_dict_append(msg, "context", ctxt);
  829. msg = xs_dict_append(msg, "url", id);
  830. msg = xs_dict_append(msg, "to", to);
  831. msg = xs_dict_append(msg, "cc", cc);
  832. msg = xs_dict_append(msg, "inReplyTo", irt);
  833. msg = xs_dict_append(msg, "tag", tag);
  834. msg = xs_dict_append(msg, "sourceContent", content);
  835. if (xs_list_len(atls))
  836. msg = xs_dict_append(msg, "attachment", atls);
  837. return msg;
  838. }
  839. xs_dict *msg_ping(snac *user, const char *rcpt)
  840. /* creates a Ping message (https://humungus.tedunangst.com/r/honk/v/tip/f/docs/ping.txt) */
  841. {
  842. xs_dict *msg = msg_base(user, "Ping", "@dummy", user->actor, NULL, NULL);
  843. msg = xs_dict_append(msg, "to", rcpt);
  844. return msg;
  845. }
  846. xs_dict *msg_pong(snac *user, const char *rcpt, const char *object)
  847. /* creates a Pong message (https://humungus.tedunangst.com/r/honk/v/tip/f/docs/ping.txt) */
  848. {
  849. xs_dict *msg = msg_base(user, "Pong", "@dummy", user->actor, NULL, object);
  850. msg = xs_dict_append(msg, "to", rcpt);
  851. return msg;
  852. }
  853. xs_dict *msg_question(snac *user, const char *content, xs_list *attach,
  854. const xs_list *opts, int multiple, int end_secs)
  855. /* creates a Question message */
  856. {
  857. xs *ntid = tid(0);
  858. xs_dict *msg = msg_note(user, content, NULL, NULL, attach, 0);
  859. int max = 8;
  860. xs_set seen;
  861. msg = xs_dict_set(msg, "type", "Question");
  862. /* make it non-editable */
  863. msg = xs_dict_del(msg, "sourceContent");
  864. xs *o = xs_list_new();
  865. xs_list *p = (xs_list *)opts;
  866. xs_str *v;
  867. xs *replies = xs_json_loads("{\"type\":\"Collection\",\"totalItems\":0}");
  868. xs_set_init(&seen);
  869. while (max && xs_list_iter(&p, &v)) {
  870. if (*v) {
  871. xs *v2 = xs_dup(v);
  872. xs *d = xs_dict_new();
  873. if (strlen(v2) > 60) {
  874. v2[60] = '\0';
  875. v2 = xs_str_cat(v2, "...");
  876. }
  877. if (xs_set_add(&seen, v2) == 1) {
  878. d = xs_dict_append(d, "name", v2);
  879. d = xs_dict_append(d, "replies", replies);
  880. o = xs_list_append(o, d);
  881. max--;
  882. }
  883. }
  884. }
  885. xs_set_free(&seen);
  886. msg = xs_dict_append(msg, multiple ? "anyOf" : "oneOf", o);
  887. /* set the end time */
  888. time_t t = time(NULL) + end_secs;
  889. xs *et = xs_str_utctime(t, ISO_DATE_SPEC);
  890. msg = xs_dict_append(msg, "endTime", et);
  891. return msg;
  892. }
  893. int update_question(snac *user, const char *id)
  894. /* updates the poll counts */
  895. {
  896. xs *msg = NULL;
  897. xs *rcnt = xs_dict_new();
  898. xs *z = xs_number_new(0);
  899. xs *rcpts = xs_list_new();
  900. xs *lopts = xs_list_new();
  901. xs_list *opts;
  902. xs_list *p;
  903. xs_val *v;
  904. /* get the object */
  905. if (!valid_status(object_get(id, &msg)))
  906. return -1;
  907. /* closed? do nothing more */
  908. if (xs_dict_get(msg, "closed"))
  909. return -2;
  910. /* get the options */
  911. if ((opts = xs_dict_get(msg, "oneOf")) == NULL &&
  912. (opts = xs_dict_get(msg, "anyOf")) == NULL)
  913. return -3;
  914. /* fill the initial count */
  915. p = opts;
  916. while (xs_list_iter(&p, &v)) {
  917. const char *name = xs_dict_get(v, "name");
  918. if (name) {
  919. lopts = xs_list_append(lopts, name);
  920. rcnt = xs_dict_set(rcnt, name, z);
  921. }
  922. }
  923. /* iterate now the children (the votes) */
  924. xs *chld = object_children(id);
  925. p = chld;
  926. while (xs_list_iter(&p, &v)) {
  927. xs *obj = NULL;
  928. if (!valid_status(object_get_by_md5(v, &obj)))
  929. continue;
  930. const char *name = xs_dict_get(obj, "name");
  931. const char *atto = xs_dict_get(obj, "attributedTo");
  932. if (name && atto) {
  933. /* get the current count */
  934. const xs_number *cnt = xs_dict_get(rcnt, name);
  935. if (xs_type(cnt) == XSTYPE_NUMBER) {
  936. /* if it exists, increment */
  937. xs *ucnt = xs_number_new(xs_number_get(cnt) + 1);
  938. rcnt = xs_dict_set(rcnt, name, ucnt);
  939. rcpts = xs_list_append(rcpts, atto);
  940. }
  941. }
  942. }
  943. /* create a new list of options with their new counts */
  944. xs *nopts = xs_list_new();
  945. p = lopts;
  946. while (xs_list_iter(&p, &v)) {
  947. const xs_number *cnt = xs_dict_get(rcnt, v);
  948. if (xs_type(cnt) == XSTYPE_NUMBER) {
  949. xs *d1 = xs_dict_new();
  950. xs *d2 = xs_dict_new();
  951. d2 = xs_dict_append(d2, "type", "Collection");
  952. d2 = xs_dict_append(d2, "totalItems", cnt);
  953. d1 = xs_dict_append(d1, "type", "Note");
  954. d1 = xs_dict_append(d1, "name", v);
  955. d1 = xs_dict_append(d1, "replies", d2);
  956. nopts = xs_list_append(nopts, d1);
  957. }
  958. }
  959. /* update the list */
  960. msg = xs_dict_set(msg, xs_dict_get(msg, "oneOf") != NULL ? "oneOf" : "anyOf", nopts);
  961. /* due date? */
  962. int closed = 0;
  963. const char *end_time = xs_dict_get(msg, "endTime");
  964. if (!xs_is_null(end_time)) {
  965. xs *now = xs_str_utctime(0, ISO_DATE_SPEC);
  966. /* is now greater than the endTime? */
  967. if (strcmp(now, end_time) >= 0) {
  968. xs *et = xs_dup(end_time);
  969. msg = xs_dict_set(msg, "closed", et);
  970. closed = 1;
  971. }
  972. }
  973. /* update the count of voters */
  974. xs *vcnt = xs_number_new(xs_list_len(rcpts));
  975. msg = xs_dict_set(msg, "votersCount", vcnt);
  976. /* store */
  977. object_add_ow(id, msg);
  978. snac_debug(user, 1, xs_fmt("recounted poll %s", id));
  979. timeline_touch(user);
  980. /* send an update message to all voters */
  981. xs *u_msg = msg_update(user, msg);
  982. u_msg = xs_dict_set(u_msg, "cc", rcpts);
  983. enqueue_message(user, u_msg);
  984. if (closed) {
  985. xs *c_msg = msg_update(user, msg);
  986. notify(user, "Update", "Question", user->actor, c_msg);
  987. }
  988. return 0;
  989. }
  990. /** queues **/
  991. int process_input_message(snac *snac, xs_dict *msg, xs_dict *req)
  992. /* processes an ActivityPub message from the input queue */
  993. {
  994. /* actor and type exist, were checked previously */
  995. char *actor = xs_dict_get(msg, "actor");
  996. char *type = xs_dict_get(msg, "type");
  997. xs *actor_o = NULL;
  998. int a_status;
  999. int do_notify = 0;
  1000. /* question votes may not have a type */
  1001. if (xs_is_null(type))
  1002. type = "Note";
  1003. if (xs_is_null(actor)) {
  1004. snac_debug(snac, 0, xs_fmt("malformed message"));
  1005. return 1;
  1006. }
  1007. char *object, *utype;
  1008. object = xs_dict_get(msg, "object");
  1009. if (object != NULL && xs_type(object) == XSTYPE_DICT)
  1010. utype = xs_dict_get(object, "type");
  1011. else
  1012. utype = "(null)";
  1013. /* reject messages that are not for this user */
  1014. if (!is_msg_for_me(snac, msg)) {
  1015. snac_debug(snac, 0, xs_fmt("message from %s of type '%s' not for us", actor, type));
  1016. return 1;
  1017. }
  1018. /* if it's a DM from someone we don't follow, reject the message */
  1019. if (xs_type(xs_dict_get(snac->config, "drop_dm_from_unknown")) == XSTYPE_TRUE) {
  1020. if (strcmp(utype, "Note") == 0 && !is_msg_public(snac, msg) &&
  1021. !following_check(snac, actor)) {
  1022. snac_log(snac, xs_fmt("DM rejected from unknown actor %s", actor));
  1023. return 1;
  1024. }
  1025. }
  1026. /* bring the actor */
  1027. a_status = actor_request(snac, actor, &actor_o);
  1028. /* if the actor does not explicitly exist, discard */
  1029. if (a_status == 404 || a_status == 410) {
  1030. snac_debug(snac, 1,
  1031. xs_fmt("dropping message due to actor error %s %d", actor, a_status));
  1032. return 1;
  1033. }
  1034. if (!valid_status(a_status)) {
  1035. /* other actor download errors may need a retry */
  1036. snac_debug(snac, 1,
  1037. xs_fmt("error requesting actor %s %d -- retry later", actor, a_status));
  1038. return 0;
  1039. }
  1040. /* check the signature */
  1041. xs *sig_err = NULL;
  1042. if (!check_signature(snac, req, &sig_err)) {
  1043. snac_log(snac, xs_fmt("bad signature %s (%s)", actor, sig_err));
  1044. srv_archive_error("check_signature", sig_err, req, msg);
  1045. return 1;
  1046. }
  1047. if (strcmp(type, "Follow") == 0) { /** **/
  1048. if (!follower_check(snac, actor)) {
  1049. xs *f_msg = xs_dup(msg);
  1050. xs *reply = msg_accept(snac, f_msg, actor);
  1051. enqueue_message(snac, reply);
  1052. if (xs_is_null(xs_dict_get(f_msg, "published"))) {
  1053. /* add a date if it doesn't include one (Mastodon) */
  1054. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  1055. f_msg = xs_dict_set(f_msg, "published", date);
  1056. }
  1057. timeline_add(snac, xs_dict_get(f_msg, "id"), f_msg);
  1058. follower_add(snac, actor);
  1059. snac_log(snac, xs_fmt("new follower %s", actor));
  1060. do_notify = 1;
  1061. }
  1062. else
  1063. snac_log(snac, xs_fmt("repeated 'Follow' from %s", actor));
  1064. }
  1065. else
  1066. if (strcmp(type, "Undo") == 0) { /** **/
  1067. if (strcmp(utype, "Follow") == 0) { /** **/
  1068. if (valid_status(follower_del(snac, actor))) {
  1069. snac_log(snac, xs_fmt("no longer following us %s", actor));
  1070. do_notify = 1;
  1071. }
  1072. else
  1073. snac_log(snac, xs_fmt("error deleting follower %s", actor));
  1074. }
  1075. else
  1076. snac_debug(snac, 1, xs_fmt("ignored 'Undo' for object type '%s'", utype));
  1077. }
  1078. else
  1079. if (strcmp(type, "Create") == 0) { /** **/
  1080. if (is_muted(snac, actor))
  1081. snac_log(snac, xs_fmt("ignored 'Create' + '%s' from muted actor %s", utype, actor));
  1082. if (strcmp(utype, "Note") == 0) { /** **/
  1083. char *id = xs_dict_get(object, "id");
  1084. char *in_reply_to = xs_dict_get(object, "inReplyTo");
  1085. xs *wrk = NULL;
  1086. timeline_request(snac, &in_reply_to, &wrk);
  1087. if (timeline_add(snac, id, object)) {
  1088. snac_log(snac, xs_fmt("new 'Note' %s %s", actor, id));
  1089. do_notify = 1;
  1090. }
  1091. /* if it has a "name" field, it may be a vote for a question */
  1092. const char *name = xs_dict_get(object, "name");
  1093. if (!xs_is_null(name) && *name && !xs_is_null(in_reply_to) && *in_reply_to)
  1094. update_question(snac, in_reply_to);
  1095. }
  1096. else
  1097. if (strcmp(utype, "Question") == 0) { /** **/
  1098. char *id = xs_dict_get(object, "id");
  1099. if (timeline_add(snac, id, object))
  1100. snac_log(snac, xs_fmt("new 'Question' %s %s", actor, id));
  1101. }
  1102. else
  1103. snac_debug(snac, 1, xs_fmt("ignored 'Create' for object type '%s'", utype));
  1104. }
  1105. else
  1106. if (strcmp(type, "Accept") == 0) { /** **/
  1107. if (strcmp(utype, "Follow") == 0) { /** **/
  1108. if (following_check(snac, actor)) {
  1109. following_add(snac, actor, msg);
  1110. snac_log(snac, xs_fmt("confirmed follow from %s", actor));
  1111. }
  1112. else
  1113. snac_log(snac, xs_fmt("spurious follow accept from %s", actor));
  1114. }
  1115. else
  1116. snac_debug(snac, 1, xs_fmt("ignored 'Accept' for object type '%s'", utype));
  1117. }
  1118. else
  1119. if (strcmp(type, "Like") == 0) { /** **/
  1120. if (xs_type(object) == XSTYPE_DICT)
  1121. object = xs_dict_get(object, "id");
  1122. timeline_admire(snac, object, actor, 1);
  1123. snac_log(snac, xs_fmt("new 'Like' %s %s", actor, object));
  1124. do_notify = 1;
  1125. }
  1126. else
  1127. if (strcmp(type, "Announce") == 0) { /** **/
  1128. xs *a_msg = NULL;
  1129. xs *wrk = NULL;
  1130. if (xs_type(object) == XSTYPE_DICT)
  1131. object = xs_dict_get(object, "id");
  1132. timeline_request(snac, &object, &wrk);
  1133. if (valid_status(object_get(object, &a_msg))) {
  1134. char *who = xs_dict_get(a_msg, "attributedTo");
  1135. if (who && !is_muted(snac, who)) {
  1136. /* bring the actor */
  1137. xs *who_o = NULL;
  1138. if (valid_status(actor_request(snac, who, &who_o))) {
  1139. timeline_admire(snac, object, actor, 0);
  1140. snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object));
  1141. do_notify = 1;
  1142. }
  1143. else
  1144. snac_log(snac, xs_fmt("dropped 'Announce' on actor request error %s", who));
  1145. }
  1146. else
  1147. snac_log(snac, xs_fmt("ignored 'Announce' about muted actor %s", who));
  1148. }
  1149. else
  1150. snac_log(snac, xs_fmt("error requesting 'Announce' object %s", object));
  1151. }
  1152. else
  1153. if (strcmp(type, "Update") == 0) { /** **/
  1154. if (strcmp(utype, "Person") == 0 || strcmp(utype, "Service") == 0) {
  1155. actor_add(actor, xs_dict_get(msg, "object"));
  1156. timeline_touch(snac);
  1157. snac_log(snac, xs_fmt("updated actor %s", actor));
  1158. }
  1159. else
  1160. if (strcmp(utype, "Note") == 0) { /** **/
  1161. const char *id = xs_dict_get(object, "id");
  1162. object_add_ow(id, object);
  1163. timeline_touch(snac);
  1164. snac_log(snac, xs_fmt("updated post %s", id));
  1165. }
  1166. else
  1167. if (strcmp(utype, "Question") == 0) { /** **/
  1168. const char *id = xs_dict_get(object, "id");
  1169. const char *closed = xs_dict_get(object, "closed");
  1170. object_add_ow(id, object);
  1171. timeline_touch(snac);
  1172. snac_log(snac, xs_fmt("%s poll %s", closed == NULL ? "updated" : "closed", id));
  1173. if (closed != NULL)
  1174. do_notify = 1;
  1175. }
  1176. else
  1177. snac_log(snac, xs_fmt("ignored 'Update' for object type '%s'", utype));
  1178. }
  1179. else
  1180. if (strcmp(type, "Delete") == 0) { /** **/
  1181. if (xs_type(object) == XSTYPE_DICT)
  1182. object = xs_dict_get(object, "id");
  1183. if (valid_status(timeline_del(snac, object)))
  1184. snac_debug(snac, 1, xs_fmt("new 'Delete' %s %s", actor, object));
  1185. else
  1186. snac_debug(snac, 1, xs_fmt("ignored 'Delete' for unknown object %s", object));
  1187. }
  1188. else
  1189. if (strcmp(type, "Pong") == 0) { /** **/
  1190. snac_log(snac, xs_fmt("'Pong' received from %s", actor));
  1191. }
  1192. else
  1193. if (strcmp(type, "Ping") == 0) { /** **/
  1194. snac_log(snac, xs_fmt("'Ping' requested from %s", actor));
  1195. xs *rsp = msg_pong(snac, actor, xs_dict_get(msg, "id"));
  1196. enqueue_output_by_actor(snac, rsp, actor, 0);
  1197. }
  1198. else
  1199. snac_debug(snac, 1, xs_fmt("process_input_message type '%s' ignored", type));
  1200. if (do_notify) {
  1201. notify(snac, type, utype, actor, msg);
  1202. timeline_touch(snac);
  1203. }
  1204. return 1;
  1205. }
  1206. int send_email(char *msg)
  1207. /* invoke sendmail with email headers and body in msg */
  1208. {
  1209. FILE *f;
  1210. int status;
  1211. int fds[2];
  1212. pid_t pid;
  1213. if (pipe(fds) == -1) return -1;
  1214. pid = vfork();
  1215. if (pid == -1) return -1;
  1216. else if (pid == 0) {
  1217. dup2(fds[0], 0);
  1218. close(fds[0]);
  1219. close(fds[1]);
  1220. execl("/usr/sbin/sendmail", "sendmail", "-t", (char *) NULL);
  1221. _exit(1);
  1222. }
  1223. close(fds[0]);
  1224. if ((f = fdopen(fds[1], "w")) == NULL) {
  1225. close(fds[1]);
  1226. return -1;
  1227. }
  1228. fprintf(f, "%s\n", msg);
  1229. fclose(f);
  1230. if (waitpid(pid, &status, 0) == -1) return -1;
  1231. return status;
  1232. }
  1233. void process_user_queue_item(snac *snac, xs_dict *q_item)
  1234. /* processes an item from the user queue */
  1235. {
  1236. char *type;
  1237. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  1238. if ((type = xs_dict_get(q_item, "type")) == NULL)
  1239. type = "output";
  1240. if (strcmp(type, "message") == 0) {
  1241. xs_dict *msg = xs_dict_get(q_item, "message");
  1242. xs *rcpts = recipient_list(snac, msg, 1);
  1243. xs_set inboxes;
  1244. xs_list *p;
  1245. xs_str *actor;
  1246. xs_set_init(&inboxes);
  1247. /* iterate the recipients */
  1248. p = rcpts;
  1249. while (xs_list_iter(&p, &actor)) {
  1250. xs *inbox = get_actor_inbox(snac, actor);
  1251. if (inbox != NULL) {
  1252. /* add to the set and, if it's not there, send message */
  1253. if (xs_set_add(&inboxes, inbox) == 1)
  1254. enqueue_output(snac, msg, inbox, 0);
  1255. }
  1256. else
  1257. snac_log(snac, xs_fmt("cannot find inbox for %s", actor));
  1258. }
  1259. /* if it's public, send to the collected inboxes */
  1260. if (is_msg_public(snac, msg)) {
  1261. xs *shibx = inbox_list();
  1262. xs_str *inbox;
  1263. p = shibx;
  1264. while (xs_list_iter(&p, &inbox)) {
  1265. if (xs_set_add(&inboxes, inbox) == 1)
  1266. enqueue_output(snac, msg, inbox, 0);
  1267. }
  1268. }
  1269. xs_set_free(&inboxes);
  1270. }
  1271. else
  1272. if (strcmp(type, "input") == 0) {
  1273. /* process the message */
  1274. xs_dict *msg = xs_dict_get(q_item, "message");
  1275. xs_dict *req = xs_dict_get(q_item, "req");
  1276. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  1277. if (xs_is_null(msg))
  1278. return;
  1279. if (!process_input_message(snac, msg, req)) {
  1280. if (retries > queue_retry_max)
  1281. snac_log(snac, xs_fmt("input giving up"));
  1282. else {
  1283. /* reenqueue */
  1284. enqueue_input(snac, msg, req, retries + 1);
  1285. snac_log(snac, xs_fmt("input requeue #%d", retries + 1));
  1286. }
  1287. }
  1288. }
  1289. else
  1290. if (strcmp(type, "close_question") == 0) {
  1291. /* the time for this question has ended */
  1292. const char *id = xs_dict_get(q_item, "message");
  1293. if (!xs_is_null(id))
  1294. update_question(snac, id);
  1295. }
  1296. else
  1297. if (strcmp(type, "request_replies") == 0) {
  1298. const char *id = xs_dict_get(q_item, "message");
  1299. if (!xs_is_null(id))
  1300. timeline_request_replies(snac, id);
  1301. }
  1302. else
  1303. snac_log(snac, xs_fmt("unexpected user q_item type '%s'", type));
  1304. }
  1305. int process_user_queue(snac *snac)
  1306. /* processes a user's queue */
  1307. {
  1308. int cnt = 0;
  1309. xs *list = user_queue(snac);
  1310. xs_list *p = list;
  1311. xs_str *fn;
  1312. while (xs_list_iter(&p, &fn)) {
  1313. xs *q_item = dequeue(fn);
  1314. if (q_item == NULL) {
  1315. snac_log(snac, xs_fmt("process_user_queue q_item error"));
  1316. continue;
  1317. }
  1318. process_user_queue_item(snac, q_item);
  1319. cnt++;
  1320. }
  1321. return cnt;
  1322. }
  1323. void process_queue_item(xs_dict *q_item)
  1324. /* processes an item from the global queue */
  1325. {
  1326. char *type = xs_dict_get(q_item, "type");
  1327. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  1328. if (strcmp(type, "output") == 0) {
  1329. int status;
  1330. xs_str *inbox = xs_dict_get(q_item, "inbox");
  1331. xs_str *keyid = xs_dict_get(q_item, "keyid");
  1332. xs_str *seckey = xs_dict_get(q_item, "seckey");
  1333. xs_dict *msg = xs_dict_get(q_item, "message");
  1334. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  1335. xs *payload = NULL;
  1336. int p_size = 0;
  1337. if (xs_is_null(inbox) || xs_is_null(msg) || xs_is_null(keyid) || xs_is_null(seckey)) {
  1338. srv_log(xs_fmt("output message error: missing fields"));
  1339. return;
  1340. }
  1341. /* deliver */
  1342. status = send_to_inbox_raw(keyid, seckey, inbox, msg, &payload, &p_size, retries == 0 ? 3 : 8);
  1343. if (payload) {
  1344. if (p_size > 64) {
  1345. /* trim the message */
  1346. payload[64] = '\0';
  1347. payload = xs_str_cat(payload, "...");
  1348. }
  1349. /* strip ugly control characters */
  1350. payload = xs_replace_i(payload, "\n", "");
  1351. payload = xs_replace_i(payload, "\r", "");
  1352. if (*payload)
  1353. payload = xs_str_wrap_i(" [", payload, "]");
  1354. }
  1355. else
  1356. payload = xs_str_new(NULL);
  1357. srv_log(xs_fmt("output message: sent to inbox %s %d%s", inbox, status, payload));
  1358. if (!valid_status(status)) {
  1359. retries++;
  1360. /* error sending; requeue? */
  1361. if (status == 404 || status == 410)
  1362. /* explicit error: discard */
  1363. srv_log(xs_fmt("output message: fatal error %s %d", inbox, status));
  1364. else
  1365. if (retries > queue_retry_max)
  1366. srv_log(xs_fmt("output message: giving up %s %d", inbox, status));
  1367. else {
  1368. /* requeue */
  1369. enqueue_output_raw(keyid, seckey, msg, inbox, retries);
  1370. srv_log(xs_fmt("output message: requeue %s #%d", inbox, retries));
  1371. }
  1372. }
  1373. }
  1374. else
  1375. if (strcmp(type, "email") == 0) {
  1376. /* send this email */
  1377. xs_str *msg = xs_dict_get(q_item, "message");
  1378. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  1379. if (!send_email(msg))
  1380. srv_debug(1, xs_fmt("email message sent"));
  1381. else {
  1382. retries++;
  1383. if (retries > queue_retry_max)
  1384. srv_log(xs_fmt("email giving up (errno: %d)", errno));
  1385. else {
  1386. /* requeue */
  1387. srv_log(xs_fmt(
  1388. "email requeue #%d (errno: %d)", retries, errno));
  1389. enqueue_email(msg, retries);
  1390. }
  1391. }
  1392. }
  1393. else
  1394. if (strcmp(type, "telegram") == 0) {
  1395. /* send this via telegram */
  1396. char *bot = xs_dict_get(q_item, "bot");
  1397. char *msg = xs_dict_get(q_item, "message");
  1398. xs *chat_id = xs_dup(xs_dict_get(q_item, "chat_id"));
  1399. int status = 0;
  1400. /* chat_id must start with a - */
  1401. if (!xs_startswith(chat_id, "-"))
  1402. chat_id = xs_str_wrap_i("-", chat_id, NULL);
  1403. xs *url = xs_fmt("https:/" "/api.telegram.org/bot%s/sendMessage", bot);
  1404. xs *body = xs_fmt("{\"chat_id\":%s,\"text\":\"%s\"}", chat_id, msg);
  1405. xs *headers = xs_dict_new();
  1406. headers = xs_dict_append(headers, "content-type", "application/json");
  1407. xs *rsp = xs_http_request("POST", url, headers,
  1408. body, strlen(body), &status, NULL, NULL, 0);
  1409. rsp = xs_free(rsp);
  1410. srv_debug(0, xs_fmt("telegram post %d", status));
  1411. }
  1412. else
  1413. if (strcmp(type, "purge") == 0) {
  1414. srv_log(xs_dup("purge start"));
  1415. purge_all();
  1416. srv_log(xs_dup("purge end"));
  1417. }
  1418. else
  1419. srv_log(xs_fmt("unexpected q_item type '%s'", type));
  1420. }
  1421. int process_queue(void)
  1422. /* processes the global queue */
  1423. {
  1424. int cnt = 0;
  1425. xs *list = queue();
  1426. xs_list *p = list;
  1427. xs_str *fn;
  1428. while (xs_list_iter(&p, &fn)) {
  1429. xs *q_item = dequeue(fn);
  1430. if (q_item != NULL) {
  1431. job_post(q_item, 0);
  1432. cnt++;
  1433. }
  1434. }
  1435. return cnt;
  1436. }
  1437. /** HTTP handlers */
  1438. int activitypub_get_handler(const xs_dict *req, const char *q_path,
  1439. char **body, int *b_size, char **ctype)
  1440. {
  1441. int status = 200;
  1442. char *accept = xs_dict_get(req, "accept");
  1443. snac snac;
  1444. xs *msg = NULL;
  1445. if (accept == NULL)
  1446. return 0;
  1447. if (xs_str_in(accept, "application/activity+json") == -1 &&
  1448. xs_str_in(accept, "application/ld+json") == -1)
  1449. return 0;
  1450. xs *l = xs_split_n(q_path, "/", 2);
  1451. char *uid, *p_path;
  1452. uid = xs_list_get(l, 1);
  1453. if (!user_open(&snac, uid)) {
  1454. /* invalid user */
  1455. srv_debug(1, xs_fmt("activitypub_get_handler bad user %s", uid));
  1456. return 404;
  1457. }
  1458. p_path = xs_list_get(l, 2);
  1459. *ctype = "application/activity+json";
  1460. if (p_path == NULL) {
  1461. /* if there was no component after the user, it's an actor request */
  1462. msg = msg_actor(&snac);
  1463. *ctype = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"";
  1464. char *ua = xs_dict_get(req, "user-agent");
  1465. snac_debug(&snac, 0, xs_fmt("serving actor [%s]", ua ? ua : "No UA"));
  1466. }
  1467. else
  1468. if (strcmp(p_path, "outbox") == 0) {
  1469. xs *id = xs_fmt("%s/outbox", snac.actor);
  1470. xs *elems = timeline_simple_list(&snac, "public", 0, 20);
  1471. xs *list = xs_list_new();
  1472. msg = msg_collection(&snac, id);
  1473. char *p, *v;
  1474. p = elems;
  1475. while (xs_list_iter(&p, &v)) {
  1476. xs *i = NULL;
  1477. if (valid_status(object_get_by_md5(v, &i))) {
  1478. char *type = xs_dict_get(i, "type");
  1479. char *id = xs_dict_get(i, "id");
  1480. if (type && id && strcmp(type, "Note") == 0 && xs_startswith(id, snac.actor)) {
  1481. i = xs_dict_del(i, "_snac");
  1482. list = xs_list_append(list, i);
  1483. }
  1484. }
  1485. }
  1486. /* replace the 'orderedItems' with the latest posts */
  1487. xs *items = xs_number_new(xs_list_len(list));
  1488. msg = xs_dict_set(msg, "orderedItems", list);
  1489. msg = xs_dict_set(msg, "totalItems", items);
  1490. }
  1491. else
  1492. if (strcmp(p_path, "followers") == 0 || strcmp(p_path, "following") == 0) {
  1493. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  1494. msg = msg_collection(&snac, id);
  1495. }
  1496. else
  1497. if (xs_startswith(p_path, "p/")) {
  1498. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  1499. status = object_get(id, &msg);
  1500. }
  1501. else
  1502. status = 404;
  1503. if (status == 200 && msg != NULL) {
  1504. *body = xs_json_dumps_pp(msg, 4);
  1505. *b_size = strlen(*body);
  1506. }
  1507. snac_debug(&snac, 1, xs_fmt("activitypub_get_handler serving %s %d", q_path, status));
  1508. user_free(&snac);
  1509. return status;
  1510. }
  1511. int activitypub_post_handler(const xs_dict *req, const char *q_path,
  1512. char *payload, int p_size,
  1513. char **body, int *b_size, char **ctype)
  1514. /* processes an input message */
  1515. {
  1516. (void)b_size;
  1517. int status = 202; /* accepted */
  1518. char *i_ctype = xs_dict_get(req, "content-type");
  1519. snac snac;
  1520. char *v;
  1521. if (i_ctype == NULL) {
  1522. *body = xs_str_new("no content-type");
  1523. *ctype = "text/plain";
  1524. return 400;
  1525. }
  1526. if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
  1527. xs_str_in(i_ctype, "application/ld+json") == -1)
  1528. return 0;
  1529. /* decode the message */
  1530. xs *msg = xs_json_loads(payload);
  1531. if (msg == NULL) {
  1532. srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
  1533. *body = xs_str_new("JSON error");
  1534. *ctype = "text/plain";
  1535. status = 400;
  1536. }
  1537. /* get the user and path */
  1538. xs *l = xs_split_n(q_path, "/", 2);
  1539. char *uid;
  1540. if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
  1541. /* strange q_path */
  1542. srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
  1543. return 404;
  1544. }
  1545. uid = xs_list_get(l, 1);
  1546. if (!user_open(&snac, uid)) {
  1547. /* invalid user */
  1548. srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid));
  1549. return 404;
  1550. }
  1551. /* if it has a digest, check it now, because
  1552. later the payload won't be exactly the same */
  1553. if ((v = xs_dict_get(req, "digest")) != NULL) {
  1554. xs *s1 = xs_sha256_base64(payload, p_size);
  1555. xs *s2 = xs_fmt("SHA-256=%s", s1);
  1556. if (strcmp(s2, v) != 0) {
  1557. srv_log(xs_fmt("digest check FAILED"));
  1558. *body = xs_str_new("bad digest");
  1559. *ctype = "text/plain";
  1560. status = 400;
  1561. }
  1562. }
  1563. /* if the message is from a muted actor, reject it right now */
  1564. if (!xs_is_null(v = xs_dict_get(msg, "actor")) && *v) {
  1565. if (is_muted(&snac, v)) {
  1566. snac_log(&snac, xs_fmt("rejected message from MUTEd actor %s", v));
  1567. *body = xs_str_new("rejected");
  1568. *ctype = "text/plain";
  1569. status = 403;
  1570. }
  1571. }
  1572. if (valid_status(status)) {
  1573. enqueue_input(&snac, msg, req, 0);
  1574. *ctype = "application/activity+json";
  1575. }
  1576. user_free(&snac);
  1577. return status;
  1578. }