mastoapi.c 66 KB

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