mastoapi.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111
  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 *display_name = xs_dict_get(actor, "name");
  395. if (xs_is_null(display_name) || *display_name == '\0')
  396. display_name = xs_dict_get(actor, "preferredUsername");
  397. const char *id = xs_dict_get(actor, "id");
  398. const char *pub = xs_dict_get(actor, "published");
  399. xs *acct_md5 = xs_md5_hex(id, strlen(id));
  400. acct = xs_dict_append(acct, "id", acct_md5);
  401. acct = xs_dict_append(acct, "username", xs_dict_get(actor, "preferredUsername"));
  402. acct = xs_dict_append(acct, "display_name", display_name);
  403. {
  404. /* create the acct field as user@host */
  405. xs *l = xs_split(id, "/");
  406. xs *fquid = xs_fmt("%s@%s", xs_dict_get(actor, "preferredUsername"), xs_list_get(l, 2));
  407. acct = xs_dict_append(acct, "acct", fquid);
  408. }
  409. if (pub)
  410. acct = xs_dict_append(acct, "created_at", pub);
  411. else {
  412. /* unset created_at crashes Tusky, so lie like a mf */
  413. xs *date = xs_str_utctime(0, "%Y-%m-%dT%H:%M:%SZ");
  414. acct = xs_dict_append(acct, "created_at", date);
  415. }
  416. const char *note = xs_dict_get(actor, "summary");
  417. if (xs_is_null(note))
  418. note = "";
  419. acct = xs_dict_append(acct, "note", note);
  420. acct = xs_dict_append(acct, "url", id);
  421. xs *avatar = NULL;
  422. xs_dict *av = xs_dict_get(actor, "icon");
  423. if (xs_type(av) == XSTYPE_DICT)
  424. avatar = xs_dup(xs_dict_get(av, "url"));
  425. else
  426. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  427. acct = xs_dict_append(acct, "avatar", avatar);
  428. /* emojis */
  429. xs_list *p;
  430. if (!xs_is_null(p = xs_dict_get(actor, "tag"))) {
  431. xs *eml = xs_list_new();
  432. xs_dict *v;
  433. xs *t = xs_val_new(XSTYPE_TRUE);
  434. while (xs_list_iter(&p, &v)) {
  435. const char *type = xs_dict_get(v, "type");
  436. if (!xs_is_null(type) && strcmp(type, "Emoji") == 0) {
  437. const char *name = xs_dict_get(v, "name");
  438. const xs_dict *icon = xs_dict_get(v, "icon");
  439. if (!xs_is_null(name) && !xs_is_null(icon)) {
  440. const char *url = xs_dict_get(icon, "url");
  441. if (!xs_is_null(url)) {
  442. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  443. xs *d1 = xs_dict_new();
  444. d1 = xs_dict_append(d1, "shortcode", nm);
  445. d1 = xs_dict_append(d1, "url", url);
  446. d1 = xs_dict_append(d1, "static_url", url);
  447. d1 = xs_dict_append(d1, "visible_in_picker", t);
  448. eml = xs_list_append(eml, d1);
  449. }
  450. }
  451. }
  452. }
  453. acct = xs_dict_append(acct, "emojis", eml);
  454. }
  455. return acct;
  456. }
  457. xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
  458. /* converts an ActivityPub note to a Mastodon status */
  459. {
  460. xs *actor = NULL;
  461. actor_get(snac, xs_dict_get(msg, "attributedTo"), &actor);
  462. /* if the author is not here, discard */
  463. if (actor == NULL)
  464. return NULL;
  465. xs *acct = mastoapi_account(actor);
  466. xs *f = xs_val_new(XSTYPE_FALSE);
  467. xs *t = xs_val_new(XSTYPE_TRUE);
  468. xs *n = xs_val_new(XSTYPE_NULL);
  469. xs *idx = NULL;
  470. xs *ixc = NULL;
  471. char *tmp;
  472. char *id = xs_dict_get(msg, "id");
  473. xs *mid = mastoapi_id(msg);
  474. xs_dict *st = xs_dict_new();
  475. st = xs_dict_append(st, "id", mid);
  476. st = xs_dict_append(st, "uri", id);
  477. st = xs_dict_append(st, "url", id);
  478. st = xs_dict_append(st, "created_at", xs_dict_get(msg, "published"));
  479. st = xs_dict_append(st, "account", acct);
  480. st = xs_dict_append(st, "content", xs_dict_get(msg, "content"));
  481. st = xs_dict_append(st, "visibility",
  482. is_msg_public(snac, msg) ? "public" : "private");
  483. tmp = xs_dict_get(msg, "sensitive");
  484. if (xs_is_null(tmp))
  485. tmp = f;
  486. st = xs_dict_append(st, "sensitive", tmp);
  487. tmp = xs_dict_get(msg, "summary");
  488. if (xs_is_null(tmp))
  489. tmp = "";
  490. st = xs_dict_append(st, "spoiler_text", tmp);
  491. /* create the list of attachments */
  492. xs *matt = xs_list_new();
  493. xs_list *att = xs_dict_get(msg, "attachment");
  494. xs_str *aobj;
  495. while (xs_list_iter(&att, &aobj)) {
  496. const char *mtype = xs_dict_get(aobj, "mediaType");
  497. if (!xs_is_null(mtype)) {
  498. if (xs_startswith(mtype, "image/") || xs_startswith(mtype, "video/")) {
  499. xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
  500. xs *matte = xs_dict_new();
  501. matte = xs_dict_append(matte, "id", matteid);
  502. matte = xs_dict_append(matte, "type", *mtype == 'i' ? "image" : "video");
  503. matte = xs_dict_append(matte, "url", xs_dict_get(aobj, "url"));
  504. matte = xs_dict_append(matte, "preview_url", xs_dict_get(aobj, "url"));
  505. matte = xs_dict_append(matte, "remote_url", xs_dict_get(aobj, "url"));
  506. const char *name = xs_dict_get(aobj, "name");
  507. if (xs_is_null(name))
  508. name = "";
  509. matte = xs_dict_append(matte, "description", name);
  510. matt = xs_list_append(matt, matte);
  511. }
  512. }
  513. }
  514. st = xs_dict_append(st, "media_attachments", matt);
  515. {
  516. xs *ml = xs_list_new();
  517. xs *htl = xs_list_new();
  518. xs *eml = xs_list_new();
  519. xs_list *p = xs_dict_get(msg, "tag");
  520. xs_dict *v;
  521. int n = 0;
  522. while (xs_list_iter(&p, &v)) {
  523. const char *type = xs_dict_get(v, "type");
  524. if (xs_is_null(type))
  525. continue;
  526. xs *d1 = xs_dict_new();
  527. if (strcmp(type, "Mention") == 0) {
  528. const char *name = xs_dict_get(v, "name");
  529. const char *href = xs_dict_get(v, "href");
  530. if (!xs_is_null(name) && !xs_is_null(href) &&
  531. strcmp(href, snac->actor) != 0) {
  532. xs *nm = xs_strip_chars_i(xs_dup(name), "@");
  533. xs *id = xs_fmt("%d", n++);
  534. d1 = xs_dict_append(d1, "id", id);
  535. d1 = xs_dict_append(d1, "username", nm);
  536. d1 = xs_dict_append(d1, "acct", nm);
  537. d1 = xs_dict_append(d1, "url", href);
  538. ml = xs_list_append(ml, d1);
  539. }
  540. }
  541. else
  542. if (strcmp(type, "Hashtag") == 0) {
  543. const char *name = xs_dict_get(v, "name");
  544. const char *href = xs_dict_get(v, "href");
  545. if (!xs_is_null(name) && !xs_is_null(href)) {
  546. xs *nm = xs_strip_chars_i(xs_dup(name), "#");
  547. d1 = xs_dict_append(d1, "name", nm);
  548. d1 = xs_dict_append(d1, "url", href);
  549. htl = xs_list_append(htl, d1);
  550. }
  551. }
  552. else
  553. if (strcmp(type, "Emoji") == 0) {
  554. const char *name = xs_dict_get(v, "name");
  555. const xs_dict *icon = xs_dict_get(v, "icon");
  556. if (!xs_is_null(name) && !xs_is_null(icon)) {
  557. const char *url = xs_dict_get(icon, "url");
  558. if (!xs_is_null(url)) {
  559. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  560. xs *t = xs_val_new(XSTYPE_TRUE);
  561. d1 = xs_dict_append(d1, "shortcode", nm);
  562. d1 = xs_dict_append(d1, "url", url);
  563. d1 = xs_dict_append(d1, "static_url", url);
  564. d1 = xs_dict_append(d1, "visible_in_picker", t);
  565. d1 = xs_dict_append(d1, "category", "Emojis");
  566. eml = xs_list_append(eml, d1);
  567. }
  568. }
  569. }
  570. }
  571. st = xs_dict_append(st, "mentions", ml);
  572. st = xs_dict_append(st, "tags", htl);
  573. st = xs_dict_append(st, "emojis", eml);
  574. }
  575. xs_free(idx);
  576. xs_free(ixc);
  577. idx = object_likes(id);
  578. ixc = xs_number_new(xs_list_len(idx));
  579. st = xs_dict_append(st, "favourites_count", ixc);
  580. st = xs_dict_append(st, "favourited",
  581. xs_list_in(idx, snac->md5) != -1 ? t : f);
  582. xs_free(idx);
  583. xs_free(ixc);
  584. idx = object_announces(id);
  585. ixc = xs_number_new(xs_list_len(idx));
  586. st = xs_dict_append(st, "reblogs_count", ixc);
  587. st = xs_dict_append(st, "reblogged",
  588. xs_list_in(idx, snac->md5) != -1 ? t : f);
  589. xs_free(idx);
  590. xs_free(ixc);
  591. idx = object_children(id);
  592. ixc = xs_number_new(xs_list_len(idx));
  593. st = xs_dict_append(st, "replies_count", ixc);
  594. /* default in_reply_to values */
  595. st = xs_dict_append(st, "in_reply_to_id", n);
  596. st = xs_dict_append(st, "in_reply_to_account_id", n);
  597. tmp = xs_dict_get(msg, "inReplyTo");
  598. if (!xs_is_null(tmp)) {
  599. xs *irto = NULL;
  600. if (valid_status(object_get(tmp, &irto))) {
  601. xs *irt_mid = mastoapi_id(irto);
  602. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  603. char *at = NULL;
  604. if (!xs_is_null(at = xs_dict_get(irto, "attributedTo"))) {
  605. xs *at_md5 = xs_md5_hex(at, strlen(at));
  606. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  607. }
  608. }
  609. }
  610. st = xs_dict_append(st, "reblog", n);
  611. st = xs_dict_append(st, "poll", n);
  612. st = xs_dict_append(st, "card", n);
  613. st = xs_dict_append(st, "language", n);
  614. tmp = xs_dict_get(msg, "sourceContent");
  615. if (xs_is_null(tmp))
  616. tmp = "";
  617. st = xs_dict_append(st, "text", tmp);
  618. tmp = xs_dict_get(msg, "updated");
  619. if (xs_is_null(tmp))
  620. tmp = n;
  621. st = xs_dict_append(st, "edited_at", tmp);
  622. return st;
  623. }
  624. xs_dict *mastoapi_relationship(snac *snac, const char *md5)
  625. {
  626. xs_dict *rel = NULL;
  627. xs *actor_o = NULL;
  628. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  629. xs *t = xs_val_new(XSTYPE_TRUE);
  630. xs *f = xs_val_new(XSTYPE_FALSE);
  631. rel = xs_dict_new();
  632. const char *actor = xs_dict_get(actor_o, "id");
  633. rel = xs_dict_append(rel, "id", md5);
  634. rel = xs_dict_append(rel, "following",
  635. following_check(snac, actor) ? t : f);
  636. rel = xs_dict_append(rel, "showing_reblogs", t);
  637. rel = xs_dict_append(rel, "notifying", f);
  638. rel = xs_dict_append(rel, "followed_by",
  639. follower_check(snac, actor) ? t : f);
  640. rel = xs_dict_append(rel, "blocking",
  641. is_muted(snac, actor) ? t : f);
  642. rel = xs_dict_append(rel, "muting", f);
  643. rel = xs_dict_append(rel, "muting_notifications", f);
  644. rel = xs_dict_append(rel, "requested", f);
  645. rel = xs_dict_append(rel, "domain_blocking", f);
  646. rel = xs_dict_append(rel, "endorsed", f);
  647. rel = xs_dict_append(rel, "note", "");
  648. }
  649. return rel;
  650. }
  651. int process_auth_token(snac *snac, const xs_dict *req)
  652. /* processes an authorization token, if there is one */
  653. {
  654. int logged_in = 0;
  655. char *v;
  656. /* if there is an authorization field, try to validate it */
  657. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  658. xs *tokid = xs_replace_n(v, "Bearer ", "", 1);
  659. xs *token = token_get(tokid);
  660. if (token != NULL) {
  661. const char *uid = xs_dict_get(token, "uid");
  662. if (!xs_is_null(uid) && user_open(snac, uid)) {
  663. logged_in = 1;
  664. /* this counts as a 'login' */
  665. lastlog_write(snac, "mastoapi");
  666. srv_debug(2, xs_fmt("mastoapi auth: valid token for user %s", uid));
  667. }
  668. else
  669. srv_log(xs_fmt("mastoapi auth: corrupted token %s", tokid));
  670. }
  671. else
  672. srv_log(xs_fmt("mastoapi auth: invalid token %s", tokid));
  673. }
  674. return logged_in;
  675. }
  676. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  677. char **body, int *b_size, char **ctype)
  678. {
  679. (void)b_size;
  680. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  681. return 0;
  682. srv_debug(1, xs_fmt("mastoapi_get_handler %s", q_path));
  683. /* {
  684. xs *j = xs_json_dumps_pp(req, 4);
  685. printf("mastoapi get:\n%s\n", j);
  686. }*/
  687. int status = 404;
  688. xs_dict *args = xs_dict_get(req, "q_vars");
  689. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  690. snac snac1 = {0};
  691. int logged_in = process_auth_token(&snac1, req);
  692. if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) { /** **/
  693. if (logged_in) {
  694. xs *acct = xs_dict_new();
  695. acct = xs_dict_append(acct, "id", xs_dict_get(snac1.config, "uid"));
  696. acct = xs_dict_append(acct, "username", xs_dict_get(snac1.config, "uid"));
  697. acct = xs_dict_append(acct, "acct", xs_dict_get(snac1.config, "uid"));
  698. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac1.config, "name"));
  699. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac1.config, "published"));
  700. acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio"));
  701. acct = xs_dict_append(acct, "url", snac1.actor);
  702. acct = xs_dict_append(acct, "header", "");
  703. xs *avatar = NULL;
  704. char *av = xs_dict_get(snac1.config, "avatar");
  705. if (xs_is_null(av) || *av == '\0')
  706. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  707. else
  708. avatar = xs_dup(av);
  709. acct = xs_dict_append(acct, "avatar", avatar);
  710. *body = xs_json_dumps_pp(acct, 4);
  711. *ctype = "application/json";
  712. status = 200;
  713. }
  714. else {
  715. status = 422; // "Unprocessable entity" (no login)
  716. }
  717. }
  718. else
  719. if (strcmp(cmd, "/v1/accounts/relationships") == 0) { /** **/
  720. /* find if an account is followed, blocked, etc. */
  721. /* the account to get relationships about is in args "id[]" */
  722. if (logged_in) {
  723. xs *res = xs_list_new();
  724. const char *md5 = xs_dict_get(args, "id[]");
  725. if (!xs_is_null(md5)) {
  726. if (xs_type(md5) == XSTYPE_LIST)
  727. md5 = xs_list_get(md5, 0);
  728. xs *rel = mastoapi_relationship(&snac1, md5);
  729. if (rel != NULL)
  730. res = xs_list_append(res, rel);
  731. }
  732. *body = xs_json_dumps_pp(res, 4);
  733. *ctype = "application/json";
  734. status = 200;
  735. }
  736. else
  737. status = 422;
  738. }
  739. else
  740. if (xs_startswith(cmd, "/v1/accounts/")) { /** **/
  741. /* account-related information */
  742. xs *l = xs_split(cmd, "/");
  743. const char *uid = xs_list_get(l, 3);
  744. const char *opt = xs_list_get(l, 4);
  745. if (uid != NULL) {
  746. snac snac2;
  747. xs *out = NULL;
  748. xs *actor = NULL;
  749. if (logged_in && strcmp(uid, "search") == 0) { /** **/
  750. /* search for accounts starting with q */
  751. const char *aq = xs_dict_get(args, "q");
  752. if (!xs_is_null(aq)) {
  753. xs *q = xs_tolower_i(xs_dup(aq));
  754. out = xs_list_new();
  755. xs *wing = following_list(&snac1);
  756. xs *wers = follower_list(&snac1);
  757. xs_list *p;
  758. xs_set seen;
  759. xs_set_init(&seen);
  760. xs_list *lsts[] = { wing, wers, NULL };
  761. int n;
  762. for (n = 0; (p = lsts[n]) != NULL; n++) {
  763. xs_str *v;
  764. while (xs_list_iter(&p, &v)) {
  765. /* already seen? skip */
  766. if (xs_set_add(&seen, v) == 0)
  767. continue;
  768. xs *actor = NULL;
  769. if (valid_status(object_get(v, &actor))) {
  770. const char *uname = xs_dict_get(actor, "preferredUsername");
  771. if (!xs_is_null(uname)) {
  772. xs *luname = xs_tolower_i(xs_dup(uname));
  773. if (xs_startswith(luname, q)) {
  774. xs *acct = mastoapi_account(actor);
  775. out = xs_list_append(out, acct);
  776. }
  777. }
  778. }
  779. }
  780. }
  781. xs_set_free(&seen);
  782. }
  783. }
  784. else
  785. /* is it a local user? */
  786. if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
  787. if (opt == NULL) {
  788. /* account information */
  789. actor = msg_actor(&snac2);
  790. out = mastoapi_account(actor);
  791. }
  792. else
  793. if (strcmp(opt, "statuses") == 0) { /** **/
  794. /* the public list of posts of a user */
  795. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  796. xs_list *p = timeline;
  797. xs_str *v;
  798. out = xs_list_new();
  799. while (xs_list_iter(&p, &v)) {
  800. xs *msg = NULL;
  801. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  802. /* add only posts by the author */
  803. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  804. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  805. xs *st = mastoapi_status(&snac2, msg);
  806. out = xs_list_append(out, st);
  807. }
  808. }
  809. }
  810. }
  811. user_free(&snac2);
  812. }
  813. else {
  814. /* try the uid as the md5 of a possibly loaded actor */
  815. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  816. if (opt == NULL) {
  817. /* account information */
  818. out = mastoapi_account(actor);
  819. }
  820. else
  821. if (strcmp(opt, "statuses") == 0) {
  822. /* we don't serve statuses of others; return the empty list */
  823. out = xs_list_new();
  824. }
  825. }
  826. }
  827. if (out != NULL) {
  828. *body = xs_json_dumps_pp(out, 4);
  829. *ctype = "application/json";
  830. status = 200;
  831. }
  832. }
  833. }
  834. else
  835. if (strcmp(cmd, "/v1/timelines/home") == 0) { /** **/
  836. /* the private timeline */
  837. if (logged_in) {
  838. const char *max_id = xs_dict_get(args, "max_id");
  839. const char *since_id = xs_dict_get(args, "since_id");
  840. const char *min_id = xs_dict_get(args, "min_id");
  841. const char *limit_s = xs_dict_get(args, "limit");
  842. int limit = 0;
  843. int cnt = 0;
  844. if (!xs_is_null(limit_s))
  845. limit = atoi(limit_s);
  846. if (limit == 0)
  847. limit = 20;
  848. xs *timeline = timeline_simple_list(&snac1, "private", 0, 256);
  849. xs *out = xs_list_new();
  850. xs_list *p = timeline;
  851. xs_str *v;
  852. while (xs_list_iter(&p, &v) && cnt < limit) {
  853. xs *msg = NULL;
  854. /* only return entries older that max_id */
  855. if (max_id) {
  856. if (strcmp(v, max_id) == 0)
  857. max_id = NULL;
  858. continue;
  859. }
  860. /* only returns entries newer than since_id */
  861. if (since_id) {
  862. if (strcmp(v, since_id) == 0)
  863. break;
  864. }
  865. /* only returns entries newer than min_id */
  866. /* what does really "Return results immediately newer than ID" mean? */
  867. if (min_id) {
  868. if (strcmp(v, min_id) == 0)
  869. break;
  870. }
  871. /* get the entry */
  872. if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
  873. continue;
  874. /* discard non-Notes */
  875. if (strcmp(xs_dict_get(msg, "type"), "Note") != 0)
  876. continue;
  877. /* discard notes from muted morons */
  878. if (is_muted(&snac1, xs_dict_get(msg, "attributedTo")))
  879. continue;
  880. /* discard hidden notes */
  881. if (is_hidden(&snac1, xs_dict_get(msg, "id")))
  882. continue;
  883. /* convert the Note into a Mastodon status */
  884. xs *st = mastoapi_status(&snac1, msg);
  885. if (st != NULL)
  886. out = xs_list_append(out, st);
  887. cnt++;
  888. }
  889. *body = xs_json_dumps_pp(out, 4);
  890. *ctype = "application/json";
  891. status = 200;
  892. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  893. }
  894. else {
  895. status = 401; // unauthorized
  896. }
  897. }
  898. else
  899. if (strcmp(cmd, "/v1/timelines/public") == 0) { /** **/
  900. /* the instance public timeline (public timelines for all users) */
  901. /* NOTE: this api call needs no authorization; but,
  902. I need a logged-in user in mastoapi_status() for
  903. is_msg_public() and the liked/boosted flags,
  904. so it will silently fail for pure public access */
  905. const char *limit_s = xs_dict_get(args, "limit");
  906. int limit = 0;
  907. int cnt = 0;
  908. if (!xs_is_null(limit_s))
  909. limit = atoi(limit_s);
  910. if (limit == 0)
  911. limit = 20;
  912. xs *timeline = timeline_instance_list(0, limit);
  913. xs *out = xs_list_new();
  914. xs_list *p = timeline;
  915. xs_str *md5;
  916. while (logged_in && xs_list_iter(&p, &md5) && cnt < limit) {
  917. xs *msg = NULL;
  918. /* get the entry */
  919. if (!valid_status(object_get_by_md5(md5, &msg)))
  920. continue;
  921. /* discard non-Notes */
  922. if (strcmp(xs_dict_get(msg, "type"), "Note") != 0)
  923. continue;
  924. /* convert the Note into a Mastodon status */
  925. xs *st = mastoapi_status(&snac1, msg);
  926. if (st != NULL) {
  927. out = xs_list_append(out, st);
  928. cnt++;
  929. }
  930. }
  931. *body = xs_json_dumps_pp(out, 4);
  932. *ctype = "application/json";
  933. status = 200;
  934. }
  935. else
  936. if (strcmp(cmd, "/v1/conversations") == 0) { /** **/
  937. /* TBD */
  938. *body = xs_dup("[]");
  939. *ctype = "application/json";
  940. status = 200;
  941. }
  942. else
  943. if (strcmp(cmd, "/v1/notifications") == 0) { /** **/
  944. if (logged_in) {
  945. xs *l = notify_list(&snac1, 0);
  946. xs *out = xs_list_new();
  947. xs_list *p = l;
  948. xs_dict *v;
  949. xs_list *excl = xs_dict_get(args, "exclude_types[]");
  950. while (xs_list_iter(&p, &v)) {
  951. xs *noti = notify_get(&snac1, v);
  952. if (noti == NULL)
  953. continue;
  954. const char *type = xs_dict_get(noti, "type");
  955. const char *objid = xs_dict_get(noti, "objid");
  956. xs *actor = NULL;
  957. xs *entry = NULL;
  958. if (!valid_status(actor_get(&snac1, xs_dict_get(noti, "actor"), &actor)))
  959. continue;
  960. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  961. continue;
  962. if (is_hidden(&snac1, objid))
  963. continue;
  964. /* convert the type */
  965. if (strcmp(type, "Like") == 0)
  966. type = "favourite";
  967. else
  968. if (strcmp(type, "Announce") == 0)
  969. type = "reblog";
  970. else
  971. if (strcmp(type, "Follow") == 0)
  972. type = "follow";
  973. else
  974. if (strcmp(type, "Create") == 0)
  975. type = "mention";
  976. else
  977. continue;
  978. /* excluded type? */
  979. if (!xs_is_null(excl) && xs_list_in(excl, type) != -1)
  980. continue;
  981. xs *mn = xs_dict_new();
  982. mn = xs_dict_append(mn, "type", type);
  983. xs *id = xs_replace(xs_dict_get(noti, "id"), ".", "");
  984. mn = xs_dict_append(mn, "id", id);
  985. mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
  986. xs *acct = mastoapi_account(actor);
  987. mn = xs_dict_append(mn, "account", acct);
  988. if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
  989. xs *st = mastoapi_status(&snac1, entry);
  990. mn = xs_dict_append(mn, "status", st);
  991. }
  992. out = xs_list_append(out, mn);
  993. }
  994. *body = xs_json_dumps_pp(out, 4);
  995. *ctype = "application/json";
  996. status = 200;
  997. }
  998. else
  999. status = 401;
  1000. }
  1001. else
  1002. if (strcmp(cmd, "/v1/filters") == 0) { /** **/
  1003. /* snac will never have filters */
  1004. *body = xs_dup("[]");
  1005. *ctype = "application/json";
  1006. status = 200;
  1007. }
  1008. else
  1009. if (strcmp(cmd, "/v1/favourites") == 0) { /** **/
  1010. /* snac will never support a list of favourites */
  1011. *body = xs_dup("[]");
  1012. *ctype = "application/json";
  1013. status = 200;
  1014. }
  1015. else
  1016. if (strcmp(cmd, "/v1/bookmarks") == 0) { /** **/
  1017. /* snac does not support bookmarks */
  1018. *body = xs_dup("[]");
  1019. *ctype = "application/json";
  1020. status = 200;
  1021. }
  1022. else
  1023. if (strcmp(cmd, "/v1/lists") == 0) { /** **/
  1024. /* snac does not support lists */
  1025. *body = xs_dup("[]");
  1026. *ctype = "application/json";
  1027. status = 200;
  1028. }
  1029. else
  1030. if (strcmp(cmd, "/v1/scheduled_statuses") == 0) { /** **/
  1031. /* snac does not schedule notes */
  1032. *body = xs_dup("[]");
  1033. *ctype = "application/json";
  1034. status = 200;
  1035. }
  1036. else
  1037. if (strcmp(cmd, "/v1/follow_requests") == 0) { /** **/
  1038. /* snac does not support optional follow confirmations */
  1039. *body = xs_dup("[]");
  1040. *ctype = "application/json";
  1041. status = 200;
  1042. }
  1043. else
  1044. if (strcmp(cmd, "/v1/announcements") == 0) { /** **/
  1045. /* snac has no announcements (yet?) */
  1046. *body = xs_dup("[]");
  1047. *ctype = "application/json";
  1048. status = 200;
  1049. }
  1050. else
  1051. if (strcmp(cmd, "/v1/custom_emojis") == 0) { /** **/
  1052. /* are you kidding me? */
  1053. *body = xs_dup("[]");
  1054. *ctype = "application/json";
  1055. status = 200;
  1056. }
  1057. else
  1058. if (strcmp(cmd, "/v1/instance") == 0) { /** **/
  1059. /* returns an instance object */
  1060. xs *ins = xs_dict_new();
  1061. const char *host = xs_dict_get(srv_config, "host");
  1062. ins = xs_dict_append(ins, "uri", host);
  1063. ins = xs_dict_append(ins, "domain", host);
  1064. ins = xs_dict_append(ins, "title", host);
  1065. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  1066. ins = xs_dict_append(ins, "source_url", WHAT_IS_SNAC_URL);
  1067. ins = xs_dict_append(ins, "description", host);
  1068. ins = xs_dict_append(ins, "short_description", host);
  1069. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  1070. ins = xs_dict_append(ins, "thumbnail", susie);
  1071. const char *v = xs_dict_get(srv_config, "admin_email");
  1072. if (xs_is_null(v) || *v == '\0')
  1073. v = "admin@localhost";
  1074. ins = xs_dict_append(ins, "email", v);
  1075. xs *l1 = xs_list_new();
  1076. ins = xs_dict_append(ins, "rules", l1);
  1077. l1 = xs_list_append(l1, "en");
  1078. ins = xs_dict_append(ins, "languages", l1);
  1079. xs *d1 = xs_dict_new();
  1080. ins = xs_dict_append(ins, "urls", d1);
  1081. xs *z = xs_number_new(0);
  1082. d1 = xs_dict_append(d1, "user_count", z);
  1083. d1 = xs_dict_append(d1, "status_count", z);
  1084. d1 = xs_dict_append(d1, "domain_count", z);
  1085. ins = xs_dict_append(ins, "stats", d1);
  1086. xs *f = xs_val_new(XSTYPE_FALSE);
  1087. ins = xs_dict_append(ins, "registrations", f);
  1088. ins = xs_dict_append(ins, "approval_required", f);
  1089. ins = xs_dict_append(ins, "invites_enabled", f);
  1090. xs *cfg = xs_dict_new();
  1091. {
  1092. xs *d11 = xs_dict_new();
  1093. xs *mc = xs_number_new(100000);
  1094. xs *mm = xs_number_new(8);
  1095. xs *cr = xs_number_new(32);
  1096. d11 = xs_dict_append(d11, "max_characters", mc);
  1097. d11 = xs_dict_append(d11, "max_media_attachments", mm);
  1098. d11 = xs_dict_append(d11, "characters_reserved_per_url", cr);
  1099. cfg = xs_dict_append(cfg, "statuses", d11);
  1100. }
  1101. ins = xs_dict_append(ins, "configuration", cfg);
  1102. const char *admin_account = xs_dict_get(srv_config, "admin_account");
  1103. if (!xs_is_null(admin_account) && *admin_account) {
  1104. snac admin;
  1105. if (user_open(&admin, admin_account)) {
  1106. xs *actor = msg_actor(&admin);
  1107. xs *acct = mastoapi_account(actor);
  1108. ins = xs_dict_append(ins, "contact_account", acct);
  1109. user_free(&admin);
  1110. }
  1111. }
  1112. *body = xs_json_dumps_pp(ins, 4);
  1113. *ctype = "application/json";
  1114. status = 200;
  1115. }
  1116. else
  1117. if (xs_startswith(cmd, "/v1/statuses/")) { /** **/
  1118. /* information about a status */
  1119. xs *l = xs_split(cmd, "/");
  1120. const char *id = xs_list_get(l, 3);
  1121. const char *op = xs_list_get(l, 4);
  1122. if (!xs_is_null(id)) {
  1123. xs *msg = NULL;
  1124. xs *out = NULL;
  1125. /* skip the 'fake' part of the id */
  1126. id = MID_TO_MD5(id);
  1127. if (valid_status(object_get_by_md5(id, &msg))) {
  1128. if (op == NULL) {
  1129. if (!is_muted(&snac1, xs_dict_get(msg, "attributedTo"))) {
  1130. /* return the status itself */
  1131. out = mastoapi_status(&snac1, msg);
  1132. }
  1133. }
  1134. else
  1135. if (strcmp(op, "context") == 0) { /** **/
  1136. /* return ancestors and children */
  1137. xs *anc = xs_list_new();
  1138. xs *des = xs_list_new();
  1139. xs_list *p;
  1140. xs_str *v;
  1141. char pid[64];
  1142. /* build the [grand]parent list, moving up */
  1143. strncpy(pid, id, sizeof(pid));
  1144. while (object_parent(pid, pid, sizeof(pid))) {
  1145. xs *m2 = NULL;
  1146. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  1147. xs *st = mastoapi_status(&snac1, m2);
  1148. anc = xs_list_insert(anc, 0, st);
  1149. }
  1150. else
  1151. break;
  1152. }
  1153. /* build the children list */
  1154. xs *children = object_children(xs_dict_get(msg, "id"));
  1155. p = children;
  1156. while (xs_list_iter(&p, &v)) {
  1157. xs *m2 = NULL;
  1158. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  1159. xs *st = mastoapi_status(&snac1, m2);
  1160. des = xs_list_append(des, st);
  1161. }
  1162. }
  1163. out = xs_dict_new();
  1164. out = xs_dict_append(out, "ancestors", anc);
  1165. out = xs_dict_append(out, "descendants", des);
  1166. }
  1167. else
  1168. if (strcmp(op, "reblogged_by") == 0 || /** **/
  1169. strcmp(op, "favourited_by") == 0) { /** **/
  1170. /* return the list of people who liked or boosted this */
  1171. out = xs_list_new();
  1172. xs *l = NULL;
  1173. if (op[0] == 'r')
  1174. l = object_announces(xs_dict_get(msg, "id"));
  1175. else
  1176. l = object_likes(xs_dict_get(msg, "id"));
  1177. xs_list *p = l;
  1178. xs_str *v;
  1179. while (xs_list_iter(&p, &v)) {
  1180. xs *actor2 = NULL;
  1181. if (valid_status(object_get_by_md5(v, &actor2))) {
  1182. xs *acct2 = mastoapi_account(actor2);
  1183. out = xs_list_append(out, acct2);
  1184. }
  1185. }
  1186. }
  1187. }
  1188. else
  1189. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  1190. if (out != NULL) {
  1191. *body = xs_json_dumps_pp(out, 4);
  1192. *ctype = "application/json";
  1193. status = 200;
  1194. }
  1195. }
  1196. }
  1197. else
  1198. if (strcmp(cmd, "/v1/preferences") == 0) { /** **/
  1199. *body = xs_dup("{}");
  1200. *ctype = "application/json";
  1201. status = 200;
  1202. }
  1203. else
  1204. if (strcmp(cmd, "/v1/markers") == 0) { /** **/
  1205. *body = xs_dup("{}");
  1206. *ctype = "application/json";
  1207. status = 200;
  1208. }
  1209. else
  1210. if (strcmp(cmd, "/v1/followed_tags") == 0) { /** **/
  1211. *body = xs_dup("[]");
  1212. *ctype = "application/json";
  1213. status = 200;
  1214. }
  1215. else
  1216. if (strcmp(cmd, "/v2/search") == 0) { /** **/
  1217. const char *q = xs_dict_get(args, "q");
  1218. const char *type = xs_dict_get(args, "type");
  1219. const char *offset = xs_dict_get(args, "offset");
  1220. xs *acl = xs_list_new();
  1221. xs *stl = xs_list_new();
  1222. xs *htl = xs_list_new();
  1223. xs *res = xs_dict_new();
  1224. if (xs_is_null(offset) || strcmp(offset, "0") == 0) {
  1225. /* reply something only for offset 0; otherwise,
  1226. apps like Tusky keep asking again and again */
  1227. if (!xs_is_null(q) && !xs_is_null(type) && strcmp(type, "accounts") == 0) {
  1228. /* do a webfinger query */
  1229. char *actor = NULL;
  1230. char *user = NULL;
  1231. if (valid_status(webfinger_request(q, &actor, &user))) {
  1232. xs *actor_o = NULL;
  1233. if (valid_status(actor_request(&snac1, actor, &actor_o))) {
  1234. xs *acct = mastoapi_account(actor_o);
  1235. acl = xs_list_append(acl, acct);
  1236. }
  1237. }
  1238. }
  1239. }
  1240. res = xs_dict_append(res, "accounts", acl);
  1241. res = xs_dict_append(res, "statuses", stl);
  1242. res = xs_dict_append(res, "hashtags", htl);
  1243. *body = xs_json_dumps_pp(res, 4);
  1244. *ctype = "application/json";
  1245. status = 200;
  1246. }
  1247. /* user cleanup */
  1248. if (logged_in)
  1249. user_free(&snac1);
  1250. return status;
  1251. }
  1252. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  1253. const char *payload, int p_size,
  1254. char **body, int *b_size, char **ctype)
  1255. {
  1256. (void)p_size;
  1257. (void)b_size;
  1258. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1259. return 0;
  1260. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1261. /* {
  1262. xs *j = xs_json_dumps_pp(req, 4);
  1263. printf("mastoapi post:\n%s\n", j);
  1264. }*/
  1265. int status = 404;
  1266. xs *args = NULL;
  1267. char *i_ctype = xs_dict_get(req, "content-type");
  1268. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1269. args = xs_json_loads(payload);
  1270. else
  1271. args = xs_dup(xs_dict_get(req, "p_vars"));
  1272. if (args == NULL)
  1273. return 400;
  1274. /* {
  1275. xs *j = xs_json_dumps_pp(args, 4);
  1276. printf("%s\n", j);
  1277. }*/
  1278. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1279. snac snac = {0};
  1280. int logged_in = process_auth_token(&snac, req);
  1281. if (strcmp(cmd, "/v1/apps") == 0) { /** **/
  1282. const char *name = xs_dict_get(args, "client_name");
  1283. const char *ruri = xs_dict_get(args, "redirect_uris");
  1284. const char *scope = xs_dict_get(args, "scope");
  1285. if (xs_type(ruri) == XSTYPE_LIST)
  1286. ruri = xs_dict_get(ruri, 0);
  1287. if (name && ruri) {
  1288. xs *app = xs_dict_new();
  1289. xs *id = xs_replace_i(tid(0), ".", "");
  1290. xs *csec = random_str();
  1291. xs *vkey = random_str();
  1292. xs *cid = NULL;
  1293. /* pick a non-existent random cid */
  1294. for (;;) {
  1295. cid = random_str();
  1296. xs *p_app = app_get(cid);
  1297. if (p_app == NULL)
  1298. break;
  1299. xs_free(cid);
  1300. }
  1301. app = xs_dict_append(app, "name", name);
  1302. app = xs_dict_append(app, "redirect_uri", ruri);
  1303. app = xs_dict_append(app, "client_id", cid);
  1304. app = xs_dict_append(app, "client_secret", csec);
  1305. app = xs_dict_append(app, "vapid_key", vkey);
  1306. app = xs_dict_append(app, "id", id);
  1307. *body = xs_json_dumps_pp(app, 4);
  1308. *ctype = "application/json";
  1309. status = 200;
  1310. app = xs_dict_append(app, "code", "");
  1311. if (scope)
  1312. app = xs_dict_append(app, "scope", scope);
  1313. app_add(cid, app);
  1314. srv_debug(1, xs_fmt("mastoapi apps: new app %s", cid));
  1315. }
  1316. }
  1317. else
  1318. if (strcmp(cmd, "/v1/statuses") == 0) { /** **/
  1319. if (logged_in) {
  1320. /* post a new Note */
  1321. /* {
  1322. xs *j = xs_json_dumps_pp(args, 4);
  1323. printf("%s\n", j);
  1324. }*/
  1325. const char *content = xs_dict_get(args, "status");
  1326. const char *mid = xs_dict_get(args, "in_reply_to_id");
  1327. const char *visibility = xs_dict_get(args, "visibility");
  1328. const char *summary = xs_dict_get(args, "spoiler_text");
  1329. const char *media_ids = xs_dict_get(args, "media_ids");
  1330. if (xs_is_null(media_ids))
  1331. media_ids = xs_dict_get(args, "media_ids[]");
  1332. if (xs_is_null(visibility))
  1333. visibility = "public";
  1334. xs *attach_list = xs_list_new();
  1335. xs *irt = NULL;
  1336. /* is it a reply? */
  1337. if (mid != NULL) {
  1338. xs *r_msg = NULL;
  1339. const char *md5 = MID_TO_MD5(mid);
  1340. if (valid_status(object_get_by_md5(md5, &r_msg)))
  1341. irt = xs_dup(xs_dict_get(r_msg, "id"));
  1342. }
  1343. /* does it have attachments? */
  1344. if (!xs_is_null(media_ids)) {
  1345. xs *mi = NULL;
  1346. if (xs_type(media_ids) == XSTYPE_LIST)
  1347. mi = xs_dup(media_ids);
  1348. else {
  1349. mi = xs_list_new();
  1350. mi = xs_list_append(mi, media_ids);
  1351. }
  1352. xs_list *p = mi;
  1353. xs_str *v;
  1354. while (xs_list_iter(&p, &v)) {
  1355. xs *l = xs_list_new();
  1356. xs *url = xs_fmt("%s/s/%s", snac.actor, v);
  1357. xs *desc = static_get_meta(&snac, v);
  1358. l = xs_list_append(l, url);
  1359. l = xs_list_append(l, desc);
  1360. attach_list = xs_list_append(attach_list, l);
  1361. }
  1362. }
  1363. /* prepare the message */
  1364. xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
  1365. strcmp(visibility, "public") == 0 ? 0 : 1);
  1366. if (!xs_is_null(summary) && *summary) {
  1367. xs *t = xs_val_new(XSTYPE_TRUE);
  1368. msg = xs_dict_set(msg, "sensitive", t);
  1369. msg = xs_dict_set(msg, "summary", summary);
  1370. }
  1371. /* store */
  1372. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1373. /* 'Create' message */
  1374. xs *c_msg = msg_create(&snac, msg);
  1375. enqueue_message(&snac, c_msg);
  1376. timeline_touch(&snac);
  1377. /* convert to a mastodon status as a response code */
  1378. xs *st = mastoapi_status(&snac, msg);
  1379. *body = xs_json_dumps_pp(st, 4);
  1380. *ctype = "application/json";
  1381. status = 200;
  1382. }
  1383. else
  1384. status = 401;
  1385. }
  1386. else
  1387. if (xs_startswith(cmd, "/v1/statuses")) { /** **/
  1388. if (logged_in) {
  1389. /* operations on a status */
  1390. xs *l = xs_split(cmd, "/");
  1391. const char *mid = xs_list_get(l, 3);
  1392. const char *op = xs_list_get(l, 4);
  1393. if (!xs_is_null(mid)) {
  1394. xs *msg = NULL;
  1395. xs *out = NULL;
  1396. /* skip the 'fake' part of the id */
  1397. mid = MID_TO_MD5(mid);
  1398. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1399. char *id = xs_dict_get(msg, "id");
  1400. if (op == NULL) {
  1401. /* no operation (?) */
  1402. }
  1403. else
  1404. if (strcmp(op, "favourite") == 0) { /** **/
  1405. xs *n_msg = msg_admiration(&snac, id, "Like");
  1406. if (n_msg != NULL) {
  1407. enqueue_message(&snac, n_msg);
  1408. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  1409. out = mastoapi_status(&snac, msg);
  1410. }
  1411. }
  1412. else
  1413. if (strcmp(op, "unfavourite") == 0) { /** **/
  1414. /* partial support: as the original Like message
  1415. is not stored anywhere here, it's not possible
  1416. to send an Undo + Like; the only thing done here
  1417. is to delete the actor from the list of likes */
  1418. object_unadmire(id, snac.actor, 1);
  1419. }
  1420. else
  1421. if (strcmp(op, "reblog") == 0) { /** **/
  1422. xs *n_msg = msg_admiration(&snac, id, "Announce");
  1423. if (n_msg != NULL) {
  1424. enqueue_message(&snac, n_msg);
  1425. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  1426. out = mastoapi_status(&snac, msg);
  1427. }
  1428. }
  1429. else
  1430. if (strcmp(op, "unreblog") == 0) { /** **/
  1431. /* partial support: see comment in 'unfavourite' */
  1432. object_unadmire(id, snac.actor, 0);
  1433. }
  1434. else
  1435. if (strcmp(op, "bookmark") == 0) { /** **/
  1436. /* snac does not support bookmarks */
  1437. }
  1438. else
  1439. if (strcmp(op, "unbookmark") == 0) { /** **/
  1440. /* snac does not support bookmarks */
  1441. }
  1442. else
  1443. if (strcmp(op, "pin") == 0) { /** **/
  1444. /* snac does not support pinning */
  1445. }
  1446. else
  1447. if (strcmp(op, "unpin") == 0) { /** **/
  1448. /* snac does not support pinning */
  1449. }
  1450. else
  1451. if (strcmp(op, "mute") == 0) { /** **/
  1452. /* Mastodon's mute is snac's hide */
  1453. }
  1454. else
  1455. if (strcmp(op, "unmute") == 0) { /** **/
  1456. /* Mastodon's unmute is snac's unhide */
  1457. }
  1458. }
  1459. if (out != NULL) {
  1460. *body = xs_json_dumps_pp(out, 4);
  1461. *ctype = "application/json";
  1462. status = 200;
  1463. }
  1464. }
  1465. }
  1466. else
  1467. status = 401;
  1468. }
  1469. else
  1470. if (strcmp(cmd, "/v1/notifications/clear") == 0) { /** **/
  1471. if (logged_in) {
  1472. notify_clear(&snac);
  1473. timeline_touch(&snac);
  1474. *body = xs_dup("{}");
  1475. *ctype = "application/json";
  1476. status = 200;
  1477. }
  1478. else
  1479. status = 401;
  1480. }
  1481. else
  1482. if (strcmp(cmd, "/v1/push/subscription") == 0) { /** **/
  1483. /* I don't know what I'm doing */
  1484. if (logged_in) {
  1485. char *v;
  1486. xs *wpush = xs_dict_new();
  1487. wpush = xs_dict_append(wpush, "id", "1");
  1488. v = xs_dict_get(args, "data");
  1489. v = xs_dict_get(v, "alerts");
  1490. wpush = xs_dict_append(wpush, "alerts", v);
  1491. v = xs_dict_get(args, "subscription");
  1492. v = xs_dict_get(v, "endpoint");
  1493. wpush = xs_dict_append(wpush, "endpoint", v);
  1494. xs *server_key = random_str();
  1495. wpush = xs_dict_append(wpush, "server_key", server_key);
  1496. *body = xs_json_dumps_pp(wpush, 4);
  1497. *ctype = "application/json";
  1498. status = 200;
  1499. }
  1500. else
  1501. status = 401;
  1502. }
  1503. else
  1504. if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { /** **/
  1505. if (logged_in) {
  1506. /* {
  1507. xs *j = xs_json_dumps_pp(args, 4);
  1508. printf("%s\n", j);
  1509. }*/
  1510. const xs_list *file = xs_dict_get(args, "file");
  1511. const char *desc = xs_dict_get(args, "description");
  1512. if (xs_is_null(desc))
  1513. desc = "";
  1514. status = 400;
  1515. if (xs_type(file) == XSTYPE_LIST) {
  1516. const char *fn = xs_list_get(file, 0);
  1517. if (*fn != '\0') {
  1518. char *ext = strrchr(fn, '.');
  1519. xs *hash = xs_md5_hex(fn, strlen(fn));
  1520. xs *id = xs_fmt("%s%s", hash, ext);
  1521. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1522. int fo = xs_number_get(xs_list_get(file, 1));
  1523. int fs = xs_number_get(xs_list_get(file, 2));
  1524. /* store */
  1525. static_put(&snac, id, payload + fo, fs);
  1526. static_put_meta(&snac, id, desc);
  1527. /* prepare a response */
  1528. xs *rsp = xs_dict_new();
  1529. rsp = xs_dict_append(rsp, "id", id);
  1530. rsp = xs_dict_append(rsp, "type", "image");
  1531. rsp = xs_dict_append(rsp, "url", url);
  1532. rsp = xs_dict_append(rsp, "preview_url", url);
  1533. rsp = xs_dict_append(rsp, "remote_url", url);
  1534. rsp = xs_dict_append(rsp, "description", desc);
  1535. *body = xs_json_dumps_pp(rsp, 4);
  1536. *ctype = "application/json";
  1537. status = 200;
  1538. }
  1539. }
  1540. }
  1541. else
  1542. status = 401;
  1543. }
  1544. else
  1545. if (xs_startswith(cmd, "/v1/accounts")) { /** **/
  1546. if (logged_in) {
  1547. /* account-related information */
  1548. xs *l = xs_split(cmd, "/");
  1549. const char *md5 = xs_list_get(l, 3);
  1550. const char *opt = xs_list_get(l, 4);
  1551. xs *rsp = NULL;
  1552. if (!xs_is_null(md5) && *md5) {
  1553. xs *actor_o = NULL;
  1554. if (xs_is_null(opt)) {
  1555. /* ? */
  1556. }
  1557. else
  1558. if (strcmp(opt, "follow") == 0) { /** **/
  1559. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1560. const char *actor = xs_dict_get(actor_o, "id");
  1561. xs *msg = msg_follow(&snac, actor);
  1562. if (msg != NULL) {
  1563. /* reload the actor from the message, in may be different */
  1564. actor = xs_dict_get(msg, "object");
  1565. following_add(&snac, actor, msg);
  1566. enqueue_output_by_actor(&snac, msg, actor, 0);
  1567. rsp = mastoapi_relationship(&snac, md5);
  1568. }
  1569. }
  1570. }
  1571. else
  1572. if (strcmp(opt, "unfollow") == 0) { /** **/
  1573. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1574. const char *actor = xs_dict_get(actor_o, "id");
  1575. /* get the following object */
  1576. xs *object = NULL;
  1577. if (valid_status(following_get(&snac, actor, &object))) {
  1578. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  1579. following_del(&snac, actor);
  1580. enqueue_output_by_actor(&snac, msg, actor, 0);
  1581. rsp = mastoapi_relationship(&snac, md5);
  1582. }
  1583. }
  1584. }
  1585. }
  1586. if (rsp != NULL) {
  1587. *body = xs_json_dumps_pp(rsp, 4);
  1588. *ctype = "application/json";
  1589. status = 200;
  1590. }
  1591. }
  1592. else
  1593. status = 401;
  1594. }
  1595. /* user cleanup */
  1596. if (logged_in)
  1597. user_free(&snac);
  1598. return status;
  1599. }
  1600. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  1601. const char *payload, int p_size,
  1602. char **body, int *b_size, char **ctype)
  1603. {
  1604. (void)p_size;
  1605. (void)b_size;
  1606. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1607. return 0;
  1608. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1609. /* {
  1610. xs *j = xs_json_dumps_pp(req, 4);
  1611. printf("mastoapi put:\n%s\n", j);
  1612. }*/
  1613. int status = 404;
  1614. xs *args = NULL;
  1615. char *i_ctype = xs_dict_get(req, "content-type");
  1616. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1617. args = xs_json_loads(payload);
  1618. else
  1619. args = xs_dup(xs_dict_get(req, "p_vars"));
  1620. if (args == NULL)
  1621. return 400;
  1622. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1623. snac snac = {0};
  1624. int logged_in = process_auth_token(&snac, req);
  1625. if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) { /** **/
  1626. if (logged_in) {
  1627. xs *l = xs_split(cmd, "/");
  1628. const char *stid = xs_list_get(l, 3);
  1629. if (!xs_is_null(stid)) {
  1630. const char *desc = xs_dict_get(args, "description");
  1631. /* set the image metadata */
  1632. static_put_meta(&snac, stid, desc);
  1633. /* prepare a response */
  1634. xs *rsp = xs_dict_new();
  1635. xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
  1636. rsp = xs_dict_append(rsp, "id", stid);
  1637. rsp = xs_dict_append(rsp, "type", "image");
  1638. rsp = xs_dict_append(rsp, "url", url);
  1639. rsp = xs_dict_append(rsp, "preview_url", url);
  1640. rsp = xs_dict_append(rsp, "remote_url", url);
  1641. rsp = xs_dict_append(rsp, "description", desc);
  1642. *body = xs_json_dumps_pp(rsp, 4);
  1643. *ctype = "application/json";
  1644. status = 200;
  1645. }
  1646. }
  1647. else
  1648. status = 401;
  1649. }
  1650. /* user cleanup */
  1651. if (logged_in)
  1652. user_free(&snac);
  1653. return status;
  1654. }
  1655. void mastoapi_purge(void)
  1656. {
  1657. xs *spec = xs_fmt("%s/app/" "*.json", srv_basedir);
  1658. xs *files = xs_glob(spec, 1, 0);
  1659. xs_list *p = files;
  1660. xs_str *v;
  1661. time_t mt = time(NULL) - 3600;
  1662. while (xs_list_iter(&p, &v)) {
  1663. xs *cid = xs_replace(v, ".json", "");
  1664. xs *fn = _app_fn(cid);
  1665. if (mtime(fn) < mt) {
  1666. /* get the app */
  1667. xs *app = app_get(cid);
  1668. if (app) {
  1669. /* old apps with no uid are incomplete cruft */
  1670. const char *uid = xs_dict_get(app, "uid");
  1671. if (xs_is_null(uid) || *uid == '\0') {
  1672. unlink(fn);
  1673. srv_debug(2, xs_fmt("purged %s", fn));
  1674. }
  1675. }
  1676. }
  1677. }
  1678. }
  1679. #endif /* #ifndef NO_MASTODON_API */