1
0

mastoapi.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736
  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. if (xs_startswith(id, srv_baseurl)) {
  490. /* if it's a local user, open it and pick its validated links */
  491. snac user;
  492. if (user_open(&user, prefu)) {
  493. val_links = xs_dict_get(user.config, "validated_links");
  494. user_free(&user);
  495. }
  496. }
  497. if (xs_is_null(val_links))
  498. val_links = xs_stock_dict;
  499. while (xs_list_iter(&p, &v)) {
  500. char *type = xs_dict_get(v, "type");
  501. char *name = xs_dict_get(v, "name");
  502. char *value = xs_dict_get(v, "value");
  503. if (!xs_is_null(type) && !xs_is_null(name) &&
  504. !xs_is_null(value) && strcmp(type, "PropertyValue") == 0) {
  505. char *val_date = NULL;
  506. if (xs_startswith(value, "https:/" "/"))
  507. val_date = xs_dict_get(val_links, value);
  508. xs *d = xs_dict_new();
  509. d = xs_dict_append(d, "name", name);
  510. d = xs_dict_append(d, "value", value);
  511. d = xs_dict_append(d, "verified_at",
  512. xs_type(val_date) == XSTYPE_STRING && *val_date ?
  513. val_date : xs_stock_null);
  514. fields = xs_list_append(fields, d);
  515. }
  516. }
  517. acct = xs_dict_append(acct, "fields", fields);
  518. return acct;
  519. }
  520. xs_str *mastoapi_date(char *date)
  521. /* converts an ISO 8601 date to whatever format Mastodon uses */
  522. {
  523. xs_str *s = xs_crop_i(xs_dup(date), 0, 19);
  524. s = xs_str_cat(s, ".000Z");
  525. return s;
  526. }
  527. xs_dict *mastoapi_poll(snac *snac, const xs_dict *msg)
  528. /* creates a mastoapi Poll object */
  529. {
  530. xs_dict *poll = xs_dict_new();
  531. xs *mid = mastoapi_id(msg);
  532. xs_list *opts = NULL;
  533. xs_val *v;
  534. int num_votes = 0;
  535. xs *options = xs_list_new();
  536. poll = xs_dict_append(poll, "id", mid);
  537. xs *fd = mastoapi_date(xs_dict_get(msg, "endTime"));
  538. poll = xs_dict_append(poll, "expires_at", fd);
  539. poll = xs_dict_append(poll, "expired",
  540. xs_dict_get(msg, "closed") != NULL ? xs_stock_true : xs_stock_false);
  541. if ((opts = xs_dict_get(msg, "oneOf")) != NULL)
  542. poll = xs_dict_append(poll, "multiple", xs_stock_false);
  543. else {
  544. opts = xs_dict_get(msg, "anyOf");
  545. poll = xs_dict_append(poll, "multiple", xs_stock_true);
  546. }
  547. while (xs_list_iter(&opts, &v)) {
  548. const char *title = xs_dict_get(v, "name");
  549. const char *replies = xs_dict_get(v, "replies");
  550. if (title && replies) {
  551. const char *votes_count = xs_dict_get(replies, "totalItems");
  552. if (xs_type(votes_count) == XSTYPE_NUMBER) {
  553. xs *d = xs_dict_new();
  554. d = xs_dict_append(d, "title", title);
  555. d = xs_dict_append(d, "votes_count", votes_count);
  556. options = xs_list_append(options, d);
  557. num_votes += xs_number_get(votes_count);
  558. }
  559. }
  560. }
  561. poll = xs_dict_append(poll, "options", options);
  562. xs *vc = xs_number_new(num_votes);
  563. poll = xs_dict_append(poll, "votes_count", vc);
  564. poll = xs_dict_append(poll, "voted",
  565. (snac && was_question_voted(snac, xs_dict_get(msg, "id"))) ?
  566. xs_stock_true : xs_stock_false);
  567. return poll;
  568. }
  569. xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
  570. /* converts an ActivityPub note to a Mastodon status */
  571. {
  572. xs *actor = NULL;
  573. actor_get(get_atto(msg), &actor);
  574. /* if the author is not here, discard */
  575. if (actor == NULL)
  576. return NULL;
  577. const char *type = xs_dict_get(msg, "type");
  578. const char *id = xs_dict_get(msg, "id");
  579. /* fail if it's not a valid actor */
  580. if (xs_is_null(type) || xs_is_null(id))
  581. return NULL;
  582. xs *acct = mastoapi_account(actor);
  583. if (acct == NULL)
  584. return NULL;
  585. xs *idx = NULL;
  586. xs *ixc = NULL;
  587. char *tmp;
  588. xs *mid = mastoapi_id(msg);
  589. xs_dict *st = xs_dict_new();
  590. st = xs_dict_append(st, "id", mid);
  591. st = xs_dict_append(st, "uri", id);
  592. st = xs_dict_append(st, "url", id);
  593. st = xs_dict_append(st, "account", acct);
  594. xs *fd = mastoapi_date(xs_dict_get(msg, "published"));
  595. st = xs_dict_append(st, "created_at", fd);
  596. {
  597. const char *content = xs_dict_get(msg, "content");
  598. const char *name = xs_dict_get(msg, "name");
  599. xs *s1 = NULL;
  600. if (name && content)
  601. s1 = xs_fmt("%s<br><br>%s", name, content);
  602. else
  603. if (name)
  604. s1 = xs_dup(name);
  605. else
  606. if (content)
  607. s1 = xs_dup(content);
  608. else
  609. s1 = xs_str_new(NULL);
  610. st = xs_dict_append(st, "content", s1);
  611. }
  612. st = xs_dict_append(st, "visibility",
  613. is_msg_public(msg) ? "public" : "private");
  614. tmp = xs_dict_get(msg, "sensitive");
  615. if (xs_is_null(tmp))
  616. tmp = xs_stock_false;
  617. st = xs_dict_append(st, "sensitive", tmp);
  618. tmp = xs_dict_get(msg, "summary");
  619. if (xs_is_null(tmp))
  620. tmp = "";
  621. st = xs_dict_append(st, "spoiler_text", tmp);
  622. /* create the list of attachments */
  623. xs *attach = get_attachments(msg);
  624. {
  625. xs_list *p = attach;
  626. xs_dict *v;
  627. xs *matt = xs_list_new();
  628. while (xs_list_iter(&p, &v)) {
  629. char *type = xs_dict_get(v, "type");
  630. char *href = xs_dict_get(v, "href");
  631. char *name = xs_dict_get(v, "name");
  632. if (xs_match(type, "image/*|video/*|Image|Video")) { /* */
  633. xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
  634. xs *d = xs_dict_new();
  635. d = xs_dict_append(d, "id", matteid);
  636. d = xs_dict_append(d, "url", href);
  637. d = xs_dict_append(d, "preview_url", href);
  638. d = xs_dict_append(d, "remote_url", href);
  639. d = xs_dict_append(d, "description", name);
  640. d = xs_dict_append(d, "type", (*type == 'v' || *type == 'V') ? "video" : "image");
  641. matt = xs_list_append(matt, d);
  642. }
  643. }
  644. st = xs_dict_append(st, "media_attachments", matt);
  645. }
  646. {
  647. xs *ml = xs_list_new();
  648. xs *htl = xs_list_new();
  649. xs *eml = xs_list_new();
  650. xs_list *tag = xs_dict_get(msg, "tag");
  651. int n = 0;
  652. xs *tag_list = NULL;
  653. if (xs_type(tag) == XSTYPE_DICT) {
  654. tag_list = xs_list_new();
  655. tag_list = xs_list_append(tag_list, tag);
  656. }
  657. else
  658. if (xs_type(tag) == XSTYPE_LIST)
  659. tag_list = xs_dup(tag);
  660. else
  661. tag_list = xs_list_new();
  662. tag = tag_list;
  663. xs_dict *v;
  664. while (xs_list_iter(&tag, &v)) {
  665. const char *type = xs_dict_get(v, "type");
  666. if (xs_is_null(type))
  667. continue;
  668. xs *d1 = xs_dict_new();
  669. if (strcmp(type, "Mention") == 0) {
  670. const char *name = xs_dict_get(v, "name");
  671. const char *href = xs_dict_get(v, "href");
  672. if (!xs_is_null(name) && !xs_is_null(href) &&
  673. (snac == NULL || strcmp(href, snac->actor) != 0)) {
  674. xs *nm = xs_strip_chars_i(xs_dup(name), "@");
  675. xs *id = xs_fmt("%d", n++);
  676. d1 = xs_dict_append(d1, "id", id);
  677. d1 = xs_dict_append(d1, "username", nm);
  678. d1 = xs_dict_append(d1, "acct", nm);
  679. d1 = xs_dict_append(d1, "url", href);
  680. ml = xs_list_append(ml, d1);
  681. }
  682. }
  683. else
  684. if (strcmp(type, "Hashtag") == 0) {
  685. const char *name = xs_dict_get(v, "name");
  686. const char *href = xs_dict_get(v, "href");
  687. if (!xs_is_null(name) && !xs_is_null(href)) {
  688. xs *nm = xs_strip_chars_i(xs_dup(name), "#");
  689. d1 = xs_dict_append(d1, "name", nm);
  690. d1 = xs_dict_append(d1, "url", href);
  691. htl = xs_list_append(htl, d1);
  692. }
  693. }
  694. else
  695. if (strcmp(type, "Emoji") == 0) {
  696. const char *name = xs_dict_get(v, "name");
  697. const xs_dict *icon = xs_dict_get(v, "icon");
  698. if (!xs_is_null(name) && !xs_is_null(icon)) {
  699. const char *url = xs_dict_get(icon, "url");
  700. if (!xs_is_null(url)) {
  701. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  702. d1 = xs_dict_append(d1, "shortcode", nm);
  703. d1 = xs_dict_append(d1, "url", url);
  704. d1 = xs_dict_append(d1, "static_url", url);
  705. d1 = xs_dict_append(d1, "visible_in_picker", xs_stock_true);
  706. d1 = xs_dict_append(d1, "category", "Emojis");
  707. eml = xs_list_append(eml, d1);
  708. }
  709. }
  710. }
  711. }
  712. st = xs_dict_append(st, "mentions", ml);
  713. st = xs_dict_append(st, "tags", htl);
  714. st = xs_dict_append(st, "emojis", eml);
  715. }
  716. xs_free(idx);
  717. xs_free(ixc);
  718. idx = object_likes(id);
  719. ixc = xs_number_new(xs_list_len(idx));
  720. st = xs_dict_append(st, "favourites_count", ixc);
  721. st = xs_dict_append(st, "favourited",
  722. (snac && xs_list_in(idx, snac->md5) != -1) ? xs_stock_true : xs_stock_false);
  723. xs_free(idx);
  724. xs_free(ixc);
  725. idx = object_announces(id);
  726. ixc = xs_number_new(xs_list_len(idx));
  727. st = xs_dict_append(st, "reblogs_count", ixc);
  728. st = xs_dict_append(st, "reblogged",
  729. (snac && xs_list_in(idx, snac->md5) != -1) ? xs_stock_true : xs_stock_false);
  730. /* get the last person who boosted this */
  731. xs *boosted_by_md5 = NULL;
  732. if (xs_list_len(idx))
  733. boosted_by_md5 = xs_dup(xs_list_get(idx, -1));
  734. xs_free(idx);
  735. xs_free(ixc);
  736. idx = object_children(id);
  737. ixc = xs_number_new(xs_list_len(idx));
  738. st = xs_dict_append(st, "replies_count", ixc);
  739. /* default in_reply_to values */
  740. st = xs_dict_append(st, "in_reply_to_id", xs_stock_null);
  741. st = xs_dict_append(st, "in_reply_to_account_id", xs_stock_null);
  742. tmp = xs_dict_get(msg, "inReplyTo");
  743. if (!xs_is_null(tmp)) {
  744. xs *irto = NULL;
  745. if (valid_status(object_get(tmp, &irto))) {
  746. xs *irt_mid = mastoapi_id(irto);
  747. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  748. char *at = NULL;
  749. if (!xs_is_null(at = get_atto(irto))) {
  750. xs *at_md5 = xs_md5_hex(at, strlen(at));
  751. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  752. }
  753. }
  754. }
  755. st = xs_dict_append(st, "reblog", xs_stock_null);
  756. st = xs_dict_append(st, "card", xs_stock_null);
  757. st = xs_dict_append(st, "language", xs_stock_null);
  758. tmp = xs_dict_get(msg, "sourceContent");
  759. if (xs_is_null(tmp))
  760. tmp = "";
  761. st = xs_dict_append(st, "text", tmp);
  762. tmp = xs_dict_get(msg, "updated");
  763. xs *fd2 = NULL;
  764. if (xs_is_null(tmp))
  765. tmp = xs_stock_null;
  766. else {
  767. fd2 = mastoapi_date(tmp);
  768. tmp = fd2;
  769. }
  770. st = xs_dict_append(st, "edited_at", tmp);
  771. if (strcmp(type, "Question") == 0) {
  772. xs *poll = mastoapi_poll(snac, msg);
  773. st = xs_dict_append(st, "poll", poll);
  774. }
  775. else
  776. st = xs_dict_append(st, "poll", xs_stock_null);
  777. st = xs_dict_append(st, "bookmarked", xs_stock_false);
  778. st = xs_dict_append(st, "pinned",
  779. (snac && is_pinned(snac, id)) ? xs_stock_true : xs_stock_false);
  780. /* is it a boost? */
  781. if (!xs_is_null(boosted_by_md5)) {
  782. /* create a new dummy status, using st as the 'reblog' field */
  783. xs_dict *bst = xs_dup(st);
  784. xs *b_actor = NULL;
  785. if (valid_status(object_get_by_md5(boosted_by_md5, &b_actor))) {
  786. xs *b_acct = mastoapi_account(b_actor);
  787. xs *fake_uri = NULL;
  788. if (snac)
  789. fake_uri = xs_fmt("%s/d/%s/Announce", snac->actor, mid);
  790. else
  791. fake_uri = xs_fmt("%s#%s", srv_baseurl, mid);
  792. bst = xs_dict_set(bst, "uri", fake_uri);
  793. bst = xs_dict_set(bst, "url", fake_uri);
  794. bst = xs_dict_set(bst, "account", b_acct);
  795. bst = xs_dict_set(bst, "content", "");
  796. bst = xs_dict_set(bst, "reblog", st);
  797. xs_free(st);
  798. st = bst;
  799. }
  800. }
  801. return st;
  802. }
  803. xs_dict *mastoapi_relationship(snac *snac, const char *md5)
  804. {
  805. xs_dict *rel = NULL;
  806. xs *actor_o = NULL;
  807. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  808. rel = xs_dict_new();
  809. const char *actor = xs_dict_get(actor_o, "id");
  810. rel = xs_dict_append(rel, "id", md5);
  811. rel = xs_dict_append(rel, "following",
  812. following_check(snac, actor) ? xs_stock_true : xs_stock_false);
  813. rel = xs_dict_append(rel, "showing_reblogs", xs_stock_true);
  814. rel = xs_dict_append(rel, "notifying", xs_stock_false);
  815. rel = xs_dict_append(rel, "followed_by",
  816. follower_check(snac, actor) ? xs_stock_true : xs_stock_false);
  817. rel = xs_dict_append(rel, "blocking",
  818. is_muted(snac, actor) ? xs_stock_true : xs_stock_false);
  819. rel = xs_dict_append(rel, "muting", xs_stock_false);
  820. rel = xs_dict_append(rel, "muting_notifications", xs_stock_false);
  821. rel = xs_dict_append(rel, "requested", xs_stock_false);
  822. rel = xs_dict_append(rel, "domain_blocking", xs_stock_false);
  823. rel = xs_dict_append(rel, "endorsed", xs_stock_false);
  824. rel = xs_dict_append(rel, "note", "");
  825. }
  826. return rel;
  827. }
  828. int process_auth_token(snac *snac, const xs_dict *req)
  829. /* processes an authorization token, if there is one */
  830. {
  831. int logged_in = 0;
  832. char *v;
  833. /* if there is an authorization field, try to validate it */
  834. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  835. xs *tokid = xs_replace_n(v, "Bearer ", "", 1);
  836. xs *token = token_get(tokid);
  837. if (token != NULL) {
  838. const char *uid = xs_dict_get(token, "uid");
  839. if (!xs_is_null(uid) && user_open(snac, uid)) {
  840. logged_in = 1;
  841. /* this counts as a 'login' */
  842. lastlog_write(snac, "mastoapi");
  843. srv_debug(2, xs_fmt("mastoapi auth: valid token for user '%s'", uid));
  844. }
  845. else
  846. srv_log(xs_fmt("mastoapi auth: corrupted token '%s'", tokid));
  847. }
  848. else
  849. srv_log(xs_fmt("mastoapi auth: invalid token '%s'", tokid));
  850. }
  851. return logged_in;
  852. }
  853. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  854. char **body, int *b_size, char **ctype)
  855. {
  856. (void)b_size;
  857. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  858. return 0;
  859. int status = 404;
  860. xs_dict *args = xs_dict_get(req, "q_vars");
  861. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  862. snac snac1 = {0};
  863. int logged_in = process_auth_token(&snac1, req);
  864. if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) { /** **/
  865. if (logged_in) {
  866. xs *acct = xs_dict_new();
  867. acct = xs_dict_append(acct, "id", snac1.md5);
  868. acct = xs_dict_append(acct, "username", xs_dict_get(snac1.config, "uid"));
  869. acct = xs_dict_append(acct, "acct", xs_dict_get(snac1.config, "uid"));
  870. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac1.config, "name"));
  871. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac1.config, "published"));
  872. acct = xs_dict_append(acct, "last_status_at", xs_dict_get(snac1.config, "published"));
  873. acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio"));
  874. acct = xs_dict_append(acct, "url", snac1.actor);
  875. acct = xs_dict_append(acct, "header", "");
  876. acct = xs_dict_append(acct, "header_static", "");
  877. acct = xs_dict_append(acct, "locked", xs_stock_false);
  878. acct = xs_dict_append(acct, "bot", xs_dict_get(snac1.config, "bot"));
  879. xs *src = xs_json_loads("{\"privacy\":\"public\","
  880. "\"sensitive\":false,\"fields\":[],\"note\":\"\"}");
  881. acct = xs_dict_append(acct, "source", src);
  882. xs *avatar = NULL;
  883. char *av = xs_dict_get(snac1.config, "avatar");
  884. if (xs_is_null(av) || *av == '\0')
  885. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  886. else
  887. avatar = xs_dup(av);
  888. acct = xs_dict_append(acct, "avatar", avatar);
  889. acct = xs_dict_append(acct, "avatar_static", avatar);
  890. xs_dict *metadata = xs_dict_get(snac1.config, "metadata");
  891. if (xs_type(metadata) == XSTYPE_DICT) {
  892. xs *fields = xs_list_new();
  893. xs_str *k;
  894. xs_str *v;
  895. xs_dict *val_links = xs_dict_get(snac1.config, "validated_links");
  896. if (xs_is_null(val_links))
  897. val_links = xs_stock_dict;
  898. while (xs_dict_iter(&metadata, &k, &v)) {
  899. char *val_date = NULL;
  900. if (xs_startswith(v, "https:/" "/"))
  901. val_date = xs_dict_get(val_links, v);
  902. xs *d = xs_dict_new();
  903. d = xs_dict_append(d, "name", k);
  904. d = xs_dict_append(d, "value", v);
  905. d = xs_dict_append(d, "verified_at",
  906. xs_type(val_date) == XSTYPE_STRING && *val_date ?
  907. val_date : xs_stock_null);
  908. fields = xs_list_append(fields, d);
  909. }
  910. acct = xs_dict_set(acct, "fields", fields);
  911. }
  912. acct = xs_dict_append(acct, "followers_count", xs_stock_0);
  913. acct = xs_dict_append(acct, "following_count", xs_stock_0);
  914. acct = xs_dict_append(acct, "statuses_count", xs_stock_0);
  915. *body = xs_json_dumps(acct, 4);
  916. *ctype = "application/json";
  917. status = 200;
  918. }
  919. else {
  920. status = 422; // "Unprocessable entity" (no login)
  921. }
  922. }
  923. else
  924. if (strcmp(cmd, "/v1/accounts/relationships") == 0) { /** **/
  925. /* find if an account is followed, blocked, etc. */
  926. /* the account to get relationships about is in args "id[]" */
  927. if (logged_in) {
  928. xs *res = xs_list_new();
  929. const char *md5 = xs_dict_get(args, "id[]");
  930. if (xs_is_null(md5))
  931. md5 = xs_dict_get(args, "id");
  932. if (!xs_is_null(md5)) {
  933. if (xs_type(md5) == XSTYPE_LIST)
  934. md5 = xs_list_get(md5, 0);
  935. xs *rel = mastoapi_relationship(&snac1, md5);
  936. if (rel != NULL)
  937. res = xs_list_append(res, rel);
  938. }
  939. *body = xs_json_dumps(res, 4);
  940. *ctype = "application/json";
  941. status = 200;
  942. }
  943. else
  944. status = 422;
  945. }
  946. else
  947. if (strcmp(cmd, "/v1/accounts/lookup") == 0) { /** **/
  948. /* lookup an account */
  949. char *acct = xs_dict_get(args, "acct");
  950. if (!xs_is_null(acct)) {
  951. xs *s = xs_strip_chars_i(xs_dup(acct), "@");
  952. xs *l = xs_split_n(s, "@", 1);
  953. char *uid = xs_list_get(l, 0);
  954. char *host = xs_list_get(l, 1);
  955. if (uid && (!host || strcmp(host, xs_dict_get(srv_config, "host")) == 0)) {
  956. snac user;
  957. if (user_open(&user, uid)) {
  958. xs *actor = msg_actor(&user);
  959. xs *macct = mastoapi_account(actor);
  960. *body = xs_json_dumps(macct, 4);
  961. *ctype = "application/json";
  962. status = 200;
  963. user_free(&user);
  964. }
  965. }
  966. }
  967. }
  968. else
  969. if (xs_startswith(cmd, "/v1/accounts/")) { /** **/
  970. /* account-related information */
  971. xs *l = xs_split(cmd, "/");
  972. const char *uid = xs_list_get(l, 3);
  973. const char *opt = xs_list_get(l, 4);
  974. if (uid != NULL) {
  975. snac snac2;
  976. xs *out = NULL;
  977. xs *actor = NULL;
  978. if (logged_in && strcmp(uid, "search") == 0) { /** **/
  979. /* search for accounts starting with q */
  980. const char *aq = xs_dict_get(args, "q");
  981. if (!xs_is_null(aq)) {
  982. xs *q = xs_tolower_i(xs_dup(aq));
  983. out = xs_list_new();
  984. xs *wing = following_list(&snac1);
  985. xs *wers = follower_list(&snac1);
  986. xs *ulst = user_list();
  987. xs_list *p;
  988. xs_str *v;
  989. xs_set seen;
  990. xs_set_init(&seen);
  991. /* user relations */
  992. xs_list *lsts[] = { wing, wers, NULL };
  993. int n;
  994. for (n = 0; (p = lsts[n]) != NULL; n++) {
  995. while (xs_list_iter(&p, &v)) {
  996. /* already seen? skip */
  997. if (xs_set_add(&seen, v) == 0)
  998. continue;
  999. xs *actor = NULL;
  1000. if (valid_status(object_get(v, &actor))) {
  1001. const char *uname = xs_dict_get(actor, "preferredUsername");
  1002. if (!xs_is_null(uname)) {
  1003. xs *luname = xs_tolower_i(xs_dup(uname));
  1004. if (xs_startswith(luname, q)) {
  1005. xs *acct = mastoapi_account(actor);
  1006. out = xs_list_append(out, acct);
  1007. }
  1008. }
  1009. }
  1010. }
  1011. }
  1012. /* local users */
  1013. p = ulst;
  1014. while (xs_list_iter(&p, &v)) {
  1015. snac user;
  1016. /* skip this same user */
  1017. if (strcmp(v, xs_dict_get(snac1.config, "uid")) == 0)
  1018. continue;
  1019. /* skip if the uid does not start with the query */
  1020. xs *v2 = xs_tolower_i(xs_dup(v));
  1021. if (!xs_startswith(v2, q))
  1022. continue;
  1023. if (user_open(&user, v)) {
  1024. /* if it's not already seen, add it */
  1025. if (xs_set_add(&seen, user.actor) == 1) {
  1026. xs *actor = msg_actor(&user);
  1027. xs *acct = mastoapi_account(actor);
  1028. out = xs_list_append(out, acct);
  1029. }
  1030. user_free(&user);
  1031. }
  1032. }
  1033. xs_set_free(&seen);
  1034. }
  1035. }
  1036. else
  1037. /* is it a local user? */
  1038. if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
  1039. if (opt == NULL) {
  1040. /* account information */
  1041. actor = msg_actor(&snac2);
  1042. out = mastoapi_account(actor);
  1043. }
  1044. else
  1045. if (strcmp(opt, "statuses") == 0) { /** **/
  1046. /* the public list of posts of a user */
  1047. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  1048. xs_list *p = timeline;
  1049. xs_str *v;
  1050. out = xs_list_new();
  1051. while (xs_list_iter(&p, &v)) {
  1052. xs *msg = NULL;
  1053. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  1054. /* add only posts by the author */
  1055. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  1056. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  1057. xs *st = mastoapi_status(&snac2, msg);
  1058. if (st)
  1059. out = xs_list_append(out, st);
  1060. }
  1061. }
  1062. }
  1063. }
  1064. user_free(&snac2);
  1065. }
  1066. else {
  1067. /* try the uid as the md5 of a possibly loaded actor */
  1068. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  1069. if (opt == NULL) {
  1070. /* account information */
  1071. out = mastoapi_account(actor);
  1072. }
  1073. else
  1074. if (strcmp(opt, "statuses") == 0) {
  1075. /* we don't serve statuses of others; return the empty list */
  1076. out = xs_list_new();
  1077. }
  1078. }
  1079. }
  1080. if (out != NULL) {
  1081. *body = xs_json_dumps(out, 4);
  1082. *ctype = "application/json";
  1083. status = 200;
  1084. }
  1085. }
  1086. }
  1087. else
  1088. if (strcmp(cmd, "/v1/timelines/home") == 0) { /** **/
  1089. /* the private timeline */
  1090. if (logged_in) {
  1091. const char *max_id = xs_dict_get(args, "max_id");
  1092. const char *since_id = xs_dict_get(args, "since_id");
  1093. const char *min_id = xs_dict_get(args, "min_id");
  1094. const char *limit_s = xs_dict_get(args, "limit");
  1095. int limit = 0;
  1096. int cnt = 0;
  1097. if (!xs_is_null(limit_s))
  1098. limit = atoi(limit_s);
  1099. if (limit == 0)
  1100. limit = 20;
  1101. xs *timeline = timeline_simple_list(&snac1, "private", 0, 2048);
  1102. xs *out = xs_list_new();
  1103. xs_list *p = timeline;
  1104. xs_str *v;
  1105. while (xs_list_iter(&p, &v) && cnt < limit) {
  1106. xs *msg = NULL;
  1107. /* only return entries older that max_id */
  1108. if (max_id) {
  1109. if (strcmp(v, MID_TO_MD5(max_id)) == 0)
  1110. max_id = NULL;
  1111. continue;
  1112. }
  1113. /* only returns entries newer than since_id */
  1114. if (since_id) {
  1115. if (strcmp(v, MID_TO_MD5(since_id)) == 0)
  1116. break;
  1117. }
  1118. /* only returns entries newer than min_id */
  1119. /* what does really "Return results immediately newer than ID" mean? */
  1120. if (min_id) {
  1121. if (strcmp(v, MID_TO_MD5(min_id)) == 0)
  1122. break;
  1123. }
  1124. /* get the entry */
  1125. if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
  1126. continue;
  1127. /* discard non-Notes */
  1128. const char *id = xs_dict_get(msg, "id");
  1129. const char *type = xs_dict_get(msg, "type");
  1130. if (!xs_match(type, "Note|Question|Page|Article"))
  1131. continue;
  1132. const char *from = NULL;
  1133. if (strcmp(type, "Page") == 0)
  1134. from = xs_dict_get(msg, "audience");
  1135. if (from == NULL)
  1136. from = get_atto(msg);
  1137. if (from == NULL)
  1138. continue;
  1139. /* is this message from a person we don't follow? */
  1140. if (strcmp(from, snac1.actor) && !following_check(&snac1, from)) {
  1141. /* discard if it was not boosted */
  1142. xs *idx = object_announces(id);
  1143. if (xs_list_len(idx) == 0)
  1144. continue;
  1145. }
  1146. /* discard notes from muted morons */
  1147. if (is_muted(&snac1, from))
  1148. continue;
  1149. /* discard hidden notes */
  1150. if (is_hidden(&snac1, id))
  1151. continue;
  1152. /* discard poll votes (they have a name) */
  1153. if (strcmp(type, "Page") != 0 && !xs_is_null(xs_dict_get(msg, "name")))
  1154. continue;
  1155. /* convert the Note into a Mastodon status */
  1156. xs *st = mastoapi_status(&snac1, msg);
  1157. if (st != NULL)
  1158. out = xs_list_append(out, st);
  1159. cnt++;
  1160. }
  1161. *body = xs_json_dumps(out, 4);
  1162. *ctype = "application/json";
  1163. status = 200;
  1164. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  1165. }
  1166. else {
  1167. status = 401; // unauthorized
  1168. }
  1169. }
  1170. else
  1171. if (strcmp(cmd, "/v1/timelines/public") == 0) { /** **/
  1172. /* the instance public timeline (public timelines for all users) */
  1173. const char *limit_s = xs_dict_get(args, "limit");
  1174. int limit = 0;
  1175. int cnt = 0;
  1176. if (!xs_is_null(limit_s))
  1177. limit = atoi(limit_s);
  1178. if (limit == 0)
  1179. limit = 20;
  1180. xs *timeline = timeline_instance_list(0, limit);
  1181. xs *out = xs_list_new();
  1182. xs_list *p = timeline;
  1183. xs_str *md5;
  1184. snac *user = NULL;
  1185. if (logged_in)
  1186. user = &snac1;
  1187. while (xs_list_iter(&p, &md5) && cnt < limit) {
  1188. xs *msg = NULL;
  1189. /* get the entry */
  1190. if (!valid_status(object_get_by_md5(md5, &msg)))
  1191. continue;
  1192. /* discard non-Notes */
  1193. const char *type = xs_dict_get(msg, "type");
  1194. if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0)
  1195. continue;
  1196. /* discard messages from private users */
  1197. if (is_msg_from_private_user(msg))
  1198. continue;
  1199. /* convert the Note into a Mastodon status */
  1200. xs *st = mastoapi_status(user, msg);
  1201. if (st != NULL) {
  1202. out = xs_list_append(out, st);
  1203. cnt++;
  1204. }
  1205. }
  1206. *body = xs_json_dumps(out, 4);
  1207. *ctype = "application/json";
  1208. status = 200;
  1209. }
  1210. else
  1211. if (xs_startswith(cmd, "/v1/timelines/tag/")) { /** **/
  1212. const char *limit_s = xs_dict_get(args, "limit");
  1213. int limit = 0;
  1214. int cnt = 0;
  1215. if (!xs_is_null(limit_s))
  1216. limit = atoi(limit_s);
  1217. if (limit == 0)
  1218. limit = 20;
  1219. /* get the tag */
  1220. xs *l = xs_split(cmd, "/");
  1221. char *tag = xs_list_get(l, -1);
  1222. xs *timeline = tag_search(tag, 0, limit);
  1223. xs *out = xs_list_new();
  1224. xs_list *p = timeline;
  1225. xs_str *md5;
  1226. while (xs_list_iter(&p, &md5) && cnt < limit) {
  1227. xs *msg = NULL;
  1228. /* get the entry */
  1229. if (!valid_status(object_get_by_md5(md5, &msg)))
  1230. continue;
  1231. /* skip non-public messages */
  1232. if (!is_msg_public(msg))
  1233. continue;
  1234. /* discard messages from private users */
  1235. if (is_msg_from_private_user(msg))
  1236. continue;
  1237. /* convert the Note into a Mastodon status */
  1238. xs *st = mastoapi_status(NULL, msg);
  1239. if (st != NULL) {
  1240. out = xs_list_append(out, st);
  1241. cnt++;
  1242. }
  1243. }
  1244. *body = xs_json_dumps(out, 4);
  1245. *ctype = "application/json";
  1246. status = 200;
  1247. }
  1248. else
  1249. if (strcmp(cmd, "/v1/conversations") == 0) { /** **/
  1250. /* TBD */
  1251. *body = xs_dup("[]");
  1252. *ctype = "application/json";
  1253. status = 200;
  1254. }
  1255. else
  1256. if (strcmp(cmd, "/v1/notifications") == 0) { /** **/
  1257. if (logged_in) {
  1258. xs *l = notify_list(&snac1, 0, 64);
  1259. xs *out = xs_list_new();
  1260. xs_list *p = l;
  1261. xs_dict *v;
  1262. xs_list *excl = xs_dict_get(args, "exclude_types[]");
  1263. while (xs_list_iter(&p, &v)) {
  1264. xs *noti = notify_get(&snac1, v);
  1265. if (noti == NULL)
  1266. continue;
  1267. const char *type = xs_dict_get(noti, "type");
  1268. const char *utype = xs_dict_get(noti, "utype");
  1269. const char *objid = xs_dict_get(noti, "objid");
  1270. xs *actor = NULL;
  1271. xs *entry = NULL;
  1272. if (!valid_status(actor_get(xs_dict_get(noti, "actor"), &actor)))
  1273. continue;
  1274. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  1275. continue;
  1276. if (is_hidden(&snac1, objid))
  1277. continue;
  1278. /* convert the type */
  1279. if (strcmp(type, "Like") == 0)
  1280. type = "favourite";
  1281. else
  1282. if (strcmp(type, "Announce") == 0)
  1283. type = "reblog";
  1284. else
  1285. if (strcmp(type, "Follow") == 0)
  1286. type = "follow";
  1287. else
  1288. if (strcmp(type, "Create") == 0)
  1289. type = "mention";
  1290. else
  1291. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  1292. type = "poll";
  1293. else
  1294. continue;
  1295. /* excluded type? */
  1296. if (!xs_is_null(excl) && xs_list_in(excl, type) != -1)
  1297. continue;
  1298. xs *mn = xs_dict_new();
  1299. mn = xs_dict_append(mn, "type", type);
  1300. xs *id = xs_replace(xs_dict_get(noti, "id"), ".", "");
  1301. mn = xs_dict_append(mn, "id", id);
  1302. mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
  1303. xs *acct = mastoapi_account(actor);
  1304. mn = xs_dict_append(mn, "account", acct);
  1305. if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
  1306. xs *st = mastoapi_status(&snac1, entry);
  1307. if (st)
  1308. mn = xs_dict_append(mn, "status", st);
  1309. }
  1310. out = xs_list_append(out, mn);
  1311. }
  1312. *body = xs_json_dumps(out, 4);
  1313. *ctype = "application/json";
  1314. status = 200;
  1315. }
  1316. else
  1317. status = 401;
  1318. }
  1319. else
  1320. if (strcmp(cmd, "/v1/filters") == 0) { /** **/
  1321. /* snac will never have filters */
  1322. *body = xs_dup("[]");
  1323. *ctype = "application/json";
  1324. status = 200;
  1325. }
  1326. else
  1327. if (strcmp(cmd, "/v2/filters") == 0) { /** **/
  1328. /* snac will never have filters
  1329. * but still, without a v2 endpoint a short delay is introduced
  1330. * in some apps */
  1331. *body = xs_dup("[]");
  1332. *ctype = "application/json";
  1333. status = 200;
  1334. }
  1335. else
  1336. if (strcmp(cmd, "/v1/favourites") == 0) { /** **/
  1337. /* snac will never support a list of favourites */
  1338. *body = xs_dup("[]");
  1339. *ctype = "application/json";
  1340. status = 200;
  1341. }
  1342. else
  1343. if (strcmp(cmd, "/v1/bookmarks") == 0) { /** **/
  1344. /* snac does not support bookmarks */
  1345. *body = xs_dup("[]");
  1346. *ctype = "application/json";
  1347. status = 200;
  1348. }
  1349. else
  1350. if (strcmp(cmd, "/v1/lists") == 0) { /** **/
  1351. /* snac does not support lists */
  1352. *body = xs_dup("[]");
  1353. *ctype = "application/json";
  1354. status = 200;
  1355. }
  1356. else
  1357. if (strcmp(cmd, "/v1/scheduled_statuses") == 0) { /** **/
  1358. /* snac does not schedule notes */
  1359. *body = xs_dup("[]");
  1360. *ctype = "application/json";
  1361. status = 200;
  1362. }
  1363. else
  1364. if (strcmp(cmd, "/v1/follow_requests") == 0) { /** **/
  1365. /* snac does not support optional follow confirmations */
  1366. *body = xs_dup("[]");
  1367. *ctype = "application/json";
  1368. status = 200;
  1369. }
  1370. else
  1371. if (strcmp(cmd, "/v1/announcements") == 0) { /** **/
  1372. /* snac has no announcements (yet?) */
  1373. *body = xs_dup("[]");
  1374. *ctype = "application/json";
  1375. status = 200;
  1376. }
  1377. else
  1378. if (strcmp(cmd, "/v1/custom_emojis") == 0) { /** **/
  1379. /* are you kidding me? */
  1380. *body = xs_dup("[]");
  1381. *ctype = "application/json";
  1382. status = 200;
  1383. }
  1384. else
  1385. if (strcmp(cmd, "/v1/instance") == 0) { /** **/
  1386. /* returns an instance object */
  1387. xs *ins = xs_dict_new();
  1388. const char *host = xs_dict_get(srv_config, "host");
  1389. const char *title = xs_dict_get(srv_config, "title");
  1390. const char *sdesc = xs_dict_get(srv_config, "short_description");
  1391. ins = xs_dict_append(ins, "uri", host);
  1392. ins = xs_dict_append(ins, "domain", host);
  1393. ins = xs_dict_append(ins, "title", title && *title ? title : host);
  1394. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  1395. ins = xs_dict_append(ins, "source_url", WHAT_IS_SNAC_URL);
  1396. ins = xs_dict_append(ins, "description", host);
  1397. ins = xs_dict_append(ins, "short_description", sdesc && *sdesc ? sdesc : host);
  1398. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  1399. ins = xs_dict_append(ins, "thumbnail", susie);
  1400. const char *v = xs_dict_get(srv_config, "admin_email");
  1401. if (xs_is_null(v) || *v == '\0')
  1402. v = "admin@localhost";
  1403. ins = xs_dict_append(ins, "email", v);
  1404. ins = xs_dict_append(ins, "rules", xs_stock_list);
  1405. xs *l1 = xs_list_append(xs_list_new(), "en");
  1406. ins = xs_dict_append(ins, "languages", l1);
  1407. ins = xs_dict_append(ins, "urls", xs_stock_dict);
  1408. xs *d2 = xs_dict_append(xs_dict_new(), "user_count", xs_stock_0);
  1409. d2 = xs_dict_append(d2, "status_count", xs_stock_0);
  1410. d2 = xs_dict_append(d2, "domain_count", xs_stock_0);
  1411. ins = xs_dict_append(ins, "stats", d2);
  1412. ins = xs_dict_append(ins, "registrations", xs_stock_false);
  1413. ins = xs_dict_append(ins, "approval_required", xs_stock_false);
  1414. ins = xs_dict_append(ins, "invites_enabled", xs_stock_false);
  1415. xs *cfg = xs_dict_new();
  1416. {
  1417. xs *d11 = xs_json_loads("{\"characters_reserved_per_url\":32,"
  1418. "\"max_characters\":100000,\"max_media_attachments\":8}");
  1419. cfg = xs_dict_append(cfg, "statuses", d11);
  1420. xs *d12 = xs_json_loads("{\"max_featured_tags\":10}");
  1421. cfg = xs_dict_append(cfg, "accounts", d12);
  1422. xs *d13 = xs_json_loads("{\"image_matrix_limit\":33177600,"
  1423. "\"image_size_limit\":16777216,"
  1424. "\"video_frame_rate_limit\":120,"
  1425. "\"video_matrix_limit\":8294400,"
  1426. "\"video_size_limit\":103809024}"
  1427. );
  1428. {
  1429. /* get the supported mime types from the internal list */
  1430. const char **p = xs_mime_types;
  1431. xs_set mtypes;
  1432. xs_set_init(&mtypes);
  1433. while (*p) {
  1434. const char *type = p[1];
  1435. if (xs_startswith(type, "image/") ||
  1436. xs_startswith(type, "video/") ||
  1437. xs_startswith(type, "audio/"))
  1438. xs_set_add(&mtypes, type);
  1439. p += 2;
  1440. }
  1441. xs *l = xs_set_result(&mtypes);
  1442. d13 = xs_dict_append(d13, "supported_mime_types", l);
  1443. }
  1444. cfg = xs_dict_append(cfg, "media_attachments", d13);
  1445. xs *d14 = xs_json_loads("{\"max_characters_per_option\":50,"
  1446. "\"max_expiration\":2629746,"
  1447. "\"max_options\":8,\"min_expiration\":300}");
  1448. cfg = xs_dict_append(cfg, "polls", d14);
  1449. }
  1450. ins = xs_dict_append(ins, "configuration", cfg);
  1451. const char *admin_account = xs_dict_get(srv_config, "admin_account");
  1452. if (!xs_is_null(admin_account) && *admin_account) {
  1453. snac admin;
  1454. if (user_open(&admin, admin_account)) {
  1455. xs *actor = msg_actor(&admin);
  1456. xs *acct = mastoapi_account(actor);
  1457. ins = xs_dict_append(ins, "contact_account", acct);
  1458. user_free(&admin);
  1459. }
  1460. }
  1461. *body = xs_json_dumps(ins, 4);
  1462. *ctype = "application/json";
  1463. status = 200;
  1464. }
  1465. else
  1466. if (xs_startswith(cmd, "/v1/statuses/")) { /** **/
  1467. /* information about a status */
  1468. if (logged_in) {
  1469. xs *l = xs_split(cmd, "/");
  1470. const char *id = xs_list_get(l, 3);
  1471. const char *op = xs_list_get(l, 4);
  1472. if (!xs_is_null(id)) {
  1473. xs *msg = NULL;
  1474. xs *out = NULL;
  1475. /* skip the 'fake' part of the id */
  1476. id = MID_TO_MD5(id);
  1477. if (valid_status(object_get_by_md5(id, &msg))) {
  1478. if (op == NULL) {
  1479. if (!is_muted(&snac1, get_atto(msg))) {
  1480. /* return the status itself */
  1481. out = mastoapi_status(&snac1, msg);
  1482. }
  1483. }
  1484. else
  1485. if (strcmp(op, "context") == 0) { /** **/
  1486. /* return ancestors and children */
  1487. xs *anc = xs_list_new();
  1488. xs *des = xs_list_new();
  1489. xs_list *p;
  1490. xs_str *v;
  1491. char pid[64];
  1492. /* build the [grand]parent list, moving up */
  1493. strncpy(pid, id, sizeof(pid));
  1494. while (object_parent(pid, pid, sizeof(pid))) {
  1495. xs *m2 = NULL;
  1496. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  1497. xs *st = mastoapi_status(&snac1, m2);
  1498. if (st)
  1499. anc = xs_list_insert(anc, 0, st);
  1500. }
  1501. else
  1502. break;
  1503. }
  1504. /* build the children list */
  1505. xs *children = object_children(xs_dict_get(msg, "id"));
  1506. p = children;
  1507. while (xs_list_iter(&p, &v)) {
  1508. xs *m2 = NULL;
  1509. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  1510. if (xs_is_null(xs_dict_get(m2, "name"))) {
  1511. xs *st = mastoapi_status(&snac1, m2);
  1512. if (st)
  1513. des = xs_list_append(des, st);
  1514. }
  1515. }
  1516. }
  1517. out = xs_dict_new();
  1518. out = xs_dict_append(out, "ancestors", anc);
  1519. out = xs_dict_append(out, "descendants", des);
  1520. }
  1521. else
  1522. if (strcmp(op, "reblogged_by") == 0 || /** **/
  1523. strcmp(op, "favourited_by") == 0) { /** **/
  1524. /* return the list of people who liked or boosted this */
  1525. out = xs_list_new();
  1526. xs *l = NULL;
  1527. if (op[0] == 'r')
  1528. l = object_announces(xs_dict_get(msg, "id"));
  1529. else
  1530. l = object_likes(xs_dict_get(msg, "id"));
  1531. xs_list *p = l;
  1532. xs_str *v;
  1533. while (xs_list_iter(&p, &v)) {
  1534. xs *actor2 = NULL;
  1535. if (valid_status(object_get_by_md5(v, &actor2))) {
  1536. xs *acct2 = mastoapi_account(actor2);
  1537. out = xs_list_append(out, acct2);
  1538. }
  1539. }
  1540. }
  1541. else
  1542. if (strcmp(op, "source") == 0) { /** **/
  1543. out = xs_dict_new();
  1544. /* get the mastoapi status id */
  1545. out = xs_dict_append(out, "id", xs_list_get(l, 3));
  1546. out = xs_dict_append(out, "text", xs_dict_get(msg, "sourceContent"));
  1547. out = xs_dict_append(out, "spoiler_text", xs_dict_get(msg, "summary"));
  1548. }
  1549. }
  1550. else
  1551. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  1552. if (out != NULL) {
  1553. *body = xs_json_dumps(out, 4);
  1554. *ctype = "application/json";
  1555. status = 200;
  1556. }
  1557. }
  1558. }
  1559. else
  1560. status = 401;
  1561. }
  1562. else
  1563. if (strcmp(cmd, "/v1/preferences") == 0) { /** **/
  1564. *body = xs_dup("{}");
  1565. *ctype = "application/json";
  1566. status = 200;
  1567. }
  1568. else
  1569. if (strcmp(cmd, "/v1/markers") == 0) { /** **/
  1570. *body = xs_dup("{}");
  1571. *ctype = "application/json";
  1572. status = 200;
  1573. }
  1574. else
  1575. if (strcmp(cmd, "/v1/followed_tags") == 0) { /** **/
  1576. *body = xs_dup("[]");
  1577. *ctype = "application/json";
  1578. status = 200;
  1579. }
  1580. else
  1581. if (strcmp(cmd, "/v2/search") == 0) { /** **/
  1582. if (logged_in) {
  1583. const char *q = xs_dict_get(args, "q");
  1584. const char *type = xs_dict_get(args, "type");
  1585. const char *offset = xs_dict_get(args, "offset");
  1586. xs *acl = xs_list_new();
  1587. xs *stl = xs_list_new();
  1588. xs *htl = xs_list_new();
  1589. xs *res = xs_dict_new();
  1590. if (xs_is_null(offset) || strcmp(offset, "0") == 0) {
  1591. /* reply something only for offset 0; otherwise,
  1592. apps like Tusky keep asking again and again */
  1593. if (!xs_is_null(q) && !xs_is_null(type) && strcmp(type, "accounts") == 0) {
  1594. /* do a webfinger query */
  1595. char *actor = NULL;
  1596. char *user = NULL;
  1597. if (valid_status(webfinger_request(q, &actor, &user) && actor)) {
  1598. xs *actor_o = NULL;
  1599. if (valid_status(actor_request(&snac1, actor, &actor_o))) {
  1600. xs *acct = mastoapi_account(actor_o);
  1601. acl = xs_list_append(acl, acct);
  1602. }
  1603. }
  1604. }
  1605. }
  1606. res = xs_dict_append(res, "accounts", acl);
  1607. res = xs_dict_append(res, "statuses", stl);
  1608. res = xs_dict_append(res, "hashtags", htl);
  1609. *body = xs_json_dumps(res, 4);
  1610. *ctype = "application/json";
  1611. status = 200;
  1612. }
  1613. else
  1614. status = 401;
  1615. }
  1616. /* user cleanup */
  1617. if (logged_in)
  1618. user_free(&snac1);
  1619. srv_debug(1, xs_fmt("mastoapi_get_handler %s %d", q_path, status));
  1620. return status;
  1621. }
  1622. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  1623. const char *payload, int p_size,
  1624. char **body, int *b_size, char **ctype)
  1625. {
  1626. (void)p_size;
  1627. (void)b_size;
  1628. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1629. return 0;
  1630. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1631. int status = 404;
  1632. xs *args = NULL;
  1633. char *i_ctype = xs_dict_get(req, "content-type");
  1634. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  1635. if (!xs_is_null(payload))
  1636. args = xs_json_loads(payload);
  1637. }
  1638. else if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded"))
  1639. {
  1640. // Some apps send form data instead of json so we should cater for those
  1641. if (!xs_is_null(payload)) {
  1642. xs *upl = xs_url_dec(payload);
  1643. args = xs_url_vars(upl);
  1644. }
  1645. }
  1646. else
  1647. args = xs_dup(xs_dict_get(req, "p_vars"));
  1648. if (args == NULL)
  1649. return 400;
  1650. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1651. snac snac = {0};
  1652. int logged_in = process_auth_token(&snac, req);
  1653. if (strcmp(cmd, "/v1/apps") == 0) { /** **/
  1654. const char *name = xs_dict_get(args, "client_name");
  1655. const char *ruri = xs_dict_get(args, "redirect_uris");
  1656. const char *scope = xs_dict_get(args, "scope");
  1657. if (xs_type(ruri) == XSTYPE_LIST)
  1658. ruri = xs_dict_get(ruri, 0);
  1659. if (name && ruri) {
  1660. xs *app = xs_dict_new();
  1661. xs *id = xs_replace_i(tid(0), ".", "");
  1662. xs *csec = random_str();
  1663. xs *vkey = random_str();
  1664. xs *cid = NULL;
  1665. /* pick a non-existent random cid */
  1666. for (;;) {
  1667. cid = random_str();
  1668. xs *p_app = app_get(cid);
  1669. if (p_app == NULL)
  1670. break;
  1671. xs_free(cid);
  1672. }
  1673. app = xs_dict_append(app, "name", name);
  1674. app = xs_dict_append(app, "redirect_uri", ruri);
  1675. app = xs_dict_append(app, "client_id", cid);
  1676. app = xs_dict_append(app, "client_secret", csec);
  1677. app = xs_dict_append(app, "vapid_key", vkey);
  1678. app = xs_dict_append(app, "id", id);
  1679. *body = xs_json_dumps(app, 4);
  1680. *ctype = "application/json";
  1681. status = 200;
  1682. app = xs_dict_append(app, "code", "");
  1683. if (scope)
  1684. app = xs_dict_append(app, "scope", scope);
  1685. app_add(cid, app);
  1686. srv_debug(1, xs_fmt("mastoapi apps: new app %s", cid));
  1687. }
  1688. }
  1689. else
  1690. if (strcmp(cmd, "/v1/statuses") == 0) { /** **/
  1691. if (logged_in) {
  1692. /* post a new Note */
  1693. const char *content = xs_dict_get(args, "status");
  1694. const char *mid = xs_dict_get(args, "in_reply_to_id");
  1695. const char *visibility = xs_dict_get(args, "visibility");
  1696. const char *summary = xs_dict_get(args, "spoiler_text");
  1697. const char *media_ids = xs_dict_get(args, "media_ids");
  1698. if (xs_is_null(media_ids))
  1699. media_ids = xs_dict_get(args, "media_ids[]");
  1700. if (xs_is_null(media_ids))
  1701. media_ids = xs_dict_get(args, "media_ids");
  1702. if (xs_is_null(visibility))
  1703. visibility = "public";
  1704. xs *attach_list = xs_list_new();
  1705. xs *irt = NULL;
  1706. /* is it a reply? */
  1707. if (mid != NULL) {
  1708. xs *r_msg = NULL;
  1709. const char *md5 = MID_TO_MD5(mid);
  1710. if (valid_status(object_get_by_md5(md5, &r_msg)))
  1711. irt = xs_dup(xs_dict_get(r_msg, "id"));
  1712. }
  1713. /* does it have attachments? */
  1714. if (!xs_is_null(media_ids)) {
  1715. xs *mi = NULL;
  1716. if (xs_type(media_ids) == XSTYPE_LIST)
  1717. mi = xs_dup(media_ids);
  1718. else {
  1719. mi = xs_list_new();
  1720. mi = xs_list_append(mi, media_ids);
  1721. }
  1722. xs_list *p = mi;
  1723. xs_str *v;
  1724. while (xs_list_iter(&p, &v)) {
  1725. xs *l = xs_list_new();
  1726. xs *url = xs_fmt("%s/s/%s", snac.actor, v);
  1727. xs *desc = static_get_meta(&snac, v);
  1728. l = xs_list_append(l, url);
  1729. l = xs_list_append(l, desc);
  1730. attach_list = xs_list_append(attach_list, l);
  1731. }
  1732. }
  1733. /* prepare the message */
  1734. xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
  1735. strcmp(visibility, "public") == 0 ? 0 : 1);
  1736. if (!xs_is_null(summary) && *summary) {
  1737. msg = xs_dict_set(msg, "sensitive", xs_stock_true);
  1738. msg = xs_dict_set(msg, "summary", summary);
  1739. }
  1740. /* store */
  1741. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1742. /* 'Create' message */
  1743. xs *c_msg = msg_create(&snac, msg);
  1744. enqueue_message(&snac, c_msg);
  1745. timeline_touch(&snac);
  1746. /* convert to a mastodon status as a response code */
  1747. xs *st = mastoapi_status(&snac, msg);
  1748. *body = xs_json_dumps(st, 4);
  1749. *ctype = "application/json";
  1750. status = 200;
  1751. }
  1752. else
  1753. status = 401;
  1754. }
  1755. else
  1756. if (xs_startswith(cmd, "/v1/statuses")) { /** **/
  1757. if (logged_in) {
  1758. /* operations on a status */
  1759. xs *l = xs_split(cmd, "/");
  1760. const char *mid = xs_list_get(l, 3);
  1761. const char *op = xs_list_get(l, 4);
  1762. if (!xs_is_null(mid)) {
  1763. xs *msg = NULL;
  1764. xs *out = NULL;
  1765. /* skip the 'fake' part of the id */
  1766. mid = MID_TO_MD5(mid);
  1767. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1768. char *id = xs_dict_get(msg, "id");
  1769. if (op == NULL) {
  1770. /* no operation (?) */
  1771. }
  1772. else
  1773. if (strcmp(op, "favourite") == 0) { /** **/
  1774. xs *n_msg = msg_admiration(&snac, id, "Like");
  1775. if (n_msg != NULL) {
  1776. enqueue_message(&snac, n_msg);
  1777. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  1778. out = mastoapi_status(&snac, msg);
  1779. }
  1780. }
  1781. else
  1782. if (strcmp(op, "unfavourite") == 0) { /** **/
  1783. /* partial support: as the original Like message
  1784. is not stored anywhere here, it's not possible
  1785. to send an Undo + Like; the only thing done here
  1786. is to delete the actor from the list of likes */
  1787. object_unadmire(id, snac.actor, 1);
  1788. }
  1789. else
  1790. if (strcmp(op, "reblog") == 0) { /** **/
  1791. xs *n_msg = msg_admiration(&snac, id, "Announce");
  1792. if (n_msg != NULL) {
  1793. enqueue_message(&snac, n_msg);
  1794. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  1795. out = mastoapi_status(&snac, msg);
  1796. }
  1797. }
  1798. else
  1799. if (strcmp(op, "unreblog") == 0) { /** **/
  1800. /* partial support: see comment in 'unfavourite' */
  1801. object_unadmire(id, snac.actor, 0);
  1802. }
  1803. else
  1804. if (strcmp(op, "bookmark") == 0) { /** **/
  1805. /* snac does not support bookmarks */
  1806. }
  1807. else
  1808. if (strcmp(op, "unbookmark") == 0) { /** **/
  1809. /* snac does not support bookmarks */
  1810. }
  1811. else
  1812. if (strcmp(op, "pin") == 0) { /** **/
  1813. /* pin this message */
  1814. if (pin(&snac, id))
  1815. out = mastoapi_status(&snac, msg);
  1816. else
  1817. status = 422;
  1818. }
  1819. else
  1820. if (strcmp(op, "unpin") == 0) { /** **/
  1821. /* unpin this message */
  1822. unpin(&snac, id);
  1823. out = mastoapi_status(&snac, msg);
  1824. }
  1825. else
  1826. if (strcmp(op, "mute") == 0) { /** **/
  1827. /* Mastodon's mute is snac's hide */
  1828. }
  1829. else
  1830. if (strcmp(op, "unmute") == 0) { /** **/
  1831. /* Mastodon's unmute is snac's unhide */
  1832. }
  1833. }
  1834. if (out != NULL) {
  1835. *body = xs_json_dumps(out, 4);
  1836. *ctype = "application/json";
  1837. status = 200;
  1838. }
  1839. }
  1840. }
  1841. else
  1842. status = 401;
  1843. }
  1844. else
  1845. if (strcmp(cmd, "/v1/notifications/clear") == 0) { /** **/
  1846. if (logged_in) {
  1847. notify_clear(&snac);
  1848. timeline_touch(&snac);
  1849. *body = xs_dup("{}");
  1850. *ctype = "application/json";
  1851. status = 200;
  1852. }
  1853. else
  1854. status = 401;
  1855. }
  1856. else
  1857. if (strcmp(cmd, "/v1/push/subscription") == 0) { /** **/
  1858. /* I don't know what I'm doing */
  1859. if (logged_in) {
  1860. char *v;
  1861. xs *wpush = xs_dict_new();
  1862. wpush = xs_dict_append(wpush, "id", "1");
  1863. v = xs_dict_get(args, "data");
  1864. v = xs_dict_get(v, "alerts");
  1865. wpush = xs_dict_append(wpush, "alerts", v);
  1866. v = xs_dict_get(args, "subscription");
  1867. v = xs_dict_get(v, "endpoint");
  1868. wpush = xs_dict_append(wpush, "endpoint", v);
  1869. xs *server_key = random_str();
  1870. wpush = xs_dict_append(wpush, "server_key", server_key);
  1871. *body = xs_json_dumps(wpush, 4);
  1872. *ctype = "application/json";
  1873. status = 200;
  1874. }
  1875. else
  1876. status = 401;
  1877. }
  1878. else
  1879. if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { /** **/
  1880. if (logged_in) {
  1881. const xs_list *file = xs_dict_get(args, "file");
  1882. const char *desc = xs_dict_get(args, "description");
  1883. if (xs_is_null(desc))
  1884. desc = "";
  1885. status = 400;
  1886. if (xs_type(file) == XSTYPE_LIST) {
  1887. const char *fn = xs_list_get(file, 0);
  1888. if (*fn != '\0') {
  1889. char *ext = strrchr(fn, '.');
  1890. xs *hash = xs_md5_hex(fn, strlen(fn));
  1891. xs *id = xs_fmt("%s%s", hash, ext);
  1892. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1893. int fo = xs_number_get(xs_list_get(file, 1));
  1894. int fs = xs_number_get(xs_list_get(file, 2));
  1895. /* store */
  1896. static_put(&snac, id, payload + fo, fs);
  1897. static_put_meta(&snac, id, desc);
  1898. /* prepare a response */
  1899. xs *rsp = xs_dict_new();
  1900. rsp = xs_dict_append(rsp, "id", id);
  1901. rsp = xs_dict_append(rsp, "type", "image");
  1902. rsp = xs_dict_append(rsp, "url", url);
  1903. rsp = xs_dict_append(rsp, "preview_url", url);
  1904. rsp = xs_dict_append(rsp, "remote_url", url);
  1905. rsp = xs_dict_append(rsp, "description", desc);
  1906. *body = xs_json_dumps(rsp, 4);
  1907. *ctype = "application/json";
  1908. status = 200;
  1909. }
  1910. }
  1911. }
  1912. else
  1913. status = 401;
  1914. }
  1915. else
  1916. if (xs_startswith(cmd, "/v1/accounts")) { /** **/
  1917. if (logged_in) {
  1918. /* account-related information */
  1919. xs *l = xs_split(cmd, "/");
  1920. const char *md5 = xs_list_get(l, 3);
  1921. const char *opt = xs_list_get(l, 4);
  1922. xs *rsp = NULL;
  1923. if (!xs_is_null(md5) && *md5) {
  1924. xs *actor_o = NULL;
  1925. if (xs_is_null(opt)) {
  1926. /* ? */
  1927. }
  1928. else
  1929. if (strcmp(opt, "follow") == 0) { /** **/
  1930. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1931. const char *actor = xs_dict_get(actor_o, "id");
  1932. xs *msg = msg_follow(&snac, actor);
  1933. if (msg != NULL) {
  1934. /* reload the actor from the message, in may be different */
  1935. actor = xs_dict_get(msg, "object");
  1936. following_add(&snac, actor, msg);
  1937. enqueue_output_by_actor(&snac, msg, actor, 0);
  1938. rsp = mastoapi_relationship(&snac, md5);
  1939. }
  1940. }
  1941. }
  1942. else
  1943. if (strcmp(opt, "unfollow") == 0) { /** **/
  1944. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1945. const char *actor = xs_dict_get(actor_o, "id");
  1946. /* get the following object */
  1947. xs *object = NULL;
  1948. if (valid_status(following_get(&snac, actor, &object))) {
  1949. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  1950. following_del(&snac, actor);
  1951. enqueue_output_by_actor(&snac, msg, actor, 0);
  1952. rsp = mastoapi_relationship(&snac, md5);
  1953. }
  1954. }
  1955. }
  1956. else
  1957. if (strcmp(opt, "block") == 0) { /** **/
  1958. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1959. const char *actor = xs_dict_get(actor_o, "id");
  1960. mute(&snac, actor);
  1961. rsp = mastoapi_relationship(&snac, md5);
  1962. }
  1963. }
  1964. else
  1965. if (strcmp(opt, "unblock") == 0) { /** **/
  1966. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1967. const char *actor = xs_dict_get(actor_o, "id");
  1968. unmute(&snac, actor);
  1969. rsp = mastoapi_relationship(&snac, md5);
  1970. }
  1971. }
  1972. }
  1973. if (rsp != NULL) {
  1974. *body = xs_json_dumps(rsp, 4);
  1975. *ctype = "application/json";
  1976. status = 200;
  1977. }
  1978. }
  1979. else
  1980. status = 401;
  1981. }
  1982. else
  1983. if (xs_startswith(cmd, "/v1/polls")) { /** **/
  1984. if (logged_in) {
  1985. /* operations on a status */
  1986. xs *l = xs_split(cmd, "/");
  1987. const char *mid = xs_list_get(l, 3);
  1988. const char *op = xs_list_get(l, 4);
  1989. if (!xs_is_null(mid)) {
  1990. xs *msg = NULL;
  1991. xs *out = NULL;
  1992. /* skip the 'fake' part of the id */
  1993. mid = MID_TO_MD5(mid);
  1994. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1995. const char *id = xs_dict_get(msg, "id");
  1996. const char *atto = get_atto(msg);
  1997. xs_list *opts = xs_dict_get(msg, "oneOf");
  1998. if (opts == NULL)
  1999. opts = xs_dict_get(msg, "anyOf");
  2000. if (op == NULL) {
  2001. }
  2002. else
  2003. if (strcmp(op, "votes") == 0) {
  2004. xs_list *choices = xs_dict_get(args, "choices[]");
  2005. if (xs_is_null(choices))
  2006. choices = xs_dict_get(args, "choices");
  2007. if (xs_type(choices) == XSTYPE_LIST) {
  2008. xs_str *v;
  2009. while (xs_list_iter(&choices, &v)) {
  2010. int io = atoi(v);
  2011. const xs_dict *o = xs_list_get(opts, io);
  2012. if (o) {
  2013. const char *name = xs_dict_get(o, "name");
  2014. xs *msg = msg_note(&snac, "", atto, (char *)id, NULL, 1);
  2015. msg = xs_dict_append(msg, "name", name);
  2016. xs *c_msg = msg_create(&snac, msg);
  2017. enqueue_message(&snac, c_msg);
  2018. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  2019. }
  2020. }
  2021. out = mastoapi_poll(&snac, msg);
  2022. }
  2023. }
  2024. }
  2025. if (out != NULL) {
  2026. *body = xs_json_dumps(out, 4);
  2027. *ctype = "application/json";
  2028. status = 200;
  2029. }
  2030. }
  2031. }
  2032. else
  2033. status = 401;
  2034. }
  2035. /* user cleanup */
  2036. if (logged_in)
  2037. user_free(&snac);
  2038. return status;
  2039. }
  2040. int mastoapi_delete_handler(const xs_dict *req, const char *q_path,
  2041. char **body, int *b_size, char **ctype) {
  2042. (void)req;
  2043. (void)body;
  2044. (void)b_size;
  2045. (void)ctype;
  2046. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  2047. return 0;
  2048. srv_debug(1, xs_fmt("mastoapi_delete_handler %s", q_path));
  2049. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2050. if (xs_startswith(cmd, "/v1/push/subscription") || xs_startswith(cmd, "/v2/push/subscription")) { /** **/
  2051. // pretend we deleted it, since it doesn't exist anyway
  2052. return 200;
  2053. }
  2054. return 0;
  2055. }
  2056. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  2057. const char *payload, int p_size,
  2058. char **body, int *b_size, char **ctype)
  2059. {
  2060. (void)p_size;
  2061. (void)b_size;
  2062. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  2063. return 0;
  2064. int status = 404;
  2065. xs *args = NULL;
  2066. char *i_ctype = xs_dict_get(req, "content-type");
  2067. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  2068. if (!xs_is_null(payload))
  2069. args = xs_json_loads(payload);
  2070. }
  2071. else
  2072. args = xs_dup(xs_dict_get(req, "p_vars"));
  2073. if (args == NULL)
  2074. return 400;
  2075. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2076. snac snac = {0};
  2077. int logged_in = process_auth_token(&snac, req);
  2078. if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) { /** **/
  2079. if (logged_in) {
  2080. xs *l = xs_split(cmd, "/");
  2081. const char *stid = xs_list_get(l, 3);
  2082. if (!xs_is_null(stid)) {
  2083. const char *desc = xs_dict_get(args, "description");
  2084. /* set the image metadata */
  2085. static_put_meta(&snac, stid, desc);
  2086. /* prepare a response */
  2087. xs *rsp = xs_dict_new();
  2088. xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
  2089. rsp = xs_dict_append(rsp, "id", stid);
  2090. rsp = xs_dict_append(rsp, "type", "image");
  2091. rsp = xs_dict_append(rsp, "url", url);
  2092. rsp = xs_dict_append(rsp, "preview_url", url);
  2093. rsp = xs_dict_append(rsp, "remote_url", url);
  2094. rsp = xs_dict_append(rsp, "description", desc);
  2095. *body = xs_json_dumps(rsp, 4);
  2096. *ctype = "application/json";
  2097. status = 200;
  2098. }
  2099. }
  2100. else
  2101. status = 401;
  2102. }
  2103. else
  2104. if (xs_startswith(cmd, "/v1/statuses")) {
  2105. if (logged_in) {
  2106. xs *l = xs_split(cmd, "/");
  2107. const char *mid = xs_list_get(l, 3);
  2108. if (!xs_is_null(mid)) {
  2109. const char *md5 = MID_TO_MD5(mid);
  2110. xs *rsp = NULL;
  2111. xs *msg = NULL;
  2112. if (valid_status(timeline_get_by_md5(&snac, md5, &msg))) {
  2113. const char *content = xs_dict_get(args, "status");
  2114. xs *atls = xs_list_new();
  2115. xs *f_content = not_really_markdown(content, &atls);
  2116. /* replace fields with new content */
  2117. msg = xs_dict_set(msg, "sourceContent", content);
  2118. msg = xs_dict_set(msg, "content", f_content);
  2119. xs *updated = xs_str_utctime(0, ISO_DATE_SPEC);
  2120. msg = xs_dict_set(msg, "updated", updated);
  2121. /* overwrite object, not updating the indexes */
  2122. object_add_ow(xs_dict_get(msg, "id"), msg);
  2123. /* update message */
  2124. xs *c_msg = msg_update(&snac, msg);
  2125. enqueue_message(&snac, c_msg);
  2126. rsp = mastoapi_status(&snac, msg);
  2127. }
  2128. if (rsp != NULL) {
  2129. *body = xs_json_dumps(rsp, 4);
  2130. *ctype = "application/json";
  2131. status = 200;
  2132. }
  2133. }
  2134. }
  2135. else
  2136. status = 401;
  2137. }
  2138. /* user cleanup */
  2139. if (logged_in)
  2140. user_free(&snac);
  2141. srv_debug(1, xs_fmt("mastoapi_put_handler %s %d", q_path, status));
  2142. return status;
  2143. }
  2144. void mastoapi_purge(void)
  2145. {
  2146. xs *spec = xs_fmt("%s/app/" "*.json", srv_basedir);
  2147. xs *files = xs_glob(spec, 1, 0);
  2148. xs_list *p = files;
  2149. xs_str *v;
  2150. time_t mt = time(NULL) - 3600;
  2151. while (xs_list_iter(&p, &v)) {
  2152. xs *cid = xs_replace(v, ".json", "");
  2153. xs *fn = _app_fn(cid);
  2154. if (mtime(fn) < mt) {
  2155. /* get the app */
  2156. xs *app = app_get(cid);
  2157. if (app) {
  2158. /* old apps with no uid are incomplete cruft */
  2159. const char *uid = xs_dict_get(app, "uid");
  2160. if (xs_is_null(uid) || *uid == '\0') {
  2161. unlink(fn);
  2162. srv_debug(2, xs_fmt("purged %s", fn));
  2163. }
  2164. }
  2165. }
  2166. }
  2167. }
  2168. #endif /* #ifndef NO_MASTODON_API */