mastoapi.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  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/") && !xs_startswith(q_path, "/api/v2/"))
  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. /* is it a local user? */
  566. if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
  567. if (opt == NULL) {
  568. /* account information */
  569. actor = msg_actor(&snac2);
  570. out = mastoapi_account(actor);
  571. }
  572. else
  573. if (strcmp(opt, "statuses") == 0) {
  574. /* the public list of posts of a user */
  575. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  576. xs_list *p = timeline;
  577. xs_str *v;
  578. out = xs_list_new();
  579. while (xs_list_iter(&p, &v)) {
  580. xs *msg = NULL;
  581. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  582. /* add only posts by the author */
  583. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  584. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  585. xs *st = mastoapi_status(&snac2, msg);
  586. out = xs_list_append(out, st);
  587. }
  588. }
  589. }
  590. }
  591. user_free(&snac2);
  592. }
  593. else {
  594. /* try the uid as the md5 of a possibly loaded actor */
  595. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  596. if (opt == NULL) {
  597. /* account information */
  598. out = mastoapi_account(actor);
  599. }
  600. else
  601. if (strcmp(opt, "statuses") == 0) {
  602. /* we don't serve statuses of others; return the empty list */
  603. out = xs_list_new();
  604. }
  605. }
  606. }
  607. if (out != NULL) {
  608. *body = xs_json_dumps_pp(out, 4);
  609. *ctype = "application/json";
  610. status = 200;
  611. }
  612. }
  613. }
  614. else
  615. if (strcmp(cmd, "/v1/timelines/home") == 0) {
  616. /* the private timeline */
  617. if (logged_in) {
  618. const char *max_id = xs_dict_get(args, "max_id");
  619. const char *since_id = xs_dict_get(args, "since_id");
  620. const char *min_id = xs_dict_get(args, "min_id");
  621. const char *limit_s = xs_dict_get(args, "limit");
  622. int limit = 0;
  623. int cnt = 0;
  624. if (!xs_is_null(limit_s))
  625. limit = atoi(limit_s);
  626. if (limit == 0)
  627. limit = 20;
  628. xs *timeline = timeline_simple_list(&snac1, "private", 0, XS_ALL);
  629. xs *out = xs_list_new();
  630. xs_list *p = timeline;
  631. xs_str *v;
  632. while (xs_list_iter(&p, &v) && cnt < limit) {
  633. xs *msg = NULL;
  634. /* only return entries older that max_id */
  635. if (max_id) {
  636. if (strcmp(v, max_id) == 0)
  637. max_id = NULL;
  638. continue;
  639. }
  640. /* only returns entries newer than since_id */
  641. if (since_id) {
  642. if (strcmp(v, since_id) == 0)
  643. break;
  644. }
  645. /* only returns entries newer than min_id */
  646. /* what does really "Return results immediately newer than ID" mean? */
  647. if (min_id) {
  648. if (strcmp(v, min_id) == 0)
  649. break;
  650. }
  651. /* get the entry */
  652. if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
  653. continue;
  654. /* discard non-Notes */
  655. if (strcmp(xs_dict_get(msg, "type"), "Note") != 0)
  656. continue;
  657. /* convert the Note into a Mastodon status */
  658. xs *st = mastoapi_status(&snac1, msg);
  659. if (st != NULL)
  660. out = xs_list_append(out, st);
  661. cnt++;
  662. }
  663. *body = xs_json_dumps_pp(out, 4);
  664. *ctype = "application/json";
  665. status = 200;
  666. {
  667. xs *j = xs_json_loads(*body);
  668. if (j == NULL) {
  669. srv_log(xs_fmt("mastoapi timeline: bad JSON"));
  670. srv_archive_error("mastoapi_timeline", "bad JSON", req, *body);
  671. }
  672. }
  673. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  674. }
  675. else {
  676. status = 401; // unauthorized
  677. }
  678. }
  679. else
  680. if (strcmp(cmd, "/v1/timelines/public") == 0) {
  681. /* the public timeline (public timelines for all users) */
  682. /* TBD */
  683. *body = xs_dup("[]");
  684. *ctype = "application/json";
  685. status = 200;
  686. }
  687. else
  688. if (strcmp(cmd, "/v1/conversations") == 0) {
  689. /* TBD */
  690. *body = xs_dup("[]");
  691. *ctype = "application/json";
  692. status = 200;
  693. }
  694. else
  695. if (strcmp(cmd, "/v1/notifications") == 0) {
  696. if (logged_in) {
  697. xs *l = notify_list(&snac1, 0);
  698. xs *out = xs_list_new();
  699. xs_list *p = l;
  700. xs_dict *v;
  701. while (xs_list_iter(&p, &v)) {
  702. xs *noti = notify_get(&snac1, v);
  703. if (noti == NULL)
  704. continue;
  705. const char *type = xs_dict_get(noti, "type");
  706. const char *objid = xs_dict_get(noti, "objid");
  707. xs *actor = NULL;
  708. xs *entry = NULL;
  709. if (!valid_status(object_get(xs_dict_get(noti, "actor"), &actor)))
  710. continue;
  711. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  712. continue;
  713. if (is_hidden(&snac1, objid))
  714. continue;
  715. /* convert the type */
  716. if (strcmp(type, "Like") == 0)
  717. type = "favourite";
  718. else
  719. if (strcmp(type, "Announce") == 0)
  720. type = "reblog";
  721. else
  722. if (strcmp(type, "Follow") == 0)
  723. type = "follow";
  724. else
  725. if (strcmp(type, "Create") == 0)
  726. type = "mention";
  727. else
  728. continue;
  729. xs *mn = xs_dict_new();
  730. mn = xs_dict_append(mn, "type", type);
  731. xs *id = xs_replace(xs_dict_get(noti, "id"), ".", "");
  732. mn = xs_dict_append(mn, "id", id);
  733. mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
  734. xs *acct = mastoapi_account(actor);
  735. mn = xs_dict_append(mn, "account", acct);
  736. if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
  737. xs *st = mastoapi_status(&snac1, entry);
  738. mn = xs_dict_append(mn, "status", st);
  739. }
  740. out = xs_list_append(out, mn);
  741. }
  742. *body = xs_json_dumps_pp(out, 4);
  743. *ctype = "application/json";
  744. status = 200;
  745. }
  746. else
  747. status = 401;
  748. }
  749. else
  750. if (strcmp(cmd, "/v1/filters") == 0) {
  751. /* snac will never have filters */
  752. *body = xs_dup("[]");
  753. *ctype = "application/json";
  754. status = 200;
  755. }
  756. else
  757. if (strcmp(cmd, "/v1/favourites") == 0) {
  758. /* snac will never support a list of favourites */
  759. *body = xs_dup("[]");
  760. *ctype = "application/json";
  761. status = 200;
  762. }
  763. else
  764. if (strcmp(cmd, "/v1/bookmarks") == 0) {
  765. /* snac does not support bookmarks */
  766. *body = xs_dup("[]");
  767. *ctype = "application/json";
  768. status = 200;
  769. }
  770. else
  771. if (strcmp(cmd, "/v1/lists") == 0) {
  772. /* snac does not support lists */
  773. *body = xs_dup("[]");
  774. *ctype = "application/json";
  775. status = 200;
  776. }
  777. else
  778. if (strcmp(cmd, "/v1/scheduled_statuses") == 0) {
  779. /* snac does not scheduled notes */
  780. *body = xs_dup("[]");
  781. *ctype = "application/json";
  782. status = 200;
  783. }
  784. else
  785. if (strcmp(cmd, "/v1/follow_requests") == 0) {
  786. /* snac does not support optional follow confirmations */
  787. *body = xs_dup("[]");
  788. *ctype = "application/json";
  789. status = 200;
  790. }
  791. else
  792. if (strcmp(cmd, "/v1/announcements") == 0) {
  793. /* snac has no announcements (yet?) */
  794. *body = xs_dup("[]");
  795. *ctype = "application/json";
  796. status = 200;
  797. }
  798. else
  799. if (strcmp(cmd, "/v1/custom_emojis") == 0) {
  800. /* are you kidding me? */
  801. *body = xs_dup("[]");
  802. *ctype = "application/json";
  803. status = 200;
  804. }
  805. else
  806. if (strcmp(cmd, "/v1/instance") == 0) {
  807. /* returns an instance object */
  808. xs *ins = xs_dict_new();
  809. const char *host = xs_dict_get(srv_config, "host");
  810. ins = xs_dict_append(ins, "uri", host);
  811. ins = xs_dict_append(ins, "domain", host);
  812. ins = xs_dict_append(ins, "title", host);
  813. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  814. ins = xs_dict_append(ins, "source_url", "https:/" "/comam.es/what-is-snac");
  815. ins = xs_dict_append(ins, "description", host);
  816. ins = xs_dict_append(ins, "short_description", host);
  817. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  818. ins = xs_dict_append(ins, "thumbnail", susie);
  819. ins = xs_dict_append(ins, "email", "admin@localhost");
  820. xs *l1 = xs_list_new();
  821. ins = xs_dict_append(ins, "rules", l1);
  822. l1 = xs_list_append(l1, "en");
  823. ins = xs_dict_append(ins, "languages", l1);
  824. xs *d1 = xs_dict_new();
  825. ins = xs_dict_append(ins, "urls", d1);
  826. ins = xs_dict_append(ins, "stats", d1);
  827. ins = xs_dict_append(ins, "configuration", d1);
  828. *body = xs_json_dumps_pp(ins, 4);
  829. *ctype = "application/json";
  830. status = 200;
  831. }
  832. else
  833. if (xs_startswith(cmd, "/v1/statuses/")) {
  834. /* operations on a status */
  835. xs *l = xs_split(cmd, "/");
  836. const char *id = xs_list_get(l, 3);
  837. const char *op = xs_list_get(l, 4);
  838. if (!xs_is_null(id)) {
  839. xs *msg = NULL;
  840. xs *out = NULL;
  841. /* skip the 'fake' part of the id */
  842. id = MID_TO_MD5(id);
  843. if (valid_status(timeline_get_by_md5(&snac1, id, &msg))) {
  844. if (op == NULL) {
  845. /* return the status itself */
  846. out = mastoapi_status(&snac1, msg);
  847. }
  848. else
  849. if (strcmp(op, "context") == 0) {
  850. /* return ancestors and children */
  851. xs *anc = xs_list_new();
  852. xs *des = xs_list_new();
  853. xs_list *p;
  854. xs_str *v;
  855. char pid[64];
  856. /* build the [grand]parent list, moving up */
  857. strncpy(pid, id, sizeof(pid));
  858. while (object_parent(pid, pid, sizeof(pid))) {
  859. xs *m2 = NULL;
  860. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  861. xs *st = mastoapi_status(&snac1, m2);
  862. anc = xs_list_insert(anc, 0, st);
  863. }
  864. else
  865. break;
  866. }
  867. /* build the children list */
  868. xs *children = object_children(xs_dict_get(msg, "id"));
  869. p = children;
  870. while (xs_list_iter(&p, &v)) {
  871. xs *m2 = NULL;
  872. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  873. xs *st = mastoapi_status(&snac1, m2);
  874. des = xs_list_append(des, st);
  875. }
  876. }
  877. out = xs_dict_new();
  878. out = xs_dict_append(out, "ancestors", anc);
  879. out = xs_dict_append(out, "descendants", des);
  880. }
  881. else
  882. if (strcmp(op, "reblogged_by") == 0 ||
  883. strcmp(op, "favourited_by") == 0) {
  884. /* return the list of people who liked or boosted this */
  885. out = xs_list_new();
  886. xs *l = NULL;
  887. if (op[0] == 'r')
  888. l = object_announces(xs_dict_get(msg, "id"));
  889. else
  890. l = object_likes(xs_dict_get(msg, "id"));
  891. xs_list *p = l;
  892. xs_str *v;
  893. while (xs_list_iter(&p, &v)) {
  894. xs *actor2 = NULL;
  895. if (valid_status(object_get_by_md5(v, &actor2))) {
  896. xs *acct2 = mastoapi_account(actor2);
  897. out = xs_list_append(out, acct2);
  898. }
  899. }
  900. }
  901. }
  902. else
  903. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  904. if (out != NULL) {
  905. *body = xs_json_dumps_pp(out, 4);
  906. *ctype = "application/json";
  907. status = 200;
  908. }
  909. }
  910. }
  911. else
  912. if (strcmp(cmd, "/v1/filters") == 0) {
  913. *body = xs_dup("[]");
  914. *ctype = "application/json";
  915. status = 200;
  916. }
  917. else
  918. if (strcmp(cmd, "/v1/preferences") == 0) {
  919. *body = xs_dup("{}");
  920. *ctype = "application/json";
  921. status = 200;
  922. }
  923. else
  924. if (strcmp(cmd, "/v1/markers") == 0) {
  925. *body = xs_dup("{}");
  926. *ctype = "application/json";
  927. status = 200;
  928. }
  929. else
  930. if (strcmp(cmd, "/v1/followed_tags") == 0) {
  931. *body = xs_dup("[]");
  932. *ctype = "application/json";
  933. status = 200;
  934. }
  935. /* user cleanup */
  936. if (logged_in)
  937. user_free(&snac1);
  938. return status;
  939. }
  940. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  941. const char *payload, int p_size,
  942. char **body, int *b_size, char **ctype)
  943. {
  944. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  945. return 0;
  946. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  947. /* {
  948. xs *j = xs_json_dumps_pp(req, 4);
  949. printf("mastoapi post:\n%s\n", j);
  950. }*/
  951. int status = 404;
  952. xs *args = NULL;
  953. char *i_ctype = xs_dict_get(req, "content-type");
  954. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  955. args = xs_json_loads(payload);
  956. else
  957. args = xs_dup(xs_dict_get(req, "p_vars"));
  958. if (args == NULL)
  959. return 400;
  960. /* {
  961. xs *j = xs_json_dumps_pp(args, 4);
  962. printf("%s\n", j);
  963. }*/
  964. xs *cmd = xs_replace(q_path, "/api", "");
  965. snac snac = {0};
  966. int logged_in = process_auth_token(&snac, req);
  967. if (strcmp(cmd, "/v1/apps") == 0) {
  968. const char *name = xs_dict_get(args, "client_name");
  969. const char *ruri = xs_dict_get(args, "redirect_uris");
  970. const char *scope = xs_dict_get(args, "scope");
  971. if (xs_type(ruri) == XSTYPE_LIST)
  972. ruri = xs_dict_get(ruri, 0);
  973. if (name && ruri) {
  974. xs *app = xs_dict_new();
  975. xs *id = xs_replace_i(tid(0), ".", "");
  976. xs *cid = random_str();
  977. xs *csec = random_str();
  978. xs *vkey = random_str();
  979. app = xs_dict_append(app, "name", name);
  980. app = xs_dict_append(app, "redirect_uri", ruri);
  981. app = xs_dict_append(app, "client_id", cid);
  982. app = xs_dict_append(app, "client_secret", csec);
  983. app = xs_dict_append(app, "vapid_key", vkey);
  984. app = xs_dict_append(app, "id", id);
  985. *body = xs_json_dumps_pp(app, 4);
  986. *ctype = "application/json";
  987. status = 200;
  988. app = xs_dict_append(app, "code", "");
  989. if (scope)
  990. app = xs_dict_append(app, "scope", scope);
  991. app_add(cid, app);
  992. srv_debug(0, xs_fmt("mastoapi apps: new app %s", cid));
  993. }
  994. }
  995. else
  996. if (strcmp(cmd, "/v1/statuses") == 0) {
  997. if (logged_in) {
  998. /* post a new Note */
  999. /* {
  1000. xs *j = xs_json_dumps_pp(args, 4);
  1001. printf("%s\n", j);
  1002. }*/
  1003. const char *content = xs_dict_get(args, "status");
  1004. const char *mid = xs_dict_get(args, "in_reply_to_id");
  1005. const char *visibility = xs_dict_get(args, "visibility");
  1006. const char *summary = xs_dict_get(args, "spoiler_text");
  1007. xs *attach_list = xs_list_new();
  1008. xs *irt = NULL;
  1009. /* is it a reply? */
  1010. if (mid != NULL) {
  1011. xs *r_msg = NULL;
  1012. const char *md5 = MID_TO_MD5(mid);
  1013. if (valid_status(object_get_by_md5(md5, &r_msg)))
  1014. irt = xs_dup(xs_dict_get(r_msg, "id"));
  1015. }
  1016. /* prepare the message */
  1017. xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
  1018. strcmp(visibility, "public") == 0 ? 0 : 1);
  1019. if (!xs_is_null(summary) && *summary) {
  1020. xs *t = xs_val_new(XSTYPE_TRUE);
  1021. msg = xs_dict_set(msg, "sensitive", t);
  1022. msg = xs_dict_set(msg, "summary", summary);
  1023. }
  1024. /* store */
  1025. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1026. /* 'Create' message */
  1027. xs *c_msg = msg_create(&snac, msg);
  1028. enqueue_message(&snac, c_msg);
  1029. timeline_touch(&snac);
  1030. /* convert to a mastodon status as a response code */
  1031. xs *st = mastoapi_status(&snac, msg);
  1032. *body = xs_json_dumps_pp(st, 4);
  1033. *ctype = "application/json";
  1034. status = 200;
  1035. }
  1036. else
  1037. status = 401;
  1038. }
  1039. else
  1040. if (xs_startswith(cmd, "/v1/statuses")) {
  1041. if (logged_in) {
  1042. /* operations on a status */
  1043. xs *l = xs_split(cmd, "/");
  1044. const char *mid = xs_list_get(l, 3);
  1045. const char *op = xs_list_get(l, 4);
  1046. if (!xs_is_null(mid)) {
  1047. xs *msg = NULL;
  1048. xs *out = NULL;
  1049. /* skip the 'fake' part of the id */
  1050. mid = MID_TO_MD5(mid);
  1051. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1052. char *id = xs_dict_get(msg, "id");
  1053. if (op == NULL) {
  1054. /* no operation (?) */
  1055. }
  1056. else
  1057. if (strcmp(op, "favourite") == 0) {
  1058. xs *n_msg = msg_admiration(&snac, id, "Like");
  1059. if (n_msg != NULL) {
  1060. enqueue_message(&snac, n_msg);
  1061. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  1062. out = mastoapi_status(&snac, msg);
  1063. }
  1064. }
  1065. else
  1066. if (strcmp(op, "unfavourite") == 0) {
  1067. /* snac does not support Undo+Like */
  1068. }
  1069. else
  1070. if (strcmp(op, "reblog") == 0) {
  1071. xs *n_msg = msg_admiration(&snac, id, "Announce");
  1072. if (n_msg != NULL) {
  1073. enqueue_message(&snac, n_msg);
  1074. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  1075. out = mastoapi_status(&snac, msg);
  1076. }
  1077. }
  1078. else
  1079. if (strcmp(op, "unreblog") == 0) {
  1080. /* snac does not support Undo+Announce */
  1081. }
  1082. else
  1083. if (strcmp(op, "bookmark") == 0) {
  1084. /* snac does not support bookmarks */
  1085. }
  1086. else
  1087. if (strcmp(op, "unbookmark") == 0) {
  1088. /* snac does not support bookmarks */
  1089. }
  1090. else
  1091. if (strcmp(op, "pin") == 0) {
  1092. /* snac does not support pinning */
  1093. }
  1094. else
  1095. if (strcmp(op, "unpin") == 0) {
  1096. /* snac does not support pinning */
  1097. }
  1098. else
  1099. if (strcmp(op, "mute") == 0) {
  1100. /* Mastodon's mute is snac's hide */
  1101. }
  1102. else
  1103. if (strcmp(op, "unmute") == 0) {
  1104. /* Mastodon's unmute is snac's unhide */
  1105. }
  1106. }
  1107. if (out != NULL) {
  1108. *body = xs_json_dumps_pp(out, 4);
  1109. *ctype = "application/json";
  1110. status = 200;
  1111. }
  1112. }
  1113. }
  1114. else
  1115. status = 401;
  1116. }
  1117. else
  1118. if (strcmp(cmd, "/v1/notifications/clear") == 0) {
  1119. if (logged_in) {
  1120. notify_clear(&snac);
  1121. timeline_touch(&snac);
  1122. *body = xs_dup("{}");
  1123. *ctype = "application/json";
  1124. status = 200;
  1125. }
  1126. else
  1127. status = 401;
  1128. }
  1129. else
  1130. if (strcmp(cmd, "/v1/push/subscription") == 0) {
  1131. /* I don't know what I'm doing */
  1132. if (logged_in) {
  1133. char *v;
  1134. xs *wpush = xs_dict_new();
  1135. wpush = xs_dict_append(wpush, "id", "1");
  1136. v = xs_dict_get(args, "data");
  1137. v = xs_dict_get(v, "alerts");
  1138. wpush = xs_dict_append(wpush, "alerts", v);
  1139. v = xs_dict_get(args, "subscription");
  1140. v = xs_dict_get(v, "endpoint");
  1141. wpush = xs_dict_append(wpush, "endpoint", v);
  1142. xs *server_key = random_str();
  1143. wpush = xs_dict_append(wpush, "server_key", server_key);
  1144. *body = xs_json_dumps_pp(wpush, 4);
  1145. *ctype = "application/json";
  1146. status = 200;
  1147. }
  1148. else
  1149. status = 401;
  1150. }
  1151. else
  1152. if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) {
  1153. if (logged_in) {
  1154. }
  1155. else
  1156. status = 401;
  1157. }
  1158. /* user cleanup */
  1159. if (logged_in)
  1160. user_free(&snac);
  1161. return status;
  1162. }