activitypub.c 50 KB

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