mastoapi.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298
  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 "xs_random.h"
  12. #include "snac.h"
  13. static xs_str *random_str(void)
  14. /* just what is says in the tin */
  15. {
  16. unsigned int data[4] = {0};
  17. xs_rnd_buf(data, sizeof(data));
  18. return xs_hex_enc((char *)data, sizeof(data));
  19. }
  20. int app_add(const char *id, const xs_dict *app)
  21. /* stores an app */
  22. {
  23. if (!xs_is_hex(id))
  24. return 500;
  25. int status = 201;
  26. xs *fn = xs_fmt("%s/app/", srv_basedir);
  27. FILE *f;
  28. mkdirx(fn);
  29. fn = xs_str_cat(fn, id);
  30. fn = xs_str_cat(fn, ".json");
  31. if ((f = fopen(fn, "w")) != NULL) {
  32. xs *j = xs_json_dumps_pp(app, 4);
  33. fwrite(j, strlen(j), 1, f);
  34. fclose(f);
  35. }
  36. else
  37. status = 500;
  38. return status;
  39. }
  40. xs_str *_app_fn(const char *id)
  41. {
  42. return xs_fmt("%s/app/%s.json", srv_basedir, id);
  43. }
  44. xs_dict *app_get(const char *id)
  45. /* gets an app */
  46. {
  47. if (!xs_is_hex(id))
  48. return NULL;
  49. xs *fn = _app_fn(id);
  50. xs_dict *app = NULL;
  51. FILE *f;
  52. if ((f = fopen(fn, "r")) != NULL) {
  53. xs *j = xs_readall(f);
  54. fclose(f);
  55. app = xs_json_loads(j);
  56. }
  57. return app;
  58. }
  59. int app_del(const char *id)
  60. /* deletes an app */
  61. {
  62. if (!xs_is_hex(id))
  63. return -1;
  64. xs *fn = _app_fn(id);
  65. return unlink(fn);
  66. }
  67. int token_add(const char *id, const xs_dict *token)
  68. /* stores a token */
  69. {
  70. if (!xs_is_hex(id))
  71. return 500;
  72. int status = 201;
  73. xs *fn = xs_fmt("%s/token/", srv_basedir);
  74. FILE *f;
  75. mkdirx(fn);
  76. fn = xs_str_cat(fn, id);
  77. fn = xs_str_cat(fn, ".json");
  78. if ((f = fopen(fn, "w")) != NULL) {
  79. xs *j = xs_json_dumps_pp(token, 4);
  80. fwrite(j, strlen(j), 1, f);
  81. fclose(f);
  82. }
  83. else
  84. status = 500;
  85. return status;
  86. }
  87. xs_dict *token_get(const char *id)
  88. /* gets a token */
  89. {
  90. if (!xs_is_hex(id))
  91. return NULL;
  92. xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
  93. xs_dict *token = NULL;
  94. FILE *f;
  95. if ((f = fopen(fn, "r")) != NULL) {
  96. xs *j = xs_readall(f);
  97. fclose(f);
  98. token = xs_json_loads(j);
  99. }
  100. return token;
  101. }
  102. int token_del(const char *id)
  103. /* deletes a token */
  104. {
  105. if (!xs_is_hex(id))
  106. return -1;
  107. xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
  108. return unlink(fn);
  109. }
  110. const char *login_page = ""
  111. "<!DOCTYPE html>\n"
  112. "<body><h1>%s OAuth identify</h1>\n"
  113. "<div style=\"background-color: red; color: white\">%s</div>\n"
  114. "<form method=\"post\" action=\"https:/" "/%s/%s\">\n"
  115. "<p>Login: <input type=\"text\" name=\"login\"></p>\n"
  116. "<p>Password: <input type=\"password\" name=\"passwd\"></p>\n"
  117. "<input type=\"hidden\" name=\"redir\" value=\"%s\">\n"
  118. "<input type=\"hidden\" name=\"cid\" value=\"%s\">\n"
  119. "<input type=\"hidden\" name=\"state\" value=\"%s\">\n"
  120. "<input type=\"submit\" value=\"OK\">\n"
  121. "</form><p>%s</p></body>\n"
  122. "";
  123. int oauth_get_handler(const xs_dict *req, const char *q_path,
  124. char **body, int *b_size, char **ctype)
  125. {
  126. (void)b_size;
  127. if (!xs_startswith(q_path, "/oauth/"))
  128. return 0;
  129. /* {
  130. xs *j = xs_json_dumps_pp(req, 4);
  131. printf("oauth get:\n%s\n", j);
  132. }*/
  133. int status = 404;
  134. xs_dict *msg = xs_dict_get(req, "q_vars");
  135. xs *cmd = xs_replace_n(q_path, "/oauth", "", 1);
  136. srv_debug(1, xs_fmt("oauth_get_handler %s", q_path));
  137. if (strcmp(cmd, "/authorize") == 0) { /** **/
  138. const char *cid = xs_dict_get(msg, "client_id");
  139. const char *ruri = xs_dict_get(msg, "redirect_uri");
  140. const char *rtype = xs_dict_get(msg, "response_type");
  141. const char *state = xs_dict_get(msg, "state");
  142. status = 400;
  143. if (cid && ruri && rtype && strcmp(rtype, "code") == 0) {
  144. xs *app = app_get(cid);
  145. if (app != NULL) {
  146. const char *host = xs_dict_get(srv_config, "host");
  147. if (xs_is_null(state))
  148. state = "";
  149. *body = xs_fmt(login_page, host, "", host, "oauth/x-snac-login",
  150. ruri, cid, state, USER_AGENT);
  151. *ctype = "text/html";
  152. status = 200;
  153. srv_debug(1, xs_fmt("oauth authorize: generating login page"));
  154. }
  155. else
  156. srv_debug(1, xs_fmt("oauth authorize: bad client_id %s", cid));
  157. }
  158. else
  159. srv_debug(1, xs_fmt("oauth authorize: invalid or unset arguments"));
  160. }
  161. else
  162. if (strcmp(cmd, "/x-snac-get-token") == 0) { /** **/
  163. const char *host = xs_dict_get(srv_config, "host");
  164. *body = xs_fmt(login_page, host, "", host, "oauth/x-snac-get-token",
  165. "", "", "", USER_AGENT);
  166. *ctype = "text/html";
  167. status = 200;
  168. }
  169. return status;
  170. }
  171. int oauth_post_handler(const xs_dict *req, const char *q_path,
  172. const char *payload, int p_size,
  173. char **body, int *b_size, char **ctype)
  174. {
  175. (void)p_size;
  176. (void)b_size;
  177. if (!xs_startswith(q_path, "/oauth/"))
  178. return 0;
  179. /* {
  180. xs *j = xs_json_dumps_pp(req, 4);
  181. printf("oauth post:\n%s\n", j);
  182. }*/
  183. int status = 404;
  184. char *i_ctype = xs_dict_get(req, "content-type");
  185. xs *args = NULL;
  186. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  187. args = xs_json_loads(payload);
  188. else
  189. args = xs_dup(xs_dict_get(req, "p_vars"));
  190. xs *cmd = xs_replace_n(q_path, "/oauth", "", 1);
  191. srv_debug(1, xs_fmt("oauth_post_handler %s", q_path));
  192. if (strcmp(cmd, "/x-snac-login") == 0) { /** **/
  193. const char *login = xs_dict_get(args, "login");
  194. const char *passwd = xs_dict_get(args, "passwd");
  195. const char *redir = xs_dict_get(args, "redir");
  196. const char *cid = xs_dict_get(args, "cid");
  197. const char *state = xs_dict_get(args, "state");
  198. const char *host = xs_dict_get(srv_config, "host");
  199. /* by default, generate another login form with an error */
  200. *body = xs_fmt(login_page, host, "LOGIN INCORRECT", host, "oauth/x-snac-login",
  201. redir, cid, state, USER_AGENT);
  202. *ctype = "text/html";
  203. status = 200;
  204. if (login && passwd && redir && cid) {
  205. snac snac;
  206. if (user_open(&snac, login)) {
  207. /* check the login + password */
  208. if (check_password(login, passwd, xs_dict_get(snac.config, "passwd"))) {
  209. /* success! redirect to the desired uri */
  210. xs *code = random_str();
  211. xs_free(*body);
  212. *body = xs_fmt("%s?code=%s", redir, code);
  213. status = 303;
  214. /* if there is a state, add it */
  215. if (!xs_is_null(state) && *state) {
  216. *body = xs_str_cat(*body, "&state=");
  217. *body = xs_str_cat(*body, state);
  218. }
  219. srv_log(xs_fmt("oauth x-snac-login: '%s' success, redirect to %s",
  220. login, *body));
  221. /* assign the login to the app */
  222. xs *app = app_get(cid);
  223. if (app != NULL) {
  224. app = xs_dict_set(app, "uid", login);
  225. app = xs_dict_set(app, "code", code);
  226. app_add(cid, app);
  227. }
  228. else
  229. srv_log(xs_fmt("oauth x-snac-login: error getting app %s", cid));
  230. }
  231. else
  232. srv_debug(1, xs_fmt("oauth x-snac-login: login '%s' incorrect", login));
  233. user_free(&snac);
  234. }
  235. else
  236. srv_debug(1, xs_fmt("oauth x-snac-login: bad user '%s'", login));
  237. }
  238. else
  239. srv_debug(1, xs_fmt("oauth x-snac-login: invalid or unset arguments"));
  240. }
  241. else
  242. if (strcmp(cmd, "/token") == 0) { /** **/
  243. xs *wrk = NULL;
  244. const char *gtype = xs_dict_get(args, "grant_type");
  245. const char *code = xs_dict_get(args, "code");
  246. const char *cid = xs_dict_get(args, "client_id");
  247. const char *csec = xs_dict_get(args, "client_secret");
  248. const char *ruri = xs_dict_get(args, "redirect_uri");
  249. /* FIXME: this 'scope' parameter is mandatory for the official Mastodon API,
  250. but if it's enabled, it makes it crash after some more steps, which
  251. is FAR WORSE */
  252. // const char *scope = xs_dict_get(args, "scope");
  253. const char *scope = NULL;
  254. /* no client_secret? check if it's inside an authorization header
  255. (AndStatus does it this way) */
  256. if (xs_is_null(csec)) {
  257. const char *auhdr = xs_dict_get(req, "authorization");
  258. if (!xs_is_null(auhdr) && xs_startswith(auhdr, "Basic ")) {
  259. xs *s1 = xs_replace_n(auhdr, "Basic ", "", 1);
  260. int size;
  261. xs *s2 = xs_base64_dec(s1, &size);
  262. if (!xs_is_null(s2)) {
  263. xs *l1 = xs_split(s2, ":");
  264. if (xs_list_len(l1) == 2) {
  265. wrk = xs_dup(xs_list_get(l1, 1));
  266. csec = wrk;
  267. }
  268. }
  269. }
  270. }
  271. if (gtype && code && cid && csec && ruri) {
  272. xs *app = app_get(cid);
  273. if (app == NULL) {
  274. status = 401;
  275. srv_log(xs_fmt("oauth token: invalid app %s", cid));
  276. }
  277. else
  278. if (strcmp(csec, xs_dict_get(app, "client_secret")) != 0) {
  279. status = 401;
  280. srv_log(xs_fmt("oauth token: invalid client_secret for app %s", cid));
  281. }
  282. else {
  283. xs *rsp = xs_dict_new();
  284. xs *cat = xs_number_new(time(NULL));
  285. xs *tokid = random_str();
  286. rsp = xs_dict_append(rsp, "access_token", tokid);
  287. rsp = xs_dict_append(rsp, "token_type", "Bearer");
  288. rsp = xs_dict_append(rsp, "created_at", cat);
  289. if (!xs_is_null(scope))
  290. rsp = xs_dict_append(rsp, "scope", scope);
  291. *body = xs_json_dumps_pp(rsp, 4);
  292. *ctype = "application/json";
  293. status = 200;
  294. const char *uid = xs_dict_get(app, "uid");
  295. srv_debug(1, xs_fmt("oauth token: "
  296. "successful login for %s, new token %s", uid, tokid));
  297. xs *token = xs_dict_new();
  298. token = xs_dict_append(token, "token", tokid);
  299. token = xs_dict_append(token, "client_id", cid);
  300. token = xs_dict_append(token, "client_secret", csec);
  301. token = xs_dict_append(token, "uid", uid);
  302. token = xs_dict_append(token, "code", code);
  303. token_add(tokid, token);
  304. }
  305. }
  306. else {
  307. srv_debug(1, xs_fmt("oauth token: invalid or unset arguments"));
  308. status = 400;
  309. }
  310. }
  311. else
  312. if (strcmp(cmd, "/revoke") == 0) { /** **/
  313. const char *cid = xs_dict_get(args, "client_id");
  314. const char *csec = xs_dict_get(args, "client_secret");
  315. const char *tokid = xs_dict_get(args, "token");
  316. if (cid && csec && tokid) {
  317. xs *token = token_get(tokid);
  318. *body = xs_str_new("{}");
  319. *ctype = "application/json";
  320. if (token == NULL || strcmp(csec, xs_dict_get(token, "client_secret")) != 0) {
  321. srv_debug(1, xs_fmt("oauth revoke: bad secret for token %s", tokid));
  322. status = 403;
  323. }
  324. else {
  325. token_del(tokid);
  326. srv_debug(1, xs_fmt("oauth revoke: revoked token %s", tokid));
  327. status = 200;
  328. /* also delete the app, as it serves no purpose from now on */
  329. app_del(cid);
  330. }
  331. }
  332. else {
  333. srv_debug(1, xs_fmt("oauth revoke: invalid or unset arguments"));
  334. status = 403;
  335. }
  336. }
  337. if (strcmp(cmd, "/x-snac-get-token") == 0) { /** **/
  338. const char *login = xs_dict_get(args, "login");
  339. const char *passwd = xs_dict_get(args, "passwd");
  340. const char *host = xs_dict_get(srv_config, "host");
  341. /* by default, generate another login form with an error */
  342. *body = xs_fmt(login_page, host, "LOGIN INCORRECT", host, "oauth/x-snac-get-token",
  343. "", "", "", USER_AGENT);
  344. *ctype = "text/html";
  345. status = 200;
  346. if (login && passwd) {
  347. snac user;
  348. if (user_open(&user, login)) {
  349. /* check the login + password */
  350. if (check_password(login, passwd, xs_dict_get(user.config, "passwd"))) {
  351. /* success! create a new token */
  352. xs *tokid = random_str();
  353. srv_debug(1, xs_fmt("x-snac-new-token: "
  354. "successful login for %s, new token %s", login, tokid));
  355. xs *token = xs_dict_new();
  356. token = xs_dict_append(token, "token", tokid);
  357. token = xs_dict_append(token, "client_id", "snac-client");
  358. token = xs_dict_append(token, "client_secret", "");
  359. token = xs_dict_append(token, "uid", login);
  360. token = xs_dict_append(token, "code", "");
  361. token_add(tokid, token);
  362. *ctype = "text/plain";
  363. xs_free(*body);
  364. *body = xs_dup(tokid);
  365. }
  366. user_free(&user);
  367. }
  368. }
  369. }
  370. return status;
  371. }
  372. xs_str *mastoapi_id(const xs_dict *msg)
  373. /* returns a somewhat Mastodon-compatible status id */
  374. {
  375. const char *id = xs_dict_get(msg, "id");
  376. xs *md5 = xs_md5_hex(id, strlen(id));
  377. return xs_fmt("%10.0f%s", object_ctime_by_md5(md5), md5);
  378. }
  379. #define MID_TO_MD5(id) (id + 10)
  380. xs_dict *mastoapi_account(const xs_dict *actor)
  381. /* converts an ActivityPub actor to a Mastodon account */
  382. {
  383. xs_dict *acct = xs_dict_new();
  384. const char *prefu = xs_dict_get(actor, "preferredUsername");
  385. const char *display_name = xs_dict_get(actor, "name");
  386. if (xs_is_null(display_name) || *display_name == '\0')
  387. display_name = prefu;
  388. const char *id = xs_dict_get(actor, "id");
  389. const char *pub = xs_dict_get(actor, "published");
  390. xs *acct_md5 = xs_md5_hex(id, strlen(id));
  391. acct = xs_dict_append(acct, "id", acct_md5);
  392. acct = xs_dict_append(acct, "username", prefu);
  393. acct = xs_dict_append(acct, "display_name", display_name);
  394. {
  395. /* create the acct field as user@host */
  396. xs *l = xs_split(id, "/");
  397. xs *fquid = xs_fmt("%s@%s", prefu, xs_list_get(l, 2));
  398. acct = xs_dict_append(acct, "acct", fquid);
  399. }
  400. if (pub)
  401. acct = xs_dict_append(acct, "created_at", pub);
  402. else {
  403. /* unset created_at crashes Tusky, so lie like a mf */
  404. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  405. acct = xs_dict_append(acct, "created_at", date);
  406. }
  407. const char *note = xs_dict_get(actor, "summary");
  408. if (xs_is_null(note))
  409. note = "";
  410. acct = xs_dict_append(acct, "note", note);
  411. acct = xs_dict_append(acct, "url", id);
  412. xs *avatar = NULL;
  413. xs_dict *av = xs_dict_get(actor, "icon");
  414. if (xs_type(av) == XSTYPE_DICT)
  415. avatar = xs_dup(xs_dict_get(av, "url"));
  416. else
  417. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  418. acct = xs_dict_append(acct, "avatar", avatar);
  419. /* emojis */
  420. xs_list *p;
  421. if (!xs_is_null(p = xs_dict_get(actor, "tag"))) {
  422. xs *eml = xs_list_new();
  423. xs_dict *v;
  424. xs *t = xs_val_new(XSTYPE_TRUE);
  425. while (xs_list_iter(&p, &v)) {
  426. const char *type = xs_dict_get(v, "type");
  427. if (!xs_is_null(type) && strcmp(type, "Emoji") == 0) {
  428. const char *name = xs_dict_get(v, "name");
  429. const xs_dict *icon = xs_dict_get(v, "icon");
  430. if (!xs_is_null(name) && !xs_is_null(icon)) {
  431. const char *url = xs_dict_get(icon, "url");
  432. if (!xs_is_null(url)) {
  433. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  434. xs *d1 = xs_dict_new();
  435. d1 = xs_dict_append(d1, "shortcode", nm);
  436. d1 = xs_dict_append(d1, "url", url);
  437. d1 = xs_dict_append(d1, "static_url", url);
  438. d1 = xs_dict_append(d1, "visible_in_picker", t);
  439. eml = xs_list_append(eml, d1);
  440. }
  441. }
  442. }
  443. }
  444. acct = xs_dict_append(acct, "emojis", eml);
  445. }
  446. acct = xs_dict_append(acct, "header", "");
  447. return acct;
  448. }
  449. xs_dict *mastoapi_poll(snac *snac, const xs_dict *msg)
  450. /* creates a mastoapi Poll object */
  451. {
  452. xs_dict *poll = xs_dict_new();
  453. xs *mid = mastoapi_id(msg);
  454. xs *f = xs_val_new(XSTYPE_FALSE);
  455. xs *t = xs_val_new(XSTYPE_TRUE);
  456. xs_list *opts = NULL;
  457. xs_val *v;
  458. int num_votes = 0;
  459. xs *options = xs_list_new();
  460. poll = xs_dict_append(poll, "id", mid);
  461. poll = xs_dict_append(poll, "expires_at", xs_dict_get(msg, "endTime"));
  462. poll = xs_dict_append(poll, "expired", xs_dict_get(msg, "closed") != NULL ? t : f);
  463. if ((opts = xs_dict_get(msg, "oneOf")) != NULL)
  464. poll = xs_dict_append(poll, "multiple", f);
  465. else {
  466. opts = xs_dict_get(msg, "anyOf");
  467. poll = xs_dict_append(poll, "multiple", t);
  468. }
  469. while (xs_list_iter(&opts, &v)) {
  470. const char *title = xs_dict_get(v, "name");
  471. const char *replies = xs_dict_get(v, "replies");
  472. if (title && replies) {
  473. const char *votes_count = xs_dict_get(replies, "totalItems");
  474. if (xs_type(votes_count) == XSTYPE_NUMBER) {
  475. xs *d = xs_dict_new();
  476. d = xs_dict_append(d, "title", title);
  477. d = xs_dict_append(d, "votes_count", votes_count);
  478. options = xs_list_append(options, d);
  479. num_votes += xs_number_get(votes_count);
  480. }
  481. }
  482. }
  483. poll = xs_dict_append(poll, "options", options);
  484. xs *vc = xs_number_new(num_votes);
  485. poll = xs_dict_append(poll, "votes_count", vc);
  486. poll = xs_dict_append(poll, "voted",
  487. was_question_voted(snac, xs_dict_get(msg, "id")) ? t : f);
  488. return poll;
  489. }
  490. xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
  491. /* converts an ActivityPub note to a Mastodon status */
  492. {
  493. xs *actor = NULL;
  494. actor_get(snac, xs_dict_get(msg, "attributedTo"), &actor);
  495. /* if the author is not here, discard */
  496. if (actor == NULL)
  497. return NULL;
  498. const char *type = xs_dict_get(msg, "type");
  499. const char *id = xs_dict_get(msg, "id");
  500. xs *acct = mastoapi_account(actor);
  501. xs *f = xs_val_new(XSTYPE_FALSE);
  502. xs *t = xs_val_new(XSTYPE_TRUE);
  503. xs *n = xs_val_new(XSTYPE_NULL);
  504. xs *idx = NULL;
  505. xs *ixc = NULL;
  506. char *tmp;
  507. xs *mid = mastoapi_id(msg);
  508. xs_dict *st = xs_dict_new();
  509. st = xs_dict_append(st, "id", mid);
  510. st = xs_dict_append(st, "uri", id);
  511. st = xs_dict_append(st, "url", id);
  512. st = xs_dict_append(st, "created_at", xs_dict_get(msg, "published"));
  513. st = xs_dict_append(st, "account", acct);
  514. st = xs_dict_append(st, "content", xs_dict_get(msg, "content"));
  515. st = xs_dict_append(st, "visibility",
  516. is_msg_public(snac, msg) ? "public" : "private");
  517. tmp = xs_dict_get(msg, "sensitive");
  518. if (xs_is_null(tmp))
  519. tmp = f;
  520. st = xs_dict_append(st, "sensitive", tmp);
  521. tmp = xs_dict_get(msg, "summary");
  522. if (xs_is_null(tmp))
  523. tmp = "";
  524. st = xs_dict_append(st, "spoiler_text", tmp);
  525. /* create the list of attachments */
  526. xs *matt = xs_list_new();
  527. xs_list *att = xs_dict_get(msg, "attachment");
  528. xs_str *aobj;
  529. while (xs_list_iter(&att, &aobj)) {
  530. const char *mtype = xs_dict_get(aobj, "mediaType");
  531. if (!xs_is_null(mtype)) {
  532. if (xs_startswith(mtype, "image/") || xs_startswith(mtype, "video/")) {
  533. xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
  534. xs *matte = xs_dict_new();
  535. matte = xs_dict_append(matte, "id", matteid);
  536. matte = xs_dict_append(matte, "type", *mtype == 'i' ? "image" : "video");
  537. matte = xs_dict_append(matte, "url", xs_dict_get(aobj, "url"));
  538. matte = xs_dict_append(matte, "preview_url", xs_dict_get(aobj, "url"));
  539. matte = xs_dict_append(matte, "remote_url", xs_dict_get(aobj, "url"));
  540. const char *name = xs_dict_get(aobj, "name");
  541. if (xs_is_null(name))
  542. name = "";
  543. matte = xs_dict_append(matte, "description", name);
  544. matt = xs_list_append(matt, matte);
  545. }
  546. }
  547. }
  548. st = xs_dict_append(st, "media_attachments", matt);
  549. {
  550. xs *ml = xs_list_new();
  551. xs *htl = xs_list_new();
  552. xs *eml = xs_list_new();
  553. xs_list *p = xs_dict_get(msg, "tag");
  554. xs_dict *v;
  555. int n = 0;
  556. while (xs_list_iter(&p, &v)) {
  557. const char *type = xs_dict_get(v, "type");
  558. if (xs_is_null(type))
  559. continue;
  560. xs *d1 = xs_dict_new();
  561. if (strcmp(type, "Mention") == 0) {
  562. const char *name = xs_dict_get(v, "name");
  563. const char *href = xs_dict_get(v, "href");
  564. if (!xs_is_null(name) && !xs_is_null(href) &&
  565. strcmp(href, snac->actor) != 0) {
  566. xs *nm = xs_strip_chars_i(xs_dup(name), "@");
  567. xs *id = xs_fmt("%d", n++);
  568. d1 = xs_dict_append(d1, "id", id);
  569. d1 = xs_dict_append(d1, "username", nm);
  570. d1 = xs_dict_append(d1, "acct", nm);
  571. d1 = xs_dict_append(d1, "url", href);
  572. ml = xs_list_append(ml, d1);
  573. }
  574. }
  575. else
  576. if (strcmp(type, "Hashtag") == 0) {
  577. const char *name = xs_dict_get(v, "name");
  578. const char *href = xs_dict_get(v, "href");
  579. if (!xs_is_null(name) && !xs_is_null(href)) {
  580. xs *nm = xs_strip_chars_i(xs_dup(name), "#");
  581. d1 = xs_dict_append(d1, "name", nm);
  582. d1 = xs_dict_append(d1, "url", href);
  583. htl = xs_list_append(htl, d1);
  584. }
  585. }
  586. else
  587. if (strcmp(type, "Emoji") == 0) {
  588. const char *name = xs_dict_get(v, "name");
  589. const xs_dict *icon = xs_dict_get(v, "icon");
  590. if (!xs_is_null(name) && !xs_is_null(icon)) {
  591. const char *url = xs_dict_get(icon, "url");
  592. if (!xs_is_null(url)) {
  593. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  594. xs *t = xs_val_new(XSTYPE_TRUE);
  595. d1 = xs_dict_append(d1, "shortcode", nm);
  596. d1 = xs_dict_append(d1, "url", url);
  597. d1 = xs_dict_append(d1, "static_url", url);
  598. d1 = xs_dict_append(d1, "visible_in_picker", t);
  599. d1 = xs_dict_append(d1, "category", "Emojis");
  600. eml = xs_list_append(eml, d1);
  601. }
  602. }
  603. }
  604. }
  605. st = xs_dict_append(st, "mentions", ml);
  606. st = xs_dict_append(st, "tags", htl);
  607. st = xs_dict_append(st, "emojis", eml);
  608. }
  609. xs_free(idx);
  610. xs_free(ixc);
  611. idx = object_likes(id);
  612. ixc = xs_number_new(xs_list_len(idx));
  613. st = xs_dict_append(st, "favourites_count", ixc);
  614. st = xs_dict_append(st, "favourited",
  615. xs_list_in(idx, snac->md5) != -1 ? t : f);
  616. xs_free(idx);
  617. xs_free(ixc);
  618. idx = object_announces(id);
  619. ixc = xs_number_new(xs_list_len(idx));
  620. st = xs_dict_append(st, "reblogs_count", ixc);
  621. st = xs_dict_append(st, "reblogged",
  622. xs_list_in(idx, snac->md5) != -1 ? t : f);
  623. xs_free(idx);
  624. xs_free(ixc);
  625. idx = object_children(id);
  626. ixc = xs_number_new(xs_list_len(idx));
  627. st = xs_dict_append(st, "replies_count", ixc);
  628. /* default in_reply_to values */
  629. st = xs_dict_append(st, "in_reply_to_id", n);
  630. st = xs_dict_append(st, "in_reply_to_account_id", n);
  631. tmp = xs_dict_get(msg, "inReplyTo");
  632. if (!xs_is_null(tmp)) {
  633. xs *irto = NULL;
  634. if (valid_status(object_get(tmp, &irto))) {
  635. xs *irt_mid = mastoapi_id(irto);
  636. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  637. char *at = NULL;
  638. if (!xs_is_null(at = xs_dict_get(irto, "attributedTo"))) {
  639. xs *at_md5 = xs_md5_hex(at, strlen(at));
  640. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  641. }
  642. }
  643. }
  644. st = xs_dict_append(st, "reblog", n);
  645. st = xs_dict_append(st, "poll", n);
  646. st = xs_dict_append(st, "card", n);
  647. st = xs_dict_append(st, "language", n);
  648. tmp = xs_dict_get(msg, "sourceContent");
  649. if (xs_is_null(tmp))
  650. tmp = "";
  651. st = xs_dict_append(st, "text", tmp);
  652. tmp = xs_dict_get(msg, "updated");
  653. if (xs_is_null(tmp))
  654. tmp = n;
  655. st = xs_dict_append(st, "edited_at", tmp);
  656. if (strcmp(type, "Question") == 0) {
  657. xs *poll = mastoapi_poll(snac, msg);
  658. st = xs_dict_append(st, "poll", poll);
  659. }
  660. return st;
  661. }
  662. xs_dict *mastoapi_relationship(snac *snac, const char *md5)
  663. {
  664. xs_dict *rel = NULL;
  665. xs *actor_o = NULL;
  666. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  667. xs *t = xs_val_new(XSTYPE_TRUE);
  668. xs *f = xs_val_new(XSTYPE_FALSE);
  669. rel = xs_dict_new();
  670. const char *actor = xs_dict_get(actor_o, "id");
  671. rel = xs_dict_append(rel, "id", md5);
  672. rel = xs_dict_append(rel, "following",
  673. following_check(snac, actor) ? t : f);
  674. rel = xs_dict_append(rel, "showing_reblogs", t);
  675. rel = xs_dict_append(rel, "notifying", f);
  676. rel = xs_dict_append(rel, "followed_by",
  677. follower_check(snac, actor) ? t : f);
  678. rel = xs_dict_append(rel, "blocking",
  679. is_muted(snac, actor) ? t : f);
  680. rel = xs_dict_append(rel, "muting", f);
  681. rel = xs_dict_append(rel, "muting_notifications", f);
  682. rel = xs_dict_append(rel, "requested", f);
  683. rel = xs_dict_append(rel, "domain_blocking", f);
  684. rel = xs_dict_append(rel, "endorsed", f);
  685. rel = xs_dict_append(rel, "note", "");
  686. }
  687. return rel;
  688. }
  689. int process_auth_token(snac *snac, const xs_dict *req)
  690. /* processes an authorization token, if there is one */
  691. {
  692. int logged_in = 0;
  693. char *v;
  694. /* if there is an authorization field, try to validate it */
  695. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  696. xs *tokid = xs_replace_n(v, "Bearer ", "", 1);
  697. xs *token = token_get(tokid);
  698. if (token != NULL) {
  699. const char *uid = xs_dict_get(token, "uid");
  700. if (!xs_is_null(uid) && user_open(snac, uid)) {
  701. logged_in = 1;
  702. /* this counts as a 'login' */
  703. lastlog_write(snac, "mastoapi");
  704. srv_debug(2, xs_fmt("mastoapi auth: valid token for user %s", uid));
  705. }
  706. else
  707. srv_log(xs_fmt("mastoapi auth: corrupted token %s", tokid));
  708. }
  709. else
  710. srv_log(xs_fmt("mastoapi auth: invalid token %s", tokid));
  711. }
  712. return logged_in;
  713. }
  714. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  715. char **body, int *b_size, char **ctype)
  716. {
  717. (void)b_size;
  718. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  719. return 0;
  720. srv_debug(1, xs_fmt("mastoapi_get_handler %s", q_path));
  721. /* {
  722. xs *j = xs_json_dumps_pp(req, 4);
  723. printf("mastoapi get:\n%s\n", j);
  724. }*/
  725. int status = 404;
  726. xs_dict *args = xs_dict_get(req, "q_vars");
  727. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  728. snac snac1 = {0};
  729. int logged_in = process_auth_token(&snac1, req);
  730. if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) { /** **/
  731. if (logged_in) {
  732. xs *acct = xs_dict_new();
  733. acct = xs_dict_append(acct, "id", xs_dict_get(snac1.config, "uid"));
  734. acct = xs_dict_append(acct, "username", xs_dict_get(snac1.config, "uid"));
  735. acct = xs_dict_append(acct, "acct", xs_dict_get(snac1.config, "uid"));
  736. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac1.config, "name"));
  737. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac1.config, "published"));
  738. acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio"));
  739. acct = xs_dict_append(acct, "url", snac1.actor);
  740. acct = xs_dict_append(acct, "header", "");
  741. xs *avatar = NULL;
  742. char *av = xs_dict_get(snac1.config, "avatar");
  743. if (xs_is_null(av) || *av == '\0')
  744. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  745. else
  746. avatar = xs_dup(av);
  747. acct = xs_dict_append(acct, "avatar", avatar);
  748. *body = xs_json_dumps_pp(acct, 4);
  749. *ctype = "application/json";
  750. status = 200;
  751. }
  752. else {
  753. status = 422; // "Unprocessable entity" (no login)
  754. }
  755. }
  756. else
  757. if (strcmp(cmd, "/v1/accounts/relationships") == 0) { /** **/
  758. /* find if an account is followed, blocked, etc. */
  759. /* the account to get relationships about is in args "id[]" */
  760. if (logged_in) {
  761. xs *res = xs_list_new();
  762. const char *md5 = xs_dict_get(args, "id[]");
  763. if (!xs_is_null(md5)) {
  764. if (xs_type(md5) == XSTYPE_LIST)
  765. md5 = xs_list_get(md5, 0);
  766. xs *rel = mastoapi_relationship(&snac1, md5);
  767. if (rel != NULL)
  768. res = xs_list_append(res, rel);
  769. }
  770. *body = xs_json_dumps_pp(res, 4);
  771. *ctype = "application/json";
  772. status = 200;
  773. }
  774. else
  775. status = 422;
  776. }
  777. else
  778. if (xs_startswith(cmd, "/v1/accounts/")) { /** **/
  779. /* account-related information */
  780. xs *l = xs_split(cmd, "/");
  781. const char *uid = xs_list_get(l, 3);
  782. const char *opt = xs_list_get(l, 4);
  783. if (uid != NULL) {
  784. snac snac2;
  785. xs *out = NULL;
  786. xs *actor = NULL;
  787. if (logged_in && strcmp(uid, "search") == 0) { /** **/
  788. /* search for accounts starting with q */
  789. const char *aq = xs_dict_get(args, "q");
  790. if (!xs_is_null(aq)) {
  791. xs *q = xs_tolower_i(xs_dup(aq));
  792. out = xs_list_new();
  793. xs *wing = following_list(&snac1);
  794. xs *wers = follower_list(&snac1);
  795. xs *ulst = user_list();
  796. xs_list *p;
  797. xs_str *v;
  798. xs_set seen;
  799. xs_set_init(&seen);
  800. /* user relations */
  801. xs_list *lsts[] = { wing, wers, NULL };
  802. int n;
  803. for (n = 0; (p = lsts[n]) != NULL; n++) {
  804. while (xs_list_iter(&p, &v)) {
  805. /* already seen? skip */
  806. if (xs_set_add(&seen, v) == 0)
  807. continue;
  808. xs *actor = NULL;
  809. if (valid_status(object_get(v, &actor))) {
  810. const char *uname = xs_dict_get(actor, "preferredUsername");
  811. if (!xs_is_null(uname)) {
  812. xs *luname = xs_tolower_i(xs_dup(uname));
  813. if (xs_startswith(luname, q)) {
  814. xs *acct = mastoapi_account(actor);
  815. out = xs_list_append(out, acct);
  816. }
  817. }
  818. }
  819. }
  820. }
  821. /* local users */
  822. p = ulst;
  823. while (xs_list_iter(&p, &v)) {
  824. snac user;
  825. /* skip this same user */
  826. if (strcmp(v, xs_dict_get(snac1.config, "uid")) == 0)
  827. continue;
  828. /* skip if the uid does not start with the query */
  829. xs *v2 = xs_tolower_i(xs_dup(v));
  830. if (!xs_startswith(v2, q))
  831. continue;
  832. if (user_open(&user, v)) {
  833. /* if it's not already seen, add it */
  834. if (xs_set_add(&seen, user.actor) == 1) {
  835. xs *actor = msg_actor(&user);
  836. xs *acct = mastoapi_account(actor);
  837. out = xs_list_append(out, acct);
  838. }
  839. user_free(&user);
  840. }
  841. }
  842. xs_set_free(&seen);
  843. }
  844. }
  845. else
  846. /* is it a local user? */
  847. if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
  848. if (opt == NULL) {
  849. /* account information */
  850. actor = msg_actor(&snac2);
  851. out = mastoapi_account(actor);
  852. }
  853. else
  854. if (strcmp(opt, "statuses") == 0) { /** **/
  855. /* the public list of posts of a user */
  856. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  857. xs_list *p = timeline;
  858. xs_str *v;
  859. out = xs_list_new();
  860. while (xs_list_iter(&p, &v)) {
  861. xs *msg = NULL;
  862. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  863. /* add only posts by the author */
  864. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  865. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  866. xs *st = mastoapi_status(&snac2, msg);
  867. out = xs_list_append(out, st);
  868. }
  869. }
  870. }
  871. }
  872. user_free(&snac2);
  873. }
  874. else {
  875. /* try the uid as the md5 of a possibly loaded actor */
  876. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  877. if (opt == NULL) {
  878. /* account information */
  879. out = mastoapi_account(actor);
  880. }
  881. else
  882. if (strcmp(opt, "statuses") == 0) {
  883. /* we don't serve statuses of others; return the empty list */
  884. out = xs_list_new();
  885. }
  886. }
  887. }
  888. if (out != NULL) {
  889. *body = xs_json_dumps_pp(out, 4);
  890. *ctype = "application/json";
  891. status = 200;
  892. }
  893. }
  894. }
  895. else
  896. if (strcmp(cmd, "/v1/timelines/home") == 0) { /** **/
  897. /* the private timeline */
  898. if (logged_in) {
  899. const char *max_id = xs_dict_get(args, "max_id");
  900. const char *since_id = xs_dict_get(args, "since_id");
  901. const char *min_id = xs_dict_get(args, "min_id");
  902. const char *limit_s = xs_dict_get(args, "limit");
  903. int limit = 0;
  904. int cnt = 0;
  905. if (!xs_is_null(limit_s))
  906. limit = atoi(limit_s);
  907. if (limit == 0)
  908. limit = 20;
  909. xs *timeline = timeline_simple_list(&snac1, "private", 0, 256);
  910. xs *out = xs_list_new();
  911. xs_list *p = timeline;
  912. xs_str *v;
  913. while (xs_list_iter(&p, &v) && cnt < limit) {
  914. xs *msg = NULL;
  915. /* only return entries older that max_id */
  916. if (max_id) {
  917. if (strcmp(v, MID_TO_MD5(max_id)) == 0)
  918. max_id = NULL;
  919. continue;
  920. }
  921. /* only returns entries newer than since_id */
  922. if (since_id) {
  923. if (strcmp(v, MID_TO_MD5(since_id)) == 0)
  924. break;
  925. }
  926. /* only returns entries newer than min_id */
  927. /* what does really "Return results immediately newer than ID" mean? */
  928. if (min_id) {
  929. if (strcmp(v, MID_TO_MD5(min_id)) == 0)
  930. break;
  931. }
  932. /* get the entry */
  933. if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
  934. continue;
  935. /* discard non-Notes */
  936. const char *type = xs_dict_get(msg, "type");
  937. if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0)
  938. continue;
  939. /* discard notes from people we don't follow with no boosts */
  940. if (!following_check(&snac1, xs_dict_get(msg, "attributedTo")) &&
  941. object_announces_len(xs_dict_get(msg, "id")) == 0)
  942. continue;
  943. /* discard notes from muted morons */
  944. if (is_muted(&snac1, xs_dict_get(msg, "attributedTo")))
  945. continue;
  946. /* discard hidden notes */
  947. if (is_hidden(&snac1, xs_dict_get(msg, "id")))
  948. continue;
  949. /* discard poll votes (they have a name) */
  950. if (!xs_is_null(xs_dict_get(msg, "name")))
  951. continue;
  952. /* convert the Note into a Mastodon status */
  953. xs *st = mastoapi_status(&snac1, msg);
  954. if (st != NULL)
  955. out = xs_list_append(out, st);
  956. cnt++;
  957. }
  958. *body = xs_json_dumps_pp(out, 4);
  959. *ctype = "application/json";
  960. status = 200;
  961. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  962. }
  963. else {
  964. status = 401; // unauthorized
  965. }
  966. }
  967. else
  968. if (strcmp(cmd, "/v1/timelines/public") == 0) { /** **/
  969. /* the instance public timeline (public timelines for all users) */
  970. /* NOTE: this api call needs no authorization; but,
  971. I need a logged-in user in mastoapi_status() for
  972. is_msg_public() and the liked/boosted flags,
  973. so it will silently fail for pure public access */
  974. const char *limit_s = xs_dict_get(args, "limit");
  975. int limit = 0;
  976. int cnt = 0;
  977. if (!xs_is_null(limit_s))
  978. limit = atoi(limit_s);
  979. if (limit == 0)
  980. limit = 20;
  981. xs *timeline = timeline_instance_list(0, limit);
  982. xs *out = xs_list_new();
  983. xs_list *p = timeline;
  984. xs_str *md5;
  985. while (logged_in && xs_list_iter(&p, &md5) && cnt < limit) {
  986. xs *msg = NULL;
  987. /* get the entry */
  988. if (!valid_status(object_get_by_md5(md5, &msg)))
  989. continue;
  990. /* discard non-Notes */
  991. const char *type = xs_dict_get(msg, "type");
  992. if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0)
  993. continue;
  994. /* convert the Note into a Mastodon status */
  995. xs *st = mastoapi_status(&snac1, msg);
  996. if (st != NULL) {
  997. out = xs_list_append(out, st);
  998. cnt++;
  999. }
  1000. }
  1001. *body = xs_json_dumps_pp(out, 4);
  1002. *ctype = "application/json";
  1003. status = 200;
  1004. }
  1005. else
  1006. if (strcmp(cmd, "/v1/conversations") == 0) { /** **/
  1007. /* TBD */
  1008. *body = xs_dup("[]");
  1009. *ctype = "application/json";
  1010. status = 200;
  1011. }
  1012. else
  1013. if (strcmp(cmd, "/v1/notifications") == 0) { /** **/
  1014. if (logged_in) {
  1015. xs *l = notify_list(&snac1, 0);
  1016. xs *out = xs_list_new();
  1017. xs_list *p = l;
  1018. xs_dict *v;
  1019. xs_list *excl = xs_dict_get(args, "exclude_types[]");
  1020. while (xs_list_iter(&p, &v)) {
  1021. xs *noti = notify_get(&snac1, v);
  1022. if (noti == NULL)
  1023. continue;
  1024. const char *type = xs_dict_get(noti, "type");
  1025. const char *utype = xs_dict_get(noti, "utype");
  1026. const char *objid = xs_dict_get(noti, "objid");
  1027. xs *actor = NULL;
  1028. xs *entry = NULL;
  1029. if (!valid_status(actor_get(&snac1, xs_dict_get(noti, "actor"), &actor)))
  1030. continue;
  1031. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  1032. continue;
  1033. if (is_hidden(&snac1, objid))
  1034. continue;
  1035. /* convert the type */
  1036. if (strcmp(type, "Like") == 0)
  1037. type = "favourite";
  1038. else
  1039. if (strcmp(type, "Announce") == 0)
  1040. type = "reblog";
  1041. else
  1042. if (strcmp(type, "Follow") == 0)
  1043. type = "follow";
  1044. else
  1045. if (strcmp(type, "Create") == 0)
  1046. type = "mention";
  1047. else
  1048. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  1049. type = "poll";
  1050. else
  1051. continue;
  1052. /* excluded type? */
  1053. if (!xs_is_null(excl) && xs_list_in(excl, type) != -1)
  1054. continue;
  1055. xs *mn = xs_dict_new();
  1056. mn = xs_dict_append(mn, "type", type);
  1057. xs *id = xs_replace(xs_dict_get(noti, "id"), ".", "");
  1058. mn = xs_dict_append(mn, "id", id);
  1059. mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
  1060. xs *acct = mastoapi_account(actor);
  1061. mn = xs_dict_append(mn, "account", acct);
  1062. if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
  1063. xs *st = mastoapi_status(&snac1, entry);
  1064. mn = xs_dict_append(mn, "status", st);
  1065. }
  1066. out = xs_list_append(out, mn);
  1067. }
  1068. *body = xs_json_dumps_pp(out, 4);
  1069. *ctype = "application/json";
  1070. status = 200;
  1071. }
  1072. else
  1073. status = 401;
  1074. }
  1075. else
  1076. if (strcmp(cmd, "/v1/filters") == 0) { /** **/
  1077. /* snac will never have filters */
  1078. *body = xs_dup("[]");
  1079. *ctype = "application/json";
  1080. status = 200;
  1081. }
  1082. else
  1083. if (strcmp(cmd, "/v1/favourites") == 0) { /** **/
  1084. /* snac will never support a list of favourites */
  1085. *body = xs_dup("[]");
  1086. *ctype = "application/json";
  1087. status = 200;
  1088. }
  1089. else
  1090. if (strcmp(cmd, "/v1/bookmarks") == 0) { /** **/
  1091. /* snac does not support bookmarks */
  1092. *body = xs_dup("[]");
  1093. *ctype = "application/json";
  1094. status = 200;
  1095. }
  1096. else
  1097. if (strcmp(cmd, "/v1/lists") == 0) { /** **/
  1098. /* snac does not support lists */
  1099. *body = xs_dup("[]");
  1100. *ctype = "application/json";
  1101. status = 200;
  1102. }
  1103. else
  1104. if (strcmp(cmd, "/v1/scheduled_statuses") == 0) { /** **/
  1105. /* snac does not schedule notes */
  1106. *body = xs_dup("[]");
  1107. *ctype = "application/json";
  1108. status = 200;
  1109. }
  1110. else
  1111. if (strcmp(cmd, "/v1/follow_requests") == 0) { /** **/
  1112. /* snac does not support optional follow confirmations */
  1113. *body = xs_dup("[]");
  1114. *ctype = "application/json";
  1115. status = 200;
  1116. }
  1117. else
  1118. if (strcmp(cmd, "/v1/announcements") == 0) { /** **/
  1119. /* snac has no announcements (yet?) */
  1120. *body = xs_dup("[]");
  1121. *ctype = "application/json";
  1122. status = 200;
  1123. }
  1124. else
  1125. if (strcmp(cmd, "/v1/custom_emojis") == 0) { /** **/
  1126. /* are you kidding me? */
  1127. *body = xs_dup("[]");
  1128. *ctype = "application/json";
  1129. status = 200;
  1130. }
  1131. else
  1132. if (strcmp(cmd, "/v1/instance") == 0) { /** **/
  1133. /* returns an instance object */
  1134. xs *ins = xs_dict_new();
  1135. const char *host = xs_dict_get(srv_config, "host");
  1136. ins = xs_dict_append(ins, "uri", host);
  1137. ins = xs_dict_append(ins, "domain", host);
  1138. ins = xs_dict_append(ins, "title", host);
  1139. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  1140. ins = xs_dict_append(ins, "source_url", WHAT_IS_SNAC_URL);
  1141. ins = xs_dict_append(ins, "description", host);
  1142. ins = xs_dict_append(ins, "short_description", host);
  1143. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  1144. ins = xs_dict_append(ins, "thumbnail", susie);
  1145. const char *v = xs_dict_get(srv_config, "admin_email");
  1146. if (xs_is_null(v) || *v == '\0')
  1147. v = "admin@localhost";
  1148. ins = xs_dict_append(ins, "email", v);
  1149. xs *l1 = xs_list_new();
  1150. ins = xs_dict_append(ins, "rules", l1);
  1151. l1 = xs_list_append(l1, "en");
  1152. ins = xs_dict_append(ins, "languages", l1);
  1153. xs *d1 = xs_dict_new();
  1154. ins = xs_dict_append(ins, "urls", d1);
  1155. xs *z = xs_number_new(0);
  1156. d1 = xs_dict_append(d1, "user_count", z);
  1157. d1 = xs_dict_append(d1, "status_count", z);
  1158. d1 = xs_dict_append(d1, "domain_count", z);
  1159. ins = xs_dict_append(ins, "stats", d1);
  1160. xs *f = xs_val_new(XSTYPE_FALSE);
  1161. ins = xs_dict_append(ins, "registrations", f);
  1162. ins = xs_dict_append(ins, "approval_required", f);
  1163. ins = xs_dict_append(ins, "invites_enabled", f);
  1164. xs *cfg = xs_dict_new();
  1165. {
  1166. xs *d11 = xs_dict_new();
  1167. xs *mc = xs_number_new(100000);
  1168. xs *mm = xs_number_new(8);
  1169. xs *cr = xs_number_new(32);
  1170. d11 = xs_dict_append(d11, "max_characters", mc);
  1171. d11 = xs_dict_append(d11, "max_media_attachments", mm);
  1172. d11 = xs_dict_append(d11, "characters_reserved_per_url", cr);
  1173. cfg = xs_dict_append(cfg, "statuses", d11);
  1174. }
  1175. ins = xs_dict_append(ins, "configuration", cfg);
  1176. const char *admin_account = xs_dict_get(srv_config, "admin_account");
  1177. if (!xs_is_null(admin_account) && *admin_account) {
  1178. snac admin;
  1179. if (user_open(&admin, admin_account)) {
  1180. xs *actor = msg_actor(&admin);
  1181. xs *acct = mastoapi_account(actor);
  1182. ins = xs_dict_append(ins, "contact_account", acct);
  1183. user_free(&admin);
  1184. }
  1185. }
  1186. *body = xs_json_dumps_pp(ins, 4);
  1187. *ctype = "application/json";
  1188. status = 200;
  1189. }
  1190. else
  1191. if (xs_startswith(cmd, "/v1/statuses/")) { /** **/
  1192. /* information about a status */
  1193. if (logged_in) {
  1194. xs *l = xs_split(cmd, "/");
  1195. const char *id = xs_list_get(l, 3);
  1196. const char *op = xs_list_get(l, 4);
  1197. if (!xs_is_null(id)) {
  1198. xs *msg = NULL;
  1199. xs *out = NULL;
  1200. /* skip the 'fake' part of the id */
  1201. id = MID_TO_MD5(id);
  1202. if (valid_status(object_get_by_md5(id, &msg))) {
  1203. if (op == NULL) {
  1204. if (!is_muted(&snac1, xs_dict_get(msg, "attributedTo"))) {
  1205. /* return the status itself */
  1206. out = mastoapi_status(&snac1, msg);
  1207. }
  1208. }
  1209. else
  1210. if (strcmp(op, "context") == 0) { /** **/
  1211. /* return ancestors and children */
  1212. xs *anc = xs_list_new();
  1213. xs *des = xs_list_new();
  1214. xs_list *p;
  1215. xs_str *v;
  1216. char pid[64];
  1217. /* build the [grand]parent list, moving up */
  1218. strncpy(pid, id, sizeof(pid));
  1219. while (object_parent(pid, pid, sizeof(pid))) {
  1220. xs *m2 = NULL;
  1221. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  1222. xs *st = mastoapi_status(&snac1, m2);
  1223. anc = xs_list_insert(anc, 0, st);
  1224. }
  1225. else
  1226. break;
  1227. }
  1228. /* build the children list */
  1229. xs *children = object_children(xs_dict_get(msg, "id"));
  1230. p = children;
  1231. while (xs_list_iter(&p, &v)) {
  1232. xs *m2 = NULL;
  1233. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  1234. if (xs_is_null(xs_dict_get(m2, "name"))) {
  1235. xs *st = mastoapi_status(&snac1, m2);
  1236. des = xs_list_append(des, st);
  1237. }
  1238. }
  1239. }
  1240. out = xs_dict_new();
  1241. out = xs_dict_append(out, "ancestors", anc);
  1242. out = xs_dict_append(out, "descendants", des);
  1243. }
  1244. else
  1245. if (strcmp(op, "reblogged_by") == 0 || /** **/
  1246. strcmp(op, "favourited_by") == 0) { /** **/
  1247. /* return the list of people who liked or boosted this */
  1248. out = xs_list_new();
  1249. xs *l = NULL;
  1250. if (op[0] == 'r')
  1251. l = object_announces(xs_dict_get(msg, "id"));
  1252. else
  1253. l = object_likes(xs_dict_get(msg, "id"));
  1254. xs_list *p = l;
  1255. xs_str *v;
  1256. while (xs_list_iter(&p, &v)) {
  1257. xs *actor2 = NULL;
  1258. if (valid_status(object_get_by_md5(v, &actor2))) {
  1259. xs *acct2 = mastoapi_account(actor2);
  1260. out = xs_list_append(out, acct2);
  1261. }
  1262. }
  1263. }
  1264. }
  1265. else
  1266. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  1267. if (out != NULL) {
  1268. *body = xs_json_dumps_pp(out, 4);
  1269. *ctype = "application/json";
  1270. status = 200;
  1271. }
  1272. }
  1273. }
  1274. else
  1275. status = 401;
  1276. }
  1277. else
  1278. if (strcmp(cmd, "/v1/preferences") == 0) { /** **/
  1279. *body = xs_dup("{}");
  1280. *ctype = "application/json";
  1281. status = 200;
  1282. }
  1283. else
  1284. if (strcmp(cmd, "/v1/markers") == 0) { /** **/
  1285. *body = xs_dup("{}");
  1286. *ctype = "application/json";
  1287. status = 200;
  1288. }
  1289. else
  1290. if (strcmp(cmd, "/v1/followed_tags") == 0) { /** **/
  1291. *body = xs_dup("[]");
  1292. *ctype = "application/json";
  1293. status = 200;
  1294. }
  1295. else
  1296. if (strcmp(cmd, "/v2/search") == 0) { /** **/
  1297. if (logged_in) {
  1298. const char *q = xs_dict_get(args, "q");
  1299. const char *type = xs_dict_get(args, "type");
  1300. const char *offset = xs_dict_get(args, "offset");
  1301. xs *acl = xs_list_new();
  1302. xs *stl = xs_list_new();
  1303. xs *htl = xs_list_new();
  1304. xs *res = xs_dict_new();
  1305. if (xs_is_null(offset) || strcmp(offset, "0") == 0) {
  1306. /* reply something only for offset 0; otherwise,
  1307. apps like Tusky keep asking again and again */
  1308. if (!xs_is_null(q) && !xs_is_null(type) && strcmp(type, "accounts") == 0) {
  1309. /* do a webfinger query */
  1310. char *actor = NULL;
  1311. char *user = NULL;
  1312. if (valid_status(webfinger_request(q, &actor, &user))) {
  1313. xs *actor_o = NULL;
  1314. if (valid_status(actor_request(&snac1, actor, &actor_o))) {
  1315. xs *acct = mastoapi_account(actor_o);
  1316. acl = xs_list_append(acl, acct);
  1317. }
  1318. }
  1319. }
  1320. }
  1321. res = xs_dict_append(res, "accounts", acl);
  1322. res = xs_dict_append(res, "statuses", stl);
  1323. res = xs_dict_append(res, "hashtags", htl);
  1324. *body = xs_json_dumps_pp(res, 4);
  1325. *ctype = "application/json";
  1326. status = 200;
  1327. }
  1328. else
  1329. status = 401;
  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. else
  1696. if (xs_startswith(cmd, "/v1/polls")) { /** **/
  1697. if (logged_in) {
  1698. /* operations on a status */
  1699. xs *l = xs_split(cmd, "/");
  1700. const char *mid = xs_list_get(l, 3);
  1701. const char *op = xs_list_get(l, 4);
  1702. if (!xs_is_null(mid)) {
  1703. xs *msg = NULL;
  1704. xs *out = NULL;
  1705. /* skip the 'fake' part of the id */
  1706. mid = MID_TO_MD5(mid);
  1707. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1708. const char *id = xs_dict_get(msg, "id");
  1709. const char *atto = xs_dict_get(msg, "attributedTo");
  1710. xs_list *opts = xs_dict_get(msg, "oneOf");
  1711. if (opts == NULL)
  1712. opts = xs_dict_get(msg, "anyOf");
  1713. if (op == NULL) {
  1714. }
  1715. else
  1716. if (strcmp(op, "votes") == 0) {
  1717. xs_list *choices = xs_dict_get(args, "choices[]");
  1718. if (xs_type(choices) == XSTYPE_LIST) {
  1719. xs_str *v;
  1720. while (xs_list_iter(&choices, &v)) {
  1721. int io = atoi(v);
  1722. const xs_dict *o = xs_list_get(opts, io);
  1723. if (o) {
  1724. const char *name = xs_dict_get(o, "name");
  1725. xs *msg = msg_note(&snac, "", atto, (char *)id, NULL, 1);
  1726. msg = xs_dict_append(msg, "name", name);
  1727. xs *c_msg = msg_create(&snac, msg);
  1728. enqueue_message(&snac, c_msg);
  1729. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1730. }
  1731. }
  1732. out = mastoapi_poll(&snac, msg);
  1733. }
  1734. }
  1735. }
  1736. if (out != NULL) {
  1737. *body = xs_json_dumps_pp(out, 4);
  1738. *ctype = "application/json";
  1739. status = 200;
  1740. }
  1741. }
  1742. }
  1743. else
  1744. status = 401;
  1745. }
  1746. /* user cleanup */
  1747. if (logged_in)
  1748. user_free(&snac);
  1749. return status;
  1750. }
  1751. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  1752. const char *payload, int p_size,
  1753. char **body, int *b_size, char **ctype)
  1754. {
  1755. (void)p_size;
  1756. (void)b_size;
  1757. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1758. return 0;
  1759. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1760. /* {
  1761. xs *j = xs_json_dumps_pp(req, 4);
  1762. printf("mastoapi put:\n%s\n", j);
  1763. }*/
  1764. int status = 404;
  1765. xs *args = NULL;
  1766. char *i_ctype = xs_dict_get(req, "content-type");
  1767. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1768. args = xs_json_loads(payload);
  1769. else
  1770. args = xs_dup(xs_dict_get(req, "p_vars"));
  1771. if (args == NULL)
  1772. return 400;
  1773. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1774. snac snac = {0};
  1775. int logged_in = process_auth_token(&snac, req);
  1776. if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) { /** **/
  1777. if (logged_in) {
  1778. xs *l = xs_split(cmd, "/");
  1779. const char *stid = xs_list_get(l, 3);
  1780. if (!xs_is_null(stid)) {
  1781. const char *desc = xs_dict_get(args, "description");
  1782. /* set the image metadata */
  1783. static_put_meta(&snac, stid, desc);
  1784. /* prepare a response */
  1785. xs *rsp = xs_dict_new();
  1786. xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
  1787. rsp = xs_dict_append(rsp, "id", stid);
  1788. rsp = xs_dict_append(rsp, "type", "image");
  1789. rsp = xs_dict_append(rsp, "url", url);
  1790. rsp = xs_dict_append(rsp, "preview_url", url);
  1791. rsp = xs_dict_append(rsp, "remote_url", url);
  1792. rsp = xs_dict_append(rsp, "description", desc);
  1793. *body = xs_json_dumps_pp(rsp, 4);
  1794. *ctype = "application/json";
  1795. status = 200;
  1796. }
  1797. }
  1798. else
  1799. status = 401;
  1800. }
  1801. /* user cleanup */
  1802. if (logged_in)
  1803. user_free(&snac);
  1804. return status;
  1805. }
  1806. void mastoapi_purge(void)
  1807. {
  1808. xs *spec = xs_fmt("%s/app/" "*.json", srv_basedir);
  1809. xs *files = xs_glob(spec, 1, 0);
  1810. xs_list *p = files;
  1811. xs_str *v;
  1812. time_t mt = time(NULL) - 3600;
  1813. while (xs_list_iter(&p, &v)) {
  1814. xs *cid = xs_replace(v, ".json", "");
  1815. xs *fn = _app_fn(cid);
  1816. if (mtime(fn) < mt) {
  1817. /* get the app */
  1818. xs *app = app_get(cid);
  1819. if (app) {
  1820. /* old apps with no uid are incomplete cruft */
  1821. const char *uid = xs_dict_get(app, "uid");
  1822. if (xs_is_null(uid) || *uid == '\0') {
  1823. unlink(fn);
  1824. srv_debug(2, xs_fmt("purged %s", fn));
  1825. }
  1826. }
  1827. }
  1828. }
  1829. }
  1830. #endif /* #ifndef NO_MASTODON_API */