activitypub.c 93 KB

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