mastoapi.c 96 KB

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