mastoapi.c 127 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801
  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 *b_id = xs_toupper_i(xs_dup(xs_dict_get(st, "id")));
  884. bst = xs_dict_set(bst, "id", b_id);
  885. xs_free(st);
  886. st = bst;
  887. }
  888. else
  889. xs_free(bst);
  890. }
  891. return st;
  892. }
  893. xs_dict *mastoapi_relationship(snac *snac, const char *md5)
  894. {
  895. xs_dict *rel = NULL;
  896. xs *actor_o = NULL;
  897. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  898. rel = xs_dict_new();
  899. const char *actor = xs_dict_get(actor_o, "id");
  900. rel = xs_dict_append(rel, "id", md5);
  901. rel = xs_dict_append(rel, "following",
  902. following_check(snac, actor) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  903. rel = xs_dict_append(rel, "showing_reblogs", xs_stock(XSTYPE_TRUE));
  904. rel = xs_dict_append(rel, "notifying", xs_stock(XSTYPE_FALSE));
  905. rel = xs_dict_append(rel, "followed_by",
  906. follower_check(snac, actor) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  907. rel = xs_dict_append(rel, "blocking",
  908. is_muted(snac, actor) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  909. rel = xs_dict_append(rel, "muting", xs_stock(XSTYPE_FALSE));
  910. rel = xs_dict_append(rel, "muting_notifications", xs_stock(XSTYPE_FALSE));
  911. rel = xs_dict_append(rel, "requested", xs_stock(XSTYPE_FALSE));
  912. rel = xs_dict_append(rel, "domain_blocking", xs_stock(XSTYPE_FALSE));
  913. rel = xs_dict_append(rel, "endorsed", xs_stock(XSTYPE_FALSE));
  914. rel = xs_dict_append(rel, "note", "");
  915. }
  916. return rel;
  917. }
  918. int process_auth_token(snac *snac, const xs_dict *req)
  919. /* processes an authorization token, if there is one */
  920. {
  921. int logged_in = 0;
  922. const char *v;
  923. /* if there is an authorization field, try to validate it */
  924. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  925. xs *tokid = xs_replace_n(v, "Bearer ", "", 1);
  926. xs *token = token_get(tokid);
  927. if (token != NULL) {
  928. const char *uid = xs_dict_get(token, "uid");
  929. if (!xs_is_null(uid) && user_open(snac, uid)) {
  930. logged_in = 1;
  931. /* this counts as a 'login' */
  932. lastlog_write(snac, "mastoapi");
  933. srv_debug(2, xs_fmt("mastoapi auth: valid token for user '%s'", uid));
  934. }
  935. else
  936. srv_log(xs_fmt("mastoapi auth: corrupted token '%s'", tokid));
  937. }
  938. else
  939. srv_log(xs_fmt("mastoapi auth: invalid token '%s'", tokid));
  940. }
  941. return logged_in;
  942. }
  943. void credentials_get(char **body, char **ctype, int *status, snac snac)
  944. {
  945. xs *acct = xs_dict_new();
  946. const xs_val *bot = xs_dict_get(snac.config, "bot");
  947. acct = xs_dict_append(acct, "id", snac.md5);
  948. acct = xs_dict_append(acct, "username", xs_dict_get(snac.config, "uid"));
  949. acct = xs_dict_append(acct, "acct", xs_dict_get(snac.config, "uid"));
  950. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac.config, "name"));
  951. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac.config, "published"));
  952. acct = xs_dict_append(acct, "last_status_at", xs_dict_get(snac.config, "published"));
  953. acct = xs_dict_append(acct, "note", xs_dict_get(snac.config, "bio"));
  954. acct = xs_dict_append(acct, "url", snac.actor);
  955. acct = xs_dict_append(acct, "locked",
  956. xs_stock(xs_is_true(xs_dict_get(snac.config, "approve_followers")) ? XSTYPE_TRUE : XSTYPE_FALSE));
  957. acct = xs_dict_append(acct, "bot", xs_stock(xs_is_true(bot) ? XSTYPE_TRUE : XSTYPE_FALSE));
  958. acct = xs_dict_append(acct, "emojis", xs_stock(XSTYPE_LIST));
  959. xs *src = xs_json_loads("{\"privacy\":\"public\", \"language\":\"en\","
  960. "\"follow_requests_count\": 0,"
  961. "\"sensitive\":false,\"fields\":[],\"note\":\"\"}");
  962. /* some apps take the note from the source object */
  963. src = xs_dict_set(src, "note", xs_dict_get(snac.config, "bio"));
  964. src = xs_dict_set(src, "privacy", xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE ? "private" : "public");
  965. const xs_str *cw = xs_dict_get(snac.config, "cw");
  966. src = xs_dict_set(src, "sensitive",
  967. strcmp(cw, "open") == 0 ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  968. src = xs_dict_set(src, "bot", xs_stock(xs_is_true(bot) ? XSTYPE_TRUE : XSTYPE_FALSE));
  969. xs *avatar = NULL;
  970. const char *av = xs_dict_get(snac.config, "avatar");
  971. if (xs_is_null(av) || *av == '\0')
  972. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  973. else
  974. avatar = xs_dup(av);
  975. acct = xs_dict_append(acct, "avatar", avatar);
  976. acct = xs_dict_append(acct, "avatar_static", avatar);
  977. xs *header = NULL;
  978. const char *hd = xs_dict_get(snac.config, "header");
  979. if (!xs_is_null(hd))
  980. header = xs_dup(hd);
  981. else
  982. header = xs_str_new(NULL);
  983. acct = xs_dict_append(acct, "header", header);
  984. acct = xs_dict_append(acct, "header_static", header);
  985. const xs_dict *metadata = xs_dict_get(snac.config, "metadata");
  986. if (xs_type(metadata) == XSTYPE_DICT) {
  987. xs *fields = xs_list_new();
  988. const xs_str *k;
  989. const xs_str *v;
  990. xs_dict *val_links = snac.links;
  991. if (xs_is_null(val_links))
  992. val_links = xs_stock(XSTYPE_DICT);
  993. int c = 0;
  994. while (xs_dict_next(metadata, &k, &v, &c)) {
  995. xs *val_date = NULL;
  996. const xs_number *verified_time = xs_dict_get(val_links, v);
  997. if (xs_type(verified_time) == XSTYPE_NUMBER) {
  998. time_t t = xs_number_get(verified_time);
  999. if (t > 0)
  1000. val_date = xs_str_utctime(t, ISO_DATE_SPEC);
  1001. }
  1002. xs *d = xs_dict_new();
  1003. d = xs_dict_append(d, "name", k);
  1004. d = xs_dict_append(d, "value", v);
  1005. d = xs_dict_append(d, "verified_at",
  1006. xs_type(val_date) == XSTYPE_STRING && *val_date ? val_date : xs_stock(XSTYPE_NULL));
  1007. fields = xs_list_append(fields, d);
  1008. }
  1009. acct = xs_dict_set(acct, "fields", fields);
  1010. /* some apps take the fields from the source object */
  1011. src = xs_dict_set(src, "fields", fields);
  1012. }
  1013. acct = xs_dict_append(acct, "source", src);
  1014. acct = xs_dict_append(acct, "followers_count", xs_stock(0));
  1015. acct = xs_dict_append(acct, "following_count", xs_stock(0));
  1016. acct = xs_dict_append(acct, "statuses_count", xs_stock(0));
  1017. /* does this user want to publish their contact metrics? */
  1018. if (xs_is_true(xs_dict_get(snac.config, "show_contact_metrics"))) {
  1019. int fwing = following_list_len(&snac);
  1020. int fwers = follower_list_len(&snac);
  1021. xs *ni = xs_number_new(fwing);
  1022. xs *ne = xs_number_new(fwers);
  1023. acct = xs_dict_append(acct, "followers_count", ne);
  1024. acct = xs_dict_append(acct, "following_count", ni);
  1025. }
  1026. *body = xs_json_dumps(acct, 4);
  1027. *ctype = "application/json";
  1028. *status = HTTP_STATUS_OK;
  1029. }
  1030. xs_list *mastoapi_timeline(snac *user, const xs_dict *args, const char *index_fn)
  1031. {
  1032. xs_list *out = xs_list_new();
  1033. FILE *f;
  1034. char md5[MD5_HEX_SIZE];
  1035. if (dbglevel) {
  1036. xs *js = xs_json_dumps(args, 0);
  1037. srv_debug(1, xs_fmt("mastoapi_timeline args %s", js));
  1038. }
  1039. if ((f = fopen(index_fn, "r")) == NULL)
  1040. return out;
  1041. const char *max_id = xs_dict_get(args, "max_id");
  1042. const char *since_id = xs_dict_get(args, "since_id");
  1043. const char *min_id = xs_dict_get(args, "min_id"); /* unsupported old-to-new navigation */
  1044. const char *limit_s = xs_dict_get(args, "limit");
  1045. int (*iterator)(FILE *, char *);
  1046. int initial_status = 0;
  1047. int ascending = 0;
  1048. int limit = 0;
  1049. int cnt = 0;
  1050. if (!xs_is_null(limit_s))
  1051. limit = atoi(limit_s);
  1052. if (limit == 0)
  1053. limit = 20;
  1054. if (min_id) {
  1055. iterator = &index_asc_next;
  1056. initial_status = index_asc_first(f, md5, MID_TO_MD5(min_id));
  1057. ascending = 1;
  1058. }
  1059. else {
  1060. iterator = &index_desc_next;
  1061. initial_status = index_desc_first(f, md5, 0);
  1062. }
  1063. if (initial_status) {
  1064. do {
  1065. xs *msg = NULL;
  1066. /* only return entries older that max_id */
  1067. if (max_id) {
  1068. if (strcmp(md5, MID_TO_MD5(max_id)) == 0) {
  1069. max_id = NULL;
  1070. if (ascending)
  1071. break;
  1072. }
  1073. if (!ascending)
  1074. continue;
  1075. }
  1076. /* only returns entries newer than since_id */
  1077. if (since_id) {
  1078. if (strcmp(md5, MID_TO_MD5(since_id)) == 0) {
  1079. if (!ascending)
  1080. break;
  1081. since_id = NULL;
  1082. }
  1083. if (ascending)
  1084. continue;
  1085. }
  1086. /* get the entry */
  1087. if (user) {
  1088. if (!valid_status(timeline_get_by_md5(user, md5, &msg)))
  1089. continue;
  1090. }
  1091. else {
  1092. if (!valid_status(object_get_by_md5(md5, &msg)))
  1093. continue;
  1094. }
  1095. /* discard non-Notes */
  1096. const char *id = xs_dict_get(msg, "id");
  1097. const char *type = xs_dict_get(msg, "type");
  1098. if (!xs_match(type, POSTLIKE_OBJECT_TYPE))
  1099. continue;
  1100. if (id && is_instance_blocked(id))
  1101. continue;
  1102. const char *from = NULL;
  1103. if (strcmp(type, "Page") == 0)
  1104. from = xs_dict_get(msg, "audience");
  1105. if (from == NULL)
  1106. from = get_atto(msg);
  1107. if (from == NULL)
  1108. continue;
  1109. if (user) {
  1110. /* is this message from a person we don't follow? */
  1111. if (strcmp(from, user->actor) && !following_check(user, from)) {
  1112. /* discard if it was not boosted */
  1113. xs *idx = object_announces(id);
  1114. if (xs_list_len(idx) == 0)
  1115. continue;
  1116. }
  1117. /* discard notes from muted morons */
  1118. if (is_muted(user, from))
  1119. continue;
  1120. /* discard hidden notes */
  1121. if (is_hidden(user, id))
  1122. continue;
  1123. }
  1124. else {
  1125. /* skip non-public messages */
  1126. if (!is_msg_public(msg))
  1127. continue;
  1128. /* discard messages from private users */
  1129. if (is_msg_from_private_user(msg))
  1130. continue;
  1131. }
  1132. /* if it has a name and it's not an object that may have one,
  1133. it's a poll vote, so discard it */
  1134. if (!xs_is_null(xs_dict_get(msg, "name")) && !xs_match(type, "Page|Video|Audio|Event"))
  1135. continue;
  1136. /* convert the Note into a Mastodon status */
  1137. xs *st = mastoapi_status(user, msg);
  1138. if (st != NULL) {
  1139. if (ascending)
  1140. out = xs_list_insert(out, 0, st);
  1141. else
  1142. out = xs_list_append(out, st);
  1143. cnt++;
  1144. }
  1145. } while ((cnt < limit) && (*iterator)(f, md5));
  1146. }
  1147. int more = index_desc_next(f, md5);
  1148. fclose(f);
  1149. srv_debug(1, xs_fmt("mastoapi_timeline ret %d%s", cnt, more ? " (+)" : ""));
  1150. return out;
  1151. }
  1152. xs_str *timeline_link_header(const char *endpoint, xs_list *timeline)
  1153. /* returns a Link header with paging information */
  1154. {
  1155. xs_str *s = NULL;
  1156. if (xs_list_len(timeline) == 0)
  1157. return NULL;
  1158. const xs_dict *first_st = xs_list_get(timeline, 0);
  1159. const xs_dict *last_st = xs_list_get(timeline, -1);
  1160. const char *first_id = xs_dict_get(first_st, "id");
  1161. const char *last_id = xs_dict_get(last_st, "id");
  1162. const char *host = xs_dict_get(srv_config, "host");
  1163. const char *protocol = xs_dict_get_def(srv_config, "protocol", "https");
  1164. s = xs_fmt(
  1165. "<%s:/" "/%s%s?max_id=%s>; rel=\"next\", "
  1166. "<%s:/" "/%s%s?since_id=%s>; rel=\"prev\"",
  1167. protocol, host, endpoint, last_id,
  1168. protocol, host, endpoint, first_id);
  1169. srv_debug(1, xs_fmt("timeline_link_header %s", s));
  1170. return s;
  1171. }
  1172. xs_list *mastoapi_account_lists(snac *user, const char *uid)
  1173. /* returns the list of list an user is in */
  1174. {
  1175. xs_list *out = xs_list_new();
  1176. xs *actor_md5 = NULL;
  1177. xs *lol = list_maint(user, NULL, 0);
  1178. if (uid) {
  1179. if (!xs_is_hex(uid))
  1180. actor_md5 = xs_md5_hex(uid, strlen(uid));
  1181. else
  1182. actor_md5 = xs_dup(uid);
  1183. }
  1184. const xs_list *li;
  1185. xs_list_foreach(lol, li) {
  1186. const char *list_id = xs_list_get(li, 0);
  1187. const char *list_title = xs_list_get(li, 1);
  1188. if (uid) {
  1189. xs *users = list_content(user, list_id, NULL, 0);
  1190. if (xs_list_in(users, actor_md5) == -1)
  1191. continue;
  1192. }
  1193. xs *d = xs_dict_new();
  1194. d = xs_dict_append(d, "id", list_id);
  1195. d = xs_dict_append(d, "title", list_title);
  1196. d = xs_dict_append(d, "replies_policy", "list");
  1197. d = xs_dict_append(d, "exclusive", xs_stock(XSTYPE_FALSE));
  1198. out = xs_list_append(out, d);
  1199. }
  1200. return out;
  1201. }
  1202. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  1203. char **body, int *b_size, char **ctype, xs_str **link)
  1204. {
  1205. (void)b_size;
  1206. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1207. return 0;
  1208. int status = HTTP_STATUS_NOT_FOUND;
  1209. const xs_dict *args = xs_dict_get(req, "q_vars");
  1210. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1211. snac snac1 = {0};
  1212. int logged_in = process_auth_token(&snac1, req);
  1213. if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) { /** **/
  1214. if (logged_in) {
  1215. credentials_get(body, ctype, &status, snac1);
  1216. }
  1217. else {
  1218. status = HTTP_STATUS_UNPROCESSABLE_CONTENT; // (no login)
  1219. }
  1220. }
  1221. else
  1222. if (strcmp(cmd, "/v1/accounts/relationships") == 0) { /** **/
  1223. /* find if an account is followed, blocked, etc. */
  1224. /* the account to get relationships about is in args "id[]" */
  1225. if (logged_in) {
  1226. xs *res = xs_list_new();
  1227. const char *md5 = xs_dict_get(args, "id[]");
  1228. if (xs_is_null(md5))
  1229. md5 = xs_dict_get(args, "id");
  1230. if (!xs_is_null(md5)) {
  1231. if (xs_type(md5) == XSTYPE_LIST)
  1232. md5 = xs_list_get(md5, 0);
  1233. xs *rel = mastoapi_relationship(&snac1, md5);
  1234. if (rel != NULL)
  1235. res = xs_list_append(res, rel);
  1236. }
  1237. *body = xs_json_dumps(res, 4);
  1238. *ctype = "application/json";
  1239. status = HTTP_STATUS_OK;
  1240. }
  1241. else
  1242. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  1243. }
  1244. else
  1245. if (strcmp(cmd, "/v1/accounts/lookup") == 0) { /** **/
  1246. /* lookup an account */
  1247. const char *acct = xs_dict_get(args, "acct");
  1248. if (!xs_is_null(acct)) {
  1249. xs *s = xs_strip_chars_i(xs_dup(acct), "@");
  1250. xs *l = xs_split_n(s, "@", 1);
  1251. const char *uid = xs_list_get(l, 0);
  1252. const char *host = xs_list_get(l, 1);
  1253. if (uid && (!host || strcmp(host, xs_dict_get(srv_config, "host")) == 0)) {
  1254. snac user;
  1255. if (user_open(&user, uid)) {
  1256. xs *actor = msg_actor(&user);
  1257. xs *macct = mastoapi_account(NULL, actor);
  1258. *body = xs_json_dumps(macct, 4);
  1259. *ctype = "application/json";
  1260. status = HTTP_STATUS_OK;
  1261. user_free(&user);
  1262. }
  1263. }
  1264. }
  1265. }
  1266. else
  1267. if (xs_startswith(cmd, "/v1/accounts/")) { /** **/
  1268. /* account-related information */
  1269. xs *l = xs_split(cmd, "/");
  1270. const char *uid = xs_list_get(l, 3);
  1271. const char *opt = xs_list_get(l, 4);
  1272. if (uid != NULL) {
  1273. snac snac2;
  1274. xs *out = NULL;
  1275. xs *actor = NULL;
  1276. if (logged_in && strcmp(uid, "search") == 0) { /** **/
  1277. /* search for accounts starting with q */
  1278. const char *aq = xs_dict_get(args, "q");
  1279. if (!xs_is_null(aq)) {
  1280. xs *q = xs_utf8_to_lower(aq);
  1281. out = xs_list_new();
  1282. xs *wing = following_list(&snac1);
  1283. xs *wers = follower_list(&snac1);
  1284. xs *ulst = user_list();
  1285. xs_list *p;
  1286. const xs_str *v;
  1287. xs_set seen;
  1288. xs_set_init(&seen);
  1289. /* user relations */
  1290. xs_list *lsts[] = { wing, wers, NULL };
  1291. int n;
  1292. for (n = 0; (p = lsts[n]) != NULL; n++) {
  1293. while (xs_list_iter(&p, &v)) {
  1294. /* already seen? skip */
  1295. if (xs_set_add(&seen, v) == 0)
  1296. continue;
  1297. xs *actor = NULL;
  1298. if (valid_status(object_get(v, &actor))) {
  1299. const char *uname = xs_dict_get(actor, "preferredUsername");
  1300. if (!xs_is_null(uname)) {
  1301. xs *luname = xs_tolower_i(xs_dup(uname));
  1302. if (xs_startswith(luname, q)) {
  1303. xs *acct = mastoapi_account(&snac1, actor);
  1304. out = xs_list_append(out, acct);
  1305. }
  1306. }
  1307. }
  1308. }
  1309. }
  1310. /* local users */
  1311. p = ulst;
  1312. while (xs_list_iter(&p, &v)) {
  1313. snac user;
  1314. /* skip this same user */
  1315. if (strcmp(v, xs_dict_get(snac1.config, "uid")) == 0)
  1316. continue;
  1317. /* skip if the uid does not start with the query */
  1318. xs *v2 = xs_tolower_i(xs_dup(v));
  1319. if (!xs_startswith(v2, q))
  1320. continue;
  1321. if (user_open(&user, v)) {
  1322. /* if it's not already seen, add it */
  1323. if (xs_set_add(&seen, user.actor) == 1) {
  1324. xs *actor = msg_actor(&user);
  1325. xs *acct = mastoapi_account(&snac1, actor);
  1326. out = xs_list_append(out, acct);
  1327. }
  1328. user_free(&user);
  1329. }
  1330. }
  1331. xs_set_free(&seen);
  1332. }
  1333. }
  1334. else
  1335. /* is it a local user? */
  1336. if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
  1337. if (opt == NULL) {
  1338. /* account information */
  1339. actor = msg_actor(&snac2);
  1340. out = mastoapi_account(NULL, actor);
  1341. }
  1342. else
  1343. if (strcmp(opt, "statuses") == 0) { /** **/
  1344. /* the public list of posts of a user */
  1345. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256, NULL);
  1346. xs_list *p = timeline;
  1347. const xs_str *v;
  1348. out = xs_list_new();
  1349. while (xs_list_iter(&p, &v)) {
  1350. xs *msg = NULL;
  1351. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  1352. /* add only posts by the author */
  1353. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  1354. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  1355. xs *st = mastoapi_status(&snac2, msg);
  1356. if (st)
  1357. out = xs_list_append(out, st);
  1358. }
  1359. }
  1360. }
  1361. }
  1362. else
  1363. if (strcmp(opt, "featured_tags") == 0) {
  1364. /* snac doesn't have features tags, yet? */
  1365. /* implement empty response so apps like Tokodon don't show an error */
  1366. out = xs_list_new();
  1367. }
  1368. else
  1369. if (strcmp(opt, "following") == 0) {
  1370. xs *wing = following_list(&snac1);
  1371. out = xs_list_new();
  1372. int c = 0;
  1373. const char *v;
  1374. while (xs_list_next(wing, &v, &c)) {
  1375. xs *actor = NULL;
  1376. if (valid_status(object_get(v, &actor))) {
  1377. xs *acct = mastoapi_account(NULL, actor);
  1378. out = xs_list_append(out, acct);
  1379. }
  1380. }
  1381. }
  1382. else
  1383. if (strcmp(opt, "followers") == 0) {
  1384. out = xs_list_new();
  1385. }
  1386. else
  1387. if (strcmp(opt, "lists") == 0) {
  1388. out = mastoapi_account_lists(&snac1, uid);
  1389. }
  1390. user_free(&snac2);
  1391. }
  1392. else {
  1393. /* try the uid as the md5 of a possibly loaded actor */
  1394. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  1395. if (opt == NULL) {
  1396. /* account information */
  1397. out = mastoapi_account(&snac1, actor);
  1398. }
  1399. else
  1400. if (strcmp(opt, "statuses") == 0) {
  1401. /* we don't serve statuses of others; return the empty list */
  1402. out = xs_list_new();
  1403. }
  1404. else
  1405. if (strcmp(opt, "featured_tags") == 0) {
  1406. /* snac doesn't have features tags, yet? */
  1407. /* implement empty response so apps like Tokodon don't show an error */
  1408. out = xs_list_new();
  1409. }
  1410. else
  1411. if (strcmp(opt, "lists") == 0) {
  1412. out = mastoapi_account_lists(&snac1, uid);
  1413. }
  1414. }
  1415. }
  1416. if (out != NULL) {
  1417. *body = xs_json_dumps(out, 4);
  1418. *ctype = "application/json";
  1419. status = HTTP_STATUS_OK;
  1420. }
  1421. }
  1422. }
  1423. else
  1424. if (strcmp(cmd, "/v1/timelines/home") == 0) { /** **/
  1425. /* the private timeline */
  1426. if (logged_in) {
  1427. xs *ifn = user_index_fn(&snac1, "private");
  1428. xs *out = mastoapi_timeline(&snac1, args, ifn);
  1429. *link = timeline_link_header("/api/v1/timelines/home", out);
  1430. *body = xs_json_dumps(out, 4);
  1431. *ctype = "application/json";
  1432. status = HTTP_STATUS_OK;
  1433. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  1434. }
  1435. else {
  1436. status = HTTP_STATUS_UNAUTHORIZED;
  1437. }
  1438. }
  1439. else
  1440. if (strcmp(cmd, "/v1/timelines/public") == 0) { /** **/
  1441. /* the instance public timeline (public timelines for all users) */
  1442. xs *ifn = instance_index_fn();
  1443. xs *out = mastoapi_timeline(NULL, args, ifn);
  1444. *body = xs_json_dumps(out, 4);
  1445. *ctype = "application/json";
  1446. status = HTTP_STATUS_OK;
  1447. }
  1448. else
  1449. if (xs_startswith(cmd, "/v1/timelines/tag/")) { /** **/
  1450. /* get the tag */
  1451. xs *l = xs_split(cmd, "/");
  1452. const char *tag = xs_list_get(l, -1);
  1453. xs *ifn = tag_fn(tag);
  1454. xs *out = mastoapi_timeline(NULL, args, ifn);
  1455. *body = xs_json_dumps(out, 4);
  1456. *ctype = "application/json";
  1457. status = HTTP_STATUS_OK;
  1458. }
  1459. else
  1460. if (xs_startswith(cmd, "/v1/timelines/list/")) { /** **/
  1461. /* get the list id */
  1462. if (logged_in) {
  1463. xs *l = xs_split(cmd, "/");
  1464. const char *list = xs_list_get(l, -1);
  1465. xs *ifn = list_timeline_fn(&snac1, list);
  1466. xs *out = mastoapi_timeline(NULL, args, ifn);
  1467. *body = xs_json_dumps(out, 4);
  1468. *ctype = "application/json";
  1469. status = HTTP_STATUS_OK;
  1470. }
  1471. else
  1472. status = HTTP_STATUS_MISDIRECTED_REQUEST;
  1473. }
  1474. else
  1475. if (strcmp(cmd, "/v1/conversations") == 0) { /** **/
  1476. /* TBD */
  1477. *body = xs_dup("[]");
  1478. *ctype = "application/json";
  1479. status = HTTP_STATUS_OK;
  1480. }
  1481. else
  1482. if (strcmp(cmd, "/v1/notifications") == 0) { /** **/
  1483. if (logged_in) {
  1484. xs *l = notify_list(&snac1, 0, 64);
  1485. xs *out = xs_list_new();
  1486. const char *v;
  1487. const xs_list *excl = xs_dict_get(args, "exclude_types[]");
  1488. const xs_list *incl = xs_dict_get(args, "types[]");
  1489. const char *min_id = xs_dict_get(args, "min_id");
  1490. const char *max_id = xs_dict_get(args, "max_id");
  1491. const char *limit = xs_dict_get(args, "limit");
  1492. int limit_count = 0;
  1493. if (xs_is_string(limit)) {
  1494. limit_count = atoi(limit);
  1495. }
  1496. if (dbglevel) {
  1497. xs *js = xs_json_dumps(args, 0);
  1498. srv_debug(1, xs_fmt("mastoapi_notifications args %s", js));
  1499. }
  1500. xs_list_foreach(l, v) {
  1501. xs *noti = notify_get(&snac1, v);
  1502. if (noti == NULL)
  1503. continue;
  1504. const char *type = xs_dict_get(noti, "type");
  1505. const char *utype = xs_dict_get(noti, "utype");
  1506. const char *objid = xs_dict_get(noti, "objid");
  1507. const char *id = xs_dict_get(noti, "id");
  1508. const char *actid = xs_dict_get(noti, "actor");
  1509. xs *fid = xs_replace(id, ".", "");
  1510. xs *actor = NULL;
  1511. xs *entry = NULL;
  1512. if (!valid_status(actor_get(actid, &actor)))
  1513. continue;
  1514. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  1515. continue;
  1516. if (is_hidden(&snac1, objid))
  1517. continue;
  1518. if (max_id) {
  1519. if (strcmp(fid, max_id) == 0)
  1520. max_id = NULL;
  1521. continue;
  1522. }
  1523. if (min_id) {
  1524. if (strcmp(fid, min_id) <= 0) {
  1525. continue;
  1526. }
  1527. }
  1528. /* convert the type */
  1529. if (strcmp(type, "Like") == 0 || strcmp(type, "EmojiReact") == 0)
  1530. type = "favourite";
  1531. else
  1532. if (strcmp(type, "Announce") == 0)
  1533. type = "reblog";
  1534. else
  1535. if (strcmp(type, "Follow") == 0)
  1536. type = "follow";
  1537. else
  1538. if (strcmp(type, "Create") == 0)
  1539. type = "mention";
  1540. else
  1541. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  1542. type = "poll";
  1543. else
  1544. continue;
  1545. /* excluded type? */
  1546. if (xs_is_list(excl) && xs_list_in(excl, type) != -1)
  1547. continue;
  1548. /* included type? */
  1549. if (xs_is_list(incl) && xs_list_in(incl, type) == -1)
  1550. continue;
  1551. xs *mn = xs_dict_new();
  1552. mn = xs_dict_append(mn, "type", type);
  1553. mn = xs_dict_append(mn, "id", fid);
  1554. mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
  1555. xs *acct = mastoapi_account(&snac1, actor);
  1556. if (acct == NULL)
  1557. continue;
  1558. mn = xs_dict_append(mn, "account", acct);
  1559. if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
  1560. xs *st = mastoapi_status(&snac1, entry);
  1561. if (st)
  1562. mn = xs_dict_append(mn, "status", st);
  1563. }
  1564. out = xs_list_append(out, mn);
  1565. if (--limit_count <= 0)
  1566. break;
  1567. }
  1568. srv_debug(1, xs_fmt("mastoapi_notifications count %d", xs_list_len(out)));
  1569. *body = xs_json_dumps(out, 4);
  1570. *ctype = "application/json";
  1571. status = HTTP_STATUS_OK;
  1572. }
  1573. else
  1574. status = HTTP_STATUS_UNAUTHORIZED;
  1575. }
  1576. else
  1577. if (strcmp(cmd, "/v1/filters") == 0) { /** **/
  1578. /* snac will never have filters */
  1579. *body = xs_dup("[]");
  1580. *ctype = "application/json";
  1581. status = HTTP_STATUS_OK;
  1582. }
  1583. else
  1584. if (strcmp(cmd, "/v2/filters") == 0) { /** **/
  1585. /* snac will never have filters
  1586. * but still, without a v2 endpoint a short delay is introduced
  1587. * in some apps */
  1588. *body = xs_dup("[]");
  1589. *ctype = "application/json";
  1590. status = HTTP_STATUS_OK;
  1591. }
  1592. else
  1593. if (strcmp(cmd, "/v1/favourites") == 0) { /** **/
  1594. /* snac will never support a list of favourites */
  1595. *body = xs_dup("[]");
  1596. *ctype = "application/json";
  1597. status = HTTP_STATUS_OK;
  1598. }
  1599. else
  1600. if (strcmp(cmd, "/v1/bookmarks") == 0) { /** **/
  1601. if (logged_in) {
  1602. xs *ifn = bookmark_index_fn(&snac1);
  1603. xs *out = mastoapi_timeline(&snac1, args, ifn);
  1604. *body = xs_json_dumps(out, 4);
  1605. *ctype = "application/json";
  1606. status = HTTP_STATUS_OK;
  1607. }
  1608. else
  1609. status = HTTP_STATUS_UNAUTHORIZED;
  1610. }
  1611. else
  1612. if (strcmp(cmd, "/v1/lists") == 0) { /** list of lists **/
  1613. if (logged_in) {
  1614. xs *l = mastoapi_account_lists(&snac1, NULL);
  1615. *body = xs_json_dumps(l, 4);
  1616. *ctype = "application/json";
  1617. status = HTTP_STATUS_OK;
  1618. }
  1619. else
  1620. status = HTTP_STATUS_UNAUTHORIZED;
  1621. }
  1622. else
  1623. if (xs_startswith(cmd, "/v1/lists/")) { /** list information **/
  1624. if (logged_in) {
  1625. xs *l = xs_split(cmd, "/");
  1626. const char *p = xs_list_get(l, -1);
  1627. if (p) {
  1628. if (strcmp(p, "accounts") == 0) {
  1629. p = xs_list_get(l, -2);
  1630. if (p && xs_is_hex(p)) {
  1631. xs *actors = list_content(&snac1, p, NULL, 0);
  1632. xs *out = xs_list_new();
  1633. int c = 0;
  1634. const char *v;
  1635. while (xs_list_next(actors, &v, &c)) {
  1636. xs *actor = NULL;
  1637. if (valid_status(object_get_by_md5(v, &actor))) {
  1638. xs *acct = mastoapi_account(&snac1, actor);
  1639. out = xs_list_append(out, acct);
  1640. }
  1641. }
  1642. *body = xs_json_dumps(out, 4);
  1643. *ctype = "application/json";
  1644. status = HTTP_STATUS_OK;
  1645. }
  1646. }
  1647. else
  1648. if (xs_is_hex(p)) {
  1649. xs *out = xs_list_new();
  1650. xs *lol = list_maint(&snac1, NULL, 0);
  1651. int c = 0;
  1652. const xs_list *v;
  1653. while (xs_list_next(lol, &v, &c)) {
  1654. const char *id = xs_list_get(v, 0);
  1655. if (id && strcmp(id, p) == 0) {
  1656. xs *d = xs_dict_new();
  1657. d = xs_dict_append(d, "id", p);
  1658. d = xs_dict_append(d, "title", xs_list_get(v, 1));
  1659. d = xs_dict_append(d, "replies_policy", "list");
  1660. d = xs_dict_append(d, "exclusive", xs_stock(XSTYPE_FALSE));
  1661. out = xs_dup(d);
  1662. break;
  1663. }
  1664. }
  1665. *body = xs_json_dumps(out, 4);
  1666. *ctype = "application/json";
  1667. status = HTTP_STATUS_OK;
  1668. }
  1669. }
  1670. }
  1671. else
  1672. status = HTTP_STATUS_UNAUTHORIZED;
  1673. }
  1674. else
  1675. if (strcmp(cmd, "/v1/scheduled_statuses") == 0) { /** **/
  1676. /* snac does not schedule notes */
  1677. *body = xs_dup("[]");
  1678. *ctype = "application/json";
  1679. status = HTTP_STATUS_OK;
  1680. }
  1681. else
  1682. if (strcmp(cmd, "/v1/follow_requests") == 0) { /** **/
  1683. if (logged_in) {
  1684. xs *pend = pending_list(&snac1);
  1685. xs *resp = xs_list_new();
  1686. const char *id;
  1687. xs_list_foreach(pend, id) {
  1688. xs *actor = NULL;
  1689. if (valid_status(object_get(id, &actor))) {
  1690. xs *acct = mastoapi_account(&snac1, actor);
  1691. if (acct)
  1692. resp = xs_list_append(resp, acct);
  1693. }
  1694. }
  1695. *body = xs_json_dumps(resp, 4);
  1696. *ctype = "application/json";
  1697. status = HTTP_STATUS_OK;
  1698. }
  1699. }
  1700. else
  1701. if (strcmp(cmd, "/v1/announcements") == 0) { /** **/
  1702. if (logged_in) {
  1703. xs *resp = xs_list_new();
  1704. double la = 0.0;
  1705. xs *user_la = xs_dup(xs_dict_get(snac1.config, "last_announcement"));
  1706. if (user_la != NULL)
  1707. la = xs_number_get(user_la);
  1708. xs *val_date = xs_str_utctime(la, ISO_DATE_SPEC);
  1709. /* contrary to html, we always send the announcement and set the read flag instead */
  1710. const t_announcement *annce = announcement(la);
  1711. if (annce != NULL && annce->text != NULL) {
  1712. xs *an = xs_dict_new();
  1713. xs *id = xs_fmt("%d", annce->timestamp);
  1714. xs *ct = xs_fmt("<p>%s</p>", annce->text);
  1715. an = xs_dict_set(an, "id", id);
  1716. an = xs_dict_set(an, "content", ct);
  1717. an = xs_dict_set(an, "starts_at", xs_stock(XSTYPE_NULL));
  1718. an = xs_dict_set(an, "ends_at", xs_stock(XSTYPE_NULL));
  1719. an = xs_dict_set(an, "all_day", xs_stock(XSTYPE_TRUE));
  1720. an = xs_dict_set(an, "published_at", val_date);
  1721. an = xs_dict_set(an, "updated_at", val_date);
  1722. an = xs_dict_set(an, "read", (annce->timestamp >= la)
  1723. ? xs_stock(XSTYPE_FALSE) : xs_stock(XSTYPE_TRUE));
  1724. an = xs_dict_set(an, "mentions", xs_stock(XSTYPE_LIST));
  1725. an = xs_dict_set(an, "statuses", xs_stock(XSTYPE_LIST));
  1726. an = xs_dict_set(an, "tags", xs_stock(XSTYPE_LIST));
  1727. an = xs_dict_set(an, "emojis", xs_stock(XSTYPE_LIST));
  1728. an = xs_dict_set(an, "reactions", xs_stock(XSTYPE_LIST));
  1729. resp = xs_list_append(resp, an);
  1730. }
  1731. *body = xs_json_dumps(resp, 4);
  1732. *ctype = "application/json";
  1733. status = HTTP_STATUS_OK;
  1734. }
  1735. }
  1736. else
  1737. if (strcmp(cmd, "/v1/custom_emojis") == 0) { /** **/
  1738. xs *emo = emojis();
  1739. xs *list = xs_list_new();
  1740. int c = 0;
  1741. const xs_str *k;
  1742. const xs_val *v;
  1743. while(xs_dict_next(emo, &k, &v, &c)) {
  1744. xs *current = xs_dict_new();
  1745. if (xs_startswith(v, "https://") && xs_startswith((xs_mime_by_ext(v)), "image/")) {
  1746. /* remove first and last colon */
  1747. xs *shortcode = xs_replace(k, ":", "");
  1748. current = xs_dict_append(current, "shortcode", shortcode);
  1749. current = xs_dict_append(current, "url", v);
  1750. current = xs_dict_append(current, "static_url", v);
  1751. current = xs_dict_append(current, "visible_in_picker", xs_stock(XSTYPE_TRUE));
  1752. list = xs_list_append(list, current);
  1753. }
  1754. }
  1755. *body = xs_json_dumps(list, 0);
  1756. *ctype = "application/json";
  1757. status = HTTP_STATUS_OK;
  1758. }
  1759. else
  1760. if (strcmp(cmd, "/v1/instance") == 0) { /** **/
  1761. /* returns an instance object */
  1762. xs *ins = xs_dict_new();
  1763. const char *host = xs_dict_get(srv_config, "host");
  1764. const char *title = xs_dict_get(srv_config, "title");
  1765. const char *sdesc = xs_dict_get(srv_config, "short_description");
  1766. ins = xs_dict_append(ins, "uri", host);
  1767. ins = xs_dict_append(ins, "domain", host);
  1768. ins = xs_dict_append(ins, "title", title && *title ? title : host);
  1769. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  1770. ins = xs_dict_append(ins, "source_url", WHAT_IS_SNAC_URL);
  1771. ins = xs_dict_append(ins, "description", host);
  1772. ins = xs_dict_append(ins, "short_description", sdesc && *sdesc ? sdesc : host);
  1773. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  1774. ins = xs_dict_append(ins, "thumbnail", susie);
  1775. const char *v = xs_dict_get(srv_config, "admin_email");
  1776. if (xs_is_null(v) || *v == '\0')
  1777. v = "admin@localhost";
  1778. ins = xs_dict_append(ins, "email", v);
  1779. ins = xs_dict_append(ins, "rules", xs_stock(XSTYPE_LIST));
  1780. xs *l1 = xs_list_append(xs_list_new(), "en");
  1781. ins = xs_dict_append(ins, "languages", l1);
  1782. xs *wss = xs_fmt("wss:/" "/%s", xs_dict_get(srv_config, "host"));
  1783. xs *urls = xs_dict_new();
  1784. urls = xs_dict_append(urls, "streaming_api", wss);
  1785. ins = xs_dict_append(ins, "urls", urls);
  1786. xs *d2 = xs_dict_append(xs_dict_new(), "user_count", xs_stock(0));
  1787. d2 = xs_dict_append(d2, "status_count", xs_stock(0));
  1788. d2 = xs_dict_append(d2, "domain_count", xs_stock(0));
  1789. ins = xs_dict_append(ins, "stats", d2);
  1790. ins = xs_dict_append(ins, "registrations", xs_stock(XSTYPE_FALSE));
  1791. ins = xs_dict_append(ins, "approval_required", xs_stock(XSTYPE_FALSE));
  1792. ins = xs_dict_append(ins, "invites_enabled", xs_stock(XSTYPE_FALSE));
  1793. xs *cfg = xs_dict_new();
  1794. {
  1795. xs *d11 = xs_json_loads("{\"characters_reserved_per_url\":32,"
  1796. "\"max_characters\":100000,\"max_media_attachments\":4}");
  1797. const xs_number *max_attachments = xs_dict_get(srv_config, "max_attachments");
  1798. if (xs_type(max_attachments) == XSTYPE_NUMBER)
  1799. d11 = xs_dict_set(d11, "max_media_attachments", max_attachments);
  1800. cfg = xs_dict_append(cfg, "statuses", d11);
  1801. xs *d12 = xs_json_loads("{\"max_featured_tags\":0}");
  1802. cfg = xs_dict_append(cfg, "accounts", d12);
  1803. xs *d13 = xs_json_loads("{\"image_matrix_limit\":33177600,"
  1804. "\"image_size_limit\":16777216,"
  1805. "\"video_frame_rate_limit\":120,"
  1806. "\"video_matrix_limit\":8294400,"
  1807. "\"video_size_limit\":103809024}"
  1808. );
  1809. {
  1810. /* get the supported mime types from the internal list */
  1811. const char **p = xs_mime_types;
  1812. xs_set mtypes;
  1813. xs_set_init(&mtypes);
  1814. while (*p) {
  1815. const char *type = p[1];
  1816. if (xs_startswith(type, "image/") ||
  1817. xs_startswith(type, "video/") ||
  1818. xs_startswith(type, "audio/"))
  1819. xs_set_add(&mtypes, type);
  1820. p += 2;
  1821. }
  1822. xs *l = xs_set_result(&mtypes);
  1823. d13 = xs_dict_append(d13, "supported_mime_types", l);
  1824. }
  1825. cfg = xs_dict_append(cfg, "media_attachments", d13);
  1826. xs *d14 = xs_json_loads("{\"max_characters_per_option\":50,"
  1827. "\"max_expiration\":2629746,"
  1828. "\"max_options\":8,\"min_expiration\":300}");
  1829. cfg = xs_dict_append(cfg, "polls", d14);
  1830. }
  1831. ins = xs_dict_append(ins, "configuration", cfg);
  1832. const char *admin_account = xs_dict_get(srv_config, "admin_account");
  1833. if (!xs_is_null(admin_account) && *admin_account) {
  1834. snac admin;
  1835. if (user_open(&admin, admin_account)) {
  1836. xs *actor = msg_actor(&admin);
  1837. xs *acct = mastoapi_account(NULL, actor);
  1838. ins = xs_dict_append(ins, "contact_account", acct);
  1839. user_free(&admin);
  1840. }
  1841. }
  1842. *body = xs_json_dumps(ins, 4);
  1843. *ctype = "application/json";
  1844. status = HTTP_STATUS_OK;
  1845. }
  1846. else
  1847. if (strcmp(cmd, "/v1/instance/peers") == 0) { /** **/
  1848. /* get the collected inbox list as the instances "this domain is aware of" */
  1849. xs *list = inbox_list();
  1850. xs *peers = xs_list_new();
  1851. const char *inbox;
  1852. xs_list_foreach(list, inbox) {
  1853. xs *l = xs_split(inbox, "/");
  1854. const char *domain = xs_list_get(l, 2);
  1855. if (xs_is_string(domain))
  1856. peers = xs_list_append(peers, domain);
  1857. }
  1858. *body = xs_json_dumps(peers, 4);
  1859. *ctype = "application/json";
  1860. status = HTTP_STATUS_OK;
  1861. }
  1862. else
  1863. if (strcmp(cmd, "/v1/instance/extended_description") == 0) { /** **/
  1864. xs *d = xs_dict_new();
  1865. xs *greeting = xs_fmt("%s/greeting.html", srv_basedir);
  1866. time_t t = mtime(greeting);
  1867. xs *updated_at = xs_str_iso_date(t);
  1868. xs *content = xs_replace(snac_blurb, "%host%", xs_dict_get(srv_config, "host"));
  1869. d = xs_dict_set(d, "updated_at", updated_at);
  1870. d = xs_dict_set(d, "content", content);
  1871. *body = xs_json_dumps(d, 4);
  1872. *ctype = "application/json";
  1873. status = HTTP_STATUS_OK;
  1874. }
  1875. else
  1876. if (xs_startswith(cmd, "/v1/statuses/")) { /** **/
  1877. /* information about a status */
  1878. if (logged_in) {
  1879. xs *l = xs_split(cmd, "/");
  1880. const char *oid = xs_list_get(l, 3);
  1881. const char *op = xs_list_get(l, 4);
  1882. if (!xs_is_null(oid)) {
  1883. xs *msg = NULL;
  1884. xs *out = NULL;
  1885. /* skip the 'fake' part of the id */
  1886. oid = MID_TO_MD5(oid);
  1887. xs *id = xs_tolower_i(xs_dup(oid));
  1888. if (valid_status(object_get_by_md5(id, &msg))) {
  1889. if (op == NULL) {
  1890. if (!is_muted(&snac1, get_atto(msg))) {
  1891. /* return the status itself */
  1892. out = mastoapi_status(&snac1, msg);
  1893. }
  1894. }
  1895. else
  1896. if (strcmp(op, "context") == 0) { /** **/
  1897. /* return ancestors and children */
  1898. xs *anc = xs_list_new();
  1899. xs *des = xs_list_new();
  1900. xs_list *p;
  1901. const xs_str *v;
  1902. char pid[MD5_HEX_SIZE];
  1903. /* build the [grand]parent list, moving up */
  1904. strncpy(pid, id, sizeof(pid));
  1905. while (object_parent(pid, pid)) {
  1906. xs *m2 = NULL;
  1907. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  1908. xs *st = mastoapi_status(&snac1, m2);
  1909. if (st)
  1910. anc = xs_list_insert(anc, 0, st);
  1911. }
  1912. else
  1913. break;
  1914. }
  1915. /* build the children list */
  1916. xs *children = object_children(xs_dict_get(msg, "id"));
  1917. p = children;
  1918. while (xs_list_iter(&p, &v)) {
  1919. xs *m2 = NULL;
  1920. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  1921. if (xs_is_null(xs_dict_get(m2, "name"))) {
  1922. xs *st = mastoapi_status(&snac1, m2);
  1923. if (st)
  1924. des = xs_list_append(des, st);
  1925. }
  1926. }
  1927. }
  1928. out = xs_dict_new();
  1929. out = xs_dict_append(out, "ancestors", anc);
  1930. out = xs_dict_append(out, "descendants", des);
  1931. }
  1932. else
  1933. if (strcmp(op, "reblogged_by") == 0 || /** **/
  1934. strcmp(op, "favourited_by") == 0) { /** **/
  1935. /* return the list of people who liked or boosted this */
  1936. out = xs_list_new();
  1937. xs *l = NULL;
  1938. if (op[0] == 'r')
  1939. l = object_announces(xs_dict_get(msg, "id"));
  1940. else
  1941. l = object_likes(xs_dict_get(msg, "id"));
  1942. xs_list *p = l;
  1943. const xs_str *v;
  1944. while (xs_list_iter(&p, &v)) {
  1945. xs *actor2 = NULL;
  1946. if (valid_status(object_get_by_md5(v, &actor2))) {
  1947. xs *acct2 = mastoapi_account(&snac1, actor2);
  1948. out = xs_list_append(out, acct2);
  1949. }
  1950. }
  1951. }
  1952. else
  1953. if (strcmp(op, "source") == 0) { /** **/
  1954. out = xs_dict_new();
  1955. /* get the mastoapi status id */
  1956. out = xs_dict_append(out, "id", xs_list_get(l, 3));
  1957. out = xs_dict_append(out, "text", xs_dict_get(msg, "sourceContent"));
  1958. out = xs_dict_append(out, "spoiler_text", xs_dict_get(msg, "summary"));
  1959. }
  1960. }
  1961. else
  1962. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  1963. if (out != NULL) {
  1964. *body = xs_json_dumps(out, 4);
  1965. *ctype = "application/json";
  1966. status = HTTP_STATUS_OK;
  1967. }
  1968. }
  1969. }
  1970. else
  1971. status = HTTP_STATUS_UNAUTHORIZED;
  1972. }
  1973. else
  1974. if (strcmp(cmd, "/v1/preferences") == 0) { /** **/
  1975. *body = xs_dup("{}");
  1976. *ctype = "application/json";
  1977. status = HTTP_STATUS_OK;
  1978. }
  1979. else
  1980. if (strcmp(cmd, "/v1/markers") == 0) { /** **/
  1981. if (logged_in) {
  1982. const xs_list *timeline = xs_dict_get(args, "timeline[]");
  1983. xs_str *json = NULL;
  1984. if (!xs_is_null(timeline))
  1985. json = xs_json_dumps(markers_get(&snac1, timeline), 4);
  1986. if (!xs_is_null(json))
  1987. *body = json;
  1988. else
  1989. *body = xs_dup("{}");
  1990. *ctype = "application/json";
  1991. status = HTTP_STATUS_OK;
  1992. }
  1993. else
  1994. status = HTTP_STATUS_UNAUTHORIZED;
  1995. }
  1996. else
  1997. if (strcmp(cmd, "/v1/followed_tags") == 0) { /** **/
  1998. if (logged_in) {
  1999. xs *r = xs_list_new();
  2000. const xs_list *followed_hashtags = xs_dict_get_def(snac1.config,
  2001. "followed_hashtags", xs_stock(XSTYPE_LIST));
  2002. const char *hashtag;
  2003. xs_list_foreach(followed_hashtags, hashtag) {
  2004. if (*hashtag == '#') {
  2005. xs *d = xs_dict_new();
  2006. xs *s = xs_fmt("%s?t=%s", srv_baseurl, hashtag + 1);
  2007. d = xs_dict_set(d, "name", hashtag + 1);
  2008. d = xs_dict_set(d, "url", s);
  2009. d = xs_dict_set(d, "history", xs_stock(XSTYPE_LIST));
  2010. r = xs_list_append(r, d);
  2011. }
  2012. }
  2013. *body = xs_json_dumps(r, 4);
  2014. *ctype = "application/json";
  2015. status = HTTP_STATUS_OK;
  2016. }
  2017. else
  2018. status = HTTP_STATUS_UNAUTHORIZED;
  2019. }
  2020. else
  2021. if (strcmp(cmd, "/v1/blocks") == 0) { /** **/
  2022. *body = xs_dup("[]");
  2023. *ctype = "application/json";
  2024. status = HTTP_STATUS_OK;
  2025. }
  2026. else
  2027. if (strcmp(cmd, "/v1/mutes") == 0) { /** **/
  2028. *body = xs_dup("[]");
  2029. *ctype = "application/json";
  2030. status = HTTP_STATUS_OK;
  2031. }
  2032. else
  2033. if (strcmp(cmd, "/v1/trends/tags") == 0) { /** **/
  2034. *body = xs_dup("[]");
  2035. *ctype = "application/json";
  2036. status = HTTP_STATUS_OK;
  2037. }
  2038. else
  2039. if (strcmp(cmd, "/v1/trends/statuses") == 0) { /** **/
  2040. *body = xs_dup("[]");
  2041. *ctype = "application/json";
  2042. status = HTTP_STATUS_OK;
  2043. }
  2044. else
  2045. if (strcmp(cmd, "/v2/search") == 0) { /** **/
  2046. if (logged_in) {
  2047. const char *q = xs_dict_get(args, "q");
  2048. const char *type = xs_dict_get(args, "type");
  2049. const char *offset = xs_dict_get(args, "offset");
  2050. xs *acl = xs_list_new();
  2051. xs *stl = xs_list_new();
  2052. xs *htl = xs_list_new();
  2053. xs *res = xs_dict_new();
  2054. if (xs_is_null(offset) || strcmp(offset, "0") == 0) {
  2055. /* reply something only for offset 0; otherwise,
  2056. apps like Tusky keep asking again and again */
  2057. if (xs_startswith(q, "https://")) {
  2058. if (!timeline_here(&snac1, q)) {
  2059. xs *object = NULL;
  2060. int status;
  2061. status = activitypub_request(&snac1, q, &object);
  2062. snac_debug(&snac1, 1, xs_fmt("Request searched URL %s %d", q, status));
  2063. if (valid_status(status)) {
  2064. /* got it; also request the actor */
  2065. const char *attr_to = get_atto(object);
  2066. xs *actor_obj = NULL;
  2067. if (!xs_is_null(attr_to)) {
  2068. status = actor_request(&snac1, attr_to, &actor_obj);
  2069. snac_debug(&snac1, 1, xs_fmt("Request author %s of %s %d", attr_to, q, status));
  2070. if (valid_status(status)) {
  2071. /* add the actor */
  2072. actor_add(attr_to, actor_obj);
  2073. /* add the post to the timeline */
  2074. timeline_add(&snac1, q, object);
  2075. }
  2076. }
  2077. }
  2078. }
  2079. }
  2080. if (!xs_is_null(q)) {
  2081. if (xs_is_null(type) || strcmp(type, "accounts") == 0) {
  2082. /* do a webfinger query */
  2083. char *actor = NULL;
  2084. char *user = NULL;
  2085. if (valid_status(webfinger_request(q, &actor, &user)) && actor) {
  2086. xs *actor_o = NULL;
  2087. if (valid_status(actor_request(&snac1, actor, &actor_o))) {
  2088. xs *acct = mastoapi_account(NULL, actor_o);
  2089. acl = xs_list_append(acl, acct);
  2090. if (!object_here(actor))
  2091. object_add(actor, actor_o);
  2092. }
  2093. }
  2094. }
  2095. if (xs_is_null(type) || strcmp(type, "hashtags") == 0) {
  2096. /* search this tag */
  2097. xs *tl = tag_search((char *)q, 0, 1);
  2098. if (xs_list_len(tl)) {
  2099. xs *d = xs_dict_new();
  2100. d = xs_dict_append(d, "name", q);
  2101. xs *url = xs_fmt("%s?t=%s", srv_baseurl, q);
  2102. d = xs_dict_append(d, "url", url);
  2103. d = xs_dict_append(d, "history", xs_stock(XSTYPE_LIST));
  2104. htl = xs_list_append(htl, d);
  2105. }
  2106. }
  2107. if (xs_is_null(type) || strcmp(type, "statuses") == 0) {
  2108. int to = 0;
  2109. int cnt = 40;
  2110. xs *tl = content_search(&snac1, q, 1, 0, cnt, 0, &to);
  2111. int c = 0;
  2112. const char *v;
  2113. while (xs_list_next(tl, &v, &c) && --cnt) {
  2114. xs *post = NULL;
  2115. if (!valid_status(timeline_get_by_md5(&snac1, v, &post)))
  2116. continue;
  2117. xs *s = mastoapi_status(&snac1, post);
  2118. if (!xs_is_null(s))
  2119. stl = xs_list_append(stl, s);
  2120. }
  2121. }
  2122. }
  2123. }
  2124. res = xs_dict_append(res, "accounts", acl);
  2125. res = xs_dict_append(res, "statuses", stl);
  2126. res = xs_dict_append(res, "hashtags", htl);
  2127. *body = xs_json_dumps(res, 4);
  2128. *ctype = "application/json";
  2129. status = HTTP_STATUS_OK;
  2130. }
  2131. else
  2132. status = HTTP_STATUS_UNAUTHORIZED;
  2133. }
  2134. /* user cleanup */
  2135. if (logged_in)
  2136. user_free(&snac1);
  2137. srv_debug(1, xs_fmt("mastoapi_get_handler %s %d", q_path, status));
  2138. return status;
  2139. }
  2140. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  2141. const char *payload, int p_size,
  2142. char **body, int *b_size, char **ctype)
  2143. {
  2144. (void)p_size;
  2145. (void)b_size;
  2146. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  2147. return 0;
  2148. int status = HTTP_STATUS_NOT_FOUND;
  2149. xs *args = NULL;
  2150. const char *i_ctype = xs_dict_get(req, "content-type");
  2151. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  2152. if (!xs_is_null(payload))
  2153. args = xs_json_loads(payload);
  2154. }
  2155. else if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded"))
  2156. {
  2157. // Some apps send form data instead of json so we should cater for those
  2158. if (!xs_is_null(payload)) {
  2159. args = xs_url_vars(payload);
  2160. }
  2161. }
  2162. else
  2163. args = xs_dup(xs_dict_get(req, "p_vars"));
  2164. if (args == NULL)
  2165. return HTTP_STATUS_BAD_REQUEST;
  2166. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2167. snac snac = {0};
  2168. int logged_in = process_auth_token(&snac, req);
  2169. if (strcmp(cmd, "/v1/apps") == 0) { /** **/
  2170. const char *name = xs_dict_get(args, "client_name");
  2171. const char *ruri = xs_dict_get(args, "redirect_uris");
  2172. const char *scope = xs_dict_get(args, "scope");
  2173. /* Ice Cubes sends these values as query parameters, so try these */
  2174. if (name == NULL && ruri == NULL && scope == NULL) {
  2175. args = xs_dup(xs_dict_get(req, "q_vars"));
  2176. name = xs_dict_get(args, "client_name");
  2177. ruri = xs_dict_get(args, "redirect_uris");
  2178. scope = xs_dict_get(args, "scope");
  2179. }
  2180. if (xs_type(ruri) == XSTYPE_LIST)
  2181. ruri = xs_dict_get(ruri, 0);
  2182. if (name && ruri) {
  2183. xs *app = xs_dict_new();
  2184. xs *id = xs_replace_i(tid(0), ".", "");
  2185. xs *csec = random_str();
  2186. xs *vkey = random_str();
  2187. xs *cid = NULL;
  2188. /* pick a non-existent random cid */
  2189. for (;;) {
  2190. cid = random_str();
  2191. xs *p_app = app_get(cid);
  2192. if (p_app == NULL)
  2193. break;
  2194. xs_free(cid);
  2195. }
  2196. app = xs_dict_append(app, "name", name);
  2197. app = xs_dict_append(app, "redirect_uri", ruri);
  2198. app = xs_dict_append(app, "client_id", cid);
  2199. app = xs_dict_append(app, "client_secret", csec);
  2200. app = xs_dict_append(app, "vapid_key", vkey);
  2201. app = xs_dict_append(app, "id", id);
  2202. *body = xs_json_dumps(app, 4);
  2203. *ctype = "application/json";
  2204. status = HTTP_STATUS_OK;
  2205. app = xs_dict_append(app, "code", "");
  2206. if (scope)
  2207. app = xs_dict_append(app, "scope", scope);
  2208. app_add(cid, app);
  2209. srv_debug(1, xs_fmt("mastoapi apps: new app %s", cid));
  2210. }
  2211. }
  2212. else
  2213. if (strcmp(cmd, "/v1/statuses") == 0) { /** **/
  2214. if (logged_in) {
  2215. /* post a new Note */
  2216. const char *content = xs_dict_get(args, "status");
  2217. const char *mid = xs_dict_get(args, "in_reply_to_id");
  2218. const char *visibility = xs_dict_get(args, "visibility");
  2219. const char *summary = xs_dict_get(args, "spoiler_text");
  2220. const char *media_ids = xs_dict_get(args, "media_ids");
  2221. const char *language = xs_dict_get(args, "language");
  2222. if (xs_is_null(media_ids))
  2223. media_ids = xs_dict_get(args, "media_ids[]");
  2224. if (xs_is_null(media_ids))
  2225. media_ids = xs_dict_get(args, "media_ids");
  2226. if (xs_is_null(visibility))
  2227. visibility = "public";
  2228. xs *attach_list = xs_list_new();
  2229. xs *irt = NULL;
  2230. /* is it a reply? */
  2231. if (mid != NULL) {
  2232. xs *r_msg = NULL;
  2233. const char *md5 = MID_TO_MD5(mid);
  2234. if (valid_status(object_get_by_md5(md5, &r_msg)))
  2235. irt = xs_dup(xs_dict_get(r_msg, "id"));
  2236. }
  2237. /* does it have attachments? */
  2238. if (!xs_is_null(media_ids)) {
  2239. xs *mi = NULL;
  2240. if (xs_type(media_ids) == XSTYPE_LIST)
  2241. mi = xs_dup(media_ids);
  2242. else {
  2243. mi = xs_list_new();
  2244. mi = xs_list_append(mi, media_ids);
  2245. }
  2246. xs_list *p = mi;
  2247. const xs_str *v;
  2248. while (xs_list_iter(&p, &v)) {
  2249. xs *l = xs_list_new();
  2250. xs *url = xs_fmt("%s/s/%s", snac.actor, v);
  2251. xs *desc = static_get_meta(&snac, v);
  2252. l = xs_list_append(l, url);
  2253. l = xs_list_append(l, desc);
  2254. attach_list = xs_list_append(attach_list, l);
  2255. }
  2256. }
  2257. /* prepare the message */
  2258. int scope = 1;
  2259. if (strcmp(visibility, "unlisted") == 0)
  2260. scope = 2;
  2261. else
  2262. if (strcmp(visibility, "public") == 0)
  2263. scope = 0;
  2264. xs *msg = msg_note(&snac, content, NULL, irt, attach_list, scope, language, NULL);
  2265. if (!xs_is_null(summary) && *summary) {
  2266. msg = xs_dict_set(msg, "sensitive", xs_stock(XSTYPE_TRUE));
  2267. msg = xs_dict_set(msg, "summary", summary);
  2268. }
  2269. /* scheduled? */
  2270. const char *scheduled_at = xs_dict_get(args, "scheduled_at");
  2271. if (xs_is_string(scheduled_at) && *scheduled_at) {
  2272. msg = xs_dict_set(msg, "published", scheduled_at);
  2273. schedule_add(&snac, xs_dict_get(msg, "id"), msg);
  2274. }
  2275. else {
  2276. /* store */
  2277. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  2278. /* 'Create' message */
  2279. xs *c_msg = msg_create(&snac, msg);
  2280. enqueue_message(&snac, c_msg);
  2281. timeline_touch(&snac);
  2282. }
  2283. /* convert to a mastodon status as a response code */
  2284. xs *st = mastoapi_status(&snac, msg);
  2285. *body = xs_json_dumps(st, 4);
  2286. *ctype = "application/json";
  2287. status = HTTP_STATUS_OK;
  2288. }
  2289. else
  2290. status = HTTP_STATUS_UNAUTHORIZED;
  2291. }
  2292. else
  2293. if (xs_startswith(cmd, "/v1/statuses")) { /** **/
  2294. if (logged_in) {
  2295. /* operations on a status */
  2296. xs *l = xs_split(cmd, "/");
  2297. const char *mid = xs_list_get(l, 3);
  2298. const char *op = xs_list_get(l, 4);
  2299. if (!xs_is_null(mid)) {
  2300. xs *msg = NULL;
  2301. xs *out = NULL;
  2302. /* skip the 'fake' part of the id */
  2303. mid = MID_TO_MD5(mid);
  2304. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  2305. const char *id = xs_dict_get(msg, "id");
  2306. if (op == NULL) {
  2307. /* no operation (?) */
  2308. }
  2309. else
  2310. if (strcmp(op, "favourite") == 0) { /** **/
  2311. xs *n_msg = msg_admiration(&snac, id, "Like");
  2312. if (n_msg != NULL) {
  2313. enqueue_message(&snac, n_msg);
  2314. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  2315. out = mastoapi_status(&snac, msg);
  2316. }
  2317. }
  2318. else
  2319. if (strcmp(op, "unfavourite") == 0) { /** **/
  2320. xs *n_msg = msg_repulsion(&snac, id, "Like");
  2321. if (n_msg != NULL) {
  2322. enqueue_message(&snac, n_msg);
  2323. out = mastoapi_status(&snac, msg);
  2324. }
  2325. }
  2326. else
  2327. if (strcmp(op, "reblog") == 0) { /** **/
  2328. xs *n_msg = msg_admiration(&snac, id, "Announce");
  2329. if (n_msg != NULL) {
  2330. enqueue_message(&snac, n_msg);
  2331. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  2332. out = mastoapi_status(&snac, msg);
  2333. }
  2334. }
  2335. else
  2336. if (strcmp(op, "unreblog") == 0) { /** **/
  2337. xs *n_msg = msg_repulsion(&snac, id, "Announce");
  2338. if (n_msg != NULL) {
  2339. enqueue_message(&snac, n_msg);
  2340. out = mastoapi_status(&snac, msg);
  2341. }
  2342. }
  2343. else
  2344. if (strcmp(op, "bookmark") == 0) { /** **/
  2345. /* bookmark this message */
  2346. if (bookmark(&snac, id) == 0)
  2347. out = mastoapi_status(&snac, msg);
  2348. else
  2349. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2350. }
  2351. else
  2352. if (strcmp(op, "unbookmark") == 0) { /** **/
  2353. /* unbookmark this message */
  2354. unbookmark(&snac, id);
  2355. out = mastoapi_status(&snac, msg);
  2356. }
  2357. else
  2358. if (strcmp(op, "pin") == 0) { /** **/
  2359. /* pin this message */
  2360. if (pin(&snac, id) == 0)
  2361. out = mastoapi_status(&snac, msg);
  2362. else
  2363. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2364. }
  2365. else
  2366. if (strcmp(op, "unpin") == 0) { /** **/
  2367. /* unpin this message */
  2368. unpin(&snac, id);
  2369. out = mastoapi_status(&snac, msg);
  2370. }
  2371. else
  2372. if (strcmp(op, "mute") == 0) { /** **/
  2373. /* Mastodon's mute is snac's hide */
  2374. }
  2375. else
  2376. if (strcmp(op, "unmute") == 0) { /** **/
  2377. /* Mastodon's unmute is snac's unhide */
  2378. }
  2379. }
  2380. if (out != NULL) {
  2381. *body = xs_json_dumps(out, 4);
  2382. *ctype = "application/json";
  2383. status = HTTP_STATUS_OK;
  2384. }
  2385. }
  2386. }
  2387. else
  2388. status = HTTP_STATUS_UNAUTHORIZED;
  2389. }
  2390. else
  2391. if (strcmp(cmd, "/v1/notifications/clear") == 0) { /** **/
  2392. if (logged_in) {
  2393. notify_clear(&snac);
  2394. timeline_touch(&snac);
  2395. *body = xs_dup("{}");
  2396. *ctype = "application/json";
  2397. status = HTTP_STATUS_OK;
  2398. }
  2399. else
  2400. status = HTTP_STATUS_UNAUTHORIZED;
  2401. }
  2402. else
  2403. if (strcmp(cmd, "/v1/push/subscription") == 0) { /** **/
  2404. /* I don't know what I'm doing */
  2405. if (logged_in) {
  2406. const char *v;
  2407. xs *wpush = xs_dict_new();
  2408. wpush = xs_dict_append(wpush, "id", "1");
  2409. v = xs_dict_get(args, "data");
  2410. v = xs_dict_get(v, "alerts");
  2411. wpush = xs_dict_append(wpush, "alerts", v);
  2412. v = xs_dict_get(args, "subscription");
  2413. v = xs_dict_get(v, "endpoint");
  2414. wpush = xs_dict_append(wpush, "endpoint", v);
  2415. xs *server_key = random_str();
  2416. wpush = xs_dict_append(wpush, "server_key", server_key);
  2417. *body = xs_json_dumps(wpush, 4);
  2418. *ctype = "application/json";
  2419. status = HTTP_STATUS_OK;
  2420. }
  2421. else
  2422. status = HTTP_STATUS_UNAUTHORIZED;
  2423. }
  2424. else
  2425. if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { /** **/
  2426. if (logged_in) {
  2427. const xs_list *file = xs_dict_get(args, "file");
  2428. const char *desc = xs_dict_get(args, "description");
  2429. if (xs_is_null(desc))
  2430. desc = "";
  2431. status = HTTP_STATUS_BAD_REQUEST;
  2432. if (xs_type(file) == XSTYPE_LIST) {
  2433. const char *fn = xs_list_get(file, 0);
  2434. if (*fn != '\0') {
  2435. char *ext = strrchr(fn, '.');
  2436. char rnd[32];
  2437. xs_rnd_buf(rnd, sizeof(rnd));
  2438. xs *hash = xs_md5_hex(rnd, sizeof(rnd));
  2439. xs *id = xs_fmt("post-%s%s", hash, ext ? ext : "");
  2440. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  2441. int fo = xs_number_get(xs_list_get(file, 1));
  2442. int fs = xs_number_get(xs_list_get(file, 2));
  2443. /* store */
  2444. static_put(&snac, id, payload + fo, fs);
  2445. static_put_meta(&snac, id, desc);
  2446. /* prepare a response */
  2447. xs *rsp = xs_dict_new();
  2448. rsp = xs_dict_append(rsp, "id", id);
  2449. rsp = xs_dict_append(rsp, "type", "image");
  2450. rsp = xs_dict_append(rsp, "url", url);
  2451. rsp = xs_dict_append(rsp, "preview_url", url);
  2452. rsp = xs_dict_append(rsp, "remote_url", url);
  2453. rsp = xs_dict_append(rsp, "description", desc);
  2454. *body = xs_json_dumps(rsp, 4);
  2455. *ctype = "application/json";
  2456. status = HTTP_STATUS_OK;
  2457. }
  2458. }
  2459. }
  2460. else
  2461. status = HTTP_STATUS_UNAUTHORIZED;
  2462. }
  2463. else
  2464. if (xs_startswith(cmd, "/v1/accounts")) { /** **/
  2465. if (logged_in) {
  2466. /* account-related information */
  2467. xs *l = xs_split(cmd, "/");
  2468. const char *md5 = xs_list_get(l, 3);
  2469. const char *opt = xs_list_get(l, 4);
  2470. xs *rsp = NULL;
  2471. if (!xs_is_null(md5) && *md5) {
  2472. xs *actor_o = NULL;
  2473. if (xs_is_null(opt)) {
  2474. /* ? */
  2475. }
  2476. else
  2477. if (strcmp(opt, "follow") == 0) { /** **/
  2478. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2479. const char *actor = xs_dict_get(actor_o, "id");
  2480. xs *msg = msg_follow(&snac, actor);
  2481. if (msg != NULL) {
  2482. /* reload the actor from the message, in may be different */
  2483. actor = xs_dict_get(msg, "object");
  2484. following_add(&snac, actor, msg);
  2485. enqueue_output_by_actor(&snac, msg, actor, 0);
  2486. rsp = mastoapi_relationship(&snac, md5);
  2487. }
  2488. }
  2489. }
  2490. else
  2491. if (strcmp(opt, "unfollow") == 0) { /** **/
  2492. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2493. const char *actor = xs_dict_get(actor_o, "id");
  2494. /* get the following object */
  2495. xs *object = NULL;
  2496. if (valid_status(following_get(&snac, actor, &object))) {
  2497. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  2498. following_del(&snac, actor);
  2499. enqueue_output_by_actor(&snac, msg, actor, 0);
  2500. rsp = mastoapi_relationship(&snac, md5);
  2501. }
  2502. }
  2503. }
  2504. else
  2505. if (strcmp(opt, "block") == 0) { /** **/
  2506. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2507. const char *actor = xs_dict_get(actor_o, "id");
  2508. mute(&snac, actor);
  2509. rsp = mastoapi_relationship(&snac, md5);
  2510. }
  2511. }
  2512. else
  2513. if (strcmp(opt, "unblock") == 0) { /** **/
  2514. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2515. const char *actor = xs_dict_get(actor_o, "id");
  2516. unmute(&snac, actor);
  2517. rsp = mastoapi_relationship(&snac, md5);
  2518. }
  2519. }
  2520. }
  2521. if (rsp != NULL) {
  2522. *body = xs_json_dumps(rsp, 4);
  2523. *ctype = "application/json";
  2524. status = HTTP_STATUS_OK;
  2525. }
  2526. }
  2527. else
  2528. status = HTTP_STATUS_UNAUTHORIZED;
  2529. }
  2530. else
  2531. if (xs_startswith(cmd, "/v1/polls")) { /** **/
  2532. if (logged_in) {
  2533. /* operations on a status */
  2534. xs *l = xs_split(cmd, "/");
  2535. const char *mid = xs_list_get(l, 3);
  2536. const char *op = xs_list_get(l, 4);
  2537. if (!xs_is_null(mid)) {
  2538. xs *msg = NULL;
  2539. xs *out = NULL;
  2540. /* skip the 'fake' part of the id */
  2541. mid = MID_TO_MD5(mid);
  2542. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  2543. const char *id = xs_dict_get(msg, "id");
  2544. const char *atto = get_atto(msg);
  2545. const xs_list *opts = xs_dict_get(msg, "oneOf");
  2546. if (opts == NULL)
  2547. opts = xs_dict_get(msg, "anyOf");
  2548. if (op == NULL) {
  2549. }
  2550. else
  2551. if (strcmp(op, "votes") == 0) {
  2552. const xs_list *choices = xs_dict_get(args, "choices[]");
  2553. if (xs_is_null(choices))
  2554. choices = xs_dict_get(args, "choices");
  2555. if (xs_type(choices) == XSTYPE_LIST) {
  2556. const xs_str *v;
  2557. int c = 0;
  2558. while (xs_list_next(choices, &v, &c)) {
  2559. int io = atoi(v);
  2560. const xs_dict *o = xs_list_get(opts, io);
  2561. if (o) {
  2562. const char *name = xs_dict_get(o, "name");
  2563. xs *msg = msg_note(&snac, "", atto, (char *)id, NULL, 1, NULL, NULL);
  2564. msg = xs_dict_append(msg, "name", name);
  2565. xs *c_msg = msg_create(&snac, msg);
  2566. enqueue_message(&snac, c_msg);
  2567. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  2568. }
  2569. }
  2570. out = mastoapi_poll(&snac, msg);
  2571. }
  2572. }
  2573. }
  2574. if (out != NULL) {
  2575. *body = xs_json_dumps(out, 4);
  2576. *ctype = "application/json";
  2577. status = HTTP_STATUS_OK;
  2578. }
  2579. }
  2580. }
  2581. else
  2582. status = HTTP_STATUS_UNAUTHORIZED;
  2583. }
  2584. else
  2585. if (strcmp(cmd, "/v1/lists") == 0) {
  2586. if (logged_in) {
  2587. const char *title = xs_dict_get(args, "title");
  2588. if (xs_type(title) == XSTYPE_STRING) {
  2589. /* add the list */
  2590. xs *out = xs_dict_new();
  2591. xs *lid = list_maint(&snac, title, 1);
  2592. if (!xs_is_null(lid)) {
  2593. out = xs_dict_append(out, "id", lid);
  2594. out = xs_dict_append(out, "title", title);
  2595. out = xs_dict_append(out, "replies_policy", xs_dict_get_def(args, "replies_policy", "list"));
  2596. out = xs_dict_append(out, "exclusive", xs_stock(XSTYPE_FALSE));
  2597. status = HTTP_STATUS_OK;
  2598. }
  2599. else {
  2600. out = xs_dict_append(out, "error", "cannot create list");
  2601. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2602. }
  2603. *body = xs_json_dumps(out, 4);
  2604. *ctype = "application/json";
  2605. }
  2606. else
  2607. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2608. }
  2609. }
  2610. else
  2611. if (xs_startswith(cmd, "/v1/lists/")) { /** list maintenance **/
  2612. if (logged_in) {
  2613. xs *l = xs_split(cmd, "/");
  2614. const char *op = xs_list_get(l, -1);
  2615. const char *id = xs_list_get(l, -2);
  2616. if (op && id && xs_is_hex(id)) {
  2617. if (strcmp(op, "accounts") == 0) {
  2618. const xs_list *accts = xs_dict_get(args, "account_ids[]");
  2619. if (xs_is_null(accts))
  2620. accts = xs_dict_get(args, "account_ids");
  2621. int c = 0;
  2622. const char *v;
  2623. while (xs_list_next(accts, &v, &c)) {
  2624. list_content(&snac, id, v, 1);
  2625. }
  2626. xs *out = xs_dict_new();
  2627. *body = xs_json_dumps(out, 4);
  2628. *ctype = "application/json";
  2629. status = HTTP_STATUS_OK;
  2630. }
  2631. }
  2632. }
  2633. }
  2634. else if (strcmp(cmd, "/v1/markers") == 0) { /** **/
  2635. xs_str *json = NULL;
  2636. if (logged_in) {
  2637. const xs_str *home_marker = xs_dict_get(args, "home[last_read_id]");
  2638. if (xs_is_null(home_marker)) {
  2639. const xs_dict *home = xs_dict_get(args, "home");
  2640. if (!xs_is_null(home))
  2641. home_marker = xs_dict_get(home, "last_read_id");
  2642. }
  2643. const xs_str *notify_marker = xs_dict_get(args, "notifications[last_read_id]");
  2644. if (xs_is_null(notify_marker)) {
  2645. const xs_dict *notify = xs_dict_get(args, "notifications");
  2646. if (!xs_is_null(notify))
  2647. notify_marker = xs_dict_get(notify, "last_read_id");
  2648. }
  2649. json = xs_json_dumps(markers_set(&snac, home_marker, notify_marker), 4);
  2650. }
  2651. if (!xs_is_null(json))
  2652. *body = json;
  2653. else
  2654. *body = xs_dup("{}");
  2655. *ctype = "application/json";
  2656. status = HTTP_STATUS_OK;
  2657. }
  2658. else
  2659. if (xs_startswith(cmd, "/v1/follow_requests")) { /** **/
  2660. if (logged_in) {
  2661. /* "authorize" or "reject" */
  2662. xs *rel = NULL;
  2663. xs *l = xs_split(cmd, "/");
  2664. const char *md5 = xs_list_get(l, -2);
  2665. const char *s_cmd = xs_list_get(l, -1);
  2666. if (xs_is_string(md5) && xs_is_string(s_cmd)) {
  2667. xs *actor = NULL;
  2668. if (valid_status(object_get_by_md5(md5, &actor))) {
  2669. const char *actor_id = xs_dict_get(actor, "id");
  2670. if (strcmp(s_cmd, "authorize") == 0) {
  2671. xs *fwreq = pending_get(&snac, actor_id);
  2672. if (fwreq != NULL) {
  2673. xs *reply = msg_accept(&snac, fwreq, actor_id);
  2674. enqueue_message(&snac, reply);
  2675. if (xs_is_null(xs_dict_get(fwreq, "published"))) {
  2676. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  2677. fwreq = xs_dict_set(fwreq, "published", date);
  2678. }
  2679. timeline_add(&snac, xs_dict_get(fwreq, "id"), fwreq);
  2680. follower_add(&snac, actor_id);
  2681. pending_del(&snac, actor_id);
  2682. rel = mastoapi_relationship(&snac, md5);
  2683. }
  2684. }
  2685. else
  2686. if (strcmp(s_cmd, "reject") == 0) {
  2687. pending_del(&snac, actor_id);
  2688. rel = mastoapi_relationship(&snac, md5);
  2689. }
  2690. }
  2691. }
  2692. if (rel != NULL) {
  2693. *body = xs_json_dumps(rel, 4);
  2694. *ctype = "application/json";
  2695. status = HTTP_STATUS_OK;
  2696. }
  2697. }
  2698. }
  2699. else
  2700. if (xs_startswith(cmd, "/v1/tags/")) { /** **/
  2701. if (logged_in) {
  2702. xs *l = xs_split(cmd, "/");
  2703. const char *i_tag = xs_list_get(l, 3);
  2704. const char *cmd = xs_list_get(l, 4);
  2705. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2706. if (xs_is_string(i_tag) && xs_is_string(cmd)) {
  2707. int ok = 0;
  2708. xs *tag = xs_fmt("#%s", i_tag);
  2709. xs *followed_hashtags = xs_dup(xs_dict_get_def(snac.config,
  2710. "followed_hashtags", xs_stock(XSTYPE_LIST)));
  2711. if (strcmp(cmd, "follow") == 0) {
  2712. followed_hashtags = xs_list_append(followed_hashtags, tag);
  2713. ok = 1;
  2714. }
  2715. else
  2716. if (strcmp(cmd, "unfollow") == 0) {
  2717. int off = xs_list_in(followed_hashtags, tag);
  2718. if (off != -1)
  2719. followed_hashtags = xs_list_del(followed_hashtags, off);
  2720. ok = 1;
  2721. }
  2722. if (ok) {
  2723. /* update */
  2724. xs_dict_set(snac.config, "followed_hashtags", followed_hashtags);
  2725. user_persist(&snac, 0);
  2726. xs *d = xs_dict_new();
  2727. xs *s = xs_fmt("%s?t=%s", srv_baseurl, i_tag);
  2728. d = xs_dict_set(d, "name", i_tag);
  2729. d = xs_dict_set(d, "url", s);
  2730. d = xs_dict_set(d, "history", xs_stock(XSTYPE_LIST));
  2731. *body = xs_json_dumps(d, 4);
  2732. *ctype = "application/json";
  2733. status = HTTP_STATUS_OK;
  2734. }
  2735. }
  2736. }
  2737. }
  2738. else
  2739. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2740. /* user cleanup */
  2741. if (logged_in)
  2742. user_free(&snac);
  2743. srv_debug(1, xs_fmt("mastoapi_post_handler %s %d", q_path, status));
  2744. return status;
  2745. }
  2746. int mastoapi_delete_handler(const xs_dict *req, const char *q_path,
  2747. const char *payload, int p_size,
  2748. char **body, int *b_size, char **ctype)
  2749. {
  2750. (void)p_size;
  2751. (void)body;
  2752. (void)b_size;
  2753. (void)ctype;
  2754. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  2755. return 0;
  2756. int status = HTTP_STATUS_NOT_FOUND;
  2757. xs *args = NULL;
  2758. const char *i_ctype = xs_dict_get(req, "content-type");
  2759. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  2760. if (!xs_is_null(payload))
  2761. args = xs_json_loads(payload);
  2762. }
  2763. else if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded"))
  2764. {
  2765. // Some apps send form data instead of json so we should cater for those
  2766. if (!xs_is_null(payload)) {
  2767. args = xs_url_vars(payload);
  2768. }
  2769. }
  2770. else
  2771. args = xs_dup(xs_dict_get(req, "p_vars"));
  2772. if (args == NULL)
  2773. return HTTP_STATUS_BAD_REQUEST;
  2774. snac snac = {0};
  2775. int logged_in = process_auth_token(&snac, req);
  2776. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2777. if (xs_startswith(cmd, "/v1/push/subscription") || xs_startswith(cmd, "/v2/push/subscription")) { /** **/
  2778. // pretend we deleted it, since it doesn't exist anyway
  2779. status = HTTP_STATUS_OK;
  2780. }
  2781. else
  2782. if (xs_startswith(cmd, "/v1/lists/")) {
  2783. if (logged_in) {
  2784. xs *l = xs_split(cmd, "/");
  2785. const char *p = xs_list_get(l, -1);
  2786. if (p) {
  2787. if (strcmp(p, "accounts") == 0) {
  2788. /* delete account from list */
  2789. p = xs_list_get(l, -2);
  2790. const xs_list *accts = xs_dict_get(args, "account_ids[]");
  2791. if (xs_is_null(accts))
  2792. accts = xs_dict_get(args, "account_ids");
  2793. int c = 0;
  2794. const char *v;
  2795. while (xs_list_next(accts, &v, &c)) {
  2796. list_content(&snac, p, v, 2);
  2797. }
  2798. }
  2799. else {
  2800. /* delete list */
  2801. if (xs_is_hex(p)) {
  2802. list_maint(&snac, p, 2);
  2803. }
  2804. }
  2805. }
  2806. *ctype = "application/json";
  2807. status = HTTP_STATUS_OK;
  2808. }
  2809. else
  2810. status = HTTP_STATUS_UNAUTHORIZED;
  2811. }
  2812. /* user cleanup */
  2813. if (logged_in)
  2814. user_free(&snac);
  2815. srv_debug(1, xs_fmt("mastoapi_delete_handler %s %d", q_path, status));
  2816. return status;
  2817. }
  2818. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  2819. const char *payload, int p_size,
  2820. char **body, int *b_size, char **ctype)
  2821. {
  2822. (void)p_size;
  2823. (void)b_size;
  2824. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  2825. return 0;
  2826. int status = HTTP_STATUS_NOT_FOUND;
  2827. xs *args = NULL;
  2828. const char *i_ctype = xs_dict_get(req, "content-type");
  2829. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  2830. if (!xs_is_null(payload))
  2831. args = xs_json_loads(payload);
  2832. }
  2833. else
  2834. args = xs_dup(xs_dict_get(req, "p_vars"));
  2835. if (args == NULL)
  2836. return HTTP_STATUS_BAD_REQUEST;
  2837. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2838. snac snac = {0};
  2839. int logged_in = process_auth_token(&snac, req);
  2840. if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) { /** **/
  2841. if (logged_in) {
  2842. xs *l = xs_split(cmd, "/");
  2843. const char *stid = xs_list_get(l, 3);
  2844. if (!xs_is_null(stid)) {
  2845. const char *desc = xs_dict_get(args, "description");
  2846. /* set the image metadata */
  2847. static_put_meta(&snac, stid, desc);
  2848. /* prepare a response */
  2849. xs *rsp = xs_dict_new();
  2850. xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
  2851. rsp = xs_dict_append(rsp, "id", stid);
  2852. rsp = xs_dict_append(rsp, "type", "image");
  2853. rsp = xs_dict_append(rsp, "url", url);
  2854. rsp = xs_dict_append(rsp, "preview_url", url);
  2855. rsp = xs_dict_append(rsp, "remote_url", url);
  2856. rsp = xs_dict_append(rsp, "description", desc);
  2857. *body = xs_json_dumps(rsp, 4);
  2858. *ctype = "application/json";
  2859. status = HTTP_STATUS_OK;
  2860. }
  2861. }
  2862. else
  2863. status = HTTP_STATUS_UNAUTHORIZED;
  2864. }
  2865. else
  2866. if (xs_startswith(cmd, "/v1/statuses")) {
  2867. if (logged_in) {
  2868. xs *l = xs_split(cmd, "/");
  2869. const char *mid = xs_list_get(l, 3);
  2870. if (!xs_is_null(mid)) {
  2871. const char *md5 = MID_TO_MD5(mid);
  2872. xs *rsp = NULL;
  2873. xs *msg = NULL;
  2874. if (valid_status(timeline_get_by_md5(&snac, md5, &msg))) {
  2875. const char *content = xs_dict_get(args, "status");
  2876. xs *atls = xs_list_new();
  2877. xs *f_content = not_really_markdown(content, &atls, NULL);
  2878. /* replace fields with new content */
  2879. msg = xs_dict_set(msg, "sourceContent", content);
  2880. msg = xs_dict_set(msg, "content", f_content);
  2881. xs *updated = xs_str_utctime(0, ISO_DATE_SPEC);
  2882. msg = xs_dict_set(msg, "updated", updated);
  2883. /* overwrite object, not updating the indexes */
  2884. object_add_ow(xs_dict_get(msg, "id"), msg);
  2885. /* update message */
  2886. xs *c_msg = msg_update(&snac, msg);
  2887. enqueue_message(&snac, c_msg);
  2888. rsp = mastoapi_status(&snac, msg);
  2889. }
  2890. if (rsp != NULL) {
  2891. *body = xs_json_dumps(rsp, 4);
  2892. *ctype = "application/json";
  2893. status = HTTP_STATUS_OK;
  2894. }
  2895. }
  2896. }
  2897. else
  2898. status = HTTP_STATUS_UNAUTHORIZED;
  2899. }
  2900. /* user cleanup */
  2901. if (logged_in)
  2902. user_free(&snac);
  2903. srv_debug(1, xs_fmt("mastoapi_put_handler %s %d", q_path, status));
  2904. return status;
  2905. }
  2906. void persist_image(const char *key, const xs_val *data, const char *payload, snac *snac)
  2907. /* Store header or avatar */
  2908. {
  2909. if (data != NULL) {
  2910. if (xs_type(data) == XSTYPE_LIST) {
  2911. const char *fn = xs_list_get(data, 0);
  2912. if (fn && *fn) {
  2913. const char *ext = strrchr(fn, '.');
  2914. /* Mona iOS sends always jpg as application/octet-stream with no filename */
  2915. if (ext == NULL || strcmp(fn, key) == 0) {
  2916. ext = ".jpg";
  2917. }
  2918. /* Make sure we have a unique file name, otherwise updated images will not be
  2919. * re-loaded by clients. */
  2920. xs *rnd = random_str();
  2921. xs *hash = xs_md5_hex(rnd, strlen(rnd));
  2922. xs *id = xs_fmt("%s%s", hash, ext);
  2923. xs *url = xs_fmt("%s/s/%s", snac->actor, id);
  2924. int fo = xs_number_get(xs_list_get(data, 1));
  2925. int fs = xs_number_get(xs_list_get(data, 2));
  2926. /* store */
  2927. static_put(snac, id, payload + fo, fs);
  2928. snac->config = xs_dict_set(snac->config, key, url);
  2929. }
  2930. }
  2931. }
  2932. }
  2933. int mastoapi_patch_handler(const xs_dict *req, const char *q_path,
  2934. const char *payload, int p_size,
  2935. char **body, int *b_size, char **ctype)
  2936. /* Handle profile updates */
  2937. {
  2938. (void)p_size;
  2939. (void)b_size;
  2940. if (!xs_startswith(q_path, "/api/v1/"))
  2941. return 0;
  2942. int status = HTTP_STATUS_NOT_FOUND;
  2943. xs *args = NULL;
  2944. const char *i_ctype = xs_dict_get(req, "content-type");
  2945. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  2946. if (!xs_is_null(payload))
  2947. args = xs_json_loads(payload);
  2948. }
  2949. else if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded"))
  2950. {
  2951. // Some apps send form data instead of json so we should cater for those
  2952. if (!xs_is_null(payload)) {
  2953. args = xs_url_vars(payload);
  2954. }
  2955. }
  2956. else
  2957. args = xs_dup(xs_dict_get(req, "p_vars"));
  2958. if (args == NULL)
  2959. return HTTP_STATUS_BAD_REQUEST;
  2960. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2961. snac snac = {0};
  2962. int logged_in = process_auth_token(&snac, req);
  2963. if (xs_startswith(cmd, "/v1/accounts/update_credentials")) {
  2964. /* Update user profile fields */
  2965. if (logged_in) {
  2966. int c = 0;
  2967. const xs_str *k;
  2968. const xs_val *v;
  2969. const xs_str *field_name = NULL;
  2970. xs *new_fields = xs_dict_new();
  2971. while (xs_dict_next(args, &k, &v, &c)) {
  2972. if (strcmp(k, "display_name") == 0) {
  2973. if (v != NULL)
  2974. snac.config = xs_dict_set(snac.config, "name", v);
  2975. }
  2976. else
  2977. if (strcmp(k, "note") == 0) {
  2978. if (v != NULL)
  2979. snac.config = xs_dict_set(snac.config, "bio", v);
  2980. }
  2981. else
  2982. if (strcmp(k, "bot") == 0) {
  2983. if (v != NULL)
  2984. snac.config = xs_dict_set(snac.config, "bot",
  2985. (strcmp(v, "true") == 0 ||
  2986. strcmp(v, "1") == 0) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  2987. }
  2988. else
  2989. if (strcmp(k, "source[sensitive]") == 0) {
  2990. if (v != NULL)
  2991. snac.config = xs_dict_set(snac.config, "cw",
  2992. strcmp(v, "true") == 0 ? "open" : "");
  2993. }
  2994. else
  2995. if (strcmp(k, "source[privacy]") == 0) {
  2996. if (v != NULL)
  2997. snac.config = xs_dict_set(snac.config, "private",
  2998. strcmp(v, "private") == 0 ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  2999. }
  3000. else
  3001. if (strcmp(k, "header") == 0) {
  3002. persist_image("header", v, payload, &snac);
  3003. }
  3004. else
  3005. if (strcmp(k, "avatar") == 0) {
  3006. persist_image("avatar", v, payload, &snac);
  3007. }
  3008. else
  3009. if (xs_between("fields_attributes", k, "[name]")) {
  3010. field_name = strcmp(v, "") != 0 ? v : NULL;
  3011. }
  3012. else
  3013. if (xs_between("fields_attributes", k, "[value]")) {
  3014. if (field_name != NULL) {
  3015. new_fields = xs_dict_set(new_fields, field_name, v);
  3016. snac.config = xs_dict_set(snac.config, "metadata", new_fields);
  3017. }
  3018. }
  3019. /* we don't have support for the following options, yet
  3020. - discoverable (0/1)
  3021. - locked (0/1)
  3022. */
  3023. }
  3024. /* Persist profile */
  3025. if (user_persist(&snac, 1) == 0)
  3026. credentials_get(body, ctype, &status, snac);
  3027. else
  3028. status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
  3029. }
  3030. else
  3031. status = HTTP_STATUS_UNAUTHORIZED;
  3032. }
  3033. /* user cleanup */
  3034. if (logged_in)
  3035. user_free(&snac);
  3036. srv_debug(1, xs_fmt("mastoapi_patch_handler %s %d", q_path, status));
  3037. return status;
  3038. }
  3039. void mastoapi_purge(void)
  3040. {
  3041. xs *spec = xs_fmt("%s/app/" "*.json", srv_basedir);
  3042. xs *files = xs_glob(spec, 1, 0);
  3043. xs_list *p = files;
  3044. const xs_str *v;
  3045. time_t mt = time(NULL) - 3600;
  3046. while (xs_list_iter(&p, &v)) {
  3047. xs *cid = xs_replace(v, ".json", "");
  3048. xs *fn = _app_fn(cid);
  3049. if (mtime(fn) < mt) {
  3050. /* get the app */
  3051. xs *app = app_get(cid);
  3052. if (app) {
  3053. /* old apps with no uid are incomplete cruft */
  3054. const char *uid = xs_dict_get(app, "uid");
  3055. if (xs_is_null(uid) || *uid == '\0') {
  3056. unlink(fn);
  3057. srv_debug(2, xs_fmt("purged %s", fn));
  3058. }
  3059. }
  3060. }
  3061. }
  3062. }
  3063. #endif /* #ifndef NO_MASTODON_API */