activitypub.c 55 KB

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