mastoapi.c 66 KB

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