mastoapi.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 grunfink / MIT license */
  3. #ifndef NO_MASTODON_API
  4. #include "xs.h"
  5. #include "xs_encdec.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 "snac.h"
  12. static xs_str *random_str(void)
  13. /* just what is says in the tin */
  14. {
  15. unsigned int data[4] = {0};
  16. FILE *f;
  17. if ((f = fopen("/dev/random", "r")) != NULL) {
  18. fread(data, sizeof(data), 1, f);
  19. fclose(f);
  20. }
  21. else {
  22. data[0] = random() % 0xffffffff;
  23. data[1] = random() % 0xffffffff;
  24. data[2] = random() % 0xffffffff;
  25. data[3] = random() % 0xffffffff;
  26. }
  27. return xs_hex_enc((char *)data, sizeof(data));
  28. }
  29. int app_add(const char *id, const xs_dict *app)
  30. /* stores an app */
  31. {
  32. if (!xs_is_hex(id))
  33. return 500;
  34. int status = 201;
  35. xs *fn = xs_fmt("%s/app/", srv_basedir);
  36. FILE *f;
  37. mkdirx(fn);
  38. fn = xs_str_cat(fn, id);
  39. fn = xs_str_cat(fn, ".json");
  40. if ((f = fopen(fn, "w")) != NULL) {
  41. xs *j = xs_json_dumps_pp(app, 4);
  42. fwrite(j, strlen(j), 1, f);
  43. fclose(f);
  44. }
  45. else
  46. status = 500;
  47. return status;
  48. }
  49. xs_str *_app_fn(const char *id)
  50. {
  51. return xs_fmt("%s/app/%s.json", srv_basedir, id);
  52. }
  53. xs_dict *app_get(const char *id)
  54. /* gets an app */
  55. {
  56. if (!xs_is_hex(id))
  57. return NULL;
  58. xs *fn = _app_fn(id);
  59. xs_dict *app = NULL;
  60. FILE *f;
  61. if ((f = fopen(fn, "r")) != NULL) {
  62. xs *j = xs_readall(f);
  63. fclose(f);
  64. app = xs_json_loads(j);
  65. }
  66. return app;
  67. }
  68. int app_del(const char *id)
  69. /* deletes an app */
  70. {
  71. if (!xs_is_hex(id))
  72. return -1;
  73. xs *fn = _app_fn(id);
  74. return unlink(fn);
  75. }
  76. int token_add(const char *id, const xs_dict *token)
  77. /* stores a token */
  78. {
  79. if (!xs_is_hex(id))
  80. return 500;
  81. int status = 201;
  82. xs *fn = xs_fmt("%s/token/", srv_basedir);
  83. FILE *f;
  84. mkdirx(fn);
  85. fn = xs_str_cat(fn, id);
  86. fn = xs_str_cat(fn, ".json");
  87. if ((f = fopen(fn, "w")) != NULL) {
  88. xs *j = xs_json_dumps_pp(token, 4);
  89. fwrite(j, strlen(j), 1, f);
  90. fclose(f);
  91. }
  92. else
  93. status = 500;
  94. return status;
  95. }
  96. xs_dict *token_get(const char *id)
  97. /* gets a token */
  98. {
  99. if (!xs_is_hex(id))
  100. return NULL;
  101. xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
  102. xs_dict *token = NULL;
  103. FILE *f;
  104. if ((f = fopen(fn, "r")) != NULL) {
  105. xs *j = xs_readall(f);
  106. fclose(f);
  107. token = xs_json_loads(j);
  108. }
  109. return token;
  110. }
  111. int token_del(const char *id)
  112. /* deletes a token */
  113. {
  114. if (!xs_is_hex(id))
  115. return -1;
  116. xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
  117. return unlink(fn);
  118. }
  119. const char *login_page = ""
  120. "<!DOCTYPE html>\n"
  121. "<body><h1>%s OAuth identify</h1>\n"
  122. "<div style=\"background-color: red; color: white\">%s</div>\n"
  123. "<form method=\"post\" action=\"https:/" "/%s/%s\">\n"
  124. "<p>Login: <input type=\"text\" name=\"login\"></p>\n"
  125. "<p>Password: <input type=\"password\" name=\"passwd\"></p>\n"
  126. "<input type=\"hidden\" name=\"redir\" value=\"%s\">\n"
  127. "<input type=\"hidden\" name=\"cid\" value=\"%s\">\n"
  128. "<input type=\"hidden\" name=\"state\" value=\"%s\">\n"
  129. "<input type=\"submit\" value=\"OK\">\n"
  130. "</form><p>%s</p></body>\n"
  131. "";
  132. int oauth_get_handler(const xs_dict *req, const char *q_path,
  133. char **body, int *b_size, char **ctype)
  134. {
  135. (void)b_size;
  136. if (!xs_startswith(q_path, "/oauth/"))
  137. return 0;
  138. /* {
  139. xs *j = xs_json_dumps_pp(req, 4);
  140. printf("oauth get:\n%s\n", j);
  141. }*/
  142. int status = 404;
  143. xs_dict *msg = xs_dict_get(req, "q_vars");
  144. xs *cmd = xs_replace_n(q_path, "/oauth", "", 1);
  145. srv_debug(1, xs_fmt("oauth_get_handler %s", q_path));
  146. if (strcmp(cmd, "/authorize") == 0) {
  147. const char *cid = xs_dict_get(msg, "client_id");
  148. const char *ruri = xs_dict_get(msg, "redirect_uri");
  149. const char *rtype = xs_dict_get(msg, "response_type");
  150. const char *state = xs_dict_get(msg, "state");
  151. status = 400;
  152. if (cid && ruri && rtype && strcmp(rtype, "code") == 0) {
  153. xs *app = app_get(cid);
  154. if (app != NULL) {
  155. const char *host = xs_dict_get(srv_config, "host");
  156. if (xs_is_null(state))
  157. state = "";
  158. *body = xs_fmt(login_page, host, "", host, "oauth/x-snac-login",
  159. ruri, cid, state, USER_AGENT);
  160. *ctype = "text/html";
  161. status = 200;
  162. srv_debug(0, xs_fmt("oauth authorize: generating login page"));
  163. }
  164. else
  165. srv_debug(0, xs_fmt("oauth authorize: bad client_id %s", cid));
  166. }
  167. else
  168. srv_debug(0, xs_fmt("oauth authorize: invalid or unset arguments"));
  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. /* {
  181. xs *j = xs_json_dumps_pp(req, 4);
  182. printf("oauth post:\n%s\n", j);
  183. }*/
  184. int status = 404;
  185. char *i_ctype = xs_dict_get(req, "content-type");
  186. xs *args = NULL;
  187. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  188. args = xs_json_loads(payload);
  189. else
  190. args = xs_dup(xs_dict_get(req, "p_vars"));
  191. xs *cmd = xs_replace_n(q_path, "/oauth", "", 1);
  192. srv_debug(1, xs_fmt("oauth_post_handler %s", q_path));
  193. if (strcmp(cmd, "/x-snac-login") == 0) {
  194. const char *login = xs_dict_get(args, "login");
  195. const char *passwd = xs_dict_get(args, "passwd");
  196. const char *redir = xs_dict_get(args, "redir");
  197. const char *cid = xs_dict_get(args, "cid");
  198. const char *state = xs_dict_get(args, "state");
  199. const char *host = xs_dict_get(srv_config, "host");
  200. /* by default, generate another login form with an error */
  201. *body = xs_fmt(login_page, host, "LOGIN INCORRECT", host, "oauth/x-snac-login",
  202. redir, cid, state, USER_AGENT);
  203. *ctype = "text/html";
  204. status = 200;
  205. if (login && passwd && redir && cid) {
  206. snac snac;
  207. if (user_open(&snac, login)) {
  208. /* check the login + password */
  209. if (check_password(login, passwd, xs_dict_get(snac.config, "passwd"))) {
  210. /* success! redirect to the desired uri */
  211. xs *code = random_str();
  212. xs_free(*body);
  213. *body = xs_fmt("%s?code=%s", redir, code);
  214. status = 303;
  215. /* if there is a state, add it */
  216. if (!xs_is_null(state) && *state) {
  217. *body = xs_str_cat(*body, "&state=");
  218. *body = xs_str_cat(*body, state);
  219. }
  220. srv_log(xs_fmt("oauth x-snac-login: '%s' success, redirect to %s",
  221. login, *body));
  222. /* assign the login to the app */
  223. xs *app = app_get(cid);
  224. if (app != NULL) {
  225. app = xs_dict_set(app, "uid", login);
  226. app = xs_dict_set(app, "code", code);
  227. app_add(cid, app);
  228. }
  229. else
  230. srv_log(xs_fmt("oauth x-snac-login: error getting app %s", cid));
  231. }
  232. else
  233. srv_debug(1, xs_fmt("oauth x-snac-login: login '%s' incorrect", login));
  234. user_free(&snac);
  235. }
  236. else
  237. srv_debug(1, xs_fmt("oauth x-snac-login: bad user '%s'", login));
  238. }
  239. else
  240. srv_debug(1, xs_fmt("oauth x-snac-login: invalid or unset arguments"));
  241. }
  242. else
  243. if (strcmp(cmd, "/token") == 0) {
  244. xs *wrk = NULL;
  245. const char *gtype = xs_dict_get(args, "grant_type");
  246. const char *code = xs_dict_get(args, "code");
  247. const char *cid = xs_dict_get(args, "client_id");
  248. const char *csec = xs_dict_get(args, "client_secret");
  249. const char *ruri = xs_dict_get(args, "redirect_uri");
  250. /* FIXME: this 'scope' parameter is mandatory for the official Mastodon API,
  251. but if it's enabled, it makes it crash after some more steps, which
  252. is FAR WORSE */
  253. // const char *scope = xs_dict_get(args, "scope");
  254. const char *scope = NULL;
  255. /* no client_secret? check if it's inside an authorization header
  256. (AndStatus does it this way) */
  257. if (xs_is_null(csec)) {
  258. const char *auhdr = xs_dict_get(req, "authorization");
  259. if (!xs_is_null(auhdr) && xs_startswith(auhdr, "Basic ")) {
  260. xs *s1 = xs_replace_n(auhdr, "Basic ", "", 1);
  261. int size;
  262. xs *s2 = xs_base64_dec(s1, &size);
  263. if (!xs_is_null(s2)) {
  264. xs *l1 = xs_split(s2, ":");
  265. if (xs_list_len(l1) == 2) {
  266. wrk = xs_dup(xs_list_get(l1, 1));
  267. csec = wrk;
  268. }
  269. }
  270. }
  271. }
  272. if (gtype && code && cid && csec && ruri) {
  273. xs *app = app_get(cid);
  274. if (app == NULL) {
  275. status = 401;
  276. srv_log(xs_fmt("oauth token: invalid app %s", cid));
  277. }
  278. else
  279. if (strcmp(csec, xs_dict_get(app, "client_secret")) != 0) {
  280. status = 401;
  281. srv_log(xs_fmt("oauth token: invalid client_secret for app %s", cid));
  282. }
  283. else {
  284. xs *rsp = xs_dict_new();
  285. xs *cat = xs_number_new(time(NULL));
  286. xs *tokid = random_str();
  287. rsp = xs_dict_append(rsp, "access_token", tokid);
  288. rsp = xs_dict_append(rsp, "token_type", "Bearer");
  289. rsp = xs_dict_append(rsp, "created_at", cat);
  290. if (!xs_is_null(scope))
  291. rsp = xs_dict_append(rsp, "scope", scope);
  292. *body = xs_json_dumps_pp(rsp, 4);
  293. *ctype = "application/json";
  294. status = 200;
  295. const char *uid = xs_dict_get(app, "uid");
  296. srv_debug(0, xs_fmt("oauth token: "
  297. "successful login for %s, new token %s", uid, tokid));
  298. xs *token = xs_dict_new();
  299. token = xs_dict_append(token, "token", tokid);
  300. token = xs_dict_append(token, "client_id", cid);
  301. token = xs_dict_append(token, "client_secret", csec);
  302. token = xs_dict_append(token, "uid", uid);
  303. token = xs_dict_append(token, "code", code);
  304. token_add(tokid, token);
  305. }
  306. }
  307. else {
  308. srv_debug(0, xs_fmt("oauth token: invalid or unset arguments"));
  309. status = 400;
  310. }
  311. }
  312. else
  313. if (strcmp(cmd, "/revoke") == 0) {
  314. const char *cid = xs_dict_get(args, "client_id");
  315. const char *csec = xs_dict_get(args, "client_secret");
  316. const char *tokid = xs_dict_get(args, "token");
  317. if (cid && csec && tokid) {
  318. xs *token = token_get(tokid);
  319. *body = xs_str_new("{}");
  320. *ctype = "application/json";
  321. if (token == NULL || strcmp(csec, xs_dict_get(token, "client_secret")) != 0) {
  322. srv_debug(1, xs_fmt("oauth revoke: bad secret for token %s", tokid));
  323. status = 403;
  324. }
  325. else {
  326. token_del(tokid);
  327. srv_debug(0, xs_fmt("oauth revoke: revoked token %s", tokid));
  328. status = 200;
  329. /* also delete the app, as it serves no purpose from now on */
  330. app_del(cid);
  331. }
  332. }
  333. else {
  334. srv_debug(0, xs_fmt("oauth revoke: invalid or unset arguments"));
  335. status = 403;
  336. }
  337. }
  338. return status;
  339. }
  340. xs_str *mastoapi_id(const xs_dict *msg)
  341. /* returns a somewhat Mastodon-compatible status id */
  342. {
  343. const char *id = xs_dict_get(msg, "id");
  344. xs *md5 = xs_md5_hex(id, strlen(id));
  345. return xs_fmt("%10.0f%s", object_ctime_by_md5(md5), md5);
  346. }
  347. #define MID_TO_MD5(id) (id + 10)
  348. xs_dict *mastoapi_account(const xs_dict *actor)
  349. /* converts an ActivityPub actor to a Mastodon account */
  350. {
  351. xs_dict *acct = xs_dict_new();
  352. const char *display_name = xs_dict_get(actor, "name");
  353. if (xs_is_null(display_name) || *display_name == '\0')
  354. display_name = xs_dict_get(actor, "preferredUsername");
  355. const char *id = xs_dict_get(actor, "id");
  356. const char *pub = xs_dict_get(actor, "published");
  357. xs *acct_md5 = xs_md5_hex(id, strlen(id));
  358. acct = xs_dict_append(acct, "id", acct_md5);
  359. acct = xs_dict_append(acct, "username", xs_dict_get(actor, "preferredUsername"));
  360. acct = xs_dict_append(acct, "acct", xs_dict_get(actor, "preferredUsername"));
  361. acct = xs_dict_append(acct, "display_name", display_name);
  362. if (pub)
  363. acct = xs_dict_append(acct, "created_at", pub);
  364. else {
  365. /* unset created_at crashes Tusky, so lie like a mf */
  366. xs *date = xs_str_utctime(0, "%Y-%m-%dT%H:%M:%SZ");
  367. acct = xs_dict_append(acct, "created_at", date);
  368. }
  369. const char *note = xs_dict_get(actor, "summary");
  370. if (xs_is_null(note))
  371. note = "";
  372. acct = xs_dict_append(acct, "note", note);
  373. acct = xs_dict_append(acct, "url", id);
  374. xs *avatar = NULL;
  375. xs_dict *av = xs_dict_get(actor, "icon");
  376. if (xs_type(av) == XSTYPE_DICT)
  377. avatar = xs_dup(xs_dict_get(av, "url"));
  378. else
  379. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  380. acct = xs_dict_append(acct, "avatar", avatar);
  381. /* emojis */
  382. xs_list *p;
  383. if (!xs_is_null(p = xs_dict_get(actor, "tag"))) {
  384. xs *eml = xs_list_new();
  385. xs_dict *v;
  386. xs *t = xs_val_new(XSTYPE_TRUE);
  387. while (xs_list_iter(&p, &v)) {
  388. const char *type = xs_dict_get(v, "type");
  389. if (!xs_is_null(type) && strcmp(type, "Emoji") == 0) {
  390. const char *name = xs_dict_get(v, "name");
  391. const xs_dict *icon = xs_dict_get(v, "icon");
  392. if (!xs_is_null(name) && !xs_is_null(icon)) {
  393. const char *url = xs_dict_get(icon, "url");
  394. if (!xs_is_null(url)) {
  395. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  396. xs *d1 = xs_dict_new();
  397. d1 = xs_dict_append(d1, "shortcode", nm);
  398. d1 = xs_dict_append(d1, "url", url);
  399. d1 = xs_dict_append(d1, "static_url", url);
  400. d1 = xs_dict_append(d1, "visible_in_picker", t);
  401. eml = xs_list_append(eml, d1);
  402. }
  403. }
  404. }
  405. }
  406. acct = xs_dict_append(acct, "emojis", eml);
  407. }
  408. return acct;
  409. }
  410. xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
  411. /* converts an ActivityPub note to a Mastodon status */
  412. {
  413. xs *actor = NULL;
  414. actor_get(snac, xs_dict_get(msg, "attributedTo"), &actor);
  415. /* if the author is not here, discard */
  416. if (actor == NULL)
  417. return NULL;
  418. xs *acct = mastoapi_account(actor);
  419. /** shave the yak converting an ActivityPub Note to a Mastodon status **/
  420. xs *f = xs_val_new(XSTYPE_FALSE);
  421. xs *t = xs_val_new(XSTYPE_TRUE);
  422. xs *n = xs_val_new(XSTYPE_NULL);
  423. xs *idx = NULL;
  424. xs *ixc = NULL;
  425. char *tmp;
  426. char *id = xs_dict_get(msg, "id");
  427. xs *mid = mastoapi_id(msg);
  428. xs_dict *st = xs_dict_new();
  429. st = xs_dict_append(st, "id", mid);
  430. st = xs_dict_append(st, "uri", id);
  431. st = xs_dict_append(st, "url", id);
  432. st = xs_dict_append(st, "created_at", xs_dict_get(msg, "published"));
  433. st = xs_dict_append(st, "account", acct);
  434. st = xs_dict_append(st, "content", xs_dict_get(msg, "content"));
  435. st = xs_dict_append(st, "visibility",
  436. is_msg_public(snac, msg) ? "public" : "private");
  437. tmp = xs_dict_get(msg, "sensitive");
  438. if (xs_is_null(tmp))
  439. tmp = f;
  440. st = xs_dict_append(st, "sensitive", tmp);
  441. tmp = xs_dict_get(msg, "summary");
  442. if (xs_is_null(tmp))
  443. tmp = "";
  444. st = xs_dict_append(st, "spoiler_text", tmp);
  445. /* create the list of attachments */
  446. xs *matt = xs_list_new();
  447. xs_list *att = xs_dict_get(msg, "attachment");
  448. xs_str *aobj;
  449. while (xs_list_iter(&att, &aobj)) {
  450. const char *mtype = xs_dict_get(aobj, "mediaType");
  451. if (!xs_is_null(mtype)) {
  452. if (xs_startswith(mtype, "image/") || xs_startswith(mtype, "video/")) {
  453. xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
  454. xs *matte = xs_dict_new();
  455. matte = xs_dict_append(matte, "id", matteid);
  456. matte = xs_dict_append(matte, "type", *mtype == 'i' ? "image" : "video");
  457. matte = xs_dict_append(matte, "url", xs_dict_get(aobj, "url"));
  458. matte = xs_dict_append(matte, "preview_url", xs_dict_get(aobj, "url"));
  459. matte = xs_dict_append(matte, "remote_url", xs_dict_get(aobj, "url"));
  460. const char *name = xs_dict_get(aobj, "name");
  461. if (xs_is_null(name))
  462. name = "";
  463. matte = xs_dict_append(matte, "description", name);
  464. matt = xs_list_append(matt, matte);
  465. }
  466. }
  467. }
  468. st = xs_dict_append(st, "media_attachments", matt);
  469. {
  470. xs *ml = xs_list_new();
  471. xs *htl = xs_list_new();
  472. xs *eml = xs_list_new();
  473. xs_list *p = xs_dict_get(msg, "tag");
  474. xs_dict *v;
  475. int n = 0;
  476. while (xs_list_iter(&p, &v)) {
  477. const char *type = xs_dict_get(v, "type");
  478. if (xs_is_null(type))
  479. continue;
  480. xs *d1 = xs_dict_new();
  481. if (strcmp(type, "Mention") == 0) {
  482. const char *name = xs_dict_get(v, "name");
  483. const char *href = xs_dict_get(v, "href");
  484. if (!xs_is_null(name) && !xs_is_null(href) &&
  485. strcmp(href, snac->actor) != 0) {
  486. xs *nm = xs_strip_chars_i(xs_dup(name), "@");
  487. xs *id = xs_fmt("%d", n++);
  488. d1 = xs_dict_append(d1, "id", id);
  489. d1 = xs_dict_append(d1, "username", nm);
  490. d1 = xs_dict_append(d1, "acct", nm);
  491. d1 = xs_dict_append(d1, "url", href);
  492. ml = xs_list_append(ml, d1);
  493. }
  494. }
  495. else
  496. if (strcmp(type, "Hashtag") == 0) {
  497. const char *name = xs_dict_get(v, "name");
  498. const char *href = xs_dict_get(v, "href");
  499. if (!xs_is_null(name) && !xs_is_null(href)) {
  500. xs *nm = xs_strip_chars_i(xs_dup(name), "#");
  501. d1 = xs_dict_append(d1, "name", nm);
  502. d1 = xs_dict_append(d1, "url", href);
  503. htl = xs_list_append(htl, d1);
  504. }
  505. }
  506. else
  507. if (strcmp(type, "Emoji") == 0) {
  508. const char *name = xs_dict_get(v, "name");
  509. const xs_dict *icon = xs_dict_get(v, "icon");
  510. if (!xs_is_null(name) && !xs_is_null(icon)) {
  511. const char *url = xs_dict_get(icon, "url");
  512. if (!xs_is_null(url)) {
  513. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  514. xs *t = xs_val_new(XSTYPE_TRUE);
  515. d1 = xs_dict_append(d1, "shortcode", nm);
  516. d1 = xs_dict_append(d1, "url", url);
  517. d1 = xs_dict_append(d1, "static_url", url);
  518. d1 = xs_dict_append(d1, "visible_in_picker", t);
  519. d1 = xs_dict_append(d1, "category", "Emojis");
  520. eml = xs_list_append(eml, d1);
  521. }
  522. }
  523. }
  524. }
  525. st = xs_dict_append(st, "mentions", ml);
  526. st = xs_dict_append(st, "tags", htl);
  527. st = xs_dict_append(st, "emojis", eml);
  528. }
  529. xs_free(idx);
  530. xs_free(ixc);
  531. idx = object_likes(id);
  532. ixc = xs_number_new(xs_list_len(idx));
  533. st = xs_dict_append(st, "favourites_count", ixc);
  534. st = xs_dict_append(st, "favourited",
  535. xs_list_in(idx, snac->md5) != -1 ? t : f);
  536. xs_free(idx);
  537. xs_free(ixc);
  538. idx = object_announces(id);
  539. ixc = xs_number_new(xs_list_len(idx));
  540. st = xs_dict_append(st, "reblogs_count", ixc);
  541. st = xs_dict_append(st, "reblogged",
  542. xs_list_in(idx, snac->md5) != -1 ? t : f);
  543. xs_free(idx);
  544. xs_free(ixc);
  545. idx = object_children(id);
  546. ixc = xs_number_new(xs_list_len(idx));
  547. st = xs_dict_append(st, "replies_count", ixc);
  548. /* default in_reply_to values */
  549. st = xs_dict_append(st, "in_reply_to_id", n);
  550. st = xs_dict_append(st, "in_reply_to_account_id", n);
  551. tmp = xs_dict_get(msg, "inReplyTo");
  552. if (!xs_is_null(tmp)) {
  553. xs *irto = NULL;
  554. if (valid_status(object_get(tmp, &irto))) {
  555. xs *irt_mid = mastoapi_id(irto);
  556. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  557. char *at = NULL;
  558. if (!xs_is_null(at = xs_dict_get(irto, "attributedTo"))) {
  559. xs *at_md5 = xs_md5_hex(at, strlen(at));
  560. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  561. }
  562. }
  563. }
  564. st = xs_dict_append(st, "reblog", n);
  565. st = xs_dict_append(st, "poll", n);
  566. st = xs_dict_append(st, "card", n);
  567. st = xs_dict_append(st, "language", n);
  568. tmp = xs_dict_get(msg, "sourceContent");
  569. if (xs_is_null(tmp))
  570. tmp = "";
  571. st = xs_dict_append(st, "text", tmp);
  572. tmp = xs_dict_get(msg, "updated");
  573. if (xs_is_null(tmp))
  574. tmp = n;
  575. st = xs_dict_append(st, "edited_at", tmp);
  576. return st;
  577. }
  578. xs_dict *mastoapi_relationship(snac *snac, const char *md5)
  579. {
  580. xs_dict *rel = NULL;
  581. xs *actor_o = NULL;
  582. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  583. xs *t = xs_val_new(XSTYPE_TRUE);
  584. xs *f = xs_val_new(XSTYPE_FALSE);
  585. rel = xs_dict_new();
  586. const char *actor = xs_dict_get(actor_o, "id");
  587. rel = xs_dict_append(rel, "id", md5);
  588. rel = xs_dict_append(rel, "following",
  589. following_check(snac, actor) ? t : f);
  590. rel = xs_dict_append(rel, "showing_reblogs", t);
  591. rel = xs_dict_append(rel, "notifying", f);
  592. rel = xs_dict_append(rel, "followed_by",
  593. follower_check(snac, actor) ? t : f);
  594. rel = xs_dict_append(rel, "blocking",
  595. is_muted(snac, actor) ? t : f);
  596. rel = xs_dict_append(rel, "muting", f);
  597. rel = xs_dict_append(rel, "muting_notifications", f);
  598. rel = xs_dict_append(rel, "requested", f);
  599. rel = xs_dict_append(rel, "domain_blocking", f);
  600. rel = xs_dict_append(rel, "endorsed", f);
  601. rel = xs_dict_append(rel, "note", "");
  602. }
  603. return rel;
  604. }
  605. int process_auth_token(snac *snac, const xs_dict *req)
  606. /* processes an authorization token, if there is one */
  607. {
  608. int logged_in = 0;
  609. char *v;
  610. /* if there is an authorization field, try to validate it */
  611. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  612. xs *tokid = xs_replace_n(v, "Bearer ", "", 1);
  613. xs *token = token_get(tokid);
  614. if (token != NULL) {
  615. const char *uid = xs_dict_get(token, "uid");
  616. if (!xs_is_null(uid) && user_open(snac, uid)) {
  617. logged_in = 1;
  618. /* this counts as a 'login' */
  619. lastlog_write(snac);
  620. srv_debug(2, xs_fmt("mastoapi auth: valid token for user %s", uid));
  621. }
  622. else
  623. srv_log(xs_fmt("mastoapi auth: corrupted token %s", tokid));
  624. }
  625. else
  626. srv_log(xs_fmt("mastoapi auth: invalid token %s", tokid));
  627. }
  628. return logged_in;
  629. }
  630. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  631. char **body, int *b_size, char **ctype)
  632. {
  633. (void)b_size;
  634. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  635. return 0;
  636. srv_debug(1, xs_fmt("mastoapi_get_handler %s", q_path));
  637. /* {
  638. xs *j = xs_json_dumps_pp(req, 4);
  639. printf("mastoapi get:\n%s\n", j);
  640. }*/
  641. int status = 404;
  642. xs_dict *args = xs_dict_get(req, "q_vars");
  643. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  644. snac snac1 = {0};
  645. int logged_in = process_auth_token(&snac1, req);
  646. if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) {
  647. if (logged_in) {
  648. xs *acct = xs_dict_new();
  649. acct = xs_dict_append(acct, "id", xs_dict_get(snac1.config, "uid"));
  650. acct = xs_dict_append(acct, "username", xs_dict_get(snac1.config, "uid"));
  651. acct = xs_dict_append(acct, "acct", xs_dict_get(snac1.config, "uid"));
  652. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac1.config, "name"));
  653. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac1.config, "published"));
  654. acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio"));
  655. acct = xs_dict_append(acct, "url", snac1.actor);
  656. acct = xs_dict_append(acct, "header", "");
  657. xs *avatar = NULL;
  658. char *av = xs_dict_get(snac1.config, "avatar");
  659. if (xs_is_null(av) || *av == '\0')
  660. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  661. else
  662. avatar = xs_dup(av);
  663. acct = xs_dict_append(acct, "avatar", avatar);
  664. *body = xs_json_dumps_pp(acct, 4);
  665. *ctype = "application/json";
  666. status = 200;
  667. }
  668. else {
  669. status = 422; // "Unprocessable entity" (no login)
  670. }
  671. }
  672. else
  673. if (strcmp(cmd, "/v1/accounts/relationships") == 0) {
  674. /* find if an account is followed, blocked, etc. */
  675. /* the account to get relationships about is in args "id[]" */
  676. if (logged_in) {
  677. xs *res = xs_list_new();
  678. const char *md5 = xs_dict_get(args, "id[]");
  679. if (!xs_is_null(md5)) {
  680. xs *rel = mastoapi_relationship(&snac1, md5);
  681. if (rel != NULL)
  682. res = xs_list_append(res, rel);
  683. }
  684. *body = xs_json_dumps_pp(res, 4);
  685. *ctype = "application/json";
  686. status = 200;
  687. }
  688. else
  689. status = 422;
  690. }
  691. else
  692. if (xs_startswith(cmd, "/v1/accounts/")) {
  693. /* account-related information */
  694. xs *l = xs_split(cmd, "/");
  695. const char *uid = xs_list_get(l, 3);
  696. const char *opt = xs_list_get(l, 4);
  697. if (uid != NULL) {
  698. snac snac2;
  699. xs *out = NULL;
  700. xs *actor = NULL;
  701. /* is it a local user? */
  702. if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
  703. if (opt == NULL) {
  704. /* account information */
  705. actor = msg_actor(&snac2);
  706. out = mastoapi_account(actor);
  707. }
  708. else
  709. if (strcmp(opt, "statuses") == 0) {
  710. /* the public list of posts of a user */
  711. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  712. xs_list *p = timeline;
  713. xs_str *v;
  714. out = xs_list_new();
  715. while (xs_list_iter(&p, &v)) {
  716. xs *msg = NULL;
  717. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  718. /* add only posts by the author */
  719. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  720. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  721. xs *st = mastoapi_status(&snac2, msg);
  722. out = xs_list_append(out, st);
  723. }
  724. }
  725. }
  726. }
  727. user_free(&snac2);
  728. }
  729. else {
  730. /* try the uid as the md5 of a possibly loaded actor */
  731. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  732. if (opt == NULL) {
  733. /* account information */
  734. out = mastoapi_account(actor);
  735. }
  736. else
  737. if (strcmp(opt, "statuses") == 0) {
  738. /* we don't serve statuses of others; return the empty list */
  739. out = xs_list_new();
  740. }
  741. }
  742. }
  743. if (out != NULL) {
  744. *body = xs_json_dumps_pp(out, 4);
  745. *ctype = "application/json";
  746. status = 200;
  747. }
  748. }
  749. }
  750. else
  751. if (strcmp(cmd, "/v1/timelines/home") == 0) {
  752. /* the private timeline */
  753. if (logged_in) {
  754. const char *max_id = xs_dict_get(args, "max_id");
  755. const char *since_id = xs_dict_get(args, "since_id");
  756. const char *min_id = xs_dict_get(args, "min_id");
  757. const char *limit_s = xs_dict_get(args, "limit");
  758. int limit = 0;
  759. int cnt = 0;
  760. if (!xs_is_null(limit_s))
  761. limit = atoi(limit_s);
  762. if (limit == 0)
  763. limit = 20;
  764. xs *timeline = timeline_simple_list(&snac1, "private", 0, 256);
  765. xs *out = xs_list_new();
  766. xs_list *p = timeline;
  767. xs_str *v;
  768. while (xs_list_iter(&p, &v) && cnt < limit) {
  769. xs *msg = NULL;
  770. /* only return entries older that max_id */
  771. if (max_id) {
  772. if (strcmp(v, max_id) == 0)
  773. max_id = NULL;
  774. continue;
  775. }
  776. /* only returns entries newer than since_id */
  777. if (since_id) {
  778. if (strcmp(v, since_id) == 0)
  779. break;
  780. }
  781. /* only returns entries newer than min_id */
  782. /* what does really "Return results immediately newer than ID" mean? */
  783. if (min_id) {
  784. if (strcmp(v, min_id) == 0)
  785. break;
  786. }
  787. /* get the entry */
  788. if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
  789. continue;
  790. /* discard non-Notes */
  791. if (strcmp(xs_dict_get(msg, "type"), "Note") != 0)
  792. continue;
  793. /* discard notes from muted morons */
  794. if (is_muted(&snac1, xs_dict_get(msg, "attributedTo")))
  795. continue;
  796. /* discard hidden notes */
  797. if (is_hidden(&snac1, xs_dict_get(msg, "id")))
  798. continue;
  799. /* convert the Note into a Mastodon status */
  800. xs *st = mastoapi_status(&snac1, msg);
  801. if (st != NULL)
  802. out = xs_list_append(out, st);
  803. cnt++;
  804. }
  805. *body = xs_json_dumps_pp(out, 4);
  806. *ctype = "application/json";
  807. status = 200;
  808. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  809. }
  810. else {
  811. status = 401; // unauthorized
  812. }
  813. }
  814. else
  815. if (strcmp(cmd, "/v1/timelines/public") == 0) {
  816. /* the instance public timeline (public timelines for all users) */
  817. /* NOTE: this api call needs no authorization; but,
  818. I need a logged-in user in mastoapi_status() for
  819. is_msg_public() and the liked/boosted flags,
  820. so it will silently fail for pure public access */
  821. const char *limit_s = xs_dict_get(args, "limit");
  822. int limit = 0;
  823. int cnt = 0;
  824. if (!xs_is_null(limit_s))
  825. limit = atoi(limit_s);
  826. if (limit == 0)
  827. limit = 20;
  828. xs *timeline = timeline_instance_list(0, limit);
  829. xs *out = xs_list_new();
  830. xs_list *p = timeline;
  831. xs_str *md5;
  832. while (logged_in && xs_list_iter(&p, &md5) && cnt < limit) {
  833. xs *msg = NULL;
  834. /* get the entry */
  835. if (!valid_status(object_get_by_md5(md5, &msg)))
  836. continue;
  837. /* discard non-Notes */
  838. if (strcmp(xs_dict_get(msg, "type"), "Note") != 0)
  839. continue;
  840. /* convert the Note into a Mastodon status */
  841. xs *st = mastoapi_status(&snac1, msg);
  842. if (st != NULL) {
  843. out = xs_list_append(out, st);
  844. cnt++;
  845. }
  846. }
  847. *body = xs_json_dumps_pp(out, 4);
  848. *ctype = "application/json";
  849. status = 200;
  850. }
  851. else
  852. if (strcmp(cmd, "/v1/conversations") == 0) {
  853. /* TBD */
  854. *body = xs_dup("[]");
  855. *ctype = "application/json";
  856. status = 200;
  857. }
  858. else
  859. if (strcmp(cmd, "/v1/notifications") == 0) {
  860. if (logged_in) {
  861. xs *l = notify_list(&snac1, 0);
  862. xs *out = xs_list_new();
  863. xs_list *p = l;
  864. xs_dict *v;
  865. while (xs_list_iter(&p, &v)) {
  866. xs *noti = notify_get(&snac1, v);
  867. if (noti == NULL)
  868. continue;
  869. const char *type = xs_dict_get(noti, "type");
  870. const char *objid = xs_dict_get(noti, "objid");
  871. xs *actor = NULL;
  872. xs *entry = NULL;
  873. if (!valid_status(actor_get(&snac1, xs_dict_get(noti, "actor"), &actor)))
  874. continue;
  875. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  876. continue;
  877. if (is_hidden(&snac1, objid))
  878. continue;
  879. /* convert the type */
  880. if (strcmp(type, "Like") == 0)
  881. type = "favourite";
  882. else
  883. if (strcmp(type, "Announce") == 0)
  884. type = "reblog";
  885. else
  886. if (strcmp(type, "Follow") == 0)
  887. type = "follow";
  888. else
  889. if (strcmp(type, "Create") == 0)
  890. type = "mention";
  891. else
  892. continue;
  893. xs *mn = xs_dict_new();
  894. mn = xs_dict_append(mn, "type", type);
  895. xs *id = xs_replace(xs_dict_get(noti, "id"), ".", "");
  896. mn = xs_dict_append(mn, "id", id);
  897. mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
  898. xs *acct = mastoapi_account(actor);
  899. mn = xs_dict_append(mn, "account", acct);
  900. if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
  901. xs *st = mastoapi_status(&snac1, entry);
  902. mn = xs_dict_append(mn, "status", st);
  903. }
  904. out = xs_list_append(out, mn);
  905. }
  906. *body = xs_json_dumps_pp(out, 4);
  907. *ctype = "application/json";
  908. status = 200;
  909. }
  910. else
  911. status = 401;
  912. }
  913. else
  914. if (strcmp(cmd, "/v1/filters") == 0) {
  915. /* snac will never have filters */
  916. *body = xs_dup("[]");
  917. *ctype = "application/json";
  918. status = 200;
  919. }
  920. else
  921. if (strcmp(cmd, "/v1/favourites") == 0) {
  922. /* snac will never support a list of favourites */
  923. *body = xs_dup("[]");
  924. *ctype = "application/json";
  925. status = 200;
  926. }
  927. else
  928. if (strcmp(cmd, "/v1/bookmarks") == 0) {
  929. /* snac does not support bookmarks */
  930. *body = xs_dup("[]");
  931. *ctype = "application/json";
  932. status = 200;
  933. }
  934. else
  935. if (strcmp(cmd, "/v1/lists") == 0) {
  936. /* snac does not support lists */
  937. *body = xs_dup("[]");
  938. *ctype = "application/json";
  939. status = 200;
  940. }
  941. else
  942. if (strcmp(cmd, "/v1/scheduled_statuses") == 0) {
  943. /* snac does not scheduled notes */
  944. *body = xs_dup("[]");
  945. *ctype = "application/json";
  946. status = 200;
  947. }
  948. else
  949. if (strcmp(cmd, "/v1/follow_requests") == 0) {
  950. /* snac does not support optional follow confirmations */
  951. *body = xs_dup("[]");
  952. *ctype = "application/json";
  953. status = 200;
  954. }
  955. else
  956. if (strcmp(cmd, "/v1/announcements") == 0) {
  957. /* snac has no announcements (yet?) */
  958. *body = xs_dup("[]");
  959. *ctype = "application/json";
  960. status = 200;
  961. }
  962. else
  963. if (strcmp(cmd, "/v1/custom_emojis") == 0) {
  964. /* are you kidding me? */
  965. *body = xs_dup("[]");
  966. *ctype = "application/json";
  967. status = 200;
  968. }
  969. else
  970. if (strcmp(cmd, "/v1/instance") == 0) {
  971. /* returns an instance object */
  972. xs *ins = xs_dict_new();
  973. const char *host = xs_dict_get(srv_config, "host");
  974. ins = xs_dict_append(ins, "uri", host);
  975. ins = xs_dict_append(ins, "domain", host);
  976. ins = xs_dict_append(ins, "title", host);
  977. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  978. ins = xs_dict_append(ins, "source_url", WHAT_IS_SNAC_URL);
  979. ins = xs_dict_append(ins, "description", host);
  980. ins = xs_dict_append(ins, "short_description", host);
  981. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  982. ins = xs_dict_append(ins, "thumbnail", susie);
  983. const char *v = xs_dict_get(srv_config, "admin_email");
  984. if (xs_is_null(v) || *v == '\0')
  985. v = "admin@localhost";
  986. ins = xs_dict_append(ins, "email", v);
  987. xs *l1 = xs_list_new();
  988. ins = xs_dict_append(ins, "rules", l1);
  989. l1 = xs_list_append(l1, "en");
  990. ins = xs_dict_append(ins, "languages", l1);
  991. xs *d1 = xs_dict_new();
  992. ins = xs_dict_append(ins, "urls", d1);
  993. xs *z = xs_number_new(0);
  994. d1 = xs_dict_append(d1, "user_count", z);
  995. d1 = xs_dict_append(d1, "status_count", z);
  996. d1 = xs_dict_append(d1, "domain_count", z);
  997. ins = xs_dict_append(ins, "stats", d1);
  998. xs *f = xs_val_new(XSTYPE_FALSE);
  999. ins = xs_dict_append(ins, "registrations", f);
  1000. ins = xs_dict_append(ins, "approval_required", f);
  1001. ins = xs_dict_append(ins, "invites_enabled", f);
  1002. xs *cfg = xs_dict_new();
  1003. {
  1004. xs *d11 = xs_dict_new();
  1005. xs *mc = xs_number_new(100000);
  1006. xs *mm = xs_number_new(8);
  1007. xs *cr = xs_number_new(32);
  1008. d11 = xs_dict_append(d11, "max_characters", mc);
  1009. d11 = xs_dict_append(d11, "max_media_attachments", mm);
  1010. d11 = xs_dict_append(d11, "characters_reserved_per_url", cr);
  1011. cfg = xs_dict_append(cfg, "statuses", d11);
  1012. }
  1013. {
  1014. xs *d11 = xs_dict_new();
  1015. xs *mt = xs_list_new();
  1016. mt = xs_list_append(mt, "image/jpeg");
  1017. mt = xs_list_append(mt, "image/png");
  1018. mt = xs_list_append(mt, "image/gif");
  1019. d11 = xs_dict_append(d11, "supported_mime_types", mt);
  1020. d11 = xs_dict_append(d11, "image_size_limit", z);
  1021. d11 = xs_dict_append(d11, "image_matrix_limit", z);
  1022. d11 = xs_dict_append(d11, "video_size_limit", z);
  1023. d11 = xs_dict_append(d11, "video_matrix_limit", z);
  1024. d11 = xs_dict_append(d11, "video_frame_rate_limit", z);
  1025. cfg = xs_dict_append(cfg, "media_attachments", d11);
  1026. }
  1027. ins = xs_dict_append(ins, "configuration", cfg);
  1028. *body = xs_json_dumps_pp(ins, 4);
  1029. *ctype = "application/json";
  1030. status = 200;
  1031. }
  1032. else
  1033. if (xs_startswith(cmd, "/v1/statuses/")) {
  1034. /* information about a status */
  1035. xs *l = xs_split(cmd, "/");
  1036. const char *id = xs_list_get(l, 3);
  1037. const char *op = xs_list_get(l, 4);
  1038. if (!xs_is_null(id)) {
  1039. xs *msg = NULL;
  1040. xs *out = NULL;
  1041. /* skip the 'fake' part of the id */
  1042. id = MID_TO_MD5(id);
  1043. if (valid_status(object_get_by_md5(id, &msg))) {
  1044. if (op == NULL) {
  1045. if (!is_muted(&snac1, xs_dict_get(msg, "attributedTo"))) {
  1046. /* return the status itself */
  1047. out = mastoapi_status(&snac1, msg);
  1048. }
  1049. }
  1050. else
  1051. if (strcmp(op, "context") == 0) {
  1052. /* return ancestors and children */
  1053. xs *anc = xs_list_new();
  1054. xs *des = xs_list_new();
  1055. xs_list *p;
  1056. xs_str *v;
  1057. char pid[64];
  1058. /* build the [grand]parent list, moving up */
  1059. strncpy(pid, id, sizeof(pid));
  1060. while (object_parent(pid, pid, sizeof(pid))) {
  1061. xs *m2 = NULL;
  1062. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  1063. xs *st = mastoapi_status(&snac1, m2);
  1064. anc = xs_list_insert(anc, 0, st);
  1065. }
  1066. else
  1067. break;
  1068. }
  1069. /* build the children list */
  1070. xs *children = object_children(xs_dict_get(msg, "id"));
  1071. p = children;
  1072. while (xs_list_iter(&p, &v)) {
  1073. xs *m2 = NULL;
  1074. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  1075. xs *st = mastoapi_status(&snac1, m2);
  1076. des = xs_list_append(des, st);
  1077. }
  1078. }
  1079. out = xs_dict_new();
  1080. out = xs_dict_append(out, "ancestors", anc);
  1081. out = xs_dict_append(out, "descendants", des);
  1082. }
  1083. else
  1084. if (strcmp(op, "reblogged_by") == 0 ||
  1085. strcmp(op, "favourited_by") == 0) {
  1086. /* return the list of people who liked or boosted this */
  1087. out = xs_list_new();
  1088. xs *l = NULL;
  1089. if (op[0] == 'r')
  1090. l = object_announces(xs_dict_get(msg, "id"));
  1091. else
  1092. l = object_likes(xs_dict_get(msg, "id"));
  1093. xs_list *p = l;
  1094. xs_str *v;
  1095. while (xs_list_iter(&p, &v)) {
  1096. xs *actor2 = NULL;
  1097. if (valid_status(object_get_by_md5(v, &actor2))) {
  1098. xs *acct2 = mastoapi_account(actor2);
  1099. out = xs_list_append(out, acct2);
  1100. }
  1101. }
  1102. }
  1103. }
  1104. else
  1105. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  1106. if (out != NULL) {
  1107. *body = xs_json_dumps_pp(out, 4);
  1108. *ctype = "application/json";
  1109. status = 200;
  1110. }
  1111. }
  1112. }
  1113. else
  1114. if (strcmp(cmd, "/v1/filters") == 0) {
  1115. *body = xs_dup("[]");
  1116. *ctype = "application/json";
  1117. status = 200;
  1118. }
  1119. else
  1120. if (strcmp(cmd, "/v1/preferences") == 0) {
  1121. *body = xs_dup("{}");
  1122. *ctype = "application/json";
  1123. status = 200;
  1124. }
  1125. else
  1126. if (strcmp(cmd, "/v1/markers") == 0) {
  1127. *body = xs_dup("{}");
  1128. *ctype = "application/json";
  1129. status = 200;
  1130. }
  1131. else
  1132. if (strcmp(cmd, "/v1/followed_tags") == 0) {
  1133. *body = xs_dup("[]");
  1134. *ctype = "application/json";
  1135. status = 200;
  1136. }
  1137. else
  1138. if (strcmp(cmd, "/v2/search") == 0) {
  1139. const char *q = xs_dict_get(args, "q");
  1140. const char *type = xs_dict_get(args, "type");
  1141. const char *offset = xs_dict_get(args, "offset");
  1142. xs *acl = xs_list_new();
  1143. xs *stl = xs_list_new();
  1144. xs *htl = xs_list_new();
  1145. xs *res = xs_dict_new();
  1146. if (xs_is_null(offset) || strcmp(offset, "0") == 0) {
  1147. /* reply something only for offset 0; otherwise,
  1148. apps like Tusky keep asking again and again */
  1149. if (!xs_is_null(q) && !xs_is_null(type) && strcmp(type, "accounts") == 0) {
  1150. /* do a webfinger query */
  1151. char *actor = NULL;
  1152. char *user = NULL;
  1153. if (valid_status(webfinger_request(q, &actor, &user))) {
  1154. xs *actor_o = NULL;
  1155. if (valid_status(actor_request(&snac1, actor, &actor_o))) {
  1156. xs *acct = mastoapi_account(actor_o);
  1157. acl = xs_list_append(acl, acct);
  1158. }
  1159. }
  1160. }
  1161. }
  1162. res = xs_dict_append(res, "accounts", acl);
  1163. res = xs_dict_append(res, "statuses", stl);
  1164. res = xs_dict_append(res, "hashtags", htl);
  1165. *body = xs_json_dumps_pp(res, 4);
  1166. *ctype = "application/json";
  1167. status = 200;
  1168. }
  1169. /* user cleanup */
  1170. if (logged_in)
  1171. user_free(&snac1);
  1172. return status;
  1173. }
  1174. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  1175. const char *payload, int p_size,
  1176. char **body, int *b_size, char **ctype)
  1177. {
  1178. (void)p_size;
  1179. (void)b_size;
  1180. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1181. return 0;
  1182. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1183. /* {
  1184. xs *j = xs_json_dumps_pp(req, 4);
  1185. printf("mastoapi post:\n%s\n", j);
  1186. }*/
  1187. int status = 404;
  1188. xs *args = NULL;
  1189. char *i_ctype = xs_dict_get(req, "content-type");
  1190. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1191. args = xs_json_loads(payload);
  1192. else
  1193. args = xs_dup(xs_dict_get(req, "p_vars"));
  1194. if (args == NULL)
  1195. return 400;
  1196. /* {
  1197. xs *j = xs_json_dumps_pp(args, 4);
  1198. printf("%s\n", j);
  1199. }*/
  1200. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1201. snac snac = {0};
  1202. int logged_in = process_auth_token(&snac, req);
  1203. if (strcmp(cmd, "/v1/apps") == 0) {
  1204. const char *name = xs_dict_get(args, "client_name");
  1205. const char *ruri = xs_dict_get(args, "redirect_uris");
  1206. const char *scope = xs_dict_get(args, "scope");
  1207. if (xs_type(ruri) == XSTYPE_LIST)
  1208. ruri = xs_dict_get(ruri, 0);
  1209. if (name && ruri) {
  1210. xs *app = xs_dict_new();
  1211. xs *id = xs_replace_i(tid(0), ".", "");
  1212. xs *csec = random_str();
  1213. xs *vkey = random_str();
  1214. xs *cid = NULL;
  1215. /* pick a non-existent random cid */
  1216. for (;;) {
  1217. cid = random_str();
  1218. xs *p_app = app_get(cid);
  1219. if (p_app == NULL)
  1220. break;
  1221. xs_free(cid);
  1222. }
  1223. app = xs_dict_append(app, "name", name);
  1224. app = xs_dict_append(app, "redirect_uri", ruri);
  1225. app = xs_dict_append(app, "client_id", cid);
  1226. app = xs_dict_append(app, "client_secret", csec);
  1227. app = xs_dict_append(app, "vapid_key", vkey);
  1228. app = xs_dict_append(app, "id", id);
  1229. *body = xs_json_dumps_pp(app, 4);
  1230. *ctype = "application/json";
  1231. status = 200;
  1232. app = xs_dict_append(app, "code", "");
  1233. if (scope)
  1234. app = xs_dict_append(app, "scope", scope);
  1235. app_add(cid, app);
  1236. srv_debug(0, xs_fmt("mastoapi apps: new app %s", cid));
  1237. }
  1238. }
  1239. else
  1240. if (strcmp(cmd, "/v1/statuses") == 0) {
  1241. if (logged_in) {
  1242. /* post a new Note */
  1243. /* {
  1244. xs *j = xs_json_dumps_pp(args, 4);
  1245. printf("%s\n", j);
  1246. }*/
  1247. const char *content = xs_dict_get(args, "status");
  1248. const char *mid = xs_dict_get(args, "in_reply_to_id");
  1249. const char *visibility = xs_dict_get(args, "visibility");
  1250. const char *summary = xs_dict_get(args, "spoiler_text");
  1251. const char *media_ids = xs_dict_get(args, "media_ids");
  1252. if (xs_is_null(media_ids))
  1253. media_ids = xs_dict_get(args, "media_ids[]");
  1254. if (xs_is_null(visibility))
  1255. visibility = "public";
  1256. xs *attach_list = xs_list_new();
  1257. xs *irt = NULL;
  1258. /* is it a reply? */
  1259. if (mid != NULL) {
  1260. xs *r_msg = NULL;
  1261. const char *md5 = MID_TO_MD5(mid);
  1262. if (valid_status(object_get_by_md5(md5, &r_msg)))
  1263. irt = xs_dup(xs_dict_get(r_msg, "id"));
  1264. }
  1265. /* does it have attachments? */
  1266. if (!xs_is_null(media_ids)) {
  1267. xs *mi = NULL;
  1268. if (xs_type(media_ids) == XSTYPE_LIST)
  1269. mi = xs_dup(media_ids);
  1270. else {
  1271. mi = xs_list_new();
  1272. mi = xs_list_append(mi, media_ids);
  1273. }
  1274. xs_list *p = mi;
  1275. xs_str *v;
  1276. while (xs_list_iter(&p, &v)) {
  1277. xs *l = xs_list_new();
  1278. xs *url = xs_fmt("%s/s/%s", snac.actor, v);
  1279. xs *desc = static_get_meta(&snac, v);
  1280. l = xs_list_append(l, url);
  1281. l = xs_list_append(l, desc);
  1282. attach_list = xs_list_append(attach_list, l);
  1283. }
  1284. }
  1285. /* prepare the message */
  1286. xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
  1287. strcmp(visibility, "public") == 0 ? 0 : 1);
  1288. if (!xs_is_null(summary) && *summary) {
  1289. xs *t = xs_val_new(XSTYPE_TRUE);
  1290. msg = xs_dict_set(msg, "sensitive", t);
  1291. msg = xs_dict_set(msg, "summary", summary);
  1292. }
  1293. /* store */
  1294. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1295. /* 'Create' message */
  1296. xs *c_msg = msg_create(&snac, msg);
  1297. enqueue_message(&snac, c_msg);
  1298. timeline_touch(&snac);
  1299. /* convert to a mastodon status as a response code */
  1300. xs *st = mastoapi_status(&snac, msg);
  1301. *body = xs_json_dumps_pp(st, 4);
  1302. *ctype = "application/json";
  1303. status = 200;
  1304. }
  1305. else
  1306. status = 401;
  1307. }
  1308. else
  1309. if (xs_startswith(cmd, "/v1/statuses")) {
  1310. if (logged_in) {
  1311. /* operations on a status */
  1312. xs *l = xs_split(cmd, "/");
  1313. const char *mid = xs_list_get(l, 3);
  1314. const char *op = xs_list_get(l, 4);
  1315. if (!xs_is_null(mid)) {
  1316. xs *msg = NULL;
  1317. xs *out = NULL;
  1318. /* skip the 'fake' part of the id */
  1319. mid = MID_TO_MD5(mid);
  1320. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1321. char *id = xs_dict_get(msg, "id");
  1322. if (op == NULL) {
  1323. /* no operation (?) */
  1324. }
  1325. else
  1326. if (strcmp(op, "favourite") == 0) {
  1327. xs *n_msg = msg_admiration(&snac, id, "Like");
  1328. if (n_msg != NULL) {
  1329. enqueue_message(&snac, n_msg);
  1330. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  1331. out = mastoapi_status(&snac, msg);
  1332. }
  1333. }
  1334. else
  1335. if (strcmp(op, "unfavourite") == 0) {
  1336. /* partial support: as the original Like message
  1337. is not stored anywhere here, it's not possible
  1338. to send an Undo + Like; the only thing done here
  1339. is to delete the actor from the list of likes */
  1340. object_unadmire(id, snac.actor, 1);
  1341. }
  1342. else
  1343. if (strcmp(op, "reblog") == 0) {
  1344. xs *n_msg = msg_admiration(&snac, id, "Announce");
  1345. if (n_msg != NULL) {
  1346. enqueue_message(&snac, n_msg);
  1347. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  1348. out = mastoapi_status(&snac, msg);
  1349. }
  1350. }
  1351. else
  1352. if (strcmp(op, "unreblog") == 0) {
  1353. /* partial support: see comment in 'unfavourite' */
  1354. object_unadmire(id, snac.actor, 0);
  1355. }
  1356. else
  1357. if (strcmp(op, "bookmark") == 0) {
  1358. /* snac does not support bookmarks */
  1359. }
  1360. else
  1361. if (strcmp(op, "unbookmark") == 0) {
  1362. /* snac does not support bookmarks */
  1363. }
  1364. else
  1365. if (strcmp(op, "pin") == 0) {
  1366. /* snac does not support pinning */
  1367. }
  1368. else
  1369. if (strcmp(op, "unpin") == 0) {
  1370. /* snac does not support pinning */
  1371. }
  1372. else
  1373. if (strcmp(op, "mute") == 0) {
  1374. /* Mastodon's mute is snac's hide */
  1375. }
  1376. else
  1377. if (strcmp(op, "unmute") == 0) {
  1378. /* Mastodon's unmute is snac's unhide */
  1379. }
  1380. }
  1381. if (out != NULL) {
  1382. *body = xs_json_dumps_pp(out, 4);
  1383. *ctype = "application/json";
  1384. status = 200;
  1385. }
  1386. }
  1387. }
  1388. else
  1389. status = 401;
  1390. }
  1391. else
  1392. if (strcmp(cmd, "/v1/notifications/clear") == 0) {
  1393. if (logged_in) {
  1394. notify_clear(&snac);
  1395. timeline_touch(&snac);
  1396. *body = xs_dup("{}");
  1397. *ctype = "application/json";
  1398. status = 200;
  1399. }
  1400. else
  1401. status = 401;
  1402. }
  1403. else
  1404. if (strcmp(cmd, "/v1/push/subscription") == 0) {
  1405. /* I don't know what I'm doing */
  1406. if (logged_in) {
  1407. char *v;
  1408. xs *wpush = xs_dict_new();
  1409. wpush = xs_dict_append(wpush, "id", "1");
  1410. v = xs_dict_get(args, "data");
  1411. v = xs_dict_get(v, "alerts");
  1412. wpush = xs_dict_append(wpush, "alerts", v);
  1413. v = xs_dict_get(args, "subscription");
  1414. v = xs_dict_get(v, "endpoint");
  1415. wpush = xs_dict_append(wpush, "endpoint", v);
  1416. xs *server_key = random_str();
  1417. wpush = xs_dict_append(wpush, "server_key", server_key);
  1418. *body = xs_json_dumps_pp(wpush, 4);
  1419. *ctype = "application/json";
  1420. status = 200;
  1421. }
  1422. else
  1423. status = 401;
  1424. }
  1425. else
  1426. if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) {
  1427. if (logged_in) {
  1428. /* {
  1429. xs *j = xs_json_dumps_pp(args, 4);
  1430. printf("%s\n", j);
  1431. }*/
  1432. const xs_list *file = xs_dict_get(args, "file");
  1433. const char *desc = xs_dict_get(args, "description");
  1434. if (xs_is_null(desc))
  1435. desc = "";
  1436. status = 400;
  1437. if (xs_type(file) == XSTYPE_LIST) {
  1438. const char *fn = xs_list_get(file, 0);
  1439. if (*fn != '\0') {
  1440. char *ext = strrchr(fn, '.');
  1441. xs *hash = xs_md5_hex(fn, strlen(fn));
  1442. xs *id = xs_fmt("%s%s", hash, ext);
  1443. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1444. int fo = xs_number_get(xs_list_get(file, 1));
  1445. int fs = xs_number_get(xs_list_get(file, 2));
  1446. /* store */
  1447. static_put(&snac, id, payload + fo, fs);
  1448. static_put_meta(&snac, id, desc);
  1449. /* prepare a response */
  1450. xs *rsp = xs_dict_new();
  1451. rsp = xs_dict_append(rsp, "id", id);
  1452. rsp = xs_dict_append(rsp, "type", "image");
  1453. rsp = xs_dict_append(rsp, "url", url);
  1454. rsp = xs_dict_append(rsp, "preview_url", url);
  1455. rsp = xs_dict_append(rsp, "remote_url", url);
  1456. rsp = xs_dict_append(rsp, "description", desc);
  1457. *body = xs_json_dumps_pp(rsp, 4);
  1458. *ctype = "application/json";
  1459. status = 200;
  1460. }
  1461. }
  1462. }
  1463. else
  1464. status = 401;
  1465. }
  1466. else
  1467. if (xs_startswith(cmd, "/v1/accounts")) {
  1468. if (logged_in) {
  1469. /* account-related information */
  1470. xs *l = xs_split(cmd, "/");
  1471. const char *md5 = xs_list_get(l, 3);
  1472. const char *opt = xs_list_get(l, 4);
  1473. xs *rsp = NULL;
  1474. if (!xs_is_null(md5) && *md5) {
  1475. xs *actor_o = NULL;
  1476. if (xs_is_null(opt)) {
  1477. /* ? */
  1478. }
  1479. else
  1480. if (strcmp(opt, "follow") == 0) {
  1481. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1482. const char *actor = xs_dict_get(actor_o, "id");
  1483. xs *msg = msg_follow(&snac, actor);
  1484. if (msg != NULL) {
  1485. /* reload the actor from the message, in may be different */
  1486. actor = xs_dict_get(msg, "object");
  1487. following_add(&snac, actor, msg);
  1488. enqueue_output_by_actor(&snac, msg, actor, 0);
  1489. rsp = mastoapi_relationship(&snac, md5);
  1490. }
  1491. }
  1492. }
  1493. else
  1494. if (strcmp(opt, "unfollow") == 0) {
  1495. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1496. const char *actor = xs_dict_get(actor_o, "id");
  1497. /* get the following object */
  1498. xs *object = NULL;
  1499. if (valid_status(following_get(&snac, actor, &object))) {
  1500. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  1501. following_del(&snac, actor);
  1502. enqueue_output_by_actor(&snac, msg, actor, 0);
  1503. rsp = mastoapi_relationship(&snac, md5);
  1504. }
  1505. }
  1506. }
  1507. }
  1508. if (rsp != NULL) {
  1509. *body = xs_json_dumps_pp(rsp, 4);
  1510. *ctype = "application/json";
  1511. status = 200;
  1512. }
  1513. }
  1514. else
  1515. status = 401;
  1516. }
  1517. /* user cleanup */
  1518. if (logged_in)
  1519. user_free(&snac);
  1520. return status;
  1521. }
  1522. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  1523. const char *payload, int p_size,
  1524. char **body, int *b_size, char **ctype)
  1525. {
  1526. (void)p_size;
  1527. (void)b_size;
  1528. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1529. return 0;
  1530. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1531. /* {
  1532. xs *j = xs_json_dumps_pp(req, 4);
  1533. printf("mastoapi put:\n%s\n", j);
  1534. }*/
  1535. int status = 404;
  1536. xs *args = NULL;
  1537. char *i_ctype = xs_dict_get(req, "content-type");
  1538. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1539. args = xs_json_loads(payload);
  1540. else
  1541. args = xs_dup(xs_dict_get(req, "p_vars"));
  1542. if (args == NULL)
  1543. return 400;
  1544. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1545. snac snac = {0};
  1546. int logged_in = process_auth_token(&snac, req);
  1547. if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) {
  1548. if (logged_in) {
  1549. xs *l = xs_split(cmd, "/");
  1550. const char *stid = xs_list_get(l, 3);
  1551. if (!xs_is_null(stid)) {
  1552. const char *desc = xs_dict_get(args, "description");
  1553. /* set the image metadata */
  1554. static_put_meta(&snac, stid, desc);
  1555. /* prepare a response */
  1556. xs *rsp = xs_dict_new();
  1557. xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
  1558. rsp = xs_dict_append(rsp, "id", stid);
  1559. rsp = xs_dict_append(rsp, "type", "image");
  1560. rsp = xs_dict_append(rsp, "url", url);
  1561. rsp = xs_dict_append(rsp, "preview_url", url);
  1562. rsp = xs_dict_append(rsp, "remote_url", url);
  1563. rsp = xs_dict_append(rsp, "description", desc);
  1564. *body = xs_json_dumps_pp(rsp, 4);
  1565. *ctype = "application/json";
  1566. status = 200;
  1567. }
  1568. }
  1569. else
  1570. status = 401;
  1571. }
  1572. /* user cleanup */
  1573. if (logged_in)
  1574. user_free(&snac);
  1575. return status;
  1576. }
  1577. void mastoapi_purge(void)
  1578. {
  1579. xs *spec = xs_fmt("%s/app/" "*.json", srv_basedir);
  1580. xs *files = xs_glob(spec, 1, 0);
  1581. xs_list *p = files;
  1582. xs_str *v;
  1583. time_t mt = time(NULL) - 3600;
  1584. while (xs_list_iter(&p, &v)) {
  1585. xs *cid = xs_replace(v, ".json", "");
  1586. xs *fn = _app_fn(cid);
  1587. if (mtime(fn) < mt) {
  1588. /* get the app */
  1589. xs *app = app_get(cid);
  1590. if (app) {
  1591. /* old apps with no uid are incomplete cruft */
  1592. const char *uid = xs_dict_get(app, "uid");
  1593. if (xs_is_null(uid) || *uid == '\0') {
  1594. unlink(fn);
  1595. srv_debug(2, xs_fmt("purged %s", fn));
  1596. }
  1597. }
  1598. }
  1599. }
  1600. }
  1601. #endif /* #ifndef NO_MASTODON_API */