mastoapi.c 74 KB

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