mastoapi.c 65 KB

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