mastoapi.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  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(1, 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. char *i_ctype = xs_dict_get(req, "content-type");
  176. xs *args = NULL;
  177. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  178. args = xs_json_loads(payload);
  179. else
  180. args = xs_dup(xs_dict_get(req, "p_vars"));
  181. xs *cmd = xs_replace(q_path, "/oauth", "");
  182. srv_debug(1, xs_fmt("oauth_post_handler %s", q_path));
  183. if (strcmp(cmd, "/x-snac-login") == 0) {
  184. const char *login = xs_dict_get(args, "login");
  185. const char *passwd = xs_dict_get(args, "passwd");
  186. const char *redir = xs_dict_get(args, "redir");
  187. const char *cid = xs_dict_get(args, "cid");
  188. const char *state = xs_dict_get(args, "state");
  189. const char *host = xs_dict_get(srv_config, "host");
  190. /* by default, generate another login form with an error */
  191. *body = xs_fmt(login_page, host, "LOGIN INCORRECT", host, redir, cid, state, USER_AGENT);
  192. *ctype = "text/html";
  193. status = 200;
  194. if (login && passwd && redir && cid) {
  195. snac snac;
  196. if (user_open(&snac, login)) {
  197. /* check the login + password */
  198. if (check_password(login, passwd,
  199. xs_dict_get(snac.config, "passwd"))) {
  200. /* success! redirect to the desired uri */
  201. xs *code = random_str();
  202. xs_free(*body);
  203. *body = xs_fmt("%s?code=%s", redir, code);
  204. status = 303;
  205. /* if there is a state, add it */
  206. if (!xs_is_null(state) && *state) {
  207. *body = xs_str_cat(*body, "&state=");
  208. *body = xs_str_cat(*body, state);
  209. }
  210. srv_log(xs_fmt("oauth x-snac-login: '%s' success, redirect to %s",
  211. login, *body));
  212. /* assign the login to the app */
  213. xs *app = app_get(cid);
  214. if (app != NULL) {
  215. app = xs_dict_set(app, "uid", login);
  216. app = xs_dict_set(app, "code", code);
  217. app_add(cid, app);
  218. }
  219. else
  220. srv_log(xs_fmt("oauth x-snac-login: error getting app %s", cid));
  221. }
  222. else
  223. srv_debug(1, xs_fmt("oauth x-snac-login: login '%s' incorrect", login));
  224. user_free(&snac);
  225. }
  226. else
  227. srv_debug(1, xs_fmt("oauth x-snac-login: bad user '%s'", login));
  228. }
  229. else
  230. srv_debug(1, xs_fmt("oauth x-snac-login: invalid or unset arguments"));
  231. }
  232. else
  233. if (strcmp(cmd, "/token") == 0) {
  234. xs *wrk = NULL;
  235. const char *gtype = xs_dict_get(args, "grant_type");
  236. const char *code = xs_dict_get(args, "code");
  237. const char *cid = xs_dict_get(args, "client_id");
  238. const char *csec = xs_dict_get(args, "client_secret");
  239. const char *ruri = xs_dict_get(args, "redirect_uri");
  240. /* FIXME: this 'scope' parameter is mandatory for the official Mastodon API,
  241. but if it's enabled, it makes it crash after some more steps, which
  242. is FAR WORSE */
  243. // const char *scope = xs_dict_get(args, "scope");
  244. const char *scope = NULL;
  245. /* no client_secret? check if it's inside an authorization header
  246. (AndStatus does it this way) */
  247. if (xs_is_null(csec)) {
  248. const char *auhdr = xs_dict_get(req, "authorization");
  249. if (!xs_is_null(auhdr) && xs_startswith(auhdr, "Basic ")) {
  250. xs *s1 = xs_replace(auhdr, "Basic ", "");
  251. int size;
  252. xs *s2 = xs_base64_dec(s1, &size);
  253. if (!xs_is_null(s2)) {
  254. xs *l1 = xs_split(s2, ":");
  255. if (xs_list_len(l1) == 2) {
  256. wrk = xs_dup(xs_list_get(l1, 1));
  257. csec = wrk;
  258. }
  259. }
  260. }
  261. }
  262. if (gtype && code && cid && csec && ruri) {
  263. xs *app = app_get(cid);
  264. if (app == NULL) {
  265. status = 401;
  266. srv_log(xs_fmt("oauth token: invalid app %s", cid));
  267. }
  268. else
  269. if (strcmp(csec, xs_dict_get(app, "client_secret")) != 0) {
  270. status = 401;
  271. srv_log(xs_fmt("oauth token: invalid client_secret for app %s", cid));
  272. }
  273. else {
  274. xs *rsp = xs_dict_new();
  275. xs *cat = xs_number_new(time(NULL));
  276. xs *tokid = random_str();
  277. rsp = xs_dict_append(rsp, "access_token", tokid);
  278. rsp = xs_dict_append(rsp, "token_type", "Bearer");
  279. rsp = xs_dict_append(rsp, "created_at", cat);
  280. if (!xs_is_null(scope))
  281. rsp = xs_dict_append(rsp, "scope", scope);
  282. *body = xs_json_dumps_pp(rsp, 4);
  283. *ctype = "application/json";
  284. status = 200;
  285. const char *uid = xs_dict_get(app, "uid");
  286. srv_debug(0, xs_fmt("oauth token: "
  287. "successful login for %s, new token %s", uid, tokid));
  288. xs *token = xs_dict_new();
  289. token = xs_dict_append(token, "token", tokid);
  290. token = xs_dict_append(token, "client_id", cid);
  291. token = xs_dict_append(token, "client_secret", csec);
  292. token = xs_dict_append(token, "uid", uid);
  293. token = xs_dict_append(token, "code", code);
  294. token_add(tokid, token);
  295. }
  296. }
  297. else {
  298. srv_debug(0, xs_fmt("oauth token: invalid or unset arguments"));
  299. status = 400;
  300. }
  301. }
  302. else
  303. if (strcmp(cmd, "/revoke") == 0) {
  304. const char *cid = xs_dict_get(args, "client_id");
  305. const char *csec = xs_dict_get(args, "client_secret");
  306. const char *tokid = xs_dict_get(args, "token");
  307. if (cid && csec && tokid) {
  308. xs *token = token_get(tokid);
  309. *body = xs_str_new("{}");
  310. *ctype = "application/json";
  311. if (token == NULL || strcmp(csec, xs_dict_get(token, "client_secret")) != 0) {
  312. srv_debug(1, xs_fmt("oauth revoke: bad secret for token %s", tokid));
  313. status = 403;
  314. }
  315. else {
  316. token_del(tokid);
  317. srv_debug(0, xs_fmt("oauth revoke: revoked token %s", tokid));
  318. status = 200;
  319. /* also delete the app, as it serves no purpose from now on */
  320. app_del(cid);
  321. }
  322. }
  323. else {
  324. srv_debug(0, xs_fmt("oauth revoke: invalid or unset arguments"));
  325. status = 403;
  326. }
  327. }
  328. return status;
  329. }
  330. xs_str *mastoapi_id(const xs_dict *msg)
  331. /* returns a somewhat Mastodon-compatible status id */
  332. {
  333. const char *id = xs_dict_get(msg, "id");
  334. xs *md5 = xs_md5_hex(id, strlen(id));
  335. return xs_fmt("%10.0f%s", object_ctime_by_md5(md5), md5);
  336. }
  337. #define MID_TO_MD5(id) (id + 10)
  338. xs_dict *mastoapi_account(const xs_dict *actor)
  339. /* converts an ActivityPub actor to a Mastodon account */
  340. {
  341. xs_dict *acct = xs_dict_new();
  342. const char *display_name = xs_dict_get(actor, "name");
  343. if (xs_is_null(display_name) || *display_name == '\0')
  344. display_name = xs_dict_get(actor, "preferredUsername");
  345. const char *id = xs_dict_get(actor, "id");
  346. const char *pub = xs_dict_get(actor, "published");
  347. xs *acct_md5 = xs_md5_hex(id, strlen(id));
  348. acct = xs_dict_append(acct, "id", acct_md5);
  349. acct = xs_dict_append(acct, "username", xs_dict_get(actor, "preferredUsername"));
  350. acct = xs_dict_append(acct, "acct", xs_dict_get(actor, "preferredUsername"));
  351. acct = xs_dict_append(acct, "display_name", display_name);
  352. if (pub)
  353. acct = xs_dict_append(acct, "created_at", pub);
  354. else {
  355. /* unset created_at crashes Tusky, so lie like a mf */
  356. xs *date = xs_str_utctime(0, "%Y-%m-%dT%H:%M:%SZ");
  357. acct = xs_dict_append(acct, "created_at", date);
  358. }
  359. acct = xs_dict_append(acct, "note", xs_dict_get(actor, "summary"));
  360. acct = xs_dict_append(acct, "url", id);
  361. xs *avatar = NULL;
  362. xs_dict *av = xs_dict_get(actor, "icon");
  363. if (xs_type(av) == XSTYPE_DICT)
  364. avatar = xs_dup(xs_dict_get(av, "url"));
  365. else
  366. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  367. acct = xs_dict_append(acct, "avatar", avatar);
  368. return acct;
  369. }
  370. xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
  371. /* converts an ActivityPub note to a Mastodon status */
  372. {
  373. xs *actor = NULL;
  374. actor_get(snac, xs_dict_get(msg, "attributedTo"), &actor);
  375. /* if the author is not here, discard */
  376. if (actor == NULL)
  377. return NULL;
  378. xs *acct = mastoapi_account(actor);
  379. /** shave the yak converting an ActivityPub Note to a Mastodon status **/
  380. xs *f = xs_val_new(XSTYPE_FALSE);
  381. xs *t = xs_val_new(XSTYPE_TRUE);
  382. xs *n = xs_val_new(XSTYPE_NULL);
  383. xs *el = xs_list_new();
  384. xs *idx = NULL;
  385. xs *ixc = NULL;
  386. char *tmp;
  387. char *id = xs_dict_get(msg, "id");
  388. xs *mid = mastoapi_id(msg);
  389. xs_dict *st = xs_dict_new();
  390. st = xs_dict_append(st, "id", mid);
  391. st = xs_dict_append(st, "uri", id);
  392. st = xs_dict_append(st, "url", id);
  393. st = xs_dict_append(st, "created_at", xs_dict_get(msg, "published"));
  394. st = xs_dict_append(st, "account", acct);
  395. st = xs_dict_append(st, "content", xs_dict_get(msg, "content"));
  396. st = xs_dict_append(st, "visibility",
  397. is_msg_public(snac, msg) ? "public" : "private");
  398. tmp = xs_dict_get(msg, "sensitive");
  399. if (xs_is_null(tmp))
  400. tmp = f;
  401. st = xs_dict_append(st, "sensitive", tmp);
  402. tmp = xs_dict_get(msg, "summary");
  403. if (xs_is_null(tmp))
  404. tmp = "";
  405. st = xs_dict_append(st, "spoiler_text", tmp);
  406. /* create the list of attachments */
  407. xs *matt = xs_list_new();
  408. xs_list *att = xs_dict_get(msg, "attachment");
  409. xs_str *aobj;
  410. while (xs_list_iter(&att, &aobj)) {
  411. const char *mtype = xs_dict_get(aobj, "mediaType");
  412. if (!xs_is_null(mtype) && xs_startswith(mtype, "image/")) {
  413. xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
  414. xs *matte = xs_dict_new();
  415. matte = xs_dict_append(matte, "id", matteid);
  416. matte = xs_dict_append(matte, "type", "image");
  417. matte = xs_dict_append(matte, "url", xs_dict_get(aobj, "url"));
  418. matte = xs_dict_append(matte, "preview_url", xs_dict_get(aobj, "url"));
  419. matte = xs_dict_append(matte, "remote_url", xs_dict_get(aobj, "url"));
  420. const char *name = xs_dict_get(aobj, "name");
  421. if (xs_is_null(name))
  422. name = "";
  423. matte = xs_dict_append(matte, "description", name);
  424. matt = xs_list_append(matt, matte);
  425. }
  426. }
  427. st = xs_dict_append(st, "media_attachments", matt);
  428. st = xs_dict_append(st, "mentions", el);
  429. st = xs_dict_append(st, "tags", el);
  430. st = xs_dict_append(st, "emojis", el);
  431. xs_free(idx);
  432. xs_free(ixc);
  433. idx = object_likes(id);
  434. ixc = xs_number_new(xs_list_len(idx));
  435. st = xs_dict_append(st, "favourites_count", ixc);
  436. st = xs_dict_append(st, "favourited",
  437. xs_list_in(idx, snac->md5) != -1 ? t : f);
  438. xs_free(idx);
  439. xs_free(ixc);
  440. idx = object_announces(id);
  441. ixc = xs_number_new(xs_list_len(idx));
  442. st = xs_dict_append(st, "reblogs_count", ixc);
  443. st = xs_dict_append(st, "reblogged",
  444. xs_list_in(idx, snac->md5) != -1 ? t : f);
  445. xs_free(idx);
  446. xs_free(ixc);
  447. idx = object_children(id);
  448. ixc = xs_number_new(xs_list_len(idx));
  449. st = xs_dict_append(st, "replies_count", ixc);
  450. /* default in_reply_to values */
  451. st = xs_dict_append(st, "in_reply_to_id", n);
  452. st = xs_dict_append(st, "in_reply_to_account_id", n);
  453. tmp = xs_dict_get(msg, "inReplyTo");
  454. if (!xs_is_null(tmp)) {
  455. xs *irto = NULL;
  456. if (valid_status(object_get(tmp, &irto))) {
  457. xs *irt_mid = mastoapi_id(irto);
  458. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  459. char *at = NULL;
  460. if (!xs_is_null(at = xs_dict_get(irto, "attributedTo"))) {
  461. xs *at_md5 = xs_md5_hex(at, strlen(at));
  462. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  463. }
  464. }
  465. }
  466. st = xs_dict_append(st, "reblog", n);
  467. st = xs_dict_append(st, "poll", n);
  468. st = xs_dict_append(st, "card", n);
  469. st = xs_dict_append(st, "language", n);
  470. tmp = xs_dict_get(msg, "sourceContent");
  471. if (xs_is_null(tmp))
  472. tmp = "";
  473. st = xs_dict_append(st, "text", tmp);
  474. tmp = xs_dict_get(msg, "updated");
  475. if (xs_is_null(tmp))
  476. tmp = n;
  477. st = xs_dict_append(st, "edited_at", tmp);
  478. return st;
  479. }
  480. int process_auth_token(snac *snac, const xs_dict *req)
  481. /* processes an authorization token, if there is one */
  482. {
  483. int logged_in = 0;
  484. char *v;
  485. /* if there is an authorization field, try to validate it */
  486. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  487. xs *tokid = xs_replace(v, "Bearer ", "");
  488. xs *token = token_get(tokid);
  489. if (token != NULL) {
  490. const char *uid = xs_dict_get(token, "uid");
  491. if (!xs_is_null(uid) && user_open(snac, uid)) {
  492. logged_in = 1;
  493. srv_debug(2, xs_fmt("mastoapi auth: valid token for user %s", uid));
  494. }
  495. else
  496. srv_log(xs_fmt("mastoapi auth: corrupted token %s", tokid));
  497. }
  498. else
  499. srv_log(xs_fmt("mastoapi auth: invalid token %s", tokid));
  500. }
  501. return logged_in;
  502. }
  503. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  504. char **body, int *b_size, char **ctype)
  505. {
  506. if (!xs_startswith(q_path, "/api/v1/"))
  507. return 0;
  508. srv_debug(1, xs_fmt("mastoapi_get_handler %s", q_path));
  509. /* {
  510. xs *j = xs_json_dumps_pp(req, 4);
  511. printf("mastoapi get:\n%s\n", j);
  512. }*/
  513. int status = 404;
  514. xs_dict *args = xs_dict_get(req, "q_vars");
  515. xs *cmd = xs_replace(q_path, "/api", "");
  516. snac snac1 = {0};
  517. int logged_in = process_auth_token(&snac1, req);
  518. if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) {
  519. if (logged_in) {
  520. xs *acct = xs_dict_new();
  521. acct = xs_dict_append(acct, "id", xs_dict_get(snac1.config, "uid"));
  522. acct = xs_dict_append(acct, "username", xs_dict_get(snac1.config, "uid"));
  523. acct = xs_dict_append(acct, "acct", xs_dict_get(snac1.config, "uid"));
  524. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac1.config, "name"));
  525. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac1.config, "published"));
  526. acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio"));
  527. acct = xs_dict_append(acct, "url", snac1.actor);
  528. acct = xs_dict_append(acct, "header", "");
  529. xs *avatar = NULL;
  530. char *av = xs_dict_get(snac1.config, "avatar");
  531. if (xs_is_null(av) || *av == '\0')
  532. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  533. else
  534. avatar = xs_dup(av);
  535. acct = xs_dict_append(acct, "avatar", avatar);
  536. *body = xs_json_dumps_pp(acct, 4);
  537. *ctype = "application/json";
  538. status = 200;
  539. }
  540. else {
  541. status = 422; // "Unprocessable entity" (no login)
  542. }
  543. }
  544. else
  545. if (strcmp(cmd, "/v1/accounts/relationships") == 0) {
  546. /* find if an account is followed, blocked, etc. */
  547. /* the account to get relationships about is in args "id[]" */
  548. /* dummy by now */
  549. if (logged_in) {
  550. *body = xs_dup("[]");
  551. *ctype = "application/json";
  552. status = 200;
  553. }
  554. }
  555. else
  556. if (xs_startswith(cmd, "/v1/accounts/")) {
  557. /* account-related information */
  558. xs *l = xs_split(cmd, "/");
  559. const char *uid = xs_list_get(l, 3);
  560. const char *opt = xs_list_get(l, 4);
  561. if (uid != NULL) {
  562. snac snac2;
  563. xs *out = NULL;
  564. xs *actor = NULL;
  565. /* if uid it's the md5 of this actor, transform into a real uid */
  566. if (strcmp(uid, snac1.md5) == 0)
  567. uid = xs_dict_get(snac1.config, "uid");
  568. /* is it a local user? */
  569. if (user_open(&snac2, uid)) {
  570. if (opt == NULL) {
  571. /* account information */
  572. actor = msg_actor(&snac2);
  573. out = mastoapi_account(actor);
  574. }
  575. else
  576. if (strcmp(opt, "statuses") == 0) {
  577. /* the public list of posts of a user */
  578. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  579. xs_list *p = timeline;
  580. xs_str *v;
  581. out = xs_list_new();
  582. while (xs_list_iter(&p, &v)) {
  583. xs *msg = NULL;
  584. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  585. /* add only posts by the author */
  586. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  587. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  588. xs *st = mastoapi_status(&snac2, msg);
  589. out = xs_list_append(out, st);
  590. }
  591. }
  592. }
  593. }
  594. user_free(&snac2);
  595. }
  596. else {
  597. /* try the uid as the md5 of a possibly loaded actor */
  598. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  599. if (opt == NULL) {
  600. /* account information */
  601. out = mastoapi_account(actor);
  602. }
  603. else
  604. if (strcmp(opt, "statuses") == 0) {
  605. /* we don't serve statuses of others; return the empty list */
  606. out = xs_list_new();
  607. }
  608. }
  609. }
  610. if (out != NULL) {
  611. *body = xs_json_dumps_pp(out, 4);
  612. *ctype = "application/json";
  613. status = 200;
  614. }
  615. }
  616. }
  617. else
  618. if (strcmp(cmd, "/v1/timelines/home") == 0) {
  619. /* the private timeline */
  620. if (logged_in) {
  621. const char *max_id = xs_dict_get(args, "max_id");
  622. const char *since_id = xs_dict_get(args, "since_id");
  623. const char *min_id = xs_dict_get(args, "min_id");
  624. const char *limit_s = xs_dict_get(args, "limit");
  625. int limit = 0;
  626. int cnt = 0;
  627. if (!xs_is_null(limit_s))
  628. limit = atoi(limit_s);
  629. if (limit == 0)
  630. limit = 20;
  631. xs *timeline = timeline_simple_list(&snac1, "private", 0, XS_ALL);
  632. xs *out = xs_list_new();
  633. xs_list *p = timeline;
  634. xs_str *v;
  635. while (xs_list_iter(&p, &v) && cnt < limit) {
  636. xs *msg = NULL;
  637. /* only return entries older that max_id */
  638. if (max_id) {
  639. if (strcmp(v, max_id) == 0)
  640. max_id = NULL;
  641. continue;
  642. }
  643. /* only returns entries newer than since_id */
  644. if (since_id) {
  645. if (strcmp(v, since_id) == 0)
  646. break;
  647. }
  648. /* only returns entries newer than min_id */
  649. /* what does really "Return results immediately newer than ID" mean? */
  650. if (min_id) {
  651. if (strcmp(v, min_id) == 0)
  652. break;
  653. }
  654. /* get the entry */
  655. if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
  656. continue;
  657. /* discard non-Notes */
  658. if (strcmp(xs_dict_get(msg, "type"), "Note") != 0)
  659. continue;
  660. /* convert the Note into a Mastodon status */
  661. xs *st = mastoapi_status(&snac1, msg);
  662. if (st != NULL)
  663. out = xs_list_append(out, st);
  664. cnt++;
  665. }
  666. *body = xs_json_dumps_pp(out, 4);
  667. *ctype = "application/json";
  668. status = 200;
  669. {
  670. xs *j = xs_json_loads(*body);
  671. if (j == NULL) {
  672. srv_log(xs_fmt("mastoapi timeline: bad JSON"));
  673. srv_archive_error("mastoapi_timeline", "bad JSON", req, *body);
  674. }
  675. }
  676. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  677. }
  678. else {
  679. status = 401; // unauthorized
  680. }
  681. }
  682. else
  683. if (strcmp(cmd, "/v1/timelines/public") == 0) {
  684. /* the public timeline (public timelines for all users) */
  685. /* TBD */
  686. *body = xs_dup("[]");
  687. *ctype = "application/json";
  688. status = 200;
  689. }
  690. else
  691. if (strcmp(cmd, "/v1/conversations") == 0) {
  692. /* TBD */
  693. *body = xs_dup("[]");
  694. *ctype = "application/json";
  695. status = 200;
  696. }
  697. else
  698. if (strcmp(cmd, "/v1/notifications") == 0) {
  699. if (logged_in) {
  700. xs *l = notify_list(&snac1, 0);
  701. xs *out = xs_list_new();
  702. xs_list *p = l;
  703. xs_dict *v;
  704. while (xs_list_iter(&p, &v)) {
  705. xs *noti = notify_get(&snac1, v);
  706. if (noti == NULL)
  707. continue;
  708. const char *type = xs_dict_get(noti, "type");
  709. const char *objid = xs_dict_get(noti, "objid");
  710. xs *actor = NULL;
  711. xs *entry = NULL;
  712. if (!valid_status(object_get(xs_dict_get(noti, "actor"), &actor)))
  713. continue;
  714. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  715. continue;
  716. if (is_hidden(&snac1, objid))
  717. continue;
  718. /* convert the type */
  719. if (strcmp(type, "Like") == 0)
  720. type = "favourite";
  721. else
  722. if (strcmp(type, "Announce") == 0)
  723. type = "reblog";
  724. else
  725. if (strcmp(type, "Follow") == 0)
  726. type = "follow";
  727. else
  728. if (strcmp(type, "Create") == 0)
  729. type = "mention";
  730. else
  731. continue;
  732. xs *mn = xs_dict_new();
  733. mn = xs_dict_append(mn, "type", type);
  734. xs *id = xs_replace(xs_dict_get(noti, "id"), ".", "");
  735. mn = xs_dict_append(mn, "id", id);
  736. mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
  737. xs *acct = mastoapi_account(actor);
  738. mn = xs_dict_append(mn, "account", acct);
  739. if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
  740. xs *st = mastoapi_status(&snac1, entry);
  741. mn = xs_dict_append(mn, "status", st);
  742. }
  743. out = xs_list_append(out, mn);
  744. }
  745. *body = xs_json_dumps_pp(out, 4);
  746. *ctype = "application/json";
  747. status = 200;
  748. }
  749. else
  750. status = 401;
  751. }
  752. else
  753. if (strcmp(cmd, "/v1/filters") == 0) {
  754. /* snac will never have filters */
  755. *body = xs_dup("[]");
  756. *ctype = "application/json";
  757. status = 200;
  758. }
  759. else
  760. if (strcmp(cmd, "/v1/favourites") == 0) {
  761. /* snac will never support a list of favourites */
  762. *body = xs_dup("[]");
  763. *ctype = "application/json";
  764. status = 200;
  765. }
  766. else
  767. if (strcmp(cmd, "/v1/bookmarks") == 0) {
  768. /* snac does not support bookmarks */
  769. *body = xs_dup("[]");
  770. *ctype = "application/json";
  771. status = 200;
  772. }
  773. else
  774. if (strcmp(cmd, "/v1/lists") == 0) {
  775. /* snac does not support lists */
  776. *body = xs_dup("[]");
  777. *ctype = "application/json";
  778. status = 200;
  779. }
  780. else
  781. if (strcmp(cmd, "/v1/scheduled_statuses") == 0) {
  782. /* snac does not scheduled notes */
  783. *body = xs_dup("[]");
  784. *ctype = "application/json";
  785. status = 200;
  786. }
  787. else
  788. if (strcmp(cmd, "/v1/follow_requests") == 0) {
  789. /* snac does not support optional follow confirmations */
  790. *body = xs_dup("[]");
  791. *ctype = "application/json";
  792. status = 200;
  793. }
  794. else
  795. if (strcmp(cmd, "/v1/announcements") == 0) {
  796. /* snac has no announcements (yet?) */
  797. *body = xs_dup("[]");
  798. *ctype = "application/json";
  799. status = 200;
  800. }
  801. else
  802. if (strcmp(cmd, "/v1/custom_emojis") == 0) {
  803. /* are you kidding me? */
  804. *body = xs_dup("[]");
  805. *ctype = "application/json";
  806. status = 200;
  807. }
  808. else
  809. if (strcmp(cmd, "/v1/instance") == 0) {
  810. /* returns an instance object */
  811. xs *ins = xs_dict_new();
  812. const char *host = xs_dict_get(srv_config, "host");
  813. ins = xs_dict_append(ins, "uri", host);
  814. ins = xs_dict_append(ins, "domain", host);
  815. ins = xs_dict_append(ins, "title", host);
  816. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  817. ins = xs_dict_append(ins, "source_url", "https:/" "/comam.es/what-is-snac");
  818. ins = xs_dict_append(ins, "description", host);
  819. ins = xs_dict_append(ins, "short_description", host);
  820. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  821. ins = xs_dict_append(ins, "thumbnail", susie);
  822. ins = xs_dict_append(ins, "email", "admin@localhost");
  823. xs *l1 = xs_list_new();
  824. ins = xs_dict_append(ins, "rules", l1);
  825. l1 = xs_list_append(l1, "en");
  826. ins = xs_dict_append(ins, "languages", l1);
  827. xs *d1 = xs_dict_new();
  828. ins = xs_dict_append(ins, "urls", d1);
  829. ins = xs_dict_append(ins, "stats", d1);
  830. ins = xs_dict_append(ins, "configuration", d1);
  831. *body = xs_json_dumps_pp(ins, 4);
  832. *ctype = "application/json";
  833. status = 200;
  834. }
  835. else
  836. if (xs_startswith(cmd, "/v1/statuses/")) {
  837. /* operations on a status */
  838. xs *l = xs_split(cmd, "/");
  839. const char *id = xs_list_get(l, 3);
  840. const char *op = xs_list_get(l, 4);
  841. if (!xs_is_null(id)) {
  842. xs *msg = NULL;
  843. xs *out = NULL;
  844. /* skip the 'fake' part of the id */
  845. id = MID_TO_MD5(id);
  846. if (valid_status(timeline_get_by_md5(&snac1, id, &msg))) {
  847. if (op == NULL) {
  848. /* return the status itself */
  849. out = mastoapi_status(&snac1, msg);
  850. }
  851. else
  852. if (strcmp(op, "context") == 0) {
  853. /* return ancestors and children */
  854. xs *anc = xs_list_new();
  855. xs *des = xs_list_new();
  856. xs_list *p;
  857. xs_str *v;
  858. char pid[64];
  859. /* build the [grand]parent list, moving up */
  860. strcpy(pid, id);
  861. while (object_parent(pid, pid, sizeof(pid))) {
  862. xs *m2 = NULL;
  863. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  864. xs *st = mastoapi_status(&snac1, m2);
  865. anc = xs_list_insert(anc, 0, st);
  866. }
  867. else
  868. break;
  869. }
  870. /* build the children list */
  871. xs *children = object_children(xs_dict_get(msg, "id"));
  872. p = children;
  873. while (xs_list_iter(&p, &v)) {
  874. xs *m2 = NULL;
  875. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  876. xs *st = mastoapi_status(&snac1, m2);
  877. des = xs_list_append(des, st);
  878. }
  879. }
  880. out = xs_dict_new();
  881. out = xs_dict_append(out, "ancestors", anc);
  882. out = xs_dict_append(out, "descendants", des);
  883. }
  884. else
  885. if (strcmp(op, "reblogged_by") == 0 ||
  886. strcmp(op, "favourited_by") == 0) {
  887. /* return the list of people who liked or boosted this */
  888. out = xs_list_new();
  889. xs *l = NULL;
  890. if (op[0] == 'r')
  891. l = object_announces(xs_dict_get(msg, "id"));
  892. else
  893. l = object_likes(xs_dict_get(msg, "id"));
  894. xs_list *p = l;
  895. xs_str *v;
  896. while (xs_list_iter(&p, &v)) {
  897. xs *actor2 = NULL;
  898. if (valid_status(object_get_by_md5(v, &actor2))) {
  899. xs *acct2 = mastoapi_account(actor2);
  900. out = xs_list_append(out, acct2);
  901. }
  902. }
  903. }
  904. }
  905. else
  906. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  907. if (out != NULL) {
  908. *body = xs_json_dumps_pp(out, 4);
  909. *ctype = "application/json";
  910. status = 200;
  911. }
  912. }
  913. }
  914. else
  915. if (strcmp(cmd, "/v1/filters") == 0) {
  916. *body = xs_dup("[]");
  917. *ctype = "application/json";
  918. status = 200;
  919. }
  920. else
  921. if (strcmp(cmd, "/v1/preferences") == 0) {
  922. *body = xs_dup("{}");
  923. *ctype = "application/json";
  924. status = 200;
  925. }
  926. else
  927. if (strcmp(cmd, "/v1/markers") == 0) {
  928. *body = xs_dup("{}");
  929. *ctype = "application/json";
  930. status = 200;
  931. }
  932. else
  933. if (strcmp(cmd, "/v1/followed_tags") == 0) {
  934. *body = xs_dup("[]");
  935. *ctype = "application/json";
  936. status = 200;
  937. }
  938. /* user cleanup */
  939. if (logged_in)
  940. user_free(&snac1);
  941. return status;
  942. }
  943. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  944. const char *payload, int p_size,
  945. char **body, int *b_size, char **ctype)
  946. {
  947. if (!xs_startswith(q_path, "/api/v1/"))
  948. return 0;
  949. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  950. /* {
  951. xs *j = xs_json_dumps_pp(req, 4);
  952. printf("mastoapi post:\n%s\n", j);
  953. }*/
  954. int status = 404;
  955. xs *args = NULL;
  956. char *i_ctype = xs_dict_get(req, "content-type");
  957. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  958. args = xs_json_loads(payload);
  959. else
  960. args = xs_dup(xs_dict_get(req, "p_vars"));
  961. if (args == NULL)
  962. return 400;
  963. /* {
  964. xs *j = xs_json_dumps_pp(args, 4);
  965. printf("%s\n", j);
  966. }*/
  967. xs *cmd = xs_replace(q_path, "/api", "");
  968. snac snac = {0};
  969. int logged_in = process_auth_token(&snac, req);
  970. if (strcmp(cmd, "/v1/apps") == 0) {
  971. const char *name = xs_dict_get(args, "client_name");
  972. const char *ruri = xs_dict_get(args, "redirect_uris");
  973. const char *scope = xs_dict_get(args, "scope");
  974. if (xs_type(ruri) == XSTYPE_LIST)
  975. ruri = xs_dict_get(ruri, 0);
  976. if (name && ruri) {
  977. xs *app = xs_dict_new();
  978. xs *id = xs_replace_i(tid(0), ".", "");
  979. xs *cid = random_str();
  980. xs *csec = random_str();
  981. xs *vkey = random_str();
  982. app = xs_dict_append(app, "name", name);
  983. app = xs_dict_append(app, "redirect_uri", ruri);
  984. app = xs_dict_append(app, "client_id", cid);
  985. app = xs_dict_append(app, "client_secret", csec);
  986. app = xs_dict_append(app, "vapid_key", vkey);
  987. app = xs_dict_append(app, "id", id);
  988. *body = xs_json_dumps_pp(app, 4);
  989. *ctype = "application/json";
  990. status = 200;
  991. app = xs_dict_append(app, "code", "");
  992. if (scope)
  993. app = xs_dict_append(app, "scope", scope);
  994. app_add(cid, app);
  995. srv_debug(0, xs_fmt("mastoapi apps: new app %s", cid));
  996. }
  997. }
  998. else
  999. if (strcmp(cmd, "/v1/statuses") == 0) {
  1000. if (logged_in) {
  1001. /* post a new Note */
  1002. /* {
  1003. xs *j = xs_json_dumps_pp(args, 4);
  1004. printf("%s\n", j);
  1005. }*/
  1006. const char *content = xs_dict_get(args, "status");
  1007. const char *mid = xs_dict_get(args, "in_reply_to_id");
  1008. const char *visibility = xs_dict_get(args, "visibility");
  1009. const char *summary = xs_dict_get(args, "spoiler_text");
  1010. xs *attach_list = xs_list_new();
  1011. xs *irt = NULL;
  1012. /* is it a reply? */
  1013. if (mid != NULL) {
  1014. xs *r_msg = NULL;
  1015. const char *md5 = MID_TO_MD5(mid);
  1016. if (valid_status(object_get_by_md5(md5, &r_msg)))
  1017. irt = xs_dup(xs_dict_get(r_msg, "id"));
  1018. }
  1019. /* prepare the message */
  1020. xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
  1021. strcmp(visibility, "public") == 0 ? 0 : 1);
  1022. if (!xs_is_null(summary) && *summary) {
  1023. xs *t = xs_val_new(XSTYPE_TRUE);
  1024. msg = xs_dict_set(msg, "sensitive", t);
  1025. msg = xs_dict_set(msg, "summary", summary);
  1026. }
  1027. /* store */
  1028. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1029. /* 'Create' message */
  1030. xs *c_msg = msg_create(&snac, msg);
  1031. enqueue_message(&snac, c_msg);
  1032. timeline_touch(&snac);
  1033. /* convert to a mastodon status as a response code */
  1034. xs *st = mastoapi_status(&snac, msg);
  1035. *body = xs_json_dumps_pp(st, 4);
  1036. *ctype = "application/json";
  1037. status = 200;
  1038. }
  1039. else
  1040. status = 401;
  1041. }
  1042. else
  1043. if (xs_startswith(cmd, "/v1/statuses")) {
  1044. if (logged_in) {
  1045. /* operations on a status */
  1046. xs *l = xs_split(cmd, "/");
  1047. const char *mid = xs_list_get(l, 3);
  1048. const char *op = xs_list_get(l, 4);
  1049. if (!xs_is_null(mid)) {
  1050. xs *msg = NULL;
  1051. xs *out = NULL;
  1052. /* skip the 'fake' part of the id */
  1053. mid = MID_TO_MD5(mid);
  1054. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1055. char *id = xs_dict_get(msg, "id");
  1056. if (op == NULL) {
  1057. /* no operation (?) */
  1058. }
  1059. else
  1060. if (strcmp(op, "favourite") == 0) {
  1061. xs *n_msg = msg_admiration(&snac, id, "Like");
  1062. if (n_msg != NULL) {
  1063. enqueue_message(&snac, n_msg);
  1064. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  1065. out = mastoapi_status(&snac, msg);
  1066. }
  1067. }
  1068. else
  1069. if (strcmp(op, "unfavourite") == 0) {
  1070. /* snac does not support Undo+Like */
  1071. }
  1072. else
  1073. if (strcmp(op, "reblog") == 0) {
  1074. xs *n_msg = msg_admiration(&snac, id, "Announce");
  1075. if (n_msg != NULL) {
  1076. enqueue_message(&snac, n_msg);
  1077. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  1078. out = mastoapi_status(&snac, msg);
  1079. }
  1080. }
  1081. else
  1082. if (strcmp(op, "unreblog") == 0) {
  1083. /* snac does not support Undo+Announce */
  1084. }
  1085. else
  1086. if (strcmp(op, "bookmark") == 0) {
  1087. /* snac does not support bookmarks */
  1088. }
  1089. else
  1090. if (strcmp(op, "unbookmark") == 0) {
  1091. /* snac does not support bookmarks */
  1092. }
  1093. else
  1094. if (strcmp(op, "pin") == 0) {
  1095. /* snac does not support pinning */
  1096. }
  1097. else
  1098. if (strcmp(op, "unpin") == 0) {
  1099. /* snac does not support pinning */
  1100. }
  1101. else
  1102. if (strcmp(op, "mute") == 0) {
  1103. /* Mastodon's mute is snac's hide */
  1104. }
  1105. else
  1106. if (strcmp(op, "unmute") == 0) {
  1107. /* Mastodon's unmute is snac's unhide */
  1108. }
  1109. }
  1110. if (out != NULL) {
  1111. *body = xs_json_dumps_pp(out, 4);
  1112. *ctype = "application/json";
  1113. status = 200;
  1114. }
  1115. }
  1116. }
  1117. else
  1118. status = 401;
  1119. }
  1120. else
  1121. if (strcmp(cmd, "/v1/notifications/clear") == 0) {
  1122. if (logged_in) {
  1123. notify_clear(&snac);
  1124. timeline_touch(&snac);
  1125. *body = xs_dup("{}");
  1126. *ctype = "application/json";
  1127. status = 200;
  1128. }
  1129. else
  1130. status = 401;
  1131. }
  1132. else
  1133. if (strcmp(cmd, "/v1/push/subscription") == 0) {
  1134. /* I don't know what I'm doing */
  1135. if (logged_in) {
  1136. char *v;
  1137. xs *wpush = xs_dict_new();
  1138. wpush = xs_dict_append(wpush, "id", "1");
  1139. v = xs_dict_get(args, "data");
  1140. v = xs_dict_get(v, "alerts");
  1141. wpush = xs_dict_append(wpush, "alerts", v);
  1142. v = xs_dict_get(args, "subscription");
  1143. v = xs_dict_get(v, "endpoint");
  1144. wpush = xs_dict_append(wpush, "endpoint", v);
  1145. xs *server_key = random_str();
  1146. wpush = xs_dict_append(wpush, "server_key", server_key);
  1147. *body = xs_json_dumps_pp(wpush, 4);
  1148. *ctype = "application/json";
  1149. status = 200;
  1150. }
  1151. else
  1152. status = 401;
  1153. }
  1154. /* user cleanup */
  1155. if (logged_in)
  1156. user_free(&snac);
  1157. return status;
  1158. }