mastoapi.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 grunfink / MIT license */
  3. #ifndef NO_MASTODON_API
  4. #include "xs.h"
  5. #include "xs_encdec.h"
  6. #include "xs_openssl.h"
  7. #include "xs_json.h"
  8. #include "xs_io.h"
  9. #include "xs_time.h"
  10. #include "xs_glob.h"
  11. #include "snac.h"
  12. static xs_str *random_str(void)
  13. /* just what is says in the tin */
  14. {
  15. unsigned int data[4] = {0};
  16. FILE *f;
  17. if ((f = fopen("/dev/random", "r")) != NULL) {
  18. fread(data, sizeof(data), 1, f);
  19. fclose(f);
  20. }
  21. else {
  22. data[0] = random() % 0xffffffff;
  23. data[1] = random() % 0xffffffff;
  24. data[2] = random() % 0xffffffff;
  25. data[3] = random() % 0xffffffff;
  26. }
  27. return xs_hex_enc((char *)data, sizeof(data));
  28. }
  29. int app_add(const char *id, const xs_dict *app)
  30. /* stores an app */
  31. {
  32. if (!xs_is_hex(id))
  33. return 500;
  34. int status = 201;
  35. xs *fn = xs_fmt("%s/app/", srv_basedir);
  36. FILE *f;
  37. mkdirx(fn);
  38. fn = xs_str_cat(fn, id);
  39. fn = xs_str_cat(fn, ".json");
  40. if ((f = fopen(fn, "w")) != NULL) {
  41. xs *j = xs_json_dumps_pp(app, 4);
  42. fwrite(j, strlen(j), 1, f);
  43. fclose(f);
  44. }
  45. else
  46. status = 500;
  47. return status;
  48. }
  49. xs_str *_app_fn(const char *id)
  50. {
  51. return xs_fmt("%s/app/%s.json", srv_basedir, id);
  52. }
  53. xs_dict *app_get(const char *id)
  54. /* gets an app */
  55. {
  56. if (!xs_is_hex(id))
  57. return NULL;
  58. xs *fn = _app_fn(id);
  59. xs_dict *app = NULL;
  60. FILE *f;
  61. if ((f = fopen(fn, "r")) != NULL) {
  62. xs *j = xs_readall(f);
  63. fclose(f);
  64. app = xs_json_loads(j);
  65. }
  66. return app;
  67. }
  68. int app_del(const char *id)
  69. /* deletes an app */
  70. {
  71. if (!xs_is_hex(id))
  72. return -1;
  73. xs *fn = _app_fn(id);
  74. return unlink(fn);
  75. }
  76. int token_add(const char *id, const xs_dict *token)
  77. /* stores a token */
  78. {
  79. if (!xs_is_hex(id))
  80. return 500;
  81. int status = 201;
  82. xs *fn = xs_fmt("%s/token/", srv_basedir);
  83. FILE *f;
  84. mkdirx(fn);
  85. fn = xs_str_cat(fn, id);
  86. fn = xs_str_cat(fn, ".json");
  87. if ((f = fopen(fn, "w")) != NULL) {
  88. xs *j = xs_json_dumps_pp(token, 4);
  89. fwrite(j, strlen(j), 1, f);
  90. fclose(f);
  91. }
  92. else
  93. status = 500;
  94. return status;
  95. }
  96. xs_dict *token_get(const char *id)
  97. /* gets a token */
  98. {
  99. if (!xs_is_hex(id))
  100. return NULL;
  101. xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
  102. xs_dict *token = NULL;
  103. FILE *f;
  104. if ((f = fopen(fn, "r")) != NULL) {
  105. xs *j = xs_readall(f);
  106. fclose(f);
  107. token = xs_json_loads(j);
  108. }
  109. return token;
  110. }
  111. int token_del(const char *id)
  112. /* deletes a token */
  113. {
  114. if (!xs_is_hex(id))
  115. return -1;
  116. xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
  117. return unlink(fn);
  118. }
  119. const char *login_page = ""
  120. "<!DOCTYPE html>\n"
  121. "<body><h1>%s OAuth identify</h1>\n"
  122. "<div style=\"background-color: red; color: white\">%s</div>\n"
  123. "<form method=\"post\" action=\"https:/" "/%s/oauth/x-snac-login\">\n"
  124. "<p>Login: <input type=\"text\" name=\"login\"></p>\n"
  125. "<p>Password: <input type=\"password\" name=\"passwd\"></p>\n"
  126. "<input type=\"hidden\" name=\"redir\" value=\"%s\">\n"
  127. "<input type=\"hidden\" name=\"cid\" value=\"%s\">\n"
  128. "<input type=\"hidden\" name=\"state\" value=\"%s\">\n"
  129. "<input type=\"submit\" value=\"OK\">\n"
  130. "</form><p>%s</p></body>\n"
  131. "";
  132. int oauth_get_handler(const xs_dict *req, const char *q_path,
  133. char **body, int *b_size, char **ctype)
  134. {
  135. if (!xs_startswith(q_path, "/oauth/"))
  136. return 0;
  137. /* {
  138. xs *j = xs_json_dumps_pp(req, 4);
  139. printf("oauth get:\n%s\n", j);
  140. }*/
  141. int status = 404;
  142. xs_dict *msg = xs_dict_get(req, "q_vars");
  143. xs *cmd = xs_replace(q_path, "/oauth", "");
  144. srv_debug(1, xs_fmt("oauth_get_handler %s", q_path));
  145. if (strcmp(cmd, "/authorize") == 0) {
  146. const char *cid = xs_dict_get(msg, "client_id");
  147. const char *ruri = xs_dict_get(msg, "redirect_uri");
  148. const char *rtype = xs_dict_get(msg, "response_type");
  149. const char *state = xs_dict_get(msg, "state");
  150. status = 400;
  151. if (cid && ruri && rtype && strcmp(rtype, "code") == 0) {
  152. xs *app = app_get(cid);
  153. if (app != NULL) {
  154. const char *host = xs_dict_get(srv_config, "host");
  155. if (xs_is_null(state))
  156. state = "";
  157. *body = xs_fmt(login_page, host, "", host, ruri, cid, state, USER_AGENT);
  158. *ctype = "text/html";
  159. status = 200;
  160. srv_debug(0, xs_fmt("oauth authorize: generating login page"));
  161. }
  162. else
  163. srv_debug(0, xs_fmt("oauth authorize: bad client_id %s", cid));
  164. }
  165. else
  166. srv_debug(0, xs_fmt("oauth authorize: invalid or unset arguments"));
  167. }
  168. return status;
  169. }
  170. int oauth_post_handler(const xs_dict *req, const char *q_path,
  171. const char *payload, int p_size,
  172. char **body, int *b_size, char **ctype)
  173. {
  174. if (!xs_startswith(q_path, "/oauth/"))
  175. return 0;
  176. /* {
  177. xs *j = xs_json_dumps_pp(req, 4);
  178. printf("oauth post:\n%s\n", j);
  179. }*/
  180. int status = 404;
  181. char *i_ctype = xs_dict_get(req, "content-type");
  182. xs *args = NULL;
  183. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  184. args = xs_json_loads(payload);
  185. else
  186. args = xs_dup(xs_dict_get(req, "p_vars"));
  187. xs *cmd = xs_replace(q_path, "/oauth", "");
  188. srv_debug(1, xs_fmt("oauth_post_handler %s", q_path));
  189. if (strcmp(cmd, "/x-snac-login") == 0) {
  190. const char *login = xs_dict_get(args, "login");
  191. const char *passwd = xs_dict_get(args, "passwd");
  192. const char *redir = xs_dict_get(args, "redir");
  193. const char *cid = xs_dict_get(args, "cid");
  194. const char *state = xs_dict_get(args, "state");
  195. const char *host = xs_dict_get(srv_config, "host");
  196. /* by default, generate another login form with an error */
  197. *body = xs_fmt(login_page, host, "LOGIN INCORRECT", host, redir, cid, state, USER_AGENT);
  198. *ctype = "text/html";
  199. status = 200;
  200. if (login && passwd && redir && cid) {
  201. snac snac;
  202. if (user_open(&snac, login)) {
  203. /* check the login + password */
  204. if (check_password(login, passwd,
  205. xs_dict_get(snac.config, "passwd"))) {
  206. /* success! redirect to the desired uri */
  207. xs *code = random_str();
  208. xs_free(*body);
  209. *body = xs_fmt("%s?code=%s", redir, code);
  210. status = 303;
  211. /* if there is a state, add it */
  212. if (!xs_is_null(state) && *state) {
  213. *body = xs_str_cat(*body, "&state=");
  214. *body = xs_str_cat(*body, state);
  215. }
  216. srv_log(xs_fmt("oauth x-snac-login: '%s' success, redirect to %s",
  217. login, *body));
  218. /* assign the login to the app */
  219. xs *app = app_get(cid);
  220. if (app != NULL) {
  221. app = xs_dict_set(app, "uid", login);
  222. app = xs_dict_set(app, "code", code);
  223. app_add(cid, app);
  224. }
  225. else
  226. srv_log(xs_fmt("oauth x-snac-login: error getting app %s", cid));
  227. }
  228. else
  229. srv_debug(1, xs_fmt("oauth x-snac-login: login '%s' incorrect", login));
  230. user_free(&snac);
  231. }
  232. else
  233. srv_debug(1, xs_fmt("oauth x-snac-login: bad user '%s'", login));
  234. }
  235. else
  236. srv_debug(1, xs_fmt("oauth x-snac-login: invalid or unset arguments"));
  237. }
  238. else
  239. if (strcmp(cmd, "/token") == 0) {
  240. xs *wrk = NULL;
  241. const char *gtype = xs_dict_get(args, "grant_type");
  242. const char *code = xs_dict_get(args, "code");
  243. const char *cid = xs_dict_get(args, "client_id");
  244. const char *csec = xs_dict_get(args, "client_secret");
  245. const char *ruri = xs_dict_get(args, "redirect_uri");
  246. /* FIXME: this 'scope' parameter is mandatory for the official Mastodon API,
  247. but if it's enabled, it makes it crash after some more steps, which
  248. is FAR WORSE */
  249. // const char *scope = xs_dict_get(args, "scope");
  250. const char *scope = NULL;
  251. /* no client_secret? check if it's inside an authorization header
  252. (AndStatus does it this way) */
  253. if (xs_is_null(csec)) {
  254. const char *auhdr = xs_dict_get(req, "authorization");
  255. if (!xs_is_null(auhdr) && xs_startswith(auhdr, "Basic ")) {
  256. xs *s1 = xs_replace(auhdr, "Basic ", "");
  257. int size;
  258. xs *s2 = xs_base64_dec(s1, &size);
  259. if (!xs_is_null(s2)) {
  260. xs *l1 = xs_split(s2, ":");
  261. if (xs_list_len(l1) == 2) {
  262. wrk = xs_dup(xs_list_get(l1, 1));
  263. csec = wrk;
  264. }
  265. }
  266. }
  267. }
  268. if (gtype && code && cid && csec && ruri) {
  269. xs *app = app_get(cid);
  270. if (app == NULL) {
  271. status = 401;
  272. srv_log(xs_fmt("oauth token: invalid app %s", cid));
  273. }
  274. else
  275. if (strcmp(csec, xs_dict_get(app, "client_secret")) != 0) {
  276. status = 401;
  277. srv_log(xs_fmt("oauth token: invalid client_secret for app %s", cid));
  278. }
  279. else {
  280. xs *rsp = xs_dict_new();
  281. xs *cat = xs_number_new(time(NULL));
  282. xs *tokid = random_str();
  283. rsp = xs_dict_append(rsp, "access_token", tokid);
  284. rsp = xs_dict_append(rsp, "token_type", "Bearer");
  285. rsp = xs_dict_append(rsp, "created_at", cat);
  286. if (!xs_is_null(scope))
  287. rsp = xs_dict_append(rsp, "scope", scope);
  288. *body = xs_json_dumps_pp(rsp, 4);
  289. *ctype = "application/json";
  290. status = 200;
  291. const char *uid = xs_dict_get(app, "uid");
  292. srv_debug(0, xs_fmt("oauth token: "
  293. "successful login for %s, new token %s", uid, tokid));
  294. xs *token = xs_dict_new();
  295. token = xs_dict_append(token, "token", tokid);
  296. token = xs_dict_append(token, "client_id", cid);
  297. token = xs_dict_append(token, "client_secret", csec);
  298. token = xs_dict_append(token, "uid", uid);
  299. token = xs_dict_append(token, "code", code);
  300. token_add(tokid, token);
  301. }
  302. }
  303. else {
  304. srv_debug(0, xs_fmt("oauth token: invalid or unset arguments"));
  305. status = 400;
  306. }
  307. }
  308. else
  309. if (strcmp(cmd, "/revoke") == 0) {
  310. const char *cid = xs_dict_get(args, "client_id");
  311. const char *csec = xs_dict_get(args, "client_secret");
  312. const char *tokid = xs_dict_get(args, "token");
  313. if (cid && csec && tokid) {
  314. xs *token = token_get(tokid);
  315. *body = xs_str_new("{}");
  316. *ctype = "application/json";
  317. if (token == NULL || strcmp(csec, xs_dict_get(token, "client_secret")) != 0) {
  318. srv_debug(1, xs_fmt("oauth revoke: bad secret for token %s", tokid));
  319. status = 403;
  320. }
  321. else {
  322. token_del(tokid);
  323. srv_debug(0, xs_fmt("oauth revoke: revoked token %s", tokid));
  324. status = 200;
  325. /* also delete the app, as it serves no purpose from now on */
  326. app_del(cid);
  327. }
  328. }
  329. else {
  330. srv_debug(0, xs_fmt("oauth revoke: invalid or unset arguments"));
  331. status = 403;
  332. }
  333. }
  334. return status;
  335. }
  336. xs_str *mastoapi_id(const xs_dict *msg)
  337. /* returns a somewhat Mastodon-compatible status id */
  338. {
  339. const char *id = xs_dict_get(msg, "id");
  340. xs *md5 = xs_md5_hex(id, strlen(id));
  341. return xs_fmt("%10.0f%s", object_ctime_by_md5(md5), md5);
  342. }
  343. #define MID_TO_MD5(id) (id + 10)
  344. xs_dict *mastoapi_account(const xs_dict *actor)
  345. /* converts an ActivityPub actor to a Mastodon account */
  346. {
  347. xs_dict *acct = xs_dict_new();
  348. const char *display_name = xs_dict_get(actor, "name");
  349. if (xs_is_null(display_name) || *display_name == '\0')
  350. display_name = xs_dict_get(actor, "preferredUsername");
  351. const char *id = xs_dict_get(actor, "id");
  352. const char *pub = xs_dict_get(actor, "published");
  353. xs *acct_md5 = xs_md5_hex(id, strlen(id));
  354. acct = xs_dict_append(acct, "id", acct_md5);
  355. acct = xs_dict_append(acct, "username", xs_dict_get(actor, "preferredUsername"));
  356. acct = xs_dict_append(acct, "acct", xs_dict_get(actor, "preferredUsername"));
  357. acct = xs_dict_append(acct, "display_name", display_name);
  358. if (pub)
  359. acct = xs_dict_append(acct, "created_at", pub);
  360. else {
  361. /* unset created_at crashes Tusky, so lie like a mf */
  362. xs *date = xs_str_utctime(0, "%Y-%m-%dT%H:%M:%SZ");
  363. acct = xs_dict_append(acct, "created_at", date);
  364. }
  365. const char *note = xs_dict_get(actor, "summary");
  366. if (xs_is_null(note))
  367. note = "";
  368. acct = xs_dict_append(acct, "note", note);
  369. acct = xs_dict_append(acct, "url", id);
  370. xs *avatar = NULL;
  371. xs_dict *av = xs_dict_get(actor, "icon");
  372. if (xs_type(av) == XSTYPE_DICT)
  373. avatar = xs_dup(xs_dict_get(av, "url"));
  374. else
  375. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  376. acct = xs_dict_append(acct, "avatar", avatar);
  377. /* emojis */
  378. xs_list *p;
  379. if (!xs_is_null(p = xs_dict_get(actor, "tag"))) {
  380. xs *eml = xs_list_new();
  381. xs_dict *v;
  382. xs *t = xs_val_new(XSTYPE_TRUE);
  383. while (xs_list_iter(&p, &v)) {
  384. const char *type = xs_dict_get(v, "type");
  385. if (!xs_is_null(type) && strcmp(type, "Emoji") == 0) {
  386. const char *name = xs_dict_get(v, "name");
  387. const xs_dict *icon = xs_dict_get(v, "icon");
  388. if (!xs_is_null(name) && !xs_is_null(icon)) {
  389. const char *url = xs_dict_get(icon, "url");
  390. if (!xs_is_null(url)) {
  391. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  392. xs *d1 = xs_dict_new();
  393. d1 = xs_dict_append(d1, "shortcode", nm);
  394. d1 = xs_dict_append(d1, "url", url);
  395. d1 = xs_dict_append(d1, "static_url", url);
  396. d1 = xs_dict_append(d1, "visible_in_picker", t);
  397. eml = xs_list_append(eml, d1);
  398. }
  399. }
  400. }
  401. }
  402. acct = xs_dict_append(acct, "emojis", eml);
  403. }
  404. return acct;
  405. }
  406. xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
  407. /* converts an ActivityPub note to a Mastodon status */
  408. {
  409. xs *actor = NULL;
  410. actor_get(snac, xs_dict_get(msg, "attributedTo"), &actor);
  411. /* if the author is not here, discard */
  412. if (actor == NULL)
  413. return NULL;
  414. xs *acct = mastoapi_account(actor);
  415. /** shave the yak converting an ActivityPub Note to a Mastodon status **/
  416. xs *f = xs_val_new(XSTYPE_FALSE);
  417. xs *t = xs_val_new(XSTYPE_TRUE);
  418. xs *n = xs_val_new(XSTYPE_NULL);
  419. xs *el = xs_list_new();
  420. xs *idx = NULL;
  421. xs *ixc = NULL;
  422. char *tmp;
  423. char *id = xs_dict_get(msg, "id");
  424. xs *mid = mastoapi_id(msg);
  425. xs_dict *st = xs_dict_new();
  426. st = xs_dict_append(st, "id", mid);
  427. st = xs_dict_append(st, "uri", id);
  428. st = xs_dict_append(st, "url", id);
  429. st = xs_dict_append(st, "created_at", xs_dict_get(msg, "published"));
  430. st = xs_dict_append(st, "account", acct);
  431. st = xs_dict_append(st, "content", xs_dict_get(msg, "content"));
  432. st = xs_dict_append(st, "visibility",
  433. is_msg_public(snac, msg) ? "public" : "private");
  434. tmp = xs_dict_get(msg, "sensitive");
  435. if (xs_is_null(tmp))
  436. tmp = f;
  437. st = xs_dict_append(st, "sensitive", tmp);
  438. tmp = xs_dict_get(msg, "summary");
  439. if (xs_is_null(tmp))
  440. tmp = "";
  441. st = xs_dict_append(st, "spoiler_text", tmp);
  442. /* create the list of attachments */
  443. xs *matt = xs_list_new();
  444. xs_list *att = xs_dict_get(msg, "attachment");
  445. xs_str *aobj;
  446. while (xs_list_iter(&att, &aobj)) {
  447. const char *mtype = xs_dict_get(aobj, "mediaType");
  448. if (!xs_is_null(mtype)) {
  449. if (xs_startswith(mtype, "image/") || xs_startswith(mtype, "video/")) {
  450. xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
  451. xs *matte = xs_dict_new();
  452. matte = xs_dict_append(matte, "id", matteid);
  453. matte = xs_dict_append(matte, "type", *mtype == 'i' ? "image" : "video");
  454. matte = xs_dict_append(matte, "url", xs_dict_get(aobj, "url"));
  455. matte = xs_dict_append(matte, "preview_url", xs_dict_get(aobj, "url"));
  456. matte = xs_dict_append(matte, "remote_url", xs_dict_get(aobj, "url"));
  457. const char *name = xs_dict_get(aobj, "name");
  458. if (xs_is_null(name))
  459. name = "";
  460. matte = xs_dict_append(matte, "description", name);
  461. matt = xs_list_append(matt, matte);
  462. }
  463. }
  464. }
  465. st = xs_dict_append(st, "media_attachments", matt);
  466. {
  467. xs *ml = xs_list_new();
  468. xs *htl = xs_list_new();
  469. xs *eml = xs_list_new();
  470. xs_list *p = xs_dict_get(msg, "tag");
  471. xs_dict *v;
  472. int n = 0;
  473. while (xs_list_iter(&p, &v)) {
  474. const char *type = xs_dict_get(v, "type");
  475. if (xs_is_null(type))
  476. continue;
  477. xs *d1 = xs_dict_new();
  478. if (strcmp(type, "Mention") == 0) {
  479. const char *name = xs_dict_get(v, "name");
  480. const char *href = xs_dict_get(v, "href");
  481. if (!xs_is_null(name) && !xs_is_null(href) &&
  482. strcmp(href, snac->actor) != 0) {
  483. xs *nm = xs_strip_chars_i(xs_dup(name), "@");
  484. xs *id = xs_fmt("%d", n++);
  485. d1 = xs_dict_append(d1, "id", id);
  486. d1 = xs_dict_append(d1, "username", nm);
  487. d1 = xs_dict_append(d1, "acct", nm);
  488. d1 = xs_dict_append(d1, "url", href);
  489. ml = xs_list_append(ml, d1);
  490. }
  491. }
  492. else
  493. if (strcmp(type, "Hashtag") == 0) {
  494. const char *name = xs_dict_get(v, "name");
  495. const char *href = xs_dict_get(v, "href");
  496. if (!xs_is_null(name) && !xs_is_null(href)) {
  497. xs *nm = xs_strip_chars_i(xs_dup(name), "#");
  498. d1 = xs_dict_append(d1, "name", nm);
  499. d1 = xs_dict_append(d1, "url", href);
  500. htl = xs_list_append(htl, d1);
  501. }
  502. }
  503. else
  504. if (strcmp(type, "Emoji") == 0) {
  505. const char *name = xs_dict_get(v, "name");
  506. const xs_dict *icon = xs_dict_get(v, "icon");
  507. if (!xs_is_null(name) && !xs_is_null(icon)) {
  508. const char *url = xs_dict_get(icon, "url");
  509. if (!xs_is_null(url)) {
  510. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  511. xs *t = xs_val_new(XSTYPE_TRUE);
  512. d1 = xs_dict_append(d1, "shortcode", nm);
  513. d1 = xs_dict_append(d1, "url", url);
  514. d1 = xs_dict_append(d1, "static_url", url);
  515. d1 = xs_dict_append(d1, "visible_in_picker", t);
  516. d1 = xs_dict_append(d1, "category", "Emojis");
  517. eml = xs_list_append(eml, d1);
  518. }
  519. }
  520. }
  521. }
  522. st = xs_dict_append(st, "mentions", ml);
  523. st = xs_dict_append(st, "tags", htl);
  524. st = xs_dict_append(st, "emojis", eml);
  525. }
  526. xs_free(idx);
  527. xs_free(ixc);
  528. idx = object_likes(id);
  529. ixc = xs_number_new(xs_list_len(idx));
  530. st = xs_dict_append(st, "favourites_count", ixc);
  531. st = xs_dict_append(st, "favourited",
  532. xs_list_in(idx, snac->md5) != -1 ? t : f);
  533. xs_free(idx);
  534. xs_free(ixc);
  535. idx = object_announces(id);
  536. ixc = xs_number_new(xs_list_len(idx));
  537. st = xs_dict_append(st, "reblogs_count", ixc);
  538. st = xs_dict_append(st, "reblogged",
  539. xs_list_in(idx, snac->md5) != -1 ? t : f);
  540. xs_free(idx);
  541. xs_free(ixc);
  542. idx = object_children(id);
  543. ixc = xs_number_new(xs_list_len(idx));
  544. st = xs_dict_append(st, "replies_count", ixc);
  545. /* default in_reply_to values */
  546. st = xs_dict_append(st, "in_reply_to_id", n);
  547. st = xs_dict_append(st, "in_reply_to_account_id", n);
  548. tmp = xs_dict_get(msg, "inReplyTo");
  549. if (!xs_is_null(tmp)) {
  550. xs *irto = NULL;
  551. if (valid_status(object_get(tmp, &irto))) {
  552. xs *irt_mid = mastoapi_id(irto);
  553. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  554. char *at = NULL;
  555. if (!xs_is_null(at = xs_dict_get(irto, "attributedTo"))) {
  556. xs *at_md5 = xs_md5_hex(at, strlen(at));
  557. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  558. }
  559. }
  560. }
  561. st = xs_dict_append(st, "reblog", n);
  562. st = xs_dict_append(st, "poll", n);
  563. st = xs_dict_append(st, "card", n);
  564. st = xs_dict_append(st, "language", n);
  565. tmp = xs_dict_get(msg, "sourceContent");
  566. if (xs_is_null(tmp))
  567. tmp = "";
  568. st = xs_dict_append(st, "text", tmp);
  569. tmp = xs_dict_get(msg, "updated");
  570. if (xs_is_null(tmp))
  571. tmp = n;
  572. st = xs_dict_append(st, "edited_at", tmp);
  573. return st;
  574. }
  575. xs_dict *mastoapi_relationship(snac *snac, const char *md5)
  576. {
  577. xs_dict *rel = NULL;
  578. xs *actor_o = NULL;
  579. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  580. xs *t = xs_val_new(XSTYPE_TRUE);
  581. xs *f = xs_val_new(XSTYPE_FALSE);
  582. rel = xs_dict_new();
  583. const char *actor = xs_dict_get(actor_o, "id");
  584. rel = xs_dict_append(rel, "id", md5);
  585. rel = xs_dict_append(rel, "following",
  586. following_check(snac, actor) ? t : f);
  587. rel = xs_dict_append(rel, "showing_reblogs", t);
  588. rel = xs_dict_append(rel, "notifying", f);
  589. rel = xs_dict_append(rel, "followed_by",
  590. follower_check(snac, actor) ? t : f);
  591. rel = xs_dict_append(rel, "blocking",
  592. is_muted(snac, actor) ? t : f);
  593. rel = xs_dict_append(rel, "muting", f);
  594. rel = xs_dict_append(rel, "muting_notifications", f);
  595. rel = xs_dict_append(rel, "requested", f);
  596. rel = xs_dict_append(rel, "domain_blocking", f);
  597. rel = xs_dict_append(rel, "endorsed", f);
  598. rel = xs_dict_append(rel, "note", "");
  599. }
  600. return rel;
  601. }
  602. int process_auth_token(snac *snac, const xs_dict *req)
  603. /* processes an authorization token, if there is one */
  604. {
  605. int logged_in = 0;
  606. char *v;
  607. /* if there is an authorization field, try to validate it */
  608. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  609. xs *tokid = xs_replace(v, "Bearer ", "");
  610. xs *token = token_get(tokid);
  611. if (token != NULL) {
  612. const char *uid = xs_dict_get(token, "uid");
  613. if (!xs_is_null(uid) && user_open(snac, uid)) {
  614. logged_in = 1;
  615. /* this counts as a 'login' */
  616. lastlog_write(snac);
  617. srv_debug(2, xs_fmt("mastoapi auth: valid token for user %s", uid));
  618. }
  619. else
  620. srv_log(xs_fmt("mastoapi auth: corrupted token %s", tokid));
  621. }
  622. else
  623. srv_log(xs_fmt("mastoapi auth: invalid token %s", tokid));
  624. }
  625. return logged_in;
  626. }
  627. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  628. char **body, int *b_size, char **ctype)
  629. {
  630. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  631. return 0;
  632. srv_debug(1, xs_fmt("mastoapi_get_handler %s", q_path));
  633. /* {
  634. xs *j = xs_json_dumps_pp(req, 4);
  635. printf("mastoapi get:\n%s\n", j);
  636. }*/
  637. int status = 404;
  638. xs_dict *args = xs_dict_get(req, "q_vars");
  639. xs *cmd = xs_replace(q_path, "/api", "");
  640. snac snac1 = {0};
  641. int logged_in = process_auth_token(&snac1, req);
  642. if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) {
  643. if (logged_in) {
  644. xs *acct = xs_dict_new();
  645. acct = xs_dict_append(acct, "id", xs_dict_get(snac1.config, "uid"));
  646. acct = xs_dict_append(acct, "username", xs_dict_get(snac1.config, "uid"));
  647. acct = xs_dict_append(acct, "acct", xs_dict_get(snac1.config, "uid"));
  648. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac1.config, "name"));
  649. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac1.config, "published"));
  650. acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio"));
  651. acct = xs_dict_append(acct, "url", snac1.actor);
  652. acct = xs_dict_append(acct, "header", "");
  653. xs *avatar = NULL;
  654. char *av = xs_dict_get(snac1.config, "avatar");
  655. if (xs_is_null(av) || *av == '\0')
  656. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  657. else
  658. avatar = xs_dup(av);
  659. acct = xs_dict_append(acct, "avatar", avatar);
  660. *body = xs_json_dumps_pp(acct, 4);
  661. *ctype = "application/json";
  662. status = 200;
  663. }
  664. else {
  665. status = 422; // "Unprocessable entity" (no login)
  666. }
  667. }
  668. else
  669. if (strcmp(cmd, "/v1/accounts/relationships") == 0) {
  670. /* find if an account is followed, blocked, etc. */
  671. /* the account to get relationships about is in args "id[]" */
  672. if (logged_in) {
  673. xs *res = xs_list_new();
  674. const char *md5 = xs_dict_get(args, "id[]");
  675. if (!xs_is_null(md5)) {
  676. xs *rel = mastoapi_relationship(&snac1, md5);
  677. if (rel != NULL)
  678. res = xs_list_append(res, rel);
  679. }
  680. *body = xs_json_dumps_pp(res, 4);
  681. *ctype = "application/json";
  682. status = 200;
  683. }
  684. else
  685. status = 422;
  686. }
  687. else
  688. if (xs_startswith(cmd, "/v1/accounts/")) {
  689. /* account-related information */
  690. xs *l = xs_split(cmd, "/");
  691. const char *uid = xs_list_get(l, 3);
  692. const char *opt = xs_list_get(l, 4);
  693. if (uid != NULL) {
  694. snac snac2;
  695. xs *out = NULL;
  696. xs *actor = NULL;
  697. /* is it a local user? */
  698. if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
  699. if (opt == NULL) {
  700. /* account information */
  701. actor = msg_actor(&snac2);
  702. out = mastoapi_account(actor);
  703. }
  704. else
  705. if (strcmp(opt, "statuses") == 0) {
  706. /* the public list of posts of a user */
  707. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  708. xs_list *p = timeline;
  709. xs_str *v;
  710. out = xs_list_new();
  711. while (xs_list_iter(&p, &v)) {
  712. xs *msg = NULL;
  713. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  714. /* add only posts by the author */
  715. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  716. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  717. xs *st = mastoapi_status(&snac2, msg);
  718. out = xs_list_append(out, st);
  719. }
  720. }
  721. }
  722. }
  723. user_free(&snac2);
  724. }
  725. else {
  726. /* try the uid as the md5 of a possibly loaded actor */
  727. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  728. if (opt == NULL) {
  729. /* account information */
  730. out = mastoapi_account(actor);
  731. }
  732. else
  733. if (strcmp(opt, "statuses") == 0) {
  734. /* we don't serve statuses of others; return the empty list */
  735. out = xs_list_new();
  736. }
  737. }
  738. }
  739. if (out != NULL) {
  740. *body = xs_json_dumps_pp(out, 4);
  741. *ctype = "application/json";
  742. status = 200;
  743. }
  744. }
  745. }
  746. else
  747. if (strcmp(cmd, "/v1/timelines/home") == 0) {
  748. /* the private timeline */
  749. if (logged_in) {
  750. const char *max_id = xs_dict_get(args, "max_id");
  751. const char *since_id = xs_dict_get(args, "since_id");
  752. const char *min_id = xs_dict_get(args, "min_id");
  753. const char *limit_s = xs_dict_get(args, "limit");
  754. int limit = 0;
  755. int cnt = 0;
  756. if (!xs_is_null(limit_s))
  757. limit = atoi(limit_s);
  758. if (limit == 0)
  759. limit = 20;
  760. xs *timeline = timeline_simple_list(&snac1, "private", 0, 256);
  761. xs *out = xs_list_new();
  762. xs_list *p = timeline;
  763. xs_str *v;
  764. while (xs_list_iter(&p, &v) && cnt < limit) {
  765. xs *msg = NULL;
  766. /* only return entries older that max_id */
  767. if (max_id) {
  768. if (strcmp(v, max_id) == 0)
  769. max_id = NULL;
  770. continue;
  771. }
  772. /* only returns entries newer than since_id */
  773. if (since_id) {
  774. if (strcmp(v, since_id) == 0)
  775. break;
  776. }
  777. /* only returns entries newer than min_id */
  778. /* what does really "Return results immediately newer than ID" mean? */
  779. if (min_id) {
  780. if (strcmp(v, min_id) == 0)
  781. break;
  782. }
  783. /* get the entry */
  784. if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
  785. continue;
  786. /* discard non-Notes */
  787. if (strcmp(xs_dict_get(msg, "type"), "Note") != 0)
  788. continue;
  789. /* discard notes from muted morons */
  790. if (is_muted(&snac1, xs_dict_get(msg, "attributedTo")))
  791. continue;
  792. /* discard hidden notes */
  793. if (is_hidden(&snac1, xs_dict_get(msg, "id")))
  794. continue;
  795. /* convert the Note into a Mastodon status */
  796. xs *st = mastoapi_status(&snac1, msg);
  797. if (st != NULL)
  798. out = xs_list_append(out, st);
  799. cnt++;
  800. }
  801. *body = xs_json_dumps_pp(out, 4);
  802. *ctype = "application/json";
  803. status = 200;
  804. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  805. }
  806. else {
  807. status = 401; // unauthorized
  808. }
  809. }
  810. else
  811. if (strcmp(cmd, "/v1/timelines/public") == 0) {
  812. /* the public timeline (public timelines for all users) */
  813. const char *limit_s = xs_dict_get(args, "limit");
  814. int limit = 0;
  815. int cnt = 0;
  816. if (!xs_is_null(limit_s))
  817. limit = atoi(limit_s);
  818. if (limit == 0)
  819. limit = 20;
  820. xs *timeline = timeline_instance_list(0, limit);
  821. xs *out = xs_list_new();
  822. xs_list *p = timeline;
  823. xs_str *md5;
  824. while (xs_list_iter(&p, &md5) && cnt < limit) {
  825. xs *msg = NULL;
  826. /* get the entry */
  827. if (!valid_status(object_get_by_md5(md5, &msg)))
  828. continue;
  829. /* discard non-Notes */
  830. if (strcmp(xs_dict_get(msg, "type"), "Note") != 0)
  831. continue;
  832. /* get the uid */
  833. xs *l = xs_split(xs_dict_get(msg, "attributedTo"), "/");
  834. const char *uid = xs_list_get(l, -1);
  835. if (!xs_is_null(uid)) {
  836. snac user;
  837. if (user_open(&user, uid)) {
  838. /* convert the Note into a Mastodon status */
  839. xs *st = mastoapi_status(&user, msg);
  840. if (st != NULL)
  841. out = xs_list_append(out, st);
  842. user_free(&user);
  843. }
  844. cnt++;
  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 *csec = random_str();
  1211. xs *vkey = random_str();
  1212. xs *cid = NULL;
  1213. /* pick a non-existent random cid */
  1214. for (;;) {
  1215. cid = random_str();
  1216. xs *p_app = app_get(cid);
  1217. if (p_app == NULL)
  1218. break;
  1219. xs_free(cid);
  1220. }
  1221. app = xs_dict_append(app, "name", name);
  1222. app = xs_dict_append(app, "redirect_uri", ruri);
  1223. app = xs_dict_append(app, "client_id", cid);
  1224. app = xs_dict_append(app, "client_secret", csec);
  1225. app = xs_dict_append(app, "vapid_key", vkey);
  1226. app = xs_dict_append(app, "id", id);
  1227. *body = xs_json_dumps_pp(app, 4);
  1228. *ctype = "application/json";
  1229. status = 200;
  1230. app = xs_dict_append(app, "code", "");
  1231. if (scope)
  1232. app = xs_dict_append(app, "scope", scope);
  1233. app_add(cid, app);
  1234. srv_debug(0, xs_fmt("mastoapi apps: new app %s", cid));
  1235. }
  1236. }
  1237. else
  1238. if (strcmp(cmd, "/v1/statuses") == 0) {
  1239. if (logged_in) {
  1240. /* post a new Note */
  1241. /* {
  1242. xs *j = xs_json_dumps_pp(args, 4);
  1243. printf("%s\n", j);
  1244. }*/
  1245. const char *content = xs_dict_get(args, "status");
  1246. const char *mid = xs_dict_get(args, "in_reply_to_id");
  1247. const char *visibility = xs_dict_get(args, "visibility");
  1248. const char *summary = xs_dict_get(args, "spoiler_text");
  1249. const char *media_ids = xs_dict_get(args, "media_ids");
  1250. if (xs_is_null(media_ids))
  1251. media_ids = xs_dict_get(args, "media_ids[]");
  1252. xs *attach_list = xs_list_new();
  1253. xs *irt = NULL;
  1254. /* is it a reply? */
  1255. if (mid != NULL) {
  1256. xs *r_msg = NULL;
  1257. const char *md5 = MID_TO_MD5(mid);
  1258. if (valid_status(object_get_by_md5(md5, &r_msg)))
  1259. irt = xs_dup(xs_dict_get(r_msg, "id"));
  1260. }
  1261. /* does it have attachments? */
  1262. if (!xs_is_null(media_ids)) {
  1263. xs *mi = NULL;
  1264. if (xs_type(media_ids) == XSTYPE_LIST)
  1265. mi = xs_dup(media_ids);
  1266. else {
  1267. mi = xs_list_new();
  1268. mi = xs_list_append(mi, media_ids);
  1269. }
  1270. xs_list *p = mi;
  1271. xs_str *v;
  1272. while (xs_list_iter(&p, &v)) {
  1273. xs *l = xs_list_new();
  1274. xs *url = xs_fmt("%s/s/%s", snac.actor, v);
  1275. xs *desc = static_get_meta(&snac, v);
  1276. l = xs_list_append(l, url);
  1277. l = xs_list_append(l, desc);
  1278. attach_list = xs_list_append(attach_list, l);
  1279. }
  1280. }
  1281. /* prepare the message */
  1282. xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
  1283. strcmp(visibility, "public") == 0 ? 0 : 1);
  1284. if (!xs_is_null(summary) && *summary) {
  1285. xs *t = xs_val_new(XSTYPE_TRUE);
  1286. msg = xs_dict_set(msg, "sensitive", t);
  1287. msg = xs_dict_set(msg, "summary", summary);
  1288. }
  1289. /* store */
  1290. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1291. /* 'Create' message */
  1292. xs *c_msg = msg_create(&snac, msg);
  1293. enqueue_message(&snac, c_msg);
  1294. timeline_touch(&snac);
  1295. /* convert to a mastodon status as a response code */
  1296. xs *st = mastoapi_status(&snac, msg);
  1297. *body = xs_json_dumps_pp(st, 4);
  1298. *ctype = "application/json";
  1299. status = 200;
  1300. }
  1301. else
  1302. status = 401;
  1303. }
  1304. else
  1305. if (xs_startswith(cmd, "/v1/statuses")) {
  1306. if (logged_in) {
  1307. /* operations on a status */
  1308. xs *l = xs_split(cmd, "/");
  1309. const char *mid = xs_list_get(l, 3);
  1310. const char *op = xs_list_get(l, 4);
  1311. if (!xs_is_null(mid)) {
  1312. xs *msg = NULL;
  1313. xs *out = NULL;
  1314. /* skip the 'fake' part of the id */
  1315. mid = MID_TO_MD5(mid);
  1316. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1317. char *id = xs_dict_get(msg, "id");
  1318. if (op == NULL) {
  1319. /* no operation (?) */
  1320. }
  1321. else
  1322. if (strcmp(op, "favourite") == 0) {
  1323. xs *n_msg = msg_admiration(&snac, id, "Like");
  1324. if (n_msg != NULL) {
  1325. enqueue_message(&snac, n_msg);
  1326. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  1327. out = mastoapi_status(&snac, msg);
  1328. }
  1329. }
  1330. else
  1331. if (strcmp(op, "unfavourite") == 0) {
  1332. /* snac does not support Undo+Like */
  1333. }
  1334. else
  1335. if (strcmp(op, "reblog") == 0) {
  1336. xs *n_msg = msg_admiration(&snac, id, "Announce");
  1337. if (n_msg != NULL) {
  1338. enqueue_message(&snac, n_msg);
  1339. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  1340. out = mastoapi_status(&snac, msg);
  1341. }
  1342. }
  1343. else
  1344. if (strcmp(op, "unreblog") == 0) {
  1345. /* snac does not support Undo+Announce */
  1346. }
  1347. else
  1348. if (strcmp(op, "bookmark") == 0) {
  1349. /* snac does not support bookmarks */
  1350. }
  1351. else
  1352. if (strcmp(op, "unbookmark") == 0) {
  1353. /* snac does not support bookmarks */
  1354. }
  1355. else
  1356. if (strcmp(op, "pin") == 0) {
  1357. /* snac does not support pinning */
  1358. }
  1359. else
  1360. if (strcmp(op, "unpin") == 0) {
  1361. /* snac does not support pinning */
  1362. }
  1363. else
  1364. if (strcmp(op, "mute") == 0) {
  1365. /* Mastodon's mute is snac's hide */
  1366. }
  1367. else
  1368. if (strcmp(op, "unmute") == 0) {
  1369. /* Mastodon's unmute is snac's unhide */
  1370. }
  1371. }
  1372. if (out != NULL) {
  1373. *body = xs_json_dumps_pp(out, 4);
  1374. *ctype = "application/json";
  1375. status = 200;
  1376. }
  1377. }
  1378. }
  1379. else
  1380. status = 401;
  1381. }
  1382. else
  1383. if (strcmp(cmd, "/v1/notifications/clear") == 0) {
  1384. if (logged_in) {
  1385. notify_clear(&snac);
  1386. timeline_touch(&snac);
  1387. *body = xs_dup("{}");
  1388. *ctype = "application/json";
  1389. status = 200;
  1390. }
  1391. else
  1392. status = 401;
  1393. }
  1394. else
  1395. if (strcmp(cmd, "/v1/push/subscription") == 0) {
  1396. /* I don't know what I'm doing */
  1397. if (logged_in) {
  1398. char *v;
  1399. xs *wpush = xs_dict_new();
  1400. wpush = xs_dict_append(wpush, "id", "1");
  1401. v = xs_dict_get(args, "data");
  1402. v = xs_dict_get(v, "alerts");
  1403. wpush = xs_dict_append(wpush, "alerts", v);
  1404. v = xs_dict_get(args, "subscription");
  1405. v = xs_dict_get(v, "endpoint");
  1406. wpush = xs_dict_append(wpush, "endpoint", v);
  1407. xs *server_key = random_str();
  1408. wpush = xs_dict_append(wpush, "server_key", server_key);
  1409. *body = xs_json_dumps_pp(wpush, 4);
  1410. *ctype = "application/json";
  1411. status = 200;
  1412. }
  1413. else
  1414. status = 401;
  1415. }
  1416. else
  1417. if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) {
  1418. if (logged_in) {
  1419. /* {
  1420. xs *j = xs_json_dumps_pp(args, 4);
  1421. printf("%s\n", j);
  1422. }*/
  1423. const xs_list *file = xs_dict_get(args, "file");
  1424. const char *desc = xs_dict_get(args, "description");
  1425. if (xs_is_null(desc))
  1426. desc = "";
  1427. status = 400;
  1428. if (xs_type(file) == XSTYPE_LIST) {
  1429. const char *fn = xs_list_get(file, 0);
  1430. if (*fn != '\0') {
  1431. char *ext = strrchr(fn, '.');
  1432. xs *hash = xs_md5_hex(fn, strlen(fn));
  1433. xs *id = xs_fmt("%s%s", hash, ext);
  1434. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1435. int fo = xs_number_get(xs_list_get(file, 1));
  1436. int fs = xs_number_get(xs_list_get(file, 2));
  1437. /* store */
  1438. static_put(&snac, id, payload + fo, fs);
  1439. static_put_meta(&snac, id, desc);
  1440. /* prepare a response */
  1441. xs *rsp = xs_dict_new();
  1442. rsp = xs_dict_append(rsp, "id", id);
  1443. rsp = xs_dict_append(rsp, "type", "image");
  1444. rsp = xs_dict_append(rsp, "url", url);
  1445. rsp = xs_dict_append(rsp, "preview_url", url);
  1446. rsp = xs_dict_append(rsp, "remote_url", url);
  1447. rsp = xs_dict_append(rsp, "description", desc);
  1448. *body = xs_json_dumps_pp(rsp, 4);
  1449. *ctype = "application/json";
  1450. status = 200;
  1451. }
  1452. }
  1453. }
  1454. else
  1455. status = 401;
  1456. }
  1457. else
  1458. if (xs_startswith(cmd, "/v1/accounts")) {
  1459. if (logged_in) {
  1460. /* account-related information */
  1461. xs *l = xs_split(cmd, "/");
  1462. const char *md5 = xs_list_get(l, 3);
  1463. const char *opt = xs_list_get(l, 4);
  1464. xs *rsp = NULL;
  1465. if (!xs_is_null(md5) && *md5) {
  1466. xs *actor_o = NULL;
  1467. if (xs_is_null(opt)) {
  1468. /* ? */
  1469. }
  1470. else
  1471. if (strcmp(opt, "follow") == 0) {
  1472. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1473. const char *actor = xs_dict_get(actor_o, "id");
  1474. xs *msg = msg_follow(&snac, actor);
  1475. if (msg != NULL) {
  1476. /* reload the actor from the message, in may be different */
  1477. actor = xs_dict_get(msg, "object");
  1478. following_add(&snac, actor, msg);
  1479. enqueue_output_by_actor(&snac, msg, actor, 0);
  1480. rsp = mastoapi_relationship(&snac, md5);
  1481. }
  1482. }
  1483. }
  1484. else
  1485. if (strcmp(opt, "unfollow") == 0) {
  1486. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1487. const char *actor = xs_dict_get(actor_o, "id");
  1488. /* get the following object */
  1489. xs *object = NULL;
  1490. if (valid_status(following_get(&snac, actor, &object))) {
  1491. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  1492. following_del(&snac, actor);
  1493. enqueue_output_by_actor(&snac, msg, actor, 0);
  1494. rsp = mastoapi_relationship(&snac, md5);
  1495. }
  1496. }
  1497. }
  1498. }
  1499. if (rsp != NULL) {
  1500. *body = xs_json_dumps_pp(rsp, 4);
  1501. *ctype = "application/json";
  1502. status = 200;
  1503. }
  1504. }
  1505. else
  1506. status = 401;
  1507. }
  1508. /* user cleanup */
  1509. if (logged_in)
  1510. user_free(&snac);
  1511. return status;
  1512. }
  1513. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  1514. const char *payload, int p_size,
  1515. char **body, int *b_size, char **ctype)
  1516. {
  1517. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1518. return 0;
  1519. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1520. /* {
  1521. xs *j = xs_json_dumps_pp(req, 4);
  1522. printf("mastoapi put:\n%s\n", j);
  1523. }*/
  1524. int status = 404;
  1525. xs *args = NULL;
  1526. char *i_ctype = xs_dict_get(req, "content-type");
  1527. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1528. args = xs_json_loads(payload);
  1529. else
  1530. args = xs_dup(xs_dict_get(req, "p_vars"));
  1531. if (args == NULL)
  1532. return 400;
  1533. xs *cmd = xs_replace(q_path, "/api", "");
  1534. snac snac = {0};
  1535. int logged_in = process_auth_token(&snac, req);
  1536. if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) {
  1537. if (logged_in) {
  1538. xs *l = xs_split(cmd, "/");
  1539. const char *stid = xs_list_get(l, 3);
  1540. if (!xs_is_null(stid)) {
  1541. const char *desc = xs_dict_get(args, "description");
  1542. /* set the image metadata */
  1543. static_put_meta(&snac, stid, desc);
  1544. /* prepare a response */
  1545. xs *rsp = xs_dict_new();
  1546. xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
  1547. rsp = xs_dict_append(rsp, "id", stid);
  1548. rsp = xs_dict_append(rsp, "type", "image");
  1549. rsp = xs_dict_append(rsp, "url", url);
  1550. rsp = xs_dict_append(rsp, "preview_url", url);
  1551. rsp = xs_dict_append(rsp, "remote_url", url);
  1552. rsp = xs_dict_append(rsp, "description", desc);
  1553. *body = xs_json_dumps_pp(rsp, 4);
  1554. *ctype = "application/json";
  1555. status = 200;
  1556. }
  1557. }
  1558. else
  1559. status = 401;
  1560. }
  1561. /* user cleanup */
  1562. if (logged_in)
  1563. user_free(&snac);
  1564. return status;
  1565. }
  1566. void mastoapi_purge(void)
  1567. {
  1568. xs *spec = xs_fmt("%s/app/" "*.json", srv_basedir);
  1569. xs *files = xs_glob(spec, 1, 0);
  1570. xs_list *p = files;
  1571. xs_str *v;
  1572. time_t mt = time(NULL) - 3600;
  1573. while (xs_list_iter(&p, &v)) {
  1574. xs *cid = xs_replace(v, ".json", "");
  1575. xs *fn = _app_fn(cid);
  1576. if (mtime(fn) < mt) {
  1577. /* get the app */
  1578. xs *app = app_get(cid);
  1579. if (app) {
  1580. /* old apps with no uid are incomplete cruft */
  1581. const char *uid = xs_dict_get(app, "uid");
  1582. if (xs_is_null(uid) || *uid == '\0') {
  1583. unlink(fn);
  1584. srv_debug(2, xs_fmt("purged %s", fn));
  1585. }
  1586. }
  1587. }
  1588. }
  1589. }
  1590. #endif /* #ifndef NO_MASTODON_API */