mastoapi.c 112 KB

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