mastoapi.c 75 KB

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