mastoapi.c 80 KB

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