mastoapi.c 91 KB

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