activitypub.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967
  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. void notify(snac *snac, const char *type, const char *utype, const char *actor, const xs_dict *msg)
  372. /* notifies the user of relevant events */
  373. {
  374. const char *id = xs_dict_get(msg, "id");
  375. if (strcmp(type, "Create") == 0) {
  376. /* only notify of notes specifically for us */
  377. xs *rcpts = recipient_list(snac, msg, 0);
  378. if (xs_list_in(rcpts, snac->actor) == -1)
  379. return;
  380. /* discard votes */
  381. const xs_dict *note = xs_dict_get(msg, "object");
  382. if (note && !xs_is_null(xs_dict_get(note, "name")))
  383. return;
  384. }
  385. if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") != 0)
  386. return;
  387. /* get the object id */
  388. const char *objid = xs_dict_get(msg, "object");
  389. if (xs_type(objid) == XSTYPE_DICT)
  390. objid = xs_dict_get(objid, "id");
  391. if (strcmp(type, "Like") == 0 || strcmp(type, "Announce") == 0) {
  392. /* if it's not an admiration about something by us, done */
  393. if (xs_is_null(objid) || !xs_startswith(objid, snac->actor))
  394. return;
  395. }
  396. /* updated poll? */
  397. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0) {
  398. const xs_dict *poll;
  399. const char *poll_id;
  400. if ((poll = xs_dict_get(msg, "object")) == NULL)
  401. return;
  402. /* if it's not closed, discard */
  403. if (xs_is_null(xs_dict_get(poll, "closed")))
  404. return;
  405. if ((poll_id = xs_dict_get(poll, "id")) == NULL)
  406. return;
  407. /* if it's not ours and we didn't vote, discard */
  408. if (!xs_startswith(poll_id, snac->actor) && !was_question_voted(snac, poll_id))
  409. return;
  410. }
  411. /* user will love to know about this! */
  412. /* prepare message body */
  413. xs *body = xs_fmt("User : @%s@%s\n",
  414. xs_dict_get(snac->config, "uid"),
  415. xs_dict_get(srv_config, "host")
  416. );
  417. if (strcmp(utype, "(null)") != 0) {
  418. xs *s1 = xs_fmt("Type : %s + %s\n", type, utype);
  419. body = xs_str_cat(body, s1);
  420. }
  421. else {
  422. xs *s1 = xs_fmt("Type : %s\n", type);
  423. body = xs_str_cat(body, s1);
  424. }
  425. {
  426. xs *s1 = xs_fmt("Actor : %s\n", actor);
  427. body = xs_str_cat(body, s1);
  428. }
  429. if (objid != NULL) {
  430. xs *s1 = xs_fmt("Object: %s\n", objid);
  431. body = xs_str_cat(body, s1);
  432. }
  433. /* email */
  434. const char *email = "[disabled by admin]";
  435. if (xs_type(xs_dict_get(srv_config, "disable_email_notifications")) != XSTYPE_TRUE) {
  436. email = xs_dict_get(snac->config_o, "email");
  437. if (xs_is_null(email)) {
  438. email = xs_dict_get(snac->config, "email");
  439. if (xs_is_null(email))
  440. email = "[empty]";
  441. }
  442. }
  443. if (*email != '\0' && *email != '[') {
  444. snac_debug(snac, 1, xs_fmt("email notify %s %s %s", type, utype, actor));
  445. xs *subject = xs_fmt("snac notify for @%s@%s",
  446. xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"));
  447. xs *from = xs_fmt("snac-daemon <snac-daemon@%s>", xs_dict_get(srv_config, "host"));
  448. xs *header = xs_fmt(
  449. "From: %s\n"
  450. "To: %s\n"
  451. "Subject: %s\n"
  452. "\n",
  453. from, email, subject);
  454. xs *email_body = xs_fmt("%s%s", header, body);
  455. enqueue_email(email_body, 0);
  456. }
  457. /* telegram */
  458. char *bot = xs_dict_get(snac->config, "telegram_bot");
  459. char *chat_id = xs_dict_get(snac->config, "telegram_chat_id");
  460. if (!xs_is_null(bot) && !xs_is_null(chat_id) && *bot && *chat_id)
  461. enqueue_telegram(body, bot, chat_id);
  462. /* finally, store it in the notification folder */
  463. if (strcmp(type, "Follow") == 0)
  464. objid = id;
  465. notify_add(snac, type, utype, actor, objid != NULL ? objid : id);
  466. }
  467. /** messages **/
  468. xs_dict *msg_base(snac *snac, const char *type, const char *id,
  469. const char *actor, const char *date, const char *object)
  470. /* creates a base ActivityPub message */
  471. {
  472. xs *did = NULL;
  473. xs *published = NULL;
  474. xs *ntid = tid(0);
  475. /* generated values */
  476. if (date && strcmp(date, "@now") == 0) {
  477. published = xs_str_utctime(0, ISO_DATE_SPEC);
  478. date = published;
  479. }
  480. if (id != NULL) {
  481. if (strcmp(id, "@dummy") == 0) {
  482. did = xs_fmt("%s/d/%s/%s", snac->actor, ntid, type);
  483. id = did;
  484. }
  485. else
  486. if (strcmp(id, "@object") == 0) {
  487. if (object != NULL) {
  488. did = xs_fmt("%s/%s_%s", xs_dict_get(object, "id"), type, ntid);
  489. id = did;
  490. }
  491. else
  492. id = NULL;
  493. }
  494. }
  495. xs_dict *msg = xs_dict_new();
  496. msg = xs_dict_append(msg, "@context", "https:/" "/www.w3.org/ns/activitystreams");
  497. msg = xs_dict_append(msg, "type", type);
  498. if (id != NULL)
  499. msg = xs_dict_append(msg, "id", id);
  500. if (actor != NULL)
  501. msg = xs_dict_append(msg, "actor", actor);
  502. if (date != NULL)
  503. msg = xs_dict_append(msg, "published", date);
  504. if (object != NULL)
  505. msg = xs_dict_append(msg, "object", object);
  506. return msg;
  507. }
  508. xs_dict *msg_collection(snac *snac, char *id)
  509. /* creates an empty OrderedCollection message */
  510. {
  511. xs_dict *msg = msg_base(snac, "OrderedCollection", id, NULL, NULL, NULL);
  512. xs *ol = xs_list_new();
  513. xs *nz = xs_number_new(0);
  514. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  515. msg = xs_dict_append(msg, "orderedItems", ol);
  516. msg = xs_dict_append(msg, "totalItems", nz);
  517. return msg;
  518. }
  519. xs_dict *msg_accept(snac *snac, char *object, char *to)
  520. /* creates an Accept message (as a response to a Follow) */
  521. {
  522. xs_dict *msg = msg_base(snac, "Accept", "@dummy", snac->actor, NULL, object);
  523. msg = xs_dict_append(msg, "to", to);
  524. return msg;
  525. }
  526. xs_dict *msg_update(snac *snac, xs_dict *object)
  527. /* creates an Update message */
  528. {
  529. xs_dict *msg = msg_base(snac, "Update", "@object", snac->actor, "@now", object);
  530. char *type = xs_dict_get(object, "type");
  531. if (strcmp(type, "Note") == 0) {
  532. msg = xs_dict_append(msg, "to", xs_dict_get(object, "to"));
  533. msg = xs_dict_append(msg, "cc", xs_dict_get(object, "cc"));
  534. }
  535. else
  536. if (strcmp(type, "Person") == 0) {
  537. msg = xs_dict_append(msg, "to", public_address);
  538. /* also spam the people being followed, so that
  539. they have the newest information about who we are */
  540. xs *cc = following_list(snac);
  541. msg = xs_dict_append(msg, "cc", cc);
  542. }
  543. else
  544. msg = xs_dict_append(msg, "to", public_address);
  545. return msg;
  546. }
  547. xs_dict *msg_admiration(snac *snac, char *object, char *type)
  548. /* creates a Like or Announce message */
  549. {
  550. xs *a_msg = NULL;
  551. xs_dict *msg = NULL;
  552. xs *wrk = NULL;
  553. /* call the object */
  554. timeline_request(snac, &object, &wrk);
  555. if (valid_status(object_get(object, &a_msg))) {
  556. xs *rcpts = xs_list_new();
  557. msg = msg_base(snac, type, "@dummy", snac->actor, "@now", object);
  558. if (is_msg_public(snac, a_msg))
  559. rcpts = xs_list_append(rcpts, public_address);
  560. rcpts = xs_list_append(rcpts, xs_dict_get(a_msg, "attributedTo"));
  561. msg = xs_dict_append(msg, "to", rcpts);
  562. }
  563. else
  564. snac_log(snac, xs_fmt("msg_admiration cannot retrieve object %s", object));
  565. return msg;
  566. }
  567. xs_dict *msg_actor(snac *snac)
  568. /* create a Person message for this actor */
  569. {
  570. xs *ctxt = xs_list_new();
  571. xs *icon = xs_dict_new();
  572. xs *keys = xs_dict_new();
  573. xs *avtr = NULL;
  574. xs *kid = NULL;
  575. xs *f_bio = NULL;
  576. xs_dict *msg = msg_base(snac, "Person", snac->actor, NULL, NULL, NULL);
  577. char *p;
  578. int n;
  579. /* change the @context (is this really necessary?) */
  580. ctxt = xs_list_append(ctxt, "https:/" "/www.w3.org/ns/activitystreams");
  581. ctxt = xs_list_append(ctxt, "https:/" "/w3id.org/security/v1");
  582. msg = xs_dict_set(msg, "@context", ctxt);
  583. msg = xs_dict_set(msg, "url", snac->actor);
  584. msg = xs_dict_set(msg, "name", xs_dict_get(snac->config, "name"));
  585. msg = xs_dict_set(msg, "preferredUsername", snac->uid);
  586. msg = xs_dict_set(msg, "published", xs_dict_get(snac->config, "published"));
  587. f_bio = not_really_markdown(xs_dict_get(snac->config, "bio"), NULL);
  588. msg = xs_dict_set(msg, "summary", f_bio);
  589. char *folders[] = { "inbox", "outbox", "followers", "following", NULL };
  590. for (n = 0; folders[n]; n++) {
  591. xs *f = xs_fmt("%s/%s", snac->actor, folders[n]);
  592. msg = xs_dict_set(msg, folders[n], f);
  593. }
  594. p = xs_dict_get(snac->config, "avatar");
  595. if (*p == '\0')
  596. avtr = xs_fmt("%s/susie.png", srv_baseurl);
  597. else
  598. avtr = xs_dup(p);
  599. icon = xs_dict_append(icon, "type", "Image");
  600. icon = xs_dict_append(icon, "mediaType", xs_mime_by_ext(avtr));
  601. icon = xs_dict_append(icon, "url", avtr);
  602. msg = xs_dict_set(msg, "icon", icon);
  603. kid = xs_fmt("%s#main-key", snac->actor);
  604. keys = xs_dict_append(keys, "id", kid);
  605. keys = xs_dict_append(keys, "owner", snac->actor);
  606. keys = xs_dict_append(keys, "publicKeyPem", xs_dict_get(snac->key, "public"));
  607. msg = xs_dict_set(msg, "publicKey", keys);
  608. /* if the "bot" config field is set to true, change type to "Service" */
  609. if (xs_type(xs_dict_get(snac->config, "bot")) == XSTYPE_TRUE)
  610. msg = xs_dict_set(msg, "type", "Service");
  611. return msg;
  612. }
  613. xs_dict *msg_create(snac *snac, const xs_dict *object)
  614. /* creates a 'Create' message */
  615. {
  616. xs_dict *msg = msg_base(snac, "Create", "@object", snac->actor, "@now", object);
  617. xs_val *v;
  618. if ((v = xs_dict_get(object, "attributedTo")))
  619. msg = xs_dict_append(msg, "attributedTo", v);
  620. if ((v = xs_dict_get(object, "cc")))
  621. msg = xs_dict_append(msg, "cc", v);
  622. if ((v = xs_dict_get(object, "to")))
  623. msg = xs_dict_append(msg, "to", v);
  624. else
  625. msg = xs_dict_append(msg, "to", public_address);
  626. return msg;
  627. }
  628. xs_dict *msg_undo(snac *snac, char *object)
  629. /* creates an 'Undo' message */
  630. {
  631. xs_dict *msg = msg_base(snac, "Undo", "@object", snac->actor, "@now", object);
  632. msg = xs_dict_append(msg, "to", xs_dict_get(object, "object"));
  633. return msg;
  634. }
  635. xs_dict *msg_delete(snac *snac, char *id)
  636. /* creates a 'Delete' + 'Tombstone' for a local entry */
  637. {
  638. xs *tomb = xs_dict_new();
  639. xs_dict *msg = NULL;
  640. /* sculpt the tombstone */
  641. tomb = xs_dict_append(tomb, "type", "Tombstone");
  642. tomb = xs_dict_append(tomb, "id", id);
  643. /* now create the Delete */
  644. msg = msg_base(snac, "Delete", "@object", snac->actor, "@now", tomb);
  645. msg = xs_dict_append(msg, "to", public_address);
  646. return msg;
  647. }
  648. xs_dict *msg_follow(snac *snac, const char *q)
  649. /* creates a 'Follow' message */
  650. {
  651. xs *actor_o = NULL;
  652. xs *actor = NULL;
  653. xs_dict *msg = NULL;
  654. int status;
  655. xs *url_or_uid = xs_strip_i(xs_str_new(q));
  656. if (xs_startswith(url_or_uid, "https:/"))
  657. actor = xs_dup(url_or_uid);
  658. else
  659. if (!valid_status(webfinger_request(url_or_uid, &actor, NULL)) || actor == NULL) {
  660. snac_log(snac, xs_fmt("cannot resolve user %s to follow", url_or_uid));
  661. return NULL;
  662. }
  663. /* request the actor */
  664. status = actor_request(snac, actor, &actor_o);
  665. if (valid_status(status)) {
  666. /* check if the actor is an alias */
  667. char *r_actor = xs_dict_get(actor_o, "id");
  668. if (r_actor && strcmp(actor, r_actor) != 0) {
  669. snac_log(snac, xs_fmt("actor to follow is an alias %s -> %s", actor, r_actor));
  670. }
  671. msg = msg_base(snac, "Follow", "@dummy", snac->actor, NULL, r_actor);
  672. }
  673. else
  674. snac_log(snac, xs_fmt("cannot get actor to follow %s %d", actor, status));
  675. return msg;
  676. }
  677. xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts,
  678. xs_str *in_reply_to, xs_list *attach, int priv)
  679. /* creates a 'Note' message */
  680. {
  681. xs *ntid = tid(0);
  682. xs *id = xs_fmt("%s/p/%s", snac->actor, ntid);
  683. xs *ctxt = NULL;
  684. xs *fc2 = NULL;
  685. xs *fc1 = NULL;
  686. xs *to = NULL;
  687. xs *cc = xs_list_new();
  688. xs *irt = NULL;
  689. xs *tag = xs_list_new();
  690. xs *atls = xs_list_new();
  691. xs_dict *msg = msg_base(snac, "Note", id, NULL, "@now", NULL);
  692. xs_list *p;
  693. xs_val *v;
  694. if (rcpts == NULL)
  695. to = xs_list_new();
  696. else {
  697. if (xs_type(rcpts) == XSTYPE_STRING) {
  698. to = xs_list_new();
  699. to = xs_list_append(to, rcpts);
  700. }
  701. else
  702. to = xs_dup(rcpts);
  703. }
  704. /* format the content */
  705. fc2 = not_really_markdown(content, &atls);
  706. if (in_reply_to != NULL && *in_reply_to) {
  707. xs *p_msg = NULL;
  708. xs *wrk = NULL;
  709. /* demand this thing */
  710. timeline_request(snac, &in_reply_to, &wrk);
  711. if (valid_status(object_get(in_reply_to, &p_msg))) {
  712. /* add this author as recipient */
  713. char *a, *v;
  714. if ((a = xs_dict_get(p_msg, "attributedTo")) && xs_list_in(to, a) == -1)
  715. to = xs_list_append(to, a);
  716. /* add this author to the tag list as a mention */
  717. xs *t_href = NULL;
  718. xs *t_name = NULL;
  719. if (!xs_is_null(a) && valid_status(webfinger_request(a, &t_href, &t_name))) {
  720. xs *t = xs_dict_new();
  721. t = xs_dict_append(t, "type", "Mention");
  722. t = xs_dict_append(t, "href", t_href);
  723. t = xs_dict_append(t, "name", t_name);
  724. tag = xs_list_append(tag, t);
  725. }
  726. /* get the context, if there is one */
  727. if ((v = xs_dict_get(p_msg, "context")))
  728. ctxt = xs_dup(v);
  729. /* if this message is public, ours will also be */
  730. if (!priv && is_msg_public(snac, p_msg) && xs_list_in(to, public_address) == -1)
  731. to = xs_list_append(to, public_address);
  732. }
  733. irt = xs_dup(in_reply_to);
  734. }
  735. else
  736. irt = xs_val_new(XSTYPE_NULL);
  737. /* extract the mentions and hashtags and convert the content */
  738. process_tags(snac, fc2, &fc1, &tag);
  739. /* create the attachment list, if there are any */
  740. if (!xs_is_null(attach)) {
  741. while (xs_list_iter(&attach, &v)) {
  742. xs *d = xs_dict_new();
  743. char *url = xs_list_get(v, 0);
  744. char *alt = xs_list_get(v, 1);
  745. char *mime = xs_mime_by_ext(url);
  746. d = xs_dict_append(d, "mediaType", mime);
  747. d = xs_dict_append(d, "url", url);
  748. d = xs_dict_append(d, "name", alt);
  749. d = xs_dict_append(d, "type",
  750. xs_startswith(mime, "image/") ? "Image" : "Document");
  751. atls = xs_list_append(atls, d);
  752. }
  753. }
  754. if (ctxt == NULL)
  755. ctxt = xs_fmt("%s#ctxt", id);
  756. /* add all mentions to the cc */
  757. p = tag;
  758. while (xs_list_iter(&p, &v)) {
  759. if (xs_type(v) == XSTYPE_DICT) {
  760. char *t;
  761. if ((t = xs_dict_get(v, "type")) != NULL && strcmp(t, "Mention") == 0) {
  762. if ((t = xs_dict_get(v, "href")) != NULL)
  763. cc = xs_list_append(cc, t);
  764. }
  765. }
  766. }
  767. /* no recipients? must be for everybody */
  768. if (!priv && xs_list_len(to) == 0)
  769. to = xs_list_append(to, public_address);
  770. /* delete all cc recipients that also are in the to */
  771. p = to;
  772. while (xs_list_iter(&p, &v)) {
  773. int i;
  774. if ((i = xs_list_in(cc, v)) != -1)
  775. cc = xs_list_del(cc, i);
  776. }
  777. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  778. msg = xs_dict_append(msg, "summary", "");
  779. msg = xs_dict_append(msg, "content", fc1);
  780. msg = xs_dict_append(msg, "context", ctxt);
  781. msg = xs_dict_append(msg, "url", id);
  782. msg = xs_dict_append(msg, "to", to);
  783. msg = xs_dict_append(msg, "cc", cc);
  784. msg = xs_dict_append(msg, "inReplyTo", irt);
  785. msg = xs_dict_append(msg, "tag", tag);
  786. msg = xs_dict_append(msg, "sourceContent", content);
  787. if (xs_list_len(atls))
  788. msg = xs_dict_append(msg, "attachment", atls);
  789. return msg;
  790. }
  791. xs_dict *msg_ping(snac *user, const char *rcpt)
  792. /* creates a Ping message (https://humungus.tedunangst.com/r/honk/v/tip/f/docs/ping.txt) */
  793. {
  794. xs_dict *msg = msg_base(user, "Ping", "@dummy", user->actor, NULL, NULL);
  795. msg = xs_dict_append(msg, "to", rcpt);
  796. return msg;
  797. }
  798. xs_dict *msg_pong(snac *user, const char *rcpt, const char *object)
  799. /* creates a Pong message (https://humungus.tedunangst.com/r/honk/v/tip/f/docs/ping.txt) */
  800. {
  801. xs_dict *msg = msg_base(user, "Pong", "@dummy", user->actor, NULL, object);
  802. msg = xs_dict_append(msg, "to", rcpt);
  803. return msg;
  804. }
  805. xs_dict *msg_question(snac *user, const char *content, xs_list *attach,
  806. const xs_list *opts, int multiple, int end_secs)
  807. /* creates a Question message */
  808. {
  809. xs *ntid = tid(0);
  810. xs_dict *msg = msg_note(user, content, NULL, NULL, attach, 0);
  811. int max = 8;
  812. xs_set seen;
  813. msg = xs_dict_set(msg, "type", "Question");
  814. /* make it non-editable */
  815. msg = xs_dict_del(msg, "sourceContent");
  816. xs *o = xs_list_new();
  817. xs_list *p = (xs_list *)opts;
  818. xs_str *v;
  819. xs *replies = xs_json_loads("{\"type\":\"Collection\",\"totalItems\":0}");
  820. xs_set_init(&seen);
  821. while (max && xs_list_iter(&p, &v)) {
  822. if (*v) {
  823. xs *v2 = xs_dup(v);
  824. xs *d = xs_dict_new();
  825. if (strlen(v2) > 60) {
  826. v2[60] = '\0';
  827. v2 = xs_str_cat(v2, "...");
  828. }
  829. if (xs_set_add(&seen, v2) == 1) {
  830. d = xs_dict_append(d, "name", v2);
  831. d = xs_dict_append(d, "replies", replies);
  832. o = xs_list_append(o, d);
  833. max--;
  834. }
  835. }
  836. }
  837. xs_set_free(&seen);
  838. msg = xs_dict_append(msg, multiple ? "anyOf" : "oneOf", o);
  839. /* set the end time */
  840. time_t t = time(NULL) + end_secs;
  841. xs *et = xs_str_utctime(t, ISO_DATE_SPEC);
  842. msg = xs_dict_append(msg, "endTime", et);
  843. return msg;
  844. }
  845. int update_question(snac *user, const char *id)
  846. /* updates the poll counts */
  847. {
  848. xs *msg = NULL;
  849. xs *rcnt = xs_dict_new();
  850. xs *z = xs_number_new(0);
  851. xs *rcpts = xs_list_new();
  852. xs *lopts = xs_list_new();
  853. xs_list *opts;
  854. xs_list *p;
  855. xs_val *v;
  856. /* get the object */
  857. if (!valid_status(object_get(id, &msg)))
  858. return -1;
  859. /* closed? do nothing more */
  860. if (xs_dict_get(msg, "closed"))
  861. return -2;
  862. /* get the options */
  863. if ((opts = xs_dict_get(msg, "oneOf")) == NULL &&
  864. (opts = xs_dict_get(msg, "anyOf")) == NULL)
  865. return -3;
  866. /* fill the initial count */
  867. p = opts;
  868. while (xs_list_iter(&p, &v)) {
  869. const char *name = xs_dict_get(v, "name");
  870. if (name) {
  871. lopts = xs_list_append(lopts, name);
  872. rcnt = xs_dict_set(rcnt, name, z);
  873. }
  874. }
  875. /* iterate now the children (the votes) */
  876. xs *chld = object_children(id);
  877. p = chld;
  878. while (xs_list_iter(&p, &v)) {
  879. xs *obj = NULL;
  880. if (!valid_status(object_get_by_md5(v, &obj)))
  881. continue;
  882. const char *name = xs_dict_get(obj, "name");
  883. const char *atto = xs_dict_get(obj, "attributedTo");
  884. if (name && atto) {
  885. /* get the current count */
  886. const xs_number *cnt = xs_dict_get(rcnt, name);
  887. if (xs_type(cnt) == XSTYPE_NUMBER) {
  888. /* if it exists, increment */
  889. xs *ucnt = xs_number_new(xs_number_get(cnt) + 1);
  890. rcnt = xs_dict_set(rcnt, name, ucnt);
  891. rcpts = xs_list_append(rcpts, atto);
  892. }
  893. }
  894. }
  895. /* create a new list of options with their new counts */
  896. xs *nopts = xs_list_new();
  897. p = lopts;
  898. while (xs_list_iter(&p, &v)) {
  899. const xs_number *cnt = xs_dict_get(rcnt, v);
  900. if (xs_type(cnt) == XSTYPE_NUMBER) {
  901. xs *d1 = xs_dict_new();
  902. xs *d2 = xs_dict_new();
  903. d2 = xs_dict_append(d2, "type", "Collection");
  904. d2 = xs_dict_append(d2, "totalItems", cnt);
  905. d1 = xs_dict_append(d1, "type", "Note");
  906. d1 = xs_dict_append(d1, "name", v);
  907. d1 = xs_dict_append(d1, "replies", d2);
  908. nopts = xs_list_append(nopts, d1);
  909. }
  910. }
  911. /* update the list */
  912. msg = xs_dict_set(msg, xs_dict_get(msg, "oneOf") != NULL ? "oneOf" : "anyOf", nopts);
  913. /* due date? */
  914. int closed = 0;
  915. const char *end_time = xs_dict_get(msg, "endTime");
  916. if (!xs_is_null(end_time)) {
  917. xs *now = xs_str_utctime(0, ISO_DATE_SPEC);
  918. /* is now greater than the endTime? */
  919. if (strcmp(now, end_time) >= 0) {
  920. xs *et = xs_dup(end_time);
  921. msg = xs_dict_set(msg, "closed", et);
  922. closed = 1;
  923. }
  924. }
  925. /* update the count of voters */
  926. xs *vcnt = xs_number_new(xs_list_len(rcpts));
  927. msg = xs_dict_set(msg, "votersCount", vcnt);
  928. /* store */
  929. object_add_ow(id, msg);
  930. snac_debug(user, 1, xs_fmt("recounted poll %s", id));
  931. timeline_touch(user);
  932. /* send an update message to all voters */
  933. xs *u_msg = msg_update(user, msg);
  934. u_msg = xs_dict_set(u_msg, "cc", rcpts);
  935. enqueue_message(user, u_msg);
  936. if (closed) {
  937. xs *c_msg = msg_update(user, msg);
  938. notify(user, "Update", "Question", user->actor, c_msg);
  939. }
  940. return 0;
  941. }
  942. /** queues **/
  943. int process_input_message(snac *snac, xs_dict *msg, xs_dict *req)
  944. /* processes an ActivityPub message from the input queue */
  945. {
  946. /* actor and type exist, were checked previously */
  947. char *actor = xs_dict_get(msg, "actor");
  948. char *type = xs_dict_get(msg, "type");
  949. xs *actor_o = NULL;
  950. int a_status;
  951. int do_notify = 0;
  952. /* question votes may not have a type */
  953. if (xs_is_null(type))
  954. type = "Note";
  955. if (xs_is_null(actor)) {
  956. snac_debug(snac, 0, xs_fmt("malformed message"));
  957. return 1;
  958. }
  959. char *object, *utype;
  960. object = xs_dict_get(msg, "object");
  961. if (object != NULL && xs_type(object) == XSTYPE_DICT)
  962. utype = xs_dict_get(object, "type");
  963. else
  964. utype = "(null)";
  965. /* reject messages that are not for this user */
  966. if (!is_msg_for_me(snac, msg)) {
  967. snac_debug(snac, 0, xs_fmt("message from %s of type '%s' not for us", actor, type));
  968. return 1;
  969. }
  970. /* if it's a DM from someone we don't follow, reject the message */
  971. if (xs_type(xs_dict_get(snac->config, "drop_dm_from_unknown")) == XSTYPE_TRUE) {
  972. if (strcmp(utype, "Note") == 0 && !is_msg_public(snac, msg) &&
  973. !following_check(snac, actor)) {
  974. snac_log(snac, xs_fmt("DM rejected from unknown actor %s", actor));
  975. return 1;
  976. }
  977. }
  978. /* bring the actor */
  979. a_status = actor_request(snac, actor, &actor_o);
  980. /* if the actor does not explicitly exist, discard */
  981. if (a_status == 404 || a_status == 410) {
  982. snac_debug(snac, 1,
  983. xs_fmt("dropping message due to actor error %s %d", actor, a_status));
  984. return 1;
  985. }
  986. if (!valid_status(a_status)) {
  987. /* other actor download errors may need a retry */
  988. snac_debug(snac, 1,
  989. xs_fmt("error requesting actor %s %d -- retry later", actor, a_status));
  990. return 0;
  991. }
  992. /* check the signature */
  993. xs *sig_err = NULL;
  994. if (!check_signature(snac, req, &sig_err)) {
  995. snac_log(snac, xs_fmt("bad signature %s (%s)", actor, sig_err));
  996. srv_archive_error("check_signature", sig_err, req, msg);
  997. return 1;
  998. }
  999. if (strcmp(type, "Follow") == 0) { /** **/
  1000. if (!follower_check(snac, actor)) {
  1001. xs *f_msg = xs_dup(msg);
  1002. xs *reply = msg_accept(snac, f_msg, actor);
  1003. enqueue_message(snac, reply);
  1004. if (xs_is_null(xs_dict_get(f_msg, "published"))) {
  1005. /* add a date if it doesn't include one (Mastodon) */
  1006. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  1007. f_msg = xs_dict_set(f_msg, "published", date);
  1008. }
  1009. timeline_add(snac, xs_dict_get(f_msg, "id"), f_msg);
  1010. follower_add(snac, actor);
  1011. snac_log(snac, xs_fmt("new follower %s", actor));
  1012. do_notify = 1;
  1013. }
  1014. else
  1015. snac_log(snac, xs_fmt("repeated 'Follow' from %s", actor));
  1016. }
  1017. else
  1018. if (strcmp(type, "Undo") == 0) { /** **/
  1019. if (strcmp(utype, "Follow") == 0) { /** **/
  1020. if (valid_status(follower_del(snac, actor))) {
  1021. snac_log(snac, xs_fmt("no longer following us %s", actor));
  1022. do_notify = 1;
  1023. }
  1024. else
  1025. snac_log(snac, xs_fmt("error deleting follower %s", actor));
  1026. }
  1027. else
  1028. snac_debug(snac, 1, xs_fmt("ignored 'Undo' for object type '%s'", utype));
  1029. }
  1030. else
  1031. if (strcmp(type, "Create") == 0) { /** **/
  1032. if (is_muted(snac, actor))
  1033. snac_log(snac, xs_fmt("ignored 'Create' + '%s' from muted actor %s", utype, actor));
  1034. if (strcmp(utype, "Note") == 0) { /** **/
  1035. char *id = xs_dict_get(object, "id");
  1036. char *in_reply_to = xs_dict_get(object, "inReplyTo");
  1037. xs *wrk = NULL;
  1038. timeline_request(snac, &in_reply_to, &wrk);
  1039. if (timeline_add(snac, id, object)) {
  1040. snac_log(snac, xs_fmt("new 'Note' %s %s", actor, id));
  1041. do_notify = 1;
  1042. }
  1043. /* if it has a "name" field, it may be a vote for a question */
  1044. const char *name = xs_dict_get(object, "name");
  1045. if (!xs_is_null(name) && *name && !xs_is_null(in_reply_to) && *in_reply_to)
  1046. update_question(snac, in_reply_to);
  1047. }
  1048. else
  1049. if (strcmp(utype, "Question") == 0) { /** **/
  1050. char *id = xs_dict_get(object, "id");
  1051. if (timeline_add(snac, id, object))
  1052. snac_log(snac, xs_fmt("new 'Question' %s %s", actor, id));
  1053. }
  1054. else
  1055. snac_debug(snac, 1, xs_fmt("ignored 'Create' for object type '%s'", utype));
  1056. }
  1057. else
  1058. if (strcmp(type, "Accept") == 0) { /** **/
  1059. if (strcmp(utype, "Follow") == 0) { /** **/
  1060. if (following_check(snac, actor)) {
  1061. following_add(snac, actor, msg);
  1062. snac_log(snac, xs_fmt("confirmed follow from %s", actor));
  1063. }
  1064. else
  1065. snac_log(snac, xs_fmt("spurious follow accept from %s", actor));
  1066. }
  1067. else
  1068. snac_debug(snac, 1, xs_fmt("ignored 'Accept' for object type '%s'", utype));
  1069. }
  1070. else
  1071. if (strcmp(type, "Like") == 0) { /** **/
  1072. if (xs_type(object) == XSTYPE_DICT)
  1073. object = xs_dict_get(object, "id");
  1074. timeline_admire(snac, object, actor, 1);
  1075. snac_log(snac, xs_fmt("new 'Like' %s %s", actor, object));
  1076. do_notify = 1;
  1077. }
  1078. else
  1079. if (strcmp(type, "Announce") == 0) { /** **/
  1080. xs *a_msg = NULL;
  1081. xs *wrk = NULL;
  1082. if (xs_type(object) == XSTYPE_DICT)
  1083. object = xs_dict_get(object, "id");
  1084. timeline_request(snac, &object, &wrk);
  1085. if (valid_status(object_get(object, &a_msg))) {
  1086. char *who = xs_dict_get(a_msg, "attributedTo");
  1087. if (who && !is_muted(snac, who)) {
  1088. /* bring the actor */
  1089. xs *who_o = NULL;
  1090. if (valid_status(actor_request(snac, who, &who_o))) {
  1091. timeline_admire(snac, object, actor, 0);
  1092. snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object));
  1093. do_notify = 1;
  1094. }
  1095. else
  1096. snac_log(snac, xs_fmt("dropped 'Announce' on actor request error %s", who));
  1097. }
  1098. else
  1099. snac_log(snac, xs_fmt("ignored 'Announce' about muted actor %s", who));
  1100. }
  1101. else
  1102. snac_log(snac, xs_fmt("error requesting 'Announce' object %s", object));
  1103. }
  1104. else
  1105. if (strcmp(type, "Update") == 0) { /** **/
  1106. if (strcmp(utype, "Person") == 0 || strcmp(utype, "Service") == 0) {
  1107. actor_add(actor, xs_dict_get(msg, "object"));
  1108. timeline_touch(snac);
  1109. snac_log(snac, xs_fmt("updated actor %s", actor));
  1110. }
  1111. else
  1112. if (strcmp(utype, "Note") == 0) { /** **/
  1113. const char *id = xs_dict_get(object, "id");
  1114. object_add_ow(id, object);
  1115. timeline_touch(snac);
  1116. snac_log(snac, xs_fmt("updated post %s", id));
  1117. }
  1118. else
  1119. if (strcmp(utype, "Question") == 0) { /** **/
  1120. const char *id = xs_dict_get(object, "id");
  1121. const char *closed = xs_dict_get(object, "closed");
  1122. object_add_ow(id, object);
  1123. timeline_touch(snac);
  1124. snac_log(snac, xs_fmt("%s poll %s", closed == NULL ? "updated" : "closed", id));
  1125. if (closed != NULL)
  1126. do_notify = 1;
  1127. }
  1128. else
  1129. snac_log(snac, xs_fmt("ignored 'Update' for object type '%s'", utype));
  1130. }
  1131. else
  1132. if (strcmp(type, "Delete") == 0) { /** **/
  1133. if (xs_type(object) == XSTYPE_DICT)
  1134. object = xs_dict_get(object, "id");
  1135. if (valid_status(timeline_del(snac, object)))
  1136. snac_debug(snac, 1, xs_fmt("new 'Delete' %s %s", actor, object));
  1137. else
  1138. snac_debug(snac, 1, xs_fmt("ignored 'Delete' for unknown object %s", object));
  1139. }
  1140. else
  1141. if (strcmp(type, "Pong") == 0) { /** **/
  1142. snac_log(snac, xs_fmt("'Pong' received from %s", actor));
  1143. }
  1144. else
  1145. if (strcmp(type, "Ping") == 0) { /** **/
  1146. snac_log(snac, xs_fmt("'Ping' requested from %s", actor));
  1147. xs *rsp = msg_pong(snac, actor, xs_dict_get(msg, "id"));
  1148. enqueue_output_by_actor(snac, rsp, actor, 0);
  1149. }
  1150. else
  1151. snac_debug(snac, 1, xs_fmt("process_input_message type '%s' ignored", type));
  1152. if (do_notify) {
  1153. notify(snac, type, utype, actor, msg);
  1154. timeline_touch(snac);
  1155. }
  1156. return 1;
  1157. }
  1158. int send_email(char *msg)
  1159. /* invoke sendmail with email headers and body in msg */
  1160. {
  1161. FILE *f;
  1162. int status;
  1163. int fds[2];
  1164. pid_t pid;
  1165. if (pipe(fds) == -1) return -1;
  1166. pid = vfork();
  1167. if (pid == -1) return -1;
  1168. else if (pid == 0) {
  1169. dup2(fds[0], 0);
  1170. close(fds[0]);
  1171. close(fds[1]);
  1172. execl("/usr/sbin/sendmail", "sendmail", "-t", (char *) NULL);
  1173. _exit(1);
  1174. }
  1175. close(fds[0]);
  1176. if ((f = fdopen(fds[1], "w")) == NULL) {
  1177. close(fds[1]);
  1178. return -1;
  1179. }
  1180. fprintf(f, "%s\n", msg);
  1181. fclose(f);
  1182. if (waitpid(pid, &status, 0) == -1) return -1;
  1183. return status;
  1184. }
  1185. void process_user_queue_item(snac *snac, xs_dict *q_item)
  1186. /* processes an item from the user queue */
  1187. {
  1188. char *type;
  1189. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  1190. if ((type = xs_dict_get(q_item, "type")) == NULL)
  1191. type = "output";
  1192. if (strcmp(type, "message") == 0) {
  1193. xs_dict *msg = xs_dict_get(q_item, "message");
  1194. xs *rcpts = recipient_list(snac, msg, 1);
  1195. xs_set inboxes;
  1196. xs_list *p;
  1197. xs_str *actor;
  1198. xs_set_init(&inboxes);
  1199. /* iterate the recipients */
  1200. p = rcpts;
  1201. while (xs_list_iter(&p, &actor)) {
  1202. xs *inbox = get_actor_inbox(snac, actor);
  1203. if (inbox != NULL) {
  1204. /* add to the set and, if it's not there, send message */
  1205. if (xs_set_add(&inboxes, inbox) == 1)
  1206. enqueue_output(snac, msg, inbox, 0);
  1207. }
  1208. else
  1209. snac_log(snac, xs_fmt("cannot find inbox for %s", actor));
  1210. }
  1211. /* if it's public, send to the collected inboxes */
  1212. if (is_msg_public(snac, msg)) {
  1213. xs *shibx = inbox_list();
  1214. xs_str *inbox;
  1215. p = shibx;
  1216. while (xs_list_iter(&p, &inbox)) {
  1217. if (xs_set_add(&inboxes, inbox) == 1)
  1218. enqueue_output(snac, msg, inbox, 0);
  1219. }
  1220. }
  1221. xs_set_free(&inboxes);
  1222. }
  1223. else
  1224. if (strcmp(type, "input") == 0) {
  1225. /* process the message */
  1226. xs_dict *msg = xs_dict_get(q_item, "message");
  1227. xs_dict *req = xs_dict_get(q_item, "req");
  1228. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  1229. if (xs_is_null(msg))
  1230. return;
  1231. if (!process_input_message(snac, msg, req)) {
  1232. if (retries > queue_retry_max)
  1233. snac_log(snac, xs_fmt("input giving up"));
  1234. else {
  1235. /* reenqueue */
  1236. enqueue_input(snac, msg, req, retries + 1);
  1237. snac_log(snac, xs_fmt("input requeue #%d", retries + 1));
  1238. }
  1239. }
  1240. }
  1241. else
  1242. if (strcmp(type, "close_question") == 0) {
  1243. /* the time for this question has ended */
  1244. const char *id = xs_dict_get(q_item, "message");
  1245. if (!xs_is_null(id))
  1246. update_question(snac, id);
  1247. }
  1248. else
  1249. snac_log(snac, xs_fmt("unexpected q_item type '%s'", type));
  1250. }
  1251. int process_user_queue(snac *snac)
  1252. /* processes a user's queue */
  1253. {
  1254. int cnt = 0;
  1255. xs *list = user_queue(snac);
  1256. xs_list *p = list;
  1257. xs_str *fn;
  1258. while (xs_list_iter(&p, &fn)) {
  1259. xs *q_item = dequeue(fn);
  1260. if (q_item == NULL) {
  1261. snac_log(snac, xs_fmt("process_user_queue q_item error"));
  1262. continue;
  1263. }
  1264. process_user_queue_item(snac, q_item);
  1265. cnt++;
  1266. }
  1267. return cnt;
  1268. }
  1269. void process_queue_item(xs_dict *q_item)
  1270. /* processes an item from the global queue */
  1271. {
  1272. char *type = xs_dict_get(q_item, "type");
  1273. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  1274. if (strcmp(type, "output") == 0) {
  1275. int status;
  1276. xs_str *inbox = xs_dict_get(q_item, "inbox");
  1277. xs_str *keyid = xs_dict_get(q_item, "keyid");
  1278. xs_str *seckey = xs_dict_get(q_item, "seckey");
  1279. xs_dict *msg = xs_dict_get(q_item, "message");
  1280. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  1281. xs *payload = NULL;
  1282. int p_size = 0;
  1283. if (xs_is_null(inbox) || xs_is_null(msg) || xs_is_null(keyid) || xs_is_null(seckey)) {
  1284. srv_log(xs_fmt("output message error: missing fields"));
  1285. return;
  1286. }
  1287. /* deliver */
  1288. status = send_to_inbox_raw(keyid, seckey, inbox, msg, &payload, &p_size, retries == 0 ? 3 : 8);
  1289. if (payload) {
  1290. if (p_size > 64) {
  1291. /* trim the message */
  1292. payload[64] = '\0';
  1293. payload = xs_str_cat(payload, "...");
  1294. }
  1295. /* strip ugly control characters */
  1296. payload = xs_replace_i(payload, "\n", "");
  1297. payload = xs_replace_i(payload, "\r", "");
  1298. if (*payload)
  1299. payload = xs_str_wrap_i(" [", payload, "]");
  1300. }
  1301. else
  1302. payload = xs_str_new(NULL);
  1303. srv_log(xs_fmt("output message: sent to inbox %s %d%s", inbox, status, payload));
  1304. if (!valid_status(status)) {
  1305. retries++;
  1306. /* error sending; requeue? */
  1307. if (status == 404 || status == 410)
  1308. /* explicit error: discard */
  1309. srv_log(xs_fmt("output message: fatal error %s %d", inbox, status));
  1310. else
  1311. if (retries > queue_retry_max)
  1312. srv_log(xs_fmt("output message: giving up %s %d", inbox, status));
  1313. else {
  1314. /* requeue */
  1315. enqueue_output_raw(keyid, seckey, msg, inbox, retries);
  1316. srv_log(xs_fmt("output message: requeue %s #%d", inbox, retries));
  1317. }
  1318. }
  1319. }
  1320. else
  1321. if (strcmp(type, "email") == 0) {
  1322. /* send this email */
  1323. xs_str *msg = xs_dict_get(q_item, "message");
  1324. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  1325. if (!send_email(msg))
  1326. srv_debug(1, xs_fmt("email message sent"));
  1327. else {
  1328. retries++;
  1329. if (retries > queue_retry_max)
  1330. srv_log(xs_fmt("email giving up (errno: %d)", errno));
  1331. else {
  1332. /* requeue */
  1333. srv_log(xs_fmt(
  1334. "email requeue #%d (errno: %d)", retries, errno));
  1335. enqueue_email(msg, retries);
  1336. }
  1337. }
  1338. }
  1339. else
  1340. if (strcmp(type, "telegram") == 0) {
  1341. /* send this via telegram */
  1342. char *bot = xs_dict_get(q_item, "bot");
  1343. char *msg = xs_dict_get(q_item, "message");
  1344. xs *chat_id = xs_dup(xs_dict_get(q_item, "chat_id"));
  1345. int status = 0;
  1346. /* chat_id must start with a - */
  1347. if (!xs_startswith(chat_id, "-"))
  1348. chat_id = xs_str_wrap_i("-", chat_id, NULL);
  1349. xs *url = xs_fmt("https:/" "/api.telegram.org/bot%s/sendMessage", bot);
  1350. xs *body = xs_fmt("{\"chat_id\":%s,\"text\":\"%s\"}", chat_id, msg);
  1351. xs *headers = xs_dict_new();
  1352. headers = xs_dict_append(headers, "content-type", "application/json");
  1353. xs *rsp = xs_http_request("POST", url, headers,
  1354. body, strlen(body), &status, NULL, NULL, 0);
  1355. rsp = xs_free(rsp);
  1356. srv_debug(0, xs_fmt("telegram post %d", status));
  1357. }
  1358. else
  1359. if (strcmp(type, "purge") == 0) {
  1360. srv_log(xs_dup("purge start"));
  1361. purge_all();
  1362. srv_log(xs_dup("purge end"));
  1363. }
  1364. else
  1365. srv_log(xs_fmt("unexpected q_item type '%s'", type));
  1366. }
  1367. int process_queue(void)
  1368. /* processes the global queue */
  1369. {
  1370. int cnt = 0;
  1371. xs *list = queue();
  1372. xs_list *p = list;
  1373. xs_str *fn;
  1374. while (xs_list_iter(&p, &fn)) {
  1375. xs *q_item = dequeue(fn);
  1376. if (q_item != NULL) {
  1377. job_post(q_item, 0);
  1378. cnt++;
  1379. }
  1380. }
  1381. return cnt;
  1382. }
  1383. /** HTTP handlers */
  1384. int activitypub_get_handler(const xs_dict *req, const char *q_path,
  1385. char **body, int *b_size, char **ctype)
  1386. {
  1387. int status = 200;
  1388. char *accept = xs_dict_get(req, "accept");
  1389. snac snac;
  1390. xs *msg = NULL;
  1391. if (accept == NULL)
  1392. return 0;
  1393. if (xs_str_in(accept, "application/activity+json") == -1 &&
  1394. xs_str_in(accept, "application/ld+json") == -1)
  1395. return 0;
  1396. xs *l = xs_split_n(q_path, "/", 2);
  1397. char *uid, *p_path;
  1398. uid = xs_list_get(l, 1);
  1399. if (!user_open(&snac, uid)) {
  1400. /* invalid user */
  1401. srv_debug(1, xs_fmt("activitypub_get_handler bad user %s", uid));
  1402. return 404;
  1403. }
  1404. p_path = xs_list_get(l, 2);
  1405. *ctype = "application/activity+json";
  1406. if (p_path == NULL) {
  1407. /* if there was no component after the user, it's an actor request */
  1408. msg = msg_actor(&snac);
  1409. *ctype = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"";
  1410. char *ua = xs_dict_get(req, "user-agent");
  1411. snac_debug(&snac, 0, xs_fmt("serving actor [%s]", ua ? ua : "No UA"));
  1412. }
  1413. else
  1414. if (strcmp(p_path, "outbox") == 0) {
  1415. xs *id = xs_fmt("%s/outbox", snac.actor);
  1416. xs *elems = timeline_simple_list(&snac, "public", 0, 20);
  1417. xs *list = xs_list_new();
  1418. msg = msg_collection(&snac, id);
  1419. char *p, *v;
  1420. p = elems;
  1421. while (xs_list_iter(&p, &v)) {
  1422. xs *i = NULL;
  1423. if (valid_status(object_get_by_md5(v, &i))) {
  1424. char *type = xs_dict_get(i, "type");
  1425. char *id = xs_dict_get(i, "id");
  1426. if (type && id && strcmp(type, "Note") == 0 && xs_startswith(id, snac.actor)) {
  1427. i = xs_dict_del(i, "_snac");
  1428. list = xs_list_append(list, i);
  1429. }
  1430. }
  1431. }
  1432. /* replace the 'orderedItems' with the latest posts */
  1433. xs *items = xs_number_new(xs_list_len(list));
  1434. msg = xs_dict_set(msg, "orderedItems", list);
  1435. msg = xs_dict_set(msg, "totalItems", items);
  1436. }
  1437. else
  1438. if (strcmp(p_path, "followers") == 0 || strcmp(p_path, "following") == 0) {
  1439. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  1440. msg = msg_collection(&snac, id);
  1441. }
  1442. else
  1443. if (xs_startswith(p_path, "p/")) {
  1444. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  1445. status = object_get(id, &msg);
  1446. }
  1447. else
  1448. status = 404;
  1449. if (status == 200 && msg != NULL) {
  1450. *body = xs_json_dumps_pp(msg, 4);
  1451. *b_size = strlen(*body);
  1452. }
  1453. snac_debug(&snac, 1, xs_fmt("activitypub_get_handler serving %s %d", q_path, status));
  1454. user_free(&snac);
  1455. return status;
  1456. }
  1457. int activitypub_post_handler(const xs_dict *req, const char *q_path,
  1458. char *payload, int p_size,
  1459. char **body, int *b_size, char **ctype)
  1460. /* processes an input message */
  1461. {
  1462. (void)b_size;
  1463. int status = 202; /* accepted */
  1464. char *i_ctype = xs_dict_get(req, "content-type");
  1465. snac snac;
  1466. char *v;
  1467. if (i_ctype == NULL) {
  1468. *body = xs_str_new("no content-type");
  1469. *ctype = "text/plain";
  1470. return 400;
  1471. }
  1472. if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
  1473. xs_str_in(i_ctype, "application/ld+json") == -1)
  1474. return 0;
  1475. /* decode the message */
  1476. xs *msg = xs_json_loads(payload);
  1477. if (msg == NULL) {
  1478. srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
  1479. *body = xs_str_new("JSON error");
  1480. *ctype = "text/plain";
  1481. status = 400;
  1482. }
  1483. /* get the user and path */
  1484. xs *l = xs_split_n(q_path, "/", 2);
  1485. char *uid;
  1486. if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
  1487. /* strange q_path */
  1488. srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
  1489. return 404;
  1490. }
  1491. uid = xs_list_get(l, 1);
  1492. if (!user_open(&snac, uid)) {
  1493. /* invalid user */
  1494. srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid));
  1495. return 404;
  1496. }
  1497. /* if it has a digest, check it now, because
  1498. later the payload won't be exactly the same */
  1499. if ((v = xs_dict_get(req, "digest")) != NULL) {
  1500. xs *s1 = xs_sha256_base64(payload, p_size);
  1501. xs *s2 = xs_fmt("SHA-256=%s", s1);
  1502. if (strcmp(s2, v) != 0) {
  1503. srv_log(xs_fmt("digest check FAILED"));
  1504. *body = xs_str_new("bad digest");
  1505. *ctype = "text/plain";
  1506. status = 400;
  1507. }
  1508. }
  1509. /* if the message is from a muted actor, reject it right now */
  1510. if (!xs_is_null(v = xs_dict_get(msg, "actor")) && *v) {
  1511. if (is_muted(&snac, v)) {
  1512. snac_log(&snac, xs_fmt("rejected message from MUTEd actor %s", v));
  1513. *body = xs_str_new("rejected");
  1514. *ctype = "text/plain";
  1515. status = 403;
  1516. }
  1517. }
  1518. if (valid_status(status)) {
  1519. enqueue_input(&snac, msg, req, 0);
  1520. *ctype = "application/activity+json";
  1521. }
  1522. user_free(&snac);
  1523. return status;
  1524. }