mastoapi.c 83 KB

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