mastoapi.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 grunfink / MIT license */
  3. #include "xs.h"
  4. #include "xs_encdec.h"
  5. #include "xs_openssl.h"
  6. #include "xs_json.h"
  7. #include "xs_io.h"
  8. #include "xs_time.h"
  9. #include "snac.h"
  10. static xs_str *random_str(void)
  11. /* just what is says in the tin */
  12. {
  13. unsigned int data[4] = {0};
  14. FILE *f;
  15. if ((f = fopen("/dev/random", "r")) != NULL) {
  16. fread(data, sizeof(data), 1, f);
  17. fclose(f);
  18. }
  19. else {
  20. data[0] = random() % 0xffffffff;
  21. data[1] = random() % 0xffffffff;
  22. data[2] = random() % 0xffffffff;
  23. data[3] = random() % 0xffffffff;
  24. }
  25. return xs_hex_enc((char *)data, sizeof(data));
  26. }
  27. int app_add(const char *id, const xs_dict *app)
  28. /* stores an app */
  29. {
  30. if (!xs_is_hex(id))
  31. return 500;
  32. int status = 201;
  33. xs *fn = xs_fmt("%s/app/", srv_basedir);
  34. FILE *f;
  35. mkdirx(fn);
  36. fn = xs_str_cat(fn, id);
  37. fn = xs_str_cat(fn, ".json");
  38. if ((f = fopen(fn, "w")) != NULL) {
  39. xs *j = xs_json_dumps_pp(app, 4);
  40. fwrite(j, strlen(j), 1, f);
  41. fclose(f);
  42. }
  43. else
  44. status = 500;
  45. return status;
  46. }
  47. xs_dict *app_get(const char *id)
  48. /* gets an app */
  49. {
  50. if (!xs_is_hex(id))
  51. return NULL;
  52. xs *fn = xs_fmt("%s/app/%s.json", srv_basedir, id);
  53. xs_dict *app = NULL;
  54. FILE *f;
  55. if ((f = fopen(fn, "r")) != NULL) {
  56. xs *j = xs_readall(f);
  57. fclose(f);
  58. app = xs_json_loads(j);
  59. }
  60. return app;
  61. }
  62. int app_del(const char *id)
  63. /* deletes an app */
  64. {
  65. if (!xs_is_hex(id))
  66. return -1;
  67. xs *fn = xs_fmt("%s/app/%s.json", srv_basedir, id);
  68. return unlink(fn);
  69. }
  70. int token_add(const char *id, const xs_dict *token)
  71. /* stores a token */
  72. {
  73. if (!xs_is_hex(id))
  74. return 500;
  75. int status = 201;
  76. xs *fn = xs_fmt("%s/token/", srv_basedir);
  77. FILE *f;
  78. mkdirx(fn);
  79. fn = xs_str_cat(fn, id);
  80. fn = xs_str_cat(fn, ".json");
  81. if ((f = fopen(fn, "w")) != NULL) {
  82. xs *j = xs_json_dumps_pp(token, 4);
  83. fwrite(j, strlen(j), 1, f);
  84. fclose(f);
  85. }
  86. else
  87. status = 500;
  88. return status;
  89. }
  90. xs_dict *token_get(const char *id)
  91. /* gets a token */
  92. {
  93. if (!xs_is_hex(id))
  94. return NULL;
  95. xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
  96. xs_dict *token = NULL;
  97. FILE *f;
  98. if ((f = fopen(fn, "r")) != NULL) {
  99. xs *j = xs_readall(f);
  100. fclose(f);
  101. token = xs_json_loads(j);
  102. }
  103. return token;
  104. }
  105. int token_del(const char *id)
  106. /* deletes a token */
  107. {
  108. if (!xs_is_hex(id))
  109. return -1;
  110. xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
  111. return unlink(fn);
  112. }
  113. const char *login_page = ""
  114. "<!DOCTYPE html>\n"
  115. "<body><h1>%s OAuth identify</h1>\n"
  116. "<div style=\"background-color: red; color: white\">%s</div>\n"
  117. "<form method=\"post\" action=\"https:/" "/%s/oauth/x-snac-login\">\n"
  118. "<p>Login: <input type=\"text\" name=\"login\"></p>\n"
  119. "<p>Password: <input type=\"password\" name=\"passwd\"></p>\n"
  120. "<input type=\"hidden\" name=\"redir\" value=\"%s\">\n"
  121. "<input type=\"hidden\" name=\"cid\" value=\"%s\">\n"
  122. "<input type=\"hidden\" name=\"state\" value=\"%s\">\n"
  123. "<input type=\"submit\" value=\"OK\">\n"
  124. "</form><p>%s</p></body>\n"
  125. "";
  126. int oauth_get_handler(const xs_dict *req, const char *q_path,
  127. char **body, int *b_size, char **ctype)
  128. {
  129. if (!xs_startswith(q_path, "/oauth/"))
  130. return 0;
  131. /* {
  132. xs *j = xs_json_dumps_pp(req, 4);
  133. printf("oauth get:\n%s\n", j);
  134. }*/
  135. int status = 404;
  136. xs_dict *msg = xs_dict_get(req, "q_vars");
  137. xs *cmd = xs_replace(q_path, "/oauth", "");
  138. srv_debug(0, xs_fmt("oauth_get_handler %s", q_path));
  139. if (strcmp(cmd, "/authorize") == 0) {
  140. const char *cid = xs_dict_get(msg, "client_id");
  141. const char *ruri = xs_dict_get(msg, "redirect_uri");
  142. const char *rtype = xs_dict_get(msg, "response_type");
  143. const char *state = xs_dict_get(msg, "state");
  144. status = 400;
  145. if (cid && ruri && rtype && strcmp(rtype, "code") == 0) {
  146. xs *app = app_get(cid);
  147. if (app != NULL) {
  148. const char *host = xs_dict_get(srv_config, "host");
  149. if (xs_is_null(state))
  150. state = "";
  151. *body = xs_fmt(login_page, host, "", host, ruri, cid, state, USER_AGENT);
  152. *ctype = "text/html";
  153. status = 200;
  154. srv_debug(0, xs_fmt("oauth authorize: generating login page"));
  155. }
  156. else
  157. srv_debug(0, xs_fmt("oauth authorize: bad client_id %s", cid));
  158. }
  159. else
  160. srv_debug(0, xs_fmt("oauth authorize: invalid or unset arguments"));
  161. }
  162. return status;
  163. }
  164. int oauth_post_handler(const xs_dict *req, const char *q_path,
  165. const char *payload, int p_size,
  166. char **body, int *b_size, char **ctype)
  167. {
  168. if (!xs_startswith(q_path, "/oauth/"))
  169. return 0;
  170. /* {
  171. xs *j = xs_json_dumps_pp(req, 4);
  172. printf("oauth post:\n%s\n", j);
  173. }*/
  174. int status = 404;
  175. xs_dict *msg = xs_dict_get(req, "p_vars");
  176. xs *cmd = xs_replace(q_path, "/oauth", "");
  177. srv_debug(0, xs_fmt("oauth_post_handler %s", q_path));
  178. if (strcmp(cmd, "/x-snac-login") == 0) {
  179. const char *login = xs_dict_get(msg, "login");
  180. const char *passwd = xs_dict_get(msg, "passwd");
  181. const char *redir = xs_dict_get(msg, "redir");
  182. const char *cid = xs_dict_get(msg, "cid");
  183. const char *state = xs_dict_get(msg, "state");
  184. const char *host = xs_dict_get(srv_config, "host");
  185. /* by default, generate another login form with an error */
  186. *body = xs_fmt(login_page, host, "LOGIN INCORRECT", host, redir, cid, state, USER_AGENT);
  187. *ctype = "text/html";
  188. status = 200;
  189. if (login && passwd && redir && cid) {
  190. snac snac;
  191. if (user_open(&snac, login)) {
  192. /* check the login + password */
  193. if (check_password(login, passwd,
  194. xs_dict_get(snac.config, "passwd"))) {
  195. /* success! redirect to the desired uri */
  196. xs *code = random_str();
  197. xs_free(*body);
  198. *body = xs_fmt("%s?code=%s", redir, code);
  199. status = 303;
  200. /* if there is a state, add it */
  201. if (!xs_is_null(state) && *state) {
  202. *body = xs_str_cat(*body, "&state=");
  203. *body = xs_str_cat(*body, state);
  204. }
  205. srv_debug(0, xs_fmt("oauth x-snac-login: success, redirect to %s", *body));
  206. /* assign the login to the app */
  207. xs *app = app_get(cid);
  208. if (app != NULL) {
  209. app = xs_dict_set(app, "uid", login);
  210. app = xs_dict_set(app, "code", code);
  211. app_add(cid, app);
  212. }
  213. else
  214. srv_log(xs_fmt("oauth x-snac-login: error getting app %s", cid));
  215. }
  216. else
  217. srv_debug(0, xs_fmt("oauth x-snac-login: login '%s' incorrect", login));
  218. user_free(&snac);
  219. }
  220. else
  221. srv_debug(0, xs_fmt("oauth x-snac-login: bad user '%s'", login));
  222. }
  223. else
  224. srv_debug(0, xs_fmt("oauth x-snac-login: invalid or unset arguments"));
  225. }
  226. else
  227. if (strcmp(cmd, "/token") == 0) {
  228. const char *gtype = xs_dict_get(msg, "grant_type");
  229. const char *code = xs_dict_get(msg, "code");
  230. const char *cid = xs_dict_get(msg, "client_id");
  231. const char *csec = xs_dict_get(msg, "client_secret");
  232. const char *ruri = xs_dict_get(msg, "redirect_uri");
  233. xs *wrk = NULL;
  234. /* no client_secret? check if it's inside an authorization header
  235. (AndStatus does it this way) */
  236. if (xs_is_null(csec)) {
  237. const char *auhdr = xs_dict_get(req, "authorization");
  238. if (!xs_is_null(auhdr) && xs_startswith(auhdr, "Basic ")) {
  239. xs *s1 = xs_replace(auhdr, "Basic ", "");
  240. int size;
  241. xs *s2 = xs_base64_dec(s1, &size);
  242. if (!xs_is_null(s2)) {
  243. xs *l1 = xs_split(s2, ":");
  244. if (xs_list_len(l1) == 2) {
  245. wrk = xs_dup(xs_list_get(l1, 1));
  246. csec = wrk;
  247. }
  248. }
  249. }
  250. }
  251. if (gtype && code && cid && csec && ruri) {
  252. xs *app = app_get(cid);
  253. if (app == NULL) {
  254. status = 401;
  255. srv_log(xs_fmt("oauth token: invalid app %s", cid));
  256. }
  257. else
  258. if (strcmp(csec, xs_dict_get(app, "client_secret")) != 0) {
  259. status = 401;
  260. srv_log(xs_fmt("oauth token: invalid client_secret for app %s", cid));
  261. }
  262. else {
  263. xs *rsp = xs_dict_new();
  264. xs *cat = xs_number_new(time(NULL));
  265. xs *tokid = random_str();
  266. rsp = xs_dict_append(rsp, "access_token", tokid);
  267. rsp = xs_dict_append(rsp, "token_type", "Bearer");
  268. rsp = xs_dict_append(rsp, "created_at", cat);
  269. *body = xs_json_dumps_pp(rsp, 4);
  270. *ctype = "application/json";
  271. status = 200;
  272. const char *uid = xs_dict_get(app, "uid");
  273. srv_debug(0, xs_fmt("oauth token: "
  274. "successful login for %s, new token %s", uid, tokid));
  275. xs *token = xs_dict_new();
  276. token = xs_dict_append(token, "token", tokid);
  277. token = xs_dict_append(token, "client_id", cid);
  278. token = xs_dict_append(token, "client_secret", csec);
  279. token = xs_dict_append(token, "uid", uid);
  280. token = xs_dict_append(token, "code", code);
  281. token_add(tokid, token);
  282. }
  283. }
  284. else {
  285. srv_debug(0, xs_fmt("oauth token: invalid or unset arguments"));
  286. status = 400;
  287. }
  288. }
  289. else
  290. if (strcmp(cmd, "/revoke") == 0) {
  291. const char *cid = xs_dict_get(msg, "client_id");
  292. const char *csec = xs_dict_get(msg, "client_secret");
  293. const char *tokid = xs_dict_get(msg, "token");
  294. if (cid && csec && tokid) {
  295. xs *token = token_get(tokid);
  296. *body = xs_str_new("{}");
  297. *ctype = "application/json";
  298. if (token == NULL || strcmp(csec, xs_dict_get(token, "client_secret")) != 0) {
  299. srv_debug(0, xs_fmt("oauth revoke: bad secret for token %s", tokid));
  300. status = 403;
  301. }
  302. else {
  303. token_del(tokid);
  304. srv_debug(0, xs_fmt("oauth revoke: revoked token %s", tokid));
  305. status = 200;
  306. /* also delete the app, as it serves no purpose from now on */
  307. app_del(cid);
  308. }
  309. }
  310. else {
  311. srv_debug(0, xs_fmt("oauth revoke: invalid or unset arguments"));
  312. status = 403;
  313. }
  314. }
  315. return status;
  316. }
  317. xs_str *mastoapi_id(const xs_dict *msg)
  318. /* returns a somewhat Mastodon-compatible status id */
  319. {
  320. const char *id = xs_dict_get(msg, "id");
  321. xs *md5 = xs_md5_hex(id, strlen(id));
  322. return xs_fmt("%10.0f%s", object_ctime_by_md5(md5), md5);
  323. }
  324. #define MID_TO_MD5(id) (id + 10)
  325. xs_dict *mastoapi_account(const xs_dict *actor)
  326. /* converts an ActivityPub actor to a Mastodon account */
  327. {
  328. xs_dict *acct = xs_dict_new();
  329. const char *display_name = xs_dict_get(actor, "name");
  330. if (xs_is_null(display_name) || *display_name == '\0')
  331. display_name = xs_dict_get(actor, "preferredUsername");
  332. const char *id = xs_dict_get(actor, "id");
  333. const char *pub = xs_dict_get(actor, "published");
  334. xs *acct_md5 = xs_md5_hex(id, strlen(id));
  335. acct = xs_dict_append(acct, "id", acct_md5);
  336. acct = xs_dict_append(acct, "username", xs_dict_get(actor, "preferredUsername"));
  337. acct = xs_dict_append(acct, "acct", xs_dict_get(actor, "preferredUsername"));
  338. acct = xs_dict_append(acct, "display_name", display_name);
  339. if (pub)
  340. acct = xs_dict_append(acct, "created_at", pub);
  341. acct = xs_dict_append(acct, "note", xs_dict_get(actor, "summary"));
  342. acct = xs_dict_append(acct, "url", id);
  343. xs *avatar = NULL;
  344. xs_dict *av = xs_dict_get(actor, "icon");
  345. if (xs_type(av) == XSTYPE_DICT)
  346. avatar = xs_dup(xs_dict_get(av, "url"));
  347. else
  348. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  349. acct = xs_dict_append(acct, "avatar", avatar);
  350. return acct;
  351. }
  352. xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
  353. /* converts an ActivityPub note to a Mastodon status */
  354. {
  355. xs *actor = NULL;
  356. actor_get(snac, xs_dict_get(msg, "attributedTo"), &actor);
  357. /* if the author is not here, discard */
  358. if (actor == NULL)
  359. return NULL;
  360. xs *acct = mastoapi_account(actor);
  361. /** shave the yak converting an ActivityPub Note to a Mastodon status **/
  362. xs *f = xs_val_new(XSTYPE_FALSE);
  363. xs *t = xs_val_new(XSTYPE_TRUE);
  364. xs *n = xs_val_new(XSTYPE_NULL);
  365. xs *el = xs_list_new();
  366. xs *idx = NULL;
  367. xs *ixc = NULL;
  368. char *tmp;
  369. char *id = xs_dict_get(msg, "id");
  370. xs *mid = mastoapi_id(msg);
  371. xs_dict *st = xs_dict_new();
  372. st = xs_dict_append(st, "id", mid);
  373. st = xs_dict_append(st, "uri", id);
  374. st = xs_dict_append(st, "url", id);
  375. st = xs_dict_append(st, "created_at", xs_dict_get(msg, "published"));
  376. st = xs_dict_append(st, "account", acct);
  377. st = xs_dict_append(st, "content", xs_dict_get(msg, "content"));
  378. st = xs_dict_append(st, "visibility",
  379. is_msg_public(snac, msg) ? "public" : "private");
  380. tmp = xs_dict_get(msg, "sensitive");
  381. if (xs_is_null(tmp))
  382. tmp = f;
  383. st = xs_dict_append(st, "sensitive", tmp);
  384. tmp = xs_dict_get(msg, "summary");
  385. if (xs_is_null(tmp))
  386. tmp = "";
  387. st = xs_dict_append(st, "spoiler_text", tmp);
  388. /* create the list of attachments */
  389. xs *matt = xs_list_new();
  390. xs_list *att = xs_dict_get(msg, "attachment");
  391. xs_str *aobj;
  392. while (xs_list_iter(&att, &aobj)) {
  393. const char *mtype = xs_dict_get(aobj, "mediaType");
  394. if (!xs_is_null(mtype) && xs_startswith(mtype, "image/")) {
  395. xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
  396. xs *matte = xs_dict_new();
  397. matte = xs_dict_append(matte, "id", matteid);
  398. matte = xs_dict_append(matte, "type", "image");
  399. matte = xs_dict_append(matte, "url", xs_dict_get(aobj, "url"));
  400. matte = xs_dict_append(matte, "preview_url", xs_dict_get(aobj, "url"));
  401. matte = xs_dict_append(matte, "remote_url", xs_dict_get(aobj, "url"));
  402. const char *name = xs_dict_get(aobj, "name");
  403. if (xs_is_null(name))
  404. name = "";
  405. matte = xs_dict_append(matte, "description", name);
  406. matt = xs_list_append(matt, matte);
  407. }
  408. }
  409. st = xs_dict_append(st, "media_attachments", matt);
  410. st = xs_dict_append(st, "mentions", el);
  411. st = xs_dict_append(st, "tags", el);
  412. st = xs_dict_append(st, "emojis", el);
  413. xs_free(idx);
  414. xs_free(ixc);
  415. idx = object_likes(id);
  416. ixc = xs_number_new(xs_list_len(idx));
  417. st = xs_dict_append(st, "favourites_count", ixc);
  418. st = xs_dict_append(st, "favourited",
  419. xs_list_in(idx, snac->md5) != -1 ? t : f);
  420. xs_free(idx);
  421. xs_free(ixc);
  422. idx = object_announces(id);
  423. ixc = xs_number_new(xs_list_len(idx));
  424. st = xs_dict_append(st, "reblogs_count", ixc);
  425. st = xs_dict_append(st, "reblogged",
  426. xs_list_in(idx, snac->md5) != -1 ? t : f);
  427. xs_free(idx);
  428. xs_free(ixc);
  429. idx = object_children(id);
  430. ixc = xs_number_new(xs_list_len(idx));
  431. st = xs_dict_append(st, "replies_count", ixc);
  432. /* default in_reply_to values */
  433. st = xs_dict_append(st, "in_reply_to_id", n);
  434. st = xs_dict_append(st, "in_reply_to_account_id", n);
  435. tmp = xs_dict_get(msg, "inReplyTo");
  436. if (!xs_is_null(tmp)) {
  437. xs *irto = NULL;
  438. if (valid_status(object_get(tmp, &irto))) {
  439. xs *irt_mid = mastoapi_id(irto);
  440. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  441. char *at = NULL;
  442. if (!xs_is_null(at = xs_dict_get(irto, "attributedTo"))) {
  443. xs *at_md5 = xs_md5_hex(at, strlen(at));
  444. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  445. }
  446. }
  447. }
  448. st = xs_dict_append(st, "reblog", n);
  449. st = xs_dict_append(st, "poll", n);
  450. st = xs_dict_append(st, "card", n);
  451. st = xs_dict_append(st, "language", n);
  452. tmp = xs_dict_get(msg, "sourceContent");
  453. if (xs_is_null(tmp))
  454. tmp = "";
  455. st = xs_dict_append(st, "text", tmp);
  456. tmp = xs_dict_get(msg, "updated");
  457. if (xs_is_null(tmp))
  458. tmp = n;
  459. st = xs_dict_append(st, "edited_at", tmp);
  460. return st;
  461. }
  462. int process_auth_token(snac *snac, const xs_dict *req)
  463. /* processes an authorization token, if there is one */
  464. {
  465. int logged_in = 0;
  466. char *v;
  467. /* if there is an authorization field, try to validate it */
  468. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  469. xs *tokid = xs_replace(v, "Bearer ", "");
  470. xs *token = token_get(tokid);
  471. if (token != NULL) {
  472. const char *uid = xs_dict_get(token, "uid");
  473. if (!xs_is_null(uid) && user_open(snac, uid)) {
  474. logged_in = 1;
  475. srv_debug(0, xs_fmt("mastoapi auth: valid token for user %s", uid));
  476. }
  477. else
  478. srv_log(xs_fmt("mastoapi auth: corrupted token %s", tokid));
  479. }
  480. else
  481. srv_log(xs_fmt("mastoapi auth: invalid token %s", tokid));
  482. }
  483. return logged_in;
  484. }
  485. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  486. char **body, int *b_size, char **ctype)
  487. {
  488. if (!xs_startswith(q_path, "/api/v1/"))
  489. return 0;
  490. srv_debug(0, xs_fmt("mastoapi_get_handler %s", q_path));
  491. /* {
  492. xs *j = xs_json_dumps_pp(req, 4);
  493. printf("mastoapi get:\n%s\n", j);
  494. }*/
  495. int status = 404;
  496. xs_dict *args = xs_dict_get(req, "q_vars");
  497. xs *cmd = xs_replace(q_path, "/api/v1", "");
  498. snac snac1 = {0};
  499. int logged_in = process_auth_token(&snac1, req);
  500. if (strcmp(cmd, "/accounts/verify_credentials") == 0) {
  501. if (logged_in) {
  502. xs *acct = xs_dict_new();
  503. acct = xs_dict_append(acct, "id", xs_dict_get(snac1.config, "uid"));
  504. acct = xs_dict_append(acct, "username", xs_dict_get(snac1.config, "uid"));
  505. acct = xs_dict_append(acct, "acct", xs_dict_get(snac1.config, "uid"));
  506. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac1.config, "name"));
  507. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac1.config, "published"));
  508. acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio"));
  509. acct = xs_dict_append(acct, "url", snac1.actor);
  510. xs *avatar = NULL;
  511. char *av = xs_dict_get(snac1.config, "avatar");
  512. if (xs_is_null(av) || *av == '\0')
  513. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  514. else
  515. avatar = xs_dup(av);
  516. acct = xs_dict_append(acct, "avatar", avatar);
  517. *body = xs_json_dumps_pp(acct, 4);
  518. *ctype = "application/json";
  519. status = 200;
  520. }
  521. else {
  522. status = 422; // "Unprocessable entity" (no login)
  523. }
  524. }
  525. else
  526. if (xs_startswith(cmd, "/accounts/")) {
  527. /* account-related information */
  528. xs *l = xs_split(cmd, "/");
  529. const char *uid = xs_list_get(l, 2);
  530. const char *opt = xs_list_get(l, 3);
  531. if (uid != NULL) {
  532. snac snac2;
  533. xs *out = NULL;
  534. xs *actor = NULL;
  535. /* is it a local user? */
  536. if (user_open(&snac2, uid)) {
  537. if (opt == NULL) {
  538. /* account information */
  539. actor = msg_actor(&snac2);
  540. out = mastoapi_account(actor);
  541. }
  542. else
  543. if (strcmp(opt, "statuses") == 0) {
  544. /* the public list of posts of a user */
  545. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  546. xs_list *p = timeline;
  547. xs_str *v;
  548. out = xs_list_new();
  549. while (xs_list_iter(&p, &v)) {
  550. xs *msg = NULL;
  551. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  552. /* add only posts by the author */
  553. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  554. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  555. xs *st = mastoapi_status(&snac2, msg);
  556. out = xs_list_append(out, st);
  557. }
  558. }
  559. }
  560. }
  561. user_free(&snac2);
  562. }
  563. else {
  564. /* try the uid as the md5 of a possibly loaded actor */
  565. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  566. if (opt == NULL) {
  567. /* account information */
  568. out = mastoapi_account(actor);
  569. }
  570. else
  571. if (strcmp(opt, "statuses") == 0) {
  572. /* we don't serve statuses of others; return the empty list */
  573. out = xs_list_new();
  574. }
  575. }
  576. }
  577. if (out != NULL) {
  578. *body = xs_json_dumps_pp(out, 4);
  579. *ctype = "application/json";
  580. status = 200;
  581. }
  582. }
  583. }
  584. else
  585. if (strcmp(cmd, "/timelines/home") == 0) {
  586. /* the private timeline */
  587. if (logged_in) {
  588. const char *max_id = xs_dict_get(args, "max_id");
  589. const char *since_id = xs_dict_get(args, "since_id");
  590. const char *min_id = xs_dict_get(args, "min_id");
  591. const char *limit_s = xs_dict_get(args, "limit");
  592. int limit = 0;
  593. int cnt = 0;
  594. if (!xs_is_null(limit_s))
  595. limit = atoi(limit_s);
  596. if (limit == 0)
  597. limit = 20;
  598. xs *timeline = timeline_simple_list(&snac1, "private", 0, XS_ALL);
  599. xs *out = xs_list_new();
  600. xs_list *p = timeline;
  601. xs_str *v;
  602. while (xs_list_iter(&p, &v) && cnt < limit) {
  603. xs *msg = NULL;
  604. /* only return entries older that max_id */
  605. if (max_id) {
  606. if (strcmp(v, max_id) == 0)
  607. max_id = NULL;
  608. continue;
  609. }
  610. /* only returns entries newer than since_id */
  611. if (since_id) {
  612. if (strcmp(v, since_id) == 0)
  613. break;
  614. }
  615. /* only returns entries newer than min_id */
  616. /* what does really "Return results immediately newer than ID" mean? */
  617. if (min_id) {
  618. if (strcmp(v, min_id) == 0)
  619. break;
  620. }
  621. /* get the entry */
  622. if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
  623. continue;
  624. /* discard non-Notes */
  625. if (strcmp(xs_dict_get(msg, "type"), "Note") != 0)
  626. continue;
  627. /* convert the Note into a Mastodon status */
  628. xs *st = mastoapi_status(&snac1, msg);
  629. if (st != NULL)
  630. out = xs_list_append(out, st);
  631. cnt++;
  632. }
  633. *body = xs_json_dumps_pp(out, 4);
  634. *ctype = "application/json";
  635. status = 200;
  636. {
  637. xs *j = xs_json_loads(*body);
  638. if (j == NULL) {
  639. srv_debug(0, xs_fmt("mastoapi timeline: bad JSON"));
  640. srv_archive_error("mastoapi_timeline", "bad JSON", req, *body);
  641. }
  642. }
  643. srv_debug(0, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  644. }
  645. else {
  646. status = 401; // unauthorized
  647. }
  648. }
  649. else
  650. if (strcmp(cmd, "/timelines/public") == 0) {
  651. /* the public timeline (public timelines for all users) */
  652. /* TBD */
  653. *body = xs_dup("[]");
  654. *ctype = "application/json";
  655. status = 200;
  656. }
  657. else
  658. if (strcmp(cmd, "/conversations") == 0) {
  659. /* TBD */
  660. *body = xs_dup("[]");
  661. *ctype = "application/json";
  662. status = 200;
  663. }
  664. else
  665. if (strcmp(cmd, "/notifications") == 0) {
  666. /* TBD */
  667. *body = xs_dup("[]");
  668. *ctype = "application/json";
  669. status = 200;
  670. }
  671. else
  672. if (strcmp(cmd, "/filters") == 0) {
  673. /* snac will never have filters */
  674. *body = xs_dup("[]");
  675. *ctype = "application/json";
  676. status = 200;
  677. }
  678. else
  679. if (strcmp(cmd, "/favourites") == 0) {
  680. /* snac will never support a list of favourites */
  681. *body = xs_dup("[]");
  682. *ctype = "application/json";
  683. status = 200;
  684. }
  685. else
  686. if (strcmp(cmd, "/bookmarks") == 0) {
  687. /* snac does not support bookmarks */
  688. *body = xs_dup("[]");
  689. *ctype = "application/json";
  690. status = 200;
  691. }
  692. else
  693. if (strcmp(cmd, "/lists") == 0) {
  694. /* snac does not support lists */
  695. *body = xs_dup("[]");
  696. *ctype = "application/json";
  697. status = 200;
  698. }
  699. else
  700. if (strcmp(cmd, "/scheduled_statuses") == 0) {
  701. /* snac does not scheduled notes */
  702. *body = xs_dup("[]");
  703. *ctype = "application/json";
  704. status = 200;
  705. }
  706. else
  707. if (strcmp(cmd, "/follow_requests") == 0) {
  708. /* snac does not support optional follow confirmations */
  709. *body = xs_dup("[]");
  710. *ctype = "application/json";
  711. status = 200;
  712. }
  713. else
  714. if (strcmp(cmd, "/announcements") == 0) {
  715. /* snac has no announcements (yet?) */
  716. *body = xs_dup("[]");
  717. *ctype = "application/json";
  718. status = 200;
  719. }
  720. else
  721. if (strcmp(cmd, "/custom_emojis") == 0) {
  722. /* are you kidding me? */
  723. *body = xs_dup("[]");
  724. *ctype = "application/json";
  725. status = 200;
  726. }
  727. else
  728. if (strcmp(cmd, "/instance") == 0) {
  729. /* returns an instance object */
  730. xs *ins = xs_dict_new();
  731. const char *host = xs_dict_get(srv_config, "host");
  732. ins = xs_dict_append(ins, "domain", host);
  733. ins = xs_dict_append(ins, "title", host);
  734. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  735. ins = xs_dict_append(ins, "source_url", "https:/" "/comam.es/snac-source");
  736. ins = xs_dict_append(ins, "description", host);
  737. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  738. xs *d1 = xs_dict_new();
  739. d1 = xs_dict_append(d1, "url", susie);
  740. ins = xs_dict_append(ins, "thumbnail", d1);
  741. xs *d2 = xs_dict_new();
  742. d2 = xs_dict_append(d2, "email", "admin@localhost");
  743. ins = xs_dict_append(ins, "contact", d2);
  744. xs *l1 = xs_list_new();
  745. ins = xs_dict_append(ins, "rules", l1);
  746. ins = xs_dict_append(ins, "languages", l1);
  747. *body = xs_json_dumps_pp(ins, 4);
  748. *ctype = "application/json";
  749. status = 200;
  750. }
  751. else
  752. if (xs_startswith(cmd, "/statuses/")) {
  753. /* operations on a status */
  754. xs *l = xs_split(cmd, "/");
  755. const char *id = xs_list_get(l, 2);
  756. const char *op = xs_list_get(l, 3);
  757. if (!xs_is_null(id)) {
  758. xs *msg = NULL;
  759. xs *out = NULL;
  760. /* skip the 'fake' part of the id */
  761. id = MID_TO_MD5(id);
  762. if (valid_status(timeline_get_by_md5(&snac1, id, &msg))) {
  763. if (op == NULL) {
  764. /* return the status itself */
  765. out = mastoapi_status(&snac1, msg);
  766. }
  767. else
  768. if (strcmp(op, "context") == 0) {
  769. /* return ancestors and children */
  770. xs *anc = xs_list_new();
  771. xs *des = xs_list_new();
  772. xs_list *p;
  773. xs_str *v;
  774. char pid[64];
  775. /* build the [grand]parent list, moving up */
  776. strcpy(pid, id);
  777. while (object_parent(pid, pid, sizeof(pid))) {
  778. xs *m2 = NULL;
  779. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  780. xs *st = mastoapi_status(&snac1, m2);
  781. anc = xs_list_append(anc, st);
  782. }
  783. else
  784. break;
  785. }
  786. /* build the children list */
  787. xs *children = object_children(xs_dict_get(msg, "id"));
  788. p = children;
  789. while (xs_list_iter(&p, &v)) {
  790. xs *m2 = NULL;
  791. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  792. xs *st = mastoapi_status(&snac1, m2);
  793. des = xs_list_append(des, st);
  794. }
  795. }
  796. out = xs_dict_new();
  797. out = xs_dict_append(out, "ancestors", anc);
  798. out = xs_dict_append(out, "descendants", des);
  799. }
  800. else
  801. if (strcmp(op, "reblogged_by") == 0 ||
  802. strcmp(op, "favourited_by") == 0) {
  803. /* return the list of people who liked or boosted this */
  804. out = xs_list_new();
  805. xs *l = NULL;
  806. if (op[0] == 'r')
  807. l = object_announces(xs_dict_get(msg, "id"));
  808. else
  809. l = object_likes(xs_dict_get(msg, "id"));
  810. xs_list *p = l;
  811. xs_str *v;
  812. while (xs_list_iter(&p, &v)) {
  813. xs *actor2 = NULL;
  814. if (valid_status(object_get_by_md5(v, &actor2))) {
  815. xs *acct2 = mastoapi_account(actor2);
  816. out = xs_list_append(out, acct2);
  817. }
  818. }
  819. }
  820. }
  821. else
  822. srv_debug(0, xs_fmt("mastoapi status: bad id %s", id));
  823. if (out != NULL) {
  824. *body = xs_json_dumps_pp(out, 4);
  825. *ctype = "application/json";
  826. status = 200;
  827. }
  828. }
  829. }
  830. /* user cleanup */
  831. if (logged_in)
  832. user_free(&snac1);
  833. return status;
  834. }
  835. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  836. const char *payload, int p_size,
  837. char **body, int *b_size, char **ctype)
  838. {
  839. if (!xs_startswith(q_path, "/api/v1/"))
  840. return 0;
  841. srv_debug(0, xs_fmt("mastoapi_post_handler %s", q_path));
  842. /* {
  843. xs *j = xs_json_dumps_pp(req, 4);
  844. printf("mastoapi post:\n%s\n", j);
  845. }*/
  846. int status = 404;
  847. xs *args = NULL;
  848. char *i_ctype = xs_dict_get(req, "content-type");
  849. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  850. args = xs_json_loads(payload);
  851. else
  852. args = xs_dup(xs_dict_get(req, "p_vars"));
  853. if (args == NULL)
  854. return 400;
  855. /* {
  856. xs *j = xs_json_dumps_pp(args, 4);
  857. printf("%s\n", j);
  858. }*/
  859. xs *cmd = xs_replace(q_path, "/api/v1", "");
  860. snac snac = {0};
  861. int logged_in = process_auth_token(&snac, req);
  862. if (strcmp(cmd, "/apps") == 0) {
  863. const char *name = xs_dict_get(args, "client_name");
  864. const char *ruri = xs_dict_get(args, "redirect_uris");
  865. const char *scope = xs_dict_get(args, "scope");
  866. if (xs_type(ruri) == XSTYPE_LIST)
  867. ruri = xs_dict_get(ruri, 0);
  868. if (name && ruri) {
  869. xs *app = xs_dict_new();
  870. xs *id = xs_replace_i(tid(0), ".", "");
  871. xs *cid = random_str();
  872. xs *csec = random_str();
  873. xs *vkey = random_str();
  874. app = xs_dict_append(app, "name", name);
  875. app = xs_dict_append(app, "redirect_uri", ruri);
  876. app = xs_dict_append(app, "client_id", cid);
  877. app = xs_dict_append(app, "client_secret", csec);
  878. app = xs_dict_append(app, "vapid_key", vkey);
  879. app = xs_dict_append(app, "id", id);
  880. *body = xs_json_dumps_pp(app, 4);
  881. *ctype = "application/json";
  882. status = 200;
  883. app = xs_dict_append(app, "code", "");
  884. if (scope)
  885. app = xs_dict_append(app, "scope", scope);
  886. app_add(cid, app);
  887. srv_debug(0, xs_fmt("mastoapi apps: new app %s", cid));
  888. }
  889. }
  890. else
  891. if (strcmp(cmd, "/statuses") == 0) {
  892. if (logged_in) {
  893. /* post a new Note */
  894. /* TBD */
  895. }
  896. else
  897. status = 401;
  898. }
  899. else
  900. if (xs_startswith(cmd, "/statuses")) {
  901. if (logged_in) {
  902. /* operations on a status */
  903. xs *l = xs_split(cmd, "/");
  904. const char *mid = xs_list_get(l, 2);
  905. const char *op = xs_list_get(l, 3);
  906. if (!xs_is_null(mid)) {
  907. xs *msg = NULL;
  908. xs *out = NULL;
  909. /* skip the 'fake' part of the id */
  910. mid = MID_TO_MD5(mid);
  911. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  912. char *id = xs_dict_get(msg, "id");
  913. if (op == NULL) {
  914. /* no operation (?) */
  915. }
  916. else
  917. if (strcmp(op, "favourite") == 0) {
  918. xs *n_msg = msg_admiration(&snac, id, "Like");
  919. if (n_msg != NULL) {
  920. enqueue_message(&snac, n_msg);
  921. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  922. out = mastoapi_status(&snac, msg);
  923. }
  924. }
  925. else
  926. if (strcmp(op, "unfavourite") == 0) {
  927. /* snac does not support Undo+Like */
  928. }
  929. else
  930. if (strcmp(op, "reblog") == 0) {
  931. xs *n_msg = msg_admiration(&snac, id, "Announce");
  932. if (n_msg != NULL) {
  933. enqueue_message(&snac, n_msg);
  934. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  935. out = mastoapi_status(&snac, msg);
  936. }
  937. }
  938. else
  939. if (strcmp(op, "unreblog") == 0) {
  940. /* snac does not support Undo+Announce */
  941. }
  942. else
  943. if (strcmp(op, "bookmark") == 0) {
  944. /* snac does not support bookmarks */
  945. }
  946. else
  947. if (strcmp(op, "unbookmark") == 0) {
  948. /* snac does not support bookmarks */
  949. }
  950. else
  951. if (strcmp(op, "pin") == 0) {
  952. /* snac does not support pinning */
  953. }
  954. else
  955. if (strcmp(op, "unpin") == 0) {
  956. /* snac does not support pinning */
  957. }
  958. else
  959. if (strcmp(op, "mute") == 0) {
  960. /* Mastodon's mute is snac's hide */
  961. }
  962. else
  963. if (strcmp(op, "unmute") == 0) {
  964. /* Mastodon's unmute is snac's unhide */
  965. }
  966. }
  967. if (out != NULL) {
  968. *body = xs_json_dumps_pp(out, 4);
  969. *ctype = "application/json";
  970. status = 200;
  971. }
  972. }
  973. }
  974. else
  975. status = 401;
  976. }
  977. /* user cleanup */
  978. if (logged_in)
  979. user_free(&snac);
  980. return status;
  981. }