mastoapi.c 66 KB

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