mastoapi.c 124 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2025 grunfink et al. / MIT license */
  3. #ifndef NO_MASTODON_API
  4. #include "xs.h"
  5. #include "xs_hex.h"
  6. #include "xs_openssl.h"
  7. #include "xs_json.h"
  8. #include "xs_io.h"
  9. #include "xs_time.h"
  10. #include "xs_glob.h"
  11. #include "xs_set.h"
  12. #include "xs_random.h"
  13. #include "xs_url.h"
  14. #include "xs_mime.h"
  15. #include "xs_match.h"
  16. #include "xs_unicode.h"
  17. #include "snac.h"
  18. #include <sys/time.h>
  19. static xs_str *random_str(void)
  20. /* just what is says in the tin */
  21. {
  22. unsigned int data[4] = {0};
  23. xs_rnd_buf(data, sizeof(data));
  24. return xs_hex_enc((char *)data, sizeof(data));
  25. }
  26. int app_add(const char *id, const xs_dict *app)
  27. /* stores an app */
  28. {
  29. if (!xs_is_hex(id))
  30. return HTTP_STATUS_INTERNAL_SERVER_ERROR;
  31. int status = HTTP_STATUS_CREATED;
  32. xs *fn = xs_fmt("%s/app/", srv_basedir);
  33. FILE *f;
  34. mkdirx(fn);
  35. fn = xs_str_cat(fn, id);
  36. fn = xs_str_cat(fn, ".json");
  37. if ((f = fopen(fn, "w")) != NULL) {
  38. xs_json_dump(app, 4, f);
  39. fclose(f);
  40. }
  41. else
  42. status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
  43. return status;
  44. }
  45. xs_str *_app_fn(const char *id)
  46. {
  47. return xs_fmt("%s/app/%s.json", srv_basedir, id);
  48. }
  49. xs_dict *app_get(const char *id)
  50. /* gets an app */
  51. {
  52. if (!xs_is_hex(id))
  53. return NULL;
  54. xs *fn = _app_fn(id);
  55. xs_dict *app = NULL;
  56. FILE *f;
  57. if ((f = fopen(fn, "r")) != NULL) {
  58. app = xs_json_load(f);
  59. fclose(f);
  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 = _app_fn(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 HTTP_STATUS_INTERNAL_SERVER_ERROR;
  76. int status = HTTP_STATUS_CREATED;
  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_json_dump(token, 4, f);
  84. fclose(f);
  85. }
  86. else
  87. status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
  88. return status;
  89. }
  90. xs_dict *token_get(const char *id)
  91. /* gets a token */
  92. {
  93. if (!xs_is_hex(id))
  94. return NULL;
  95. xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
  96. xs_dict *token = NULL;
  97. FILE *f;
  98. if ((f = fopen(fn, "r")) != NULL) {
  99. token = xs_json_load(f);
  100. fclose(f);
  101. /* 'touch' the file */
  102. utimes(fn, NULL);
  103. /* also 'touch' the app */
  104. const char *app_id = xs_dict_get(token, "client_id");
  105. if (app_id) {
  106. xs *afn = xs_fmt("%s/app/%s.json", srv_basedir, app_id);
  107. utimes(afn, NULL);
  108. }
  109. }
  110. return token;
  111. }
  112. int token_del(const char *id)
  113. /* deletes a token */
  114. {
  115. if (!xs_is_hex(id))
  116. return -1;
  117. xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
  118. return unlink(fn);
  119. }
  120. const char *login_page = ""
  121. "<!DOCTYPE html>\n"
  122. "<html>\n"
  123. "<head>\n"
  124. "<title>%s OAuth - Snac2</title>\n"
  125. "<meta content=\"width=device-width, initial-scale=1, minimum-scale=1, user-scalable=no\" name=\"viewport\">"
  126. "<style>:root {color-scheme: light dark}</style>\n"
  127. "</head>\n"
  128. "<body><h1>%s OAuth identify</h1>\n"
  129. "<div style=\"background-color: red; color: white\">%s</div>\n"
  130. "<form method=\"post\" action=\"%s:/" "/%s/%s\">\n"
  131. "<p>Login: <input type=\"text\" name=\"login\" autocapitalize=\"off\"></p>\n"
  132. "<p>Password: <input type=\"password\" name=\"passwd\"></p>\n"
  133. "<input type=\"hidden\" name=\"redir\" value=\"%s\">\n"
  134. "<input type=\"hidden\" name=\"cid\" value=\"%s\">\n"
  135. "<input type=\"hidden\" name=\"state\" value=\"%s\">\n"
  136. "<input type=\"submit\" value=\"OK\">\n"
  137. "</form><p>%s</p></body></html>\n"
  138. "";
  139. int oauth_get_handler(const xs_dict *req, const char *q_path,
  140. char **body, int *b_size, char **ctype)
  141. {
  142. (void)b_size;
  143. if (!xs_startswith(q_path, "/oauth/"))
  144. return 0;
  145. int status = HTTP_STATUS_NOT_FOUND;
  146. const xs_dict *msg = xs_dict_get(req, "q_vars");
  147. xs *cmd = xs_replace_n(q_path, "/oauth", "", 1);
  148. srv_debug(1, xs_fmt("oauth_get_handler %s", q_path));
  149. if (strcmp(cmd, "/authorize") == 0) { /** **/
  150. const char *cid = xs_dict_get(msg, "client_id");
  151. const char *ruri = xs_dict_get(msg, "redirect_uri");
  152. const char *rtype = xs_dict_get(msg, "response_type");
  153. const char *state = xs_dict_get(msg, "state");
  154. status = HTTP_STATUS_BAD_REQUEST;
  155. if (cid && ruri && rtype && strcmp(rtype, "code") == 0) {
  156. xs *app = app_get(cid);
  157. if (app != NULL) {
  158. const char *host = xs_dict_get(srv_config, "host");
  159. const char *proto = xs_dict_get_def(srv_config, "protocol", "https");
  160. if (xs_is_null(state))
  161. state = "";
  162. *body = xs_fmt(login_page, host, host, "", proto, host, "oauth/x-snac-login",
  163. ruri, cid, state, USER_AGENT);
  164. *ctype = "text/html";
  165. status = HTTP_STATUS_OK;
  166. srv_debug(1, xs_fmt("oauth authorize: generating login page"));
  167. }
  168. else
  169. srv_debug(1, xs_fmt("oauth authorize: bad client_id %s", cid));
  170. }
  171. else
  172. srv_debug(1, xs_fmt("oauth authorize: invalid or unset arguments"));
  173. }
  174. else
  175. if (strcmp(cmd, "/x-snac-get-token") == 0) { /** **/
  176. const char *host = xs_dict_get(srv_config, "host");
  177. const char *proto = xs_dict_get_def(srv_config, "protocol", "https");
  178. *body = xs_fmt(login_page, host, host, "", proto, host, "oauth/x-snac-get-token",
  179. "", "", "", USER_AGENT);
  180. *ctype = "text/html";
  181. status = HTTP_STATUS_OK;
  182. }
  183. return status;
  184. }
  185. int oauth_post_handler(const xs_dict *req, const char *q_path,
  186. const char *payload, int p_size,
  187. char **body, int *b_size, char **ctype)
  188. {
  189. (void)p_size;
  190. (void)b_size;
  191. if (!xs_startswith(q_path, "/oauth/"))
  192. return 0;
  193. int status = HTTP_STATUS_NOT_FOUND;
  194. const char *i_ctype = xs_dict_get(req, "content-type");
  195. xs *args = NULL;
  196. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  197. if (!xs_is_null(payload))
  198. args = xs_json_loads(payload);
  199. }
  200. else
  201. if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded") && payload) {
  202. args = xs_url_vars(payload);
  203. }
  204. else
  205. args = xs_dup(xs_dict_get(req, "p_vars"));
  206. if (args == NULL)
  207. return HTTP_STATUS_BAD_REQUEST;
  208. xs *cmd = xs_replace_n(q_path, "/oauth", "", 1);
  209. srv_debug(1, xs_fmt("oauth_post_handler %s", q_path));
  210. if (strcmp(cmd, "/x-snac-login") == 0) { /** **/
  211. const char *login = xs_dict_get(args, "login");
  212. const char *passwd = xs_dict_get(args, "passwd");
  213. const char *redir = xs_dict_get(args, "redir");
  214. const char *cid = xs_dict_get(args, "cid");
  215. const char *state = xs_dict_get(args, "state");
  216. const char *host = xs_dict_get(srv_config, "host");
  217. const char *proto = xs_dict_get_def(srv_config, "protocol", "https");
  218. /* by default, generate another login form with an error */
  219. *body = xs_fmt(login_page, host, host, "LOGIN INCORRECT", proto, host, "oauth/x-snac-login",
  220. redir, cid, state, USER_AGENT);
  221. *ctype = "text/html";
  222. status = HTTP_STATUS_OK;
  223. if (login && passwd && redir && cid) {
  224. snac snac;
  225. if (user_open(&snac, login)) {
  226. const char *addr = xs_or(xs_dict_get(req, "remote-addr"),
  227. xs_dict_get(req, "x-forwarded-for"));
  228. if (badlogin_check(login, addr)) {
  229. /* check the login + password */
  230. if (check_password(login, passwd, xs_dict_get(snac.config, "passwd"))) {
  231. /* success! redirect to the desired uri */
  232. xs *code = random_str();
  233. xs_free(*body);
  234. if (strcmp(redir, "urn:ietf:wg:oauth:2.0:oob") == 0) {
  235. *body = xs_dup(code);
  236. }
  237. else {
  238. if (xs_str_in(redir, "?") != -1)
  239. *body = xs_fmt("%s&code=%s", redir, code);
  240. else
  241. *body = xs_fmt("%s?code=%s", redir, code);
  242. status = HTTP_STATUS_SEE_OTHER;
  243. }
  244. /* if there is a state, add it */
  245. if (!xs_is_null(state) && *state) {
  246. *body = xs_str_cat(*body, "&state=");
  247. *body = xs_str_cat(*body, state);
  248. }
  249. srv_log(xs_fmt("oauth x-snac-login: '%s' success, redirect to %s",
  250. login, *body));
  251. /* assign the login to the app */
  252. xs *app = app_get(cid);
  253. if (app != NULL) {
  254. app = xs_dict_set(app, "uid", login);
  255. app = xs_dict_set(app, "code", code);
  256. app_add(cid, app);
  257. }
  258. else
  259. srv_log(xs_fmt("oauth x-snac-login: error getting app %s", cid));
  260. }
  261. else {
  262. srv_debug(1, xs_fmt("oauth x-snac-login: login '%s' incorrect", login));
  263. badlogin_inc(login, addr);
  264. }
  265. }
  266. user_free(&snac);
  267. }
  268. else
  269. srv_debug(1, xs_fmt("oauth x-snac-login: bad user '%s'", login));
  270. }
  271. else
  272. srv_debug(1, xs_fmt("oauth x-snac-login: invalid or unset arguments"));
  273. }
  274. else
  275. if (strcmp(cmd, "/token") == 0) { /** **/
  276. xs *wrk = NULL;
  277. const char *gtype = xs_dict_get(args, "grant_type");
  278. const char *code = xs_dict_get(args, "code");
  279. const char *cid = xs_dict_get(args, "client_id");
  280. const char *csec = xs_dict_get(args, "client_secret");
  281. const char *ruri = xs_dict_get(args, "redirect_uri");
  282. const char *scope = xs_dict_get(args, "scope");
  283. /* no client_secret? check if it's inside an authorization header
  284. (AndStatus does it this way) */
  285. if (xs_is_null(csec)) {
  286. const char *auhdr = xs_dict_get(req, "authorization");
  287. if (!xs_is_null(auhdr) && xs_startswith(auhdr, "Basic ")) {
  288. xs *s1 = xs_replace_n(auhdr, "Basic ", "", 1);
  289. int size;
  290. xs *s2 = xs_base64_dec(s1, &size);
  291. if (!xs_is_null(s2)) {
  292. xs *l1 = xs_split(s2, ":");
  293. if (xs_list_len(l1) == 2) {
  294. wrk = xs_dup(xs_list_get(l1, 1));
  295. csec = wrk;
  296. }
  297. }
  298. }
  299. }
  300. /* no code?
  301. I'm not sure of the impacts of this right now, but Subway Tooter does not
  302. provide a code so one must be generated */
  303. if (xs_is_null(code)){
  304. code = random_str();
  305. }
  306. if (gtype && code && cid && csec && ruri) {
  307. xs *app = app_get(cid);
  308. if (app == NULL) {
  309. status = HTTP_STATUS_UNAUTHORIZED;
  310. srv_log(xs_fmt("oauth token: invalid app %s", cid));
  311. }
  312. else
  313. if (strcmp(csec, xs_dict_get(app, "client_secret")) != 0) {
  314. status = HTTP_STATUS_UNAUTHORIZED;
  315. srv_log(xs_fmt("oauth token: invalid client_secret for app %s", cid));
  316. }
  317. else {
  318. xs *rsp = xs_dict_new();
  319. xs *cat = xs_number_new(time(NULL));
  320. xs *tokid = random_str();
  321. rsp = xs_dict_append(rsp, "access_token", tokid);
  322. rsp = xs_dict_append(rsp, "token_type", "Bearer");
  323. rsp = xs_dict_append(rsp, "created_at", cat);
  324. if (!xs_is_null(scope))
  325. rsp = xs_dict_append(rsp, "scope", scope);
  326. *body = xs_json_dumps(rsp, 4);
  327. *ctype = "application/json";
  328. status = HTTP_STATUS_OK;
  329. const char *uid = xs_dict_get(app, "uid");
  330. srv_debug(1, xs_fmt("oauth token: "
  331. "successful login for %s, new token %s", uid, tokid));
  332. xs *token = xs_dict_new();
  333. token = xs_dict_append(token, "token", tokid);
  334. token = xs_dict_append(token, "client_id", cid);
  335. token = xs_dict_append(token, "client_secret", csec);
  336. token = xs_dict_append(token, "uid", uid);
  337. token = xs_dict_append(token, "code", code);
  338. token_add(tokid, token);
  339. }
  340. }
  341. else {
  342. srv_debug(1, xs_fmt("oauth token: invalid or unset arguments"));
  343. status = HTTP_STATUS_BAD_REQUEST;
  344. }
  345. }
  346. else
  347. if (strcmp(cmd, "/revoke") == 0) { /** **/
  348. const char *cid = xs_dict_get(args, "client_id");
  349. const char *csec = xs_dict_get(args, "client_secret");
  350. const char *tokid = xs_dict_get(args, "token");
  351. if (cid && csec && tokid) {
  352. xs *token = token_get(tokid);
  353. *body = xs_str_new("{}");
  354. *ctype = "application/json";
  355. if (token == NULL || strcmp(csec, xs_dict_get(token, "client_secret")) != 0) {
  356. srv_debug(1, xs_fmt("oauth revoke: bad secret for token %s", tokid));
  357. status = HTTP_STATUS_FORBIDDEN;
  358. }
  359. else {
  360. token_del(tokid);
  361. srv_debug(1, xs_fmt("oauth revoke: revoked token %s", tokid));
  362. status = HTTP_STATUS_OK;
  363. /* also delete the app, as it serves no purpose from now on */
  364. app_del(cid);
  365. }
  366. }
  367. else {
  368. srv_debug(1, xs_fmt("oauth revoke: invalid or unset arguments"));
  369. status = HTTP_STATUS_FORBIDDEN;
  370. }
  371. }
  372. if (strcmp(cmd, "/x-snac-get-token") == 0) { /** **/
  373. const char *login = xs_dict_get(args, "login");
  374. const char *passwd = xs_dict_get(args, "passwd");
  375. const char *host = xs_dict_get(srv_config, "host");
  376. const char *proto = xs_dict_get_def(srv_config, "protocol", "https");
  377. /* by default, generate another login form with an error */
  378. *body = xs_fmt(login_page, host, host, "LOGIN INCORRECT", proto, host, "oauth/x-snac-get-token",
  379. "", "", "", USER_AGENT);
  380. *ctype = "text/html";
  381. status = HTTP_STATUS_OK;
  382. if (login && passwd) {
  383. snac user;
  384. if (user_open(&user, login)) {
  385. const char *addr = xs_or(xs_dict_get(req, "remote-addr"),
  386. xs_dict_get(req, "x-forwarded-for"));
  387. if (badlogin_check(login, addr)) {
  388. /* check the login + password */
  389. if (check_password(login, passwd, xs_dict_get(user.config, "passwd"))) {
  390. /* success! create a new token */
  391. xs *tokid = random_str();
  392. srv_debug(1, xs_fmt("x-snac-new-token: "
  393. "successful login for %s, new token %s", login, tokid));
  394. xs *token = xs_dict_new();
  395. token = xs_dict_append(token, "token", tokid);
  396. token = xs_dict_append(token, "client_id", "snac-client");
  397. token = xs_dict_append(token, "client_secret", "");
  398. token = xs_dict_append(token, "uid", login);
  399. token = xs_dict_append(token, "code", "");
  400. token_add(tokid, token);
  401. *ctype = "text/plain";
  402. xs_free(*body);
  403. *body = xs_dup(tokid);
  404. }
  405. else
  406. badlogin_inc(login, addr);
  407. user_free(&user);
  408. }
  409. }
  410. }
  411. }
  412. return status;
  413. }
  414. xs_str *mastoapi_id(const xs_dict *msg)
  415. /* returns a somewhat Mastodon-compatible status id */
  416. {
  417. const char *id = xs_dict_get(msg, "id");
  418. xs *md5 = xs_md5_hex(id, strlen(id));
  419. return xs_fmt("%10.0f%s", object_ctime_by_md5(md5), md5);
  420. }
  421. #define MID_TO_MD5(id) (id + 10)
  422. xs_dict *mastoapi_account(snac *logged, const xs_dict *actor)
  423. /* converts an ActivityPub actor to a Mastodon account */
  424. {
  425. const char *id = xs_dict_get(actor, "id");
  426. const char *pub = xs_dict_get(actor, "published");
  427. const char *proxy = NULL;
  428. if (xs_type(id) != XSTYPE_STRING)
  429. return NULL;
  430. if (logged && xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  431. proxy = logged->actor;
  432. const char *prefu = xs_dict_get(actor, "preferredUsername");
  433. const char *display_name = xs_dict_get(actor, "name");
  434. if (xs_is_null(display_name) || *display_name == '\0')
  435. display_name = prefu;
  436. xs_dict *acct = xs_dict_new();
  437. xs *acct_md5 = xs_md5_hex(id, strlen(id));
  438. acct = xs_dict_append(acct, "id", acct_md5);
  439. acct = xs_dict_append(acct, "username", prefu);
  440. acct = xs_dict_append(acct, "display_name", display_name);
  441. acct = xs_dict_append(acct, "discoverable", xs_stock(XSTYPE_TRUE));
  442. acct = xs_dict_append(acct, "group", xs_stock(XSTYPE_FALSE));
  443. acct = xs_dict_append(acct, "hide_collections", xs_stock(XSTYPE_FALSE));
  444. acct = xs_dict_append(acct, "indexable", xs_stock(XSTYPE_TRUE));
  445. acct = xs_dict_append(acct, "noindex", xs_stock(XSTYPE_FALSE));
  446. acct = xs_dict_append(acct, "roles", xs_stock(XSTYPE_LIST));
  447. {
  448. /* create the acct field as user@host */
  449. xs *l = xs_split(id, "/");
  450. xs *fquid = xs_fmt("%s@%s", prefu, xs_list_get(l, 2));
  451. acct = xs_dict_append(acct, "acct", fquid);
  452. }
  453. if (pub)
  454. acct = xs_dict_append(acct, "created_at", pub);
  455. else {
  456. /* unset created_at crashes Tusky, so lie like a mf */
  457. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  458. acct = xs_dict_append(acct, "created_at", date);
  459. }
  460. xs *last_status_at = xs_str_utctime(0, "%Y-%m-%d");
  461. acct = xs_dict_append(acct, "last_status_at", last_status_at);
  462. const char *note = xs_dict_get(actor, "summary");
  463. if (xs_is_null(note))
  464. note = "";
  465. if (strcmp(xs_dict_get(actor, "type"), "Service") == 0)
  466. acct = xs_dict_append(acct, "bot", xs_stock(XSTYPE_TRUE));
  467. else
  468. acct = xs_dict_append(acct, "bot", xs_stock(XSTYPE_FALSE));
  469. acct = xs_dict_append(acct, "note", note);
  470. acct = xs_dict_append(acct, "url", id);
  471. acct = xs_dict_append(acct, "uri", id);
  472. xs *avatar = NULL;
  473. const xs_dict *av = xs_dict_get(actor, "icon");
  474. if (xs_type(av) == XSTYPE_DICT) {
  475. const char *url = xs_dict_get(av, "url");
  476. if (url != NULL)
  477. avatar = make_url(url, proxy, 1);
  478. }
  479. if (avatar == NULL)
  480. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  481. acct = xs_dict_append(acct, "avatar", avatar);
  482. acct = xs_dict_append(acct, "avatar_static", avatar);
  483. xs *header = NULL;
  484. const xs_dict *hd = xs_dict_get(actor, "image");
  485. if (xs_type(hd) == XSTYPE_DICT)
  486. header = make_url(xs_dict_get(hd, "url"), proxy, 1);
  487. if (xs_is_null(header))
  488. header = xs_str_new(NULL);
  489. acct = xs_dict_append(acct, "header", header);
  490. acct = xs_dict_append(acct, "header_static", header);
  491. /* emojis */
  492. const xs_list *p;
  493. if (!xs_is_null(p = xs_dict_get(actor, "tag"))) {
  494. xs *eml = xs_list_new();
  495. const xs_dict *v;
  496. int c = 0;
  497. while (xs_list_next(p, &v, &c)) {
  498. const char *type = xs_dict_get(v, "type");
  499. if (!xs_is_null(type) && strcmp(type, "Emoji") == 0) {
  500. const char *name = xs_dict_get(v, "name");
  501. const xs_dict *icon = xs_dict_get(v, "icon");
  502. if (!xs_is_null(name) && !xs_is_null(icon)) {
  503. const char *o_url = xs_dict_get(icon, "url");
  504. if (!xs_is_null(o_url)) {
  505. xs *url = make_url(o_url, proxy, 1);
  506. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  507. xs *d1 = xs_dict_new();
  508. d1 = xs_dict_append(d1, "shortcode", nm);
  509. d1 = xs_dict_append(d1, "url", url);
  510. d1 = xs_dict_append(d1, "static_url", url);
  511. d1 = xs_dict_append(d1, "visible_in_picker", xs_stock(XSTYPE_TRUE));
  512. eml = xs_list_append(eml, d1);
  513. }
  514. }
  515. }
  516. }
  517. acct = xs_dict_append(acct, "emojis", eml);
  518. }
  519. acct = xs_dict_append(acct, "locked", xs_stock(XSTYPE_FALSE));
  520. acct = xs_dict_append(acct, "followers_count", xs_stock(0));
  521. acct = xs_dict_append(acct, "following_count", xs_stock(0));
  522. acct = xs_dict_append(acct, "statuses_count", xs_stock(0));
  523. xs *fields = xs_list_new();
  524. p = xs_dict_get(actor, "attachment");
  525. const xs_dict *v;
  526. /* dict of validated links */
  527. xs_dict *val_links = NULL;
  528. const xs_dict *metadata = xs_stock(XSTYPE_DICT);
  529. snac user = {0};
  530. if (xs_startswith(id, srv_baseurl)) {
  531. /* if it's a local user, open it and pick its validated links */
  532. if (user_open(&user, prefu)) {
  533. val_links = user.links;
  534. metadata = xs_dict_get_def(user.config, "metadata", xs_stock(XSTYPE_DICT));
  535. /* does this user want to publish their contact metrics? */
  536. if (xs_is_true(xs_dict_get(user.config, "show_contact_metrics"))) {
  537. int fwing = following_list_len(&user);
  538. int fwers = follower_list_len(&user);
  539. xs *ni = xs_number_new(fwing);
  540. xs *ne = xs_number_new(fwers);
  541. acct = xs_dict_append(acct, "followers_count", ne);
  542. acct = xs_dict_append(acct, "following_count", ni);
  543. }
  544. }
  545. }
  546. if (xs_is_null(val_links))
  547. val_links = xs_stock(XSTYPE_DICT);
  548. int c = 0;
  549. while (xs_list_next(p, &v, &c)) {
  550. const char *type = xs_dict_get(v, "type");
  551. const char *name = xs_dict_get(v, "name");
  552. const char *value = xs_dict_get(v, "value");
  553. if (!xs_is_null(type) && !xs_is_null(name) &&
  554. !xs_is_null(value) && strcmp(type, "PropertyValue") == 0) {
  555. xs *val_date = NULL;
  556. const char *url = xs_dict_get(metadata, name);
  557. if (!xs_is_null(url) && xs_startswith(url, "https:/" "/")) {
  558. const xs_number *verified_time = xs_dict_get(val_links, url);
  559. if (xs_type(verified_time) == XSTYPE_NUMBER) {
  560. time_t t = xs_number_get(verified_time);
  561. if (t > 0)
  562. val_date = xs_str_utctime(t, ISO_DATE_SPEC);
  563. }
  564. }
  565. xs *d = xs_dict_new();
  566. d = xs_dict_append(d, "name", name);
  567. d = xs_dict_append(d, "value", value);
  568. d = xs_dict_append(d, "verified_at",
  569. xs_type(val_date) == XSTYPE_STRING && *val_date ?
  570. val_date : xs_stock(XSTYPE_NULL));
  571. fields = xs_list_append(fields, d);
  572. }
  573. }
  574. user_free(&user);
  575. acct = xs_dict_append(acct, "fields", fields);
  576. return acct;
  577. }
  578. xs_str *mastoapi_date(const char *date)
  579. /* converts an ISO 8601 date to whatever format Mastodon uses */
  580. {
  581. xs_str *s = xs_crop_i(xs_dup(date), 0, 19);
  582. s = xs_str_cat(s, ".000Z");
  583. return s;
  584. }
  585. xs_dict *mastoapi_poll(snac *snac, const xs_dict *msg)
  586. /* creates a mastoapi Poll object */
  587. {
  588. xs_dict *poll = xs_dict_new();
  589. xs *mid = mastoapi_id(msg);
  590. const xs_list *opts = NULL;
  591. const xs_val *v;
  592. int num_votes = 0;
  593. xs *options = xs_list_new();
  594. poll = xs_dict_append(poll, "id", mid);
  595. const char *date = xs_dict_get(msg, "endTime");
  596. if (date == NULL)
  597. date = xs_dict_get(msg, "closed");
  598. if (date == NULL)
  599. return NULL;
  600. xs *fd = mastoapi_date(date);
  601. poll = xs_dict_append(poll, "expires_at", fd);
  602. date = xs_dict_get(msg, "closed");
  603. time_t t = 0;
  604. if (date != NULL)
  605. t = xs_parse_iso_date(date, 0);
  606. poll = xs_dict_append(poll, "expired",
  607. t < time(NULL) ? xs_stock(XSTYPE_FALSE) : xs_stock(XSTYPE_TRUE));
  608. if ((opts = xs_dict_get(msg, "oneOf")) != NULL)
  609. poll = xs_dict_append(poll, "multiple", xs_stock(XSTYPE_FALSE));
  610. else {
  611. opts = xs_dict_get(msg, "anyOf");
  612. poll = xs_dict_append(poll, "multiple", xs_stock(XSTYPE_TRUE));
  613. }
  614. int c = 0;
  615. while (xs_list_next(opts, &v, &c)) {
  616. const char *title = xs_dict_get(v, "name");
  617. const char *replies = xs_dict_get(v, "replies");
  618. if (title && replies) {
  619. const char *votes_count = xs_dict_get(replies, "totalItems");
  620. if (xs_type(votes_count) == XSTYPE_NUMBER) {
  621. xs *d = xs_dict_new();
  622. d = xs_dict_append(d, "title", title);
  623. d = xs_dict_append(d, "votes_count", votes_count);
  624. options = xs_list_append(options, d);
  625. num_votes += xs_number_get(votes_count);
  626. }
  627. }
  628. }
  629. poll = xs_dict_append(poll, "options", options);
  630. xs *vc = xs_number_new(num_votes);
  631. poll = xs_dict_append(poll, "votes_count", vc);
  632. poll = xs_dict_append(poll, "emojis", xs_stock(XSTYPE_LIST));
  633. poll = xs_dict_append(poll, "voted",
  634. (snac && was_question_voted(snac, xs_dict_get(msg, "id"))) ?
  635. xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  636. return poll;
  637. }
  638. xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
  639. /* converts an ActivityPub note to a Mastodon status */
  640. {
  641. xs *actor = NULL;
  642. actor_get_refresh(snac, get_atto(msg), &actor);
  643. const char *proxy = NULL;
  644. /* if the author is not here, discard */
  645. if (actor == NULL)
  646. return NULL;
  647. if (snac && xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  648. proxy = snac->actor;
  649. const char *type = xs_dict_get(msg, "type");
  650. const char *id = xs_dict_get(msg, "id");
  651. /* fail if it's not a valid actor */
  652. if (xs_is_null(type) || xs_is_null(id))
  653. return NULL;
  654. xs *acct = mastoapi_account(snac, actor);
  655. if (acct == NULL)
  656. return NULL;
  657. xs *idx = NULL;
  658. xs *ixc = NULL;
  659. const char *tmp;
  660. xs *mid = mastoapi_id(msg);
  661. xs_dict *st = xs_dict_new();
  662. st = xs_dict_append(st, "id", mid);
  663. st = xs_dict_append(st, "uri", id);
  664. st = xs_dict_append(st, "url", id);
  665. st = xs_dict_append(st, "account", acct);
  666. const char *published = xs_dict_get(msg, "published");
  667. xs *fd = NULL;
  668. if (published)
  669. fd = mastoapi_date(published);
  670. else {
  671. xs *p = xs_str_iso_date(0);
  672. fd = mastoapi_date(p);
  673. }
  674. st = xs_dict_append(st, "created_at", fd);
  675. {
  676. const char *content = xs_dict_get(msg, "content");
  677. const char *name = xs_dict_get(msg, "name");
  678. xs *s1 = NULL;
  679. if (name && content)
  680. s1 = xs_fmt("%s<br><br>%s", name, content);
  681. else
  682. if (name)
  683. s1 = xs_dup(name);
  684. else
  685. if (content)
  686. s1 = xs_dup(content);
  687. else
  688. s1 = xs_str_new(NULL);
  689. st = xs_dict_append(st, "content", s1);
  690. }
  691. st = xs_dict_append(st, "visibility",
  692. is_msg_public(msg) ? "public" : "private");
  693. tmp = xs_dict_get(msg, "sensitive");
  694. if (xs_is_null(tmp))
  695. tmp = xs_stock(XSTYPE_FALSE);
  696. st = xs_dict_append(st, "sensitive", tmp);
  697. tmp = xs_dict_get(msg, "summary");
  698. if (xs_is_null(tmp))
  699. tmp = "";
  700. st = xs_dict_append(st, "spoiler_text", tmp);
  701. /* create the list of attachments */
  702. xs *attach = get_attachments(msg);
  703. {
  704. xs_list *p = attach;
  705. const xs_dict *v;
  706. xs *matt = xs_list_new();
  707. while (xs_list_iter(&p, &v)) {
  708. const char *type = xs_dict_get(v, "type");
  709. const char *o_href = xs_dict_get(v, "href");
  710. const char *name = xs_dict_get(v, "name");
  711. if (xs_match(type, "image/*|video/*|audio/*|Image|Video")) { /* */
  712. xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
  713. xs *href = make_url(o_href, proxy, 1);
  714. xs *d = xs_dict_new();
  715. d = xs_dict_append(d, "id", matteid);
  716. d = xs_dict_append(d, "url", href);
  717. d = xs_dict_append(d, "preview_url", href);
  718. d = xs_dict_append(d, "remote_url", href);
  719. d = xs_dict_append(d, "description", name);
  720. d = xs_dict_append(d, "type", (*type == 'v' || *type == 'V') ? "video" :
  721. (*type == 'a' || *type == 'A') ? "audio" : "image");
  722. matt = xs_list_append(matt, d);
  723. }
  724. }
  725. st = xs_dict_append(st, "media_attachments", matt);
  726. }
  727. {
  728. xs *ml = xs_list_new();
  729. xs *htl = xs_list_new();
  730. xs *eml = xs_list_new();
  731. const xs_list *tag = xs_dict_get(msg, "tag");
  732. int n = 0;
  733. xs *tag_list = NULL;
  734. if (xs_type(tag) == XSTYPE_DICT) {
  735. tag_list = xs_list_new();
  736. tag_list = xs_list_append(tag_list, tag);
  737. }
  738. else
  739. if (xs_type(tag) == XSTYPE_LIST)
  740. tag_list = xs_dup(tag);
  741. else
  742. tag_list = xs_list_new();
  743. tag = tag_list;
  744. const xs_dict *v;
  745. int c = 0;
  746. while (xs_list_next(tag, &v, &c)) {
  747. const char *type = xs_dict_get(v, "type");
  748. if (xs_is_null(type))
  749. continue;
  750. xs *d1 = xs_dict_new();
  751. if (strcmp(type, "Mention") == 0) {
  752. const char *name = xs_dict_get(v, "name");
  753. const char *href = xs_dict_get(v, "href");
  754. if (!xs_is_null(name) && !xs_is_null(href) &&
  755. (snac == NULL || strcmp(href, snac->actor) != 0)) {
  756. xs *nm = xs_strip_chars_i(xs_dup(name), "@");
  757. xs *id = xs_fmt("%d", n++);
  758. d1 = xs_dict_append(d1, "id", id);
  759. d1 = xs_dict_append(d1, "username", nm);
  760. d1 = xs_dict_append(d1, "acct", nm);
  761. d1 = xs_dict_append(d1, "url", href);
  762. ml = xs_list_append(ml, d1);
  763. }
  764. }
  765. else
  766. if (strcmp(type, "Hashtag") == 0) {
  767. const char *name = xs_dict_get(v, "name");
  768. const char *href = xs_dict_get(v, "href");
  769. if (!xs_is_null(name) && !xs_is_null(href)) {
  770. xs *nm = xs_strip_chars_i(xs_dup(name), "#");
  771. d1 = xs_dict_append(d1, "name", nm);
  772. d1 = xs_dict_append(d1, "url", href);
  773. htl = xs_list_append(htl, d1);
  774. }
  775. }
  776. else
  777. if (strcmp(type, "Emoji") == 0) {
  778. const char *name = xs_dict_get(v, "name");
  779. const xs_dict *icon = xs_dict_get(v, "icon");
  780. if (!xs_is_null(name) && !xs_is_null(icon)) {
  781. const char *o_url = xs_dict_get(icon, "url");
  782. if (!xs_is_null(o_url)) {
  783. xs *url = make_url(o_url, snac ? snac->actor : NULL, 1);
  784. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  785. d1 = xs_dict_append(d1, "shortcode", nm);
  786. d1 = xs_dict_append(d1, "url", url);
  787. d1 = xs_dict_append(d1, "static_url", url);
  788. d1 = xs_dict_append(d1, "visible_in_picker", xs_stock(XSTYPE_TRUE));
  789. d1 = xs_dict_append(d1, "category", "Emojis");
  790. eml = xs_list_append(eml, d1);
  791. }
  792. }
  793. }
  794. }
  795. st = xs_dict_append(st, "mentions", ml);
  796. st = xs_dict_append(st, "tags", htl);
  797. st = xs_dict_append(st, "emojis", eml);
  798. }
  799. xs_free(idx);
  800. xs_free(ixc);
  801. idx = object_likes(id);
  802. ixc = xs_number_new(xs_list_len(idx));
  803. st = xs_dict_append(st, "favourites_count", ixc);
  804. st = xs_dict_append(st, "favourited",
  805. (snac && xs_list_in(idx, snac->md5) != -1) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  806. xs_free(idx);
  807. xs_free(ixc);
  808. idx = object_announces(id);
  809. ixc = xs_number_new(xs_list_len(idx));
  810. st = xs_dict_append(st, "reblogs_count", ixc);
  811. st = xs_dict_append(st, "reblogged",
  812. (snac && xs_list_in(idx, snac->md5) != -1) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  813. /* get the last person who boosted this */
  814. xs *boosted_by_md5 = NULL;
  815. if (xs_list_len(idx))
  816. boosted_by_md5 = xs_dup(xs_list_get(idx, -1));
  817. xs_free(idx);
  818. xs_free(ixc);
  819. idx = object_children(id);
  820. ixc = xs_number_new(xs_list_len(idx));
  821. st = xs_dict_append(st, "replies_count", ixc);
  822. /* default in_reply_to values */
  823. st = xs_dict_append(st, "in_reply_to_id", xs_stock(XSTYPE_NULL));
  824. st = xs_dict_append(st, "in_reply_to_account_id", xs_stock(XSTYPE_NULL));
  825. tmp = get_in_reply_to(msg);
  826. if (!xs_is_null(tmp)) {
  827. xs *irto = NULL;
  828. if (valid_status(object_get(tmp, &irto))) {
  829. xs *irt_mid = mastoapi_id(irto);
  830. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  831. const char *at = NULL;
  832. if (!xs_is_null(at = get_atto(irto))) {
  833. xs *at_md5 = xs_md5_hex(at, strlen(at));
  834. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  835. }
  836. }
  837. }
  838. st = xs_dict_append(st, "reblog", xs_stock(XSTYPE_NULL));
  839. st = xs_dict_append(st, "card", xs_stock(XSTYPE_NULL));
  840. st = xs_dict_append(st, "language", "en");
  841. st = xs_dict_append(st, "filtered", xs_stock(XSTYPE_LIST));
  842. st = xs_dict_append(st, "muted", xs_stock(XSTYPE_FALSE));
  843. tmp = xs_dict_get(msg, "sourceContent");
  844. if (xs_is_null(tmp))
  845. tmp = "";
  846. st = xs_dict_append(st, "text", tmp);
  847. tmp = xs_dict_get(msg, "updated");
  848. xs *fd2 = NULL;
  849. if (xs_is_null(tmp))
  850. tmp = xs_stock(XSTYPE_NULL);
  851. else {
  852. fd2 = mastoapi_date(tmp);
  853. tmp = fd2;
  854. }
  855. st = xs_dict_append(st, "edited_at", tmp);
  856. if (strcmp(type, "Question") == 0) {
  857. xs *poll = mastoapi_poll(snac, msg);
  858. st = xs_dict_append(st, "poll", poll);
  859. }
  860. else
  861. st = xs_dict_append(st, "poll", xs_stock(XSTYPE_NULL));
  862. st = xs_dict_append(st, "bookmarked",
  863. (snac && is_bookmarked(snac, id)) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  864. st = xs_dict_append(st, "pinned",
  865. (snac && is_pinned(snac, id)) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  866. /* is it a boost? */
  867. if (!xs_is_null(boosted_by_md5)) {
  868. /* create a new dummy status, using st as the 'reblog' field */
  869. xs_dict *bst = xs_dup(st);
  870. xs *b_actor = NULL;
  871. if (valid_status(object_get_by_md5(boosted_by_md5, &b_actor))) {
  872. xs *b_acct = mastoapi_account(snac, b_actor);
  873. xs *fake_uri = NULL;
  874. if (snac)
  875. fake_uri = xs_fmt("%s/d/%s/Announce", snac->actor, mid);
  876. else
  877. fake_uri = xs_fmt("%s#%s", srv_baseurl, mid);
  878. bst = xs_dict_set(bst, "uri", fake_uri);
  879. bst = xs_dict_set(bst, "url", fake_uri);
  880. bst = xs_dict_set(bst, "account", b_acct);
  881. bst = xs_dict_set(bst, "content", "");
  882. bst = xs_dict_set(bst, "reblog", st);
  883. xs_free(st);
  884. st = bst;
  885. }
  886. }
  887. return st;
  888. }
  889. xs_dict *mastoapi_relationship(snac *snac, const char *md5)
  890. {
  891. xs_dict *rel = NULL;
  892. xs *actor_o = NULL;
  893. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  894. rel = xs_dict_new();
  895. const char *actor = xs_dict_get(actor_o, "id");
  896. rel = xs_dict_append(rel, "id", md5);
  897. rel = xs_dict_append(rel, "following",
  898. following_check(snac, actor) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  899. rel = xs_dict_append(rel, "showing_reblogs", xs_stock(XSTYPE_TRUE));
  900. rel = xs_dict_append(rel, "notifying", xs_stock(XSTYPE_FALSE));
  901. rel = xs_dict_append(rel, "followed_by",
  902. follower_check(snac, actor) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  903. rel = xs_dict_append(rel, "blocking",
  904. is_muted(snac, actor) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  905. rel = xs_dict_append(rel, "muting", xs_stock(XSTYPE_FALSE));
  906. rel = xs_dict_append(rel, "muting_notifications", xs_stock(XSTYPE_FALSE));
  907. rel = xs_dict_append(rel, "requested", xs_stock(XSTYPE_FALSE));
  908. rel = xs_dict_append(rel, "domain_blocking", xs_stock(XSTYPE_FALSE));
  909. rel = xs_dict_append(rel, "endorsed", xs_stock(XSTYPE_FALSE));
  910. rel = xs_dict_append(rel, "note", "");
  911. }
  912. return rel;
  913. }
  914. int process_auth_token(snac *snac, const xs_dict *req)
  915. /* processes an authorization token, if there is one */
  916. {
  917. int logged_in = 0;
  918. const char *v;
  919. /* if there is an authorization field, try to validate it */
  920. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  921. xs *tokid = xs_replace_n(v, "Bearer ", "", 1);
  922. xs *token = token_get(tokid);
  923. if (token != NULL) {
  924. const char *uid = xs_dict_get(token, "uid");
  925. if (!xs_is_null(uid) && user_open(snac, uid)) {
  926. logged_in = 1;
  927. /* this counts as a 'login' */
  928. lastlog_write(snac, "mastoapi");
  929. srv_debug(2, xs_fmt("mastoapi auth: valid token for user '%s'", uid));
  930. }
  931. else
  932. srv_log(xs_fmt("mastoapi auth: corrupted token '%s'", tokid));
  933. }
  934. else
  935. srv_log(xs_fmt("mastoapi auth: invalid token '%s'", tokid));
  936. }
  937. return logged_in;
  938. }
  939. void credentials_get(char **body, char **ctype, int *status, snac snac)
  940. {
  941. xs *acct = xs_dict_new();
  942. const xs_val *bot = xs_dict_get(snac.config, "bot");
  943. acct = xs_dict_append(acct, "id", snac.md5);
  944. acct = xs_dict_append(acct, "username", xs_dict_get(snac.config, "uid"));
  945. acct = xs_dict_append(acct, "acct", xs_dict_get(snac.config, "uid"));
  946. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac.config, "name"));
  947. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac.config, "published"));
  948. acct = xs_dict_append(acct, "last_status_at", xs_dict_get(snac.config, "published"));
  949. acct = xs_dict_append(acct, "note", xs_dict_get(snac.config, "bio"));
  950. acct = xs_dict_append(acct, "url", snac.actor);
  951. acct = xs_dict_append(acct, "locked",
  952. xs_stock(xs_is_true(xs_dict_get(snac.config, "approve_followers")) ? XSTYPE_TRUE : XSTYPE_FALSE));
  953. acct = xs_dict_append(acct, "bot", xs_stock(xs_is_true(bot) ? XSTYPE_TRUE : XSTYPE_FALSE));
  954. acct = xs_dict_append(acct, "emojis", xs_stock(XSTYPE_LIST));
  955. xs *src = xs_json_loads("{\"privacy\":\"public\", \"language\":\"en\","
  956. "\"follow_requests_count\": 0,"
  957. "\"sensitive\":false,\"fields\":[],\"note\":\"\"}");
  958. /* some apps take the note from the source object */
  959. src = xs_dict_set(src, "note", xs_dict_get(snac.config, "bio"));
  960. src = xs_dict_set(src, "privacy", xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE ? "private" : "public");
  961. const xs_str *cw = xs_dict_get(snac.config, "cw");
  962. src = xs_dict_set(src, "sensitive",
  963. strcmp(cw, "open") == 0 ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  964. src = xs_dict_set(src, "bot", xs_stock(xs_is_true(bot) ? XSTYPE_TRUE : XSTYPE_FALSE));
  965. xs *avatar = NULL;
  966. const char *av = xs_dict_get(snac.config, "avatar");
  967. if (xs_is_null(av) || *av == '\0')
  968. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  969. else
  970. avatar = xs_dup(av);
  971. acct = xs_dict_append(acct, "avatar", avatar);
  972. acct = xs_dict_append(acct, "avatar_static", avatar);
  973. xs *header = NULL;
  974. const char *hd = xs_dict_get(snac.config, "header");
  975. if (!xs_is_null(hd))
  976. header = xs_dup(hd);
  977. else
  978. header = xs_str_new(NULL);
  979. acct = xs_dict_append(acct, "header", header);
  980. acct = xs_dict_append(acct, "header_static", header);
  981. const xs_dict *metadata = xs_dict_get(snac.config, "metadata");
  982. if (xs_type(metadata) == XSTYPE_DICT) {
  983. xs *fields = xs_list_new();
  984. const xs_str *k;
  985. const xs_str *v;
  986. xs_dict *val_links = snac.links;
  987. if (xs_is_null(val_links))
  988. val_links = xs_stock(XSTYPE_DICT);
  989. int c = 0;
  990. while (xs_dict_next(metadata, &k, &v, &c)) {
  991. xs *val_date = NULL;
  992. const xs_number *verified_time = xs_dict_get(val_links, v);
  993. if (xs_type(verified_time) == XSTYPE_NUMBER) {
  994. time_t t = xs_number_get(verified_time);
  995. if (t > 0)
  996. val_date = xs_str_utctime(t, ISO_DATE_SPEC);
  997. }
  998. xs *d = xs_dict_new();
  999. d = xs_dict_append(d, "name", k);
  1000. d = xs_dict_append(d, "value", v);
  1001. d = xs_dict_append(d, "verified_at",
  1002. xs_type(val_date) == XSTYPE_STRING && *val_date ? val_date : xs_stock(XSTYPE_NULL));
  1003. fields = xs_list_append(fields, d);
  1004. }
  1005. acct = xs_dict_set(acct, "fields", fields);
  1006. /* some apps take the fields from the source object */
  1007. src = xs_dict_set(src, "fields", fields);
  1008. }
  1009. acct = xs_dict_append(acct, "source", src);
  1010. acct = xs_dict_append(acct, "followers_count", xs_stock(0));
  1011. acct = xs_dict_append(acct, "following_count", xs_stock(0));
  1012. acct = xs_dict_append(acct, "statuses_count", xs_stock(0));
  1013. /* does this user want to publish their contact metrics? */
  1014. if (xs_is_true(xs_dict_get(snac.config, "show_contact_metrics"))) {
  1015. int fwing = following_list_len(&snac);
  1016. int fwers = follower_list_len(&snac);
  1017. xs *ni = xs_number_new(fwing);
  1018. xs *ne = xs_number_new(fwers);
  1019. acct = xs_dict_append(acct, "followers_count", ne);
  1020. acct = xs_dict_append(acct, "following_count", ni);
  1021. }
  1022. *body = xs_json_dumps(acct, 4);
  1023. *ctype = "application/json";
  1024. *status = HTTP_STATUS_OK;
  1025. }
  1026. xs_list *mastoapi_timeline(snac *user, const xs_dict *args, const char *index_fn)
  1027. {
  1028. xs_list *out = xs_list_new();
  1029. FILE *f;
  1030. char md5[MD5_HEX_SIZE];
  1031. if (dbglevel) {
  1032. xs *js = xs_json_dumps(args, 0);
  1033. srv_debug(1, xs_fmt("mastoapi_timeline args %s", js));
  1034. }
  1035. if ((f = fopen(index_fn, "r")) == NULL)
  1036. return out;
  1037. const char *max_id = xs_dict_get(args, "max_id");
  1038. const char *since_id = xs_dict_get(args, "since_id");
  1039. const char *min_id = xs_dict_get(args, "min_id"); /* unsupported old-to-new navigation */
  1040. const char *limit_s = xs_dict_get(args, "limit");
  1041. int (*iterator)(FILE *, char *);
  1042. int initial_status = 0;
  1043. int ascending = 0;
  1044. int limit = 0;
  1045. int cnt = 0;
  1046. if (!xs_is_null(limit_s))
  1047. limit = atoi(limit_s);
  1048. if (limit == 0)
  1049. limit = 20;
  1050. if (min_id) {
  1051. iterator = &index_asc_next;
  1052. initial_status = index_asc_first(f, md5, MID_TO_MD5(min_id));
  1053. ascending = 1;
  1054. }
  1055. else {
  1056. iterator = &index_desc_next;
  1057. initial_status = index_desc_first(f, md5, 0);
  1058. }
  1059. if (initial_status) {
  1060. do {
  1061. xs *msg = NULL;
  1062. /* only return entries older that max_id */
  1063. if (max_id) {
  1064. if (strcmp(md5, MID_TO_MD5(max_id)) == 0) {
  1065. max_id = NULL;
  1066. if (ascending)
  1067. break;
  1068. }
  1069. if (!ascending)
  1070. continue;
  1071. }
  1072. /* only returns entries newer than since_id */
  1073. if (since_id) {
  1074. if (strcmp(md5, MID_TO_MD5(since_id)) == 0) {
  1075. if (!ascending)
  1076. break;
  1077. since_id = NULL;
  1078. }
  1079. if (ascending)
  1080. continue;
  1081. }
  1082. /* get the entry */
  1083. if (user) {
  1084. if (!valid_status(timeline_get_by_md5(user, md5, &msg)))
  1085. continue;
  1086. }
  1087. else {
  1088. if (!valid_status(object_get_by_md5(md5, &msg)))
  1089. continue;
  1090. }
  1091. /* discard non-Notes */
  1092. const char *id = xs_dict_get(msg, "id");
  1093. const char *type = xs_dict_get(msg, "type");
  1094. if (!xs_match(type, POSTLIKE_OBJECT_TYPE))
  1095. continue;
  1096. if (id && is_instance_blocked(id))
  1097. continue;
  1098. const char *from = NULL;
  1099. if (strcmp(type, "Page") == 0)
  1100. from = xs_dict_get(msg, "audience");
  1101. if (from == NULL)
  1102. from = get_atto(msg);
  1103. if (from == NULL)
  1104. continue;
  1105. if (user) {
  1106. /* is this message from a person we don't follow? */
  1107. if (strcmp(from, user->actor) && !following_check(user, from)) {
  1108. /* discard if it was not boosted */
  1109. xs *idx = object_announces(id);
  1110. if (xs_list_len(idx) == 0)
  1111. continue;
  1112. }
  1113. /* discard notes from muted morons */
  1114. if (is_muted(user, from))
  1115. continue;
  1116. /* discard hidden notes */
  1117. if (is_hidden(user, id))
  1118. continue;
  1119. }
  1120. else {
  1121. /* skip non-public messages */
  1122. if (!is_msg_public(msg))
  1123. continue;
  1124. /* discard messages from private users */
  1125. if (is_msg_from_private_user(msg))
  1126. continue;
  1127. }
  1128. /* if it has a name and it's not an object that may have one,
  1129. it's a poll vote, so discard it */
  1130. if (!xs_is_null(xs_dict_get(msg, "name")) && !xs_match(type, "Page|Video|Audio|Event"))
  1131. continue;
  1132. /* convert the Note into a Mastodon status */
  1133. xs *st = mastoapi_status(user, msg);
  1134. if (st != NULL) {
  1135. if (ascending)
  1136. out = xs_list_insert(out, 0, st);
  1137. else
  1138. out = xs_list_append(out, st);
  1139. cnt++;
  1140. }
  1141. } while ((cnt < limit) && (*iterator)(f, md5));
  1142. }
  1143. int more = index_desc_next(f, md5);
  1144. fclose(f);
  1145. srv_debug(1, xs_fmt("mastoapi_timeline ret %d%s", cnt, more ? " (+)" : ""));
  1146. return out;
  1147. }
  1148. xs_str *timeline_link_header(const char *endpoint, xs_list *timeline)
  1149. /* returns a Link header with paging information */
  1150. {
  1151. xs_str *s = NULL;
  1152. if (xs_list_len(timeline) == 0)
  1153. return NULL;
  1154. const xs_dict *first_st = xs_list_get(timeline, 0);
  1155. const xs_dict *last_st = xs_list_get(timeline, -1);
  1156. const char *first_id = xs_dict_get(first_st, "id");
  1157. const char *last_id = xs_dict_get(last_st, "id");
  1158. const char *host = xs_dict_get(srv_config, "host");
  1159. const char *protocol = xs_dict_get_def(srv_config, "protocol", "https");
  1160. s = xs_fmt(
  1161. "<%s:/" "/%s%s?max_id=%s>; rel=\"next\", "
  1162. "<%s:/" "/%s%s?since_id=%s>; rel=\"prev\"",
  1163. protocol, host, endpoint, last_id,
  1164. protocol, host, endpoint, first_id);
  1165. srv_debug(1, xs_fmt("timeline_link_header %s", s));
  1166. return s;
  1167. }
  1168. xs_list *mastoapi_account_lists(snac *user, const char *uid)
  1169. /* returns the list of list an user is in */
  1170. {
  1171. xs_list *out = xs_list_new();
  1172. xs *actor_md5 = NULL;
  1173. xs *lol = list_maint(user, NULL, 0);
  1174. if (uid) {
  1175. if (!xs_is_hex(uid))
  1176. actor_md5 = xs_md5_hex(uid, strlen(uid));
  1177. else
  1178. actor_md5 = xs_dup(uid);
  1179. }
  1180. const xs_list *li;
  1181. xs_list_foreach(lol, li) {
  1182. const char *list_id = xs_list_get(li, 0);
  1183. const char *list_title = xs_list_get(li, 1);
  1184. if (uid) {
  1185. xs *users = list_content(user, list_id, NULL, 0);
  1186. if (xs_list_in(users, actor_md5) == -1)
  1187. continue;
  1188. }
  1189. xs *d = xs_dict_new();
  1190. d = xs_dict_append(d, "id", list_id);
  1191. d = xs_dict_append(d, "title", list_title);
  1192. d = xs_dict_append(d, "replies_policy", "list");
  1193. d = xs_dict_append(d, "exclusive", xs_stock(XSTYPE_FALSE));
  1194. out = xs_list_append(out, d);
  1195. }
  1196. return out;
  1197. }
  1198. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  1199. char **body, int *b_size, char **ctype, xs_str **link)
  1200. {
  1201. (void)b_size;
  1202. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1203. return 0;
  1204. int status = HTTP_STATUS_NOT_FOUND;
  1205. const xs_dict *args = xs_dict_get(req, "q_vars");
  1206. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1207. snac snac1 = {0};
  1208. int logged_in = process_auth_token(&snac1, req);
  1209. if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) { /** **/
  1210. if (logged_in) {
  1211. credentials_get(body, ctype, &status, snac1);
  1212. }
  1213. else {
  1214. status = HTTP_STATUS_UNPROCESSABLE_CONTENT; // (no login)
  1215. }
  1216. }
  1217. else
  1218. if (strcmp(cmd, "/v1/accounts/relationships") == 0) { /** **/
  1219. /* find if an account is followed, blocked, etc. */
  1220. /* the account to get relationships about is in args "id[]" */
  1221. if (logged_in) {
  1222. xs *res = xs_list_new();
  1223. const char *md5 = xs_dict_get(args, "id[]");
  1224. if (xs_is_null(md5))
  1225. md5 = xs_dict_get(args, "id");
  1226. if (!xs_is_null(md5)) {
  1227. if (xs_type(md5) == XSTYPE_LIST)
  1228. md5 = xs_list_get(md5, 0);
  1229. xs *rel = mastoapi_relationship(&snac1, md5);
  1230. if (rel != NULL)
  1231. res = xs_list_append(res, rel);
  1232. }
  1233. *body = xs_json_dumps(res, 4);
  1234. *ctype = "application/json";
  1235. status = HTTP_STATUS_OK;
  1236. }
  1237. else
  1238. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  1239. }
  1240. else
  1241. if (strcmp(cmd, "/v1/accounts/lookup") == 0) { /** **/
  1242. /* lookup an account */
  1243. const char *acct = xs_dict_get(args, "acct");
  1244. if (!xs_is_null(acct)) {
  1245. xs *s = xs_strip_chars_i(xs_dup(acct), "@");
  1246. xs *l = xs_split_n(s, "@", 1);
  1247. const char *uid = xs_list_get(l, 0);
  1248. const char *host = xs_list_get(l, 1);
  1249. if (uid && (!host || strcmp(host, xs_dict_get(srv_config, "host")) == 0)) {
  1250. snac user;
  1251. if (user_open(&user, uid)) {
  1252. xs *actor = msg_actor(&user);
  1253. xs *macct = mastoapi_account(NULL, actor);
  1254. *body = xs_json_dumps(macct, 4);
  1255. *ctype = "application/json";
  1256. status = HTTP_STATUS_OK;
  1257. user_free(&user);
  1258. }
  1259. }
  1260. }
  1261. }
  1262. else
  1263. if (xs_startswith(cmd, "/v1/accounts/")) { /** **/
  1264. /* account-related information */
  1265. xs *l = xs_split(cmd, "/");
  1266. const char *uid = xs_list_get(l, 3);
  1267. const char *opt = xs_list_get(l, 4);
  1268. if (uid != NULL) {
  1269. snac snac2;
  1270. xs *out = NULL;
  1271. xs *actor = NULL;
  1272. if (logged_in && strcmp(uid, "search") == 0) { /** **/
  1273. /* search for accounts starting with q */
  1274. const char *aq = xs_dict_get(args, "q");
  1275. if (!xs_is_null(aq)) {
  1276. xs *q = xs_utf8_to_lower(aq);
  1277. out = xs_list_new();
  1278. xs *wing = following_list(&snac1);
  1279. xs *wers = follower_list(&snac1);
  1280. xs *ulst = user_list();
  1281. xs_list *p;
  1282. const xs_str *v;
  1283. xs_set seen;
  1284. xs_set_init(&seen);
  1285. /* user relations */
  1286. xs_list *lsts[] = { wing, wers, NULL };
  1287. int n;
  1288. for (n = 0; (p = lsts[n]) != NULL; n++) {
  1289. while (xs_list_iter(&p, &v)) {
  1290. /* already seen? skip */
  1291. if (xs_set_add(&seen, v) == 0)
  1292. continue;
  1293. xs *actor = NULL;
  1294. if (valid_status(object_get(v, &actor))) {
  1295. const char *uname = xs_dict_get(actor, "preferredUsername");
  1296. if (!xs_is_null(uname)) {
  1297. xs *luname = xs_tolower_i(xs_dup(uname));
  1298. if (xs_startswith(luname, q)) {
  1299. xs *acct = mastoapi_account(&snac1, actor);
  1300. out = xs_list_append(out, acct);
  1301. }
  1302. }
  1303. }
  1304. }
  1305. }
  1306. /* local users */
  1307. p = ulst;
  1308. while (xs_list_iter(&p, &v)) {
  1309. snac user;
  1310. /* skip this same user */
  1311. if (strcmp(v, xs_dict_get(snac1.config, "uid")) == 0)
  1312. continue;
  1313. /* skip if the uid does not start with the query */
  1314. xs *v2 = xs_tolower_i(xs_dup(v));
  1315. if (!xs_startswith(v2, q))
  1316. continue;
  1317. if (user_open(&user, v)) {
  1318. /* if it's not already seen, add it */
  1319. if (xs_set_add(&seen, user.actor) == 1) {
  1320. xs *actor = msg_actor(&user);
  1321. xs *acct = mastoapi_account(&snac1, actor);
  1322. out = xs_list_append(out, acct);
  1323. }
  1324. user_free(&user);
  1325. }
  1326. }
  1327. xs_set_free(&seen);
  1328. }
  1329. }
  1330. else
  1331. /* is it a local user? */
  1332. if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
  1333. if (opt == NULL) {
  1334. /* account information */
  1335. actor = msg_actor(&snac2);
  1336. out = mastoapi_account(NULL, actor);
  1337. }
  1338. else
  1339. if (strcmp(opt, "statuses") == 0) { /** **/
  1340. /* the public list of posts of a user */
  1341. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256, NULL);
  1342. xs_list *p = timeline;
  1343. const xs_str *v;
  1344. out = xs_list_new();
  1345. while (xs_list_iter(&p, &v)) {
  1346. xs *msg = NULL;
  1347. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  1348. /* add only posts by the author */
  1349. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  1350. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  1351. xs *st = mastoapi_status(&snac2, msg);
  1352. if (st)
  1353. out = xs_list_append(out, st);
  1354. }
  1355. }
  1356. }
  1357. }
  1358. else
  1359. if (strcmp(opt, "featured_tags") == 0) {
  1360. /* snac doesn't have features tags, yet? */
  1361. /* implement empty response so apps like Tokodon don't show an error */
  1362. out = xs_list_new();
  1363. }
  1364. else
  1365. if (strcmp(opt, "following") == 0) {
  1366. xs *wing = following_list(&snac1);
  1367. out = xs_list_new();
  1368. int c = 0;
  1369. const char *v;
  1370. while (xs_list_next(wing, &v, &c)) {
  1371. xs *actor = NULL;
  1372. if (valid_status(object_get(v, &actor))) {
  1373. xs *acct = mastoapi_account(NULL, actor);
  1374. out = xs_list_append(out, acct);
  1375. }
  1376. }
  1377. }
  1378. else
  1379. if (strcmp(opt, "followers") == 0) {
  1380. out = xs_list_new();
  1381. }
  1382. else
  1383. if (strcmp(opt, "lists") == 0) {
  1384. out = mastoapi_account_lists(&snac1, uid);
  1385. }
  1386. user_free(&snac2);
  1387. }
  1388. else {
  1389. /* try the uid as the md5 of a possibly loaded actor */
  1390. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  1391. if (opt == NULL) {
  1392. /* account information */
  1393. out = mastoapi_account(&snac1, actor);
  1394. }
  1395. else
  1396. if (strcmp(opt, "statuses") == 0) {
  1397. /* we don't serve statuses of others; return the empty list */
  1398. out = xs_list_new();
  1399. }
  1400. else
  1401. if (strcmp(opt, "featured_tags") == 0) {
  1402. /* snac doesn't have features tags, yet? */
  1403. /* implement empty response so apps like Tokodon don't show an error */
  1404. out = xs_list_new();
  1405. }
  1406. else
  1407. if (strcmp(opt, "lists") == 0) {
  1408. out = mastoapi_account_lists(&snac1, uid);
  1409. }
  1410. }
  1411. }
  1412. if (out != NULL) {
  1413. *body = xs_json_dumps(out, 4);
  1414. *ctype = "application/json";
  1415. status = HTTP_STATUS_OK;
  1416. }
  1417. }
  1418. }
  1419. else
  1420. if (strcmp(cmd, "/v1/timelines/home") == 0) { /** **/
  1421. /* the private timeline */
  1422. if (logged_in) {
  1423. xs *ifn = user_index_fn(&snac1, "private");
  1424. xs *out = mastoapi_timeline(&snac1, args, ifn);
  1425. *link = timeline_link_header("/api/v1/timelines/home", out);
  1426. *body = xs_json_dumps(out, 4);
  1427. *ctype = "application/json";
  1428. status = HTTP_STATUS_OK;
  1429. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  1430. }
  1431. else {
  1432. status = HTTP_STATUS_UNAUTHORIZED;
  1433. }
  1434. }
  1435. else
  1436. if (strcmp(cmd, "/v1/timelines/public") == 0) { /** **/
  1437. /* the instance public timeline (public timelines for all users) */
  1438. xs *ifn = instance_index_fn();
  1439. xs *out = mastoapi_timeline(NULL, args, ifn);
  1440. *body = xs_json_dumps(out, 4);
  1441. *ctype = "application/json";
  1442. status = HTTP_STATUS_OK;
  1443. }
  1444. else
  1445. if (xs_startswith(cmd, "/v1/timelines/tag/")) { /** **/
  1446. /* get the tag */
  1447. xs *l = xs_split(cmd, "/");
  1448. const char *tag = xs_list_get(l, -1);
  1449. xs *ifn = tag_fn(tag);
  1450. xs *out = mastoapi_timeline(NULL, args, ifn);
  1451. *body = xs_json_dumps(out, 4);
  1452. *ctype = "application/json";
  1453. status = HTTP_STATUS_OK;
  1454. }
  1455. else
  1456. if (xs_startswith(cmd, "/v1/timelines/list/")) { /** **/
  1457. /* get the list id */
  1458. if (logged_in) {
  1459. xs *l = xs_split(cmd, "/");
  1460. const char *list = xs_list_get(l, -1);
  1461. xs *ifn = list_timeline_fn(&snac1, list);
  1462. xs *out = mastoapi_timeline(NULL, args, ifn);
  1463. *body = xs_json_dumps(out, 4);
  1464. *ctype = "application/json";
  1465. status = HTTP_STATUS_OK;
  1466. }
  1467. else
  1468. status = HTTP_STATUS_MISDIRECTED_REQUEST;
  1469. }
  1470. else
  1471. if (strcmp(cmd, "/v1/conversations") == 0) { /** **/
  1472. /* TBD */
  1473. *body = xs_dup("[]");
  1474. *ctype = "application/json";
  1475. status = HTTP_STATUS_OK;
  1476. }
  1477. else
  1478. if (strcmp(cmd, "/v1/notifications") == 0) { /** **/
  1479. if (logged_in) {
  1480. xs *l = notify_list(&snac1, 0, 64);
  1481. xs *out = xs_list_new();
  1482. const char *v;
  1483. const xs_list *excl = xs_dict_get(args, "exclude_types[]");
  1484. const xs_list *incl = xs_dict_get(args, "types[]");
  1485. const char *min_id = xs_dict_get(args, "min_id");
  1486. const char *max_id = xs_dict_get(args, "max_id");
  1487. const char *limit = xs_dict_get(args, "limit");
  1488. int limit_count = 0;
  1489. if (xs_is_string(limit)) {
  1490. limit_count = atoi(limit);
  1491. }
  1492. if (dbglevel) {
  1493. xs *js = xs_json_dumps(args, 0);
  1494. srv_debug(1, xs_fmt("mastoapi_notifications args %s", js));
  1495. }
  1496. xs_list_foreach(l, v) {
  1497. xs *noti = notify_get(&snac1, v);
  1498. if (noti == NULL)
  1499. continue;
  1500. const char *type = xs_dict_get(noti, "type");
  1501. const char *utype = xs_dict_get(noti, "utype");
  1502. const char *objid = xs_dict_get(noti, "objid");
  1503. const char *id = xs_dict_get(noti, "id");
  1504. const char *actid = xs_dict_get(noti, "actor");
  1505. xs *fid = xs_replace(id, ".", "");
  1506. xs *actor = NULL;
  1507. xs *entry = NULL;
  1508. if (!valid_status(actor_get(actid, &actor)))
  1509. continue;
  1510. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  1511. continue;
  1512. if (is_hidden(&snac1, objid))
  1513. continue;
  1514. if (max_id) {
  1515. if (strcmp(fid, max_id) == 0)
  1516. max_id = NULL;
  1517. continue;
  1518. }
  1519. if (min_id) {
  1520. if (strcmp(fid, min_id) <= 0) {
  1521. continue;
  1522. }
  1523. }
  1524. /* convert the type */
  1525. if (strcmp(type, "Like") == 0 || strcmp(type, "EmojiReact") == 0)
  1526. type = "favourite";
  1527. else
  1528. if (strcmp(type, "Announce") == 0)
  1529. type = "reblog";
  1530. else
  1531. if (strcmp(type, "Follow") == 0)
  1532. type = "follow";
  1533. else
  1534. if (strcmp(type, "Create") == 0)
  1535. type = "mention";
  1536. else
  1537. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  1538. type = "poll";
  1539. else
  1540. continue;
  1541. /* excluded type? */
  1542. if (xs_is_list(excl) && xs_list_in(excl, type) != -1)
  1543. continue;
  1544. /* included type? */
  1545. if (xs_is_list(incl) && xs_list_in(incl, type) == -1)
  1546. continue;
  1547. xs *mn = xs_dict_new();
  1548. mn = xs_dict_append(mn, "type", type);
  1549. mn = xs_dict_append(mn, "id", fid);
  1550. mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
  1551. xs *acct = mastoapi_account(&snac1, actor);
  1552. if (acct == NULL)
  1553. continue;
  1554. mn = xs_dict_append(mn, "account", acct);
  1555. if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
  1556. xs *st = mastoapi_status(&snac1, entry);
  1557. if (st)
  1558. mn = xs_dict_append(mn, "status", st);
  1559. }
  1560. out = xs_list_append(out, mn);
  1561. if (--limit_count <= 0)
  1562. break;
  1563. }
  1564. srv_debug(1, xs_fmt("mastoapi_notifications count %d", xs_list_len(out)));
  1565. *body = xs_json_dumps(out, 4);
  1566. *ctype = "application/json";
  1567. status = HTTP_STATUS_OK;
  1568. }
  1569. else
  1570. status = HTTP_STATUS_UNAUTHORIZED;
  1571. }
  1572. else
  1573. if (strcmp(cmd, "/v1/filters") == 0) { /** **/
  1574. /* snac will never have filters */
  1575. *body = xs_dup("[]");
  1576. *ctype = "application/json";
  1577. status = HTTP_STATUS_OK;
  1578. }
  1579. else
  1580. if (strcmp(cmd, "/v2/filters") == 0) { /** **/
  1581. /* snac will never have filters
  1582. * but still, without a v2 endpoint a short delay is introduced
  1583. * in some apps */
  1584. *body = xs_dup("[]");
  1585. *ctype = "application/json";
  1586. status = HTTP_STATUS_OK;
  1587. }
  1588. else
  1589. if (strcmp(cmd, "/v1/favourites") == 0) { /** **/
  1590. /* snac will never support a list of favourites */
  1591. *body = xs_dup("[]");
  1592. *ctype = "application/json";
  1593. status = HTTP_STATUS_OK;
  1594. }
  1595. else
  1596. if (strcmp(cmd, "/v1/bookmarks") == 0) { /** **/
  1597. if (logged_in) {
  1598. xs *ifn = bookmark_index_fn(&snac1);
  1599. xs *out = mastoapi_timeline(&snac1, args, ifn);
  1600. *body = xs_json_dumps(out, 4);
  1601. *ctype = "application/json";
  1602. status = HTTP_STATUS_OK;
  1603. }
  1604. else
  1605. status = HTTP_STATUS_UNAUTHORIZED;
  1606. }
  1607. else
  1608. if (strcmp(cmd, "/v1/lists") == 0) { /** list of lists **/
  1609. if (logged_in) {
  1610. xs *l = mastoapi_account_lists(&snac1, NULL);
  1611. *body = xs_json_dumps(l, 4);
  1612. *ctype = "application/json";
  1613. status = HTTP_STATUS_OK;
  1614. }
  1615. else
  1616. status = HTTP_STATUS_UNAUTHORIZED;
  1617. }
  1618. else
  1619. if (xs_startswith(cmd, "/v1/lists/")) { /** list information **/
  1620. if (logged_in) {
  1621. xs *l = xs_split(cmd, "/");
  1622. const char *p = xs_list_get(l, -1);
  1623. if (p) {
  1624. if (strcmp(p, "accounts") == 0) {
  1625. p = xs_list_get(l, -2);
  1626. if (p && xs_is_hex(p)) {
  1627. xs *actors = list_content(&snac1, p, NULL, 0);
  1628. xs *out = xs_list_new();
  1629. int c = 0;
  1630. const char *v;
  1631. while (xs_list_next(actors, &v, &c)) {
  1632. xs *actor = NULL;
  1633. if (valid_status(object_get_by_md5(v, &actor))) {
  1634. xs *acct = mastoapi_account(&snac1, actor);
  1635. out = xs_list_append(out, acct);
  1636. }
  1637. }
  1638. *body = xs_json_dumps(out, 4);
  1639. *ctype = "application/json";
  1640. status = HTTP_STATUS_OK;
  1641. }
  1642. }
  1643. else
  1644. if (xs_is_hex(p)) {
  1645. xs *out = xs_list_new();
  1646. xs *lol = list_maint(&snac1, NULL, 0);
  1647. int c = 0;
  1648. const xs_list *v;
  1649. while (xs_list_next(lol, &v, &c)) {
  1650. const char *id = xs_list_get(v, 0);
  1651. if (id && strcmp(id, p) == 0) {
  1652. xs *d = xs_dict_new();
  1653. d = xs_dict_append(d, "id", p);
  1654. d = xs_dict_append(d, "title", xs_list_get(v, 1));
  1655. d = xs_dict_append(d, "replies_policy", "list");
  1656. d = xs_dict_append(d, "exclusive", xs_stock(XSTYPE_FALSE));
  1657. out = xs_dup(d);
  1658. break;
  1659. }
  1660. }
  1661. *body = xs_json_dumps(out, 4);
  1662. *ctype = "application/json";
  1663. status = HTTP_STATUS_OK;
  1664. }
  1665. }
  1666. }
  1667. else
  1668. status = HTTP_STATUS_UNAUTHORIZED;
  1669. }
  1670. else
  1671. if (strcmp(cmd, "/v1/scheduled_statuses") == 0) { /** **/
  1672. /* snac does not schedule notes */
  1673. *body = xs_dup("[]");
  1674. *ctype = "application/json";
  1675. status = HTTP_STATUS_OK;
  1676. }
  1677. else
  1678. if (strcmp(cmd, "/v1/follow_requests") == 0) { /** **/
  1679. if (logged_in) {
  1680. xs *pend = pending_list(&snac1);
  1681. xs *resp = xs_list_new();
  1682. const char *id;
  1683. xs_list_foreach(pend, id) {
  1684. xs *actor = NULL;
  1685. if (valid_status(object_get(id, &actor))) {
  1686. xs *acct = mastoapi_account(&snac1, actor);
  1687. if (acct)
  1688. resp = xs_list_append(resp, acct);
  1689. }
  1690. }
  1691. *body = xs_json_dumps(resp, 4);
  1692. *ctype = "application/json";
  1693. status = HTTP_STATUS_OK;
  1694. }
  1695. }
  1696. else
  1697. if (strcmp(cmd, "/v1/announcements") == 0) { /** **/
  1698. if (logged_in) {
  1699. xs *resp = xs_list_new();
  1700. double la = 0.0;
  1701. xs *user_la = xs_dup(xs_dict_get(snac1.config, "last_announcement"));
  1702. if (user_la != NULL)
  1703. la = xs_number_get(user_la);
  1704. xs *val_date = xs_str_utctime(la, ISO_DATE_SPEC);
  1705. /* contrary to html, we always send the announcement and set the read flag instead */
  1706. const t_announcement *annce = announcement(la);
  1707. if (annce != NULL && annce->text != NULL) {
  1708. xs *an = xs_dict_new();
  1709. xs *id = xs_fmt("%d", annce->timestamp);
  1710. xs *ct = xs_fmt("<p>%s</p>", annce->text);
  1711. an = xs_dict_set(an, "id", id);
  1712. an = xs_dict_set(an, "content", ct);
  1713. an = xs_dict_set(an, "starts_at", xs_stock(XSTYPE_NULL));
  1714. an = xs_dict_set(an, "ends_at", xs_stock(XSTYPE_NULL));
  1715. an = xs_dict_set(an, "all_day", xs_stock(XSTYPE_TRUE));
  1716. an = xs_dict_set(an, "published_at", val_date);
  1717. an = xs_dict_set(an, "updated_at", val_date);
  1718. an = xs_dict_set(an, "read", (annce->timestamp >= la)
  1719. ? xs_stock(XSTYPE_FALSE) : xs_stock(XSTYPE_TRUE));
  1720. an = xs_dict_set(an, "mentions", xs_stock(XSTYPE_LIST));
  1721. an = xs_dict_set(an, "statuses", xs_stock(XSTYPE_LIST));
  1722. an = xs_dict_set(an, "tags", xs_stock(XSTYPE_LIST));
  1723. an = xs_dict_set(an, "emojis", xs_stock(XSTYPE_LIST));
  1724. an = xs_dict_set(an, "reactions", xs_stock(XSTYPE_LIST));
  1725. resp = xs_list_append(resp, an);
  1726. }
  1727. *body = xs_json_dumps(resp, 4);
  1728. *ctype = "application/json";
  1729. status = HTTP_STATUS_OK;
  1730. }
  1731. }
  1732. else
  1733. if (strcmp(cmd, "/v1/custom_emojis") == 0) { /** **/
  1734. xs *emo = emojis();
  1735. xs *list = xs_list_new();
  1736. int c = 0;
  1737. const xs_str *k;
  1738. const xs_val *v;
  1739. while(xs_dict_next(emo, &k, &v, &c)) {
  1740. xs *current = xs_dict_new();
  1741. if (xs_startswith(v, "https://") && xs_startswith((xs_mime_by_ext(v)), "image/")) {
  1742. /* remove first and last colon */
  1743. xs *shortcode = xs_replace(k, ":", "");
  1744. current = xs_dict_append(current, "shortcode", shortcode);
  1745. current = xs_dict_append(current, "url", v);
  1746. current = xs_dict_append(current, "static_url", v);
  1747. current = xs_dict_append(current, "visible_in_picker", xs_stock(XSTYPE_TRUE));
  1748. list = xs_list_append(list, current);
  1749. }
  1750. }
  1751. *body = xs_json_dumps(list, 0);
  1752. *ctype = "application/json";
  1753. status = HTTP_STATUS_OK;
  1754. }
  1755. else
  1756. if (strcmp(cmd, "/v1/instance") == 0) { /** **/
  1757. /* returns an instance object */
  1758. xs *ins = xs_dict_new();
  1759. const char *host = xs_dict_get(srv_config, "host");
  1760. const char *title = xs_dict_get(srv_config, "title");
  1761. const char *sdesc = xs_dict_get(srv_config, "short_description");
  1762. ins = xs_dict_append(ins, "uri", host);
  1763. ins = xs_dict_append(ins, "domain", host);
  1764. ins = xs_dict_append(ins, "title", title && *title ? title : host);
  1765. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  1766. ins = xs_dict_append(ins, "source_url", WHAT_IS_SNAC_URL);
  1767. ins = xs_dict_append(ins, "description", host);
  1768. ins = xs_dict_append(ins, "short_description", sdesc && *sdesc ? sdesc : host);
  1769. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  1770. ins = xs_dict_append(ins, "thumbnail", susie);
  1771. const char *v = xs_dict_get(srv_config, "admin_email");
  1772. if (xs_is_null(v) || *v == '\0')
  1773. v = "admin@localhost";
  1774. ins = xs_dict_append(ins, "email", v);
  1775. ins = xs_dict_append(ins, "rules", xs_stock(XSTYPE_LIST));
  1776. xs *l1 = xs_list_append(xs_list_new(), "en");
  1777. ins = xs_dict_append(ins, "languages", l1);
  1778. xs *wss = xs_fmt("wss:/" "/%s", xs_dict_get(srv_config, "host"));
  1779. xs *urls = xs_dict_new();
  1780. urls = xs_dict_append(urls, "streaming_api", wss);
  1781. ins = xs_dict_append(ins, "urls", urls);
  1782. xs *d2 = xs_dict_append(xs_dict_new(), "user_count", xs_stock(0));
  1783. d2 = xs_dict_append(d2, "status_count", xs_stock(0));
  1784. d2 = xs_dict_append(d2, "domain_count", xs_stock(0));
  1785. ins = xs_dict_append(ins, "stats", d2);
  1786. ins = xs_dict_append(ins, "registrations", xs_stock(XSTYPE_FALSE));
  1787. ins = xs_dict_append(ins, "approval_required", xs_stock(XSTYPE_FALSE));
  1788. ins = xs_dict_append(ins, "invites_enabled", xs_stock(XSTYPE_FALSE));
  1789. xs *cfg = xs_dict_new();
  1790. {
  1791. xs *d11 = xs_json_loads("{\"characters_reserved_per_url\":32,"
  1792. "\"max_characters\":100000,\"max_media_attachments\":4}");
  1793. const xs_number *max_attachments = xs_dict_get(srv_config, "max_attachments");
  1794. if (xs_type(max_attachments) == XSTYPE_NUMBER)
  1795. d11 = xs_dict_set(d11, "max_media_attachments", max_attachments);
  1796. cfg = xs_dict_append(cfg, "statuses", d11);
  1797. xs *d12 = xs_json_loads("{\"max_featured_tags\":0}");
  1798. cfg = xs_dict_append(cfg, "accounts", d12);
  1799. xs *d13 = xs_json_loads("{\"image_matrix_limit\":33177600,"
  1800. "\"image_size_limit\":16777216,"
  1801. "\"video_frame_rate_limit\":120,"
  1802. "\"video_matrix_limit\":8294400,"
  1803. "\"video_size_limit\":103809024}"
  1804. );
  1805. {
  1806. /* get the supported mime types from the internal list */
  1807. const char **p = xs_mime_types;
  1808. xs_set mtypes;
  1809. xs_set_init(&mtypes);
  1810. while (*p) {
  1811. const char *type = p[1];
  1812. if (xs_startswith(type, "image/") ||
  1813. xs_startswith(type, "video/") ||
  1814. xs_startswith(type, "audio/"))
  1815. xs_set_add(&mtypes, type);
  1816. p += 2;
  1817. }
  1818. xs *l = xs_set_result(&mtypes);
  1819. d13 = xs_dict_append(d13, "supported_mime_types", l);
  1820. }
  1821. cfg = xs_dict_append(cfg, "media_attachments", d13);
  1822. xs *d14 = xs_json_loads("{\"max_characters_per_option\":50,"
  1823. "\"max_expiration\":2629746,"
  1824. "\"max_options\":8,\"min_expiration\":300}");
  1825. cfg = xs_dict_append(cfg, "polls", d14);
  1826. }
  1827. ins = xs_dict_append(ins, "configuration", cfg);
  1828. const char *admin_account = xs_dict_get(srv_config, "admin_account");
  1829. if (!xs_is_null(admin_account) && *admin_account) {
  1830. snac admin;
  1831. if (user_open(&admin, admin_account)) {
  1832. xs *actor = msg_actor(&admin);
  1833. xs *acct = mastoapi_account(NULL, actor);
  1834. ins = xs_dict_append(ins, "contact_account", acct);
  1835. user_free(&admin);
  1836. }
  1837. }
  1838. *body = xs_json_dumps(ins, 4);
  1839. *ctype = "application/json";
  1840. status = HTTP_STATUS_OK;
  1841. }
  1842. else
  1843. if (strcmp(cmd, "/v1/instance/peers") == 0) { /** **/
  1844. /* get the collected inbox list as the instances "this domain is aware of" */
  1845. xs *list = inbox_list();
  1846. xs *peers = xs_list_new();
  1847. const char *inbox;
  1848. xs_list_foreach(list, inbox) {
  1849. xs *l = xs_split(inbox, "/");
  1850. const char *domain = xs_list_get(l, 2);
  1851. if (xs_is_string(domain))
  1852. peers = xs_list_append(peers, domain);
  1853. }
  1854. *body = xs_json_dumps(peers, 4);
  1855. *ctype = "application/json";
  1856. status = HTTP_STATUS_OK;
  1857. }
  1858. else
  1859. if (xs_startswith(cmd, "/v1/statuses/")) { /** **/
  1860. /* information about a status */
  1861. if (logged_in) {
  1862. xs *l = xs_split(cmd, "/");
  1863. const char *id = xs_list_get(l, 3);
  1864. const char *op = xs_list_get(l, 4);
  1865. if (!xs_is_null(id)) {
  1866. xs *msg = NULL;
  1867. xs *out = NULL;
  1868. /* skip the 'fake' part of the id */
  1869. id = MID_TO_MD5(id);
  1870. if (valid_status(object_get_by_md5(id, &msg))) {
  1871. if (op == NULL) {
  1872. if (!is_muted(&snac1, get_atto(msg))) {
  1873. /* return the status itself */
  1874. out = mastoapi_status(&snac1, msg);
  1875. }
  1876. }
  1877. else
  1878. if (strcmp(op, "context") == 0) { /** **/
  1879. /* return ancestors and children */
  1880. xs *anc = xs_list_new();
  1881. xs *des = xs_list_new();
  1882. xs_list *p;
  1883. const xs_str *v;
  1884. char pid[MD5_HEX_SIZE];
  1885. /* build the [grand]parent list, moving up */
  1886. strncpy(pid, id, sizeof(pid));
  1887. while (object_parent(pid, pid)) {
  1888. xs *m2 = NULL;
  1889. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  1890. xs *st = mastoapi_status(&snac1, m2);
  1891. if (st)
  1892. anc = xs_list_insert(anc, 0, st);
  1893. }
  1894. else
  1895. break;
  1896. }
  1897. /* build the children list */
  1898. xs *children = object_children(xs_dict_get(msg, "id"));
  1899. p = children;
  1900. while (xs_list_iter(&p, &v)) {
  1901. xs *m2 = NULL;
  1902. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  1903. if (xs_is_null(xs_dict_get(m2, "name"))) {
  1904. xs *st = mastoapi_status(&snac1, m2);
  1905. if (st)
  1906. des = xs_list_append(des, st);
  1907. }
  1908. }
  1909. }
  1910. out = xs_dict_new();
  1911. out = xs_dict_append(out, "ancestors", anc);
  1912. out = xs_dict_append(out, "descendants", des);
  1913. }
  1914. else
  1915. if (strcmp(op, "reblogged_by") == 0 || /** **/
  1916. strcmp(op, "favourited_by") == 0) { /** **/
  1917. /* return the list of people who liked or boosted this */
  1918. out = xs_list_new();
  1919. xs *l = NULL;
  1920. if (op[0] == 'r')
  1921. l = object_announces(xs_dict_get(msg, "id"));
  1922. else
  1923. l = object_likes(xs_dict_get(msg, "id"));
  1924. xs_list *p = l;
  1925. const xs_str *v;
  1926. while (xs_list_iter(&p, &v)) {
  1927. xs *actor2 = NULL;
  1928. if (valid_status(object_get_by_md5(v, &actor2))) {
  1929. xs *acct2 = mastoapi_account(&snac1, actor2);
  1930. out = xs_list_append(out, acct2);
  1931. }
  1932. }
  1933. }
  1934. else
  1935. if (strcmp(op, "source") == 0) { /** **/
  1936. out = xs_dict_new();
  1937. /* get the mastoapi status id */
  1938. out = xs_dict_append(out, "id", xs_list_get(l, 3));
  1939. out = xs_dict_append(out, "text", xs_dict_get(msg, "sourceContent"));
  1940. out = xs_dict_append(out, "spoiler_text", xs_dict_get(msg, "summary"));
  1941. }
  1942. }
  1943. else
  1944. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  1945. if (out != NULL) {
  1946. *body = xs_json_dumps(out, 4);
  1947. *ctype = "application/json";
  1948. status = HTTP_STATUS_OK;
  1949. }
  1950. }
  1951. }
  1952. else
  1953. status = HTTP_STATUS_UNAUTHORIZED;
  1954. }
  1955. else
  1956. if (strcmp(cmd, "/v1/preferences") == 0) { /** **/
  1957. *body = xs_dup("{}");
  1958. *ctype = "application/json";
  1959. status = HTTP_STATUS_OK;
  1960. }
  1961. else
  1962. if (strcmp(cmd, "/v1/markers") == 0) { /** **/
  1963. if (logged_in) {
  1964. const xs_list *timeline = xs_dict_get(args, "timeline[]");
  1965. xs_str *json = NULL;
  1966. if (!xs_is_null(timeline))
  1967. json = xs_json_dumps(markers_get(&snac1, timeline), 4);
  1968. if (!xs_is_null(json))
  1969. *body = json;
  1970. else
  1971. *body = xs_dup("{}");
  1972. *ctype = "application/json";
  1973. status = HTTP_STATUS_OK;
  1974. }
  1975. else
  1976. status = HTTP_STATUS_UNAUTHORIZED;
  1977. }
  1978. else
  1979. if (strcmp(cmd, "/v1/followed_tags") == 0) { /** **/
  1980. *body = xs_dup("[]");
  1981. *ctype = "application/json";
  1982. status = HTTP_STATUS_OK;
  1983. }
  1984. else
  1985. if (strcmp(cmd, "/v1/trends/tags") == 0) { /** **/
  1986. *body = xs_dup("[]");
  1987. *ctype = "application/json";
  1988. status = HTTP_STATUS_OK;
  1989. }
  1990. else
  1991. if (strcmp(cmd, "/v1/trends/statuses") == 0) { /** **/
  1992. *body = xs_dup("[]");
  1993. *ctype = "application/json";
  1994. status = HTTP_STATUS_OK;
  1995. }
  1996. else
  1997. if (strcmp(cmd, "/v2/search") == 0) { /** **/
  1998. if (logged_in) {
  1999. const char *q = xs_dict_get(args, "q");
  2000. const char *type = xs_dict_get(args, "type");
  2001. const char *offset = xs_dict_get(args, "offset");
  2002. xs *acl = xs_list_new();
  2003. xs *stl = xs_list_new();
  2004. xs *htl = xs_list_new();
  2005. xs *res = xs_dict_new();
  2006. if (xs_is_null(offset) || strcmp(offset, "0") == 0) {
  2007. /* reply something only for offset 0; otherwise,
  2008. apps like Tusky keep asking again and again */
  2009. if (xs_startswith(q, "https://")) {
  2010. if (!timeline_here(&snac1, q)) {
  2011. xs *object = NULL;
  2012. int status;
  2013. status = activitypub_request(&snac1, q, &object);
  2014. snac_debug(&snac1, 1, xs_fmt("Request searched URL %s %d", q, status));
  2015. if (valid_status(status)) {
  2016. /* got it; also request the actor */
  2017. const char *attr_to = get_atto(object);
  2018. xs *actor_obj = NULL;
  2019. if (!xs_is_null(attr_to)) {
  2020. status = actor_request(&snac1, attr_to, &actor_obj);
  2021. snac_debug(&snac1, 1, xs_fmt("Request author %s of %s %d", attr_to, q, status));
  2022. if (valid_status(status)) {
  2023. /* add the actor */
  2024. actor_add(attr_to, actor_obj);
  2025. /* add the post to the timeline */
  2026. timeline_add(&snac1, q, object);
  2027. }
  2028. }
  2029. }
  2030. }
  2031. }
  2032. if (!xs_is_null(q)) {
  2033. if (xs_is_null(type) || strcmp(type, "accounts") == 0) {
  2034. /* do a webfinger query */
  2035. char *actor = NULL;
  2036. char *user = NULL;
  2037. if (valid_status(webfinger_request(q, &actor, &user)) && actor) {
  2038. xs *actor_o = NULL;
  2039. if (valid_status(actor_request(&snac1, actor, &actor_o))) {
  2040. xs *acct = mastoapi_account(NULL, actor_o);
  2041. acl = xs_list_append(acl, acct);
  2042. if (!object_here(actor))
  2043. object_add(actor, actor_o);
  2044. }
  2045. }
  2046. }
  2047. if (xs_is_null(type) || strcmp(type, "hashtags") == 0) {
  2048. /* search this tag */
  2049. xs *tl = tag_search((char *)q, 0, 1);
  2050. if (xs_list_len(tl)) {
  2051. xs *d = xs_dict_new();
  2052. d = xs_dict_append(d, "name", q);
  2053. xs *url = xs_fmt("%s?t=%s", srv_baseurl, q);
  2054. d = xs_dict_append(d, "url", url);
  2055. d = xs_dict_append(d, "history", xs_stock(XSTYPE_LIST));
  2056. htl = xs_list_append(htl, d);
  2057. }
  2058. }
  2059. if (xs_is_null(type) || strcmp(type, "statuses") == 0) {
  2060. int to = 0;
  2061. int cnt = 40;
  2062. xs *tl = content_search(&snac1, q, 1, 0, cnt, 0, &to);
  2063. int c = 0;
  2064. const char *v;
  2065. while (xs_list_next(tl, &v, &c) && --cnt) {
  2066. xs *post = NULL;
  2067. if (!valid_status(timeline_get_by_md5(&snac1, v, &post)))
  2068. continue;
  2069. xs *s = mastoapi_status(&snac1, post);
  2070. if (!xs_is_null(s))
  2071. stl = xs_list_append(stl, s);
  2072. }
  2073. }
  2074. }
  2075. }
  2076. res = xs_dict_append(res, "accounts", acl);
  2077. res = xs_dict_append(res, "statuses", stl);
  2078. res = xs_dict_append(res, "hashtags", htl);
  2079. *body = xs_json_dumps(res, 4);
  2080. *ctype = "application/json";
  2081. status = HTTP_STATUS_OK;
  2082. }
  2083. else
  2084. status = HTTP_STATUS_UNAUTHORIZED;
  2085. }
  2086. /* user cleanup */
  2087. if (logged_in)
  2088. user_free(&snac1);
  2089. srv_debug(1, xs_fmt("mastoapi_get_handler %s %d", q_path, status));
  2090. return status;
  2091. }
  2092. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  2093. const char *payload, int p_size,
  2094. char **body, int *b_size, char **ctype)
  2095. {
  2096. (void)p_size;
  2097. (void)b_size;
  2098. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  2099. return 0;
  2100. int status = HTTP_STATUS_NOT_FOUND;
  2101. xs *args = NULL;
  2102. const char *i_ctype = xs_dict_get(req, "content-type");
  2103. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  2104. if (!xs_is_null(payload))
  2105. args = xs_json_loads(payload);
  2106. }
  2107. else if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded"))
  2108. {
  2109. // Some apps send form data instead of json so we should cater for those
  2110. if (!xs_is_null(payload)) {
  2111. args = xs_url_vars(payload);
  2112. }
  2113. }
  2114. else
  2115. args = xs_dup(xs_dict_get(req, "p_vars"));
  2116. if (args == NULL)
  2117. return HTTP_STATUS_BAD_REQUEST;
  2118. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2119. snac snac = {0};
  2120. int logged_in = process_auth_token(&snac, req);
  2121. if (strcmp(cmd, "/v1/apps") == 0) { /** **/
  2122. const char *name = xs_dict_get(args, "client_name");
  2123. const char *ruri = xs_dict_get(args, "redirect_uris");
  2124. const char *scope = xs_dict_get(args, "scope");
  2125. /* Ice Cubes sends these values as query parameters, so try these */
  2126. if (name == NULL && ruri == NULL && scope == NULL) {
  2127. args = xs_dup(xs_dict_get(req, "q_vars"));
  2128. name = xs_dict_get(args, "client_name");
  2129. ruri = xs_dict_get(args, "redirect_uris");
  2130. scope = xs_dict_get(args, "scope");
  2131. }
  2132. if (xs_type(ruri) == XSTYPE_LIST)
  2133. ruri = xs_dict_get(ruri, 0);
  2134. if (name && ruri) {
  2135. xs *app = xs_dict_new();
  2136. xs *id = xs_replace_i(tid(0), ".", "");
  2137. xs *csec = random_str();
  2138. xs *vkey = random_str();
  2139. xs *cid = NULL;
  2140. /* pick a non-existent random cid */
  2141. for (;;) {
  2142. cid = random_str();
  2143. xs *p_app = app_get(cid);
  2144. if (p_app == NULL)
  2145. break;
  2146. xs_free(cid);
  2147. }
  2148. app = xs_dict_append(app, "name", name);
  2149. app = xs_dict_append(app, "redirect_uri", ruri);
  2150. app = xs_dict_append(app, "client_id", cid);
  2151. app = xs_dict_append(app, "client_secret", csec);
  2152. app = xs_dict_append(app, "vapid_key", vkey);
  2153. app = xs_dict_append(app, "id", id);
  2154. *body = xs_json_dumps(app, 4);
  2155. *ctype = "application/json";
  2156. status = HTTP_STATUS_OK;
  2157. app = xs_dict_append(app, "code", "");
  2158. if (scope)
  2159. app = xs_dict_append(app, "scope", scope);
  2160. app_add(cid, app);
  2161. srv_debug(1, xs_fmt("mastoapi apps: new app %s", cid));
  2162. }
  2163. }
  2164. else
  2165. if (strcmp(cmd, "/v1/statuses") == 0) { /** **/
  2166. if (logged_in) {
  2167. /* post a new Note */
  2168. const char *content = xs_dict_get(args, "status");
  2169. const char *mid = xs_dict_get(args, "in_reply_to_id");
  2170. const char *visibility = xs_dict_get(args, "visibility");
  2171. const char *summary = xs_dict_get(args, "spoiler_text");
  2172. const char *media_ids = xs_dict_get(args, "media_ids");
  2173. const char *language = xs_dict_get(args, "language");
  2174. if (xs_is_null(media_ids))
  2175. media_ids = xs_dict_get(args, "media_ids[]");
  2176. if (xs_is_null(media_ids))
  2177. media_ids = xs_dict_get(args, "media_ids");
  2178. if (xs_is_null(visibility))
  2179. visibility = "public";
  2180. xs *attach_list = xs_list_new();
  2181. xs *irt = NULL;
  2182. /* is it a reply? */
  2183. if (mid != NULL) {
  2184. xs *r_msg = NULL;
  2185. const char *md5 = MID_TO_MD5(mid);
  2186. if (valid_status(object_get_by_md5(md5, &r_msg)))
  2187. irt = xs_dup(xs_dict_get(r_msg, "id"));
  2188. }
  2189. /* does it have attachments? */
  2190. if (!xs_is_null(media_ids)) {
  2191. xs *mi = NULL;
  2192. if (xs_type(media_ids) == XSTYPE_LIST)
  2193. mi = xs_dup(media_ids);
  2194. else {
  2195. mi = xs_list_new();
  2196. mi = xs_list_append(mi, media_ids);
  2197. }
  2198. xs_list *p = mi;
  2199. const xs_str *v;
  2200. while (xs_list_iter(&p, &v)) {
  2201. xs *l = xs_list_new();
  2202. xs *url = xs_fmt("%s/s/%s", snac.actor, v);
  2203. xs *desc = static_get_meta(&snac, v);
  2204. l = xs_list_append(l, url);
  2205. l = xs_list_append(l, desc);
  2206. attach_list = xs_list_append(attach_list, l);
  2207. }
  2208. }
  2209. /* prepare the message */
  2210. int scope = 1;
  2211. if (strcmp(visibility, "unlisted") == 0)
  2212. scope = 2;
  2213. else
  2214. if (strcmp(visibility, "public") == 0)
  2215. scope = 0;
  2216. xs *msg = msg_note(&snac, content, NULL, irt, attach_list, scope, language, NULL);
  2217. if (!xs_is_null(summary) && *summary) {
  2218. msg = xs_dict_set(msg, "sensitive", xs_stock(XSTYPE_TRUE));
  2219. msg = xs_dict_set(msg, "summary", summary);
  2220. }
  2221. /* scheduled? */
  2222. const char *scheduled_at = xs_dict_get(args, "scheduled_at");
  2223. if (xs_is_string(scheduled_at) && *scheduled_at) {
  2224. msg = xs_dict_set(msg, "published", scheduled_at);
  2225. schedule_add(&snac, xs_dict_get(msg, "id"), msg);
  2226. }
  2227. else {
  2228. /* store */
  2229. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  2230. /* 'Create' message */
  2231. xs *c_msg = msg_create(&snac, msg);
  2232. enqueue_message(&snac, c_msg);
  2233. timeline_touch(&snac);
  2234. }
  2235. /* convert to a mastodon status as a response code */
  2236. xs *st = mastoapi_status(&snac, msg);
  2237. *body = xs_json_dumps(st, 4);
  2238. *ctype = "application/json";
  2239. status = HTTP_STATUS_OK;
  2240. }
  2241. else
  2242. status = HTTP_STATUS_UNAUTHORIZED;
  2243. }
  2244. else
  2245. if (xs_startswith(cmd, "/v1/statuses")) { /** **/
  2246. if (logged_in) {
  2247. /* operations on a status */
  2248. xs *l = xs_split(cmd, "/");
  2249. const char *mid = xs_list_get(l, 3);
  2250. const char *op = xs_list_get(l, 4);
  2251. if (!xs_is_null(mid)) {
  2252. xs *msg = NULL;
  2253. xs *out = NULL;
  2254. /* skip the 'fake' part of the id */
  2255. mid = MID_TO_MD5(mid);
  2256. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  2257. const char *id = xs_dict_get(msg, "id");
  2258. if (op == NULL) {
  2259. /* no operation (?) */
  2260. }
  2261. else
  2262. if (strcmp(op, "favourite") == 0) { /** **/
  2263. xs *n_msg = msg_admiration(&snac, id, "Like");
  2264. if (n_msg != NULL) {
  2265. enqueue_message(&snac, n_msg);
  2266. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  2267. out = mastoapi_status(&snac, msg);
  2268. }
  2269. }
  2270. else
  2271. if (strcmp(op, "unfavourite") == 0) { /** **/
  2272. xs *n_msg = msg_repulsion(&snac, id, "Like");
  2273. if (n_msg != NULL) {
  2274. enqueue_message(&snac, n_msg);
  2275. out = mastoapi_status(&snac, msg);
  2276. }
  2277. }
  2278. else
  2279. if (strcmp(op, "reblog") == 0) { /** **/
  2280. xs *n_msg = msg_admiration(&snac, id, "Announce");
  2281. if (n_msg != NULL) {
  2282. enqueue_message(&snac, n_msg);
  2283. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  2284. out = mastoapi_status(&snac, msg);
  2285. }
  2286. }
  2287. else
  2288. if (strcmp(op, "unreblog") == 0) { /** **/
  2289. xs *n_msg = msg_repulsion(&snac, id, "Announce");
  2290. if (n_msg != NULL) {
  2291. enqueue_message(&snac, n_msg);
  2292. out = mastoapi_status(&snac, msg);
  2293. }
  2294. }
  2295. else
  2296. if (strcmp(op, "bookmark") == 0) { /** **/
  2297. /* bookmark this message */
  2298. if (bookmark(&snac, id) == 0)
  2299. out = mastoapi_status(&snac, msg);
  2300. else
  2301. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2302. }
  2303. else
  2304. if (strcmp(op, "unbookmark") == 0) { /** **/
  2305. /* unbookmark this message */
  2306. unbookmark(&snac, id);
  2307. out = mastoapi_status(&snac, msg);
  2308. }
  2309. else
  2310. if (strcmp(op, "pin") == 0) { /** **/
  2311. /* pin this message */
  2312. if (pin(&snac, id) == 0)
  2313. out = mastoapi_status(&snac, msg);
  2314. else
  2315. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2316. }
  2317. else
  2318. if (strcmp(op, "unpin") == 0) { /** **/
  2319. /* unpin this message */
  2320. unpin(&snac, id);
  2321. out = mastoapi_status(&snac, msg);
  2322. }
  2323. else
  2324. if (strcmp(op, "mute") == 0) { /** **/
  2325. /* Mastodon's mute is snac's hide */
  2326. }
  2327. else
  2328. if (strcmp(op, "unmute") == 0) { /** **/
  2329. /* Mastodon's unmute is snac's unhide */
  2330. }
  2331. }
  2332. if (out != NULL) {
  2333. *body = xs_json_dumps(out, 4);
  2334. *ctype = "application/json";
  2335. status = HTTP_STATUS_OK;
  2336. }
  2337. }
  2338. }
  2339. else
  2340. status = HTTP_STATUS_UNAUTHORIZED;
  2341. }
  2342. else
  2343. if (strcmp(cmd, "/v1/notifications/clear") == 0) { /** **/
  2344. if (logged_in) {
  2345. notify_clear(&snac);
  2346. timeline_touch(&snac);
  2347. *body = xs_dup("{}");
  2348. *ctype = "application/json";
  2349. status = HTTP_STATUS_OK;
  2350. }
  2351. else
  2352. status = HTTP_STATUS_UNAUTHORIZED;
  2353. }
  2354. else
  2355. if (strcmp(cmd, "/v1/push/subscription") == 0) { /** **/
  2356. /* I don't know what I'm doing */
  2357. if (logged_in) {
  2358. const char *v;
  2359. xs *wpush = xs_dict_new();
  2360. wpush = xs_dict_append(wpush, "id", "1");
  2361. v = xs_dict_get(args, "data");
  2362. v = xs_dict_get(v, "alerts");
  2363. wpush = xs_dict_append(wpush, "alerts", v);
  2364. v = xs_dict_get(args, "subscription");
  2365. v = xs_dict_get(v, "endpoint");
  2366. wpush = xs_dict_append(wpush, "endpoint", v);
  2367. xs *server_key = random_str();
  2368. wpush = xs_dict_append(wpush, "server_key", server_key);
  2369. *body = xs_json_dumps(wpush, 4);
  2370. *ctype = "application/json";
  2371. status = HTTP_STATUS_OK;
  2372. }
  2373. else
  2374. status = HTTP_STATUS_UNAUTHORIZED;
  2375. }
  2376. else
  2377. if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { /** **/
  2378. if (logged_in) {
  2379. const xs_list *file = xs_dict_get(args, "file");
  2380. const char *desc = xs_dict_get(args, "description");
  2381. if (xs_is_null(desc))
  2382. desc = "";
  2383. status = HTTP_STATUS_BAD_REQUEST;
  2384. if (xs_type(file) == XSTYPE_LIST) {
  2385. const char *fn = xs_list_get(file, 0);
  2386. if (*fn != '\0') {
  2387. char *ext = strrchr(fn, '.');
  2388. xs *hash = xs_md5_hex(fn, strlen(fn));
  2389. xs *id = xs_fmt("%s%s", hash, ext);
  2390. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  2391. int fo = xs_number_get(xs_list_get(file, 1));
  2392. int fs = xs_number_get(xs_list_get(file, 2));
  2393. /* store */
  2394. static_put(&snac, id, payload + fo, fs);
  2395. static_put_meta(&snac, id, desc);
  2396. /* prepare a response */
  2397. xs *rsp = xs_dict_new();
  2398. rsp = xs_dict_append(rsp, "id", id);
  2399. rsp = xs_dict_append(rsp, "type", "image");
  2400. rsp = xs_dict_append(rsp, "url", url);
  2401. rsp = xs_dict_append(rsp, "preview_url", url);
  2402. rsp = xs_dict_append(rsp, "remote_url", url);
  2403. rsp = xs_dict_append(rsp, "description", desc);
  2404. *body = xs_json_dumps(rsp, 4);
  2405. *ctype = "application/json";
  2406. status = HTTP_STATUS_OK;
  2407. }
  2408. }
  2409. }
  2410. else
  2411. status = HTTP_STATUS_UNAUTHORIZED;
  2412. }
  2413. else
  2414. if (xs_startswith(cmd, "/v1/accounts")) { /** **/
  2415. if (logged_in) {
  2416. /* account-related information */
  2417. xs *l = xs_split(cmd, "/");
  2418. const char *md5 = xs_list_get(l, 3);
  2419. const char *opt = xs_list_get(l, 4);
  2420. xs *rsp = NULL;
  2421. if (!xs_is_null(md5) && *md5) {
  2422. xs *actor_o = NULL;
  2423. if (xs_is_null(opt)) {
  2424. /* ? */
  2425. }
  2426. else
  2427. if (strcmp(opt, "follow") == 0) { /** **/
  2428. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2429. const char *actor = xs_dict_get(actor_o, "id");
  2430. xs *msg = msg_follow(&snac, actor);
  2431. if (msg != NULL) {
  2432. /* reload the actor from the message, in may be different */
  2433. actor = xs_dict_get(msg, "object");
  2434. following_add(&snac, actor, msg);
  2435. enqueue_output_by_actor(&snac, msg, actor, 0);
  2436. rsp = mastoapi_relationship(&snac, md5);
  2437. }
  2438. }
  2439. }
  2440. else
  2441. if (strcmp(opt, "unfollow") == 0) { /** **/
  2442. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2443. const char *actor = xs_dict_get(actor_o, "id");
  2444. /* get the following object */
  2445. xs *object = NULL;
  2446. if (valid_status(following_get(&snac, actor, &object))) {
  2447. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  2448. following_del(&snac, actor);
  2449. enqueue_output_by_actor(&snac, msg, actor, 0);
  2450. rsp = mastoapi_relationship(&snac, md5);
  2451. }
  2452. }
  2453. }
  2454. else
  2455. if (strcmp(opt, "block") == 0) { /** **/
  2456. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2457. const char *actor = xs_dict_get(actor_o, "id");
  2458. mute(&snac, actor);
  2459. rsp = mastoapi_relationship(&snac, md5);
  2460. }
  2461. }
  2462. else
  2463. if (strcmp(opt, "unblock") == 0) { /** **/
  2464. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2465. const char *actor = xs_dict_get(actor_o, "id");
  2466. unmute(&snac, actor);
  2467. rsp = mastoapi_relationship(&snac, md5);
  2468. }
  2469. }
  2470. }
  2471. if (rsp != NULL) {
  2472. *body = xs_json_dumps(rsp, 4);
  2473. *ctype = "application/json";
  2474. status = HTTP_STATUS_OK;
  2475. }
  2476. }
  2477. else
  2478. status = HTTP_STATUS_UNAUTHORIZED;
  2479. }
  2480. else
  2481. if (xs_startswith(cmd, "/v1/polls")) { /** **/
  2482. if (logged_in) {
  2483. /* operations on a status */
  2484. xs *l = xs_split(cmd, "/");
  2485. const char *mid = xs_list_get(l, 3);
  2486. const char *op = xs_list_get(l, 4);
  2487. if (!xs_is_null(mid)) {
  2488. xs *msg = NULL;
  2489. xs *out = NULL;
  2490. /* skip the 'fake' part of the id */
  2491. mid = MID_TO_MD5(mid);
  2492. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  2493. const char *id = xs_dict_get(msg, "id");
  2494. const char *atto = get_atto(msg);
  2495. const xs_list *opts = xs_dict_get(msg, "oneOf");
  2496. if (opts == NULL)
  2497. opts = xs_dict_get(msg, "anyOf");
  2498. if (op == NULL) {
  2499. }
  2500. else
  2501. if (strcmp(op, "votes") == 0) {
  2502. const xs_list *choices = xs_dict_get(args, "choices[]");
  2503. if (xs_is_null(choices))
  2504. choices = xs_dict_get(args, "choices");
  2505. if (xs_type(choices) == XSTYPE_LIST) {
  2506. const xs_str *v;
  2507. int c = 0;
  2508. while (xs_list_next(choices, &v, &c)) {
  2509. int io = atoi(v);
  2510. const xs_dict *o = xs_list_get(opts, io);
  2511. if (o) {
  2512. const char *name = xs_dict_get(o, "name");
  2513. xs *msg = msg_note(&snac, "", atto, (char *)id, NULL, 1, NULL, NULL);
  2514. msg = xs_dict_append(msg, "name", name);
  2515. xs *c_msg = msg_create(&snac, msg);
  2516. enqueue_message(&snac, c_msg);
  2517. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  2518. }
  2519. }
  2520. out = mastoapi_poll(&snac, msg);
  2521. }
  2522. }
  2523. }
  2524. if (out != NULL) {
  2525. *body = xs_json_dumps(out, 4);
  2526. *ctype = "application/json";
  2527. status = HTTP_STATUS_OK;
  2528. }
  2529. }
  2530. }
  2531. else
  2532. status = HTTP_STATUS_UNAUTHORIZED;
  2533. }
  2534. else
  2535. if (strcmp(cmd, "/v1/lists") == 0) {
  2536. if (logged_in) {
  2537. const char *title = xs_dict_get(args, "title");
  2538. if (xs_type(title) == XSTYPE_STRING) {
  2539. /* add the list */
  2540. xs *out = xs_dict_new();
  2541. xs *lid = list_maint(&snac, title, 1);
  2542. if (!xs_is_null(lid)) {
  2543. out = xs_dict_append(out, "id", lid);
  2544. out = xs_dict_append(out, "title", title);
  2545. out = xs_dict_append(out, "replies_policy", xs_dict_get_def(args, "replies_policy", "list"));
  2546. out = xs_dict_append(out, "exclusive", xs_stock(XSTYPE_FALSE));
  2547. status = HTTP_STATUS_OK;
  2548. }
  2549. else {
  2550. out = xs_dict_append(out, "error", "cannot create list");
  2551. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2552. }
  2553. *body = xs_json_dumps(out, 4);
  2554. *ctype = "application/json";
  2555. }
  2556. else
  2557. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2558. }
  2559. }
  2560. else
  2561. if (xs_startswith(cmd, "/v1/lists/")) { /** list maintenance **/
  2562. if (logged_in) {
  2563. xs *l = xs_split(cmd, "/");
  2564. const char *op = xs_list_get(l, -1);
  2565. const char *id = xs_list_get(l, -2);
  2566. if (op && id && xs_is_hex(id)) {
  2567. if (strcmp(op, "accounts") == 0) {
  2568. const xs_list *accts = xs_dict_get(args, "account_ids[]");
  2569. if (xs_is_null(accts))
  2570. accts = xs_dict_get(args, "account_ids");
  2571. int c = 0;
  2572. const char *v;
  2573. while (xs_list_next(accts, &v, &c)) {
  2574. list_content(&snac, id, v, 1);
  2575. }
  2576. xs *out = xs_dict_new();
  2577. *body = xs_json_dumps(out, 4);
  2578. *ctype = "application/json";
  2579. status = HTTP_STATUS_OK;
  2580. }
  2581. }
  2582. }
  2583. }
  2584. else if (strcmp(cmd, "/v1/markers") == 0) { /** **/
  2585. xs_str *json = NULL;
  2586. if (logged_in) {
  2587. const xs_str *home_marker = xs_dict_get(args, "home[last_read_id]");
  2588. if (xs_is_null(home_marker)) {
  2589. const xs_dict *home = xs_dict_get(args, "home");
  2590. if (!xs_is_null(home))
  2591. home_marker = xs_dict_get(home, "last_read_id");
  2592. }
  2593. const xs_str *notify_marker = xs_dict_get(args, "notifications[last_read_id]");
  2594. if (xs_is_null(notify_marker)) {
  2595. const xs_dict *notify = xs_dict_get(args, "notifications");
  2596. if (!xs_is_null(notify))
  2597. notify_marker = xs_dict_get(notify, "last_read_id");
  2598. }
  2599. json = xs_json_dumps(markers_set(&snac, home_marker, notify_marker), 4);
  2600. }
  2601. if (!xs_is_null(json))
  2602. *body = json;
  2603. else
  2604. *body = xs_dup("{}");
  2605. *ctype = "application/json";
  2606. status = HTTP_STATUS_OK;
  2607. }
  2608. else
  2609. if (xs_startswith(cmd, "/v1/follow_requests")) { /** **/
  2610. if (logged_in) {
  2611. /* "authorize" or "reject" */
  2612. xs *rel = NULL;
  2613. xs *l = xs_split(cmd, "/");
  2614. const char *md5 = xs_list_get(l, -2);
  2615. const char *s_cmd = xs_list_get(l, -1);
  2616. if (xs_is_string(md5) && xs_is_string(s_cmd)) {
  2617. xs *actor = NULL;
  2618. if (valid_status(object_get_by_md5(md5, &actor))) {
  2619. const char *actor_id = xs_dict_get(actor, "id");
  2620. if (strcmp(s_cmd, "authorize") == 0) {
  2621. xs *fwreq = pending_get(&snac, actor_id);
  2622. if (fwreq != NULL) {
  2623. xs *reply = msg_accept(&snac, fwreq, actor_id);
  2624. enqueue_message(&snac, reply);
  2625. if (xs_is_null(xs_dict_get(fwreq, "published"))) {
  2626. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  2627. fwreq = xs_dict_set(fwreq, "published", date);
  2628. }
  2629. timeline_add(&snac, xs_dict_get(fwreq, "id"), fwreq);
  2630. follower_add(&snac, actor_id);
  2631. pending_del(&snac, actor_id);
  2632. rel = mastoapi_relationship(&snac, md5);
  2633. }
  2634. }
  2635. else
  2636. if (strcmp(s_cmd, "reject") == 0) {
  2637. pending_del(&snac, actor_id);
  2638. rel = mastoapi_relationship(&snac, md5);
  2639. }
  2640. }
  2641. }
  2642. if (rel != NULL) {
  2643. *body = xs_json_dumps(rel, 4);
  2644. *ctype = "application/json";
  2645. status = HTTP_STATUS_OK;
  2646. }
  2647. }
  2648. }
  2649. else
  2650. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2651. /* user cleanup */
  2652. if (logged_in)
  2653. user_free(&snac);
  2654. srv_debug(1, xs_fmt("mastoapi_post_handler %s %d", q_path, status));
  2655. return status;
  2656. }
  2657. int mastoapi_delete_handler(const xs_dict *req, const char *q_path,
  2658. const char *payload, int p_size,
  2659. char **body, int *b_size, char **ctype)
  2660. {
  2661. (void)p_size;
  2662. (void)body;
  2663. (void)b_size;
  2664. (void)ctype;
  2665. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  2666. return 0;
  2667. int status = HTTP_STATUS_NOT_FOUND;
  2668. xs *args = NULL;
  2669. const char *i_ctype = xs_dict_get(req, "content-type");
  2670. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  2671. if (!xs_is_null(payload))
  2672. args = xs_json_loads(payload);
  2673. }
  2674. else if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded"))
  2675. {
  2676. // Some apps send form data instead of json so we should cater for those
  2677. if (!xs_is_null(payload)) {
  2678. args = xs_url_vars(payload);
  2679. }
  2680. }
  2681. else
  2682. args = xs_dup(xs_dict_get(req, "p_vars"));
  2683. if (args == NULL)
  2684. return HTTP_STATUS_BAD_REQUEST;
  2685. snac snac = {0};
  2686. int logged_in = process_auth_token(&snac, req);
  2687. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2688. if (xs_startswith(cmd, "/v1/push/subscription") || xs_startswith(cmd, "/v2/push/subscription")) { /** **/
  2689. // pretend we deleted it, since it doesn't exist anyway
  2690. status = HTTP_STATUS_OK;
  2691. }
  2692. else
  2693. if (xs_startswith(cmd, "/v1/lists/")) {
  2694. if (logged_in) {
  2695. xs *l = xs_split(cmd, "/");
  2696. const char *p = xs_list_get(l, -1);
  2697. if (p) {
  2698. if (strcmp(p, "accounts") == 0) {
  2699. /* delete account from list */
  2700. p = xs_list_get(l, -2);
  2701. const xs_list *accts = xs_dict_get(args, "account_ids[]");
  2702. if (xs_is_null(accts))
  2703. accts = xs_dict_get(args, "account_ids");
  2704. int c = 0;
  2705. const char *v;
  2706. while (xs_list_next(accts, &v, &c)) {
  2707. list_content(&snac, p, v, 2);
  2708. }
  2709. }
  2710. else {
  2711. /* delete list */
  2712. if (xs_is_hex(p)) {
  2713. list_maint(&snac, p, 2);
  2714. }
  2715. }
  2716. }
  2717. *ctype = "application/json";
  2718. status = HTTP_STATUS_OK;
  2719. }
  2720. else
  2721. status = HTTP_STATUS_UNAUTHORIZED;
  2722. }
  2723. /* user cleanup */
  2724. if (logged_in)
  2725. user_free(&snac);
  2726. srv_debug(1, xs_fmt("mastoapi_delete_handler %s %d", q_path, status));
  2727. return status;
  2728. }
  2729. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  2730. const char *payload, int p_size,
  2731. char **body, int *b_size, char **ctype)
  2732. {
  2733. (void)p_size;
  2734. (void)b_size;
  2735. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  2736. return 0;
  2737. int status = HTTP_STATUS_NOT_FOUND;
  2738. xs *args = NULL;
  2739. const char *i_ctype = xs_dict_get(req, "content-type");
  2740. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  2741. if (!xs_is_null(payload))
  2742. args = xs_json_loads(payload);
  2743. }
  2744. else
  2745. args = xs_dup(xs_dict_get(req, "p_vars"));
  2746. if (args == NULL)
  2747. return HTTP_STATUS_BAD_REQUEST;
  2748. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2749. snac snac = {0};
  2750. int logged_in = process_auth_token(&snac, req);
  2751. if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) { /** **/
  2752. if (logged_in) {
  2753. xs *l = xs_split(cmd, "/");
  2754. const char *stid = xs_list_get(l, 3);
  2755. if (!xs_is_null(stid)) {
  2756. const char *desc = xs_dict_get(args, "description");
  2757. /* set the image metadata */
  2758. static_put_meta(&snac, stid, desc);
  2759. /* prepare a response */
  2760. xs *rsp = xs_dict_new();
  2761. xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
  2762. rsp = xs_dict_append(rsp, "id", stid);
  2763. rsp = xs_dict_append(rsp, "type", "image");
  2764. rsp = xs_dict_append(rsp, "url", url);
  2765. rsp = xs_dict_append(rsp, "preview_url", url);
  2766. rsp = xs_dict_append(rsp, "remote_url", url);
  2767. rsp = xs_dict_append(rsp, "description", desc);
  2768. *body = xs_json_dumps(rsp, 4);
  2769. *ctype = "application/json";
  2770. status = HTTP_STATUS_OK;
  2771. }
  2772. }
  2773. else
  2774. status = HTTP_STATUS_UNAUTHORIZED;
  2775. }
  2776. else
  2777. if (xs_startswith(cmd, "/v1/statuses")) {
  2778. if (logged_in) {
  2779. xs *l = xs_split(cmd, "/");
  2780. const char *mid = xs_list_get(l, 3);
  2781. if (!xs_is_null(mid)) {
  2782. const char *md5 = MID_TO_MD5(mid);
  2783. xs *rsp = NULL;
  2784. xs *msg = NULL;
  2785. if (valid_status(timeline_get_by_md5(&snac, md5, &msg))) {
  2786. const char *content = xs_dict_get(args, "status");
  2787. xs *atls = xs_list_new();
  2788. xs *f_content = not_really_markdown(content, &atls, NULL);
  2789. /* replace fields with new content */
  2790. msg = xs_dict_set(msg, "sourceContent", content);
  2791. msg = xs_dict_set(msg, "content", f_content);
  2792. xs *updated = xs_str_utctime(0, ISO_DATE_SPEC);
  2793. msg = xs_dict_set(msg, "updated", updated);
  2794. /* overwrite object, not updating the indexes */
  2795. object_add_ow(xs_dict_get(msg, "id"), msg);
  2796. /* update message */
  2797. xs *c_msg = msg_update(&snac, msg);
  2798. enqueue_message(&snac, c_msg);
  2799. rsp = mastoapi_status(&snac, msg);
  2800. }
  2801. if (rsp != NULL) {
  2802. *body = xs_json_dumps(rsp, 4);
  2803. *ctype = "application/json";
  2804. status = HTTP_STATUS_OK;
  2805. }
  2806. }
  2807. }
  2808. else
  2809. status = HTTP_STATUS_UNAUTHORIZED;
  2810. }
  2811. /* user cleanup */
  2812. if (logged_in)
  2813. user_free(&snac);
  2814. srv_debug(1, xs_fmt("mastoapi_put_handler %s %d", q_path, status));
  2815. return status;
  2816. }
  2817. void persist_image(const char *key, const xs_val *data, const char *payload, snac *snac)
  2818. /* Store header or avatar */
  2819. {
  2820. if (data != NULL) {
  2821. if (xs_type(data) == XSTYPE_LIST) {
  2822. const char *fn = xs_list_get(data, 0);
  2823. if (fn && *fn) {
  2824. const char *ext = strrchr(fn, '.');
  2825. /* Mona iOS sends always jpg as application/octet-stream with no filename */
  2826. if (ext == NULL || strcmp(fn, key) == 0) {
  2827. ext = ".jpg";
  2828. }
  2829. /* Make sure we have a unique file name, otherwise updated images will not be
  2830. * re-loaded by clients. */
  2831. xs *rnd = random_str();
  2832. xs *hash = xs_md5_hex(rnd, strlen(rnd));
  2833. xs *id = xs_fmt("%s%s", hash, ext);
  2834. xs *url = xs_fmt("%s/s/%s", snac->actor, id);
  2835. int fo = xs_number_get(xs_list_get(data, 1));
  2836. int fs = xs_number_get(xs_list_get(data, 2));
  2837. /* store */
  2838. static_put(snac, id, payload + fo, fs);
  2839. snac->config = xs_dict_set(snac->config, key, url);
  2840. }
  2841. }
  2842. }
  2843. }
  2844. int mastoapi_patch_handler(const xs_dict *req, const char *q_path,
  2845. const char *payload, int p_size,
  2846. char **body, int *b_size, char **ctype)
  2847. /* Handle profile updates */
  2848. {
  2849. (void)p_size;
  2850. (void)b_size;
  2851. if (!xs_startswith(q_path, "/api/v1/"))
  2852. return 0;
  2853. int status = HTTP_STATUS_NOT_FOUND;
  2854. xs *args = NULL;
  2855. const char *i_ctype = xs_dict_get(req, "content-type");
  2856. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  2857. if (!xs_is_null(payload))
  2858. args = xs_json_loads(payload);
  2859. }
  2860. else if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded"))
  2861. {
  2862. // Some apps send form data instead of json so we should cater for those
  2863. if (!xs_is_null(payload)) {
  2864. args = xs_url_vars(payload);
  2865. }
  2866. }
  2867. else
  2868. args = xs_dup(xs_dict_get(req, "p_vars"));
  2869. if (args == NULL)
  2870. return HTTP_STATUS_BAD_REQUEST;
  2871. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2872. snac snac = {0};
  2873. int logged_in = process_auth_token(&snac, req);
  2874. if (xs_startswith(cmd, "/v1/accounts/update_credentials")) {
  2875. /* Update user profile fields */
  2876. if (logged_in) {
  2877. int c = 0;
  2878. const xs_str *k;
  2879. const xs_val *v;
  2880. const xs_str *field_name = NULL;
  2881. xs *new_fields = xs_dict_new();
  2882. while (xs_dict_next(args, &k, &v, &c)) {
  2883. if (strcmp(k, "display_name") == 0) {
  2884. if (v != NULL)
  2885. snac.config = xs_dict_set(snac.config, "name", v);
  2886. }
  2887. else
  2888. if (strcmp(k, "note") == 0) {
  2889. if (v != NULL)
  2890. snac.config = xs_dict_set(snac.config, "bio", v);
  2891. }
  2892. else
  2893. if (strcmp(k, "bot") == 0) {
  2894. if (v != NULL)
  2895. snac.config = xs_dict_set(snac.config, "bot",
  2896. (strcmp(v, "true") == 0 ||
  2897. strcmp(v, "1") == 0) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  2898. }
  2899. else
  2900. if (strcmp(k, "source[sensitive]") == 0) {
  2901. if (v != NULL)
  2902. snac.config = xs_dict_set(snac.config, "cw",
  2903. strcmp(v, "true") == 0 ? "open" : "");
  2904. }
  2905. else
  2906. if (strcmp(k, "source[privacy]") == 0) {
  2907. if (v != NULL)
  2908. snac.config = xs_dict_set(snac.config, "private",
  2909. strcmp(v, "private") == 0 ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  2910. }
  2911. else
  2912. if (strcmp(k, "header") == 0) {
  2913. persist_image("header", v, payload, &snac);
  2914. }
  2915. else
  2916. if (strcmp(k, "avatar") == 0) {
  2917. persist_image("avatar", v, payload, &snac);
  2918. }
  2919. else
  2920. if (xs_between("fields_attributes", k, "[name]")) {
  2921. field_name = strcmp(v, "") != 0 ? v : NULL;
  2922. }
  2923. else
  2924. if (xs_between("fields_attributes", k, "[value]")) {
  2925. if (field_name != NULL) {
  2926. new_fields = xs_dict_set(new_fields, field_name, v);
  2927. snac.config = xs_dict_set(snac.config, "metadata", new_fields);
  2928. }
  2929. }
  2930. /* we don't have support for the following options, yet
  2931. - discoverable (0/1)
  2932. - locked (0/1)
  2933. */
  2934. }
  2935. /* Persist profile */
  2936. if (user_persist(&snac, 1) == 0)
  2937. credentials_get(body, ctype, &status, snac);
  2938. else
  2939. status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
  2940. }
  2941. else
  2942. status = HTTP_STATUS_UNAUTHORIZED;
  2943. }
  2944. /* user cleanup */
  2945. if (logged_in)
  2946. user_free(&snac);
  2947. srv_debug(1, xs_fmt("mastoapi_patch_handler %s %d", q_path, status));
  2948. return status;
  2949. }
  2950. void mastoapi_purge(void)
  2951. {
  2952. xs *spec = xs_fmt("%s/app/" "*.json", srv_basedir);
  2953. xs *files = xs_glob(spec, 1, 0);
  2954. xs_list *p = files;
  2955. const xs_str *v;
  2956. time_t mt = time(NULL) - 3600;
  2957. while (xs_list_iter(&p, &v)) {
  2958. xs *cid = xs_replace(v, ".json", "");
  2959. xs *fn = _app_fn(cid);
  2960. if (mtime(fn) < mt) {
  2961. /* get the app */
  2962. xs *app = app_get(cid);
  2963. if (app) {
  2964. /* old apps with no uid are incomplete cruft */
  2965. const char *uid = xs_dict_get(app, "uid");
  2966. if (xs_is_null(uid) || *uid == '\0') {
  2967. unlink(fn);
  2968. srv_debug(2, xs_fmt("purged %s", fn));
  2969. }
  2970. }
  2971. }
  2972. }
  2973. }
  2974. #endif /* #ifndef NO_MASTODON_API */