1
0

mastoapi.c 67 KB

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