mastoapi.c 84 KB

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