mastoapi.c 77 KB

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