mastoapi.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313
  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. if (strcmp(xs_dict_get(actor, "type"), "Service") == 0)
  411. acct = xs_dict_append(acct, "bot", "true");
  412. else
  413. acct = xs_dict_append(acct, "bot", "false");
  414. acct = xs_dict_append(acct, "note", note);
  415. acct = xs_dict_append(acct, "url", id);
  416. xs *avatar = NULL;
  417. xs_dict *av = xs_dict_get(actor, "icon");
  418. if (xs_type(av) == XSTYPE_DICT)
  419. avatar = xs_dup(xs_dict_get(av, "url"));
  420. else
  421. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  422. acct = xs_dict_append(acct, "avatar", avatar);
  423. acct = xs_dict_append(acct, "avatar_static", avatar);
  424. xs *header = NULL;
  425. xs_dict *hd = xs_dict_get(actor, "image");
  426. if (xs_type(hd) == XSTYPE_DICT)
  427. header = xs_dup(xs_dict_get(hd, "url"));
  428. acct = xs_dict_append(acct, "header", header);
  429. acct = xs_dict_append(acct, "header_static", header);
  430. /* emojis */
  431. xs_list *p;
  432. if (!xs_is_null(p = xs_dict_get(actor, "tag"))) {
  433. xs *eml = xs_list_new();
  434. xs_dict *v;
  435. xs *t = xs_val_new(XSTYPE_TRUE);
  436. while (xs_list_iter(&p, &v)) {
  437. const char *type = xs_dict_get(v, "type");
  438. if (!xs_is_null(type) && strcmp(type, "Emoji") == 0) {
  439. const char *name = xs_dict_get(v, "name");
  440. const xs_dict *icon = xs_dict_get(v, "icon");
  441. if (!xs_is_null(name) && !xs_is_null(icon)) {
  442. const char *url = xs_dict_get(icon, "url");
  443. if (!xs_is_null(url)) {
  444. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  445. xs *d1 = xs_dict_new();
  446. d1 = xs_dict_append(d1, "shortcode", nm);
  447. d1 = xs_dict_append(d1, "url", url);
  448. d1 = xs_dict_append(d1, "static_url", url);
  449. d1 = xs_dict_append(d1, "visible_in_picker", t);
  450. eml = xs_list_append(eml, d1);
  451. }
  452. }
  453. }
  454. }
  455. acct = xs_dict_append(acct, "emojis", eml);
  456. }
  457. acct = xs_dict_append(acct, "header", "");
  458. return acct;
  459. }
  460. xs_dict *mastoapi_poll(snac *snac, const xs_dict *msg)
  461. /* creates a mastoapi Poll object */
  462. {
  463. xs_dict *poll = xs_dict_new();
  464. xs *mid = mastoapi_id(msg);
  465. xs *f = xs_val_new(XSTYPE_FALSE);
  466. xs *t = xs_val_new(XSTYPE_TRUE);
  467. xs_list *opts = NULL;
  468. xs_val *v;
  469. int num_votes = 0;
  470. xs *options = xs_list_new();
  471. poll = xs_dict_append(poll, "id", mid);
  472. poll = xs_dict_append(poll, "expires_at", xs_dict_get(msg, "endTime"));
  473. poll = xs_dict_append(poll, "expired", xs_dict_get(msg, "closed") != NULL ? t : f);
  474. if ((opts = xs_dict_get(msg, "oneOf")) != NULL)
  475. poll = xs_dict_append(poll, "multiple", f);
  476. else {
  477. opts = xs_dict_get(msg, "anyOf");
  478. poll = xs_dict_append(poll, "multiple", t);
  479. }
  480. while (xs_list_iter(&opts, &v)) {
  481. const char *title = xs_dict_get(v, "name");
  482. const char *replies = xs_dict_get(v, "replies");
  483. if (title && replies) {
  484. const char *votes_count = xs_dict_get(replies, "totalItems");
  485. if (xs_type(votes_count) == XSTYPE_NUMBER) {
  486. xs *d = xs_dict_new();
  487. d = xs_dict_append(d, "title", title);
  488. d = xs_dict_append(d, "votes_count", votes_count);
  489. options = xs_list_append(options, d);
  490. num_votes += xs_number_get(votes_count);
  491. }
  492. }
  493. }
  494. poll = xs_dict_append(poll, "options", options);
  495. xs *vc = xs_number_new(num_votes);
  496. poll = xs_dict_append(poll, "votes_count", vc);
  497. poll = xs_dict_append(poll, "voted",
  498. was_question_voted(snac, xs_dict_get(msg, "id")) ? t : f);
  499. return poll;
  500. }
  501. xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
  502. /* converts an ActivityPub note to a Mastodon status */
  503. {
  504. xs *actor = NULL;
  505. actor_get(snac, xs_dict_get(msg, "attributedTo"), &actor);
  506. /* if the author is not here, discard */
  507. if (actor == NULL)
  508. return NULL;
  509. const char *type = xs_dict_get(msg, "type");
  510. const char *id = xs_dict_get(msg, "id");
  511. xs *acct = mastoapi_account(actor);
  512. xs *f = xs_val_new(XSTYPE_FALSE);
  513. xs *t = xs_val_new(XSTYPE_TRUE);
  514. xs *n = xs_val_new(XSTYPE_NULL);
  515. xs *idx = NULL;
  516. xs *ixc = NULL;
  517. char *tmp;
  518. xs *mid = mastoapi_id(msg);
  519. xs_dict *st = xs_dict_new();
  520. st = xs_dict_append(st, "id", mid);
  521. st = xs_dict_append(st, "uri", id);
  522. st = xs_dict_append(st, "url", id);
  523. st = xs_dict_append(st, "created_at", xs_dict_get(msg, "published"));
  524. st = xs_dict_append(st, "account", acct);
  525. st = xs_dict_append(st, "content", xs_dict_get(msg, "content"));
  526. st = xs_dict_append(st, "visibility",
  527. is_msg_public(snac, msg) ? "public" : "private");
  528. tmp = xs_dict_get(msg, "sensitive");
  529. if (xs_is_null(tmp))
  530. tmp = f;
  531. st = xs_dict_append(st, "sensitive", tmp);
  532. tmp = xs_dict_get(msg, "summary");
  533. if (xs_is_null(tmp))
  534. tmp = "";
  535. st = xs_dict_append(st, "spoiler_text", tmp);
  536. /* create the list of attachments */
  537. xs *matt = xs_list_new();
  538. xs_list *att = xs_dict_get(msg, "attachment");
  539. xs_str *aobj;
  540. while (xs_list_iter(&att, &aobj)) {
  541. const char *mtype = xs_dict_get(aobj, "mediaType");
  542. if (!xs_is_null(mtype)) {
  543. if (xs_startswith(mtype, "image/") || xs_startswith(mtype, "video/")) {
  544. xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
  545. xs *matte = xs_dict_new();
  546. matte = xs_dict_append(matte, "id", matteid);
  547. matte = xs_dict_append(matte, "type", *mtype == 'i' ? "image" : "video");
  548. matte = xs_dict_append(matte, "url", xs_dict_get(aobj, "url"));
  549. matte = xs_dict_append(matte, "preview_url", xs_dict_get(aobj, "url"));
  550. matte = xs_dict_append(matte, "remote_url", xs_dict_get(aobj, "url"));
  551. const char *name = xs_dict_get(aobj, "name");
  552. if (xs_is_null(name))
  553. name = "";
  554. matte = xs_dict_append(matte, "description", name);
  555. matt = xs_list_append(matt, matte);
  556. }
  557. }
  558. }
  559. st = xs_dict_append(st, "media_attachments", matt);
  560. {
  561. xs *ml = xs_list_new();
  562. xs *htl = xs_list_new();
  563. xs *eml = xs_list_new();
  564. xs_list *p = xs_dict_get(msg, "tag");
  565. xs_dict *v;
  566. int n = 0;
  567. while (xs_list_iter(&p, &v)) {
  568. const char *type = xs_dict_get(v, "type");
  569. if (xs_is_null(type))
  570. continue;
  571. xs *d1 = xs_dict_new();
  572. if (strcmp(type, "Mention") == 0) {
  573. const char *name = xs_dict_get(v, "name");
  574. const char *href = xs_dict_get(v, "href");
  575. if (!xs_is_null(name) && !xs_is_null(href) &&
  576. strcmp(href, snac->actor) != 0) {
  577. xs *nm = xs_strip_chars_i(xs_dup(name), "@");
  578. xs *id = xs_fmt("%d", n++);
  579. d1 = xs_dict_append(d1, "id", id);
  580. d1 = xs_dict_append(d1, "username", nm);
  581. d1 = xs_dict_append(d1, "acct", nm);
  582. d1 = xs_dict_append(d1, "url", href);
  583. ml = xs_list_append(ml, d1);
  584. }
  585. }
  586. else
  587. if (strcmp(type, "Hashtag") == 0) {
  588. const char *name = xs_dict_get(v, "name");
  589. const char *href = xs_dict_get(v, "href");
  590. if (!xs_is_null(name) && !xs_is_null(href)) {
  591. xs *nm = xs_strip_chars_i(xs_dup(name), "#");
  592. d1 = xs_dict_append(d1, "name", nm);
  593. d1 = xs_dict_append(d1, "url", href);
  594. htl = xs_list_append(htl, d1);
  595. }
  596. }
  597. else
  598. if (strcmp(type, "Emoji") == 0) {
  599. const char *name = xs_dict_get(v, "name");
  600. const xs_dict *icon = xs_dict_get(v, "icon");
  601. if (!xs_is_null(name) && !xs_is_null(icon)) {
  602. const char *url = xs_dict_get(icon, "url");
  603. if (!xs_is_null(url)) {
  604. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  605. xs *t = xs_val_new(XSTYPE_TRUE);
  606. d1 = xs_dict_append(d1, "shortcode", nm);
  607. d1 = xs_dict_append(d1, "url", url);
  608. d1 = xs_dict_append(d1, "static_url", url);
  609. d1 = xs_dict_append(d1, "visible_in_picker", t);
  610. d1 = xs_dict_append(d1, "category", "Emojis");
  611. eml = xs_list_append(eml, d1);
  612. }
  613. }
  614. }
  615. }
  616. st = xs_dict_append(st, "mentions", ml);
  617. st = xs_dict_append(st, "tags", htl);
  618. st = xs_dict_append(st, "emojis", eml);
  619. }
  620. xs_free(idx);
  621. xs_free(ixc);
  622. idx = object_likes(id);
  623. ixc = xs_number_new(xs_list_len(idx));
  624. st = xs_dict_append(st, "favourites_count", ixc);
  625. st = xs_dict_append(st, "favourited",
  626. xs_list_in(idx, snac->md5) != -1 ? t : f);
  627. xs_free(idx);
  628. xs_free(ixc);
  629. idx = object_announces(id);
  630. ixc = xs_number_new(xs_list_len(idx));
  631. st = xs_dict_append(st, "reblogs_count", ixc);
  632. st = xs_dict_append(st, "reblogged",
  633. xs_list_in(idx, snac->md5) != -1 ? t : f);
  634. xs_free(idx);
  635. xs_free(ixc);
  636. idx = object_children(id);
  637. ixc = xs_number_new(xs_list_len(idx));
  638. st = xs_dict_append(st, "replies_count", ixc);
  639. /* default in_reply_to values */
  640. st = xs_dict_append(st, "in_reply_to_id", n);
  641. st = xs_dict_append(st, "in_reply_to_account_id", n);
  642. tmp = xs_dict_get(msg, "inReplyTo");
  643. if (!xs_is_null(tmp)) {
  644. xs *irto = NULL;
  645. if (valid_status(object_get(tmp, &irto))) {
  646. xs *irt_mid = mastoapi_id(irto);
  647. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  648. char *at = NULL;
  649. if (!xs_is_null(at = xs_dict_get(irto, "attributedTo"))) {
  650. xs *at_md5 = xs_md5_hex(at, strlen(at));
  651. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  652. }
  653. }
  654. }
  655. st = xs_dict_append(st, "reblog", n);
  656. st = xs_dict_append(st, "poll", n);
  657. st = xs_dict_append(st, "card", n);
  658. st = xs_dict_append(st, "language", n);
  659. tmp = xs_dict_get(msg, "sourceContent");
  660. if (xs_is_null(tmp))
  661. tmp = "";
  662. st = xs_dict_append(st, "text", tmp);
  663. tmp = xs_dict_get(msg, "updated");
  664. if (xs_is_null(tmp))
  665. tmp = n;
  666. st = xs_dict_append(st, "edited_at", tmp);
  667. if (strcmp(type, "Question") == 0) {
  668. xs *poll = mastoapi_poll(snac, msg);
  669. st = xs_dict_append(st, "poll", poll);
  670. }
  671. return st;
  672. }
  673. xs_dict *mastoapi_relationship(snac *snac, const char *md5)
  674. {
  675. xs_dict *rel = NULL;
  676. xs *actor_o = NULL;
  677. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  678. xs *t = xs_val_new(XSTYPE_TRUE);
  679. xs *f = xs_val_new(XSTYPE_FALSE);
  680. rel = xs_dict_new();
  681. const char *actor = xs_dict_get(actor_o, "id");
  682. rel = xs_dict_append(rel, "id", md5);
  683. rel = xs_dict_append(rel, "following",
  684. following_check(snac, actor) ? t : f);
  685. rel = xs_dict_append(rel, "showing_reblogs", t);
  686. rel = xs_dict_append(rel, "notifying", f);
  687. rel = xs_dict_append(rel, "followed_by",
  688. follower_check(snac, actor) ? t : f);
  689. rel = xs_dict_append(rel, "blocking",
  690. is_muted(snac, actor) ? t : f);
  691. rel = xs_dict_append(rel, "muting", f);
  692. rel = xs_dict_append(rel, "muting_notifications", f);
  693. rel = xs_dict_append(rel, "requested", f);
  694. rel = xs_dict_append(rel, "domain_blocking", f);
  695. rel = xs_dict_append(rel, "endorsed", f);
  696. rel = xs_dict_append(rel, "note", "");
  697. }
  698. return rel;
  699. }
  700. int process_auth_token(snac *snac, const xs_dict *req)
  701. /* processes an authorization token, if there is one */
  702. {
  703. int logged_in = 0;
  704. char *v;
  705. /* if there is an authorization field, try to validate it */
  706. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  707. xs *tokid = xs_replace_n(v, "Bearer ", "", 1);
  708. xs *token = token_get(tokid);
  709. if (token != NULL) {
  710. const char *uid = xs_dict_get(token, "uid");
  711. if (!xs_is_null(uid) && user_open(snac, uid)) {
  712. logged_in = 1;
  713. /* this counts as a 'login' */
  714. lastlog_write(snac, "mastoapi");
  715. srv_debug(2, xs_fmt("mastoapi auth: valid token for user %s", uid));
  716. }
  717. else
  718. srv_log(xs_fmt("mastoapi auth: corrupted token %s", tokid));
  719. }
  720. else
  721. srv_log(xs_fmt("mastoapi auth: invalid token %s", tokid));
  722. }
  723. return logged_in;
  724. }
  725. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  726. char **body, int *b_size, char **ctype)
  727. {
  728. (void)b_size;
  729. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  730. return 0;
  731. srv_debug(1, xs_fmt("mastoapi_get_handler %s", q_path));
  732. /* {
  733. xs *j = xs_json_dumps_pp(req, 4);
  734. printf("mastoapi get:\n%s\n", j);
  735. }*/
  736. int status = 404;
  737. xs_dict *args = xs_dict_get(req, "q_vars");
  738. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  739. snac snac1 = {0};
  740. int logged_in = process_auth_token(&snac1, req);
  741. if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) { /** **/
  742. if (logged_in) {
  743. xs *acct = xs_dict_new();
  744. acct = xs_dict_append(acct, "id", xs_dict_get(snac1.config, "uid"));
  745. acct = xs_dict_append(acct, "username", xs_dict_get(snac1.config, "uid"));
  746. acct = xs_dict_append(acct, "acct", xs_dict_get(snac1.config, "uid"));
  747. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac1.config, "name"));
  748. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac1.config, "published"));
  749. acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio"));
  750. acct = xs_dict_append(acct, "url", snac1.actor);
  751. acct = xs_dict_append(acct, "header", "");
  752. xs *avatar = NULL;
  753. char *av = xs_dict_get(snac1.config, "avatar");
  754. if (xs_is_null(av) || *av == '\0')
  755. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  756. else
  757. avatar = xs_dup(av);
  758. acct = xs_dict_append(acct, "avatar", avatar);
  759. *body = xs_json_dumps_pp(acct, 4);
  760. *ctype = "application/json";
  761. status = 200;
  762. }
  763. else {
  764. status = 422; // "Unprocessable entity" (no login)
  765. }
  766. }
  767. else
  768. if (strcmp(cmd, "/v1/accounts/relationships") == 0) { /** **/
  769. /* find if an account is followed, blocked, etc. */
  770. /* the account to get relationships about is in args "id[]" */
  771. if (logged_in) {
  772. xs *res = xs_list_new();
  773. const char *md5 = xs_dict_get(args, "id[]");
  774. if (!xs_is_null(md5)) {
  775. if (xs_type(md5) == XSTYPE_LIST)
  776. md5 = xs_list_get(md5, 0);
  777. xs *rel = mastoapi_relationship(&snac1, md5);
  778. if (rel != NULL)
  779. res = xs_list_append(res, rel);
  780. }
  781. *body = xs_json_dumps_pp(res, 4);
  782. *ctype = "application/json";
  783. status = 200;
  784. }
  785. else
  786. status = 422;
  787. }
  788. else
  789. if (xs_startswith(cmd, "/v1/accounts/")) { /** **/
  790. /* account-related information */
  791. xs *l = xs_split(cmd, "/");
  792. const char *uid = xs_list_get(l, 3);
  793. const char *opt = xs_list_get(l, 4);
  794. if (uid != NULL) {
  795. snac snac2;
  796. xs *out = NULL;
  797. xs *actor = NULL;
  798. if (logged_in && strcmp(uid, "search") == 0) { /** **/
  799. /* search for accounts starting with q */
  800. const char *aq = xs_dict_get(args, "q");
  801. if (!xs_is_null(aq)) {
  802. xs *q = xs_tolower_i(xs_dup(aq));
  803. out = xs_list_new();
  804. xs *wing = following_list(&snac1);
  805. xs *wers = follower_list(&snac1);
  806. xs *ulst = user_list();
  807. xs_list *p;
  808. xs_str *v;
  809. xs_set seen;
  810. xs_set_init(&seen);
  811. /* user relations */
  812. xs_list *lsts[] = { wing, wers, NULL };
  813. int n;
  814. for (n = 0; (p = lsts[n]) != NULL; n++) {
  815. while (xs_list_iter(&p, &v)) {
  816. /* already seen? skip */
  817. if (xs_set_add(&seen, v) == 0)
  818. continue;
  819. xs *actor = NULL;
  820. if (valid_status(object_get(v, &actor))) {
  821. const char *uname = xs_dict_get(actor, "preferredUsername");
  822. if (!xs_is_null(uname)) {
  823. xs *luname = xs_tolower_i(xs_dup(uname));
  824. if (xs_startswith(luname, q)) {
  825. xs *acct = mastoapi_account(actor);
  826. out = xs_list_append(out, acct);
  827. }
  828. }
  829. }
  830. }
  831. }
  832. /* local users */
  833. p = ulst;
  834. while (xs_list_iter(&p, &v)) {
  835. snac user;
  836. /* skip this same user */
  837. if (strcmp(v, xs_dict_get(snac1.config, "uid")) == 0)
  838. continue;
  839. /* skip if the uid does not start with the query */
  840. xs *v2 = xs_tolower_i(xs_dup(v));
  841. if (!xs_startswith(v2, q))
  842. continue;
  843. if (user_open(&user, v)) {
  844. /* if it's not already seen, add it */
  845. if (xs_set_add(&seen, user.actor) == 1) {
  846. xs *actor = msg_actor(&user);
  847. xs *acct = mastoapi_account(actor);
  848. out = xs_list_append(out, acct);
  849. }
  850. user_free(&user);
  851. }
  852. }
  853. xs_set_free(&seen);
  854. }
  855. }
  856. else
  857. /* is it a local user? */
  858. if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
  859. if (opt == NULL) {
  860. /* account information */
  861. actor = msg_actor(&snac2);
  862. out = mastoapi_account(actor);
  863. }
  864. else
  865. if (strcmp(opt, "statuses") == 0) { /** **/
  866. /* the public list of posts of a user */
  867. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  868. xs_list *p = timeline;
  869. xs_str *v;
  870. out = xs_list_new();
  871. while (xs_list_iter(&p, &v)) {
  872. xs *msg = NULL;
  873. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  874. /* add only posts by the author */
  875. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  876. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  877. xs *st = mastoapi_status(&snac2, msg);
  878. out = xs_list_append(out, st);
  879. }
  880. }
  881. }
  882. }
  883. user_free(&snac2);
  884. }
  885. else {
  886. /* try the uid as the md5 of a possibly loaded actor */
  887. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  888. if (opt == NULL) {
  889. /* account information */
  890. out = mastoapi_account(actor);
  891. }
  892. else
  893. if (strcmp(opt, "statuses") == 0) {
  894. /* we don't serve statuses of others; return the empty list */
  895. out = xs_list_new();
  896. }
  897. }
  898. }
  899. if (out != NULL) {
  900. *body = xs_json_dumps_pp(out, 4);
  901. *ctype = "application/json";
  902. status = 200;
  903. }
  904. }
  905. }
  906. else
  907. if (strcmp(cmd, "/v1/timelines/home") == 0) { /** **/
  908. /* the private timeline */
  909. if (logged_in) {
  910. const char *max_id = xs_dict_get(args, "max_id");
  911. const char *since_id = xs_dict_get(args, "since_id");
  912. const char *min_id = xs_dict_get(args, "min_id");
  913. const char *limit_s = xs_dict_get(args, "limit");
  914. int limit = 0;
  915. int cnt = 0;
  916. if (!xs_is_null(limit_s))
  917. limit = atoi(limit_s);
  918. if (limit == 0)
  919. limit = 20;
  920. xs *timeline = timeline_simple_list(&snac1, "private", 0, 256);
  921. xs *out = xs_list_new();
  922. xs_list *p = timeline;
  923. xs_str *v;
  924. while (xs_list_iter(&p, &v) && cnt < limit) {
  925. xs *msg = NULL;
  926. /* only return entries older that max_id */
  927. if (max_id) {
  928. if (strcmp(v, MID_TO_MD5(max_id)) == 0)
  929. max_id = NULL;
  930. continue;
  931. }
  932. /* only returns entries newer than since_id */
  933. if (since_id) {
  934. if (strcmp(v, MID_TO_MD5(since_id)) == 0)
  935. break;
  936. }
  937. /* only returns entries newer than min_id */
  938. /* what does really "Return results immediately newer than ID" mean? */
  939. if (min_id) {
  940. if (strcmp(v, MID_TO_MD5(min_id)) == 0)
  941. break;
  942. }
  943. /* get the entry */
  944. if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
  945. continue;
  946. /* discard non-Notes */
  947. const char *type = xs_dict_get(msg, "type");
  948. if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0)
  949. continue;
  950. /* discard notes from people we don't follow with no boosts */
  951. if (!following_check(&snac1, xs_dict_get(msg, "attributedTo")) &&
  952. object_announces_len(xs_dict_get(msg, "id")) == 0)
  953. continue;
  954. /* discard notes from muted morons */
  955. if (is_muted(&snac1, xs_dict_get(msg, "attributedTo")))
  956. continue;
  957. /* discard hidden notes */
  958. if (is_hidden(&snac1, xs_dict_get(msg, "id")))
  959. continue;
  960. /* discard poll votes (they have a name) */
  961. if (!xs_is_null(xs_dict_get(msg, "name")))
  962. continue;
  963. /* convert the Note into a Mastodon status */
  964. xs *st = mastoapi_status(&snac1, msg);
  965. if (st != NULL)
  966. out = xs_list_append(out, st);
  967. cnt++;
  968. }
  969. *body = xs_json_dumps_pp(out, 4);
  970. *ctype = "application/json";
  971. status = 200;
  972. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  973. }
  974. else {
  975. status = 401; // unauthorized
  976. }
  977. }
  978. else
  979. if (strcmp(cmd, "/v1/timelines/public") == 0) { /** **/
  980. /* the instance public timeline (public timelines for all users) */
  981. /* NOTE: this api call needs no authorization; but,
  982. I need a logged-in user in mastoapi_status() for
  983. is_msg_public() and the liked/boosted flags,
  984. so it will silently fail for pure public access */
  985. const char *limit_s = xs_dict_get(args, "limit");
  986. int limit = 0;
  987. int cnt = 0;
  988. if (!xs_is_null(limit_s))
  989. limit = atoi(limit_s);
  990. if (limit == 0)
  991. limit = 20;
  992. xs *timeline = timeline_instance_list(0, limit);
  993. xs *out = xs_list_new();
  994. xs_list *p = timeline;
  995. xs_str *md5;
  996. while (logged_in && xs_list_iter(&p, &md5) && cnt < limit) {
  997. xs *msg = NULL;
  998. /* get the entry */
  999. if (!valid_status(object_get_by_md5(md5, &msg)))
  1000. continue;
  1001. /* discard non-Notes */
  1002. const char *type = xs_dict_get(msg, "type");
  1003. if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0)
  1004. continue;
  1005. /* convert the Note into a Mastodon status */
  1006. xs *st = mastoapi_status(&snac1, msg);
  1007. if (st != NULL) {
  1008. out = xs_list_append(out, st);
  1009. cnt++;
  1010. }
  1011. }
  1012. *body = xs_json_dumps_pp(out, 4);
  1013. *ctype = "application/json";
  1014. status = 200;
  1015. }
  1016. else
  1017. if (strcmp(cmd, "/v1/conversations") == 0) { /** **/
  1018. /* TBD */
  1019. *body = xs_dup("[]");
  1020. *ctype = "application/json";
  1021. status = 200;
  1022. }
  1023. else
  1024. if (strcmp(cmd, "/v1/notifications") == 0) { /** **/
  1025. if (logged_in) {
  1026. xs *l = notify_list(&snac1, 0);
  1027. xs *out = xs_list_new();
  1028. xs_list *p = l;
  1029. xs_dict *v;
  1030. xs_list *excl = xs_dict_get(args, "exclude_types[]");
  1031. while (xs_list_iter(&p, &v)) {
  1032. xs *noti = notify_get(&snac1, v);
  1033. if (noti == NULL)
  1034. continue;
  1035. const char *type = xs_dict_get(noti, "type");
  1036. const char *utype = xs_dict_get(noti, "utype");
  1037. const char *objid = xs_dict_get(noti, "objid");
  1038. xs *actor = NULL;
  1039. xs *entry = NULL;
  1040. if (!valid_status(actor_get(&snac1, xs_dict_get(noti, "actor"), &actor)))
  1041. continue;
  1042. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  1043. continue;
  1044. if (is_hidden(&snac1, objid))
  1045. continue;
  1046. /* convert the type */
  1047. if (strcmp(type, "Like") == 0)
  1048. type = "favourite";
  1049. else
  1050. if (strcmp(type, "Announce") == 0)
  1051. type = "reblog";
  1052. else
  1053. if (strcmp(type, "Follow") == 0)
  1054. type = "follow";
  1055. else
  1056. if (strcmp(type, "Create") == 0)
  1057. type = "mention";
  1058. else
  1059. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  1060. type = "poll";
  1061. else
  1062. continue;
  1063. /* excluded type? */
  1064. if (!xs_is_null(excl) && xs_list_in(excl, type) != -1)
  1065. continue;
  1066. xs *mn = xs_dict_new();
  1067. mn = xs_dict_append(mn, "type", type);
  1068. xs *id = xs_replace(xs_dict_get(noti, "id"), ".", "");
  1069. mn = xs_dict_append(mn, "id", id);
  1070. mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
  1071. xs *acct = mastoapi_account(actor);
  1072. mn = xs_dict_append(mn, "account", acct);
  1073. if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
  1074. xs *st = mastoapi_status(&snac1, entry);
  1075. mn = xs_dict_append(mn, "status", st);
  1076. }
  1077. out = xs_list_append(out, mn);
  1078. }
  1079. *body = xs_json_dumps_pp(out, 4);
  1080. *ctype = "application/json";
  1081. status = 200;
  1082. }
  1083. else
  1084. status = 401;
  1085. }
  1086. else
  1087. if (strcmp(cmd, "/v1/filters") == 0) { /** **/
  1088. /* snac will never have filters */
  1089. *body = xs_dup("[]");
  1090. *ctype = "application/json";
  1091. status = 200;
  1092. }
  1093. else
  1094. if (strcmp(cmd, "/v1/favourites") == 0) { /** **/
  1095. /* snac will never support a list of favourites */
  1096. *body = xs_dup("[]");
  1097. *ctype = "application/json";
  1098. status = 200;
  1099. }
  1100. else
  1101. if (strcmp(cmd, "/v1/bookmarks") == 0) { /** **/
  1102. /* snac does not support bookmarks */
  1103. *body = xs_dup("[]");
  1104. *ctype = "application/json";
  1105. status = 200;
  1106. }
  1107. else
  1108. if (strcmp(cmd, "/v1/lists") == 0) { /** **/
  1109. /* snac does not support lists */
  1110. *body = xs_dup("[]");
  1111. *ctype = "application/json";
  1112. status = 200;
  1113. }
  1114. else
  1115. if (strcmp(cmd, "/v1/scheduled_statuses") == 0) { /** **/
  1116. /* snac does not schedule notes */
  1117. *body = xs_dup("[]");
  1118. *ctype = "application/json";
  1119. status = 200;
  1120. }
  1121. else
  1122. if (strcmp(cmd, "/v1/follow_requests") == 0) { /** **/
  1123. /* snac does not support optional follow confirmations */
  1124. *body = xs_dup("[]");
  1125. *ctype = "application/json";
  1126. status = 200;
  1127. }
  1128. else
  1129. if (strcmp(cmd, "/v1/announcements") == 0) { /** **/
  1130. /* snac has no announcements (yet?) */
  1131. *body = xs_dup("[]");
  1132. *ctype = "application/json";
  1133. status = 200;
  1134. }
  1135. else
  1136. if (strcmp(cmd, "/v1/custom_emojis") == 0) { /** **/
  1137. /* are you kidding me? */
  1138. *body = xs_dup("[]");
  1139. *ctype = "application/json";
  1140. status = 200;
  1141. }
  1142. else
  1143. if (strcmp(cmd, "/v1/instance") == 0) { /** **/
  1144. /* returns an instance object */
  1145. xs *ins = xs_dict_new();
  1146. const char *host = xs_dict_get(srv_config, "host");
  1147. ins = xs_dict_append(ins, "uri", host);
  1148. ins = xs_dict_append(ins, "domain", host);
  1149. ins = xs_dict_append(ins, "title", host);
  1150. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  1151. ins = xs_dict_append(ins, "source_url", WHAT_IS_SNAC_URL);
  1152. ins = xs_dict_append(ins, "description", host);
  1153. ins = xs_dict_append(ins, "short_description", host);
  1154. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  1155. ins = xs_dict_append(ins, "thumbnail", susie);
  1156. const char *v = xs_dict_get(srv_config, "admin_email");
  1157. if (xs_is_null(v) || *v == '\0')
  1158. v = "admin@localhost";
  1159. ins = xs_dict_append(ins, "email", v);
  1160. xs *l1 = xs_list_new();
  1161. ins = xs_dict_append(ins, "rules", l1);
  1162. l1 = xs_list_append(l1, "en");
  1163. ins = xs_dict_append(ins, "languages", l1);
  1164. xs *d1 = xs_dict_new();
  1165. ins = xs_dict_append(ins, "urls", d1);
  1166. xs *z = xs_number_new(0);
  1167. d1 = xs_dict_append(d1, "user_count", z);
  1168. d1 = xs_dict_append(d1, "status_count", z);
  1169. d1 = xs_dict_append(d1, "domain_count", z);
  1170. ins = xs_dict_append(ins, "stats", d1);
  1171. xs *f = xs_val_new(XSTYPE_FALSE);
  1172. ins = xs_dict_append(ins, "registrations", f);
  1173. ins = xs_dict_append(ins, "approval_required", f);
  1174. ins = xs_dict_append(ins, "invites_enabled", f);
  1175. xs *cfg = xs_dict_new();
  1176. {
  1177. xs *d11 = xs_dict_new();
  1178. xs *mc = xs_number_new(100000);
  1179. xs *mm = xs_number_new(8);
  1180. xs *cr = xs_number_new(32);
  1181. d11 = xs_dict_append(d11, "max_characters", mc);
  1182. d11 = xs_dict_append(d11, "max_media_attachments", mm);
  1183. d11 = xs_dict_append(d11, "characters_reserved_per_url", cr);
  1184. cfg = xs_dict_append(cfg, "statuses", d11);
  1185. }
  1186. ins = xs_dict_append(ins, "configuration", cfg);
  1187. const char *admin_account = xs_dict_get(srv_config, "admin_account");
  1188. if (!xs_is_null(admin_account) && *admin_account) {
  1189. snac admin;
  1190. if (user_open(&admin, admin_account)) {
  1191. xs *actor = msg_actor(&admin);
  1192. xs *acct = mastoapi_account(actor);
  1193. ins = xs_dict_append(ins, "contact_account", acct);
  1194. user_free(&admin);
  1195. }
  1196. }
  1197. *body = xs_json_dumps_pp(ins, 4);
  1198. *ctype = "application/json";
  1199. status = 200;
  1200. }
  1201. else
  1202. if (xs_startswith(cmd, "/v1/statuses/")) { /** **/
  1203. /* information about a status */
  1204. if (logged_in) {
  1205. xs *l = xs_split(cmd, "/");
  1206. const char *id = xs_list_get(l, 3);
  1207. const char *op = xs_list_get(l, 4);
  1208. if (!xs_is_null(id)) {
  1209. xs *msg = NULL;
  1210. xs *out = NULL;
  1211. /* skip the 'fake' part of the id */
  1212. id = MID_TO_MD5(id);
  1213. if (valid_status(object_get_by_md5(id, &msg))) {
  1214. if (op == NULL) {
  1215. if (!is_muted(&snac1, xs_dict_get(msg, "attributedTo"))) {
  1216. /* return the status itself */
  1217. out = mastoapi_status(&snac1, msg);
  1218. }
  1219. }
  1220. else
  1221. if (strcmp(op, "context") == 0) { /** **/
  1222. /* return ancestors and children */
  1223. xs *anc = xs_list_new();
  1224. xs *des = xs_list_new();
  1225. xs_list *p;
  1226. xs_str *v;
  1227. char pid[64];
  1228. /* build the [grand]parent list, moving up */
  1229. strncpy(pid, id, sizeof(pid));
  1230. while (object_parent(pid, pid, sizeof(pid))) {
  1231. xs *m2 = NULL;
  1232. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  1233. xs *st = mastoapi_status(&snac1, m2);
  1234. anc = xs_list_insert(anc, 0, st);
  1235. }
  1236. else
  1237. break;
  1238. }
  1239. /* build the children list */
  1240. xs *children = object_children(xs_dict_get(msg, "id"));
  1241. p = children;
  1242. while (xs_list_iter(&p, &v)) {
  1243. xs *m2 = NULL;
  1244. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  1245. if (xs_is_null(xs_dict_get(m2, "name"))) {
  1246. xs *st = mastoapi_status(&snac1, m2);
  1247. des = xs_list_append(des, st);
  1248. }
  1249. }
  1250. }
  1251. out = xs_dict_new();
  1252. out = xs_dict_append(out, "ancestors", anc);
  1253. out = xs_dict_append(out, "descendants", des);
  1254. }
  1255. else
  1256. if (strcmp(op, "reblogged_by") == 0 || /** **/
  1257. strcmp(op, "favourited_by") == 0) { /** **/
  1258. /* return the list of people who liked or boosted this */
  1259. out = xs_list_new();
  1260. xs *l = NULL;
  1261. if (op[0] == 'r')
  1262. l = object_announces(xs_dict_get(msg, "id"));
  1263. else
  1264. l = object_likes(xs_dict_get(msg, "id"));
  1265. xs_list *p = l;
  1266. xs_str *v;
  1267. while (xs_list_iter(&p, &v)) {
  1268. xs *actor2 = NULL;
  1269. if (valid_status(object_get_by_md5(v, &actor2))) {
  1270. xs *acct2 = mastoapi_account(actor2);
  1271. out = xs_list_append(out, acct2);
  1272. }
  1273. }
  1274. }
  1275. }
  1276. else
  1277. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  1278. if (out != NULL) {
  1279. *body = xs_json_dumps_pp(out, 4);
  1280. *ctype = "application/json";
  1281. status = 200;
  1282. }
  1283. }
  1284. }
  1285. else
  1286. status = 401;
  1287. }
  1288. else
  1289. if (strcmp(cmd, "/v1/preferences") == 0) { /** **/
  1290. *body = xs_dup("{}");
  1291. *ctype = "application/json";
  1292. status = 200;
  1293. }
  1294. else
  1295. if (strcmp(cmd, "/v1/markers") == 0) { /** **/
  1296. *body = xs_dup("{}");
  1297. *ctype = "application/json";
  1298. status = 200;
  1299. }
  1300. else
  1301. if (strcmp(cmd, "/v1/followed_tags") == 0) { /** **/
  1302. *body = xs_dup("[]");
  1303. *ctype = "application/json";
  1304. status = 200;
  1305. }
  1306. else
  1307. if (strcmp(cmd, "/v2/search") == 0) { /** **/
  1308. if (logged_in) {
  1309. const char *q = xs_dict_get(args, "q");
  1310. const char *type = xs_dict_get(args, "type");
  1311. const char *offset = xs_dict_get(args, "offset");
  1312. xs *acl = xs_list_new();
  1313. xs *stl = xs_list_new();
  1314. xs *htl = xs_list_new();
  1315. xs *res = xs_dict_new();
  1316. if (xs_is_null(offset) || strcmp(offset, "0") == 0) {
  1317. /* reply something only for offset 0; otherwise,
  1318. apps like Tusky keep asking again and again */
  1319. if (!xs_is_null(q) && !xs_is_null(type) && strcmp(type, "accounts") == 0) {
  1320. /* do a webfinger query */
  1321. char *actor = NULL;
  1322. char *user = NULL;
  1323. if (valid_status(webfinger_request(q, &actor, &user))) {
  1324. xs *actor_o = NULL;
  1325. if (valid_status(actor_request(&snac1, actor, &actor_o))) {
  1326. xs *acct = mastoapi_account(actor_o);
  1327. acl = xs_list_append(acl, acct);
  1328. }
  1329. }
  1330. }
  1331. }
  1332. res = xs_dict_append(res, "accounts", acl);
  1333. res = xs_dict_append(res, "statuses", stl);
  1334. res = xs_dict_append(res, "hashtags", htl);
  1335. *body = xs_json_dumps_pp(res, 4);
  1336. *ctype = "application/json";
  1337. status = 200;
  1338. }
  1339. else
  1340. status = 401;
  1341. }
  1342. /* user cleanup */
  1343. if (logged_in)
  1344. user_free(&snac1);
  1345. return status;
  1346. }
  1347. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  1348. const char *payload, int p_size,
  1349. char **body, int *b_size, char **ctype)
  1350. {
  1351. (void)p_size;
  1352. (void)b_size;
  1353. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1354. return 0;
  1355. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1356. /* {
  1357. xs *j = xs_json_dumps_pp(req, 4);
  1358. printf("mastoapi post:\n%s\n", j);
  1359. }*/
  1360. int status = 404;
  1361. xs *args = NULL;
  1362. char *i_ctype = xs_dict_get(req, "content-type");
  1363. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1364. args = xs_json_loads(payload);
  1365. else
  1366. args = xs_dup(xs_dict_get(req, "p_vars"));
  1367. if (args == NULL)
  1368. return 400;
  1369. /* {
  1370. xs *j = xs_json_dumps_pp(args, 4);
  1371. printf("%s\n", j);
  1372. }*/
  1373. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1374. snac snac = {0};
  1375. int logged_in = process_auth_token(&snac, req);
  1376. if (strcmp(cmd, "/v1/apps") == 0) { /** **/
  1377. const char *name = xs_dict_get(args, "client_name");
  1378. const char *ruri = xs_dict_get(args, "redirect_uris");
  1379. const char *scope = xs_dict_get(args, "scope");
  1380. if (xs_type(ruri) == XSTYPE_LIST)
  1381. ruri = xs_dict_get(ruri, 0);
  1382. if (name && ruri) {
  1383. xs *app = xs_dict_new();
  1384. xs *id = xs_replace_i(tid(0), ".", "");
  1385. xs *csec = random_str();
  1386. xs *vkey = random_str();
  1387. xs *cid = NULL;
  1388. /* pick a non-existent random cid */
  1389. for (;;) {
  1390. cid = random_str();
  1391. xs *p_app = app_get(cid);
  1392. if (p_app == NULL)
  1393. break;
  1394. xs_free(cid);
  1395. }
  1396. app = xs_dict_append(app, "name", name);
  1397. app = xs_dict_append(app, "redirect_uri", ruri);
  1398. app = xs_dict_append(app, "client_id", cid);
  1399. app = xs_dict_append(app, "client_secret", csec);
  1400. app = xs_dict_append(app, "vapid_key", vkey);
  1401. app = xs_dict_append(app, "id", id);
  1402. *body = xs_json_dumps_pp(app, 4);
  1403. *ctype = "application/json";
  1404. status = 200;
  1405. app = xs_dict_append(app, "code", "");
  1406. if (scope)
  1407. app = xs_dict_append(app, "scope", scope);
  1408. app_add(cid, app);
  1409. srv_debug(1, xs_fmt("mastoapi apps: new app %s", cid));
  1410. }
  1411. }
  1412. else
  1413. if (strcmp(cmd, "/v1/statuses") == 0) { /** **/
  1414. if (logged_in) {
  1415. /* post a new Note */
  1416. /* {
  1417. xs *j = xs_json_dumps_pp(args, 4);
  1418. printf("%s\n", j);
  1419. }*/
  1420. const char *content = xs_dict_get(args, "status");
  1421. const char *mid = xs_dict_get(args, "in_reply_to_id");
  1422. const char *visibility = xs_dict_get(args, "visibility");
  1423. const char *summary = xs_dict_get(args, "spoiler_text");
  1424. const char *media_ids = xs_dict_get(args, "media_ids");
  1425. if (xs_is_null(media_ids))
  1426. media_ids = xs_dict_get(args, "media_ids[]");
  1427. if (xs_is_null(visibility))
  1428. visibility = "public";
  1429. xs *attach_list = xs_list_new();
  1430. xs *irt = NULL;
  1431. /* is it a reply? */
  1432. if (mid != NULL) {
  1433. xs *r_msg = NULL;
  1434. const char *md5 = MID_TO_MD5(mid);
  1435. if (valid_status(object_get_by_md5(md5, &r_msg)))
  1436. irt = xs_dup(xs_dict_get(r_msg, "id"));
  1437. }
  1438. /* does it have attachments? */
  1439. if (!xs_is_null(media_ids)) {
  1440. xs *mi = NULL;
  1441. if (xs_type(media_ids) == XSTYPE_LIST)
  1442. mi = xs_dup(media_ids);
  1443. else {
  1444. mi = xs_list_new();
  1445. mi = xs_list_append(mi, media_ids);
  1446. }
  1447. xs_list *p = mi;
  1448. xs_str *v;
  1449. while (xs_list_iter(&p, &v)) {
  1450. xs *l = xs_list_new();
  1451. xs *url = xs_fmt("%s/s/%s", snac.actor, v);
  1452. xs *desc = static_get_meta(&snac, v);
  1453. l = xs_list_append(l, url);
  1454. l = xs_list_append(l, desc);
  1455. attach_list = xs_list_append(attach_list, l);
  1456. }
  1457. }
  1458. /* prepare the message */
  1459. xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
  1460. strcmp(visibility, "public") == 0 ? 0 : 1);
  1461. if (!xs_is_null(summary) && *summary) {
  1462. xs *t = xs_val_new(XSTYPE_TRUE);
  1463. msg = xs_dict_set(msg, "sensitive", t);
  1464. msg = xs_dict_set(msg, "summary", summary);
  1465. }
  1466. /* store */
  1467. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1468. /* 'Create' message */
  1469. xs *c_msg = msg_create(&snac, msg);
  1470. enqueue_message(&snac, c_msg);
  1471. timeline_touch(&snac);
  1472. /* convert to a mastodon status as a response code */
  1473. xs *st = mastoapi_status(&snac, msg);
  1474. *body = xs_json_dumps_pp(st, 4);
  1475. *ctype = "application/json";
  1476. status = 200;
  1477. }
  1478. else
  1479. status = 401;
  1480. }
  1481. else
  1482. if (xs_startswith(cmd, "/v1/statuses")) { /** **/
  1483. if (logged_in) {
  1484. /* operations on a status */
  1485. xs *l = xs_split(cmd, "/");
  1486. const char *mid = xs_list_get(l, 3);
  1487. const char *op = xs_list_get(l, 4);
  1488. if (!xs_is_null(mid)) {
  1489. xs *msg = NULL;
  1490. xs *out = NULL;
  1491. /* skip the 'fake' part of the id */
  1492. mid = MID_TO_MD5(mid);
  1493. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1494. char *id = xs_dict_get(msg, "id");
  1495. if (op == NULL) {
  1496. /* no operation (?) */
  1497. }
  1498. else
  1499. if (strcmp(op, "favourite") == 0) { /** **/
  1500. xs *n_msg = msg_admiration(&snac, id, "Like");
  1501. if (n_msg != NULL) {
  1502. enqueue_message(&snac, n_msg);
  1503. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  1504. out = mastoapi_status(&snac, msg);
  1505. }
  1506. }
  1507. else
  1508. if (strcmp(op, "unfavourite") == 0) { /** **/
  1509. /* partial support: as the original Like message
  1510. is not stored anywhere here, it's not possible
  1511. to send an Undo + Like; the only thing done here
  1512. is to delete the actor from the list of likes */
  1513. object_unadmire(id, snac.actor, 1);
  1514. }
  1515. else
  1516. if (strcmp(op, "reblog") == 0) { /** **/
  1517. xs *n_msg = msg_admiration(&snac, id, "Announce");
  1518. if (n_msg != NULL) {
  1519. enqueue_message(&snac, n_msg);
  1520. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  1521. out = mastoapi_status(&snac, msg);
  1522. }
  1523. }
  1524. else
  1525. if (strcmp(op, "unreblog") == 0) { /** **/
  1526. /* partial support: see comment in 'unfavourite' */
  1527. object_unadmire(id, snac.actor, 0);
  1528. }
  1529. else
  1530. if (strcmp(op, "bookmark") == 0) { /** **/
  1531. /* snac does not support bookmarks */
  1532. }
  1533. else
  1534. if (strcmp(op, "unbookmark") == 0) { /** **/
  1535. /* snac does not support bookmarks */
  1536. }
  1537. else
  1538. if (strcmp(op, "pin") == 0) { /** **/
  1539. /* snac does not support pinning */
  1540. }
  1541. else
  1542. if (strcmp(op, "unpin") == 0) { /** **/
  1543. /* snac does not support pinning */
  1544. }
  1545. else
  1546. if (strcmp(op, "mute") == 0) { /** **/
  1547. /* Mastodon's mute is snac's hide */
  1548. }
  1549. else
  1550. if (strcmp(op, "unmute") == 0) { /** **/
  1551. /* Mastodon's unmute is snac's unhide */
  1552. }
  1553. }
  1554. if (out != NULL) {
  1555. *body = xs_json_dumps_pp(out, 4);
  1556. *ctype = "application/json";
  1557. status = 200;
  1558. }
  1559. }
  1560. }
  1561. else
  1562. status = 401;
  1563. }
  1564. else
  1565. if (strcmp(cmd, "/v1/notifications/clear") == 0) { /** **/
  1566. if (logged_in) {
  1567. notify_clear(&snac);
  1568. timeline_touch(&snac);
  1569. *body = xs_dup("{}");
  1570. *ctype = "application/json";
  1571. status = 200;
  1572. }
  1573. else
  1574. status = 401;
  1575. }
  1576. else
  1577. if (strcmp(cmd, "/v1/push/subscription") == 0) { /** **/
  1578. /* I don't know what I'm doing */
  1579. if (logged_in) {
  1580. char *v;
  1581. xs *wpush = xs_dict_new();
  1582. wpush = xs_dict_append(wpush, "id", "1");
  1583. v = xs_dict_get(args, "data");
  1584. v = xs_dict_get(v, "alerts");
  1585. wpush = xs_dict_append(wpush, "alerts", v);
  1586. v = xs_dict_get(args, "subscription");
  1587. v = xs_dict_get(v, "endpoint");
  1588. wpush = xs_dict_append(wpush, "endpoint", v);
  1589. xs *server_key = random_str();
  1590. wpush = xs_dict_append(wpush, "server_key", server_key);
  1591. *body = xs_json_dumps_pp(wpush, 4);
  1592. *ctype = "application/json";
  1593. status = 200;
  1594. }
  1595. else
  1596. status = 401;
  1597. }
  1598. else
  1599. if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { /** **/
  1600. if (logged_in) {
  1601. /* {
  1602. xs *j = xs_json_dumps_pp(args, 4);
  1603. printf("%s\n", j);
  1604. }*/
  1605. const xs_list *file = xs_dict_get(args, "file");
  1606. const char *desc = xs_dict_get(args, "description");
  1607. if (xs_is_null(desc))
  1608. desc = "";
  1609. status = 400;
  1610. if (xs_type(file) == XSTYPE_LIST) {
  1611. const char *fn = xs_list_get(file, 0);
  1612. if (*fn != '\0') {
  1613. char *ext = strrchr(fn, '.');
  1614. xs *hash = xs_md5_hex(fn, strlen(fn));
  1615. xs *id = xs_fmt("%s%s", hash, ext);
  1616. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1617. int fo = xs_number_get(xs_list_get(file, 1));
  1618. int fs = xs_number_get(xs_list_get(file, 2));
  1619. /* store */
  1620. static_put(&snac, id, payload + fo, fs);
  1621. static_put_meta(&snac, id, desc);
  1622. /* prepare a response */
  1623. xs *rsp = xs_dict_new();
  1624. rsp = xs_dict_append(rsp, "id", id);
  1625. rsp = xs_dict_append(rsp, "type", "image");
  1626. rsp = xs_dict_append(rsp, "url", url);
  1627. rsp = xs_dict_append(rsp, "preview_url", url);
  1628. rsp = xs_dict_append(rsp, "remote_url", url);
  1629. rsp = xs_dict_append(rsp, "description", desc);
  1630. *body = xs_json_dumps_pp(rsp, 4);
  1631. *ctype = "application/json";
  1632. status = 200;
  1633. }
  1634. }
  1635. }
  1636. else
  1637. status = 401;
  1638. }
  1639. else
  1640. if (xs_startswith(cmd, "/v1/accounts")) { /** **/
  1641. if (logged_in) {
  1642. /* account-related information */
  1643. xs *l = xs_split(cmd, "/");
  1644. const char *md5 = xs_list_get(l, 3);
  1645. const char *opt = xs_list_get(l, 4);
  1646. xs *rsp = NULL;
  1647. if (!xs_is_null(md5) && *md5) {
  1648. xs *actor_o = NULL;
  1649. if (xs_is_null(opt)) {
  1650. /* ? */
  1651. }
  1652. else
  1653. if (strcmp(opt, "follow") == 0) { /** **/
  1654. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1655. const char *actor = xs_dict_get(actor_o, "id");
  1656. xs *msg = msg_follow(&snac, actor);
  1657. if (msg != NULL) {
  1658. /* reload the actor from the message, in may be different */
  1659. actor = xs_dict_get(msg, "object");
  1660. following_add(&snac, actor, msg);
  1661. enqueue_output_by_actor(&snac, msg, actor, 0);
  1662. rsp = mastoapi_relationship(&snac, md5);
  1663. }
  1664. }
  1665. }
  1666. else
  1667. if (strcmp(opt, "unfollow") == 0) { /** **/
  1668. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1669. const char *actor = xs_dict_get(actor_o, "id");
  1670. /* get the following object */
  1671. xs *object = NULL;
  1672. if (valid_status(following_get(&snac, actor, &object))) {
  1673. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  1674. following_del(&snac, actor);
  1675. enqueue_output_by_actor(&snac, msg, actor, 0);
  1676. rsp = mastoapi_relationship(&snac, md5);
  1677. }
  1678. }
  1679. }
  1680. else
  1681. if (strcmp(opt, "block") == 0) { /** **/
  1682. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1683. const char *actor = xs_dict_get(actor_o, "id");
  1684. mute(&snac, actor);
  1685. rsp = mastoapi_relationship(&snac, md5);
  1686. }
  1687. }
  1688. else
  1689. if (strcmp(opt, "unblock") == 0) { /** **/
  1690. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1691. const char *actor = xs_dict_get(actor_o, "id");
  1692. unmute(&snac, actor);
  1693. rsp = mastoapi_relationship(&snac, md5);
  1694. }
  1695. }
  1696. }
  1697. if (rsp != NULL) {
  1698. *body = xs_json_dumps_pp(rsp, 4);
  1699. *ctype = "application/json";
  1700. status = 200;
  1701. }
  1702. }
  1703. else
  1704. status = 401;
  1705. }
  1706. else
  1707. if (xs_startswith(cmd, "/v1/polls")) { /** **/
  1708. if (logged_in) {
  1709. /* operations on a status */
  1710. xs *l = xs_split(cmd, "/");
  1711. const char *mid = xs_list_get(l, 3);
  1712. const char *op = xs_list_get(l, 4);
  1713. if (!xs_is_null(mid)) {
  1714. xs *msg = NULL;
  1715. xs *out = NULL;
  1716. /* skip the 'fake' part of the id */
  1717. mid = MID_TO_MD5(mid);
  1718. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1719. const char *id = xs_dict_get(msg, "id");
  1720. const char *atto = xs_dict_get(msg, "attributedTo");
  1721. xs_list *opts = xs_dict_get(msg, "oneOf");
  1722. if (opts == NULL)
  1723. opts = xs_dict_get(msg, "anyOf");
  1724. if (op == NULL) {
  1725. }
  1726. else
  1727. if (strcmp(op, "votes") == 0) {
  1728. xs_list *choices = xs_dict_get(args, "choices[]");
  1729. if (xs_type(choices) == XSTYPE_LIST) {
  1730. xs_str *v;
  1731. while (xs_list_iter(&choices, &v)) {
  1732. int io = atoi(v);
  1733. const xs_dict *o = xs_list_get(opts, io);
  1734. if (o) {
  1735. const char *name = xs_dict_get(o, "name");
  1736. xs *msg = msg_note(&snac, "", atto, (char *)id, NULL, 1);
  1737. msg = xs_dict_append(msg, "name", name);
  1738. xs *c_msg = msg_create(&snac, msg);
  1739. enqueue_message(&snac, c_msg);
  1740. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1741. }
  1742. }
  1743. out = mastoapi_poll(&snac, msg);
  1744. }
  1745. }
  1746. }
  1747. if (out != NULL) {
  1748. *body = xs_json_dumps_pp(out, 4);
  1749. *ctype = "application/json";
  1750. status = 200;
  1751. }
  1752. }
  1753. }
  1754. else
  1755. status = 401;
  1756. }
  1757. /* user cleanup */
  1758. if (logged_in)
  1759. user_free(&snac);
  1760. return status;
  1761. }
  1762. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  1763. const char *payload, int p_size,
  1764. char **body, int *b_size, char **ctype)
  1765. {
  1766. (void)p_size;
  1767. (void)b_size;
  1768. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1769. return 0;
  1770. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1771. /* {
  1772. xs *j = xs_json_dumps_pp(req, 4);
  1773. printf("mastoapi put:\n%s\n", j);
  1774. }*/
  1775. int status = 404;
  1776. xs *args = NULL;
  1777. char *i_ctype = xs_dict_get(req, "content-type");
  1778. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1779. args = xs_json_loads(payload);
  1780. else
  1781. args = xs_dup(xs_dict_get(req, "p_vars"));
  1782. if (args == NULL)
  1783. return 400;
  1784. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1785. snac snac = {0};
  1786. int logged_in = process_auth_token(&snac, req);
  1787. if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) { /** **/
  1788. if (logged_in) {
  1789. xs *l = xs_split(cmd, "/");
  1790. const char *stid = xs_list_get(l, 3);
  1791. if (!xs_is_null(stid)) {
  1792. const char *desc = xs_dict_get(args, "description");
  1793. /* set the image metadata */
  1794. static_put_meta(&snac, stid, desc);
  1795. /* prepare a response */
  1796. xs *rsp = xs_dict_new();
  1797. xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
  1798. rsp = xs_dict_append(rsp, "id", stid);
  1799. rsp = xs_dict_append(rsp, "type", "image");
  1800. rsp = xs_dict_append(rsp, "url", url);
  1801. rsp = xs_dict_append(rsp, "preview_url", url);
  1802. rsp = xs_dict_append(rsp, "remote_url", url);
  1803. rsp = xs_dict_append(rsp, "description", desc);
  1804. *body = xs_json_dumps_pp(rsp, 4);
  1805. *ctype = "application/json";
  1806. status = 200;
  1807. }
  1808. }
  1809. else
  1810. status = 401;
  1811. }
  1812. /* user cleanup */
  1813. if (logged_in)
  1814. user_free(&snac);
  1815. return status;
  1816. }
  1817. void mastoapi_purge(void)
  1818. {
  1819. xs *spec = xs_fmt("%s/app/" "*.json", srv_basedir);
  1820. xs *files = xs_glob(spec, 1, 0);
  1821. xs_list *p = files;
  1822. xs_str *v;
  1823. time_t mt = time(NULL) - 3600;
  1824. while (xs_list_iter(&p, &v)) {
  1825. xs *cid = xs_replace(v, ".json", "");
  1826. xs *fn = _app_fn(cid);
  1827. if (mtime(fn) < mt) {
  1828. /* get the app */
  1829. xs *app = app_get(cid);
  1830. if (app) {
  1831. /* old apps with no uid are incomplete cruft */
  1832. const char *uid = xs_dict_get(app, "uid");
  1833. if (xs_is_null(uid) || *uid == '\0') {
  1834. unlink(fn);
  1835. srv_debug(2, xs_fmt("purged %s", fn));
  1836. }
  1837. }
  1838. }
  1839. }
  1840. }
  1841. #endif /* #ifndef NO_MASTODON_API */