mastoapi.c 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479
  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. /* get the last person who boosted this */
  688. xs *boosted_by_md5 = NULL;
  689. if (xs_list_len(idx))
  690. boosted_by_md5 = xs_dup(xs_list_get(idx, -1));
  691. xs_free(idx);
  692. xs_free(ixc);
  693. idx = object_children(id);
  694. ixc = xs_number_new(xs_list_len(idx));
  695. st = xs_dict_append(st, "replies_count", ixc);
  696. /* default in_reply_to values */
  697. st = xs_dict_append(st, "in_reply_to_id", xs_stock_null);
  698. st = xs_dict_append(st, "in_reply_to_account_id", xs_stock_null);
  699. tmp = xs_dict_get(msg, "inReplyTo");
  700. if (!xs_is_null(tmp)) {
  701. xs *irto = NULL;
  702. if (valid_status(object_get(tmp, &irto))) {
  703. xs *irt_mid = mastoapi_id(irto);
  704. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  705. char *at = NULL;
  706. if (!xs_is_null(at = xs_dict_get(irto, "attributedTo"))) {
  707. xs *at_md5 = xs_md5_hex(at, strlen(at));
  708. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  709. }
  710. }
  711. }
  712. st = xs_dict_append(st, "reblog", xs_stock_null);
  713. st = xs_dict_append(st, "card", xs_stock_null);
  714. st = xs_dict_append(st, "language", xs_stock_null);
  715. tmp = xs_dict_get(msg, "sourceContent");
  716. if (xs_is_null(tmp))
  717. tmp = "";
  718. st = xs_dict_append(st, "text", tmp);
  719. tmp = xs_dict_get(msg, "updated");
  720. if (xs_is_null(tmp))
  721. tmp = xs_stock_null;
  722. st = xs_dict_append(st, "edited_at", tmp);
  723. if (strcmp(type, "Question") == 0) {
  724. xs *poll = mastoapi_poll(snac, msg);
  725. st = xs_dict_append(st, "poll", poll);
  726. }
  727. else
  728. st = xs_dict_append(st, "poll", xs_stock_null);
  729. st = xs_dict_append(st, "bookmarked", xs_stock_false);
  730. st = xs_dict_append(st, "pinned",
  731. (snac && is_pinned(snac, id)) ? xs_stock_true : xs_stock_false);
  732. /* is it a boost? */
  733. if (!xs_is_null(boosted_by_md5)) {
  734. /* create a new dummy status, using st as the 'reblog' field */
  735. xs_dict *bst = xs_dict_new();
  736. xs *b_actor = NULL;
  737. if (valid_status(object_get_by_md5(boosted_by_md5, &b_actor))) {
  738. xs *b_acct = mastoapi_account(b_actor);
  739. xs *fake_uri = xs_fmt("%s/d/%s", srv_baseurl, mid);
  740. bst = xs_dict_append(bst, "id", mid);
  741. bst = xs_dict_append(bst, "created_at", xs_dict_get(st, "created_at"));
  742. bst = xs_dict_append(bst, "account", b_acct);
  743. bst = xs_dict_append(bst, "uri", fake_uri);
  744. bst = xs_dict_append(bst, "content", "");
  745. bst = xs_dict_append(bst, "reblog", st);
  746. xs_free(st);
  747. st = bst;
  748. }
  749. }
  750. return st;
  751. }
  752. xs_dict *mastoapi_relationship(snac *snac, const char *md5)
  753. {
  754. xs_dict *rel = NULL;
  755. xs *actor_o = NULL;
  756. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  757. rel = xs_dict_new();
  758. const char *actor = xs_dict_get(actor_o, "id");
  759. rel = xs_dict_append(rel, "id", md5);
  760. rel = xs_dict_append(rel, "following",
  761. following_check(snac, actor) ? xs_stock_true : xs_stock_false);
  762. rel = xs_dict_append(rel, "showing_reblogs", xs_stock_true);
  763. rel = xs_dict_append(rel, "notifying", xs_stock_false);
  764. rel = xs_dict_append(rel, "followed_by",
  765. follower_check(snac, actor) ? xs_stock_true : xs_stock_false);
  766. rel = xs_dict_append(rel, "blocking",
  767. is_muted(snac, actor) ? xs_stock_true : xs_stock_false);
  768. rel = xs_dict_append(rel, "muting", xs_stock_false);
  769. rel = xs_dict_append(rel, "muting_notifications", xs_stock_false);
  770. rel = xs_dict_append(rel, "requested", xs_stock_false);
  771. rel = xs_dict_append(rel, "domain_blocking", xs_stock_false);
  772. rel = xs_dict_append(rel, "endorsed", xs_stock_false);
  773. rel = xs_dict_append(rel, "note", "");
  774. }
  775. return rel;
  776. }
  777. int process_auth_token(snac *snac, const xs_dict *req)
  778. /* processes an authorization token, if there is one */
  779. {
  780. int logged_in = 0;
  781. char *v;
  782. /* if there is an authorization field, try to validate it */
  783. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  784. xs *tokid = xs_replace_n(v, "Bearer ", "", 1);
  785. xs *token = token_get(tokid);
  786. if (token != NULL) {
  787. const char *uid = xs_dict_get(token, "uid");
  788. if (!xs_is_null(uid) && user_open(snac, uid)) {
  789. logged_in = 1;
  790. /* this counts as a 'login' */
  791. lastlog_write(snac, "mastoapi");
  792. srv_debug(2, xs_fmt("mastoapi auth: valid token for user %s", uid));
  793. }
  794. else
  795. srv_log(xs_fmt("mastoapi auth: corrupted token %s", tokid));
  796. }
  797. else
  798. srv_log(xs_fmt("mastoapi auth: invalid token %s", tokid));
  799. }
  800. return logged_in;
  801. }
  802. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  803. char **body, int *b_size, char **ctype)
  804. {
  805. (void)b_size;
  806. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  807. return 0;
  808. srv_debug(1, xs_fmt("mastoapi_get_handler %s", q_path));
  809. int status = 404;
  810. xs_dict *args = xs_dict_get(req, "q_vars");
  811. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  812. snac snac1 = {0};
  813. int logged_in = process_auth_token(&snac1, req);
  814. if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) { /** **/
  815. if (logged_in) {
  816. xs *acct = xs_dict_new();
  817. acct = xs_dict_append(acct, "id", snac1.md5);
  818. acct = xs_dict_append(acct, "username", xs_dict_get(snac1.config, "uid"));
  819. acct = xs_dict_append(acct, "acct", xs_dict_get(snac1.config, "uid"));
  820. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac1.config, "name"));
  821. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac1.config, "published"));
  822. acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio"));
  823. acct = xs_dict_append(acct, "url", snac1.actor);
  824. acct = xs_dict_append(acct, "header", "");
  825. xs *src = xs_json_loads("{\"privacy\":\"public\","
  826. "\"sensitive\":false,\"fields\":[],\"note\":\"\"}");
  827. acct = xs_dict_append(acct, "source", src);
  828. xs *avatar = NULL;
  829. char *av = xs_dict_get(snac1.config, "avatar");
  830. if (xs_is_null(av) || *av == '\0')
  831. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  832. else
  833. avatar = xs_dup(av);
  834. acct = xs_dict_append(acct, "avatar", avatar);
  835. acct = xs_dict_append(acct, "avatar_static", avatar);
  836. xs_dict *metadata = xs_dict_get(snac1.config, "metadata");
  837. if (xs_type(metadata) == XSTYPE_DICT) {
  838. xs *fields = xs_list_new();
  839. xs_str *k;
  840. xs_str *v;
  841. while (xs_dict_iter(&metadata, &k, &v)) {
  842. xs *d = xs_dict_new();
  843. d = xs_dict_append(d, "name", k);
  844. d = xs_dict_append(d, "value", v);
  845. d = xs_dict_append(d, "verified_at", xs_stock_null);
  846. fields = xs_list_append(fields, d);
  847. }
  848. acct = xs_dict_set(acct, "fields", fields);
  849. }
  850. *body = xs_json_dumps(acct, 4);
  851. *ctype = "application/json";
  852. status = 200;
  853. }
  854. else {
  855. status = 422; // "Unprocessable entity" (no login)
  856. }
  857. }
  858. else
  859. if (strcmp(cmd, "/v1/accounts/relationships") == 0) { /** **/
  860. /* find if an account is followed, blocked, etc. */
  861. /* the account to get relationships about is in args "id[]" */
  862. if (logged_in) {
  863. xs *res = xs_list_new();
  864. const char *md5 = xs_dict_get(args, "id[]");
  865. if (xs_is_null(md5))
  866. md5 = xs_dict_get(args, "id");
  867. if (!xs_is_null(md5)) {
  868. if (xs_type(md5) == XSTYPE_LIST)
  869. md5 = xs_list_get(md5, 0);
  870. xs *rel = mastoapi_relationship(&snac1, md5);
  871. if (rel != NULL)
  872. res = xs_list_append(res, rel);
  873. }
  874. *body = xs_json_dumps(res, 4);
  875. *ctype = "application/json";
  876. status = 200;
  877. }
  878. else
  879. status = 422;
  880. }
  881. else
  882. if (xs_startswith(cmd, "/v1/accounts/")) { /** **/
  883. /* account-related information */
  884. xs *l = xs_split(cmd, "/");
  885. const char *uid = xs_list_get(l, 3);
  886. const char *opt = xs_list_get(l, 4);
  887. if (uid != NULL) {
  888. snac snac2;
  889. xs *out = NULL;
  890. xs *actor = NULL;
  891. if (logged_in && strcmp(uid, "search") == 0) { /** **/
  892. /* search for accounts starting with q */
  893. const char *aq = xs_dict_get(args, "q");
  894. if (!xs_is_null(aq)) {
  895. xs *q = xs_tolower_i(xs_dup(aq));
  896. out = xs_list_new();
  897. xs *wing = following_list(&snac1);
  898. xs *wers = follower_list(&snac1);
  899. xs *ulst = user_list();
  900. xs_list *p;
  901. xs_str *v;
  902. xs_set seen;
  903. xs_set_init(&seen);
  904. /* user relations */
  905. xs_list *lsts[] = { wing, wers, NULL };
  906. int n;
  907. for (n = 0; (p = lsts[n]) != NULL; n++) {
  908. while (xs_list_iter(&p, &v)) {
  909. /* already seen? skip */
  910. if (xs_set_add(&seen, v) == 0)
  911. continue;
  912. xs *actor = NULL;
  913. if (valid_status(object_get(v, &actor))) {
  914. const char *uname = xs_dict_get(actor, "preferredUsername");
  915. if (!xs_is_null(uname)) {
  916. xs *luname = xs_tolower_i(xs_dup(uname));
  917. if (xs_startswith(luname, q)) {
  918. xs *acct = mastoapi_account(actor);
  919. out = xs_list_append(out, acct);
  920. }
  921. }
  922. }
  923. }
  924. }
  925. /* local users */
  926. p = ulst;
  927. while (xs_list_iter(&p, &v)) {
  928. snac user;
  929. /* skip this same user */
  930. if (strcmp(v, xs_dict_get(snac1.config, "uid")) == 0)
  931. continue;
  932. /* skip if the uid does not start with the query */
  933. xs *v2 = xs_tolower_i(xs_dup(v));
  934. if (!xs_startswith(v2, q))
  935. continue;
  936. if (user_open(&user, v)) {
  937. /* if it's not already seen, add it */
  938. if (xs_set_add(&seen, user.actor) == 1) {
  939. xs *actor = msg_actor(&user);
  940. xs *acct = mastoapi_account(actor);
  941. out = xs_list_append(out, acct);
  942. }
  943. user_free(&user);
  944. }
  945. }
  946. xs_set_free(&seen);
  947. }
  948. }
  949. else
  950. /* is it a local user? */
  951. if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
  952. if (opt == NULL) {
  953. /* account information */
  954. actor = msg_actor(&snac2);
  955. out = mastoapi_account(actor);
  956. }
  957. else
  958. if (strcmp(opt, "statuses") == 0) { /** **/
  959. /* the public list of posts of a user */
  960. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  961. xs_list *p = timeline;
  962. xs_str *v;
  963. out = xs_list_new();
  964. while (xs_list_iter(&p, &v)) {
  965. xs *msg = NULL;
  966. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  967. /* add only posts by the author */
  968. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  969. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  970. xs *st = mastoapi_status(&snac2, msg);
  971. if (st)
  972. out = xs_list_append(out, st);
  973. }
  974. }
  975. }
  976. }
  977. user_free(&snac2);
  978. }
  979. else {
  980. /* try the uid as the md5 of a possibly loaded actor */
  981. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  982. if (opt == NULL) {
  983. /* account information */
  984. out = mastoapi_account(actor);
  985. }
  986. else
  987. if (strcmp(opt, "statuses") == 0) {
  988. /* we don't serve statuses of others; return the empty list */
  989. out = xs_list_new();
  990. }
  991. }
  992. }
  993. if (out != NULL) {
  994. *body = xs_json_dumps(out, 4);
  995. *ctype = "application/json";
  996. status = 200;
  997. }
  998. }
  999. }
  1000. else
  1001. if (strcmp(cmd, "/v1/timelines/home") == 0) { /** **/
  1002. /* the private timeline */
  1003. if (logged_in) {
  1004. const char *max_id = xs_dict_get(args, "max_id");
  1005. const char *since_id = xs_dict_get(args, "since_id");
  1006. const char *min_id = xs_dict_get(args, "min_id");
  1007. const char *limit_s = xs_dict_get(args, "limit");
  1008. int limit = 0;
  1009. int cnt = 0;
  1010. if (!xs_is_null(limit_s))
  1011. limit = atoi(limit_s);
  1012. if (limit == 0)
  1013. limit = 20;
  1014. xs *timeline = timeline_simple_list(&snac1, "private", 0, 2048);
  1015. xs *out = xs_list_new();
  1016. xs_list *p = timeline;
  1017. xs_str *v;
  1018. while (xs_list_iter(&p, &v) && cnt < limit) {
  1019. xs *msg = NULL;
  1020. /* only return entries older that max_id */
  1021. if (max_id) {
  1022. if (strcmp(v, MID_TO_MD5(max_id)) == 0)
  1023. max_id = NULL;
  1024. continue;
  1025. }
  1026. /* only returns entries newer than since_id */
  1027. if (since_id) {
  1028. if (strcmp(v, MID_TO_MD5(since_id)) == 0)
  1029. break;
  1030. }
  1031. /* only returns entries newer than min_id */
  1032. /* what does really "Return results immediately newer than ID" mean? */
  1033. if (min_id) {
  1034. if (strcmp(v, MID_TO_MD5(min_id)) == 0)
  1035. break;
  1036. }
  1037. /* get the entry */
  1038. if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
  1039. continue;
  1040. /* discard non-Notes */
  1041. const char *type = xs_dict_get(msg, "type");
  1042. if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0)
  1043. continue;
  1044. const char *atto = xs_dict_get(msg, "attributedTo");
  1045. const char *id = xs_dict_get(msg, "id");
  1046. /* is this message from a person we don't follow? */
  1047. if (strcmp(atto, snac1.actor) && !following_check(&snac1, atto)) {
  1048. /* discard if it was not boosted */
  1049. xs *idx = object_announces(id);
  1050. if (xs_list_len(idx) == 0)
  1051. continue;
  1052. }
  1053. /* discard notes from muted morons */
  1054. if (is_muted(&snac1, atto))
  1055. continue;
  1056. /* discard hidden notes */
  1057. if (is_hidden(&snac1, id))
  1058. continue;
  1059. /* discard poll votes (they have a name) */
  1060. if (!xs_is_null(xs_dict_get(msg, "name")))
  1061. continue;
  1062. /* convert the Note into a Mastodon status */
  1063. xs *st = mastoapi_status(&snac1, msg);
  1064. if (st != NULL)
  1065. out = xs_list_append(out, st);
  1066. cnt++;
  1067. }
  1068. *body = xs_json_dumps(out, 4);
  1069. *ctype = "application/json";
  1070. status = 200;
  1071. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  1072. }
  1073. else {
  1074. status = 401; // unauthorized
  1075. }
  1076. }
  1077. else
  1078. if (strcmp(cmd, "/v1/timelines/public") == 0) { /** **/
  1079. /* the instance public timeline (public timelines for all users) */
  1080. const char *limit_s = xs_dict_get(args, "limit");
  1081. int limit = 0;
  1082. int cnt = 0;
  1083. if (!xs_is_null(limit_s))
  1084. limit = atoi(limit_s);
  1085. if (limit == 0)
  1086. limit = 20;
  1087. xs *timeline = timeline_instance_list(0, limit);
  1088. xs *out = xs_list_new();
  1089. xs_list *p = timeline;
  1090. xs_str *md5;
  1091. snac *user = NULL;
  1092. if (logged_in)
  1093. user = &snac1;
  1094. while (xs_list_iter(&p, &md5) && cnt < limit) {
  1095. xs *msg = NULL;
  1096. /* get the entry */
  1097. if (!valid_status(object_get_by_md5(md5, &msg)))
  1098. continue;
  1099. /* discard non-Notes */
  1100. const char *type = xs_dict_get(msg, "type");
  1101. if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0)
  1102. continue;
  1103. /* discard private users */
  1104. {
  1105. const char *atto = xs_dict_get(msg, "attributedTo");
  1106. xs *l = xs_split(atto, "/");
  1107. const char *uid = xs_list_get(l, -1);
  1108. snac p_user;
  1109. int skip = 1;
  1110. if (uid && user_open(&p_user, uid)) {
  1111. if (xs_type(xs_dict_get(p_user.config, "private")) != XSTYPE_TRUE)
  1112. skip = 0;
  1113. user_free(&p_user);
  1114. }
  1115. if (skip)
  1116. continue;
  1117. }
  1118. /* convert the Note into a Mastodon status */
  1119. xs *st = mastoapi_status(user, msg);
  1120. if (st != NULL) {
  1121. out = xs_list_append(out, st);
  1122. cnt++;
  1123. }
  1124. }
  1125. *body = xs_json_dumps(out, 4);
  1126. *ctype = "application/json";
  1127. status = 200;
  1128. }
  1129. else
  1130. if (strcmp(cmd, "/v1/conversations") == 0) { /** **/
  1131. /* TBD */
  1132. *body = xs_dup("[]");
  1133. *ctype = "application/json";
  1134. status = 200;
  1135. }
  1136. else
  1137. if (strcmp(cmd, "/v1/notifications") == 0) { /** **/
  1138. if (logged_in) {
  1139. xs *l = notify_list(&snac1, 0);
  1140. xs *out = xs_list_new();
  1141. xs_list *p = l;
  1142. xs_dict *v;
  1143. xs_list *excl = xs_dict_get(args, "exclude_types[]");
  1144. while (xs_list_iter(&p, &v)) {
  1145. xs *noti = notify_get(&snac1, v);
  1146. if (noti == NULL)
  1147. continue;
  1148. const char *type = xs_dict_get(noti, "type");
  1149. const char *utype = xs_dict_get(noti, "utype");
  1150. const char *objid = xs_dict_get(noti, "objid");
  1151. xs *actor = NULL;
  1152. xs *entry = NULL;
  1153. if (!valid_status(actor_get(xs_dict_get(noti, "actor"), &actor)))
  1154. continue;
  1155. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  1156. continue;
  1157. if (is_hidden(&snac1, objid))
  1158. continue;
  1159. /* convert the type */
  1160. if (strcmp(type, "Like") == 0)
  1161. type = "favourite";
  1162. else
  1163. if (strcmp(type, "Announce") == 0)
  1164. type = "reblog";
  1165. else
  1166. if (strcmp(type, "Follow") == 0)
  1167. type = "follow";
  1168. else
  1169. if (strcmp(type, "Create") == 0)
  1170. type = "mention";
  1171. else
  1172. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  1173. type = "poll";
  1174. else
  1175. continue;
  1176. /* excluded type? */
  1177. if (!xs_is_null(excl) && xs_list_in(excl, type) != -1)
  1178. continue;
  1179. xs *mn = xs_dict_new();
  1180. mn = xs_dict_append(mn, "type", type);
  1181. xs *id = xs_replace(xs_dict_get(noti, "id"), ".", "");
  1182. mn = xs_dict_append(mn, "id", id);
  1183. mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
  1184. xs *acct = mastoapi_account(actor);
  1185. mn = xs_dict_append(mn, "account", acct);
  1186. if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
  1187. xs *st = mastoapi_status(&snac1, entry);
  1188. if (st)
  1189. mn = xs_dict_append(mn, "status", st);
  1190. }
  1191. out = xs_list_append(out, mn);
  1192. }
  1193. *body = xs_json_dumps(out, 4);
  1194. *ctype = "application/json";
  1195. status = 200;
  1196. }
  1197. else
  1198. status = 401;
  1199. }
  1200. else
  1201. if (strcmp(cmd, "/v1/filters") == 0) { /** **/
  1202. /* snac will never have filters */
  1203. *body = xs_dup("[]");
  1204. *ctype = "application/json";
  1205. status = 200;
  1206. }
  1207. else
  1208. if (strcmp(cmd, "/v1/favourites") == 0) { /** **/
  1209. /* snac will never support a list of favourites */
  1210. *body = xs_dup("[]");
  1211. *ctype = "application/json";
  1212. status = 200;
  1213. }
  1214. else
  1215. if (strcmp(cmd, "/v1/bookmarks") == 0) { /** **/
  1216. /* snac does not support bookmarks */
  1217. *body = xs_dup("[]");
  1218. *ctype = "application/json";
  1219. status = 200;
  1220. }
  1221. else
  1222. if (strcmp(cmd, "/v1/lists") == 0) { /** **/
  1223. /* snac does not support lists */
  1224. *body = xs_dup("[]");
  1225. *ctype = "application/json";
  1226. status = 200;
  1227. }
  1228. else
  1229. if (strcmp(cmd, "/v1/scheduled_statuses") == 0) { /** **/
  1230. /* snac does not schedule notes */
  1231. *body = xs_dup("[]");
  1232. *ctype = "application/json";
  1233. status = 200;
  1234. }
  1235. else
  1236. if (strcmp(cmd, "/v1/follow_requests") == 0) { /** **/
  1237. /* snac does not support optional follow confirmations */
  1238. *body = xs_dup("[]");
  1239. *ctype = "application/json";
  1240. status = 200;
  1241. }
  1242. else
  1243. if (strcmp(cmd, "/v1/announcements") == 0) { /** **/
  1244. /* snac has no announcements (yet?) */
  1245. *body = xs_dup("[]");
  1246. *ctype = "application/json";
  1247. status = 200;
  1248. }
  1249. else
  1250. if (strcmp(cmd, "/v1/custom_emojis") == 0) { /** **/
  1251. /* are you kidding me? */
  1252. *body = xs_dup("[]");
  1253. *ctype = "application/json";
  1254. status = 200;
  1255. }
  1256. else
  1257. if (strcmp(cmd, "/v1/instance") == 0) { /** **/
  1258. /* returns an instance object */
  1259. xs *ins = xs_dict_new();
  1260. const char *host = xs_dict_get(srv_config, "host");
  1261. const char *title = xs_dict_get(srv_config, "title");
  1262. const char *sdesc = xs_dict_get(srv_config, "short_description");
  1263. ins = xs_dict_append(ins, "uri", host);
  1264. ins = xs_dict_append(ins, "domain", host);
  1265. ins = xs_dict_append(ins, "title", title && *title ? title : host);
  1266. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  1267. ins = xs_dict_append(ins, "source_url", WHAT_IS_SNAC_URL);
  1268. ins = xs_dict_append(ins, "description", host);
  1269. ins = xs_dict_append(ins, "short_description", sdesc && *sdesc ? sdesc : host);
  1270. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  1271. ins = xs_dict_append(ins, "thumbnail", susie);
  1272. const char *v = xs_dict_get(srv_config, "admin_email");
  1273. if (xs_is_null(v) || *v == '\0')
  1274. v = "admin@localhost";
  1275. ins = xs_dict_append(ins, "email", v);
  1276. ins = xs_dict_append(ins, "rules", xs_stock_list);
  1277. xs *l1 = xs_list_append(xs_list_new(), "en");
  1278. ins = xs_dict_append(ins, "languages", l1);
  1279. ins = xs_dict_append(ins, "urls", xs_stock_dict);
  1280. xs *d2 = xs_dict_append(xs_dict_new(), "user_count", xs_stock_0);
  1281. d2 = xs_dict_append(d2, "status_count", xs_stock_0);
  1282. d2 = xs_dict_append(d2, "domain_count", xs_stock_0);
  1283. ins = xs_dict_append(ins, "stats", d2);
  1284. ins = xs_dict_append(ins, "registrations", xs_stock_false);
  1285. ins = xs_dict_append(ins, "approval_required", xs_stock_false);
  1286. ins = xs_dict_append(ins, "invites_enabled", xs_stock_false);
  1287. xs *cfg = xs_dict_new();
  1288. {
  1289. xs *d11 = xs_json_loads("{\"characters_reserved_per_url\":32,"
  1290. "\"max_characters\":100000,\"max_media_attachments\":8}");
  1291. cfg = xs_dict_append(cfg, "statuses", d11);
  1292. xs *d12 = xs_json_loads("{\"max_featured_tags\":10}");
  1293. cfg = xs_dict_append(cfg, "accounts", d12);
  1294. xs *d13 = xs_json_loads("{\"image_matrix_limit\":33177600,"
  1295. "\"image_size_limit\":16777216,"
  1296. "\"video_frame_rate_limit\":120,"
  1297. "\"video_matrix_limit\":8294400,"
  1298. "\"video_size_limit\":103809024}"
  1299. );
  1300. {
  1301. /* get the supported mime types from the internal list */
  1302. const char **p = xs_mime_types;
  1303. xs_set mtypes;
  1304. xs_set_init(&mtypes);
  1305. while (*p) {
  1306. const char *type = p[1];
  1307. if (xs_startswith(type, "image/") ||
  1308. xs_startswith(type, "video/") ||
  1309. xs_startswith(type, "audio/"))
  1310. xs_set_add(&mtypes, type);
  1311. p += 2;
  1312. }
  1313. xs *l = xs_set_result(&mtypes);
  1314. d13 = xs_dict_append(d13, "supported_mime_types", l);
  1315. }
  1316. cfg = xs_dict_append(cfg, "media_attachments", d13);
  1317. xs *d14 = xs_json_loads("{\"max_characters_per_option\":50,"
  1318. "\"max_expiration\":2629746,"
  1319. "\"max_options\":8,\"min_expiration\":300}");
  1320. cfg = xs_dict_append(cfg, "polls", d14);
  1321. }
  1322. ins = xs_dict_append(ins, "configuration", cfg);
  1323. const char *admin_account = xs_dict_get(srv_config, "admin_account");
  1324. if (!xs_is_null(admin_account) && *admin_account) {
  1325. snac admin;
  1326. if (user_open(&admin, admin_account)) {
  1327. xs *actor = msg_actor(&admin);
  1328. xs *acct = mastoapi_account(actor);
  1329. ins = xs_dict_append(ins, "contact_account", acct);
  1330. user_free(&admin);
  1331. }
  1332. }
  1333. *body = xs_json_dumps(ins, 4);
  1334. *ctype = "application/json";
  1335. status = 200;
  1336. }
  1337. else
  1338. if (xs_startswith(cmd, "/v1/statuses/")) { /** **/
  1339. /* information about a status */
  1340. if (logged_in) {
  1341. xs *l = xs_split(cmd, "/");
  1342. const char *id = xs_list_get(l, 3);
  1343. const char *op = xs_list_get(l, 4);
  1344. if (!xs_is_null(id)) {
  1345. xs *msg = NULL;
  1346. xs *out = NULL;
  1347. /* skip the 'fake' part of the id */
  1348. id = MID_TO_MD5(id);
  1349. if (valid_status(object_get_by_md5(id, &msg))) {
  1350. if (op == NULL) {
  1351. if (!is_muted(&snac1, xs_dict_get(msg, "attributedTo"))) {
  1352. /* return the status itself */
  1353. out = mastoapi_status(&snac1, msg);
  1354. }
  1355. }
  1356. else
  1357. if (strcmp(op, "context") == 0) { /** **/
  1358. /* return ancestors and children */
  1359. xs *anc = xs_list_new();
  1360. xs *des = xs_list_new();
  1361. xs_list *p;
  1362. xs_str *v;
  1363. char pid[64];
  1364. /* build the [grand]parent list, moving up */
  1365. strncpy(pid, id, sizeof(pid));
  1366. while (object_parent(pid, pid, sizeof(pid))) {
  1367. xs *m2 = NULL;
  1368. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  1369. xs *st = mastoapi_status(&snac1, m2);
  1370. if (st)
  1371. anc = xs_list_insert(anc, 0, st);
  1372. }
  1373. else
  1374. break;
  1375. }
  1376. /* build the children list */
  1377. xs *children = object_children(xs_dict_get(msg, "id"));
  1378. p = children;
  1379. while (xs_list_iter(&p, &v)) {
  1380. xs *m2 = NULL;
  1381. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  1382. if (xs_is_null(xs_dict_get(m2, "name"))) {
  1383. xs *st = mastoapi_status(&snac1, m2);
  1384. if (st)
  1385. des = xs_list_append(des, st);
  1386. }
  1387. }
  1388. }
  1389. out = xs_dict_new();
  1390. out = xs_dict_append(out, "ancestors", anc);
  1391. out = xs_dict_append(out, "descendants", des);
  1392. }
  1393. else
  1394. if (strcmp(op, "reblogged_by") == 0 || /** **/
  1395. strcmp(op, "favourited_by") == 0) { /** **/
  1396. /* return the list of people who liked or boosted this */
  1397. out = xs_list_new();
  1398. xs *l = NULL;
  1399. if (op[0] == 'r')
  1400. l = object_announces(xs_dict_get(msg, "id"));
  1401. else
  1402. l = object_likes(xs_dict_get(msg, "id"));
  1403. xs_list *p = l;
  1404. xs_str *v;
  1405. while (xs_list_iter(&p, &v)) {
  1406. xs *actor2 = NULL;
  1407. if (valid_status(object_get_by_md5(v, &actor2))) {
  1408. xs *acct2 = mastoapi_account(actor2);
  1409. out = xs_list_append(out, acct2);
  1410. }
  1411. }
  1412. }
  1413. }
  1414. else
  1415. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  1416. if (out != NULL) {
  1417. *body = xs_json_dumps(out, 4);
  1418. *ctype = "application/json";
  1419. status = 200;
  1420. }
  1421. }
  1422. }
  1423. else
  1424. status = 401;
  1425. }
  1426. else
  1427. if (strcmp(cmd, "/v1/preferences") == 0) { /** **/
  1428. *body = xs_dup("{}");
  1429. *ctype = "application/json";
  1430. status = 200;
  1431. }
  1432. else
  1433. if (strcmp(cmd, "/v1/markers") == 0) { /** **/
  1434. *body = xs_dup("{}");
  1435. *ctype = "application/json";
  1436. status = 200;
  1437. }
  1438. else
  1439. if (strcmp(cmd, "/v1/followed_tags") == 0) { /** **/
  1440. *body = xs_dup("[]");
  1441. *ctype = "application/json";
  1442. status = 200;
  1443. }
  1444. else
  1445. if (strcmp(cmd, "/v2/search") == 0) { /** **/
  1446. if (logged_in) {
  1447. const char *q = xs_dict_get(args, "q");
  1448. const char *type = xs_dict_get(args, "type");
  1449. const char *offset = xs_dict_get(args, "offset");
  1450. xs *acl = xs_list_new();
  1451. xs *stl = xs_list_new();
  1452. xs *htl = xs_list_new();
  1453. xs *res = xs_dict_new();
  1454. if (xs_is_null(offset) || strcmp(offset, "0") == 0) {
  1455. /* reply something only for offset 0; otherwise,
  1456. apps like Tusky keep asking again and again */
  1457. if (!xs_is_null(q) && !xs_is_null(type) && strcmp(type, "accounts") == 0) {
  1458. /* do a webfinger query */
  1459. char *actor = NULL;
  1460. char *user = NULL;
  1461. if (valid_status(webfinger_request(q, &actor, &user))) {
  1462. xs *actor_o = NULL;
  1463. if (valid_status(actor_request(&snac1, actor, &actor_o))) {
  1464. xs *acct = mastoapi_account(actor_o);
  1465. acl = xs_list_append(acl, acct);
  1466. }
  1467. }
  1468. }
  1469. }
  1470. res = xs_dict_append(res, "accounts", acl);
  1471. res = xs_dict_append(res, "statuses", stl);
  1472. res = xs_dict_append(res, "hashtags", htl);
  1473. *body = xs_json_dumps(res, 4);
  1474. *ctype = "application/json";
  1475. status = 200;
  1476. }
  1477. else
  1478. status = 401;
  1479. }
  1480. /* user cleanup */
  1481. if (logged_in)
  1482. user_free(&snac1);
  1483. return status;
  1484. }
  1485. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  1486. const char *payload, int p_size,
  1487. char **body, int *b_size, char **ctype)
  1488. {
  1489. (void)p_size;
  1490. (void)b_size;
  1491. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1492. return 0;
  1493. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1494. int status = 404;
  1495. xs *args = NULL;
  1496. char *i_ctype = xs_dict_get(req, "content-type");
  1497. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1498. args = xs_json_loads(payload);
  1499. else
  1500. args = xs_dup(xs_dict_get(req, "p_vars"));
  1501. if (args == NULL)
  1502. return 400;
  1503. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1504. snac snac = {0};
  1505. int logged_in = process_auth_token(&snac, req);
  1506. if (strcmp(cmd, "/v1/apps") == 0) { /** **/
  1507. const char *name = xs_dict_get(args, "client_name");
  1508. const char *ruri = xs_dict_get(args, "redirect_uris");
  1509. const char *scope = xs_dict_get(args, "scope");
  1510. if (xs_type(ruri) == XSTYPE_LIST)
  1511. ruri = xs_dict_get(ruri, 0);
  1512. if (name && ruri) {
  1513. xs *app = xs_dict_new();
  1514. xs *id = xs_replace_i(tid(0), ".", "");
  1515. xs *csec = random_str();
  1516. xs *vkey = random_str();
  1517. xs *cid = NULL;
  1518. /* pick a non-existent random cid */
  1519. for (;;) {
  1520. cid = random_str();
  1521. xs *p_app = app_get(cid);
  1522. if (p_app == NULL)
  1523. break;
  1524. xs_free(cid);
  1525. }
  1526. app = xs_dict_append(app, "name", name);
  1527. app = xs_dict_append(app, "redirect_uri", ruri);
  1528. app = xs_dict_append(app, "client_id", cid);
  1529. app = xs_dict_append(app, "client_secret", csec);
  1530. app = xs_dict_append(app, "vapid_key", vkey);
  1531. app = xs_dict_append(app, "id", id);
  1532. *body = xs_json_dumps(app, 4);
  1533. *ctype = "application/json";
  1534. status = 200;
  1535. app = xs_dict_append(app, "code", "");
  1536. if (scope)
  1537. app = xs_dict_append(app, "scope", scope);
  1538. app_add(cid, app);
  1539. srv_debug(1, xs_fmt("mastoapi apps: new app %s", cid));
  1540. }
  1541. }
  1542. else
  1543. if (strcmp(cmd, "/v1/statuses") == 0) { /** **/
  1544. if (logged_in) {
  1545. /* post a new Note */
  1546. const char *content = xs_dict_get(args, "status");
  1547. const char *mid = xs_dict_get(args, "in_reply_to_id");
  1548. const char *visibility = xs_dict_get(args, "visibility");
  1549. const char *summary = xs_dict_get(args, "spoiler_text");
  1550. const char *media_ids = xs_dict_get(args, "media_ids");
  1551. if (xs_is_null(media_ids))
  1552. media_ids = xs_dict_get(args, "media_ids[]");
  1553. if (xs_is_null(media_ids))
  1554. media_ids = xs_dict_get(args, "media_ids");
  1555. if (xs_is_null(visibility))
  1556. visibility = "public";
  1557. xs *attach_list = xs_list_new();
  1558. xs *irt = NULL;
  1559. /* is it a reply? */
  1560. if (mid != NULL) {
  1561. xs *r_msg = NULL;
  1562. const char *md5 = MID_TO_MD5(mid);
  1563. if (valid_status(object_get_by_md5(md5, &r_msg)))
  1564. irt = xs_dup(xs_dict_get(r_msg, "id"));
  1565. }
  1566. /* does it have attachments? */
  1567. if (!xs_is_null(media_ids)) {
  1568. xs *mi = NULL;
  1569. if (xs_type(media_ids) == XSTYPE_LIST)
  1570. mi = xs_dup(media_ids);
  1571. else {
  1572. mi = xs_list_new();
  1573. mi = xs_list_append(mi, media_ids);
  1574. }
  1575. xs_list *p = mi;
  1576. xs_str *v;
  1577. while (xs_list_iter(&p, &v)) {
  1578. xs *l = xs_list_new();
  1579. xs *url = xs_fmt("%s/s/%s", snac.actor, v);
  1580. xs *desc = static_get_meta(&snac, v);
  1581. l = xs_list_append(l, url);
  1582. l = xs_list_append(l, desc);
  1583. attach_list = xs_list_append(attach_list, l);
  1584. }
  1585. }
  1586. /* prepare the message */
  1587. xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
  1588. strcmp(visibility, "public") == 0 ? 0 : 1);
  1589. if (!xs_is_null(summary) && *summary) {
  1590. msg = xs_dict_set(msg, "sensitive", xs_stock_true);
  1591. msg = xs_dict_set(msg, "summary", summary);
  1592. }
  1593. /* store */
  1594. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1595. /* 'Create' message */
  1596. xs *c_msg = msg_create(&snac, msg);
  1597. enqueue_message(&snac, c_msg);
  1598. timeline_touch(&snac);
  1599. /* convert to a mastodon status as a response code */
  1600. xs *st = mastoapi_status(&snac, msg);
  1601. *body = xs_json_dumps(st, 4);
  1602. *ctype = "application/json";
  1603. status = 200;
  1604. }
  1605. else
  1606. status = 401;
  1607. }
  1608. else
  1609. if (xs_startswith(cmd, "/v1/statuses")) { /** **/
  1610. if (logged_in) {
  1611. /* operations on a status */
  1612. xs *l = xs_split(cmd, "/");
  1613. const char *mid = xs_list_get(l, 3);
  1614. const char *op = xs_list_get(l, 4);
  1615. if (!xs_is_null(mid)) {
  1616. xs *msg = NULL;
  1617. xs *out = NULL;
  1618. /* skip the 'fake' part of the id */
  1619. mid = MID_TO_MD5(mid);
  1620. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1621. char *id = xs_dict_get(msg, "id");
  1622. if (op == NULL) {
  1623. /* no operation (?) */
  1624. }
  1625. else
  1626. if (strcmp(op, "favourite") == 0) { /** **/
  1627. xs *n_msg = msg_admiration(&snac, id, "Like");
  1628. if (n_msg != NULL) {
  1629. enqueue_message(&snac, n_msg);
  1630. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  1631. out = mastoapi_status(&snac, msg);
  1632. }
  1633. }
  1634. else
  1635. if (strcmp(op, "unfavourite") == 0) { /** **/
  1636. /* partial support: as the original Like message
  1637. is not stored anywhere here, it's not possible
  1638. to send an Undo + Like; the only thing done here
  1639. is to delete the actor from the list of likes */
  1640. object_unadmire(id, snac.actor, 1);
  1641. }
  1642. else
  1643. if (strcmp(op, "reblog") == 0) { /** **/
  1644. xs *n_msg = msg_admiration(&snac, id, "Announce");
  1645. if (n_msg != NULL) {
  1646. enqueue_message(&snac, n_msg);
  1647. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  1648. out = mastoapi_status(&snac, msg);
  1649. }
  1650. }
  1651. else
  1652. if (strcmp(op, "unreblog") == 0) { /** **/
  1653. /* partial support: see comment in 'unfavourite' */
  1654. object_unadmire(id, snac.actor, 0);
  1655. }
  1656. else
  1657. if (strcmp(op, "bookmark") == 0) { /** **/
  1658. /* snac does not support bookmarks */
  1659. }
  1660. else
  1661. if (strcmp(op, "unbookmark") == 0) { /** **/
  1662. /* snac does not support bookmarks */
  1663. }
  1664. else
  1665. if (strcmp(op, "pin") == 0) { /** **/
  1666. /* pin this message */
  1667. if (pin(&snac, id))
  1668. out = mastoapi_status(&snac, msg);
  1669. else
  1670. status = 422;
  1671. }
  1672. else
  1673. if (strcmp(op, "unpin") == 0) { /** **/
  1674. /* unpin this message */
  1675. unpin(&snac, id);
  1676. out = mastoapi_status(&snac, msg);
  1677. }
  1678. else
  1679. if (strcmp(op, "mute") == 0) { /** **/
  1680. /* Mastodon's mute is snac's hide */
  1681. }
  1682. else
  1683. if (strcmp(op, "unmute") == 0) { /** **/
  1684. /* Mastodon's unmute is snac's unhide */
  1685. }
  1686. }
  1687. if (out != NULL) {
  1688. *body = xs_json_dumps(out, 4);
  1689. *ctype = "application/json";
  1690. status = 200;
  1691. }
  1692. }
  1693. }
  1694. else
  1695. status = 401;
  1696. }
  1697. else
  1698. if (strcmp(cmd, "/v1/notifications/clear") == 0) { /** **/
  1699. if (logged_in) {
  1700. notify_clear(&snac);
  1701. timeline_touch(&snac);
  1702. *body = xs_dup("{}");
  1703. *ctype = "application/json";
  1704. status = 200;
  1705. }
  1706. else
  1707. status = 401;
  1708. }
  1709. else
  1710. if (strcmp(cmd, "/v1/push/subscription") == 0) { /** **/
  1711. /* I don't know what I'm doing */
  1712. if (logged_in) {
  1713. char *v;
  1714. xs *wpush = xs_dict_new();
  1715. wpush = xs_dict_append(wpush, "id", "1");
  1716. v = xs_dict_get(args, "data");
  1717. v = xs_dict_get(v, "alerts");
  1718. wpush = xs_dict_append(wpush, "alerts", v);
  1719. v = xs_dict_get(args, "subscription");
  1720. v = xs_dict_get(v, "endpoint");
  1721. wpush = xs_dict_append(wpush, "endpoint", v);
  1722. xs *server_key = random_str();
  1723. wpush = xs_dict_append(wpush, "server_key", server_key);
  1724. *body = xs_json_dumps(wpush, 4);
  1725. *ctype = "application/json";
  1726. status = 200;
  1727. }
  1728. else
  1729. status = 401;
  1730. }
  1731. else
  1732. if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { /** **/
  1733. if (logged_in) {
  1734. const xs_list *file = xs_dict_get(args, "file");
  1735. const char *desc = xs_dict_get(args, "description");
  1736. if (xs_is_null(desc))
  1737. desc = "";
  1738. status = 400;
  1739. if (xs_type(file) == XSTYPE_LIST) {
  1740. const char *fn = xs_list_get(file, 0);
  1741. if (*fn != '\0') {
  1742. char *ext = strrchr(fn, '.');
  1743. xs *hash = xs_md5_hex(fn, strlen(fn));
  1744. xs *id = xs_fmt("%s%s", hash, ext);
  1745. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1746. int fo = xs_number_get(xs_list_get(file, 1));
  1747. int fs = xs_number_get(xs_list_get(file, 2));
  1748. /* store */
  1749. static_put(&snac, id, payload + fo, fs);
  1750. static_put_meta(&snac, id, desc);
  1751. /* prepare a response */
  1752. xs *rsp = xs_dict_new();
  1753. rsp = xs_dict_append(rsp, "id", id);
  1754. rsp = xs_dict_append(rsp, "type", "image");
  1755. rsp = xs_dict_append(rsp, "url", url);
  1756. rsp = xs_dict_append(rsp, "preview_url", url);
  1757. rsp = xs_dict_append(rsp, "remote_url", url);
  1758. rsp = xs_dict_append(rsp, "description", desc);
  1759. *body = xs_json_dumps(rsp, 4);
  1760. *ctype = "application/json";
  1761. status = 200;
  1762. }
  1763. }
  1764. }
  1765. else
  1766. status = 401;
  1767. }
  1768. else
  1769. if (xs_startswith(cmd, "/v1/accounts")) { /** **/
  1770. if (logged_in) {
  1771. /* account-related information */
  1772. xs *l = xs_split(cmd, "/");
  1773. const char *md5 = xs_list_get(l, 3);
  1774. const char *opt = xs_list_get(l, 4);
  1775. xs *rsp = NULL;
  1776. if (!xs_is_null(md5) && *md5) {
  1777. xs *actor_o = NULL;
  1778. if (xs_is_null(opt)) {
  1779. /* ? */
  1780. }
  1781. else
  1782. if (strcmp(opt, "follow") == 0) { /** **/
  1783. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1784. const char *actor = xs_dict_get(actor_o, "id");
  1785. xs *msg = msg_follow(&snac, actor);
  1786. if (msg != NULL) {
  1787. /* reload the actor from the message, in may be different */
  1788. actor = xs_dict_get(msg, "object");
  1789. following_add(&snac, actor, msg);
  1790. enqueue_output_by_actor(&snac, msg, actor, 0);
  1791. rsp = mastoapi_relationship(&snac, md5);
  1792. }
  1793. }
  1794. }
  1795. else
  1796. if (strcmp(opt, "unfollow") == 0) { /** **/
  1797. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1798. const char *actor = xs_dict_get(actor_o, "id");
  1799. /* get the following object */
  1800. xs *object = NULL;
  1801. if (valid_status(following_get(&snac, actor, &object))) {
  1802. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  1803. following_del(&snac, actor);
  1804. enqueue_output_by_actor(&snac, msg, actor, 0);
  1805. rsp = mastoapi_relationship(&snac, md5);
  1806. }
  1807. }
  1808. }
  1809. else
  1810. if (strcmp(opt, "block") == 0) { /** **/
  1811. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1812. const char *actor = xs_dict_get(actor_o, "id");
  1813. mute(&snac, actor);
  1814. rsp = mastoapi_relationship(&snac, md5);
  1815. }
  1816. }
  1817. else
  1818. if (strcmp(opt, "unblock") == 0) { /** **/
  1819. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1820. const char *actor = xs_dict_get(actor_o, "id");
  1821. unmute(&snac, actor);
  1822. rsp = mastoapi_relationship(&snac, md5);
  1823. }
  1824. }
  1825. }
  1826. if (rsp != NULL) {
  1827. *body = xs_json_dumps(rsp, 4);
  1828. *ctype = "application/json";
  1829. status = 200;
  1830. }
  1831. }
  1832. else
  1833. status = 401;
  1834. }
  1835. else
  1836. if (xs_startswith(cmd, "/v1/polls")) { /** **/
  1837. if (logged_in) {
  1838. /* operations on a status */
  1839. xs *l = xs_split(cmd, "/");
  1840. const char *mid = xs_list_get(l, 3);
  1841. const char *op = xs_list_get(l, 4);
  1842. if (!xs_is_null(mid)) {
  1843. xs *msg = NULL;
  1844. xs *out = NULL;
  1845. /* skip the 'fake' part of the id */
  1846. mid = MID_TO_MD5(mid);
  1847. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1848. const char *id = xs_dict_get(msg, "id");
  1849. const char *atto = xs_dict_get(msg, "attributedTo");
  1850. xs_list *opts = xs_dict_get(msg, "oneOf");
  1851. if (opts == NULL)
  1852. opts = xs_dict_get(msg, "anyOf");
  1853. if (op == NULL) {
  1854. }
  1855. else
  1856. if (strcmp(op, "votes") == 0) {
  1857. xs_list *choices = xs_dict_get(args, "choices[]");
  1858. if (xs_is_null(choices))
  1859. choices = xs_dict_get(args, "choices");
  1860. if (xs_type(choices) == XSTYPE_LIST) {
  1861. xs_str *v;
  1862. while (xs_list_iter(&choices, &v)) {
  1863. int io = atoi(v);
  1864. const xs_dict *o = xs_list_get(opts, io);
  1865. if (o) {
  1866. const char *name = xs_dict_get(o, "name");
  1867. xs *msg = msg_note(&snac, "", atto, (char *)id, NULL, 1);
  1868. msg = xs_dict_append(msg, "name", name);
  1869. xs *c_msg = msg_create(&snac, msg);
  1870. enqueue_message(&snac, c_msg);
  1871. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1872. }
  1873. }
  1874. out = mastoapi_poll(&snac, msg);
  1875. }
  1876. }
  1877. }
  1878. if (out != NULL) {
  1879. *body = xs_json_dumps(out, 4);
  1880. *ctype = "application/json";
  1881. status = 200;
  1882. }
  1883. }
  1884. }
  1885. else
  1886. status = 401;
  1887. }
  1888. /* user cleanup */
  1889. if (logged_in)
  1890. user_free(&snac);
  1891. return status;
  1892. }
  1893. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  1894. const char *payload, int p_size,
  1895. char **body, int *b_size, char **ctype)
  1896. {
  1897. (void)p_size;
  1898. (void)b_size;
  1899. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1900. return 0;
  1901. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1902. int status = 404;
  1903. xs *args = NULL;
  1904. char *i_ctype = xs_dict_get(req, "content-type");
  1905. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1906. args = xs_json_loads(payload);
  1907. else
  1908. args = xs_dup(xs_dict_get(req, "p_vars"));
  1909. if (args == NULL)
  1910. return 400;
  1911. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1912. snac snac = {0};
  1913. int logged_in = process_auth_token(&snac, req);
  1914. if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) { /** **/
  1915. if (logged_in) {
  1916. xs *l = xs_split(cmd, "/");
  1917. const char *stid = xs_list_get(l, 3);
  1918. if (!xs_is_null(stid)) {
  1919. const char *desc = xs_dict_get(args, "description");
  1920. /* set the image metadata */
  1921. static_put_meta(&snac, stid, desc);
  1922. /* prepare a response */
  1923. xs *rsp = xs_dict_new();
  1924. xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
  1925. rsp = xs_dict_append(rsp, "id", stid);
  1926. rsp = xs_dict_append(rsp, "type", "image");
  1927. rsp = xs_dict_append(rsp, "url", url);
  1928. rsp = xs_dict_append(rsp, "preview_url", url);
  1929. rsp = xs_dict_append(rsp, "remote_url", url);
  1930. rsp = xs_dict_append(rsp, "description", desc);
  1931. *body = xs_json_dumps(rsp, 4);
  1932. *ctype = "application/json";
  1933. status = 200;
  1934. }
  1935. }
  1936. else
  1937. status = 401;
  1938. }
  1939. /* user cleanup */
  1940. if (logged_in)
  1941. user_free(&snac);
  1942. return status;
  1943. }
  1944. void mastoapi_purge(void)
  1945. {
  1946. xs *spec = xs_fmt("%s/app/" "*.json", srv_basedir);
  1947. xs *files = xs_glob(spec, 1, 0);
  1948. xs_list *p = files;
  1949. xs_str *v;
  1950. time_t mt = time(NULL) - 3600;
  1951. while (xs_list_iter(&p, &v)) {
  1952. xs *cid = xs_replace(v, ".json", "");
  1953. xs *fn = _app_fn(cid);
  1954. if (mtime(fn) < mt) {
  1955. /* get the app */
  1956. xs *app = app_get(cid);
  1957. if (app) {
  1958. /* old apps with no uid are incomplete cruft */
  1959. const char *uid = xs_dict_get(app, "uid");
  1960. if (xs_is_null(uid) || *uid == '\0') {
  1961. unlink(fn);
  1962. srv_debug(2, xs_fmt("purged %s", fn));
  1963. }
  1964. }
  1965. }
  1966. }
  1967. }
  1968. #endif /* #ifndef NO_MASTODON_API */