mastoapi.c 91 KB

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