activitypub.c 58 KB

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