mastoapi.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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_status(snac *snac, const xs_dict *msg)
  326. /* converts an ActivityPub note to a Mastodon status */
  327. {
  328. xs *actor = NULL;
  329. actor_get(snac, xs_dict_get(msg, "attributedTo"), &actor);
  330. /* if the author is not here, discard */
  331. if (actor == NULL)
  332. return NULL;
  333. /** shave the yak converting an ActivityPub Note to a Mastodon status **/
  334. xs *acct = xs_dict_new();
  335. const char *display_name = xs_dict_get(actor, "name");
  336. if (xs_is_null(display_name) || *display_name == '\0')
  337. display_name = xs_dict_get(actor, "preferredUsername");
  338. const char *id = xs_dict_get(actor, "id");
  339. const char *pub = xs_dict_get(actor, "published");
  340. xs *acct_md5 = xs_md5_hex(id, strlen(id));
  341. acct = xs_dict_append(acct, "id", acct_md5);
  342. acct = xs_dict_append(acct, "username", xs_dict_get(actor, "preferredUsername"));
  343. acct = xs_dict_append(acct, "acct", xs_dict_get(actor, "preferredUsername"));
  344. acct = xs_dict_append(acct, "display_name", display_name);
  345. if (pub)
  346. acct = xs_dict_append(acct, "created_at", pub);
  347. acct = xs_dict_append(acct, "note", xs_dict_get(actor, "summary"));
  348. acct = xs_dict_append(acct, "url", id);
  349. xs *avatar = NULL;
  350. xs_dict *av = xs_dict_get(actor, "icon");
  351. if (xs_type(av) == XSTYPE_DICT)
  352. avatar = xs_dup(xs_dict_get(av, "url"));
  353. else
  354. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  355. acct = xs_dict_append(acct, "avatar", avatar);
  356. xs *f = xs_val_new(XSTYPE_FALSE);
  357. xs *t = xs_val_new(XSTYPE_TRUE);
  358. xs *n = xs_val_new(XSTYPE_NULL);
  359. xs *el = xs_list_new();
  360. xs *idx = NULL;
  361. xs *ixc = NULL;
  362. char *tmp;
  363. id = xs_dict_get(msg, "id");
  364. xs *mid = mastoapi_id(msg);
  365. xs_dict *st = xs_dict_new();
  366. st = xs_dict_append(st, "id", mid);
  367. st = xs_dict_append(st, "uri", id);
  368. st = xs_dict_append(st, "url", id);
  369. st = xs_dict_append(st, "created_at", xs_dict_get(msg, "published"));
  370. st = xs_dict_append(st, "account", acct);
  371. st = xs_dict_append(st, "content", xs_dict_get(msg, "content"));
  372. st = xs_dict_append(st, "visibility",
  373. is_msg_public(snac, msg) ? "public" : "private");
  374. tmp = xs_dict_get(msg, "sensitive");
  375. if (xs_is_null(tmp))
  376. tmp = f;
  377. st = xs_dict_append(st, "sensitive", tmp);
  378. tmp = xs_dict_get(msg, "summary");
  379. if (xs_is_null(tmp))
  380. tmp = "";
  381. st = xs_dict_append(st, "spoiler_text", tmp);
  382. /* create the list of attachments */
  383. xs *matt = xs_list_new();
  384. xs_list *att = xs_dict_get(msg, "attachment");
  385. xs_str *aobj;
  386. while (xs_list_iter(&att, &aobj)) {
  387. const char *mtype = xs_dict_get(aobj, "mediaType");
  388. if (!xs_is_null(mtype) && xs_startswith(mtype, "image/")) {
  389. xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
  390. xs *matte = xs_dict_new();
  391. matte = xs_dict_append(matte, "id", matteid);
  392. matte = xs_dict_append(matte, "type", "image");
  393. matte = xs_dict_append(matte, "url", xs_dict_get(aobj, "url"));
  394. matte = xs_dict_append(matte, "preview_url", xs_dict_get(aobj, "url"));
  395. matte = xs_dict_append(matte, "remote_url", xs_dict_get(aobj, "url"));
  396. matte = xs_dict_append(matte, "description", xs_dict_get(aobj, "name"));
  397. matt = xs_list_append(matt, matte);
  398. }
  399. }
  400. st = xs_dict_append(st, "media_attachments", matt);
  401. st = xs_dict_append(st, "mentions", el);
  402. st = xs_dict_append(st, "tags", el);
  403. st = xs_dict_append(st, "emojis", el);
  404. xs_free(idx);
  405. xs_free(ixc);
  406. idx = object_likes(id);
  407. ixc = xs_number_new(xs_list_len(idx));
  408. st = xs_dict_append(st, "favourites_count", ixc);
  409. st = xs_dict_append(st, "favourited",
  410. xs_list_in(idx, snac->md5) != -1 ? t : f);
  411. xs_free(idx);
  412. xs_free(ixc);
  413. idx = object_announces(id);
  414. ixc = xs_number_new(xs_list_len(idx));
  415. st = xs_dict_append(st, "reblogs_count", ixc);
  416. st = xs_dict_append(st, "reblogged",
  417. xs_list_in(idx, snac->md5) != -1 ? t : f);
  418. xs_free(idx);
  419. xs_free(ixc);
  420. idx = object_children(id);
  421. ixc = xs_number_new(xs_list_len(idx));
  422. st = xs_dict_append(st, "replies_count", ixc);
  423. /* default in_reply_to values */
  424. st = xs_dict_append(st, "in_reply_to_id", n);
  425. st = xs_dict_append(st, "in_reply_to_account_id", n);
  426. tmp = xs_dict_get(msg, "inReplyTo");
  427. if (!xs_is_null(tmp)) {
  428. xs *irto = NULL;
  429. if (valid_status(object_get(tmp, &irto))) {
  430. xs *irt_mid = mastoapi_id(irto);
  431. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  432. char *at = NULL;
  433. if (!xs_is_null(at = xs_dict_get(irto, "attributedTo"))) {
  434. xs *at_md5 = xs_md5_hex(at, strlen(at));
  435. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  436. }
  437. }
  438. }
  439. st = xs_dict_append(st, "reblog", n);
  440. st = xs_dict_append(st, "poll", n);
  441. st = xs_dict_append(st, "card", n);
  442. st = xs_dict_append(st, "language", n);
  443. tmp = xs_dict_get(msg, "sourceContent");
  444. if (xs_is_null(tmp))
  445. tmp = "";
  446. st = xs_dict_append(st, "text", tmp);
  447. tmp = xs_dict_get(msg, "updated");
  448. if (xs_is_null(tmp))
  449. tmp = n;
  450. st = xs_dict_append(st, "edited_at", tmp);
  451. return st;
  452. }
  453. int process_auth_token(snac *snac, const xs_dict *req)
  454. /* processes an authorization token, if there is one */
  455. {
  456. int logged_in = 0;
  457. char *v;
  458. /* if there is an authorization field, try to validate it */
  459. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  460. xs *tokid = xs_replace(v, "Bearer ", "");
  461. xs *token = token_get(tokid);
  462. if (token != NULL) {
  463. const char *uid = xs_dict_get(token, "uid");
  464. if (!xs_is_null(uid) && user_open(snac, uid)) {
  465. logged_in = 1;
  466. srv_debug(0, xs_fmt("mastoapi auth: valid token for user %s", uid));
  467. }
  468. else
  469. srv_log(xs_fmt("mastoapi auth: corrupted token %s", tokid));
  470. }
  471. else
  472. srv_log(xs_fmt("mastoapi auth: invalid token %s", tokid));
  473. }
  474. return logged_in;
  475. }
  476. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  477. char **body, int *b_size, char **ctype)
  478. {
  479. if (!xs_startswith(q_path, "/api/v1/"))
  480. return 0;
  481. srv_debug(0, xs_fmt("mastoapi_get_handler %s", q_path));
  482. /* {
  483. xs *j = xs_json_dumps_pp(req, 4);
  484. printf("mastoapi get:\n%s\n", j);
  485. }*/
  486. int status = 404;
  487. xs_dict *args = xs_dict_get(req, "q_vars");
  488. xs *cmd = xs_replace(q_path, "/api/v1", "");
  489. snac snac = {0};
  490. int logged_in = process_auth_token(&snac, req);;
  491. if (strcmp(cmd, "/accounts/verify_credentials") == 0) {
  492. if (logged_in) {
  493. xs *acct = xs_dict_new();
  494. acct = xs_dict_append(acct, "id", xs_dict_get(snac.config, "uid"));
  495. acct = xs_dict_append(acct, "username", xs_dict_get(snac.config, "uid"));
  496. acct = xs_dict_append(acct, "acct", xs_dict_get(snac.config, "uid"));
  497. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac.config, "name"));
  498. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac.config, "published"));
  499. acct = xs_dict_append(acct, "note", xs_dict_get(snac.config, "bio"));
  500. acct = xs_dict_append(acct, "url", snac.actor);
  501. xs *avatar = NULL;
  502. char *av = xs_dict_get(snac.config, "avatar");
  503. if (xs_is_null(av) || *av == '\0')
  504. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  505. else
  506. avatar = xs_dup(av);
  507. acct = xs_dict_append(acct, "avatar", avatar);
  508. *body = xs_json_dumps_pp(acct, 4);
  509. *ctype = "application/json";
  510. status = 200;
  511. }
  512. else {
  513. status = 422; // "Unprocessable entity" (no login)
  514. }
  515. }
  516. else
  517. if (strcmp(cmd, "/timelines/home") == 0) {
  518. /* the private timeline */
  519. if (logged_in) {
  520. const char *max_id = xs_dict_get(args, "max_id");
  521. const char *since_id = xs_dict_get(args, "since_id");
  522. const char *min_id = xs_dict_get(args, "min_id");
  523. const char *limit_s = xs_dict_get(args, "limit");
  524. int limit = 0;
  525. int cnt = 0;
  526. if (!xs_is_null(limit_s))
  527. limit = atoi(limit_s);
  528. if (limit == 0)
  529. limit = 20;
  530. xs *timeline = timeline_simple_list(&snac, "private", 0, XS_ALL);
  531. xs *out = xs_list_new();
  532. xs_list *p = timeline;
  533. xs_str *v;
  534. while (xs_list_iter(&p, &v) && cnt < limit) {
  535. xs *msg = NULL;
  536. /* only return entries older that max_id */
  537. if (max_id) {
  538. if (strcmp(v, max_id) == 0)
  539. max_id = NULL;
  540. continue;
  541. }
  542. /* only returns entries newer than since_id */
  543. if (since_id) {
  544. if (strcmp(v, since_id) == 0)
  545. break;
  546. }
  547. /* only returns entries newer than min_id */
  548. /* what does really "Return results immediately newer than ID" mean? */
  549. if (min_id) {
  550. if (strcmp(v, min_id) == 0)
  551. break;
  552. }
  553. /* get the entry */
  554. if (!valid_status(timeline_get_by_md5(&snac, v, &msg)))
  555. continue;
  556. /* discard non-Notes */
  557. if (strcmp(xs_dict_get(msg, "type"), "Note") != 0)
  558. continue;
  559. /* convert the Note into a Mastodon status */
  560. xs *st = mastoapi_status(&snac, msg);
  561. if (st != NULL)
  562. out = xs_list_append(out, st);
  563. cnt++;
  564. }
  565. *body = xs_json_dumps_pp(out, 4);
  566. *ctype = "application/json";
  567. status = 200;
  568. srv_debug(0, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  569. }
  570. else {
  571. status = 401; // unauthorized
  572. }
  573. }
  574. else
  575. if (strcmp(cmd, "/timelines/public") == 0) {
  576. /* the public timeline (public timelines for all users) */
  577. /* TBD */
  578. *body = xs_dup("[]");
  579. *ctype = "application/json";
  580. status = 200;
  581. }
  582. else
  583. if (strcmp(cmd, "/conversations") == 0) {
  584. /* TBD */
  585. *body = xs_dup("[]");
  586. *ctype = "application/json";
  587. status = 200;
  588. }
  589. else
  590. if (strcmp(cmd, "/notifications") == 0) {
  591. /* TBD */
  592. *body = xs_dup("[]");
  593. *ctype = "application/json";
  594. status = 200;
  595. }
  596. else
  597. if (strcmp(cmd, "/filters") == 0) {
  598. /* snac will never have filters */
  599. *body = xs_dup("[]");
  600. *ctype = "application/json";
  601. status = 200;
  602. }
  603. else
  604. if (strcmp(cmd, "/favourites") == 0) {
  605. /* snac will never support a list of favourites */
  606. *body = xs_dup("[]");
  607. *ctype = "application/json";
  608. status = 200;
  609. }
  610. else
  611. if (strcmp(cmd, "/bookmarks") == 0) {
  612. /* snac does not support bookmarks */
  613. *body = xs_dup("[]");
  614. *ctype = "application/json";
  615. status = 200;
  616. }
  617. else
  618. if (strcmp(cmd, "/lists") == 0) {
  619. /* snac does not support lists */
  620. *body = xs_dup("[]");
  621. *ctype = "application/json";
  622. status = 200;
  623. }
  624. else
  625. if (strcmp(cmd, "/scheduled_statuses") == 0) {
  626. /* snac does not scheduled notes */
  627. *body = xs_dup("[]");
  628. *ctype = "application/json";
  629. status = 200;
  630. }
  631. else
  632. if (strcmp(cmd, "/follow_requests") == 0) {
  633. /* snac does not support optional follow confirmations */
  634. *body = xs_dup("[]");
  635. *ctype = "application/json";
  636. status = 200;
  637. }
  638. else
  639. if (strcmp(cmd, "/announcements") == 0) {
  640. /* snac has no announcements (yet?) */
  641. *body = xs_dup("[]");
  642. *ctype = "application/json";
  643. status = 200;
  644. }
  645. else
  646. if (strcmp(cmd, "/custom_emojis") == 0) {
  647. /* are you kidding me? */
  648. *body = xs_dup("[]");
  649. *ctype = "application/json";
  650. status = 200;
  651. }
  652. else
  653. if (strcmp(cmd, "/instance") == 0) {
  654. /* returns an instance object */
  655. xs *ins = xs_dict_new();
  656. const char *host = xs_dict_get(srv_config, "host");
  657. ins = xs_dict_append(ins, "domain", host);
  658. ins = xs_dict_append(ins, "title", host);
  659. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  660. ins = xs_dict_append(ins, "source_url", "https:/" "/comam.es/snac-source");
  661. ins = xs_dict_append(ins, "description", host);
  662. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  663. xs *d1 = xs_dict_new();
  664. d1 = xs_dict_append(d1, "url", susie);
  665. ins = xs_dict_append(ins, "thumbnail", d1);
  666. xs *d2 = xs_dict_new();
  667. d2 = xs_dict_append(d2, "email", "admin@localhost");
  668. ins = xs_dict_append(ins, "contact", d2);
  669. xs *l1 = xs_list_new();
  670. ins = xs_dict_append(ins, "rules", l1);
  671. ins = xs_dict_append(ins, "languages", l1);
  672. *body = xs_json_dumps_pp(ins, 4);
  673. *ctype = "application/json";
  674. status = 200;
  675. }
  676. else
  677. if (xs_startswith(cmd, "/statuses/")) {
  678. /* operations on a status */
  679. xs *l = xs_split(cmd, "/");
  680. const char *id = xs_list_get(l, 2);
  681. const char *op = xs_list_get(l, 3);
  682. if (!xs_is_null(id)) {
  683. xs *msg = NULL;
  684. xs *out = NULL;
  685. /* skip the fake part of the id (the date) */
  686. id = MID_TO_MD5(id);
  687. if (valid_status(timeline_get_by_md5(&snac, id, &msg))) {
  688. if (op == NULL) {
  689. /* return the status itself */
  690. out = mastoapi_status(&snac, msg);
  691. }
  692. else
  693. if (strcmp(op, "context") == 0) {
  694. /* return ancestors and children */
  695. xs *anc = xs_list_new();
  696. xs *des = xs_list_new();
  697. xs_list *p;
  698. xs_str *v;
  699. char pid[64];
  700. /* build the [grand]parent list, moving up */
  701. strcpy(pid, id);
  702. while (object_parent(pid, pid, sizeof(pid))) {
  703. xs *m2 = NULL;
  704. if (valid_status(timeline_get_by_md5(&snac, pid, &m2))) {
  705. xs *st = mastoapi_status(&snac, m2);
  706. anc = xs_list_append(anc, st);
  707. }
  708. else
  709. break;
  710. }
  711. /* build the children list */
  712. xs *children = object_children(xs_dict_get(msg, "id"));
  713. p = children;
  714. while (xs_list_iter(&p, &v)) {
  715. xs *m2 = NULL;
  716. if (valid_status(timeline_get_by_md5(&snac, v, &m2))) {
  717. xs *st = mastoapi_status(&snac, m2);
  718. des = xs_list_append(des, st);
  719. }
  720. }
  721. out = xs_dict_new();
  722. out = xs_dict_append(out, "ancestors", anc);
  723. out = xs_dict_append(out, "descendants", des);
  724. }
  725. else
  726. if (strcmp(op, "reblogged_by") == 0) {
  727. /* return the list of boosts */
  728. }
  729. else
  730. if (strcmp(op, "favourited_by") == 0) {
  731. /* return the list of likes */
  732. }
  733. }
  734. else
  735. srv_debug(0, xs_fmt("mastoapi status: bad id %s", id));
  736. if (out != NULL) {
  737. *body = xs_json_dumps_pp(out, 4);
  738. *ctype = "application/json";
  739. status = 200;
  740. }
  741. }
  742. }
  743. /* user cleanup */
  744. if (logged_in)
  745. user_free(&snac);
  746. return status;
  747. }
  748. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  749. const char *payload, int p_size,
  750. char **body, int *b_size, char **ctype)
  751. {
  752. if (!xs_startswith(q_path, "/api/v1/"))
  753. return 0;
  754. srv_debug(0, xs_fmt("mastoapi_post_handler %s", q_path));
  755. /* {
  756. xs *j = xs_json_dumps_pp(req, 4);
  757. printf("mastoapi post:\n%s\n", j);
  758. }*/
  759. int status = 404;
  760. xs *args = NULL;
  761. char *i_ctype = xs_dict_get(req, "content-type");
  762. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  763. args = xs_json_loads(payload);
  764. else
  765. args = xs_dup(xs_dict_get(req, "p_vars"));
  766. if (args == NULL)
  767. return 400;
  768. /* {
  769. xs *j = xs_json_dumps_pp(args, 4);
  770. printf("%s\n", j);
  771. }*/
  772. xs *cmd = xs_replace(q_path, "/api/v1", "");
  773. snac snac = {0};
  774. int logged_in = process_auth_token(&snac, req);;
  775. if (strcmp(cmd, "/apps") == 0) {
  776. const char *name = xs_dict_get(args, "client_name");
  777. const char *ruri = xs_dict_get(args, "redirect_uris");
  778. const char *scope = xs_dict_get(args, "scope");
  779. if (xs_type(ruri) == XSTYPE_LIST)
  780. ruri = xs_dict_get(ruri, 0);
  781. if (name && ruri) {
  782. xs *app = xs_dict_new();
  783. xs *id = xs_replace_i(tid(0), ".", "");
  784. xs *cid = random_str();
  785. xs *csec = random_str();
  786. xs *vkey = random_str();
  787. app = xs_dict_append(app, "name", name);
  788. app = xs_dict_append(app, "redirect_uri", ruri);
  789. app = xs_dict_append(app, "client_id", cid);
  790. app = xs_dict_append(app, "client_secret", csec);
  791. app = xs_dict_append(app, "vapid_key", vkey);
  792. app = xs_dict_append(app, "id", id);
  793. *body = xs_json_dumps_pp(app, 4);
  794. *ctype = "application/json";
  795. status = 200;
  796. app = xs_dict_append(app, "code", "");
  797. if (scope)
  798. app = xs_dict_append(app, "scope", scope);
  799. app_add(cid, app);
  800. srv_debug(0, xs_fmt("mastoapi apps: new app %s", cid));
  801. }
  802. }
  803. else
  804. if (strcmp(cmd, "/statuses") == 0) {
  805. if (logged_in) {
  806. /* post a new Note */
  807. /* TBD */
  808. }
  809. else
  810. status = 401;
  811. }
  812. else
  813. if (xs_startswith(cmd, "/statuses")) {
  814. if (logged_in) {
  815. /* operations on a status */
  816. xs *l = xs_split(cmd, "/");
  817. const char *mid = xs_list_get(l, 2);
  818. const char *op = xs_list_get(l, 3);
  819. if (!xs_is_null(mid)) {
  820. xs *msg = NULL;
  821. xs *out = NULL;
  822. /* skip the fake part of the id (the date) */
  823. mid = MID_TO_MD5(mid);
  824. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  825. char *id = xs_dict_get(msg, "id");
  826. if (op == NULL) {
  827. /* no operation (?) */
  828. }
  829. else
  830. if (strcmp(op, "favourite") == 0) {
  831. xs *n_msg = msg_admiration(&snac, id, "Like");
  832. if (n_msg != NULL) {
  833. enqueue_message(&snac, n_msg);
  834. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  835. out = mastoapi_status(&snac, msg);
  836. }
  837. }
  838. else
  839. if (strcmp(op, "unfavourite") == 0) {
  840. /* snac does not support Undo+Like */
  841. }
  842. else
  843. if (strcmp(op, "reblog") == 0) {
  844. xs *n_msg = msg_admiration(&snac, id, "Announce");
  845. if (n_msg != NULL) {
  846. enqueue_message(&snac, n_msg);
  847. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  848. out = mastoapi_status(&snac, msg);
  849. }
  850. }
  851. else
  852. if (strcmp(op, "unreblog") == 0) {
  853. /* snac does not support Undo+Announce */
  854. }
  855. else
  856. if (strcmp(op, "bookmark") == 0) {
  857. /* snac does not support bookmarks */
  858. }
  859. else
  860. if (strcmp(op, "unbookmark") == 0) {
  861. /* snac does not support bookmarks */
  862. }
  863. else
  864. if (strcmp(op, "pin") == 0) {
  865. /* snac does not support pinning */
  866. }
  867. else
  868. if (strcmp(op, "unpin") == 0) {
  869. /* snac does not support pinning */
  870. }
  871. else
  872. if (strcmp(op, "mute") == 0) {
  873. /* Mastodon's mute is snac's hide */
  874. }
  875. else
  876. if (strcmp(op, "unmute") == 0) {
  877. /* Mastodon's unmute is snac's unhide */
  878. }
  879. }
  880. if (out != NULL) {
  881. *body = xs_json_dumps_pp(out, 4);
  882. *ctype = "application/json";
  883. status = 200;
  884. }
  885. }
  886. }
  887. else
  888. status = 401;
  889. }
  890. return status;
  891. }