mastoapi.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 grunfink / MIT license */
  3. #ifndef NO_MASTODON_API
  4. #include "xs.h"
  5. #include "xs_openssl.h"
  6. #include "xs_json.h"
  7. #include "xs_io.h"
  8. #include "xs_time.h"
  9. #include "xs_glob.h"
  10. #include "xs_set.h"
  11. #include "snac.h"
  12. static xs_str *random_str(void)
  13. /* just what is says in the tin */
  14. {
  15. unsigned int data[4] = {0};
  16. FILE *f;
  17. if ((f = fopen("/dev/random", "r")) != NULL) {
  18. fread(data, sizeof(data), 1, f);
  19. fclose(f);
  20. }
  21. else {
  22. data[0] = random() % 0xffffffff;
  23. data[1] = random() % 0xffffffff;
  24. data[2] = random() % 0xffffffff;
  25. data[3] = random() % 0xffffffff;
  26. }
  27. return xs_hex_enc((char *)data, sizeof(data));
  28. }
  29. int app_add(const char *id, const xs_dict *app)
  30. /* stores an app */
  31. {
  32. if (!xs_is_hex(id))
  33. return 500;
  34. int status = 201;
  35. xs *fn = xs_fmt("%s/app/", srv_basedir);
  36. FILE *f;
  37. mkdirx(fn);
  38. fn = xs_str_cat(fn, id);
  39. fn = xs_str_cat(fn, ".json");
  40. if ((f = fopen(fn, "w")) != NULL) {
  41. xs *j = xs_json_dumps_pp(app, 4);
  42. fwrite(j, strlen(j), 1, f);
  43. fclose(f);
  44. }
  45. else
  46. status = 500;
  47. return status;
  48. }
  49. xs_str *_app_fn(const char *id)
  50. {
  51. return xs_fmt("%s/app/%s.json", srv_basedir, id);
  52. }
  53. xs_dict *app_get(const char *id)
  54. /* gets an app */
  55. {
  56. if (!xs_is_hex(id))
  57. return NULL;
  58. xs *fn = _app_fn(id);
  59. xs_dict *app = NULL;
  60. FILE *f;
  61. if ((f = fopen(fn, "r")) != NULL) {
  62. xs *j = xs_readall(f);
  63. fclose(f);
  64. app = xs_json_loads(j);
  65. }
  66. return app;
  67. }
  68. int app_del(const char *id)
  69. /* deletes an app */
  70. {
  71. if (!xs_is_hex(id))
  72. return -1;
  73. xs *fn = _app_fn(id);
  74. return unlink(fn);
  75. }
  76. int token_add(const char *id, const xs_dict *token)
  77. /* stores a token */
  78. {
  79. if (!xs_is_hex(id))
  80. return 500;
  81. int status = 201;
  82. xs *fn = xs_fmt("%s/token/", srv_basedir);
  83. FILE *f;
  84. mkdirx(fn);
  85. fn = xs_str_cat(fn, id);
  86. fn = xs_str_cat(fn, ".json");
  87. if ((f = fopen(fn, "w")) != NULL) {
  88. xs *j = xs_json_dumps_pp(token, 4);
  89. fwrite(j, strlen(j), 1, f);
  90. fclose(f);
  91. }
  92. else
  93. status = 500;
  94. return status;
  95. }
  96. xs_dict *token_get(const char *id)
  97. /* gets a token */
  98. {
  99. if (!xs_is_hex(id))
  100. return NULL;
  101. xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
  102. xs_dict *token = NULL;
  103. FILE *f;
  104. if ((f = fopen(fn, "r")) != NULL) {
  105. xs *j = xs_readall(f);
  106. fclose(f);
  107. token = xs_json_loads(j);
  108. }
  109. return token;
  110. }
  111. int token_del(const char *id)
  112. /* deletes a token */
  113. {
  114. if (!xs_is_hex(id))
  115. return -1;
  116. xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
  117. return unlink(fn);
  118. }
  119. const char *login_page = ""
  120. "<!DOCTYPE html>\n"
  121. "<body><h1>%s OAuth identify</h1>\n"
  122. "<div style=\"background-color: red; color: white\">%s</div>\n"
  123. "<form method=\"post\" action=\"https:/" "/%s/%s\">\n"
  124. "<p>Login: <input type=\"text\" name=\"login\"></p>\n"
  125. "<p>Password: <input type=\"password\" name=\"passwd\"></p>\n"
  126. "<input type=\"hidden\" name=\"redir\" value=\"%s\">\n"
  127. "<input type=\"hidden\" name=\"cid\" value=\"%s\">\n"
  128. "<input type=\"hidden\" name=\"state\" value=\"%s\">\n"
  129. "<input type=\"submit\" value=\"OK\">\n"
  130. "</form><p>%s</p></body>\n"
  131. "";
  132. int oauth_get_handler(const xs_dict *req, const char *q_path,
  133. char **body, int *b_size, char **ctype)
  134. {
  135. (void)b_size;
  136. if (!xs_startswith(q_path, "/oauth/"))
  137. return 0;
  138. /* {
  139. xs *j = xs_json_dumps_pp(req, 4);
  140. printf("oauth get:\n%s\n", j);
  141. }*/
  142. int status = 404;
  143. xs_dict *msg = xs_dict_get(req, "q_vars");
  144. xs *cmd = xs_replace_n(q_path, "/oauth", "", 1);
  145. srv_debug(1, xs_fmt("oauth_get_handler %s", q_path));
  146. if (strcmp(cmd, "/authorize") == 0) { /** **/
  147. const char *cid = xs_dict_get(msg, "client_id");
  148. const char *ruri = xs_dict_get(msg, "redirect_uri");
  149. const char *rtype = xs_dict_get(msg, "response_type");
  150. const char *state = xs_dict_get(msg, "state");
  151. status = 400;
  152. if (cid && ruri && rtype && strcmp(rtype, "code") == 0) {
  153. xs *app = app_get(cid);
  154. if (app != NULL) {
  155. const char *host = xs_dict_get(srv_config, "host");
  156. if (xs_is_null(state))
  157. state = "";
  158. *body = xs_fmt(login_page, host, "", host, "oauth/x-snac-login",
  159. ruri, cid, state, USER_AGENT);
  160. *ctype = "text/html";
  161. status = 200;
  162. srv_debug(1, xs_fmt("oauth authorize: generating login page"));
  163. }
  164. else
  165. srv_debug(1, xs_fmt("oauth authorize: bad client_id %s", cid));
  166. }
  167. else
  168. srv_debug(1, xs_fmt("oauth authorize: invalid or unset arguments"));
  169. }
  170. else
  171. if (strcmp(cmd, "/x-snac-get-token") == 0) { /** **/
  172. const char *host = xs_dict_get(srv_config, "host");
  173. *body = xs_fmt(login_page, host, "", host, "oauth/x-snac-get-token",
  174. "", "", "", USER_AGENT);
  175. *ctype = "text/html";
  176. status = 200;
  177. }
  178. return status;
  179. }
  180. int oauth_post_handler(const xs_dict *req, const char *q_path,
  181. const char *payload, int p_size,
  182. char **body, int *b_size, char **ctype)
  183. {
  184. (void)p_size;
  185. (void)b_size;
  186. if (!xs_startswith(q_path, "/oauth/"))
  187. return 0;
  188. /* {
  189. xs *j = xs_json_dumps_pp(req, 4);
  190. printf("oauth post:\n%s\n", j);
  191. }*/
  192. int status = 404;
  193. char *i_ctype = xs_dict_get(req, "content-type");
  194. xs *args = NULL;
  195. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  196. args = xs_json_loads(payload);
  197. else
  198. args = xs_dup(xs_dict_get(req, "p_vars"));
  199. xs *cmd = xs_replace_n(q_path, "/oauth", "", 1);
  200. srv_debug(1, xs_fmt("oauth_post_handler %s", q_path));
  201. if (strcmp(cmd, "/x-snac-login") == 0) { /** **/
  202. const char *login = xs_dict_get(args, "login");
  203. const char *passwd = xs_dict_get(args, "passwd");
  204. const char *redir = xs_dict_get(args, "redir");
  205. const char *cid = xs_dict_get(args, "cid");
  206. const char *state = xs_dict_get(args, "state");
  207. const char *host = xs_dict_get(srv_config, "host");
  208. /* by default, generate another login form with an error */
  209. *body = xs_fmt(login_page, host, "LOGIN INCORRECT", host, "oauth/x-snac-login",
  210. redir, cid, state, USER_AGENT);
  211. *ctype = "text/html";
  212. status = 200;
  213. if (login && passwd && redir && cid) {
  214. snac snac;
  215. if (user_open(&snac, login)) {
  216. /* check the login + password */
  217. if (check_password(login, passwd, xs_dict_get(snac.config, "passwd"))) {
  218. /* success! redirect to the desired uri */
  219. xs *code = random_str();
  220. xs_free(*body);
  221. *body = xs_fmt("%s?code=%s", redir, code);
  222. status = 303;
  223. /* if there is a state, add it */
  224. if (!xs_is_null(state) && *state) {
  225. *body = xs_str_cat(*body, "&state=");
  226. *body = xs_str_cat(*body, state);
  227. }
  228. srv_log(xs_fmt("oauth x-snac-login: '%s' success, redirect to %s",
  229. login, *body));
  230. /* assign the login to the app */
  231. xs *app = app_get(cid);
  232. if (app != NULL) {
  233. app = xs_dict_set(app, "uid", login);
  234. app = xs_dict_set(app, "code", code);
  235. app_add(cid, app);
  236. }
  237. else
  238. srv_log(xs_fmt("oauth x-snac-login: error getting app %s", cid));
  239. }
  240. else
  241. srv_debug(1, xs_fmt("oauth x-snac-login: login '%s' incorrect", login));
  242. user_free(&snac);
  243. }
  244. else
  245. srv_debug(1, xs_fmt("oauth x-snac-login: bad user '%s'", login));
  246. }
  247. else
  248. srv_debug(1, xs_fmt("oauth x-snac-login: invalid or unset arguments"));
  249. }
  250. else
  251. if (strcmp(cmd, "/token") == 0) { /** **/
  252. xs *wrk = NULL;
  253. const char *gtype = xs_dict_get(args, "grant_type");
  254. const char *code = xs_dict_get(args, "code");
  255. const char *cid = xs_dict_get(args, "client_id");
  256. const char *csec = xs_dict_get(args, "client_secret");
  257. const char *ruri = xs_dict_get(args, "redirect_uri");
  258. /* FIXME: this 'scope' parameter is mandatory for the official Mastodon API,
  259. but if it's enabled, it makes it crash after some more steps, which
  260. is FAR WORSE */
  261. // const char *scope = xs_dict_get(args, "scope");
  262. const char *scope = NULL;
  263. /* no client_secret? check if it's inside an authorization header
  264. (AndStatus does it this way) */
  265. if (xs_is_null(csec)) {
  266. const char *auhdr = xs_dict_get(req, "authorization");
  267. if (!xs_is_null(auhdr) && xs_startswith(auhdr, "Basic ")) {
  268. xs *s1 = xs_replace_n(auhdr, "Basic ", "", 1);
  269. int size;
  270. xs *s2 = xs_base64_dec(s1, &size);
  271. if (!xs_is_null(s2)) {
  272. xs *l1 = xs_split(s2, ":");
  273. if (xs_list_len(l1) == 2) {
  274. wrk = xs_dup(xs_list_get(l1, 1));
  275. csec = wrk;
  276. }
  277. }
  278. }
  279. }
  280. if (gtype && code && cid && csec && ruri) {
  281. xs *app = app_get(cid);
  282. if (app == NULL) {
  283. status = 401;
  284. srv_log(xs_fmt("oauth token: invalid app %s", cid));
  285. }
  286. else
  287. if (strcmp(csec, xs_dict_get(app, "client_secret")) != 0) {
  288. status = 401;
  289. srv_log(xs_fmt("oauth token: invalid client_secret for app %s", cid));
  290. }
  291. else {
  292. xs *rsp = xs_dict_new();
  293. xs *cat = xs_number_new(time(NULL));
  294. xs *tokid = random_str();
  295. rsp = xs_dict_append(rsp, "access_token", tokid);
  296. rsp = xs_dict_append(rsp, "token_type", "Bearer");
  297. rsp = xs_dict_append(rsp, "created_at", cat);
  298. if (!xs_is_null(scope))
  299. rsp = xs_dict_append(rsp, "scope", scope);
  300. *body = xs_json_dumps_pp(rsp, 4);
  301. *ctype = "application/json";
  302. status = 200;
  303. const char *uid = xs_dict_get(app, "uid");
  304. srv_debug(1, xs_fmt("oauth token: "
  305. "successful login for %s, new token %s", uid, tokid));
  306. xs *token = xs_dict_new();
  307. token = xs_dict_append(token, "token", tokid);
  308. token = xs_dict_append(token, "client_id", cid);
  309. token = xs_dict_append(token, "client_secret", csec);
  310. token = xs_dict_append(token, "uid", uid);
  311. token = xs_dict_append(token, "code", code);
  312. token_add(tokid, token);
  313. }
  314. }
  315. else {
  316. srv_debug(1, xs_fmt("oauth token: invalid or unset arguments"));
  317. status = 400;
  318. }
  319. }
  320. else
  321. if (strcmp(cmd, "/revoke") == 0) { /** **/
  322. const char *cid = xs_dict_get(args, "client_id");
  323. const char *csec = xs_dict_get(args, "client_secret");
  324. const char *tokid = xs_dict_get(args, "token");
  325. if (cid && csec && tokid) {
  326. xs *token = token_get(tokid);
  327. *body = xs_str_new("{}");
  328. *ctype = "application/json";
  329. if (token == NULL || strcmp(csec, xs_dict_get(token, "client_secret")) != 0) {
  330. srv_debug(1, xs_fmt("oauth revoke: bad secret for token %s", tokid));
  331. status = 403;
  332. }
  333. else {
  334. token_del(tokid);
  335. srv_debug(1, xs_fmt("oauth revoke: revoked token %s", tokid));
  336. status = 200;
  337. /* also delete the app, as it serves no purpose from now on */
  338. app_del(cid);
  339. }
  340. }
  341. else {
  342. srv_debug(1, xs_fmt("oauth revoke: invalid or unset arguments"));
  343. status = 403;
  344. }
  345. }
  346. if (strcmp(cmd, "/x-snac-get-token") == 0) { /** **/
  347. const char *login = xs_dict_get(args, "login");
  348. const char *passwd = xs_dict_get(args, "passwd");
  349. const char *host = xs_dict_get(srv_config, "host");
  350. /* by default, generate another login form with an error */
  351. *body = xs_fmt(login_page, host, "LOGIN INCORRECT", host, "oauth/x-snac-get-token",
  352. "", "", "", USER_AGENT);
  353. *ctype = "text/html";
  354. status = 200;
  355. if (login && passwd) {
  356. snac user;
  357. if (user_open(&user, login)) {
  358. /* check the login + password */
  359. if (check_password(login, passwd, xs_dict_get(user.config, "passwd"))) {
  360. /* success! create a new token */
  361. xs *tokid = random_str();
  362. srv_debug(1, xs_fmt("x-snac-new-token: "
  363. "successful login for %s, new token %s", login, tokid));
  364. xs *token = xs_dict_new();
  365. token = xs_dict_append(token, "token", tokid);
  366. token = xs_dict_append(token, "client_id", "snac-client");
  367. token = xs_dict_append(token, "client_secret", "");
  368. token = xs_dict_append(token, "uid", login);
  369. token = xs_dict_append(token, "code", "");
  370. token_add(tokid, token);
  371. *ctype = "text/plain";
  372. xs_free(*body);
  373. *body = xs_dup(tokid);
  374. }
  375. user_free(&user);
  376. }
  377. }
  378. }
  379. return status;
  380. }
  381. xs_str *mastoapi_id(const xs_dict *msg)
  382. /* returns a somewhat Mastodon-compatible status id */
  383. {
  384. const char *id = xs_dict_get(msg, "id");
  385. xs *md5 = xs_md5_hex(id, strlen(id));
  386. return xs_fmt("%10.0f%s", object_ctime_by_md5(md5), md5);
  387. }
  388. #define MID_TO_MD5(id) (id + 10)
  389. xs_dict *mastoapi_account(const xs_dict *actor)
  390. /* converts an ActivityPub actor to a Mastodon account */
  391. {
  392. xs_dict *acct = xs_dict_new();
  393. const char *prefu = xs_dict_get(actor, "preferredUsername");
  394. const char *display_name = xs_dict_get(actor, "name");
  395. if (xs_is_null(display_name) || *display_name == '\0')
  396. display_name = prefu;
  397. const char *id = xs_dict_get(actor, "id");
  398. const char *pub = xs_dict_get(actor, "published");
  399. xs *acct_md5 = xs_md5_hex(id, strlen(id));
  400. acct = xs_dict_append(acct, "id", acct_md5);
  401. acct = xs_dict_append(acct, "username", prefu);
  402. acct = xs_dict_append(acct, "display_name", display_name);
  403. {
  404. /* create the acct field as user@host */
  405. xs *l = xs_split(id, "/");
  406. xs *fquid = xs_fmt("%s@%s", prefu, xs_list_get(l, 2));
  407. acct = xs_dict_append(acct, "acct", fquid);
  408. }
  409. if (pub)
  410. acct = xs_dict_append(acct, "created_at", pub);
  411. else {
  412. /* unset created_at crashes Tusky, so lie like a mf */
  413. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  414. acct = xs_dict_append(acct, "created_at", date);
  415. }
  416. const char *note = xs_dict_get(actor, "summary");
  417. if (xs_is_null(note))
  418. note = "";
  419. acct = xs_dict_append(acct, "note", note);
  420. acct = xs_dict_append(acct, "url", id);
  421. xs *avatar = NULL;
  422. xs_dict *av = xs_dict_get(actor, "icon");
  423. if (xs_type(av) == XSTYPE_DICT)
  424. avatar = xs_dup(xs_dict_get(av, "url"));
  425. else
  426. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  427. acct = xs_dict_append(acct, "avatar", avatar);
  428. /* emojis */
  429. xs_list *p;
  430. if (!xs_is_null(p = xs_dict_get(actor, "tag"))) {
  431. xs *eml = xs_list_new();
  432. xs_dict *v;
  433. xs *t = xs_val_new(XSTYPE_TRUE);
  434. while (xs_list_iter(&p, &v)) {
  435. const char *type = xs_dict_get(v, "type");
  436. if (!xs_is_null(type) && strcmp(type, "Emoji") == 0) {
  437. const char *name = xs_dict_get(v, "name");
  438. const xs_dict *icon = xs_dict_get(v, "icon");
  439. if (!xs_is_null(name) && !xs_is_null(icon)) {
  440. const char *url = xs_dict_get(icon, "url");
  441. if (!xs_is_null(url)) {
  442. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  443. xs *d1 = xs_dict_new();
  444. d1 = xs_dict_append(d1, "shortcode", nm);
  445. d1 = xs_dict_append(d1, "url", url);
  446. d1 = xs_dict_append(d1, "static_url", url);
  447. d1 = xs_dict_append(d1, "visible_in_picker", t);
  448. eml = xs_list_append(eml, d1);
  449. }
  450. }
  451. }
  452. }
  453. acct = xs_dict_append(acct, "emojis", eml);
  454. }
  455. acct = xs_dict_append(acct, "header", "");
  456. return acct;
  457. }
  458. xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
  459. /* converts an ActivityPub note to a Mastodon status */
  460. {
  461. xs *actor = NULL;
  462. actor_get(snac, xs_dict_get(msg, "attributedTo"), &actor);
  463. /* if the author is not here, discard */
  464. if (actor == NULL)
  465. return NULL;
  466. xs *acct = mastoapi_account(actor);
  467. xs *f = xs_val_new(XSTYPE_FALSE);
  468. xs *t = xs_val_new(XSTYPE_TRUE);
  469. xs *n = xs_val_new(XSTYPE_NULL);
  470. xs *idx = NULL;
  471. xs *ixc = NULL;
  472. char *tmp;
  473. char *id = xs_dict_get(msg, "id");
  474. xs *mid = mastoapi_id(msg);
  475. xs_dict *st = xs_dict_new();
  476. st = xs_dict_append(st, "id", mid);
  477. st = xs_dict_append(st, "uri", id);
  478. st = xs_dict_append(st, "url", id);
  479. st = xs_dict_append(st, "created_at", xs_dict_get(msg, "published"));
  480. st = xs_dict_append(st, "account", acct);
  481. st = xs_dict_append(st, "content", xs_dict_get(msg, "content"));
  482. st = xs_dict_append(st, "visibility",
  483. is_msg_public(snac, msg) ? "public" : "private");
  484. tmp = xs_dict_get(msg, "sensitive");
  485. if (xs_is_null(tmp))
  486. tmp = f;
  487. st = xs_dict_append(st, "sensitive", tmp);
  488. tmp = xs_dict_get(msg, "summary");
  489. if (xs_is_null(tmp))
  490. tmp = "";
  491. st = xs_dict_append(st, "spoiler_text", tmp);
  492. /* create the list of attachments */
  493. xs *matt = xs_list_new();
  494. xs_list *att = xs_dict_get(msg, "attachment");
  495. xs_str *aobj;
  496. while (xs_list_iter(&att, &aobj)) {
  497. const char *mtype = xs_dict_get(aobj, "mediaType");
  498. if (!xs_is_null(mtype)) {
  499. if (xs_startswith(mtype, "image/") || xs_startswith(mtype, "video/")) {
  500. xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
  501. xs *matte = xs_dict_new();
  502. matte = xs_dict_append(matte, "id", matteid);
  503. matte = xs_dict_append(matte, "type", *mtype == 'i' ? "image" : "video");
  504. matte = xs_dict_append(matte, "url", xs_dict_get(aobj, "url"));
  505. matte = xs_dict_append(matte, "preview_url", xs_dict_get(aobj, "url"));
  506. matte = xs_dict_append(matte, "remote_url", xs_dict_get(aobj, "url"));
  507. const char *name = xs_dict_get(aobj, "name");
  508. if (xs_is_null(name))
  509. name = "";
  510. matte = xs_dict_append(matte, "description", name);
  511. matt = xs_list_append(matt, matte);
  512. }
  513. }
  514. }
  515. st = xs_dict_append(st, "media_attachments", matt);
  516. {
  517. xs *ml = xs_list_new();
  518. xs *htl = xs_list_new();
  519. xs *eml = xs_list_new();
  520. xs_list *p = xs_dict_get(msg, "tag");
  521. xs_dict *v;
  522. int n = 0;
  523. while (xs_list_iter(&p, &v)) {
  524. const char *type = xs_dict_get(v, "type");
  525. if (xs_is_null(type))
  526. continue;
  527. xs *d1 = xs_dict_new();
  528. if (strcmp(type, "Mention") == 0) {
  529. const char *name = xs_dict_get(v, "name");
  530. const char *href = xs_dict_get(v, "href");
  531. if (!xs_is_null(name) && !xs_is_null(href) &&
  532. strcmp(href, snac->actor) != 0) {
  533. xs *nm = xs_strip_chars_i(xs_dup(name), "@");
  534. xs *id = xs_fmt("%d", n++);
  535. d1 = xs_dict_append(d1, "id", id);
  536. d1 = xs_dict_append(d1, "username", nm);
  537. d1 = xs_dict_append(d1, "acct", nm);
  538. d1 = xs_dict_append(d1, "url", href);
  539. ml = xs_list_append(ml, d1);
  540. }
  541. }
  542. else
  543. if (strcmp(type, "Hashtag") == 0) {
  544. const char *name = xs_dict_get(v, "name");
  545. const char *href = xs_dict_get(v, "href");
  546. if (!xs_is_null(name) && !xs_is_null(href)) {
  547. xs *nm = xs_strip_chars_i(xs_dup(name), "#");
  548. d1 = xs_dict_append(d1, "name", nm);
  549. d1 = xs_dict_append(d1, "url", href);
  550. htl = xs_list_append(htl, d1);
  551. }
  552. }
  553. else
  554. if (strcmp(type, "Emoji") == 0) {
  555. const char *name = xs_dict_get(v, "name");
  556. const xs_dict *icon = xs_dict_get(v, "icon");
  557. if (!xs_is_null(name) && !xs_is_null(icon)) {
  558. const char *url = xs_dict_get(icon, "url");
  559. if (!xs_is_null(url)) {
  560. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  561. xs *t = xs_val_new(XSTYPE_TRUE);
  562. d1 = xs_dict_append(d1, "shortcode", nm);
  563. d1 = xs_dict_append(d1, "url", url);
  564. d1 = xs_dict_append(d1, "static_url", url);
  565. d1 = xs_dict_append(d1, "visible_in_picker", t);
  566. d1 = xs_dict_append(d1, "category", "Emojis");
  567. eml = xs_list_append(eml, d1);
  568. }
  569. }
  570. }
  571. }
  572. st = xs_dict_append(st, "mentions", ml);
  573. st = xs_dict_append(st, "tags", htl);
  574. st = xs_dict_append(st, "emojis", eml);
  575. }
  576. xs_free(idx);
  577. xs_free(ixc);
  578. idx = object_likes(id);
  579. ixc = xs_number_new(xs_list_len(idx));
  580. st = xs_dict_append(st, "favourites_count", ixc);
  581. st = xs_dict_append(st, "favourited",
  582. xs_list_in(idx, snac->md5) != -1 ? t : f);
  583. xs_free(idx);
  584. xs_free(ixc);
  585. idx = object_announces(id);
  586. ixc = xs_number_new(xs_list_len(idx));
  587. st = xs_dict_append(st, "reblogs_count", ixc);
  588. st = xs_dict_append(st, "reblogged",
  589. xs_list_in(idx, snac->md5) != -1 ? t : f);
  590. xs_free(idx);
  591. xs_free(ixc);
  592. idx = object_children(id);
  593. ixc = xs_number_new(xs_list_len(idx));
  594. st = xs_dict_append(st, "replies_count", ixc);
  595. /* default in_reply_to values */
  596. st = xs_dict_append(st, "in_reply_to_id", n);
  597. st = xs_dict_append(st, "in_reply_to_account_id", n);
  598. tmp = xs_dict_get(msg, "inReplyTo");
  599. if (!xs_is_null(tmp)) {
  600. xs *irto = NULL;
  601. if (valid_status(object_get(tmp, &irto))) {
  602. xs *irt_mid = mastoapi_id(irto);
  603. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  604. char *at = NULL;
  605. if (!xs_is_null(at = xs_dict_get(irto, "attributedTo"))) {
  606. xs *at_md5 = xs_md5_hex(at, strlen(at));
  607. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  608. }
  609. }
  610. }
  611. st = xs_dict_append(st, "reblog", n);
  612. st = xs_dict_append(st, "poll", n);
  613. st = xs_dict_append(st, "card", n);
  614. st = xs_dict_append(st, "language", n);
  615. tmp = xs_dict_get(msg, "sourceContent");
  616. if (xs_is_null(tmp))
  617. tmp = "";
  618. st = xs_dict_append(st, "text", tmp);
  619. tmp = xs_dict_get(msg, "updated");
  620. if (xs_is_null(tmp))
  621. tmp = n;
  622. st = xs_dict_append(st, "edited_at", tmp);
  623. /* is it a poll? */
  624. if (strcmp(xs_dict_get(msg, "type"), "Question") == 0) {
  625. xs *poll = xs_dict_new();
  626. xs_list *opts = NULL;
  627. xs_list *p;
  628. xs_val *v;
  629. int num_votes = 0;
  630. xs *options = xs_list_new();
  631. poll = xs_dict_append(poll, "id", mid);
  632. poll = xs_dict_append(poll, "expires_at", xs_dict_get(msg, "endTime"));
  633. poll = xs_dict_append(poll, "expired", xs_dict_get(msg, "closed") != NULL ? t : f);
  634. if ((opts = xs_dict_get(msg, "oneOf")) != NULL)
  635. poll = xs_dict_append(poll, "multiple", f);
  636. else {
  637. opts = xs_dict_get(msg, "anyOf");
  638. poll = xs_dict_append(poll, "multiple", t);
  639. }
  640. while (xs_list_iter(&opts, &v)) {
  641. const char *title = xs_dict_get(v, "name");
  642. const char *replies = xs_dict_get(v, "replies");
  643. if (title && replies) {
  644. const char *votes_count = xs_dict_get(replies, "totalItems");
  645. if (xs_type(votes_count) == XSTYPE_NUMBER) {
  646. xs *d = xs_dict_new();
  647. d = xs_dict_append(d, "title", title);
  648. d = xs_dict_append(d, "votes_count", votes_count);
  649. options = xs_list_append(options, d);
  650. num_votes += xs_number_get(votes_count);
  651. }
  652. }
  653. }
  654. poll = xs_dict_append(poll, "options", options);
  655. xs *vc = xs_number_new(num_votes);
  656. poll = xs_dict_append(poll, "votes_count", vc);
  657. xs *children = object_children(id);
  658. int voted = 0;
  659. p = children;
  660. while (xs_list_iter(&p, &v)) {
  661. xs *obj = NULL;
  662. if (valid_status(object_get_by_md5(v, &obj))) {
  663. if (strcmp(xs_dict_get(obj, "attributedTo"), snac->actor) == 0) {
  664. voted = 1;
  665. break;
  666. }
  667. }
  668. }
  669. poll = xs_dict_append(poll, "voted", voted ? t : f);
  670. st = xs_dict_append(st, "poll", poll);
  671. }
  672. return st;
  673. }
  674. xs_dict *mastoapi_relationship(snac *snac, const char *md5)
  675. {
  676. xs_dict *rel = NULL;
  677. xs *actor_o = NULL;
  678. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  679. xs *t = xs_val_new(XSTYPE_TRUE);
  680. xs *f = xs_val_new(XSTYPE_FALSE);
  681. rel = xs_dict_new();
  682. const char *actor = xs_dict_get(actor_o, "id");
  683. rel = xs_dict_append(rel, "id", md5);
  684. rel = xs_dict_append(rel, "following",
  685. following_check(snac, actor) ? t : f);
  686. rel = xs_dict_append(rel, "showing_reblogs", t);
  687. rel = xs_dict_append(rel, "notifying", f);
  688. rel = xs_dict_append(rel, "followed_by",
  689. follower_check(snac, actor) ? t : f);
  690. rel = xs_dict_append(rel, "blocking",
  691. is_muted(snac, actor) ? t : f);
  692. rel = xs_dict_append(rel, "muting", f);
  693. rel = xs_dict_append(rel, "muting_notifications", f);
  694. rel = xs_dict_append(rel, "requested", f);
  695. rel = xs_dict_append(rel, "domain_blocking", f);
  696. rel = xs_dict_append(rel, "endorsed", f);
  697. rel = xs_dict_append(rel, "note", "");
  698. }
  699. return rel;
  700. }
  701. int process_auth_token(snac *snac, const xs_dict *req)
  702. /* processes an authorization token, if there is one */
  703. {
  704. int logged_in = 0;
  705. char *v;
  706. /* if there is an authorization field, try to validate it */
  707. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  708. xs *tokid = xs_replace_n(v, "Bearer ", "", 1);
  709. xs *token = token_get(tokid);
  710. if (token != NULL) {
  711. const char *uid = xs_dict_get(token, "uid");
  712. if (!xs_is_null(uid) && user_open(snac, uid)) {
  713. logged_in = 1;
  714. /* this counts as a 'login' */
  715. lastlog_write(snac, "mastoapi");
  716. srv_debug(2, xs_fmt("mastoapi auth: valid token for user %s", uid));
  717. }
  718. else
  719. srv_log(xs_fmt("mastoapi auth: corrupted token %s", tokid));
  720. }
  721. else
  722. srv_log(xs_fmt("mastoapi auth: invalid token %s", tokid));
  723. }
  724. return logged_in;
  725. }
  726. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  727. char **body, int *b_size, char **ctype)
  728. {
  729. (void)b_size;
  730. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  731. return 0;
  732. srv_debug(1, xs_fmt("mastoapi_get_handler %s", q_path));
  733. /* {
  734. xs *j = xs_json_dumps_pp(req, 4);
  735. printf("mastoapi get:\n%s\n", j);
  736. }*/
  737. int status = 404;
  738. xs_dict *args = xs_dict_get(req, "q_vars");
  739. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  740. snac snac1 = {0};
  741. int logged_in = process_auth_token(&snac1, req);
  742. if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) { /** **/
  743. if (logged_in) {
  744. xs *acct = xs_dict_new();
  745. acct = xs_dict_append(acct, "id", xs_dict_get(snac1.config, "uid"));
  746. acct = xs_dict_append(acct, "username", xs_dict_get(snac1.config, "uid"));
  747. acct = xs_dict_append(acct, "acct", xs_dict_get(snac1.config, "uid"));
  748. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac1.config, "name"));
  749. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac1.config, "published"));
  750. acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio"));
  751. acct = xs_dict_append(acct, "url", snac1.actor);
  752. acct = xs_dict_append(acct, "header", "");
  753. xs *avatar = NULL;
  754. char *av = xs_dict_get(snac1.config, "avatar");
  755. if (xs_is_null(av) || *av == '\0')
  756. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  757. else
  758. avatar = xs_dup(av);
  759. acct = xs_dict_append(acct, "avatar", avatar);
  760. *body = xs_json_dumps_pp(acct, 4);
  761. *ctype = "application/json";
  762. status = 200;
  763. }
  764. else {
  765. status = 422; // "Unprocessable entity" (no login)
  766. }
  767. }
  768. else
  769. if (strcmp(cmd, "/v1/accounts/relationships") == 0) { /** **/
  770. /* find if an account is followed, blocked, etc. */
  771. /* the account to get relationships about is in args "id[]" */
  772. if (logged_in) {
  773. xs *res = xs_list_new();
  774. const char *md5 = xs_dict_get(args, "id[]");
  775. if (!xs_is_null(md5)) {
  776. if (xs_type(md5) == XSTYPE_LIST)
  777. md5 = xs_list_get(md5, 0);
  778. xs *rel = mastoapi_relationship(&snac1, md5);
  779. if (rel != NULL)
  780. res = xs_list_append(res, rel);
  781. }
  782. *body = xs_json_dumps_pp(res, 4);
  783. *ctype = "application/json";
  784. status = 200;
  785. }
  786. else
  787. status = 422;
  788. }
  789. else
  790. if (xs_startswith(cmd, "/v1/accounts/")) { /** **/
  791. /* account-related information */
  792. xs *l = xs_split(cmd, "/");
  793. const char *uid = xs_list_get(l, 3);
  794. const char *opt = xs_list_get(l, 4);
  795. if (uid != NULL) {
  796. snac snac2;
  797. xs *out = NULL;
  798. xs *actor = NULL;
  799. if (logged_in && strcmp(uid, "search") == 0) { /** **/
  800. /* search for accounts starting with q */
  801. const char *aq = xs_dict_get(args, "q");
  802. if (!xs_is_null(aq)) {
  803. xs *q = xs_tolower_i(xs_dup(aq));
  804. out = xs_list_new();
  805. xs *wing = following_list(&snac1);
  806. xs *wers = follower_list(&snac1);
  807. xs *ulst = user_list();
  808. xs_list *p;
  809. xs_str *v;
  810. xs_set seen;
  811. xs_set_init(&seen);
  812. /* user relations */
  813. xs_list *lsts[] = { wing, wers, NULL };
  814. int n;
  815. for (n = 0; (p = lsts[n]) != NULL; n++) {
  816. while (xs_list_iter(&p, &v)) {
  817. /* already seen? skip */
  818. if (xs_set_add(&seen, v) == 0)
  819. continue;
  820. xs *actor = NULL;
  821. if (valid_status(object_get(v, &actor))) {
  822. const char *uname = xs_dict_get(actor, "preferredUsername");
  823. if (!xs_is_null(uname)) {
  824. xs *luname = xs_tolower_i(xs_dup(uname));
  825. if (xs_startswith(luname, q)) {
  826. xs *acct = mastoapi_account(actor);
  827. out = xs_list_append(out, acct);
  828. }
  829. }
  830. }
  831. }
  832. }
  833. /* local users */
  834. p = ulst;
  835. while (xs_list_iter(&p, &v)) {
  836. snac user;
  837. /* skip this same user */
  838. if (strcmp(v, xs_dict_get(snac1.config, "uid")) == 0)
  839. continue;
  840. /* skip if the uid does not start with the query */
  841. xs *v2 = xs_tolower_i(xs_dup(v));
  842. if (!xs_startswith(v2, q))
  843. continue;
  844. if (user_open(&user, v)) {
  845. /* if it's not already seen, add it */
  846. if (xs_set_add(&seen, user.actor) == 1) {
  847. xs *actor = msg_actor(&user);
  848. xs *acct = mastoapi_account(actor);
  849. out = xs_list_append(out, acct);
  850. }
  851. user_free(&user);
  852. }
  853. }
  854. xs_set_free(&seen);
  855. }
  856. }
  857. else
  858. /* is it a local user? */
  859. if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
  860. if (opt == NULL) {
  861. /* account information */
  862. actor = msg_actor(&snac2);
  863. out = mastoapi_account(actor);
  864. }
  865. else
  866. if (strcmp(opt, "statuses") == 0) { /** **/
  867. /* the public list of posts of a user */
  868. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  869. xs_list *p = timeline;
  870. xs_str *v;
  871. out = xs_list_new();
  872. while (xs_list_iter(&p, &v)) {
  873. xs *msg = NULL;
  874. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  875. /* add only posts by the author */
  876. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  877. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  878. xs *st = mastoapi_status(&snac2, msg);
  879. out = xs_list_append(out, st);
  880. }
  881. }
  882. }
  883. }
  884. user_free(&snac2);
  885. }
  886. else {
  887. /* try the uid as the md5 of a possibly loaded actor */
  888. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  889. if (opt == NULL) {
  890. /* account information */
  891. out = mastoapi_account(actor);
  892. }
  893. else
  894. if (strcmp(opt, "statuses") == 0) {
  895. /* we don't serve statuses of others; return the empty list */
  896. out = xs_list_new();
  897. }
  898. }
  899. }
  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, "/v1/timelines/home") == 0) { /** **/
  909. /* the private timeline */
  910. if (logged_in) {
  911. const char *max_id = xs_dict_get(args, "max_id");
  912. const char *since_id = xs_dict_get(args, "since_id");
  913. const char *min_id = xs_dict_get(args, "min_id");
  914. const char *limit_s = xs_dict_get(args, "limit");
  915. int limit = 0;
  916. int cnt = 0;
  917. if (!xs_is_null(limit_s))
  918. limit = atoi(limit_s);
  919. if (limit == 0)
  920. limit = 20;
  921. xs *timeline = timeline_simple_list(&snac1, "private", 0, 256);
  922. xs *out = xs_list_new();
  923. xs_list *p = timeline;
  924. xs_str *v;
  925. while (xs_list_iter(&p, &v) && cnt < limit) {
  926. xs *msg = NULL;
  927. /* only return entries older that max_id */
  928. if (max_id) {
  929. if (strcmp(v, max_id) == 0)
  930. max_id = NULL;
  931. continue;
  932. }
  933. /* only returns entries newer than since_id */
  934. if (since_id) {
  935. if (strcmp(v, since_id) == 0)
  936. break;
  937. }
  938. /* only returns entries newer than min_id */
  939. /* what does really "Return results immediately newer than ID" mean? */
  940. if (min_id) {
  941. if (strcmp(v, min_id) == 0)
  942. break;
  943. }
  944. /* get the entry */
  945. if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
  946. continue;
  947. /* discard non-Notes */
  948. const char *type = xs_dict_get(msg, "type");
  949. if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0)
  950. continue;
  951. /* discard notes from muted morons */
  952. if (is_muted(&snac1, xs_dict_get(msg, "attributedTo")))
  953. continue;
  954. /* discard hidden notes */
  955. if (is_hidden(&snac1, xs_dict_get(msg, "id")))
  956. continue;
  957. /* discard poll votes (they have a name) */
  958. if (!xs_is_null(xs_dict_get(msg, "name")))
  959. continue;
  960. /* convert the Note into a Mastodon status */
  961. xs *st = mastoapi_status(&snac1, msg);
  962. if (st != NULL)
  963. out = xs_list_append(out, st);
  964. cnt++;
  965. }
  966. *body = xs_json_dumps_pp(out, 4);
  967. *ctype = "application/json";
  968. status = 200;
  969. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  970. }
  971. else {
  972. status = 401; // unauthorized
  973. }
  974. }
  975. else
  976. if (strcmp(cmd, "/v1/timelines/public") == 0) { /** **/
  977. /* the instance public timeline (public timelines for all users) */
  978. /* NOTE: this api call needs no authorization; but,
  979. I need a logged-in user in mastoapi_status() for
  980. is_msg_public() and the liked/boosted flags,
  981. so it will silently fail for pure public access */
  982. const char *limit_s = xs_dict_get(args, "limit");
  983. int limit = 0;
  984. int cnt = 0;
  985. if (!xs_is_null(limit_s))
  986. limit = atoi(limit_s);
  987. if (limit == 0)
  988. limit = 20;
  989. xs *timeline = timeline_instance_list(0, limit);
  990. xs *out = xs_list_new();
  991. xs_list *p = timeline;
  992. xs_str *md5;
  993. while (logged_in && xs_list_iter(&p, &md5) && cnt < limit) {
  994. xs *msg = NULL;
  995. /* get the entry */
  996. if (!valid_status(object_get_by_md5(md5, &msg)))
  997. continue;
  998. /* discard non-Notes */
  999. const char *type = xs_dict_get(msg, "type");
  1000. if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0)
  1001. continue;
  1002. /* convert the Note into a Mastodon status */
  1003. xs *st = mastoapi_status(&snac1, msg);
  1004. if (st != NULL) {
  1005. out = xs_list_append(out, st);
  1006. cnt++;
  1007. }
  1008. }
  1009. *body = xs_json_dumps_pp(out, 4);
  1010. *ctype = "application/json";
  1011. status = 200;
  1012. }
  1013. else
  1014. if (strcmp(cmd, "/v1/conversations") == 0) { /** **/
  1015. /* TBD */
  1016. *body = xs_dup("[]");
  1017. *ctype = "application/json";
  1018. status = 200;
  1019. }
  1020. else
  1021. if (strcmp(cmd, "/v1/notifications") == 0) { /** **/
  1022. if (logged_in) {
  1023. xs *l = notify_list(&snac1, 0);
  1024. xs *out = xs_list_new();
  1025. xs_list *p = l;
  1026. xs_dict *v;
  1027. xs_list *excl = xs_dict_get(args, "exclude_types[]");
  1028. while (xs_list_iter(&p, &v)) {
  1029. xs *noti = notify_get(&snac1, v);
  1030. if (noti == NULL)
  1031. continue;
  1032. const char *type = xs_dict_get(noti, "type");
  1033. const char *utype = xs_dict_get(noti, "utype");
  1034. const char *objid = xs_dict_get(noti, "objid");
  1035. xs *actor = NULL;
  1036. xs *entry = NULL;
  1037. if (!valid_status(actor_get(&snac1, xs_dict_get(noti, "actor"), &actor)))
  1038. continue;
  1039. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  1040. continue;
  1041. if (is_hidden(&snac1, objid))
  1042. continue;
  1043. /* convert the type */
  1044. if (strcmp(type, "Like") == 0)
  1045. type = "favourite";
  1046. else
  1047. if (strcmp(type, "Announce") == 0)
  1048. type = "reblog";
  1049. else
  1050. if (strcmp(type, "Follow") == 0)
  1051. type = "follow";
  1052. else
  1053. if (strcmp(type, "Create") == 0)
  1054. type = "mention";
  1055. else
  1056. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  1057. type = "poll";
  1058. else
  1059. continue;
  1060. /* excluded type? */
  1061. if (!xs_is_null(excl) && xs_list_in(excl, type) != -1)
  1062. continue;
  1063. xs *mn = xs_dict_new();
  1064. mn = xs_dict_append(mn, "type", type);
  1065. xs *id = xs_replace(xs_dict_get(noti, "id"), ".", "");
  1066. mn = xs_dict_append(mn, "id", id);
  1067. mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
  1068. xs *acct = mastoapi_account(actor);
  1069. mn = xs_dict_append(mn, "account", acct);
  1070. if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
  1071. xs *st = mastoapi_status(&snac1, entry);
  1072. mn = xs_dict_append(mn, "status", st);
  1073. }
  1074. out = xs_list_append(out, mn);
  1075. }
  1076. *body = xs_json_dumps_pp(out, 4);
  1077. *ctype = "application/json";
  1078. status = 200;
  1079. }
  1080. else
  1081. status = 401;
  1082. }
  1083. else
  1084. if (strcmp(cmd, "/v1/filters") == 0) { /** **/
  1085. /* snac will never have filters */
  1086. *body = xs_dup("[]");
  1087. *ctype = "application/json";
  1088. status = 200;
  1089. }
  1090. else
  1091. if (strcmp(cmd, "/v1/favourites") == 0) { /** **/
  1092. /* snac will never support a list of favourites */
  1093. *body = xs_dup("[]");
  1094. *ctype = "application/json";
  1095. status = 200;
  1096. }
  1097. else
  1098. if (strcmp(cmd, "/v1/bookmarks") == 0) { /** **/
  1099. /* snac does not support bookmarks */
  1100. *body = xs_dup("[]");
  1101. *ctype = "application/json";
  1102. status = 200;
  1103. }
  1104. else
  1105. if (strcmp(cmd, "/v1/lists") == 0) { /** **/
  1106. /* snac does not support lists */
  1107. *body = xs_dup("[]");
  1108. *ctype = "application/json";
  1109. status = 200;
  1110. }
  1111. else
  1112. if (strcmp(cmd, "/v1/scheduled_statuses") == 0) { /** **/
  1113. /* snac does not schedule notes */
  1114. *body = xs_dup("[]");
  1115. *ctype = "application/json";
  1116. status = 200;
  1117. }
  1118. else
  1119. if (strcmp(cmd, "/v1/follow_requests") == 0) { /** **/
  1120. /* snac does not support optional follow confirmations */
  1121. *body = xs_dup("[]");
  1122. *ctype = "application/json";
  1123. status = 200;
  1124. }
  1125. else
  1126. if (strcmp(cmd, "/v1/announcements") == 0) { /** **/
  1127. /* snac has no announcements (yet?) */
  1128. *body = xs_dup("[]");
  1129. *ctype = "application/json";
  1130. status = 200;
  1131. }
  1132. else
  1133. if (strcmp(cmd, "/v1/custom_emojis") == 0) { /** **/
  1134. /* are you kidding me? */
  1135. *body = xs_dup("[]");
  1136. *ctype = "application/json";
  1137. status = 200;
  1138. }
  1139. else
  1140. if (strcmp(cmd, "/v1/instance") == 0) { /** **/
  1141. /* returns an instance object */
  1142. xs *ins = xs_dict_new();
  1143. const char *host = xs_dict_get(srv_config, "host");
  1144. ins = xs_dict_append(ins, "uri", host);
  1145. ins = xs_dict_append(ins, "domain", host);
  1146. ins = xs_dict_append(ins, "title", host);
  1147. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  1148. ins = xs_dict_append(ins, "source_url", WHAT_IS_SNAC_URL);
  1149. ins = xs_dict_append(ins, "description", host);
  1150. ins = xs_dict_append(ins, "short_description", host);
  1151. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  1152. ins = xs_dict_append(ins, "thumbnail", susie);
  1153. const char *v = xs_dict_get(srv_config, "admin_email");
  1154. if (xs_is_null(v) || *v == '\0')
  1155. v = "admin@localhost";
  1156. ins = xs_dict_append(ins, "email", v);
  1157. xs *l1 = xs_list_new();
  1158. ins = xs_dict_append(ins, "rules", l1);
  1159. l1 = xs_list_append(l1, "en");
  1160. ins = xs_dict_append(ins, "languages", l1);
  1161. xs *d1 = xs_dict_new();
  1162. ins = xs_dict_append(ins, "urls", d1);
  1163. xs *z = xs_number_new(0);
  1164. d1 = xs_dict_append(d1, "user_count", z);
  1165. d1 = xs_dict_append(d1, "status_count", z);
  1166. d1 = xs_dict_append(d1, "domain_count", z);
  1167. ins = xs_dict_append(ins, "stats", d1);
  1168. xs *f = xs_val_new(XSTYPE_FALSE);
  1169. ins = xs_dict_append(ins, "registrations", f);
  1170. ins = xs_dict_append(ins, "approval_required", f);
  1171. ins = xs_dict_append(ins, "invites_enabled", f);
  1172. xs *cfg = xs_dict_new();
  1173. {
  1174. xs *d11 = xs_dict_new();
  1175. xs *mc = xs_number_new(100000);
  1176. xs *mm = xs_number_new(8);
  1177. xs *cr = xs_number_new(32);
  1178. d11 = xs_dict_append(d11, "max_characters", mc);
  1179. d11 = xs_dict_append(d11, "max_media_attachments", mm);
  1180. d11 = xs_dict_append(d11, "characters_reserved_per_url", cr);
  1181. cfg = xs_dict_append(cfg, "statuses", d11);
  1182. }
  1183. ins = xs_dict_append(ins, "configuration", cfg);
  1184. const char *admin_account = xs_dict_get(srv_config, "admin_account");
  1185. if (!xs_is_null(admin_account) && *admin_account) {
  1186. snac admin;
  1187. if (user_open(&admin, admin_account)) {
  1188. xs *actor = msg_actor(&admin);
  1189. xs *acct = mastoapi_account(actor);
  1190. ins = xs_dict_append(ins, "contact_account", acct);
  1191. user_free(&admin);
  1192. }
  1193. }
  1194. *body = xs_json_dumps_pp(ins, 4);
  1195. *ctype = "application/json";
  1196. status = 200;
  1197. }
  1198. else
  1199. if (xs_startswith(cmd, "/v1/statuses/")) { /** **/
  1200. /* information about a status */
  1201. xs *l = xs_split(cmd, "/");
  1202. const char *id = xs_list_get(l, 3);
  1203. const char *op = xs_list_get(l, 4);
  1204. if (!xs_is_null(id)) {
  1205. xs *msg = NULL;
  1206. xs *out = NULL;
  1207. /* skip the 'fake' part of the id */
  1208. id = MID_TO_MD5(id);
  1209. if (valid_status(object_get_by_md5(id, &msg))) {
  1210. if (op == NULL) {
  1211. if (!is_muted(&snac1, xs_dict_get(msg, "attributedTo"))) {
  1212. /* return the status itself */
  1213. out = mastoapi_status(&snac1, msg);
  1214. }
  1215. }
  1216. else
  1217. if (strcmp(op, "context") == 0) { /** **/
  1218. /* return ancestors and children */
  1219. xs *anc = xs_list_new();
  1220. xs *des = xs_list_new();
  1221. xs_list *p;
  1222. xs_str *v;
  1223. char pid[64];
  1224. /* build the [grand]parent list, moving up */
  1225. strncpy(pid, id, sizeof(pid));
  1226. while (object_parent(pid, pid, sizeof(pid))) {
  1227. xs *m2 = NULL;
  1228. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  1229. xs *st = mastoapi_status(&snac1, m2);
  1230. anc = xs_list_insert(anc, 0, st);
  1231. }
  1232. else
  1233. break;
  1234. }
  1235. /* build the children list */
  1236. xs *children = object_children(xs_dict_get(msg, "id"));
  1237. p = children;
  1238. while (xs_list_iter(&p, &v)) {
  1239. xs *m2 = NULL;
  1240. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  1241. if (xs_is_null(xs_dict_get(m2, "name"))) {
  1242. xs *st = mastoapi_status(&snac1, m2);
  1243. des = xs_list_append(des, st);
  1244. }
  1245. }
  1246. }
  1247. out = xs_dict_new();
  1248. out = xs_dict_append(out, "ancestors", anc);
  1249. out = xs_dict_append(out, "descendants", des);
  1250. }
  1251. else
  1252. if (strcmp(op, "reblogged_by") == 0 || /** **/
  1253. strcmp(op, "favourited_by") == 0) { /** **/
  1254. /* return the list of people who liked or boosted this */
  1255. out = xs_list_new();
  1256. xs *l = NULL;
  1257. if (op[0] == 'r')
  1258. l = object_announces(xs_dict_get(msg, "id"));
  1259. else
  1260. l = object_likes(xs_dict_get(msg, "id"));
  1261. xs_list *p = l;
  1262. xs_str *v;
  1263. while (xs_list_iter(&p, &v)) {
  1264. xs *actor2 = NULL;
  1265. if (valid_status(object_get_by_md5(v, &actor2))) {
  1266. xs *acct2 = mastoapi_account(actor2);
  1267. out = xs_list_append(out, acct2);
  1268. }
  1269. }
  1270. }
  1271. }
  1272. else
  1273. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  1274. if (out != NULL) {
  1275. *body = xs_json_dumps_pp(out, 4);
  1276. *ctype = "application/json";
  1277. status = 200;
  1278. }
  1279. }
  1280. }
  1281. else
  1282. if (strcmp(cmd, "/v1/preferences") == 0) { /** **/
  1283. *body = xs_dup("{}");
  1284. *ctype = "application/json";
  1285. status = 200;
  1286. }
  1287. else
  1288. if (strcmp(cmd, "/v1/markers") == 0) { /** **/
  1289. *body = xs_dup("{}");
  1290. *ctype = "application/json";
  1291. status = 200;
  1292. }
  1293. else
  1294. if (strcmp(cmd, "/v1/followed_tags") == 0) { /** **/
  1295. *body = xs_dup("[]");
  1296. *ctype = "application/json";
  1297. status = 200;
  1298. }
  1299. else
  1300. if (strcmp(cmd, "/v2/search") == 0) { /** **/
  1301. const char *q = xs_dict_get(args, "q");
  1302. const char *type = xs_dict_get(args, "type");
  1303. const char *offset = xs_dict_get(args, "offset");
  1304. xs *acl = xs_list_new();
  1305. xs *stl = xs_list_new();
  1306. xs *htl = xs_list_new();
  1307. xs *res = xs_dict_new();
  1308. if (xs_is_null(offset) || strcmp(offset, "0") == 0) {
  1309. /* reply something only for offset 0; otherwise,
  1310. apps like Tusky keep asking again and again */
  1311. if (!xs_is_null(q) && !xs_is_null(type) && strcmp(type, "accounts") == 0) {
  1312. /* do a webfinger query */
  1313. char *actor = NULL;
  1314. char *user = NULL;
  1315. if (valid_status(webfinger_request(q, &actor, &user))) {
  1316. xs *actor_o = NULL;
  1317. if (valid_status(actor_request(&snac1, actor, &actor_o))) {
  1318. xs *acct = mastoapi_account(actor_o);
  1319. acl = xs_list_append(acl, acct);
  1320. }
  1321. }
  1322. }
  1323. }
  1324. res = xs_dict_append(res, "accounts", acl);
  1325. res = xs_dict_append(res, "statuses", stl);
  1326. res = xs_dict_append(res, "hashtags", htl);
  1327. *body = xs_json_dumps_pp(res, 4);
  1328. *ctype = "application/json";
  1329. status = 200;
  1330. }
  1331. /* user cleanup */
  1332. if (logged_in)
  1333. user_free(&snac1);
  1334. return status;
  1335. }
  1336. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  1337. const char *payload, int p_size,
  1338. char **body, int *b_size, char **ctype)
  1339. {
  1340. (void)p_size;
  1341. (void)b_size;
  1342. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1343. return 0;
  1344. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1345. /* {
  1346. xs *j = xs_json_dumps_pp(req, 4);
  1347. printf("mastoapi post:\n%s\n", j);
  1348. }*/
  1349. int status = 404;
  1350. xs *args = NULL;
  1351. char *i_ctype = xs_dict_get(req, "content-type");
  1352. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1353. args = xs_json_loads(payload);
  1354. else
  1355. args = xs_dup(xs_dict_get(req, "p_vars"));
  1356. if (args == NULL)
  1357. return 400;
  1358. /* {
  1359. xs *j = xs_json_dumps_pp(args, 4);
  1360. printf("%s\n", j);
  1361. }*/
  1362. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1363. snac snac = {0};
  1364. int logged_in = process_auth_token(&snac, req);
  1365. if (strcmp(cmd, "/v1/apps") == 0) { /** **/
  1366. const char *name = xs_dict_get(args, "client_name");
  1367. const char *ruri = xs_dict_get(args, "redirect_uris");
  1368. const char *scope = xs_dict_get(args, "scope");
  1369. if (xs_type(ruri) == XSTYPE_LIST)
  1370. ruri = xs_dict_get(ruri, 0);
  1371. if (name && ruri) {
  1372. xs *app = xs_dict_new();
  1373. xs *id = xs_replace_i(tid(0), ".", "");
  1374. xs *csec = random_str();
  1375. xs *vkey = random_str();
  1376. xs *cid = NULL;
  1377. /* pick a non-existent random cid */
  1378. for (;;) {
  1379. cid = random_str();
  1380. xs *p_app = app_get(cid);
  1381. if (p_app == NULL)
  1382. break;
  1383. xs_free(cid);
  1384. }
  1385. app = xs_dict_append(app, "name", name);
  1386. app = xs_dict_append(app, "redirect_uri", ruri);
  1387. app = xs_dict_append(app, "client_id", cid);
  1388. app = xs_dict_append(app, "client_secret", csec);
  1389. app = xs_dict_append(app, "vapid_key", vkey);
  1390. app = xs_dict_append(app, "id", id);
  1391. *body = xs_json_dumps_pp(app, 4);
  1392. *ctype = "application/json";
  1393. status = 200;
  1394. app = xs_dict_append(app, "code", "");
  1395. if (scope)
  1396. app = xs_dict_append(app, "scope", scope);
  1397. app_add(cid, app);
  1398. srv_debug(1, xs_fmt("mastoapi apps: new app %s", cid));
  1399. }
  1400. }
  1401. else
  1402. if (strcmp(cmd, "/v1/statuses") == 0) { /** **/
  1403. if (logged_in) {
  1404. /* post a new Note */
  1405. /* {
  1406. xs *j = xs_json_dumps_pp(args, 4);
  1407. printf("%s\n", j);
  1408. }*/
  1409. const char *content = xs_dict_get(args, "status");
  1410. const char *mid = xs_dict_get(args, "in_reply_to_id");
  1411. const char *visibility = xs_dict_get(args, "visibility");
  1412. const char *summary = xs_dict_get(args, "spoiler_text");
  1413. const char *media_ids = xs_dict_get(args, "media_ids");
  1414. if (xs_is_null(media_ids))
  1415. media_ids = xs_dict_get(args, "media_ids[]");
  1416. if (xs_is_null(visibility))
  1417. visibility = "public";
  1418. xs *attach_list = xs_list_new();
  1419. xs *irt = NULL;
  1420. /* is it a reply? */
  1421. if (mid != NULL) {
  1422. xs *r_msg = NULL;
  1423. const char *md5 = MID_TO_MD5(mid);
  1424. if (valid_status(object_get_by_md5(md5, &r_msg)))
  1425. irt = xs_dup(xs_dict_get(r_msg, "id"));
  1426. }
  1427. /* does it have attachments? */
  1428. if (!xs_is_null(media_ids)) {
  1429. xs *mi = NULL;
  1430. if (xs_type(media_ids) == XSTYPE_LIST)
  1431. mi = xs_dup(media_ids);
  1432. else {
  1433. mi = xs_list_new();
  1434. mi = xs_list_append(mi, media_ids);
  1435. }
  1436. xs_list *p = mi;
  1437. xs_str *v;
  1438. while (xs_list_iter(&p, &v)) {
  1439. xs *l = xs_list_new();
  1440. xs *url = xs_fmt("%s/s/%s", snac.actor, v);
  1441. xs *desc = static_get_meta(&snac, v);
  1442. l = xs_list_append(l, url);
  1443. l = xs_list_append(l, desc);
  1444. attach_list = xs_list_append(attach_list, l);
  1445. }
  1446. }
  1447. /* prepare the message */
  1448. xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
  1449. strcmp(visibility, "public") == 0 ? 0 : 1);
  1450. if (!xs_is_null(summary) && *summary) {
  1451. xs *t = xs_val_new(XSTYPE_TRUE);
  1452. msg = xs_dict_set(msg, "sensitive", t);
  1453. msg = xs_dict_set(msg, "summary", summary);
  1454. }
  1455. /* store */
  1456. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1457. /* 'Create' message */
  1458. xs *c_msg = msg_create(&snac, msg);
  1459. enqueue_message(&snac, c_msg);
  1460. timeline_touch(&snac);
  1461. /* convert to a mastodon status as a response code */
  1462. xs *st = mastoapi_status(&snac, msg);
  1463. *body = xs_json_dumps_pp(st, 4);
  1464. *ctype = "application/json";
  1465. status = 200;
  1466. }
  1467. else
  1468. status = 401;
  1469. }
  1470. else
  1471. if (xs_startswith(cmd, "/v1/statuses")) { /** **/
  1472. if (logged_in) {
  1473. /* operations on a status */
  1474. xs *l = xs_split(cmd, "/");
  1475. const char *mid = xs_list_get(l, 3);
  1476. const char *op = xs_list_get(l, 4);
  1477. if (!xs_is_null(mid)) {
  1478. xs *msg = NULL;
  1479. xs *out = NULL;
  1480. /* skip the 'fake' part of the id */
  1481. mid = MID_TO_MD5(mid);
  1482. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1483. char *id = xs_dict_get(msg, "id");
  1484. if (op == NULL) {
  1485. /* no operation (?) */
  1486. }
  1487. else
  1488. if (strcmp(op, "favourite") == 0) { /** **/
  1489. xs *n_msg = msg_admiration(&snac, id, "Like");
  1490. if (n_msg != NULL) {
  1491. enqueue_message(&snac, n_msg);
  1492. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  1493. out = mastoapi_status(&snac, msg);
  1494. }
  1495. }
  1496. else
  1497. if (strcmp(op, "unfavourite") == 0) { /** **/
  1498. /* partial support: as the original Like message
  1499. is not stored anywhere here, it's not possible
  1500. to send an Undo + Like; the only thing done here
  1501. is to delete the actor from the list of likes */
  1502. object_unadmire(id, snac.actor, 1);
  1503. }
  1504. else
  1505. if (strcmp(op, "reblog") == 0) { /** **/
  1506. xs *n_msg = msg_admiration(&snac, id, "Announce");
  1507. if (n_msg != NULL) {
  1508. enqueue_message(&snac, n_msg);
  1509. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  1510. out = mastoapi_status(&snac, msg);
  1511. }
  1512. }
  1513. else
  1514. if (strcmp(op, "unreblog") == 0) { /** **/
  1515. /* partial support: see comment in 'unfavourite' */
  1516. object_unadmire(id, snac.actor, 0);
  1517. }
  1518. else
  1519. if (strcmp(op, "bookmark") == 0) { /** **/
  1520. /* snac does not support bookmarks */
  1521. }
  1522. else
  1523. if (strcmp(op, "unbookmark") == 0) { /** **/
  1524. /* snac does not support bookmarks */
  1525. }
  1526. else
  1527. if (strcmp(op, "pin") == 0) { /** **/
  1528. /* snac does not support pinning */
  1529. }
  1530. else
  1531. if (strcmp(op, "unpin") == 0) { /** **/
  1532. /* snac does not support pinning */
  1533. }
  1534. else
  1535. if (strcmp(op, "mute") == 0) { /** **/
  1536. /* Mastodon's mute is snac's hide */
  1537. }
  1538. else
  1539. if (strcmp(op, "unmute") == 0) { /** **/
  1540. /* Mastodon's unmute is snac's unhide */
  1541. }
  1542. }
  1543. if (out != NULL) {
  1544. *body = xs_json_dumps_pp(out, 4);
  1545. *ctype = "application/json";
  1546. status = 200;
  1547. }
  1548. }
  1549. }
  1550. else
  1551. status = 401;
  1552. }
  1553. else
  1554. if (strcmp(cmd, "/v1/notifications/clear") == 0) { /** **/
  1555. if (logged_in) {
  1556. notify_clear(&snac);
  1557. timeline_touch(&snac);
  1558. *body = xs_dup("{}");
  1559. *ctype = "application/json";
  1560. status = 200;
  1561. }
  1562. else
  1563. status = 401;
  1564. }
  1565. else
  1566. if (strcmp(cmd, "/v1/push/subscription") == 0) { /** **/
  1567. /* I don't know what I'm doing */
  1568. if (logged_in) {
  1569. char *v;
  1570. xs *wpush = xs_dict_new();
  1571. wpush = xs_dict_append(wpush, "id", "1");
  1572. v = xs_dict_get(args, "data");
  1573. v = xs_dict_get(v, "alerts");
  1574. wpush = xs_dict_append(wpush, "alerts", v);
  1575. v = xs_dict_get(args, "subscription");
  1576. v = xs_dict_get(v, "endpoint");
  1577. wpush = xs_dict_append(wpush, "endpoint", v);
  1578. xs *server_key = random_str();
  1579. wpush = xs_dict_append(wpush, "server_key", server_key);
  1580. *body = xs_json_dumps_pp(wpush, 4);
  1581. *ctype = "application/json";
  1582. status = 200;
  1583. }
  1584. else
  1585. status = 401;
  1586. }
  1587. else
  1588. if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { /** **/
  1589. if (logged_in) {
  1590. /* {
  1591. xs *j = xs_json_dumps_pp(args, 4);
  1592. printf("%s\n", j);
  1593. }*/
  1594. const xs_list *file = xs_dict_get(args, "file");
  1595. const char *desc = xs_dict_get(args, "description");
  1596. if (xs_is_null(desc))
  1597. desc = "";
  1598. status = 400;
  1599. if (xs_type(file) == XSTYPE_LIST) {
  1600. const char *fn = xs_list_get(file, 0);
  1601. if (*fn != '\0') {
  1602. char *ext = strrchr(fn, '.');
  1603. xs *hash = xs_md5_hex(fn, strlen(fn));
  1604. xs *id = xs_fmt("%s%s", hash, ext);
  1605. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1606. int fo = xs_number_get(xs_list_get(file, 1));
  1607. int fs = xs_number_get(xs_list_get(file, 2));
  1608. /* store */
  1609. static_put(&snac, id, payload + fo, fs);
  1610. static_put_meta(&snac, id, desc);
  1611. /* prepare a response */
  1612. xs *rsp = xs_dict_new();
  1613. rsp = xs_dict_append(rsp, "id", id);
  1614. rsp = xs_dict_append(rsp, "type", "image");
  1615. rsp = xs_dict_append(rsp, "url", url);
  1616. rsp = xs_dict_append(rsp, "preview_url", url);
  1617. rsp = xs_dict_append(rsp, "remote_url", url);
  1618. rsp = xs_dict_append(rsp, "description", desc);
  1619. *body = xs_json_dumps_pp(rsp, 4);
  1620. *ctype = "application/json";
  1621. status = 200;
  1622. }
  1623. }
  1624. }
  1625. else
  1626. status = 401;
  1627. }
  1628. else
  1629. if (xs_startswith(cmd, "/v1/accounts")) { /** **/
  1630. if (logged_in) {
  1631. /* account-related information */
  1632. xs *l = xs_split(cmd, "/");
  1633. const char *md5 = xs_list_get(l, 3);
  1634. const char *opt = xs_list_get(l, 4);
  1635. xs *rsp = NULL;
  1636. if (!xs_is_null(md5) && *md5) {
  1637. xs *actor_o = NULL;
  1638. if (xs_is_null(opt)) {
  1639. /* ? */
  1640. }
  1641. else
  1642. if (strcmp(opt, "follow") == 0) { /** **/
  1643. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1644. const char *actor = xs_dict_get(actor_o, "id");
  1645. xs *msg = msg_follow(&snac, actor);
  1646. if (msg != NULL) {
  1647. /* reload the actor from the message, in may be different */
  1648. actor = xs_dict_get(msg, "object");
  1649. following_add(&snac, actor, msg);
  1650. enqueue_output_by_actor(&snac, msg, actor, 0);
  1651. rsp = mastoapi_relationship(&snac, md5);
  1652. }
  1653. }
  1654. }
  1655. else
  1656. if (strcmp(opt, "unfollow") == 0) { /** **/
  1657. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1658. const char *actor = xs_dict_get(actor_o, "id");
  1659. /* get the following object */
  1660. xs *object = NULL;
  1661. if (valid_status(following_get(&snac, actor, &object))) {
  1662. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  1663. following_del(&snac, actor);
  1664. enqueue_output_by_actor(&snac, msg, actor, 0);
  1665. rsp = mastoapi_relationship(&snac, md5);
  1666. }
  1667. }
  1668. }
  1669. else
  1670. if (strcmp(opt, "block") == 0) { /** **/
  1671. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1672. const char *actor = xs_dict_get(actor_o, "id");
  1673. mute(&snac, actor);
  1674. rsp = mastoapi_relationship(&snac, md5);
  1675. }
  1676. }
  1677. else
  1678. if (strcmp(opt, "unblock") == 0) { /** **/
  1679. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1680. const char *actor = xs_dict_get(actor_o, "id");
  1681. unmute(&snac, actor);
  1682. rsp = mastoapi_relationship(&snac, md5);
  1683. }
  1684. }
  1685. }
  1686. if (rsp != NULL) {
  1687. *body = xs_json_dumps_pp(rsp, 4);
  1688. *ctype = "application/json";
  1689. status = 200;
  1690. }
  1691. }
  1692. else
  1693. status = 401;
  1694. }
  1695. /* user cleanup */
  1696. if (logged_in)
  1697. user_free(&snac);
  1698. return status;
  1699. }
  1700. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  1701. const char *payload, int p_size,
  1702. char **body, int *b_size, char **ctype)
  1703. {
  1704. (void)p_size;
  1705. (void)b_size;
  1706. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1707. return 0;
  1708. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1709. /* {
  1710. xs *j = xs_json_dumps_pp(req, 4);
  1711. printf("mastoapi put:\n%s\n", j);
  1712. }*/
  1713. int status = 404;
  1714. xs *args = NULL;
  1715. char *i_ctype = xs_dict_get(req, "content-type");
  1716. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1717. args = xs_json_loads(payload);
  1718. else
  1719. args = xs_dup(xs_dict_get(req, "p_vars"));
  1720. if (args == NULL)
  1721. return 400;
  1722. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1723. snac snac = {0};
  1724. int logged_in = process_auth_token(&snac, req);
  1725. if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) { /** **/
  1726. if (logged_in) {
  1727. xs *l = xs_split(cmd, "/");
  1728. const char *stid = xs_list_get(l, 3);
  1729. if (!xs_is_null(stid)) {
  1730. const char *desc = xs_dict_get(args, "description");
  1731. /* set the image metadata */
  1732. static_put_meta(&snac, stid, desc);
  1733. /* prepare a response */
  1734. xs *rsp = xs_dict_new();
  1735. xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
  1736. rsp = xs_dict_append(rsp, "id", stid);
  1737. rsp = xs_dict_append(rsp, "type", "image");
  1738. rsp = xs_dict_append(rsp, "url", url);
  1739. rsp = xs_dict_append(rsp, "preview_url", url);
  1740. rsp = xs_dict_append(rsp, "remote_url", url);
  1741. rsp = xs_dict_append(rsp, "description", desc);
  1742. *body = xs_json_dumps_pp(rsp, 4);
  1743. *ctype = "application/json";
  1744. status = 200;
  1745. }
  1746. }
  1747. else
  1748. status = 401;
  1749. }
  1750. /* user cleanup */
  1751. if (logged_in)
  1752. user_free(&snac);
  1753. return status;
  1754. }
  1755. void mastoapi_purge(void)
  1756. {
  1757. xs *spec = xs_fmt("%s/app/" "*.json", srv_basedir);
  1758. xs *files = xs_glob(spec, 1, 0);
  1759. xs_list *p = files;
  1760. xs_str *v;
  1761. time_t mt = time(NULL) - 3600;
  1762. while (xs_list_iter(&p, &v)) {
  1763. xs *cid = xs_replace(v, ".json", "");
  1764. xs *fn = _app_fn(cid);
  1765. if (mtime(fn) < mt) {
  1766. /* get the app */
  1767. xs *app = app_get(cid);
  1768. if (app) {
  1769. /* old apps with no uid are incomplete cruft */
  1770. const char *uid = xs_dict_get(app, "uid");
  1771. if (xs_is_null(uid) || *uid == '\0') {
  1772. unlink(fn);
  1773. srv_debug(2, xs_fmt("purged %s", fn));
  1774. }
  1775. }
  1776. }
  1777. }
  1778. }
  1779. #endif /* #ifndef NO_MASTODON_API */