mastoapi.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 grunfink et al. / 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 "xs_url.h"
  13. #include "xs_mime.h"
  14. #include "snac.h"
  15. static xs_str *random_str(void)
  16. /* just what is says in the tin */
  17. {
  18. unsigned int data[4] = {0};
  19. xs_rnd_buf(data, sizeof(data));
  20. return xs_hex_enc((char *)data, sizeof(data));
  21. }
  22. int app_add(const char *id, const xs_dict *app)
  23. /* stores an app */
  24. {
  25. if (!xs_is_hex(id))
  26. return 500;
  27. int status = 201;
  28. xs *fn = xs_fmt("%s/app/", srv_basedir);
  29. FILE *f;
  30. mkdirx(fn);
  31. fn = xs_str_cat(fn, id);
  32. fn = xs_str_cat(fn, ".json");
  33. if ((f = fopen(fn, "w")) != NULL) {
  34. xs_json_dump(app, 4, f);
  35. fclose(f);
  36. }
  37. else
  38. status = 500;
  39. return status;
  40. }
  41. xs_str *_app_fn(const char *id)
  42. {
  43. return xs_fmt("%s/app/%s.json", srv_basedir, id);
  44. }
  45. xs_dict *app_get(const char *id)
  46. /* gets an app */
  47. {
  48. if (!xs_is_hex(id))
  49. return NULL;
  50. xs *fn = _app_fn(id);
  51. xs_dict *app = NULL;
  52. FILE *f;
  53. if ((f = fopen(fn, "r")) != NULL) {
  54. app = xs_json_load(f);
  55. fclose(f);
  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_json_dump(token, 4, f);
  80. fclose(f);
  81. }
  82. else
  83. status = 500;
  84. return status;
  85. }
  86. xs_dict *token_get(const char *id)
  87. /* gets a token */
  88. {
  89. if (!xs_is_hex(id))
  90. return NULL;
  91. xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
  92. xs_dict *token = NULL;
  93. FILE *f;
  94. if ((f = fopen(fn, "r")) != NULL) {
  95. token = xs_json_load(f);
  96. fclose(f);
  97. }
  98. return token;
  99. }
  100. int token_del(const char *id)
  101. /* deletes a token */
  102. {
  103. if (!xs_is_hex(id))
  104. return -1;
  105. xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
  106. return unlink(fn);
  107. }
  108. const char *login_page = ""
  109. "<!DOCTYPE html>\n"
  110. "<html>\n"
  111. "<head>\n"
  112. "<title>%s OAuth - Snac2</title>\n"
  113. "<style>:root {color-scheme: light dark}</style>\n"
  114. "</head>\n"
  115. "<body><h1>%s OAuth identify</h1>\n"
  116. "<div style=\"background-color: red; color: white\">%s</div>\n"
  117. "<form method=\"post\" action=\"https:/" "/%s/%s\">\n"
  118. "<p>Login: <input type=\"text\" name=\"login\"></p>\n"
  119. "<p>Password: <input type=\"password\" name=\"passwd\"></p>\n"
  120. "<input type=\"hidden\" name=\"redir\" value=\"%s\">\n"
  121. "<input type=\"hidden\" name=\"cid\" value=\"%s\">\n"
  122. "<input type=\"hidden\" name=\"state\" value=\"%s\">\n"
  123. "<input type=\"submit\" value=\"OK\">\n"
  124. "</form><p>%s</p></body></html>\n"
  125. "";
  126. int oauth_get_handler(const xs_dict *req, const char *q_path,
  127. char **body, int *b_size, char **ctype)
  128. {
  129. (void)b_size;
  130. if (!xs_startswith(q_path, "/oauth/"))
  131. return 0;
  132. int status = 404;
  133. xs_dict *msg = xs_dict_get(req, "q_vars");
  134. xs *cmd = xs_replace_n(q_path, "/oauth", "", 1);
  135. srv_debug(1, xs_fmt("oauth_get_handler %s", q_path));
  136. if (strcmp(cmd, "/authorize") == 0) { /** **/
  137. const char *cid = xs_dict_get(msg, "client_id");
  138. const char *ruri = xs_dict_get(msg, "redirect_uri");
  139. const char *rtype = xs_dict_get(msg, "response_type");
  140. const char *state = xs_dict_get(msg, "state");
  141. status = 400;
  142. if (cid && ruri && rtype && strcmp(rtype, "code") == 0) {
  143. xs *app = app_get(cid);
  144. if (app != NULL) {
  145. const char *host = xs_dict_get(srv_config, "host");
  146. if (xs_is_null(state))
  147. state = "";
  148. *body = xs_fmt(login_page, host, host, "", host, "oauth/x-snac-login",
  149. ruri, cid, state, USER_AGENT);
  150. *ctype = "text/html";
  151. status = 200;
  152. srv_debug(1, xs_fmt("oauth authorize: generating login page"));
  153. }
  154. else
  155. srv_debug(1, xs_fmt("oauth authorize: bad client_id %s", cid));
  156. }
  157. else
  158. srv_debug(1, xs_fmt("oauth authorize: invalid or unset arguments"));
  159. }
  160. else
  161. if (strcmp(cmd, "/x-snac-get-token") == 0) { /** **/
  162. const char *host = xs_dict_get(srv_config, "host");
  163. *body = xs_fmt(login_page, host, host, "", host, "oauth/x-snac-get-token",
  164. "", "", "", USER_AGENT);
  165. *ctype = "text/html";
  166. status = 200;
  167. }
  168. return status;
  169. }
  170. int oauth_post_handler(const xs_dict *req, const char *q_path,
  171. const char *payload, int p_size,
  172. char **body, int *b_size, char **ctype)
  173. {
  174. (void)p_size;
  175. (void)b_size;
  176. if (!xs_startswith(q_path, "/oauth/"))
  177. return 0;
  178. int status = 404;
  179. char *i_ctype = xs_dict_get(req, "content-type");
  180. xs *args = NULL;
  181. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  182. args = xs_json_loads(payload);
  183. else
  184. if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded") && payload) {
  185. xs *upl = xs_url_dec(payload);
  186. args = xs_url_vars(upl);
  187. }
  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, 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(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, 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", xs_stock_true);
  412. else
  413. acct = xs_dict_append(acct, "bot", xs_stock_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. acct = xs_dict_append(acct, "avatar_static", avatar);
  424. xs *header = NULL;
  425. xs_dict *hd = xs_dict_get(actor, "image");
  426. if (xs_type(hd) == XSTYPE_DICT)
  427. header = xs_dup(xs_dict_get(hd, "url"));
  428. if (xs_is_null(header))
  429. header = xs_dup("");
  430. acct = xs_dict_append(acct, "header", header);
  431. acct = xs_dict_append(acct, "header_static", header);
  432. /* emojis */
  433. xs_list *p;
  434. if (!xs_is_null(p = xs_dict_get(actor, "tag"))) {
  435. xs *eml = xs_list_new();
  436. xs_dict *v;
  437. while (xs_list_iter(&p, &v)) {
  438. const char *type = xs_dict_get(v, "type");
  439. if (!xs_is_null(type) && strcmp(type, "Emoji") == 0) {
  440. const char *name = xs_dict_get(v, "name");
  441. const xs_dict *icon = xs_dict_get(v, "icon");
  442. if (!xs_is_null(name) && !xs_is_null(icon)) {
  443. const char *url = xs_dict_get(icon, "url");
  444. if (!xs_is_null(url)) {
  445. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  446. xs *d1 = xs_dict_new();
  447. d1 = xs_dict_append(d1, "shortcode", nm);
  448. d1 = xs_dict_append(d1, "url", url);
  449. d1 = xs_dict_append(d1, "static_url", url);
  450. d1 = xs_dict_append(d1, "visible_in_picker", xs_stock_true);
  451. eml = xs_list_append(eml, d1);
  452. }
  453. }
  454. }
  455. }
  456. acct = xs_dict_append(acct, "emojis", eml);
  457. }
  458. return acct;
  459. }
  460. xs_dict *mastoapi_poll(snac *snac, const xs_dict *msg)
  461. /* creates a mastoapi Poll object */
  462. {
  463. xs_dict *poll = xs_dict_new();
  464. xs *mid = mastoapi_id(msg);
  465. xs_list *opts = NULL;
  466. xs_val *v;
  467. int num_votes = 0;
  468. xs *options = xs_list_new();
  469. poll = xs_dict_append(poll, "id", mid);
  470. poll = xs_dict_append(poll, "expires_at", xs_dict_get(msg, "endTime"));
  471. poll = xs_dict_append(poll, "expired",
  472. xs_dict_get(msg, "closed") != NULL ? xs_stock_true : xs_stock_false);
  473. if ((opts = xs_dict_get(msg, "oneOf")) != NULL)
  474. poll = xs_dict_append(poll, "multiple", xs_stock_false);
  475. else {
  476. opts = xs_dict_get(msg, "anyOf");
  477. poll = xs_dict_append(poll, "multiple", xs_stock_true);
  478. }
  479. while (xs_list_iter(&opts, &v)) {
  480. const char *title = xs_dict_get(v, "name");
  481. const char *replies = xs_dict_get(v, "replies");
  482. if (title && replies) {
  483. const char *votes_count = xs_dict_get(replies, "totalItems");
  484. if (xs_type(votes_count) == XSTYPE_NUMBER) {
  485. xs *d = xs_dict_new();
  486. d = xs_dict_append(d, "title", title);
  487. d = xs_dict_append(d, "votes_count", votes_count);
  488. options = xs_list_append(options, d);
  489. num_votes += xs_number_get(votes_count);
  490. }
  491. }
  492. }
  493. poll = xs_dict_append(poll, "options", options);
  494. xs *vc = xs_number_new(num_votes);
  495. poll = xs_dict_append(poll, "votes_count", vc);
  496. poll = xs_dict_append(poll, "voted",
  497. (snac && was_question_voted(snac, xs_dict_get(msg, "id"))) ?
  498. xs_stock_true : xs_stock_false);
  499. return poll;
  500. }
  501. xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
  502. /* converts an ActivityPub note to a Mastodon status */
  503. {
  504. xs *actor = NULL;
  505. actor_get(xs_dict_get(msg, "attributedTo"), &actor);
  506. /* if the author is not here, discard */
  507. if (actor == NULL)
  508. return NULL;
  509. const char *type = xs_dict_get(msg, "type");
  510. const char *id = xs_dict_get(msg, "id");
  511. xs *acct = mastoapi_account(actor);
  512. xs *idx = NULL;
  513. xs *ixc = NULL;
  514. char *tmp;
  515. xs *mid = mastoapi_id(msg);
  516. xs_dict *st = xs_dict_new();
  517. st = xs_dict_append(st, "id", mid);
  518. st = xs_dict_append(st, "uri", id);
  519. st = xs_dict_append(st, "url", id);
  520. st = xs_dict_append(st, "created_at", xs_dict_get(msg, "published"));
  521. st = xs_dict_append(st, "account", acct);
  522. {
  523. const char *content = xs_dict_get(msg, "content");
  524. const char *name = xs_dict_get(msg, "name");
  525. xs *s1 = NULL;
  526. if (name && content)
  527. s1 = xs_fmt("%s<br><br>%s", name, content);
  528. else
  529. if (name)
  530. s1 = xs_dup(name);
  531. else
  532. if (content)
  533. s1 = xs_dup(content);
  534. else
  535. s1 = xs_str_new(NULL);
  536. st = xs_dict_append(st, "content", s1);
  537. }
  538. st = xs_dict_append(st, "visibility",
  539. is_msg_public(msg) ? "public" : "private");
  540. tmp = xs_dict_get(msg, "sensitive");
  541. if (xs_is_null(tmp))
  542. tmp = xs_stock_false;
  543. st = xs_dict_append(st, "sensitive", tmp);
  544. tmp = xs_dict_get(msg, "summary");
  545. if (xs_is_null(tmp))
  546. tmp = "";
  547. st = xs_dict_append(st, "spoiler_text", tmp);
  548. /* create the list of attachments */
  549. xs *matt = xs_list_new();
  550. xs_list *att = xs_dict_get(msg, "attachment");
  551. xs_str *aobj;
  552. xs *attr_list = NULL;
  553. if (xs_type(att) == XSTYPE_DICT) {
  554. attr_list = xs_list_new();
  555. attr_list = xs_list_append(attr_list, att);
  556. }
  557. else
  558. if (xs_type(att) == XSTYPE_LIST)
  559. attr_list = xs_dup(att);
  560. else
  561. attr_list = xs_list_new();
  562. /* if it has an image, add it as an attachment */
  563. xs_dict *image = xs_dict_get(msg, "image");
  564. if (!xs_is_null(image))
  565. attr_list = xs_list_append(attr_list, image);
  566. att = attr_list;
  567. while (xs_list_iter(&att, &aobj)) {
  568. const char *mtype = xs_dict_get(aobj, "mediaType");
  569. if (xs_is_null(mtype))
  570. mtype = xs_dict_get(aobj, "type");
  571. const char *url = xs_dict_get(aobj, "url");
  572. if (xs_is_null(url))
  573. url = xs_dict_get(aobj, "href");
  574. if (xs_is_null(url))
  575. continue;
  576. /* if it's a plain Link, check if it can be "rewritten" */
  577. if (xs_list_len(attr_list) < 2 && strcmp(mtype, "Link") == 0) {
  578. const char *mt = xs_mime_by_ext(url);
  579. if (xs_startswith(mt, "image/") ||
  580. xs_startswith(mt, "audio/") ||
  581. xs_startswith(mt, "video/"))
  582. mtype = mt;
  583. }
  584. if (!xs_is_null(mtype)) {
  585. if (xs_startswith(mtype, "image/") || xs_startswith(mtype, "video/") ||
  586. strcmp(mtype, "Image") == 0) {
  587. xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
  588. xs *matte = xs_dict_new();
  589. matte = xs_dict_append(matte, "id", matteid);
  590. matte = xs_dict_append(matte, "type", *mtype == 'v' ? "video" : "image");
  591. matte = xs_dict_append(matte, "url", url);
  592. matte = xs_dict_append(matte, "preview_url", url);
  593. matte = xs_dict_append(matte, "remote_url", url);
  594. const char *name = xs_dict_get(aobj, "name");
  595. if (xs_is_null(name))
  596. name = "";
  597. matte = xs_dict_append(matte, "description", name);
  598. matt = xs_list_append(matt, matte);
  599. }
  600. }
  601. }
  602. st = xs_dict_append(st, "media_attachments", matt);
  603. {
  604. xs *ml = xs_list_new();
  605. xs *htl = xs_list_new();
  606. xs *eml = xs_list_new();
  607. xs_list *tag = xs_dict_get(msg, "tag");
  608. int n = 0;
  609. xs *tag_list = NULL;
  610. if (xs_type(tag) == XSTYPE_DICT) {
  611. tag_list = xs_list_new();
  612. tag_list = xs_list_append(tag_list, tag);
  613. }
  614. else
  615. if (xs_type(tag) == XSTYPE_LIST)
  616. tag_list = xs_dup(tag);
  617. else
  618. tag_list = xs_list_new();
  619. tag = tag_list;
  620. xs_dict *v;
  621. while (xs_list_iter(&tag, &v)) {
  622. const char *type = xs_dict_get(v, "type");
  623. if (xs_is_null(type))
  624. continue;
  625. xs *d1 = xs_dict_new();
  626. if (strcmp(type, "Mention") == 0) {
  627. const char *name = xs_dict_get(v, "name");
  628. const char *href = xs_dict_get(v, "href");
  629. if (!xs_is_null(name) && !xs_is_null(href) &&
  630. (snac == NULL || strcmp(href, snac->actor) != 0)) {
  631. xs *nm = xs_strip_chars_i(xs_dup(name), "@");
  632. xs *id = xs_fmt("%d", n++);
  633. d1 = xs_dict_append(d1, "id", id);
  634. d1 = xs_dict_append(d1, "username", nm);
  635. d1 = xs_dict_append(d1, "acct", nm);
  636. d1 = xs_dict_append(d1, "url", href);
  637. ml = xs_list_append(ml, d1);
  638. }
  639. }
  640. else
  641. if (strcmp(type, "Hashtag") == 0) {
  642. const char *name = xs_dict_get(v, "name");
  643. const char *href = xs_dict_get(v, "href");
  644. if (!xs_is_null(name) && !xs_is_null(href)) {
  645. xs *nm = xs_strip_chars_i(xs_dup(name), "#");
  646. d1 = xs_dict_append(d1, "name", nm);
  647. d1 = xs_dict_append(d1, "url", href);
  648. htl = xs_list_append(htl, d1);
  649. }
  650. }
  651. else
  652. if (strcmp(type, "Emoji") == 0) {
  653. const char *name = xs_dict_get(v, "name");
  654. const xs_dict *icon = xs_dict_get(v, "icon");
  655. if (!xs_is_null(name) && !xs_is_null(icon)) {
  656. const char *url = xs_dict_get(icon, "url");
  657. if (!xs_is_null(url)) {
  658. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  659. d1 = xs_dict_append(d1, "shortcode", nm);
  660. d1 = xs_dict_append(d1, "url", url);
  661. d1 = xs_dict_append(d1, "static_url", url);
  662. d1 = xs_dict_append(d1, "visible_in_picker", xs_stock_true);
  663. d1 = xs_dict_append(d1, "category", "Emojis");
  664. eml = xs_list_append(eml, d1);
  665. }
  666. }
  667. }
  668. }
  669. st = xs_dict_append(st, "mentions", ml);
  670. st = xs_dict_append(st, "tags", htl);
  671. st = xs_dict_append(st, "emojis", eml);
  672. }
  673. xs_free(idx);
  674. xs_free(ixc);
  675. idx = object_likes(id);
  676. ixc = xs_number_new(xs_list_len(idx));
  677. st = xs_dict_append(st, "favourites_count", ixc);
  678. st = xs_dict_append(st, "favourited",
  679. (snac && xs_list_in(idx, snac->md5) != -1) ? xs_stock_true : xs_stock_false);
  680. xs_free(idx);
  681. xs_free(ixc);
  682. idx = object_announces(id);
  683. ixc = xs_number_new(xs_list_len(idx));
  684. st = xs_dict_append(st, "reblogs_count", ixc);
  685. st = xs_dict_append(st, "reblogged",
  686. (snac && xs_list_in(idx, snac->md5) != -1) ? xs_stock_true : xs_stock_false);
  687. xs_free(idx);
  688. xs_free(ixc);
  689. idx = object_children(id);
  690. ixc = xs_number_new(xs_list_len(idx));
  691. st = xs_dict_append(st, "replies_count", ixc);
  692. /* default in_reply_to values */
  693. st = xs_dict_append(st, "in_reply_to_id", xs_stock_null);
  694. st = xs_dict_append(st, "in_reply_to_account_id", xs_stock_null);
  695. tmp = xs_dict_get(msg, "inReplyTo");
  696. if (!xs_is_null(tmp)) {
  697. xs *irto = NULL;
  698. if (valid_status(object_get(tmp, &irto))) {
  699. xs *irt_mid = mastoapi_id(irto);
  700. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  701. char *at = NULL;
  702. if (!xs_is_null(at = xs_dict_get(irto, "attributedTo"))) {
  703. xs *at_md5 = xs_md5_hex(at, strlen(at));
  704. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  705. }
  706. }
  707. }
  708. st = xs_dict_append(st, "reblog", xs_stock_null);
  709. st = xs_dict_append(st, "card", xs_stock_null);
  710. st = xs_dict_append(st, "language", xs_stock_null);
  711. tmp = xs_dict_get(msg, "sourceContent");
  712. if (xs_is_null(tmp))
  713. tmp = "";
  714. st = xs_dict_append(st, "text", tmp);
  715. tmp = xs_dict_get(msg, "updated");
  716. if (xs_is_null(tmp))
  717. tmp = xs_stock_null;
  718. st = xs_dict_append(st, "edited_at", tmp);
  719. if (strcmp(type, "Question") == 0) {
  720. xs *poll = mastoapi_poll(snac, msg);
  721. st = xs_dict_append(st, "poll", poll);
  722. }
  723. else
  724. st = xs_dict_append(st, "poll", xs_stock_null);
  725. st = xs_dict_append(st, "bookmarked", xs_stock_false);
  726. st = xs_dict_append(st, "pinned",
  727. (snac && is_pinned(snac, id)) ? xs_stock_true : xs_stock_false);
  728. return st;
  729. }
  730. xs_dict *mastoapi_relationship(snac *snac, const char *md5)
  731. {
  732. xs_dict *rel = NULL;
  733. xs *actor_o = NULL;
  734. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  735. rel = xs_dict_new();
  736. const char *actor = xs_dict_get(actor_o, "id");
  737. rel = xs_dict_append(rel, "id", md5);
  738. rel = xs_dict_append(rel, "following",
  739. following_check(snac, actor) ? xs_stock_true : xs_stock_false);
  740. rel = xs_dict_append(rel, "showing_reblogs", xs_stock_true);
  741. rel = xs_dict_append(rel, "notifying", xs_stock_false);
  742. rel = xs_dict_append(rel, "followed_by",
  743. follower_check(snac, actor) ? xs_stock_true : xs_stock_false);
  744. rel = xs_dict_append(rel, "blocking",
  745. is_muted(snac, actor) ? xs_stock_true : xs_stock_false);
  746. rel = xs_dict_append(rel, "muting", xs_stock_false);
  747. rel = xs_dict_append(rel, "muting_notifications", xs_stock_false);
  748. rel = xs_dict_append(rel, "requested", xs_stock_false);
  749. rel = xs_dict_append(rel, "domain_blocking", xs_stock_false);
  750. rel = xs_dict_append(rel, "endorsed", xs_stock_false);
  751. rel = xs_dict_append(rel, "note", "");
  752. }
  753. return rel;
  754. }
  755. int process_auth_token(snac *snac, const xs_dict *req)
  756. /* processes an authorization token, if there is one */
  757. {
  758. int logged_in = 0;
  759. char *v;
  760. /* if there is an authorization field, try to validate it */
  761. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  762. xs *tokid = xs_replace_n(v, "Bearer ", "", 1);
  763. xs *token = token_get(tokid);
  764. if (token != NULL) {
  765. const char *uid = xs_dict_get(token, "uid");
  766. if (!xs_is_null(uid) && user_open(snac, uid)) {
  767. logged_in = 1;
  768. /* this counts as a 'login' */
  769. lastlog_write(snac, "mastoapi");
  770. srv_debug(2, xs_fmt("mastoapi auth: valid token for user %s", uid));
  771. }
  772. else
  773. srv_log(xs_fmt("mastoapi auth: corrupted token %s", tokid));
  774. }
  775. else
  776. srv_log(xs_fmt("mastoapi auth: invalid token %s", tokid));
  777. }
  778. return logged_in;
  779. }
  780. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  781. char **body, int *b_size, char **ctype)
  782. {
  783. (void)b_size;
  784. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  785. return 0;
  786. srv_debug(1, xs_fmt("mastoapi_get_handler %s", q_path));
  787. int status = 404;
  788. xs_dict *args = xs_dict_get(req, "q_vars");
  789. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  790. snac snac1 = {0};
  791. int logged_in = process_auth_token(&snac1, req);
  792. if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) { /** **/
  793. if (logged_in) {
  794. xs *acct = xs_dict_new();
  795. acct = xs_dict_append(acct, "id", snac1.md5);
  796. acct = xs_dict_append(acct, "username", xs_dict_get(snac1.config, "uid"));
  797. acct = xs_dict_append(acct, "acct", xs_dict_get(snac1.config, "uid"));
  798. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac1.config, "name"));
  799. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac1.config, "published"));
  800. acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio"));
  801. acct = xs_dict_append(acct, "url", snac1.actor);
  802. acct = xs_dict_append(acct, "header", "");
  803. xs *src = xs_json_loads("{\"privacy\":\"public\","
  804. "\"sensitive\":false,\"fields\":[],\"note\":\"\"}");
  805. acct = xs_dict_append(acct, "source", src);
  806. xs *avatar = NULL;
  807. char *av = xs_dict_get(snac1.config, "avatar");
  808. if (xs_is_null(av) || *av == '\0')
  809. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  810. else
  811. avatar = xs_dup(av);
  812. acct = xs_dict_append(acct, "avatar", avatar);
  813. acct = xs_dict_append(acct, "avatar_static", avatar);
  814. xs_dict *metadata = xs_dict_get(snac1.config, "metadata");
  815. if (xs_type(metadata) == XSTYPE_DICT) {
  816. xs *fields = xs_list_new();
  817. xs_str *k;
  818. xs_str *v;
  819. while (xs_dict_iter(&metadata, &k, &v)) {
  820. xs *d = xs_dict_new();
  821. d = xs_dict_append(d, "name", k);
  822. d = xs_dict_append(d, "value", v);
  823. d = xs_dict_append(d, "verified_at", xs_stock_null);
  824. fields = xs_list_append(fields, d);
  825. }
  826. acct = xs_dict_set(acct, "fields", fields);
  827. }
  828. *body = xs_json_dumps(acct, 4);
  829. *ctype = "application/json";
  830. status = 200;
  831. }
  832. else {
  833. status = 422; // "Unprocessable entity" (no login)
  834. }
  835. }
  836. else
  837. if (strcmp(cmd, "/v1/accounts/relationships") == 0) { /** **/
  838. /* find if an account is followed, blocked, etc. */
  839. /* the account to get relationships about is in args "id[]" */
  840. if (logged_in) {
  841. xs *res = xs_list_new();
  842. const char *md5 = xs_dict_get(args, "id[]");
  843. if (xs_is_null(md5))
  844. md5 = xs_dict_get(args, "id");
  845. if (!xs_is_null(md5)) {
  846. if (xs_type(md5) == XSTYPE_LIST)
  847. md5 = xs_list_get(md5, 0);
  848. xs *rel = mastoapi_relationship(&snac1, md5);
  849. if (rel != NULL)
  850. res = xs_list_append(res, rel);
  851. }
  852. *body = xs_json_dumps(res, 4);
  853. *ctype = "application/json";
  854. status = 200;
  855. }
  856. else
  857. status = 422;
  858. }
  859. else
  860. if (xs_startswith(cmd, "/v1/accounts/")) { /** **/
  861. /* account-related information */
  862. xs *l = xs_split(cmd, "/");
  863. const char *uid = xs_list_get(l, 3);
  864. const char *opt = xs_list_get(l, 4);
  865. if (uid != NULL) {
  866. snac snac2;
  867. xs *out = NULL;
  868. xs *actor = NULL;
  869. if (logged_in && strcmp(uid, "search") == 0) { /** **/
  870. /* search for accounts starting with q */
  871. const char *aq = xs_dict_get(args, "q");
  872. if (!xs_is_null(aq)) {
  873. xs *q = xs_tolower_i(xs_dup(aq));
  874. out = xs_list_new();
  875. xs *wing = following_list(&snac1);
  876. xs *wers = follower_list(&snac1);
  877. xs *ulst = user_list();
  878. xs_list *p;
  879. xs_str *v;
  880. xs_set seen;
  881. xs_set_init(&seen);
  882. /* user relations */
  883. xs_list *lsts[] = { wing, wers, NULL };
  884. int n;
  885. for (n = 0; (p = lsts[n]) != NULL; n++) {
  886. while (xs_list_iter(&p, &v)) {
  887. /* already seen? skip */
  888. if (xs_set_add(&seen, v) == 0)
  889. continue;
  890. xs *actor = NULL;
  891. if (valid_status(object_get(v, &actor))) {
  892. const char *uname = xs_dict_get(actor, "preferredUsername");
  893. if (!xs_is_null(uname)) {
  894. xs *luname = xs_tolower_i(xs_dup(uname));
  895. if (xs_startswith(luname, q)) {
  896. xs *acct = mastoapi_account(actor);
  897. out = xs_list_append(out, acct);
  898. }
  899. }
  900. }
  901. }
  902. }
  903. /* local users */
  904. p = ulst;
  905. while (xs_list_iter(&p, &v)) {
  906. snac user;
  907. /* skip this same user */
  908. if (strcmp(v, xs_dict_get(snac1.config, "uid")) == 0)
  909. continue;
  910. /* skip if the uid does not start with the query */
  911. xs *v2 = xs_tolower_i(xs_dup(v));
  912. if (!xs_startswith(v2, q))
  913. continue;
  914. if (user_open(&user, v)) {
  915. /* if it's not already seen, add it */
  916. if (xs_set_add(&seen, user.actor) == 1) {
  917. xs *actor = msg_actor(&user);
  918. xs *acct = mastoapi_account(actor);
  919. out = xs_list_append(out, acct);
  920. }
  921. user_free(&user);
  922. }
  923. }
  924. xs_set_free(&seen);
  925. }
  926. }
  927. else
  928. /* is it a local user? */
  929. if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
  930. if (opt == NULL) {
  931. /* account information */
  932. actor = msg_actor(&snac2);
  933. out = mastoapi_account(actor);
  934. }
  935. else
  936. if (strcmp(opt, "statuses") == 0) { /** **/
  937. /* the public list of posts of a user */
  938. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  939. xs_list *p = timeline;
  940. xs_str *v;
  941. out = xs_list_new();
  942. while (xs_list_iter(&p, &v)) {
  943. xs *msg = NULL;
  944. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  945. /* add only posts by the author */
  946. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  947. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  948. xs *st = mastoapi_status(&snac2, msg);
  949. if (st)
  950. out = xs_list_append(out, st);
  951. }
  952. }
  953. }
  954. }
  955. user_free(&snac2);
  956. }
  957. else {
  958. /* try the uid as the md5 of a possibly loaded actor */
  959. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  960. if (opt == NULL) {
  961. /* account information */
  962. out = mastoapi_account(actor);
  963. }
  964. else
  965. if (strcmp(opt, "statuses") == 0) {
  966. /* we don't serve statuses of others; return the empty list */
  967. out = xs_list_new();
  968. }
  969. }
  970. }
  971. if (out != NULL) {
  972. *body = xs_json_dumps(out, 4);
  973. *ctype = "application/json";
  974. status = 200;
  975. }
  976. }
  977. }
  978. else
  979. if (strcmp(cmd, "/v1/timelines/home") == 0) { /** **/
  980. /* the private timeline */
  981. if (logged_in) {
  982. const char *max_id = xs_dict_get(args, "max_id");
  983. const char *since_id = xs_dict_get(args, "since_id");
  984. const char *min_id = xs_dict_get(args, "min_id");
  985. const char *limit_s = xs_dict_get(args, "limit");
  986. int limit = 0;
  987. int cnt = 0;
  988. if (!xs_is_null(limit_s))
  989. limit = atoi(limit_s);
  990. if (limit == 0)
  991. limit = 20;
  992. xs *timeline = timeline_simple_list(&snac1, "private", 0, 256);
  993. xs *out = xs_list_new();
  994. xs_list *p = timeline;
  995. xs_str *v;
  996. while (xs_list_iter(&p, &v) && cnt < limit) {
  997. xs *msg = NULL;
  998. /* only return entries older that max_id */
  999. if (max_id) {
  1000. if (strcmp(v, MID_TO_MD5(max_id)) == 0)
  1001. max_id = NULL;
  1002. continue;
  1003. }
  1004. /* only returns entries newer than since_id */
  1005. if (since_id) {
  1006. if (strcmp(v, MID_TO_MD5(since_id)) == 0)
  1007. break;
  1008. }
  1009. /* only returns entries newer than min_id */
  1010. /* what does really "Return results immediately newer than ID" mean? */
  1011. if (min_id) {
  1012. if (strcmp(v, MID_TO_MD5(min_id)) == 0)
  1013. break;
  1014. }
  1015. /* get the entry */
  1016. if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
  1017. continue;
  1018. /* discard non-Notes */
  1019. const char *type = xs_dict_get(msg, "type");
  1020. if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0)
  1021. continue;
  1022. #if 0
  1023. /* discard notes from people we don't follow with no boosts */
  1024. if (!following_check(&snac1, xs_dict_get(msg, "attributedTo")) &&
  1025. object_announces_len(xs_dict_get(msg, "id")) == 0)
  1026. continue;
  1027. #endif
  1028. /* discard notes from muted morons */
  1029. if (is_muted(&snac1, xs_dict_get(msg, "attributedTo")))
  1030. continue;
  1031. /* discard hidden notes */
  1032. if (is_hidden(&snac1, xs_dict_get(msg, "id")))
  1033. continue;
  1034. /* discard poll votes (they have a name) */
  1035. if (!xs_is_null(xs_dict_get(msg, "name")))
  1036. continue;
  1037. /* convert the Note into a Mastodon status */
  1038. xs *st = mastoapi_status(&snac1, msg);
  1039. if (st != NULL)
  1040. out = xs_list_append(out, st);
  1041. cnt++;
  1042. }
  1043. *body = xs_json_dumps(out, 4);
  1044. *ctype = "application/json";
  1045. status = 200;
  1046. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  1047. }
  1048. else {
  1049. status = 401; // unauthorized
  1050. }
  1051. }
  1052. else
  1053. if (strcmp(cmd, "/v1/timelines/public") == 0) { /** **/
  1054. /* the instance public timeline (public timelines for all users) */
  1055. const char *limit_s = xs_dict_get(args, "limit");
  1056. int limit = 0;
  1057. int cnt = 0;
  1058. if (!xs_is_null(limit_s))
  1059. limit = atoi(limit_s);
  1060. if (limit == 0)
  1061. limit = 20;
  1062. xs *timeline = timeline_instance_list(0, limit);
  1063. xs *out = xs_list_new();
  1064. xs_list *p = timeline;
  1065. xs_str *md5;
  1066. snac *user = NULL;
  1067. if (logged_in)
  1068. user = &snac1;
  1069. while (xs_list_iter(&p, &md5) && cnt < limit) {
  1070. xs *msg = NULL;
  1071. /* get the entry */
  1072. if (!valid_status(object_get_by_md5(md5, &msg)))
  1073. continue;
  1074. /* discard non-Notes */
  1075. const char *type = xs_dict_get(msg, "type");
  1076. if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0)
  1077. continue;
  1078. /* discard private users */
  1079. {
  1080. const char *atto = xs_dict_get(msg, "attributedTo");
  1081. xs *l = xs_split(atto, "/");
  1082. const char *uid = xs_list_get(l, -1);
  1083. snac p_user;
  1084. int skip = 1;
  1085. if (uid && user_open(&p_user, uid)) {
  1086. if (xs_type(xs_dict_get(p_user.config, "private")) != XSTYPE_TRUE)
  1087. skip = 0;
  1088. user_free(&p_user);
  1089. }
  1090. if (skip)
  1091. continue;
  1092. }
  1093. /* convert the Note into a Mastodon status */
  1094. xs *st = mastoapi_status(user, msg);
  1095. if (st != NULL) {
  1096. out = xs_list_append(out, st);
  1097. cnt++;
  1098. }
  1099. }
  1100. *body = xs_json_dumps(out, 4);
  1101. *ctype = "application/json";
  1102. status = 200;
  1103. }
  1104. else
  1105. if (strcmp(cmd, "/v1/conversations") == 0) { /** **/
  1106. /* TBD */
  1107. *body = xs_dup("[]");
  1108. *ctype = "application/json";
  1109. status = 200;
  1110. }
  1111. else
  1112. if (strcmp(cmd, "/v1/notifications") == 0) { /** **/
  1113. if (logged_in) {
  1114. xs *l = notify_list(&snac1, 0);
  1115. xs *out = xs_list_new();
  1116. xs_list *p = l;
  1117. xs_dict *v;
  1118. xs_list *excl = xs_dict_get(args, "exclude_types[]");
  1119. while (xs_list_iter(&p, &v)) {
  1120. xs *noti = notify_get(&snac1, v);
  1121. if (noti == NULL)
  1122. continue;
  1123. const char *type = xs_dict_get(noti, "type");
  1124. const char *utype = xs_dict_get(noti, "utype");
  1125. const char *objid = xs_dict_get(noti, "objid");
  1126. xs *actor = NULL;
  1127. xs *entry = NULL;
  1128. if (!valid_status(actor_get(xs_dict_get(noti, "actor"), &actor)))
  1129. continue;
  1130. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  1131. continue;
  1132. if (is_hidden(&snac1, objid))
  1133. continue;
  1134. /* convert the type */
  1135. if (strcmp(type, "Like") == 0)
  1136. type = "favourite";
  1137. else
  1138. if (strcmp(type, "Announce") == 0)
  1139. type = "reblog";
  1140. else
  1141. if (strcmp(type, "Follow") == 0)
  1142. type = "follow";
  1143. else
  1144. if (strcmp(type, "Create") == 0)
  1145. type = "mention";
  1146. else
  1147. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  1148. type = "poll";
  1149. else
  1150. continue;
  1151. /* excluded type? */
  1152. if (!xs_is_null(excl) && xs_list_in(excl, type) != -1)
  1153. continue;
  1154. xs *mn = xs_dict_new();
  1155. mn = xs_dict_append(mn, "type", type);
  1156. xs *id = xs_replace(xs_dict_get(noti, "id"), ".", "");
  1157. mn = xs_dict_append(mn, "id", id);
  1158. mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
  1159. xs *acct = mastoapi_account(actor);
  1160. mn = xs_dict_append(mn, "account", acct);
  1161. if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
  1162. xs *st = mastoapi_status(&snac1, entry);
  1163. if (st)
  1164. mn = xs_dict_append(mn, "status", st);
  1165. }
  1166. out = xs_list_append(out, mn);
  1167. }
  1168. *body = xs_json_dumps(out, 4);
  1169. *ctype = "application/json";
  1170. status = 200;
  1171. }
  1172. else
  1173. status = 401;
  1174. }
  1175. else
  1176. if (strcmp(cmd, "/v1/filters") == 0) { /** **/
  1177. /* snac will never have filters */
  1178. *body = xs_dup("[]");
  1179. *ctype = "application/json";
  1180. status = 200;
  1181. }
  1182. else
  1183. if (strcmp(cmd, "/v1/favourites") == 0) { /** **/
  1184. /* snac will never support a list of favourites */
  1185. *body = xs_dup("[]");
  1186. *ctype = "application/json";
  1187. status = 200;
  1188. }
  1189. else
  1190. if (strcmp(cmd, "/v1/bookmarks") == 0) { /** **/
  1191. /* snac does not support bookmarks */
  1192. *body = xs_dup("[]");
  1193. *ctype = "application/json";
  1194. status = 200;
  1195. }
  1196. else
  1197. if (strcmp(cmd, "/v1/lists") == 0) { /** **/
  1198. /* snac does not support lists */
  1199. *body = xs_dup("[]");
  1200. *ctype = "application/json";
  1201. status = 200;
  1202. }
  1203. else
  1204. if (strcmp(cmd, "/v1/scheduled_statuses") == 0) { /** **/
  1205. /* snac does not schedule notes */
  1206. *body = xs_dup("[]");
  1207. *ctype = "application/json";
  1208. status = 200;
  1209. }
  1210. else
  1211. if (strcmp(cmd, "/v1/follow_requests") == 0) { /** **/
  1212. /* snac does not support optional follow confirmations */
  1213. *body = xs_dup("[]");
  1214. *ctype = "application/json";
  1215. status = 200;
  1216. }
  1217. else
  1218. if (strcmp(cmd, "/v1/announcements") == 0) { /** **/
  1219. /* snac has no announcements (yet?) */
  1220. *body = xs_dup("[]");
  1221. *ctype = "application/json";
  1222. status = 200;
  1223. }
  1224. else
  1225. if (strcmp(cmd, "/v1/custom_emojis") == 0) { /** **/
  1226. /* are you kidding me? */
  1227. *body = xs_dup("[]");
  1228. *ctype = "application/json";
  1229. status = 200;
  1230. }
  1231. else
  1232. if (strcmp(cmd, "/v1/instance") == 0) { /** **/
  1233. /* returns an instance object */
  1234. xs *ins = xs_dict_new();
  1235. const char *host = xs_dict_get(srv_config, "host");
  1236. const char *title = xs_dict_get(srv_config, "title");
  1237. const char *sdesc = xs_dict_get(srv_config, "short_description");
  1238. ins = xs_dict_append(ins, "uri", host);
  1239. ins = xs_dict_append(ins, "domain", host);
  1240. ins = xs_dict_append(ins, "title", title && *title ? title : host);
  1241. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  1242. ins = xs_dict_append(ins, "source_url", WHAT_IS_SNAC_URL);
  1243. ins = xs_dict_append(ins, "description", host);
  1244. ins = xs_dict_append(ins, "short_description", sdesc && *sdesc ? sdesc : host);
  1245. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  1246. ins = xs_dict_append(ins, "thumbnail", susie);
  1247. const char *v = xs_dict_get(srv_config, "admin_email");
  1248. if (xs_is_null(v) || *v == '\0')
  1249. v = "admin@localhost";
  1250. ins = xs_dict_append(ins, "email", v);
  1251. ins = xs_dict_append(ins, "rules", xs_stock_list);
  1252. xs *l1 = xs_list_append(xs_list_new(), "en");
  1253. ins = xs_dict_append(ins, "languages", l1);
  1254. ins = xs_dict_append(ins, "urls", xs_stock_dict);
  1255. xs *d2 = xs_dict_append(xs_dict_new(), "user_count", xs_stock_0);
  1256. d2 = xs_dict_append(d2, "status_count", xs_stock_0);
  1257. d2 = xs_dict_append(d2, "domain_count", xs_stock_0);
  1258. ins = xs_dict_append(ins, "stats", d2);
  1259. ins = xs_dict_append(ins, "registrations", xs_stock_false);
  1260. ins = xs_dict_append(ins, "approval_required", xs_stock_false);
  1261. ins = xs_dict_append(ins, "invites_enabled", xs_stock_false);
  1262. xs *cfg = xs_dict_new();
  1263. {
  1264. xs *d11 = xs_json_loads("{\"characters_reserved_per_url\":32,"
  1265. "\"max_characters\":100000,\"max_media_attachments\":8}");
  1266. cfg = xs_dict_append(cfg, "statuses", d11);
  1267. xs *d12 = xs_json_loads("{\"max_featured_tags\":10}");
  1268. cfg = xs_dict_append(cfg, "accounts", d12);
  1269. xs *d13 = xs_json_loads("{\"image_matrix_limit\":33177600,"
  1270. "\"image_size_limit\":16777216,"
  1271. "\"video_frame_rate_limit\":120,"
  1272. "\"video_matrix_limit\":8294400,"
  1273. "\"video_size_limit\":103809024}"
  1274. );
  1275. {
  1276. /* get the supported mime types from the internal list */
  1277. const char **p = xs_mime_types;
  1278. xs_set mtypes;
  1279. xs_set_init(&mtypes);
  1280. while (*p) {
  1281. const char *type = p[1];
  1282. if (xs_startswith(type, "image/") ||
  1283. xs_startswith(type, "video/") ||
  1284. xs_startswith(type, "audio/"))
  1285. xs_set_add(&mtypes, type);
  1286. p += 2;
  1287. }
  1288. xs *l = xs_set_result(&mtypes);
  1289. d13 = xs_dict_append(d13, "supported_mime_types", l);
  1290. }
  1291. cfg = xs_dict_append(cfg, "media_attachments", d13);
  1292. xs *d14 = xs_json_loads("{\"max_characters_per_option\":50,"
  1293. "\"max_expiration\":2629746,"
  1294. "\"max_options\":8,\"min_expiration\":300}");
  1295. cfg = xs_dict_append(cfg, "polls", d14);
  1296. }
  1297. ins = xs_dict_append(ins, "configuration", cfg);
  1298. const char *admin_account = xs_dict_get(srv_config, "admin_account");
  1299. if (!xs_is_null(admin_account) && *admin_account) {
  1300. snac admin;
  1301. if (user_open(&admin, admin_account)) {
  1302. xs *actor = msg_actor(&admin);
  1303. xs *acct = mastoapi_account(actor);
  1304. ins = xs_dict_append(ins, "contact_account", acct);
  1305. user_free(&admin);
  1306. }
  1307. }
  1308. *body = xs_json_dumps(ins, 4);
  1309. *ctype = "application/json";
  1310. status = 200;
  1311. }
  1312. else
  1313. if (xs_startswith(cmd, "/v1/statuses/")) { /** **/
  1314. /* information about a status */
  1315. if (logged_in) {
  1316. xs *l = xs_split(cmd, "/");
  1317. const char *id = xs_list_get(l, 3);
  1318. const char *op = xs_list_get(l, 4);
  1319. if (!xs_is_null(id)) {
  1320. xs *msg = NULL;
  1321. xs *out = NULL;
  1322. /* skip the 'fake' part of the id */
  1323. id = MID_TO_MD5(id);
  1324. if (valid_status(object_get_by_md5(id, &msg))) {
  1325. if (op == NULL) {
  1326. if (!is_muted(&snac1, xs_dict_get(msg, "attributedTo"))) {
  1327. /* return the status itself */
  1328. out = mastoapi_status(&snac1, msg);
  1329. }
  1330. }
  1331. else
  1332. if (strcmp(op, "context") == 0) { /** **/
  1333. /* return ancestors and children */
  1334. xs *anc = xs_list_new();
  1335. xs *des = xs_list_new();
  1336. xs_list *p;
  1337. xs_str *v;
  1338. char pid[64];
  1339. /* build the [grand]parent list, moving up */
  1340. strncpy(pid, id, sizeof(pid));
  1341. while (object_parent(pid, pid, sizeof(pid))) {
  1342. xs *m2 = NULL;
  1343. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  1344. xs *st = mastoapi_status(&snac1, m2);
  1345. if (st)
  1346. anc = xs_list_insert(anc, 0, st);
  1347. }
  1348. else
  1349. break;
  1350. }
  1351. /* build the children list */
  1352. xs *children = object_children(xs_dict_get(msg, "id"));
  1353. p = children;
  1354. while (xs_list_iter(&p, &v)) {
  1355. xs *m2 = NULL;
  1356. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  1357. if (xs_is_null(xs_dict_get(m2, "name"))) {
  1358. xs *st = mastoapi_status(&snac1, m2);
  1359. if (st)
  1360. des = xs_list_append(des, st);
  1361. }
  1362. }
  1363. }
  1364. out = xs_dict_new();
  1365. out = xs_dict_append(out, "ancestors", anc);
  1366. out = xs_dict_append(out, "descendants", des);
  1367. }
  1368. else
  1369. if (strcmp(op, "reblogged_by") == 0 || /** **/
  1370. strcmp(op, "favourited_by") == 0) { /** **/
  1371. /* return the list of people who liked or boosted this */
  1372. out = xs_list_new();
  1373. xs *l = NULL;
  1374. if (op[0] == 'r')
  1375. l = object_announces(xs_dict_get(msg, "id"));
  1376. else
  1377. l = object_likes(xs_dict_get(msg, "id"));
  1378. xs_list *p = l;
  1379. xs_str *v;
  1380. while (xs_list_iter(&p, &v)) {
  1381. xs *actor2 = NULL;
  1382. if (valid_status(object_get_by_md5(v, &actor2))) {
  1383. xs *acct2 = mastoapi_account(actor2);
  1384. out = xs_list_append(out, acct2);
  1385. }
  1386. }
  1387. }
  1388. }
  1389. else
  1390. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  1391. if (out != NULL) {
  1392. *body = xs_json_dumps(out, 4);
  1393. *ctype = "application/json";
  1394. status = 200;
  1395. }
  1396. }
  1397. }
  1398. else
  1399. status = 401;
  1400. }
  1401. else
  1402. if (strcmp(cmd, "/v1/preferences") == 0) { /** **/
  1403. *body = xs_dup("{}");
  1404. *ctype = "application/json";
  1405. status = 200;
  1406. }
  1407. else
  1408. if (strcmp(cmd, "/v1/markers") == 0) { /** **/
  1409. *body = xs_dup("{}");
  1410. *ctype = "application/json";
  1411. status = 200;
  1412. }
  1413. else
  1414. if (strcmp(cmd, "/v1/followed_tags") == 0) { /** **/
  1415. *body = xs_dup("[]");
  1416. *ctype = "application/json";
  1417. status = 200;
  1418. }
  1419. else
  1420. if (strcmp(cmd, "/v2/search") == 0) { /** **/
  1421. if (logged_in) {
  1422. const char *q = xs_dict_get(args, "q");
  1423. const char *type = xs_dict_get(args, "type");
  1424. const char *offset = xs_dict_get(args, "offset");
  1425. xs *acl = xs_list_new();
  1426. xs *stl = xs_list_new();
  1427. xs *htl = xs_list_new();
  1428. xs *res = xs_dict_new();
  1429. if (xs_is_null(offset) || strcmp(offset, "0") == 0) {
  1430. /* reply something only for offset 0; otherwise,
  1431. apps like Tusky keep asking again and again */
  1432. if (!xs_is_null(q) && !xs_is_null(type) && strcmp(type, "accounts") == 0) {
  1433. /* do a webfinger query */
  1434. char *actor = NULL;
  1435. char *user = NULL;
  1436. if (valid_status(webfinger_request(q, &actor, &user))) {
  1437. xs *actor_o = NULL;
  1438. if (valid_status(actor_request(&snac1, actor, &actor_o))) {
  1439. xs *acct = mastoapi_account(actor_o);
  1440. acl = xs_list_append(acl, acct);
  1441. }
  1442. }
  1443. }
  1444. }
  1445. res = xs_dict_append(res, "accounts", acl);
  1446. res = xs_dict_append(res, "statuses", stl);
  1447. res = xs_dict_append(res, "hashtags", htl);
  1448. *body = xs_json_dumps(res, 4);
  1449. *ctype = "application/json";
  1450. status = 200;
  1451. }
  1452. else
  1453. status = 401;
  1454. }
  1455. /* user cleanup */
  1456. if (logged_in)
  1457. user_free(&snac1);
  1458. return status;
  1459. }
  1460. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  1461. const char *payload, int p_size,
  1462. char **body, int *b_size, char **ctype)
  1463. {
  1464. (void)p_size;
  1465. (void)b_size;
  1466. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1467. return 0;
  1468. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1469. int status = 404;
  1470. xs *args = NULL;
  1471. char *i_ctype = xs_dict_get(req, "content-type");
  1472. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1473. args = xs_json_loads(payload);
  1474. else
  1475. args = xs_dup(xs_dict_get(req, "p_vars"));
  1476. if (args == NULL)
  1477. return 400;
  1478. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1479. snac snac = {0};
  1480. int logged_in = process_auth_token(&snac, req);
  1481. if (strcmp(cmd, "/v1/apps") == 0) { /** **/
  1482. const char *name = xs_dict_get(args, "client_name");
  1483. const char *ruri = xs_dict_get(args, "redirect_uris");
  1484. const char *scope = xs_dict_get(args, "scope");
  1485. if (xs_type(ruri) == XSTYPE_LIST)
  1486. ruri = xs_dict_get(ruri, 0);
  1487. if (name && ruri) {
  1488. xs *app = xs_dict_new();
  1489. xs *id = xs_replace_i(tid(0), ".", "");
  1490. xs *csec = random_str();
  1491. xs *vkey = random_str();
  1492. xs *cid = NULL;
  1493. /* pick a non-existent random cid */
  1494. for (;;) {
  1495. cid = random_str();
  1496. xs *p_app = app_get(cid);
  1497. if (p_app == NULL)
  1498. break;
  1499. xs_free(cid);
  1500. }
  1501. app = xs_dict_append(app, "name", name);
  1502. app = xs_dict_append(app, "redirect_uri", ruri);
  1503. app = xs_dict_append(app, "client_id", cid);
  1504. app = xs_dict_append(app, "client_secret", csec);
  1505. app = xs_dict_append(app, "vapid_key", vkey);
  1506. app = xs_dict_append(app, "id", id);
  1507. *body = xs_json_dumps(app, 4);
  1508. *ctype = "application/json";
  1509. status = 200;
  1510. app = xs_dict_append(app, "code", "");
  1511. if (scope)
  1512. app = xs_dict_append(app, "scope", scope);
  1513. app_add(cid, app);
  1514. srv_debug(1, xs_fmt("mastoapi apps: new app %s", cid));
  1515. }
  1516. }
  1517. else
  1518. if (strcmp(cmd, "/v1/statuses") == 0) { /** **/
  1519. if (logged_in) {
  1520. /* post a new Note */
  1521. const char *content = xs_dict_get(args, "status");
  1522. const char *mid = xs_dict_get(args, "in_reply_to_id");
  1523. const char *visibility = xs_dict_get(args, "visibility");
  1524. const char *summary = xs_dict_get(args, "spoiler_text");
  1525. const char *media_ids = xs_dict_get(args, "media_ids");
  1526. if (xs_is_null(media_ids))
  1527. media_ids = xs_dict_get(args, "media_ids[]");
  1528. if (xs_is_null(media_ids))
  1529. media_ids = xs_dict_get(args, "media_ids");
  1530. if (xs_is_null(visibility))
  1531. visibility = "public";
  1532. xs *attach_list = xs_list_new();
  1533. xs *irt = NULL;
  1534. /* is it a reply? */
  1535. if (mid != NULL) {
  1536. xs *r_msg = NULL;
  1537. const char *md5 = MID_TO_MD5(mid);
  1538. if (valid_status(object_get_by_md5(md5, &r_msg)))
  1539. irt = xs_dup(xs_dict_get(r_msg, "id"));
  1540. }
  1541. /* does it have attachments? */
  1542. if (!xs_is_null(media_ids)) {
  1543. xs *mi = NULL;
  1544. if (xs_type(media_ids) == XSTYPE_LIST)
  1545. mi = xs_dup(media_ids);
  1546. else {
  1547. mi = xs_list_new();
  1548. mi = xs_list_append(mi, media_ids);
  1549. }
  1550. xs_list *p = mi;
  1551. xs_str *v;
  1552. while (xs_list_iter(&p, &v)) {
  1553. xs *l = xs_list_new();
  1554. xs *url = xs_fmt("%s/s/%s", snac.actor, v);
  1555. xs *desc = static_get_meta(&snac, v);
  1556. l = xs_list_append(l, url);
  1557. l = xs_list_append(l, desc);
  1558. attach_list = xs_list_append(attach_list, l);
  1559. }
  1560. }
  1561. /* prepare the message */
  1562. xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
  1563. strcmp(visibility, "public") == 0 ? 0 : 1);
  1564. if (!xs_is_null(summary) && *summary) {
  1565. msg = xs_dict_set(msg, "sensitive", xs_stock_true);
  1566. msg = xs_dict_set(msg, "summary", summary);
  1567. }
  1568. /* store */
  1569. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1570. /* 'Create' message */
  1571. xs *c_msg = msg_create(&snac, msg);
  1572. enqueue_message(&snac, c_msg);
  1573. timeline_touch(&snac);
  1574. /* convert to a mastodon status as a response code */
  1575. xs *st = mastoapi_status(&snac, msg);
  1576. *body = xs_json_dumps(st, 4);
  1577. *ctype = "application/json";
  1578. status = 200;
  1579. }
  1580. else
  1581. status = 401;
  1582. }
  1583. else
  1584. if (xs_startswith(cmd, "/v1/statuses")) { /** **/
  1585. if (logged_in) {
  1586. /* operations on a status */
  1587. xs *l = xs_split(cmd, "/");
  1588. const char *mid = xs_list_get(l, 3);
  1589. const char *op = xs_list_get(l, 4);
  1590. if (!xs_is_null(mid)) {
  1591. xs *msg = NULL;
  1592. xs *out = NULL;
  1593. /* skip the 'fake' part of the id */
  1594. mid = MID_TO_MD5(mid);
  1595. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1596. char *id = xs_dict_get(msg, "id");
  1597. if (op == NULL) {
  1598. /* no operation (?) */
  1599. }
  1600. else
  1601. if (strcmp(op, "favourite") == 0) { /** **/
  1602. xs *n_msg = msg_admiration(&snac, id, "Like");
  1603. if (n_msg != NULL) {
  1604. enqueue_message(&snac, n_msg);
  1605. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  1606. out = mastoapi_status(&snac, msg);
  1607. }
  1608. }
  1609. else
  1610. if (strcmp(op, "unfavourite") == 0) { /** **/
  1611. /* partial support: as the original Like message
  1612. is not stored anywhere here, it's not possible
  1613. to send an Undo + Like; the only thing done here
  1614. is to delete the actor from the list of likes */
  1615. object_unadmire(id, snac.actor, 1);
  1616. }
  1617. else
  1618. if (strcmp(op, "reblog") == 0) { /** **/
  1619. xs *n_msg = msg_admiration(&snac, id, "Announce");
  1620. if (n_msg != NULL) {
  1621. enqueue_message(&snac, n_msg);
  1622. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  1623. out = mastoapi_status(&snac, msg);
  1624. }
  1625. }
  1626. else
  1627. if (strcmp(op, "unreblog") == 0) { /** **/
  1628. /* partial support: see comment in 'unfavourite' */
  1629. object_unadmire(id, snac.actor, 0);
  1630. }
  1631. else
  1632. if (strcmp(op, "bookmark") == 0) { /** **/
  1633. /* snac does not support bookmarks */
  1634. }
  1635. else
  1636. if (strcmp(op, "unbookmark") == 0) { /** **/
  1637. /* snac does not support bookmarks */
  1638. }
  1639. else
  1640. if (strcmp(op, "pin") == 0) { /** **/
  1641. /* pin this message */
  1642. if (pin(&snac, id))
  1643. out = mastoapi_status(&snac, msg);
  1644. else
  1645. status = 422;
  1646. }
  1647. else
  1648. if (strcmp(op, "unpin") == 0) { /** **/
  1649. /* unpin this message */
  1650. unpin(&snac, id);
  1651. out = mastoapi_status(&snac, msg);
  1652. }
  1653. else
  1654. if (strcmp(op, "mute") == 0) { /** **/
  1655. /* Mastodon's mute is snac's hide */
  1656. }
  1657. else
  1658. if (strcmp(op, "unmute") == 0) { /** **/
  1659. /* Mastodon's unmute is snac's unhide */
  1660. }
  1661. }
  1662. if (out != NULL) {
  1663. *body = xs_json_dumps(out, 4);
  1664. *ctype = "application/json";
  1665. status = 200;
  1666. }
  1667. }
  1668. }
  1669. else
  1670. status = 401;
  1671. }
  1672. else
  1673. if (strcmp(cmd, "/v1/notifications/clear") == 0) { /** **/
  1674. if (logged_in) {
  1675. notify_clear(&snac);
  1676. timeline_touch(&snac);
  1677. *body = xs_dup("{}");
  1678. *ctype = "application/json";
  1679. status = 200;
  1680. }
  1681. else
  1682. status = 401;
  1683. }
  1684. else
  1685. if (strcmp(cmd, "/v1/push/subscription") == 0) { /** **/
  1686. /* I don't know what I'm doing */
  1687. if (logged_in) {
  1688. char *v;
  1689. xs *wpush = xs_dict_new();
  1690. wpush = xs_dict_append(wpush, "id", "1");
  1691. v = xs_dict_get(args, "data");
  1692. v = xs_dict_get(v, "alerts");
  1693. wpush = xs_dict_append(wpush, "alerts", v);
  1694. v = xs_dict_get(args, "subscription");
  1695. v = xs_dict_get(v, "endpoint");
  1696. wpush = xs_dict_append(wpush, "endpoint", v);
  1697. xs *server_key = random_str();
  1698. wpush = xs_dict_append(wpush, "server_key", server_key);
  1699. *body = xs_json_dumps(wpush, 4);
  1700. *ctype = "application/json";
  1701. status = 200;
  1702. }
  1703. else
  1704. status = 401;
  1705. }
  1706. else
  1707. if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { /** **/
  1708. if (logged_in) {
  1709. const xs_list *file = xs_dict_get(args, "file");
  1710. const char *desc = xs_dict_get(args, "description");
  1711. if (xs_is_null(desc))
  1712. desc = "";
  1713. status = 400;
  1714. if (xs_type(file) == XSTYPE_LIST) {
  1715. const char *fn = xs_list_get(file, 0);
  1716. if (*fn != '\0') {
  1717. char *ext = strrchr(fn, '.');
  1718. xs *hash = xs_md5_hex(fn, strlen(fn));
  1719. xs *id = xs_fmt("%s%s", hash, ext);
  1720. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1721. int fo = xs_number_get(xs_list_get(file, 1));
  1722. int fs = xs_number_get(xs_list_get(file, 2));
  1723. /* store */
  1724. static_put(&snac, id, payload + fo, fs);
  1725. static_put_meta(&snac, id, desc);
  1726. /* prepare a response */
  1727. xs *rsp = xs_dict_new();
  1728. rsp = xs_dict_append(rsp, "id", id);
  1729. rsp = xs_dict_append(rsp, "type", "image");
  1730. rsp = xs_dict_append(rsp, "url", url);
  1731. rsp = xs_dict_append(rsp, "preview_url", url);
  1732. rsp = xs_dict_append(rsp, "remote_url", url);
  1733. rsp = xs_dict_append(rsp, "description", desc);
  1734. *body = xs_json_dumps(rsp, 4);
  1735. *ctype = "application/json";
  1736. status = 200;
  1737. }
  1738. }
  1739. }
  1740. else
  1741. status = 401;
  1742. }
  1743. else
  1744. if (xs_startswith(cmd, "/v1/accounts")) { /** **/
  1745. if (logged_in) {
  1746. /* account-related information */
  1747. xs *l = xs_split(cmd, "/");
  1748. const char *md5 = xs_list_get(l, 3);
  1749. const char *opt = xs_list_get(l, 4);
  1750. xs *rsp = NULL;
  1751. if (!xs_is_null(md5) && *md5) {
  1752. xs *actor_o = NULL;
  1753. if (xs_is_null(opt)) {
  1754. /* ? */
  1755. }
  1756. else
  1757. if (strcmp(opt, "follow") == 0) { /** **/
  1758. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1759. const char *actor = xs_dict_get(actor_o, "id");
  1760. xs *msg = msg_follow(&snac, actor);
  1761. if (msg != NULL) {
  1762. /* reload the actor from the message, in may be different */
  1763. actor = xs_dict_get(msg, "object");
  1764. following_add(&snac, actor, msg);
  1765. enqueue_output_by_actor(&snac, msg, actor, 0);
  1766. rsp = mastoapi_relationship(&snac, md5);
  1767. }
  1768. }
  1769. }
  1770. else
  1771. if (strcmp(opt, "unfollow") == 0) { /** **/
  1772. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1773. const char *actor = xs_dict_get(actor_o, "id");
  1774. /* get the following object */
  1775. xs *object = NULL;
  1776. if (valid_status(following_get(&snac, actor, &object))) {
  1777. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  1778. following_del(&snac, actor);
  1779. enqueue_output_by_actor(&snac, msg, actor, 0);
  1780. rsp = mastoapi_relationship(&snac, md5);
  1781. }
  1782. }
  1783. }
  1784. else
  1785. if (strcmp(opt, "block") == 0) { /** **/
  1786. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1787. const char *actor = xs_dict_get(actor_o, "id");
  1788. mute(&snac, actor);
  1789. rsp = mastoapi_relationship(&snac, md5);
  1790. }
  1791. }
  1792. else
  1793. if (strcmp(opt, "unblock") == 0) { /** **/
  1794. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1795. const char *actor = xs_dict_get(actor_o, "id");
  1796. unmute(&snac, actor);
  1797. rsp = mastoapi_relationship(&snac, md5);
  1798. }
  1799. }
  1800. }
  1801. if (rsp != NULL) {
  1802. *body = xs_json_dumps(rsp, 4);
  1803. *ctype = "application/json";
  1804. status = 200;
  1805. }
  1806. }
  1807. else
  1808. status = 401;
  1809. }
  1810. else
  1811. if (xs_startswith(cmd, "/v1/polls")) { /** **/
  1812. if (logged_in) {
  1813. /* operations on a status */
  1814. xs *l = xs_split(cmd, "/");
  1815. const char *mid = xs_list_get(l, 3);
  1816. const char *op = xs_list_get(l, 4);
  1817. if (!xs_is_null(mid)) {
  1818. xs *msg = NULL;
  1819. xs *out = NULL;
  1820. /* skip the 'fake' part of the id */
  1821. mid = MID_TO_MD5(mid);
  1822. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1823. const char *id = xs_dict_get(msg, "id");
  1824. const char *atto = xs_dict_get(msg, "attributedTo");
  1825. xs_list *opts = xs_dict_get(msg, "oneOf");
  1826. if (opts == NULL)
  1827. opts = xs_dict_get(msg, "anyOf");
  1828. if (op == NULL) {
  1829. }
  1830. else
  1831. if (strcmp(op, "votes") == 0) {
  1832. xs_list *choices = xs_dict_get(args, "choices[]");
  1833. if (xs_is_null(choices))
  1834. choices = xs_dict_get(args, "choices");
  1835. if (xs_type(choices) == XSTYPE_LIST) {
  1836. xs_str *v;
  1837. while (xs_list_iter(&choices, &v)) {
  1838. int io = atoi(v);
  1839. const xs_dict *o = xs_list_get(opts, io);
  1840. if (o) {
  1841. const char *name = xs_dict_get(o, "name");
  1842. xs *msg = msg_note(&snac, "", atto, (char *)id, NULL, 1);
  1843. msg = xs_dict_append(msg, "name", name);
  1844. xs *c_msg = msg_create(&snac, msg);
  1845. enqueue_message(&snac, c_msg);
  1846. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1847. }
  1848. }
  1849. out = mastoapi_poll(&snac, msg);
  1850. }
  1851. }
  1852. }
  1853. if (out != NULL) {
  1854. *body = xs_json_dumps(out, 4);
  1855. *ctype = "application/json";
  1856. status = 200;
  1857. }
  1858. }
  1859. }
  1860. else
  1861. status = 401;
  1862. }
  1863. /* user cleanup */
  1864. if (logged_in)
  1865. user_free(&snac);
  1866. return status;
  1867. }
  1868. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  1869. const char *payload, int p_size,
  1870. char **body, int *b_size, char **ctype)
  1871. {
  1872. (void)p_size;
  1873. (void)b_size;
  1874. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1875. return 0;
  1876. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1877. int status = 404;
  1878. xs *args = NULL;
  1879. char *i_ctype = xs_dict_get(req, "content-type");
  1880. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1881. args = xs_json_loads(payload);
  1882. else
  1883. args = xs_dup(xs_dict_get(req, "p_vars"));
  1884. if (args == NULL)
  1885. return 400;
  1886. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1887. snac snac = {0};
  1888. int logged_in = process_auth_token(&snac, req);
  1889. if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) { /** **/
  1890. if (logged_in) {
  1891. xs *l = xs_split(cmd, "/");
  1892. const char *stid = xs_list_get(l, 3);
  1893. if (!xs_is_null(stid)) {
  1894. const char *desc = xs_dict_get(args, "description");
  1895. /* set the image metadata */
  1896. static_put_meta(&snac, stid, desc);
  1897. /* prepare a response */
  1898. xs *rsp = xs_dict_new();
  1899. xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
  1900. rsp = xs_dict_append(rsp, "id", stid);
  1901. rsp = xs_dict_append(rsp, "type", "image");
  1902. rsp = xs_dict_append(rsp, "url", url);
  1903. rsp = xs_dict_append(rsp, "preview_url", url);
  1904. rsp = xs_dict_append(rsp, "remote_url", url);
  1905. rsp = xs_dict_append(rsp, "description", desc);
  1906. *body = xs_json_dumps(rsp, 4);
  1907. *ctype = "application/json";
  1908. status = 200;
  1909. }
  1910. }
  1911. else
  1912. status = 401;
  1913. }
  1914. /* user cleanup */
  1915. if (logged_in)
  1916. user_free(&snac);
  1917. return status;
  1918. }
  1919. void mastoapi_purge(void)
  1920. {
  1921. xs *spec = xs_fmt("%s/app/" "*.json", srv_basedir);
  1922. xs *files = xs_glob(spec, 1, 0);
  1923. xs_list *p = files;
  1924. xs_str *v;
  1925. time_t mt = time(NULL) - 3600;
  1926. while (xs_list_iter(&p, &v)) {
  1927. xs *cid = xs_replace(v, ".json", "");
  1928. xs *fn = _app_fn(cid);
  1929. if (mtime(fn) < mt) {
  1930. /* get the app */
  1931. xs *app = app_get(cid);
  1932. if (app) {
  1933. /* old apps with no uid are incomplete cruft */
  1934. const char *uid = xs_dict_get(app, "uid");
  1935. if (xs_is_null(uid) || *uid == '\0') {
  1936. unlink(fn);
  1937. srv_debug(2, xs_fmt("purged %s", fn));
  1938. }
  1939. }
  1940. }
  1941. }
  1942. }
  1943. #endif /* #ifndef NO_MASTODON_API */