mastoapi.c 82 KB

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