mastoapi.c 113 KB

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