mastoapi.c 83 KB

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