mastoapi.c 62 KB

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