mastoapi.c 34 KB

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