mastoapi.c 75 KB

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