activitypub.c 44 KB

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