mastoapi.c 81 KB

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