activitypub.c 96 KB

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