mastoapi.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483
  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 || strcmp(mtype, "Document") == 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. /* get the last person who boosted this */
  688. xs *boosted_by_md5 = NULL;
  689. if (xs_list_len(idx))
  690. boosted_by_md5 = xs_dup(xs_list_get(idx, -1));
  691. xs_free(idx);
  692. xs_free(ixc);
  693. idx = object_children(id);
  694. ixc = xs_number_new(xs_list_len(idx));
  695. st = xs_dict_append(st, "replies_count", ixc);
  696. /* default in_reply_to values */
  697. st = xs_dict_append(st, "in_reply_to_id", xs_stock_null);
  698. st = xs_dict_append(st, "in_reply_to_account_id", xs_stock_null);
  699. tmp = xs_dict_get(msg, "inReplyTo");
  700. if (!xs_is_null(tmp)) {
  701. xs *irto = NULL;
  702. if (valid_status(object_get(tmp, &irto))) {
  703. xs *irt_mid = mastoapi_id(irto);
  704. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  705. char *at = NULL;
  706. if (!xs_is_null(at = xs_dict_get(irto, "attributedTo"))) {
  707. xs *at_md5 = xs_md5_hex(at, strlen(at));
  708. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  709. }
  710. }
  711. }
  712. st = xs_dict_append(st, "reblog", xs_stock_null);
  713. st = xs_dict_append(st, "card", xs_stock_null);
  714. st = xs_dict_append(st, "language", xs_stock_null);
  715. tmp = xs_dict_get(msg, "sourceContent");
  716. if (xs_is_null(tmp))
  717. tmp = "";
  718. st = xs_dict_append(st, "text", tmp);
  719. tmp = xs_dict_get(msg, "updated");
  720. if (xs_is_null(tmp))
  721. tmp = xs_stock_null;
  722. st = xs_dict_append(st, "edited_at", tmp);
  723. if (strcmp(type, "Question") == 0) {
  724. xs *poll = mastoapi_poll(snac, msg);
  725. st = xs_dict_append(st, "poll", poll);
  726. }
  727. else
  728. st = xs_dict_append(st, "poll", xs_stock_null);
  729. st = xs_dict_append(st, "bookmarked", xs_stock_false);
  730. st = xs_dict_append(st, "pinned",
  731. (snac && is_pinned(snac, id)) ? xs_stock_true : xs_stock_false);
  732. /* is it a boost? */
  733. if (!xs_is_null(boosted_by_md5)) {
  734. /* create a new dummy status, using st as the 'reblog' field */
  735. xs_dict *bst = xs_dup(st);
  736. xs *b_actor = NULL;
  737. if (valid_status(object_get_by_md5(boosted_by_md5, &b_actor))) {
  738. xs *b_acct = mastoapi_account(b_actor);
  739. xs *fake_uri = NULL;
  740. if (snac)
  741. fake_uri = xs_fmt("%s/d/%s/Announce", snac->actor, mid);
  742. else
  743. fake_uri = xs_fmt("%s#%s", srv_baseurl, mid);
  744. bst = xs_dict_set(bst, "uri", fake_uri);
  745. bst = xs_dict_set(bst, "url", fake_uri);
  746. bst = xs_dict_set(bst, "account", b_acct);
  747. bst = xs_dict_set(bst, "content", "");
  748. bst = xs_dict_set(bst, "reblog", st);
  749. xs_free(st);
  750. st = bst;
  751. }
  752. }
  753. return st;
  754. }
  755. xs_dict *mastoapi_relationship(snac *snac, const char *md5)
  756. {
  757. xs_dict *rel = NULL;
  758. xs *actor_o = NULL;
  759. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  760. rel = xs_dict_new();
  761. const char *actor = xs_dict_get(actor_o, "id");
  762. rel = xs_dict_append(rel, "id", md5);
  763. rel = xs_dict_append(rel, "following",
  764. following_check(snac, actor) ? xs_stock_true : xs_stock_false);
  765. rel = xs_dict_append(rel, "showing_reblogs", xs_stock_true);
  766. rel = xs_dict_append(rel, "notifying", xs_stock_false);
  767. rel = xs_dict_append(rel, "followed_by",
  768. follower_check(snac, actor) ? xs_stock_true : xs_stock_false);
  769. rel = xs_dict_append(rel, "blocking",
  770. is_muted(snac, actor) ? xs_stock_true : xs_stock_false);
  771. rel = xs_dict_append(rel, "muting", xs_stock_false);
  772. rel = xs_dict_append(rel, "muting_notifications", xs_stock_false);
  773. rel = xs_dict_append(rel, "requested", xs_stock_false);
  774. rel = xs_dict_append(rel, "domain_blocking", xs_stock_false);
  775. rel = xs_dict_append(rel, "endorsed", xs_stock_false);
  776. rel = xs_dict_append(rel, "note", "");
  777. }
  778. return rel;
  779. }
  780. int process_auth_token(snac *snac, const xs_dict *req)
  781. /* processes an authorization token, if there is one */
  782. {
  783. int logged_in = 0;
  784. char *v;
  785. /* if there is an authorization field, try to validate it */
  786. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  787. xs *tokid = xs_replace_n(v, "Bearer ", "", 1);
  788. xs *token = token_get(tokid);
  789. if (token != NULL) {
  790. const char *uid = xs_dict_get(token, "uid");
  791. if (!xs_is_null(uid) && user_open(snac, uid)) {
  792. logged_in = 1;
  793. /* this counts as a 'login' */
  794. lastlog_write(snac, "mastoapi");
  795. srv_debug(2, xs_fmt("mastoapi auth: valid token for user %s", uid));
  796. }
  797. else
  798. srv_log(xs_fmt("mastoapi auth: corrupted token %s", tokid));
  799. }
  800. else
  801. srv_log(xs_fmt("mastoapi auth: invalid token %s", tokid));
  802. }
  803. return logged_in;
  804. }
  805. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  806. char **body, int *b_size, char **ctype)
  807. {
  808. (void)b_size;
  809. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  810. return 0;
  811. srv_debug(1, xs_fmt("mastoapi_get_handler %s", q_path));
  812. int status = 404;
  813. xs_dict *args = xs_dict_get(req, "q_vars");
  814. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  815. snac snac1 = {0};
  816. int logged_in = process_auth_token(&snac1, req);
  817. if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) { /** **/
  818. if (logged_in) {
  819. xs *acct = xs_dict_new();
  820. acct = xs_dict_append(acct, "id", snac1.md5);
  821. acct = xs_dict_append(acct, "username", xs_dict_get(snac1.config, "uid"));
  822. acct = xs_dict_append(acct, "acct", xs_dict_get(snac1.config, "uid"));
  823. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac1.config, "name"));
  824. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac1.config, "published"));
  825. acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio"));
  826. acct = xs_dict_append(acct, "url", snac1.actor);
  827. acct = xs_dict_append(acct, "header", "");
  828. xs *src = xs_json_loads("{\"privacy\":\"public\","
  829. "\"sensitive\":false,\"fields\":[],\"note\":\"\"}");
  830. acct = xs_dict_append(acct, "source", src);
  831. xs *avatar = NULL;
  832. char *av = xs_dict_get(snac1.config, "avatar");
  833. if (xs_is_null(av) || *av == '\0')
  834. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  835. else
  836. avatar = xs_dup(av);
  837. acct = xs_dict_append(acct, "avatar", avatar);
  838. acct = xs_dict_append(acct, "avatar_static", avatar);
  839. xs_dict *metadata = xs_dict_get(snac1.config, "metadata");
  840. if (xs_type(metadata) == XSTYPE_DICT) {
  841. xs *fields = xs_list_new();
  842. xs_str *k;
  843. xs_str *v;
  844. while (xs_dict_iter(&metadata, &k, &v)) {
  845. xs *d = xs_dict_new();
  846. d = xs_dict_append(d, "name", k);
  847. d = xs_dict_append(d, "value", v);
  848. d = xs_dict_append(d, "verified_at", xs_stock_null);
  849. fields = xs_list_append(fields, d);
  850. }
  851. acct = xs_dict_set(acct, "fields", fields);
  852. }
  853. *body = xs_json_dumps(acct, 4);
  854. *ctype = "application/json";
  855. status = 200;
  856. }
  857. else {
  858. status = 422; // "Unprocessable entity" (no login)
  859. }
  860. }
  861. else
  862. if (strcmp(cmd, "/v1/accounts/relationships") == 0) { /** **/
  863. /* find if an account is followed, blocked, etc. */
  864. /* the account to get relationships about is in args "id[]" */
  865. if (logged_in) {
  866. xs *res = xs_list_new();
  867. const char *md5 = xs_dict_get(args, "id[]");
  868. if (xs_is_null(md5))
  869. md5 = xs_dict_get(args, "id");
  870. if (!xs_is_null(md5)) {
  871. if (xs_type(md5) == XSTYPE_LIST)
  872. md5 = xs_list_get(md5, 0);
  873. xs *rel = mastoapi_relationship(&snac1, md5);
  874. if (rel != NULL)
  875. res = xs_list_append(res, rel);
  876. }
  877. *body = xs_json_dumps(res, 4);
  878. *ctype = "application/json";
  879. status = 200;
  880. }
  881. else
  882. status = 422;
  883. }
  884. else
  885. if (xs_startswith(cmd, "/v1/accounts/")) { /** **/
  886. /* account-related information */
  887. xs *l = xs_split(cmd, "/");
  888. const char *uid = xs_list_get(l, 3);
  889. const char *opt = xs_list_get(l, 4);
  890. if (uid != NULL) {
  891. snac snac2;
  892. xs *out = NULL;
  893. xs *actor = NULL;
  894. if (logged_in && strcmp(uid, "search") == 0) { /** **/
  895. /* search for accounts starting with q */
  896. const char *aq = xs_dict_get(args, "q");
  897. if (!xs_is_null(aq)) {
  898. xs *q = xs_tolower_i(xs_dup(aq));
  899. out = xs_list_new();
  900. xs *wing = following_list(&snac1);
  901. xs *wers = follower_list(&snac1);
  902. xs *ulst = user_list();
  903. xs_list *p;
  904. xs_str *v;
  905. xs_set seen;
  906. xs_set_init(&seen);
  907. /* user relations */
  908. xs_list *lsts[] = { wing, wers, NULL };
  909. int n;
  910. for (n = 0; (p = lsts[n]) != NULL; n++) {
  911. while (xs_list_iter(&p, &v)) {
  912. /* already seen? skip */
  913. if (xs_set_add(&seen, v) == 0)
  914. continue;
  915. xs *actor = NULL;
  916. if (valid_status(object_get(v, &actor))) {
  917. const char *uname = xs_dict_get(actor, "preferredUsername");
  918. if (!xs_is_null(uname)) {
  919. xs *luname = xs_tolower_i(xs_dup(uname));
  920. if (xs_startswith(luname, q)) {
  921. xs *acct = mastoapi_account(actor);
  922. out = xs_list_append(out, acct);
  923. }
  924. }
  925. }
  926. }
  927. }
  928. /* local users */
  929. p = ulst;
  930. while (xs_list_iter(&p, &v)) {
  931. snac user;
  932. /* skip this same user */
  933. if (strcmp(v, xs_dict_get(snac1.config, "uid")) == 0)
  934. continue;
  935. /* skip if the uid does not start with the query */
  936. xs *v2 = xs_tolower_i(xs_dup(v));
  937. if (!xs_startswith(v2, q))
  938. continue;
  939. if (user_open(&user, v)) {
  940. /* if it's not already seen, add it */
  941. if (xs_set_add(&seen, user.actor) == 1) {
  942. xs *actor = msg_actor(&user);
  943. xs *acct = mastoapi_account(actor);
  944. out = xs_list_append(out, acct);
  945. }
  946. user_free(&user);
  947. }
  948. }
  949. xs_set_free(&seen);
  950. }
  951. }
  952. else
  953. /* is it a local user? */
  954. if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
  955. if (opt == NULL) {
  956. /* account information */
  957. actor = msg_actor(&snac2);
  958. out = mastoapi_account(actor);
  959. }
  960. else
  961. if (strcmp(opt, "statuses") == 0) { /** **/
  962. /* the public list of posts of a user */
  963. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  964. xs_list *p = timeline;
  965. xs_str *v;
  966. out = xs_list_new();
  967. while (xs_list_iter(&p, &v)) {
  968. xs *msg = NULL;
  969. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  970. /* add only posts by the author */
  971. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  972. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  973. xs *st = mastoapi_status(&snac2, msg);
  974. if (st)
  975. out = xs_list_append(out, st);
  976. }
  977. }
  978. }
  979. }
  980. user_free(&snac2);
  981. }
  982. else {
  983. /* try the uid as the md5 of a possibly loaded actor */
  984. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  985. if (opt == NULL) {
  986. /* account information */
  987. out = mastoapi_account(actor);
  988. }
  989. else
  990. if (strcmp(opt, "statuses") == 0) {
  991. /* we don't serve statuses of others; return the empty list */
  992. out = xs_list_new();
  993. }
  994. }
  995. }
  996. if (out != NULL) {
  997. *body = xs_json_dumps(out, 4);
  998. *ctype = "application/json";
  999. status = 200;
  1000. }
  1001. }
  1002. }
  1003. else
  1004. if (strcmp(cmd, "/v1/timelines/home") == 0) { /** **/
  1005. /* the private timeline */
  1006. if (logged_in) {
  1007. const char *max_id = xs_dict_get(args, "max_id");
  1008. const char *since_id = xs_dict_get(args, "since_id");
  1009. const char *min_id = xs_dict_get(args, "min_id");
  1010. const char *limit_s = xs_dict_get(args, "limit");
  1011. int limit = 0;
  1012. int cnt = 0;
  1013. if (!xs_is_null(limit_s))
  1014. limit = atoi(limit_s);
  1015. if (limit == 0)
  1016. limit = 20;
  1017. xs *timeline = timeline_simple_list(&snac1, "private", 0, 2048);
  1018. xs *out = xs_list_new();
  1019. xs_list *p = timeline;
  1020. xs_str *v;
  1021. while (xs_list_iter(&p, &v) && cnt < limit) {
  1022. xs *msg = NULL;
  1023. /* only return entries older that max_id */
  1024. if (max_id) {
  1025. if (strcmp(v, MID_TO_MD5(max_id)) == 0)
  1026. max_id = NULL;
  1027. continue;
  1028. }
  1029. /* only returns entries newer than since_id */
  1030. if (since_id) {
  1031. if (strcmp(v, MID_TO_MD5(since_id)) == 0)
  1032. break;
  1033. }
  1034. /* only returns entries newer than min_id */
  1035. /* what does really "Return results immediately newer than ID" mean? */
  1036. if (min_id) {
  1037. if (strcmp(v, MID_TO_MD5(min_id)) == 0)
  1038. break;
  1039. }
  1040. /* get the entry */
  1041. if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
  1042. continue;
  1043. /* discard non-Notes */
  1044. const char *type = xs_dict_get(msg, "type");
  1045. if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0)
  1046. continue;
  1047. const char *atto = xs_dict_get(msg, "attributedTo");
  1048. const char *id = xs_dict_get(msg, "id");
  1049. /* is this message from a person we don't follow? */
  1050. if (strcmp(atto, snac1.actor) && !following_check(&snac1, atto)) {
  1051. /* discard if it was not boosted */
  1052. xs *idx = object_announces(id);
  1053. if (xs_list_len(idx) == 0)
  1054. continue;
  1055. }
  1056. /* discard notes from muted morons */
  1057. if (is_muted(&snac1, atto))
  1058. continue;
  1059. /* discard hidden notes */
  1060. if (is_hidden(&snac1, id))
  1061. continue;
  1062. /* discard poll votes (they have a name) */
  1063. if (!xs_is_null(xs_dict_get(msg, "name")))
  1064. continue;
  1065. /* convert the Note into a Mastodon status */
  1066. xs *st = mastoapi_status(&snac1, msg);
  1067. if (st != NULL)
  1068. out = xs_list_append(out, st);
  1069. cnt++;
  1070. }
  1071. *body = xs_json_dumps(out, 4);
  1072. *ctype = "application/json";
  1073. status = 200;
  1074. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  1075. }
  1076. else {
  1077. status = 401; // unauthorized
  1078. }
  1079. }
  1080. else
  1081. if (strcmp(cmd, "/v1/timelines/public") == 0) { /** **/
  1082. /* the instance public timeline (public timelines for all users) */
  1083. const char *limit_s = xs_dict_get(args, "limit");
  1084. int limit = 0;
  1085. int cnt = 0;
  1086. if (!xs_is_null(limit_s))
  1087. limit = atoi(limit_s);
  1088. if (limit == 0)
  1089. limit = 20;
  1090. xs *timeline = timeline_instance_list(0, limit);
  1091. xs *out = xs_list_new();
  1092. xs_list *p = timeline;
  1093. xs_str *md5;
  1094. snac *user = NULL;
  1095. if (logged_in)
  1096. user = &snac1;
  1097. while (xs_list_iter(&p, &md5) && cnt < limit) {
  1098. xs *msg = NULL;
  1099. /* get the entry */
  1100. if (!valid_status(object_get_by_md5(md5, &msg)))
  1101. continue;
  1102. /* discard non-Notes */
  1103. const char *type = xs_dict_get(msg, "type");
  1104. if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0)
  1105. continue;
  1106. /* discard private users */
  1107. {
  1108. const char *atto = xs_dict_get(msg, "attributedTo");
  1109. xs *l = xs_split(atto, "/");
  1110. const char *uid = xs_list_get(l, -1);
  1111. snac p_user;
  1112. int skip = 1;
  1113. if (uid && user_open(&p_user, uid)) {
  1114. if (xs_type(xs_dict_get(p_user.config, "private")) != XSTYPE_TRUE)
  1115. skip = 0;
  1116. user_free(&p_user);
  1117. }
  1118. if (skip)
  1119. continue;
  1120. }
  1121. /* convert the Note into a Mastodon status */
  1122. xs *st = mastoapi_status(user, msg);
  1123. if (st != NULL) {
  1124. out = xs_list_append(out, st);
  1125. cnt++;
  1126. }
  1127. }
  1128. *body = xs_json_dumps(out, 4);
  1129. *ctype = "application/json";
  1130. status = 200;
  1131. }
  1132. else
  1133. if (strcmp(cmd, "/v1/conversations") == 0) { /** **/
  1134. /* TBD */
  1135. *body = xs_dup("[]");
  1136. *ctype = "application/json";
  1137. status = 200;
  1138. }
  1139. else
  1140. if (strcmp(cmd, "/v1/notifications") == 0) { /** **/
  1141. if (logged_in) {
  1142. xs *l = notify_list(&snac1, 0);
  1143. xs *out = xs_list_new();
  1144. xs_list *p = l;
  1145. xs_dict *v;
  1146. xs_list *excl = xs_dict_get(args, "exclude_types[]");
  1147. while (xs_list_iter(&p, &v)) {
  1148. xs *noti = notify_get(&snac1, v);
  1149. if (noti == NULL)
  1150. continue;
  1151. const char *type = xs_dict_get(noti, "type");
  1152. const char *utype = xs_dict_get(noti, "utype");
  1153. const char *objid = xs_dict_get(noti, "objid");
  1154. xs *actor = NULL;
  1155. xs *entry = NULL;
  1156. if (!valid_status(actor_get(xs_dict_get(noti, "actor"), &actor)))
  1157. continue;
  1158. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  1159. continue;
  1160. if (is_hidden(&snac1, objid))
  1161. continue;
  1162. /* convert the type */
  1163. if (strcmp(type, "Like") == 0)
  1164. type = "favourite";
  1165. else
  1166. if (strcmp(type, "Announce") == 0)
  1167. type = "reblog";
  1168. else
  1169. if (strcmp(type, "Follow") == 0)
  1170. type = "follow";
  1171. else
  1172. if (strcmp(type, "Create") == 0)
  1173. type = "mention";
  1174. else
  1175. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  1176. type = "poll";
  1177. else
  1178. continue;
  1179. /* excluded type? */
  1180. if (!xs_is_null(excl) && xs_list_in(excl, type) != -1)
  1181. continue;
  1182. xs *mn = xs_dict_new();
  1183. mn = xs_dict_append(mn, "type", type);
  1184. xs *id = xs_replace(xs_dict_get(noti, "id"), ".", "");
  1185. mn = xs_dict_append(mn, "id", id);
  1186. mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
  1187. xs *acct = mastoapi_account(actor);
  1188. mn = xs_dict_append(mn, "account", acct);
  1189. if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
  1190. xs *st = mastoapi_status(&snac1, entry);
  1191. if (st)
  1192. mn = xs_dict_append(mn, "status", st);
  1193. }
  1194. out = xs_list_append(out, mn);
  1195. }
  1196. *body = xs_json_dumps(out, 4);
  1197. *ctype = "application/json";
  1198. status = 200;
  1199. }
  1200. else
  1201. status = 401;
  1202. }
  1203. else
  1204. if (strcmp(cmd, "/v1/filters") == 0) { /** **/
  1205. /* snac will never have filters */
  1206. *body = xs_dup("[]");
  1207. *ctype = "application/json";
  1208. status = 200;
  1209. }
  1210. else
  1211. if (strcmp(cmd, "/v1/favourites") == 0) { /** **/
  1212. /* snac will never support a list of favourites */
  1213. *body = xs_dup("[]");
  1214. *ctype = "application/json";
  1215. status = 200;
  1216. }
  1217. else
  1218. if (strcmp(cmd, "/v1/bookmarks") == 0) { /** **/
  1219. /* snac does not support bookmarks */
  1220. *body = xs_dup("[]");
  1221. *ctype = "application/json";
  1222. status = 200;
  1223. }
  1224. else
  1225. if (strcmp(cmd, "/v1/lists") == 0) { /** **/
  1226. /* snac does not support lists */
  1227. *body = xs_dup("[]");
  1228. *ctype = "application/json";
  1229. status = 200;
  1230. }
  1231. else
  1232. if (strcmp(cmd, "/v1/scheduled_statuses") == 0) { /** **/
  1233. /* snac does not schedule notes */
  1234. *body = xs_dup("[]");
  1235. *ctype = "application/json";
  1236. status = 200;
  1237. }
  1238. else
  1239. if (strcmp(cmd, "/v1/follow_requests") == 0) { /** **/
  1240. /* snac does not support optional follow confirmations */
  1241. *body = xs_dup("[]");
  1242. *ctype = "application/json";
  1243. status = 200;
  1244. }
  1245. else
  1246. if (strcmp(cmd, "/v1/announcements") == 0) { /** **/
  1247. /* snac has no announcements (yet?) */
  1248. *body = xs_dup("[]");
  1249. *ctype = "application/json";
  1250. status = 200;
  1251. }
  1252. else
  1253. if (strcmp(cmd, "/v1/custom_emojis") == 0) { /** **/
  1254. /* are you kidding me? */
  1255. *body = xs_dup("[]");
  1256. *ctype = "application/json";
  1257. status = 200;
  1258. }
  1259. else
  1260. if (strcmp(cmd, "/v1/instance") == 0) { /** **/
  1261. /* returns an instance object */
  1262. xs *ins = xs_dict_new();
  1263. const char *host = xs_dict_get(srv_config, "host");
  1264. const char *title = xs_dict_get(srv_config, "title");
  1265. const char *sdesc = xs_dict_get(srv_config, "short_description");
  1266. ins = xs_dict_append(ins, "uri", host);
  1267. ins = xs_dict_append(ins, "domain", host);
  1268. ins = xs_dict_append(ins, "title", title && *title ? title : host);
  1269. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  1270. ins = xs_dict_append(ins, "source_url", WHAT_IS_SNAC_URL);
  1271. ins = xs_dict_append(ins, "description", host);
  1272. ins = xs_dict_append(ins, "short_description", sdesc && *sdesc ? sdesc : host);
  1273. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  1274. ins = xs_dict_append(ins, "thumbnail", susie);
  1275. const char *v = xs_dict_get(srv_config, "admin_email");
  1276. if (xs_is_null(v) || *v == '\0')
  1277. v = "admin@localhost";
  1278. ins = xs_dict_append(ins, "email", v);
  1279. ins = xs_dict_append(ins, "rules", xs_stock_list);
  1280. xs *l1 = xs_list_append(xs_list_new(), "en");
  1281. ins = xs_dict_append(ins, "languages", l1);
  1282. ins = xs_dict_append(ins, "urls", xs_stock_dict);
  1283. xs *d2 = xs_dict_append(xs_dict_new(), "user_count", xs_stock_0);
  1284. d2 = xs_dict_append(d2, "status_count", xs_stock_0);
  1285. d2 = xs_dict_append(d2, "domain_count", xs_stock_0);
  1286. ins = xs_dict_append(ins, "stats", d2);
  1287. ins = xs_dict_append(ins, "registrations", xs_stock_false);
  1288. ins = xs_dict_append(ins, "approval_required", xs_stock_false);
  1289. ins = xs_dict_append(ins, "invites_enabled", xs_stock_false);
  1290. xs *cfg = xs_dict_new();
  1291. {
  1292. xs *d11 = xs_json_loads("{\"characters_reserved_per_url\":32,"
  1293. "\"max_characters\":100000,\"max_media_attachments\":8}");
  1294. cfg = xs_dict_append(cfg, "statuses", d11);
  1295. xs *d12 = xs_json_loads("{\"max_featured_tags\":10}");
  1296. cfg = xs_dict_append(cfg, "accounts", d12);
  1297. xs *d13 = xs_json_loads("{\"image_matrix_limit\":33177600,"
  1298. "\"image_size_limit\":16777216,"
  1299. "\"video_frame_rate_limit\":120,"
  1300. "\"video_matrix_limit\":8294400,"
  1301. "\"video_size_limit\":103809024}"
  1302. );
  1303. {
  1304. /* get the supported mime types from the internal list */
  1305. const char **p = xs_mime_types;
  1306. xs_set mtypes;
  1307. xs_set_init(&mtypes);
  1308. while (*p) {
  1309. const char *type = p[1];
  1310. if (xs_startswith(type, "image/") ||
  1311. xs_startswith(type, "video/") ||
  1312. xs_startswith(type, "audio/"))
  1313. xs_set_add(&mtypes, type);
  1314. p += 2;
  1315. }
  1316. xs *l = xs_set_result(&mtypes);
  1317. d13 = xs_dict_append(d13, "supported_mime_types", l);
  1318. }
  1319. cfg = xs_dict_append(cfg, "media_attachments", d13);
  1320. xs *d14 = xs_json_loads("{\"max_characters_per_option\":50,"
  1321. "\"max_expiration\":2629746,"
  1322. "\"max_options\":8,\"min_expiration\":300}");
  1323. cfg = xs_dict_append(cfg, "polls", d14);
  1324. }
  1325. ins = xs_dict_append(ins, "configuration", cfg);
  1326. const char *admin_account = xs_dict_get(srv_config, "admin_account");
  1327. if (!xs_is_null(admin_account) && *admin_account) {
  1328. snac admin;
  1329. if (user_open(&admin, admin_account)) {
  1330. xs *actor = msg_actor(&admin);
  1331. xs *acct = mastoapi_account(actor);
  1332. ins = xs_dict_append(ins, "contact_account", acct);
  1333. user_free(&admin);
  1334. }
  1335. }
  1336. *body = xs_json_dumps(ins, 4);
  1337. *ctype = "application/json";
  1338. status = 200;
  1339. }
  1340. else
  1341. if (xs_startswith(cmd, "/v1/statuses/")) { /** **/
  1342. /* information about a status */
  1343. if (logged_in) {
  1344. xs *l = xs_split(cmd, "/");
  1345. const char *id = xs_list_get(l, 3);
  1346. const char *op = xs_list_get(l, 4);
  1347. if (!xs_is_null(id)) {
  1348. xs *msg = NULL;
  1349. xs *out = NULL;
  1350. /* skip the 'fake' part of the id */
  1351. id = MID_TO_MD5(id);
  1352. if (valid_status(object_get_by_md5(id, &msg))) {
  1353. if (op == NULL) {
  1354. if (!is_muted(&snac1, xs_dict_get(msg, "attributedTo"))) {
  1355. /* return the status itself */
  1356. out = mastoapi_status(&snac1, msg);
  1357. }
  1358. }
  1359. else
  1360. if (strcmp(op, "context") == 0) { /** **/
  1361. /* return ancestors and children */
  1362. xs *anc = xs_list_new();
  1363. xs *des = xs_list_new();
  1364. xs_list *p;
  1365. xs_str *v;
  1366. char pid[64];
  1367. /* build the [grand]parent list, moving up */
  1368. strncpy(pid, id, sizeof(pid));
  1369. while (object_parent(pid, pid, sizeof(pid))) {
  1370. xs *m2 = NULL;
  1371. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  1372. xs *st = mastoapi_status(&snac1, m2);
  1373. if (st)
  1374. anc = xs_list_insert(anc, 0, st);
  1375. }
  1376. else
  1377. break;
  1378. }
  1379. /* build the children list */
  1380. xs *children = object_children(xs_dict_get(msg, "id"));
  1381. p = children;
  1382. while (xs_list_iter(&p, &v)) {
  1383. xs *m2 = NULL;
  1384. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  1385. if (xs_is_null(xs_dict_get(m2, "name"))) {
  1386. xs *st = mastoapi_status(&snac1, m2);
  1387. if (st)
  1388. des = xs_list_append(des, st);
  1389. }
  1390. }
  1391. }
  1392. out = xs_dict_new();
  1393. out = xs_dict_append(out, "ancestors", anc);
  1394. out = xs_dict_append(out, "descendants", des);
  1395. }
  1396. else
  1397. if (strcmp(op, "reblogged_by") == 0 || /** **/
  1398. strcmp(op, "favourited_by") == 0) { /** **/
  1399. /* return the list of people who liked or boosted this */
  1400. out = xs_list_new();
  1401. xs *l = NULL;
  1402. if (op[0] == 'r')
  1403. l = object_announces(xs_dict_get(msg, "id"));
  1404. else
  1405. l = object_likes(xs_dict_get(msg, "id"));
  1406. xs_list *p = l;
  1407. xs_str *v;
  1408. while (xs_list_iter(&p, &v)) {
  1409. xs *actor2 = NULL;
  1410. if (valid_status(object_get_by_md5(v, &actor2))) {
  1411. xs *acct2 = mastoapi_account(actor2);
  1412. out = xs_list_append(out, acct2);
  1413. }
  1414. }
  1415. }
  1416. }
  1417. else
  1418. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  1419. if (out != NULL) {
  1420. *body = xs_json_dumps(out, 4);
  1421. *ctype = "application/json";
  1422. status = 200;
  1423. }
  1424. }
  1425. }
  1426. else
  1427. status = 401;
  1428. }
  1429. else
  1430. if (strcmp(cmd, "/v1/preferences") == 0) { /** **/
  1431. *body = xs_dup("{}");
  1432. *ctype = "application/json";
  1433. status = 200;
  1434. }
  1435. else
  1436. if (strcmp(cmd, "/v1/markers") == 0) { /** **/
  1437. *body = xs_dup("{}");
  1438. *ctype = "application/json";
  1439. status = 200;
  1440. }
  1441. else
  1442. if (strcmp(cmd, "/v1/followed_tags") == 0) { /** **/
  1443. *body = xs_dup("[]");
  1444. *ctype = "application/json";
  1445. status = 200;
  1446. }
  1447. else
  1448. if (strcmp(cmd, "/v2/search") == 0) { /** **/
  1449. if (logged_in) {
  1450. const char *q = xs_dict_get(args, "q");
  1451. const char *type = xs_dict_get(args, "type");
  1452. const char *offset = xs_dict_get(args, "offset");
  1453. xs *acl = xs_list_new();
  1454. xs *stl = xs_list_new();
  1455. xs *htl = xs_list_new();
  1456. xs *res = xs_dict_new();
  1457. if (xs_is_null(offset) || strcmp(offset, "0") == 0) {
  1458. /* reply something only for offset 0; otherwise,
  1459. apps like Tusky keep asking again and again */
  1460. if (!xs_is_null(q) && !xs_is_null(type) && strcmp(type, "accounts") == 0) {
  1461. /* do a webfinger query */
  1462. char *actor = NULL;
  1463. char *user = NULL;
  1464. if (valid_status(webfinger_request(q, &actor, &user))) {
  1465. xs *actor_o = NULL;
  1466. if (valid_status(actor_request(&snac1, actor, &actor_o))) {
  1467. xs *acct = mastoapi_account(actor_o);
  1468. acl = xs_list_append(acl, acct);
  1469. }
  1470. }
  1471. }
  1472. }
  1473. res = xs_dict_append(res, "accounts", acl);
  1474. res = xs_dict_append(res, "statuses", stl);
  1475. res = xs_dict_append(res, "hashtags", htl);
  1476. *body = xs_json_dumps(res, 4);
  1477. *ctype = "application/json";
  1478. status = 200;
  1479. }
  1480. else
  1481. status = 401;
  1482. }
  1483. /* user cleanup */
  1484. if (logged_in)
  1485. user_free(&snac1);
  1486. return status;
  1487. }
  1488. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  1489. const char *payload, int p_size,
  1490. char **body, int *b_size, char **ctype)
  1491. {
  1492. (void)p_size;
  1493. (void)b_size;
  1494. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1495. return 0;
  1496. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1497. int status = 404;
  1498. xs *args = NULL;
  1499. char *i_ctype = xs_dict_get(req, "content-type");
  1500. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1501. args = xs_json_loads(payload);
  1502. else
  1503. args = xs_dup(xs_dict_get(req, "p_vars"));
  1504. if (args == NULL)
  1505. return 400;
  1506. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1507. snac snac = {0};
  1508. int logged_in = process_auth_token(&snac, req);
  1509. if (strcmp(cmd, "/v1/apps") == 0) { /** **/
  1510. const char *name = xs_dict_get(args, "client_name");
  1511. const char *ruri = xs_dict_get(args, "redirect_uris");
  1512. const char *scope = xs_dict_get(args, "scope");
  1513. if (xs_type(ruri) == XSTYPE_LIST)
  1514. ruri = xs_dict_get(ruri, 0);
  1515. if (name && ruri) {
  1516. xs *app = xs_dict_new();
  1517. xs *id = xs_replace_i(tid(0), ".", "");
  1518. xs *csec = random_str();
  1519. xs *vkey = random_str();
  1520. xs *cid = NULL;
  1521. /* pick a non-existent random cid */
  1522. for (;;) {
  1523. cid = random_str();
  1524. xs *p_app = app_get(cid);
  1525. if (p_app == NULL)
  1526. break;
  1527. xs_free(cid);
  1528. }
  1529. app = xs_dict_append(app, "name", name);
  1530. app = xs_dict_append(app, "redirect_uri", ruri);
  1531. app = xs_dict_append(app, "client_id", cid);
  1532. app = xs_dict_append(app, "client_secret", csec);
  1533. app = xs_dict_append(app, "vapid_key", vkey);
  1534. app = xs_dict_append(app, "id", id);
  1535. *body = xs_json_dumps(app, 4);
  1536. *ctype = "application/json";
  1537. status = 200;
  1538. app = xs_dict_append(app, "code", "");
  1539. if (scope)
  1540. app = xs_dict_append(app, "scope", scope);
  1541. app_add(cid, app);
  1542. srv_debug(1, xs_fmt("mastoapi apps: new app %s", cid));
  1543. }
  1544. }
  1545. else
  1546. if (strcmp(cmd, "/v1/statuses") == 0) { /** **/
  1547. if (logged_in) {
  1548. /* post a new Note */
  1549. const char *content = xs_dict_get(args, "status");
  1550. const char *mid = xs_dict_get(args, "in_reply_to_id");
  1551. const char *visibility = xs_dict_get(args, "visibility");
  1552. const char *summary = xs_dict_get(args, "spoiler_text");
  1553. const char *media_ids = xs_dict_get(args, "media_ids");
  1554. if (xs_is_null(media_ids))
  1555. media_ids = xs_dict_get(args, "media_ids[]");
  1556. if (xs_is_null(media_ids))
  1557. media_ids = xs_dict_get(args, "media_ids");
  1558. if (xs_is_null(visibility))
  1559. visibility = "public";
  1560. xs *attach_list = xs_list_new();
  1561. xs *irt = NULL;
  1562. /* is it a reply? */
  1563. if (mid != NULL) {
  1564. xs *r_msg = NULL;
  1565. const char *md5 = MID_TO_MD5(mid);
  1566. if (valid_status(object_get_by_md5(md5, &r_msg)))
  1567. irt = xs_dup(xs_dict_get(r_msg, "id"));
  1568. }
  1569. /* does it have attachments? */
  1570. if (!xs_is_null(media_ids)) {
  1571. xs *mi = NULL;
  1572. if (xs_type(media_ids) == XSTYPE_LIST)
  1573. mi = xs_dup(media_ids);
  1574. else {
  1575. mi = xs_list_new();
  1576. mi = xs_list_append(mi, media_ids);
  1577. }
  1578. xs_list *p = mi;
  1579. xs_str *v;
  1580. while (xs_list_iter(&p, &v)) {
  1581. xs *l = xs_list_new();
  1582. xs *url = xs_fmt("%s/s/%s", snac.actor, v);
  1583. xs *desc = static_get_meta(&snac, v);
  1584. l = xs_list_append(l, url);
  1585. l = xs_list_append(l, desc);
  1586. attach_list = xs_list_append(attach_list, l);
  1587. }
  1588. }
  1589. /* prepare the message */
  1590. xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
  1591. strcmp(visibility, "public") == 0 ? 0 : 1);
  1592. if (!xs_is_null(summary) && *summary) {
  1593. msg = xs_dict_set(msg, "sensitive", xs_stock_true);
  1594. msg = xs_dict_set(msg, "summary", summary);
  1595. }
  1596. /* store */
  1597. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1598. /* 'Create' message */
  1599. xs *c_msg = msg_create(&snac, msg);
  1600. enqueue_message(&snac, c_msg);
  1601. timeline_touch(&snac);
  1602. /* convert to a mastodon status as a response code */
  1603. xs *st = mastoapi_status(&snac, msg);
  1604. *body = xs_json_dumps(st, 4);
  1605. *ctype = "application/json";
  1606. status = 200;
  1607. }
  1608. else
  1609. status = 401;
  1610. }
  1611. else
  1612. if (xs_startswith(cmd, "/v1/statuses")) { /** **/
  1613. if (logged_in) {
  1614. /* operations on a status */
  1615. xs *l = xs_split(cmd, "/");
  1616. const char *mid = xs_list_get(l, 3);
  1617. const char *op = xs_list_get(l, 4);
  1618. if (!xs_is_null(mid)) {
  1619. xs *msg = NULL;
  1620. xs *out = NULL;
  1621. /* skip the 'fake' part of the id */
  1622. mid = MID_TO_MD5(mid);
  1623. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1624. char *id = xs_dict_get(msg, "id");
  1625. if (op == NULL) {
  1626. /* no operation (?) */
  1627. }
  1628. else
  1629. if (strcmp(op, "favourite") == 0) { /** **/
  1630. xs *n_msg = msg_admiration(&snac, id, "Like");
  1631. if (n_msg != NULL) {
  1632. enqueue_message(&snac, n_msg);
  1633. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  1634. out = mastoapi_status(&snac, msg);
  1635. }
  1636. }
  1637. else
  1638. if (strcmp(op, "unfavourite") == 0) { /** **/
  1639. /* partial support: as the original Like message
  1640. is not stored anywhere here, it's not possible
  1641. to send an Undo + Like; the only thing done here
  1642. is to delete the actor from the list of likes */
  1643. object_unadmire(id, snac.actor, 1);
  1644. }
  1645. else
  1646. if (strcmp(op, "reblog") == 0) { /** **/
  1647. xs *n_msg = msg_admiration(&snac, id, "Announce");
  1648. if (n_msg != NULL) {
  1649. enqueue_message(&snac, n_msg);
  1650. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  1651. out = mastoapi_status(&snac, msg);
  1652. }
  1653. }
  1654. else
  1655. if (strcmp(op, "unreblog") == 0) { /** **/
  1656. /* partial support: see comment in 'unfavourite' */
  1657. object_unadmire(id, snac.actor, 0);
  1658. }
  1659. else
  1660. if (strcmp(op, "bookmark") == 0) { /** **/
  1661. /* snac does not support bookmarks */
  1662. }
  1663. else
  1664. if (strcmp(op, "unbookmark") == 0) { /** **/
  1665. /* snac does not support bookmarks */
  1666. }
  1667. else
  1668. if (strcmp(op, "pin") == 0) { /** **/
  1669. /* pin this message */
  1670. if (pin(&snac, id))
  1671. out = mastoapi_status(&snac, msg);
  1672. else
  1673. status = 422;
  1674. }
  1675. else
  1676. if (strcmp(op, "unpin") == 0) { /** **/
  1677. /* unpin this message */
  1678. unpin(&snac, id);
  1679. out = mastoapi_status(&snac, msg);
  1680. }
  1681. else
  1682. if (strcmp(op, "mute") == 0) { /** **/
  1683. /* Mastodon's mute is snac's hide */
  1684. }
  1685. else
  1686. if (strcmp(op, "unmute") == 0) { /** **/
  1687. /* Mastodon's unmute is snac's unhide */
  1688. }
  1689. }
  1690. if (out != NULL) {
  1691. *body = xs_json_dumps(out, 4);
  1692. *ctype = "application/json";
  1693. status = 200;
  1694. }
  1695. }
  1696. }
  1697. else
  1698. status = 401;
  1699. }
  1700. else
  1701. if (strcmp(cmd, "/v1/notifications/clear") == 0) { /** **/
  1702. if (logged_in) {
  1703. notify_clear(&snac);
  1704. timeline_touch(&snac);
  1705. *body = xs_dup("{}");
  1706. *ctype = "application/json";
  1707. status = 200;
  1708. }
  1709. else
  1710. status = 401;
  1711. }
  1712. else
  1713. if (strcmp(cmd, "/v1/push/subscription") == 0) { /** **/
  1714. /* I don't know what I'm doing */
  1715. if (logged_in) {
  1716. char *v;
  1717. xs *wpush = xs_dict_new();
  1718. wpush = xs_dict_append(wpush, "id", "1");
  1719. v = xs_dict_get(args, "data");
  1720. v = xs_dict_get(v, "alerts");
  1721. wpush = xs_dict_append(wpush, "alerts", v);
  1722. v = xs_dict_get(args, "subscription");
  1723. v = xs_dict_get(v, "endpoint");
  1724. wpush = xs_dict_append(wpush, "endpoint", v);
  1725. xs *server_key = random_str();
  1726. wpush = xs_dict_append(wpush, "server_key", server_key);
  1727. *body = xs_json_dumps(wpush, 4);
  1728. *ctype = "application/json";
  1729. status = 200;
  1730. }
  1731. else
  1732. status = 401;
  1733. }
  1734. else
  1735. if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { /** **/
  1736. if (logged_in) {
  1737. const xs_list *file = xs_dict_get(args, "file");
  1738. const char *desc = xs_dict_get(args, "description");
  1739. if (xs_is_null(desc))
  1740. desc = "";
  1741. status = 400;
  1742. if (xs_type(file) == XSTYPE_LIST) {
  1743. const char *fn = xs_list_get(file, 0);
  1744. if (*fn != '\0') {
  1745. char *ext = strrchr(fn, '.');
  1746. xs *hash = xs_md5_hex(fn, strlen(fn));
  1747. xs *id = xs_fmt("%s%s", hash, ext);
  1748. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1749. int fo = xs_number_get(xs_list_get(file, 1));
  1750. int fs = xs_number_get(xs_list_get(file, 2));
  1751. /* store */
  1752. static_put(&snac, id, payload + fo, fs);
  1753. static_put_meta(&snac, id, desc);
  1754. /* prepare a response */
  1755. xs *rsp = xs_dict_new();
  1756. rsp = xs_dict_append(rsp, "id", id);
  1757. rsp = xs_dict_append(rsp, "type", "image");
  1758. rsp = xs_dict_append(rsp, "url", url);
  1759. rsp = xs_dict_append(rsp, "preview_url", url);
  1760. rsp = xs_dict_append(rsp, "remote_url", url);
  1761. rsp = xs_dict_append(rsp, "description", desc);
  1762. *body = xs_json_dumps(rsp, 4);
  1763. *ctype = "application/json";
  1764. status = 200;
  1765. }
  1766. }
  1767. }
  1768. else
  1769. status = 401;
  1770. }
  1771. else
  1772. if (xs_startswith(cmd, "/v1/accounts")) { /** **/
  1773. if (logged_in) {
  1774. /* account-related information */
  1775. xs *l = xs_split(cmd, "/");
  1776. const char *md5 = xs_list_get(l, 3);
  1777. const char *opt = xs_list_get(l, 4);
  1778. xs *rsp = NULL;
  1779. if (!xs_is_null(md5) && *md5) {
  1780. xs *actor_o = NULL;
  1781. if (xs_is_null(opt)) {
  1782. /* ? */
  1783. }
  1784. else
  1785. if (strcmp(opt, "follow") == 0) { /** **/
  1786. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1787. const char *actor = xs_dict_get(actor_o, "id");
  1788. xs *msg = msg_follow(&snac, actor);
  1789. if (msg != NULL) {
  1790. /* reload the actor from the message, in may be different */
  1791. actor = xs_dict_get(msg, "object");
  1792. following_add(&snac, actor, msg);
  1793. enqueue_output_by_actor(&snac, msg, actor, 0);
  1794. rsp = mastoapi_relationship(&snac, md5);
  1795. }
  1796. }
  1797. }
  1798. else
  1799. if (strcmp(opt, "unfollow") == 0) { /** **/
  1800. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1801. const char *actor = xs_dict_get(actor_o, "id");
  1802. /* get the following object */
  1803. xs *object = NULL;
  1804. if (valid_status(following_get(&snac, actor, &object))) {
  1805. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  1806. following_del(&snac, actor);
  1807. enqueue_output_by_actor(&snac, msg, actor, 0);
  1808. rsp = mastoapi_relationship(&snac, md5);
  1809. }
  1810. }
  1811. }
  1812. else
  1813. if (strcmp(opt, "block") == 0) { /** **/
  1814. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1815. const char *actor = xs_dict_get(actor_o, "id");
  1816. mute(&snac, actor);
  1817. rsp = mastoapi_relationship(&snac, md5);
  1818. }
  1819. }
  1820. else
  1821. if (strcmp(opt, "unblock") == 0) { /** **/
  1822. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1823. const char *actor = xs_dict_get(actor_o, "id");
  1824. unmute(&snac, actor);
  1825. rsp = mastoapi_relationship(&snac, md5);
  1826. }
  1827. }
  1828. }
  1829. if (rsp != NULL) {
  1830. *body = xs_json_dumps(rsp, 4);
  1831. *ctype = "application/json";
  1832. status = 200;
  1833. }
  1834. }
  1835. else
  1836. status = 401;
  1837. }
  1838. else
  1839. if (xs_startswith(cmd, "/v1/polls")) { /** **/
  1840. if (logged_in) {
  1841. /* operations on a status */
  1842. xs *l = xs_split(cmd, "/");
  1843. const char *mid = xs_list_get(l, 3);
  1844. const char *op = xs_list_get(l, 4);
  1845. if (!xs_is_null(mid)) {
  1846. xs *msg = NULL;
  1847. xs *out = NULL;
  1848. /* skip the 'fake' part of the id */
  1849. mid = MID_TO_MD5(mid);
  1850. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1851. const char *id = xs_dict_get(msg, "id");
  1852. const char *atto = xs_dict_get(msg, "attributedTo");
  1853. xs_list *opts = xs_dict_get(msg, "oneOf");
  1854. if (opts == NULL)
  1855. opts = xs_dict_get(msg, "anyOf");
  1856. if (op == NULL) {
  1857. }
  1858. else
  1859. if (strcmp(op, "votes") == 0) {
  1860. xs_list *choices = xs_dict_get(args, "choices[]");
  1861. if (xs_is_null(choices))
  1862. choices = xs_dict_get(args, "choices");
  1863. if (xs_type(choices) == XSTYPE_LIST) {
  1864. xs_str *v;
  1865. while (xs_list_iter(&choices, &v)) {
  1866. int io = atoi(v);
  1867. const xs_dict *o = xs_list_get(opts, io);
  1868. if (o) {
  1869. const char *name = xs_dict_get(o, "name");
  1870. xs *msg = msg_note(&snac, "", atto, (char *)id, NULL, 1);
  1871. msg = xs_dict_append(msg, "name", name);
  1872. xs *c_msg = msg_create(&snac, msg);
  1873. enqueue_message(&snac, c_msg);
  1874. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1875. }
  1876. }
  1877. out = mastoapi_poll(&snac, msg);
  1878. }
  1879. }
  1880. }
  1881. if (out != NULL) {
  1882. *body = xs_json_dumps(out, 4);
  1883. *ctype = "application/json";
  1884. status = 200;
  1885. }
  1886. }
  1887. }
  1888. else
  1889. status = 401;
  1890. }
  1891. /* user cleanup */
  1892. if (logged_in)
  1893. user_free(&snac);
  1894. return status;
  1895. }
  1896. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  1897. const char *payload, int p_size,
  1898. char **body, int *b_size, char **ctype)
  1899. {
  1900. (void)p_size;
  1901. (void)b_size;
  1902. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1903. return 0;
  1904. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1905. int status = 404;
  1906. xs *args = NULL;
  1907. char *i_ctype = xs_dict_get(req, "content-type");
  1908. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1909. args = xs_json_loads(payload);
  1910. else
  1911. args = xs_dup(xs_dict_get(req, "p_vars"));
  1912. if (args == NULL)
  1913. return 400;
  1914. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1915. snac snac = {0};
  1916. int logged_in = process_auth_token(&snac, req);
  1917. if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) { /** **/
  1918. if (logged_in) {
  1919. xs *l = xs_split(cmd, "/");
  1920. const char *stid = xs_list_get(l, 3);
  1921. if (!xs_is_null(stid)) {
  1922. const char *desc = xs_dict_get(args, "description");
  1923. /* set the image metadata */
  1924. static_put_meta(&snac, stid, desc);
  1925. /* prepare a response */
  1926. xs *rsp = xs_dict_new();
  1927. xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
  1928. rsp = xs_dict_append(rsp, "id", stid);
  1929. rsp = xs_dict_append(rsp, "type", "image");
  1930. rsp = xs_dict_append(rsp, "url", url);
  1931. rsp = xs_dict_append(rsp, "preview_url", url);
  1932. rsp = xs_dict_append(rsp, "remote_url", url);
  1933. rsp = xs_dict_append(rsp, "description", desc);
  1934. *body = xs_json_dumps(rsp, 4);
  1935. *ctype = "application/json";
  1936. status = 200;
  1937. }
  1938. }
  1939. else
  1940. status = 401;
  1941. }
  1942. /* user cleanup */
  1943. if (logged_in)
  1944. user_free(&snac);
  1945. return status;
  1946. }
  1947. void mastoapi_purge(void)
  1948. {
  1949. xs *spec = xs_fmt("%s/app/" "*.json", srv_basedir);
  1950. xs *files = xs_glob(spec, 1, 0);
  1951. xs_list *p = files;
  1952. xs_str *v;
  1953. time_t mt = time(NULL) - 3600;
  1954. while (xs_list_iter(&p, &v)) {
  1955. xs *cid = xs_replace(v, ".json", "");
  1956. xs *fn = _app_fn(cid);
  1957. if (mtime(fn) < mt) {
  1958. /* get the app */
  1959. xs *app = app_get(cid);
  1960. if (app) {
  1961. /* old apps with no uid are incomplete cruft */
  1962. const char *uid = xs_dict_get(app, "uid");
  1963. if (xs_is_null(uid) || *uid == '\0') {
  1964. unlink(fn);
  1965. srv_debug(2, xs_fmt("purged %s", fn));
  1966. }
  1967. }
  1968. }
  1969. }
  1970. }
  1971. #endif /* #ifndef NO_MASTODON_API */