mastoapi.c 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363
  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->actor, 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. acct = xs_dict_append(acct, "id", snac.md5);
  929. acct = xs_dict_append(acct, "username", xs_dict_get(snac.config, "uid"));
  930. acct = xs_dict_append(acct, "acct", xs_dict_get(snac.config, "uid"));
  931. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac.config, "name"));
  932. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac.config, "published"));
  933. acct = xs_dict_append(acct, "last_status_at", xs_dict_get(snac.config, "published"));
  934. acct = xs_dict_append(acct, "note", xs_dict_get(snac.config, "bio"));
  935. acct = xs_dict_append(acct, "url", snac.actor);
  936. acct = xs_dict_append(acct, "locked", xs_stock(XSTYPE_FALSE));
  937. acct = xs_dict_append(acct, "bot", xs_dict_get(snac.config, "bot"));
  938. acct = xs_dict_append(acct, "emojis", xs_stock(XSTYPE_LIST));
  939. xs *src = xs_json_loads("{\"privacy\":\"public\", \"language\":\"en\","
  940. "\"follow_requests_count\": 0,"
  941. "\"sensitive\":false,\"fields\":[],\"note\":\"\"}");
  942. /* some apps take the note from the source object */
  943. src = xs_dict_set(src, "note", xs_dict_get(snac.config, "bio"));
  944. src = xs_dict_set(src, "privacy", xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE ? "private" : "public");
  945. const xs_str *cw = xs_dict_get(snac.config, "cw");
  946. src = xs_dict_set(src, "sensitive",
  947. strcmp(cw, "open") == 0 ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  948. src = xs_dict_set(src, "bot", xs_dict_get(snac.config, "bot"));
  949. xs *avatar = NULL;
  950. const char *av = xs_dict_get(snac.config, "avatar");
  951. if (xs_is_null(av) || *av == '\0')
  952. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  953. else
  954. avatar = xs_dup(av);
  955. acct = xs_dict_append(acct, "avatar", avatar);
  956. acct = xs_dict_append(acct, "avatar_static", avatar);
  957. xs *header = NULL;
  958. const char *hd = xs_dict_get(snac.config, "header");
  959. if (!xs_is_null(hd))
  960. header = xs_dup(hd);
  961. else
  962. header = xs_str_new(NULL);
  963. acct = xs_dict_append(acct, "header", header);
  964. acct = xs_dict_append(acct, "header_static", header);
  965. const xs_dict *metadata = xs_dict_get(snac.config, "metadata");
  966. if (xs_type(metadata) == XSTYPE_DICT) {
  967. xs *fields = xs_list_new();
  968. const xs_str *k;
  969. const xs_str *v;
  970. xs_dict *val_links = snac.links;
  971. if (xs_is_null(val_links))
  972. val_links = xs_stock(XSTYPE_DICT);
  973. int c = 0;
  974. while (xs_dict_next(metadata, &k, &v, &c)) {
  975. xs *val_date = NULL;
  976. const xs_number *verified_time = xs_dict_get(val_links, v);
  977. if (xs_type(verified_time) == XSTYPE_NUMBER) {
  978. time_t t = xs_number_get(verified_time);
  979. if (t > 0)
  980. val_date = xs_str_utctime(t, ISO_DATE_SPEC);
  981. }
  982. xs *d = xs_dict_new();
  983. d = xs_dict_append(d, "name", k);
  984. d = xs_dict_append(d, "value", v);
  985. d = xs_dict_append(d, "verified_at",
  986. xs_type(val_date) == XSTYPE_STRING && *val_date ? val_date : xs_stock(XSTYPE_NULL));
  987. fields = xs_list_append(fields, d);
  988. }
  989. acct = xs_dict_set(acct, "fields", fields);
  990. /* some apps take the fields from the source object */
  991. src = xs_dict_set(src, "fields", fields);
  992. }
  993. acct = xs_dict_append(acct, "source", src);
  994. acct = xs_dict_append(acct, "followers_count", xs_stock(0));
  995. acct = xs_dict_append(acct, "following_count", xs_stock(0));
  996. acct = xs_dict_append(acct, "statuses_count", xs_stock(0));
  997. *body = xs_json_dumps(acct, 4);
  998. *ctype = "application/json";
  999. *status = HTTP_STATUS_OK;
  1000. }
  1001. xs_list *mastoapi_timeline(snac *user, const xs_dict *args, const char *index_fn)
  1002. {
  1003. xs_list *out = xs_list_new();
  1004. FILE *f;
  1005. char md5[MD5_HEX_SIZE];
  1006. if (dbglevel) {
  1007. xs *js = xs_json_dumps(args, 0);
  1008. srv_debug(1, xs_fmt("mastoapi_timeline args %s", js));
  1009. }
  1010. if ((f = fopen(index_fn, "r")) == NULL)
  1011. return out;
  1012. const char *max_id = xs_dict_get(args, "max_id");
  1013. const char *since_id = xs_dict_get(args, "since_id");
  1014. const char *min_id = xs_dict_get(args, "min_id");
  1015. const char *limit_s = xs_dict_get(args, "limit");
  1016. int limit = 0;
  1017. int cnt = 0;
  1018. if (!xs_is_null(limit_s))
  1019. limit = atoi(limit_s);
  1020. if (limit == 0)
  1021. limit = 20;
  1022. if (index_desc_first(f, md5, 0)) {
  1023. do {
  1024. xs *msg = NULL;
  1025. /* only return entries older that max_id */
  1026. if (max_id) {
  1027. if (strcmp(md5, MID_TO_MD5(max_id)) == 0)
  1028. max_id = NULL;
  1029. continue;
  1030. }
  1031. /* only returns entries newer than since_id */
  1032. if (since_id) {
  1033. if (strcmp(md5, MID_TO_MD5(since_id)) == 0)
  1034. break;
  1035. }
  1036. /* only returns entries newer than min_id */
  1037. /* what does really "Return results immediately newer than ID" mean? */
  1038. if (min_id) {
  1039. if (strcmp(md5, MID_TO_MD5(min_id)) == 0)
  1040. break;
  1041. }
  1042. /* get the entry */
  1043. if (user) {
  1044. if (!valid_status(timeline_get_by_md5(user, md5, &msg)))
  1045. continue;
  1046. }
  1047. else {
  1048. if (!valid_status(object_get_by_md5(md5, &msg)))
  1049. continue;
  1050. }
  1051. /* discard non-Notes */
  1052. const char *id = xs_dict_get(msg, "id");
  1053. const char *type = xs_dict_get(msg, "type");
  1054. if (!xs_match(type, POSTLIKE_OBJECT_TYPE))
  1055. continue;
  1056. if (id && is_instance_blocked(id))
  1057. continue;
  1058. const char *from = NULL;
  1059. if (strcmp(type, "Page") == 0)
  1060. from = xs_dict_get(msg, "audience");
  1061. if (from == NULL)
  1062. from = get_atto(msg);
  1063. if (from == NULL)
  1064. continue;
  1065. if (user) {
  1066. /* is this message from a person we don't follow? */
  1067. if (strcmp(from, user->actor) && !following_check(user, from)) {
  1068. /* discard if it was not boosted */
  1069. xs *idx = object_announces(id);
  1070. if (xs_list_len(idx) == 0)
  1071. continue;
  1072. }
  1073. /* discard notes from muted morons */
  1074. if (is_muted(user, from))
  1075. continue;
  1076. /* discard hidden notes */
  1077. if (is_hidden(user, id))
  1078. continue;
  1079. }
  1080. else {
  1081. /* skip non-public messages */
  1082. if (!is_msg_public(msg))
  1083. continue;
  1084. /* discard messages from private users */
  1085. if (is_msg_from_private_user(msg))
  1086. continue;
  1087. }
  1088. /* if it has a name and it's not a Page or a Video,
  1089. it's a poll vote, so discard it */
  1090. if (!xs_is_null(xs_dict_get(msg, "name")) && !xs_match(type, "Page|Video"))
  1091. continue;
  1092. /* convert the Note into a Mastodon status */
  1093. xs *st = mastoapi_status(user, msg);
  1094. if (st != NULL) {
  1095. out = xs_list_append(out, st);
  1096. cnt++;
  1097. }
  1098. } while (cnt < limit && index_desc_next(f, md5));
  1099. }
  1100. int more = index_desc_next(f, md5);
  1101. fclose(f);
  1102. srv_debug(1, xs_fmt("mastoapi_timeline ret %d%s", cnt, more ? " (+)" : ""));
  1103. return out;
  1104. }
  1105. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  1106. char **body, int *b_size, char **ctype)
  1107. {
  1108. (void)b_size;
  1109. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1110. return 0;
  1111. int status = HTTP_STATUS_NOT_FOUND;
  1112. const xs_dict *args = xs_dict_get(req, "q_vars");
  1113. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1114. snac snac1 = {0};
  1115. int logged_in = process_auth_token(&snac1, req);
  1116. if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) { /** **/
  1117. if (logged_in) {
  1118. credentials_get(body, ctype, &status, snac1);
  1119. }
  1120. else {
  1121. status = HTTP_STATUS_UNPROCESSABLE_CONTENT; // (no login)
  1122. }
  1123. }
  1124. else
  1125. if (strcmp(cmd, "/v1/accounts/relationships") == 0) { /** **/
  1126. /* find if an account is followed, blocked, etc. */
  1127. /* the account to get relationships about is in args "id[]" */
  1128. if (logged_in) {
  1129. xs *res = xs_list_new();
  1130. const char *md5 = xs_dict_get(args, "id[]");
  1131. if (xs_is_null(md5))
  1132. md5 = xs_dict_get(args, "id");
  1133. if (!xs_is_null(md5)) {
  1134. if (xs_type(md5) == XSTYPE_LIST)
  1135. md5 = xs_list_get(md5, 0);
  1136. xs *rel = mastoapi_relationship(&snac1, md5);
  1137. if (rel != NULL)
  1138. res = xs_list_append(res, rel);
  1139. }
  1140. *body = xs_json_dumps(res, 4);
  1141. *ctype = "application/json";
  1142. status = HTTP_STATUS_OK;
  1143. }
  1144. else
  1145. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  1146. }
  1147. else
  1148. if (strcmp(cmd, "/v1/accounts/lookup") == 0) { /** **/
  1149. /* lookup an account */
  1150. const char *acct = xs_dict_get(args, "acct");
  1151. if (!xs_is_null(acct)) {
  1152. xs *s = xs_strip_chars_i(xs_dup(acct), "@");
  1153. xs *l = xs_split_n(s, "@", 1);
  1154. const char *uid = xs_list_get(l, 0);
  1155. const char *host = xs_list_get(l, 1);
  1156. if (uid && (!host || strcmp(host, xs_dict_get(srv_config, "host")) == 0)) {
  1157. snac user;
  1158. if (user_open(&user, uid)) {
  1159. xs *actor = msg_actor(&user);
  1160. xs *macct = mastoapi_account(NULL, actor);
  1161. *body = xs_json_dumps(macct, 4);
  1162. *ctype = "application/json";
  1163. status = HTTP_STATUS_OK;
  1164. user_free(&user);
  1165. }
  1166. }
  1167. }
  1168. }
  1169. else
  1170. if (xs_startswith(cmd, "/v1/accounts/")) { /** **/
  1171. /* account-related information */
  1172. xs *l = xs_split(cmd, "/");
  1173. const char *uid = xs_list_get(l, 3);
  1174. const char *opt = xs_list_get(l, 4);
  1175. if (uid != NULL) {
  1176. snac snac2;
  1177. xs *out = NULL;
  1178. xs *actor = NULL;
  1179. if (logged_in && strcmp(uid, "search") == 0) { /** **/
  1180. /* search for accounts starting with q */
  1181. const char *aq = xs_dict_get(args, "q");
  1182. if (!xs_is_null(aq)) {
  1183. xs *q = xs_tolower_i(xs_dup(aq));
  1184. out = xs_list_new();
  1185. xs *wing = following_list(&snac1);
  1186. xs *wers = follower_list(&snac1);
  1187. xs *ulst = user_list();
  1188. xs_list *p;
  1189. const xs_str *v;
  1190. xs_set seen;
  1191. xs_set_init(&seen);
  1192. /* user relations */
  1193. xs_list *lsts[] = { wing, wers, NULL };
  1194. int n;
  1195. for (n = 0; (p = lsts[n]) != NULL; n++) {
  1196. while (xs_list_iter(&p, &v)) {
  1197. /* already seen? skip */
  1198. if (xs_set_add(&seen, v) == 0)
  1199. continue;
  1200. xs *actor = NULL;
  1201. if (valid_status(object_get(v, &actor))) {
  1202. const char *uname = xs_dict_get(actor, "preferredUsername");
  1203. if (!xs_is_null(uname)) {
  1204. xs *luname = xs_tolower_i(xs_dup(uname));
  1205. if (xs_startswith(luname, q)) {
  1206. xs *acct = mastoapi_account(&snac1, actor);
  1207. out = xs_list_append(out, acct);
  1208. }
  1209. }
  1210. }
  1211. }
  1212. }
  1213. /* local users */
  1214. p = ulst;
  1215. while (xs_list_iter(&p, &v)) {
  1216. snac user;
  1217. /* skip this same user */
  1218. if (strcmp(v, xs_dict_get(snac1.config, "uid")) == 0)
  1219. continue;
  1220. /* skip if the uid does not start with the query */
  1221. xs *v2 = xs_tolower_i(xs_dup(v));
  1222. if (!xs_startswith(v2, q))
  1223. continue;
  1224. if (user_open(&user, v)) {
  1225. /* if it's not already seen, add it */
  1226. if (xs_set_add(&seen, user.actor) == 1) {
  1227. xs *actor = msg_actor(&user);
  1228. xs *acct = mastoapi_account(&snac1, actor);
  1229. out = xs_list_append(out, acct);
  1230. }
  1231. user_free(&user);
  1232. }
  1233. }
  1234. xs_set_free(&seen);
  1235. }
  1236. }
  1237. else
  1238. /* is it a local user? */
  1239. if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
  1240. if (opt == NULL) {
  1241. /* account information */
  1242. actor = msg_actor(&snac2);
  1243. out = mastoapi_account(NULL, actor);
  1244. }
  1245. else
  1246. if (strcmp(opt, "statuses") == 0) { /** **/
  1247. /* the public list of posts of a user */
  1248. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  1249. xs_list *p = timeline;
  1250. const xs_str *v;
  1251. out = xs_list_new();
  1252. while (xs_list_iter(&p, &v)) {
  1253. xs *msg = NULL;
  1254. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  1255. /* add only posts by the author */
  1256. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  1257. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  1258. xs *st = mastoapi_status(&snac2, msg);
  1259. if (st)
  1260. out = xs_list_append(out, st);
  1261. }
  1262. }
  1263. }
  1264. }
  1265. else
  1266. if (strcmp(opt, "featured_tags") == 0) {
  1267. /* snac doesn't have features tags, yet? */
  1268. /* implement empty response so apps like Tokodon don't show an error */
  1269. out = xs_list_new();
  1270. }
  1271. else
  1272. if (strcmp(opt, "following") == 0) {
  1273. xs *wing = following_list(&snac1);
  1274. out = xs_list_new();
  1275. int c = 0;
  1276. const char *v;
  1277. while (xs_list_next(wing, &v, &c)) {
  1278. xs *actor = NULL;
  1279. if (valid_status(object_get(v, &actor))) {
  1280. xs *acct = mastoapi_account(NULL, actor);
  1281. out = xs_list_append(out, acct);
  1282. }
  1283. }
  1284. }
  1285. else
  1286. if (strcmp(opt, "followers") == 0) {
  1287. out = xs_list_new();
  1288. }
  1289. user_free(&snac2);
  1290. }
  1291. else {
  1292. /* try the uid as the md5 of a possibly loaded actor */
  1293. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  1294. if (opt == NULL) {
  1295. /* account information */
  1296. out = mastoapi_account(&snac1, actor);
  1297. }
  1298. else
  1299. if (strcmp(opt, "statuses") == 0) {
  1300. /* we don't serve statuses of others; return the empty list */
  1301. out = xs_list_new();
  1302. }
  1303. else
  1304. if (strcmp(opt, "featured_tags") == 0) {
  1305. /* snac doesn't have features tags, yet? */
  1306. /* implement empty response so apps like Tokodon don't show an error */
  1307. out = xs_list_new();
  1308. }
  1309. }
  1310. }
  1311. if (out != NULL) {
  1312. *body = xs_json_dumps(out, 4);
  1313. *ctype = "application/json";
  1314. status = HTTP_STATUS_OK;
  1315. }
  1316. }
  1317. }
  1318. else
  1319. if (strcmp(cmd, "/v1/timelines/home") == 0) { /** **/
  1320. /* the private timeline */
  1321. if (logged_in) {
  1322. xs *ifn = user_index_fn(&snac1, "private");
  1323. xs *out = mastoapi_timeline(&snac1, args, ifn);
  1324. *body = xs_json_dumps(out, 4);
  1325. *ctype = "application/json";
  1326. status = HTTP_STATUS_OK;
  1327. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  1328. }
  1329. else {
  1330. status = HTTP_STATUS_UNAUTHORIZED;
  1331. }
  1332. }
  1333. else
  1334. if (strcmp(cmd, "/v1/timelines/public") == 0) { /** **/
  1335. /* the instance public timeline (public timelines for all users) */
  1336. xs *ifn = instance_index_fn();
  1337. xs *out = mastoapi_timeline(NULL, args, ifn);
  1338. *body = xs_json_dumps(out, 4);
  1339. *ctype = "application/json";
  1340. status = HTTP_STATUS_OK;
  1341. }
  1342. else
  1343. if (xs_startswith(cmd, "/v1/timelines/tag/")) { /** **/
  1344. /* get the tag */
  1345. xs *l = xs_split(cmd, "/");
  1346. const char *tag = xs_list_get(l, -1);
  1347. xs *ifn = tag_fn(tag);
  1348. xs *out = mastoapi_timeline(NULL, args, ifn);
  1349. *body = xs_json_dumps(out, 4);
  1350. *ctype = "application/json";
  1351. status = HTTP_STATUS_OK;
  1352. }
  1353. else
  1354. if (xs_startswith(cmd, "/v1/timelines/list/")) { /** **/
  1355. /* get the list id */
  1356. if (logged_in) {
  1357. xs *l = xs_split(cmd, "/");
  1358. const char *list = xs_list_get(l, -1);
  1359. xs *ifn = list_timeline_fn(&snac1, list);
  1360. xs *out = mastoapi_timeline(NULL, args, ifn);
  1361. *body = xs_json_dumps(out, 4);
  1362. *ctype = "application/json";
  1363. status = HTTP_STATUS_OK;
  1364. }
  1365. else
  1366. status = HTTP_STATUS_MISDIRECTED_REQUEST;
  1367. }
  1368. else
  1369. if (strcmp(cmd, "/v1/conversations") == 0) { /** **/
  1370. /* TBD */
  1371. *body = xs_dup("[]");
  1372. *ctype = "application/json";
  1373. status = HTTP_STATUS_OK;
  1374. }
  1375. else
  1376. if (strcmp(cmd, "/v1/notifications") == 0) { /** **/
  1377. if (logged_in) {
  1378. xs *l = notify_list(&snac1, 0, 64);
  1379. xs *out = xs_list_new();
  1380. const xs_dict *v;
  1381. const xs_list *excl = xs_dict_get(args, "exclude_types[]");
  1382. const char *max_id = xs_dict_get(args, "max_id");
  1383. xs_list_foreach(l, v) {
  1384. xs *noti = notify_get(&snac1, v);
  1385. if (noti == NULL)
  1386. continue;
  1387. const char *type = xs_dict_get(noti, "type");
  1388. const char *utype = xs_dict_get(noti, "utype");
  1389. const char *objid = xs_dict_get(noti, "objid");
  1390. const char *id = xs_dict_get(noti, "id");
  1391. xs *fid = xs_replace(id, ".", "");
  1392. xs *actor = NULL;
  1393. xs *entry = NULL;
  1394. if (!valid_status(actor_get(xs_dict_get(noti, "actor"), &actor)))
  1395. continue;
  1396. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  1397. continue;
  1398. if (is_hidden(&snac1, objid))
  1399. continue;
  1400. if (max_id) {
  1401. if (strcmp(fid, max_id) == 0)
  1402. max_id = NULL;
  1403. continue;
  1404. }
  1405. /* convert the type */
  1406. if (strcmp(type, "Like") == 0 || strcmp(type, "EmojiReact") == 0)
  1407. type = "favourite";
  1408. else
  1409. if (strcmp(type, "Announce") == 0)
  1410. type = "reblog";
  1411. else
  1412. if (strcmp(type, "Follow") == 0)
  1413. type = "follow";
  1414. else
  1415. if (strcmp(type, "Create") == 0)
  1416. type = "mention";
  1417. else
  1418. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  1419. type = "poll";
  1420. else
  1421. continue;
  1422. /* excluded type? */
  1423. if (!xs_is_null(excl) && xs_list_in(excl, type) != -1)
  1424. continue;
  1425. xs *mn = xs_dict_new();
  1426. mn = xs_dict_append(mn, "type", type);
  1427. mn = xs_dict_append(mn, "id", fid);
  1428. mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
  1429. xs *acct = mastoapi_account(&snac1, actor);
  1430. if (acct == NULL)
  1431. continue;
  1432. mn = xs_dict_append(mn, "account", acct);
  1433. if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
  1434. xs *st = mastoapi_status(&snac1, entry);
  1435. if (st)
  1436. mn = xs_dict_append(mn, "status", st);
  1437. }
  1438. out = xs_list_append(out, mn);
  1439. }
  1440. *body = xs_json_dumps(out, 4);
  1441. *ctype = "application/json";
  1442. status = HTTP_STATUS_OK;
  1443. }
  1444. else
  1445. status = HTTP_STATUS_UNAUTHORIZED;
  1446. }
  1447. else
  1448. if (strcmp(cmd, "/v1/filters") == 0) { /** **/
  1449. /* snac will never have filters */
  1450. *body = xs_dup("[]");
  1451. *ctype = "application/json";
  1452. status = HTTP_STATUS_OK;
  1453. }
  1454. else
  1455. if (strcmp(cmd, "/v2/filters") == 0) { /** **/
  1456. /* snac will never have filters
  1457. * but still, without a v2 endpoint a short delay is introduced
  1458. * in some apps */
  1459. *body = xs_dup("[]");
  1460. *ctype = "application/json";
  1461. status = HTTP_STATUS_OK;
  1462. }
  1463. else
  1464. if (strcmp(cmd, "/v1/favourites") == 0) { /** **/
  1465. /* snac will never support a list of favourites */
  1466. *body = xs_dup("[]");
  1467. *ctype = "application/json";
  1468. status = HTTP_STATUS_OK;
  1469. }
  1470. else
  1471. if (strcmp(cmd, "/v1/bookmarks") == 0) { /** **/
  1472. if (logged_in) {
  1473. xs *ifn = bookmark_index_fn(&snac1);
  1474. xs *out = mastoapi_timeline(&snac1, args, ifn);
  1475. *body = xs_json_dumps(out, 4);
  1476. *ctype = "application/json";
  1477. status = HTTP_STATUS_OK;
  1478. }
  1479. else
  1480. status = HTTP_STATUS_UNAUTHORIZED;
  1481. }
  1482. else
  1483. if (strcmp(cmd, "/v1/lists") == 0) { /** list of lists **/
  1484. if (logged_in) {
  1485. xs *lol = list_maint(&snac1, NULL, 0);
  1486. xs *l = xs_list_new();
  1487. int c = 0;
  1488. const xs_list *li;
  1489. while (xs_list_next(lol, &li, &c)) {
  1490. xs *d = xs_dict_new();
  1491. d = xs_dict_append(d, "id", xs_list_get(li, 0));
  1492. d = xs_dict_append(d, "title", xs_list_get(li, 1));
  1493. d = xs_dict_append(d, "replies_policy", "list");
  1494. d = xs_dict_append(d, "exclusive", xs_stock(XSTYPE_FALSE));
  1495. l = xs_list_append(l, d);
  1496. }
  1497. *body = xs_json_dumps(l, 4);
  1498. *ctype = "application/json";
  1499. status = HTTP_STATUS_OK;
  1500. }
  1501. else
  1502. status = HTTP_STATUS_UNAUTHORIZED;
  1503. }
  1504. else
  1505. if (xs_startswith(cmd, "/v1/lists/")) { /** list information **/
  1506. if (logged_in) {
  1507. xs *l = xs_split(cmd, "/");
  1508. const char *p = xs_list_get(l, -1);
  1509. if (p) {
  1510. if (strcmp(p, "accounts") == 0) {
  1511. p = xs_list_get(l, -2);
  1512. if (p && xs_is_hex(p)) {
  1513. xs *actors = list_content(&snac1, p, NULL, 0);
  1514. xs *out = xs_list_new();
  1515. int c = 0;
  1516. const char *v;
  1517. while (xs_list_next(actors, &v, &c)) {
  1518. xs *actor = NULL;
  1519. if (valid_status(object_get_by_md5(v, &actor))) {
  1520. xs *acct = mastoapi_account(&snac1, actor);
  1521. out = xs_list_append(out, acct);
  1522. }
  1523. }
  1524. *body = xs_json_dumps(out, 4);
  1525. *ctype = "application/json";
  1526. status = HTTP_STATUS_OK;
  1527. }
  1528. }
  1529. else
  1530. if (xs_is_hex(p)) {
  1531. xs *out = xs_list_new();
  1532. xs *lol = list_maint(&snac1, NULL, 0);
  1533. int c = 0;
  1534. const xs_list *v;
  1535. while (xs_list_next(lol, &v, &c)) {
  1536. const char *id = xs_list_get(v, 0);
  1537. if (id && strcmp(id, p) == 0) {
  1538. xs *d = xs_dict_new();
  1539. d = xs_dict_append(d, "id", p);
  1540. d = xs_dict_append(d, "title", xs_list_get(v, 1));
  1541. d = xs_dict_append(d, "replies_policy", "list");
  1542. d = xs_dict_append(d, "exclusive", xs_stock(XSTYPE_FALSE));
  1543. out = xs_dup(d);
  1544. break;
  1545. }
  1546. }
  1547. *body = xs_json_dumps(out, 4);
  1548. *ctype = "application/json";
  1549. status = HTTP_STATUS_OK;
  1550. }
  1551. }
  1552. }
  1553. else
  1554. status = HTTP_STATUS_UNAUTHORIZED;
  1555. }
  1556. else
  1557. if (strcmp(cmd, "/v1/scheduled_statuses") == 0) { /** **/
  1558. /* snac does not schedule notes */
  1559. *body = xs_dup("[]");
  1560. *ctype = "application/json";
  1561. status = HTTP_STATUS_OK;
  1562. }
  1563. else
  1564. if (strcmp(cmd, "/v1/follow_requests") == 0) { /** **/
  1565. /* snac does not support optional follow confirmations */
  1566. *body = xs_dup("[]");
  1567. *ctype = "application/json";
  1568. status = HTTP_STATUS_OK;
  1569. }
  1570. else
  1571. if (strcmp(cmd, "/v1/announcements") == 0) { /** **/
  1572. if (logged_in) {
  1573. xs *resp = xs_list_new();
  1574. double la = 0.0;
  1575. xs *user_la = xs_dup(xs_dict_get(snac1.config, "last_announcement"));
  1576. if (user_la != NULL)
  1577. la = xs_number_get(user_la);
  1578. xs *val_date = xs_str_utctime(la, ISO_DATE_SPEC);
  1579. /* contrary to html, we always send the announcement and set the read flag instead */
  1580. const t_announcement *annce = announcement(la);
  1581. if (annce != NULL && annce->text != NULL) {
  1582. xs *an = xs_dict_new();
  1583. xs *id = xs_fmt("%d", annce->timestamp);
  1584. xs *ct = xs_fmt("<p>%s</p>", annce->text);
  1585. an = xs_dict_set(an, "id", id);
  1586. an = xs_dict_set(an, "content", ct);
  1587. an = xs_dict_set(an, "starts_at", xs_stock(XSTYPE_NULL));
  1588. an = xs_dict_set(an, "ends_at", xs_stock(XSTYPE_NULL));
  1589. an = xs_dict_set(an, "all_day", xs_stock(XSTYPE_TRUE));
  1590. an = xs_dict_set(an, "published_at", val_date);
  1591. an = xs_dict_set(an, "updated_at", val_date);
  1592. an = xs_dict_set(an, "read", (annce->timestamp >= la)
  1593. ? xs_stock(XSTYPE_FALSE) : xs_stock(XSTYPE_TRUE));
  1594. an = xs_dict_set(an, "mentions", xs_stock(XSTYPE_LIST));
  1595. an = xs_dict_set(an, "statuses", xs_stock(XSTYPE_LIST));
  1596. an = xs_dict_set(an, "tags", xs_stock(XSTYPE_LIST));
  1597. an = xs_dict_set(an, "emojis", xs_stock(XSTYPE_LIST));
  1598. an = xs_dict_set(an, "reactions", xs_stock(XSTYPE_LIST));
  1599. resp = xs_list_append(resp, an);
  1600. }
  1601. *body = xs_json_dumps(resp, 4);
  1602. *ctype = "application/json";
  1603. status = HTTP_STATUS_OK;
  1604. }
  1605. }
  1606. else
  1607. if (strcmp(cmd, "/v1/custom_emojis") == 0) { /** **/
  1608. /* are you kidding me? */
  1609. *body = xs_dup("[]");
  1610. *ctype = "application/json";
  1611. status = HTTP_STATUS_OK;
  1612. }
  1613. else
  1614. if (strcmp(cmd, "/v1/instance") == 0) { /** **/
  1615. /* returns an instance object */
  1616. xs *ins = xs_dict_new();
  1617. const char *host = xs_dict_get(srv_config, "host");
  1618. const char *title = xs_dict_get(srv_config, "title");
  1619. const char *sdesc = xs_dict_get(srv_config, "short_description");
  1620. ins = xs_dict_append(ins, "uri", host);
  1621. ins = xs_dict_append(ins, "domain", host);
  1622. ins = xs_dict_append(ins, "title", title && *title ? title : host);
  1623. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  1624. ins = xs_dict_append(ins, "source_url", WHAT_IS_SNAC_URL);
  1625. ins = xs_dict_append(ins, "description", host);
  1626. ins = xs_dict_append(ins, "short_description", sdesc && *sdesc ? sdesc : host);
  1627. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  1628. ins = xs_dict_append(ins, "thumbnail", susie);
  1629. const char *v = xs_dict_get(srv_config, "admin_email");
  1630. if (xs_is_null(v) || *v == '\0')
  1631. v = "admin@localhost";
  1632. ins = xs_dict_append(ins, "email", v);
  1633. ins = xs_dict_append(ins, "rules", xs_stock(XSTYPE_LIST));
  1634. xs *l1 = xs_list_append(xs_list_new(), "en");
  1635. ins = xs_dict_append(ins, "languages", l1);
  1636. xs *wss = xs_fmt("wss:/" "/%s", xs_dict_get(srv_config, "host"));
  1637. xs *urls = xs_dict_new();
  1638. urls = xs_dict_append(urls, "streaming_api", wss);
  1639. ins = xs_dict_append(ins, "urls", urls);
  1640. xs *d2 = xs_dict_append(xs_dict_new(), "user_count", xs_stock(0));
  1641. d2 = xs_dict_append(d2, "status_count", xs_stock(0));
  1642. d2 = xs_dict_append(d2, "domain_count", xs_stock(0));
  1643. ins = xs_dict_append(ins, "stats", d2);
  1644. ins = xs_dict_append(ins, "registrations", xs_stock(XSTYPE_FALSE));
  1645. ins = xs_dict_append(ins, "approval_required", xs_stock(XSTYPE_FALSE));
  1646. ins = xs_dict_append(ins, "invites_enabled", xs_stock(XSTYPE_FALSE));
  1647. xs *cfg = xs_dict_new();
  1648. {
  1649. xs *d11 = xs_json_loads("{\"characters_reserved_per_url\":32,"
  1650. "\"max_characters\":100000,\"max_media_attachments\":8}");
  1651. cfg = xs_dict_append(cfg, "statuses", d11);
  1652. xs *d12 = xs_json_loads("{\"max_featured_tags\":0}");
  1653. cfg = xs_dict_append(cfg, "accounts", d12);
  1654. xs *d13 = xs_json_loads("{\"image_matrix_limit\":33177600,"
  1655. "\"image_size_limit\":16777216,"
  1656. "\"video_frame_rate_limit\":120,"
  1657. "\"video_matrix_limit\":8294400,"
  1658. "\"video_size_limit\":103809024}"
  1659. );
  1660. {
  1661. /* get the supported mime types from the internal list */
  1662. const char **p = xs_mime_types;
  1663. xs_set mtypes;
  1664. xs_set_init(&mtypes);
  1665. while (*p) {
  1666. const char *type = p[1];
  1667. if (xs_startswith(type, "image/") ||
  1668. xs_startswith(type, "video/") ||
  1669. xs_startswith(type, "audio/"))
  1670. xs_set_add(&mtypes, type);
  1671. p += 2;
  1672. }
  1673. xs *l = xs_set_result(&mtypes);
  1674. d13 = xs_dict_append(d13, "supported_mime_types", l);
  1675. }
  1676. cfg = xs_dict_append(cfg, "media_attachments", d13);
  1677. xs *d14 = xs_json_loads("{\"max_characters_per_option\":50,"
  1678. "\"max_expiration\":2629746,"
  1679. "\"max_options\":8,\"min_expiration\":300}");
  1680. cfg = xs_dict_append(cfg, "polls", d14);
  1681. }
  1682. ins = xs_dict_append(ins, "configuration", cfg);
  1683. const char *admin_account = xs_dict_get(srv_config, "admin_account");
  1684. if (!xs_is_null(admin_account) && *admin_account) {
  1685. snac admin;
  1686. if (user_open(&admin, admin_account)) {
  1687. xs *actor = msg_actor(&admin);
  1688. xs *acct = mastoapi_account(NULL, actor);
  1689. ins = xs_dict_append(ins, "contact_account", acct);
  1690. user_free(&admin);
  1691. }
  1692. }
  1693. *body = xs_json_dumps(ins, 4);
  1694. *ctype = "application/json";
  1695. status = HTTP_STATUS_OK;
  1696. }
  1697. else
  1698. if (xs_startswith(cmd, "/v1/statuses/")) { /** **/
  1699. /* information about a status */
  1700. if (logged_in) {
  1701. xs *l = xs_split(cmd, "/");
  1702. const char *id = xs_list_get(l, 3);
  1703. const char *op = xs_list_get(l, 4);
  1704. if (!xs_is_null(id)) {
  1705. xs *msg = NULL;
  1706. xs *out = NULL;
  1707. /* skip the 'fake' part of the id */
  1708. id = MID_TO_MD5(id);
  1709. if (valid_status(object_get_by_md5(id, &msg))) {
  1710. if (op == NULL) {
  1711. if (!is_muted(&snac1, get_atto(msg))) {
  1712. /* return the status itself */
  1713. out = mastoapi_status(&snac1, msg);
  1714. }
  1715. }
  1716. else
  1717. if (strcmp(op, "context") == 0) { /** **/
  1718. /* return ancestors and children */
  1719. xs *anc = xs_list_new();
  1720. xs *des = xs_list_new();
  1721. xs_list *p;
  1722. const xs_str *v;
  1723. char pid[MD5_HEX_SIZE];
  1724. /* build the [grand]parent list, moving up */
  1725. strncpy(pid, id, sizeof(pid));
  1726. while (object_parent(pid, pid)) {
  1727. xs *m2 = NULL;
  1728. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  1729. xs *st = mastoapi_status(&snac1, m2);
  1730. if (st)
  1731. anc = xs_list_insert(anc, 0, st);
  1732. }
  1733. else
  1734. break;
  1735. }
  1736. /* build the children list */
  1737. xs *children = object_children(xs_dict_get(msg, "id"));
  1738. p = children;
  1739. while (xs_list_iter(&p, &v)) {
  1740. xs *m2 = NULL;
  1741. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  1742. if (xs_is_null(xs_dict_get(m2, "name"))) {
  1743. xs *st = mastoapi_status(&snac1, m2);
  1744. if (st)
  1745. des = xs_list_append(des, st);
  1746. }
  1747. }
  1748. }
  1749. out = xs_dict_new();
  1750. out = xs_dict_append(out, "ancestors", anc);
  1751. out = xs_dict_append(out, "descendants", des);
  1752. }
  1753. else
  1754. if (strcmp(op, "reblogged_by") == 0 || /** **/
  1755. strcmp(op, "favourited_by") == 0) { /** **/
  1756. /* return the list of people who liked or boosted this */
  1757. out = xs_list_new();
  1758. xs *l = NULL;
  1759. if (op[0] == 'r')
  1760. l = object_announces(xs_dict_get(msg, "id"));
  1761. else
  1762. l = object_likes(xs_dict_get(msg, "id"));
  1763. xs_list *p = l;
  1764. const xs_str *v;
  1765. while (xs_list_iter(&p, &v)) {
  1766. xs *actor2 = NULL;
  1767. if (valid_status(object_get_by_md5(v, &actor2))) {
  1768. xs *acct2 = mastoapi_account(&snac1, actor2);
  1769. out = xs_list_append(out, acct2);
  1770. }
  1771. }
  1772. }
  1773. else
  1774. if (strcmp(op, "source") == 0) { /** **/
  1775. out = xs_dict_new();
  1776. /* get the mastoapi status id */
  1777. out = xs_dict_append(out, "id", xs_list_get(l, 3));
  1778. out = xs_dict_append(out, "text", xs_dict_get(msg, "sourceContent"));
  1779. out = xs_dict_append(out, "spoiler_text", xs_dict_get(msg, "summary"));
  1780. }
  1781. }
  1782. else
  1783. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  1784. if (out != NULL) {
  1785. *body = xs_json_dumps(out, 4);
  1786. *ctype = "application/json";
  1787. status = HTTP_STATUS_OK;
  1788. }
  1789. }
  1790. }
  1791. else
  1792. status = HTTP_STATUS_UNAUTHORIZED;
  1793. }
  1794. else
  1795. if (strcmp(cmd, "/v1/preferences") == 0) { /** **/
  1796. *body = xs_dup("{}");
  1797. *ctype = "application/json";
  1798. status = HTTP_STATUS_OK;
  1799. }
  1800. else
  1801. if (strcmp(cmd, "/v1/markers") == 0) { /** **/
  1802. *body = xs_dup("{}");
  1803. *ctype = "application/json";
  1804. status = HTTP_STATUS_OK;
  1805. }
  1806. else
  1807. if (strcmp(cmd, "/v1/followed_tags") == 0) { /** **/
  1808. *body = xs_dup("[]");
  1809. *ctype = "application/json";
  1810. status = HTTP_STATUS_OK;
  1811. }
  1812. else
  1813. if (strcmp(cmd, "/v1/trends/tags") == 0) { /** **/
  1814. *body = xs_dup("[]");
  1815. *ctype = "application/json";
  1816. status = HTTP_STATUS_OK;
  1817. }
  1818. else
  1819. if (strcmp(cmd, "/v1/trends/statuses") == 0) { /** **/
  1820. *body = xs_dup("[]");
  1821. *ctype = "application/json";
  1822. status = HTTP_STATUS_OK;
  1823. }
  1824. else
  1825. if (strcmp(cmd, "/v2/search") == 0) { /** **/
  1826. if (logged_in) {
  1827. const char *q = xs_dict_get(args, "q");
  1828. const char *type = xs_dict_get(args, "type");
  1829. const char *offset = xs_dict_get(args, "offset");
  1830. xs *acl = xs_list_new();
  1831. xs *stl = xs_list_new();
  1832. xs *htl = xs_list_new();
  1833. xs *res = xs_dict_new();
  1834. if (xs_is_null(offset) || strcmp(offset, "0") == 0) {
  1835. /* reply something only for offset 0; otherwise,
  1836. apps like Tusky keep asking again and again */
  1837. if (!xs_is_null(q)) {
  1838. if (xs_is_null(type) || strcmp(type, "accounts") == 0) {
  1839. /* do a webfinger query */
  1840. char *actor = NULL;
  1841. char *user = NULL;
  1842. if (valid_status(webfinger_request(q, &actor, &user)) && actor) {
  1843. xs *actor_o = NULL;
  1844. if (valid_status(actor_request(&snac1, actor, &actor_o))) {
  1845. xs *acct = mastoapi_account(NULL, actor_o);
  1846. acl = xs_list_append(acl, acct);
  1847. if (!object_here(actor))
  1848. object_add(actor, actor_o);
  1849. }
  1850. }
  1851. }
  1852. if (xs_is_null(type) || strcmp(type, "hashtags") == 0) {
  1853. /* search this tag */
  1854. xs *tl = tag_search((char *)q, 0, 1);
  1855. if (xs_list_len(tl)) {
  1856. xs *d = xs_dict_new();
  1857. d = xs_dict_append(d, "name", q);
  1858. xs *url = xs_fmt("%s?t=%s", srv_baseurl, q);
  1859. d = xs_dict_append(d, "url", url);
  1860. d = xs_dict_append(d, "history", xs_stock(XSTYPE_LIST));
  1861. htl = xs_list_append(htl, d);
  1862. }
  1863. }
  1864. if (xs_is_null(type) || strcmp(type, "statuses") == 0) {
  1865. int to = 0;
  1866. int cnt = 40;
  1867. xs *tl = content_search(&snac1, q, 1, 0, cnt, 0, &to);
  1868. int c = 0;
  1869. const char *v;
  1870. while (xs_list_next(tl, &v, &c) && --cnt) {
  1871. xs *post = NULL;
  1872. if (!valid_status(timeline_get_by_md5(&snac1, v, &post)))
  1873. continue;
  1874. xs *s = mastoapi_status(&snac1, post);
  1875. if (!xs_is_null(s))
  1876. stl = xs_list_append(stl, s);
  1877. }
  1878. }
  1879. }
  1880. }
  1881. res = xs_dict_append(res, "accounts", acl);
  1882. res = xs_dict_append(res, "statuses", stl);
  1883. res = xs_dict_append(res, "hashtags", htl);
  1884. *body = xs_json_dumps(res, 4);
  1885. *ctype = "application/json";
  1886. status = HTTP_STATUS_OK;
  1887. }
  1888. else
  1889. status = HTTP_STATUS_UNAUTHORIZED;
  1890. }
  1891. /* user cleanup */
  1892. if (logged_in)
  1893. user_free(&snac1);
  1894. srv_debug(1, xs_fmt("mastoapi_get_handler %s %d", q_path, status));
  1895. return status;
  1896. }
  1897. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  1898. const char *payload, int p_size,
  1899. char **body, int *b_size, char **ctype)
  1900. {
  1901. (void)p_size;
  1902. (void)b_size;
  1903. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1904. return 0;
  1905. int status = HTTP_STATUS_NOT_FOUND;
  1906. xs *args = NULL;
  1907. const char *i_ctype = xs_dict_get(req, "content-type");
  1908. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  1909. if (!xs_is_null(payload))
  1910. args = xs_json_loads(payload);
  1911. }
  1912. else if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded"))
  1913. {
  1914. // Some apps send form data instead of json so we should cater for those
  1915. if (!xs_is_null(payload)) {
  1916. args = xs_url_vars(payload);
  1917. }
  1918. }
  1919. else
  1920. args = xs_dup(xs_dict_get(req, "p_vars"));
  1921. if (args == NULL)
  1922. return HTTP_STATUS_BAD_REQUEST;
  1923. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1924. snac snac = {0};
  1925. int logged_in = process_auth_token(&snac, req);
  1926. if (strcmp(cmd, "/v1/apps") == 0) { /** **/
  1927. const char *name = xs_dict_get(args, "client_name");
  1928. const char *ruri = xs_dict_get(args, "redirect_uris");
  1929. const char *scope = xs_dict_get(args, "scope");
  1930. /* Ice Cubes sends these values as query parameters, so try these */
  1931. if (name == NULL && ruri == NULL && scope == NULL) {
  1932. args = xs_dup(xs_dict_get(req, "q_vars"));
  1933. name = xs_dict_get(args, "client_name");
  1934. ruri = xs_dict_get(args, "redirect_uris");
  1935. scope = xs_dict_get(args, "scope");
  1936. }
  1937. if (xs_type(ruri) == XSTYPE_LIST)
  1938. ruri = xs_dict_get(ruri, 0);
  1939. if (name && ruri) {
  1940. xs *app = xs_dict_new();
  1941. xs *id = xs_replace_i(tid(0), ".", "");
  1942. xs *csec = random_str();
  1943. xs *vkey = random_str();
  1944. xs *cid = NULL;
  1945. /* pick a non-existent random cid */
  1946. for (;;) {
  1947. cid = random_str();
  1948. xs *p_app = app_get(cid);
  1949. if (p_app == NULL)
  1950. break;
  1951. xs_free(cid);
  1952. }
  1953. app = xs_dict_append(app, "name", name);
  1954. app = xs_dict_append(app, "redirect_uri", ruri);
  1955. app = xs_dict_append(app, "client_id", cid);
  1956. app = xs_dict_append(app, "client_secret", csec);
  1957. app = xs_dict_append(app, "vapid_key", vkey);
  1958. app = xs_dict_append(app, "id", id);
  1959. *body = xs_json_dumps(app, 4);
  1960. *ctype = "application/json";
  1961. status = HTTP_STATUS_OK;
  1962. app = xs_dict_append(app, "code", "");
  1963. if (scope)
  1964. app = xs_dict_append(app, "scope", scope);
  1965. app_add(cid, app);
  1966. srv_debug(1, xs_fmt("mastoapi apps: new app %s", cid));
  1967. }
  1968. }
  1969. else
  1970. if (strcmp(cmd, "/v1/statuses") == 0) { /** **/
  1971. if (logged_in) {
  1972. /* post a new Note */
  1973. const char *content = xs_dict_get(args, "status");
  1974. const char *mid = xs_dict_get(args, "in_reply_to_id");
  1975. const char *visibility = xs_dict_get(args, "visibility");
  1976. const char *summary = xs_dict_get(args, "spoiler_text");
  1977. const char *media_ids = xs_dict_get(args, "media_ids");
  1978. if (xs_is_null(media_ids))
  1979. media_ids = xs_dict_get(args, "media_ids[]");
  1980. if (xs_is_null(media_ids))
  1981. media_ids = xs_dict_get(args, "media_ids");
  1982. if (xs_is_null(visibility))
  1983. visibility = "public";
  1984. xs *attach_list = xs_list_new();
  1985. xs *irt = NULL;
  1986. /* is it a reply? */
  1987. if (mid != NULL) {
  1988. xs *r_msg = NULL;
  1989. const char *md5 = MID_TO_MD5(mid);
  1990. if (valid_status(object_get_by_md5(md5, &r_msg)))
  1991. irt = xs_dup(xs_dict_get(r_msg, "id"));
  1992. }
  1993. /* does it have attachments? */
  1994. if (!xs_is_null(media_ids)) {
  1995. xs *mi = NULL;
  1996. if (xs_type(media_ids) == XSTYPE_LIST)
  1997. mi = xs_dup(media_ids);
  1998. else {
  1999. mi = xs_list_new();
  2000. mi = xs_list_append(mi, media_ids);
  2001. }
  2002. xs_list *p = mi;
  2003. const xs_str *v;
  2004. while (xs_list_iter(&p, &v)) {
  2005. xs *l = xs_list_new();
  2006. xs *url = xs_fmt("%s/s/%s", snac.actor, v);
  2007. xs *desc = static_get_meta(&snac, v);
  2008. l = xs_list_append(l, url);
  2009. l = xs_list_append(l, desc);
  2010. attach_list = xs_list_append(attach_list, l);
  2011. }
  2012. }
  2013. /* prepare the message */
  2014. xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
  2015. strcmp(visibility, "public") == 0 ? 0 : 1);
  2016. if (!xs_is_null(summary) && *summary) {
  2017. msg = xs_dict_set(msg, "sensitive", xs_stock(XSTYPE_TRUE));
  2018. msg = xs_dict_set(msg, "summary", summary);
  2019. }
  2020. /* store */
  2021. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  2022. /* 'Create' message */
  2023. xs *c_msg = msg_create(&snac, msg);
  2024. enqueue_message(&snac, c_msg);
  2025. timeline_touch(&snac);
  2026. /* convert to a mastodon status as a response code */
  2027. xs *st = mastoapi_status(&snac, msg);
  2028. *body = xs_json_dumps(st, 4);
  2029. *ctype = "application/json";
  2030. status = HTTP_STATUS_OK;
  2031. }
  2032. else
  2033. status = HTTP_STATUS_UNAUTHORIZED;
  2034. }
  2035. else
  2036. if (xs_startswith(cmd, "/v1/statuses")) { /** **/
  2037. if (logged_in) {
  2038. /* operations on a status */
  2039. xs *l = xs_split(cmd, "/");
  2040. const char *mid = xs_list_get(l, 3);
  2041. const char *op = xs_list_get(l, 4);
  2042. if (!xs_is_null(mid)) {
  2043. xs *msg = NULL;
  2044. xs *out = NULL;
  2045. /* skip the 'fake' part of the id */
  2046. mid = MID_TO_MD5(mid);
  2047. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  2048. const char *id = xs_dict_get(msg, "id");
  2049. if (op == NULL) {
  2050. /* no operation (?) */
  2051. }
  2052. else
  2053. if (strcmp(op, "favourite") == 0) { /** **/
  2054. xs *n_msg = msg_admiration(&snac, id, "Like");
  2055. if (n_msg != NULL) {
  2056. enqueue_message(&snac, n_msg);
  2057. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  2058. out = mastoapi_status(&snac, msg);
  2059. }
  2060. }
  2061. else
  2062. if (strcmp(op, "unfavourite") == 0) { /** **/
  2063. xs *n_msg = msg_repulsion(&snac, id, "Like");
  2064. if (n_msg != NULL) {
  2065. enqueue_message(&snac, n_msg);
  2066. out = mastoapi_status(&snac, msg);
  2067. }
  2068. }
  2069. else
  2070. if (strcmp(op, "reblog") == 0) { /** **/
  2071. xs *n_msg = msg_admiration(&snac, id, "Announce");
  2072. if (n_msg != NULL) {
  2073. enqueue_message(&snac, n_msg);
  2074. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  2075. out = mastoapi_status(&snac, msg);
  2076. }
  2077. }
  2078. else
  2079. if (strcmp(op, "unreblog") == 0) { /** **/
  2080. xs *n_msg = msg_repulsion(&snac, id, "Announce");
  2081. if (n_msg != NULL) {
  2082. enqueue_message(&snac, n_msg);
  2083. out = mastoapi_status(&snac, msg);
  2084. }
  2085. }
  2086. else
  2087. if (strcmp(op, "bookmark") == 0) { /** **/
  2088. /* bookmark this message */
  2089. if (bookmark(&snac, id) == 0)
  2090. out = mastoapi_status(&snac, msg);
  2091. else
  2092. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2093. }
  2094. else
  2095. if (strcmp(op, "unbookmark") == 0) { /** **/
  2096. /* unbookmark this message */
  2097. unbookmark(&snac, id);
  2098. out = mastoapi_status(&snac, msg);
  2099. }
  2100. else
  2101. if (strcmp(op, "pin") == 0) { /** **/
  2102. /* pin this message */
  2103. if (pin(&snac, id) == 0)
  2104. out = mastoapi_status(&snac, msg);
  2105. else
  2106. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2107. }
  2108. else
  2109. if (strcmp(op, "unpin") == 0) { /** **/
  2110. /* unpin this message */
  2111. unpin(&snac, id);
  2112. out = mastoapi_status(&snac, msg);
  2113. }
  2114. else
  2115. if (strcmp(op, "mute") == 0) { /** **/
  2116. /* Mastodon's mute is snac's hide */
  2117. }
  2118. else
  2119. if (strcmp(op, "unmute") == 0) { /** **/
  2120. /* Mastodon's unmute is snac's unhide */
  2121. }
  2122. }
  2123. if (out != NULL) {
  2124. *body = xs_json_dumps(out, 4);
  2125. *ctype = "application/json";
  2126. status = HTTP_STATUS_OK;
  2127. }
  2128. }
  2129. }
  2130. else
  2131. status = HTTP_STATUS_UNAUTHORIZED;
  2132. }
  2133. else
  2134. if (strcmp(cmd, "/v1/notifications/clear") == 0) { /** **/
  2135. if (logged_in) {
  2136. notify_clear(&snac);
  2137. timeline_touch(&snac);
  2138. *body = xs_dup("{}");
  2139. *ctype = "application/json";
  2140. status = HTTP_STATUS_OK;
  2141. }
  2142. else
  2143. status = HTTP_STATUS_UNAUTHORIZED;
  2144. }
  2145. else
  2146. if (strcmp(cmd, "/v1/push/subscription") == 0) { /** **/
  2147. /* I don't know what I'm doing */
  2148. if (logged_in) {
  2149. const char *v;
  2150. xs *wpush = xs_dict_new();
  2151. wpush = xs_dict_append(wpush, "id", "1");
  2152. v = xs_dict_get(args, "data");
  2153. v = xs_dict_get(v, "alerts");
  2154. wpush = xs_dict_append(wpush, "alerts", v);
  2155. v = xs_dict_get(args, "subscription");
  2156. v = xs_dict_get(v, "endpoint");
  2157. wpush = xs_dict_append(wpush, "endpoint", v);
  2158. xs *server_key = random_str();
  2159. wpush = xs_dict_append(wpush, "server_key", server_key);
  2160. *body = xs_json_dumps(wpush, 4);
  2161. *ctype = "application/json";
  2162. status = HTTP_STATUS_OK;
  2163. }
  2164. else
  2165. status = HTTP_STATUS_UNAUTHORIZED;
  2166. }
  2167. else
  2168. if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { /** **/
  2169. if (logged_in) {
  2170. const xs_list *file = xs_dict_get(args, "file");
  2171. const char *desc = xs_dict_get(args, "description");
  2172. if (xs_is_null(desc))
  2173. desc = "";
  2174. status = HTTP_STATUS_BAD_REQUEST;
  2175. if (xs_type(file) == XSTYPE_LIST) {
  2176. const char *fn = xs_list_get(file, 0);
  2177. if (*fn != '\0') {
  2178. char *ext = strrchr(fn, '.');
  2179. xs *hash = xs_md5_hex(fn, strlen(fn));
  2180. xs *id = xs_fmt("%s%s", hash, ext);
  2181. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  2182. int fo = xs_number_get(xs_list_get(file, 1));
  2183. int fs = xs_number_get(xs_list_get(file, 2));
  2184. /* store */
  2185. static_put(&snac, id, payload + fo, fs);
  2186. static_put_meta(&snac, id, desc);
  2187. /* prepare a response */
  2188. xs *rsp = xs_dict_new();
  2189. rsp = xs_dict_append(rsp, "id", id);
  2190. rsp = xs_dict_append(rsp, "type", "image");
  2191. rsp = xs_dict_append(rsp, "url", url);
  2192. rsp = xs_dict_append(rsp, "preview_url", url);
  2193. rsp = xs_dict_append(rsp, "remote_url", url);
  2194. rsp = xs_dict_append(rsp, "description", desc);
  2195. *body = xs_json_dumps(rsp, 4);
  2196. *ctype = "application/json";
  2197. status = HTTP_STATUS_OK;
  2198. }
  2199. }
  2200. }
  2201. else
  2202. status = HTTP_STATUS_UNAUTHORIZED;
  2203. }
  2204. else
  2205. if (xs_startswith(cmd, "/v1/accounts")) { /** **/
  2206. if (logged_in) {
  2207. /* account-related information */
  2208. xs *l = xs_split(cmd, "/");
  2209. const char *md5 = xs_list_get(l, 3);
  2210. const char *opt = xs_list_get(l, 4);
  2211. xs *rsp = NULL;
  2212. if (!xs_is_null(md5) && *md5) {
  2213. xs *actor_o = NULL;
  2214. if (xs_is_null(opt)) {
  2215. /* ? */
  2216. }
  2217. else
  2218. if (strcmp(opt, "follow") == 0) { /** **/
  2219. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2220. const char *actor = xs_dict_get(actor_o, "id");
  2221. xs *msg = msg_follow(&snac, actor);
  2222. if (msg != NULL) {
  2223. /* reload the actor from the message, in may be different */
  2224. actor = xs_dict_get(msg, "object");
  2225. following_add(&snac, actor, msg);
  2226. enqueue_output_by_actor(&snac, msg, actor, 0);
  2227. rsp = mastoapi_relationship(&snac, md5);
  2228. }
  2229. }
  2230. }
  2231. else
  2232. if (strcmp(opt, "unfollow") == 0) { /** **/
  2233. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2234. const char *actor = xs_dict_get(actor_o, "id");
  2235. /* get the following object */
  2236. xs *object = NULL;
  2237. if (valid_status(following_get(&snac, actor, &object))) {
  2238. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  2239. following_del(&snac, actor);
  2240. enqueue_output_by_actor(&snac, msg, actor, 0);
  2241. rsp = mastoapi_relationship(&snac, md5);
  2242. }
  2243. }
  2244. }
  2245. else
  2246. if (strcmp(opt, "block") == 0) { /** **/
  2247. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2248. const char *actor = xs_dict_get(actor_o, "id");
  2249. mute(&snac, actor);
  2250. rsp = mastoapi_relationship(&snac, md5);
  2251. }
  2252. }
  2253. else
  2254. if (strcmp(opt, "unblock") == 0) { /** **/
  2255. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2256. const char *actor = xs_dict_get(actor_o, "id");
  2257. unmute(&snac, actor);
  2258. rsp = mastoapi_relationship(&snac, md5);
  2259. }
  2260. }
  2261. }
  2262. if (rsp != NULL) {
  2263. *body = xs_json_dumps(rsp, 4);
  2264. *ctype = "application/json";
  2265. status = HTTP_STATUS_OK;
  2266. }
  2267. }
  2268. else
  2269. status = HTTP_STATUS_UNAUTHORIZED;
  2270. }
  2271. else
  2272. if (xs_startswith(cmd, "/v1/polls")) { /** **/
  2273. if (logged_in) {
  2274. /* operations on a status */
  2275. xs *l = xs_split(cmd, "/");
  2276. const char *mid = xs_list_get(l, 3);
  2277. const char *op = xs_list_get(l, 4);
  2278. if (!xs_is_null(mid)) {
  2279. xs *msg = NULL;
  2280. xs *out = NULL;
  2281. /* skip the 'fake' part of the id */
  2282. mid = MID_TO_MD5(mid);
  2283. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  2284. const char *id = xs_dict_get(msg, "id");
  2285. const char *atto = get_atto(msg);
  2286. const xs_list *opts = xs_dict_get(msg, "oneOf");
  2287. if (opts == NULL)
  2288. opts = xs_dict_get(msg, "anyOf");
  2289. if (op == NULL) {
  2290. }
  2291. else
  2292. if (strcmp(op, "votes") == 0) {
  2293. const xs_list *choices = xs_dict_get(args, "choices[]");
  2294. if (xs_is_null(choices))
  2295. choices = xs_dict_get(args, "choices");
  2296. if (xs_type(choices) == XSTYPE_LIST) {
  2297. const xs_str *v;
  2298. int c = 0;
  2299. while (xs_list_next(choices, &v, &c)) {
  2300. int io = atoi(v);
  2301. const xs_dict *o = xs_list_get(opts, io);
  2302. if (o) {
  2303. const char *name = xs_dict_get(o, "name");
  2304. xs *msg = msg_note(&snac, "", atto, (char *)id, NULL, 1);
  2305. msg = xs_dict_append(msg, "name", name);
  2306. xs *c_msg = msg_create(&snac, msg);
  2307. enqueue_message(&snac, c_msg);
  2308. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  2309. }
  2310. }
  2311. out = mastoapi_poll(&snac, msg);
  2312. }
  2313. }
  2314. }
  2315. if (out != NULL) {
  2316. *body = xs_json_dumps(out, 4);
  2317. *ctype = "application/json";
  2318. status = HTTP_STATUS_OK;
  2319. }
  2320. }
  2321. }
  2322. else
  2323. status = HTTP_STATUS_UNAUTHORIZED;
  2324. }
  2325. else
  2326. if (strcmp(cmd, "/v1/lists") == 0) {
  2327. if (logged_in) {
  2328. const char *title = xs_dict_get(args, "title");
  2329. if (xs_type(title) == XSTYPE_STRING) {
  2330. /* add the list */
  2331. xs *out = xs_dict_new();
  2332. xs *lid = list_maint(&snac, title, 1);
  2333. if (!xs_is_null(lid)) {
  2334. out = xs_dict_append(out, "id", lid);
  2335. out = xs_dict_append(out, "title", title);
  2336. out = xs_dict_append(out, "replies_policy", xs_dict_get_def(args, "replies_policy", "list"));
  2337. out = xs_dict_append(out, "exclusive", xs_stock(XSTYPE_FALSE));
  2338. status = HTTP_STATUS_OK;
  2339. }
  2340. else {
  2341. out = xs_dict_append(out, "error", "cannot create list");
  2342. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2343. }
  2344. *body = xs_json_dumps(out, 4);
  2345. *ctype = "application/json";
  2346. }
  2347. else
  2348. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2349. }
  2350. }
  2351. if (xs_startswith(cmd, "/v1/lists/")) { /** list maintenance **/
  2352. if (logged_in) {
  2353. xs *l = xs_split(cmd, "/");
  2354. const char *op = xs_list_get(l, -1);
  2355. const char *id = xs_list_get(l, -2);
  2356. if (op && id && xs_is_hex(id)) {
  2357. if (strcmp(op, "accounts") == 0) {
  2358. const xs_list *accts = xs_dict_get(args, "account_ids[]");
  2359. if (xs_is_null(accts))
  2360. accts = xs_dict_get(args, "account_ids");
  2361. int c = 0;
  2362. const char *v;
  2363. while (xs_list_next(accts, &v, &c)) {
  2364. list_content(&snac, id, v, 1);
  2365. }
  2366. xs *out = xs_dict_new();
  2367. *body = xs_json_dumps(out, 4);
  2368. *ctype = "application/json";
  2369. status = HTTP_STATUS_OK;
  2370. }
  2371. }
  2372. }
  2373. else
  2374. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2375. }
  2376. /* user cleanup */
  2377. if (logged_in)
  2378. user_free(&snac);
  2379. srv_debug(1, xs_fmt("mastoapi_post_handler %s %d", q_path, status));
  2380. return status;
  2381. }
  2382. int mastoapi_delete_handler(const xs_dict *req, const char *q_path,
  2383. const char *payload, int p_size,
  2384. char **body, int *b_size, char **ctype)
  2385. {
  2386. (void)p_size;
  2387. (void)body;
  2388. (void)b_size;
  2389. (void)ctype;
  2390. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  2391. return 0;
  2392. int status = HTTP_STATUS_NOT_FOUND;
  2393. xs *args = NULL;
  2394. const char *i_ctype = xs_dict_get(req, "content-type");
  2395. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  2396. if (!xs_is_null(payload))
  2397. args = xs_json_loads(payload);
  2398. }
  2399. else if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded"))
  2400. {
  2401. // Some apps send form data instead of json so we should cater for those
  2402. if (!xs_is_null(payload)) {
  2403. args = xs_url_vars(payload);
  2404. }
  2405. }
  2406. else
  2407. args = xs_dup(xs_dict_get(req, "p_vars"));
  2408. if (args == NULL)
  2409. return HTTP_STATUS_BAD_REQUEST;
  2410. snac snac = {0};
  2411. int logged_in = process_auth_token(&snac, req);
  2412. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2413. if (xs_startswith(cmd, "/v1/push/subscription") || xs_startswith(cmd, "/v2/push/subscription")) { /** **/
  2414. // pretend we deleted it, since it doesn't exist anyway
  2415. status = HTTP_STATUS_OK;
  2416. }
  2417. else
  2418. if (xs_startswith(cmd, "/v1/lists/")) {
  2419. if (logged_in) {
  2420. xs *l = xs_split(cmd, "/");
  2421. const char *p = xs_list_get(l, -1);
  2422. if (p) {
  2423. if (strcmp(p, "accounts") == 0) {
  2424. /* delete account from list */
  2425. p = xs_list_get(l, -2);
  2426. const xs_list *accts = xs_dict_get(args, "account_ids[]");
  2427. if (xs_is_null(accts))
  2428. accts = xs_dict_get(args, "account_ids");
  2429. int c = 0;
  2430. const char *v;
  2431. while (xs_list_next(accts, &v, &c)) {
  2432. list_content(&snac, p, v, 2);
  2433. }
  2434. }
  2435. else {
  2436. /* delete list */
  2437. if (xs_is_hex(p)) {
  2438. list_maint(&snac, p, 2);
  2439. }
  2440. }
  2441. }
  2442. *ctype = "application/json";
  2443. status = HTTP_STATUS_OK;
  2444. }
  2445. else
  2446. status = HTTP_STATUS_UNAUTHORIZED;
  2447. }
  2448. /* user cleanup */
  2449. if (logged_in)
  2450. user_free(&snac);
  2451. srv_debug(1, xs_fmt("mastoapi_delete_handler %s %d", q_path, status));
  2452. return status;
  2453. }
  2454. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  2455. const char *payload, int p_size,
  2456. char **body, int *b_size, char **ctype)
  2457. {
  2458. (void)p_size;
  2459. (void)b_size;
  2460. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  2461. return 0;
  2462. int status = HTTP_STATUS_NOT_FOUND;
  2463. xs *args = NULL;
  2464. const char *i_ctype = xs_dict_get(req, "content-type");
  2465. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  2466. if (!xs_is_null(payload))
  2467. args = xs_json_loads(payload);
  2468. }
  2469. else
  2470. args = xs_dup(xs_dict_get(req, "p_vars"));
  2471. if (args == NULL)
  2472. return HTTP_STATUS_BAD_REQUEST;
  2473. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2474. snac snac = {0};
  2475. int logged_in = process_auth_token(&snac, req);
  2476. if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) { /** **/
  2477. if (logged_in) {
  2478. xs *l = xs_split(cmd, "/");
  2479. const char *stid = xs_list_get(l, 3);
  2480. if (!xs_is_null(stid)) {
  2481. const char *desc = xs_dict_get(args, "description");
  2482. /* set the image metadata */
  2483. static_put_meta(&snac, stid, desc);
  2484. /* prepare a response */
  2485. xs *rsp = xs_dict_new();
  2486. xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
  2487. rsp = xs_dict_append(rsp, "id", stid);
  2488. rsp = xs_dict_append(rsp, "type", "image");
  2489. rsp = xs_dict_append(rsp, "url", url);
  2490. rsp = xs_dict_append(rsp, "preview_url", url);
  2491. rsp = xs_dict_append(rsp, "remote_url", url);
  2492. rsp = xs_dict_append(rsp, "description", desc);
  2493. *body = xs_json_dumps(rsp, 4);
  2494. *ctype = "application/json";
  2495. status = HTTP_STATUS_OK;
  2496. }
  2497. }
  2498. else
  2499. status = HTTP_STATUS_UNAUTHORIZED;
  2500. }
  2501. else
  2502. if (xs_startswith(cmd, "/v1/statuses")) {
  2503. if (logged_in) {
  2504. xs *l = xs_split(cmd, "/");
  2505. const char *mid = xs_list_get(l, 3);
  2506. if (!xs_is_null(mid)) {
  2507. const char *md5 = MID_TO_MD5(mid);
  2508. xs *rsp = NULL;
  2509. xs *msg = NULL;
  2510. if (valid_status(timeline_get_by_md5(&snac, md5, &msg))) {
  2511. const char *content = xs_dict_get(args, "status");
  2512. xs *atls = xs_list_new();
  2513. xs *f_content = not_really_markdown(content, &atls, NULL);
  2514. /* replace fields with new content */
  2515. msg = xs_dict_set(msg, "sourceContent", content);
  2516. msg = xs_dict_set(msg, "content", f_content);
  2517. xs *updated = xs_str_utctime(0, ISO_DATE_SPEC);
  2518. msg = xs_dict_set(msg, "updated", updated);
  2519. /* overwrite object, not updating the indexes */
  2520. object_add_ow(xs_dict_get(msg, "id"), msg);
  2521. /* update message */
  2522. xs *c_msg = msg_update(&snac, msg);
  2523. enqueue_message(&snac, c_msg);
  2524. rsp = mastoapi_status(&snac, msg);
  2525. }
  2526. if (rsp != NULL) {
  2527. *body = xs_json_dumps(rsp, 4);
  2528. *ctype = "application/json";
  2529. status = HTTP_STATUS_OK;
  2530. }
  2531. }
  2532. }
  2533. else
  2534. status = HTTP_STATUS_UNAUTHORIZED;
  2535. }
  2536. /* user cleanup */
  2537. if (logged_in)
  2538. user_free(&snac);
  2539. srv_debug(1, xs_fmt("mastoapi_put_handler %s %d", q_path, status));
  2540. return status;
  2541. }
  2542. void persist_image(const char *key, const xs_val *data, const char *payload, snac *snac)
  2543. /* Store header or avatar */
  2544. {
  2545. if (data != NULL) {
  2546. if (xs_type(data) == XSTYPE_LIST) {
  2547. const char *fn = xs_list_get(data, 0);
  2548. if (fn && *fn) {
  2549. const char *ext = strrchr(fn, '.');
  2550. /* Mona iOS sends always jpg as application/octet-stream with no filename */
  2551. if (ext == NULL || strcmp(fn, key) == 0) {
  2552. ext = ".jpg";
  2553. }
  2554. /* Make sure we have a unique file name, otherwise updated images will not be
  2555. * re-loaded by clients. */
  2556. xs *rnd = random_str();
  2557. xs *hash = xs_md5_hex(rnd, strlen(rnd));
  2558. xs *id = xs_fmt("%s%s", hash, ext);
  2559. xs *url = xs_fmt("%s/s/%s", snac->actor, id);
  2560. int fo = xs_number_get(xs_list_get(data, 1));
  2561. int fs = xs_number_get(xs_list_get(data, 2));
  2562. /* store */
  2563. static_put(snac, id, payload + fo, fs);
  2564. snac->config = xs_dict_set(snac->config, key, url);
  2565. }
  2566. }
  2567. }
  2568. }
  2569. int mastoapi_patch_handler(const xs_dict *req, const char *q_path,
  2570. const char *payload, int p_size,
  2571. char **body, int *b_size, char **ctype)
  2572. /* Handle profile updates */
  2573. {
  2574. (void)p_size;
  2575. (void)b_size;
  2576. if (!xs_startswith(q_path, "/api/v1/"))
  2577. return 0;
  2578. int status = HTTP_STATUS_NOT_FOUND;
  2579. xs *args = NULL;
  2580. const char *i_ctype = xs_dict_get(req, "content-type");
  2581. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  2582. if (!xs_is_null(payload))
  2583. args = xs_json_loads(payload);
  2584. }
  2585. else if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded"))
  2586. {
  2587. // Some apps send form data instead of json so we should cater for those
  2588. if (!xs_is_null(payload)) {
  2589. args = xs_url_vars(payload);
  2590. }
  2591. }
  2592. else
  2593. args = xs_dup(xs_dict_get(req, "p_vars"));
  2594. if (args == NULL)
  2595. return HTTP_STATUS_BAD_REQUEST;
  2596. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2597. snac snac = {0};
  2598. int logged_in = process_auth_token(&snac, req);
  2599. if (xs_startswith(cmd, "/v1/accounts/update_credentials")) {
  2600. /* Update user profile fields */
  2601. if (logged_in) {
  2602. int c = 0;
  2603. const xs_str *k;
  2604. const xs_val *v;
  2605. const xs_str *field_name = NULL;
  2606. xs *new_fields = xs_dict_new();
  2607. while (xs_dict_next(args, &k, &v, &c)) {
  2608. if (strcmp(k, "display_name") == 0) {
  2609. if (v != NULL)
  2610. snac.config = xs_dict_set(snac.config, "name", v);
  2611. }
  2612. else
  2613. if (strcmp(k, "note") == 0) {
  2614. if (v != NULL)
  2615. snac.config = xs_dict_set(snac.config, "bio", v);
  2616. }
  2617. else
  2618. if (strcmp(k, "bot") == 0) {
  2619. if (v != NULL)
  2620. snac.config = xs_dict_set(snac.config, "bot",
  2621. (strcmp(v, "true") == 0 ||
  2622. strcmp(v, "1") == 0) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  2623. }
  2624. else
  2625. if (strcmp(k, "source[sensitive]") == 0) {
  2626. if (v != NULL)
  2627. snac.config = xs_dict_set(snac.config, "cw",
  2628. strcmp(v, "true") == 0 ? "open" : "");
  2629. }
  2630. else
  2631. if (strcmp(k, "source[privacy]") == 0) {
  2632. if (v != NULL)
  2633. snac.config = xs_dict_set(snac.config, "private",
  2634. strcmp(v, "private") == 0 ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  2635. }
  2636. else
  2637. if (strcmp(k, "header") == 0) {
  2638. persist_image("header", v, payload, &snac);
  2639. }
  2640. else
  2641. if (strcmp(k, "avatar") == 0) {
  2642. persist_image("avatar", v, payload, &snac);
  2643. }
  2644. else
  2645. if (xs_between("fields_attributes", k, "[name]")) {
  2646. field_name = strcmp(v, "") != 0 ? v : NULL;
  2647. }
  2648. else
  2649. if (xs_between("fields_attributes", k, "[value]")) {
  2650. if (field_name != NULL) {
  2651. new_fields = xs_dict_set(new_fields, field_name, v);
  2652. snac.config = xs_dict_set(snac.config, "metadata", new_fields);
  2653. }
  2654. }
  2655. /* we don't have support for the following options, yet
  2656. - discoverable (0/1)
  2657. - locked (0/1)
  2658. */
  2659. }
  2660. /* Persist profile */
  2661. if (user_persist(&snac, 1) == 0)
  2662. credentials_get(body, ctype, &status, snac);
  2663. else
  2664. status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
  2665. }
  2666. else
  2667. status = HTTP_STATUS_UNAUTHORIZED;
  2668. }
  2669. /* user cleanup */
  2670. if (logged_in)
  2671. user_free(&snac);
  2672. srv_debug(1, xs_fmt("mastoapi_patch_handler %s %d", q_path, status));
  2673. return status;
  2674. }
  2675. void mastoapi_purge(void)
  2676. {
  2677. xs *spec = xs_fmt("%s/app/" "*.json", srv_basedir);
  2678. xs *files = xs_glob(spec, 1, 0);
  2679. xs_list *p = files;
  2680. const xs_str *v;
  2681. time_t mt = time(NULL) - 3600;
  2682. while (xs_list_iter(&p, &v)) {
  2683. xs *cid = xs_replace(v, ".json", "");
  2684. xs *fn = _app_fn(cid);
  2685. if (mtime(fn) < mt) {
  2686. /* get the app */
  2687. xs *app = app_get(cid);
  2688. if (app) {
  2689. /* old apps with no uid are incomplete cruft */
  2690. const char *uid = xs_dict_get(app, "uid");
  2691. if (xs_is_null(uid) || *uid == '\0') {
  2692. unlink(fn);
  2693. srv_debug(2, xs_fmt("purged %s", fn));
  2694. }
  2695. }
  2696. }
  2697. }
  2698. }
  2699. #endif /* #ifndef NO_MASTODON_API */