activitypub.c 98 KB

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