activitypub.c 55 KB

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