mastoapi.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806
  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. acct = xs_dict_append(acct, "note", xs_dict_get(actor, "summary"));
  361. acct = xs_dict_append(acct, "url", id);
  362. xs *avatar = NULL;
  363. xs_dict *av = xs_dict_get(actor, "icon");
  364. if (xs_type(av) == XSTYPE_DICT)
  365. avatar = xs_dup(xs_dict_get(av, "url"));
  366. else
  367. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  368. acct = xs_dict_append(acct, "avatar", avatar);
  369. return acct;
  370. }
  371. xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
  372. /* converts an ActivityPub note to a Mastodon status */
  373. {
  374. xs *actor = NULL;
  375. actor_get(snac, xs_dict_get(msg, "attributedTo"), &actor);
  376. /* if the author is not here, discard */
  377. if (actor == NULL)
  378. return NULL;
  379. xs *acct = mastoapi_account(actor);
  380. /** shave the yak converting an ActivityPub Note to a Mastodon status **/
  381. xs *f = xs_val_new(XSTYPE_FALSE);
  382. xs *t = xs_val_new(XSTYPE_TRUE);
  383. xs *n = xs_val_new(XSTYPE_NULL);
  384. xs *el = xs_list_new();
  385. xs *idx = NULL;
  386. xs *ixc = NULL;
  387. char *tmp;
  388. char *id = xs_dict_get(msg, "id");
  389. xs *mid = mastoapi_id(msg);
  390. xs_dict *st = xs_dict_new();
  391. st = xs_dict_append(st, "id", mid);
  392. st = xs_dict_append(st, "uri", id);
  393. st = xs_dict_append(st, "url", id);
  394. st = xs_dict_append(st, "created_at", xs_dict_get(msg, "published"));
  395. st = xs_dict_append(st, "account", acct);
  396. st = xs_dict_append(st, "content", xs_dict_get(msg, "content"));
  397. st = xs_dict_append(st, "visibility",
  398. is_msg_public(snac, msg) ? "public" : "private");
  399. tmp = xs_dict_get(msg, "sensitive");
  400. if (xs_is_null(tmp))
  401. tmp = f;
  402. st = xs_dict_append(st, "sensitive", tmp);
  403. tmp = xs_dict_get(msg, "summary");
  404. if (xs_is_null(tmp))
  405. tmp = "";
  406. st = xs_dict_append(st, "spoiler_text", tmp);
  407. /* create the list of attachments */
  408. xs *matt = xs_list_new();
  409. xs_list *att = xs_dict_get(msg, "attachment");
  410. xs_str *aobj;
  411. while (xs_list_iter(&att, &aobj)) {
  412. const char *mtype = xs_dict_get(aobj, "mediaType");
  413. if (!xs_is_null(mtype) && xs_startswith(mtype, "image/")) {
  414. xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
  415. xs *matte = xs_dict_new();
  416. matte = xs_dict_append(matte, "id", matteid);
  417. matte = xs_dict_append(matte, "type", "image");
  418. matte = xs_dict_append(matte, "url", xs_dict_get(aobj, "url"));
  419. matte = xs_dict_append(matte, "preview_url", xs_dict_get(aobj, "url"));
  420. matte = xs_dict_append(matte, "remote_url", xs_dict_get(aobj, "url"));
  421. const char *name = xs_dict_get(aobj, "name");
  422. if (xs_is_null(name))
  423. name = "";
  424. matte = xs_dict_append(matte, "description", name);
  425. matt = xs_list_append(matt, matte);
  426. }
  427. }
  428. st = xs_dict_append(st, "media_attachments", matt);
  429. st = xs_dict_append(st, "mentions", el);
  430. st = xs_dict_append(st, "tags", el);
  431. st = xs_dict_append(st, "emojis", el);
  432. xs_free(idx);
  433. xs_free(ixc);
  434. idx = object_likes(id);
  435. ixc = xs_number_new(xs_list_len(idx));
  436. st = xs_dict_append(st, "favourites_count", ixc);
  437. st = xs_dict_append(st, "favourited",
  438. xs_list_in(idx, snac->md5) != -1 ? t : f);
  439. xs_free(idx);
  440. xs_free(ixc);
  441. idx = object_announces(id);
  442. ixc = xs_number_new(xs_list_len(idx));
  443. st = xs_dict_append(st, "reblogs_count", ixc);
  444. st = xs_dict_append(st, "reblogged",
  445. xs_list_in(idx, snac->md5) != -1 ? t : f);
  446. xs_free(idx);
  447. xs_free(ixc);
  448. idx = object_children(id);
  449. ixc = xs_number_new(xs_list_len(idx));
  450. st = xs_dict_append(st, "replies_count", ixc);
  451. /* default in_reply_to values */
  452. st = xs_dict_append(st, "in_reply_to_id", n);
  453. st = xs_dict_append(st, "in_reply_to_account_id", n);
  454. tmp = xs_dict_get(msg, "inReplyTo");
  455. if (!xs_is_null(tmp)) {
  456. xs *irto = NULL;
  457. if (valid_status(object_get(tmp, &irto))) {
  458. xs *irt_mid = mastoapi_id(irto);
  459. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  460. char *at = NULL;
  461. if (!xs_is_null(at = xs_dict_get(irto, "attributedTo"))) {
  462. xs *at_md5 = xs_md5_hex(at, strlen(at));
  463. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  464. }
  465. }
  466. }
  467. st = xs_dict_append(st, "reblog", n);
  468. st = xs_dict_append(st, "poll", n);
  469. st = xs_dict_append(st, "card", n);
  470. st = xs_dict_append(st, "language", n);
  471. tmp = xs_dict_get(msg, "sourceContent");
  472. if (xs_is_null(tmp))
  473. tmp = "";
  474. st = xs_dict_append(st, "text", tmp);
  475. tmp = xs_dict_get(msg, "updated");
  476. if (xs_is_null(tmp))
  477. tmp = n;
  478. st = xs_dict_append(st, "edited_at", tmp);
  479. return st;
  480. }
  481. xs_dict *mastoapi_relationship(snac *snac, const char *md5)
  482. {
  483. xs_dict *rel = NULL;
  484. xs *actor_o = NULL;
  485. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  486. xs *t = xs_val_new(XSTYPE_TRUE);
  487. xs *f = xs_val_new(XSTYPE_FALSE);
  488. rel = xs_dict_new();
  489. const char *actor = xs_dict_get(actor_o, "id");
  490. rel = xs_dict_append(rel, "id", md5);
  491. rel = xs_dict_append(rel, "following",
  492. following_check(snac, actor) ? t : f);
  493. rel = xs_dict_append(rel, "showing_reblogs", t);
  494. rel = xs_dict_append(rel, "notifying", f);
  495. rel = xs_dict_append(rel, "followed_by",
  496. follower_check(snac, actor) ? t : f);
  497. rel = xs_dict_append(rel, "blocking",
  498. is_muted(snac, actor) ? t : f);
  499. rel = xs_dict_append(rel, "muting", f);
  500. rel = xs_dict_append(rel, "muting_notifications", f);
  501. rel = xs_dict_append(rel, "requested", f);
  502. rel = xs_dict_append(rel, "domain_blocking", f);
  503. rel = xs_dict_append(rel, "endorsed", f);
  504. rel = xs_dict_append(rel, "note", "");
  505. }
  506. return rel;
  507. }
  508. int process_auth_token(snac *snac, const xs_dict *req)
  509. /* processes an authorization token, if there is one */
  510. {
  511. int logged_in = 0;
  512. char *v;
  513. /* if there is an authorization field, try to validate it */
  514. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  515. xs *tokid = xs_replace(v, "Bearer ", "");
  516. xs *token = token_get(tokid);
  517. if (token != NULL) {
  518. const char *uid = xs_dict_get(token, "uid");
  519. if (!xs_is_null(uid) && user_open(snac, uid)) {
  520. logged_in = 1;
  521. srv_debug(2, xs_fmt("mastoapi auth: valid token for user %s", uid));
  522. }
  523. else
  524. srv_log(xs_fmt("mastoapi auth: corrupted token %s", tokid));
  525. }
  526. else
  527. srv_log(xs_fmt("mastoapi auth: invalid token %s", tokid));
  528. }
  529. return logged_in;
  530. }
  531. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  532. char **body, int *b_size, char **ctype)
  533. {
  534. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  535. return 0;
  536. srv_debug(1, xs_fmt("mastoapi_get_handler %s", q_path));
  537. /* {
  538. xs *j = xs_json_dumps_pp(req, 4);
  539. printf("mastoapi get:\n%s\n", j);
  540. }*/
  541. int status = 404;
  542. xs_dict *args = xs_dict_get(req, "q_vars");
  543. xs *cmd = xs_replace(q_path, "/api", "");
  544. snac snac1 = {0};
  545. int logged_in = process_auth_token(&snac1, req);
  546. if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) {
  547. if (logged_in) {
  548. xs *acct = xs_dict_new();
  549. acct = xs_dict_append(acct, "id", xs_dict_get(snac1.config, "uid"));
  550. acct = xs_dict_append(acct, "username", xs_dict_get(snac1.config, "uid"));
  551. acct = xs_dict_append(acct, "acct", xs_dict_get(snac1.config, "uid"));
  552. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac1.config, "name"));
  553. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac1.config, "published"));
  554. acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio"));
  555. acct = xs_dict_append(acct, "url", snac1.actor);
  556. acct = xs_dict_append(acct, "header", "");
  557. xs *avatar = NULL;
  558. char *av = xs_dict_get(snac1.config, "avatar");
  559. if (xs_is_null(av) || *av == '\0')
  560. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  561. else
  562. avatar = xs_dup(av);
  563. acct = xs_dict_append(acct, "avatar", avatar);
  564. *body = xs_json_dumps_pp(acct, 4);
  565. *ctype = "application/json";
  566. status = 200;
  567. }
  568. else {
  569. status = 422; // "Unprocessable entity" (no login)
  570. }
  571. }
  572. else
  573. if (strcmp(cmd, "/v1/accounts/relationships") == 0) {
  574. /* find if an account is followed, blocked, etc. */
  575. /* the account to get relationships about is in args "id[]" */
  576. if (logged_in) {
  577. xs *res = xs_list_new();
  578. const char *md5 = xs_dict_get(args, "id[]");
  579. if (!xs_is_null(md5)) {
  580. xs *rel = mastoapi_relationship(&snac1, md5);
  581. if (rel != NULL)
  582. res = xs_list_append(res, rel);
  583. }
  584. *body = xs_json_dumps_pp(res, 4);
  585. *ctype = "application/json";
  586. status = 200;
  587. }
  588. else
  589. status = 422;
  590. }
  591. else
  592. if (xs_startswith(cmd, "/v1/accounts/")) {
  593. /* account-related information */
  594. xs *l = xs_split(cmd, "/");
  595. const char *uid = xs_list_get(l, 3);
  596. const char *opt = xs_list_get(l, 4);
  597. if (uid != NULL) {
  598. snac snac2;
  599. xs *out = NULL;
  600. xs *actor = NULL;
  601. /* is it a local user? */
  602. if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
  603. if (opt == NULL) {
  604. /* account information */
  605. actor = msg_actor(&snac2);
  606. out = mastoapi_account(actor);
  607. }
  608. else
  609. if (strcmp(opt, "statuses") == 0) {
  610. /* the public list of posts of a user */
  611. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  612. xs_list *p = timeline;
  613. xs_str *v;
  614. out = xs_list_new();
  615. while (xs_list_iter(&p, &v)) {
  616. xs *msg = NULL;
  617. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  618. /* add only posts by the author */
  619. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  620. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  621. xs *st = mastoapi_status(&snac2, msg);
  622. out = xs_list_append(out, st);
  623. }
  624. }
  625. }
  626. }
  627. user_free(&snac2);
  628. }
  629. else {
  630. /* try the uid as the md5 of a possibly loaded actor */
  631. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  632. if (opt == NULL) {
  633. /* account information */
  634. out = mastoapi_account(actor);
  635. }
  636. else
  637. if (strcmp(opt, "statuses") == 0) {
  638. /* we don't serve statuses of others; return the empty list */
  639. out = xs_list_new();
  640. }
  641. }
  642. }
  643. if (out != NULL) {
  644. *body = xs_json_dumps_pp(out, 4);
  645. *ctype = "application/json";
  646. status = 200;
  647. }
  648. }
  649. }
  650. else
  651. if (strcmp(cmd, "/v1/timelines/home") == 0) {
  652. /* the private timeline */
  653. if (logged_in) {
  654. const char *max_id = xs_dict_get(args, "max_id");
  655. const char *since_id = xs_dict_get(args, "since_id");
  656. const char *min_id = xs_dict_get(args, "min_id");
  657. const char *limit_s = xs_dict_get(args, "limit");
  658. int limit = 0;
  659. int cnt = 0;
  660. if (!xs_is_null(limit_s))
  661. limit = atoi(limit_s);
  662. if (limit == 0)
  663. limit = 20;
  664. xs *timeline = timeline_simple_list(&snac1, "private", 0, 256);
  665. xs *out = xs_list_new();
  666. xs_list *p = timeline;
  667. xs_str *v;
  668. while (xs_list_iter(&p, &v) && cnt < limit) {
  669. xs *msg = NULL;
  670. /* only return entries older that max_id */
  671. if (max_id) {
  672. if (strcmp(v, max_id) == 0)
  673. max_id = NULL;
  674. continue;
  675. }
  676. /* only returns entries newer than since_id */
  677. if (since_id) {
  678. if (strcmp(v, since_id) == 0)
  679. break;
  680. }
  681. /* only returns entries newer than min_id */
  682. /* what does really "Return results immediately newer than ID" mean? */
  683. if (min_id) {
  684. if (strcmp(v, min_id) == 0)
  685. break;
  686. }
  687. /* get the entry */
  688. if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
  689. continue;
  690. /* discard non-Notes */
  691. if (strcmp(xs_dict_get(msg, "type"), "Note") != 0)
  692. continue;
  693. /* convert the Note into a Mastodon status */
  694. xs *st = mastoapi_status(&snac1, msg);
  695. if (st != NULL)
  696. out = xs_list_append(out, st);
  697. cnt++;
  698. }
  699. *body = xs_json_dumps_pp(out, 4);
  700. *ctype = "application/json";
  701. status = 200;
  702. {
  703. xs *j = xs_json_loads(*body);
  704. if (j == NULL) {
  705. srv_log(xs_fmt("mastoapi timeline: bad JSON"));
  706. srv_archive_error("mastoapi_timeline", "bad JSON", req, *body);
  707. }
  708. }
  709. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  710. }
  711. else {
  712. status = 401; // unauthorized
  713. }
  714. }
  715. else
  716. if (strcmp(cmd, "/v1/timelines/public") == 0) {
  717. /* the public timeline (public timelines for all users) */
  718. /* this is an ugly kludge: first users in the list get all the fame */
  719. const char *limit_s = xs_dict_get(args, "limit");
  720. int limit = 0;
  721. int cnt = 0;
  722. if (!xs_is_null(limit_s))
  723. limit = atoi(limit_s);
  724. if (limit == 0)
  725. limit = 20;
  726. xs *out = xs_list_new();
  727. xs *users = user_list();
  728. xs_list *p = users;
  729. xs_str *uid;
  730. while (xs_list_iter(&p, &uid) && cnt < limit) {
  731. snac user;
  732. if (user_open(&user, uid)) {
  733. xs *timeline = timeline_simple_list(&user, "public", 0, 4);
  734. xs_list *p2 = timeline;
  735. xs_str *v;
  736. while (xs_list_iter(&p2, &v) && cnt < limit) {
  737. xs *msg = NULL;
  738. /* get the entry */
  739. if (!valid_status(timeline_get_by_md5(&user, v, &msg)))
  740. continue;
  741. /* discard non-Notes */
  742. if (strcmp(xs_dict_get(msg, "type"), "Note") != 0)
  743. continue;
  744. /* discard entries not by this user */
  745. if (!xs_startswith(xs_dict_get(msg, "id"), user.actor))
  746. continue;
  747. /* convert the Note into a Mastodon status */
  748. xs *st = mastoapi_status(&user, msg);
  749. if (st != NULL) {
  750. out = xs_list_append(out, st);
  751. cnt++;
  752. }
  753. }
  754. user_free(&user);
  755. }
  756. }
  757. *body = xs_json_dumps_pp(out, 4);
  758. *ctype = "application/json";
  759. status = 200;
  760. }
  761. else
  762. if (strcmp(cmd, "/v1/conversations") == 0) {
  763. /* TBD */
  764. *body = xs_dup("[]");
  765. *ctype = "application/json";
  766. status = 200;
  767. }
  768. else
  769. if (strcmp(cmd, "/v1/notifications") == 0) {
  770. if (logged_in) {
  771. xs *l = notify_list(&snac1, 0);
  772. xs *out = xs_list_new();
  773. xs_list *p = l;
  774. xs_dict *v;
  775. while (xs_list_iter(&p, &v)) {
  776. xs *noti = notify_get(&snac1, v);
  777. if (noti == NULL)
  778. continue;
  779. const char *type = xs_dict_get(noti, "type");
  780. const char *objid = xs_dict_get(noti, "objid");
  781. xs *actor = NULL;
  782. xs *entry = NULL;
  783. if (!valid_status(object_get(xs_dict_get(noti, "actor"), &actor)))
  784. continue;
  785. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  786. continue;
  787. if (is_hidden(&snac1, objid))
  788. continue;
  789. /* convert the type */
  790. if (strcmp(type, "Like") == 0)
  791. type = "favourite";
  792. else
  793. if (strcmp(type, "Announce") == 0)
  794. type = "reblog";
  795. else
  796. if (strcmp(type, "Follow") == 0)
  797. type = "follow";
  798. else
  799. if (strcmp(type, "Create") == 0)
  800. type = "mention";
  801. else
  802. continue;
  803. xs *mn = xs_dict_new();
  804. mn = xs_dict_append(mn, "type", type);
  805. xs *id = xs_replace(xs_dict_get(noti, "id"), ".", "");
  806. mn = xs_dict_append(mn, "id", id);
  807. mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
  808. xs *acct = mastoapi_account(actor);
  809. mn = xs_dict_append(mn, "account", acct);
  810. if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
  811. xs *st = mastoapi_status(&snac1, entry);
  812. mn = xs_dict_append(mn, "status", st);
  813. }
  814. out = xs_list_append(out, mn);
  815. }
  816. *body = xs_json_dumps_pp(out, 4);
  817. *ctype = "application/json";
  818. status = 200;
  819. }
  820. else
  821. status = 401;
  822. }
  823. else
  824. if (strcmp(cmd, "/v1/filters") == 0) {
  825. /* snac will never have filters */
  826. *body = xs_dup("[]");
  827. *ctype = "application/json";
  828. status = 200;
  829. }
  830. else
  831. if (strcmp(cmd, "/v1/favourites") == 0) {
  832. /* snac will never support a list of favourites */
  833. *body = xs_dup("[]");
  834. *ctype = "application/json";
  835. status = 200;
  836. }
  837. else
  838. if (strcmp(cmd, "/v1/bookmarks") == 0) {
  839. /* snac does not support bookmarks */
  840. *body = xs_dup("[]");
  841. *ctype = "application/json";
  842. status = 200;
  843. }
  844. else
  845. if (strcmp(cmd, "/v1/lists") == 0) {
  846. /* snac does not support lists */
  847. *body = xs_dup("[]");
  848. *ctype = "application/json";
  849. status = 200;
  850. }
  851. else
  852. if (strcmp(cmd, "/v1/scheduled_statuses") == 0) {
  853. /* snac does not scheduled notes */
  854. *body = xs_dup("[]");
  855. *ctype = "application/json";
  856. status = 200;
  857. }
  858. else
  859. if (strcmp(cmd, "/v1/follow_requests") == 0) {
  860. /* snac does not support optional follow confirmations */
  861. *body = xs_dup("[]");
  862. *ctype = "application/json";
  863. status = 200;
  864. }
  865. else
  866. if (strcmp(cmd, "/v1/announcements") == 0) {
  867. /* snac has no announcements (yet?) */
  868. *body = xs_dup("[]");
  869. *ctype = "application/json";
  870. status = 200;
  871. }
  872. else
  873. if (strcmp(cmd, "/v1/custom_emojis") == 0) {
  874. /* are you kidding me? */
  875. *body = xs_dup("[]");
  876. *ctype = "application/json";
  877. status = 200;
  878. }
  879. else
  880. if (strcmp(cmd, "/v1/instance") == 0) {
  881. /* returns an instance object */
  882. xs *ins = xs_dict_new();
  883. const char *host = xs_dict_get(srv_config, "host");
  884. ins = xs_dict_append(ins, "uri", host);
  885. ins = xs_dict_append(ins, "domain", host);
  886. ins = xs_dict_append(ins, "title", host);
  887. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  888. ins = xs_dict_append(ins, "source_url", "https:/" "/comam.es/what-is-snac");
  889. ins = xs_dict_append(ins, "description", host);
  890. ins = xs_dict_append(ins, "short_description", host);
  891. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  892. ins = xs_dict_append(ins, "thumbnail", susie);
  893. const char *v = xs_dict_get(srv_config, "admin_email");
  894. if (xs_is_null(v) || *v == '\0')
  895. v = "admin@localhost";
  896. ins = xs_dict_append(ins, "email", v);
  897. xs *l1 = xs_list_new();
  898. ins = xs_dict_append(ins, "rules", l1);
  899. l1 = xs_list_append(l1, "en");
  900. ins = xs_dict_append(ins, "languages", l1);
  901. xs *d1 = xs_dict_new();
  902. ins = xs_dict_append(ins, "urls", d1);
  903. ins = xs_dict_append(ins, "configuration", d1);
  904. xs *z = xs_number_new(0);
  905. d1 = xs_dict_append(d1, "user_count", z);
  906. d1 = xs_dict_append(d1, "status_count", z);
  907. d1 = xs_dict_append(d1, "domain_count", z);
  908. ins = xs_dict_append(ins, "stats", d1);
  909. xs *f = xs_val_new(XSTYPE_FALSE);
  910. ins = xs_dict_append(ins, "registrations", f);
  911. ins = xs_dict_append(ins, "approval_required", f);
  912. ins = xs_dict_append(ins, "invites_enabled", f);
  913. /* {
  914. snac snac;
  915. user_open(&snac, "test1");
  916. xs *actor = msg_actor(&snac);
  917. xs *acc = mastoapi_account(actor);
  918. ins = xs_dict_append(ins, "contact_account", acc);
  919. user_free(&snac);
  920. }*/
  921. *body = xs_json_dumps_pp(ins, 4);
  922. *ctype = "application/json";
  923. status = 200;
  924. }
  925. else
  926. if (xs_startswith(cmd, "/v1/statuses/")) {
  927. /* operations on a status */
  928. xs *l = xs_split(cmd, "/");
  929. const char *id = xs_list_get(l, 3);
  930. const char *op = xs_list_get(l, 4);
  931. if (!xs_is_null(id)) {
  932. xs *msg = NULL;
  933. xs *out = NULL;
  934. /* skip the 'fake' part of the id */
  935. id = MID_TO_MD5(id);
  936. if (valid_status(timeline_get_by_md5(&snac1, id, &msg))) {
  937. if (op == NULL) {
  938. /* return the status itself */
  939. out = mastoapi_status(&snac1, msg);
  940. }
  941. else
  942. if (strcmp(op, "context") == 0) {
  943. /* return ancestors and children */
  944. xs *anc = xs_list_new();
  945. xs *des = xs_list_new();
  946. xs_list *p;
  947. xs_str *v;
  948. char pid[64];
  949. /* build the [grand]parent list, moving up */
  950. strncpy(pid, id, sizeof(pid));
  951. while (object_parent(pid, pid, sizeof(pid))) {
  952. xs *m2 = NULL;
  953. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  954. xs *st = mastoapi_status(&snac1, m2);
  955. anc = xs_list_insert(anc, 0, st);
  956. }
  957. else
  958. break;
  959. }
  960. /* build the children list */
  961. xs *children = object_children(xs_dict_get(msg, "id"));
  962. p = children;
  963. while (xs_list_iter(&p, &v)) {
  964. xs *m2 = NULL;
  965. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  966. xs *st = mastoapi_status(&snac1, m2);
  967. des = xs_list_append(des, st);
  968. }
  969. }
  970. out = xs_dict_new();
  971. out = xs_dict_append(out, "ancestors", anc);
  972. out = xs_dict_append(out, "descendants", des);
  973. }
  974. else
  975. if (strcmp(op, "reblogged_by") == 0 ||
  976. strcmp(op, "favourited_by") == 0) {
  977. /* return the list of people who liked or boosted this */
  978. out = xs_list_new();
  979. xs *l = NULL;
  980. if (op[0] == 'r')
  981. l = object_announces(xs_dict_get(msg, "id"));
  982. else
  983. l = object_likes(xs_dict_get(msg, "id"));
  984. xs_list *p = l;
  985. xs_str *v;
  986. while (xs_list_iter(&p, &v)) {
  987. xs *actor2 = NULL;
  988. if (valid_status(object_get_by_md5(v, &actor2))) {
  989. xs *acct2 = mastoapi_account(actor2);
  990. out = xs_list_append(out, acct2);
  991. }
  992. }
  993. }
  994. }
  995. else
  996. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  997. if (out != NULL) {
  998. *body = xs_json_dumps_pp(out, 4);
  999. *ctype = "application/json";
  1000. status = 200;
  1001. }
  1002. }
  1003. }
  1004. else
  1005. if (strcmp(cmd, "/v1/filters") == 0) {
  1006. *body = xs_dup("[]");
  1007. *ctype = "application/json";
  1008. status = 200;
  1009. }
  1010. else
  1011. if (strcmp(cmd, "/v1/preferences") == 0) {
  1012. *body = xs_dup("{}");
  1013. *ctype = "application/json";
  1014. status = 200;
  1015. }
  1016. else
  1017. if (strcmp(cmd, "/v1/markers") == 0) {
  1018. *body = xs_dup("{}");
  1019. *ctype = "application/json";
  1020. status = 200;
  1021. }
  1022. else
  1023. if (strcmp(cmd, "/v1/followed_tags") == 0) {
  1024. *body = xs_dup("[]");
  1025. *ctype = "application/json";
  1026. status = 200;
  1027. }
  1028. else
  1029. if (strcmp(cmd, "/v2/search") == 0) {
  1030. const char *q = xs_dict_get(args, "q");
  1031. const char *type = xs_dict_get(args, "type");
  1032. const char *offset = xs_dict_get(args, "offset");
  1033. xs *acl = xs_list_new();
  1034. xs *stl = xs_list_new();
  1035. xs *htl = xs_list_new();
  1036. xs *res = xs_dict_new();
  1037. if (xs_is_null(offset) || strcmp(offset, "0") == 0) {
  1038. /* reply something only for offset 0; otherwise,
  1039. apps like Tusky keep asking again and again */
  1040. if (!xs_is_null(q) && strcmp(type, "accounts") == 0) {
  1041. /* do a webfinger query */
  1042. char *actor = NULL;
  1043. char *user = NULL;
  1044. if (valid_status(webfinger_request(q, &actor, &user))) {
  1045. xs *actor_o = NULL;
  1046. if (valid_status(actor_request(&snac1, actor, &actor_o))) {
  1047. xs *acct = mastoapi_account(actor_o);
  1048. acl = xs_list_append(acl, acct);
  1049. }
  1050. }
  1051. }
  1052. }
  1053. res = xs_dict_append(res, "accounts", acl);
  1054. res = xs_dict_append(res, "statuses", stl);
  1055. res = xs_dict_append(res, "hashtags", htl);
  1056. *body = xs_json_dumps_pp(res, 4);
  1057. *ctype = "application/json";
  1058. status = 200;
  1059. }
  1060. /* user cleanup */
  1061. if (logged_in)
  1062. user_free(&snac1);
  1063. return status;
  1064. }
  1065. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  1066. const char *payload, int p_size,
  1067. char **body, int *b_size, char **ctype)
  1068. {
  1069. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1070. return 0;
  1071. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1072. /* {
  1073. xs *j = xs_json_dumps_pp(req, 4);
  1074. printf("mastoapi post:\n%s\n", j);
  1075. }*/
  1076. int status = 404;
  1077. xs *args = NULL;
  1078. char *i_ctype = xs_dict_get(req, "content-type");
  1079. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1080. args = xs_json_loads(payload);
  1081. else
  1082. args = xs_dup(xs_dict_get(req, "p_vars"));
  1083. if (args == NULL)
  1084. return 400;
  1085. /* {
  1086. xs *j = xs_json_dumps_pp(args, 4);
  1087. printf("%s\n", j);
  1088. }*/
  1089. xs *cmd = xs_replace(q_path, "/api", "");
  1090. snac snac = {0};
  1091. int logged_in = process_auth_token(&snac, req);
  1092. if (strcmp(cmd, "/v1/apps") == 0) {
  1093. const char *name = xs_dict_get(args, "client_name");
  1094. const char *ruri = xs_dict_get(args, "redirect_uris");
  1095. const char *scope = xs_dict_get(args, "scope");
  1096. if (xs_type(ruri) == XSTYPE_LIST)
  1097. ruri = xs_dict_get(ruri, 0);
  1098. if (name && ruri) {
  1099. xs *app = xs_dict_new();
  1100. xs *id = xs_replace_i(tid(0), ".", "");
  1101. xs *cid = random_str();
  1102. xs *csec = random_str();
  1103. xs *vkey = random_str();
  1104. app = xs_dict_append(app, "name", name);
  1105. app = xs_dict_append(app, "redirect_uri", ruri);
  1106. app = xs_dict_append(app, "client_id", cid);
  1107. app = xs_dict_append(app, "client_secret", csec);
  1108. app = xs_dict_append(app, "vapid_key", vkey);
  1109. app = xs_dict_append(app, "id", id);
  1110. *body = xs_json_dumps_pp(app, 4);
  1111. *ctype = "application/json";
  1112. status = 200;
  1113. app = xs_dict_append(app, "code", "");
  1114. if (scope)
  1115. app = xs_dict_append(app, "scope", scope);
  1116. app_add(cid, app);
  1117. srv_debug(0, xs_fmt("mastoapi apps: new app %s", cid));
  1118. }
  1119. }
  1120. else
  1121. if (strcmp(cmd, "/v1/statuses") == 0) {
  1122. if (logged_in) {
  1123. /* post a new Note */
  1124. /* {
  1125. xs *j = xs_json_dumps_pp(args, 4);
  1126. printf("%s\n", j);
  1127. }*/
  1128. const char *content = xs_dict_get(args, "status");
  1129. const char *mid = xs_dict_get(args, "in_reply_to_id");
  1130. const char *visibility = xs_dict_get(args, "visibility");
  1131. const char *summary = xs_dict_get(args, "spoiler_text");
  1132. const char *media_ids = xs_dict_get(args, "media_ids");
  1133. if (xs_is_null(media_ids))
  1134. media_ids = xs_dict_get(args, "media_ids[]");
  1135. xs *attach_list = xs_list_new();
  1136. xs *irt = NULL;
  1137. /* is it a reply? */
  1138. if (mid != NULL) {
  1139. xs *r_msg = NULL;
  1140. const char *md5 = MID_TO_MD5(mid);
  1141. if (valid_status(object_get_by_md5(md5, &r_msg)))
  1142. irt = xs_dup(xs_dict_get(r_msg, "id"));
  1143. }
  1144. /* does it have attachments? */
  1145. if (!xs_is_null(media_ids)) {
  1146. xs *mi = NULL;
  1147. if (xs_type(media_ids) == XSTYPE_LIST)
  1148. mi = xs_dup(media_ids);
  1149. else {
  1150. mi = xs_list_new();
  1151. mi = xs_list_append(mi, media_ids);
  1152. }
  1153. xs_list *p = mi;
  1154. xs_str *v;
  1155. while (xs_list_iter(&p, &v)) {
  1156. xs *l = xs_list_new();
  1157. xs *url = xs_fmt("%s/s/%s", snac.actor, v);
  1158. xs *desc = static_get_meta(&snac, v);
  1159. l = xs_list_append(l, url);
  1160. l = xs_list_append(l, desc);
  1161. attach_list = xs_list_append(attach_list, l);
  1162. }
  1163. }
  1164. /* prepare the message */
  1165. xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
  1166. strcmp(visibility, "public") == 0 ? 0 : 1);
  1167. if (!xs_is_null(summary) && *summary) {
  1168. xs *t = xs_val_new(XSTYPE_TRUE);
  1169. msg = xs_dict_set(msg, "sensitive", t);
  1170. msg = xs_dict_set(msg, "summary", summary);
  1171. }
  1172. /* store */
  1173. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1174. /* 'Create' message */
  1175. xs *c_msg = msg_create(&snac, msg);
  1176. enqueue_message(&snac, c_msg);
  1177. timeline_touch(&snac);
  1178. /* convert to a mastodon status as a response code */
  1179. xs *st = mastoapi_status(&snac, msg);
  1180. *body = xs_json_dumps_pp(st, 4);
  1181. *ctype = "application/json";
  1182. status = 200;
  1183. }
  1184. else
  1185. status = 401;
  1186. }
  1187. else
  1188. if (xs_startswith(cmd, "/v1/statuses")) {
  1189. if (logged_in) {
  1190. /* operations on a status */
  1191. xs *l = xs_split(cmd, "/");
  1192. const char *mid = xs_list_get(l, 3);
  1193. const char *op = xs_list_get(l, 4);
  1194. if (!xs_is_null(mid)) {
  1195. xs *msg = NULL;
  1196. xs *out = NULL;
  1197. /* skip the 'fake' part of the id */
  1198. mid = MID_TO_MD5(mid);
  1199. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1200. char *id = xs_dict_get(msg, "id");
  1201. if (op == NULL) {
  1202. /* no operation (?) */
  1203. }
  1204. else
  1205. if (strcmp(op, "favourite") == 0) {
  1206. xs *n_msg = msg_admiration(&snac, id, "Like");
  1207. if (n_msg != NULL) {
  1208. enqueue_message(&snac, n_msg);
  1209. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  1210. out = mastoapi_status(&snac, msg);
  1211. }
  1212. }
  1213. else
  1214. if (strcmp(op, "unfavourite") == 0) {
  1215. /* snac does not support Undo+Like */
  1216. }
  1217. else
  1218. if (strcmp(op, "reblog") == 0) {
  1219. xs *n_msg = msg_admiration(&snac, id, "Announce");
  1220. if (n_msg != NULL) {
  1221. enqueue_message(&snac, n_msg);
  1222. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  1223. out = mastoapi_status(&snac, msg);
  1224. }
  1225. }
  1226. else
  1227. if (strcmp(op, "unreblog") == 0) {
  1228. /* snac does not support Undo+Announce */
  1229. }
  1230. else
  1231. if (strcmp(op, "bookmark") == 0) {
  1232. /* snac does not support bookmarks */
  1233. }
  1234. else
  1235. if (strcmp(op, "unbookmark") == 0) {
  1236. /* snac does not support bookmarks */
  1237. }
  1238. else
  1239. if (strcmp(op, "pin") == 0) {
  1240. /* snac does not support pinning */
  1241. }
  1242. else
  1243. if (strcmp(op, "unpin") == 0) {
  1244. /* snac does not support pinning */
  1245. }
  1246. else
  1247. if (strcmp(op, "mute") == 0) {
  1248. /* Mastodon's mute is snac's hide */
  1249. }
  1250. else
  1251. if (strcmp(op, "unmute") == 0) {
  1252. /* Mastodon's unmute is snac's unhide */
  1253. }
  1254. }
  1255. if (out != NULL) {
  1256. *body = xs_json_dumps_pp(out, 4);
  1257. *ctype = "application/json";
  1258. status = 200;
  1259. }
  1260. }
  1261. }
  1262. else
  1263. status = 401;
  1264. }
  1265. else
  1266. if (strcmp(cmd, "/v1/notifications/clear") == 0) {
  1267. if (logged_in) {
  1268. notify_clear(&snac);
  1269. timeline_touch(&snac);
  1270. *body = xs_dup("{}");
  1271. *ctype = "application/json";
  1272. status = 200;
  1273. }
  1274. else
  1275. status = 401;
  1276. }
  1277. else
  1278. if (strcmp(cmd, "/v1/push/subscription") == 0) {
  1279. /* I don't know what I'm doing */
  1280. if (logged_in) {
  1281. char *v;
  1282. xs *wpush = xs_dict_new();
  1283. wpush = xs_dict_append(wpush, "id", "1");
  1284. v = xs_dict_get(args, "data");
  1285. v = xs_dict_get(v, "alerts");
  1286. wpush = xs_dict_append(wpush, "alerts", v);
  1287. v = xs_dict_get(args, "subscription");
  1288. v = xs_dict_get(v, "endpoint");
  1289. wpush = xs_dict_append(wpush, "endpoint", v);
  1290. xs *server_key = random_str();
  1291. wpush = xs_dict_append(wpush, "server_key", server_key);
  1292. *body = xs_json_dumps_pp(wpush, 4);
  1293. *ctype = "application/json";
  1294. status = 200;
  1295. }
  1296. else
  1297. status = 401;
  1298. }
  1299. else
  1300. if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) {
  1301. if (logged_in) {
  1302. /* {
  1303. xs *j = xs_json_dumps_pp(args, 4);
  1304. printf("%s\n", j);
  1305. }*/
  1306. const xs_list *file = xs_dict_get(args, "file");
  1307. const char *desc = xs_dict_get(args, "description");
  1308. if (xs_is_null(desc))
  1309. desc = "";
  1310. status = 400;
  1311. if (xs_type(file) == XSTYPE_LIST) {
  1312. const char *fn = xs_list_get(file, 0);
  1313. if (*fn != '\0') {
  1314. char *ext = strrchr(fn, '.');
  1315. xs *hash = xs_md5_hex(fn, strlen(fn));
  1316. xs *id = xs_fmt("%s%s", hash, ext);
  1317. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1318. int fo = xs_number_get(xs_list_get(file, 1));
  1319. int fs = xs_number_get(xs_list_get(file, 2));
  1320. /* store */
  1321. static_put(&snac, id, payload + fo, fs);
  1322. static_put_meta(&snac, id, desc);
  1323. /* prepare a response */
  1324. xs *rsp = xs_dict_new();
  1325. rsp = xs_dict_append(rsp, "id", id);
  1326. rsp = xs_dict_append(rsp, "type", "image");
  1327. rsp = xs_dict_append(rsp, "url", url);
  1328. rsp = xs_dict_append(rsp, "preview_url", url);
  1329. rsp = xs_dict_append(rsp, "remote_url", url);
  1330. rsp = xs_dict_append(rsp, "description", desc);
  1331. *body = xs_json_dumps_pp(rsp, 4);
  1332. *ctype = "application/json";
  1333. status = 200;
  1334. }
  1335. }
  1336. }
  1337. else
  1338. status = 401;
  1339. }
  1340. else
  1341. if (xs_startswith(cmd, "/v1/accounts")) {
  1342. if (logged_in) {
  1343. /* account-related information */
  1344. xs *l = xs_split(cmd, "/");
  1345. const char *md5 = xs_list_get(l, 3);
  1346. const char *opt = xs_list_get(l, 4);
  1347. xs *rsp = NULL;
  1348. if (!xs_is_null(md5) && *md5) {
  1349. xs *actor_o = NULL;
  1350. if (xs_is_null(opt)) {
  1351. /* ? */
  1352. }
  1353. else
  1354. if (strcmp(opt, "follow") == 0) {
  1355. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1356. const char *actor = xs_dict_get(actor_o, "id");
  1357. xs *msg = msg_follow(&snac, actor);
  1358. if (msg != NULL) {
  1359. /* reload the actor from the message, in may be different */
  1360. actor = xs_dict_get(msg, "object");
  1361. following_add(&snac, actor, msg);
  1362. enqueue_output_by_actor(&snac, msg, actor, 0);
  1363. rsp = mastoapi_relationship(&snac, md5);
  1364. }
  1365. }
  1366. }
  1367. else
  1368. if (strcmp(opt, "unfollow") == 0) {
  1369. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1370. const char *actor = xs_dict_get(actor_o, "id");
  1371. /* get the following object */
  1372. xs *object = NULL;
  1373. if (valid_status(following_get(&snac, actor, &object))) {
  1374. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  1375. following_del(&snac, actor);
  1376. enqueue_output_by_actor(&snac, msg, actor, 0);
  1377. rsp = mastoapi_relationship(&snac, md5);
  1378. }
  1379. }
  1380. }
  1381. }
  1382. if (rsp != NULL) {
  1383. *body = xs_json_dumps_pp(rsp, 4);
  1384. *ctype = "application/json";
  1385. status = 200;
  1386. }
  1387. }
  1388. else
  1389. status = 401;
  1390. }
  1391. /* user cleanup */
  1392. if (logged_in)
  1393. user_free(&snac);
  1394. return status;
  1395. }
  1396. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  1397. const char *payload, int p_size,
  1398. char **body, int *b_size, char **ctype)
  1399. {
  1400. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1401. return 0;
  1402. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1403. /* {
  1404. xs *j = xs_json_dumps_pp(req, 4);
  1405. printf("mastoapi put:\n%s\n", j);
  1406. }*/
  1407. int status = 404;
  1408. xs *args = NULL;
  1409. char *i_ctype = xs_dict_get(req, "content-type");
  1410. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1411. args = xs_json_loads(payload);
  1412. else
  1413. args = xs_dup(xs_dict_get(req, "p_vars"));
  1414. if (args == NULL)
  1415. return 400;
  1416. xs *cmd = xs_replace(q_path, "/api", "");
  1417. snac snac = {0};
  1418. int logged_in = process_auth_token(&snac, req);
  1419. if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) {
  1420. if (logged_in) {
  1421. xs *l = xs_split(cmd, "/");
  1422. const char *stid = xs_list_get(l, 3);
  1423. if (!xs_is_null(stid)) {
  1424. const char *desc = xs_dict_get(args, "description");
  1425. /* set the image metadata */
  1426. static_put_meta(&snac, stid, desc);
  1427. /* prepare a response */
  1428. xs *rsp = xs_dict_new();
  1429. xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
  1430. rsp = xs_dict_append(rsp, "id", stid);
  1431. rsp = xs_dict_append(rsp, "type", "image");
  1432. rsp = xs_dict_append(rsp, "url", url);
  1433. rsp = xs_dict_append(rsp, "preview_url", url);
  1434. rsp = xs_dict_append(rsp, "remote_url", url);
  1435. rsp = xs_dict_append(rsp, "description", desc);
  1436. *body = xs_json_dumps_pp(rsp, 4);
  1437. *ctype = "application/json";
  1438. status = 200;
  1439. }
  1440. }
  1441. else
  1442. status = 401;
  1443. }
  1444. /* user cleanup */
  1445. if (logged_in)
  1446. user_free(&snac);
  1447. return status;
  1448. }
  1449. #endif /* #ifndef NO_MASTODON_API */