activitypub.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 grunfink et al. / 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 "xs_match.h"
  12. #include "snac.h"
  13. #include <sys/wait.h>
  14. const char *public_address = "https:/" "/www.w3.org/ns/activitystreams#Public";
  15. /* susie.png */
  16. const char *susie =
  17. "iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
  18. "CEkxzAAAAUUlEQVQoz43R0QkAMQwCUDdw/y3dwE"
  19. "vsvzlL4X1IoQkAisKmwfAFT3RgJHbQezpSRoXEq"
  20. "eqCL9BJBf7h3QbOCCxV5EVWMEMwG7K1/WODtlvx"
  21. "AYTtEsDU9F34AAAAAElFTkSuQmCC";
  22. const char *susie_cool =
  23. "iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
  24. "CEkxzAAAAV0lEQVQoz43RwQ3AMAwCQDZg/y3ZgN"
  25. "qo3+JaedwDOUQBQFHYaTB8wTM6sGl2cMPu+DFzn"
  26. "+ZcgN7wF7ZVihXkfSlWIVzIA6dbQzaygllpNuTX"
  27. "ZmmFNlvxADX1+o0cUPMbAAAAAElFTkSuQmCC";
  28. const char *susie_muertos =
  29. "iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
  30. "CEkxzAAAAV0lEQVQoz4XQsQ0AMQxCUW/A/lv+DT"
  31. "ic6zGRolekIMyMELNp8PiCEw6Q4w4NoAt53IH5m"
  32. "xXksrZYgZwJrIox+Z8vJAfe2lCxG6AK7eKkWcEb"
  33. "QHbF617xAQatAAD7jJHUAAAAAElFTkSuQmCC";
  34. const char *default_avatar_base64(void)
  35. /* returns the default avatar in base64 */
  36. {
  37. time_t t = time(NULL);
  38. struct tm tm;
  39. const char *p = susie;
  40. gmtime_r(&t, &tm);
  41. if (tm.tm_mon == 10 && tm.tm_mday == 2)
  42. p = susie_muertos;
  43. else
  44. if (tm.tm_wday == 0 || tm.tm_wday == 6)
  45. p = susie_cool;
  46. return p;
  47. }
  48. int activitypub_request(snac *snac, const char *url, xs_dict **data)
  49. /* request an object */
  50. {
  51. int status;
  52. xs *response = NULL;
  53. xs *payload = NULL;
  54. int p_size;
  55. char *ctype;
  56. /* get from the net */
  57. response = http_signed_request(snac, "GET", url,
  58. NULL, NULL, 0, &status, &payload, &p_size, 0);
  59. if (status == 0 || (status >= 500 && status <= 599)) {
  60. /* I found an instance running Misskey that returned
  61. 500 on signed messages but returned the object
  62. perfectly without signing (?), so why not try */
  63. xs_free(response);
  64. xs *hdrs = xs_dict_new();
  65. hdrs = xs_dict_append(hdrs, "accept", "application/activity+json");
  66. hdrs = xs_dict_append(hdrs, "user-agent", USER_AGENT);
  67. response = xs_http_request("GET", url, hdrs,
  68. NULL, 0, &status, &payload, &p_size, 0);
  69. }
  70. if (valid_status(status)) {
  71. /* ensure it's ActivityPub data */
  72. ctype = xs_dict_get(response, "content-type");
  73. if (xs_is_null(ctype))
  74. status = 400;
  75. else
  76. if (xs_str_in(ctype, "application/activity+json") != -1 ||
  77. xs_str_in(ctype, "application/ld+json") != -1)
  78. *data = xs_json_loads(payload);
  79. else
  80. status = 500;
  81. }
  82. if (!valid_status(status))
  83. *data = NULL;
  84. return status;
  85. }
  86. int actor_request(snac *snac, const char *actor, xs_dict **data)
  87. /* request an actor */
  88. {
  89. int status, status2;
  90. xs *payload = NULL;
  91. if (data)
  92. *data = NULL;
  93. /* get from disk first */
  94. status = actor_get(actor, data);
  95. if (status != 200) {
  96. /* actor data non-existent or stale: get from the net */
  97. status2 = activitypub_request(snac, actor, &payload);
  98. if (valid_status(status2)) {
  99. /* renew data */
  100. status = actor_add(actor, payload);
  101. if (data != NULL) {
  102. *data = payload;
  103. payload = NULL;
  104. }
  105. }
  106. }
  107. /* collect the (presumed) shared inbox in this actor */
  108. if (xs_type(xs_dict_get(srv_config, "disable_inbox_collection")) != XSTYPE_TRUE) {
  109. if (valid_status(status) && data && *data)
  110. inbox_add_by_actor(*data);
  111. }
  112. else
  113. srv_debug(2, xs_fmt("NOT collected"));
  114. return status;
  115. }
  116. int timeline_request(snac *snac, char **id, xs_str **wrk, int level)
  117. /* ensures that an entry and its ancestors are in the timeline */
  118. {
  119. int status = 0;
  120. if (level < 256 && !xs_is_null(*id)) {
  121. xs *msg = NULL;
  122. /* is the object already there? */
  123. if (!valid_status(object_get(*id, &msg))) {
  124. /* no; download it */
  125. status = activitypub_request(snac, *id, &msg);
  126. if (valid_status(status)) {
  127. xs_dict *object = msg;
  128. const char *type = xs_dict_get(object, "type");
  129. /* get the id again from the object, as it may be different */
  130. const char *nid = xs_dict_get(object, "id");
  131. if (xs_type(nid) != XSTYPE_STRING)
  132. return 0;
  133. if (wrk && strcmp(nid, *id) != 0) {
  134. snac_debug(snac, 1,
  135. xs_fmt("timeline_request canonical id for %s is %s", *id, nid));
  136. *wrk = xs_dup(nid);
  137. *id = *wrk;
  138. }
  139. if (xs_is_null(type))
  140. type = "(null)";
  141. srv_debug(1, xs_fmt("timeline_request type %s '%s'", nid, type));
  142. if (strcmp(type, "Create") == 0) {
  143. /* some software like lemmy nest Announce + Create + Note */
  144. if (!xs_is_null(object = xs_dict_get(object, "object"))) {
  145. type = xs_dict_get(object, "type");
  146. nid = xs_dict_get(object, "id");
  147. }
  148. else
  149. type = "(null)";
  150. }
  151. if (xs_match(type, "Note|Page|Article")) {
  152. const char *actor = xs_dict_get(object, "attributedTo");
  153. /* request (and drop) the actor for this entry */
  154. if (!xs_is_null(actor))
  155. actor_request(snac, actor, NULL);
  156. /* does it have an ancestor? */
  157. char *in_reply_to = xs_dict_get(object, "inReplyTo");
  158. /* store */
  159. timeline_add(snac, nid, object);
  160. /* recurse! */
  161. timeline_request(snac, &in_reply_to, NULL, level + 1);
  162. }
  163. }
  164. }
  165. enqueue_request_replies(snac, *id);
  166. }
  167. return status;
  168. }
  169. void timeline_request_replies(snac *user, const char *id)
  170. /* requests all replies of a message */
  171. /* FIXME: experimental -- needs more testing */
  172. {
  173. /* FIXME: TEMPORARILY DISABLED */
  174. /* Reason: I've found that many of the posts in the 'replies' Collection
  175. do not have an inReplyTo field (why??? aren't they 'replies'???).
  176. For this reason, these requested objects are not stored as children
  177. of the original post and they are shown as out-of-context, top level posts.
  178. This process is disabled until I find an elegant way of providing a parent
  179. for these 'stray' children. */
  180. return;
  181. xs *msg = NULL;
  182. if (!valid_status(object_get(id, &msg)))
  183. return;
  184. /* does it have a replies collection? */
  185. const xs_dict *replies = xs_dict_get(msg, "replies");
  186. if (!xs_is_null(replies)) {
  187. const char *type = xs_dict_get(replies, "type");
  188. const char *first = xs_dict_get(replies, "first");
  189. if (!xs_is_null(type) && !xs_is_null(first) && strcmp(type, "Collection") == 0) {
  190. const char *next = xs_dict_get(first, "next");
  191. if (!xs_is_null(next)) {
  192. xs *rpls = NULL;
  193. int status = activitypub_request(user, next, &rpls);
  194. /* request the Collection of replies */
  195. if (valid_status(status)) {
  196. xs_list *items = xs_dict_get(rpls, "items");
  197. if (xs_type(items) == XSTYPE_LIST) {
  198. xs_val *v;
  199. /* request them all */
  200. while (xs_list_iter(&items, &v)) {
  201. if (xs_type(v) == XSTYPE_DICT) {
  202. /* not an id, but the object itself (!) */
  203. const char *c_id = xs_dict_get(v, "id");
  204. if (!xs_is_null(id)) {
  205. snac_debug(user, 0, xs_fmt("embedded reply %s", c_id));
  206. object_add(c_id, v);
  207. /* get its own children */
  208. timeline_request_replies(user, v);
  209. }
  210. }
  211. else {
  212. snac_debug(user, 0, xs_fmt("request reply %s", v));
  213. timeline_request(user, &v, NULL, 0);
  214. }
  215. }
  216. }
  217. }
  218. else
  219. snac_debug(user, 0, xs_fmt("replies request error %s %d", next, status));
  220. }
  221. }
  222. }
  223. }
  224. int send_to_inbox_raw(const char *keyid, const char *seckey,
  225. const xs_str *inbox, const xs_dict *msg,
  226. xs_val **payload, int *p_size, int timeout)
  227. /* sends a message to an Inbox */
  228. {
  229. int status;
  230. xs_dict *response;
  231. xs *j_msg = xs_json_dumps((xs_dict *)msg, 4);
  232. response = http_signed_request_raw(keyid, seckey, "POST", inbox,
  233. NULL, j_msg, strlen(j_msg), &status, payload, p_size, timeout);
  234. xs_free(response);
  235. return status;
  236. }
  237. int send_to_inbox(snac *snac, const xs_str *inbox, const xs_dict *msg,
  238. xs_val **payload, int *p_size, int timeout)
  239. /* sends a message to an Inbox */
  240. {
  241. char *seckey = xs_dict_get(snac->key, "secret");
  242. return send_to_inbox_raw(snac->actor, seckey, inbox, msg, payload, p_size, timeout);
  243. }
  244. xs_str *get_actor_inbox(snac *snac, const char *actor)
  245. /* gets an actor's inbox */
  246. {
  247. xs *data = NULL;
  248. char *v = NULL;
  249. if (valid_status(actor_request(snac, actor, &data))) {
  250. /* try first endpoints/sharedInbox */
  251. if ((v = xs_dict_get(data, "endpoints")))
  252. v = xs_dict_get(v, "sharedInbox");
  253. /* try then the regular inbox */
  254. if (xs_is_null(v))
  255. v = xs_dict_get(data, "inbox");
  256. }
  257. return xs_is_null(v) ? NULL : xs_dup(v);
  258. }
  259. int send_to_actor(snac *snac, const char *actor, const xs_dict *msg,
  260. xs_val **payload, int *p_size, int timeout)
  261. /* sends a message to an actor */
  262. {
  263. int status = 400;
  264. xs *inbox = get_actor_inbox(snac, actor);
  265. if (!xs_is_null(inbox))
  266. status = send_to_inbox(snac, inbox, msg, payload, p_size, timeout);
  267. return status;
  268. }
  269. void post_message(snac *snac, const char *actor, const xs_dict *msg)
  270. /* posts a message immediately (bypassing the output queues) */
  271. {
  272. xs *payload = NULL;
  273. int p_size;
  274. int status = send_to_actor(snac, actor, msg, &payload, &p_size, 3);
  275. srv_log(xs_fmt("post_message to actor %s %d", actor, status));
  276. if (!valid_status(status))
  277. /* cannot send right now, enqueue */
  278. enqueue_message(snac, msg);
  279. }
  280. xs_list *recipient_list(snac *snac, const xs_dict *msg, int expand_public)
  281. /* returns the list of recipients for a message */
  282. {
  283. char *to = xs_dict_get(msg, "to");
  284. char *cc = xs_dict_get(msg, "cc");
  285. xs_set rcpts;
  286. int n;
  287. xs_set_init(&rcpts);
  288. char *lists[] = { to, cc, NULL };
  289. for (n = 0; lists[n]; n++) {
  290. char *l = lists[n];
  291. char *v;
  292. xs *tl = NULL;
  293. /* if it's a string, create a list with only one element */
  294. if (xs_type(l) == XSTYPE_STRING) {
  295. tl = xs_list_new();
  296. tl = xs_list_append(tl, l);
  297. l = tl;
  298. }
  299. while (xs_list_iter(&l, &v)) {
  300. if (expand_public && strcmp(v, public_address) == 0) {
  301. /* iterate the followers and add them */
  302. xs *fwers = follower_list(snac);
  303. char *actor;
  304. char *p = fwers;
  305. while (xs_list_iter(&p, &actor))
  306. xs_set_add(&rcpts, actor);
  307. }
  308. else
  309. xs_set_add(&rcpts, v);
  310. }
  311. }
  312. return xs_set_result(&rcpts);
  313. }
  314. int is_msg_public(const xs_dict *msg)
  315. /* checks if a message is public */
  316. {
  317. const char *to = xs_dict_get(msg, "to");
  318. const char *cc = xs_dict_get(msg, "cc");
  319. int n;
  320. const char *lists[] = { to, cc, NULL };
  321. for (n = 0; lists[n]; n++) {
  322. const xs_val *l = lists[n];
  323. if (xs_type(l) == XSTYPE_STRING) {
  324. if (strcmp(l, public_address) == 0)
  325. return 1;
  326. }
  327. else
  328. if (xs_type(l) == XSTYPE_LIST) {
  329. if (xs_list_in(l, public_address) != -1)
  330. return 1;
  331. }
  332. }
  333. return 0;
  334. }
  335. int is_msg_for_me(snac *snac, const xs_dict *c_msg)
  336. /* checks if this message is for me */
  337. {
  338. const char *type = xs_dict_get(c_msg, "type");
  339. if (xs_match(type, "Like|Announce")) {
  340. const char *object = xs_dict_get(c_msg, "object");
  341. if (xs_type(object) == XSTYPE_DICT)
  342. object = xs_dict_get(object, "id");
  343. /* bad object id? reject */
  344. if (xs_type(object) != XSTYPE_STRING)
  345. return 0;
  346. /* if it's about one of our posts, accept it */
  347. if (xs_startswith(object, snac->actor))
  348. return 2;
  349. /* if it's by someone we don't follow, reject */
  350. if (!following_check(snac, xs_dict_get(c_msg, "actor")))
  351. return 0;
  352. }
  353. /* if it's not a Create, allow */
  354. if (strcmp(type, "Create") != 0)
  355. return 1;
  356. xs_dict *msg = xs_dict_get(c_msg, "object");
  357. xs *rcpts = recipient_list(snac, msg, 0);
  358. xs_list *p = rcpts;
  359. xs_str *v;
  360. while(xs_list_iter(&p, &v)) {
  361. /* explicitly for me? accept */
  362. if (strcmp(v, snac->actor) == 0)
  363. return 2;
  364. /* for someone we follow? (probably cc'ed) accept */
  365. if (following_check(snac, v))
  366. return 5;
  367. }
  368. /* accept if it's by someone we follow */
  369. char *atto = xs_dict_get(msg, "attributedTo");
  370. if (!xs_is_null(atto) && following_check(snac, atto))
  371. return 3;
  372. /* is this message a reply to another? */
  373. char *irt = xs_dict_get(msg, "inReplyTo");
  374. if (!xs_is_null(irt)) {
  375. xs *r_msg = NULL;
  376. /* try to get the replied message */
  377. if (valid_status(object_get(irt, &r_msg))) {
  378. atto = xs_dict_get(r_msg, "attributedTo");
  379. /* accept if the replied message is from someone we follow */
  380. if (!xs_is_null(atto) && following_check(snac, atto))
  381. return 4;
  382. }
  383. }
  384. return 0;
  385. }
  386. xs_str *process_tags(snac *snac, const char *content, xs_list **tag)
  387. /* parses mentions and tags from content */
  388. {
  389. xs_str *nc = xs_str_new(NULL);
  390. xs_list *tl = *tag;
  391. xs *split;
  392. xs_list *p;
  393. xs_val *v;
  394. int n = 0;
  395. /* create a default server for incomplete mentions */
  396. xs *def_srv = NULL;
  397. if (xs_list_len(tl)) {
  398. /* if there are any mentions, get the server from
  399. the first one, which is the inReplyTo author */
  400. p = tl;
  401. while (xs_list_iter(&p, &v)) {
  402. const char *type = xs_dict_get(v, "type");
  403. const char *name = xs_dict_get(v, "name");
  404. if (type && name && strcmp(type, "Mention") == 0) {
  405. xs *l = xs_split(name, "@");
  406. def_srv = xs_dup(xs_list_get(l, -1));
  407. break;
  408. }
  409. }
  410. }
  411. if (xs_is_null(def_srv))
  412. /* use this same server */
  413. def_srv = xs_dup(xs_dict_get(srv_config, "host"));
  414. split = xs_regex_split(content, "(@[A-Za-z0-9_]+(@[A-Za-z0-9\\.-]+)?|&#[0-9]+;|#[^ ,\\.:;<]+)");
  415. p = split;
  416. while (xs_list_iter(&p, &v)) {
  417. if ((n & 0x1)) {
  418. if (*v == '@') {
  419. xs *link = NULL;
  420. xs *wuid = NULL;
  421. if (strchr(v + 1, '@') == NULL) {
  422. /* only one @? it's a dumb Mastodon-like mention
  423. without server; add the default one */
  424. wuid = xs_fmt("%s@%s", v, def_srv);
  425. snac_debug(snac, 2, xs_fmt("mention without server '%s' '%s'", v, wuid));
  426. }
  427. else
  428. wuid = xs_dup(v);
  429. /* query the webfinger about this fellow */
  430. xs *actor = NULL;
  431. xs *uid = NULL;
  432. int status;
  433. status = webfinger_request(wuid, &actor, &uid);
  434. if (valid_status(status)) {
  435. xs *d = xs_dict_new();
  436. xs *n = xs_fmt("@%s", uid);
  437. d = xs_dict_append(d, "type", "Mention");
  438. d = xs_dict_append(d, "href", actor);
  439. d = xs_dict_append(d, "name", n);
  440. tl = xs_list_append(tl, d);
  441. link = xs_fmt("<a href=\"%s\" class=\"u-url mention\">%s</a>", actor, n);
  442. }
  443. if (!xs_is_null(link))
  444. nc = xs_str_cat(nc, link);
  445. else
  446. nc = xs_str_cat(nc, v);
  447. }
  448. else
  449. if (*v == '#') {
  450. /* hashtag */
  451. xs *d = xs_dict_new();
  452. xs *n = xs_tolower_i(xs_dup(v));
  453. xs *h = xs_fmt("%s%s", snac->actor, n);
  454. xs *l = xs_fmt("<a href=\"%s\" class=\"mention hashtag\" rel=\"tag\">%s</a>", h, v);
  455. d = xs_dict_append(d, "type", "Hashtag");
  456. d = xs_dict_append(d, "href", h);
  457. d = xs_dict_append(d, "name", n);
  458. tl = xs_list_append(tl, d);
  459. /* add the code */
  460. nc = xs_str_cat(nc, l);
  461. }
  462. else
  463. if (*v == '&') {
  464. /* HTML Unicode entity, probably part of an emoji */
  465. /* write as is */
  466. nc = xs_str_cat(nc, v);
  467. }
  468. }
  469. else
  470. nc = xs_str_cat(nc, v);
  471. n++;
  472. }
  473. *tag = tl;
  474. return nc;
  475. }
  476. void notify(snac *snac, const char *type, const char *utype, const char *actor, const xs_dict *msg)
  477. /* notifies the user of relevant events */
  478. {
  479. const char *id = xs_dict_get(msg, "id");
  480. if (strcmp(type, "Create") == 0) {
  481. /* only notify of notes specifically for us */
  482. xs *rcpts = recipient_list(snac, msg, 0);
  483. if (xs_list_in(rcpts, snac->actor) == -1)
  484. return;
  485. /* discard votes */
  486. const xs_dict *note = xs_dict_get(msg, "object");
  487. if (note && !xs_is_null(xs_dict_get(note, "name")))
  488. return;
  489. }
  490. if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") != 0)
  491. return;
  492. /* get the object id */
  493. const char *objid = xs_dict_get(msg, "object");
  494. if (xs_type(objid) == XSTYPE_DICT)
  495. objid = xs_dict_get(objid, "id");
  496. if (strcmp(type, "Like") == 0 || strcmp(type, "Announce") == 0) {
  497. /* if it's not an admiration about something by us, done */
  498. if (xs_is_null(objid) || !xs_startswith(objid, snac->actor))
  499. return;
  500. }
  501. /* updated poll? */
  502. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0) {
  503. const xs_dict *poll;
  504. const char *poll_id;
  505. if ((poll = xs_dict_get(msg, "object")) == NULL)
  506. return;
  507. /* if it's not closed, discard */
  508. if (xs_is_null(xs_dict_get(poll, "closed")))
  509. return;
  510. if ((poll_id = xs_dict_get(poll, "id")) == NULL)
  511. return;
  512. /* if it's not ours and we didn't vote, discard */
  513. if (!xs_startswith(poll_id, snac->actor) && !was_question_voted(snac, poll_id))
  514. return;
  515. }
  516. /* user will love to know about this! */
  517. /* prepare message body */
  518. xs *body = xs_fmt("User : @%s@%s\n",
  519. xs_dict_get(snac->config, "uid"),
  520. xs_dict_get(srv_config, "host")
  521. );
  522. if (strcmp(utype, "(null)") != 0) {
  523. xs *s1 = xs_fmt("Type : %s + %s\n", type, utype);
  524. body = xs_str_cat(body, s1);
  525. }
  526. else {
  527. xs *s1 = xs_fmt("Type : %s\n", type);
  528. body = xs_str_cat(body, s1);
  529. }
  530. {
  531. xs *s1 = xs_fmt("Actor : %s\n", actor);
  532. body = xs_str_cat(body, s1);
  533. }
  534. if (objid != NULL) {
  535. xs *s1 = xs_fmt("Object: %s\n", objid);
  536. body = xs_str_cat(body, s1);
  537. }
  538. /* email */
  539. const char *email = "[disabled by admin]";
  540. if (xs_type(xs_dict_get(srv_config, "disable_email_notifications")) != XSTYPE_TRUE) {
  541. email = xs_dict_get(snac->config_o, "email");
  542. if (xs_is_null(email)) {
  543. email = xs_dict_get(snac->config, "email");
  544. if (xs_is_null(email))
  545. email = "[empty]";
  546. }
  547. }
  548. if (*email != '\0' && *email != '[') {
  549. snac_debug(snac, 1, xs_fmt("email notify %s %s %s", type, utype, actor));
  550. xs *subject = xs_fmt("snac notify for @%s@%s",
  551. xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"));
  552. xs *from = xs_fmt("snac-daemon <snac-daemon@%s>", xs_dict_get(srv_config, "host"));
  553. xs *header = xs_fmt(
  554. "From: %s\n"
  555. "To: %s\n"
  556. "Subject: %s\n"
  557. "\n",
  558. from, email, subject);
  559. xs *email_body = xs_fmt("%s%s", header, body);
  560. enqueue_email(email_body, 0);
  561. }
  562. /* telegram */
  563. char *bot = xs_dict_get(snac->config, "telegram_bot");
  564. char *chat_id = xs_dict_get(snac->config, "telegram_chat_id");
  565. if (!xs_is_null(bot) && !xs_is_null(chat_id) && *bot && *chat_id)
  566. enqueue_telegram(body, bot, chat_id);
  567. /* finally, store it in the notification folder */
  568. if (strcmp(type, "Follow") == 0)
  569. objid = id;
  570. else
  571. if (strcmp(utype, "Follow") == 0)
  572. objid = actor;
  573. notify_add(snac, type, utype, actor, objid != NULL ? objid : id);
  574. }
  575. /** messages **/
  576. xs_dict *msg_base(snac *snac, const char *type, const char *id,
  577. const char *actor, const char *date, const char *object)
  578. /* creates a base ActivityPub message */
  579. {
  580. xs *did = NULL;
  581. xs *published = NULL;
  582. xs *ntid = tid(0);
  583. const char *obj_id;
  584. if (xs_type(object) == XSTYPE_DICT)
  585. obj_id = xs_dict_get(object, "id");
  586. else
  587. obj_id = object;
  588. /* generated values */
  589. if (date && strcmp(date, "@now") == 0) {
  590. published = xs_str_utctime(0, ISO_DATE_SPEC);
  591. date = published;
  592. }
  593. if (id != NULL) {
  594. if (strcmp(id, "@dummy") == 0) {
  595. did = xs_fmt("%s/d/%s/%s", snac->actor, ntid, type);
  596. id = did;
  597. }
  598. else
  599. if (strcmp(id, "@object") == 0) {
  600. if (obj_id != NULL) {
  601. did = xs_fmt("%s/%s_%s", obj_id, type, ntid);
  602. id = did;
  603. }
  604. else
  605. id = NULL;
  606. }
  607. else
  608. if (strcmp(id, "@wrapper") == 0) {
  609. /* like @object, but always generate the same id */
  610. if (object != NULL) {
  611. date = xs_dict_get(object, "published");
  612. did = xs_fmt("%s/%s", obj_id, type);
  613. id = did;
  614. }
  615. else
  616. id = NULL;
  617. }
  618. }
  619. xs_dict *msg = xs_dict_new();
  620. msg = xs_dict_append(msg, "@context", "https:/" "/www.w3.org/ns/activitystreams");
  621. msg = xs_dict_append(msg, "type", type);
  622. if (id != NULL)
  623. msg = xs_dict_append(msg, "id", id);
  624. if (actor != NULL)
  625. msg = xs_dict_append(msg, "actor", actor);
  626. if (date != NULL)
  627. msg = xs_dict_append(msg, "published", date);
  628. if (object != NULL)
  629. msg = xs_dict_append(msg, "object", object);
  630. return msg;
  631. }
  632. xs_dict *msg_collection(snac *snac, char *id)
  633. /* creates an empty OrderedCollection message */
  634. {
  635. xs_dict *msg = msg_base(snac, "OrderedCollection", id, NULL, NULL, NULL);
  636. xs *ol = xs_list_new();
  637. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  638. msg = xs_dict_append(msg, "orderedItems", ol);
  639. msg = xs_dict_append(msg, "totalItems", xs_stock_0);
  640. return msg;
  641. }
  642. xs_dict *msg_accept(snac *snac, char *object, char *to)
  643. /* creates an Accept message (as a response to a Follow) */
  644. {
  645. xs_dict *msg = msg_base(snac, "Accept", "@dummy", snac->actor, NULL, object);
  646. msg = xs_dict_append(msg, "to", to);
  647. return msg;
  648. }
  649. xs_dict *msg_update(snac *snac, xs_dict *object)
  650. /* creates an Update message */
  651. {
  652. xs_dict *msg = msg_base(snac, "Update", "@object", snac->actor, "@now", object);
  653. char *type = xs_dict_get(object, "type");
  654. if (strcmp(type, "Note") == 0) {
  655. msg = xs_dict_append(msg, "to", xs_dict_get(object, "to"));
  656. msg = xs_dict_append(msg, "cc", xs_dict_get(object, "cc"));
  657. }
  658. else
  659. if (strcmp(type, "Person") == 0) {
  660. msg = xs_dict_append(msg, "to", public_address);
  661. /* also spam the people being followed, so that
  662. they have the newest information about who we are */
  663. xs *cc = following_list(snac);
  664. msg = xs_dict_append(msg, "cc", cc);
  665. }
  666. else
  667. msg = xs_dict_append(msg, "to", public_address);
  668. return msg;
  669. }
  670. xs_dict *msg_admiration(snac *snac, char *object, char *type)
  671. /* creates a Like or Announce message */
  672. {
  673. xs *a_msg = NULL;
  674. xs_dict *msg = NULL;
  675. xs *wrk = NULL;
  676. /* call the object */
  677. timeline_request(snac, &object, &wrk, 0);
  678. if (valid_status(object_get(object, &a_msg))) {
  679. xs *rcpts = xs_list_new();
  680. msg = msg_base(snac, type, "@dummy", snac->actor, "@now", object);
  681. if (is_msg_public(a_msg))
  682. rcpts = xs_list_append(rcpts, public_address);
  683. rcpts = xs_list_append(rcpts, xs_dict_get(a_msg, "attributedTo"));
  684. msg = xs_dict_append(msg, "to", rcpts);
  685. }
  686. else
  687. snac_log(snac, xs_fmt("msg_admiration cannot retrieve object %s", object));
  688. return msg;
  689. }
  690. xs_dict *msg_actor(snac *snac)
  691. /* create a Person message for this actor */
  692. {
  693. xs *ctxt = xs_list_new();
  694. xs *icon = xs_dict_new();
  695. xs *keys = xs_dict_new();
  696. xs *tags = xs_list_new();
  697. xs *avtr = NULL;
  698. xs *kid = NULL;
  699. xs *f_bio = NULL;
  700. xs_dict *msg = msg_base(snac, "Person", snac->actor, NULL, NULL, NULL);
  701. char *p;
  702. int n;
  703. /* change the @context (is this really necessary?) */
  704. ctxt = xs_list_append(ctxt, "https:/" "/www.w3.org/ns/activitystreams");
  705. ctxt = xs_list_append(ctxt, "https:/" "/w3id.org/security/v1");
  706. msg = xs_dict_set(msg, "@context", ctxt);
  707. msg = xs_dict_set(msg, "url", snac->actor);
  708. msg = xs_dict_set(msg, "name", xs_dict_get(snac->config, "name"));
  709. msg = xs_dict_set(msg, "preferredUsername", snac->uid);
  710. msg = xs_dict_set(msg, "published", xs_dict_get(snac->config, "published"));
  711. xs *f_bio_2 = not_really_markdown(xs_dict_get(snac->config, "bio"), NULL);
  712. f_bio = process_tags(snac, f_bio_2, &tags);
  713. msg = xs_dict_set(msg, "summary", f_bio);
  714. msg = xs_dict_set(msg, "tag", tags);
  715. char *folders[] = { "inbox", "outbox", "followers", "following", NULL };
  716. for (n = 0; folders[n]; n++) {
  717. xs *f = xs_fmt("%s/%s", snac->actor, folders[n]);
  718. msg = xs_dict_set(msg, folders[n], f);
  719. }
  720. p = xs_dict_get(snac->config, "avatar");
  721. if (*p == '\0')
  722. avtr = xs_fmt("%s/susie.png", srv_baseurl);
  723. else
  724. avtr = xs_dup(p);
  725. icon = xs_dict_append(icon, "type", "Image");
  726. icon = xs_dict_append(icon, "mediaType", xs_mime_by_ext(avtr));
  727. icon = xs_dict_append(icon, "url", avtr);
  728. msg = xs_dict_set(msg, "icon", icon);
  729. kid = xs_fmt("%s#main-key", snac->actor);
  730. keys = xs_dict_append(keys, "id", kid);
  731. keys = xs_dict_append(keys, "owner", snac->actor);
  732. keys = xs_dict_append(keys, "publicKeyPem", xs_dict_get(snac->key, "public"));
  733. msg = xs_dict_set(msg, "publicKey", keys);
  734. /* if the "bot" config field is set to true, change type to "Service" */
  735. if (xs_type(xs_dict_get(snac->config, "bot")) == XSTYPE_TRUE)
  736. msg = xs_dict_set(msg, "type", "Service");
  737. /* add the header image, if there is one defined */
  738. const char *header = xs_dict_get(snac->config, "header");
  739. if (!xs_is_null(header)) {
  740. xs *d = xs_dict_new();
  741. d = xs_dict_append(d, "type", "Image");
  742. d = xs_dict_append(d, "mediaType", xs_mime_by_ext(header));
  743. d = xs_dict_append(d, "url", header);
  744. msg = xs_dict_set(msg, "image", d);
  745. }
  746. /* add the metadata as attachments of PropertyValue */
  747. xs_dict *metadata = xs_dict_get(snac->config, "metadata");
  748. if (xs_type(metadata) == XSTYPE_DICT) {
  749. xs *attach = xs_list_new();
  750. xs_str *k;
  751. xs_str *v;
  752. while (xs_dict_iter(&metadata, &k, &v)) {
  753. xs *d = xs_dict_new();
  754. xs *k2 = encode_html(k);
  755. xs *v2 = NULL;
  756. if (xs_startswith(v, "https:")) {
  757. xs *t = encode_html(v);
  758. v2 = xs_fmt("<a href=\"%s\">%s</a>", t, t);
  759. }
  760. else
  761. v2 = encode_html(v);
  762. d = xs_dict_append(d, "type", "PropertyValue");
  763. d = xs_dict_append(d, "name", k2);
  764. d = xs_dict_append(d, "value", v2);
  765. attach = xs_list_append(attach, d);
  766. }
  767. msg = xs_dict_set(msg, "attachment", attach);
  768. }
  769. return msg;
  770. }
  771. xs_dict *msg_create(snac *snac, const xs_dict *object)
  772. /* creates a 'Create' message */
  773. {
  774. xs_dict *msg = msg_base(snac, "Create", "@wrapper", snac->actor, NULL, object);
  775. xs_val *v;
  776. if ((v = xs_dict_get(object, "attributedTo")))
  777. msg = xs_dict_append(msg, "attributedTo", v);
  778. if ((v = xs_dict_get(object, "cc")))
  779. msg = xs_dict_append(msg, "cc", v);
  780. if ((v = xs_dict_get(object, "to")))
  781. msg = xs_dict_append(msg, "to", v);
  782. else
  783. msg = xs_dict_append(msg, "to", public_address);
  784. return msg;
  785. }
  786. xs_dict *msg_undo(snac *snac, char *object)
  787. /* creates an 'Undo' message */
  788. {
  789. xs_dict *msg = msg_base(snac, "Undo", "@object", snac->actor, "@now", object);
  790. const char *to;
  791. if (xs_type(object) == XSTYPE_DICT && (to = xs_dict_get(object, "object")))
  792. msg = xs_dict_append(msg, "to", to);
  793. return msg;
  794. }
  795. xs_dict *msg_delete(snac *snac, char *id)
  796. /* creates a 'Delete' + 'Tombstone' for a local entry */
  797. {
  798. xs *tomb = xs_dict_new();
  799. xs_dict *msg = NULL;
  800. /* sculpt the tombstone */
  801. tomb = xs_dict_append(tomb, "type", "Tombstone");
  802. tomb = xs_dict_append(tomb, "id", id);
  803. /* now create the Delete */
  804. msg = msg_base(snac, "Delete", "@object", snac->actor, "@now", tomb);
  805. msg = xs_dict_append(msg, "to", public_address);
  806. return msg;
  807. }
  808. xs_dict *msg_follow(snac *snac, const char *q)
  809. /* creates a 'Follow' message */
  810. {
  811. xs *actor_o = NULL;
  812. xs *actor = NULL;
  813. xs_dict *msg = NULL;
  814. int status;
  815. xs *url_or_uid = xs_strip_i(xs_str_new(q));
  816. if (xs_startswith(url_or_uid, "https:/"))
  817. actor = xs_dup(url_or_uid);
  818. else
  819. if (!valid_status(webfinger_request(url_or_uid, &actor, NULL)) || actor == NULL) {
  820. snac_log(snac, xs_fmt("cannot resolve user %s to follow", url_or_uid));
  821. return NULL;
  822. }
  823. /* request the actor */
  824. status = actor_request(snac, actor, &actor_o);
  825. if (valid_status(status)) {
  826. /* check if the actor is an alias */
  827. char *r_actor = xs_dict_get(actor_o, "id");
  828. if (r_actor && strcmp(actor, r_actor) != 0) {
  829. snac_log(snac, xs_fmt("actor to follow is an alias %s -> %s", actor, r_actor));
  830. }
  831. msg = msg_base(snac, "Follow", "@dummy", snac->actor, NULL, r_actor);
  832. }
  833. else
  834. snac_log(snac, xs_fmt("cannot get actor to follow %s %d", actor, status));
  835. return msg;
  836. }
  837. xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts,
  838. xs_str *in_reply_to, xs_list *attach, int priv)
  839. /* creates a 'Note' message */
  840. {
  841. xs *ntid = tid(0);
  842. xs *id = xs_fmt("%s/p/%s", snac->actor, ntid);
  843. xs *ctxt = NULL;
  844. xs *fc2 = NULL;
  845. xs *fc1 = NULL;
  846. xs *to = NULL;
  847. xs *cc = xs_list_new();
  848. xs *irt = NULL;
  849. xs *tag = xs_list_new();
  850. xs *atls = xs_list_new();
  851. xs_dict *msg = msg_base(snac, "Note", id, NULL, "@now", NULL);
  852. xs_list *p;
  853. xs_val *v;
  854. if (rcpts == NULL)
  855. to = xs_list_new();
  856. else {
  857. if (xs_type(rcpts) == XSTYPE_STRING) {
  858. to = xs_list_new();
  859. to = xs_list_append(to, rcpts);
  860. }
  861. else
  862. to = xs_dup(rcpts);
  863. }
  864. /* format the content */
  865. fc2 = not_really_markdown(content, &atls);
  866. if (in_reply_to != NULL && *in_reply_to) {
  867. xs *p_msg = NULL;
  868. xs *wrk = NULL;
  869. /* demand this thing */
  870. timeline_request(snac, &in_reply_to, &wrk, 0);
  871. if (valid_status(object_get(in_reply_to, &p_msg))) {
  872. /* add this author as recipient */
  873. char *a, *v;
  874. if ((a = xs_dict_get(p_msg, "attributedTo")) && xs_list_in(to, a) == -1)
  875. to = xs_list_append(to, a);
  876. /* add this author to the tag list as a mention */
  877. xs *t_href = NULL;
  878. xs *t_name = NULL;
  879. if (!xs_is_null(a) && valid_status(webfinger_request(a, &t_href, &t_name))) {
  880. xs *t = xs_dict_new();
  881. t = xs_dict_append(t, "type", "Mention");
  882. t = xs_dict_append(t, "href", t_href);
  883. t = xs_dict_append(t, "name", t_name);
  884. tag = xs_list_append(tag, t);
  885. }
  886. /* get the context, if there is one */
  887. if ((v = xs_dict_get(p_msg, "context")))
  888. ctxt = xs_dup(v);
  889. /* propagate the conversation field, if there is one */
  890. if ((v = xs_dict_get(p_msg, "conversation")))
  891. msg = xs_dict_append(msg, "conversation", v);
  892. /* if this message is public, ours will also be */
  893. if (!priv && is_msg_public(p_msg) && xs_list_in(to, public_address) == -1)
  894. to = xs_list_append(to, public_address);
  895. }
  896. irt = xs_dup(in_reply_to);
  897. }
  898. else
  899. irt = xs_val_new(XSTYPE_NULL);
  900. /* extract the mentions and hashtags and convert the content */
  901. fc1 = process_tags(snac, fc2, &tag);
  902. /* create the attachment list, if there are any */
  903. if (!xs_is_null(attach)) {
  904. while (xs_list_iter(&attach, &v)) {
  905. xs *d = xs_dict_new();
  906. const char *url = xs_list_get(v, 0);
  907. const char *alt = xs_list_get(v, 1);
  908. const char *mime = xs_mime_by_ext(url);
  909. d = xs_dict_append(d, "mediaType", mime);
  910. d = xs_dict_append(d, "url", url);
  911. d = xs_dict_append(d, "name", alt);
  912. d = xs_dict_append(d, "type",
  913. xs_startswith(mime, "image/") ? "Image" : "Document");
  914. atls = xs_list_append(atls, d);
  915. }
  916. }
  917. if (ctxt == NULL)
  918. ctxt = xs_fmt("%s#ctxt", id);
  919. /* add all mentions to the cc */
  920. p = tag;
  921. while (xs_list_iter(&p, &v)) {
  922. if (xs_type(v) == XSTYPE_DICT) {
  923. char *t;
  924. if ((t = xs_dict_get(v, "type")) != NULL && strcmp(t, "Mention") == 0) {
  925. if ((t = xs_dict_get(v, "href")) != NULL)
  926. cc = xs_list_append(cc, t);
  927. }
  928. }
  929. }
  930. /* no recipients? must be for everybody */
  931. if (!priv && xs_list_len(to) == 0)
  932. to = xs_list_append(to, public_address);
  933. /* delete all cc recipients that also are in the to */
  934. p = to;
  935. while (xs_list_iter(&p, &v)) {
  936. int i;
  937. if ((i = xs_list_in(cc, v)) != -1)
  938. cc = xs_list_del(cc, i);
  939. }
  940. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  941. msg = xs_dict_append(msg, "summary", "");
  942. msg = xs_dict_append(msg, "content", fc1);
  943. msg = xs_dict_append(msg, "context", ctxt);
  944. msg = xs_dict_append(msg, "url", id);
  945. msg = xs_dict_append(msg, "to", to);
  946. msg = xs_dict_append(msg, "cc", cc);
  947. msg = xs_dict_append(msg, "inReplyTo", irt);
  948. msg = xs_dict_append(msg, "tag", tag);
  949. msg = xs_dict_append(msg, "sourceContent", content);
  950. if (xs_list_len(atls))
  951. msg = xs_dict_append(msg, "attachment", atls);
  952. return msg;
  953. }
  954. xs_dict *msg_ping(snac *user, const char *rcpt)
  955. /* creates a Ping message (https://humungus.tedunangst.com/r/honk/v/tip/f/docs/ping.txt) */
  956. {
  957. xs_dict *msg = msg_base(user, "Ping", "@dummy", user->actor, NULL, NULL);
  958. msg = xs_dict_append(msg, "to", rcpt);
  959. return msg;
  960. }
  961. xs_dict *msg_pong(snac *user, const char *rcpt, const char *object)
  962. /* creates a Pong message (https://humungus.tedunangst.com/r/honk/v/tip/f/docs/ping.txt) */
  963. {
  964. xs_dict *msg = msg_base(user, "Pong", "@dummy", user->actor, NULL, object);
  965. msg = xs_dict_append(msg, "to", rcpt);
  966. return msg;
  967. }
  968. xs_dict *msg_question(snac *user, const char *content, xs_list *attach,
  969. const xs_list *opts, int multiple, int end_secs)
  970. /* creates a Question message */
  971. {
  972. xs_dict *msg = msg_note(user, content, NULL, NULL, attach, 0);
  973. int max = 8;
  974. xs_set seen;
  975. msg = xs_dict_set(msg, "type", "Question");
  976. /* make it non-editable */
  977. msg = xs_dict_del(msg, "sourceContent");
  978. xs *o = xs_list_new();
  979. xs_list *p = (xs_list *)opts;
  980. xs_str *v;
  981. xs *replies = xs_json_loads("{\"type\":\"Collection\",\"totalItems\":0}");
  982. xs_set_init(&seen);
  983. while (max && xs_list_iter(&p, &v)) {
  984. if (*v) {
  985. xs *v2 = xs_dup(v);
  986. xs *d = xs_dict_new();
  987. if (strlen(v2) > 60) {
  988. v2[60] = '\0';
  989. v2 = xs_str_cat(v2, "...");
  990. }
  991. if (xs_set_add(&seen, v2) == 1) {
  992. d = xs_dict_append(d, "name", v2);
  993. d = xs_dict_append(d, "replies", replies);
  994. o = xs_list_append(o, d);
  995. max--;
  996. }
  997. }
  998. }
  999. xs_set_free(&seen);
  1000. msg = xs_dict_append(msg, multiple ? "anyOf" : "oneOf", o);
  1001. /* set the end time */
  1002. time_t t = time(NULL) + end_secs;
  1003. xs *et = xs_str_utctime(t, ISO_DATE_SPEC);
  1004. msg = xs_dict_append(msg, "endTime", et);
  1005. return msg;
  1006. }
  1007. int update_question(snac *user, const char *id)
  1008. /* updates the poll counts */
  1009. {
  1010. xs *msg = NULL;
  1011. xs *rcnt = xs_dict_new();
  1012. xs *lopts = xs_list_new();
  1013. xs_list *opts;
  1014. xs_list *p;
  1015. xs_val *v;
  1016. /* get the object */
  1017. if (!valid_status(object_get(id, &msg)))
  1018. return -1;
  1019. /* closed? do nothing more */
  1020. if (xs_dict_get(msg, "closed"))
  1021. return -2;
  1022. /* get the options */
  1023. if ((opts = xs_dict_get(msg, "oneOf")) == NULL &&
  1024. (opts = xs_dict_get(msg, "anyOf")) == NULL)
  1025. return -3;
  1026. /* fill the initial count */
  1027. p = opts;
  1028. while (xs_list_iter(&p, &v)) {
  1029. const char *name = xs_dict_get(v, "name");
  1030. if (name) {
  1031. lopts = xs_list_append(lopts, name);
  1032. rcnt = xs_dict_set(rcnt, name, xs_stock_0);
  1033. }
  1034. }
  1035. xs_set s;
  1036. xs_set_init(&s);
  1037. /* iterate now the children (the votes) */
  1038. xs *chld = object_children(id);
  1039. p = chld;
  1040. while (xs_list_iter(&p, &v)) {
  1041. xs *obj = NULL;
  1042. if (!valid_status(object_get_by_md5(v, &obj)))
  1043. continue;
  1044. const char *name = xs_dict_get(obj, "name");
  1045. const char *atto = xs_dict_get(obj, "attributedTo");
  1046. if (name && atto) {
  1047. /* get the current count */
  1048. const xs_number *cnt = xs_dict_get(rcnt, name);
  1049. if (xs_type(cnt) == XSTYPE_NUMBER) {
  1050. /* if it exists, increment */
  1051. xs *ucnt = xs_number_new(xs_number_get(cnt) + 1);
  1052. rcnt = xs_dict_set(rcnt, name, ucnt);
  1053. xs_set_add(&s, atto);
  1054. }
  1055. }
  1056. }
  1057. xs *rcpts = xs_set_result(&s);
  1058. /* create a new list of options with their new counts */
  1059. xs *nopts = xs_list_new();
  1060. p = lopts;
  1061. while (xs_list_iter(&p, &v)) {
  1062. const xs_number *cnt = xs_dict_get(rcnt, v);
  1063. if (xs_type(cnt) == XSTYPE_NUMBER) {
  1064. xs *d1 = xs_dict_new();
  1065. xs *d2 = xs_dict_new();
  1066. d2 = xs_dict_append(d2, "type", "Collection");
  1067. d2 = xs_dict_append(d2, "totalItems", cnt);
  1068. d1 = xs_dict_append(d1, "type", "Note");
  1069. d1 = xs_dict_append(d1, "name", v);
  1070. d1 = xs_dict_append(d1, "replies", d2);
  1071. nopts = xs_list_append(nopts, d1);
  1072. }
  1073. }
  1074. /* update the list */
  1075. msg = xs_dict_set(msg, xs_dict_get(msg, "oneOf") != NULL ? "oneOf" : "anyOf", nopts);
  1076. /* due date? */
  1077. int closed = 0;
  1078. const char *end_time = xs_dict_get(msg, "endTime");
  1079. if (!xs_is_null(end_time)) {
  1080. xs *now = xs_str_utctime(0, ISO_DATE_SPEC);
  1081. /* is now greater than the endTime? */
  1082. if (strcmp(now, end_time) >= 0) {
  1083. xs *et = xs_dup(end_time);
  1084. msg = xs_dict_set(msg, "closed", et);
  1085. closed = 1;
  1086. }
  1087. }
  1088. /* update the count of voters */
  1089. xs *vcnt = xs_number_new(xs_list_len(rcpts));
  1090. msg = xs_dict_set(msg, "votersCount", vcnt);
  1091. msg = xs_dict_set(msg, "cc", rcpts);
  1092. /* store */
  1093. object_add_ow(id, msg);
  1094. snac_debug(user, 1, xs_fmt("recounted poll %s", id));
  1095. timeline_touch(user);
  1096. /* send an update message to all voters */
  1097. xs *u_msg = msg_update(user, msg);
  1098. u_msg = xs_dict_set(u_msg, "cc", rcpts);
  1099. enqueue_message(user, u_msg);
  1100. if (closed) {
  1101. xs *c_msg = msg_update(user, msg);
  1102. notify(user, "Update", "Question", user->actor, c_msg);
  1103. }
  1104. return 0;
  1105. }
  1106. /** queues **/
  1107. int process_input_message(snac *snac, xs_dict *msg, xs_dict *req)
  1108. /* processes an ActivityPub message from the input queue */
  1109. {
  1110. /* actor and type exist, were checked previously */
  1111. char *actor = xs_dict_get(msg, "actor");
  1112. char *type = xs_dict_get(msg, "type");
  1113. xs *actor_o = NULL;
  1114. int a_status;
  1115. int do_notify = 0;
  1116. /* question votes may not have a type */
  1117. if (xs_is_null(type))
  1118. type = "Note";
  1119. /* reject uninteresting messages right now */
  1120. if (strcmp(type, "Add") == 0) {
  1121. snac_debug(snac, 0, xs_fmt("Ignored message of type '%s'", type));
  1122. return 1;
  1123. }
  1124. if (xs_is_null(actor)) {
  1125. snac_debug(snac, 0, xs_fmt("malformed message"));
  1126. return 1;
  1127. }
  1128. char *object, *utype;
  1129. object = xs_dict_get(msg, "object");
  1130. if (object != NULL && xs_type(object) == XSTYPE_DICT)
  1131. utype = xs_dict_get(object, "type");
  1132. else
  1133. utype = "(null)";
  1134. /* reject messages that are not for this user */
  1135. if (!is_msg_for_me(snac, msg)) {
  1136. snac_debug(snac, 1, xs_fmt("message from %s of type '%s' not for us", actor, type));
  1137. return 1;
  1138. }
  1139. /* if it's a DM from someone we don't follow, reject the message */
  1140. if (xs_type(xs_dict_get(snac->config, "drop_dm_from_unknown")) == XSTYPE_TRUE) {
  1141. if (strcmp(utype, "Note") == 0 && !is_msg_public(msg) &&
  1142. !following_check(snac, actor)) {
  1143. snac_log(snac, xs_fmt("DM rejected from unknown actor %s", actor));
  1144. return 1;
  1145. }
  1146. }
  1147. /* bring the actor */
  1148. a_status = actor_request(snac, actor, &actor_o);
  1149. /* do not retry permanent failures */
  1150. if (a_status == 404 || a_status == 410 || a_status < 0) {
  1151. snac_debug(snac, 1,
  1152. xs_fmt("dropping message due to actor error %s %d", actor, a_status));
  1153. return 1;
  1154. }
  1155. if (!valid_status(a_status)) {
  1156. /* other actor download errors may need a retry */
  1157. snac_debug(snac, 1,
  1158. xs_fmt("error requesting actor %s %d -- retry later", actor, a_status));
  1159. return 0;
  1160. }
  1161. /* check the signature */
  1162. xs *sig_err = NULL;
  1163. if (!check_signature(snac, req, &sig_err)) {
  1164. snac_log(snac, xs_fmt("bad signature %s (%s)", actor, sig_err));
  1165. srv_archive_error("check_signature", sig_err, req, msg);
  1166. return 1;
  1167. }
  1168. if (strcmp(type, "Follow") == 0) { /** **/
  1169. if (!follower_check(snac, actor)) {
  1170. xs *f_msg = xs_dup(msg);
  1171. xs *reply = msg_accept(snac, f_msg, actor);
  1172. post_message(snac, actor, reply);
  1173. if (xs_is_null(xs_dict_get(f_msg, "published"))) {
  1174. /* add a date if it doesn't include one (Mastodon) */
  1175. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  1176. f_msg = xs_dict_set(f_msg, "published", date);
  1177. }
  1178. timeline_add(snac, xs_dict_get(f_msg, "id"), f_msg);
  1179. follower_add(snac, actor);
  1180. snac_log(snac, xs_fmt("new follower %s", actor));
  1181. do_notify = 1;
  1182. }
  1183. else
  1184. snac_log(snac, xs_fmt("repeated 'Follow' from %s", actor));
  1185. }
  1186. else
  1187. if (strcmp(type, "Undo") == 0) { /** **/
  1188. if (strcmp(utype, "Follow") == 0) { /** **/
  1189. if (valid_status(follower_del(snac, actor))) {
  1190. snac_log(snac, xs_fmt("no longer following us %s", actor));
  1191. do_notify = 1;
  1192. }
  1193. else
  1194. snac_log(snac, xs_fmt("error deleting follower %s", actor));
  1195. }
  1196. else
  1197. snac_debug(snac, 1, xs_fmt("ignored 'Undo' for object type '%s'", utype));
  1198. }
  1199. else
  1200. if (strcmp(type, "Create") == 0) { /** **/
  1201. if (is_muted(snac, actor))
  1202. snac_log(snac, xs_fmt("ignored 'Create' + '%s' from muted actor %s", utype, actor));
  1203. if (strcmp(utype, "Note") == 0) { /** **/
  1204. char *id = xs_dict_get(object, "id");
  1205. char *in_reply_to = xs_dict_get(object, "inReplyTo");
  1206. xs *wrk = NULL;
  1207. timeline_request(snac, &in_reply_to, &wrk, 0);
  1208. if (timeline_add(snac, id, object)) {
  1209. snac_log(snac, xs_fmt("new 'Note' %s %s", actor, id));
  1210. do_notify = 1;
  1211. }
  1212. /* if it has a "name" field, it may be a vote for a question */
  1213. const char *name = xs_dict_get(object, "name");
  1214. if (!xs_is_null(name) && *name && !xs_is_null(in_reply_to) && *in_reply_to)
  1215. update_question(snac, in_reply_to);
  1216. }
  1217. else
  1218. if (strcmp(utype, "Question") == 0) { /** **/
  1219. char *id = xs_dict_get(object, "id");
  1220. if (timeline_add(snac, id, object))
  1221. snac_log(snac, xs_fmt("new 'Question' %s %s", actor, id));
  1222. }
  1223. else
  1224. snac_debug(snac, 1, xs_fmt("ignored 'Create' for object type '%s'", utype));
  1225. }
  1226. else
  1227. if (strcmp(type, "Accept") == 0) { /** **/
  1228. if (strcmp(utype, "(null)") == 0) {
  1229. const char *obj_id = xs_dict_get(msg, "object");
  1230. /* if the accepted object id is a string that may
  1231. be created by us, it's a follow */
  1232. if (xs_type(obj_id) == XSTYPE_STRING &&
  1233. xs_startswith(obj_id, srv_baseurl) &&
  1234. xs_endswith(obj_id, "/Follow"))
  1235. utype = "Follow";
  1236. }
  1237. if (strcmp(utype, "Follow") == 0) { /** **/
  1238. if (following_check(snac, actor)) {
  1239. following_add(snac, actor, msg);
  1240. snac_log(snac, xs_fmt("confirmed follow from %s", actor));
  1241. }
  1242. else
  1243. snac_log(snac, xs_fmt("spurious follow accept from %s", actor));
  1244. }
  1245. else
  1246. if (strcmp(utype, "Create") == 0) {
  1247. /* some implementations send Create confirmations, go figure */
  1248. snac_debug(snac, 1, xs_dup("ignored 'Accept' + 'Create'"));
  1249. }
  1250. else {
  1251. srv_archive_error("accept", "ignored Accept", req, msg);
  1252. snac_debug(snac, 1, xs_fmt("ignored 'Accept' for object type '%s'", utype));
  1253. }
  1254. }
  1255. else
  1256. if (strcmp(type, "Like") == 0) { /** **/
  1257. if (xs_type(object) == XSTYPE_DICT)
  1258. object = xs_dict_get(object, "id");
  1259. timeline_admire(snac, object, actor, 1);
  1260. snac_log(snac, xs_fmt("new 'Like' %s %s", actor, object));
  1261. do_notify = 1;
  1262. }
  1263. else
  1264. if (strcmp(type, "Announce") == 0) { /** **/
  1265. if (xs_type(object) == XSTYPE_DICT)
  1266. object = xs_dict_get(object, "id");
  1267. if (is_limited(snac, actor) && !xs_startswith(object, snac->actor))
  1268. snac_log(snac, xs_fmt("dropped 'Announce' from limited actor %s", actor));
  1269. else {
  1270. xs *a_msg = NULL;
  1271. xs *wrk = NULL;
  1272. timeline_request(snac, &object, &wrk, 0);
  1273. if (valid_status(object_get(object, &a_msg))) {
  1274. const char *who = xs_dict_get(a_msg, "attributedTo");
  1275. if (who && !is_muted(snac, who)) {
  1276. /* bring the actor */
  1277. xs *who_o = NULL;
  1278. if (valid_status(actor_request(snac, who, &who_o))) {
  1279. timeline_admire(snac, object, actor, 0);
  1280. snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object));
  1281. do_notify = 1;
  1282. }
  1283. else
  1284. snac_log(snac, xs_fmt("dropped 'Announce' on actor request error %s", who));
  1285. }
  1286. else
  1287. snac_log(snac, xs_fmt("ignored 'Announce' about muted actor %s", who));
  1288. }
  1289. else
  1290. snac_debug(snac, 1, xs_fmt("error requesting 'Announce' object %s", object));
  1291. }
  1292. }
  1293. else
  1294. if (strcmp(type, "Update") == 0) { /** **/
  1295. if (xs_match(utype, "Person|Service")) { /** **/
  1296. actor_add(actor, xs_dict_get(msg, "object"));
  1297. timeline_touch(snac);
  1298. snac_log(snac, xs_fmt("updated actor %s", actor));
  1299. }
  1300. else
  1301. if (xs_match(utype, "Note|Page|Article")) { /** **/
  1302. const char *id = xs_dict_get(object, "id");
  1303. object_add_ow(id, object);
  1304. timeline_touch(snac);
  1305. snac_log(snac, xs_fmt("updated post %s", id));
  1306. }
  1307. else
  1308. if (strcmp(utype, "Question") == 0) { /** **/
  1309. const char *id = xs_dict_get(object, "id");
  1310. const char *closed = xs_dict_get(object, "closed");
  1311. object_add_ow(id, object);
  1312. timeline_touch(snac);
  1313. snac_log(snac, xs_fmt("%s poll %s", closed == NULL ? "updated" : "closed", id));
  1314. if (closed != NULL)
  1315. do_notify = 1;
  1316. }
  1317. else
  1318. snac_log(snac, xs_fmt("ignored 'Update' for object type '%s'", utype));
  1319. }
  1320. else
  1321. if (strcmp(type, "Delete") == 0) { /** **/
  1322. if (xs_type(object) == XSTYPE_DICT)
  1323. object = xs_dict_get(object, "id");
  1324. if (valid_status(timeline_del(snac, object)))
  1325. snac_debug(snac, 1, xs_fmt("new 'Delete' %s %s", actor, object));
  1326. else
  1327. snac_debug(snac, 1, xs_fmt("ignored 'Delete' for unknown object %s", object));
  1328. }
  1329. else
  1330. if (strcmp(type, "Pong") == 0) { /** **/
  1331. snac_log(snac, xs_fmt("'Pong' received from %s", actor));
  1332. }
  1333. else
  1334. if (strcmp(type, "Ping") == 0) { /** **/
  1335. snac_log(snac, xs_fmt("'Ping' requested from %s", actor));
  1336. xs *rsp = msg_pong(snac, actor, xs_dict_get(msg, "id"));
  1337. enqueue_output_by_actor(snac, rsp, actor, 0);
  1338. }
  1339. else
  1340. snac_debug(snac, 1, xs_fmt("process_input_message type '%s' ignored", type));
  1341. if (do_notify) {
  1342. notify(snac, type, utype, actor, msg);
  1343. timeline_touch(snac);
  1344. }
  1345. return 1;
  1346. }
  1347. int send_email(char *msg)
  1348. /* invoke sendmail with email headers and body in msg */
  1349. {
  1350. FILE *f;
  1351. int status;
  1352. int fds[2];
  1353. pid_t pid;
  1354. if (pipe(fds) == -1) return -1;
  1355. pid = vfork();
  1356. if (pid == -1) return -1;
  1357. else if (pid == 0) {
  1358. dup2(fds[0], 0);
  1359. close(fds[0]);
  1360. close(fds[1]);
  1361. execl("/usr/sbin/sendmail", "sendmail", "-t", (char *) NULL);
  1362. _exit(1);
  1363. }
  1364. close(fds[0]);
  1365. if ((f = fdopen(fds[1], "w")) == NULL) {
  1366. close(fds[1]);
  1367. return -1;
  1368. }
  1369. fprintf(f, "%s\n", msg);
  1370. fclose(f);
  1371. if (waitpid(pid, &status, 0) == -1) return -1;
  1372. return status;
  1373. }
  1374. void process_user_queue_item(snac *snac, xs_dict *q_item)
  1375. /* processes an item from the user queue */
  1376. {
  1377. char *type;
  1378. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  1379. if ((type = xs_dict_get(q_item, "type")) == NULL)
  1380. type = "output";
  1381. if (strcmp(type, "message") == 0) {
  1382. xs_dict *msg = xs_dict_get(q_item, "message");
  1383. xs *rcpts = recipient_list(snac, msg, 1);
  1384. xs_set inboxes;
  1385. xs_list *p;
  1386. xs_str *actor;
  1387. xs_set_init(&inboxes);
  1388. /* iterate the recipients */
  1389. p = rcpts;
  1390. while (xs_list_iter(&p, &actor)) {
  1391. xs *inbox = get_actor_inbox(snac, actor);
  1392. if (inbox != NULL) {
  1393. /* add to the set and, if it's not there, send message */
  1394. if (xs_set_add(&inboxes, inbox) == 1)
  1395. enqueue_output(snac, msg, inbox, 0);
  1396. }
  1397. else
  1398. snac_log(snac, xs_fmt("cannot find inbox for %s", actor));
  1399. }
  1400. /* if it's public, send to the collected inboxes */
  1401. if (is_msg_public(msg)) {
  1402. xs *shibx = inbox_list();
  1403. xs_str *inbox;
  1404. p = shibx;
  1405. while (xs_list_iter(&p, &inbox)) {
  1406. if (xs_set_add(&inboxes, inbox) == 1)
  1407. enqueue_output(snac, msg, inbox, 0);
  1408. }
  1409. }
  1410. xs_set_free(&inboxes);
  1411. }
  1412. else
  1413. if (strcmp(type, "input") == 0) {
  1414. /* process the message */
  1415. xs_dict *msg = xs_dict_get(q_item, "message");
  1416. xs_dict *req = xs_dict_get(q_item, "req");
  1417. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  1418. if (xs_is_null(msg))
  1419. return;
  1420. if (!process_input_message(snac, msg, req)) {
  1421. if (retries > queue_retry_max)
  1422. snac_log(snac, xs_fmt("input giving up"));
  1423. else {
  1424. /* reenqueue */
  1425. enqueue_input(snac, msg, req, retries + 1);
  1426. snac_log(snac, xs_fmt("input requeue #%d", retries + 1));
  1427. }
  1428. }
  1429. }
  1430. else
  1431. if (strcmp(type, "close_question") == 0) {
  1432. /* the time for this question has ended */
  1433. const char *id = xs_dict_get(q_item, "message");
  1434. if (!xs_is_null(id))
  1435. update_question(snac, id);
  1436. }
  1437. else
  1438. if (strcmp(type, "request_replies") == 0) {
  1439. const char *id = xs_dict_get(q_item, "message");
  1440. if (!xs_is_null(id))
  1441. timeline_request_replies(snac, id);
  1442. }
  1443. else
  1444. snac_log(snac, xs_fmt("unexpected user q_item type '%s'", type));
  1445. }
  1446. int process_user_queue(snac *snac)
  1447. /* processes a user's queue */
  1448. {
  1449. int cnt = 0;
  1450. xs *list = user_queue(snac);
  1451. xs_list *p = list;
  1452. xs_str *fn;
  1453. while (xs_list_iter(&p, &fn)) {
  1454. xs *q_item = dequeue(fn);
  1455. if (q_item == NULL) {
  1456. snac_log(snac, xs_fmt("process_user_queue q_item error"));
  1457. continue;
  1458. }
  1459. process_user_queue_item(snac, q_item);
  1460. cnt++;
  1461. }
  1462. return cnt;
  1463. }
  1464. void process_queue_item(xs_dict *q_item)
  1465. /* processes an item from the global queue */
  1466. {
  1467. char *type = xs_dict_get(q_item, "type");
  1468. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  1469. if (strcmp(type, "output") == 0) {
  1470. int status;
  1471. xs_str *inbox = xs_dict_get(q_item, "inbox");
  1472. xs_str *keyid = xs_dict_get(q_item, "keyid");
  1473. xs_str *seckey = xs_dict_get(q_item, "seckey");
  1474. xs_dict *msg = xs_dict_get(q_item, "message");
  1475. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  1476. xs *payload = NULL;
  1477. int p_size = 0;
  1478. if (xs_is_null(inbox) || xs_is_null(msg) || xs_is_null(keyid) || xs_is_null(seckey)) {
  1479. srv_log(xs_fmt("output message error: missing fields"));
  1480. return;
  1481. }
  1482. if (is_instance_blocked(inbox)) {
  1483. srv_debug(0, xs_fmt("discarded output message to blocked instance %s", inbox));
  1484. return;
  1485. }
  1486. /* deliver */
  1487. status = send_to_inbox_raw(keyid, seckey, inbox, msg, &payload, &p_size, retries == 0 ? 3 : 8);
  1488. if (payload) {
  1489. if (p_size > 64) {
  1490. /* trim the message */
  1491. payload[64] = '\0';
  1492. payload = xs_str_cat(payload, "...");
  1493. }
  1494. /* strip ugly control characters */
  1495. payload = xs_replace_i(payload, "\n", "");
  1496. payload = xs_replace_i(payload, "\r", "");
  1497. if (*payload)
  1498. payload = xs_str_wrap_i(" [", payload, "]");
  1499. }
  1500. else
  1501. payload = xs_str_new(NULL);
  1502. srv_log(xs_fmt("output message: sent to inbox %s %d%s", inbox, status, payload));
  1503. if (!valid_status(status)) {
  1504. retries++;
  1505. /* error sending; requeue? */
  1506. if (status == 400 || status == 404 || status == 410 || status < 0)
  1507. /* explicit error: discard */
  1508. srv_log(xs_fmt("output message: fatal error %s %d", inbox, status));
  1509. else
  1510. if (retries > queue_retry_max)
  1511. srv_log(xs_fmt("output message: giving up %s %d", inbox, status));
  1512. else {
  1513. /* requeue */
  1514. enqueue_output_raw(keyid, seckey, msg, inbox, retries);
  1515. srv_log(xs_fmt("output message: requeue %s #%d", inbox, retries));
  1516. }
  1517. }
  1518. }
  1519. else
  1520. if (strcmp(type, "email") == 0) {
  1521. /* send this email */
  1522. xs_str *msg = xs_dict_get(q_item, "message");
  1523. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  1524. if (!send_email(msg))
  1525. srv_debug(1, xs_fmt("email message sent"));
  1526. else {
  1527. retries++;
  1528. if (retries > queue_retry_max)
  1529. srv_log(xs_fmt("email giving up (errno: %d)", errno));
  1530. else {
  1531. /* requeue */
  1532. srv_log(xs_fmt(
  1533. "email requeue #%d (errno: %d)", retries, errno));
  1534. enqueue_email(msg, retries);
  1535. }
  1536. }
  1537. }
  1538. else
  1539. if (strcmp(type, "telegram") == 0) {
  1540. /* send this via telegram */
  1541. char *bot = xs_dict_get(q_item, "bot");
  1542. char *msg = xs_dict_get(q_item, "message");
  1543. xs *chat_id = xs_dup(xs_dict_get(q_item, "chat_id"));
  1544. int status = 0;
  1545. /* chat_id must start with a - */
  1546. if (!xs_startswith(chat_id, "-"))
  1547. chat_id = xs_str_wrap_i("-", chat_id, NULL);
  1548. xs *url = xs_fmt("https:/" "/api.telegram.org/bot%s/sendMessage", bot);
  1549. xs *body = xs_fmt("{\"chat_id\":%s,\"text\":\"%s\"}", chat_id, msg);
  1550. xs *headers = xs_dict_new();
  1551. headers = xs_dict_append(headers, "content-type", "application/json");
  1552. xs *rsp = xs_http_request("POST", url, headers,
  1553. body, strlen(body), &status, NULL, NULL, 0);
  1554. rsp = xs_free(rsp);
  1555. srv_debug(0, xs_fmt("telegram post %d", status));
  1556. }
  1557. else
  1558. if (strcmp(type, "purge") == 0) {
  1559. srv_log(xs_dup("purge start"));
  1560. purge_all();
  1561. srv_log(xs_dup("purge end"));
  1562. }
  1563. else
  1564. srv_log(xs_fmt("unexpected q_item type '%s'", type));
  1565. }
  1566. int process_queue(void)
  1567. /* processes the global queue */
  1568. {
  1569. int cnt = 0;
  1570. xs *list = queue();
  1571. xs_list *p = list;
  1572. xs_str *fn;
  1573. while (xs_list_iter(&p, &fn)) {
  1574. xs *q_item = dequeue(fn);
  1575. if (q_item != NULL) {
  1576. job_post(q_item, 0);
  1577. cnt++;
  1578. }
  1579. }
  1580. return cnt;
  1581. }
  1582. /** HTTP handlers */
  1583. int activitypub_get_handler(const xs_dict *req, const char *q_path,
  1584. char **body, int *b_size, char **ctype)
  1585. {
  1586. int status = 200;
  1587. char *accept = xs_dict_get(req, "accept");
  1588. snac snac;
  1589. xs *msg = NULL;
  1590. if (accept == NULL)
  1591. return 0;
  1592. if (xs_str_in(accept, "application/activity+json") == -1 &&
  1593. xs_str_in(accept, "application/ld+json") == -1)
  1594. return 0;
  1595. xs *l = xs_split_n(q_path, "/", 2);
  1596. char *uid, *p_path;
  1597. uid = xs_list_get(l, 1);
  1598. if (!user_open(&snac, uid)) {
  1599. /* invalid user */
  1600. srv_debug(1, xs_fmt("activitypub_get_handler bad user %s", uid));
  1601. return 404;
  1602. }
  1603. p_path = xs_list_get(l, 2);
  1604. *ctype = "application/activity+json";
  1605. if (p_path == NULL) {
  1606. /* if there was no component after the user, it's an actor request */
  1607. msg = msg_actor(&snac);
  1608. *ctype = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"";
  1609. char *ua = xs_dict_get(req, "user-agent");
  1610. snac_debug(&snac, 0, xs_fmt("serving actor [%s]", ua ? ua : "No UA"));
  1611. }
  1612. else
  1613. if (strcmp(p_path, "outbox") == 0) {
  1614. xs *id = xs_fmt("%s/outbox", snac.actor);
  1615. xs *elems = timeline_simple_list(&snac, "public", 0, 20);
  1616. xs *list = xs_list_new();
  1617. msg = msg_collection(&snac, id);
  1618. char *p, *v;
  1619. p = elems;
  1620. while (xs_list_iter(&p, &v)) {
  1621. xs *i = NULL;
  1622. if (valid_status(object_get_by_md5(v, &i))) {
  1623. char *type = xs_dict_get(i, "type");
  1624. char *id = xs_dict_get(i, "id");
  1625. if (type && id && strcmp(type, "Note") == 0 && xs_startswith(id, snac.actor)) {
  1626. xs *c_msg = msg_create(&snac, i);
  1627. list = xs_list_append(list, c_msg);
  1628. }
  1629. }
  1630. }
  1631. /* replace the 'orderedItems' with the latest posts */
  1632. xs *items = xs_number_new(xs_list_len(list));
  1633. msg = xs_dict_set(msg, "orderedItems", list);
  1634. msg = xs_dict_set(msg, "totalItems", items);
  1635. }
  1636. else
  1637. if (strcmp(p_path, "followers") == 0 || strcmp(p_path, "following") == 0) {
  1638. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  1639. msg = msg_collection(&snac, id);
  1640. }
  1641. else
  1642. if (xs_startswith(p_path, "p/")) {
  1643. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  1644. status = object_get(id, &msg);
  1645. /* don't return non-public objects */
  1646. if (valid_status(status) && !is_msg_public(msg))
  1647. status = 404;
  1648. }
  1649. else
  1650. status = 404;
  1651. if (status == 200 && msg != NULL) {
  1652. *body = xs_json_dumps(msg, 4);
  1653. *b_size = strlen(*body);
  1654. }
  1655. snac_debug(&snac, 1, xs_fmt("activitypub_get_handler serving %s %d", q_path, status));
  1656. user_free(&snac);
  1657. return status;
  1658. }
  1659. int activitypub_post_handler(const xs_dict *req, const char *q_path,
  1660. char *payload, int p_size,
  1661. char **body, int *b_size, char **ctype)
  1662. /* processes an input message */
  1663. {
  1664. (void)b_size;
  1665. int status = 202; /* accepted */
  1666. char *i_ctype = xs_dict_get(req, "content-type");
  1667. snac snac;
  1668. char *v;
  1669. if (i_ctype == NULL) {
  1670. *body = xs_str_new("no content-type");
  1671. *ctype = "text/plain";
  1672. return 400;
  1673. }
  1674. if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
  1675. xs_str_in(i_ctype, "application/ld+json") == -1)
  1676. return 0;
  1677. /* decode the message */
  1678. xs *msg = xs_json_loads(payload);
  1679. const char *id = xs_dict_get(msg, "id");
  1680. if (msg == NULL) {
  1681. srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
  1682. srv_archive_error("activitypub_post_handler", "JSON error", req, payload);
  1683. *body = xs_str_new("JSON error");
  1684. *ctype = "text/plain";
  1685. return 400;
  1686. }
  1687. if (id && is_instance_blocked(id)) {
  1688. srv_debug(1, xs_fmt("full instance block for %s", id));
  1689. *body = xs_str_new("blocked");
  1690. *ctype = "text/plain";
  1691. return 403;
  1692. }
  1693. /* get the user and path */
  1694. xs *l = xs_split_n(q_path, "/", 2);
  1695. if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
  1696. /* strange q_path */
  1697. srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
  1698. return 404;
  1699. }
  1700. const char *uid = xs_list_get(l, 1);
  1701. if (!user_open(&snac, uid)) {
  1702. /* invalid user */
  1703. srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid));
  1704. return 404;
  1705. }
  1706. /* if it has a digest, check it now, because
  1707. later the payload won't be exactly the same */
  1708. if ((v = xs_dict_get(req, "digest")) != NULL) {
  1709. xs *s1 = xs_sha256_base64(payload, p_size);
  1710. xs *s2 = xs_fmt("SHA-256=%s", s1);
  1711. if (strcmp(s2, v) != 0) {
  1712. srv_log(xs_fmt("digest check FAILED"));
  1713. *body = xs_str_new("bad digest");
  1714. *ctype = "text/plain";
  1715. status = 400;
  1716. }
  1717. }
  1718. /* if the message is from a muted actor, reject it right now */
  1719. if (!xs_is_null(v = xs_dict_get(msg, "actor")) && *v) {
  1720. if (is_muted(&snac, v)) {
  1721. snac_log(&snac, xs_fmt("rejected message from MUTEd actor %s", v));
  1722. *body = xs_str_new("rejected");
  1723. *ctype = "text/plain";
  1724. status = 403;
  1725. }
  1726. }
  1727. if (valid_status(status)) {
  1728. enqueue_input(&snac, msg, req, 0);
  1729. *ctype = "application/activity+json";
  1730. }
  1731. user_free(&snac);
  1732. return status;
  1733. }