mastoapi.c 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
  3. #ifndef NO_MASTODON_API
  4. #include "xs.h"
  5. #include "xs_hex.h"
  6. #include "xs_openssl.h"
  7. #include "xs_json.h"
  8. #include "xs_io.h"
  9. #include "xs_time.h"
  10. #include "xs_glob.h"
  11. #include "xs_set.h"
  12. #include "xs_random.h"
  13. #include "xs_url.h"
  14. #include "xs_mime.h"
  15. #include "xs_match.h"
  16. #include "snac.h"
  17. static xs_str *random_str(void)
  18. /* just what is says in the tin */
  19. {
  20. unsigned int data[4] = {0};
  21. xs_rnd_buf(data, sizeof(data));
  22. return xs_hex_enc((char *)data, sizeof(data));
  23. }
  24. int app_add(const char *id, const xs_dict *app)
  25. /* stores an app */
  26. {
  27. if (!xs_is_hex(id))
  28. return 500;
  29. int status = 201;
  30. xs *fn = xs_fmt("%s/app/", srv_basedir);
  31. FILE *f;
  32. mkdirx(fn);
  33. fn = xs_str_cat(fn, id);
  34. fn = xs_str_cat(fn, ".json");
  35. if ((f = fopen(fn, "w")) != NULL) {
  36. xs_json_dump(app, 4, f);
  37. fclose(f);
  38. }
  39. else
  40. status = 500;
  41. return status;
  42. }
  43. xs_str *_app_fn(const char *id)
  44. {
  45. return xs_fmt("%s/app/%s.json", srv_basedir, id);
  46. }
  47. xs_dict *app_get(const char *id)
  48. /* gets an app */
  49. {
  50. if (!xs_is_hex(id))
  51. return NULL;
  52. xs *fn = _app_fn(id);
  53. xs_dict *app = NULL;
  54. FILE *f;
  55. if ((f = fopen(fn, "r")) != NULL) {
  56. app = xs_json_load(f);
  57. fclose(f);
  58. }
  59. return app;
  60. }
  61. int app_del(const char *id)
  62. /* deletes an app */
  63. {
  64. if (!xs_is_hex(id))
  65. return -1;
  66. xs *fn = _app_fn(id);
  67. return unlink(fn);
  68. }
  69. int token_add(const char *id, const xs_dict *token)
  70. /* stores a token */
  71. {
  72. if (!xs_is_hex(id))
  73. return 500;
  74. int status = 201;
  75. xs *fn = xs_fmt("%s/token/", srv_basedir);
  76. FILE *f;
  77. mkdirx(fn);
  78. fn = xs_str_cat(fn, id);
  79. fn = xs_str_cat(fn, ".json");
  80. if ((f = fopen(fn, "w")) != NULL) {
  81. xs_json_dump(token, 4, f);
  82. fclose(f);
  83. }
  84. else
  85. status = 500;
  86. return status;
  87. }
  88. xs_dict *token_get(const char *id)
  89. /* gets a token */
  90. {
  91. if (!xs_is_hex(id))
  92. return NULL;
  93. xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
  94. xs_dict *token = NULL;
  95. FILE *f;
  96. if ((f = fopen(fn, "r")) != NULL) {
  97. token = xs_json_load(f);
  98. fclose(f);
  99. }
  100. return token;
  101. }
  102. int token_del(const char *id)
  103. /* deletes a token */
  104. {
  105. if (!xs_is_hex(id))
  106. return -1;
  107. xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
  108. return unlink(fn);
  109. }
  110. const char *login_page = ""
  111. "<!DOCTYPE html>\n"
  112. "<html>\n"
  113. "<head>\n"
  114. "<title>%s OAuth - Snac2</title>\n"
  115. "<style>:root {color-scheme: light dark}</style>\n"
  116. "</head>\n"
  117. "<body><h1>%s OAuth identify</h1>\n"
  118. "<div style=\"background-color: red; color: white\">%s</div>\n"
  119. "<form method=\"post\" action=\"https:/" "/%s/%s\">\n"
  120. "<p>Login: <input type=\"text\" name=\"login\"></p>\n"
  121. "<p>Password: <input type=\"password\" name=\"passwd\"></p>\n"
  122. "<input type=\"hidden\" name=\"redir\" value=\"%s\">\n"
  123. "<input type=\"hidden\" name=\"cid\" value=\"%s\">\n"
  124. "<input type=\"hidden\" name=\"state\" value=\"%s\">\n"
  125. "<input type=\"submit\" value=\"OK\">\n"
  126. "</form><p>%s</p></body></html>\n"
  127. "";
  128. int oauth_get_handler(const xs_dict *req, const char *q_path,
  129. char **body, int *b_size, char **ctype)
  130. {
  131. (void)b_size;
  132. if (!xs_startswith(q_path, "/oauth/"))
  133. return 0;
  134. int status = 404;
  135. xs_dict *msg = xs_dict_get(req, "q_vars");
  136. xs *cmd = xs_replace_n(q_path, "/oauth", "", 1);
  137. srv_debug(1, xs_fmt("oauth_get_handler %s", q_path));
  138. if (strcmp(cmd, "/authorize") == 0) { /** **/
  139. const char *cid = xs_dict_get(msg, "client_id");
  140. const char *ruri = xs_dict_get(msg, "redirect_uri");
  141. const char *rtype = xs_dict_get(msg, "response_type");
  142. const char *state = xs_dict_get(msg, "state");
  143. status = 400;
  144. if (cid && ruri && rtype && strcmp(rtype, "code") == 0) {
  145. xs *app = app_get(cid);
  146. if (app != NULL) {
  147. const char *host = xs_dict_get(srv_config, "host");
  148. if (xs_is_null(state))
  149. state = "";
  150. *body = xs_fmt(login_page, host, host, "", host, "oauth/x-snac-login",
  151. ruri, cid, state, USER_AGENT);
  152. *ctype = "text/html";
  153. status = 200;
  154. srv_debug(1, xs_fmt("oauth authorize: generating login page"));
  155. }
  156. else
  157. srv_debug(1, xs_fmt("oauth authorize: bad client_id %s", cid));
  158. }
  159. else
  160. srv_debug(1, xs_fmt("oauth authorize: invalid or unset arguments"));
  161. }
  162. else
  163. if (strcmp(cmd, "/x-snac-get-token") == 0) { /** **/
  164. const char *host = xs_dict_get(srv_config, "host");
  165. *body = xs_fmt(login_page, host, host, "", host, "oauth/x-snac-get-token",
  166. "", "", "", USER_AGENT);
  167. *ctype = "text/html";
  168. status = 200;
  169. }
  170. return status;
  171. }
  172. int oauth_post_handler(const xs_dict *req, const char *q_path,
  173. const char *payload, int p_size,
  174. char **body, int *b_size, char **ctype)
  175. {
  176. (void)p_size;
  177. (void)b_size;
  178. if (!xs_startswith(q_path, "/oauth/"))
  179. return 0;
  180. int status = 404;
  181. char *i_ctype = xs_dict_get(req, "content-type");
  182. xs *args = NULL;
  183. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  184. args = xs_json_loads(payload);
  185. else
  186. if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded") && payload) {
  187. xs *upl = xs_url_dec(payload);
  188. args = xs_url_vars(upl);
  189. }
  190. else
  191. args = xs_dup(xs_dict_get(req, "p_vars"));
  192. xs *cmd = xs_replace_n(q_path, "/oauth", "", 1);
  193. srv_debug(1, xs_fmt("oauth_post_handler %s", q_path));
  194. if (strcmp(cmd, "/x-snac-login") == 0) { /** **/
  195. const char *login = xs_dict_get(args, "login");
  196. const char *passwd = xs_dict_get(args, "passwd");
  197. const char *redir = xs_dict_get(args, "redir");
  198. const char *cid = xs_dict_get(args, "cid");
  199. const char *state = xs_dict_get(args, "state");
  200. const char *host = xs_dict_get(srv_config, "host");
  201. /* by default, generate another login form with an error */
  202. *body = xs_fmt(login_page, host, host, "LOGIN INCORRECT", host, "oauth/x-snac-login",
  203. redir, cid, state, USER_AGENT);
  204. *ctype = "text/html";
  205. status = 200;
  206. if (login && passwd && redir && cid) {
  207. snac snac;
  208. if (user_open(&snac, login)) {
  209. /* check the login + password */
  210. if (check_password(login, passwd, xs_dict_get(snac.config, "passwd"))) {
  211. /* success! redirect to the desired uri */
  212. xs *code = random_str();
  213. xs_free(*body);
  214. if (strcmp(redir, "urn:ietf:wg:oauth:2.0:oob") == 0) {
  215. *body = xs_dup(code);
  216. }
  217. else {
  218. *body = xs_fmt("%s?code=%s", redir, code);
  219. status = 303;
  220. }
  221. /* if there is a state, add it */
  222. if (!xs_is_null(state) && *state) {
  223. *body = xs_str_cat(*body, "&state=");
  224. *body = xs_str_cat(*body, state);
  225. }
  226. srv_log(xs_fmt("oauth x-snac-login: '%s' success, redirect to %s",
  227. login, *body));
  228. /* assign the login to the app */
  229. xs *app = app_get(cid);
  230. if (app != NULL) {
  231. app = xs_dict_set(app, "uid", login);
  232. app = xs_dict_set(app, "code", code);
  233. app_add(cid, app);
  234. }
  235. else
  236. srv_log(xs_fmt("oauth x-snac-login: error getting app %s", cid));
  237. }
  238. else
  239. srv_debug(1, xs_fmt("oauth x-snac-login: login '%s' incorrect", login));
  240. user_free(&snac);
  241. }
  242. else
  243. srv_debug(1, xs_fmt("oauth x-snac-login: bad user '%s'", login));
  244. }
  245. else
  246. srv_debug(1, xs_fmt("oauth x-snac-login: invalid or unset arguments"));
  247. }
  248. else
  249. if (strcmp(cmd, "/token") == 0) { /** **/
  250. xs *wrk = NULL;
  251. const char *gtype = xs_dict_get(args, "grant_type");
  252. const char *code = xs_dict_get(args, "code");
  253. const char *cid = xs_dict_get(args, "client_id");
  254. const char *csec = xs_dict_get(args, "client_secret");
  255. const char *ruri = xs_dict_get(args, "redirect_uri");
  256. /* FIXME: this 'scope' parameter is mandatory for the official Mastodon API,
  257. but if it's enabled, it makes it crash after some more steps, which
  258. is FAR WORSE */
  259. // const char *scope = xs_dict_get(args, "scope");
  260. const char *scope = NULL;
  261. /* no client_secret? check if it's inside an authorization header
  262. (AndStatus does it this way) */
  263. if (xs_is_null(csec)) {
  264. const char *auhdr = xs_dict_get(req, "authorization");
  265. if (!xs_is_null(auhdr) && xs_startswith(auhdr, "Basic ")) {
  266. xs *s1 = xs_replace_n(auhdr, "Basic ", "", 1);
  267. int size;
  268. xs *s2 = xs_base64_dec(s1, &size);
  269. if (!xs_is_null(s2)) {
  270. xs *l1 = xs_split(s2, ":");
  271. if (xs_list_len(l1) == 2) {
  272. wrk = xs_dup(xs_list_get(l1, 1));
  273. csec = wrk;
  274. }
  275. }
  276. }
  277. }
  278. if (gtype && code && cid && csec && ruri) {
  279. xs *app = app_get(cid);
  280. if (app == NULL) {
  281. status = 401;
  282. srv_log(xs_fmt("oauth token: invalid app %s", cid));
  283. }
  284. else
  285. if (strcmp(csec, xs_dict_get(app, "client_secret")) != 0) {
  286. status = 401;
  287. srv_log(xs_fmt("oauth token: invalid client_secret for app %s", cid));
  288. }
  289. else {
  290. xs *rsp = xs_dict_new();
  291. xs *cat = xs_number_new(time(NULL));
  292. xs *tokid = random_str();
  293. rsp = xs_dict_append(rsp, "access_token", tokid);
  294. rsp = xs_dict_append(rsp, "token_type", "Bearer");
  295. rsp = xs_dict_append(rsp, "created_at", cat);
  296. if (!xs_is_null(scope))
  297. rsp = xs_dict_append(rsp, "scope", scope);
  298. *body = xs_json_dumps(rsp, 4);
  299. *ctype = "application/json";
  300. status = 200;
  301. const char *uid = xs_dict_get(app, "uid");
  302. srv_debug(1, xs_fmt("oauth token: "
  303. "successful login for %s, new token %s", uid, tokid));
  304. xs *token = xs_dict_new();
  305. token = xs_dict_append(token, "token", tokid);
  306. token = xs_dict_append(token, "client_id", cid);
  307. token = xs_dict_append(token, "client_secret", csec);
  308. token = xs_dict_append(token, "uid", uid);
  309. token = xs_dict_append(token, "code", code);
  310. token_add(tokid, token);
  311. }
  312. }
  313. else {
  314. srv_debug(1, xs_fmt("oauth token: invalid or unset arguments"));
  315. status = 400;
  316. }
  317. }
  318. else
  319. if (strcmp(cmd, "/revoke") == 0) { /** **/
  320. const char *cid = xs_dict_get(args, "client_id");
  321. const char *csec = xs_dict_get(args, "client_secret");
  322. const char *tokid = xs_dict_get(args, "token");
  323. if (cid && csec && tokid) {
  324. xs *token = token_get(tokid);
  325. *body = xs_str_new("{}");
  326. *ctype = "application/json";
  327. if (token == NULL || strcmp(csec, xs_dict_get(token, "client_secret")) != 0) {
  328. srv_debug(1, xs_fmt("oauth revoke: bad secret for token %s", tokid));
  329. status = 403;
  330. }
  331. else {
  332. token_del(tokid);
  333. srv_debug(1, xs_fmt("oauth revoke: revoked token %s", tokid));
  334. status = 200;
  335. /* also delete the app, as it serves no purpose from now on */
  336. app_del(cid);
  337. }
  338. }
  339. else {
  340. srv_debug(1, xs_fmt("oauth revoke: invalid or unset arguments"));
  341. status = 403;
  342. }
  343. }
  344. if (strcmp(cmd, "/x-snac-get-token") == 0) { /** **/
  345. const char *login = xs_dict_get(args, "login");
  346. const char *passwd = xs_dict_get(args, "passwd");
  347. const char *host = xs_dict_get(srv_config, "host");
  348. /* by default, generate another login form with an error */
  349. *body = xs_fmt(login_page, host, host, "LOGIN INCORRECT", host, "oauth/x-snac-get-token",
  350. "", "", "", USER_AGENT);
  351. *ctype = "text/html";
  352. status = 200;
  353. if (login && passwd) {
  354. snac user;
  355. if (user_open(&user, login)) {
  356. /* check the login + password */
  357. if (check_password(login, passwd, xs_dict_get(user.config, "passwd"))) {
  358. /* success! create a new token */
  359. xs *tokid = random_str();
  360. srv_debug(1, xs_fmt("x-snac-new-token: "
  361. "successful login for %s, new token %s", login, tokid));
  362. xs *token = xs_dict_new();
  363. token = xs_dict_append(token, "token", tokid);
  364. token = xs_dict_append(token, "client_id", "snac-client");
  365. token = xs_dict_append(token, "client_secret", "");
  366. token = xs_dict_append(token, "uid", login);
  367. token = xs_dict_append(token, "code", "");
  368. token_add(tokid, token);
  369. *ctype = "text/plain";
  370. xs_free(*body);
  371. *body = xs_dup(tokid);
  372. }
  373. user_free(&user);
  374. }
  375. }
  376. }
  377. return status;
  378. }
  379. xs_str *mastoapi_id(const xs_dict *msg)
  380. /* returns a somewhat Mastodon-compatible status id */
  381. {
  382. const char *id = xs_dict_get(msg, "id");
  383. xs *md5 = xs_md5_hex(id, strlen(id));
  384. return xs_fmt("%10.0f%s", object_ctime_by_md5(md5), md5);
  385. }
  386. #define MID_TO_MD5(id) (id + 10)
  387. xs_dict *mastoapi_account(const xs_dict *actor)
  388. /* converts an ActivityPub actor to a Mastodon account */
  389. {
  390. const char *id = xs_dict_get(actor, "id");
  391. const char *pub = xs_dict_get(actor, "published");
  392. if (xs_type(id) != XSTYPE_STRING)
  393. return NULL;
  394. const char *prefu = xs_dict_get(actor, "preferredUsername");
  395. const char *display_name = xs_dict_get(actor, "name");
  396. if (xs_is_null(display_name) || *display_name == '\0')
  397. display_name = prefu;
  398. xs_dict *acct = xs_dict_new();
  399. xs *acct_md5 = xs_md5_hex(id, strlen(id));
  400. acct = xs_dict_append(acct, "id", acct_md5);
  401. acct = xs_dict_append(acct, "username", prefu);
  402. acct = xs_dict_append(acct, "display_name", display_name);
  403. {
  404. /* create the acct field as user@host */
  405. xs *l = xs_split(id, "/");
  406. xs *fquid = xs_fmt("%s@%s", prefu, xs_list_get(l, 2));
  407. acct = xs_dict_append(acct, "acct", fquid);
  408. }
  409. if (pub)
  410. acct = xs_dict_append(acct, "created_at", pub);
  411. else {
  412. /* unset created_at crashes Tusky, so lie like a mf */
  413. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  414. acct = xs_dict_append(acct, "created_at", date);
  415. }
  416. const char *note = xs_dict_get(actor, "summary");
  417. if (xs_is_null(note))
  418. note = "";
  419. if (strcmp(xs_dict_get(actor, "type"), "Service") == 0)
  420. acct = xs_dict_append(acct, "bot", xs_stock_true);
  421. else
  422. acct = xs_dict_append(acct, "bot", xs_stock_false);
  423. acct = xs_dict_append(acct, "note", note);
  424. acct = xs_dict_append(acct, "url", id);
  425. xs *avatar = NULL;
  426. xs_dict *av = xs_dict_get(actor, "icon");
  427. if (xs_type(av) == XSTYPE_DICT) {
  428. char *url = xs_dict_get(av, "url");
  429. if (url != NULL)
  430. avatar = xs_dup(url);
  431. }
  432. if (avatar == NULL)
  433. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  434. acct = xs_dict_append(acct, "avatar", avatar);
  435. acct = xs_dict_append(acct, "avatar_static", avatar);
  436. xs *header = NULL;
  437. xs_dict *hd = xs_dict_get(actor, "image");
  438. if (xs_type(hd) == XSTYPE_DICT)
  439. header = xs_dup(xs_dict_get(hd, "url"));
  440. if (xs_is_null(header))
  441. header = xs_dup("");
  442. acct = xs_dict_append(acct, "header", header);
  443. acct = xs_dict_append(acct, "header_static", header);
  444. /* emojis */
  445. xs_list *p;
  446. if (!xs_is_null(p = xs_dict_get(actor, "tag"))) {
  447. xs *eml = xs_list_new();
  448. xs_dict *v;
  449. while (xs_list_iter(&p, &v)) {
  450. const char *type = xs_dict_get(v, "type");
  451. if (!xs_is_null(type) && strcmp(type, "Emoji") == 0) {
  452. const char *name = xs_dict_get(v, "name");
  453. const xs_dict *icon = xs_dict_get(v, "icon");
  454. if (!xs_is_null(name) && !xs_is_null(icon)) {
  455. const char *url = xs_dict_get(icon, "url");
  456. if (!xs_is_null(url)) {
  457. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  458. xs *d1 = xs_dict_new();
  459. d1 = xs_dict_append(d1, "shortcode", nm);
  460. d1 = xs_dict_append(d1, "url", url);
  461. d1 = xs_dict_append(d1, "static_url", url);
  462. d1 = xs_dict_append(d1, "visible_in_picker", xs_stock_true);
  463. eml = xs_list_append(eml, d1);
  464. }
  465. }
  466. }
  467. }
  468. acct = xs_dict_append(acct, "emojis", eml);
  469. }
  470. acct = xs_dict_append(acct, "locked", xs_stock_false);
  471. acct = xs_dict_append(acct, "followers_count", xs_stock_0);
  472. acct = xs_dict_append(acct, "following_count", xs_stock_0);
  473. acct = xs_dict_append(acct, "statuses_count", xs_stock_0);
  474. xs *fields = xs_list_new();
  475. p = xs_dict_get(actor, "attachment");
  476. xs_dict *v;
  477. while (xs_list_iter(&p, &v)) {
  478. char *type = xs_dict_get(v, "type");
  479. char *name = xs_dict_get(v, "name");
  480. char *value = xs_dict_get(v, "value");
  481. if (!xs_is_null(type) && !xs_is_null(name) &&
  482. !xs_is_null(value) && strcmp(type, "PropertyValue") == 0) {
  483. xs *d = xs_dict_new();
  484. d = xs_dict_append(d, "name", name);
  485. d = xs_dict_append(d, "value", value);
  486. d = xs_dict_append(d, "verified_at", xs_stock_null);
  487. fields = xs_list_append(fields, d);
  488. }
  489. }
  490. acct = xs_dict_append(acct, "fields", fields);
  491. return acct;
  492. }
  493. xs_str *mastoapi_date(char *date)
  494. /* converts an ISO 8601 date to whatever format Mastodon uses */
  495. {
  496. xs_str *s = xs_crop_i(xs_dup(date), 0, 19);
  497. s = xs_str_cat(s, ".000Z");
  498. return s;
  499. }
  500. xs_dict *mastoapi_poll(snac *snac, const xs_dict *msg)
  501. /* creates a mastoapi Poll object */
  502. {
  503. xs_dict *poll = xs_dict_new();
  504. xs *mid = mastoapi_id(msg);
  505. xs_list *opts = NULL;
  506. xs_val *v;
  507. int num_votes = 0;
  508. xs *options = xs_list_new();
  509. poll = xs_dict_append(poll, "id", mid);
  510. xs *fd = mastoapi_date(xs_dict_get(msg, "endTime"));
  511. poll = xs_dict_append(poll, "expires_at", fd);
  512. poll = xs_dict_append(poll, "expired",
  513. xs_dict_get(msg, "closed") != NULL ? xs_stock_true : xs_stock_false);
  514. if ((opts = xs_dict_get(msg, "oneOf")) != NULL)
  515. poll = xs_dict_append(poll, "multiple", xs_stock_false);
  516. else {
  517. opts = xs_dict_get(msg, "anyOf");
  518. poll = xs_dict_append(poll, "multiple", xs_stock_true);
  519. }
  520. while (xs_list_iter(&opts, &v)) {
  521. const char *title = xs_dict_get(v, "name");
  522. const char *replies = xs_dict_get(v, "replies");
  523. if (title && replies) {
  524. const char *votes_count = xs_dict_get(replies, "totalItems");
  525. if (xs_type(votes_count) == XSTYPE_NUMBER) {
  526. xs *d = xs_dict_new();
  527. d = xs_dict_append(d, "title", title);
  528. d = xs_dict_append(d, "votes_count", votes_count);
  529. options = xs_list_append(options, d);
  530. num_votes += xs_number_get(votes_count);
  531. }
  532. }
  533. }
  534. poll = xs_dict_append(poll, "options", options);
  535. xs *vc = xs_number_new(num_votes);
  536. poll = xs_dict_append(poll, "votes_count", vc);
  537. poll = xs_dict_append(poll, "voted",
  538. (snac && was_question_voted(snac, xs_dict_get(msg, "id"))) ?
  539. xs_stock_true : xs_stock_false);
  540. return poll;
  541. }
  542. xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
  543. /* converts an ActivityPub note to a Mastodon status */
  544. {
  545. xs *actor = NULL;
  546. actor_get(get_atto(msg), &actor);
  547. /* if the author is not here, discard */
  548. if (actor == NULL)
  549. return NULL;
  550. const char *type = xs_dict_get(msg, "type");
  551. const char *id = xs_dict_get(msg, "id");
  552. /* fail if it's not a valid actor */
  553. if (xs_is_null(type) || xs_is_null(id))
  554. return NULL;
  555. xs *acct = mastoapi_account(actor);
  556. if (acct == NULL)
  557. return NULL;
  558. xs *idx = NULL;
  559. xs *ixc = NULL;
  560. char *tmp;
  561. xs *mid = mastoapi_id(msg);
  562. xs_dict *st = xs_dict_new();
  563. st = xs_dict_append(st, "id", mid);
  564. st = xs_dict_append(st, "uri", id);
  565. st = xs_dict_append(st, "url", id);
  566. st = xs_dict_append(st, "account", acct);
  567. xs *fd = mastoapi_date(xs_dict_get(msg, "published"));
  568. st = xs_dict_append(st, "created_at", fd);
  569. {
  570. const char *content = xs_dict_get(msg, "content");
  571. const char *name = xs_dict_get(msg, "name");
  572. xs *s1 = NULL;
  573. if (name && content)
  574. s1 = xs_fmt("%s<br><br>%s", name, content);
  575. else
  576. if (name)
  577. s1 = xs_dup(name);
  578. else
  579. if (content)
  580. s1 = xs_dup(content);
  581. else
  582. s1 = xs_str_new(NULL);
  583. st = xs_dict_append(st, "content", s1);
  584. }
  585. st = xs_dict_append(st, "visibility",
  586. is_msg_public(msg) ? "public" : "private");
  587. tmp = xs_dict_get(msg, "sensitive");
  588. if (xs_is_null(tmp))
  589. tmp = xs_stock_false;
  590. st = xs_dict_append(st, "sensitive", tmp);
  591. tmp = xs_dict_get(msg, "summary");
  592. if (xs_is_null(tmp))
  593. tmp = "";
  594. st = xs_dict_append(st, "spoiler_text", tmp);
  595. /* create the list of attachments */
  596. xs *attach = get_attachments(msg);
  597. {
  598. xs_list *p = attach;
  599. xs_dict *v;
  600. xs *matt = xs_list_new();
  601. while (xs_list_iter(&p, &v)) {
  602. char *type = xs_dict_get(v, "type");
  603. char *href = xs_dict_get(v, "href");
  604. char *name = xs_dict_get(v, "name");
  605. if (xs_match(type, "image/*|video/*|Image|Video")) { /* */
  606. xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
  607. xs *d = xs_dict_new();
  608. d = xs_dict_append(d, "id", matteid);
  609. d = xs_dict_append(d, "url", href);
  610. d = xs_dict_append(d, "preview_url", href);
  611. d = xs_dict_append(d, "remote_url", href);
  612. d = xs_dict_append(d, "description", name);
  613. d = xs_dict_append(d, "type", (*type == 'v' || *type == 'V') ? "video" : "image");
  614. matt = xs_list_append(matt, d);
  615. }
  616. }
  617. st = xs_dict_append(st, "media_attachments", matt);
  618. }
  619. {
  620. xs *ml = xs_list_new();
  621. xs *htl = xs_list_new();
  622. xs *eml = xs_list_new();
  623. xs_list *tag = xs_dict_get(msg, "tag");
  624. int n = 0;
  625. xs *tag_list = NULL;
  626. if (xs_type(tag) == XSTYPE_DICT) {
  627. tag_list = xs_list_new();
  628. tag_list = xs_list_append(tag_list, tag);
  629. }
  630. else
  631. if (xs_type(tag) == XSTYPE_LIST)
  632. tag_list = xs_dup(tag);
  633. else
  634. tag_list = xs_list_new();
  635. tag = tag_list;
  636. xs_dict *v;
  637. while (xs_list_iter(&tag, &v)) {
  638. const char *type = xs_dict_get(v, "type");
  639. if (xs_is_null(type))
  640. continue;
  641. xs *d1 = xs_dict_new();
  642. if (strcmp(type, "Mention") == 0) {
  643. const char *name = xs_dict_get(v, "name");
  644. const char *href = xs_dict_get(v, "href");
  645. if (!xs_is_null(name) && !xs_is_null(href) &&
  646. (snac == NULL || strcmp(href, snac->actor) != 0)) {
  647. xs *nm = xs_strip_chars_i(xs_dup(name), "@");
  648. xs *id = xs_fmt("%d", n++);
  649. d1 = xs_dict_append(d1, "id", id);
  650. d1 = xs_dict_append(d1, "username", nm);
  651. d1 = xs_dict_append(d1, "acct", nm);
  652. d1 = xs_dict_append(d1, "url", href);
  653. ml = xs_list_append(ml, d1);
  654. }
  655. }
  656. else
  657. if (strcmp(type, "Hashtag") == 0) {
  658. const char *name = xs_dict_get(v, "name");
  659. const char *href = xs_dict_get(v, "href");
  660. if (!xs_is_null(name) && !xs_is_null(href)) {
  661. xs *nm = xs_strip_chars_i(xs_dup(name), "#");
  662. d1 = xs_dict_append(d1, "name", nm);
  663. d1 = xs_dict_append(d1, "url", href);
  664. htl = xs_list_append(htl, d1);
  665. }
  666. }
  667. else
  668. if (strcmp(type, "Emoji") == 0) {
  669. const char *name = xs_dict_get(v, "name");
  670. const xs_dict *icon = xs_dict_get(v, "icon");
  671. if (!xs_is_null(name) && !xs_is_null(icon)) {
  672. const char *url = xs_dict_get(icon, "url");
  673. if (!xs_is_null(url)) {
  674. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  675. d1 = xs_dict_append(d1, "shortcode", nm);
  676. d1 = xs_dict_append(d1, "url", url);
  677. d1 = xs_dict_append(d1, "static_url", url);
  678. d1 = xs_dict_append(d1, "visible_in_picker", xs_stock_true);
  679. d1 = xs_dict_append(d1, "category", "Emojis");
  680. eml = xs_list_append(eml, d1);
  681. }
  682. }
  683. }
  684. }
  685. st = xs_dict_append(st, "mentions", ml);
  686. st = xs_dict_append(st, "tags", htl);
  687. st = xs_dict_append(st, "emojis", eml);
  688. }
  689. xs_free(idx);
  690. xs_free(ixc);
  691. idx = object_likes(id);
  692. ixc = xs_number_new(xs_list_len(idx));
  693. st = xs_dict_append(st, "favourites_count", ixc);
  694. st = xs_dict_append(st, "favourited",
  695. (snac && xs_list_in(idx, snac->md5) != -1) ? xs_stock_true : xs_stock_false);
  696. xs_free(idx);
  697. xs_free(ixc);
  698. idx = object_announces(id);
  699. ixc = xs_number_new(xs_list_len(idx));
  700. st = xs_dict_append(st, "reblogs_count", ixc);
  701. st = xs_dict_append(st, "reblogged",
  702. (snac && xs_list_in(idx, snac->md5) != -1) ? xs_stock_true : xs_stock_false);
  703. /* get the last person who boosted this */
  704. xs *boosted_by_md5 = NULL;
  705. if (xs_list_len(idx))
  706. boosted_by_md5 = xs_dup(xs_list_get(idx, -1));
  707. xs_free(idx);
  708. xs_free(ixc);
  709. idx = object_children(id);
  710. ixc = xs_number_new(xs_list_len(idx));
  711. st = xs_dict_append(st, "replies_count", ixc);
  712. /* default in_reply_to values */
  713. st = xs_dict_append(st, "in_reply_to_id", xs_stock_null);
  714. st = xs_dict_append(st, "in_reply_to_account_id", xs_stock_null);
  715. tmp = xs_dict_get(msg, "inReplyTo");
  716. if (!xs_is_null(tmp)) {
  717. xs *irto = NULL;
  718. if (valid_status(object_get(tmp, &irto))) {
  719. xs *irt_mid = mastoapi_id(irto);
  720. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  721. char *at = NULL;
  722. if (!xs_is_null(at = get_atto(irto))) {
  723. xs *at_md5 = xs_md5_hex(at, strlen(at));
  724. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  725. }
  726. }
  727. }
  728. st = xs_dict_append(st, "reblog", xs_stock_null);
  729. st = xs_dict_append(st, "card", xs_stock_null);
  730. st = xs_dict_append(st, "language", xs_stock_null);
  731. tmp = xs_dict_get(msg, "sourceContent");
  732. if (xs_is_null(tmp))
  733. tmp = "";
  734. st = xs_dict_append(st, "text", tmp);
  735. tmp = xs_dict_get(msg, "updated");
  736. xs *fd2 = NULL;
  737. if (xs_is_null(tmp))
  738. tmp = xs_stock_null;
  739. else {
  740. fd2 = mastoapi_date(tmp);
  741. tmp = fd2;
  742. }
  743. st = xs_dict_append(st, "edited_at", tmp);
  744. if (strcmp(type, "Question") == 0) {
  745. xs *poll = mastoapi_poll(snac, msg);
  746. st = xs_dict_append(st, "poll", poll);
  747. }
  748. else
  749. st = xs_dict_append(st, "poll", xs_stock_null);
  750. st = xs_dict_append(st, "bookmarked", xs_stock_false);
  751. st = xs_dict_append(st, "pinned",
  752. (snac && is_pinned(snac, id)) ? xs_stock_true : xs_stock_false);
  753. /* is it a boost? */
  754. if (!xs_is_null(boosted_by_md5)) {
  755. /* create a new dummy status, using st as the 'reblog' field */
  756. xs_dict *bst = xs_dup(st);
  757. xs *b_actor = NULL;
  758. if (valid_status(object_get_by_md5(boosted_by_md5, &b_actor))) {
  759. xs *b_acct = mastoapi_account(b_actor);
  760. xs *fake_uri = NULL;
  761. if (snac)
  762. fake_uri = xs_fmt("%s/d/%s/Announce", snac->actor, mid);
  763. else
  764. fake_uri = xs_fmt("%s#%s", srv_baseurl, mid);
  765. bst = xs_dict_set(bst, "uri", fake_uri);
  766. bst = xs_dict_set(bst, "url", fake_uri);
  767. bst = xs_dict_set(bst, "account", b_acct);
  768. bst = xs_dict_set(bst, "content", "");
  769. bst = xs_dict_set(bst, "reblog", st);
  770. xs_free(st);
  771. st = bst;
  772. }
  773. }
  774. return st;
  775. }
  776. xs_dict *mastoapi_relationship(snac *snac, const char *md5)
  777. {
  778. xs_dict *rel = NULL;
  779. xs *actor_o = NULL;
  780. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  781. rel = xs_dict_new();
  782. const char *actor = xs_dict_get(actor_o, "id");
  783. rel = xs_dict_append(rel, "id", md5);
  784. rel = xs_dict_append(rel, "following",
  785. following_check(snac, actor) ? xs_stock_true : xs_stock_false);
  786. rel = xs_dict_append(rel, "showing_reblogs", xs_stock_true);
  787. rel = xs_dict_append(rel, "notifying", xs_stock_false);
  788. rel = xs_dict_append(rel, "followed_by",
  789. follower_check(snac, actor) ? xs_stock_true : xs_stock_false);
  790. rel = xs_dict_append(rel, "blocking",
  791. is_muted(snac, actor) ? xs_stock_true : xs_stock_false);
  792. rel = xs_dict_append(rel, "muting", xs_stock_false);
  793. rel = xs_dict_append(rel, "muting_notifications", xs_stock_false);
  794. rel = xs_dict_append(rel, "requested", xs_stock_false);
  795. rel = xs_dict_append(rel, "domain_blocking", xs_stock_false);
  796. rel = xs_dict_append(rel, "endorsed", xs_stock_false);
  797. rel = xs_dict_append(rel, "note", "");
  798. }
  799. return rel;
  800. }
  801. int process_auth_token(snac *snac, const xs_dict *req)
  802. /* processes an authorization token, if there is one */
  803. {
  804. int logged_in = 0;
  805. char *v;
  806. /* if there is an authorization field, try to validate it */
  807. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  808. xs *tokid = xs_replace_n(v, "Bearer ", "", 1);
  809. xs *token = token_get(tokid);
  810. if (token != NULL) {
  811. const char *uid = xs_dict_get(token, "uid");
  812. if (!xs_is_null(uid) && user_open(snac, uid)) {
  813. logged_in = 1;
  814. /* this counts as a 'login' */
  815. lastlog_write(snac, "mastoapi");
  816. srv_debug(2, xs_fmt("mastoapi auth: valid token for user '%s'", uid));
  817. }
  818. else
  819. srv_log(xs_fmt("mastoapi auth: corrupted token '%s'", tokid));
  820. }
  821. else
  822. srv_log(xs_fmt("mastoapi auth: invalid token '%s'", tokid));
  823. }
  824. return logged_in;
  825. }
  826. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  827. char **body, int *b_size, char **ctype)
  828. {
  829. (void)b_size;
  830. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  831. return 0;
  832. int status = 404;
  833. xs_dict *args = xs_dict_get(req, "q_vars");
  834. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  835. snac snac1 = {0};
  836. int logged_in = process_auth_token(&snac1, req);
  837. if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) { /** **/
  838. if (logged_in) {
  839. xs *acct = xs_dict_new();
  840. acct = xs_dict_append(acct, "id", snac1.md5);
  841. acct = xs_dict_append(acct, "username", xs_dict_get(snac1.config, "uid"));
  842. acct = xs_dict_append(acct, "acct", xs_dict_get(snac1.config, "uid"));
  843. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac1.config, "name"));
  844. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac1.config, "published"));
  845. acct = xs_dict_append(acct, "last_status_at", xs_dict_get(snac1.config, "published"));
  846. acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio"));
  847. acct = xs_dict_append(acct, "url", snac1.actor);
  848. acct = xs_dict_append(acct, "header", "");
  849. acct = xs_dict_append(acct, "header_static", "");
  850. acct = xs_dict_append(acct, "locked", xs_stock_false);
  851. acct = xs_dict_append(acct, "bot", xs_dict_get(snac1.config, "bot"));
  852. xs *src = xs_json_loads("{\"privacy\":\"public\","
  853. "\"sensitive\":false,\"fields\":[],\"note\":\"\"}");
  854. acct = xs_dict_append(acct, "source", src);
  855. xs *avatar = NULL;
  856. char *av = xs_dict_get(snac1.config, "avatar");
  857. if (xs_is_null(av) || *av == '\0')
  858. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  859. else
  860. avatar = xs_dup(av);
  861. acct = xs_dict_append(acct, "avatar", avatar);
  862. acct = xs_dict_append(acct, "avatar_static", avatar);
  863. xs_dict *metadata = xs_dict_get(snac1.config, "metadata");
  864. if (xs_type(metadata) == XSTYPE_DICT) {
  865. xs *fields = xs_list_new();
  866. xs_str *k;
  867. xs_str *v;
  868. while (xs_dict_iter(&metadata, &k, &v)) {
  869. xs *d = xs_dict_new();
  870. d = xs_dict_append(d, "name", k);
  871. d = xs_dict_append(d, "value", v);
  872. d = xs_dict_append(d, "verified_at", xs_stock_null);
  873. fields = xs_list_append(fields, d);
  874. }
  875. acct = xs_dict_set(acct, "fields", fields);
  876. }
  877. acct = xs_dict_append(acct, "followers_count", xs_stock_0);
  878. acct = xs_dict_append(acct, "following_count", xs_stock_0);
  879. acct = xs_dict_append(acct, "statuses_count", xs_stock_0);
  880. *body = xs_json_dumps(acct, 4);
  881. *ctype = "application/json";
  882. status = 200;
  883. }
  884. else {
  885. status = 422; // "Unprocessable entity" (no login)
  886. }
  887. }
  888. else
  889. if (strcmp(cmd, "/v1/accounts/relationships") == 0) { /** **/
  890. /* find if an account is followed, blocked, etc. */
  891. /* the account to get relationships about is in args "id[]" */
  892. if (logged_in) {
  893. xs *res = xs_list_new();
  894. const char *md5 = xs_dict_get(args, "id[]");
  895. if (xs_is_null(md5))
  896. md5 = xs_dict_get(args, "id");
  897. if (!xs_is_null(md5)) {
  898. if (xs_type(md5) == XSTYPE_LIST)
  899. md5 = xs_list_get(md5, 0);
  900. xs *rel = mastoapi_relationship(&snac1, md5);
  901. if (rel != NULL)
  902. res = xs_list_append(res, rel);
  903. }
  904. *body = xs_json_dumps(res, 4);
  905. *ctype = "application/json";
  906. status = 200;
  907. }
  908. else
  909. status = 422;
  910. }
  911. else
  912. if (strcmp(cmd, "/v1/accounts/lookup") == 0) { /** **/
  913. /* lookup an account */
  914. char *acct = xs_dict_get(args, "acct");
  915. if (!xs_is_null(acct)) {
  916. xs *s = xs_strip_chars_i(xs_dup(acct), "@");
  917. xs *l = xs_split_n(s, "@", 1);
  918. char *uid = xs_list_get(l, 0);
  919. char *host = xs_list_get(l, 1);
  920. if (uid && (!host || strcmp(host, xs_dict_get(srv_config, "host")) == 0)) {
  921. snac user;
  922. if (user_open(&user, uid)) {
  923. xs *actor = msg_actor(&user);
  924. xs *macct = mastoapi_account(actor);
  925. *body = xs_json_dumps(macct, 4);
  926. *ctype = "application/json";
  927. status = 200;
  928. user_free(&user);
  929. }
  930. }
  931. }
  932. }
  933. else
  934. if (xs_startswith(cmd, "/v1/accounts/")) { /** **/
  935. /* account-related information */
  936. xs *l = xs_split(cmd, "/");
  937. const char *uid = xs_list_get(l, 3);
  938. const char *opt = xs_list_get(l, 4);
  939. if (uid != NULL) {
  940. snac snac2;
  941. xs *out = NULL;
  942. xs *actor = NULL;
  943. if (logged_in && strcmp(uid, "search") == 0) { /** **/
  944. /* search for accounts starting with q */
  945. const char *aq = xs_dict_get(args, "q");
  946. if (!xs_is_null(aq)) {
  947. xs *q = xs_tolower_i(xs_dup(aq));
  948. out = xs_list_new();
  949. xs *wing = following_list(&snac1);
  950. xs *wers = follower_list(&snac1);
  951. xs *ulst = user_list();
  952. xs_list *p;
  953. xs_str *v;
  954. xs_set seen;
  955. xs_set_init(&seen);
  956. /* user relations */
  957. xs_list *lsts[] = { wing, wers, NULL };
  958. int n;
  959. for (n = 0; (p = lsts[n]) != NULL; n++) {
  960. while (xs_list_iter(&p, &v)) {
  961. /* already seen? skip */
  962. if (xs_set_add(&seen, v) == 0)
  963. continue;
  964. xs *actor = NULL;
  965. if (valid_status(object_get(v, &actor))) {
  966. const char *uname = xs_dict_get(actor, "preferredUsername");
  967. if (!xs_is_null(uname)) {
  968. xs *luname = xs_tolower_i(xs_dup(uname));
  969. if (xs_startswith(luname, q)) {
  970. xs *acct = mastoapi_account(actor);
  971. out = xs_list_append(out, acct);
  972. }
  973. }
  974. }
  975. }
  976. }
  977. /* local users */
  978. p = ulst;
  979. while (xs_list_iter(&p, &v)) {
  980. snac user;
  981. /* skip this same user */
  982. if (strcmp(v, xs_dict_get(snac1.config, "uid")) == 0)
  983. continue;
  984. /* skip if the uid does not start with the query */
  985. xs *v2 = xs_tolower_i(xs_dup(v));
  986. if (!xs_startswith(v2, q))
  987. continue;
  988. if (user_open(&user, v)) {
  989. /* if it's not already seen, add it */
  990. if (xs_set_add(&seen, user.actor) == 1) {
  991. xs *actor = msg_actor(&user);
  992. xs *acct = mastoapi_account(actor);
  993. out = xs_list_append(out, acct);
  994. }
  995. user_free(&user);
  996. }
  997. }
  998. xs_set_free(&seen);
  999. }
  1000. }
  1001. else
  1002. /* is it a local user? */
  1003. if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
  1004. if (opt == NULL) {
  1005. /* account information */
  1006. actor = msg_actor(&snac2);
  1007. out = mastoapi_account(actor);
  1008. }
  1009. else
  1010. if (strcmp(opt, "statuses") == 0) { /** **/
  1011. /* the public list of posts of a user */
  1012. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  1013. xs_list *p = timeline;
  1014. xs_str *v;
  1015. out = xs_list_new();
  1016. while (xs_list_iter(&p, &v)) {
  1017. xs *msg = NULL;
  1018. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  1019. /* add only posts by the author */
  1020. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  1021. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  1022. xs *st = mastoapi_status(&snac2, msg);
  1023. if (st)
  1024. out = xs_list_append(out, st);
  1025. }
  1026. }
  1027. }
  1028. }
  1029. user_free(&snac2);
  1030. }
  1031. else {
  1032. /* try the uid as the md5 of a possibly loaded actor */
  1033. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  1034. if (opt == NULL) {
  1035. /* account information */
  1036. out = mastoapi_account(actor);
  1037. }
  1038. else
  1039. if (strcmp(opt, "statuses") == 0) {
  1040. /* we don't serve statuses of others; return the empty list */
  1041. out = xs_list_new();
  1042. }
  1043. }
  1044. }
  1045. if (out != NULL) {
  1046. *body = xs_json_dumps(out, 4);
  1047. *ctype = "application/json";
  1048. status = 200;
  1049. }
  1050. }
  1051. }
  1052. else
  1053. if (strcmp(cmd, "/v1/timelines/home") == 0) { /** **/
  1054. /* the private timeline */
  1055. if (logged_in) {
  1056. const char *max_id = xs_dict_get(args, "max_id");
  1057. const char *since_id = xs_dict_get(args, "since_id");
  1058. const char *min_id = xs_dict_get(args, "min_id");
  1059. const char *limit_s = xs_dict_get(args, "limit");
  1060. int limit = 0;
  1061. int cnt = 0;
  1062. if (!xs_is_null(limit_s))
  1063. limit = atoi(limit_s);
  1064. if (limit == 0)
  1065. limit = 20;
  1066. xs *timeline = timeline_simple_list(&snac1, "private", 0, 2048);
  1067. xs *out = xs_list_new();
  1068. xs_list *p = timeline;
  1069. xs_str *v;
  1070. while (xs_list_iter(&p, &v) && cnt < limit) {
  1071. xs *msg = NULL;
  1072. /* only return entries older that max_id */
  1073. if (max_id) {
  1074. if (strcmp(v, MID_TO_MD5(max_id)) == 0)
  1075. max_id = NULL;
  1076. continue;
  1077. }
  1078. /* only returns entries newer than since_id */
  1079. if (since_id) {
  1080. if (strcmp(v, MID_TO_MD5(since_id)) == 0)
  1081. break;
  1082. }
  1083. /* only returns entries newer than min_id */
  1084. /* what does really "Return results immediately newer than ID" mean? */
  1085. if (min_id) {
  1086. if (strcmp(v, MID_TO_MD5(min_id)) == 0)
  1087. break;
  1088. }
  1089. /* get the entry */
  1090. if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
  1091. continue;
  1092. /* discard non-Notes */
  1093. const char *id = xs_dict_get(msg, "id");
  1094. const char *type = xs_dict_get(msg, "type");
  1095. if (!xs_match(type, "Note|Question|Page|Article"))
  1096. continue;
  1097. const char *from = NULL;
  1098. if (strcmp(type, "Page") == 0)
  1099. from = xs_dict_get(msg, "audience");
  1100. if (from == NULL)
  1101. from = get_atto(msg);
  1102. if (from == NULL)
  1103. continue;
  1104. /* is this message from a person we don't follow? */
  1105. if (strcmp(from, snac1.actor) && !following_check(&snac1, from)) {
  1106. /* discard if it was not boosted */
  1107. xs *idx = object_announces(id);
  1108. if (xs_list_len(idx) == 0)
  1109. continue;
  1110. }
  1111. /* discard notes from muted morons */
  1112. if (is_muted(&snac1, from))
  1113. continue;
  1114. /* discard hidden notes */
  1115. if (is_hidden(&snac1, id))
  1116. continue;
  1117. /* discard poll votes (they have a name) */
  1118. if (strcmp(type, "Page") != 0 && !xs_is_null(xs_dict_get(msg, "name")))
  1119. continue;
  1120. /* convert the Note into a Mastodon status */
  1121. xs *st = mastoapi_status(&snac1, msg);
  1122. if (st != NULL)
  1123. out = xs_list_append(out, st);
  1124. cnt++;
  1125. }
  1126. *body = xs_json_dumps(out, 4);
  1127. *ctype = "application/json";
  1128. status = 200;
  1129. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  1130. }
  1131. else {
  1132. status = 401; // unauthorized
  1133. }
  1134. }
  1135. else
  1136. if (strcmp(cmd, "/v1/timelines/public") == 0) { /** **/
  1137. /* the instance public timeline (public timelines for all users) */
  1138. const char *limit_s = xs_dict_get(args, "limit");
  1139. int limit = 0;
  1140. int cnt = 0;
  1141. if (!xs_is_null(limit_s))
  1142. limit = atoi(limit_s);
  1143. if (limit == 0)
  1144. limit = 20;
  1145. xs *timeline = timeline_instance_list(0, limit);
  1146. xs *out = xs_list_new();
  1147. xs_list *p = timeline;
  1148. xs_str *md5;
  1149. snac *user = NULL;
  1150. if (logged_in)
  1151. user = &snac1;
  1152. while (xs_list_iter(&p, &md5) && cnt < limit) {
  1153. xs *msg = NULL;
  1154. /* get the entry */
  1155. if (!valid_status(object_get_by_md5(md5, &msg)))
  1156. continue;
  1157. /* discard non-Notes */
  1158. const char *type = xs_dict_get(msg, "type");
  1159. if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0)
  1160. continue;
  1161. /* discard private users */
  1162. {
  1163. const char *atto = get_atto(msg);
  1164. xs *l = xs_split(atto, "/");
  1165. const char *uid = xs_list_get(l, -1);
  1166. snac p_user;
  1167. int skip = 1;
  1168. if (uid && user_open(&p_user, uid)) {
  1169. if (xs_type(xs_dict_get(p_user.config, "private")) != XSTYPE_TRUE)
  1170. skip = 0;
  1171. user_free(&p_user);
  1172. }
  1173. if (skip)
  1174. continue;
  1175. }
  1176. /* convert the Note into a Mastodon status */
  1177. xs *st = mastoapi_status(user, msg);
  1178. if (st != NULL) {
  1179. out = xs_list_append(out, st);
  1180. cnt++;
  1181. }
  1182. }
  1183. *body = xs_json_dumps(out, 4);
  1184. *ctype = "application/json";
  1185. status = 200;
  1186. }
  1187. else
  1188. if (strcmp(cmd, "/v1/conversations") == 0) { /** **/
  1189. /* TBD */
  1190. *body = xs_dup("[]");
  1191. *ctype = "application/json";
  1192. status = 200;
  1193. }
  1194. else
  1195. if (strcmp(cmd, "/v1/notifications") == 0) { /** **/
  1196. if (logged_in) {
  1197. xs *l = notify_list(&snac1, 0);
  1198. xs *out = xs_list_new();
  1199. xs_list *p = l;
  1200. xs_dict *v;
  1201. xs_list *excl = xs_dict_get(args, "exclude_types[]");
  1202. while (xs_list_iter(&p, &v)) {
  1203. xs *noti = notify_get(&snac1, v);
  1204. if (noti == NULL)
  1205. continue;
  1206. const char *type = xs_dict_get(noti, "type");
  1207. const char *utype = xs_dict_get(noti, "utype");
  1208. const char *objid = xs_dict_get(noti, "objid");
  1209. xs *actor = NULL;
  1210. xs *entry = NULL;
  1211. if (!valid_status(actor_get(xs_dict_get(noti, "actor"), &actor)))
  1212. continue;
  1213. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  1214. continue;
  1215. if (is_hidden(&snac1, objid))
  1216. continue;
  1217. /* convert the type */
  1218. if (strcmp(type, "Like") == 0)
  1219. type = "favourite";
  1220. else
  1221. if (strcmp(type, "Announce") == 0)
  1222. type = "reblog";
  1223. else
  1224. if (strcmp(type, "Follow") == 0)
  1225. type = "follow";
  1226. else
  1227. if (strcmp(type, "Create") == 0)
  1228. type = "mention";
  1229. else
  1230. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  1231. type = "poll";
  1232. else
  1233. continue;
  1234. /* excluded type? */
  1235. if (!xs_is_null(excl) && xs_list_in(excl, type) != -1)
  1236. continue;
  1237. xs *mn = xs_dict_new();
  1238. mn = xs_dict_append(mn, "type", type);
  1239. xs *id = xs_replace(xs_dict_get(noti, "id"), ".", "");
  1240. mn = xs_dict_append(mn, "id", id);
  1241. mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
  1242. xs *acct = mastoapi_account(actor);
  1243. mn = xs_dict_append(mn, "account", acct);
  1244. if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
  1245. xs *st = mastoapi_status(&snac1, entry);
  1246. if (st)
  1247. mn = xs_dict_append(mn, "status", st);
  1248. }
  1249. out = xs_list_append(out, mn);
  1250. }
  1251. *body = xs_json_dumps(out, 4);
  1252. *ctype = "application/json";
  1253. status = 200;
  1254. }
  1255. else
  1256. status = 401;
  1257. }
  1258. else
  1259. if (strcmp(cmd, "/v1/filters") == 0) { /** **/
  1260. /* snac will never have filters */
  1261. *body = xs_dup("[]");
  1262. *ctype = "application/json";
  1263. status = 200;
  1264. }
  1265. else
  1266. if (strcmp(cmd, "/v1/favourites") == 0) { /** **/
  1267. /* snac will never support a list of favourites */
  1268. *body = xs_dup("[]");
  1269. *ctype = "application/json";
  1270. status = 200;
  1271. }
  1272. else
  1273. if (strcmp(cmd, "/v1/bookmarks") == 0) { /** **/
  1274. /* snac does not support bookmarks */
  1275. *body = xs_dup("[]");
  1276. *ctype = "application/json";
  1277. status = 200;
  1278. }
  1279. else
  1280. if (strcmp(cmd, "/v1/lists") == 0) { /** **/
  1281. /* snac does not support lists */
  1282. *body = xs_dup("[]");
  1283. *ctype = "application/json";
  1284. status = 200;
  1285. }
  1286. else
  1287. if (strcmp(cmd, "/v1/scheduled_statuses") == 0) { /** **/
  1288. /* snac does not schedule notes */
  1289. *body = xs_dup("[]");
  1290. *ctype = "application/json";
  1291. status = 200;
  1292. }
  1293. else
  1294. if (strcmp(cmd, "/v1/follow_requests") == 0) { /** **/
  1295. /* snac does not support optional follow confirmations */
  1296. *body = xs_dup("[]");
  1297. *ctype = "application/json";
  1298. status = 200;
  1299. }
  1300. else
  1301. if (strcmp(cmd, "/v1/announcements") == 0) { /** **/
  1302. /* snac has no announcements (yet?) */
  1303. *body = xs_dup("[]");
  1304. *ctype = "application/json";
  1305. status = 200;
  1306. }
  1307. else
  1308. if (strcmp(cmd, "/v1/custom_emojis") == 0) { /** **/
  1309. /* are you kidding me? */
  1310. *body = xs_dup("[]");
  1311. *ctype = "application/json";
  1312. status = 200;
  1313. }
  1314. else
  1315. if (strcmp(cmd, "/v1/instance") == 0) { /** **/
  1316. /* returns an instance object */
  1317. xs *ins = xs_dict_new();
  1318. const char *host = xs_dict_get(srv_config, "host");
  1319. const char *title = xs_dict_get(srv_config, "title");
  1320. const char *sdesc = xs_dict_get(srv_config, "short_description");
  1321. ins = xs_dict_append(ins, "uri", host);
  1322. ins = xs_dict_append(ins, "domain", host);
  1323. ins = xs_dict_append(ins, "title", title && *title ? title : host);
  1324. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  1325. ins = xs_dict_append(ins, "source_url", WHAT_IS_SNAC_URL);
  1326. ins = xs_dict_append(ins, "description", host);
  1327. ins = xs_dict_append(ins, "short_description", sdesc && *sdesc ? sdesc : host);
  1328. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  1329. ins = xs_dict_append(ins, "thumbnail", susie);
  1330. const char *v = xs_dict_get(srv_config, "admin_email");
  1331. if (xs_is_null(v) || *v == '\0')
  1332. v = "admin@localhost";
  1333. ins = xs_dict_append(ins, "email", v);
  1334. ins = xs_dict_append(ins, "rules", xs_stock_list);
  1335. xs *l1 = xs_list_append(xs_list_new(), "en");
  1336. ins = xs_dict_append(ins, "languages", l1);
  1337. ins = xs_dict_append(ins, "urls", xs_stock_dict);
  1338. xs *d2 = xs_dict_append(xs_dict_new(), "user_count", xs_stock_0);
  1339. d2 = xs_dict_append(d2, "status_count", xs_stock_0);
  1340. d2 = xs_dict_append(d2, "domain_count", xs_stock_0);
  1341. ins = xs_dict_append(ins, "stats", d2);
  1342. ins = xs_dict_append(ins, "registrations", xs_stock_false);
  1343. ins = xs_dict_append(ins, "approval_required", xs_stock_false);
  1344. ins = xs_dict_append(ins, "invites_enabled", xs_stock_false);
  1345. xs *cfg = xs_dict_new();
  1346. {
  1347. xs *d11 = xs_json_loads("{\"characters_reserved_per_url\":32,"
  1348. "\"max_characters\":100000,\"max_media_attachments\":8}");
  1349. cfg = xs_dict_append(cfg, "statuses", d11);
  1350. xs *d12 = xs_json_loads("{\"max_featured_tags\":10}");
  1351. cfg = xs_dict_append(cfg, "accounts", d12);
  1352. xs *d13 = xs_json_loads("{\"image_matrix_limit\":33177600,"
  1353. "\"image_size_limit\":16777216,"
  1354. "\"video_frame_rate_limit\":120,"
  1355. "\"video_matrix_limit\":8294400,"
  1356. "\"video_size_limit\":103809024}"
  1357. );
  1358. {
  1359. /* get the supported mime types from the internal list */
  1360. const char **p = xs_mime_types;
  1361. xs_set mtypes;
  1362. xs_set_init(&mtypes);
  1363. while (*p) {
  1364. const char *type = p[1];
  1365. if (xs_startswith(type, "image/") ||
  1366. xs_startswith(type, "video/") ||
  1367. xs_startswith(type, "audio/"))
  1368. xs_set_add(&mtypes, type);
  1369. p += 2;
  1370. }
  1371. xs *l = xs_set_result(&mtypes);
  1372. d13 = xs_dict_append(d13, "supported_mime_types", l);
  1373. }
  1374. cfg = xs_dict_append(cfg, "media_attachments", d13);
  1375. xs *d14 = xs_json_loads("{\"max_characters_per_option\":50,"
  1376. "\"max_expiration\":2629746,"
  1377. "\"max_options\":8,\"min_expiration\":300}");
  1378. cfg = xs_dict_append(cfg, "polls", d14);
  1379. }
  1380. ins = xs_dict_append(ins, "configuration", cfg);
  1381. const char *admin_account = xs_dict_get(srv_config, "admin_account");
  1382. if (!xs_is_null(admin_account) && *admin_account) {
  1383. snac admin;
  1384. if (user_open(&admin, admin_account)) {
  1385. xs *actor = msg_actor(&admin);
  1386. xs *acct = mastoapi_account(actor);
  1387. ins = xs_dict_append(ins, "contact_account", acct);
  1388. user_free(&admin);
  1389. }
  1390. }
  1391. *body = xs_json_dumps(ins, 4);
  1392. *ctype = "application/json";
  1393. status = 200;
  1394. }
  1395. else
  1396. if (xs_startswith(cmd, "/v1/statuses/")) { /** **/
  1397. /* information about a status */
  1398. if (logged_in) {
  1399. xs *l = xs_split(cmd, "/");
  1400. const char *id = xs_list_get(l, 3);
  1401. const char *op = xs_list_get(l, 4);
  1402. if (!xs_is_null(id)) {
  1403. xs *msg = NULL;
  1404. xs *out = NULL;
  1405. /* skip the 'fake' part of the id */
  1406. id = MID_TO_MD5(id);
  1407. if (valid_status(object_get_by_md5(id, &msg))) {
  1408. if (op == NULL) {
  1409. if (!is_muted(&snac1, get_atto(msg))) {
  1410. /* return the status itself */
  1411. out = mastoapi_status(&snac1, msg);
  1412. }
  1413. }
  1414. else
  1415. if (strcmp(op, "context") == 0) { /** **/
  1416. /* return ancestors and children */
  1417. xs *anc = xs_list_new();
  1418. xs *des = xs_list_new();
  1419. xs_list *p;
  1420. xs_str *v;
  1421. char pid[64];
  1422. /* build the [grand]parent list, moving up */
  1423. strncpy(pid, id, sizeof(pid));
  1424. while (object_parent(pid, pid, sizeof(pid))) {
  1425. xs *m2 = NULL;
  1426. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  1427. xs *st = mastoapi_status(&snac1, m2);
  1428. if (st)
  1429. anc = xs_list_insert(anc, 0, st);
  1430. }
  1431. else
  1432. break;
  1433. }
  1434. /* build the children list */
  1435. xs *children = object_children(xs_dict_get(msg, "id"));
  1436. p = children;
  1437. while (xs_list_iter(&p, &v)) {
  1438. xs *m2 = NULL;
  1439. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  1440. if (xs_is_null(xs_dict_get(m2, "name"))) {
  1441. xs *st = mastoapi_status(&snac1, m2);
  1442. if (st)
  1443. des = xs_list_append(des, st);
  1444. }
  1445. }
  1446. }
  1447. out = xs_dict_new();
  1448. out = xs_dict_append(out, "ancestors", anc);
  1449. out = xs_dict_append(out, "descendants", des);
  1450. }
  1451. else
  1452. if (strcmp(op, "reblogged_by") == 0 || /** **/
  1453. strcmp(op, "favourited_by") == 0) { /** **/
  1454. /* return the list of people who liked or boosted this */
  1455. out = xs_list_new();
  1456. xs *l = NULL;
  1457. if (op[0] == 'r')
  1458. l = object_announces(xs_dict_get(msg, "id"));
  1459. else
  1460. l = object_likes(xs_dict_get(msg, "id"));
  1461. xs_list *p = l;
  1462. xs_str *v;
  1463. while (xs_list_iter(&p, &v)) {
  1464. xs *actor2 = NULL;
  1465. if (valid_status(object_get_by_md5(v, &actor2))) {
  1466. xs *acct2 = mastoapi_account(actor2);
  1467. out = xs_list_append(out, acct2);
  1468. }
  1469. }
  1470. }
  1471. else
  1472. if (strcmp(op, "source") == 0) { /** **/
  1473. out = xs_dict_new();
  1474. /* get the mastoapi status id */
  1475. out = xs_dict_append(out, "id", xs_list_get(l, 3));
  1476. out = xs_dict_append(out, "text", xs_dict_get(msg, "sourceContent"));
  1477. out = xs_dict_append(out, "spoiler_text", xs_dict_get(msg, "summary"));
  1478. }
  1479. }
  1480. else
  1481. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  1482. if (out != NULL) {
  1483. *body = xs_json_dumps(out, 4);
  1484. *ctype = "application/json";
  1485. status = 200;
  1486. }
  1487. }
  1488. }
  1489. else
  1490. status = 401;
  1491. }
  1492. else
  1493. if (strcmp(cmd, "/v1/preferences") == 0) { /** **/
  1494. *body = xs_dup("{}");
  1495. *ctype = "application/json";
  1496. status = 200;
  1497. }
  1498. else
  1499. if (strcmp(cmd, "/v1/markers") == 0) { /** **/
  1500. *body = xs_dup("{}");
  1501. *ctype = "application/json";
  1502. status = 200;
  1503. }
  1504. else
  1505. if (strcmp(cmd, "/v1/followed_tags") == 0) { /** **/
  1506. *body = xs_dup("[]");
  1507. *ctype = "application/json";
  1508. status = 200;
  1509. }
  1510. else
  1511. if (strcmp(cmd, "/v2/search") == 0) { /** **/
  1512. if (logged_in) {
  1513. const char *q = xs_dict_get(args, "q");
  1514. const char *type = xs_dict_get(args, "type");
  1515. const char *offset = xs_dict_get(args, "offset");
  1516. xs *acl = xs_list_new();
  1517. xs *stl = xs_list_new();
  1518. xs *htl = xs_list_new();
  1519. xs *res = xs_dict_new();
  1520. if (xs_is_null(offset) || strcmp(offset, "0") == 0) {
  1521. /* reply something only for offset 0; otherwise,
  1522. apps like Tusky keep asking again and again */
  1523. if (!xs_is_null(q) && !xs_is_null(type) && strcmp(type, "accounts") == 0) {
  1524. /* do a webfinger query */
  1525. char *actor = NULL;
  1526. char *user = NULL;
  1527. if (valid_status(webfinger_request(q, &actor, &user) && actor)) {
  1528. xs *actor_o = NULL;
  1529. if (valid_status(actor_request(&snac1, actor, &actor_o))) {
  1530. xs *acct = mastoapi_account(actor_o);
  1531. acl = xs_list_append(acl, acct);
  1532. }
  1533. }
  1534. }
  1535. }
  1536. res = xs_dict_append(res, "accounts", acl);
  1537. res = xs_dict_append(res, "statuses", stl);
  1538. res = xs_dict_append(res, "hashtags", htl);
  1539. *body = xs_json_dumps(res, 4);
  1540. *ctype = "application/json";
  1541. status = 200;
  1542. }
  1543. else
  1544. status = 401;
  1545. }
  1546. /* user cleanup */
  1547. if (logged_in)
  1548. user_free(&snac1);
  1549. srv_debug(1, xs_fmt("mastoapi_get_handler %s %d", q_path, status));
  1550. return status;
  1551. }
  1552. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  1553. const char *payload, int p_size,
  1554. char **body, int *b_size, char **ctype)
  1555. {
  1556. (void)p_size;
  1557. (void)b_size;
  1558. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1559. return 0;
  1560. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1561. int status = 404;
  1562. xs *args = NULL;
  1563. char *i_ctype = xs_dict_get(req, "content-type");
  1564. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1565. args = xs_json_loads(payload);
  1566. else
  1567. args = xs_dup(xs_dict_get(req, "p_vars"));
  1568. if (args == NULL)
  1569. return 400;
  1570. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1571. snac snac = {0};
  1572. int logged_in = process_auth_token(&snac, req);
  1573. if (strcmp(cmd, "/v1/apps") == 0) { /** **/
  1574. const char *name = xs_dict_get(args, "client_name");
  1575. const char *ruri = xs_dict_get(args, "redirect_uris");
  1576. const char *scope = xs_dict_get(args, "scope");
  1577. if (xs_type(ruri) == XSTYPE_LIST)
  1578. ruri = xs_dict_get(ruri, 0);
  1579. if (name && ruri) {
  1580. xs *app = xs_dict_new();
  1581. xs *id = xs_replace_i(tid(0), ".", "");
  1582. xs *csec = random_str();
  1583. xs *vkey = random_str();
  1584. xs *cid = NULL;
  1585. /* pick a non-existent random cid */
  1586. for (;;) {
  1587. cid = random_str();
  1588. xs *p_app = app_get(cid);
  1589. if (p_app == NULL)
  1590. break;
  1591. xs_free(cid);
  1592. }
  1593. app = xs_dict_append(app, "name", name);
  1594. app = xs_dict_append(app, "redirect_uri", ruri);
  1595. app = xs_dict_append(app, "client_id", cid);
  1596. app = xs_dict_append(app, "client_secret", csec);
  1597. app = xs_dict_append(app, "vapid_key", vkey);
  1598. app = xs_dict_append(app, "id", id);
  1599. *body = xs_json_dumps(app, 4);
  1600. *ctype = "application/json";
  1601. status = 200;
  1602. app = xs_dict_append(app, "code", "");
  1603. if (scope)
  1604. app = xs_dict_append(app, "scope", scope);
  1605. app_add(cid, app);
  1606. srv_debug(1, xs_fmt("mastoapi apps: new app %s", cid));
  1607. }
  1608. }
  1609. else
  1610. if (strcmp(cmd, "/v1/statuses") == 0) { /** **/
  1611. if (logged_in) {
  1612. /* post a new Note */
  1613. const char *content = xs_dict_get(args, "status");
  1614. const char *mid = xs_dict_get(args, "in_reply_to_id");
  1615. const char *visibility = xs_dict_get(args, "visibility");
  1616. const char *summary = xs_dict_get(args, "spoiler_text");
  1617. const char *media_ids = xs_dict_get(args, "media_ids");
  1618. if (xs_is_null(media_ids))
  1619. media_ids = xs_dict_get(args, "media_ids[]");
  1620. if (xs_is_null(media_ids))
  1621. media_ids = xs_dict_get(args, "media_ids");
  1622. if (xs_is_null(visibility))
  1623. visibility = "public";
  1624. xs *attach_list = xs_list_new();
  1625. xs *irt = NULL;
  1626. /* is it a reply? */
  1627. if (mid != NULL) {
  1628. xs *r_msg = NULL;
  1629. const char *md5 = MID_TO_MD5(mid);
  1630. if (valid_status(object_get_by_md5(md5, &r_msg)))
  1631. irt = xs_dup(xs_dict_get(r_msg, "id"));
  1632. }
  1633. /* does it have attachments? */
  1634. if (!xs_is_null(media_ids)) {
  1635. xs *mi = NULL;
  1636. if (xs_type(media_ids) == XSTYPE_LIST)
  1637. mi = xs_dup(media_ids);
  1638. else {
  1639. mi = xs_list_new();
  1640. mi = xs_list_append(mi, media_ids);
  1641. }
  1642. xs_list *p = mi;
  1643. xs_str *v;
  1644. while (xs_list_iter(&p, &v)) {
  1645. xs *l = xs_list_new();
  1646. xs *url = xs_fmt("%s/s/%s", snac.actor, v);
  1647. xs *desc = static_get_meta(&snac, v);
  1648. l = xs_list_append(l, url);
  1649. l = xs_list_append(l, desc);
  1650. attach_list = xs_list_append(attach_list, l);
  1651. }
  1652. }
  1653. /* prepare the message */
  1654. xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
  1655. strcmp(visibility, "public") == 0 ? 0 : 1);
  1656. if (!xs_is_null(summary) && *summary) {
  1657. msg = xs_dict_set(msg, "sensitive", xs_stock_true);
  1658. msg = xs_dict_set(msg, "summary", summary);
  1659. }
  1660. /* store */
  1661. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1662. /* 'Create' message */
  1663. xs *c_msg = msg_create(&snac, msg);
  1664. enqueue_message(&snac, c_msg);
  1665. timeline_touch(&snac);
  1666. /* convert to a mastodon status as a response code */
  1667. xs *st = mastoapi_status(&snac, msg);
  1668. *body = xs_json_dumps(st, 4);
  1669. *ctype = "application/json";
  1670. status = 200;
  1671. }
  1672. else
  1673. status = 401;
  1674. }
  1675. else
  1676. if (xs_startswith(cmd, "/v1/statuses")) { /** **/
  1677. if (logged_in) {
  1678. /* operations on a status */
  1679. xs *l = xs_split(cmd, "/");
  1680. const char *mid = xs_list_get(l, 3);
  1681. const char *op = xs_list_get(l, 4);
  1682. if (!xs_is_null(mid)) {
  1683. xs *msg = NULL;
  1684. xs *out = NULL;
  1685. /* skip the 'fake' part of the id */
  1686. mid = MID_TO_MD5(mid);
  1687. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1688. char *id = xs_dict_get(msg, "id");
  1689. if (op == NULL) {
  1690. /* no operation (?) */
  1691. }
  1692. else
  1693. if (strcmp(op, "favourite") == 0) { /** **/
  1694. xs *n_msg = msg_admiration(&snac, id, "Like");
  1695. if (n_msg != NULL) {
  1696. enqueue_message(&snac, n_msg);
  1697. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  1698. out = mastoapi_status(&snac, msg);
  1699. }
  1700. }
  1701. else
  1702. if (strcmp(op, "unfavourite") == 0) { /** **/
  1703. /* partial support: as the original Like message
  1704. is not stored anywhere here, it's not possible
  1705. to send an Undo + Like; the only thing done here
  1706. is to delete the actor from the list of likes */
  1707. object_unadmire(id, snac.actor, 1);
  1708. }
  1709. else
  1710. if (strcmp(op, "reblog") == 0) { /** **/
  1711. xs *n_msg = msg_admiration(&snac, id, "Announce");
  1712. if (n_msg != NULL) {
  1713. enqueue_message(&snac, n_msg);
  1714. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  1715. out = mastoapi_status(&snac, msg);
  1716. }
  1717. }
  1718. else
  1719. if (strcmp(op, "unreblog") == 0) { /** **/
  1720. /* partial support: see comment in 'unfavourite' */
  1721. object_unadmire(id, snac.actor, 0);
  1722. }
  1723. else
  1724. if (strcmp(op, "bookmark") == 0) { /** **/
  1725. /* snac does not support bookmarks */
  1726. }
  1727. else
  1728. if (strcmp(op, "unbookmark") == 0) { /** **/
  1729. /* snac does not support bookmarks */
  1730. }
  1731. else
  1732. if (strcmp(op, "pin") == 0) { /** **/
  1733. /* pin this message */
  1734. if (pin(&snac, id))
  1735. out = mastoapi_status(&snac, msg);
  1736. else
  1737. status = 422;
  1738. }
  1739. else
  1740. if (strcmp(op, "unpin") == 0) { /** **/
  1741. /* unpin this message */
  1742. unpin(&snac, id);
  1743. out = mastoapi_status(&snac, msg);
  1744. }
  1745. else
  1746. if (strcmp(op, "mute") == 0) { /** **/
  1747. /* Mastodon's mute is snac's hide */
  1748. }
  1749. else
  1750. if (strcmp(op, "unmute") == 0) { /** **/
  1751. /* Mastodon's unmute is snac's unhide */
  1752. }
  1753. }
  1754. if (out != NULL) {
  1755. *body = xs_json_dumps(out, 4);
  1756. *ctype = "application/json";
  1757. status = 200;
  1758. }
  1759. }
  1760. }
  1761. else
  1762. status = 401;
  1763. }
  1764. else
  1765. if (strcmp(cmd, "/v1/notifications/clear") == 0) { /** **/
  1766. if (logged_in) {
  1767. notify_clear(&snac);
  1768. timeline_touch(&snac);
  1769. *body = xs_dup("{}");
  1770. *ctype = "application/json";
  1771. status = 200;
  1772. }
  1773. else
  1774. status = 401;
  1775. }
  1776. else
  1777. if (strcmp(cmd, "/v1/push/subscription") == 0) { /** **/
  1778. /* I don't know what I'm doing */
  1779. if (logged_in) {
  1780. char *v;
  1781. xs *wpush = xs_dict_new();
  1782. wpush = xs_dict_append(wpush, "id", "1");
  1783. v = xs_dict_get(args, "data");
  1784. v = xs_dict_get(v, "alerts");
  1785. wpush = xs_dict_append(wpush, "alerts", v);
  1786. v = xs_dict_get(args, "subscription");
  1787. v = xs_dict_get(v, "endpoint");
  1788. wpush = xs_dict_append(wpush, "endpoint", v);
  1789. xs *server_key = random_str();
  1790. wpush = xs_dict_append(wpush, "server_key", server_key);
  1791. *body = xs_json_dumps(wpush, 4);
  1792. *ctype = "application/json";
  1793. status = 200;
  1794. }
  1795. else
  1796. status = 401;
  1797. }
  1798. else
  1799. if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { /** **/
  1800. if (logged_in) {
  1801. const xs_list *file = xs_dict_get(args, "file");
  1802. const char *desc = xs_dict_get(args, "description");
  1803. if (xs_is_null(desc))
  1804. desc = "";
  1805. status = 400;
  1806. if (xs_type(file) == XSTYPE_LIST) {
  1807. const char *fn = xs_list_get(file, 0);
  1808. if (*fn != '\0') {
  1809. char *ext = strrchr(fn, '.');
  1810. xs *hash = xs_md5_hex(fn, strlen(fn));
  1811. xs *id = xs_fmt("%s%s", hash, ext);
  1812. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1813. int fo = xs_number_get(xs_list_get(file, 1));
  1814. int fs = xs_number_get(xs_list_get(file, 2));
  1815. /* store */
  1816. static_put(&snac, id, payload + fo, fs);
  1817. static_put_meta(&snac, id, desc);
  1818. /* prepare a response */
  1819. xs *rsp = xs_dict_new();
  1820. rsp = xs_dict_append(rsp, "id", id);
  1821. rsp = xs_dict_append(rsp, "type", "image");
  1822. rsp = xs_dict_append(rsp, "url", url);
  1823. rsp = xs_dict_append(rsp, "preview_url", url);
  1824. rsp = xs_dict_append(rsp, "remote_url", url);
  1825. rsp = xs_dict_append(rsp, "description", desc);
  1826. *body = xs_json_dumps(rsp, 4);
  1827. *ctype = "application/json";
  1828. status = 200;
  1829. }
  1830. }
  1831. }
  1832. else
  1833. status = 401;
  1834. }
  1835. else
  1836. if (xs_startswith(cmd, "/v1/accounts")) { /** **/
  1837. if (logged_in) {
  1838. /* account-related information */
  1839. xs *l = xs_split(cmd, "/");
  1840. const char *md5 = xs_list_get(l, 3);
  1841. const char *opt = xs_list_get(l, 4);
  1842. xs *rsp = NULL;
  1843. if (!xs_is_null(md5) && *md5) {
  1844. xs *actor_o = NULL;
  1845. if (xs_is_null(opt)) {
  1846. /* ? */
  1847. }
  1848. else
  1849. if (strcmp(opt, "follow") == 0) { /** **/
  1850. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1851. const char *actor = xs_dict_get(actor_o, "id");
  1852. xs *msg = msg_follow(&snac, actor);
  1853. if (msg != NULL) {
  1854. /* reload the actor from the message, in may be different */
  1855. actor = xs_dict_get(msg, "object");
  1856. following_add(&snac, actor, msg);
  1857. enqueue_output_by_actor(&snac, msg, actor, 0);
  1858. rsp = mastoapi_relationship(&snac, md5);
  1859. }
  1860. }
  1861. }
  1862. else
  1863. if (strcmp(opt, "unfollow") == 0) { /** **/
  1864. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1865. const char *actor = xs_dict_get(actor_o, "id");
  1866. /* get the following object */
  1867. xs *object = NULL;
  1868. if (valid_status(following_get(&snac, actor, &object))) {
  1869. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  1870. following_del(&snac, actor);
  1871. enqueue_output_by_actor(&snac, msg, actor, 0);
  1872. rsp = mastoapi_relationship(&snac, md5);
  1873. }
  1874. }
  1875. }
  1876. else
  1877. if (strcmp(opt, "block") == 0) { /** **/
  1878. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1879. const char *actor = xs_dict_get(actor_o, "id");
  1880. mute(&snac, actor);
  1881. rsp = mastoapi_relationship(&snac, md5);
  1882. }
  1883. }
  1884. else
  1885. if (strcmp(opt, "unblock") == 0) { /** **/
  1886. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1887. const char *actor = xs_dict_get(actor_o, "id");
  1888. unmute(&snac, actor);
  1889. rsp = mastoapi_relationship(&snac, md5);
  1890. }
  1891. }
  1892. }
  1893. if (rsp != NULL) {
  1894. *body = xs_json_dumps(rsp, 4);
  1895. *ctype = "application/json";
  1896. status = 200;
  1897. }
  1898. }
  1899. else
  1900. status = 401;
  1901. }
  1902. else
  1903. if (xs_startswith(cmd, "/v1/polls")) { /** **/
  1904. if (logged_in) {
  1905. /* operations on a status */
  1906. xs *l = xs_split(cmd, "/");
  1907. const char *mid = xs_list_get(l, 3);
  1908. const char *op = xs_list_get(l, 4);
  1909. if (!xs_is_null(mid)) {
  1910. xs *msg = NULL;
  1911. xs *out = NULL;
  1912. /* skip the 'fake' part of the id */
  1913. mid = MID_TO_MD5(mid);
  1914. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1915. const char *id = xs_dict_get(msg, "id");
  1916. const char *atto = get_atto(msg);
  1917. xs_list *opts = xs_dict_get(msg, "oneOf");
  1918. if (opts == NULL)
  1919. opts = xs_dict_get(msg, "anyOf");
  1920. if (op == NULL) {
  1921. }
  1922. else
  1923. if (strcmp(op, "votes") == 0) {
  1924. xs_list *choices = xs_dict_get(args, "choices[]");
  1925. if (xs_is_null(choices))
  1926. choices = xs_dict_get(args, "choices");
  1927. if (xs_type(choices) == XSTYPE_LIST) {
  1928. xs_str *v;
  1929. while (xs_list_iter(&choices, &v)) {
  1930. int io = atoi(v);
  1931. const xs_dict *o = xs_list_get(opts, io);
  1932. if (o) {
  1933. const char *name = xs_dict_get(o, "name");
  1934. xs *msg = msg_note(&snac, "", atto, (char *)id, NULL, 1);
  1935. msg = xs_dict_append(msg, "name", name);
  1936. xs *c_msg = msg_create(&snac, msg);
  1937. enqueue_message(&snac, c_msg);
  1938. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1939. }
  1940. }
  1941. out = mastoapi_poll(&snac, msg);
  1942. }
  1943. }
  1944. }
  1945. if (out != NULL) {
  1946. *body = xs_json_dumps(out, 4);
  1947. *ctype = "application/json";
  1948. status = 200;
  1949. }
  1950. }
  1951. }
  1952. else
  1953. status = 401;
  1954. }
  1955. /* user cleanup */
  1956. if (logged_in)
  1957. user_free(&snac);
  1958. return status;
  1959. }
  1960. int mastoapi_delete_handler(const xs_dict *req, const char *q_path,
  1961. char **body, int *b_size, char **ctype) {
  1962. (void)req;
  1963. (void)body;
  1964. (void)b_size;
  1965. (void)ctype;
  1966. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1967. return 0;
  1968. srv_debug(1, xs_fmt("mastoapi_delete_handler %s", q_path));
  1969. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1970. if (xs_startswith(cmd, "/v1/push/subscription") || xs_startswith(cmd, "/v2/push/subscription")) { /** **/
  1971. // pretend we deleted it, since it doesn't exist anyway
  1972. return 200;
  1973. }
  1974. return 0;
  1975. }
  1976. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  1977. const char *payload, int p_size,
  1978. char **body, int *b_size, char **ctype)
  1979. {
  1980. (void)p_size;
  1981. (void)b_size;
  1982. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1983. return 0;
  1984. int status = 404;
  1985. xs *args = NULL;
  1986. char *i_ctype = xs_dict_get(req, "content-type");
  1987. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1988. args = xs_json_loads(payload);
  1989. else
  1990. args = xs_dup(xs_dict_get(req, "p_vars"));
  1991. if (args == NULL)
  1992. return 400;
  1993. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1994. snac snac = {0};
  1995. int logged_in = process_auth_token(&snac, req);
  1996. if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) { /** **/
  1997. if (logged_in) {
  1998. xs *l = xs_split(cmd, "/");
  1999. const char *stid = xs_list_get(l, 3);
  2000. if (!xs_is_null(stid)) {
  2001. const char *desc = xs_dict_get(args, "description");
  2002. /* set the image metadata */
  2003. static_put_meta(&snac, stid, desc);
  2004. /* prepare a response */
  2005. xs *rsp = xs_dict_new();
  2006. xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
  2007. rsp = xs_dict_append(rsp, "id", stid);
  2008. rsp = xs_dict_append(rsp, "type", "image");
  2009. rsp = xs_dict_append(rsp, "url", url);
  2010. rsp = xs_dict_append(rsp, "preview_url", url);
  2011. rsp = xs_dict_append(rsp, "remote_url", url);
  2012. rsp = xs_dict_append(rsp, "description", desc);
  2013. *body = xs_json_dumps(rsp, 4);
  2014. *ctype = "application/json";
  2015. status = 200;
  2016. }
  2017. }
  2018. else
  2019. status = 401;
  2020. }
  2021. else
  2022. if (xs_startswith(cmd, "/v1/statuses")) {
  2023. if (logged_in) {
  2024. xs *l = xs_split(cmd, "/");
  2025. const char *mid = xs_list_get(l, 3);
  2026. if (!xs_is_null(mid)) {
  2027. const char *md5 = MID_TO_MD5(mid);
  2028. xs *rsp = NULL;
  2029. xs *msg = NULL;
  2030. if (valid_status(timeline_get_by_md5(&snac, md5, &msg))) {
  2031. const char *content = xs_dict_get(args, "status");
  2032. xs *atls = xs_list_new();
  2033. xs *f_content = not_really_markdown(content, &atls);
  2034. /* replace fields with new content */
  2035. msg = xs_dict_set(msg, "sourceContent", content);
  2036. msg = xs_dict_set(msg, "content", f_content);
  2037. xs *updated = xs_str_utctime(0, ISO_DATE_SPEC);
  2038. msg = xs_dict_set(msg, "updated", updated);
  2039. /* overwrite object, not updating the indexes */
  2040. object_add_ow(xs_dict_get(msg, "id"), msg);
  2041. /* update message */
  2042. xs *c_msg = msg_update(&snac, msg);
  2043. enqueue_message(&snac, c_msg);
  2044. rsp = mastoapi_status(&snac, msg);
  2045. }
  2046. if (rsp != NULL) {
  2047. *body = xs_json_dumps(rsp, 4);
  2048. *ctype = "application/json";
  2049. status = 200;
  2050. }
  2051. }
  2052. }
  2053. else
  2054. status = 401;
  2055. }
  2056. /* user cleanup */
  2057. if (logged_in)
  2058. user_free(&snac);
  2059. srv_debug(1, xs_fmt("mastoapi_put_handler %s %d", q_path, status));
  2060. return status;
  2061. }
  2062. void mastoapi_purge(void)
  2063. {
  2064. xs *spec = xs_fmt("%s/app/" "*.json", srv_basedir);
  2065. xs *files = xs_glob(spec, 1, 0);
  2066. xs_list *p = files;
  2067. xs_str *v;
  2068. time_t mt = time(NULL) - 3600;
  2069. while (xs_list_iter(&p, &v)) {
  2070. xs *cid = xs_replace(v, ".json", "");
  2071. xs *fn = _app_fn(cid);
  2072. if (mtime(fn) < mt) {
  2073. /* get the app */
  2074. xs *app = app_get(cid);
  2075. if (app) {
  2076. /* old apps with no uid are incomplete cruft */
  2077. const char *uid = xs_dict_get(app, "uid");
  2078. if (xs_is_null(uid) || *uid == '\0') {
  2079. unlink(fn);
  2080. srv_debug(2, xs_fmt("purged %s", fn));
  2081. }
  2082. }
  2083. }
  2084. }
  2085. }
  2086. #endif /* #ifndef NO_MASTODON_API */