mastoapi.c 109 KB

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