mastoapi.c 112 KB

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