activitypub.c 44 KB

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