mastoapi.c 78 KB

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