mastoapi.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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(snac *snac, 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(snac, 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. snac snac2;
  532. if (uid != NULL && user_open(&snac2, uid)) {
  533. xs *out = NULL;
  534. if (opt == NULL) {
  535. /* account information */
  536. xs *actor = msg_actor(&snac2);
  537. out = mastoapi_account(&snac2, actor);
  538. }
  539. else
  540. if (strcmp(opt, "statuses") == 0) {
  541. /* the public list of posts of a user */
  542. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  543. xs_list *p = timeline;
  544. xs_str *v;
  545. out = xs_list_new();
  546. while (xs_list_iter(&p, &v)) {
  547. xs *msg = NULL;
  548. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  549. /* add only posts by the author */
  550. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  551. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  552. xs *st = mastoapi_status(&snac2, msg);
  553. out = xs_list_append(out, st);
  554. }
  555. }
  556. }
  557. }
  558. if (out != NULL) {
  559. *body = xs_json_dumps_pp(out, 4);
  560. *ctype = "application/json";
  561. status = 200;
  562. }
  563. user_free(&snac2);
  564. }
  565. }
  566. else
  567. if (strcmp(cmd, "/timelines/home") == 0) {
  568. /* the private timeline */
  569. if (logged_in) {
  570. const char *max_id = xs_dict_get(args, "max_id");
  571. const char *since_id = xs_dict_get(args, "since_id");
  572. const char *min_id = xs_dict_get(args, "min_id");
  573. const char *limit_s = xs_dict_get(args, "limit");
  574. int limit = 0;
  575. int cnt = 0;
  576. if (!xs_is_null(limit_s))
  577. limit = atoi(limit_s);
  578. if (limit == 0)
  579. limit = 20;
  580. xs *timeline = timeline_simple_list(&snac1, "private", 0, XS_ALL);
  581. xs *out = xs_list_new();
  582. xs_list *p = timeline;
  583. xs_str *v;
  584. while (xs_list_iter(&p, &v) && cnt < limit) {
  585. xs *msg = NULL;
  586. /* only return entries older that max_id */
  587. if (max_id) {
  588. if (strcmp(v, max_id) == 0)
  589. max_id = NULL;
  590. continue;
  591. }
  592. /* only returns entries newer than since_id */
  593. if (since_id) {
  594. if (strcmp(v, since_id) == 0)
  595. break;
  596. }
  597. /* only returns entries newer than min_id */
  598. /* what does really "Return results immediately newer than ID" mean? */
  599. if (min_id) {
  600. if (strcmp(v, min_id) == 0)
  601. break;
  602. }
  603. /* get the entry */
  604. if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
  605. continue;
  606. /* discard non-Notes */
  607. if (strcmp(xs_dict_get(msg, "type"), "Note") != 0)
  608. continue;
  609. /* convert the Note into a Mastodon status */
  610. xs *st = mastoapi_status(&snac1, msg);
  611. if (st != NULL)
  612. out = xs_list_append(out, st);
  613. cnt++;
  614. }
  615. *body = xs_json_dumps_pp(out, 4);
  616. *ctype = "application/json";
  617. status = 200;
  618. {
  619. xs *j = xs_json_loads(*body);
  620. if (j == NULL) {
  621. srv_debug(0, xs_fmt("mastoapi timeline: bad JSON"));
  622. srv_archive_error("mastoapi_timeline", "bad JSON", req, *body);
  623. }
  624. }
  625. srv_debug(0, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  626. }
  627. else {
  628. status = 401; // unauthorized
  629. }
  630. }
  631. else
  632. if (strcmp(cmd, "/timelines/public") == 0) {
  633. /* the public timeline (public timelines for all users) */
  634. /* TBD */
  635. *body = xs_dup("[]");
  636. *ctype = "application/json";
  637. status = 200;
  638. }
  639. else
  640. if (strcmp(cmd, "/conversations") == 0) {
  641. /* TBD */
  642. *body = xs_dup("[]");
  643. *ctype = "application/json";
  644. status = 200;
  645. }
  646. else
  647. if (strcmp(cmd, "/notifications") == 0) {
  648. /* TBD */
  649. *body = xs_dup("[]");
  650. *ctype = "application/json";
  651. status = 200;
  652. }
  653. else
  654. if (strcmp(cmd, "/filters") == 0) {
  655. /* snac will never have filters */
  656. *body = xs_dup("[]");
  657. *ctype = "application/json";
  658. status = 200;
  659. }
  660. else
  661. if (strcmp(cmd, "/favourites") == 0) {
  662. /* snac will never support a list of favourites */
  663. *body = xs_dup("[]");
  664. *ctype = "application/json";
  665. status = 200;
  666. }
  667. else
  668. if (strcmp(cmd, "/bookmarks") == 0) {
  669. /* snac does not support bookmarks */
  670. *body = xs_dup("[]");
  671. *ctype = "application/json";
  672. status = 200;
  673. }
  674. else
  675. if (strcmp(cmd, "/lists") == 0) {
  676. /* snac does not support lists */
  677. *body = xs_dup("[]");
  678. *ctype = "application/json";
  679. status = 200;
  680. }
  681. else
  682. if (strcmp(cmd, "/scheduled_statuses") == 0) {
  683. /* snac does not scheduled notes */
  684. *body = xs_dup("[]");
  685. *ctype = "application/json";
  686. status = 200;
  687. }
  688. else
  689. if (strcmp(cmd, "/follow_requests") == 0) {
  690. /* snac does not support optional follow confirmations */
  691. *body = xs_dup("[]");
  692. *ctype = "application/json";
  693. status = 200;
  694. }
  695. else
  696. if (strcmp(cmd, "/announcements") == 0) {
  697. /* snac has no announcements (yet?) */
  698. *body = xs_dup("[]");
  699. *ctype = "application/json";
  700. status = 200;
  701. }
  702. else
  703. if (strcmp(cmd, "/custom_emojis") == 0) {
  704. /* are you kidding me? */
  705. *body = xs_dup("[]");
  706. *ctype = "application/json";
  707. status = 200;
  708. }
  709. else
  710. if (strcmp(cmd, "/instance") == 0) {
  711. /* returns an instance object */
  712. xs *ins = xs_dict_new();
  713. const char *host = xs_dict_get(srv_config, "host");
  714. ins = xs_dict_append(ins, "domain", host);
  715. ins = xs_dict_append(ins, "title", host);
  716. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  717. ins = xs_dict_append(ins, "source_url", "https:/" "/comam.es/snac-source");
  718. ins = xs_dict_append(ins, "description", host);
  719. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  720. xs *d1 = xs_dict_new();
  721. d1 = xs_dict_append(d1, "url", susie);
  722. ins = xs_dict_append(ins, "thumbnail", d1);
  723. xs *d2 = xs_dict_new();
  724. d2 = xs_dict_append(d2, "email", "admin@localhost");
  725. ins = xs_dict_append(ins, "contact", d2);
  726. xs *l1 = xs_list_new();
  727. ins = xs_dict_append(ins, "rules", l1);
  728. ins = xs_dict_append(ins, "languages", l1);
  729. *body = xs_json_dumps_pp(ins, 4);
  730. *ctype = "application/json";
  731. status = 200;
  732. }
  733. else
  734. if (xs_startswith(cmd, "/statuses/")) {
  735. /* operations on a status */
  736. xs *l = xs_split(cmd, "/");
  737. const char *id = xs_list_get(l, 2);
  738. const char *op = xs_list_get(l, 3);
  739. if (!xs_is_null(id)) {
  740. xs *msg = NULL;
  741. xs *out = NULL;
  742. /* skip the 'fake' part of the id */
  743. id = MID_TO_MD5(id);
  744. if (valid_status(timeline_get_by_md5(&snac1, id, &msg))) {
  745. if (op == NULL) {
  746. /* return the status itself */
  747. out = mastoapi_status(&snac1, msg);
  748. }
  749. else
  750. if (strcmp(op, "context") == 0) {
  751. /* return ancestors and children */
  752. xs *anc = xs_list_new();
  753. xs *des = xs_list_new();
  754. xs_list *p;
  755. xs_str *v;
  756. char pid[64];
  757. /* build the [grand]parent list, moving up */
  758. strcpy(pid, id);
  759. while (object_parent(pid, pid, sizeof(pid))) {
  760. xs *m2 = NULL;
  761. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  762. xs *st = mastoapi_status(&snac1, m2);
  763. anc = xs_list_append(anc, st);
  764. }
  765. else
  766. break;
  767. }
  768. /* build the children list */
  769. xs *children = object_children(xs_dict_get(msg, "id"));
  770. p = children;
  771. while (xs_list_iter(&p, &v)) {
  772. xs *m2 = NULL;
  773. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  774. xs *st = mastoapi_status(&snac1, m2);
  775. des = xs_list_append(des, st);
  776. }
  777. }
  778. out = xs_dict_new();
  779. out = xs_dict_append(out, "ancestors", anc);
  780. out = xs_dict_append(out, "descendants", des);
  781. }
  782. else
  783. if (strcmp(op, "reblogged_by") == 0 ||
  784. strcmp(op, "favourited_by") == 0) {
  785. /* return the list of people who liked or boosted this */
  786. out = xs_list_new();
  787. xs *l = NULL;
  788. if (op[0] == 'r')
  789. l = object_announces(xs_dict_get(msg, "id"));
  790. else
  791. l = object_likes(xs_dict_get(msg, "id"));
  792. xs_list *p = l;
  793. xs_str *v;
  794. while (xs_list_iter(&p, &v)) {
  795. xs *actor2 = NULL;
  796. if (valid_status(object_get_by_md5(v, &actor2))) {
  797. xs *acct2 = mastoapi_account(&snac1, actor2);
  798. out = xs_list_append(out, acct2);
  799. }
  800. }
  801. }
  802. }
  803. else
  804. srv_debug(0, xs_fmt("mastoapi status: bad id %s", id));
  805. if (out != NULL) {
  806. *body = xs_json_dumps_pp(out, 4);
  807. *ctype = "application/json";
  808. status = 200;
  809. }
  810. }
  811. }
  812. /* user cleanup */
  813. if (logged_in)
  814. user_free(&snac1);
  815. return status;
  816. }
  817. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  818. const char *payload, int p_size,
  819. char **body, int *b_size, char **ctype)
  820. {
  821. if (!xs_startswith(q_path, "/api/v1/"))
  822. return 0;
  823. srv_debug(0, xs_fmt("mastoapi_post_handler %s", q_path));
  824. /* {
  825. xs *j = xs_json_dumps_pp(req, 4);
  826. printf("mastoapi post:\n%s\n", j);
  827. }*/
  828. int status = 404;
  829. xs *args = NULL;
  830. char *i_ctype = xs_dict_get(req, "content-type");
  831. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  832. args = xs_json_loads(payload);
  833. else
  834. args = xs_dup(xs_dict_get(req, "p_vars"));
  835. if (args == NULL)
  836. return 400;
  837. /* {
  838. xs *j = xs_json_dumps_pp(args, 4);
  839. printf("%s\n", j);
  840. }*/
  841. xs *cmd = xs_replace(q_path, "/api/v1", "");
  842. snac snac = {0};
  843. int logged_in = process_auth_token(&snac, req);
  844. if (strcmp(cmd, "/apps") == 0) {
  845. const char *name = xs_dict_get(args, "client_name");
  846. const char *ruri = xs_dict_get(args, "redirect_uris");
  847. const char *scope = xs_dict_get(args, "scope");
  848. if (xs_type(ruri) == XSTYPE_LIST)
  849. ruri = xs_dict_get(ruri, 0);
  850. if (name && ruri) {
  851. xs *app = xs_dict_new();
  852. xs *id = xs_replace_i(tid(0), ".", "");
  853. xs *cid = random_str();
  854. xs *csec = random_str();
  855. xs *vkey = random_str();
  856. app = xs_dict_append(app, "name", name);
  857. app = xs_dict_append(app, "redirect_uri", ruri);
  858. app = xs_dict_append(app, "client_id", cid);
  859. app = xs_dict_append(app, "client_secret", csec);
  860. app = xs_dict_append(app, "vapid_key", vkey);
  861. app = xs_dict_append(app, "id", id);
  862. *body = xs_json_dumps_pp(app, 4);
  863. *ctype = "application/json";
  864. status = 200;
  865. app = xs_dict_append(app, "code", "");
  866. if (scope)
  867. app = xs_dict_append(app, "scope", scope);
  868. app_add(cid, app);
  869. srv_debug(0, xs_fmt("mastoapi apps: new app %s", cid));
  870. }
  871. }
  872. else
  873. if (strcmp(cmd, "/statuses") == 0) {
  874. if (logged_in) {
  875. /* post a new Note */
  876. /* TBD */
  877. }
  878. else
  879. status = 401;
  880. }
  881. else
  882. if (xs_startswith(cmd, "/statuses")) {
  883. if (logged_in) {
  884. /* operations on a status */
  885. xs *l = xs_split(cmd, "/");
  886. const char *mid = xs_list_get(l, 2);
  887. const char *op = xs_list_get(l, 3);
  888. if (!xs_is_null(mid)) {
  889. xs *msg = NULL;
  890. xs *out = NULL;
  891. /* skip the 'fake' part of the id */
  892. mid = MID_TO_MD5(mid);
  893. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  894. char *id = xs_dict_get(msg, "id");
  895. if (op == NULL) {
  896. /* no operation (?) */
  897. }
  898. else
  899. if (strcmp(op, "favourite") == 0) {
  900. xs *n_msg = msg_admiration(&snac, id, "Like");
  901. if (n_msg != NULL) {
  902. enqueue_message(&snac, n_msg);
  903. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  904. out = mastoapi_status(&snac, msg);
  905. }
  906. }
  907. else
  908. if (strcmp(op, "unfavourite") == 0) {
  909. /* snac does not support Undo+Like */
  910. }
  911. else
  912. if (strcmp(op, "reblog") == 0) {
  913. xs *n_msg = msg_admiration(&snac, id, "Announce");
  914. if (n_msg != NULL) {
  915. enqueue_message(&snac, n_msg);
  916. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  917. out = mastoapi_status(&snac, msg);
  918. }
  919. }
  920. else
  921. if (strcmp(op, "unreblog") == 0) {
  922. /* snac does not support Undo+Announce */
  923. }
  924. else
  925. if (strcmp(op, "bookmark") == 0) {
  926. /* snac does not support bookmarks */
  927. }
  928. else
  929. if (strcmp(op, "unbookmark") == 0) {
  930. /* snac does not support bookmarks */
  931. }
  932. else
  933. if (strcmp(op, "pin") == 0) {
  934. /* snac does not support pinning */
  935. }
  936. else
  937. if (strcmp(op, "unpin") == 0) {
  938. /* snac does not support pinning */
  939. }
  940. else
  941. if (strcmp(op, "mute") == 0) {
  942. /* Mastodon's mute is snac's hide */
  943. }
  944. else
  945. if (strcmp(op, "unmute") == 0) {
  946. /* Mastodon's unmute is snac's unhide */
  947. }
  948. }
  949. if (out != NULL) {
  950. *body = xs_json_dumps_pp(out, 4);
  951. *ctype = "application/json";
  952. status = 200;
  953. }
  954. }
  955. }
  956. else
  957. status = 401;
  958. }
  959. /* user cleanup */
  960. if (logged_in)
  961. user_free(&snac);
  962. return status;
  963. }