mastoapi.c 77 KB

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