mastoapi.c 81 KB

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