mastoapi.c 62 KB

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