mastoapi.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 grunfink / MIT license */
  3. #ifndef NO_MASTODON_API
  4. #include "xs.h"
  5. #include "xs_encdec.h"
  6. #include "xs_openssl.h"
  7. #include "xs_json.h"
  8. #include "xs_io.h"
  9. #include "xs_time.h"
  10. #include "snac.h"
  11. static xs_str *random_str(void)
  12. /* just what is says in the tin */
  13. {
  14. unsigned int data[4] = {0};
  15. FILE *f;
  16. if ((f = fopen("/dev/random", "r")) != NULL) {
  17. fread(data, sizeof(data), 1, f);
  18. fclose(f);
  19. }
  20. else {
  21. data[0] = random() % 0xffffffff;
  22. data[1] = random() % 0xffffffff;
  23. data[2] = random() % 0xffffffff;
  24. data[3] = random() % 0xffffffff;
  25. }
  26. return xs_hex_enc((char *)data, sizeof(data));
  27. }
  28. int app_add(const char *id, const xs_dict *app)
  29. /* stores an app */
  30. {
  31. if (!xs_is_hex(id))
  32. return 500;
  33. int status = 201;
  34. xs *fn = xs_fmt("%s/app/", srv_basedir);
  35. FILE *f;
  36. mkdirx(fn);
  37. fn = xs_str_cat(fn, id);
  38. fn = xs_str_cat(fn, ".json");
  39. if ((f = fopen(fn, "w")) != NULL) {
  40. xs *j = xs_json_dumps_pp(app, 4);
  41. fwrite(j, strlen(j), 1, f);
  42. fclose(f);
  43. }
  44. else
  45. status = 500;
  46. return status;
  47. }
  48. xs_dict *app_get(const char *id)
  49. /* gets an app */
  50. {
  51. if (!xs_is_hex(id))
  52. return NULL;
  53. xs *fn = xs_fmt("%s/app/%s.json", srv_basedir, id);
  54. xs_dict *app = NULL;
  55. FILE *f;
  56. if ((f = fopen(fn, "r")) != NULL) {
  57. xs *j = xs_readall(f);
  58. fclose(f);
  59. app = xs_json_loads(j);
  60. }
  61. return app;
  62. }
  63. int app_del(const char *id)
  64. /* deletes an app */
  65. {
  66. if (!xs_is_hex(id))
  67. return -1;
  68. xs *fn = xs_fmt("%s/app/%s.json", srv_basedir, id);
  69. return unlink(fn);
  70. }
  71. int token_add(const char *id, const xs_dict *token)
  72. /* stores a token */
  73. {
  74. if (!xs_is_hex(id))
  75. return 500;
  76. int status = 201;
  77. xs *fn = xs_fmt("%s/token/", srv_basedir);
  78. FILE *f;
  79. mkdirx(fn);
  80. fn = xs_str_cat(fn, id);
  81. fn = xs_str_cat(fn, ".json");
  82. if ((f = fopen(fn, "w")) != NULL) {
  83. xs *j = xs_json_dumps_pp(token, 4);
  84. fwrite(j, strlen(j), 1, f);
  85. fclose(f);
  86. }
  87. else
  88. status = 500;
  89. return status;
  90. }
  91. xs_dict *token_get(const char *id)
  92. /* gets a token */
  93. {
  94. if (!xs_is_hex(id))
  95. return NULL;
  96. xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
  97. xs_dict *token = NULL;
  98. FILE *f;
  99. if ((f = fopen(fn, "r")) != NULL) {
  100. xs *j = xs_readall(f);
  101. fclose(f);
  102. token = xs_json_loads(j);
  103. }
  104. return token;
  105. }
  106. int token_del(const char *id)
  107. /* deletes a token */
  108. {
  109. if (!xs_is_hex(id))
  110. return -1;
  111. xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
  112. return unlink(fn);
  113. }
  114. const char *login_page = ""
  115. "<!DOCTYPE html>\n"
  116. "<body><h1>%s OAuth identify</h1>\n"
  117. "<div style=\"background-color: red; color: white\">%s</div>\n"
  118. "<form method=\"post\" action=\"https:/" "/%s/oauth/x-snac-login\">\n"
  119. "<p>Login: <input type=\"text\" name=\"login\"></p>\n"
  120. "<p>Password: <input type=\"password\" name=\"passwd\"></p>\n"
  121. "<input type=\"hidden\" name=\"redir\" value=\"%s\">\n"
  122. "<input type=\"hidden\" name=\"cid\" value=\"%s\">\n"
  123. "<input type=\"hidden\" name=\"state\" value=\"%s\">\n"
  124. "<input type=\"submit\" value=\"OK\">\n"
  125. "</form><p>%s</p></body>\n"
  126. "";
  127. int oauth_get_handler(const xs_dict *req, const char *q_path,
  128. char **body, int *b_size, char **ctype)
  129. {
  130. if (!xs_startswith(q_path, "/oauth/"))
  131. return 0;
  132. /* {
  133. xs *j = xs_json_dumps_pp(req, 4);
  134. printf("oauth get:\n%s\n", j);
  135. }*/
  136. int status = 404;
  137. xs_dict *msg = xs_dict_get(req, "q_vars");
  138. xs *cmd = xs_replace(q_path, "/oauth", "");
  139. srv_debug(1, xs_fmt("oauth_get_handler %s", q_path));
  140. if (strcmp(cmd, "/authorize") == 0) {
  141. const char *cid = xs_dict_get(msg, "client_id");
  142. const char *ruri = xs_dict_get(msg, "redirect_uri");
  143. const char *rtype = xs_dict_get(msg, "response_type");
  144. const char *state = xs_dict_get(msg, "state");
  145. status = 400;
  146. if (cid && ruri && rtype && strcmp(rtype, "code") == 0) {
  147. xs *app = app_get(cid);
  148. if (app != NULL) {
  149. const char *host = xs_dict_get(srv_config, "host");
  150. if (xs_is_null(state))
  151. state = "";
  152. *body = xs_fmt(login_page, host, "", host, ruri, cid, state, USER_AGENT);
  153. *ctype = "text/html";
  154. status = 200;
  155. srv_debug(0, xs_fmt("oauth authorize: generating login page"));
  156. }
  157. else
  158. srv_debug(0, xs_fmt("oauth authorize: bad client_id %s", cid));
  159. }
  160. else
  161. srv_debug(0, xs_fmt("oauth authorize: invalid or unset arguments"));
  162. }
  163. return status;
  164. }
  165. int oauth_post_handler(const xs_dict *req, const char *q_path,
  166. const char *payload, int p_size,
  167. char **body, int *b_size, char **ctype)
  168. {
  169. if (!xs_startswith(q_path, "/oauth/"))
  170. return 0;
  171. /* {
  172. xs *j = xs_json_dumps_pp(req, 4);
  173. printf("oauth post:\n%s\n", j);
  174. }*/
  175. int status = 404;
  176. char *i_ctype = xs_dict_get(req, "content-type");
  177. xs *args = NULL;
  178. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  179. args = xs_json_loads(payload);
  180. else
  181. args = xs_dup(xs_dict_get(req, "p_vars"));
  182. xs *cmd = xs_replace(q_path, "/oauth", "");
  183. srv_debug(1, xs_fmt("oauth_post_handler %s", q_path));
  184. if (strcmp(cmd, "/x-snac-login") == 0) {
  185. const char *login = xs_dict_get(args, "login");
  186. const char *passwd = xs_dict_get(args, "passwd");
  187. const char *redir = xs_dict_get(args, "redir");
  188. const char *cid = xs_dict_get(args, "cid");
  189. const char *state = xs_dict_get(args, "state");
  190. const char *host = xs_dict_get(srv_config, "host");
  191. /* by default, generate another login form with an error */
  192. *body = xs_fmt(login_page, host, "LOGIN INCORRECT", host, redir, cid, state, USER_AGENT);
  193. *ctype = "text/html";
  194. status = 200;
  195. if (login && passwd && redir && cid) {
  196. snac snac;
  197. if (user_open(&snac, login)) {
  198. /* check the login + password */
  199. if (check_password(login, passwd,
  200. xs_dict_get(snac.config, "passwd"))) {
  201. /* success! redirect to the desired uri */
  202. xs *code = random_str();
  203. xs_free(*body);
  204. *body = xs_fmt("%s?code=%s", redir, code);
  205. status = 303;
  206. /* if there is a state, add it */
  207. if (!xs_is_null(state) && *state) {
  208. *body = xs_str_cat(*body, "&state=");
  209. *body = xs_str_cat(*body, state);
  210. }
  211. srv_log(xs_fmt("oauth x-snac-login: '%s' success, redirect to %s",
  212. login, *body));
  213. /* assign the login to the app */
  214. xs *app = app_get(cid);
  215. if (app != NULL) {
  216. app = xs_dict_set(app, "uid", login);
  217. app = xs_dict_set(app, "code", code);
  218. app_add(cid, app);
  219. }
  220. else
  221. srv_log(xs_fmt("oauth x-snac-login: error getting app %s", cid));
  222. }
  223. else
  224. srv_debug(1, xs_fmt("oauth x-snac-login: login '%s' incorrect", login));
  225. user_free(&snac);
  226. }
  227. else
  228. srv_debug(1, xs_fmt("oauth x-snac-login: bad user '%s'", login));
  229. }
  230. else
  231. srv_debug(1, xs_fmt("oauth x-snac-login: invalid or unset arguments"));
  232. }
  233. else
  234. if (strcmp(cmd, "/token") == 0) {
  235. xs *wrk = NULL;
  236. const char *gtype = xs_dict_get(args, "grant_type");
  237. const char *code = xs_dict_get(args, "code");
  238. const char *cid = xs_dict_get(args, "client_id");
  239. const char *csec = xs_dict_get(args, "client_secret");
  240. const char *ruri = xs_dict_get(args, "redirect_uri");
  241. /* FIXME: this 'scope' parameter is mandatory for the official Mastodon API,
  242. but if it's enabled, it makes it crash after some more steps, which
  243. is FAR WORSE */
  244. // const char *scope = xs_dict_get(args, "scope");
  245. const char *scope = NULL;
  246. /* no client_secret? check if it's inside an authorization header
  247. (AndStatus does it this way) */
  248. if (xs_is_null(csec)) {
  249. const char *auhdr = xs_dict_get(req, "authorization");
  250. if (!xs_is_null(auhdr) && xs_startswith(auhdr, "Basic ")) {
  251. xs *s1 = xs_replace(auhdr, "Basic ", "");
  252. int size;
  253. xs *s2 = xs_base64_dec(s1, &size);
  254. if (!xs_is_null(s2)) {
  255. xs *l1 = xs_split(s2, ":");
  256. if (xs_list_len(l1) == 2) {
  257. wrk = xs_dup(xs_list_get(l1, 1));
  258. csec = wrk;
  259. }
  260. }
  261. }
  262. }
  263. if (gtype && code && cid && csec && ruri) {
  264. xs *app = app_get(cid);
  265. if (app == NULL) {
  266. status = 401;
  267. srv_log(xs_fmt("oauth token: invalid app %s", cid));
  268. }
  269. else
  270. if (strcmp(csec, xs_dict_get(app, "client_secret")) != 0) {
  271. status = 401;
  272. srv_log(xs_fmt("oauth token: invalid client_secret for app %s", cid));
  273. }
  274. else {
  275. xs *rsp = xs_dict_new();
  276. xs *cat = xs_number_new(time(NULL));
  277. xs *tokid = random_str();
  278. rsp = xs_dict_append(rsp, "access_token", tokid);
  279. rsp = xs_dict_append(rsp, "token_type", "Bearer");
  280. rsp = xs_dict_append(rsp, "created_at", cat);
  281. if (!xs_is_null(scope))
  282. rsp = xs_dict_append(rsp, "scope", scope);
  283. *body = xs_json_dumps_pp(rsp, 4);
  284. *ctype = "application/json";
  285. status = 200;
  286. const char *uid = xs_dict_get(app, "uid");
  287. srv_debug(0, xs_fmt("oauth token: "
  288. "successful login for %s, new token %s", uid, tokid));
  289. xs *token = xs_dict_new();
  290. token = xs_dict_append(token, "token", tokid);
  291. token = xs_dict_append(token, "client_id", cid);
  292. token = xs_dict_append(token, "client_secret", csec);
  293. token = xs_dict_append(token, "uid", uid);
  294. token = xs_dict_append(token, "code", code);
  295. token_add(tokid, token);
  296. }
  297. }
  298. else {
  299. srv_debug(0, xs_fmt("oauth token: invalid or unset arguments"));
  300. status = 400;
  301. }
  302. }
  303. else
  304. if (strcmp(cmd, "/revoke") == 0) {
  305. const char *cid = xs_dict_get(args, "client_id");
  306. const char *csec = xs_dict_get(args, "client_secret");
  307. const char *tokid = xs_dict_get(args, "token");
  308. if (cid && csec && tokid) {
  309. xs *token = token_get(tokid);
  310. *body = xs_str_new("{}");
  311. *ctype = "application/json";
  312. if (token == NULL || strcmp(csec, xs_dict_get(token, "client_secret")) != 0) {
  313. srv_debug(1, xs_fmt("oauth revoke: bad secret for token %s", tokid));
  314. status = 403;
  315. }
  316. else {
  317. token_del(tokid);
  318. srv_debug(0, xs_fmt("oauth revoke: revoked token %s", tokid));
  319. status = 200;
  320. /* also delete the app, as it serves no purpose from now on */
  321. app_del(cid);
  322. }
  323. }
  324. else {
  325. srv_debug(0, xs_fmt("oauth revoke: invalid or unset arguments"));
  326. status = 403;
  327. }
  328. }
  329. return status;
  330. }
  331. xs_str *mastoapi_id(const xs_dict *msg)
  332. /* returns a somewhat Mastodon-compatible status id */
  333. {
  334. const char *id = xs_dict_get(msg, "id");
  335. xs *md5 = xs_md5_hex(id, strlen(id));
  336. return xs_fmt("%10.0f%s", object_ctime_by_md5(md5), md5);
  337. }
  338. #define MID_TO_MD5(id) (id + 10)
  339. xs_dict *mastoapi_account(const xs_dict *actor)
  340. /* converts an ActivityPub actor to a Mastodon account */
  341. {
  342. xs_dict *acct = xs_dict_new();
  343. const char *display_name = xs_dict_get(actor, "name");
  344. if (xs_is_null(display_name) || *display_name == '\0')
  345. display_name = xs_dict_get(actor, "preferredUsername");
  346. const char *id = xs_dict_get(actor, "id");
  347. const char *pub = xs_dict_get(actor, "published");
  348. xs *acct_md5 = xs_md5_hex(id, strlen(id));
  349. acct = xs_dict_append(acct, "id", acct_md5);
  350. acct = xs_dict_append(acct, "username", xs_dict_get(actor, "preferredUsername"));
  351. acct = xs_dict_append(acct, "acct", xs_dict_get(actor, "preferredUsername"));
  352. acct = xs_dict_append(acct, "display_name", display_name);
  353. if (pub)
  354. acct = xs_dict_append(acct, "created_at", pub);
  355. else {
  356. /* unset created_at crashes Tusky, so lie like a mf */
  357. xs *date = xs_str_utctime(0, "%Y-%m-%dT%H:%M:%SZ");
  358. acct = xs_dict_append(acct, "created_at", date);
  359. }
  360. acct = xs_dict_append(acct, "note", xs_dict_get(actor, "summary"));
  361. acct = xs_dict_append(acct, "url", id);
  362. xs *avatar = NULL;
  363. xs_dict *av = xs_dict_get(actor, "icon");
  364. if (xs_type(av) == XSTYPE_DICT)
  365. avatar = xs_dup(xs_dict_get(av, "url"));
  366. else
  367. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  368. acct = xs_dict_append(acct, "avatar", avatar);
  369. return acct;
  370. }
  371. xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
  372. /* converts an ActivityPub note to a Mastodon status */
  373. {
  374. xs *actor = NULL;
  375. actor_get(snac, xs_dict_get(msg, "attributedTo"), &actor);
  376. /* if the author is not here, discard */
  377. if (actor == NULL)
  378. return NULL;
  379. xs *acct = mastoapi_account(actor);
  380. /** shave the yak converting an ActivityPub Note to a Mastodon status **/
  381. xs *f = xs_val_new(XSTYPE_FALSE);
  382. xs *t = xs_val_new(XSTYPE_TRUE);
  383. xs *n = xs_val_new(XSTYPE_NULL);
  384. xs *el = xs_list_new();
  385. xs *idx = NULL;
  386. xs *ixc = NULL;
  387. char *tmp;
  388. char *id = xs_dict_get(msg, "id");
  389. xs *mid = mastoapi_id(msg);
  390. xs_dict *st = xs_dict_new();
  391. st = xs_dict_append(st, "id", mid);
  392. st = xs_dict_append(st, "uri", id);
  393. st = xs_dict_append(st, "url", id);
  394. st = xs_dict_append(st, "created_at", xs_dict_get(msg, "published"));
  395. st = xs_dict_append(st, "account", acct);
  396. st = xs_dict_append(st, "content", xs_dict_get(msg, "content"));
  397. st = xs_dict_append(st, "visibility",
  398. is_msg_public(snac, msg) ? "public" : "private");
  399. tmp = xs_dict_get(msg, "sensitive");
  400. if (xs_is_null(tmp))
  401. tmp = f;
  402. st = xs_dict_append(st, "sensitive", tmp);
  403. tmp = xs_dict_get(msg, "summary");
  404. if (xs_is_null(tmp))
  405. tmp = "";
  406. st = xs_dict_append(st, "spoiler_text", tmp);
  407. /* create the list of attachments */
  408. xs *matt = xs_list_new();
  409. xs_list *att = xs_dict_get(msg, "attachment");
  410. xs_str *aobj;
  411. while (xs_list_iter(&att, &aobj)) {
  412. const char *mtype = xs_dict_get(aobj, "mediaType");
  413. if (!xs_is_null(mtype) && xs_startswith(mtype, "image/")) {
  414. xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
  415. xs *matte = xs_dict_new();
  416. matte = xs_dict_append(matte, "id", matteid);
  417. matte = xs_dict_append(matte, "type", "image");
  418. matte = xs_dict_append(matte, "url", xs_dict_get(aobj, "url"));
  419. matte = xs_dict_append(matte, "preview_url", xs_dict_get(aobj, "url"));
  420. matte = xs_dict_append(matte, "remote_url", xs_dict_get(aobj, "url"));
  421. const char *name = xs_dict_get(aobj, "name");
  422. if (xs_is_null(name))
  423. name = "";
  424. matte = xs_dict_append(matte, "description", name);
  425. matt = xs_list_append(matt, matte);
  426. }
  427. }
  428. st = xs_dict_append(st, "media_attachments", matt);
  429. st = xs_dict_append(st, "mentions", el);
  430. st = xs_dict_append(st, "tags", el);
  431. st = xs_dict_append(st, "emojis", el);
  432. xs_free(idx);
  433. xs_free(ixc);
  434. idx = object_likes(id);
  435. ixc = xs_number_new(xs_list_len(idx));
  436. st = xs_dict_append(st, "favourites_count", ixc);
  437. st = xs_dict_append(st, "favourited",
  438. xs_list_in(idx, snac->md5) != -1 ? t : f);
  439. xs_free(idx);
  440. xs_free(ixc);
  441. idx = object_announces(id);
  442. ixc = xs_number_new(xs_list_len(idx));
  443. st = xs_dict_append(st, "reblogs_count", ixc);
  444. st = xs_dict_append(st, "reblogged",
  445. xs_list_in(idx, snac->md5) != -1 ? t : f);
  446. xs_free(idx);
  447. xs_free(ixc);
  448. idx = object_children(id);
  449. ixc = xs_number_new(xs_list_len(idx));
  450. st = xs_dict_append(st, "replies_count", ixc);
  451. /* default in_reply_to values */
  452. st = xs_dict_append(st, "in_reply_to_id", n);
  453. st = xs_dict_append(st, "in_reply_to_account_id", n);
  454. tmp = xs_dict_get(msg, "inReplyTo");
  455. if (!xs_is_null(tmp)) {
  456. xs *irto = NULL;
  457. if (valid_status(object_get(tmp, &irto))) {
  458. xs *irt_mid = mastoapi_id(irto);
  459. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  460. char *at = NULL;
  461. if (!xs_is_null(at = xs_dict_get(irto, "attributedTo"))) {
  462. xs *at_md5 = xs_md5_hex(at, strlen(at));
  463. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  464. }
  465. }
  466. }
  467. st = xs_dict_append(st, "reblog", n);
  468. st = xs_dict_append(st, "poll", n);
  469. st = xs_dict_append(st, "card", n);
  470. st = xs_dict_append(st, "language", n);
  471. tmp = xs_dict_get(msg, "sourceContent");
  472. if (xs_is_null(tmp))
  473. tmp = "";
  474. st = xs_dict_append(st, "text", tmp);
  475. tmp = xs_dict_get(msg, "updated");
  476. if (xs_is_null(tmp))
  477. tmp = n;
  478. st = xs_dict_append(st, "edited_at", tmp);
  479. return st;
  480. }
  481. int process_auth_token(snac *snac, const xs_dict *req)
  482. /* processes an authorization token, if there is one */
  483. {
  484. int logged_in = 0;
  485. char *v;
  486. /* if there is an authorization field, try to validate it */
  487. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  488. xs *tokid = xs_replace(v, "Bearer ", "");
  489. xs *token = token_get(tokid);
  490. if (token != NULL) {
  491. const char *uid = xs_dict_get(token, "uid");
  492. if (!xs_is_null(uid) && user_open(snac, uid)) {
  493. logged_in = 1;
  494. srv_debug(2, xs_fmt("mastoapi auth: valid token for user %s", uid));
  495. }
  496. else
  497. srv_log(xs_fmt("mastoapi auth: corrupted token %s", tokid));
  498. }
  499. else
  500. srv_log(xs_fmt("mastoapi auth: invalid token %s", tokid));
  501. }
  502. return logged_in;
  503. }
  504. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  505. char **body, int *b_size, char **ctype)
  506. {
  507. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  508. return 0;
  509. srv_debug(1, xs_fmt("mastoapi_get_handler %s", q_path));
  510. /* {
  511. xs *j = xs_json_dumps_pp(req, 4);
  512. printf("mastoapi get:\n%s\n", j);
  513. }*/
  514. int status = 404;
  515. xs_dict *args = xs_dict_get(req, "q_vars");
  516. xs *cmd = xs_replace(q_path, "/api", "");
  517. snac snac1 = {0};
  518. int logged_in = process_auth_token(&snac1, req);
  519. if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) {
  520. if (logged_in) {
  521. xs *acct = xs_dict_new();
  522. acct = xs_dict_append(acct, "id", xs_dict_get(snac1.config, "uid"));
  523. acct = xs_dict_append(acct, "username", xs_dict_get(snac1.config, "uid"));
  524. acct = xs_dict_append(acct, "acct", xs_dict_get(snac1.config, "uid"));
  525. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac1.config, "name"));
  526. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac1.config, "published"));
  527. acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio"));
  528. acct = xs_dict_append(acct, "url", snac1.actor);
  529. acct = xs_dict_append(acct, "header", "");
  530. xs *avatar = NULL;
  531. char *av = xs_dict_get(snac1.config, "avatar");
  532. if (xs_is_null(av) || *av == '\0')
  533. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  534. else
  535. avatar = xs_dup(av);
  536. acct = xs_dict_append(acct, "avatar", avatar);
  537. *body = xs_json_dumps_pp(acct, 4);
  538. *ctype = "application/json";
  539. status = 200;
  540. }
  541. else {
  542. status = 422; // "Unprocessable entity" (no login)
  543. }
  544. }
  545. else
  546. if (strcmp(cmd, "/v1/accounts/relationships") == 0) {
  547. /* find if an account is followed, blocked, etc. */
  548. /* the account to get relationships about is in args "id[]" */
  549. /* dummy by now */
  550. if (logged_in) {
  551. *body = xs_dup("[]");
  552. *ctype = "application/json";
  553. status = 200;
  554. }
  555. }
  556. else
  557. if (xs_startswith(cmd, "/v1/accounts/")) {
  558. /* account-related information */
  559. xs *l = xs_split(cmd, "/");
  560. const char *uid = xs_list_get(l, 3);
  561. const char *opt = xs_list_get(l, 4);
  562. if (uid != NULL) {
  563. snac snac2;
  564. xs *out = NULL;
  565. xs *actor = NULL;
  566. /* is it a local user? */
  567. if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
  568. if (opt == NULL) {
  569. /* account information */
  570. actor = msg_actor(&snac2);
  571. out = mastoapi_account(actor);
  572. }
  573. else
  574. if (strcmp(opt, "statuses") == 0) {
  575. /* the public list of posts of a user */
  576. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  577. xs_list *p = timeline;
  578. xs_str *v;
  579. out = xs_list_new();
  580. while (xs_list_iter(&p, &v)) {
  581. xs *msg = NULL;
  582. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  583. /* add only posts by the author */
  584. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  585. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  586. xs *st = mastoapi_status(&snac2, msg);
  587. out = xs_list_append(out, st);
  588. }
  589. }
  590. }
  591. }
  592. user_free(&snac2);
  593. }
  594. else {
  595. /* try the uid as the md5 of a possibly loaded actor */
  596. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  597. if (opt == NULL) {
  598. /* account information */
  599. out = mastoapi_account(actor);
  600. }
  601. else
  602. if (strcmp(opt, "statuses") == 0) {
  603. /* we don't serve statuses of others; return the empty list */
  604. out = xs_list_new();
  605. }
  606. }
  607. }
  608. if (out != NULL) {
  609. *body = xs_json_dumps_pp(out, 4);
  610. *ctype = "application/json";
  611. status = 200;
  612. }
  613. }
  614. }
  615. else
  616. if (strcmp(cmd, "/v1/timelines/home") == 0) {
  617. /* the private timeline */
  618. if (logged_in) {
  619. const char *max_id = xs_dict_get(args, "max_id");
  620. const char *since_id = xs_dict_get(args, "since_id");
  621. const char *min_id = xs_dict_get(args, "min_id");
  622. const char *limit_s = xs_dict_get(args, "limit");
  623. int limit = 0;
  624. int cnt = 0;
  625. if (!xs_is_null(limit_s))
  626. limit = atoi(limit_s);
  627. if (limit == 0)
  628. limit = 20;
  629. xs *timeline = timeline_simple_list(&snac1, "private", 0, XS_ALL);
  630. xs *out = xs_list_new();
  631. xs_list *p = timeline;
  632. xs_str *v;
  633. while (xs_list_iter(&p, &v) && cnt < limit) {
  634. xs *msg = NULL;
  635. /* only return entries older that max_id */
  636. if (max_id) {
  637. if (strcmp(v, max_id) == 0)
  638. max_id = NULL;
  639. continue;
  640. }
  641. /* only returns entries newer than since_id */
  642. if (since_id) {
  643. if (strcmp(v, since_id) == 0)
  644. break;
  645. }
  646. /* only returns entries newer than min_id */
  647. /* what does really "Return results immediately newer than ID" mean? */
  648. if (min_id) {
  649. if (strcmp(v, min_id) == 0)
  650. break;
  651. }
  652. /* get the entry */
  653. if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
  654. continue;
  655. /* discard non-Notes */
  656. if (strcmp(xs_dict_get(msg, "type"), "Note") != 0)
  657. continue;
  658. /* convert the Note into a Mastodon status */
  659. xs *st = mastoapi_status(&snac1, msg);
  660. if (st != NULL)
  661. out = xs_list_append(out, st);
  662. cnt++;
  663. }
  664. *body = xs_json_dumps_pp(out, 4);
  665. *ctype = "application/json";
  666. status = 200;
  667. {
  668. xs *j = xs_json_loads(*body);
  669. if (j == NULL) {
  670. srv_log(xs_fmt("mastoapi timeline: bad JSON"));
  671. srv_archive_error("mastoapi_timeline", "bad JSON", req, *body);
  672. }
  673. }
  674. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  675. }
  676. else {
  677. status = 401; // unauthorized
  678. }
  679. }
  680. else
  681. if (strcmp(cmd, "/v1/timelines/public") == 0) {
  682. /* the public timeline (public timelines for all users) */
  683. /* TBD */
  684. *body = xs_dup("[]");
  685. *ctype = "application/json";
  686. status = 200;
  687. }
  688. else
  689. if (strcmp(cmd, "/v1/conversations") == 0) {
  690. /* TBD */
  691. *body = xs_dup("[]");
  692. *ctype = "application/json";
  693. status = 200;
  694. }
  695. else
  696. if (strcmp(cmd, "/v1/notifications") == 0) {
  697. if (logged_in) {
  698. xs *l = notify_list(&snac1, 0);
  699. xs *out = xs_list_new();
  700. xs_list *p = l;
  701. xs_dict *v;
  702. while (xs_list_iter(&p, &v)) {
  703. xs *noti = notify_get(&snac1, v);
  704. if (noti == NULL)
  705. continue;
  706. const char *type = xs_dict_get(noti, "type");
  707. const char *objid = xs_dict_get(noti, "objid");
  708. xs *actor = NULL;
  709. xs *entry = NULL;
  710. if (!valid_status(object_get(xs_dict_get(noti, "actor"), &actor)))
  711. continue;
  712. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  713. continue;
  714. if (is_hidden(&snac1, objid))
  715. continue;
  716. /* convert the type */
  717. if (strcmp(type, "Like") == 0)
  718. type = "favourite";
  719. else
  720. if (strcmp(type, "Announce") == 0)
  721. type = "reblog";
  722. else
  723. if (strcmp(type, "Follow") == 0)
  724. type = "follow";
  725. else
  726. if (strcmp(type, "Create") == 0)
  727. type = "mention";
  728. else
  729. continue;
  730. xs *mn = xs_dict_new();
  731. mn = xs_dict_append(mn, "type", type);
  732. xs *id = xs_replace(xs_dict_get(noti, "id"), ".", "");
  733. mn = xs_dict_append(mn, "id", id);
  734. mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
  735. xs *acct = mastoapi_account(actor);
  736. mn = xs_dict_append(mn, "account", acct);
  737. if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
  738. xs *st = mastoapi_status(&snac1, entry);
  739. mn = xs_dict_append(mn, "status", st);
  740. }
  741. out = xs_list_append(out, mn);
  742. }
  743. *body = xs_json_dumps_pp(out, 4);
  744. *ctype = "application/json";
  745. status = 200;
  746. }
  747. else
  748. status = 401;
  749. }
  750. else
  751. if (strcmp(cmd, "/v1/filters") == 0) {
  752. /* snac will never have filters */
  753. *body = xs_dup("[]");
  754. *ctype = "application/json";
  755. status = 200;
  756. }
  757. else
  758. if (strcmp(cmd, "/v1/favourites") == 0) {
  759. /* snac will never support a list of favourites */
  760. *body = xs_dup("[]");
  761. *ctype = "application/json";
  762. status = 200;
  763. }
  764. else
  765. if (strcmp(cmd, "/v1/bookmarks") == 0) {
  766. /* snac does not support bookmarks */
  767. *body = xs_dup("[]");
  768. *ctype = "application/json";
  769. status = 200;
  770. }
  771. else
  772. if (strcmp(cmd, "/v1/lists") == 0) {
  773. /* snac does not support lists */
  774. *body = xs_dup("[]");
  775. *ctype = "application/json";
  776. status = 200;
  777. }
  778. else
  779. if (strcmp(cmd, "/v1/scheduled_statuses") == 0) {
  780. /* snac does not scheduled notes */
  781. *body = xs_dup("[]");
  782. *ctype = "application/json";
  783. status = 200;
  784. }
  785. else
  786. if (strcmp(cmd, "/v1/follow_requests") == 0) {
  787. /* snac does not support optional follow confirmations */
  788. *body = xs_dup("[]");
  789. *ctype = "application/json";
  790. status = 200;
  791. }
  792. else
  793. if (strcmp(cmd, "/v1/announcements") == 0) {
  794. /* snac has no announcements (yet?) */
  795. *body = xs_dup("[]");
  796. *ctype = "application/json";
  797. status = 200;
  798. }
  799. else
  800. if (strcmp(cmd, "/v1/custom_emojis") == 0) {
  801. /* are you kidding me? */
  802. *body = xs_dup("[]");
  803. *ctype = "application/json";
  804. status = 200;
  805. }
  806. else
  807. if (strcmp(cmd, "/v1/instance") == 0) {
  808. /* returns an instance object */
  809. xs *ins = xs_dict_new();
  810. const char *host = xs_dict_get(srv_config, "host");
  811. ins = xs_dict_append(ins, "uri", host);
  812. ins = xs_dict_append(ins, "domain", host);
  813. ins = xs_dict_append(ins, "title", host);
  814. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  815. ins = xs_dict_append(ins, "source_url", "https:/" "/comam.es/what-is-snac");
  816. ins = xs_dict_append(ins, "description", host);
  817. ins = xs_dict_append(ins, "short_description", host);
  818. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  819. ins = xs_dict_append(ins, "thumbnail", susie);
  820. const char *v = xs_dict_get(srv_config, "admin_email");
  821. if (xs_is_null(v) || *v == '\0')
  822. v = "admin@localhost";
  823. ins = xs_dict_append(ins, "email", v);
  824. xs *l1 = xs_list_new();
  825. ins = xs_dict_append(ins, "rules", l1);
  826. l1 = xs_list_append(l1, "en");
  827. ins = xs_dict_append(ins, "languages", l1);
  828. xs *d1 = xs_dict_new();
  829. ins = xs_dict_append(ins, "urls", d1);
  830. ins = xs_dict_append(ins, "configuration", d1);
  831. xs *z = xs_number_new(0);
  832. d1 = xs_dict_append(d1, "user_count", z);
  833. d1 = xs_dict_append(d1, "status_count", z);
  834. d1 = xs_dict_append(d1, "domain_count", z);
  835. ins = xs_dict_append(ins, "stats", d1);
  836. xs *f = xs_val_new(XSTYPE_FALSE);
  837. ins = xs_dict_append(ins, "registrations", f);
  838. ins = xs_dict_append(ins, "approval_required", f);
  839. ins = xs_dict_append(ins, "invites_enabled", f);
  840. /* {
  841. snac snac;
  842. user_open(&snac, "test1");
  843. xs *actor = msg_actor(&snac);
  844. xs *acc = mastoapi_account(actor);
  845. ins = xs_dict_append(ins, "contact_account", acc);
  846. user_free(&snac);
  847. }*/
  848. *body = xs_json_dumps_pp(ins, 4);
  849. *ctype = "application/json";
  850. status = 200;
  851. }
  852. else
  853. if (xs_startswith(cmd, "/v1/statuses/")) {
  854. /* operations on a status */
  855. xs *l = xs_split(cmd, "/");
  856. const char *id = xs_list_get(l, 3);
  857. const char *op = xs_list_get(l, 4);
  858. if (!xs_is_null(id)) {
  859. xs *msg = NULL;
  860. xs *out = NULL;
  861. /* skip the 'fake' part of the id */
  862. id = MID_TO_MD5(id);
  863. if (valid_status(timeline_get_by_md5(&snac1, id, &msg))) {
  864. if (op == NULL) {
  865. /* return the status itself */
  866. out = mastoapi_status(&snac1, msg);
  867. }
  868. else
  869. if (strcmp(op, "context") == 0) {
  870. /* return ancestors and children */
  871. xs *anc = xs_list_new();
  872. xs *des = xs_list_new();
  873. xs_list *p;
  874. xs_str *v;
  875. char pid[64];
  876. /* build the [grand]parent list, moving up */
  877. strncpy(pid, id, sizeof(pid));
  878. while (object_parent(pid, pid, sizeof(pid))) {
  879. xs *m2 = NULL;
  880. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  881. xs *st = mastoapi_status(&snac1, m2);
  882. anc = xs_list_insert(anc, 0, st);
  883. }
  884. else
  885. break;
  886. }
  887. /* build the children list */
  888. xs *children = object_children(xs_dict_get(msg, "id"));
  889. p = children;
  890. while (xs_list_iter(&p, &v)) {
  891. xs *m2 = NULL;
  892. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  893. xs *st = mastoapi_status(&snac1, m2);
  894. des = xs_list_append(des, st);
  895. }
  896. }
  897. out = xs_dict_new();
  898. out = xs_dict_append(out, "ancestors", anc);
  899. out = xs_dict_append(out, "descendants", des);
  900. }
  901. else
  902. if (strcmp(op, "reblogged_by") == 0 ||
  903. strcmp(op, "favourited_by") == 0) {
  904. /* return the list of people who liked or boosted this */
  905. out = xs_list_new();
  906. xs *l = NULL;
  907. if (op[0] == 'r')
  908. l = object_announces(xs_dict_get(msg, "id"));
  909. else
  910. l = object_likes(xs_dict_get(msg, "id"));
  911. xs_list *p = l;
  912. xs_str *v;
  913. while (xs_list_iter(&p, &v)) {
  914. xs *actor2 = NULL;
  915. if (valid_status(object_get_by_md5(v, &actor2))) {
  916. xs *acct2 = mastoapi_account(actor2);
  917. out = xs_list_append(out, acct2);
  918. }
  919. }
  920. }
  921. }
  922. else
  923. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  924. if (out != NULL) {
  925. *body = xs_json_dumps_pp(out, 4);
  926. *ctype = "application/json";
  927. status = 200;
  928. }
  929. }
  930. }
  931. else
  932. if (strcmp(cmd, "/v1/filters") == 0) {
  933. *body = xs_dup("[]");
  934. *ctype = "application/json";
  935. status = 200;
  936. }
  937. else
  938. if (strcmp(cmd, "/v1/preferences") == 0) {
  939. *body = xs_dup("{}");
  940. *ctype = "application/json";
  941. status = 200;
  942. }
  943. else
  944. if (strcmp(cmd, "/v1/markers") == 0) {
  945. *body = xs_dup("{}");
  946. *ctype = "application/json";
  947. status = 200;
  948. }
  949. else
  950. if (strcmp(cmd, "/v1/followed_tags") == 0) {
  951. *body = xs_dup("[]");
  952. *ctype = "application/json";
  953. status = 200;
  954. }
  955. /* user cleanup */
  956. if (logged_in)
  957. user_free(&snac1);
  958. return status;
  959. }
  960. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  961. const char *payload, int p_size,
  962. char **body, int *b_size, char **ctype)
  963. {
  964. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  965. return 0;
  966. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  967. /* {
  968. xs *j = xs_json_dumps_pp(req, 4);
  969. printf("mastoapi post:\n%s\n", j);
  970. }*/
  971. int status = 404;
  972. xs *args = NULL;
  973. char *i_ctype = xs_dict_get(req, "content-type");
  974. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  975. args = xs_json_loads(payload);
  976. else
  977. args = xs_dup(xs_dict_get(req, "p_vars"));
  978. if (args == NULL)
  979. return 400;
  980. /* {
  981. xs *j = xs_json_dumps_pp(args, 4);
  982. printf("%s\n", j);
  983. }*/
  984. xs *cmd = xs_replace(q_path, "/api", "");
  985. snac snac = {0};
  986. int logged_in = process_auth_token(&snac, req);
  987. if (strcmp(cmd, "/v1/apps") == 0) {
  988. const char *name = xs_dict_get(args, "client_name");
  989. const char *ruri = xs_dict_get(args, "redirect_uris");
  990. const char *scope = xs_dict_get(args, "scope");
  991. if (xs_type(ruri) == XSTYPE_LIST)
  992. ruri = xs_dict_get(ruri, 0);
  993. if (name && ruri) {
  994. xs *app = xs_dict_new();
  995. xs *id = xs_replace_i(tid(0), ".", "");
  996. xs *cid = random_str();
  997. xs *csec = random_str();
  998. xs *vkey = random_str();
  999. app = xs_dict_append(app, "name", name);
  1000. app = xs_dict_append(app, "redirect_uri", ruri);
  1001. app = xs_dict_append(app, "client_id", cid);
  1002. app = xs_dict_append(app, "client_secret", csec);
  1003. app = xs_dict_append(app, "vapid_key", vkey);
  1004. app = xs_dict_append(app, "id", id);
  1005. *body = xs_json_dumps_pp(app, 4);
  1006. *ctype = "application/json";
  1007. status = 200;
  1008. app = xs_dict_append(app, "code", "");
  1009. if (scope)
  1010. app = xs_dict_append(app, "scope", scope);
  1011. app_add(cid, app);
  1012. srv_debug(0, xs_fmt("mastoapi apps: new app %s", cid));
  1013. }
  1014. }
  1015. else
  1016. if (strcmp(cmd, "/v1/statuses") == 0) {
  1017. if (logged_in) {
  1018. /* post a new Note */
  1019. /* {
  1020. xs *j = xs_json_dumps_pp(args, 4);
  1021. printf("%s\n", j);
  1022. }*/
  1023. const char *content = xs_dict_get(args, "status");
  1024. const char *mid = xs_dict_get(args, "in_reply_to_id");
  1025. const char *visibility = xs_dict_get(args, "visibility");
  1026. const char *summary = xs_dict_get(args, "spoiler_text");
  1027. const char *media_ids = xs_dict_get(args, "media_ids");
  1028. if (xs_is_null(media_ids))
  1029. media_ids = xs_dict_get(args, "media_ids[]");
  1030. xs *attach_list = xs_list_new();
  1031. xs *irt = NULL;
  1032. /* is it a reply? */
  1033. if (mid != NULL) {
  1034. xs *r_msg = NULL;
  1035. const char *md5 = MID_TO_MD5(mid);
  1036. if (valid_status(object_get_by_md5(md5, &r_msg)))
  1037. irt = xs_dup(xs_dict_get(r_msg, "id"));
  1038. }
  1039. /* does it have attachments? */
  1040. if (!xs_is_null(media_ids)) {
  1041. xs *mi = NULL;
  1042. if (xs_type(media_ids) == XSTYPE_LIST)
  1043. mi = xs_dup(media_ids);
  1044. else {
  1045. mi = xs_list_new();
  1046. mi = xs_list_append(mi, media_ids);
  1047. }
  1048. xs_list *p = mi;
  1049. xs_str *v;
  1050. while (xs_list_iter(&p, &v)) {
  1051. xs *l = xs_list_new();
  1052. xs *url = xs_fmt("%s/s/%s", snac.actor, v);
  1053. xs *desc = static_get_meta(&snac, v);
  1054. l = xs_list_append(l, url);
  1055. l = xs_list_append(l, desc);
  1056. attach_list = xs_list_append(attach_list, l);
  1057. }
  1058. }
  1059. /* prepare the message */
  1060. xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
  1061. strcmp(visibility, "public") == 0 ? 0 : 1);
  1062. if (!xs_is_null(summary) && *summary) {
  1063. xs *t = xs_val_new(XSTYPE_TRUE);
  1064. msg = xs_dict_set(msg, "sensitive", t);
  1065. msg = xs_dict_set(msg, "summary", summary);
  1066. }
  1067. /* store */
  1068. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1069. /* 'Create' message */
  1070. xs *c_msg = msg_create(&snac, msg);
  1071. enqueue_message(&snac, c_msg);
  1072. timeline_touch(&snac);
  1073. /* convert to a mastodon status as a response code */
  1074. xs *st = mastoapi_status(&snac, msg);
  1075. *body = xs_json_dumps_pp(st, 4);
  1076. *ctype = "application/json";
  1077. status = 200;
  1078. }
  1079. else
  1080. status = 401;
  1081. }
  1082. else
  1083. if (xs_startswith(cmd, "/v1/statuses")) {
  1084. if (logged_in) {
  1085. /* operations on a status */
  1086. xs *l = xs_split(cmd, "/");
  1087. const char *mid = xs_list_get(l, 3);
  1088. const char *op = xs_list_get(l, 4);
  1089. if (!xs_is_null(mid)) {
  1090. xs *msg = NULL;
  1091. xs *out = NULL;
  1092. /* skip the 'fake' part of the id */
  1093. mid = MID_TO_MD5(mid);
  1094. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1095. char *id = xs_dict_get(msg, "id");
  1096. if (op == NULL) {
  1097. /* no operation (?) */
  1098. }
  1099. else
  1100. if (strcmp(op, "favourite") == 0) {
  1101. xs *n_msg = msg_admiration(&snac, id, "Like");
  1102. if (n_msg != NULL) {
  1103. enqueue_message(&snac, n_msg);
  1104. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  1105. out = mastoapi_status(&snac, msg);
  1106. }
  1107. }
  1108. else
  1109. if (strcmp(op, "unfavourite") == 0) {
  1110. /* snac does not support Undo+Like */
  1111. }
  1112. else
  1113. if (strcmp(op, "reblog") == 0) {
  1114. xs *n_msg = msg_admiration(&snac, id, "Announce");
  1115. if (n_msg != NULL) {
  1116. enqueue_message(&snac, n_msg);
  1117. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  1118. out = mastoapi_status(&snac, msg);
  1119. }
  1120. }
  1121. else
  1122. if (strcmp(op, "unreblog") == 0) {
  1123. /* snac does not support Undo+Announce */
  1124. }
  1125. else
  1126. if (strcmp(op, "bookmark") == 0) {
  1127. /* snac does not support bookmarks */
  1128. }
  1129. else
  1130. if (strcmp(op, "unbookmark") == 0) {
  1131. /* snac does not support bookmarks */
  1132. }
  1133. else
  1134. if (strcmp(op, "pin") == 0) {
  1135. /* snac does not support pinning */
  1136. }
  1137. else
  1138. if (strcmp(op, "unpin") == 0) {
  1139. /* snac does not support pinning */
  1140. }
  1141. else
  1142. if (strcmp(op, "mute") == 0) {
  1143. /* Mastodon's mute is snac's hide */
  1144. }
  1145. else
  1146. if (strcmp(op, "unmute") == 0) {
  1147. /* Mastodon's unmute is snac's unhide */
  1148. }
  1149. }
  1150. if (out != NULL) {
  1151. *body = xs_json_dumps_pp(out, 4);
  1152. *ctype = "application/json";
  1153. status = 200;
  1154. }
  1155. }
  1156. }
  1157. else
  1158. status = 401;
  1159. }
  1160. else
  1161. if (strcmp(cmd, "/v1/notifications/clear") == 0) {
  1162. if (logged_in) {
  1163. notify_clear(&snac);
  1164. timeline_touch(&snac);
  1165. *body = xs_dup("{}");
  1166. *ctype = "application/json";
  1167. status = 200;
  1168. }
  1169. else
  1170. status = 401;
  1171. }
  1172. else
  1173. if (strcmp(cmd, "/v1/push/subscription") == 0) {
  1174. /* I don't know what I'm doing */
  1175. if (logged_in) {
  1176. char *v;
  1177. xs *wpush = xs_dict_new();
  1178. wpush = xs_dict_append(wpush, "id", "1");
  1179. v = xs_dict_get(args, "data");
  1180. v = xs_dict_get(v, "alerts");
  1181. wpush = xs_dict_append(wpush, "alerts", v);
  1182. v = xs_dict_get(args, "subscription");
  1183. v = xs_dict_get(v, "endpoint");
  1184. wpush = xs_dict_append(wpush, "endpoint", v);
  1185. xs *server_key = random_str();
  1186. wpush = xs_dict_append(wpush, "server_key", server_key);
  1187. *body = xs_json_dumps_pp(wpush, 4);
  1188. *ctype = "application/json";
  1189. status = 200;
  1190. }
  1191. else
  1192. status = 401;
  1193. }
  1194. else
  1195. if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) {
  1196. if (logged_in) {
  1197. /* {
  1198. xs *j = xs_json_dumps_pp(args, 4);
  1199. printf("%s\n", j);
  1200. }*/
  1201. const xs_list *file = xs_dict_get(args, "file");
  1202. const char *desc = xs_dict_get(args, "description");
  1203. if (xs_is_null(desc))
  1204. desc = "";
  1205. status = 400;
  1206. if (xs_type(file) == XSTYPE_LIST) {
  1207. const char *fn = xs_list_get(file, 0);
  1208. if (*fn != '\0') {
  1209. char *ext = strrchr(fn, '.');
  1210. xs *hash = xs_md5_hex(fn, strlen(fn));
  1211. xs *id = xs_fmt("%s%s", hash, ext);
  1212. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1213. int fo = xs_number_get(xs_list_get(file, 1));
  1214. int fs = xs_number_get(xs_list_get(file, 2));
  1215. /* store */
  1216. static_put(&snac, id, payload + fo, fs);
  1217. static_put_meta(&snac, id, desc);
  1218. /* prepare a response */
  1219. xs *rsp = xs_dict_new();
  1220. rsp = xs_dict_append(rsp, "id", id);
  1221. rsp = xs_dict_append(rsp, "type", "image");
  1222. rsp = xs_dict_append(rsp, "url", url);
  1223. rsp = xs_dict_append(rsp, "preview_url", url);
  1224. rsp = xs_dict_append(rsp, "remote_url", url);
  1225. rsp = xs_dict_append(rsp, "description", desc);
  1226. *body = xs_json_dumps_pp(rsp, 4);
  1227. *ctype = "application/json";
  1228. status = 200;
  1229. }
  1230. }
  1231. }
  1232. else
  1233. status = 401;
  1234. }
  1235. /* user cleanup */
  1236. if (logged_in)
  1237. user_free(&snac);
  1238. return status;
  1239. }
  1240. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  1241. const char *payload, int p_size,
  1242. char **body, int *b_size, char **ctype)
  1243. {
  1244. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1245. return 0;
  1246. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1247. /* {
  1248. xs *j = xs_json_dumps_pp(req, 4);
  1249. printf("mastoapi put:\n%s\n", j);
  1250. }*/
  1251. int status = 404;
  1252. xs *args = NULL;
  1253. char *i_ctype = xs_dict_get(req, "content-type");
  1254. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1255. args = xs_json_loads(payload);
  1256. else
  1257. args = xs_dup(xs_dict_get(req, "p_vars"));
  1258. if (args == NULL)
  1259. return 400;
  1260. xs *cmd = xs_replace(q_path, "/api", "");
  1261. snac snac = {0};
  1262. int logged_in = process_auth_token(&snac, req);
  1263. if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) {
  1264. if (logged_in) {
  1265. xs *l = xs_split(cmd, "/");
  1266. const char *stid = xs_list_get(l, 3);
  1267. if (!xs_is_null(stid)) {
  1268. const char *desc = xs_dict_get(args, "description");
  1269. /* set the image metadata */
  1270. static_put_meta(&snac, stid, desc);
  1271. /* prepare a response */
  1272. xs *rsp = xs_dict_new();
  1273. xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
  1274. rsp = xs_dict_append(rsp, "id", stid);
  1275. rsp = xs_dict_append(rsp, "type", "image");
  1276. rsp = xs_dict_append(rsp, "url", url);
  1277. rsp = xs_dict_append(rsp, "preview_url", url);
  1278. rsp = xs_dict_append(rsp, "remote_url", url);
  1279. rsp = xs_dict_append(rsp, "description", desc);
  1280. *body = xs_json_dumps_pp(rsp, 4);
  1281. *ctype = "application/json";
  1282. status = 200;
  1283. }
  1284. }
  1285. else
  1286. status = 401;
  1287. }
  1288. /* user cleanup */
  1289. if (logged_in)
  1290. user_free(&snac);
  1291. return status;
  1292. }
  1293. #endif /* #ifndef NO_MASTODON_API */