mastoapi.c 84 KB

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