mastoapi.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616
  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(xs_dict_get(msg, "attributedTo"), &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 = xs_dict_get(irto, "attributedTo"))) {
  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", fd2);
  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 = xs_dict_get(msg, "attributedTo");
  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 = xs_dict_get(msg, "attributedTo");
  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, xs_dict_get(msg, "attributedTo"))) {
  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. }
  1503. else
  1504. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  1505. if (out != NULL) {
  1506. *body = xs_json_dumps(out, 4);
  1507. *ctype = "application/json";
  1508. status = 200;
  1509. }
  1510. }
  1511. }
  1512. else
  1513. status = 401;
  1514. }
  1515. else
  1516. if (strcmp(cmd, "/v1/preferences") == 0) { /** **/
  1517. *body = xs_dup("{}");
  1518. *ctype = "application/json";
  1519. status = 200;
  1520. }
  1521. else
  1522. if (strcmp(cmd, "/v1/markers") == 0) { /** **/
  1523. *body = xs_dup("{}");
  1524. *ctype = "application/json";
  1525. status = 200;
  1526. }
  1527. else
  1528. if (strcmp(cmd, "/v1/followed_tags") == 0) { /** **/
  1529. *body = xs_dup("[]");
  1530. *ctype = "application/json";
  1531. status = 200;
  1532. }
  1533. else
  1534. if (strcmp(cmd, "/v2/search") == 0) { /** **/
  1535. if (logged_in) {
  1536. const char *q = xs_dict_get(args, "q");
  1537. const char *type = xs_dict_get(args, "type");
  1538. const char *offset = xs_dict_get(args, "offset");
  1539. xs *acl = xs_list_new();
  1540. xs *stl = xs_list_new();
  1541. xs *htl = xs_list_new();
  1542. xs *res = xs_dict_new();
  1543. if (xs_is_null(offset) || strcmp(offset, "0") == 0) {
  1544. /* reply something only for offset 0; otherwise,
  1545. apps like Tusky keep asking again and again */
  1546. if (!xs_is_null(q) && !xs_is_null(type) && strcmp(type, "accounts") == 0) {
  1547. /* do a webfinger query */
  1548. char *actor = NULL;
  1549. char *user = NULL;
  1550. if (valid_status(webfinger_request(q, &actor, &user) && actor)) {
  1551. xs *actor_o = NULL;
  1552. if (valid_status(actor_request(&snac1, actor, &actor_o))) {
  1553. xs *acct = mastoapi_account(actor_o);
  1554. acl = xs_list_append(acl, acct);
  1555. }
  1556. }
  1557. }
  1558. }
  1559. res = xs_dict_append(res, "accounts", acl);
  1560. res = xs_dict_append(res, "statuses", stl);
  1561. res = xs_dict_append(res, "hashtags", htl);
  1562. *body = xs_json_dumps(res, 4);
  1563. *ctype = "application/json";
  1564. status = 200;
  1565. }
  1566. else
  1567. status = 401;
  1568. }
  1569. /* user cleanup */
  1570. if (logged_in)
  1571. user_free(&snac1);
  1572. srv_debug(1, xs_fmt("mastoapi_get_handler %s %d", q_path, status));
  1573. return status;
  1574. }
  1575. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  1576. const char *payload, int p_size,
  1577. char **body, int *b_size, char **ctype)
  1578. {
  1579. (void)p_size;
  1580. (void)b_size;
  1581. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1582. return 0;
  1583. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1584. int status = 404;
  1585. xs *args = NULL;
  1586. char *i_ctype = xs_dict_get(req, "content-type");
  1587. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1588. args = xs_json_loads(payload);
  1589. else
  1590. args = xs_dup(xs_dict_get(req, "p_vars"));
  1591. if (args == NULL)
  1592. return 400;
  1593. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1594. snac snac = {0};
  1595. int logged_in = process_auth_token(&snac, req);
  1596. if (strcmp(cmd, "/v1/apps") == 0) { /** **/
  1597. const char *name = xs_dict_get(args, "client_name");
  1598. const char *ruri = xs_dict_get(args, "redirect_uris");
  1599. const char *scope = xs_dict_get(args, "scope");
  1600. if (xs_type(ruri) == XSTYPE_LIST)
  1601. ruri = xs_dict_get(ruri, 0);
  1602. if (name && ruri) {
  1603. xs *app = xs_dict_new();
  1604. xs *id = xs_replace_i(tid(0), ".", "");
  1605. xs *csec = random_str();
  1606. xs *vkey = random_str();
  1607. xs *cid = NULL;
  1608. /* pick a non-existent random cid */
  1609. for (;;) {
  1610. cid = random_str();
  1611. xs *p_app = app_get(cid);
  1612. if (p_app == NULL)
  1613. break;
  1614. xs_free(cid);
  1615. }
  1616. app = xs_dict_append(app, "name", name);
  1617. app = xs_dict_append(app, "redirect_uri", ruri);
  1618. app = xs_dict_append(app, "client_id", cid);
  1619. app = xs_dict_append(app, "client_secret", csec);
  1620. app = xs_dict_append(app, "vapid_key", vkey);
  1621. app = xs_dict_append(app, "id", id);
  1622. *body = xs_json_dumps(app, 4);
  1623. *ctype = "application/json";
  1624. status = 200;
  1625. app = xs_dict_append(app, "code", "");
  1626. if (scope)
  1627. app = xs_dict_append(app, "scope", scope);
  1628. app_add(cid, app);
  1629. srv_debug(1, xs_fmt("mastoapi apps: new app %s", cid));
  1630. }
  1631. }
  1632. else
  1633. if (strcmp(cmd, "/v1/statuses") == 0) { /** **/
  1634. if (logged_in) {
  1635. /* post a new Note */
  1636. const char *content = xs_dict_get(args, "status");
  1637. const char *mid = xs_dict_get(args, "in_reply_to_id");
  1638. const char *visibility = xs_dict_get(args, "visibility");
  1639. const char *summary = xs_dict_get(args, "spoiler_text");
  1640. const char *media_ids = xs_dict_get(args, "media_ids");
  1641. if (xs_is_null(media_ids))
  1642. media_ids = xs_dict_get(args, "media_ids[]");
  1643. if (xs_is_null(media_ids))
  1644. media_ids = xs_dict_get(args, "media_ids");
  1645. if (xs_is_null(visibility))
  1646. visibility = "public";
  1647. xs *attach_list = xs_list_new();
  1648. xs *irt = NULL;
  1649. /* is it a reply? */
  1650. if (mid != NULL) {
  1651. xs *r_msg = NULL;
  1652. const char *md5 = MID_TO_MD5(mid);
  1653. if (valid_status(object_get_by_md5(md5, &r_msg)))
  1654. irt = xs_dup(xs_dict_get(r_msg, "id"));
  1655. }
  1656. /* does it have attachments? */
  1657. if (!xs_is_null(media_ids)) {
  1658. xs *mi = NULL;
  1659. if (xs_type(media_ids) == XSTYPE_LIST)
  1660. mi = xs_dup(media_ids);
  1661. else {
  1662. mi = xs_list_new();
  1663. mi = xs_list_append(mi, media_ids);
  1664. }
  1665. xs_list *p = mi;
  1666. xs_str *v;
  1667. while (xs_list_iter(&p, &v)) {
  1668. xs *l = xs_list_new();
  1669. xs *url = xs_fmt("%s/s/%s", snac.actor, v);
  1670. xs *desc = static_get_meta(&snac, v);
  1671. l = xs_list_append(l, url);
  1672. l = xs_list_append(l, desc);
  1673. attach_list = xs_list_append(attach_list, l);
  1674. }
  1675. }
  1676. /* prepare the message */
  1677. xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
  1678. strcmp(visibility, "public") == 0 ? 0 : 1);
  1679. if (!xs_is_null(summary) && *summary) {
  1680. msg = xs_dict_set(msg, "sensitive", xs_stock_true);
  1681. msg = xs_dict_set(msg, "summary", summary);
  1682. }
  1683. /* store */
  1684. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1685. /* 'Create' message */
  1686. xs *c_msg = msg_create(&snac, msg);
  1687. enqueue_message(&snac, c_msg);
  1688. timeline_touch(&snac);
  1689. /* convert to a mastodon status as a response code */
  1690. xs *st = mastoapi_status(&snac, msg);
  1691. *body = xs_json_dumps(st, 4);
  1692. *ctype = "application/json";
  1693. status = 200;
  1694. }
  1695. else
  1696. status = 401;
  1697. }
  1698. else
  1699. if (xs_startswith(cmd, "/v1/statuses")) { /** **/
  1700. if (logged_in) {
  1701. /* operations on a status */
  1702. xs *l = xs_split(cmd, "/");
  1703. const char *mid = xs_list_get(l, 3);
  1704. const char *op = xs_list_get(l, 4);
  1705. if (!xs_is_null(mid)) {
  1706. xs *msg = NULL;
  1707. xs *out = NULL;
  1708. /* skip the 'fake' part of the id */
  1709. mid = MID_TO_MD5(mid);
  1710. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1711. char *id = xs_dict_get(msg, "id");
  1712. if (op == NULL) {
  1713. /* no operation (?) */
  1714. }
  1715. else
  1716. if (strcmp(op, "favourite") == 0) { /** **/
  1717. xs *n_msg = msg_admiration(&snac, id, "Like");
  1718. if (n_msg != NULL) {
  1719. enqueue_message(&snac, n_msg);
  1720. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  1721. out = mastoapi_status(&snac, msg);
  1722. }
  1723. }
  1724. else
  1725. if (strcmp(op, "unfavourite") == 0) { /** **/
  1726. /* partial support: as the original Like message
  1727. is not stored anywhere here, it's not possible
  1728. to send an Undo + Like; the only thing done here
  1729. is to delete the actor from the list of likes */
  1730. object_unadmire(id, snac.actor, 1);
  1731. }
  1732. else
  1733. if (strcmp(op, "reblog") == 0) { /** **/
  1734. xs *n_msg = msg_admiration(&snac, id, "Announce");
  1735. if (n_msg != NULL) {
  1736. enqueue_message(&snac, n_msg);
  1737. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  1738. out = mastoapi_status(&snac, msg);
  1739. }
  1740. }
  1741. else
  1742. if (strcmp(op, "unreblog") == 0) { /** **/
  1743. /* partial support: see comment in 'unfavourite' */
  1744. object_unadmire(id, snac.actor, 0);
  1745. }
  1746. else
  1747. if (strcmp(op, "bookmark") == 0) { /** **/
  1748. /* snac does not support bookmarks */
  1749. }
  1750. else
  1751. if (strcmp(op, "unbookmark") == 0) { /** **/
  1752. /* snac does not support bookmarks */
  1753. }
  1754. else
  1755. if (strcmp(op, "pin") == 0) { /** **/
  1756. /* pin this message */
  1757. if (pin(&snac, id))
  1758. out = mastoapi_status(&snac, msg);
  1759. else
  1760. status = 422;
  1761. }
  1762. else
  1763. if (strcmp(op, "unpin") == 0) { /** **/
  1764. /* unpin this message */
  1765. unpin(&snac, id);
  1766. out = mastoapi_status(&snac, msg);
  1767. }
  1768. else
  1769. if (strcmp(op, "mute") == 0) { /** **/
  1770. /* Mastodon's mute is snac's hide */
  1771. }
  1772. else
  1773. if (strcmp(op, "unmute") == 0) { /** **/
  1774. /* Mastodon's unmute is snac's unhide */
  1775. }
  1776. }
  1777. if (out != NULL) {
  1778. *body = xs_json_dumps(out, 4);
  1779. *ctype = "application/json";
  1780. status = 200;
  1781. }
  1782. }
  1783. }
  1784. else
  1785. status = 401;
  1786. }
  1787. else
  1788. if (strcmp(cmd, "/v1/notifications/clear") == 0) { /** **/
  1789. if (logged_in) {
  1790. notify_clear(&snac);
  1791. timeline_touch(&snac);
  1792. *body = xs_dup("{}");
  1793. *ctype = "application/json";
  1794. status = 200;
  1795. }
  1796. else
  1797. status = 401;
  1798. }
  1799. else
  1800. if (strcmp(cmd, "/v1/push/subscription") == 0) { /** **/
  1801. /* I don't know what I'm doing */
  1802. if (logged_in) {
  1803. char *v;
  1804. xs *wpush = xs_dict_new();
  1805. wpush = xs_dict_append(wpush, "id", "1");
  1806. v = xs_dict_get(args, "data");
  1807. v = xs_dict_get(v, "alerts");
  1808. wpush = xs_dict_append(wpush, "alerts", v);
  1809. v = xs_dict_get(args, "subscription");
  1810. v = xs_dict_get(v, "endpoint");
  1811. wpush = xs_dict_append(wpush, "endpoint", v);
  1812. xs *server_key = random_str();
  1813. wpush = xs_dict_append(wpush, "server_key", server_key);
  1814. *body = xs_json_dumps(wpush, 4);
  1815. *ctype = "application/json";
  1816. status = 200;
  1817. }
  1818. else
  1819. status = 401;
  1820. }
  1821. else
  1822. if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { /** **/
  1823. if (logged_in) {
  1824. const xs_list *file = xs_dict_get(args, "file");
  1825. const char *desc = xs_dict_get(args, "description");
  1826. if (xs_is_null(desc))
  1827. desc = "";
  1828. status = 400;
  1829. if (xs_type(file) == XSTYPE_LIST) {
  1830. const char *fn = xs_list_get(file, 0);
  1831. if (*fn != '\0') {
  1832. char *ext = strrchr(fn, '.');
  1833. xs *hash = xs_md5_hex(fn, strlen(fn));
  1834. xs *id = xs_fmt("%s%s", hash, ext);
  1835. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1836. int fo = xs_number_get(xs_list_get(file, 1));
  1837. int fs = xs_number_get(xs_list_get(file, 2));
  1838. /* store */
  1839. static_put(&snac, id, payload + fo, fs);
  1840. static_put_meta(&snac, id, desc);
  1841. /* prepare a response */
  1842. xs *rsp = xs_dict_new();
  1843. rsp = xs_dict_append(rsp, "id", id);
  1844. rsp = xs_dict_append(rsp, "type", "image");
  1845. rsp = xs_dict_append(rsp, "url", url);
  1846. rsp = xs_dict_append(rsp, "preview_url", url);
  1847. rsp = xs_dict_append(rsp, "remote_url", url);
  1848. rsp = xs_dict_append(rsp, "description", desc);
  1849. *body = xs_json_dumps(rsp, 4);
  1850. *ctype = "application/json";
  1851. status = 200;
  1852. }
  1853. }
  1854. }
  1855. else
  1856. status = 401;
  1857. }
  1858. else
  1859. if (xs_startswith(cmd, "/v1/accounts")) { /** **/
  1860. if (logged_in) {
  1861. /* account-related information */
  1862. xs *l = xs_split(cmd, "/");
  1863. const char *md5 = xs_list_get(l, 3);
  1864. const char *opt = xs_list_get(l, 4);
  1865. xs *rsp = NULL;
  1866. if (!xs_is_null(md5) && *md5) {
  1867. xs *actor_o = NULL;
  1868. if (xs_is_null(opt)) {
  1869. /* ? */
  1870. }
  1871. else
  1872. if (strcmp(opt, "follow") == 0) { /** **/
  1873. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1874. const char *actor = xs_dict_get(actor_o, "id");
  1875. xs *msg = msg_follow(&snac, actor);
  1876. if (msg != NULL) {
  1877. /* reload the actor from the message, in may be different */
  1878. actor = xs_dict_get(msg, "object");
  1879. following_add(&snac, actor, msg);
  1880. enqueue_output_by_actor(&snac, msg, actor, 0);
  1881. rsp = mastoapi_relationship(&snac, md5);
  1882. }
  1883. }
  1884. }
  1885. else
  1886. if (strcmp(opt, "unfollow") == 0) { /** **/
  1887. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1888. const char *actor = xs_dict_get(actor_o, "id");
  1889. /* get the following object */
  1890. xs *object = NULL;
  1891. if (valid_status(following_get(&snac, actor, &object))) {
  1892. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  1893. following_del(&snac, actor);
  1894. enqueue_output_by_actor(&snac, msg, actor, 0);
  1895. rsp = mastoapi_relationship(&snac, md5);
  1896. }
  1897. }
  1898. }
  1899. else
  1900. if (strcmp(opt, "block") == 0) { /** **/
  1901. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1902. const char *actor = xs_dict_get(actor_o, "id");
  1903. mute(&snac, actor);
  1904. rsp = mastoapi_relationship(&snac, md5);
  1905. }
  1906. }
  1907. else
  1908. if (strcmp(opt, "unblock") == 0) { /** **/
  1909. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1910. const char *actor = xs_dict_get(actor_o, "id");
  1911. unmute(&snac, actor);
  1912. rsp = mastoapi_relationship(&snac, md5);
  1913. }
  1914. }
  1915. }
  1916. if (rsp != NULL) {
  1917. *body = xs_json_dumps(rsp, 4);
  1918. *ctype = "application/json";
  1919. status = 200;
  1920. }
  1921. }
  1922. else
  1923. status = 401;
  1924. }
  1925. else
  1926. if (xs_startswith(cmd, "/v1/polls")) { /** **/
  1927. if (logged_in) {
  1928. /* operations on a status */
  1929. xs *l = xs_split(cmd, "/");
  1930. const char *mid = xs_list_get(l, 3);
  1931. const char *op = xs_list_get(l, 4);
  1932. if (!xs_is_null(mid)) {
  1933. xs *msg = NULL;
  1934. xs *out = NULL;
  1935. /* skip the 'fake' part of the id */
  1936. mid = MID_TO_MD5(mid);
  1937. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1938. const char *id = xs_dict_get(msg, "id");
  1939. const char *atto = xs_dict_get(msg, "attributedTo");
  1940. xs_list *opts = xs_dict_get(msg, "oneOf");
  1941. if (opts == NULL)
  1942. opts = xs_dict_get(msg, "anyOf");
  1943. if (op == NULL) {
  1944. }
  1945. else
  1946. if (strcmp(op, "votes") == 0) {
  1947. xs_list *choices = xs_dict_get(args, "choices[]");
  1948. if (xs_is_null(choices))
  1949. choices = xs_dict_get(args, "choices");
  1950. if (xs_type(choices) == XSTYPE_LIST) {
  1951. xs_str *v;
  1952. while (xs_list_iter(&choices, &v)) {
  1953. int io = atoi(v);
  1954. const xs_dict *o = xs_list_get(opts, io);
  1955. if (o) {
  1956. const char *name = xs_dict_get(o, "name");
  1957. xs *msg = msg_note(&snac, "", atto, (char *)id, NULL, 1);
  1958. msg = xs_dict_append(msg, "name", name);
  1959. xs *c_msg = msg_create(&snac, msg);
  1960. enqueue_message(&snac, c_msg);
  1961. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1962. }
  1963. }
  1964. out = mastoapi_poll(&snac, msg);
  1965. }
  1966. }
  1967. }
  1968. if (out != NULL) {
  1969. *body = xs_json_dumps(out, 4);
  1970. *ctype = "application/json";
  1971. status = 200;
  1972. }
  1973. }
  1974. }
  1975. else
  1976. status = 401;
  1977. }
  1978. /* user cleanup */
  1979. if (logged_in)
  1980. user_free(&snac);
  1981. return status;
  1982. }
  1983. int mastoapi_delete_handler(const xs_dict *req, const char *q_path,
  1984. char **body, int *b_size, char **ctype) {
  1985. (void)req;
  1986. (void)body;
  1987. (void)b_size;
  1988. (void)ctype;
  1989. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1990. return 0;
  1991. srv_debug(1, xs_fmt("mastoapi_delete_handler %s", q_path));
  1992. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1993. if (xs_startswith(cmd, "/v1/push/subscription") || xs_startswith(cmd, "/v2/push/subscription")) { /** **/
  1994. // pretend we deleted it, since it doesn't exist anyway
  1995. return 200;
  1996. }
  1997. return 0;
  1998. }
  1999. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  2000. const char *payload, int p_size,
  2001. char **body, int *b_size, char **ctype)
  2002. {
  2003. (void)p_size;
  2004. (void)b_size;
  2005. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  2006. return 0;
  2007. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  2008. int status = 404;
  2009. xs *args = NULL;
  2010. char *i_ctype = xs_dict_get(req, "content-type");
  2011. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  2012. args = xs_json_loads(payload);
  2013. else
  2014. args = xs_dup(xs_dict_get(req, "p_vars"));
  2015. if (args == NULL)
  2016. return 400;
  2017. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2018. snac snac = {0};
  2019. int logged_in = process_auth_token(&snac, req);
  2020. if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) { /** **/
  2021. if (logged_in) {
  2022. xs *l = xs_split(cmd, "/");
  2023. const char *stid = xs_list_get(l, 3);
  2024. if (!xs_is_null(stid)) {
  2025. const char *desc = xs_dict_get(args, "description");
  2026. /* set the image metadata */
  2027. static_put_meta(&snac, stid, desc);
  2028. /* prepare a response */
  2029. xs *rsp = xs_dict_new();
  2030. xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
  2031. rsp = xs_dict_append(rsp, "id", stid);
  2032. rsp = xs_dict_append(rsp, "type", "image");
  2033. rsp = xs_dict_append(rsp, "url", url);
  2034. rsp = xs_dict_append(rsp, "preview_url", url);
  2035. rsp = xs_dict_append(rsp, "remote_url", url);
  2036. rsp = xs_dict_append(rsp, "description", desc);
  2037. *body = xs_json_dumps(rsp, 4);
  2038. *ctype = "application/json";
  2039. status = 200;
  2040. }
  2041. }
  2042. else
  2043. status = 401;
  2044. }
  2045. /* user cleanup */
  2046. if (logged_in)
  2047. user_free(&snac);
  2048. return status;
  2049. }
  2050. void mastoapi_purge(void)
  2051. {
  2052. xs *spec = xs_fmt("%s/app/" "*.json", srv_basedir);
  2053. xs *files = xs_glob(spec, 1, 0);
  2054. xs_list *p = files;
  2055. xs_str *v;
  2056. time_t mt = time(NULL) - 3600;
  2057. while (xs_list_iter(&p, &v)) {
  2058. xs *cid = xs_replace(v, ".json", "");
  2059. xs *fn = _app_fn(cid);
  2060. if (mtime(fn) < mt) {
  2061. /* get the app */
  2062. xs *app = app_get(cid);
  2063. if (app) {
  2064. /* old apps with no uid are incomplete cruft */
  2065. const char *uid = xs_dict_get(app, "uid");
  2066. if (xs_is_null(uid) || *uid == '\0') {
  2067. unlink(fn);
  2068. srv_debug(2, xs_fmt("purged %s", fn));
  2069. }
  2070. }
  2071. }
  2072. }
  2073. }
  2074. #endif /* #ifndef NO_MASTODON_API */