activitypub.c 99 KB

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