mastoapi.c 64 KB

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