mastoapi.c 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2025 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 "xs_unicode.h"
  17. #include "snac.h"
  18. #include <sys/time.h>
  19. static xs_str *random_str(void)
  20. /* just what is says in the tin */
  21. {
  22. unsigned int data[4] = {0};
  23. xs_rnd_buf(data, sizeof(data));
  24. return xs_hex_enc((char *)data, sizeof(data));
  25. }
  26. int app_add(const char *id, const xs_dict *app)
  27. /* stores an app */
  28. {
  29. if (!xs_is_hex(id))
  30. return HTTP_STATUS_INTERNAL_SERVER_ERROR;
  31. int status = HTTP_STATUS_CREATED;
  32. xs *fn = xs_fmt("%s/app/", srv_basedir);
  33. FILE *f;
  34. mkdirx(fn);
  35. fn = xs_str_cat(fn, id);
  36. fn = xs_str_cat(fn, ".json");
  37. if ((f = fopen(fn, "w")) != NULL) {
  38. xs_json_dump(app, 4, f);
  39. fclose(f);
  40. }
  41. else
  42. status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
  43. return status;
  44. }
  45. xs_str *_app_fn(const char *id)
  46. {
  47. return xs_fmt("%s/app/%s.json", srv_basedir, id);
  48. }
  49. xs_dict *app_get(const char *id)
  50. /* gets an app */
  51. {
  52. if (!xs_is_hex(id))
  53. return NULL;
  54. xs *fn = _app_fn(id);
  55. xs_dict *app = NULL;
  56. FILE *f;
  57. if ((f = fopen(fn, "r")) != NULL) {
  58. app = xs_json_load(f);
  59. fclose(f);
  60. }
  61. return app;
  62. }
  63. int app_del(const char *id)
  64. /* deletes an app */
  65. {
  66. if (!xs_is_hex(id))
  67. return -1;
  68. xs *fn = _app_fn(id);
  69. return unlink(fn);
  70. }
  71. int token_add(const char *id, const xs_dict *token)
  72. /* stores a token */
  73. {
  74. if (!xs_is_hex(id))
  75. return HTTP_STATUS_INTERNAL_SERVER_ERROR;
  76. int status = HTTP_STATUS_CREATED;
  77. xs *fn = xs_fmt("%s/token/", srv_basedir);
  78. FILE *f;
  79. mkdirx(fn);
  80. fn = xs_str_cat(fn, id);
  81. fn = xs_str_cat(fn, ".json");
  82. if ((f = fopen(fn, "w")) != NULL) {
  83. xs_json_dump(token, 4, f);
  84. fclose(f);
  85. }
  86. else
  87. status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
  88. return status;
  89. }
  90. xs_dict *token_get(const char *id)
  91. /* gets a token */
  92. {
  93. if (!xs_is_hex(id))
  94. return NULL;
  95. xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
  96. xs_dict *token = NULL;
  97. FILE *f;
  98. if ((f = fopen(fn, "r")) != NULL) {
  99. token = xs_json_load(f);
  100. fclose(f);
  101. /* 'touch' the file */
  102. utimes(fn, NULL);
  103. /* also 'touch' the app */
  104. const char *app_id = xs_dict_get(token, "client_id");
  105. if (app_id) {
  106. xs *afn = xs_fmt("%s/app/%s.json", srv_basedir, app_id);
  107. utimes(afn, NULL);
  108. }
  109. }
  110. return token;
  111. }
  112. int token_del(const char *id)
  113. /* deletes a token */
  114. {
  115. if (!xs_is_hex(id))
  116. return -1;
  117. xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
  118. return unlink(fn);
  119. }
  120. const char *login_page = ""
  121. "<!DOCTYPE html>\n"
  122. "<html>\n"
  123. "<head>\n"
  124. "<title>%s OAuth - Snac2</title>\n"
  125. "<meta content=\"width=device-width, initial-scale=1, minimum-scale=1, user-scalable=no\" name=\"viewport\">"
  126. "<style>:root {color-scheme: light dark}</style>\n"
  127. "</head>\n"
  128. "<body><h1>%s OAuth identify</h1>\n"
  129. "<div style=\"background-color: red; color: white\">%s</div>\n"
  130. "<form method=\"post\" action=\"%s:/" "/%s/%s\">\n"
  131. "<p>Login: <input type=\"text\" name=\"login\" autocapitalize=\"off\"></p>\n"
  132. "<p>Password: <input type=\"password\" name=\"passwd\"></p>\n"
  133. "<input type=\"hidden\" name=\"redir\" value=\"%s\">\n"
  134. "<input type=\"hidden\" name=\"cid\" value=\"%s\">\n"
  135. "<input type=\"hidden\" name=\"state\" value=\"%s\">\n"
  136. "<input type=\"submit\" value=\"OK\">\n"
  137. "</form><p>%s</p></body></html>\n"
  138. "";
  139. int oauth_get_handler(const xs_dict *req, const char *q_path,
  140. char **body, int *b_size, char **ctype)
  141. {
  142. (void)b_size;
  143. if (!xs_startswith(q_path, "/oauth/"))
  144. return 0;
  145. int status = HTTP_STATUS_NOT_FOUND;
  146. const xs_dict *msg = xs_dict_get(req, "q_vars");
  147. xs *cmd = xs_replace_n(q_path, "/oauth", "", 1);
  148. srv_debug(1, xs_fmt("oauth_get_handler %s", q_path));
  149. if (strcmp(cmd, "/authorize") == 0) { /** **/
  150. const char *cid = xs_dict_get(msg, "client_id");
  151. const char *ruri = xs_dict_get(msg, "redirect_uri");
  152. const char *rtype = xs_dict_get(msg, "response_type");
  153. const char *state = xs_dict_get(msg, "state");
  154. status = HTTP_STATUS_BAD_REQUEST;
  155. if (cid && ruri && rtype && strcmp(rtype, "code") == 0) {
  156. xs *app = app_get(cid);
  157. if (app != NULL) {
  158. const char *host = xs_dict_get(srv_config, "host");
  159. const char *proto = xs_dict_get_def(srv_config, "protocol", "https");
  160. if (xs_is_null(state))
  161. state = "";
  162. *body = xs_fmt(login_page, host, host, "", proto, host, "oauth/x-snac-login",
  163. ruri, cid, state, USER_AGENT);
  164. *ctype = "text/html";
  165. status = HTTP_STATUS_OK;
  166. srv_debug(1, xs_fmt("oauth authorize: generating login page"));
  167. }
  168. else
  169. srv_debug(1, xs_fmt("oauth authorize: bad client_id %s", cid));
  170. }
  171. else
  172. srv_debug(1, xs_fmt("oauth authorize: invalid or unset arguments"));
  173. }
  174. else
  175. if (strcmp(cmd, "/x-snac-get-token") == 0) { /** **/
  176. const char *host = xs_dict_get(srv_config, "host");
  177. const char *proto = xs_dict_get_def(srv_config, "protocol", "https");
  178. *body = xs_fmt(login_page, host, host, "", proto, host, "oauth/x-snac-get-token",
  179. "", "", "", USER_AGENT);
  180. *ctype = "text/html";
  181. status = HTTP_STATUS_OK;
  182. }
  183. return status;
  184. }
  185. int oauth_post_handler(const xs_dict *req, const char *q_path,
  186. const char *payload, int p_size,
  187. char **body, int *b_size, char **ctype)
  188. {
  189. (void)p_size;
  190. (void)b_size;
  191. if (!xs_startswith(q_path, "/oauth/"))
  192. return 0;
  193. int status = HTTP_STATUS_NOT_FOUND;
  194. const char *i_ctype = xs_dict_get(req, "content-type");
  195. xs *args = NULL;
  196. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  197. if (!xs_is_null(payload))
  198. args = xs_json_loads(payload);
  199. }
  200. else
  201. if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded") && payload) {
  202. args = xs_url_vars(payload);
  203. }
  204. else
  205. args = xs_dup(xs_dict_get(req, "p_vars"));
  206. if (args == NULL)
  207. return HTTP_STATUS_BAD_REQUEST;
  208. xs *cmd = xs_replace_n(q_path, "/oauth", "", 1);
  209. srv_debug(1, xs_fmt("oauth_post_handler %s", q_path));
  210. if (strcmp(cmd, "/x-snac-login") == 0) { /** **/
  211. const char *login = xs_dict_get(args, "login");
  212. const char *passwd = xs_dict_get(args, "passwd");
  213. const char *redir = xs_dict_get(args, "redir");
  214. const char *cid = xs_dict_get(args, "cid");
  215. const char *state = xs_dict_get(args, "state");
  216. const char *host = xs_dict_get(srv_config, "host");
  217. const char *proto = xs_dict_get_def(srv_config, "protocol", "https");
  218. /* by default, generate another login form with an error */
  219. *body = xs_fmt(login_page, host, host, "LOGIN INCORRECT", proto, host, "oauth/x-snac-login",
  220. redir, cid, state, USER_AGENT);
  221. *ctype = "text/html";
  222. status = HTTP_STATUS_OK;
  223. if (login && passwd && redir && cid) {
  224. snac snac;
  225. if (user_open(&snac, login)) {
  226. const char *addr = xs_or(xs_dict_get(req, "remote-addr"),
  227. xs_dict_get(req, "x-forwarded-for"));
  228. if (badlogin_check(login, addr)) {
  229. /* check the login + password */
  230. if (check_password(login, passwd, xs_dict_get(snac.config, "passwd"))) {
  231. /* success! redirect to the desired uri */
  232. xs *code = random_str();
  233. xs_free(*body);
  234. if (strcmp(redir, "urn:ietf:wg:oauth:2.0:oob") == 0) {
  235. *body = xs_dup(code);
  236. }
  237. else {
  238. if (xs_str_in(redir, "?") != -1)
  239. *body = xs_fmt("%s&code=%s", redir, code);
  240. else
  241. *body = xs_fmt("%s?code=%s", redir, code);
  242. status = HTTP_STATUS_SEE_OTHER;
  243. }
  244. /* if there is a state, add it */
  245. if (!xs_is_null(state) && *state) {
  246. *body = xs_str_cat(*body, "&state=");
  247. *body = xs_str_cat(*body, state);
  248. }
  249. srv_log(xs_fmt("oauth x-snac-login: '%s' success, redirect to %s",
  250. login, *body));
  251. /* assign the login to the app */
  252. xs *app = app_get(cid);
  253. if (app != NULL) {
  254. app = xs_dict_set(app, "uid", login);
  255. app = xs_dict_set(app, "code", code);
  256. app_add(cid, app);
  257. }
  258. else
  259. srv_log(xs_fmt("oauth x-snac-login: error getting app %s", cid));
  260. }
  261. else {
  262. srv_debug(1, xs_fmt("oauth x-snac-login: login '%s' incorrect", login));
  263. badlogin_inc(login, addr);
  264. }
  265. }
  266. user_free(&snac);
  267. }
  268. else
  269. srv_debug(1, xs_fmt("oauth x-snac-login: bad user '%s'", login));
  270. }
  271. else
  272. srv_debug(1, xs_fmt("oauth x-snac-login: invalid or unset arguments"));
  273. }
  274. else
  275. if (strcmp(cmd, "/token") == 0) { /** **/
  276. xs *wrk = NULL;
  277. const char *gtype = xs_dict_get(args, "grant_type");
  278. const char *code = xs_dict_get(args, "code");
  279. const char *cid = xs_dict_get(args, "client_id");
  280. const char *csec = xs_dict_get(args, "client_secret");
  281. const char *ruri = xs_dict_get(args, "redirect_uri");
  282. const char *scope = xs_dict_get(args, "scope");
  283. /* no client_secret? check if it's inside an authorization header
  284. (AndStatus does it this way) */
  285. if (xs_is_null(csec)) {
  286. const char *auhdr = xs_dict_get(req, "authorization");
  287. if (!xs_is_null(auhdr) && xs_startswith(auhdr, "Basic ")) {
  288. xs *s1 = xs_replace_n(auhdr, "Basic ", "", 1);
  289. int size;
  290. xs *s2 = xs_base64_dec(s1, &size);
  291. if (!xs_is_null(s2)) {
  292. xs *l1 = xs_split(s2, ":");
  293. if (xs_list_len(l1) == 2) {
  294. wrk = xs_dup(xs_list_get(l1, 1));
  295. csec = wrk;
  296. }
  297. }
  298. }
  299. }
  300. /* no code?
  301. I'm not sure of the impacts of this right now, but Subway Tooter does not
  302. provide a code so one must be generated */
  303. if (xs_is_null(code)){
  304. code = random_str();
  305. }
  306. if (gtype && code && cid && csec && ruri) {
  307. xs *app = app_get(cid);
  308. if (app == NULL) {
  309. status = HTTP_STATUS_UNAUTHORIZED;
  310. srv_log(xs_fmt("oauth token: invalid app %s", cid));
  311. }
  312. else
  313. if (strcmp(csec, xs_dict_get(app, "client_secret")) != 0) {
  314. status = HTTP_STATUS_UNAUTHORIZED;
  315. srv_log(xs_fmt("oauth token: invalid client_secret for app %s", cid));
  316. }
  317. else {
  318. xs *rsp = xs_dict_new();
  319. xs *cat = xs_number_new(time(NULL));
  320. xs *tokid = random_str();
  321. rsp = xs_dict_append(rsp, "access_token", tokid);
  322. rsp = xs_dict_append(rsp, "token_type", "Bearer");
  323. rsp = xs_dict_append(rsp, "created_at", cat);
  324. if (!xs_is_null(scope))
  325. rsp = xs_dict_append(rsp, "scope", scope);
  326. *body = xs_json_dumps(rsp, 4);
  327. *ctype = "application/json";
  328. status = HTTP_STATUS_OK;
  329. const char *uid = xs_dict_get(app, "uid");
  330. srv_debug(1, xs_fmt("oauth token: "
  331. "successful login for %s, new token %s", uid, tokid));
  332. xs *token = xs_dict_new();
  333. token = xs_dict_append(token, "token", tokid);
  334. token = xs_dict_append(token, "client_id", cid);
  335. token = xs_dict_append(token, "client_secret", csec);
  336. token = xs_dict_append(token, "uid", uid);
  337. token = xs_dict_append(token, "code", code);
  338. token_add(tokid, token);
  339. }
  340. }
  341. else {
  342. srv_debug(1, xs_fmt("oauth token: invalid or unset arguments"));
  343. status = HTTP_STATUS_BAD_REQUEST;
  344. }
  345. }
  346. else
  347. if (strcmp(cmd, "/revoke") == 0) { /** **/
  348. const char *cid = xs_dict_get(args, "client_id");
  349. const char *csec = xs_dict_get(args, "client_secret");
  350. const char *tokid = xs_dict_get(args, "token");
  351. if (cid && csec && tokid) {
  352. xs *token = token_get(tokid);
  353. *body = xs_str_new("{}");
  354. *ctype = "application/json";
  355. if (token == NULL || strcmp(csec, xs_dict_get(token, "client_secret")) != 0) {
  356. srv_debug(1, xs_fmt("oauth revoke: bad secret for token %s", tokid));
  357. status = HTTP_STATUS_FORBIDDEN;
  358. }
  359. else {
  360. token_del(tokid);
  361. srv_debug(1, xs_fmt("oauth revoke: revoked token %s", tokid));
  362. status = HTTP_STATUS_OK;
  363. /* also delete the app, as it serves no purpose from now on */
  364. app_del(cid);
  365. }
  366. }
  367. else {
  368. srv_debug(1, xs_fmt("oauth revoke: invalid or unset arguments"));
  369. status = HTTP_STATUS_FORBIDDEN;
  370. }
  371. }
  372. if (strcmp(cmd, "/x-snac-get-token") == 0) { /** **/
  373. const char *login = xs_dict_get(args, "login");
  374. const char *passwd = xs_dict_get(args, "passwd");
  375. const char *host = xs_dict_get(srv_config, "host");
  376. const char *proto = xs_dict_get_def(srv_config, "protocol", "https");
  377. /* by default, generate another login form with an error */
  378. *body = xs_fmt(login_page, host, host, "LOGIN INCORRECT", proto, host, "oauth/x-snac-get-token",
  379. "", "", "", USER_AGENT);
  380. *ctype = "text/html";
  381. status = HTTP_STATUS_OK;
  382. if (login && passwd) {
  383. snac user;
  384. if (user_open(&user, login)) {
  385. const char *addr = xs_or(xs_dict_get(req, "remote-addr"),
  386. xs_dict_get(req, "x-forwarded-for"));
  387. if (badlogin_check(login, addr)) {
  388. /* check the login + password */
  389. if (check_password(login, passwd, xs_dict_get(user.config, "passwd"))) {
  390. /* success! create a new token */
  391. xs *tokid = random_str();
  392. srv_debug(1, xs_fmt("x-snac-new-token: "
  393. "successful login for %s, new token %s", login, tokid));
  394. xs *token = xs_dict_new();
  395. token = xs_dict_append(token, "token", tokid);
  396. token = xs_dict_append(token, "client_id", "snac-client");
  397. token = xs_dict_append(token, "client_secret", "");
  398. token = xs_dict_append(token, "uid", login);
  399. token = xs_dict_append(token, "code", "");
  400. token_add(tokid, token);
  401. *ctype = "text/plain";
  402. xs_free(*body);
  403. *body = xs_dup(tokid);
  404. }
  405. else
  406. badlogin_inc(login, addr);
  407. user_free(&user);
  408. }
  409. }
  410. }
  411. }
  412. return status;
  413. }
  414. xs_str *mastoapi_id(const xs_dict *msg)
  415. /* returns a somewhat Mastodon-compatible status id */
  416. {
  417. const char *id = xs_dict_get(msg, "id");
  418. xs *md5 = xs_md5_hex(id, strlen(id));
  419. return xs_fmt("%10.0f%s", object_ctime_by_md5(md5), md5);
  420. }
  421. #define MID_TO_MD5(id) (id + 10)
  422. xs_dict *mastoapi_account(snac *logged, const xs_dict *actor)
  423. /* converts an ActivityPub actor to a Mastodon account */
  424. {
  425. const char *id = xs_dict_get(actor, "id");
  426. const char *pub = xs_dict_get(actor, "published");
  427. const char *proxy = NULL;
  428. if (xs_type(id) != XSTYPE_STRING)
  429. return NULL;
  430. if (logged && xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  431. proxy = logged->actor;
  432. const char *prefu = xs_dict_get(actor, "preferredUsername");
  433. const char *display_name = xs_dict_get(actor, "name");
  434. if (xs_is_null(display_name) || *display_name == '\0')
  435. display_name = prefu;
  436. xs_dict *acct = xs_dict_new();
  437. xs *acct_md5 = xs_md5_hex(id, strlen(id));
  438. acct = xs_dict_append(acct, "id", acct_md5);
  439. acct = xs_dict_append(acct, "username", prefu);
  440. acct = xs_dict_append(acct, "display_name", display_name);
  441. acct = xs_dict_append(acct, "discoverable", xs_stock(XSTYPE_TRUE));
  442. acct = xs_dict_append(acct, "group", xs_stock(XSTYPE_FALSE));
  443. acct = xs_dict_append(acct, "hide_collections", xs_stock(XSTYPE_FALSE));
  444. acct = xs_dict_append(acct, "indexable", xs_stock(XSTYPE_TRUE));
  445. acct = xs_dict_append(acct, "noindex", xs_stock(XSTYPE_FALSE));
  446. acct = xs_dict_append(acct, "roles", xs_stock(XSTYPE_LIST));
  447. {
  448. /* create the acct field as user@host */
  449. xs *l = xs_split(id, "/");
  450. xs *fquid = xs_fmt("%s@%s", prefu, xs_list_get(l, 2));
  451. acct = xs_dict_append(acct, "acct", fquid);
  452. }
  453. if (pub)
  454. acct = xs_dict_append(acct, "created_at", pub);
  455. else {
  456. /* unset created_at crashes Tusky, so lie like a mf */
  457. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  458. acct = xs_dict_append(acct, "created_at", date);
  459. }
  460. xs *last_status_at = xs_str_utctime(0, "%Y-%m-%d");
  461. acct = xs_dict_append(acct, "last_status_at", last_status_at);
  462. const char *note = xs_dict_get(actor, "summary");
  463. if (xs_is_null(note))
  464. note = "";
  465. if (strcmp(xs_dict_get(actor, "type"), "Service") == 0)
  466. acct = xs_dict_append(acct, "bot", xs_stock(XSTYPE_TRUE));
  467. else
  468. acct = xs_dict_append(acct, "bot", xs_stock(XSTYPE_FALSE));
  469. acct = xs_dict_append(acct, "note", note);
  470. acct = xs_dict_append(acct, "url", id);
  471. acct = xs_dict_append(acct, "uri", id);
  472. xs *avatar = NULL;
  473. const xs_dict *av = xs_dict_get(actor, "icon");
  474. if (xs_type(av) == XSTYPE_DICT) {
  475. const char *url = xs_dict_get(av, "url");
  476. if (url != NULL)
  477. avatar = make_url(url, proxy, 1);
  478. }
  479. if (avatar == NULL)
  480. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  481. acct = xs_dict_append(acct, "avatar", avatar);
  482. acct = xs_dict_append(acct, "avatar_static", avatar);
  483. xs *header = NULL;
  484. const xs_dict *hd = xs_dict_get(actor, "image");
  485. if (xs_type(hd) == XSTYPE_DICT)
  486. header = make_url(xs_dict_get(hd, "url"), proxy, 1);
  487. if (xs_is_null(header))
  488. header = xs_str_new(NULL);
  489. acct = xs_dict_append(acct, "header", header);
  490. acct = xs_dict_append(acct, "header_static", header);
  491. /* emojis */
  492. const xs_list *p;
  493. if (!xs_is_null(p = xs_dict_get(actor, "tag"))) {
  494. xs *eml = xs_list_new();
  495. const xs_dict *v;
  496. int c = 0;
  497. while (xs_list_next(p, &v, &c)) {
  498. const char *type = xs_dict_get(v, "type");
  499. if (!xs_is_null(type) && strcmp(type, "Emoji") == 0) {
  500. const char *name = xs_dict_get(v, "name");
  501. const xs_dict *icon = xs_dict_get(v, "icon");
  502. if (!xs_is_null(name) && !xs_is_null(icon)) {
  503. const char *o_url = xs_dict_get(icon, "url");
  504. if (!xs_is_null(o_url)) {
  505. xs *url = make_url(o_url, proxy, 1);
  506. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  507. xs *d1 = xs_dict_new();
  508. d1 = xs_dict_append(d1, "shortcode", nm);
  509. d1 = xs_dict_append(d1, "url", url);
  510. d1 = xs_dict_append(d1, "static_url", url);
  511. d1 = xs_dict_append(d1, "visible_in_picker", xs_stock(XSTYPE_TRUE));
  512. eml = xs_list_append(eml, d1);
  513. }
  514. }
  515. }
  516. }
  517. acct = xs_dict_append(acct, "emojis", eml);
  518. }
  519. acct = xs_dict_append(acct, "locked", xs_stock(XSTYPE_FALSE));
  520. acct = xs_dict_append(acct, "followers_count", xs_stock(0));
  521. acct = xs_dict_append(acct, "following_count", xs_stock(0));
  522. acct = xs_dict_append(acct, "statuses_count", xs_stock(0));
  523. xs *fields = xs_list_new();
  524. p = xs_dict_get(actor, "attachment");
  525. const xs_dict *v;
  526. /* dict of validated links */
  527. xs_dict *val_links = NULL;
  528. const xs_dict *metadata = xs_stock(XSTYPE_DICT);
  529. snac user = {0};
  530. if (xs_startswith(id, srv_baseurl)) {
  531. /* if it's a local user, open it and pick its validated links */
  532. if (user_open(&user, prefu)) {
  533. val_links = user.links;
  534. metadata = xs_dict_get_def(user.config, "metadata", xs_stock(XSTYPE_DICT));
  535. /* does this user want to publish their contact metrics? */
  536. if (xs_is_true(xs_dict_get(user.config, "show_contact_metrics"))) {
  537. int fwing = following_list_len(&user);
  538. int fwers = follower_list_len(&user);
  539. xs *ni = xs_number_new(fwing);
  540. xs *ne = xs_number_new(fwers);
  541. acct = xs_dict_append(acct, "followers_count", ne);
  542. acct = xs_dict_append(acct, "following_count", ni);
  543. }
  544. }
  545. }
  546. if (xs_is_null(val_links))
  547. val_links = xs_stock(XSTYPE_DICT);
  548. int c = 0;
  549. while (xs_list_next(p, &v, &c)) {
  550. const char *type = xs_dict_get(v, "type");
  551. const char *name = xs_dict_get(v, "name");
  552. const char *value = xs_dict_get(v, "value");
  553. if (!xs_is_null(type) && !xs_is_null(name) &&
  554. !xs_is_null(value) && strcmp(type, "PropertyValue") == 0) {
  555. xs *val_date = NULL;
  556. const char *url = xs_dict_get(metadata, name);
  557. if (!xs_is_null(url) && xs_startswith(url, "https:/" "/")) {
  558. const xs_number *verified_time = xs_dict_get(val_links, url);
  559. if (xs_type(verified_time) == XSTYPE_NUMBER) {
  560. time_t t = xs_number_get(verified_time);
  561. if (t > 0)
  562. val_date = xs_str_utctime(t, ISO_DATE_SPEC);
  563. }
  564. }
  565. xs *d = xs_dict_new();
  566. d = xs_dict_append(d, "name", name);
  567. d = xs_dict_append(d, "value", value);
  568. d = xs_dict_append(d, "verified_at",
  569. xs_type(val_date) == XSTYPE_STRING && *val_date ?
  570. val_date : xs_stock(XSTYPE_NULL));
  571. fields = xs_list_append(fields, d);
  572. }
  573. }
  574. user_free(&user);
  575. acct = xs_dict_append(acct, "fields", fields);
  576. return acct;
  577. }
  578. xs_str *mastoapi_date(const char *date)
  579. /* converts an ISO 8601 date to whatever format Mastodon uses */
  580. {
  581. xs_str *s = xs_crop_i(xs_dup(date), 0, 19);
  582. s = xs_str_cat(s, ".000Z");
  583. return s;
  584. }
  585. xs_dict *mastoapi_poll(snac *snac, const xs_dict *msg)
  586. /* creates a mastoapi Poll object */
  587. {
  588. xs_dict *poll = xs_dict_new();
  589. xs *mid = mastoapi_id(msg);
  590. const xs_list *opts = NULL;
  591. const xs_val *v;
  592. int num_votes = 0;
  593. xs *options = xs_list_new();
  594. poll = xs_dict_append(poll, "id", mid);
  595. const char *date = xs_dict_get(msg, "endTime");
  596. if (date == NULL)
  597. date = xs_dict_get(msg, "closed");
  598. if (date == NULL)
  599. return NULL;
  600. xs *fd = mastoapi_date(date);
  601. poll = xs_dict_append(poll, "expires_at", fd);
  602. date = xs_dict_get(msg, "closed");
  603. time_t t = 0;
  604. if (date != NULL)
  605. t = xs_parse_iso_date(date, 0);
  606. poll = xs_dict_append(poll, "expired",
  607. t < time(NULL) ? xs_stock(XSTYPE_FALSE) : xs_stock(XSTYPE_TRUE));
  608. if ((opts = xs_dict_get(msg, "oneOf")) != NULL)
  609. poll = xs_dict_append(poll, "multiple", xs_stock(XSTYPE_FALSE));
  610. else {
  611. opts = xs_dict_get(msg, "anyOf");
  612. poll = xs_dict_append(poll, "multiple", xs_stock(XSTYPE_TRUE));
  613. }
  614. int c = 0;
  615. while (xs_list_next(opts, &v, &c)) {
  616. const char *title = xs_dict_get(v, "name");
  617. const char *replies = xs_dict_get(v, "replies");
  618. if (title && replies) {
  619. const char *votes_count = xs_dict_get(replies, "totalItems");
  620. if (xs_type(votes_count) == XSTYPE_NUMBER) {
  621. xs *d = xs_dict_new();
  622. d = xs_dict_append(d, "title", title);
  623. d = xs_dict_append(d, "votes_count", votes_count);
  624. options = xs_list_append(options, d);
  625. num_votes += xs_number_get(votes_count);
  626. }
  627. }
  628. }
  629. poll = xs_dict_append(poll, "options", options);
  630. xs *vc = xs_number_new(num_votes);
  631. poll = xs_dict_append(poll, "votes_count", vc);
  632. poll = xs_dict_append(poll, "emojis", xs_stock(XSTYPE_LIST));
  633. poll = xs_dict_append(poll, "voted",
  634. (snac && was_question_voted(snac, xs_dict_get(msg, "id"))) ?
  635. xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  636. return poll;
  637. }
  638. xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
  639. /* converts an ActivityPub note to a Mastodon status */
  640. {
  641. xs *actor = NULL;
  642. actor_get_refresh(snac, get_atto(msg), &actor);
  643. const char *proxy = NULL;
  644. /* if the author is not here, discard */
  645. if (actor == NULL)
  646. return NULL;
  647. if (snac && xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  648. proxy = snac->actor;
  649. const char *type = xs_dict_get(msg, "type");
  650. const char *id = xs_dict_get(msg, "id");
  651. /* fail if it's not a valid actor */
  652. if (xs_is_null(type) || xs_is_null(id))
  653. return NULL;
  654. xs *acct = mastoapi_account(snac, actor);
  655. if (acct == NULL)
  656. return NULL;
  657. xs *idx = NULL;
  658. xs *ixc = NULL;
  659. const char *tmp;
  660. xs *mid = mastoapi_id(msg);
  661. xs_dict *st = xs_dict_new();
  662. st = xs_dict_append(st, "id", mid);
  663. st = xs_dict_append(st, "uri", id);
  664. st = xs_dict_append(st, "url", id);
  665. st = xs_dict_append(st, "account", acct);
  666. const char *published = xs_dict_get(msg, "published");
  667. xs *fd = NULL;
  668. if (published)
  669. fd = mastoapi_date(published);
  670. else {
  671. xs *p = xs_str_iso_date(0);
  672. fd = mastoapi_date(p);
  673. }
  674. st = xs_dict_append(st, "created_at", fd);
  675. {
  676. const char *content = xs_dict_get(msg, "content");
  677. const char *name = xs_dict_get(msg, "name");
  678. xs *s1 = NULL;
  679. if (name && content)
  680. s1 = xs_fmt("%s<br><br>%s", name, content);
  681. else
  682. if (name)
  683. s1 = xs_dup(name);
  684. else
  685. if (content)
  686. s1 = xs_dup(content);
  687. else
  688. s1 = xs_str_new(NULL);
  689. st = xs_dict_append(st, "content", s1);
  690. }
  691. st = xs_dict_append(st, "visibility",
  692. is_msg_public(msg) ? "public" : "private");
  693. tmp = xs_dict_get(msg, "sensitive");
  694. if (xs_is_null(tmp))
  695. tmp = xs_stock(XSTYPE_FALSE);
  696. st = xs_dict_append(st, "sensitive", tmp);
  697. tmp = xs_dict_get(msg, "summary");
  698. if (xs_is_null(tmp))
  699. tmp = "";
  700. st = xs_dict_append(st, "spoiler_text", tmp);
  701. /* create the list of attachments */
  702. xs *attach = get_attachments(msg);
  703. {
  704. xs_list *p = attach;
  705. const xs_dict *v;
  706. xs *matt = xs_list_new();
  707. while (xs_list_iter(&p, &v)) {
  708. const char *type = xs_dict_get(v, "type");
  709. const char *o_href = xs_dict_get(v, "href");
  710. const char *name = xs_dict_get(v, "name");
  711. if (xs_match(type, "image/*|video/*|audio/*|Image|Video")) { /* */
  712. xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
  713. xs *href = make_url(o_href, proxy, 1);
  714. xs *d = xs_dict_new();
  715. d = xs_dict_append(d, "id", matteid);
  716. d = xs_dict_append(d, "url", href);
  717. d = xs_dict_append(d, "preview_url", href);
  718. d = xs_dict_append(d, "remote_url", href);
  719. d = xs_dict_append(d, "description", name);
  720. d = xs_dict_append(d, "type", (*type == 'v' || *type == 'V') ? "video" :
  721. (*type == 'a' || *type == 'A') ? "audio" : "image");
  722. matt = xs_list_append(matt, d);
  723. }
  724. }
  725. st = xs_dict_append(st, "media_attachments", matt);
  726. }
  727. {
  728. xs *ml = xs_list_new();
  729. xs *htl = xs_list_new();
  730. xs *eml = xs_list_new();
  731. const xs_list *tag = xs_dict_get(msg, "tag");
  732. int n = 0;
  733. xs *tag_list = NULL;
  734. if (xs_type(tag) == XSTYPE_DICT) {
  735. tag_list = xs_list_new();
  736. tag_list = xs_list_append(tag_list, tag);
  737. }
  738. else
  739. if (xs_type(tag) == XSTYPE_LIST)
  740. tag_list = xs_dup(tag);
  741. else
  742. tag_list = xs_list_new();
  743. tag = tag_list;
  744. const xs_dict *v;
  745. int c = 0;
  746. while (xs_list_next(tag, &v, &c)) {
  747. const char *type = xs_dict_get(v, "type");
  748. if (xs_is_null(type))
  749. continue;
  750. xs *d1 = xs_dict_new();
  751. if (strcmp(type, "Mention") == 0) {
  752. const char *name = xs_dict_get(v, "name");
  753. const char *href = xs_dict_get(v, "href");
  754. if (!xs_is_null(name) && !xs_is_null(href) &&
  755. (snac == NULL || strcmp(href, snac->actor) != 0)) {
  756. xs *nm = xs_strip_chars_i(xs_dup(name), "@");
  757. xs *id = xs_fmt("%d", n++);
  758. d1 = xs_dict_append(d1, "id", id);
  759. d1 = xs_dict_append(d1, "username", nm);
  760. d1 = xs_dict_append(d1, "acct", nm);
  761. d1 = xs_dict_append(d1, "url", href);
  762. ml = xs_list_append(ml, d1);
  763. }
  764. }
  765. else
  766. if (strcmp(type, "Hashtag") == 0) {
  767. const char *name = xs_dict_get(v, "name");
  768. const char *href = xs_dict_get(v, "href");
  769. if (!xs_is_null(name) && !xs_is_null(href)) {
  770. xs *nm = xs_strip_chars_i(xs_dup(name), "#");
  771. d1 = xs_dict_append(d1, "name", nm);
  772. d1 = xs_dict_append(d1, "url", href);
  773. htl = xs_list_append(htl, d1);
  774. }
  775. }
  776. else
  777. if (strcmp(type, "Emoji") == 0) {
  778. const char *name = xs_dict_get(v, "name");
  779. const xs_dict *icon = xs_dict_get(v, "icon");
  780. if (!xs_is_null(name) && !xs_is_null(icon)) {
  781. const char *o_url = xs_dict_get(icon, "url");
  782. if (!xs_is_null(o_url)) {
  783. xs *url = make_url(o_url, snac ? snac->actor : NULL, 1);
  784. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  785. d1 = xs_dict_append(d1, "shortcode", nm);
  786. d1 = xs_dict_append(d1, "url", url);
  787. d1 = xs_dict_append(d1, "static_url", url);
  788. d1 = xs_dict_append(d1, "visible_in_picker", xs_stock(XSTYPE_TRUE));
  789. d1 = xs_dict_append(d1, "category", "Emojis");
  790. eml = xs_list_append(eml, d1);
  791. }
  792. }
  793. }
  794. }
  795. st = xs_dict_append(st, "mentions", ml);
  796. st = xs_dict_append(st, "tags", htl);
  797. st = xs_dict_append(st, "emojis", eml);
  798. }
  799. xs_free(idx);
  800. xs_free(ixc);
  801. idx = object_likes(id);
  802. ixc = xs_number_new(xs_list_len(idx));
  803. st = xs_dict_append(st, "favourites_count", ixc);
  804. st = xs_dict_append(st, "favourited",
  805. (snac && xs_list_in(idx, snac->md5) != -1) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  806. xs_free(idx);
  807. xs_free(ixc);
  808. idx = object_announces(id);
  809. ixc = xs_number_new(xs_list_len(idx));
  810. st = xs_dict_append(st, "reblogs_count", ixc);
  811. st = xs_dict_append(st, "reblogged",
  812. (snac && xs_list_in(idx, snac->md5) != -1) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  813. /* get the last person who boosted this */
  814. xs *boosted_by_md5 = NULL;
  815. if (xs_list_len(idx))
  816. boosted_by_md5 = xs_dup(xs_list_get(idx, -1));
  817. xs_free(idx);
  818. xs_free(ixc);
  819. idx = object_children(id);
  820. ixc = xs_number_new(xs_list_len(idx));
  821. st = xs_dict_append(st, "replies_count", ixc);
  822. /* default in_reply_to values */
  823. st = xs_dict_append(st, "in_reply_to_id", xs_stock(XSTYPE_NULL));
  824. st = xs_dict_append(st, "in_reply_to_account_id", xs_stock(XSTYPE_NULL));
  825. tmp = get_in_reply_to(msg);
  826. if (!xs_is_null(tmp)) {
  827. xs *irto = NULL;
  828. if (valid_status(object_get(tmp, &irto))) {
  829. xs *irt_mid = mastoapi_id(irto);
  830. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  831. const char *at = NULL;
  832. if (!xs_is_null(at = get_atto(irto))) {
  833. xs *at_md5 = xs_md5_hex(at, strlen(at));
  834. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  835. }
  836. }
  837. }
  838. st = xs_dict_append(st, "reblog", xs_stock(XSTYPE_NULL));
  839. st = xs_dict_append(st, "card", xs_stock(XSTYPE_NULL));
  840. st = xs_dict_append(st, "language", "en");
  841. st = xs_dict_append(st, "filtered", xs_stock(XSTYPE_LIST));
  842. st = xs_dict_append(st, "muted", xs_stock(XSTYPE_FALSE));
  843. tmp = xs_dict_get(msg, "sourceContent");
  844. if (xs_is_null(tmp))
  845. tmp = "";
  846. st = xs_dict_append(st, "text", tmp);
  847. tmp = xs_dict_get(msg, "updated");
  848. xs *fd2 = NULL;
  849. if (xs_is_null(tmp))
  850. tmp = xs_stock(XSTYPE_NULL);
  851. else {
  852. fd2 = mastoapi_date(tmp);
  853. tmp = fd2;
  854. }
  855. st = xs_dict_append(st, "edited_at", tmp);
  856. if (strcmp(type, "Question") == 0) {
  857. xs *poll = mastoapi_poll(snac, msg);
  858. st = xs_dict_append(st, "poll", poll);
  859. }
  860. else
  861. st = xs_dict_append(st, "poll", xs_stock(XSTYPE_NULL));
  862. st = xs_dict_append(st, "bookmarked",
  863. (snac && is_bookmarked(snac, id)) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  864. st = xs_dict_append(st, "pinned",
  865. (snac && is_pinned(snac, id)) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  866. /* is it a boost? */
  867. if (!xs_is_null(boosted_by_md5)) {
  868. /* create a new dummy status, using st as the 'reblog' field */
  869. xs_dict *bst = xs_dup(st);
  870. xs *b_actor = NULL;
  871. if (valid_status(object_get_by_md5(boosted_by_md5, &b_actor))) {
  872. xs *b_acct = mastoapi_account(snac, b_actor);
  873. xs *fake_uri = NULL;
  874. if (snac)
  875. fake_uri = xs_fmt("%s/d/%s/Announce", snac->actor, mid);
  876. else
  877. fake_uri = xs_fmt("%s#%s", srv_baseurl, mid);
  878. bst = xs_dict_set(bst, "uri", fake_uri);
  879. bst = xs_dict_set(bst, "url", fake_uri);
  880. bst = xs_dict_set(bst, "account", b_acct);
  881. bst = xs_dict_set(bst, "content", "");
  882. bst = xs_dict_set(bst, "reblog", st);
  883. xs_free(st);
  884. st = bst;
  885. }
  886. else
  887. xs_free(bst);
  888. }
  889. return st;
  890. }
  891. xs_dict *mastoapi_relationship(snac *snac, const char *md5)
  892. {
  893. xs_dict *rel = NULL;
  894. xs *actor_o = NULL;
  895. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  896. rel = xs_dict_new();
  897. const char *actor = xs_dict_get(actor_o, "id");
  898. rel = xs_dict_append(rel, "id", md5);
  899. rel = xs_dict_append(rel, "following",
  900. following_check(snac, actor) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  901. rel = xs_dict_append(rel, "showing_reblogs", xs_stock(XSTYPE_TRUE));
  902. rel = xs_dict_append(rel, "notifying", xs_stock(XSTYPE_FALSE));
  903. rel = xs_dict_append(rel, "followed_by",
  904. follower_check(snac, actor) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  905. rel = xs_dict_append(rel, "blocking",
  906. is_muted(snac, actor) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  907. rel = xs_dict_append(rel, "muting", xs_stock(XSTYPE_FALSE));
  908. rel = xs_dict_append(rel, "muting_notifications", xs_stock(XSTYPE_FALSE));
  909. rel = xs_dict_append(rel, "requested", xs_stock(XSTYPE_FALSE));
  910. rel = xs_dict_append(rel, "domain_blocking", xs_stock(XSTYPE_FALSE));
  911. rel = xs_dict_append(rel, "endorsed", xs_stock(XSTYPE_FALSE));
  912. rel = xs_dict_append(rel, "note", "");
  913. }
  914. return rel;
  915. }
  916. int process_auth_token(snac *snac, const xs_dict *req)
  917. /* processes an authorization token, if there is one */
  918. {
  919. int logged_in = 0;
  920. const char *v;
  921. /* if there is an authorization field, try to validate it */
  922. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  923. xs *tokid = xs_replace_n(v, "Bearer ", "", 1);
  924. xs *token = token_get(tokid);
  925. if (token != NULL) {
  926. const char *uid = xs_dict_get(token, "uid");
  927. if (!xs_is_null(uid) && user_open(snac, uid)) {
  928. logged_in = 1;
  929. /* this counts as a 'login' */
  930. lastlog_write(snac, "mastoapi");
  931. srv_debug(2, xs_fmt("mastoapi auth: valid token for user '%s'", uid));
  932. }
  933. else
  934. srv_log(xs_fmt("mastoapi auth: corrupted token '%s'", tokid));
  935. }
  936. else
  937. srv_log(xs_fmt("mastoapi auth: invalid token '%s'", tokid));
  938. }
  939. return logged_in;
  940. }
  941. void credentials_get(char **body, char **ctype, int *status, snac snac)
  942. {
  943. xs *acct = xs_dict_new();
  944. const xs_val *bot = xs_dict_get(snac.config, "bot");
  945. acct = xs_dict_append(acct, "id", snac.md5);
  946. acct = xs_dict_append(acct, "username", xs_dict_get(snac.config, "uid"));
  947. acct = xs_dict_append(acct, "acct", xs_dict_get(snac.config, "uid"));
  948. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac.config, "name"));
  949. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac.config, "published"));
  950. acct = xs_dict_append(acct, "last_status_at", xs_dict_get(snac.config, "published"));
  951. acct = xs_dict_append(acct, "note", xs_dict_get(snac.config, "bio"));
  952. acct = xs_dict_append(acct, "url", snac.actor);
  953. acct = xs_dict_append(acct, "locked",
  954. xs_stock(xs_is_true(xs_dict_get(snac.config, "approve_followers")) ? XSTYPE_TRUE : XSTYPE_FALSE));
  955. acct = xs_dict_append(acct, "bot", xs_stock(xs_is_true(bot) ? XSTYPE_TRUE : XSTYPE_FALSE));
  956. acct = xs_dict_append(acct, "emojis", xs_stock(XSTYPE_LIST));
  957. xs *src = xs_json_loads("{\"privacy\":\"public\", \"language\":\"en\","
  958. "\"follow_requests_count\": 0,"
  959. "\"sensitive\":false,\"fields\":[],\"note\":\"\"}");
  960. /* some apps take the note from the source object */
  961. src = xs_dict_set(src, "note", xs_dict_get(snac.config, "bio"));
  962. src = xs_dict_set(src, "privacy", xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE ? "private" : "public");
  963. const xs_str *cw = xs_dict_get(snac.config, "cw");
  964. src = xs_dict_set(src, "sensitive",
  965. strcmp(cw, "open") == 0 ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  966. src = xs_dict_set(src, "bot", xs_stock(xs_is_true(bot) ? XSTYPE_TRUE : XSTYPE_FALSE));
  967. xs *avatar = NULL;
  968. const char *av = xs_dict_get(snac.config, "avatar");
  969. if (xs_is_null(av) || *av == '\0')
  970. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  971. else
  972. avatar = xs_dup(av);
  973. acct = xs_dict_append(acct, "avatar", avatar);
  974. acct = xs_dict_append(acct, "avatar_static", avatar);
  975. xs *header = NULL;
  976. const char *hd = xs_dict_get(snac.config, "header");
  977. if (!xs_is_null(hd))
  978. header = xs_dup(hd);
  979. else
  980. header = xs_str_new(NULL);
  981. acct = xs_dict_append(acct, "header", header);
  982. acct = xs_dict_append(acct, "header_static", header);
  983. const xs_dict *metadata = xs_dict_get(snac.config, "metadata");
  984. if (xs_type(metadata) == XSTYPE_DICT) {
  985. xs *fields = xs_list_new();
  986. const xs_str *k;
  987. const xs_str *v;
  988. xs_dict *val_links = snac.links;
  989. if (xs_is_null(val_links))
  990. val_links = xs_stock(XSTYPE_DICT);
  991. int c = 0;
  992. while (xs_dict_next(metadata, &k, &v, &c)) {
  993. xs *val_date = NULL;
  994. const xs_number *verified_time = xs_dict_get(val_links, v);
  995. if (xs_type(verified_time) == XSTYPE_NUMBER) {
  996. time_t t = xs_number_get(verified_time);
  997. if (t > 0)
  998. val_date = xs_str_utctime(t, ISO_DATE_SPEC);
  999. }
  1000. xs *d = xs_dict_new();
  1001. d = xs_dict_append(d, "name", k);
  1002. d = xs_dict_append(d, "value", v);
  1003. d = xs_dict_append(d, "verified_at",
  1004. xs_type(val_date) == XSTYPE_STRING && *val_date ? val_date : xs_stock(XSTYPE_NULL));
  1005. fields = xs_list_append(fields, d);
  1006. }
  1007. acct = xs_dict_set(acct, "fields", fields);
  1008. /* some apps take the fields from the source object */
  1009. src = xs_dict_set(src, "fields", fields);
  1010. }
  1011. acct = xs_dict_append(acct, "source", src);
  1012. acct = xs_dict_append(acct, "followers_count", xs_stock(0));
  1013. acct = xs_dict_append(acct, "following_count", xs_stock(0));
  1014. acct = xs_dict_append(acct, "statuses_count", xs_stock(0));
  1015. /* does this user want to publish their contact metrics? */
  1016. if (xs_is_true(xs_dict_get(snac.config, "show_contact_metrics"))) {
  1017. int fwing = following_list_len(&snac);
  1018. int fwers = follower_list_len(&snac);
  1019. xs *ni = xs_number_new(fwing);
  1020. xs *ne = xs_number_new(fwers);
  1021. acct = xs_dict_append(acct, "followers_count", ne);
  1022. acct = xs_dict_append(acct, "following_count", ni);
  1023. }
  1024. *body = xs_json_dumps(acct, 4);
  1025. *ctype = "application/json";
  1026. *status = HTTP_STATUS_OK;
  1027. }
  1028. xs_list *mastoapi_timeline(snac *user, const xs_dict *args, const char *index_fn)
  1029. {
  1030. xs_list *out = xs_list_new();
  1031. FILE *f;
  1032. char md5[MD5_HEX_SIZE];
  1033. if (dbglevel) {
  1034. xs *js = xs_json_dumps(args, 0);
  1035. srv_debug(1, xs_fmt("mastoapi_timeline args %s", js));
  1036. }
  1037. if ((f = fopen(index_fn, "r")) == NULL)
  1038. return out;
  1039. const char *max_id = xs_dict_get(args, "max_id");
  1040. const char *since_id = xs_dict_get(args, "since_id");
  1041. const char *min_id = xs_dict_get(args, "min_id"); /* unsupported old-to-new navigation */
  1042. const char *limit_s = xs_dict_get(args, "limit");
  1043. int (*iterator)(FILE *, char *);
  1044. int initial_status = 0;
  1045. int ascending = 0;
  1046. int limit = 0;
  1047. int cnt = 0;
  1048. if (!xs_is_null(limit_s))
  1049. limit = atoi(limit_s);
  1050. if (limit == 0)
  1051. limit = 20;
  1052. if (min_id) {
  1053. iterator = &index_asc_next;
  1054. initial_status = index_asc_first(f, md5, MID_TO_MD5(min_id));
  1055. ascending = 1;
  1056. }
  1057. else {
  1058. iterator = &index_desc_next;
  1059. initial_status = index_desc_first(f, md5, 0);
  1060. }
  1061. if (initial_status) {
  1062. do {
  1063. xs *msg = NULL;
  1064. /* only return entries older that max_id */
  1065. if (max_id) {
  1066. if (strcmp(md5, MID_TO_MD5(max_id)) == 0) {
  1067. max_id = NULL;
  1068. if (ascending)
  1069. break;
  1070. }
  1071. if (!ascending)
  1072. continue;
  1073. }
  1074. /* only returns entries newer than since_id */
  1075. if (since_id) {
  1076. if (strcmp(md5, MID_TO_MD5(since_id)) == 0) {
  1077. if (!ascending)
  1078. break;
  1079. since_id = NULL;
  1080. }
  1081. if (ascending)
  1082. continue;
  1083. }
  1084. /* get the entry */
  1085. if (user) {
  1086. if (!valid_status(timeline_get_by_md5(user, md5, &msg)))
  1087. continue;
  1088. }
  1089. else {
  1090. if (!valid_status(object_get_by_md5(md5, &msg)))
  1091. continue;
  1092. }
  1093. /* discard non-Notes */
  1094. const char *id = xs_dict_get(msg, "id");
  1095. const char *type = xs_dict_get(msg, "type");
  1096. if (!xs_match(type, POSTLIKE_OBJECT_TYPE))
  1097. continue;
  1098. if (id && is_instance_blocked(id))
  1099. continue;
  1100. const char *from = NULL;
  1101. if (strcmp(type, "Page") == 0)
  1102. from = xs_dict_get(msg, "audience");
  1103. if (from == NULL)
  1104. from = get_atto(msg);
  1105. if (from == NULL)
  1106. continue;
  1107. if (user) {
  1108. /* is this message from a person we don't follow? */
  1109. if (strcmp(from, user->actor) && !following_check(user, from)) {
  1110. /* discard if it was not boosted */
  1111. xs *idx = object_announces(id);
  1112. if (xs_list_len(idx) == 0)
  1113. continue;
  1114. }
  1115. /* discard notes from muted morons */
  1116. if (is_muted(user, from))
  1117. continue;
  1118. /* discard hidden notes */
  1119. if (is_hidden(user, id))
  1120. continue;
  1121. }
  1122. else {
  1123. /* skip non-public messages */
  1124. if (!is_msg_public(msg))
  1125. continue;
  1126. /* discard messages from private users */
  1127. if (is_msg_from_private_user(msg))
  1128. continue;
  1129. }
  1130. /* if it has a name and it's not an object that may have one,
  1131. it's a poll vote, so discard it */
  1132. if (!xs_is_null(xs_dict_get(msg, "name")) && !xs_match(type, "Page|Video|Audio|Event"))
  1133. continue;
  1134. /* convert the Note into a Mastodon status */
  1135. xs *st = mastoapi_status(user, msg);
  1136. if (st != NULL) {
  1137. if (ascending)
  1138. out = xs_list_insert(out, 0, st);
  1139. else
  1140. out = xs_list_append(out, st);
  1141. cnt++;
  1142. }
  1143. } while ((cnt < limit) && (*iterator)(f, md5));
  1144. }
  1145. int more = index_desc_next(f, md5);
  1146. fclose(f);
  1147. srv_debug(1, xs_fmt("mastoapi_timeline ret %d%s", cnt, more ? " (+)" : ""));
  1148. return out;
  1149. }
  1150. xs_str *timeline_link_header(const char *endpoint, xs_list *timeline)
  1151. /* returns a Link header with paging information */
  1152. {
  1153. xs_str *s = NULL;
  1154. if (xs_list_len(timeline) == 0)
  1155. return NULL;
  1156. const xs_dict *first_st = xs_list_get(timeline, 0);
  1157. const xs_dict *last_st = xs_list_get(timeline, -1);
  1158. const char *first_id = xs_dict_get(first_st, "id");
  1159. const char *last_id = xs_dict_get(last_st, "id");
  1160. const char *host = xs_dict_get(srv_config, "host");
  1161. const char *protocol = xs_dict_get_def(srv_config, "protocol", "https");
  1162. s = xs_fmt(
  1163. "<%s:/" "/%s%s?max_id=%s>; rel=\"next\", "
  1164. "<%s:/" "/%s%s?since_id=%s>; rel=\"prev\"",
  1165. protocol, host, endpoint, last_id,
  1166. protocol, host, endpoint, first_id);
  1167. srv_debug(1, xs_fmt("timeline_link_header %s", s));
  1168. return s;
  1169. }
  1170. xs_list *mastoapi_account_lists(snac *user, const char *uid)
  1171. /* returns the list of list an user is in */
  1172. {
  1173. xs_list *out = xs_list_new();
  1174. xs *actor_md5 = NULL;
  1175. xs *lol = list_maint(user, NULL, 0);
  1176. if (uid) {
  1177. if (!xs_is_hex(uid))
  1178. actor_md5 = xs_md5_hex(uid, strlen(uid));
  1179. else
  1180. actor_md5 = xs_dup(uid);
  1181. }
  1182. const xs_list *li;
  1183. xs_list_foreach(lol, li) {
  1184. const char *list_id = xs_list_get(li, 0);
  1185. const char *list_title = xs_list_get(li, 1);
  1186. if (uid) {
  1187. xs *users = list_content(user, list_id, NULL, 0);
  1188. if (xs_list_in(users, actor_md5) == -1)
  1189. continue;
  1190. }
  1191. xs *d = xs_dict_new();
  1192. d = xs_dict_append(d, "id", list_id);
  1193. d = xs_dict_append(d, "title", list_title);
  1194. d = xs_dict_append(d, "replies_policy", "list");
  1195. d = xs_dict_append(d, "exclusive", xs_stock(XSTYPE_FALSE));
  1196. out = xs_list_append(out, d);
  1197. }
  1198. return out;
  1199. }
  1200. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  1201. char **body, int *b_size, char **ctype, xs_str **link)
  1202. {
  1203. (void)b_size;
  1204. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1205. return 0;
  1206. int status = HTTP_STATUS_NOT_FOUND;
  1207. const xs_dict *args = xs_dict_get(req, "q_vars");
  1208. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1209. snac snac1 = {0};
  1210. int logged_in = process_auth_token(&snac1, req);
  1211. if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) { /** **/
  1212. if (logged_in) {
  1213. credentials_get(body, ctype, &status, snac1);
  1214. }
  1215. else {
  1216. status = HTTP_STATUS_UNPROCESSABLE_CONTENT; // (no login)
  1217. }
  1218. }
  1219. else
  1220. if (strcmp(cmd, "/v1/accounts/relationships") == 0) { /** **/
  1221. /* find if an account is followed, blocked, etc. */
  1222. /* the account to get relationships about is in args "id[]" */
  1223. if (logged_in) {
  1224. xs *res = xs_list_new();
  1225. const char *md5 = xs_dict_get(args, "id[]");
  1226. if (xs_is_null(md5))
  1227. md5 = xs_dict_get(args, "id");
  1228. if (!xs_is_null(md5)) {
  1229. if (xs_type(md5) == XSTYPE_LIST)
  1230. md5 = xs_list_get(md5, 0);
  1231. xs *rel = mastoapi_relationship(&snac1, md5);
  1232. if (rel != NULL)
  1233. res = xs_list_append(res, rel);
  1234. }
  1235. *body = xs_json_dumps(res, 4);
  1236. *ctype = "application/json";
  1237. status = HTTP_STATUS_OK;
  1238. }
  1239. else
  1240. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  1241. }
  1242. else
  1243. if (strcmp(cmd, "/v1/accounts/lookup") == 0) { /** **/
  1244. /* lookup an account */
  1245. const char *acct = xs_dict_get(args, "acct");
  1246. if (!xs_is_null(acct)) {
  1247. xs *s = xs_strip_chars_i(xs_dup(acct), "@");
  1248. xs *l = xs_split_n(s, "@", 1);
  1249. const char *uid = xs_list_get(l, 0);
  1250. const char *host = xs_list_get(l, 1);
  1251. if (uid && (!host || strcmp(host, xs_dict_get(srv_config, "host")) == 0)) {
  1252. snac user;
  1253. if (user_open(&user, uid)) {
  1254. xs *actor = msg_actor(&user);
  1255. xs *macct = mastoapi_account(NULL, actor);
  1256. *body = xs_json_dumps(macct, 4);
  1257. *ctype = "application/json";
  1258. status = HTTP_STATUS_OK;
  1259. user_free(&user);
  1260. }
  1261. }
  1262. }
  1263. }
  1264. else
  1265. if (xs_startswith(cmd, "/v1/accounts/")) { /** **/
  1266. /* account-related information */
  1267. xs *l = xs_split(cmd, "/");
  1268. const char *uid = xs_list_get(l, 3);
  1269. const char *opt = xs_list_get(l, 4);
  1270. if (uid != NULL) {
  1271. snac snac2;
  1272. xs *out = NULL;
  1273. xs *actor = NULL;
  1274. if (logged_in && strcmp(uid, "search") == 0) { /** **/
  1275. /* search for accounts starting with q */
  1276. const char *aq = xs_dict_get(args, "q");
  1277. if (!xs_is_null(aq)) {
  1278. xs *q = xs_utf8_to_lower(aq);
  1279. out = xs_list_new();
  1280. xs *wing = following_list(&snac1);
  1281. xs *wers = follower_list(&snac1);
  1282. xs *ulst = user_list();
  1283. xs_list *p;
  1284. const xs_str *v;
  1285. xs_set seen;
  1286. xs_set_init(&seen);
  1287. /* user relations */
  1288. xs_list *lsts[] = { wing, wers, NULL };
  1289. int n;
  1290. for (n = 0; (p = lsts[n]) != NULL; n++) {
  1291. while (xs_list_iter(&p, &v)) {
  1292. /* already seen? skip */
  1293. if (xs_set_add(&seen, v) == 0)
  1294. continue;
  1295. xs *actor = NULL;
  1296. if (valid_status(object_get(v, &actor))) {
  1297. const char *uname = xs_dict_get(actor, "preferredUsername");
  1298. if (!xs_is_null(uname)) {
  1299. xs *luname = xs_tolower_i(xs_dup(uname));
  1300. if (xs_startswith(luname, q)) {
  1301. xs *acct = mastoapi_account(&snac1, actor);
  1302. out = xs_list_append(out, acct);
  1303. }
  1304. }
  1305. }
  1306. }
  1307. }
  1308. /* local users */
  1309. p = ulst;
  1310. while (xs_list_iter(&p, &v)) {
  1311. snac user;
  1312. /* skip this same user */
  1313. if (strcmp(v, xs_dict_get(snac1.config, "uid")) == 0)
  1314. continue;
  1315. /* skip if the uid does not start with the query */
  1316. xs *v2 = xs_tolower_i(xs_dup(v));
  1317. if (!xs_startswith(v2, q))
  1318. continue;
  1319. if (user_open(&user, v)) {
  1320. /* if it's not already seen, add it */
  1321. if (xs_set_add(&seen, user.actor) == 1) {
  1322. xs *actor = msg_actor(&user);
  1323. xs *acct = mastoapi_account(&snac1, actor);
  1324. out = xs_list_append(out, acct);
  1325. }
  1326. user_free(&user);
  1327. }
  1328. }
  1329. xs_set_free(&seen);
  1330. }
  1331. }
  1332. else
  1333. /* is it a local user? */
  1334. if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
  1335. if (opt == NULL) {
  1336. /* account information */
  1337. actor = msg_actor(&snac2);
  1338. out = mastoapi_account(NULL, actor);
  1339. }
  1340. else
  1341. if (strcmp(opt, "statuses") == 0) { /** **/
  1342. /* the public list of posts of a user */
  1343. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256, NULL);
  1344. xs_list *p = timeline;
  1345. const xs_str *v;
  1346. out = xs_list_new();
  1347. while (xs_list_iter(&p, &v)) {
  1348. xs *msg = NULL;
  1349. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  1350. /* add only posts by the author */
  1351. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  1352. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  1353. xs *st = mastoapi_status(&snac2, msg);
  1354. if (st)
  1355. out = xs_list_append(out, st);
  1356. }
  1357. }
  1358. }
  1359. }
  1360. else
  1361. if (strcmp(opt, "featured_tags") == 0) {
  1362. /* snac doesn't have features tags, yet? */
  1363. /* implement empty response so apps like Tokodon don't show an error */
  1364. out = xs_list_new();
  1365. }
  1366. else
  1367. if (strcmp(opt, "following") == 0) {
  1368. xs *wing = following_list(&snac1);
  1369. out = xs_list_new();
  1370. int c = 0;
  1371. const char *v;
  1372. while (xs_list_next(wing, &v, &c)) {
  1373. xs *actor = NULL;
  1374. if (valid_status(object_get(v, &actor))) {
  1375. xs *acct = mastoapi_account(NULL, actor);
  1376. out = xs_list_append(out, acct);
  1377. }
  1378. }
  1379. }
  1380. else
  1381. if (strcmp(opt, "followers") == 0) {
  1382. out = xs_list_new();
  1383. }
  1384. else
  1385. if (strcmp(opt, "lists") == 0) {
  1386. out = mastoapi_account_lists(&snac1, uid);
  1387. }
  1388. user_free(&snac2);
  1389. }
  1390. else {
  1391. /* try the uid as the md5 of a possibly loaded actor */
  1392. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  1393. if (opt == NULL) {
  1394. /* account information */
  1395. out = mastoapi_account(&snac1, actor);
  1396. }
  1397. else
  1398. if (strcmp(opt, "statuses") == 0) {
  1399. /* we don't serve statuses of others; return the empty list */
  1400. out = xs_list_new();
  1401. }
  1402. else
  1403. if (strcmp(opt, "featured_tags") == 0) {
  1404. /* snac doesn't have features tags, yet? */
  1405. /* implement empty response so apps like Tokodon don't show an error */
  1406. out = xs_list_new();
  1407. }
  1408. else
  1409. if (strcmp(opt, "lists") == 0) {
  1410. out = mastoapi_account_lists(&snac1, uid);
  1411. }
  1412. }
  1413. }
  1414. if (out != NULL) {
  1415. *body = xs_json_dumps(out, 4);
  1416. *ctype = "application/json";
  1417. status = HTTP_STATUS_OK;
  1418. }
  1419. }
  1420. }
  1421. else
  1422. if (strcmp(cmd, "/v1/timelines/home") == 0) { /** **/
  1423. /* the private timeline */
  1424. if (logged_in) {
  1425. xs *ifn = user_index_fn(&snac1, "private");
  1426. xs *out = mastoapi_timeline(&snac1, args, ifn);
  1427. *link = timeline_link_header("/api/v1/timelines/home", out);
  1428. *body = xs_json_dumps(out, 4);
  1429. *ctype = "application/json";
  1430. status = HTTP_STATUS_OK;
  1431. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  1432. }
  1433. else {
  1434. status = HTTP_STATUS_UNAUTHORIZED;
  1435. }
  1436. }
  1437. else
  1438. if (strcmp(cmd, "/v1/timelines/public") == 0) { /** **/
  1439. /* the instance public timeline (public timelines for all users) */
  1440. xs *ifn = instance_index_fn();
  1441. xs *out = mastoapi_timeline(NULL, args, ifn);
  1442. *body = xs_json_dumps(out, 4);
  1443. *ctype = "application/json";
  1444. status = HTTP_STATUS_OK;
  1445. }
  1446. else
  1447. if (xs_startswith(cmd, "/v1/timelines/tag/")) { /** **/
  1448. /* get the tag */
  1449. xs *l = xs_split(cmd, "/");
  1450. const char *tag = xs_list_get(l, -1);
  1451. xs *ifn = tag_fn(tag);
  1452. xs *out = mastoapi_timeline(NULL, args, ifn);
  1453. *body = xs_json_dumps(out, 4);
  1454. *ctype = "application/json";
  1455. status = HTTP_STATUS_OK;
  1456. }
  1457. else
  1458. if (xs_startswith(cmd, "/v1/timelines/list/")) { /** **/
  1459. /* get the list id */
  1460. if (logged_in) {
  1461. xs *l = xs_split(cmd, "/");
  1462. const char *list = xs_list_get(l, -1);
  1463. xs *ifn = list_timeline_fn(&snac1, list);
  1464. xs *out = mastoapi_timeline(NULL, args, ifn);
  1465. *body = xs_json_dumps(out, 4);
  1466. *ctype = "application/json";
  1467. status = HTTP_STATUS_OK;
  1468. }
  1469. else
  1470. status = HTTP_STATUS_MISDIRECTED_REQUEST;
  1471. }
  1472. else
  1473. if (strcmp(cmd, "/v1/conversations") == 0) { /** **/
  1474. /* TBD */
  1475. *body = xs_dup("[]");
  1476. *ctype = "application/json";
  1477. status = HTTP_STATUS_OK;
  1478. }
  1479. else
  1480. if (strcmp(cmd, "/v1/notifications") == 0) { /** **/
  1481. if (logged_in) {
  1482. xs *l = notify_list(&snac1, 0, 64);
  1483. xs *out = xs_list_new();
  1484. const char *v;
  1485. const xs_list *excl = xs_dict_get(args, "exclude_types[]");
  1486. const xs_list *incl = xs_dict_get(args, "types[]");
  1487. const char *min_id = xs_dict_get(args, "min_id");
  1488. const char *max_id = xs_dict_get(args, "max_id");
  1489. const char *limit = xs_dict_get(args, "limit");
  1490. int limit_count = 0;
  1491. if (xs_is_string(limit)) {
  1492. limit_count = atoi(limit);
  1493. }
  1494. if (dbglevel) {
  1495. xs *js = xs_json_dumps(args, 0);
  1496. srv_debug(1, xs_fmt("mastoapi_notifications args %s", js));
  1497. }
  1498. xs_list_foreach(l, v) {
  1499. xs *noti = notify_get(&snac1, v);
  1500. if (noti == NULL)
  1501. continue;
  1502. const char *type = xs_dict_get(noti, "type");
  1503. const char *utype = xs_dict_get(noti, "utype");
  1504. const char *objid = xs_dict_get(noti, "objid");
  1505. const char *id = xs_dict_get(noti, "id");
  1506. const char *actid = xs_dict_get(noti, "actor");
  1507. xs *fid = xs_replace(id, ".", "");
  1508. xs *actor = NULL;
  1509. xs *entry = NULL;
  1510. if (!valid_status(actor_get(actid, &actor)))
  1511. continue;
  1512. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  1513. continue;
  1514. if (is_hidden(&snac1, objid))
  1515. continue;
  1516. if (max_id) {
  1517. if (strcmp(fid, max_id) == 0)
  1518. max_id = NULL;
  1519. continue;
  1520. }
  1521. if (min_id) {
  1522. if (strcmp(fid, min_id) <= 0) {
  1523. continue;
  1524. }
  1525. }
  1526. /* convert the type */
  1527. if (strcmp(type, "Like") == 0 || strcmp(type, "EmojiReact") == 0)
  1528. type = "favourite";
  1529. else
  1530. if (strcmp(type, "Announce") == 0)
  1531. type = "reblog";
  1532. else
  1533. if (strcmp(type, "Follow") == 0)
  1534. type = "follow";
  1535. else
  1536. if (strcmp(type, "Create") == 0)
  1537. type = "mention";
  1538. else
  1539. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  1540. type = "poll";
  1541. else
  1542. continue;
  1543. /* excluded type? */
  1544. if (xs_is_list(excl) && xs_list_in(excl, type) != -1)
  1545. continue;
  1546. /* included type? */
  1547. if (xs_is_list(incl) && xs_list_in(incl, type) == -1)
  1548. continue;
  1549. xs *mn = xs_dict_new();
  1550. mn = xs_dict_append(mn, "type", type);
  1551. mn = xs_dict_append(mn, "id", fid);
  1552. mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
  1553. xs *acct = mastoapi_account(&snac1, actor);
  1554. if (acct == NULL)
  1555. continue;
  1556. mn = xs_dict_append(mn, "account", acct);
  1557. if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
  1558. xs *st = mastoapi_status(&snac1, entry);
  1559. if (st)
  1560. mn = xs_dict_append(mn, "status", st);
  1561. }
  1562. out = xs_list_append(out, mn);
  1563. if (--limit_count <= 0)
  1564. break;
  1565. }
  1566. srv_debug(1, xs_fmt("mastoapi_notifications count %d", xs_list_len(out)));
  1567. *body = xs_json_dumps(out, 4);
  1568. *ctype = "application/json";
  1569. status = HTTP_STATUS_OK;
  1570. }
  1571. else
  1572. status = HTTP_STATUS_UNAUTHORIZED;
  1573. }
  1574. else
  1575. if (strcmp(cmd, "/v1/filters") == 0) { /** **/
  1576. /* snac will never have filters */
  1577. *body = xs_dup("[]");
  1578. *ctype = "application/json";
  1579. status = HTTP_STATUS_OK;
  1580. }
  1581. else
  1582. if (strcmp(cmd, "/v2/filters") == 0) { /** **/
  1583. /* snac will never have filters
  1584. * but still, without a v2 endpoint a short delay is introduced
  1585. * in some apps */
  1586. *body = xs_dup("[]");
  1587. *ctype = "application/json";
  1588. status = HTTP_STATUS_OK;
  1589. }
  1590. else
  1591. if (strcmp(cmd, "/v1/favourites") == 0) { /** **/
  1592. /* snac will never support a list of favourites */
  1593. *body = xs_dup("[]");
  1594. *ctype = "application/json";
  1595. status = HTTP_STATUS_OK;
  1596. }
  1597. else
  1598. if (strcmp(cmd, "/v1/bookmarks") == 0) { /** **/
  1599. if (logged_in) {
  1600. xs *ifn = bookmark_index_fn(&snac1);
  1601. xs *out = mastoapi_timeline(&snac1, args, ifn);
  1602. *body = xs_json_dumps(out, 4);
  1603. *ctype = "application/json";
  1604. status = HTTP_STATUS_OK;
  1605. }
  1606. else
  1607. status = HTTP_STATUS_UNAUTHORIZED;
  1608. }
  1609. else
  1610. if (strcmp(cmd, "/v1/lists") == 0) { /** list of lists **/
  1611. if (logged_in) {
  1612. xs *l = mastoapi_account_lists(&snac1, NULL);
  1613. *body = xs_json_dumps(l, 4);
  1614. *ctype = "application/json";
  1615. status = HTTP_STATUS_OK;
  1616. }
  1617. else
  1618. status = HTTP_STATUS_UNAUTHORIZED;
  1619. }
  1620. else
  1621. if (xs_startswith(cmd, "/v1/lists/")) { /** list information **/
  1622. if (logged_in) {
  1623. xs *l = xs_split(cmd, "/");
  1624. const char *p = xs_list_get(l, -1);
  1625. if (p) {
  1626. if (strcmp(p, "accounts") == 0) {
  1627. p = xs_list_get(l, -2);
  1628. if (p && xs_is_hex(p)) {
  1629. xs *actors = list_content(&snac1, p, NULL, 0);
  1630. xs *out = xs_list_new();
  1631. int c = 0;
  1632. const char *v;
  1633. while (xs_list_next(actors, &v, &c)) {
  1634. xs *actor = NULL;
  1635. if (valid_status(object_get_by_md5(v, &actor))) {
  1636. xs *acct = mastoapi_account(&snac1, actor);
  1637. out = xs_list_append(out, acct);
  1638. }
  1639. }
  1640. *body = xs_json_dumps(out, 4);
  1641. *ctype = "application/json";
  1642. status = HTTP_STATUS_OK;
  1643. }
  1644. }
  1645. else
  1646. if (xs_is_hex(p)) {
  1647. xs *out = xs_list_new();
  1648. xs *lol = list_maint(&snac1, NULL, 0);
  1649. int c = 0;
  1650. const xs_list *v;
  1651. while (xs_list_next(lol, &v, &c)) {
  1652. const char *id = xs_list_get(v, 0);
  1653. if (id && strcmp(id, p) == 0) {
  1654. xs *d = xs_dict_new();
  1655. d = xs_dict_append(d, "id", p);
  1656. d = xs_dict_append(d, "title", xs_list_get(v, 1));
  1657. d = xs_dict_append(d, "replies_policy", "list");
  1658. d = xs_dict_append(d, "exclusive", xs_stock(XSTYPE_FALSE));
  1659. out = xs_dup(d);
  1660. break;
  1661. }
  1662. }
  1663. *body = xs_json_dumps(out, 4);
  1664. *ctype = "application/json";
  1665. status = HTTP_STATUS_OK;
  1666. }
  1667. }
  1668. }
  1669. else
  1670. status = HTTP_STATUS_UNAUTHORIZED;
  1671. }
  1672. else
  1673. if (strcmp(cmd, "/v1/scheduled_statuses") == 0) { /** **/
  1674. /* snac does not schedule notes */
  1675. *body = xs_dup("[]");
  1676. *ctype = "application/json";
  1677. status = HTTP_STATUS_OK;
  1678. }
  1679. else
  1680. if (strcmp(cmd, "/v1/follow_requests") == 0) { /** **/
  1681. if (logged_in) {
  1682. xs *pend = pending_list(&snac1);
  1683. xs *resp = xs_list_new();
  1684. const char *id;
  1685. xs_list_foreach(pend, id) {
  1686. xs *actor = NULL;
  1687. if (valid_status(object_get(id, &actor))) {
  1688. xs *acct = mastoapi_account(&snac1, actor);
  1689. if (acct)
  1690. resp = xs_list_append(resp, acct);
  1691. }
  1692. }
  1693. *body = xs_json_dumps(resp, 4);
  1694. *ctype = "application/json";
  1695. status = HTTP_STATUS_OK;
  1696. }
  1697. }
  1698. else
  1699. if (strcmp(cmd, "/v1/announcements") == 0) { /** **/
  1700. if (logged_in) {
  1701. xs *resp = xs_list_new();
  1702. double la = 0.0;
  1703. xs *user_la = xs_dup(xs_dict_get(snac1.config, "last_announcement"));
  1704. if (user_la != NULL)
  1705. la = xs_number_get(user_la);
  1706. xs *val_date = xs_str_utctime(la, ISO_DATE_SPEC);
  1707. /* contrary to html, we always send the announcement and set the read flag instead */
  1708. const t_announcement *annce = announcement(la);
  1709. if (annce != NULL && annce->text != NULL) {
  1710. xs *an = xs_dict_new();
  1711. xs *id = xs_fmt("%d", annce->timestamp);
  1712. xs *ct = xs_fmt("<p>%s</p>", annce->text);
  1713. an = xs_dict_set(an, "id", id);
  1714. an = xs_dict_set(an, "content", ct);
  1715. an = xs_dict_set(an, "starts_at", xs_stock(XSTYPE_NULL));
  1716. an = xs_dict_set(an, "ends_at", xs_stock(XSTYPE_NULL));
  1717. an = xs_dict_set(an, "all_day", xs_stock(XSTYPE_TRUE));
  1718. an = xs_dict_set(an, "published_at", val_date);
  1719. an = xs_dict_set(an, "updated_at", val_date);
  1720. an = xs_dict_set(an, "read", (annce->timestamp >= la)
  1721. ? xs_stock(XSTYPE_FALSE) : xs_stock(XSTYPE_TRUE));
  1722. an = xs_dict_set(an, "mentions", xs_stock(XSTYPE_LIST));
  1723. an = xs_dict_set(an, "statuses", xs_stock(XSTYPE_LIST));
  1724. an = xs_dict_set(an, "tags", xs_stock(XSTYPE_LIST));
  1725. an = xs_dict_set(an, "emojis", xs_stock(XSTYPE_LIST));
  1726. an = xs_dict_set(an, "reactions", xs_stock(XSTYPE_LIST));
  1727. resp = xs_list_append(resp, an);
  1728. }
  1729. *body = xs_json_dumps(resp, 4);
  1730. *ctype = "application/json";
  1731. status = HTTP_STATUS_OK;
  1732. }
  1733. }
  1734. else
  1735. if (strcmp(cmd, "/v1/custom_emojis") == 0) { /** **/
  1736. xs *emo = emojis();
  1737. xs *list = xs_list_new();
  1738. int c = 0;
  1739. const xs_str *k;
  1740. const xs_val *v;
  1741. while(xs_dict_next(emo, &k, &v, &c)) {
  1742. xs *current = xs_dict_new();
  1743. if (xs_startswith(v, "https://") && xs_startswith((xs_mime_by_ext(v)), "image/")) {
  1744. /* remove first and last colon */
  1745. xs *shortcode = xs_replace(k, ":", "");
  1746. current = xs_dict_append(current, "shortcode", shortcode);
  1747. current = xs_dict_append(current, "url", v);
  1748. current = xs_dict_append(current, "static_url", v);
  1749. current = xs_dict_append(current, "visible_in_picker", xs_stock(XSTYPE_TRUE));
  1750. list = xs_list_append(list, current);
  1751. }
  1752. }
  1753. *body = xs_json_dumps(list, 0);
  1754. *ctype = "application/json";
  1755. status = HTTP_STATUS_OK;
  1756. }
  1757. else
  1758. if (strcmp(cmd, "/v1/instance") == 0) { /** **/
  1759. /* returns an instance object */
  1760. xs *ins = xs_dict_new();
  1761. const char *host = xs_dict_get(srv_config, "host");
  1762. const char *title = xs_dict_get(srv_config, "title");
  1763. const char *sdesc = xs_dict_get(srv_config, "short_description");
  1764. ins = xs_dict_append(ins, "uri", host);
  1765. ins = xs_dict_append(ins, "domain", host);
  1766. ins = xs_dict_append(ins, "title", title && *title ? title : host);
  1767. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  1768. ins = xs_dict_append(ins, "source_url", WHAT_IS_SNAC_URL);
  1769. ins = xs_dict_append(ins, "description", host);
  1770. ins = xs_dict_append(ins, "short_description", sdesc && *sdesc ? sdesc : host);
  1771. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  1772. ins = xs_dict_append(ins, "thumbnail", susie);
  1773. const char *v = xs_dict_get(srv_config, "admin_email");
  1774. if (xs_is_null(v) || *v == '\0')
  1775. v = "admin@localhost";
  1776. ins = xs_dict_append(ins, "email", v);
  1777. ins = xs_dict_append(ins, "rules", xs_stock(XSTYPE_LIST));
  1778. xs *l1 = xs_list_append(xs_list_new(), "en");
  1779. ins = xs_dict_append(ins, "languages", l1);
  1780. xs *wss = xs_fmt("wss:/" "/%s", xs_dict_get(srv_config, "host"));
  1781. xs *urls = xs_dict_new();
  1782. urls = xs_dict_append(urls, "streaming_api", wss);
  1783. ins = xs_dict_append(ins, "urls", urls);
  1784. xs *d2 = xs_dict_append(xs_dict_new(), "user_count", xs_stock(0));
  1785. d2 = xs_dict_append(d2, "status_count", xs_stock(0));
  1786. d2 = xs_dict_append(d2, "domain_count", xs_stock(0));
  1787. ins = xs_dict_append(ins, "stats", d2);
  1788. ins = xs_dict_append(ins, "registrations", xs_stock(XSTYPE_FALSE));
  1789. ins = xs_dict_append(ins, "approval_required", xs_stock(XSTYPE_FALSE));
  1790. ins = xs_dict_append(ins, "invites_enabled", xs_stock(XSTYPE_FALSE));
  1791. xs *cfg = xs_dict_new();
  1792. {
  1793. xs *d11 = xs_json_loads("{\"characters_reserved_per_url\":32,"
  1794. "\"max_characters\":100000,\"max_media_attachments\":4}");
  1795. const xs_number *max_attachments = xs_dict_get(srv_config, "max_attachments");
  1796. if (xs_type(max_attachments) == XSTYPE_NUMBER)
  1797. d11 = xs_dict_set(d11, "max_media_attachments", max_attachments);
  1798. cfg = xs_dict_append(cfg, "statuses", d11);
  1799. xs *d12 = xs_json_loads("{\"max_featured_tags\":0}");
  1800. cfg = xs_dict_append(cfg, "accounts", d12);
  1801. xs *d13 = xs_json_loads("{\"image_matrix_limit\":33177600,"
  1802. "\"image_size_limit\":16777216,"
  1803. "\"video_frame_rate_limit\":120,"
  1804. "\"video_matrix_limit\":8294400,"
  1805. "\"video_size_limit\":103809024}"
  1806. );
  1807. {
  1808. /* get the supported mime types from the internal list */
  1809. const char **p = xs_mime_types;
  1810. xs_set mtypes;
  1811. xs_set_init(&mtypes);
  1812. while (*p) {
  1813. const char *type = p[1];
  1814. if (xs_startswith(type, "image/") ||
  1815. xs_startswith(type, "video/") ||
  1816. xs_startswith(type, "audio/"))
  1817. xs_set_add(&mtypes, type);
  1818. p += 2;
  1819. }
  1820. xs *l = xs_set_result(&mtypes);
  1821. d13 = xs_dict_append(d13, "supported_mime_types", l);
  1822. }
  1823. cfg = xs_dict_append(cfg, "media_attachments", d13);
  1824. xs *d14 = xs_json_loads("{\"max_characters_per_option\":50,"
  1825. "\"max_expiration\":2629746,"
  1826. "\"max_options\":8,\"min_expiration\":300}");
  1827. cfg = xs_dict_append(cfg, "polls", d14);
  1828. }
  1829. ins = xs_dict_append(ins, "configuration", cfg);
  1830. const char *admin_account = xs_dict_get(srv_config, "admin_account");
  1831. if (!xs_is_null(admin_account) && *admin_account) {
  1832. snac admin;
  1833. if (user_open(&admin, admin_account)) {
  1834. xs *actor = msg_actor(&admin);
  1835. xs *acct = mastoapi_account(NULL, actor);
  1836. ins = xs_dict_append(ins, "contact_account", acct);
  1837. user_free(&admin);
  1838. }
  1839. }
  1840. *body = xs_json_dumps(ins, 4);
  1841. *ctype = "application/json";
  1842. status = HTTP_STATUS_OK;
  1843. }
  1844. else
  1845. if (strcmp(cmd, "/v1/instance/peers") == 0) { /** **/
  1846. /* get the collected inbox list as the instances "this domain is aware of" */
  1847. xs *list = inbox_list();
  1848. xs *peers = xs_list_new();
  1849. const char *inbox;
  1850. xs_list_foreach(list, inbox) {
  1851. xs *l = xs_split(inbox, "/");
  1852. const char *domain = xs_list_get(l, 2);
  1853. if (xs_is_string(domain))
  1854. peers = xs_list_append(peers, domain);
  1855. }
  1856. *body = xs_json_dumps(peers, 4);
  1857. *ctype = "application/json";
  1858. status = HTTP_STATUS_OK;
  1859. }
  1860. else
  1861. if (xs_startswith(cmd, "/v1/statuses/")) { /** **/
  1862. /* information about a status */
  1863. if (logged_in) {
  1864. xs *l = xs_split(cmd, "/");
  1865. const char *id = xs_list_get(l, 3);
  1866. const char *op = xs_list_get(l, 4);
  1867. if (!xs_is_null(id)) {
  1868. xs *msg = NULL;
  1869. xs *out = NULL;
  1870. /* skip the 'fake' part of the id */
  1871. id = MID_TO_MD5(id);
  1872. if (valid_status(object_get_by_md5(id, &msg))) {
  1873. if (op == NULL) {
  1874. if (!is_muted(&snac1, get_atto(msg))) {
  1875. /* return the status itself */
  1876. out = mastoapi_status(&snac1, msg);
  1877. }
  1878. }
  1879. else
  1880. if (strcmp(op, "context") == 0) { /** **/
  1881. /* return ancestors and children */
  1882. xs *anc = xs_list_new();
  1883. xs *des = xs_list_new();
  1884. xs_list *p;
  1885. const xs_str *v;
  1886. char pid[MD5_HEX_SIZE];
  1887. /* build the [grand]parent list, moving up */
  1888. strncpy(pid, id, sizeof(pid));
  1889. while (object_parent(pid, pid)) {
  1890. xs *m2 = NULL;
  1891. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  1892. xs *st = mastoapi_status(&snac1, m2);
  1893. if (st)
  1894. anc = xs_list_insert(anc, 0, st);
  1895. }
  1896. else
  1897. break;
  1898. }
  1899. /* build the children list */
  1900. xs *children = object_children(xs_dict_get(msg, "id"));
  1901. p = children;
  1902. while (xs_list_iter(&p, &v)) {
  1903. xs *m2 = NULL;
  1904. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  1905. if (xs_is_null(xs_dict_get(m2, "name"))) {
  1906. xs *st = mastoapi_status(&snac1, m2);
  1907. if (st)
  1908. des = xs_list_append(des, st);
  1909. }
  1910. }
  1911. }
  1912. out = xs_dict_new();
  1913. out = xs_dict_append(out, "ancestors", anc);
  1914. out = xs_dict_append(out, "descendants", des);
  1915. }
  1916. else
  1917. if (strcmp(op, "reblogged_by") == 0 || /** **/
  1918. strcmp(op, "favourited_by") == 0) { /** **/
  1919. /* return the list of people who liked or boosted this */
  1920. out = xs_list_new();
  1921. xs *l = NULL;
  1922. if (op[0] == 'r')
  1923. l = object_announces(xs_dict_get(msg, "id"));
  1924. else
  1925. l = object_likes(xs_dict_get(msg, "id"));
  1926. xs_list *p = l;
  1927. const xs_str *v;
  1928. while (xs_list_iter(&p, &v)) {
  1929. xs *actor2 = NULL;
  1930. if (valid_status(object_get_by_md5(v, &actor2))) {
  1931. xs *acct2 = mastoapi_account(&snac1, actor2);
  1932. out = xs_list_append(out, acct2);
  1933. }
  1934. }
  1935. }
  1936. else
  1937. if (strcmp(op, "source") == 0) { /** **/
  1938. out = xs_dict_new();
  1939. /* get the mastoapi status id */
  1940. out = xs_dict_append(out, "id", xs_list_get(l, 3));
  1941. out = xs_dict_append(out, "text", xs_dict_get(msg, "sourceContent"));
  1942. out = xs_dict_append(out, "spoiler_text", xs_dict_get(msg, "summary"));
  1943. }
  1944. }
  1945. else
  1946. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  1947. if (out != NULL) {
  1948. *body = xs_json_dumps(out, 4);
  1949. *ctype = "application/json";
  1950. status = HTTP_STATUS_OK;
  1951. }
  1952. }
  1953. }
  1954. else
  1955. status = HTTP_STATUS_UNAUTHORIZED;
  1956. }
  1957. else
  1958. if (strcmp(cmd, "/v1/preferences") == 0) { /** **/
  1959. *body = xs_dup("{}");
  1960. *ctype = "application/json";
  1961. status = HTTP_STATUS_OK;
  1962. }
  1963. else
  1964. if (strcmp(cmd, "/v1/markers") == 0) { /** **/
  1965. if (logged_in) {
  1966. const xs_list *timeline = xs_dict_get(args, "timeline[]");
  1967. xs_str *json = NULL;
  1968. if (!xs_is_null(timeline))
  1969. json = xs_json_dumps(markers_get(&snac1, timeline), 4);
  1970. if (!xs_is_null(json))
  1971. *body = json;
  1972. else
  1973. *body = xs_dup("{}");
  1974. *ctype = "application/json";
  1975. status = HTTP_STATUS_OK;
  1976. }
  1977. else
  1978. status = HTTP_STATUS_UNAUTHORIZED;
  1979. }
  1980. else
  1981. if (strcmp(cmd, "/v1/followed_tags") == 0) { /** **/
  1982. *body = xs_dup("[]");
  1983. *ctype = "application/json";
  1984. status = HTTP_STATUS_OK;
  1985. }
  1986. else
  1987. if (strcmp(cmd, "/v1/trends/tags") == 0) { /** **/
  1988. *body = xs_dup("[]");
  1989. *ctype = "application/json";
  1990. status = HTTP_STATUS_OK;
  1991. }
  1992. else
  1993. if (strcmp(cmd, "/v1/trends/statuses") == 0) { /** **/
  1994. *body = xs_dup("[]");
  1995. *ctype = "application/json";
  1996. status = HTTP_STATUS_OK;
  1997. }
  1998. else
  1999. if (strcmp(cmd, "/v2/search") == 0) { /** **/
  2000. if (logged_in) {
  2001. const char *q = xs_dict_get(args, "q");
  2002. const char *type = xs_dict_get(args, "type");
  2003. const char *offset = xs_dict_get(args, "offset");
  2004. xs *acl = xs_list_new();
  2005. xs *stl = xs_list_new();
  2006. xs *htl = xs_list_new();
  2007. xs *res = xs_dict_new();
  2008. if (xs_is_null(offset) || strcmp(offset, "0") == 0) {
  2009. /* reply something only for offset 0; otherwise,
  2010. apps like Tusky keep asking again and again */
  2011. if (xs_startswith(q, "https://")) {
  2012. if (!timeline_here(&snac1, q)) {
  2013. xs *object = NULL;
  2014. int status;
  2015. status = activitypub_request(&snac1, q, &object);
  2016. snac_debug(&snac1, 1, xs_fmt("Request searched URL %s %d", q, status));
  2017. if (valid_status(status)) {
  2018. /* got it; also request the actor */
  2019. const char *attr_to = get_atto(object);
  2020. xs *actor_obj = NULL;
  2021. if (!xs_is_null(attr_to)) {
  2022. status = actor_request(&snac1, attr_to, &actor_obj);
  2023. snac_debug(&snac1, 1, xs_fmt("Request author %s of %s %d", attr_to, q, status));
  2024. if (valid_status(status)) {
  2025. /* add the actor */
  2026. actor_add(attr_to, actor_obj);
  2027. /* add the post to the timeline */
  2028. timeline_add(&snac1, q, object);
  2029. }
  2030. }
  2031. }
  2032. }
  2033. }
  2034. if (!xs_is_null(q)) {
  2035. if (xs_is_null(type) || strcmp(type, "accounts") == 0) {
  2036. /* do a webfinger query */
  2037. char *actor = NULL;
  2038. char *user = NULL;
  2039. if (valid_status(webfinger_request(q, &actor, &user)) && actor) {
  2040. xs *actor_o = NULL;
  2041. if (valid_status(actor_request(&snac1, actor, &actor_o))) {
  2042. xs *acct = mastoapi_account(NULL, actor_o);
  2043. acl = xs_list_append(acl, acct);
  2044. if (!object_here(actor))
  2045. object_add(actor, actor_o);
  2046. }
  2047. }
  2048. }
  2049. if (xs_is_null(type) || strcmp(type, "hashtags") == 0) {
  2050. /* search this tag */
  2051. xs *tl = tag_search((char *)q, 0, 1);
  2052. if (xs_list_len(tl)) {
  2053. xs *d = xs_dict_new();
  2054. d = xs_dict_append(d, "name", q);
  2055. xs *url = xs_fmt("%s?t=%s", srv_baseurl, q);
  2056. d = xs_dict_append(d, "url", url);
  2057. d = xs_dict_append(d, "history", xs_stock(XSTYPE_LIST));
  2058. htl = xs_list_append(htl, d);
  2059. }
  2060. }
  2061. if (xs_is_null(type) || strcmp(type, "statuses") == 0) {
  2062. int to = 0;
  2063. int cnt = 40;
  2064. xs *tl = content_search(&snac1, q, 1, 0, cnt, 0, &to);
  2065. int c = 0;
  2066. const char *v;
  2067. while (xs_list_next(tl, &v, &c) && --cnt) {
  2068. xs *post = NULL;
  2069. if (!valid_status(timeline_get_by_md5(&snac1, v, &post)))
  2070. continue;
  2071. xs *s = mastoapi_status(&snac1, post);
  2072. if (!xs_is_null(s))
  2073. stl = xs_list_append(stl, s);
  2074. }
  2075. }
  2076. }
  2077. }
  2078. res = xs_dict_append(res, "accounts", acl);
  2079. res = xs_dict_append(res, "statuses", stl);
  2080. res = xs_dict_append(res, "hashtags", htl);
  2081. *body = xs_json_dumps(res, 4);
  2082. *ctype = "application/json";
  2083. status = HTTP_STATUS_OK;
  2084. }
  2085. else
  2086. status = HTTP_STATUS_UNAUTHORIZED;
  2087. }
  2088. /* user cleanup */
  2089. if (logged_in)
  2090. user_free(&snac1);
  2091. srv_debug(1, xs_fmt("mastoapi_get_handler %s %d", q_path, status));
  2092. return status;
  2093. }
  2094. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  2095. const char *payload, int p_size,
  2096. char **body, int *b_size, char **ctype)
  2097. {
  2098. (void)p_size;
  2099. (void)b_size;
  2100. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  2101. return 0;
  2102. int status = HTTP_STATUS_NOT_FOUND;
  2103. xs *args = NULL;
  2104. const char *i_ctype = xs_dict_get(req, "content-type");
  2105. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  2106. if (!xs_is_null(payload))
  2107. args = xs_json_loads(payload);
  2108. }
  2109. else if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded"))
  2110. {
  2111. // Some apps send form data instead of json so we should cater for those
  2112. if (!xs_is_null(payload)) {
  2113. args = xs_url_vars(payload);
  2114. }
  2115. }
  2116. else
  2117. args = xs_dup(xs_dict_get(req, "p_vars"));
  2118. if (args == NULL)
  2119. return HTTP_STATUS_BAD_REQUEST;
  2120. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2121. snac snac = {0};
  2122. int logged_in = process_auth_token(&snac, req);
  2123. if (strcmp(cmd, "/v1/apps") == 0) { /** **/
  2124. const char *name = xs_dict_get(args, "client_name");
  2125. const char *ruri = xs_dict_get(args, "redirect_uris");
  2126. const char *scope = xs_dict_get(args, "scope");
  2127. /* Ice Cubes sends these values as query parameters, so try these */
  2128. if (name == NULL && ruri == NULL && scope == NULL) {
  2129. args = xs_dup(xs_dict_get(req, "q_vars"));
  2130. name = xs_dict_get(args, "client_name");
  2131. ruri = xs_dict_get(args, "redirect_uris");
  2132. scope = xs_dict_get(args, "scope");
  2133. }
  2134. if (xs_type(ruri) == XSTYPE_LIST)
  2135. ruri = xs_dict_get(ruri, 0);
  2136. if (name && ruri) {
  2137. xs *app = xs_dict_new();
  2138. xs *id = xs_replace_i(tid(0), ".", "");
  2139. xs *csec = random_str();
  2140. xs *vkey = random_str();
  2141. xs *cid = NULL;
  2142. /* pick a non-existent random cid */
  2143. for (;;) {
  2144. cid = random_str();
  2145. xs *p_app = app_get(cid);
  2146. if (p_app == NULL)
  2147. break;
  2148. xs_free(cid);
  2149. }
  2150. app = xs_dict_append(app, "name", name);
  2151. app = xs_dict_append(app, "redirect_uri", ruri);
  2152. app = xs_dict_append(app, "client_id", cid);
  2153. app = xs_dict_append(app, "client_secret", csec);
  2154. app = xs_dict_append(app, "vapid_key", vkey);
  2155. app = xs_dict_append(app, "id", id);
  2156. *body = xs_json_dumps(app, 4);
  2157. *ctype = "application/json";
  2158. status = HTTP_STATUS_OK;
  2159. app = xs_dict_append(app, "code", "");
  2160. if (scope)
  2161. app = xs_dict_append(app, "scope", scope);
  2162. app_add(cid, app);
  2163. srv_debug(1, xs_fmt("mastoapi apps: new app %s", cid));
  2164. }
  2165. }
  2166. else
  2167. if (strcmp(cmd, "/v1/statuses") == 0) { /** **/
  2168. if (logged_in) {
  2169. /* post a new Note */
  2170. const char *content = xs_dict_get(args, "status");
  2171. const char *mid = xs_dict_get(args, "in_reply_to_id");
  2172. const char *visibility = xs_dict_get(args, "visibility");
  2173. const char *summary = xs_dict_get(args, "spoiler_text");
  2174. const char *media_ids = xs_dict_get(args, "media_ids");
  2175. const char *language = xs_dict_get(args, "language");
  2176. if (xs_is_null(media_ids))
  2177. media_ids = xs_dict_get(args, "media_ids[]");
  2178. if (xs_is_null(media_ids))
  2179. media_ids = xs_dict_get(args, "media_ids");
  2180. if (xs_is_null(visibility))
  2181. visibility = "public";
  2182. xs *attach_list = xs_list_new();
  2183. xs *irt = NULL;
  2184. /* is it a reply? */
  2185. if (mid != NULL) {
  2186. xs *r_msg = NULL;
  2187. const char *md5 = MID_TO_MD5(mid);
  2188. if (valid_status(object_get_by_md5(md5, &r_msg)))
  2189. irt = xs_dup(xs_dict_get(r_msg, "id"));
  2190. }
  2191. /* does it have attachments? */
  2192. if (!xs_is_null(media_ids)) {
  2193. xs *mi = NULL;
  2194. if (xs_type(media_ids) == XSTYPE_LIST)
  2195. mi = xs_dup(media_ids);
  2196. else {
  2197. mi = xs_list_new();
  2198. mi = xs_list_append(mi, media_ids);
  2199. }
  2200. xs_list *p = mi;
  2201. const xs_str *v;
  2202. while (xs_list_iter(&p, &v)) {
  2203. xs *l = xs_list_new();
  2204. xs *url = xs_fmt("%s/s/%s", snac.actor, v);
  2205. xs *desc = static_get_meta(&snac, v);
  2206. l = xs_list_append(l, url);
  2207. l = xs_list_append(l, desc);
  2208. attach_list = xs_list_append(attach_list, l);
  2209. }
  2210. }
  2211. /* prepare the message */
  2212. int scope = 1;
  2213. if (strcmp(visibility, "unlisted") == 0)
  2214. scope = 2;
  2215. else
  2216. if (strcmp(visibility, "public") == 0)
  2217. scope = 0;
  2218. xs *msg = msg_note(&snac, content, NULL, irt, attach_list, scope, language, NULL);
  2219. if (!xs_is_null(summary) && *summary) {
  2220. msg = xs_dict_set(msg, "sensitive", xs_stock(XSTYPE_TRUE));
  2221. msg = xs_dict_set(msg, "summary", summary);
  2222. }
  2223. /* scheduled? */
  2224. const char *scheduled_at = xs_dict_get(args, "scheduled_at");
  2225. if (xs_is_string(scheduled_at) && *scheduled_at) {
  2226. msg = xs_dict_set(msg, "published", scheduled_at);
  2227. schedule_add(&snac, xs_dict_get(msg, "id"), msg);
  2228. }
  2229. else {
  2230. /* store */
  2231. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  2232. /* 'Create' message */
  2233. xs *c_msg = msg_create(&snac, msg);
  2234. enqueue_message(&snac, c_msg);
  2235. timeline_touch(&snac);
  2236. }
  2237. /* convert to a mastodon status as a response code */
  2238. xs *st = mastoapi_status(&snac, msg);
  2239. *body = xs_json_dumps(st, 4);
  2240. *ctype = "application/json";
  2241. status = HTTP_STATUS_OK;
  2242. }
  2243. else
  2244. status = HTTP_STATUS_UNAUTHORIZED;
  2245. }
  2246. else
  2247. if (xs_startswith(cmd, "/v1/statuses")) { /** **/
  2248. if (logged_in) {
  2249. /* operations on a status */
  2250. xs *l = xs_split(cmd, "/");
  2251. const char *mid = xs_list_get(l, 3);
  2252. const char *op = xs_list_get(l, 4);
  2253. if (!xs_is_null(mid)) {
  2254. xs *msg = NULL;
  2255. xs *out = NULL;
  2256. /* skip the 'fake' part of the id */
  2257. mid = MID_TO_MD5(mid);
  2258. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  2259. const char *id = xs_dict_get(msg, "id");
  2260. if (op == NULL) {
  2261. /* no operation (?) */
  2262. }
  2263. else
  2264. if (strcmp(op, "favourite") == 0) { /** **/
  2265. xs *n_msg = msg_admiration(&snac, id, "Like");
  2266. if (n_msg != NULL) {
  2267. enqueue_message(&snac, n_msg);
  2268. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  2269. out = mastoapi_status(&snac, msg);
  2270. }
  2271. }
  2272. else
  2273. if (strcmp(op, "unfavourite") == 0) { /** **/
  2274. xs *n_msg = msg_repulsion(&snac, id, "Like");
  2275. if (n_msg != NULL) {
  2276. enqueue_message(&snac, n_msg);
  2277. out = mastoapi_status(&snac, msg);
  2278. }
  2279. }
  2280. else
  2281. if (strcmp(op, "reblog") == 0) { /** **/
  2282. xs *n_msg = msg_admiration(&snac, id, "Announce");
  2283. if (n_msg != NULL) {
  2284. enqueue_message(&snac, n_msg);
  2285. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  2286. out = mastoapi_status(&snac, msg);
  2287. }
  2288. }
  2289. else
  2290. if (strcmp(op, "unreblog") == 0) { /** **/
  2291. xs *n_msg = msg_repulsion(&snac, id, "Announce");
  2292. if (n_msg != NULL) {
  2293. enqueue_message(&snac, n_msg);
  2294. out = mastoapi_status(&snac, msg);
  2295. }
  2296. }
  2297. else
  2298. if (strcmp(op, "bookmark") == 0) { /** **/
  2299. /* bookmark this message */
  2300. if (bookmark(&snac, id) == 0)
  2301. out = mastoapi_status(&snac, msg);
  2302. else
  2303. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2304. }
  2305. else
  2306. if (strcmp(op, "unbookmark") == 0) { /** **/
  2307. /* unbookmark this message */
  2308. unbookmark(&snac, id);
  2309. out = mastoapi_status(&snac, msg);
  2310. }
  2311. else
  2312. if (strcmp(op, "pin") == 0) { /** **/
  2313. /* pin this message */
  2314. if (pin(&snac, id) == 0)
  2315. out = mastoapi_status(&snac, msg);
  2316. else
  2317. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2318. }
  2319. else
  2320. if (strcmp(op, "unpin") == 0) { /** **/
  2321. /* unpin this message */
  2322. unpin(&snac, id);
  2323. out = mastoapi_status(&snac, msg);
  2324. }
  2325. else
  2326. if (strcmp(op, "mute") == 0) { /** **/
  2327. /* Mastodon's mute is snac's hide */
  2328. }
  2329. else
  2330. if (strcmp(op, "unmute") == 0) { /** **/
  2331. /* Mastodon's unmute is snac's unhide */
  2332. }
  2333. }
  2334. if (out != NULL) {
  2335. *body = xs_json_dumps(out, 4);
  2336. *ctype = "application/json";
  2337. status = HTTP_STATUS_OK;
  2338. }
  2339. }
  2340. }
  2341. else
  2342. status = HTTP_STATUS_UNAUTHORIZED;
  2343. }
  2344. else
  2345. if (strcmp(cmd, "/v1/notifications/clear") == 0) { /** **/
  2346. if (logged_in) {
  2347. notify_clear(&snac);
  2348. timeline_touch(&snac);
  2349. *body = xs_dup("{}");
  2350. *ctype = "application/json";
  2351. status = HTTP_STATUS_OK;
  2352. }
  2353. else
  2354. status = HTTP_STATUS_UNAUTHORIZED;
  2355. }
  2356. else
  2357. if (strcmp(cmd, "/v1/push/subscription") == 0) { /** **/
  2358. /* I don't know what I'm doing */
  2359. if (logged_in) {
  2360. const char *v;
  2361. xs *wpush = xs_dict_new();
  2362. wpush = xs_dict_append(wpush, "id", "1");
  2363. v = xs_dict_get(args, "data");
  2364. v = xs_dict_get(v, "alerts");
  2365. wpush = xs_dict_append(wpush, "alerts", v);
  2366. v = xs_dict_get(args, "subscription");
  2367. v = xs_dict_get(v, "endpoint");
  2368. wpush = xs_dict_append(wpush, "endpoint", v);
  2369. xs *server_key = random_str();
  2370. wpush = xs_dict_append(wpush, "server_key", server_key);
  2371. *body = xs_json_dumps(wpush, 4);
  2372. *ctype = "application/json";
  2373. status = HTTP_STATUS_OK;
  2374. }
  2375. else
  2376. status = HTTP_STATUS_UNAUTHORIZED;
  2377. }
  2378. else
  2379. if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { /** **/
  2380. if (logged_in) {
  2381. const xs_list *file = xs_dict_get(args, "file");
  2382. const char *desc = xs_dict_get(args, "description");
  2383. if (xs_is_null(desc))
  2384. desc = "";
  2385. status = HTTP_STATUS_BAD_REQUEST;
  2386. if (xs_type(file) == XSTYPE_LIST) {
  2387. const char *fn = xs_list_get(file, 0);
  2388. if (*fn != '\0') {
  2389. char *ext = strrchr(fn, '.');
  2390. char rnd[32];
  2391. xs_rnd_buf(rnd, sizeof(rnd));
  2392. xs *hash = xs_md5_hex(rnd, sizeof(rnd));
  2393. xs *id = xs_fmt("post-%s%s", hash, ext ? ext : "");
  2394. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  2395. int fo = xs_number_get(xs_list_get(file, 1));
  2396. int fs = xs_number_get(xs_list_get(file, 2));
  2397. /* store */
  2398. static_put(&snac, id, payload + fo, fs);
  2399. static_put_meta(&snac, id, desc);
  2400. /* prepare a response */
  2401. xs *rsp = xs_dict_new();
  2402. rsp = xs_dict_append(rsp, "id", id);
  2403. rsp = xs_dict_append(rsp, "type", "image");
  2404. rsp = xs_dict_append(rsp, "url", url);
  2405. rsp = xs_dict_append(rsp, "preview_url", url);
  2406. rsp = xs_dict_append(rsp, "remote_url", url);
  2407. rsp = xs_dict_append(rsp, "description", desc);
  2408. *body = xs_json_dumps(rsp, 4);
  2409. *ctype = "application/json";
  2410. status = HTTP_STATUS_OK;
  2411. }
  2412. }
  2413. }
  2414. else
  2415. status = HTTP_STATUS_UNAUTHORIZED;
  2416. }
  2417. else
  2418. if (xs_startswith(cmd, "/v1/accounts")) { /** **/
  2419. if (logged_in) {
  2420. /* account-related information */
  2421. xs *l = xs_split(cmd, "/");
  2422. const char *md5 = xs_list_get(l, 3);
  2423. const char *opt = xs_list_get(l, 4);
  2424. xs *rsp = NULL;
  2425. if (!xs_is_null(md5) && *md5) {
  2426. xs *actor_o = NULL;
  2427. if (xs_is_null(opt)) {
  2428. /* ? */
  2429. }
  2430. else
  2431. if (strcmp(opt, "follow") == 0) { /** **/
  2432. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2433. const char *actor = xs_dict_get(actor_o, "id");
  2434. xs *msg = msg_follow(&snac, actor);
  2435. if (msg != NULL) {
  2436. /* reload the actor from the message, in may be different */
  2437. actor = xs_dict_get(msg, "object");
  2438. following_add(&snac, actor, msg);
  2439. enqueue_output_by_actor(&snac, msg, actor, 0);
  2440. rsp = mastoapi_relationship(&snac, md5);
  2441. }
  2442. }
  2443. }
  2444. else
  2445. if (strcmp(opt, "unfollow") == 0) { /** **/
  2446. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2447. const char *actor = xs_dict_get(actor_o, "id");
  2448. /* get the following object */
  2449. xs *object = NULL;
  2450. if (valid_status(following_get(&snac, actor, &object))) {
  2451. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  2452. following_del(&snac, actor);
  2453. enqueue_output_by_actor(&snac, msg, actor, 0);
  2454. rsp = mastoapi_relationship(&snac, md5);
  2455. }
  2456. }
  2457. }
  2458. else
  2459. if (strcmp(opt, "block") == 0) { /** **/
  2460. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2461. const char *actor = xs_dict_get(actor_o, "id");
  2462. mute(&snac, actor);
  2463. rsp = mastoapi_relationship(&snac, md5);
  2464. }
  2465. }
  2466. else
  2467. if (strcmp(opt, "unblock") == 0) { /** **/
  2468. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2469. const char *actor = xs_dict_get(actor_o, "id");
  2470. unmute(&snac, actor);
  2471. rsp = mastoapi_relationship(&snac, md5);
  2472. }
  2473. }
  2474. }
  2475. if (rsp != NULL) {
  2476. *body = xs_json_dumps(rsp, 4);
  2477. *ctype = "application/json";
  2478. status = HTTP_STATUS_OK;
  2479. }
  2480. }
  2481. else
  2482. status = HTTP_STATUS_UNAUTHORIZED;
  2483. }
  2484. else
  2485. if (xs_startswith(cmd, "/v1/polls")) { /** **/
  2486. if (logged_in) {
  2487. /* operations on a status */
  2488. xs *l = xs_split(cmd, "/");
  2489. const char *mid = xs_list_get(l, 3);
  2490. const char *op = xs_list_get(l, 4);
  2491. if (!xs_is_null(mid)) {
  2492. xs *msg = NULL;
  2493. xs *out = NULL;
  2494. /* skip the 'fake' part of the id */
  2495. mid = MID_TO_MD5(mid);
  2496. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  2497. const char *id = xs_dict_get(msg, "id");
  2498. const char *atto = get_atto(msg);
  2499. const xs_list *opts = xs_dict_get(msg, "oneOf");
  2500. if (opts == NULL)
  2501. opts = xs_dict_get(msg, "anyOf");
  2502. if (op == NULL) {
  2503. }
  2504. else
  2505. if (strcmp(op, "votes") == 0) {
  2506. const xs_list *choices = xs_dict_get(args, "choices[]");
  2507. if (xs_is_null(choices))
  2508. choices = xs_dict_get(args, "choices");
  2509. if (xs_type(choices) == XSTYPE_LIST) {
  2510. const xs_str *v;
  2511. int c = 0;
  2512. while (xs_list_next(choices, &v, &c)) {
  2513. int io = atoi(v);
  2514. const xs_dict *o = xs_list_get(opts, io);
  2515. if (o) {
  2516. const char *name = xs_dict_get(o, "name");
  2517. xs *msg = msg_note(&snac, "", atto, (char *)id, NULL, 1, NULL, NULL);
  2518. msg = xs_dict_append(msg, "name", name);
  2519. xs *c_msg = msg_create(&snac, msg);
  2520. enqueue_message(&snac, c_msg);
  2521. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  2522. }
  2523. }
  2524. out = mastoapi_poll(&snac, msg);
  2525. }
  2526. }
  2527. }
  2528. if (out != NULL) {
  2529. *body = xs_json_dumps(out, 4);
  2530. *ctype = "application/json";
  2531. status = HTTP_STATUS_OK;
  2532. }
  2533. }
  2534. }
  2535. else
  2536. status = HTTP_STATUS_UNAUTHORIZED;
  2537. }
  2538. else
  2539. if (strcmp(cmd, "/v1/lists") == 0) {
  2540. if (logged_in) {
  2541. const char *title = xs_dict_get(args, "title");
  2542. if (xs_type(title) == XSTYPE_STRING) {
  2543. /* add the list */
  2544. xs *out = xs_dict_new();
  2545. xs *lid = list_maint(&snac, title, 1);
  2546. if (!xs_is_null(lid)) {
  2547. out = xs_dict_append(out, "id", lid);
  2548. out = xs_dict_append(out, "title", title);
  2549. out = xs_dict_append(out, "replies_policy", xs_dict_get_def(args, "replies_policy", "list"));
  2550. out = xs_dict_append(out, "exclusive", xs_stock(XSTYPE_FALSE));
  2551. status = HTTP_STATUS_OK;
  2552. }
  2553. else {
  2554. out = xs_dict_append(out, "error", "cannot create list");
  2555. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2556. }
  2557. *body = xs_json_dumps(out, 4);
  2558. *ctype = "application/json";
  2559. }
  2560. else
  2561. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2562. }
  2563. }
  2564. else
  2565. if (xs_startswith(cmd, "/v1/lists/")) { /** list maintenance **/
  2566. if (logged_in) {
  2567. xs *l = xs_split(cmd, "/");
  2568. const char *op = xs_list_get(l, -1);
  2569. const char *id = xs_list_get(l, -2);
  2570. if (op && id && xs_is_hex(id)) {
  2571. if (strcmp(op, "accounts") == 0) {
  2572. const xs_list *accts = xs_dict_get(args, "account_ids[]");
  2573. if (xs_is_null(accts))
  2574. accts = xs_dict_get(args, "account_ids");
  2575. int c = 0;
  2576. const char *v;
  2577. while (xs_list_next(accts, &v, &c)) {
  2578. list_content(&snac, id, v, 1);
  2579. }
  2580. xs *out = xs_dict_new();
  2581. *body = xs_json_dumps(out, 4);
  2582. *ctype = "application/json";
  2583. status = HTTP_STATUS_OK;
  2584. }
  2585. }
  2586. }
  2587. }
  2588. else if (strcmp(cmd, "/v1/markers") == 0) { /** **/
  2589. xs_str *json = NULL;
  2590. if (logged_in) {
  2591. const xs_str *home_marker = xs_dict_get(args, "home[last_read_id]");
  2592. if (xs_is_null(home_marker)) {
  2593. const xs_dict *home = xs_dict_get(args, "home");
  2594. if (!xs_is_null(home))
  2595. home_marker = xs_dict_get(home, "last_read_id");
  2596. }
  2597. const xs_str *notify_marker = xs_dict_get(args, "notifications[last_read_id]");
  2598. if (xs_is_null(notify_marker)) {
  2599. const xs_dict *notify = xs_dict_get(args, "notifications");
  2600. if (!xs_is_null(notify))
  2601. notify_marker = xs_dict_get(notify, "last_read_id");
  2602. }
  2603. json = xs_json_dumps(markers_set(&snac, home_marker, notify_marker), 4);
  2604. }
  2605. if (!xs_is_null(json))
  2606. *body = json;
  2607. else
  2608. *body = xs_dup("{}");
  2609. *ctype = "application/json";
  2610. status = HTTP_STATUS_OK;
  2611. }
  2612. else
  2613. if (xs_startswith(cmd, "/v1/follow_requests")) { /** **/
  2614. if (logged_in) {
  2615. /* "authorize" or "reject" */
  2616. xs *rel = NULL;
  2617. xs *l = xs_split(cmd, "/");
  2618. const char *md5 = xs_list_get(l, -2);
  2619. const char *s_cmd = xs_list_get(l, -1);
  2620. if (xs_is_string(md5) && xs_is_string(s_cmd)) {
  2621. xs *actor = NULL;
  2622. if (valid_status(object_get_by_md5(md5, &actor))) {
  2623. const char *actor_id = xs_dict_get(actor, "id");
  2624. if (strcmp(s_cmd, "authorize") == 0) {
  2625. xs *fwreq = pending_get(&snac, actor_id);
  2626. if (fwreq != NULL) {
  2627. xs *reply = msg_accept(&snac, fwreq, actor_id);
  2628. enqueue_message(&snac, reply);
  2629. if (xs_is_null(xs_dict_get(fwreq, "published"))) {
  2630. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  2631. fwreq = xs_dict_set(fwreq, "published", date);
  2632. }
  2633. timeline_add(&snac, xs_dict_get(fwreq, "id"), fwreq);
  2634. follower_add(&snac, actor_id);
  2635. pending_del(&snac, actor_id);
  2636. rel = mastoapi_relationship(&snac, md5);
  2637. }
  2638. }
  2639. else
  2640. if (strcmp(s_cmd, "reject") == 0) {
  2641. pending_del(&snac, actor_id);
  2642. rel = mastoapi_relationship(&snac, md5);
  2643. }
  2644. }
  2645. }
  2646. if (rel != NULL) {
  2647. *body = xs_json_dumps(rel, 4);
  2648. *ctype = "application/json";
  2649. status = HTTP_STATUS_OK;
  2650. }
  2651. }
  2652. }
  2653. else
  2654. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2655. /* user cleanup */
  2656. if (logged_in)
  2657. user_free(&snac);
  2658. srv_debug(1, xs_fmt("mastoapi_post_handler %s %d", q_path, status));
  2659. return status;
  2660. }
  2661. int mastoapi_delete_handler(const xs_dict *req, const char *q_path,
  2662. const char *payload, int p_size,
  2663. char **body, int *b_size, char **ctype)
  2664. {
  2665. (void)p_size;
  2666. (void)body;
  2667. (void)b_size;
  2668. (void)ctype;
  2669. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  2670. return 0;
  2671. int status = HTTP_STATUS_NOT_FOUND;
  2672. xs *args = NULL;
  2673. const char *i_ctype = xs_dict_get(req, "content-type");
  2674. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  2675. if (!xs_is_null(payload))
  2676. args = xs_json_loads(payload);
  2677. }
  2678. else if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded"))
  2679. {
  2680. // Some apps send form data instead of json so we should cater for those
  2681. if (!xs_is_null(payload)) {
  2682. args = xs_url_vars(payload);
  2683. }
  2684. }
  2685. else
  2686. args = xs_dup(xs_dict_get(req, "p_vars"));
  2687. if (args == NULL)
  2688. return HTTP_STATUS_BAD_REQUEST;
  2689. snac snac = {0};
  2690. int logged_in = process_auth_token(&snac, req);
  2691. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2692. if (xs_startswith(cmd, "/v1/push/subscription") || xs_startswith(cmd, "/v2/push/subscription")) { /** **/
  2693. // pretend we deleted it, since it doesn't exist anyway
  2694. status = HTTP_STATUS_OK;
  2695. }
  2696. else
  2697. if (xs_startswith(cmd, "/v1/lists/")) {
  2698. if (logged_in) {
  2699. xs *l = xs_split(cmd, "/");
  2700. const char *p = xs_list_get(l, -1);
  2701. if (p) {
  2702. if (strcmp(p, "accounts") == 0) {
  2703. /* delete account from list */
  2704. p = xs_list_get(l, -2);
  2705. const xs_list *accts = xs_dict_get(args, "account_ids[]");
  2706. if (xs_is_null(accts))
  2707. accts = xs_dict_get(args, "account_ids");
  2708. int c = 0;
  2709. const char *v;
  2710. while (xs_list_next(accts, &v, &c)) {
  2711. list_content(&snac, p, v, 2);
  2712. }
  2713. }
  2714. else {
  2715. /* delete list */
  2716. if (xs_is_hex(p)) {
  2717. list_maint(&snac, p, 2);
  2718. }
  2719. }
  2720. }
  2721. *ctype = "application/json";
  2722. status = HTTP_STATUS_OK;
  2723. }
  2724. else
  2725. status = HTTP_STATUS_UNAUTHORIZED;
  2726. }
  2727. /* user cleanup */
  2728. if (logged_in)
  2729. user_free(&snac);
  2730. srv_debug(1, xs_fmt("mastoapi_delete_handler %s %d", q_path, status));
  2731. return status;
  2732. }
  2733. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  2734. const char *payload, int p_size,
  2735. char **body, int *b_size, char **ctype)
  2736. {
  2737. (void)p_size;
  2738. (void)b_size;
  2739. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  2740. return 0;
  2741. int status = HTTP_STATUS_NOT_FOUND;
  2742. xs *args = NULL;
  2743. const char *i_ctype = xs_dict_get(req, "content-type");
  2744. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  2745. if (!xs_is_null(payload))
  2746. args = xs_json_loads(payload);
  2747. }
  2748. else
  2749. args = xs_dup(xs_dict_get(req, "p_vars"));
  2750. if (args == NULL)
  2751. return HTTP_STATUS_BAD_REQUEST;
  2752. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2753. snac snac = {0};
  2754. int logged_in = process_auth_token(&snac, req);
  2755. if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) { /** **/
  2756. if (logged_in) {
  2757. xs *l = xs_split(cmd, "/");
  2758. const char *stid = xs_list_get(l, 3);
  2759. if (!xs_is_null(stid)) {
  2760. const char *desc = xs_dict_get(args, "description");
  2761. /* set the image metadata */
  2762. static_put_meta(&snac, stid, desc);
  2763. /* prepare a response */
  2764. xs *rsp = xs_dict_new();
  2765. xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
  2766. rsp = xs_dict_append(rsp, "id", stid);
  2767. rsp = xs_dict_append(rsp, "type", "image");
  2768. rsp = xs_dict_append(rsp, "url", url);
  2769. rsp = xs_dict_append(rsp, "preview_url", url);
  2770. rsp = xs_dict_append(rsp, "remote_url", url);
  2771. rsp = xs_dict_append(rsp, "description", desc);
  2772. *body = xs_json_dumps(rsp, 4);
  2773. *ctype = "application/json";
  2774. status = HTTP_STATUS_OK;
  2775. }
  2776. }
  2777. else
  2778. status = HTTP_STATUS_UNAUTHORIZED;
  2779. }
  2780. else
  2781. if (xs_startswith(cmd, "/v1/statuses")) {
  2782. if (logged_in) {
  2783. xs *l = xs_split(cmd, "/");
  2784. const char *mid = xs_list_get(l, 3);
  2785. if (!xs_is_null(mid)) {
  2786. const char *md5 = MID_TO_MD5(mid);
  2787. xs *rsp = NULL;
  2788. xs *msg = NULL;
  2789. if (valid_status(timeline_get_by_md5(&snac, md5, &msg))) {
  2790. const char *content = xs_dict_get(args, "status");
  2791. xs *atls = xs_list_new();
  2792. xs *f_content = not_really_markdown(content, &atls, NULL);
  2793. /* replace fields with new content */
  2794. msg = xs_dict_set(msg, "sourceContent", content);
  2795. msg = xs_dict_set(msg, "content", f_content);
  2796. xs *updated = xs_str_utctime(0, ISO_DATE_SPEC);
  2797. msg = xs_dict_set(msg, "updated", updated);
  2798. /* overwrite object, not updating the indexes */
  2799. object_add_ow(xs_dict_get(msg, "id"), msg);
  2800. /* update message */
  2801. xs *c_msg = msg_update(&snac, msg);
  2802. enqueue_message(&snac, c_msg);
  2803. rsp = mastoapi_status(&snac, msg);
  2804. }
  2805. if (rsp != NULL) {
  2806. *body = xs_json_dumps(rsp, 4);
  2807. *ctype = "application/json";
  2808. status = HTTP_STATUS_OK;
  2809. }
  2810. }
  2811. }
  2812. else
  2813. status = HTTP_STATUS_UNAUTHORIZED;
  2814. }
  2815. /* user cleanup */
  2816. if (logged_in)
  2817. user_free(&snac);
  2818. srv_debug(1, xs_fmt("mastoapi_put_handler %s %d", q_path, status));
  2819. return status;
  2820. }
  2821. void persist_image(const char *key, const xs_val *data, const char *payload, snac *snac)
  2822. /* Store header or avatar */
  2823. {
  2824. if (data != NULL) {
  2825. if (xs_type(data) == XSTYPE_LIST) {
  2826. const char *fn = xs_list_get(data, 0);
  2827. if (fn && *fn) {
  2828. const char *ext = strrchr(fn, '.');
  2829. /* Mona iOS sends always jpg as application/octet-stream with no filename */
  2830. if (ext == NULL || strcmp(fn, key) == 0) {
  2831. ext = ".jpg";
  2832. }
  2833. /* Make sure we have a unique file name, otherwise updated images will not be
  2834. * re-loaded by clients. */
  2835. xs *rnd = random_str();
  2836. xs *hash = xs_md5_hex(rnd, strlen(rnd));
  2837. xs *id = xs_fmt("%s%s", hash, ext);
  2838. xs *url = xs_fmt("%s/s/%s", snac->actor, id);
  2839. int fo = xs_number_get(xs_list_get(data, 1));
  2840. int fs = xs_number_get(xs_list_get(data, 2));
  2841. /* store */
  2842. static_put(snac, id, payload + fo, fs);
  2843. snac->config = xs_dict_set(snac->config, key, url);
  2844. }
  2845. }
  2846. }
  2847. }
  2848. int mastoapi_patch_handler(const xs_dict *req, const char *q_path,
  2849. const char *payload, int p_size,
  2850. char **body, int *b_size, char **ctype)
  2851. /* Handle profile updates */
  2852. {
  2853. (void)p_size;
  2854. (void)b_size;
  2855. if (!xs_startswith(q_path, "/api/v1/"))
  2856. return 0;
  2857. int status = HTTP_STATUS_NOT_FOUND;
  2858. xs *args = NULL;
  2859. const char *i_ctype = xs_dict_get(req, "content-type");
  2860. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  2861. if (!xs_is_null(payload))
  2862. args = xs_json_loads(payload);
  2863. }
  2864. else if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded"))
  2865. {
  2866. // Some apps send form data instead of json so we should cater for those
  2867. if (!xs_is_null(payload)) {
  2868. args = xs_url_vars(payload);
  2869. }
  2870. }
  2871. else
  2872. args = xs_dup(xs_dict_get(req, "p_vars"));
  2873. if (args == NULL)
  2874. return HTTP_STATUS_BAD_REQUEST;
  2875. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2876. snac snac = {0};
  2877. int logged_in = process_auth_token(&snac, req);
  2878. if (xs_startswith(cmd, "/v1/accounts/update_credentials")) {
  2879. /* Update user profile fields */
  2880. if (logged_in) {
  2881. int c = 0;
  2882. const xs_str *k;
  2883. const xs_val *v;
  2884. const xs_str *field_name = NULL;
  2885. xs *new_fields = xs_dict_new();
  2886. while (xs_dict_next(args, &k, &v, &c)) {
  2887. if (strcmp(k, "display_name") == 0) {
  2888. if (v != NULL)
  2889. snac.config = xs_dict_set(snac.config, "name", v);
  2890. }
  2891. else
  2892. if (strcmp(k, "note") == 0) {
  2893. if (v != NULL)
  2894. snac.config = xs_dict_set(snac.config, "bio", v);
  2895. }
  2896. else
  2897. if (strcmp(k, "bot") == 0) {
  2898. if (v != NULL)
  2899. snac.config = xs_dict_set(snac.config, "bot",
  2900. (strcmp(v, "true") == 0 ||
  2901. strcmp(v, "1") == 0) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  2902. }
  2903. else
  2904. if (strcmp(k, "source[sensitive]") == 0) {
  2905. if (v != NULL)
  2906. snac.config = xs_dict_set(snac.config, "cw",
  2907. strcmp(v, "true") == 0 ? "open" : "");
  2908. }
  2909. else
  2910. if (strcmp(k, "source[privacy]") == 0) {
  2911. if (v != NULL)
  2912. snac.config = xs_dict_set(snac.config, "private",
  2913. strcmp(v, "private") == 0 ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  2914. }
  2915. else
  2916. if (strcmp(k, "header") == 0) {
  2917. persist_image("header", v, payload, &snac);
  2918. }
  2919. else
  2920. if (strcmp(k, "avatar") == 0) {
  2921. persist_image("avatar", v, payload, &snac);
  2922. }
  2923. else
  2924. if (xs_between("fields_attributes", k, "[name]")) {
  2925. field_name = strcmp(v, "") != 0 ? v : NULL;
  2926. }
  2927. else
  2928. if (xs_between("fields_attributes", k, "[value]")) {
  2929. if (field_name != NULL) {
  2930. new_fields = xs_dict_set(new_fields, field_name, v);
  2931. snac.config = xs_dict_set(snac.config, "metadata", new_fields);
  2932. }
  2933. }
  2934. /* we don't have support for the following options, yet
  2935. - discoverable (0/1)
  2936. - locked (0/1)
  2937. */
  2938. }
  2939. /* Persist profile */
  2940. if (user_persist(&snac, 1) == 0)
  2941. credentials_get(body, ctype, &status, snac);
  2942. else
  2943. status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
  2944. }
  2945. else
  2946. status = HTTP_STATUS_UNAUTHORIZED;
  2947. }
  2948. /* user cleanup */
  2949. if (logged_in)
  2950. user_free(&snac);
  2951. srv_debug(1, xs_fmt("mastoapi_patch_handler %s %d", q_path, status));
  2952. return status;
  2953. }
  2954. void mastoapi_purge(void)
  2955. {
  2956. xs *spec = xs_fmt("%s/app/" "*.json", srv_basedir);
  2957. xs *files = xs_glob(spec, 1, 0);
  2958. xs_list *p = files;
  2959. const xs_str *v;
  2960. time_t mt = time(NULL) - 3600;
  2961. while (xs_list_iter(&p, &v)) {
  2962. xs *cid = xs_replace(v, ".json", "");
  2963. xs *fn = _app_fn(cid);
  2964. if (mtime(fn) < mt) {
  2965. /* get the app */
  2966. xs *app = app_get(cid);
  2967. if (app) {
  2968. /* old apps with no uid are incomplete cruft */
  2969. const char *uid = xs_dict_get(app, "uid");
  2970. if (xs_is_null(uid) || *uid == '\0') {
  2971. unlink(fn);
  2972. srv_debug(2, xs_fmt("purged %s", fn));
  2973. }
  2974. }
  2975. }
  2976. }
  2977. }
  2978. #endif /* #ifndef NO_MASTODON_API */