mastoapi.c 74 KB

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