mastoapi.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520
  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. int status = 404;
  816. xs_dict *args = xs_dict_get(req, "q_vars");
  817. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  818. snac snac1 = {0};
  819. int logged_in = process_auth_token(&snac1, req);
  820. if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) { /** **/
  821. if (logged_in) {
  822. xs *acct = xs_dict_new();
  823. acct = xs_dict_append(acct, "id", snac1.md5);
  824. acct = xs_dict_append(acct, "username", xs_dict_get(snac1.config, "uid"));
  825. acct = xs_dict_append(acct, "acct", xs_dict_get(snac1.config, "uid"));
  826. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac1.config, "name"));
  827. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac1.config, "published"));
  828. acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio"));
  829. acct = xs_dict_append(acct, "url", snac1.actor);
  830. acct = xs_dict_append(acct, "header", "");
  831. xs *src = xs_json_loads("{\"privacy\":\"public\","
  832. "\"sensitive\":false,\"fields\":[],\"note\":\"\"}");
  833. acct = xs_dict_append(acct, "source", src);
  834. xs *avatar = NULL;
  835. char *av = xs_dict_get(snac1.config, "avatar");
  836. if (xs_is_null(av) || *av == '\0')
  837. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  838. else
  839. avatar = xs_dup(av);
  840. acct = xs_dict_append(acct, "avatar", avatar);
  841. acct = xs_dict_append(acct, "avatar_static", avatar);
  842. xs_dict *metadata = xs_dict_get(snac1.config, "metadata");
  843. if (xs_type(metadata) == XSTYPE_DICT) {
  844. xs *fields = xs_list_new();
  845. xs_str *k;
  846. xs_str *v;
  847. while (xs_dict_iter(&metadata, &k, &v)) {
  848. xs *d = xs_dict_new();
  849. d = xs_dict_append(d, "name", k);
  850. d = xs_dict_append(d, "value", v);
  851. d = xs_dict_append(d, "verified_at", xs_stock_null);
  852. fields = xs_list_append(fields, d);
  853. }
  854. acct = xs_dict_set(acct, "fields", fields);
  855. }
  856. *body = xs_json_dumps(acct, 4);
  857. *ctype = "application/json";
  858. status = 200;
  859. }
  860. else {
  861. status = 422; // "Unprocessable entity" (no login)
  862. }
  863. }
  864. else
  865. if (strcmp(cmd, "/v1/accounts/relationships") == 0) { /** **/
  866. /* find if an account is followed, blocked, etc. */
  867. /* the account to get relationships about is in args "id[]" */
  868. if (logged_in) {
  869. xs *res = xs_list_new();
  870. const char *md5 = xs_dict_get(args, "id[]");
  871. if (xs_is_null(md5))
  872. md5 = xs_dict_get(args, "id");
  873. if (!xs_is_null(md5)) {
  874. if (xs_type(md5) == XSTYPE_LIST)
  875. md5 = xs_list_get(md5, 0);
  876. xs *rel = mastoapi_relationship(&snac1, md5);
  877. if (rel != NULL)
  878. res = xs_list_append(res, rel);
  879. }
  880. *body = xs_json_dumps(res, 4);
  881. *ctype = "application/json";
  882. status = 200;
  883. }
  884. else
  885. status = 422;
  886. }
  887. else
  888. if (strcmp(cmd, "/v1/accounts/lookup") == 0) { /** **/
  889. /* lookup an account */
  890. char *acct = xs_dict_get(args, "acct");
  891. if (!xs_is_null(acct)) {
  892. xs *s = xs_strip_chars_i(xs_dup(acct), "@");
  893. xs *l = xs_split_n(s, "@", 1);
  894. char *uid = xs_list_get(l, 0);
  895. char *host = xs_list_get(l, 1);
  896. if (uid && (!host || strcmp(host, xs_dict_get(srv_config, "host")) == 0)) {
  897. snac user;
  898. if (user_open(&user, uid)) {
  899. xs *actor = msg_actor(&user);
  900. xs *macct = mastoapi_account(actor);
  901. *body = xs_json_dumps(macct, 4);
  902. *ctype = "application/json";
  903. status = 200;
  904. user_free(&user);
  905. }
  906. }
  907. }
  908. }
  909. else
  910. if (xs_startswith(cmd, "/v1/accounts/")) { /** **/
  911. /* account-related information */
  912. xs *l = xs_split(cmd, "/");
  913. const char *uid = xs_list_get(l, 3);
  914. const char *opt = xs_list_get(l, 4);
  915. if (uid != NULL) {
  916. snac snac2;
  917. xs *out = NULL;
  918. xs *actor = NULL;
  919. if (logged_in && strcmp(uid, "search") == 0) { /** **/
  920. /* search for accounts starting with q */
  921. const char *aq = xs_dict_get(args, "q");
  922. if (!xs_is_null(aq)) {
  923. xs *q = xs_tolower_i(xs_dup(aq));
  924. out = xs_list_new();
  925. xs *wing = following_list(&snac1);
  926. xs *wers = follower_list(&snac1);
  927. xs *ulst = user_list();
  928. xs_list *p;
  929. xs_str *v;
  930. xs_set seen;
  931. xs_set_init(&seen);
  932. /* user relations */
  933. xs_list *lsts[] = { wing, wers, NULL };
  934. int n;
  935. for (n = 0; (p = lsts[n]) != NULL; n++) {
  936. while (xs_list_iter(&p, &v)) {
  937. /* already seen? skip */
  938. if (xs_set_add(&seen, v) == 0)
  939. continue;
  940. xs *actor = NULL;
  941. if (valid_status(object_get(v, &actor))) {
  942. const char *uname = xs_dict_get(actor, "preferredUsername");
  943. if (!xs_is_null(uname)) {
  944. xs *luname = xs_tolower_i(xs_dup(uname));
  945. if (xs_startswith(luname, q)) {
  946. xs *acct = mastoapi_account(actor);
  947. out = xs_list_append(out, acct);
  948. }
  949. }
  950. }
  951. }
  952. }
  953. /* local users */
  954. p = ulst;
  955. while (xs_list_iter(&p, &v)) {
  956. snac user;
  957. /* skip this same user */
  958. if (strcmp(v, xs_dict_get(snac1.config, "uid")) == 0)
  959. continue;
  960. /* skip if the uid does not start with the query */
  961. xs *v2 = xs_tolower_i(xs_dup(v));
  962. if (!xs_startswith(v2, q))
  963. continue;
  964. if (user_open(&user, v)) {
  965. /* if it's not already seen, add it */
  966. if (xs_set_add(&seen, user.actor) == 1) {
  967. xs *actor = msg_actor(&user);
  968. xs *acct = mastoapi_account(actor);
  969. out = xs_list_append(out, acct);
  970. }
  971. user_free(&user);
  972. }
  973. }
  974. xs_set_free(&seen);
  975. }
  976. }
  977. else
  978. /* is it a local user? */
  979. if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
  980. if (opt == NULL) {
  981. /* account information */
  982. actor = msg_actor(&snac2);
  983. out = mastoapi_account(actor);
  984. }
  985. else
  986. if (strcmp(opt, "statuses") == 0) { /** **/
  987. /* the public list of posts of a user */
  988. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  989. xs_list *p = timeline;
  990. xs_str *v;
  991. out = xs_list_new();
  992. while (xs_list_iter(&p, &v)) {
  993. xs *msg = NULL;
  994. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  995. /* add only posts by the author */
  996. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  997. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  998. xs *st = mastoapi_status(&snac2, msg);
  999. if (st)
  1000. out = xs_list_append(out, st);
  1001. }
  1002. }
  1003. }
  1004. }
  1005. user_free(&snac2);
  1006. }
  1007. else {
  1008. /* try the uid as the md5 of a possibly loaded actor */
  1009. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  1010. if (opt == NULL) {
  1011. /* account information */
  1012. out = mastoapi_account(actor);
  1013. }
  1014. else
  1015. if (strcmp(opt, "statuses") == 0) {
  1016. /* we don't serve statuses of others; return the empty list */
  1017. out = xs_list_new();
  1018. }
  1019. }
  1020. }
  1021. if (out != NULL) {
  1022. *body = xs_json_dumps(out, 4);
  1023. *ctype = "application/json";
  1024. status = 200;
  1025. }
  1026. }
  1027. }
  1028. else
  1029. if (strcmp(cmd, "/v1/timelines/home") == 0) { /** **/
  1030. /* the private timeline */
  1031. if (logged_in) {
  1032. const char *max_id = xs_dict_get(args, "max_id");
  1033. const char *since_id = xs_dict_get(args, "since_id");
  1034. const char *min_id = xs_dict_get(args, "min_id");
  1035. const char *limit_s = xs_dict_get(args, "limit");
  1036. int limit = 0;
  1037. int cnt = 0;
  1038. if (!xs_is_null(limit_s))
  1039. limit = atoi(limit_s);
  1040. if (limit == 0)
  1041. limit = 20;
  1042. xs *timeline = timeline_simple_list(&snac1, "private", 0, 2048);
  1043. xs *out = xs_list_new();
  1044. xs_list *p = timeline;
  1045. xs_str *v;
  1046. while (xs_list_iter(&p, &v) && cnt < limit) {
  1047. xs *msg = NULL;
  1048. /* only return entries older that max_id */
  1049. if (max_id) {
  1050. if (strcmp(v, MID_TO_MD5(max_id)) == 0)
  1051. max_id = NULL;
  1052. continue;
  1053. }
  1054. /* only returns entries newer than since_id */
  1055. if (since_id) {
  1056. if (strcmp(v, MID_TO_MD5(since_id)) == 0)
  1057. break;
  1058. }
  1059. /* only returns entries newer than min_id */
  1060. /* what does really "Return results immediately newer than ID" mean? */
  1061. if (min_id) {
  1062. if (strcmp(v, MID_TO_MD5(min_id)) == 0)
  1063. break;
  1064. }
  1065. /* get the entry */
  1066. if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
  1067. continue;
  1068. /* discard non-Notes */
  1069. const char *id = xs_dict_get(msg, "id");
  1070. const char *type = xs_dict_get(msg, "type");
  1071. if (!xs_match(type, "Note|Question|Page|Article"))
  1072. continue;
  1073. const char *from;
  1074. if (strcmp(type, "Page") == 0)
  1075. from = xs_dict_get(msg, "audience");
  1076. else
  1077. from = xs_dict_get(msg, "attributedTo");
  1078. /* is this message from a person we don't follow? */
  1079. if (strcmp(from, snac1.actor) && !following_check(&snac1, from)) {
  1080. /* discard if it was not boosted */
  1081. xs *idx = object_announces(id);
  1082. if (xs_list_len(idx) == 0)
  1083. continue;
  1084. }
  1085. /* discard notes from muted morons */
  1086. if (is_muted(&snac1, from))
  1087. continue;
  1088. /* discard hidden notes */
  1089. if (is_hidden(&snac1, id))
  1090. continue;
  1091. /* discard poll votes (they have a name) */
  1092. if (strcmp(type, "Page") != 0 && !xs_is_null(xs_dict_get(msg, "name")))
  1093. continue;
  1094. /* convert the Note into a Mastodon status */
  1095. xs *st = mastoapi_status(&snac1, msg);
  1096. if (st != NULL)
  1097. out = xs_list_append(out, st);
  1098. cnt++;
  1099. }
  1100. *body = xs_json_dumps(out, 4);
  1101. *ctype = "application/json";
  1102. status = 200;
  1103. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  1104. }
  1105. else {
  1106. status = 401; // unauthorized
  1107. }
  1108. }
  1109. else
  1110. if (strcmp(cmd, "/v1/timelines/public") == 0) { /** **/
  1111. /* the instance public timeline (public timelines for all users) */
  1112. const char *limit_s = xs_dict_get(args, "limit");
  1113. int limit = 0;
  1114. int cnt = 0;
  1115. if (!xs_is_null(limit_s))
  1116. limit = atoi(limit_s);
  1117. if (limit == 0)
  1118. limit = 20;
  1119. xs *timeline = timeline_instance_list(0, limit);
  1120. xs *out = xs_list_new();
  1121. xs_list *p = timeline;
  1122. xs_str *md5;
  1123. snac *user = NULL;
  1124. if (logged_in)
  1125. user = &snac1;
  1126. while (xs_list_iter(&p, &md5) && cnt < limit) {
  1127. xs *msg = NULL;
  1128. /* get the entry */
  1129. if (!valid_status(object_get_by_md5(md5, &msg)))
  1130. continue;
  1131. /* discard non-Notes */
  1132. const char *type = xs_dict_get(msg, "type");
  1133. if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0)
  1134. continue;
  1135. /* discard private users */
  1136. {
  1137. const char *atto = xs_dict_get(msg, "attributedTo");
  1138. xs *l = xs_split(atto, "/");
  1139. const char *uid = xs_list_get(l, -1);
  1140. snac p_user;
  1141. int skip = 1;
  1142. if (uid && user_open(&p_user, uid)) {
  1143. if (xs_type(xs_dict_get(p_user.config, "private")) != XSTYPE_TRUE)
  1144. skip = 0;
  1145. user_free(&p_user);
  1146. }
  1147. if (skip)
  1148. continue;
  1149. }
  1150. /* convert the Note into a Mastodon status */
  1151. xs *st = mastoapi_status(user, msg);
  1152. if (st != NULL) {
  1153. out = xs_list_append(out, st);
  1154. cnt++;
  1155. }
  1156. }
  1157. *body = xs_json_dumps(out, 4);
  1158. *ctype = "application/json";
  1159. status = 200;
  1160. }
  1161. else
  1162. if (strcmp(cmd, "/v1/conversations") == 0) { /** **/
  1163. /* TBD */
  1164. *body = xs_dup("[]");
  1165. *ctype = "application/json";
  1166. status = 200;
  1167. }
  1168. else
  1169. if (strcmp(cmd, "/v1/notifications") == 0) { /** **/
  1170. if (logged_in) {
  1171. xs *l = notify_list(&snac1, 0);
  1172. xs *out = xs_list_new();
  1173. xs_list *p = l;
  1174. xs_dict *v;
  1175. xs_list *excl = xs_dict_get(args, "exclude_types[]");
  1176. while (xs_list_iter(&p, &v)) {
  1177. xs *noti = notify_get(&snac1, v);
  1178. if (noti == NULL)
  1179. continue;
  1180. const char *type = xs_dict_get(noti, "type");
  1181. const char *utype = xs_dict_get(noti, "utype");
  1182. const char *objid = xs_dict_get(noti, "objid");
  1183. xs *actor = NULL;
  1184. xs *entry = NULL;
  1185. if (!valid_status(actor_get(xs_dict_get(noti, "actor"), &actor)))
  1186. continue;
  1187. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  1188. continue;
  1189. if (is_hidden(&snac1, objid))
  1190. continue;
  1191. /* convert the type */
  1192. if (strcmp(type, "Like") == 0)
  1193. type = "favourite";
  1194. else
  1195. if (strcmp(type, "Announce") == 0)
  1196. type = "reblog";
  1197. else
  1198. if (strcmp(type, "Follow") == 0)
  1199. type = "follow";
  1200. else
  1201. if (strcmp(type, "Create") == 0)
  1202. type = "mention";
  1203. else
  1204. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  1205. type = "poll";
  1206. else
  1207. continue;
  1208. /* excluded type? */
  1209. if (!xs_is_null(excl) && xs_list_in(excl, type) != -1)
  1210. continue;
  1211. xs *mn = xs_dict_new();
  1212. mn = xs_dict_append(mn, "type", type);
  1213. xs *id = xs_replace(xs_dict_get(noti, "id"), ".", "");
  1214. mn = xs_dict_append(mn, "id", id);
  1215. mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
  1216. xs *acct = mastoapi_account(actor);
  1217. mn = xs_dict_append(mn, "account", acct);
  1218. if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
  1219. xs *st = mastoapi_status(&snac1, entry);
  1220. if (st)
  1221. mn = xs_dict_append(mn, "status", st);
  1222. }
  1223. out = xs_list_append(out, mn);
  1224. }
  1225. *body = xs_json_dumps(out, 4);
  1226. *ctype = "application/json";
  1227. status = 200;
  1228. }
  1229. else
  1230. status = 401;
  1231. }
  1232. else
  1233. if (strcmp(cmd, "/v1/filters") == 0) { /** **/
  1234. /* snac will never have filters */
  1235. *body = xs_dup("[]");
  1236. *ctype = "application/json";
  1237. status = 200;
  1238. }
  1239. else
  1240. if (strcmp(cmd, "/v1/favourites") == 0) { /** **/
  1241. /* snac will never support a list of favourites */
  1242. *body = xs_dup("[]");
  1243. *ctype = "application/json";
  1244. status = 200;
  1245. }
  1246. else
  1247. if (strcmp(cmd, "/v1/bookmarks") == 0) { /** **/
  1248. /* snac does not support bookmarks */
  1249. *body = xs_dup("[]");
  1250. *ctype = "application/json";
  1251. status = 200;
  1252. }
  1253. else
  1254. if (strcmp(cmd, "/v1/lists") == 0) { /** **/
  1255. /* snac does not support lists */
  1256. *body = xs_dup("[]");
  1257. *ctype = "application/json";
  1258. status = 200;
  1259. }
  1260. else
  1261. if (strcmp(cmd, "/v1/scheduled_statuses") == 0) { /** **/
  1262. /* snac does not schedule notes */
  1263. *body = xs_dup("[]");
  1264. *ctype = "application/json";
  1265. status = 200;
  1266. }
  1267. else
  1268. if (strcmp(cmd, "/v1/follow_requests") == 0) { /** **/
  1269. /* snac does not support optional follow confirmations */
  1270. *body = xs_dup("[]");
  1271. *ctype = "application/json";
  1272. status = 200;
  1273. }
  1274. else
  1275. if (strcmp(cmd, "/v1/announcements") == 0) { /** **/
  1276. /* snac has no announcements (yet?) */
  1277. *body = xs_dup("[]");
  1278. *ctype = "application/json";
  1279. status = 200;
  1280. }
  1281. else
  1282. if (strcmp(cmd, "/v1/custom_emojis") == 0) { /** **/
  1283. /* are you kidding me? */
  1284. *body = xs_dup("[]");
  1285. *ctype = "application/json";
  1286. status = 200;
  1287. }
  1288. else
  1289. if (strcmp(cmd, "/v1/instance") == 0) { /** **/
  1290. /* returns an instance object */
  1291. xs *ins = xs_dict_new();
  1292. const char *host = xs_dict_get(srv_config, "host");
  1293. const char *title = xs_dict_get(srv_config, "title");
  1294. const char *sdesc = xs_dict_get(srv_config, "short_description");
  1295. ins = xs_dict_append(ins, "uri", host);
  1296. ins = xs_dict_append(ins, "domain", host);
  1297. ins = xs_dict_append(ins, "title", title && *title ? title : host);
  1298. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  1299. ins = xs_dict_append(ins, "source_url", WHAT_IS_SNAC_URL);
  1300. ins = xs_dict_append(ins, "description", host);
  1301. ins = xs_dict_append(ins, "short_description", sdesc && *sdesc ? sdesc : host);
  1302. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  1303. ins = xs_dict_append(ins, "thumbnail", susie);
  1304. const char *v = xs_dict_get(srv_config, "admin_email");
  1305. if (xs_is_null(v) || *v == '\0')
  1306. v = "admin@localhost";
  1307. ins = xs_dict_append(ins, "email", v);
  1308. ins = xs_dict_append(ins, "rules", xs_stock_list);
  1309. xs *l1 = xs_list_append(xs_list_new(), "en");
  1310. ins = xs_dict_append(ins, "languages", l1);
  1311. ins = xs_dict_append(ins, "urls", xs_stock_dict);
  1312. xs *d2 = xs_dict_append(xs_dict_new(), "user_count", xs_stock_0);
  1313. d2 = xs_dict_append(d2, "status_count", xs_stock_0);
  1314. d2 = xs_dict_append(d2, "domain_count", xs_stock_0);
  1315. ins = xs_dict_append(ins, "stats", d2);
  1316. ins = xs_dict_append(ins, "registrations", xs_stock_false);
  1317. ins = xs_dict_append(ins, "approval_required", xs_stock_false);
  1318. ins = xs_dict_append(ins, "invites_enabled", xs_stock_false);
  1319. xs *cfg = xs_dict_new();
  1320. {
  1321. xs *d11 = xs_json_loads("{\"characters_reserved_per_url\":32,"
  1322. "\"max_characters\":100000,\"max_media_attachments\":8}");
  1323. cfg = xs_dict_append(cfg, "statuses", d11);
  1324. xs *d12 = xs_json_loads("{\"max_featured_tags\":10}");
  1325. cfg = xs_dict_append(cfg, "accounts", d12);
  1326. xs *d13 = xs_json_loads("{\"image_matrix_limit\":33177600,"
  1327. "\"image_size_limit\":16777216,"
  1328. "\"video_frame_rate_limit\":120,"
  1329. "\"video_matrix_limit\":8294400,"
  1330. "\"video_size_limit\":103809024}"
  1331. );
  1332. {
  1333. /* get the supported mime types from the internal list */
  1334. const char **p = xs_mime_types;
  1335. xs_set mtypes;
  1336. xs_set_init(&mtypes);
  1337. while (*p) {
  1338. const char *type = p[1];
  1339. if (xs_startswith(type, "image/") ||
  1340. xs_startswith(type, "video/") ||
  1341. xs_startswith(type, "audio/"))
  1342. xs_set_add(&mtypes, type);
  1343. p += 2;
  1344. }
  1345. xs *l = xs_set_result(&mtypes);
  1346. d13 = xs_dict_append(d13, "supported_mime_types", l);
  1347. }
  1348. cfg = xs_dict_append(cfg, "media_attachments", d13);
  1349. xs *d14 = xs_json_loads("{\"max_characters_per_option\":50,"
  1350. "\"max_expiration\":2629746,"
  1351. "\"max_options\":8,\"min_expiration\":300}");
  1352. cfg = xs_dict_append(cfg, "polls", d14);
  1353. }
  1354. ins = xs_dict_append(ins, "configuration", cfg);
  1355. const char *admin_account = xs_dict_get(srv_config, "admin_account");
  1356. if (!xs_is_null(admin_account) && *admin_account) {
  1357. snac admin;
  1358. if (user_open(&admin, admin_account)) {
  1359. xs *actor = msg_actor(&admin);
  1360. xs *acct = mastoapi_account(actor);
  1361. ins = xs_dict_append(ins, "contact_account", acct);
  1362. user_free(&admin);
  1363. }
  1364. }
  1365. *body = xs_json_dumps(ins, 4);
  1366. *ctype = "application/json";
  1367. status = 200;
  1368. }
  1369. else
  1370. if (xs_startswith(cmd, "/v1/statuses/")) { /** **/
  1371. /* information about a status */
  1372. if (logged_in) {
  1373. xs *l = xs_split(cmd, "/");
  1374. const char *id = xs_list_get(l, 3);
  1375. const char *op = xs_list_get(l, 4);
  1376. if (!xs_is_null(id)) {
  1377. xs *msg = NULL;
  1378. xs *out = NULL;
  1379. /* skip the 'fake' part of the id */
  1380. id = MID_TO_MD5(id);
  1381. if (valid_status(object_get_by_md5(id, &msg))) {
  1382. if (op == NULL) {
  1383. if (!is_muted(&snac1, xs_dict_get(msg, "attributedTo"))) {
  1384. /* return the status itself */
  1385. out = mastoapi_status(&snac1, msg);
  1386. }
  1387. }
  1388. else
  1389. if (strcmp(op, "context") == 0) { /** **/
  1390. /* return ancestors and children */
  1391. xs *anc = xs_list_new();
  1392. xs *des = xs_list_new();
  1393. xs_list *p;
  1394. xs_str *v;
  1395. char pid[64];
  1396. /* build the [grand]parent list, moving up */
  1397. strncpy(pid, id, sizeof(pid));
  1398. while (object_parent(pid, pid, sizeof(pid))) {
  1399. xs *m2 = NULL;
  1400. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  1401. xs *st = mastoapi_status(&snac1, m2);
  1402. if (st)
  1403. anc = xs_list_insert(anc, 0, st);
  1404. }
  1405. else
  1406. break;
  1407. }
  1408. /* build the children list */
  1409. xs *children = object_children(xs_dict_get(msg, "id"));
  1410. p = children;
  1411. while (xs_list_iter(&p, &v)) {
  1412. xs *m2 = NULL;
  1413. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  1414. if (xs_is_null(xs_dict_get(m2, "name"))) {
  1415. xs *st = mastoapi_status(&snac1, m2);
  1416. if (st)
  1417. des = xs_list_append(des, st);
  1418. }
  1419. }
  1420. }
  1421. out = xs_dict_new();
  1422. out = xs_dict_append(out, "ancestors", anc);
  1423. out = xs_dict_append(out, "descendants", des);
  1424. }
  1425. else
  1426. if (strcmp(op, "reblogged_by") == 0 || /** **/
  1427. strcmp(op, "favourited_by") == 0) { /** **/
  1428. /* return the list of people who liked or boosted this */
  1429. out = xs_list_new();
  1430. xs *l = NULL;
  1431. if (op[0] == 'r')
  1432. l = object_announces(xs_dict_get(msg, "id"));
  1433. else
  1434. l = object_likes(xs_dict_get(msg, "id"));
  1435. xs_list *p = l;
  1436. xs_str *v;
  1437. while (xs_list_iter(&p, &v)) {
  1438. xs *actor2 = NULL;
  1439. if (valid_status(object_get_by_md5(v, &actor2))) {
  1440. xs *acct2 = mastoapi_account(actor2);
  1441. out = xs_list_append(out, acct2);
  1442. }
  1443. }
  1444. }
  1445. }
  1446. else
  1447. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  1448. if (out != NULL) {
  1449. *body = xs_json_dumps(out, 4);
  1450. *ctype = "application/json";
  1451. status = 200;
  1452. }
  1453. }
  1454. }
  1455. else
  1456. status = 401;
  1457. }
  1458. else
  1459. if (strcmp(cmd, "/v1/preferences") == 0) { /** **/
  1460. *body = xs_dup("{}");
  1461. *ctype = "application/json";
  1462. status = 200;
  1463. }
  1464. else
  1465. if (strcmp(cmd, "/v1/markers") == 0) { /** **/
  1466. *body = xs_dup("{}");
  1467. *ctype = "application/json";
  1468. status = 200;
  1469. }
  1470. else
  1471. if (strcmp(cmd, "/v1/followed_tags") == 0) { /** **/
  1472. *body = xs_dup("[]");
  1473. *ctype = "application/json";
  1474. status = 200;
  1475. }
  1476. else
  1477. if (strcmp(cmd, "/v2/search") == 0) { /** **/
  1478. if (logged_in) {
  1479. const char *q = xs_dict_get(args, "q");
  1480. const char *type = xs_dict_get(args, "type");
  1481. const char *offset = xs_dict_get(args, "offset");
  1482. xs *acl = xs_list_new();
  1483. xs *stl = xs_list_new();
  1484. xs *htl = xs_list_new();
  1485. xs *res = xs_dict_new();
  1486. if (xs_is_null(offset) || strcmp(offset, "0") == 0) {
  1487. /* reply something only for offset 0; otherwise,
  1488. apps like Tusky keep asking again and again */
  1489. if (!xs_is_null(q) && !xs_is_null(type) && strcmp(type, "accounts") == 0) {
  1490. /* do a webfinger query */
  1491. char *actor = NULL;
  1492. char *user = NULL;
  1493. if (valid_status(webfinger_request(q, &actor, &user))) {
  1494. xs *actor_o = NULL;
  1495. if (valid_status(actor_request(&snac1, actor, &actor_o))) {
  1496. xs *acct = mastoapi_account(actor_o);
  1497. acl = xs_list_append(acl, acct);
  1498. }
  1499. }
  1500. }
  1501. }
  1502. res = xs_dict_append(res, "accounts", acl);
  1503. res = xs_dict_append(res, "statuses", stl);
  1504. res = xs_dict_append(res, "hashtags", htl);
  1505. *body = xs_json_dumps(res, 4);
  1506. *ctype = "application/json";
  1507. status = 200;
  1508. }
  1509. else
  1510. status = 401;
  1511. }
  1512. /* user cleanup */
  1513. if (logged_in)
  1514. user_free(&snac1);
  1515. srv_debug(1, xs_fmt("mastoapi_get_handler %s %d", q_path, status));
  1516. return status;
  1517. }
  1518. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  1519. const char *payload, int p_size,
  1520. char **body, int *b_size, char **ctype)
  1521. {
  1522. (void)p_size;
  1523. (void)b_size;
  1524. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1525. return 0;
  1526. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1527. int status = 404;
  1528. xs *args = NULL;
  1529. char *i_ctype = xs_dict_get(req, "content-type");
  1530. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1531. args = xs_json_loads(payload);
  1532. else
  1533. args = xs_dup(xs_dict_get(req, "p_vars"));
  1534. if (args == NULL)
  1535. return 400;
  1536. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1537. snac snac = {0};
  1538. int logged_in = process_auth_token(&snac, req);
  1539. if (strcmp(cmd, "/v1/apps") == 0) { /** **/
  1540. const char *name = xs_dict_get(args, "client_name");
  1541. const char *ruri = xs_dict_get(args, "redirect_uris");
  1542. const char *scope = xs_dict_get(args, "scope");
  1543. if (xs_type(ruri) == XSTYPE_LIST)
  1544. ruri = xs_dict_get(ruri, 0);
  1545. if (name && ruri) {
  1546. xs *app = xs_dict_new();
  1547. xs *id = xs_replace_i(tid(0), ".", "");
  1548. xs *csec = random_str();
  1549. xs *vkey = random_str();
  1550. xs *cid = NULL;
  1551. /* pick a non-existent random cid */
  1552. for (;;) {
  1553. cid = random_str();
  1554. xs *p_app = app_get(cid);
  1555. if (p_app == NULL)
  1556. break;
  1557. xs_free(cid);
  1558. }
  1559. app = xs_dict_append(app, "name", name);
  1560. app = xs_dict_append(app, "redirect_uri", ruri);
  1561. app = xs_dict_append(app, "client_id", cid);
  1562. app = xs_dict_append(app, "client_secret", csec);
  1563. app = xs_dict_append(app, "vapid_key", vkey);
  1564. app = xs_dict_append(app, "id", id);
  1565. *body = xs_json_dumps(app, 4);
  1566. *ctype = "application/json";
  1567. status = 200;
  1568. app = xs_dict_append(app, "code", "");
  1569. if (scope)
  1570. app = xs_dict_append(app, "scope", scope);
  1571. app_add(cid, app);
  1572. srv_debug(1, xs_fmt("mastoapi apps: new app %s", cid));
  1573. }
  1574. }
  1575. else
  1576. if (strcmp(cmd, "/v1/statuses") == 0) { /** **/
  1577. if (logged_in) {
  1578. /* post a new Note */
  1579. const char *content = xs_dict_get(args, "status");
  1580. const char *mid = xs_dict_get(args, "in_reply_to_id");
  1581. const char *visibility = xs_dict_get(args, "visibility");
  1582. const char *summary = xs_dict_get(args, "spoiler_text");
  1583. const char *media_ids = xs_dict_get(args, "media_ids");
  1584. if (xs_is_null(media_ids))
  1585. media_ids = xs_dict_get(args, "media_ids[]");
  1586. if (xs_is_null(media_ids))
  1587. media_ids = xs_dict_get(args, "media_ids");
  1588. if (xs_is_null(visibility))
  1589. visibility = "public";
  1590. xs *attach_list = xs_list_new();
  1591. xs *irt = NULL;
  1592. /* is it a reply? */
  1593. if (mid != NULL) {
  1594. xs *r_msg = NULL;
  1595. const char *md5 = MID_TO_MD5(mid);
  1596. if (valid_status(object_get_by_md5(md5, &r_msg)))
  1597. irt = xs_dup(xs_dict_get(r_msg, "id"));
  1598. }
  1599. /* does it have attachments? */
  1600. if (!xs_is_null(media_ids)) {
  1601. xs *mi = NULL;
  1602. if (xs_type(media_ids) == XSTYPE_LIST)
  1603. mi = xs_dup(media_ids);
  1604. else {
  1605. mi = xs_list_new();
  1606. mi = xs_list_append(mi, media_ids);
  1607. }
  1608. xs_list *p = mi;
  1609. xs_str *v;
  1610. while (xs_list_iter(&p, &v)) {
  1611. xs *l = xs_list_new();
  1612. xs *url = xs_fmt("%s/s/%s", snac.actor, v);
  1613. xs *desc = static_get_meta(&snac, v);
  1614. l = xs_list_append(l, url);
  1615. l = xs_list_append(l, desc);
  1616. attach_list = xs_list_append(attach_list, l);
  1617. }
  1618. }
  1619. /* prepare the message */
  1620. xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
  1621. strcmp(visibility, "public") == 0 ? 0 : 1);
  1622. if (!xs_is_null(summary) && *summary) {
  1623. msg = xs_dict_set(msg, "sensitive", xs_stock_true);
  1624. msg = xs_dict_set(msg, "summary", summary);
  1625. }
  1626. /* store */
  1627. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1628. /* 'Create' message */
  1629. xs *c_msg = msg_create(&snac, msg);
  1630. enqueue_message(&snac, c_msg);
  1631. timeline_touch(&snac);
  1632. /* convert to a mastodon status as a response code */
  1633. xs *st = mastoapi_status(&snac, msg);
  1634. *body = xs_json_dumps(st, 4);
  1635. *ctype = "application/json";
  1636. status = 200;
  1637. }
  1638. else
  1639. status = 401;
  1640. }
  1641. else
  1642. if (xs_startswith(cmd, "/v1/statuses")) { /** **/
  1643. if (logged_in) {
  1644. /* operations on a status */
  1645. xs *l = xs_split(cmd, "/");
  1646. const char *mid = xs_list_get(l, 3);
  1647. const char *op = xs_list_get(l, 4);
  1648. if (!xs_is_null(mid)) {
  1649. xs *msg = NULL;
  1650. xs *out = NULL;
  1651. /* skip the 'fake' part of the id */
  1652. mid = MID_TO_MD5(mid);
  1653. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1654. char *id = xs_dict_get(msg, "id");
  1655. if (op == NULL) {
  1656. /* no operation (?) */
  1657. }
  1658. else
  1659. if (strcmp(op, "favourite") == 0) { /** **/
  1660. xs *n_msg = msg_admiration(&snac, id, "Like");
  1661. if (n_msg != NULL) {
  1662. enqueue_message(&snac, n_msg);
  1663. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  1664. out = mastoapi_status(&snac, msg);
  1665. }
  1666. }
  1667. else
  1668. if (strcmp(op, "unfavourite") == 0) { /** **/
  1669. /* partial support: as the original Like message
  1670. is not stored anywhere here, it's not possible
  1671. to send an Undo + Like; the only thing done here
  1672. is to delete the actor from the list of likes */
  1673. object_unadmire(id, snac.actor, 1);
  1674. }
  1675. else
  1676. if (strcmp(op, "reblog") == 0) { /** **/
  1677. xs *n_msg = msg_admiration(&snac, id, "Announce");
  1678. if (n_msg != NULL) {
  1679. enqueue_message(&snac, n_msg);
  1680. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  1681. out = mastoapi_status(&snac, msg);
  1682. }
  1683. }
  1684. else
  1685. if (strcmp(op, "unreblog") == 0) { /** **/
  1686. /* partial support: see comment in 'unfavourite' */
  1687. object_unadmire(id, snac.actor, 0);
  1688. }
  1689. else
  1690. if (strcmp(op, "bookmark") == 0) { /** **/
  1691. /* snac does not support bookmarks */
  1692. }
  1693. else
  1694. if (strcmp(op, "unbookmark") == 0) { /** **/
  1695. /* snac does not support bookmarks */
  1696. }
  1697. else
  1698. if (strcmp(op, "pin") == 0) { /** **/
  1699. /* pin this message */
  1700. if (pin(&snac, id))
  1701. out = mastoapi_status(&snac, msg);
  1702. else
  1703. status = 422;
  1704. }
  1705. else
  1706. if (strcmp(op, "unpin") == 0) { /** **/
  1707. /* unpin this message */
  1708. unpin(&snac, id);
  1709. out = mastoapi_status(&snac, msg);
  1710. }
  1711. else
  1712. if (strcmp(op, "mute") == 0) { /** **/
  1713. /* Mastodon's mute is snac's hide */
  1714. }
  1715. else
  1716. if (strcmp(op, "unmute") == 0) { /** **/
  1717. /* Mastodon's unmute is snac's unhide */
  1718. }
  1719. }
  1720. if (out != NULL) {
  1721. *body = xs_json_dumps(out, 4);
  1722. *ctype = "application/json";
  1723. status = 200;
  1724. }
  1725. }
  1726. }
  1727. else
  1728. status = 401;
  1729. }
  1730. else
  1731. if (strcmp(cmd, "/v1/notifications/clear") == 0) { /** **/
  1732. if (logged_in) {
  1733. notify_clear(&snac);
  1734. timeline_touch(&snac);
  1735. *body = xs_dup("{}");
  1736. *ctype = "application/json";
  1737. status = 200;
  1738. }
  1739. else
  1740. status = 401;
  1741. }
  1742. else
  1743. if (strcmp(cmd, "/v1/push/subscription") == 0) { /** **/
  1744. /* I don't know what I'm doing */
  1745. if (logged_in) {
  1746. char *v;
  1747. xs *wpush = xs_dict_new();
  1748. wpush = xs_dict_append(wpush, "id", "1");
  1749. v = xs_dict_get(args, "data");
  1750. v = xs_dict_get(v, "alerts");
  1751. wpush = xs_dict_append(wpush, "alerts", v);
  1752. v = xs_dict_get(args, "subscription");
  1753. v = xs_dict_get(v, "endpoint");
  1754. wpush = xs_dict_append(wpush, "endpoint", v);
  1755. xs *server_key = random_str();
  1756. wpush = xs_dict_append(wpush, "server_key", server_key);
  1757. *body = xs_json_dumps(wpush, 4);
  1758. *ctype = "application/json";
  1759. status = 200;
  1760. }
  1761. else
  1762. status = 401;
  1763. }
  1764. else
  1765. if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { /** **/
  1766. if (logged_in) {
  1767. const xs_list *file = xs_dict_get(args, "file");
  1768. const char *desc = xs_dict_get(args, "description");
  1769. if (xs_is_null(desc))
  1770. desc = "";
  1771. status = 400;
  1772. if (xs_type(file) == XSTYPE_LIST) {
  1773. const char *fn = xs_list_get(file, 0);
  1774. if (*fn != '\0') {
  1775. char *ext = strrchr(fn, '.');
  1776. xs *hash = xs_md5_hex(fn, strlen(fn));
  1777. xs *id = xs_fmt("%s%s", hash, ext);
  1778. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1779. int fo = xs_number_get(xs_list_get(file, 1));
  1780. int fs = xs_number_get(xs_list_get(file, 2));
  1781. /* store */
  1782. static_put(&snac, id, payload + fo, fs);
  1783. static_put_meta(&snac, id, desc);
  1784. /* prepare a response */
  1785. xs *rsp = xs_dict_new();
  1786. rsp = xs_dict_append(rsp, "id", id);
  1787. rsp = xs_dict_append(rsp, "type", "image");
  1788. rsp = xs_dict_append(rsp, "url", url);
  1789. rsp = xs_dict_append(rsp, "preview_url", url);
  1790. rsp = xs_dict_append(rsp, "remote_url", url);
  1791. rsp = xs_dict_append(rsp, "description", desc);
  1792. *body = xs_json_dumps(rsp, 4);
  1793. *ctype = "application/json";
  1794. status = 200;
  1795. }
  1796. }
  1797. }
  1798. else
  1799. status = 401;
  1800. }
  1801. else
  1802. if (xs_startswith(cmd, "/v1/accounts")) { /** **/
  1803. if (logged_in) {
  1804. /* account-related information */
  1805. xs *l = xs_split(cmd, "/");
  1806. const char *md5 = xs_list_get(l, 3);
  1807. const char *opt = xs_list_get(l, 4);
  1808. xs *rsp = NULL;
  1809. if (!xs_is_null(md5) && *md5) {
  1810. xs *actor_o = NULL;
  1811. if (xs_is_null(opt)) {
  1812. /* ? */
  1813. }
  1814. else
  1815. if (strcmp(opt, "follow") == 0) { /** **/
  1816. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1817. const char *actor = xs_dict_get(actor_o, "id");
  1818. xs *msg = msg_follow(&snac, actor);
  1819. if (msg != NULL) {
  1820. /* reload the actor from the message, in may be different */
  1821. actor = xs_dict_get(msg, "object");
  1822. following_add(&snac, actor, msg);
  1823. enqueue_output_by_actor(&snac, msg, actor, 0);
  1824. rsp = mastoapi_relationship(&snac, md5);
  1825. }
  1826. }
  1827. }
  1828. else
  1829. if (strcmp(opt, "unfollow") == 0) { /** **/
  1830. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1831. const char *actor = xs_dict_get(actor_o, "id");
  1832. /* get the following object */
  1833. xs *object = NULL;
  1834. if (valid_status(following_get(&snac, actor, &object))) {
  1835. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  1836. following_del(&snac, actor);
  1837. enqueue_output_by_actor(&snac, msg, actor, 0);
  1838. rsp = mastoapi_relationship(&snac, md5);
  1839. }
  1840. }
  1841. }
  1842. else
  1843. if (strcmp(opt, "block") == 0) { /** **/
  1844. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1845. const char *actor = xs_dict_get(actor_o, "id");
  1846. mute(&snac, actor);
  1847. rsp = mastoapi_relationship(&snac, md5);
  1848. }
  1849. }
  1850. else
  1851. if (strcmp(opt, "unblock") == 0) { /** **/
  1852. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1853. const char *actor = xs_dict_get(actor_o, "id");
  1854. unmute(&snac, actor);
  1855. rsp = mastoapi_relationship(&snac, md5);
  1856. }
  1857. }
  1858. }
  1859. if (rsp != NULL) {
  1860. *body = xs_json_dumps(rsp, 4);
  1861. *ctype = "application/json";
  1862. status = 200;
  1863. }
  1864. }
  1865. else
  1866. status = 401;
  1867. }
  1868. else
  1869. if (xs_startswith(cmd, "/v1/polls")) { /** **/
  1870. if (logged_in) {
  1871. /* operations on a status */
  1872. xs *l = xs_split(cmd, "/");
  1873. const char *mid = xs_list_get(l, 3);
  1874. const char *op = xs_list_get(l, 4);
  1875. if (!xs_is_null(mid)) {
  1876. xs *msg = NULL;
  1877. xs *out = NULL;
  1878. /* skip the 'fake' part of the id */
  1879. mid = MID_TO_MD5(mid);
  1880. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1881. const char *id = xs_dict_get(msg, "id");
  1882. const char *atto = xs_dict_get(msg, "attributedTo");
  1883. xs_list *opts = xs_dict_get(msg, "oneOf");
  1884. if (opts == NULL)
  1885. opts = xs_dict_get(msg, "anyOf");
  1886. if (op == NULL) {
  1887. }
  1888. else
  1889. if (strcmp(op, "votes") == 0) {
  1890. xs_list *choices = xs_dict_get(args, "choices[]");
  1891. if (xs_is_null(choices))
  1892. choices = xs_dict_get(args, "choices");
  1893. if (xs_type(choices) == XSTYPE_LIST) {
  1894. xs_str *v;
  1895. while (xs_list_iter(&choices, &v)) {
  1896. int io = atoi(v);
  1897. const xs_dict *o = xs_list_get(opts, io);
  1898. if (o) {
  1899. const char *name = xs_dict_get(o, "name");
  1900. xs *msg = msg_note(&snac, "", atto, (char *)id, NULL, 1);
  1901. msg = xs_dict_append(msg, "name", name);
  1902. xs *c_msg = msg_create(&snac, msg);
  1903. enqueue_message(&snac, c_msg);
  1904. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1905. }
  1906. }
  1907. out = mastoapi_poll(&snac, msg);
  1908. }
  1909. }
  1910. }
  1911. if (out != NULL) {
  1912. *body = xs_json_dumps(out, 4);
  1913. *ctype = "application/json";
  1914. status = 200;
  1915. }
  1916. }
  1917. }
  1918. else
  1919. status = 401;
  1920. }
  1921. /* user cleanup */
  1922. if (logged_in)
  1923. user_free(&snac);
  1924. return status;
  1925. }
  1926. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  1927. const char *payload, int p_size,
  1928. char **body, int *b_size, char **ctype)
  1929. {
  1930. (void)p_size;
  1931. (void)b_size;
  1932. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1933. return 0;
  1934. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1935. int status = 404;
  1936. xs *args = NULL;
  1937. char *i_ctype = xs_dict_get(req, "content-type");
  1938. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1939. args = xs_json_loads(payload);
  1940. else
  1941. args = xs_dup(xs_dict_get(req, "p_vars"));
  1942. if (args == NULL)
  1943. return 400;
  1944. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1945. snac snac = {0};
  1946. int logged_in = process_auth_token(&snac, req);
  1947. if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) { /** **/
  1948. if (logged_in) {
  1949. xs *l = xs_split(cmd, "/");
  1950. const char *stid = xs_list_get(l, 3);
  1951. if (!xs_is_null(stid)) {
  1952. const char *desc = xs_dict_get(args, "description");
  1953. /* set the image metadata */
  1954. static_put_meta(&snac, stid, desc);
  1955. /* prepare a response */
  1956. xs *rsp = xs_dict_new();
  1957. xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
  1958. rsp = xs_dict_append(rsp, "id", stid);
  1959. rsp = xs_dict_append(rsp, "type", "image");
  1960. rsp = xs_dict_append(rsp, "url", url);
  1961. rsp = xs_dict_append(rsp, "preview_url", url);
  1962. rsp = xs_dict_append(rsp, "remote_url", url);
  1963. rsp = xs_dict_append(rsp, "description", desc);
  1964. *body = xs_json_dumps(rsp, 4);
  1965. *ctype = "application/json";
  1966. status = 200;
  1967. }
  1968. }
  1969. else
  1970. status = 401;
  1971. }
  1972. /* user cleanup */
  1973. if (logged_in)
  1974. user_free(&snac);
  1975. return status;
  1976. }
  1977. void mastoapi_purge(void)
  1978. {
  1979. xs *spec = xs_fmt("%s/app/" "*.json", srv_basedir);
  1980. xs *files = xs_glob(spec, 1, 0);
  1981. xs_list *p = files;
  1982. xs_str *v;
  1983. time_t mt = time(NULL) - 3600;
  1984. while (xs_list_iter(&p, &v)) {
  1985. xs *cid = xs_replace(v, ".json", "");
  1986. xs *fn = _app_fn(cid);
  1987. if (mtime(fn) < mt) {
  1988. /* get the app */
  1989. xs *app = app_get(cid);
  1990. if (app) {
  1991. /* old apps with no uid are incomplete cruft */
  1992. const char *uid = xs_dict_get(app, "uid");
  1993. if (xs_is_null(uid) || *uid == '\0') {
  1994. unlink(fn);
  1995. srv_debug(2, xs_fmt("purged %s", fn));
  1996. }
  1997. }
  1998. }
  1999. }
  2000. }
  2001. #endif /* #ifndef NO_MASTODON_API */