mastoapi.c 61 KB

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