mastoapi.c 110 KB

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