activitypub.c 59 KB

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