mastoapi.c 75 KB

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