activitypub.c 48 KB

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