mastoapi.c 55 KB

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