mastoapi.c 80 KB

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