mastoapi.c 79 KB

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