mastoapi.c 89 KB

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