activitypub.c 118 KB

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