activitypub.c 101 KB

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