activitypub.c 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2024 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 *user, const char *url, xs_dict **data)
  49. /* request an object */
  50. {
  51. int status = 0;
  52. xs *response = NULL;
  53. xs *payload = NULL;
  54. int p_size;
  55. const char *ctype;
  56. *data = NULL;
  57. if (user != NULL) {
  58. /* get from the net */
  59. response = http_signed_request(user, "GET", url,
  60. NULL, NULL, 0, &status, &payload, &p_size, 0);
  61. }
  62. if (status == 0 || (status >= 500 && status <= 599)) {
  63. /* I found an instance running Misskey that returned
  64. 500 on signed messages but returned the object
  65. perfectly without signing (?), so why not try */
  66. xs_free(response);
  67. xs *hdrs = xs_dict_new();
  68. hdrs = xs_dict_append(hdrs, "accept", "application/activity+json");
  69. hdrs = xs_dict_append(hdrs, "user-agent", USER_AGENT);
  70. response = xs_http_request("GET", url, hdrs,
  71. NULL, 0, &status, &payload, &p_size, 0);
  72. }
  73. if (valid_status(status)) {
  74. /* ensure it's ActivityPub data */
  75. ctype = xs_dict_get(response, "content-type");
  76. if (xs_is_null(ctype))
  77. status = HTTP_STATUS_BAD_REQUEST;
  78. else
  79. if (xs_str_in(ctype, "application/activity+json") != -1 ||
  80. xs_str_in(ctype, "application/ld+json") != -1) {
  81. /* if there is no payload, fail */
  82. if (xs_is_null(payload))
  83. status = HTTP_STATUS_BAD_REQUEST;
  84. else
  85. *data = xs_json_loads(payload);
  86. }
  87. else
  88. status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
  89. }
  90. return status;
  91. }
  92. int actor_request(snac *user, const char *actor, xs_dict **data)
  93. /* request an actor */
  94. {
  95. int status;
  96. xs *payload = NULL;
  97. if (data)
  98. *data = NULL;
  99. /* get from disk first */
  100. status = actor_get_refresh(user, actor, data);
  101. if (!valid_status(status)) {
  102. /* actor data non-existent: get from the net */
  103. status = activitypub_request(user, actor, &payload);
  104. if (valid_status(status)) {
  105. /* renew data */
  106. status = actor_add(actor, payload);
  107. if (data != NULL) {
  108. *data = payload;
  109. payload = NULL;
  110. }
  111. }
  112. else
  113. srv_debug(1, xs_fmt("actor_request error %s %d", actor, status));
  114. }
  115. /* collect the (presumed) shared inbox in this actor */
  116. if (xs_type(xs_dict_get(srv_config, "disable_inbox_collection")) != XSTYPE_TRUE) {
  117. if (valid_status(status) && data && *data)
  118. inbox_add_by_actor(*data);
  119. }
  120. return status;
  121. }
  122. const char *get_atto(const xs_dict *msg)
  123. /* gets the attributedTo field (an actor) */
  124. {
  125. const xs_val *actor = xs_dict_get(msg, "attributedTo");
  126. /* if the actor is a list of objects (like on Peertube videos), pick the Person */
  127. if (xs_type(actor) == XSTYPE_LIST) {
  128. const xs_list *p = actor;
  129. int c = 0;
  130. const xs_dict *v;
  131. actor = NULL;
  132. while (actor == NULL && xs_list_next(p, &v, &c)) {
  133. if (xs_type(v) == XSTYPE_DICT) {
  134. const char *type = xs_dict_get(v, "type");
  135. if (xs_type(type) == XSTYPE_STRING && strcmp(type, "Person") == 0) {
  136. actor = xs_dict_get(v, "id");
  137. if (xs_type(actor) != XSTYPE_STRING)
  138. actor = NULL;
  139. }
  140. }
  141. }
  142. }
  143. else
  144. if (xs_type(actor) == XSTYPE_DICT) {
  145. /* bandwagon.fm returns this */
  146. actor = xs_dict_get(actor, "id");
  147. }
  148. return actor;
  149. }
  150. const char *get_in_reply_to(const xs_dict *msg)
  151. /* gets the inReplyTo id */
  152. {
  153. const xs_val *in_reply_to = xs_dict_get(msg, "inReplyTo");
  154. if (xs_type(in_reply_to) == XSTYPE_DICT)
  155. in_reply_to = xs_dict_get(in_reply_to, "id");
  156. return in_reply_to;
  157. }
  158. xs_list *get_attachments(const xs_dict *msg)
  159. /* unify the garbage fire that are the attachments */
  160. {
  161. xs_list *l = xs_list_new();
  162. const xs_list *p;
  163. /* try first the attachments list */
  164. if (!xs_is_null(p = xs_dict_get(msg, "attachment"))) {
  165. xs *attach = NULL;
  166. const xs_val *v;
  167. /* ensure it's a list */
  168. if (xs_type(p) == XSTYPE_DICT) {
  169. attach = xs_list_new();
  170. attach = xs_list_append(attach, p);
  171. }
  172. else
  173. attach = xs_dup(p);
  174. if (xs_type(attach) == XSTYPE_LIST && xs_list_len(attach) == 0) {
  175. /* does the message have an image? */
  176. const xs_dict *d = xs_dict_get(msg, "image");
  177. if (xs_type(d) == XSTYPE_DICT) {
  178. /* add it to the attachment list */
  179. attach = xs_list_append(attach, d);
  180. }
  181. }
  182. /* now iterate the list */
  183. int c = 0;
  184. while (xs_list_next(attach, &v, &c)) {
  185. const char *type = xs_dict_get(v, "mediaType");
  186. if (xs_is_null(type))
  187. type = xs_dict_get(v, "type");
  188. if (xs_is_null(type))
  189. continue;
  190. const char *href = xs_dict_get(v, "url");
  191. if (xs_is_null(href))
  192. href = xs_dict_get(v, "href");
  193. if (xs_is_null(href))
  194. continue;
  195. /* infer MIME type from non-specific attachments */
  196. if (xs_list_len(attach) < 2 && xs_match(type, "Link|Document")) {
  197. char *mt = (char *)xs_mime_by_ext(href);
  198. if (xs_match(mt, "image/*|audio/*|video/*")) /* */
  199. type = mt;
  200. }
  201. const char *name = xs_dict_get(v, "name");
  202. if (xs_is_null(name))
  203. name = xs_dict_get(msg, "name");
  204. if (xs_is_null(name))
  205. name = "";
  206. xs *d = xs_dict_new();
  207. d = xs_dict_append(d, "type", type);
  208. d = xs_dict_append(d, "href", href);
  209. d = xs_dict_append(d, "name", name);
  210. const xs_dict *icon = xs_dict_get(v, "icon");
  211. if (xs_type(icon) == XSTYPE_DICT)
  212. d = xs_dict_append(d, "icon", icon);
  213. l = xs_list_append(l, d);
  214. }
  215. }
  216. /** urls (attachments from Peertube) **/
  217. p = xs_dict_get(msg, "url");
  218. if (xs_type(p) == XSTYPE_LIST) {
  219. const char *href = NULL;
  220. const char *type = NULL;
  221. int c = 0;
  222. const xs_val *v;
  223. while (href == NULL && xs_list_next(p, &v, &c)) {
  224. if (xs_type(v) == XSTYPE_DICT) {
  225. const char *mtype = xs_dict_get(v, "type");
  226. if (xs_type(mtype) == XSTYPE_STRING && strcmp(mtype, "Link") == 0) {
  227. mtype = xs_dict_get(v, "mediaType");
  228. const xs_list *tag = xs_dict_get(v, "tag");
  229. if (xs_type(mtype) == XSTYPE_STRING &&
  230. strcmp(mtype, "application/x-mpegURL") == 0 &&
  231. xs_type(tag) == XSTYPE_LIST) {
  232. /* now iterate the tag list, looking for a video URL */
  233. const xs_dict *d;
  234. int c = 0;
  235. while (href == NULL && xs_list_next(tag, &d, &c)) {
  236. if (xs_type(d) == XSTYPE_DICT) {
  237. if (xs_type(mtype = xs_dict_get(d, "mediaType")) == XSTYPE_STRING &&
  238. xs_startswith(mtype, "video/")) {
  239. const char *h = xs_dict_get(d, "href");
  240. /* this is probably it */
  241. if (xs_type(h) == XSTYPE_STRING) {
  242. href = h;
  243. type = mtype;
  244. }
  245. }
  246. }
  247. }
  248. }
  249. }
  250. }
  251. }
  252. if (href && type) {
  253. xs *d = xs_dict_new();
  254. d = xs_dict_append(d, "href", href);
  255. d = xs_dict_append(d, "type", type);
  256. d = xs_dict_append(d, "name", "---");
  257. l = xs_list_append(l, d);
  258. }
  259. }
  260. return l;
  261. }
  262. int timeline_request(snac *snac, const char **id, xs_str **wrk, int level)
  263. /* ensures that an entry and its ancestors are in the timeline */
  264. {
  265. int status = 0;
  266. if (level < MAX_CONVERSATION_LEVELS && !xs_is_null(*id)) {
  267. xs *msg = NULL;
  268. /* from a blocked instance? discard and break */
  269. if (is_instance_blocked(*id)) {
  270. snac_debug(snac, 1, xs_fmt("timeline_request blocked instance %s", *id));
  271. return status;
  272. }
  273. /* is the object already there? */
  274. if (!valid_status(object_get(*id, &msg))) {
  275. /* no; download it */
  276. status = activitypub_request(snac, *id, &msg);
  277. if (valid_status(status)) {
  278. const xs_dict *object = msg;
  279. const char *type = xs_dict_get(object, "type");
  280. /* get the id again from the object, as it may be different */
  281. const char *nid = xs_dict_get(object, "id");
  282. if (xs_type(nid) != XSTYPE_STRING)
  283. return 0;
  284. if (wrk && strcmp(nid, *id) != 0) {
  285. snac_debug(snac, 1,
  286. xs_fmt("timeline_request canonical id for %s is %s", *id, nid));
  287. *wrk = xs_dup(nid);
  288. *id = *wrk;
  289. }
  290. if (xs_is_null(type))
  291. type = "(null)";
  292. srv_debug(2, xs_fmt("timeline_request type %s '%s'", nid, type));
  293. if (strcmp(type, "Create") == 0) {
  294. /* some software like lemmy nest Announce + Create + Note */
  295. if (!xs_is_null(object = xs_dict_get(object, "object"))) {
  296. type = xs_dict_get(object, "type");
  297. nid = xs_dict_get(object, "id");
  298. }
  299. else
  300. type = "(null)";
  301. }
  302. if (xs_match(type, POSTLIKE_OBJECT_TYPE)) {
  303. if (content_match("filter_reject.txt", object))
  304. snac_log(snac, xs_fmt("timeline_request rejected by content %s", nid));
  305. else {
  306. const char *actor = get_atto(object);
  307. if (!xs_is_null(actor)) {
  308. /* request (and drop) the actor for this entry */
  309. if (!valid_status(actor_request(snac, actor, NULL))) {
  310. /* failed? retry later */
  311. enqueue_actor_refresh(snac, actor, 60);
  312. }
  313. /* does it have an ancestor? */
  314. const char *in_reply_to = get_in_reply_to(object);
  315. /* store */
  316. timeline_add(snac, nid, object);
  317. /* redistribute to lists for this user */
  318. list_distribute(snac, actor, object);
  319. /* recurse! */
  320. timeline_request(snac, &in_reply_to, NULL, level + 1);
  321. }
  322. }
  323. }
  324. }
  325. }
  326. }
  327. return status;
  328. }
  329. int send_to_inbox_raw(const char *keyid, const char *seckey,
  330. const xs_str *inbox, const xs_dict *msg,
  331. xs_val **payload, int *p_size, int timeout)
  332. /* sends a message to an Inbox */
  333. {
  334. int status;
  335. xs_dict *response;
  336. xs *j_msg = xs_json_dumps((xs_dict *)msg, 4);
  337. response = http_signed_request_raw(keyid, seckey, "POST", inbox,
  338. NULL, j_msg, strlen(j_msg), &status, payload, p_size, timeout);
  339. xs_free(response);
  340. return status;
  341. }
  342. int send_to_inbox(snac *snac, const xs_str *inbox, const xs_dict *msg,
  343. xs_val **payload, int *p_size, int timeout)
  344. /* sends a message to an Inbox */
  345. {
  346. const char *seckey = xs_dict_get(snac->key, "secret");
  347. return send_to_inbox_raw(snac->actor, seckey, inbox, msg, payload, p_size, timeout);
  348. }
  349. xs_str *get_actor_inbox(const char *actor, int shared)
  350. /* gets an actor's inbox */
  351. {
  352. xs *data = NULL;
  353. const char *v = NULL;
  354. if (valid_status(actor_request(NULL, actor, &data))) {
  355. /* try first endpoints/sharedInbox */
  356. if (shared && (v = xs_dict_get(data, "endpoints")))
  357. v = xs_dict_get(v, "sharedInbox");
  358. /* try then the regular inbox */
  359. if (xs_is_null(v))
  360. v = xs_dict_get(data, "inbox");
  361. }
  362. return xs_is_null(v) ? NULL : xs_dup(v);
  363. }
  364. int send_to_actor(snac *snac, const char *actor, const xs_dict *msg,
  365. xs_val **payload, int *p_size, int timeout)
  366. /* sends a message to an actor */
  367. {
  368. int status = HTTP_STATUS_BAD_REQUEST;
  369. xs *inbox = get_actor_inbox(actor, 1);
  370. if (!xs_is_null(inbox))
  371. status = send_to_inbox(snac, inbox, msg, payload, p_size, timeout);
  372. return status;
  373. }
  374. void post_message(snac *snac, const char *actor, const xs_dict *msg)
  375. /* posts a message immediately (bypassing the output queues) */
  376. {
  377. xs *payload = NULL;
  378. int p_size;
  379. int status = send_to_actor(snac, actor, msg, &payload, &p_size, 3);
  380. srv_log(xs_fmt("post_message to actor %s %d", actor, status));
  381. if (!valid_status(status))
  382. /* cannot send right now, enqueue */
  383. enqueue_message(snac, msg);
  384. }
  385. xs_list *recipient_list(snac *snac, const xs_dict *msg, int expand_public)
  386. /* returns the list of recipients for a message */
  387. {
  388. const xs_val *to = xs_dict_get(msg, "to");
  389. const xs_val *cc = xs_dict_get(msg, "cc");
  390. xs_set rcpts;
  391. int n;
  392. xs_set_init(&rcpts);
  393. const xs_list *lists[] = { to, cc, NULL };
  394. for (n = 0; lists[n]; n++) {
  395. xs_list *l = (xs_list *)lists[n];
  396. const char *v;
  397. xs *tl = NULL;
  398. /* if it's a string, create a list with only one element */
  399. if (xs_type(l) == XSTYPE_STRING) {
  400. tl = xs_list_new();
  401. tl = xs_list_append(tl, l);
  402. l = tl;
  403. }
  404. while (xs_list_iter(&l, &v)) {
  405. if (expand_public && strcmp(v, public_address) == 0) {
  406. /* iterate the followers and add them */
  407. xs *fwers = follower_list(snac);
  408. const char *actor;
  409. char *p = fwers;
  410. while (xs_list_iter(&p, &actor))
  411. xs_set_add(&rcpts, actor);
  412. }
  413. else
  414. xs_set_add(&rcpts, v);
  415. }
  416. }
  417. return xs_set_result(&rcpts);
  418. }
  419. int is_msg_public(const xs_dict *msg)
  420. /* checks if a message is public */
  421. {
  422. const char *to = xs_dict_get(msg, "to");
  423. const char *cc = xs_dict_get(msg, "cc");
  424. int n;
  425. const char *lists[] = { to, cc, NULL };
  426. for (n = 0; lists[n]; n++) {
  427. const xs_val *l = lists[n];
  428. if (xs_type(l) == XSTYPE_STRING) {
  429. if (strcmp(l, public_address) == 0)
  430. return 1;
  431. }
  432. else
  433. if (xs_type(l) == XSTYPE_LIST) {
  434. if (xs_list_in(l, public_address) != -1)
  435. return 1;
  436. }
  437. }
  438. return 0;
  439. }
  440. int is_msg_from_private_user(const xs_dict *msg)
  441. /* checks if a message is from a local, private user */
  442. {
  443. int ret = 0;
  444. /* is this message from a local user? */
  445. if (xs_startswith(xs_dict_get(msg, "id"), srv_baseurl)) {
  446. const char *atto = get_atto(msg);
  447. xs *l = xs_split(atto, "/");
  448. const char *uid = xs_list_get(l, -1);
  449. snac user;
  450. if (uid && user_open(&user, uid)) {
  451. if (xs_type(xs_dict_get(user.config, "private")) == XSTYPE_TRUE)
  452. ret = 1;
  453. user_free(&user);
  454. }
  455. }
  456. return ret;
  457. }
  458. int is_msg_for_me(snac *snac, const xs_dict *c_msg)
  459. /* checks if this message is for me */
  460. {
  461. const char *type = xs_dict_get(c_msg, "type");
  462. const char *actor = xs_dict_get(c_msg, "actor");
  463. if (strcmp(actor, snac->actor) == 0) {
  464. /* message by myself? (most probably via the shared-inbox) reject */
  465. snac_debug(snac, 1, xs_fmt("ignoring message by myself"));
  466. return 0;
  467. }
  468. if (xs_match(type, "Like|Announce|EmojiReact")) {
  469. const char *object = xs_dict_get(c_msg, "object");
  470. if (xs_type(object) == XSTYPE_DICT)
  471. object = xs_dict_get(object, "id");
  472. /* bad object id? reject */
  473. if (xs_type(object) != XSTYPE_STRING)
  474. return 0;
  475. /* if it's about one of our posts, accept it */
  476. if (xs_startswith(object, snac->actor))
  477. return 2;
  478. /* if it's by someone we don't follow, reject */
  479. return following_check(snac, actor);
  480. }
  481. /* if it's an Undo, it must be from someone related to us */
  482. if (xs_match(type, "Undo")) {
  483. return follower_check(snac, actor) || following_check(snac, actor);
  484. }
  485. /* if it's an Accept + Follow, it must be for a Follow we created */
  486. if (xs_match(type, "Accept")) {
  487. return following_check(snac, actor);
  488. }
  489. /* if it's a Follow, it must be explicitly for us */
  490. if (xs_match(type, "Follow")) {
  491. const char *object = xs_dict_get(c_msg, "object");
  492. return !xs_is_null(object) && strcmp(snac->actor, object) == 0;
  493. }
  494. /* only accept Ping directed to us */
  495. if (xs_match(type, "Ping")) {
  496. const char *dest = xs_dict_get(c_msg, "to");
  497. return !xs_is_null(dest) && strcmp(snac->actor, dest) == 0;
  498. }
  499. /* if it's not a Create or Update, allow as is */
  500. if (!xs_match(type, "Create|Update")) {
  501. return 1;
  502. }
  503. int pub_msg = is_msg_public(c_msg);
  504. /* if this message is public and we follow the actor of this post, allow */
  505. if (pub_msg && following_check(snac, actor))
  506. return 1;
  507. const xs_dict *msg = xs_dict_get(c_msg, "object");
  508. xs *rcpts = recipient_list(snac, msg, 0);
  509. xs_list *p = rcpts;
  510. const xs_str *v;
  511. xs *actor_followers = NULL;
  512. if (!pub_msg) {
  513. /* not a public message; get the actor and its followers list */
  514. xs *actor_obj = NULL;
  515. if (valid_status(object_get(actor, &actor_obj))) {
  516. const xs_val *fw = xs_dict_get(actor_obj, "followers");
  517. if (fw)
  518. actor_followers = xs_dup(fw);
  519. }
  520. }
  521. while(xs_list_iter(&p, &v)) {
  522. /* explicitly for me? accept */
  523. if (strcmp(v, snac->actor) == 0)
  524. return 2;
  525. if (pub_msg) {
  526. /* a public message for someone we follow? (probably cc'ed) accept */
  527. if (following_check(snac, v))
  528. return 5;
  529. }
  530. else
  531. if (actor_followers && strcmp(v, actor_followers) == 0) {
  532. /* if this message is for this actor's followers, are we one of them? */
  533. if (following_check(snac, actor))
  534. return 6;
  535. }
  536. }
  537. /* accept if it's by someone we follow */
  538. const char *atto = get_atto(msg);
  539. if (pub_msg && !xs_is_null(atto) && following_check(snac, atto))
  540. return 3;
  541. /* is this message a reply to another? */
  542. const char *irt = get_in_reply_to(msg);
  543. if (!xs_is_null(irt)) {
  544. xs *r_msg = NULL;
  545. /* try to get the replied message */
  546. if (valid_status(object_get(irt, &r_msg))) {
  547. atto = get_atto(r_msg);
  548. /* accept if the replied message is from someone we follow */
  549. if (pub_msg && !xs_is_null(atto) && following_check(snac, atto))
  550. return 4;
  551. }
  552. }
  553. /* does this message contain a tag we are following? */
  554. const xs_list *fw_tags = xs_dict_get(snac->config, "followed_hashtags");
  555. if (pub_msg && xs_type(fw_tags) == XSTYPE_LIST) {
  556. const xs_list *tags_in_msg = xs_dict_get(msg, "tag");
  557. if (xs_type(tags_in_msg) == XSTYPE_LIST) {
  558. const xs_dict *te;
  559. /* iterate the tags in the message */
  560. xs_list_foreach(tags_in_msg, te) {
  561. if (xs_type(te) == XSTYPE_DICT) {
  562. const char *type = xs_dict_get(te, "type");
  563. const char *name = xs_dict_get(te, "name");
  564. if (xs_type(type) == XSTYPE_STRING && xs_type(name) == XSTYPE_STRING) {
  565. if (strcmp(type, "Hashtag") == 0 && xs_list_in(fw_tags, name) != -1)
  566. return 7;
  567. }
  568. }
  569. }
  570. }
  571. }
  572. return 0;
  573. }
  574. xs_str *process_tags(snac *snac, const char *content, xs_list **tag)
  575. /* parses mentions and tags from content */
  576. {
  577. xs_str *nc = xs_str_new(NULL);
  578. xs_list *tl = *tag;
  579. xs *split;
  580. xs_list *p;
  581. const xs_val *v;
  582. int n = 0;
  583. /* create a default server for incomplete mentions */
  584. xs *def_srv = NULL;
  585. if (xs_list_len(tl)) {
  586. /* if there are any mentions, get the server from
  587. the first one, which is the inReplyTo author */
  588. p = tl;
  589. while (xs_list_iter(&p, &v)) {
  590. const char *type = xs_dict_get(v, "type");
  591. const char *name = xs_dict_get(v, "name");
  592. if (type && name && strcmp(type, "Mention") == 0) {
  593. xs *l = xs_split(name, "@");
  594. def_srv = xs_dup(xs_list_get(l, -1));
  595. break;
  596. }
  597. }
  598. }
  599. if (xs_is_null(def_srv))
  600. /* use this same server */
  601. def_srv = xs_dup(xs_dict_get(srv_config, "host"));
  602. split = xs_regex_split(content, "(@[A-Za-z0-9_]+(@[A-Za-z0-9\\.-]+)?|&#[0-9]+;|#(_|[^[:punct:][:space:]])+)");
  603. p = split;
  604. while (xs_list_iter(&p, &v)) {
  605. if ((n & 0x1)) {
  606. if (*v == '@') {
  607. xs *link = NULL;
  608. xs *wuid = NULL;
  609. if (strchr(v + 1, '@') == NULL) {
  610. /* only one @? it's a dumb Mastodon-like mention
  611. without server; add the default one */
  612. wuid = xs_fmt("%s@%s", v, def_srv);
  613. snac_debug(snac, 2, xs_fmt("mention without server '%s' '%s'", v, wuid));
  614. }
  615. else
  616. wuid = xs_dup(v);
  617. /* query the webfinger about this fellow */
  618. xs *actor = NULL;
  619. xs *uid = NULL;
  620. int status;
  621. status = webfinger_request(wuid, &actor, &uid);
  622. if (valid_status(status) && actor && uid) {
  623. xs *d = xs_dict_new();
  624. xs *n = xs_fmt("@%s", uid);
  625. d = xs_dict_append(d, "type", "Mention");
  626. d = xs_dict_append(d, "href", actor);
  627. d = xs_dict_append(d, "name", n);
  628. tl = xs_list_append(tl, d);
  629. link = xs_fmt("<span class=\"h-card\"><a href=\"%s\" class=\"u-url mention\">%s</a></span>", actor, n);
  630. }
  631. if (!xs_is_null(link))
  632. nc = xs_str_cat(nc, link);
  633. else
  634. nc = xs_str_cat(nc, v);
  635. }
  636. else
  637. if (*v == '#') {
  638. /* hashtag */
  639. xs *d = xs_dict_new();
  640. xs *n = xs_tolower_i(xs_dup(v));
  641. xs *h = xs_fmt("%s?t=%s", srv_baseurl, n + 1);
  642. xs *l = xs_fmt("<a href=\"%s\" class=\"mention hashtag\" rel=\"tag\">%s</a>", h, v);
  643. d = xs_dict_append(d, "type", "Hashtag");
  644. d = xs_dict_append(d, "href", h);
  645. d = xs_dict_append(d, "name", n);
  646. tl = xs_list_append(tl, d);
  647. /* add the code */
  648. nc = xs_str_cat(nc, l);
  649. }
  650. else
  651. if (*v == '&') {
  652. /* HTML Unicode entity, probably part of an emoji */
  653. /* write as is */
  654. nc = xs_str_cat(nc, v);
  655. }
  656. }
  657. else
  658. nc = xs_str_cat(nc, v);
  659. n++;
  660. }
  661. *tag = tl;
  662. return nc;
  663. }
  664. void notify(snac *snac, const char *type, const char *utype, const char *actor, const xs_dict *msg)
  665. /* notifies the user of relevant events */
  666. {
  667. /* skip our own notifications */
  668. if (strcmp(snac->actor, actor) == 0)
  669. return;
  670. const char *id = xs_dict_get(msg, "id");
  671. if (strcmp(type, "Create") == 0) {
  672. /* only notify of notes specifically for us */
  673. xs *rcpts = recipient_list(snac, msg, 0);
  674. if (xs_list_in(rcpts, snac->actor) == -1)
  675. return;
  676. /* discard votes */
  677. const xs_dict *note = xs_dict_get(msg, "object");
  678. if (note && !xs_is_null(xs_dict_get(note, "name")))
  679. return;
  680. }
  681. if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") != 0)
  682. return;
  683. /* get the object id */
  684. const char *objid = xs_dict_get(msg, "object");
  685. if (xs_type(objid) == XSTYPE_DICT)
  686. objid = xs_dict_get(objid, "id");
  687. if (xs_match(type, "Like|Announce|EmojiReact")) {
  688. /* if it's not an admiration about something by us, done */
  689. if (xs_is_null(objid) || !xs_startswith(objid, snac->actor))
  690. return;
  691. }
  692. /* updated poll? */
  693. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0) {
  694. const xs_dict *poll;
  695. const char *poll_id;
  696. if ((poll = xs_dict_get(msg, "object")) == NULL)
  697. return;
  698. /* if it's not closed, discard */
  699. if (xs_is_null(xs_dict_get(poll, "closed")))
  700. return;
  701. if ((poll_id = xs_dict_get(poll, "id")) == NULL)
  702. return;
  703. /* if it's not ours and we didn't vote, discard */
  704. if (!xs_startswith(poll_id, snac->actor) && !was_question_voted(snac, poll_id))
  705. return;
  706. }
  707. /* user will love to know about this! */
  708. /* prepare message body */
  709. xs *body = xs_fmt("User : @%s@%s\n",
  710. xs_dict_get(snac->config, "uid"),
  711. xs_dict_get(srv_config, "host")
  712. );
  713. if (strcmp(utype, "(null)") != 0) {
  714. xs *s1 = xs_fmt("Type : %s + %s\n", type, utype);
  715. body = xs_str_cat(body, s1);
  716. }
  717. else {
  718. xs *s1 = xs_fmt("Type : %s\n", type);
  719. body = xs_str_cat(body, s1);
  720. }
  721. {
  722. xs *s1 = xs_fmt("Actor : %s\n", actor);
  723. body = xs_str_cat(body, s1);
  724. }
  725. if (objid != NULL) {
  726. xs *s1 = xs_fmt("Object: %s\n", objid);
  727. body = xs_str_cat(body, s1);
  728. }
  729. /* email */
  730. const char *email = "[disabled by admin]";
  731. if (xs_type(xs_dict_get(srv_config, "disable_email_notifications")) != XSTYPE_TRUE) {
  732. email = xs_dict_get(snac->config_o, "email");
  733. if (xs_is_null(email)) {
  734. email = xs_dict_get(snac->config, "email");
  735. if (xs_is_null(email))
  736. email = "[empty]";
  737. }
  738. }
  739. if (*email != '\0' && *email != '[') {
  740. snac_debug(snac, 1, xs_fmt("email notify %s %s %s", type, utype, actor));
  741. xs *subject = xs_fmt("snac notify for @%s@%s",
  742. xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"));
  743. xs *from = xs_fmt("snac-daemon <snac-daemon@%s>", xs_dict_get(srv_config, "host"));
  744. xs *header = xs_fmt(
  745. "From: %s\n"
  746. "To: %s\n"
  747. "Subject: %s\n"
  748. "\n",
  749. from, email, subject);
  750. xs *email_body = xs_fmt("%s%s", header, body);
  751. enqueue_email(email_body, 0);
  752. }
  753. /* telegram */
  754. const char *bot = xs_dict_get(snac->config, "telegram_bot");
  755. const char *chat_id = xs_dict_get(snac->config, "telegram_chat_id");
  756. if (!xs_is_null(bot) && !xs_is_null(chat_id) && *bot && *chat_id)
  757. enqueue_telegram(body, bot, chat_id);
  758. /* ntfy */
  759. const char *ntfy_server = xs_dict_get(snac->config, "ntfy_server");
  760. const char *ntfy_token = xs_dict_get(snac->config, "ntfy_token");
  761. if (!xs_is_null(ntfy_server) && *ntfy_server)
  762. enqueue_ntfy(body, ntfy_server, ntfy_token);
  763. /* auto boost */
  764. if (xs_match(type, "Create") && xs_is_true(xs_dict_get(snac->config, "auto_boost"))) {
  765. xs *msg = msg_admiration(snac, objid, "Announce");
  766. enqueue_message(snac, msg);
  767. snac_debug(snac, 1, xs_fmt("auto boosted %s", objid));
  768. }
  769. /* finally, store it in the notification folder */
  770. if (strcmp(type, "Follow") == 0)
  771. objid = id;
  772. else
  773. if (strcmp(utype, "Follow") == 0)
  774. objid = actor;
  775. notify_add(snac, type, utype, actor, objid != NULL ? objid : id, msg);
  776. }
  777. /** messages **/
  778. xs_dict *msg_base(snac *snac, const char *type, const char *id,
  779. const char *actor, const char *date, const char *object)
  780. /* creates a base ActivityPub message */
  781. {
  782. xs *did = NULL;
  783. xs *published = NULL;
  784. xs *ntid = tid(0);
  785. const char *obj_id;
  786. if (xs_type(object) == XSTYPE_DICT)
  787. obj_id = xs_dict_get(object, "id");
  788. else
  789. obj_id = object;
  790. /* generated values */
  791. if (date && strcmp(date, "@now") == 0) {
  792. published = xs_str_utctime(0, ISO_DATE_SPEC);
  793. date = published;
  794. }
  795. if (id != NULL) {
  796. if (strcmp(id, "@dummy") == 0) {
  797. did = xs_fmt("%s/d/%s/%s", snac->actor, ntid, type);
  798. id = did;
  799. }
  800. else
  801. if (strcmp(id, "@object") == 0) {
  802. if (obj_id != NULL) {
  803. did = xs_fmt("%s/%s_%s", obj_id, type, ntid);
  804. id = did;
  805. }
  806. else
  807. id = NULL;
  808. }
  809. else
  810. if (strcmp(id, "@wrapper") == 0) {
  811. /* like @object, but always generate the same id */
  812. if (object != NULL) {
  813. date = xs_dict_get(object, "published");
  814. did = xs_fmt("%s/%s", obj_id, type);
  815. id = did;
  816. }
  817. else
  818. id = NULL;
  819. }
  820. }
  821. xs_dict *msg = xs_dict_new();
  822. msg = xs_dict_append(msg, "@context", "https:/" "/www.w3.org/ns/activitystreams");
  823. msg = xs_dict_append(msg, "type", type);
  824. if (id != NULL)
  825. msg = xs_dict_append(msg, "id", id);
  826. if (actor != NULL)
  827. msg = xs_dict_append(msg, "actor", actor);
  828. if (date != NULL)
  829. msg = xs_dict_append(msg, "published", date);
  830. if (object != NULL)
  831. msg = xs_dict_append(msg, "object", object);
  832. return msg;
  833. }
  834. xs_dict *msg_collection(snac *snac, const char *id, int items)
  835. /* creates an empty OrderedCollection message */
  836. {
  837. xs_dict *msg = msg_base(snac, "OrderedCollection", id, NULL, NULL, NULL);
  838. xs *n = xs_number_new(items);
  839. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  840. msg = xs_dict_append(msg, "totalItems", n);
  841. return msg;
  842. }
  843. xs_dict *msg_accept(snac *snac, const xs_val *object, const char *to)
  844. /* creates an Accept message (as a response to a Follow) */
  845. {
  846. xs_dict *msg = msg_base(snac, "Accept", "@dummy", snac->actor, NULL, object);
  847. msg = xs_dict_append(msg, "to", to);
  848. return msg;
  849. }
  850. xs_dict *msg_update(snac *snac, const xs_dict *object)
  851. /* creates an Update message */
  852. {
  853. xs_dict *msg = msg_base(snac, "Update", "@object", snac->actor, "@now", object);
  854. const char *type = xs_dict_get(object, "type");
  855. if (strcmp(type, "Note") == 0) {
  856. msg = xs_dict_append(msg, "to", xs_dict_get(object, "to"));
  857. msg = xs_dict_append(msg, "cc", xs_dict_get(object, "cc"));
  858. }
  859. else
  860. if (strcmp(type, "Person") == 0) {
  861. msg = xs_dict_append(msg, "to", public_address);
  862. /* also spam the people being followed, so that
  863. they have the newest information about who we are */
  864. xs *cc = following_list(snac);
  865. msg = xs_dict_append(msg, "cc", cc);
  866. }
  867. else
  868. msg = xs_dict_append(msg, "to", public_address);
  869. return msg;
  870. }
  871. xs_dict *msg_admiration(snac *snac, const char *object, const char *type)
  872. /* creates a Like or Announce message */
  873. {
  874. xs *a_msg = NULL;
  875. xs_dict *msg = NULL;
  876. xs *wrk = NULL;
  877. /* call the object */
  878. timeline_request(snac, &object, &wrk, 0);
  879. if (valid_status(object_get(object, &a_msg))) {
  880. xs *rcpts = xs_list_new();
  881. xs *o_md5 = xs_md5_hex(object, strlen(object));
  882. xs *id = xs_fmt("%s/%s/%s", snac->actor, *type == 'L' ? "l" : "a", o_md5);
  883. msg = msg_base(snac, type, id, snac->actor, "@now", object);
  884. if (is_msg_public(a_msg))
  885. rcpts = xs_list_append(rcpts, public_address);
  886. rcpts = xs_list_append(rcpts, get_atto(a_msg));
  887. msg = xs_dict_append(msg, "to", rcpts);
  888. }
  889. else
  890. snac_log(snac, xs_fmt("msg_admiration cannot retrieve object %s", object));
  891. return msg;
  892. }
  893. xs_dict *msg_repulsion(snac *user, const char *id, const char *type)
  894. /* creates an Undo + admiration message */
  895. {
  896. xs *a_msg = NULL;
  897. xs_dict *msg = NULL;
  898. if (valid_status(object_get(id, &a_msg))) {
  899. /* create a clone of the original admiration message */
  900. xs *object = msg_admiration(user, id, type);
  901. /* delete the published date */
  902. object = xs_dict_del(object, "published");
  903. /* create an undo message for this object */
  904. msg = msg_undo(user, object);
  905. /* copy the 'to' field */
  906. msg = xs_dict_set(msg, "to", xs_dict_get(object, "to"));
  907. }
  908. /* now we despise this */
  909. object_unadmire(id, user->actor, *type == 'L' ? 1 : 0);
  910. return msg;
  911. }
  912. xs_dict *msg_actor(snac *snac)
  913. /* create a Person message for this actor */
  914. {
  915. xs *ctxt = xs_list_new();
  916. xs *icon = xs_dict_new();
  917. xs *keys = xs_dict_new();
  918. xs *tags = xs_list_new();
  919. xs *avtr = NULL;
  920. xs *kid = NULL;
  921. xs *f_bio = NULL;
  922. xs_dict *msg = msg_base(snac, "Person", snac->actor, NULL, NULL, NULL);
  923. const char *p;
  924. int n;
  925. /* change the @context (is this really necessary?) */
  926. ctxt = xs_list_append(ctxt, "https:/" "/www.w3.org/ns/activitystreams");
  927. ctxt = xs_list_append(ctxt, "https:/" "/w3id.org/security/v1");
  928. msg = xs_dict_set(msg, "@context", ctxt);
  929. msg = xs_dict_set(msg, "url", snac->actor);
  930. msg = xs_dict_set(msg, "name", xs_dict_get(snac->config, "name"));
  931. msg = xs_dict_set(msg, "preferredUsername", snac->uid);
  932. msg = xs_dict_set(msg, "published", xs_dict_get(snac->config, "published"));
  933. xs *f_bio_2 = not_really_markdown(xs_dict_get(snac->config, "bio"), NULL, &tags);
  934. f_bio = process_tags(snac, f_bio_2, &tags);
  935. msg = xs_dict_set(msg, "summary", f_bio);
  936. msg = xs_dict_set(msg, "tag", tags);
  937. char *folders[] = { "inbox", "outbox", "followers", "following", "featured", NULL };
  938. for (n = 0; folders[n]; n++) {
  939. xs *f = xs_fmt("%s/%s", snac->actor, folders[n]);
  940. msg = xs_dict_set(msg, folders[n], f);
  941. }
  942. p = xs_dict_get(snac->config, "avatar");
  943. if (*p == '\0')
  944. avtr = xs_fmt("%s/susie.png", srv_baseurl);
  945. else
  946. avtr = xs_dup(p);
  947. icon = xs_dict_append(icon, "type", "Image");
  948. icon = xs_dict_append(icon, "mediaType", xs_mime_by_ext(avtr));
  949. icon = xs_dict_append(icon, "url", avtr);
  950. msg = xs_dict_set(msg, "icon", icon);
  951. kid = xs_fmt("%s#main-key", snac->actor);
  952. keys = xs_dict_append(keys, "id", kid);
  953. keys = xs_dict_append(keys, "owner", snac->actor);
  954. keys = xs_dict_append(keys, "publicKeyPem", xs_dict_get(snac->key, "public"));
  955. msg = xs_dict_set(msg, "publicKey", keys);
  956. /* if the "bot" config field is set to true, change type to "Service" */
  957. if (xs_type(xs_dict_get(snac->config, "bot")) == XSTYPE_TRUE)
  958. msg = xs_dict_set(msg, "type", "Service");
  959. /* add the header image, if there is one defined */
  960. const char *header = xs_dict_get(snac->config, "header");
  961. if (!xs_is_null(header)) {
  962. xs *d = xs_dict_new();
  963. d = xs_dict_append(d, "type", "Image");
  964. d = xs_dict_append(d, "mediaType", xs_mime_by_ext(header));
  965. d = xs_dict_append(d, "url", header);
  966. msg = xs_dict_set(msg, "image", d);
  967. }
  968. /* add the metadata as attachments of PropertyValue */
  969. xs *metadata = NULL;
  970. const xs_dict *md = xs_dict_get(snac->config, "metadata");
  971. if (xs_type(md) == XSTYPE_DICT)
  972. metadata = xs_dup(md);
  973. else
  974. if (xs_type(md) == XSTYPE_STRING) {
  975. metadata = xs_dict_new();
  976. xs *l = xs_split(md, "\n");
  977. const char *ll;
  978. xs_list_foreach(l, ll) {
  979. xs *kv = xs_split_n(ll, "=", 1);
  980. const char *k = xs_list_get(kv, 0);
  981. const char *v = xs_list_get(kv, 1);
  982. if (k && v) {
  983. xs *kk = xs_strip_i(xs_dup(k));
  984. xs *vv = xs_strip_i(xs_dup(v));
  985. metadata = xs_dict_set(metadata, kk, vv);
  986. }
  987. }
  988. }
  989. if (xs_type(metadata) == XSTYPE_DICT) {
  990. xs *attach = xs_list_new();
  991. const xs_str *k;
  992. const xs_str *v;
  993. int c = 0;
  994. while (xs_dict_next(metadata, &k, &v, &c)) {
  995. xs *d = xs_dict_new();
  996. xs *k2 = encode_html(k);
  997. xs *v2 = NULL;
  998. if (xs_startswith(v, "https:/") || xs_startswith(v, "http:/")) {
  999. xs *t = encode_html(v);
  1000. v2 = xs_fmt("<a href=\"%s\" rel=\"me\">%s</a>", t, t);
  1001. }
  1002. else
  1003. v2 = encode_html(v);
  1004. d = xs_dict_append(d, "type", "PropertyValue");
  1005. d = xs_dict_append(d, "name", k2);
  1006. d = xs_dict_append(d, "value", v2);
  1007. attach = xs_list_append(attach, d);
  1008. }
  1009. msg = xs_dict_set(msg, "attachment", attach);
  1010. }
  1011. /* use shared inboxes? */
  1012. if (xs_type(xs_dict_get(srv_config, "shared_inboxes")) == XSTYPE_TRUE) {
  1013. xs *d = xs_dict_new();
  1014. xs *si = xs_fmt("%s/shared-inbox", srv_baseurl);
  1015. d = xs_dict_append(d, "sharedInbox", si);
  1016. msg = xs_dict_set(msg, "endpoints", d);
  1017. }
  1018. /* does this user have an aka? */
  1019. const char *aka = xs_dict_get(snac->config, "alias");
  1020. if (xs_type(aka) == XSTYPE_STRING && *aka) {
  1021. xs *loaka = xs_list_append(xs_list_new(), aka);
  1022. msg = xs_dict_set(msg, "alsoKnownAs", loaka);
  1023. }
  1024. const xs_val *manually = xs_dict_get(snac->config, "approve_followers");
  1025. msg = xs_dict_set(msg, "manuallyApprovesFollowers",
  1026. xs_stock(xs_is_true(manually) ? XSTYPE_TRUE : XSTYPE_FALSE));
  1027. return msg;
  1028. }
  1029. xs_dict *msg_create(snac *snac, const xs_dict *object)
  1030. /* creates a 'Create' message */
  1031. {
  1032. xs_dict *msg = msg_base(snac, "Create", "@wrapper", snac->actor, NULL, object);
  1033. const xs_val *v;
  1034. if ((v = get_atto(object)))
  1035. msg = xs_dict_append(msg, "attributedTo", v);
  1036. if ((v = xs_dict_get(object, "cc")))
  1037. msg = xs_dict_append(msg, "cc", v);
  1038. if ((v = xs_dict_get(object, "to")))
  1039. msg = xs_dict_append(msg, "to", v);
  1040. else
  1041. msg = xs_dict_append(msg, "to", public_address);
  1042. return msg;
  1043. }
  1044. xs_dict *msg_undo(snac *snac, const xs_val *object)
  1045. /* creates an 'Undo' message */
  1046. {
  1047. xs_dict *msg = msg_base(snac, "Undo", "@object", snac->actor, "@now", object);
  1048. const char *to;
  1049. if (xs_type(object) == XSTYPE_DICT && (to = xs_dict_get(object, "object")))
  1050. msg = xs_dict_append(msg, "to", to);
  1051. return msg;
  1052. }
  1053. xs_dict *msg_delete(snac *snac, const char *id)
  1054. /* creates a 'Delete' + 'Tombstone' for a local entry */
  1055. {
  1056. xs *tomb = xs_dict_new();
  1057. xs_dict *msg = NULL;
  1058. /* sculpt the tombstone */
  1059. tomb = xs_dict_append(tomb, "type", "Tombstone");
  1060. tomb = xs_dict_append(tomb, "id", id);
  1061. /* now create the Delete */
  1062. msg = msg_base(snac, "Delete", "@object", snac->actor, "@now", tomb);
  1063. msg = xs_dict_append(msg, "to", public_address);
  1064. return msg;
  1065. }
  1066. xs_dict *msg_follow(snac *snac, const char *q)
  1067. /* creates a 'Follow' message */
  1068. {
  1069. xs *actor_o = NULL;
  1070. xs *actor = NULL;
  1071. xs_dict *msg = NULL;
  1072. int status;
  1073. xs *url_or_uid = xs_strip_i(xs_str_new(q));
  1074. if (xs_startswith(url_or_uid, "https:/") || xs_startswith(url_or_uid, "http:/"))
  1075. actor = xs_dup(url_or_uid);
  1076. else
  1077. if (!valid_status(webfinger_request(url_or_uid, &actor, NULL)) || actor == NULL) {
  1078. snac_log(snac, xs_fmt("cannot resolve user %s to follow", url_or_uid));
  1079. return NULL;
  1080. }
  1081. /* request the actor */
  1082. status = actor_request(snac, actor, &actor_o);
  1083. if (valid_status(status)) {
  1084. /* check if the actor is an alias */
  1085. const char *r_actor = xs_dict_get(actor_o, "id");
  1086. if (r_actor && strcmp(actor, r_actor) != 0) {
  1087. snac_log(snac, xs_fmt("actor to follow is an alias %s -> %s", actor, r_actor));
  1088. }
  1089. msg = msg_base(snac, "Follow", "@dummy", snac->actor, NULL, r_actor);
  1090. }
  1091. else
  1092. snac_log(snac, xs_fmt("cannot get actor to follow %s %d", actor, status));
  1093. return msg;
  1094. }
  1095. xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts,
  1096. const xs_str *in_reply_to, const xs_list *attach, int priv)
  1097. /* creates a 'Note' message */
  1098. {
  1099. xs *ntid = tid(0);
  1100. xs *id = xs_fmt("%s/p/%s", snac->actor, ntid);
  1101. xs *ctxt = NULL;
  1102. xs *fc2 = NULL;
  1103. xs *fc1 = NULL;
  1104. xs *to = NULL;
  1105. xs *cc = xs_list_new();
  1106. xs *irt = NULL;
  1107. xs *tag = xs_list_new();
  1108. xs *atls = xs_list_new();
  1109. xs_dict *msg = msg_base(snac, "Note", id, NULL, "@now", NULL);
  1110. xs_list *p;
  1111. const xs_val *v;
  1112. if (rcpts == NULL)
  1113. to = xs_list_new();
  1114. else {
  1115. if (xs_type(rcpts) == XSTYPE_STRING) {
  1116. to = xs_list_new();
  1117. to = xs_list_append(to, rcpts);
  1118. }
  1119. else
  1120. to = xs_dup(rcpts);
  1121. }
  1122. /* format the content */
  1123. fc2 = not_really_markdown(content, &atls, &tag);
  1124. if (in_reply_to != NULL && *in_reply_to) {
  1125. xs *p_msg = NULL;
  1126. xs *wrk = NULL;
  1127. /* demand this thing */
  1128. timeline_request(snac, &in_reply_to, &wrk, 0);
  1129. if (valid_status(object_get(in_reply_to, &p_msg))) {
  1130. /* add this author as recipient */
  1131. const char *a, *v;
  1132. if ((a = get_atto(p_msg)) && xs_list_in(to, a) == -1)
  1133. to = xs_list_append(to, a);
  1134. /* add this author to the tag list as a mention */
  1135. if (!xs_is_null(a)) {
  1136. xs *l = xs_split(a, "/");
  1137. xs *actor_o = NULL;
  1138. if (xs_list_len(l) > 3 && valid_status(object_get(a, &actor_o))) {
  1139. const char *uname = xs_dict_get(actor_o, "preferredUsername");
  1140. if (!xs_is_null(uname) && *uname) {
  1141. xs *handle = xs_fmt("@%s@%s", uname, xs_list_get(l, 2));
  1142. xs *t = xs_dict_new();
  1143. t = xs_dict_append(t, "type", "Mention");
  1144. t = xs_dict_append(t, "href", a);
  1145. t = xs_dict_append(t, "name", handle);
  1146. tag = xs_list_append(tag, t);
  1147. }
  1148. }
  1149. }
  1150. /* get the context, if there is one */
  1151. if ((v = xs_dict_get(p_msg, "context")))
  1152. ctxt = xs_dup(v);
  1153. /* propagate the conversation field, if there is one */
  1154. if ((v = xs_dict_get(p_msg, "conversation")))
  1155. msg = xs_dict_append(msg, "conversation", v);
  1156. /* if this message is public, ours will also be */
  1157. if (!priv && is_msg_public(p_msg) && xs_list_in(to, public_address) == -1)
  1158. to = xs_list_append(to, public_address);
  1159. }
  1160. irt = xs_dup(in_reply_to);
  1161. }
  1162. else
  1163. irt = xs_val_new(XSTYPE_NULL);
  1164. /* extract the mentions and hashtags and convert the content */
  1165. fc1 = process_tags(snac, fc2, &tag);
  1166. /* create the attachment list, if there are any */
  1167. if (!xs_is_null(attach)) {
  1168. xs_list_foreach(attach, v) {
  1169. const char *url = xs_list_get(v, 0);
  1170. const char *alt = xs_list_get(v, 1);
  1171. const char *mime = xs_mime_by_ext(url);
  1172. int add = 1;
  1173. /* check if it's already here */
  1174. const xs_dict *ad;
  1175. xs_list_foreach(atls, ad) {
  1176. if (strcmp(xs_dict_get_def(ad, "url", ""), url) == 0) {
  1177. add = 0;
  1178. break;
  1179. }
  1180. }
  1181. if (add) {
  1182. xs *d = xs_dict_new();
  1183. d = xs_dict_append(d, "mediaType", mime);
  1184. d = xs_dict_append(d, "url", url);
  1185. d = xs_dict_append(d, "name", alt);
  1186. d = xs_dict_append(d, "type",
  1187. xs_startswith(mime, "image/") ? "Image" : "Document");
  1188. atls = xs_list_append(atls, d);
  1189. }
  1190. }
  1191. }
  1192. if (ctxt == NULL)
  1193. ctxt = xs_fmt("%s#ctxt", id);
  1194. /* add all mentions to the cc */
  1195. p = tag;
  1196. while (xs_list_iter(&p, &v)) {
  1197. if (xs_type(v) == XSTYPE_DICT) {
  1198. const char *t;
  1199. if (!xs_is_null(t = xs_dict_get(v, "type")) && strcmp(t, "Mention") == 0) {
  1200. if (!xs_is_null(t = xs_dict_get(v, "href")))
  1201. cc = xs_list_append(cc, t);
  1202. }
  1203. }
  1204. }
  1205. /* no recipients? must be for everybody */
  1206. if (!priv && xs_list_len(to) == 0)
  1207. to = xs_list_append(to, public_address);
  1208. /* delete all cc recipients that also are in the to */
  1209. p = to;
  1210. while (xs_list_iter(&p, &v)) {
  1211. int i;
  1212. if ((i = xs_list_in(cc, v)) != -1)
  1213. cc = xs_list_del(cc, i);
  1214. }
  1215. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  1216. msg = xs_dict_append(msg, "summary", "");
  1217. msg = xs_dict_append(msg, "content", fc1);
  1218. msg = xs_dict_append(msg, "context", ctxt);
  1219. msg = xs_dict_append(msg, "url", id);
  1220. msg = xs_dict_append(msg, "to", to);
  1221. msg = xs_dict_append(msg, "cc", cc);
  1222. msg = xs_dict_append(msg, "inReplyTo", irt);
  1223. msg = xs_dict_append(msg, "tag", tag);
  1224. msg = xs_dict_append(msg, "sourceContent", content);
  1225. if (xs_list_len(atls))
  1226. msg = xs_dict_append(msg, "attachment", atls);
  1227. return msg;
  1228. }
  1229. xs_dict *msg_ping(snac *user, const char *rcpt)
  1230. /* creates a Ping message (https://humungus.tedunangst.com/r/honk/v/tip/f/docs/ping.txt) */
  1231. {
  1232. xs_dict *msg = msg_base(user, "Ping", "@dummy", user->actor, NULL, NULL);
  1233. msg = xs_dict_append(msg, "to", rcpt);
  1234. return msg;
  1235. }
  1236. xs_dict *msg_pong(snac *user, const char *rcpt, const char *object)
  1237. /* creates a Pong message (https://humungus.tedunangst.com/r/honk/v/tip/f/docs/ping.txt) */
  1238. {
  1239. xs_dict *msg = msg_base(user, "Pong", "@dummy", user->actor, NULL, object);
  1240. msg = xs_dict_append(msg, "to", rcpt);
  1241. return msg;
  1242. }
  1243. xs_dict *msg_move(snac *user, const char *new_account)
  1244. /* creates a Move message (to move the user to new_account) */
  1245. {
  1246. xs_dict *msg = msg_base(user, "Move", "@dummy", user->actor, NULL, user->actor);
  1247. msg = xs_dict_append(msg, "target", new_account);
  1248. return msg;
  1249. }
  1250. xs_dict *msg_question(snac *user, const char *content, xs_list *attach,
  1251. const xs_list *opts, int multiple, int end_secs)
  1252. /* creates a Question message */
  1253. {
  1254. xs_dict *msg = msg_note(user, content, NULL, NULL, attach, 0);
  1255. int max = 8;
  1256. xs_set seen;
  1257. msg = xs_dict_set(msg, "type", "Question");
  1258. /* make it non-editable */
  1259. msg = xs_dict_del(msg, "sourceContent");
  1260. xs *o = xs_list_new();
  1261. xs_list *p = (xs_list *)opts;
  1262. const xs_str *v;
  1263. xs *replies = xs_json_loads("{\"type\":\"Collection\",\"totalItems\":0}");
  1264. xs_set_init(&seen);
  1265. while (max && xs_list_iter(&p, &v)) {
  1266. if (*v) {
  1267. xs *v2 = xs_dup(v);
  1268. xs *d = xs_dict_new();
  1269. if (strlen(v2) > 60) {
  1270. v2[60] = '\0';
  1271. v2 = xs_str_cat(v2, "...");
  1272. }
  1273. if (xs_set_add(&seen, v2) == 1) {
  1274. d = xs_dict_append(d, "type", "Note");
  1275. d = xs_dict_append(d, "name", v2);
  1276. d = xs_dict_append(d, "replies", replies);
  1277. o = xs_list_append(o, d);
  1278. max--;
  1279. }
  1280. }
  1281. }
  1282. xs_set_free(&seen);
  1283. msg = xs_dict_append(msg, multiple ? "anyOf" : "oneOf", o);
  1284. /* set the end time */
  1285. time_t t = time(NULL) + end_secs;
  1286. xs *et = xs_str_utctime(t, ISO_DATE_SPEC);
  1287. msg = xs_dict_append(msg, "endTime", et);
  1288. return msg;
  1289. }
  1290. int update_question(snac *user, const char *id)
  1291. /* updates the poll counts */
  1292. {
  1293. xs *msg = NULL;
  1294. xs *rcnt = xs_dict_new();
  1295. xs *lopts = xs_list_new();
  1296. const xs_list *opts;
  1297. xs_list *p;
  1298. const xs_val *v;
  1299. /* get the object */
  1300. if (!valid_status(object_get(id, &msg)))
  1301. return -1;
  1302. /* closed? do nothing more */
  1303. if (xs_dict_get(msg, "closed"))
  1304. return -2;
  1305. /* get the options */
  1306. if ((opts = xs_dict_get(msg, "oneOf")) == NULL &&
  1307. (opts = xs_dict_get(msg, "anyOf")) == NULL)
  1308. return -3;
  1309. /* fill the initial count */
  1310. int c = 0;
  1311. while (xs_list_next(opts, &v, &c)) {
  1312. const char *name = xs_dict_get(v, "name");
  1313. if (name) {
  1314. lopts = xs_list_append(lopts, name);
  1315. rcnt = xs_dict_set(rcnt, name, xs_stock(0));
  1316. }
  1317. }
  1318. xs_set s;
  1319. xs_set_init(&s);
  1320. /* iterate now the children (the votes) */
  1321. xs *chld = object_children(id);
  1322. p = chld;
  1323. while (xs_list_iter(&p, &v)) {
  1324. xs *obj = NULL;
  1325. if (!valid_status(object_get_by_md5(v, &obj)))
  1326. continue;
  1327. const char *name = xs_dict_get(obj, "name");
  1328. const char *atto = get_atto(obj);
  1329. if (name && atto) {
  1330. /* get the current count */
  1331. const xs_number *cnt = xs_dict_get(rcnt, name);
  1332. if (xs_type(cnt) == XSTYPE_NUMBER) {
  1333. /* if it exists, increment */
  1334. xs *ucnt = xs_number_new(xs_number_get(cnt) + 1);
  1335. rcnt = xs_dict_set(rcnt, name, ucnt);
  1336. xs_set_add(&s, atto);
  1337. }
  1338. }
  1339. }
  1340. xs *rcpts = xs_set_result(&s);
  1341. /* create a new list of options with their new counts */
  1342. xs *nopts = xs_list_new();
  1343. p = lopts;
  1344. while (xs_list_iter(&p, &v)) {
  1345. const xs_number *cnt = xs_dict_get(rcnt, v);
  1346. if (xs_type(cnt) == XSTYPE_NUMBER) {
  1347. xs *d1 = xs_dict_new();
  1348. xs *d2 = xs_dict_new();
  1349. d2 = xs_dict_append(d2, "type", "Collection");
  1350. d2 = xs_dict_append(d2, "totalItems", cnt);
  1351. d1 = xs_dict_append(d1, "type", "Note");
  1352. d1 = xs_dict_append(d1, "name", v);
  1353. d1 = xs_dict_append(d1, "replies", d2);
  1354. nopts = xs_list_append(nopts, d1);
  1355. }
  1356. }
  1357. /* update the list */
  1358. msg = xs_dict_set(msg, xs_dict_get(msg, "oneOf") != NULL ? "oneOf" : "anyOf", nopts);
  1359. /* due date? */
  1360. int closed = 0;
  1361. const char *end_time = xs_dict_get(msg, "endTime");
  1362. if (!xs_is_null(end_time)) {
  1363. xs *now = xs_str_utctime(0, ISO_DATE_SPEC);
  1364. /* is now greater than the endTime? */
  1365. if (strcmp(now, end_time) >= 0) {
  1366. xs *et = xs_dup(end_time);
  1367. msg = xs_dict_set(msg, "closed", et);
  1368. closed = 1;
  1369. }
  1370. }
  1371. /* update the count of voters */
  1372. xs *vcnt = xs_number_new(xs_list_len(rcpts));
  1373. msg = xs_dict_set(msg, "votersCount", vcnt);
  1374. msg = xs_dict_set(msg, "cc", rcpts);
  1375. /* store */
  1376. object_add_ow(id, msg);
  1377. snac_debug(user, 1, xs_fmt("recounted poll %s", id));
  1378. timeline_touch(user);
  1379. /* send an update message to all voters */
  1380. xs *u_msg = msg_update(user, msg);
  1381. u_msg = xs_dict_set(u_msg, "cc", rcpts);
  1382. enqueue_message(user, u_msg);
  1383. if (closed) {
  1384. xs *c_msg = msg_update(user, msg);
  1385. notify(user, "Update", "Question", user->actor, c_msg);
  1386. }
  1387. return 0;
  1388. }
  1389. /** queues **/
  1390. int process_input_message(snac *snac, const xs_dict *msg, const xs_dict *req)
  1391. /* processes an ActivityPub message from the input queue */
  1392. /* return values: -1, fatal error; 0, transient error, retry;
  1393. 1, processed and done; 2, propagate to users (only when no user is set) */
  1394. {
  1395. const char *actor = xs_dict_get(msg, "actor");
  1396. const char *type = xs_dict_get(msg, "type");
  1397. xs *actor_o = NULL;
  1398. int a_status;
  1399. int do_notify = 0;
  1400. if (xs_is_null(actor) || *actor == '\0') {
  1401. srv_debug(0, xs_fmt("malformed message (bad actor)"));
  1402. return -1;
  1403. }
  1404. /* question votes may not have a type */
  1405. if (xs_is_null(type))
  1406. type = "Note";
  1407. /* reject uninteresting messages right now */
  1408. if (xs_match(type, "Add|View|Reject|Read|Remove")) {
  1409. srv_debug(0, xs_fmt("Ignored message of type '%s'", type));
  1410. return -1;
  1411. }
  1412. const char *object, *utype;
  1413. object = xs_dict_get(msg, "object");
  1414. if (object != NULL && xs_type(object) == XSTYPE_DICT)
  1415. utype = xs_dict_get(object, "type");
  1416. else
  1417. utype = "(null)";
  1418. /* special case for Delete messages */
  1419. if (strcmp(type, "Delete") == 0) {
  1420. /* if the actor is not here, do not even try */
  1421. if (!object_here(actor)) {
  1422. srv_debug(1, xs_fmt("dropped 'Delete' message from unknown actor '%s'", actor));
  1423. return -1;
  1424. }
  1425. /* discard crap */
  1426. if (xs_is_null(object)) {
  1427. srv_log(xs_fmt("dropped 'Delete' message with invalid object from actor '%s'", actor));
  1428. return -1;
  1429. }
  1430. /* also discard if the object to be deleted is not here */
  1431. const char *obj_id = object;
  1432. if (xs_type(obj_id) == XSTYPE_DICT)
  1433. obj_id = xs_dict_get(obj_id, "id");
  1434. if (xs_is_null(obj_id) || !object_here(obj_id)) {
  1435. srv_debug(1, xs_fmt("dropped 'Delete' message from unknown object '%s'", obj_id));
  1436. return -1;
  1437. }
  1438. }
  1439. /* bring the actor */
  1440. a_status = actor_request(snac, actor, &actor_o);
  1441. /* do not retry permanent failures */
  1442. if (a_status == HTTP_STATUS_NOT_FOUND
  1443. || a_status == HTTP_STATUS_GONE
  1444. || a_status < 0) {
  1445. srv_debug(1, xs_fmt("dropping message due to actor error %s %d", actor, a_status));
  1446. return -1;
  1447. }
  1448. if (!valid_status(a_status)) {
  1449. /* do not retry 'Delete' messages */
  1450. if (strcmp(type, "Delete") == 0) {
  1451. srv_debug(1, xs_fmt("dropping 'Delete' message due to actor error %s %d", actor, a_status));
  1452. return -1;
  1453. }
  1454. /* other actor download errors */
  1455. /* the actor may require a signed request; propagate if no user is set */
  1456. if (snac == NULL)
  1457. return 2;
  1458. /* may need a retry */
  1459. srv_debug(0, xs_fmt("error requesting actor %s %d -- retry later", actor, a_status));
  1460. return 0;
  1461. }
  1462. /* check the signature */
  1463. xs *sig_err = NULL;
  1464. if (!check_signature(req, &sig_err)) {
  1465. srv_log(xs_fmt("bad signature %s (%s)", actor, sig_err));
  1466. srv_archive_error("check_signature", sig_err, req, msg);
  1467. return -1;
  1468. }
  1469. /* if no user is set, no further checks can be done; propagate */
  1470. if (snac == NULL)
  1471. return 2;
  1472. /* reject messages that are not for this user */
  1473. if (!is_msg_for_me(snac, msg)) {
  1474. snac_debug(snac, 1, xs_fmt("message from %s of type '%s' not for us", actor, type));
  1475. return 1;
  1476. }
  1477. /* if it's a DM from someone we don't follow, reject the message */
  1478. if (xs_type(xs_dict_get(snac->config, "drop_dm_from_unknown")) == XSTYPE_TRUE) {
  1479. if (strcmp(utype, "Note") == 0 && !is_msg_public(msg) &&
  1480. !following_check(snac, actor)) {
  1481. snac_log(snac, xs_fmt("DM rejected from unknown actor %s", actor));
  1482. return 1;
  1483. }
  1484. }
  1485. /* check the minimum acceptable account age */
  1486. int min_account_age = xs_number_get(xs_dict_get(srv_config, "min_account_age"));
  1487. if (min_account_age > 0) {
  1488. const char *actor_date = xs_dict_get(actor_o, "published");
  1489. if (!xs_is_null(actor_date)) {
  1490. time_t actor_t = xs_parse_iso_date(actor_date, 0);
  1491. if (actor_t < 950000000) {
  1492. snac_log(snac, xs_fmt("rejected activity from %s (suspicious date, %s)",
  1493. actor, actor_date));
  1494. return 1;
  1495. }
  1496. if (actor_t > 0) {
  1497. int td = (int)(time(NULL) - actor_t);
  1498. snac_debug(snac, 2, xs_fmt("actor %s age: %d seconds", actor, td));
  1499. if (td < min_account_age) {
  1500. snac_log(snac, xs_fmt("rejected activity from %s (too new, %d seconds)",
  1501. actor, td));
  1502. return 1;
  1503. }
  1504. }
  1505. }
  1506. else
  1507. snac_debug(snac, 1, xs_fmt("warning: empty or null creation date for %s", actor));
  1508. }
  1509. if (strcmp(type, "Follow") == 0) { /** **/
  1510. const char *id = xs_dict_get(msg, "id");
  1511. if (xs_is_null(id)) {
  1512. snac_log(snac, xs_fmt("malformed message: no 'id' field"));
  1513. }
  1514. else
  1515. if (!follower_check(snac, actor)) {
  1516. /* ensure the actor object is here */
  1517. if (!object_here(actor)) {
  1518. xs *actor_obj = NULL;
  1519. actor_request(snac, actor, &actor_obj);
  1520. object_add(actor, actor_obj);
  1521. }
  1522. if (xs_is_true(xs_dict_get(snac->config, "approve_followers"))) {
  1523. pending_add(snac, actor, msg);
  1524. snac_log(snac, xs_fmt("new pending follower approval %s", actor));
  1525. }
  1526. else {
  1527. /* automatic following */
  1528. xs *f_msg = xs_dup(msg);
  1529. xs *reply = msg_accept(snac, f_msg, actor);
  1530. post_message(snac, actor, reply);
  1531. if (xs_is_null(xs_dict_get(f_msg, "published"))) {
  1532. /* add a date if it doesn't include one (Mastodon) */
  1533. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  1534. f_msg = xs_dict_set(f_msg, "published", date);
  1535. }
  1536. timeline_add(snac, id, f_msg);
  1537. follower_add(snac, actor);
  1538. snac_log(snac, xs_fmt("new follower %s", actor));
  1539. }
  1540. do_notify = 1;
  1541. }
  1542. else
  1543. snac_log(snac, xs_fmt("repeated 'Follow' from %s", actor));
  1544. }
  1545. else
  1546. if (strcmp(type, "Undo") == 0) { /** **/
  1547. const char *id = xs_dict_get(object, "object");
  1548. if (xs_type(object) != XSTYPE_DICT)
  1549. utype = "Follow";
  1550. if (strcmp(utype, "Follow") == 0) { /** **/
  1551. if (id && strcmp(id, snac->actor) != 0)
  1552. snac_debug(snac, 1, xs_fmt("Undo + Follow from %s not for us (%s)", actor, id));
  1553. else {
  1554. if (valid_status(follower_del(snac, actor))) {
  1555. snac_log(snac, xs_fmt("no longer following us %s", actor));
  1556. do_notify = 1;
  1557. }
  1558. else
  1559. if (pending_check(snac, actor)) {
  1560. pending_del(snac, actor);
  1561. snac_log(snac, xs_fmt("cancelled pending follow from %s", actor));
  1562. }
  1563. else
  1564. snac_log(snac, xs_fmt("error deleting follower %s", actor));
  1565. }
  1566. }
  1567. else
  1568. if (strcmp(utype, "Like") == 0 || strcmp(utype, "EmojiReact") == 0) { /** **/
  1569. int status = object_unadmire(id, actor, 1);
  1570. snac_log(snac, xs_fmt("Undo '%s' for %s %d", utype, id, status));
  1571. }
  1572. else
  1573. if (strcmp(utype, "Announce") == 0) { /** **/
  1574. int status = HTTP_STATUS_OK;
  1575. /* commented out: if a followed user boosts something that
  1576. is requested and then unboosts, the post remains here,
  1577. but with no apparent reason, and that is confusing */
  1578. //status = object_unadmire(id, actor, 0);
  1579. snac_log(snac, xs_fmt("Unboost for %s %d", id, status));
  1580. }
  1581. else
  1582. snac_debug(snac, 1, xs_fmt("ignored 'Undo' for object type '%s'", utype));
  1583. }
  1584. else
  1585. if (strcmp(type, "Create") == 0) { /** **/
  1586. if (is_muted(snac, actor)) {
  1587. snac_log(snac, xs_fmt("ignored 'Create' + '%s' from muted actor %s", utype, actor));
  1588. return 1;
  1589. }
  1590. if (xs_match(utype, "Note|Article")) { /** **/
  1591. const char *id = xs_dict_get(object, "id");
  1592. const char *in_reply_to = get_in_reply_to(object);
  1593. const char *atto = get_atto(object);
  1594. xs *wrk = NULL;
  1595. if (xs_is_null(id))
  1596. snac_log(snac, xs_fmt("malformed message: no 'id' field"));
  1597. else
  1598. if (xs_is_null(atto))
  1599. snac_log(snac, xs_fmt("malformed message: no 'attributedTo' field"));
  1600. else
  1601. if (!xs_is_null(in_reply_to) && is_hidden(snac, in_reply_to)) {
  1602. snac_debug(snac, 0, xs_fmt("dropped reply %s to hidden post %s", id, in_reply_to));
  1603. }
  1604. else {
  1605. if (content_match("filter_reject.txt", object)) {
  1606. snac_log(snac, xs_fmt("rejected by content %s", id));
  1607. return 1;
  1608. }
  1609. if (strcmp(actor, atto) != 0)
  1610. snac_log(snac, xs_fmt("SUSPICIOUS: actor != atto (%s != %s)", actor, atto));
  1611. timeline_request(snac, &in_reply_to, &wrk, 0);
  1612. if (timeline_add(snac, id, object)) {
  1613. snac_log(snac, xs_fmt("new '%s' %s %s", utype, actor, id));
  1614. do_notify = 1;
  1615. }
  1616. /* if it has a "name" field, it may be a vote for a question */
  1617. const char *name = xs_dict_get(object, "name");
  1618. if (!xs_is_null(name) && *name && !xs_is_null(in_reply_to) && *in_reply_to)
  1619. update_question(snac, in_reply_to);
  1620. }
  1621. }
  1622. else
  1623. if (strcmp(utype, "Question") == 0) { /** **/
  1624. const char *id = xs_dict_get(object, "id");
  1625. if (xs_is_null(id))
  1626. snac_log(snac, xs_fmt("malformed message: no 'id' field"));
  1627. else
  1628. if (timeline_add(snac, id, object))
  1629. snac_log(snac, xs_fmt("new 'Question' %s %s", actor, id));
  1630. }
  1631. else
  1632. if (strcmp(utype, "Video") == 0) { /** **/
  1633. const char *id = xs_dict_get(object, "id");
  1634. if (xs_is_null(id))
  1635. snac_log(snac, xs_fmt("malformed message: no 'id' field"));
  1636. else
  1637. if (timeline_add(snac, id, object))
  1638. snac_log(snac, xs_fmt("new 'Video' %s %s", actor, id));
  1639. }
  1640. else
  1641. snac_debug(snac, 1, xs_fmt("ignored 'Create' for object type '%s'", utype));
  1642. }
  1643. else
  1644. if (strcmp(type, "Accept") == 0) { /** **/
  1645. if (strcmp(utype, "(null)") == 0) {
  1646. const char *obj_id = xs_dict_get(msg, "object");
  1647. /* if the accepted object id is a string that may
  1648. be created by us, it's a follow */
  1649. if (xs_type(obj_id) == XSTYPE_STRING &&
  1650. xs_startswith(obj_id, srv_baseurl) &&
  1651. xs_endswith(obj_id, "/Follow"))
  1652. utype = "Follow";
  1653. }
  1654. if (strcmp(utype, "Follow") == 0) { /** **/
  1655. if (following_check(snac, actor)) {
  1656. following_add(snac, actor, msg);
  1657. snac_log(snac, xs_fmt("confirmed follow from %s", actor));
  1658. }
  1659. else
  1660. snac_log(snac, xs_fmt("spurious follow accept from %s", actor));
  1661. }
  1662. else
  1663. if (strcmp(utype, "Create") == 0) {
  1664. /* some implementations send Create confirmations, go figure */
  1665. snac_debug(snac, 1, xs_dup("ignored 'Accept' + 'Create'"));
  1666. }
  1667. else {
  1668. srv_archive_error("accept", "ignored Accept", req, msg);
  1669. snac_debug(snac, 1, xs_fmt("ignored 'Accept' for object type '%s'", utype));
  1670. }
  1671. }
  1672. else
  1673. if (strcmp(type, "Like") == 0 || strcmp(type, "EmojiReact") == 0) { /** **/
  1674. if (xs_type(object) == XSTYPE_DICT)
  1675. object = xs_dict_get(object, "id");
  1676. if (xs_is_null(object))
  1677. snac_log(snac, xs_fmt("malformed message: no 'id' field"));
  1678. else
  1679. if (timeline_admire(snac, object, actor, 1) == HTTP_STATUS_CREATED)
  1680. snac_log(snac, xs_fmt("new '%s' %s %s", type, actor, object));
  1681. else
  1682. snac_log(snac, xs_fmt("repeated '%s' from %s to %s", type, actor, object));
  1683. do_notify = 1;
  1684. }
  1685. else
  1686. if (strcmp(type, "Announce") == 0) { /** **/
  1687. if (xs_type(object) == XSTYPE_DICT)
  1688. object = xs_dict_get(object, "id");
  1689. if (xs_is_null(object))
  1690. snac_log(snac, xs_fmt("malformed message: no 'id' field"));
  1691. else
  1692. if (is_muted(snac, actor) && !xs_startswith(object, snac->actor))
  1693. snac_log(snac, xs_fmt("dropped 'Announce' from muted actor %s", actor));
  1694. else
  1695. if (is_limited(snac, actor) && !xs_startswith(object, snac->actor))
  1696. snac_log(snac, xs_fmt("dropped 'Announce' from limited actor %s", actor));
  1697. else {
  1698. xs *a_msg = NULL;
  1699. xs *wrk = NULL;
  1700. timeline_request(snac, &object, &wrk, 0);
  1701. if (valid_status(object_get(object, &a_msg))) {
  1702. const char *who = get_atto(a_msg);
  1703. if (who && !is_muted(snac, who)) {
  1704. /* bring the actor */
  1705. xs *who_o = NULL;
  1706. if (valid_status(actor_request(snac, who, &who_o))) {
  1707. if (timeline_admire(snac, object, actor, 0) == HTTP_STATUS_CREATED)
  1708. snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object));
  1709. else
  1710. snac_log(snac, xs_fmt("repeated 'Announce' from %s to %s",
  1711. actor, object));
  1712. /* distribute the post with the actor as 'proxy' */
  1713. list_distribute(snac, actor, a_msg);
  1714. do_notify = 1;
  1715. }
  1716. else
  1717. snac_debug(snac, 1, xs_fmt("dropped 'Announce' on actor request error %s", who));
  1718. }
  1719. else
  1720. snac_log(snac, xs_fmt("ignored 'Announce' about muted actor %s", who));
  1721. }
  1722. else
  1723. snac_debug(snac, 2, xs_fmt("error requesting 'Announce' object %s", object));
  1724. }
  1725. }
  1726. else
  1727. if (strcmp(type, "Update") == 0) { /** **/
  1728. if (xs_match(utype, "Person|Service")) { /** **/
  1729. actor_add(actor, xs_dict_get(msg, "object"));
  1730. timeline_touch(snac);
  1731. snac_log(snac, xs_fmt("updated actor %s", actor));
  1732. }
  1733. else
  1734. if (xs_match(utype, "Note|Page|Article|Video")) { /** **/
  1735. const char *id = xs_dict_get(object, "id");
  1736. if (xs_is_null(id))
  1737. snac_log(snac, xs_fmt("malformed message: no 'id' field"));
  1738. else
  1739. if (object_here(id)) {
  1740. object_add_ow(id, object);
  1741. timeline_touch(snac);
  1742. snac_log(snac, xs_fmt("updated '%s' %s", utype, id));
  1743. }
  1744. else
  1745. snac_log(snac, xs_fmt("dropped update for unknown '%s' %s", utype, id));
  1746. }
  1747. else
  1748. if (strcmp(utype, "Question") == 0) { /** **/
  1749. const char *id = xs_dict_get(object, "id");
  1750. const char *closed = xs_dict_get(object, "closed");
  1751. if (xs_is_null(id))
  1752. snac_log(snac, xs_fmt("malformed message: no 'id' field"));
  1753. else {
  1754. object_add_ow(id, object);
  1755. timeline_touch(snac);
  1756. snac_log(snac, xs_fmt("%s poll %s", closed == NULL ? "updated" : "closed", id));
  1757. if (closed != NULL)
  1758. do_notify = 1;
  1759. }
  1760. }
  1761. else {
  1762. srv_archive_error("unsupported_update", "unsupported_update", req, msg);
  1763. snac_log(snac, xs_fmt("ignored 'Update' for object type '%s'", utype));
  1764. }
  1765. }
  1766. else
  1767. if (strcmp(type, "Delete") == 0) { /** **/
  1768. if (xs_type(object) == XSTYPE_DICT)
  1769. object = xs_dict_get(object, "id");
  1770. if (xs_is_null(object))
  1771. snac_log(snac, xs_fmt("malformed message: no 'id' field"));
  1772. else
  1773. if (object_here(object)) {
  1774. timeline_del(snac, object);
  1775. snac_debug(snac, 1, xs_fmt("new 'Delete' %s %s", actor, object));
  1776. }
  1777. else
  1778. snac_debug(snac, 1, xs_fmt("ignored 'Delete' for unknown object %s", object));
  1779. }
  1780. else
  1781. if (strcmp(type, "Pong") == 0) { /** **/
  1782. snac_log(snac, xs_fmt("'Pong' received from %s", actor));
  1783. }
  1784. else
  1785. if (strcmp(type, "Ping") == 0) { /** **/
  1786. const char *id = xs_dict_get(msg, "id");
  1787. snac_log(snac, xs_fmt("'Ping' requested from %s", actor));
  1788. if (!xs_is_null(id)) {
  1789. xs *rsp = msg_pong(snac, actor, id);
  1790. enqueue_output_by_actor(snac, rsp, actor, 0);
  1791. }
  1792. }
  1793. else
  1794. if (strcmp(type, "Block") == 0) { /** **/
  1795. snac_debug(snac, 1, xs_fmt("'Block' received from %s", actor));
  1796. /* should we MUTE the actor back? */
  1797. /* mute(snac, actor); */
  1798. if (!xs_is_true(xs_dict_get(srv_config, "disable_block_notifications")))
  1799. do_notify = 1;
  1800. }
  1801. else
  1802. if (strcmp(type, "Move") == 0) { /** **/
  1803. do_notify = 1;
  1804. const char *old_account = xs_dict_get(msg, "object");
  1805. const char *new_account = xs_dict_get(msg, "target");
  1806. if (!xs_is_null(old_account) && !xs_is_null(new_account)) {
  1807. if (following_check(snac, old_account)) {
  1808. xs *n_actor = NULL;
  1809. if (valid_status(object_get(new_account, &n_actor))) {
  1810. const xs_list *aka = xs_dict_get(n_actor, "alsoKnownAs");
  1811. if (xs_type(aka) == XSTYPE_LIST) {
  1812. if (xs_list_in(aka, old_account) != -1) {
  1813. /* all conditions met! */
  1814. /* follow new account */
  1815. xs *f_msg = msg_follow(snac, new_account);
  1816. if (f_msg != NULL) {
  1817. const char *new_actor = xs_dict_get(f_msg, "object");
  1818. following_add(snac, new_actor, f_msg);
  1819. enqueue_output_by_actor(snac, f_msg, new_actor, 0);
  1820. snac_log(snac, xs_fmt("'Move': following %s", new_account));
  1821. }
  1822. /* unfollow old account */
  1823. xs *of_msg = NULL;
  1824. if (valid_status(following_get(snac, old_account, &of_msg))) {
  1825. xs *uf_msg = msg_undo(snac, xs_dict_get(of_msg, "object"));
  1826. following_del(snac, old_account);
  1827. enqueue_output_by_actor(snac, uf_msg, old_account, 0);
  1828. snac_log(snac, xs_fmt("'Move': unfollowing %s", old_account));
  1829. }
  1830. }
  1831. else
  1832. snac_log(snac, xs_fmt("'Move' error: old actor %s not found in %s 'alsoKnownAs'",
  1833. old_account, new_account));
  1834. }
  1835. else
  1836. snac_log(snac, xs_fmt("'Move' error: cannot get %s 'alsoKnownAs'", new_account));
  1837. }
  1838. else {
  1839. snac_log(snac, xs_fmt("'Move' error: cannot get new actor %s", new_account));
  1840. /* may be a server hiccup, retry later */
  1841. return 0;
  1842. }
  1843. }
  1844. else
  1845. snac_log(snac, xs_fmt("'Move' error: actor %s is not being followed", old_account));
  1846. }
  1847. else {
  1848. snac_log(snac, xs_fmt("'Move' error: malformed message from %s", actor));
  1849. srv_archive_error("move", "move", req, msg);
  1850. }
  1851. }
  1852. else {
  1853. srv_archive_error("unsupported_type", "unsupported_type", req, msg);
  1854. snac_debug(snac, 1, xs_fmt("process_input_message type '%s' ignored", type));
  1855. }
  1856. if (do_notify) {
  1857. notify(snac, type, utype, actor, msg);
  1858. timeline_touch(snac);
  1859. }
  1860. return 1;
  1861. }
  1862. int send_email(const char *msg)
  1863. /* invoke sendmail with email headers and body in msg */
  1864. {
  1865. FILE *f;
  1866. int status;
  1867. int fds[2];
  1868. pid_t pid;
  1869. if (pipe(fds) == -1) return -1;
  1870. pid = vfork();
  1871. if (pid == -1) return -1;
  1872. else if (pid == 0) {
  1873. dup2(fds[0], 0);
  1874. close(fds[0]);
  1875. close(fds[1]);
  1876. execl("/usr/sbin/sendmail", "sendmail", "-t", (char *) NULL);
  1877. _exit(1);
  1878. }
  1879. close(fds[0]);
  1880. if ((f = fdopen(fds[1], "w")) == NULL) {
  1881. close(fds[1]);
  1882. return -1;
  1883. }
  1884. fprintf(f, "%s\n", msg);
  1885. fclose(f);
  1886. if (waitpid(pid, &status, 0) == -1) return -1;
  1887. return status;
  1888. }
  1889. void process_user_queue_item(snac *snac, xs_dict *q_item)
  1890. /* processes an item from the user queue */
  1891. {
  1892. const char *type;
  1893. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  1894. if ((type = xs_dict_get(q_item, "type")) == NULL)
  1895. type = "output";
  1896. if (strcmp(type, "message") == 0) {
  1897. const xs_dict *msg = xs_dict_get(q_item, "message");
  1898. xs *rcpts = recipient_list(snac, msg, 1);
  1899. xs_set inboxes;
  1900. const xs_str *actor;
  1901. xs_set_init(&inboxes);
  1902. /* add this shared inbox first */
  1903. xs *this_shared_inbox = xs_fmt("%s/shared-inbox", srv_baseurl);
  1904. xs_set_add(&inboxes, this_shared_inbox);
  1905. enqueue_output(snac, msg, this_shared_inbox, 0, 0);
  1906. /* iterate the recipients */
  1907. xs_list_foreach(rcpts, actor) {
  1908. xs *inbox = get_actor_inbox(actor, 1);
  1909. if (inbox != NULL) {
  1910. /* add to the set and, if it's not there, send message */
  1911. if (xs_set_add(&inboxes, inbox) == 1)
  1912. enqueue_output(snac, msg, inbox, 0, 0);
  1913. }
  1914. else
  1915. snac_log(snac, xs_fmt("cannot find inbox for %s", actor));
  1916. }
  1917. /* if it's a public note or question, send to the collected inboxes */
  1918. if (xs_match(xs_dict_get_def(msg, "type", ""), "Create|Update") && is_msg_public(msg)) {
  1919. if (xs_type(xs_dict_get(srv_config, "disable_inbox_collection")) != XSTYPE_TRUE) {
  1920. xs *shibx = inbox_list();
  1921. const xs_str *inbox;
  1922. xs_list_foreach(shibx, inbox) {
  1923. if (xs_set_add(&inboxes, inbox) == 1)
  1924. enqueue_output(snac, msg, inbox, 0, 0);
  1925. }
  1926. }
  1927. }
  1928. xs_set_free(&inboxes);
  1929. }
  1930. else
  1931. if (strcmp(type, "input") == 0) {
  1932. /* process the message */
  1933. const xs_dict *msg = xs_dict_get(q_item, "message");
  1934. const xs_dict *req = xs_dict_get(q_item, "req");
  1935. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  1936. if (xs_is_null(msg))
  1937. return;
  1938. if (!process_input_message(snac, msg, req)) {
  1939. if (retries > queue_retry_max)
  1940. snac_log(snac, xs_fmt("input giving up"));
  1941. else {
  1942. /* reenqueue */
  1943. enqueue_input(snac, msg, req, retries + 1);
  1944. snac_log(snac, xs_fmt("input requeue #%d", retries + 1));
  1945. }
  1946. }
  1947. }
  1948. else
  1949. if (strcmp(type, "close_question") == 0) {
  1950. /* the time for this question has ended */
  1951. const char *id = xs_dict_get(q_item, "message");
  1952. if (!xs_is_null(id))
  1953. update_question(snac, id);
  1954. }
  1955. else
  1956. if (strcmp(type, "object_request") == 0) {
  1957. const char *id = xs_dict_get(q_item, "message");
  1958. if (!xs_is_null(id)) {
  1959. int status;
  1960. xs *data = NULL;
  1961. status = activitypub_request(snac, id, &data);
  1962. if (valid_status(status))
  1963. object_add_ow(id, data);
  1964. snac_debug(snac, 1, xs_fmt("object_request %s %d", id, status));
  1965. }
  1966. }
  1967. else
  1968. if (strcmp(type, "verify_links") == 0) {
  1969. verify_links(snac);
  1970. }
  1971. else
  1972. if (strcmp(type, "actor_refresh") == 0) {
  1973. const char *actor = xs_dict_get(q_item, "actor");
  1974. double mtime = object_mtime(actor);
  1975. /* only refresh if it was refreshed more than an hour ago */
  1976. if (mtime + 3600.0 < (double) time(NULL)) {
  1977. xs *actor_o = NULL;
  1978. int status;
  1979. if (valid_status((status = activitypub_request(snac, actor, &actor_o))))
  1980. actor_add(actor, actor_o);
  1981. else
  1982. object_touch(actor);
  1983. snac_log(snac, xs_fmt("actor_refresh %s %d", actor, status));
  1984. }
  1985. }
  1986. else
  1987. snac_log(snac, xs_fmt("unexpected user q_item type '%s'", type));
  1988. }
  1989. int process_user_queue(snac *snac)
  1990. /* processes a user's queue */
  1991. {
  1992. int cnt = 0;
  1993. xs *list = user_queue(snac);
  1994. xs_list *p = list;
  1995. const xs_str *fn;
  1996. while (xs_list_iter(&p, &fn)) {
  1997. xs *q_item = dequeue(fn);
  1998. if (q_item == NULL)
  1999. continue;
  2000. process_user_queue_item(snac, q_item);
  2001. cnt++;
  2002. }
  2003. return cnt;
  2004. }
  2005. void process_queue_item(xs_dict *q_item)
  2006. /* processes an item from the global queue */
  2007. {
  2008. const char *type = xs_dict_get(q_item, "type");
  2009. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  2010. if (strcmp(type, "output") == 0) {
  2011. int status;
  2012. const xs_str *inbox = xs_dict_get(q_item, "inbox");
  2013. const xs_str *keyid = xs_dict_get(q_item, "keyid");
  2014. const xs_str *seckey = xs_dict_get(q_item, "seckey");
  2015. const xs_dict *msg = xs_dict_get(q_item, "message");
  2016. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  2017. int p_status = xs_number_get(xs_dict_get(q_item, "p_status"));
  2018. xs *payload = NULL;
  2019. int p_size = 0;
  2020. int timeout = 0;
  2021. if (xs_is_null(inbox) || xs_is_null(msg) || xs_is_null(keyid) || xs_is_null(seckey)) {
  2022. srv_log(xs_fmt("output message error: missing fields"));
  2023. return;
  2024. }
  2025. if (is_instance_blocked(inbox)) {
  2026. srv_debug(0, xs_fmt("discarded output message to blocked instance %s", inbox));
  2027. return;
  2028. }
  2029. /* deliver (if previous error status was a timeout, try now longer) */
  2030. if (p_status == 599)
  2031. timeout = xs_number_get(xs_dict_get_def(srv_config, "queue_timeout_2", "8"));
  2032. else
  2033. timeout = xs_number_get(xs_dict_get_def(srv_config, "queue_timeout", "6"));
  2034. if (timeout == 0)
  2035. timeout = 6;
  2036. status = send_to_inbox_raw(keyid, seckey, inbox, msg, &payload, &p_size, timeout);
  2037. if (payload) {
  2038. if (p_size > 64) {
  2039. /* trim the message */
  2040. payload[64] = '\0';
  2041. payload = xs_str_cat(payload, "...");
  2042. }
  2043. /* strip ugly control characters */
  2044. payload = xs_replace_i(payload, "\n", "");
  2045. payload = xs_replace_i(payload, "\r", "");
  2046. if (*payload)
  2047. payload = xs_str_wrap_i(" [", payload, "]");
  2048. }
  2049. else
  2050. payload = xs_str_new(NULL);
  2051. srv_log(xs_fmt("output message: sent to inbox %s %d%s", inbox, status, payload));
  2052. if (!valid_status(status)) {
  2053. retries++;
  2054. /* if it's not the first time it fails with a timeout,
  2055. penalize the server by skipping one retry */
  2056. if (p_status == status && status == HTTP_STATUS_CLIENT_CLOSED_REQUEST)
  2057. retries++;
  2058. /* error sending; requeue? */
  2059. if (status == HTTP_STATUS_BAD_REQUEST
  2060. || status == HTTP_STATUS_NOT_FOUND
  2061. || status == HTTP_STATUS_METHOD_NOT_ALLOWED
  2062. || status == HTTP_STATUS_GONE
  2063. || status == HTTP_STATUS_UNPROCESSABLE_CONTENT
  2064. || status < 0)
  2065. /* explicit error: discard */
  2066. srv_log(xs_fmt("output message: fatal error %s %d", inbox, status));
  2067. else
  2068. if (retries > queue_retry_max)
  2069. srv_log(xs_fmt("output message: giving up %s %d", inbox, status));
  2070. else {
  2071. /* requeue */
  2072. enqueue_output_raw(keyid, seckey, msg, inbox, retries, status);
  2073. srv_log(xs_fmt("output message: requeue %s #%d", inbox, retries));
  2074. }
  2075. }
  2076. }
  2077. else
  2078. if (strcmp(type, "email") == 0) {
  2079. /* send this email */
  2080. const xs_str *msg = xs_dict_get(q_item, "message");
  2081. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  2082. if (!send_email(msg))
  2083. srv_debug(1, xs_fmt("email message sent"));
  2084. else {
  2085. retries++;
  2086. if (retries > queue_retry_max)
  2087. srv_log(xs_fmt("email giving up (errno: %d)", errno));
  2088. else {
  2089. /* requeue */
  2090. srv_log(xs_fmt(
  2091. "email requeue #%d (errno: %d)", retries, errno));
  2092. enqueue_email(msg, retries);
  2093. }
  2094. }
  2095. }
  2096. else
  2097. if (strcmp(type, "telegram") == 0) {
  2098. /* send this via telegram */
  2099. const char *bot = xs_dict_get(q_item, "bot");
  2100. const char *msg = xs_dict_get(q_item, "message");
  2101. xs *chat_id = xs_dup(xs_dict_get(q_item, "chat_id"));
  2102. int status = 0;
  2103. /* chat_id must start with a - */
  2104. if (!xs_startswith(chat_id, "-"))
  2105. chat_id = xs_str_wrap_i("-", chat_id, NULL);
  2106. xs *url = xs_fmt("https:/" "/api.telegram.org/bot%s/sendMessage", bot);
  2107. xs *body = xs_fmt("{\"chat_id\":%s,\"text\":\"%s\"}", chat_id, msg);
  2108. xs *headers = xs_dict_new();
  2109. headers = xs_dict_append(headers, "content-type", "application/json");
  2110. xs *rsp = xs_http_request("POST", url, headers,
  2111. body, strlen(body), &status, NULL, NULL, 0);
  2112. rsp = xs_free(rsp);
  2113. srv_debug(0, xs_fmt("telegram post %d", status));
  2114. }
  2115. else
  2116. if (strcmp(type, "ntfy") == 0) {
  2117. /* send this via ntfy */
  2118. const char *ntfy_server = xs_dict_get(q_item, "ntfy_server");
  2119. const char *msg = xs_dict_get(q_item, "message");
  2120. const char *ntfy_token = xs_dict_get(q_item, "ntfy_token");
  2121. int status = 0;
  2122. xs *url = xs_fmt("%s", ntfy_server);
  2123. xs *body = xs_fmt("%s", msg);
  2124. xs *headers = xs_dict_new();
  2125. headers = xs_dict_append(headers, "content-type", "text/plain");
  2126. // Append the Authorization header only if ntfy_token is not NULL
  2127. if (ntfy_token != NULL) {
  2128. headers = xs_dict_append(headers, "Authorization", xs_fmt("Bearer %s", ntfy_token));
  2129. }
  2130. xs *rsp = xs_http_request("POST", url, headers,
  2131. body, strlen(body), &status, NULL, NULL, 0);
  2132. rsp = xs_free(rsp);
  2133. srv_debug(0, xs_fmt("ntfy post %d", status));
  2134. }
  2135. else
  2136. if (strcmp(type, "purge") == 0) {
  2137. srv_log(xs_dup("purge start"));
  2138. purge_all();
  2139. srv_log(xs_dup("purge end"));
  2140. }
  2141. else
  2142. if (strcmp(type, "input") == 0) {
  2143. const xs_dict *msg = xs_dict_get(q_item, "message");
  2144. const xs_dict *req = xs_dict_get(q_item, "req");
  2145. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  2146. /* do some instance-level checks */
  2147. int r = process_input_message(NULL, msg, req);
  2148. if (r == 0) {
  2149. /* transient error? retry */
  2150. if (retries > queue_retry_max)
  2151. srv_log(xs_fmt("shared input giving up"));
  2152. else {
  2153. /* reenqueue */
  2154. enqueue_shared_input(msg, req, retries + 1);
  2155. srv_log(xs_fmt("shared input requeue #%d", retries + 1));
  2156. }
  2157. }
  2158. else
  2159. if (r == 2) {
  2160. /* redistribute the input message to all users */
  2161. const char *ntid = xs_dict_get(q_item, "ntid");
  2162. xs *tmpfn = xs_fmt("%s/tmp/%s.json", srv_basedir, ntid);
  2163. FILE *f;
  2164. if ((f = fopen(tmpfn, "w")) != NULL) {
  2165. xs_json_dump(q_item, 4, f);
  2166. fclose(f);
  2167. }
  2168. xs *users = user_list();
  2169. xs_list *p = users;
  2170. const char *v;
  2171. int cnt = 0;
  2172. while (xs_list_iter(&p, &v)) {
  2173. snac user;
  2174. if (user_open(&user, v)) {
  2175. if (is_msg_for_me(&user, msg)) {
  2176. xs *fn = xs_fmt("%s/queue/%s.json", user.basedir, ntid);
  2177. snac_debug(&user, 1,
  2178. xs_fmt("enqueue_input (from shared inbox) %s", xs_dict_get(msg, "id")));
  2179. if (link(tmpfn, fn) < 0)
  2180. srv_log(xs_fmt("link(%s, %s) error", tmpfn, fn));
  2181. cnt++;
  2182. }
  2183. user_free(&user);
  2184. }
  2185. }
  2186. unlink(tmpfn);
  2187. if (cnt == 0) {
  2188. srv_debug(1, xs_fmt("no valid recipients for %s", xs_dict_get(msg, "id")));
  2189. }
  2190. }
  2191. }
  2192. else
  2193. srv_log(xs_fmt("unexpected q_item type '%s'", type));
  2194. }
  2195. int process_queue(void)
  2196. /* processes the global queue */
  2197. {
  2198. int cnt = 0;
  2199. xs *list = queue();
  2200. xs_list *p = list;
  2201. const xs_str *fn;
  2202. while (xs_list_iter(&p, &fn)) {
  2203. xs *q_item = dequeue(fn);
  2204. if (q_item != NULL) {
  2205. job_post(q_item, 0);
  2206. cnt++;
  2207. }
  2208. }
  2209. return cnt;
  2210. }
  2211. /** account migration **/
  2212. int migrate_account(snac *user)
  2213. /* migrates this account to a new one (stored in the 'alias' user field) */
  2214. {
  2215. const char *new_account = xs_dict_get(user->config, "alias");
  2216. if (xs_type(new_account) != XSTYPE_STRING) {
  2217. snac_log(user, xs_fmt("Cannot migrate: alias (destination account) not set"));
  2218. return 1;
  2219. }
  2220. xs *new_actor = NULL;
  2221. int status;
  2222. if (!valid_status(status = activitypub_request(user, new_account, &new_actor))) {
  2223. snac_log(user, xs_fmt("Cannot migrate: error requesting actor %s %d", new_account, status));
  2224. return 1;
  2225. }
  2226. actor_add(new_account, new_actor);
  2227. const char *loaka = xs_dict_get(new_actor, "alsoKnownAs");
  2228. if (xs_type(loaka) != XSTYPE_LIST) {
  2229. snac_log(user, xs_fmt("Cannot migrate: destination account doesn't have any aliases"));
  2230. return 1;
  2231. }
  2232. if (xs_list_in(loaka, user->actor) == -1) {
  2233. snac_log(user, xs_fmt("Cannot migrate: destination account doesn't have this one as an alias"));
  2234. return 1;
  2235. }
  2236. xs *move = msg_move(user, new_account);
  2237. xs *fwers = follower_list(user);
  2238. const char *actor;
  2239. xs_list_foreach(fwers, actor) {
  2240. /* get the actor inbox, excluding the shared one */
  2241. xs *inbox = get_actor_inbox(actor, 0);
  2242. if (xs_is_null(inbox))
  2243. snac_log(user, xs_fmt("migrate_account: cannot get inbox for actor %s", actor));
  2244. else
  2245. enqueue_output(user, move, inbox, 0, 0);
  2246. }
  2247. return 0;
  2248. }
  2249. /** HTTP handlers */
  2250. int activitypub_get_handler(const xs_dict *req, const char *q_path,
  2251. char **body, int *b_size, char **ctype)
  2252. {
  2253. int status = HTTP_STATUS_OK;
  2254. const char *accept = xs_dict_get(req, "accept");
  2255. snac snac;
  2256. xs *msg = NULL;
  2257. if (accept == NULL)
  2258. return 0;
  2259. if (xs_str_in(accept, "application/activity+json") == -1 &&
  2260. xs_str_in(accept, "application/ld+json") == -1)
  2261. return 0;
  2262. xs *l = xs_split_n(q_path, "/", 2);
  2263. const char *uid;
  2264. const char *p_path;
  2265. uid = xs_list_get(l, 1);
  2266. if (!user_open(&snac, uid)) {
  2267. /* invalid user */
  2268. srv_debug(1, xs_fmt("activitypub_get_handler bad user %s", uid));
  2269. return HTTP_STATUS_NOT_FOUND;
  2270. }
  2271. p_path = xs_list_get(l, 2);
  2272. *ctype = "application/activity+json";
  2273. int show_contact_metrics = xs_is_true(xs_dict_get(snac.config, "show_contact_metrics"));
  2274. if (p_path == NULL) {
  2275. /* if there was no component after the user, it's an actor request */
  2276. msg = msg_actor(&snac);
  2277. *ctype = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"";
  2278. const char *ua = xs_dict_get(req, "user-agent");
  2279. snac_debug(&snac, 0, xs_fmt("serving actor [%s]", ua ? ua : "No UA"));
  2280. }
  2281. else
  2282. if (strcmp(p_path, "outbox") == 0 || strcmp(p_path, "featured") == 0) {
  2283. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  2284. xs *list = xs_list_new();
  2285. const char *v;
  2286. int tc = 0;
  2287. /* get the public outbox or the pinned list */
  2288. xs *elems = *p_path == 'o' ? timeline_simple_list(&snac, "public", 0, 20) : pinned_list(&snac);
  2289. while (xs_list_next(elems, &v, &tc)) {
  2290. xs *i = NULL;
  2291. if (valid_status(object_get_by_md5(v, &i))) {
  2292. const char *type = xs_dict_get(i, "type");
  2293. const char *id = xs_dict_get(i, "id");
  2294. if (type && id && strcmp(type, "Note") == 0 && xs_startswith(id, snac.actor)) {
  2295. xs *c_msg = msg_create(&snac, i);
  2296. list = xs_list_append(list, c_msg);
  2297. }
  2298. }
  2299. }
  2300. /* replace the 'orderedItems' with the latest posts */
  2301. msg = msg_collection(&snac, id, xs_list_len(list));
  2302. msg = xs_dict_set(msg, "orderedItems", list);
  2303. }
  2304. else
  2305. if (strcmp(p_path, "followers") == 0) {
  2306. int total = 0;
  2307. if (show_contact_metrics) {
  2308. xs *l = follower_list(&snac);
  2309. total = xs_list_len(l);
  2310. }
  2311. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  2312. msg = msg_collection(&snac, id, total);
  2313. }
  2314. else
  2315. if (strcmp(p_path, "following") == 0) {
  2316. int total = 0;
  2317. if (show_contact_metrics) {
  2318. xs *l = following_list(&snac);
  2319. total = xs_list_len(l);
  2320. }
  2321. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  2322. msg = msg_collection(&snac, id, total);
  2323. }
  2324. else
  2325. if (xs_startswith(p_path, "p/")) {
  2326. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  2327. status = object_get(id, &msg);
  2328. /* don't return non-public objects */
  2329. if (valid_status(status) && !is_msg_public(msg))
  2330. status = HTTP_STATUS_NOT_FOUND;
  2331. }
  2332. else
  2333. status = HTTP_STATUS_NOT_FOUND;
  2334. if (status == HTTP_STATUS_OK && msg != NULL) {
  2335. *body = xs_json_dumps(msg, 4);
  2336. *b_size = strlen(*body);
  2337. }
  2338. snac_debug(&snac, 1, xs_fmt("activitypub_get_handler serving %s %d", q_path, status));
  2339. user_free(&snac);
  2340. return status;
  2341. }
  2342. int activitypub_post_handler(const xs_dict *req, const char *q_path,
  2343. char *payload, int p_size,
  2344. char **body, int *b_size, char **ctype)
  2345. /* processes an input message */
  2346. {
  2347. (void)b_size;
  2348. int status = HTTP_STATUS_ACCEPTED;
  2349. const char *i_ctype = xs_dict_get(req, "content-type");
  2350. snac snac;
  2351. const char *v;
  2352. if (i_ctype == NULL) {
  2353. *body = xs_str_new("no content-type");
  2354. *ctype = "text/plain";
  2355. return HTTP_STATUS_BAD_REQUEST;
  2356. }
  2357. if (xs_is_null(payload)) {
  2358. *body = xs_str_new("no payload");
  2359. *ctype = "text/plain";
  2360. return HTTP_STATUS_BAD_REQUEST;
  2361. }
  2362. if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
  2363. xs_str_in(i_ctype, "application/ld+json") == -1)
  2364. return 0;
  2365. /* decode the message */
  2366. xs *msg = xs_json_loads(payload);
  2367. const char *id = xs_dict_get(msg, "id");
  2368. if (msg == NULL) {
  2369. srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
  2370. srv_archive_error("activitypub_post_handler", "JSON error", req, payload);
  2371. *body = xs_str_new("JSON error");
  2372. *ctype = "text/plain";
  2373. return HTTP_STATUS_BAD_REQUEST;
  2374. }
  2375. if (id && is_instance_blocked(id)) {
  2376. srv_debug(1, xs_fmt("full instance block for %s", id));
  2377. *body = xs_str_new("blocked");
  2378. *ctype = "text/plain";
  2379. return HTTP_STATUS_FORBIDDEN;
  2380. }
  2381. /* get the user and path */
  2382. xs *l = xs_split_n(q_path, "/", 2);
  2383. if (xs_list_len(l) == 2 && strcmp(xs_list_get(l, 1), "shared-inbox") == 0) {
  2384. enqueue_shared_input(msg, req, 0);
  2385. return HTTP_STATUS_ACCEPTED;
  2386. }
  2387. if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
  2388. /* strange q_path */
  2389. srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
  2390. return HTTP_STATUS_NOT_FOUND;
  2391. }
  2392. const char *uid = xs_list_get(l, 1);
  2393. if (!user_open(&snac, uid)) {
  2394. /* invalid user */
  2395. srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid));
  2396. return HTTP_STATUS_NOT_FOUND;
  2397. }
  2398. /* if it has a digest, check it now, because
  2399. later the payload won't be exactly the same */
  2400. if ((v = xs_dict_get(req, "digest")) != NULL) {
  2401. xs *s1 = xs_sha256_base64(payload, p_size);
  2402. xs *s2 = xs_fmt("SHA-256=%s", s1);
  2403. if (strcmp(s2, v) != 0) {
  2404. srv_log(xs_fmt("digest check FAILED"));
  2405. *body = xs_str_new("bad digest");
  2406. *ctype = "text/plain";
  2407. status = HTTP_STATUS_BAD_REQUEST;
  2408. }
  2409. }
  2410. /* if the message is from a muted actor, reject it right now */
  2411. if (!xs_is_null(v = xs_dict_get(msg, "actor")) && *v) {
  2412. if (is_muted(&snac, v)) {
  2413. snac_log(&snac, xs_fmt("rejected message from MUTEd actor %s", v));
  2414. *body = xs_str_new("rejected");
  2415. *ctype = "text/plain";
  2416. status = HTTP_STATUS_FORBIDDEN;
  2417. }
  2418. }
  2419. if (valid_status(status)) {
  2420. enqueue_input(&snac, msg, req, 0);
  2421. *ctype = "application/activity+json";
  2422. }
  2423. user_free(&snac);
  2424. return status;
  2425. }