mastoapi.c 76 KB

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