activitypub.c 118 KB

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