mastoapi.c 42 KB

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