mastoapi.c 129 KB

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