mastoapi.c 121 KB

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