activitypub.c 45 KB

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