mastoapi.c 150 KB

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