12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601 |
- #ifndef NO_MASTODON_API
- #include "xs.h"
- #include "xs_encdec.h"
- #include "xs_openssl.h"
- #include "xs_json.h"
- #include "xs_io.h"
- #include "xs_time.h"
- #include "snac.h"
- static xs_str *random_str(void)
- {
- unsigned int data[4] = {0};
- FILE *f;
- if ((f = fopen("/dev/random", "r")) != NULL) {
- fread(data, sizeof(data), 1, f);
- fclose(f);
- }
- else {
- data[0] = random() % 0xffffffff;
- data[1] = random() % 0xffffffff;
- data[2] = random() % 0xffffffff;
- data[3] = random() % 0xffffffff;
- }
- return xs_hex_enc((char *)data, sizeof(data));
- }
- int app_add(const char *id, const xs_dict *app)
- {
- if (!xs_is_hex(id))
- return 500;
- int status = 201;
- xs *fn = xs_fmt("%s/app/", srv_basedir);
- FILE *f;
- mkdirx(fn);
- fn = xs_str_cat(fn, id);
- fn = xs_str_cat(fn, ".json");
- if ((f = fopen(fn, "w")) != NULL) {
- xs *j = xs_json_dumps_pp(app, 4);
- fwrite(j, strlen(j), 1, f);
- fclose(f);
- }
- else
- status = 500;
- return status;
- }
- xs_dict *app_get(const char *id)
- {
- if (!xs_is_hex(id))
- return NULL;
- xs *fn = xs_fmt("%s/app/%s.json", srv_basedir, id);
- xs_dict *app = NULL;
- FILE *f;
- if ((f = fopen(fn, "r")) != NULL) {
- xs *j = xs_readall(f);
- fclose(f);
- app = xs_json_loads(j);
- }
- return app;
- }
- int app_del(const char *id)
- {
- if (!xs_is_hex(id))
- return -1;
- xs *fn = xs_fmt("%s/app/%s.json", srv_basedir, id);
- return unlink(fn);
- }
- int token_add(const char *id, const xs_dict *token)
- {
- if (!xs_is_hex(id))
- return 500;
- int status = 201;
- xs *fn = xs_fmt("%s/token/", srv_basedir);
- FILE *f;
- mkdirx(fn);
- fn = xs_str_cat(fn, id);
- fn = xs_str_cat(fn, ".json");
- if ((f = fopen(fn, "w")) != NULL) {
- xs *j = xs_json_dumps_pp(token, 4);
- fwrite(j, strlen(j), 1, f);
- fclose(f);
- }
- else
- status = 500;
- return status;
- }
- xs_dict *token_get(const char *id)
- {
- if (!xs_is_hex(id))
- return NULL;
- xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
- xs_dict *token = NULL;
- FILE *f;
- if ((f = fopen(fn, "r")) != NULL) {
- xs *j = xs_readall(f);
- fclose(f);
- token = xs_json_loads(j);
- }
- return token;
- }
- int token_del(const char *id)
- {
- if (!xs_is_hex(id))
- return -1;
- xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
- return unlink(fn);
- }
- const char *login_page = ""
- "<!DOCTYPE html>\n"
- "<body><h1>%s OAuth identify</h1>\n"
- "<div style=\"background-color: red; color: white\">%s</div>\n"
- "<form method=\"post\" action=\"https:/" "/%s/oauth/x-snac-login\">\n"
- "<p>Login: <input type=\"text\" name=\"login\"></p>\n"
- "<p>Password: <input type=\"password\" name=\"passwd\"></p>\n"
- "<input type=\"hidden\" name=\"redir\" value=\"%s\">\n"
- "<input type=\"hidden\" name=\"cid\" value=\"%s\">\n"
- "<input type=\"hidden\" name=\"state\" value=\"%s\">\n"
- "<input type=\"submit\" value=\"OK\">\n"
- "</form><p>%s</p></body>\n"
- "";
- int oauth_get_handler(const xs_dict *req, const char *q_path,
- char **body, int *b_size, char **ctype)
- {
- if (!xs_startswith(q_path, "/oauth/"))
- return 0;
- int status = 404;
- xs_dict *msg = xs_dict_get(req, "q_vars");
- xs *cmd = xs_replace(q_path, "/oauth", "");
- srv_debug(1, xs_fmt("oauth_get_handler %s", q_path));
- if (strcmp(cmd, "/authorize") == 0) {
- const char *cid = xs_dict_get(msg, "client_id");
- const char *ruri = xs_dict_get(msg, "redirect_uri");
- const char *rtype = xs_dict_get(msg, "response_type");
- const char *state = xs_dict_get(msg, "state");
- status = 400;
- if (cid && ruri && rtype && strcmp(rtype, "code") == 0) {
- xs *app = app_get(cid);
- if (app != NULL) {
- const char *host = xs_dict_get(srv_config, "host");
- if (xs_is_null(state))
- state = "";
- *body = xs_fmt(login_page, host, "", host, ruri, cid, state, USER_AGENT);
- *ctype = "text/html";
- status = 200;
- srv_debug(0, xs_fmt("oauth authorize: generating login page"));
- }
- else
- srv_debug(0, xs_fmt("oauth authorize: bad client_id %s", cid));
- }
- else
- srv_debug(0, xs_fmt("oauth authorize: invalid or unset arguments"));
- }
- return status;
- }
- int oauth_post_handler(const xs_dict *req, const char *q_path,
- const char *payload, int p_size,
- char **body, int *b_size, char **ctype)
- {
- if (!xs_startswith(q_path, "/oauth/"))
- return 0;
- int status = 404;
- char *i_ctype = xs_dict_get(req, "content-type");
- xs *args = NULL;
- if (i_ctype && xs_startswith(i_ctype, "application/json"))
- args = xs_json_loads(payload);
- else
- args = xs_dup(xs_dict_get(req, "p_vars"));
- xs *cmd = xs_replace(q_path, "/oauth", "");
- srv_debug(1, xs_fmt("oauth_post_handler %s", q_path));
- if (strcmp(cmd, "/x-snac-login") == 0) {
- const char *login = xs_dict_get(args, "login");
- const char *passwd = xs_dict_get(args, "passwd");
- const char *redir = xs_dict_get(args, "redir");
- const char *cid = xs_dict_get(args, "cid");
- const char *state = xs_dict_get(args, "state");
- const char *host = xs_dict_get(srv_config, "host");
-
- *body = xs_fmt(login_page, host, "LOGIN INCORRECT", host, redir, cid, state, USER_AGENT);
- *ctype = "text/html";
- status = 200;
- if (login && passwd && redir && cid) {
- snac snac;
- if (user_open(&snac, login)) {
-
- if (check_password(login, passwd,
- xs_dict_get(snac.config, "passwd"))) {
-
- xs *code = random_str();
- xs_free(*body);
- *body = xs_fmt("%s?code=%s", redir, code);
- status = 303;
-
- if (!xs_is_null(state) && *state) {
- *body = xs_str_cat(*body, "&state=");
- *body = xs_str_cat(*body, state);
- }
- srv_log(xs_fmt("oauth x-snac-login: '%s' success, redirect to %s",
- login, *body));
-
- xs *app = app_get(cid);
- if (app != NULL) {
- app = xs_dict_set(app, "uid", login);
- app = xs_dict_set(app, "code", code);
- app_add(cid, app);
- }
- else
- srv_log(xs_fmt("oauth x-snac-login: error getting app %s", cid));
- }
- else
- srv_debug(1, xs_fmt("oauth x-snac-login: login '%s' incorrect", login));
- user_free(&snac);
- }
- else
- srv_debug(1, xs_fmt("oauth x-snac-login: bad user '%s'", login));
- }
- else
- srv_debug(1, xs_fmt("oauth x-snac-login: invalid or unset arguments"));
- }
- else
- if (strcmp(cmd, "/token") == 0) {
- xs *wrk = NULL;
- const char *gtype = xs_dict_get(args, "grant_type");
- const char *code = xs_dict_get(args, "code");
- const char *cid = xs_dict_get(args, "client_id");
- const char *csec = xs_dict_get(args, "client_secret");
- const char *ruri = xs_dict_get(args, "redirect_uri");
-
- const char *scope = NULL;
-
- if (xs_is_null(csec)) {
- const char *auhdr = xs_dict_get(req, "authorization");
- if (!xs_is_null(auhdr) && xs_startswith(auhdr, "Basic ")) {
- xs *s1 = xs_replace(auhdr, "Basic ", "");
- int size;
- xs *s2 = xs_base64_dec(s1, &size);
- if (!xs_is_null(s2)) {
- xs *l1 = xs_split(s2, ":");
- if (xs_list_len(l1) == 2) {
- wrk = xs_dup(xs_list_get(l1, 1));
- csec = wrk;
- }
- }
- }
- }
- if (gtype && code && cid && csec && ruri) {
- xs *app = app_get(cid);
- if (app == NULL) {
- status = 401;
- srv_log(xs_fmt("oauth token: invalid app %s", cid));
- }
- else
- if (strcmp(csec, xs_dict_get(app, "client_secret")) != 0) {
- status = 401;
- srv_log(xs_fmt("oauth token: invalid client_secret for app %s", cid));
- }
- else {
- xs *rsp = xs_dict_new();
- xs *cat = xs_number_new(time(NULL));
- xs *tokid = random_str();
- rsp = xs_dict_append(rsp, "access_token", tokid);
- rsp = xs_dict_append(rsp, "token_type", "Bearer");
- rsp = xs_dict_append(rsp, "created_at", cat);
- if (!xs_is_null(scope))
- rsp = xs_dict_append(rsp, "scope", scope);
- *body = xs_json_dumps_pp(rsp, 4);
- *ctype = "application/json";
- status = 200;
- const char *uid = xs_dict_get(app, "uid");
- srv_debug(0, xs_fmt("oauth token: "
- "successful login for %s, new token %s", uid, tokid));
- xs *token = xs_dict_new();
- token = xs_dict_append(token, "token", tokid);
- token = xs_dict_append(token, "client_id", cid);
- token = xs_dict_append(token, "client_secret", csec);
- token = xs_dict_append(token, "uid", uid);
- token = xs_dict_append(token, "code", code);
- token_add(tokid, token);
- }
- }
- else {
- srv_debug(0, xs_fmt("oauth token: invalid or unset arguments"));
- status = 400;
- }
- }
- else
- if (strcmp(cmd, "/revoke") == 0) {
- const char *cid = xs_dict_get(args, "client_id");
- const char *csec = xs_dict_get(args, "client_secret");
- const char *tokid = xs_dict_get(args, "token");
- if (cid && csec && tokid) {
- xs *token = token_get(tokid);
- *body = xs_str_new("{}");
- *ctype = "application/json";
- if (token == NULL || strcmp(csec, xs_dict_get(token, "client_secret")) != 0) {
- srv_debug(1, xs_fmt("oauth revoke: bad secret for token %s", tokid));
- status = 403;
- }
- else {
- token_del(tokid);
- srv_debug(0, xs_fmt("oauth revoke: revoked token %s", tokid));
- status = 200;
-
- app_del(cid);
- }
- }
- else {
- srv_debug(0, xs_fmt("oauth revoke: invalid or unset arguments"));
- status = 403;
- }
- }
- return status;
- }
- xs_str *mastoapi_id(const xs_dict *msg)
- {
- const char *id = xs_dict_get(msg, "id");
- xs *md5 = xs_md5_hex(id, strlen(id));
- return xs_fmt("%10.0f%s", object_ctime_by_md5(md5), md5);
- }
- #define MID_TO_MD5(id) (id + 10)
- xs_dict *mastoapi_account(const xs_dict *actor)
- {
- xs_dict *acct = xs_dict_new();
- const char *display_name = xs_dict_get(actor, "name");
- if (xs_is_null(display_name) || *display_name == '\0')
- display_name = xs_dict_get(actor, "preferredUsername");
- const char *id = xs_dict_get(actor, "id");
- const char *pub = xs_dict_get(actor, "published");
- xs *acct_md5 = xs_md5_hex(id, strlen(id));
- acct = xs_dict_append(acct, "id", acct_md5);
- acct = xs_dict_append(acct, "username", xs_dict_get(actor, "preferredUsername"));
- acct = xs_dict_append(acct, "acct", xs_dict_get(actor, "preferredUsername"));
- acct = xs_dict_append(acct, "display_name", display_name);
- if (pub)
- acct = xs_dict_append(acct, "created_at", pub);
- else {
-
- xs *date = xs_str_utctime(0, "%Y-%m-%dT%H:%M:%SZ");
- acct = xs_dict_append(acct, "created_at", date);
- }
- acct = xs_dict_append(acct, "note", xs_dict_get(actor, "summary"));
- acct = xs_dict_append(acct, "url", id);
- xs *avatar = NULL;
- xs_dict *av = xs_dict_get(actor, "icon");
- if (xs_type(av) == XSTYPE_DICT)
- avatar = xs_dup(xs_dict_get(av, "url"));
- else
- avatar = xs_fmt("%s/susie.png", srv_baseurl);
- acct = xs_dict_append(acct, "avatar", avatar);
- return acct;
- }
- xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
- {
- xs *actor = NULL;
- actor_get(snac, xs_dict_get(msg, "attributedTo"), &actor);
-
- if (actor == NULL)
- return NULL;
- xs *acct = mastoapi_account(actor);
-
- xs *f = xs_val_new(XSTYPE_FALSE);
- xs *t = xs_val_new(XSTYPE_TRUE);
- xs *n = xs_val_new(XSTYPE_NULL);
- xs *el = xs_list_new();
- xs *idx = NULL;
- xs *ixc = NULL;
- char *tmp;
- char *id = xs_dict_get(msg, "id");
- xs *mid = mastoapi_id(msg);
- xs_dict *st = xs_dict_new();
- st = xs_dict_append(st, "id", mid);
- st = xs_dict_append(st, "uri", id);
- st = xs_dict_append(st, "url", id);
- st = xs_dict_append(st, "created_at", xs_dict_get(msg, "published"));
- st = xs_dict_append(st, "account", acct);
- st = xs_dict_append(st, "content", xs_dict_get(msg, "content"));
- st = xs_dict_append(st, "visibility",
- is_msg_public(snac, msg) ? "public" : "private");
- tmp = xs_dict_get(msg, "sensitive");
- if (xs_is_null(tmp))
- tmp = f;
- st = xs_dict_append(st, "sensitive", tmp);
- tmp = xs_dict_get(msg, "summary");
- if (xs_is_null(tmp))
- tmp = "";
- st = xs_dict_append(st, "spoiler_text", tmp);
-
- xs *matt = xs_list_new();
- xs_list *att = xs_dict_get(msg, "attachment");
- xs_str *aobj;
- while (xs_list_iter(&att, &aobj)) {
- const char *mtype = xs_dict_get(aobj, "mediaType");
- if (!xs_is_null(mtype) && xs_startswith(mtype, "image/")) {
- xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
- xs *matte = xs_dict_new();
- matte = xs_dict_append(matte, "id", matteid);
- matte = xs_dict_append(matte, "type", "image");
- matte = xs_dict_append(matte, "url", xs_dict_get(aobj, "url"));
- matte = xs_dict_append(matte, "preview_url", xs_dict_get(aobj, "url"));
- matte = xs_dict_append(matte, "remote_url", xs_dict_get(aobj, "url"));
- const char *name = xs_dict_get(aobj, "name");
- if (xs_is_null(name))
- name = "";
- matte = xs_dict_append(matte, "description", name);
- matt = xs_list_append(matt, matte);
- }
- }
- st = xs_dict_append(st, "media_attachments", matt);
- st = xs_dict_append(st, "mentions", el);
- st = xs_dict_append(st, "tags", el);
- st = xs_dict_append(st, "emojis", el);
- xs_free(idx);
- xs_free(ixc);
- idx = object_likes(id);
- ixc = xs_number_new(xs_list_len(idx));
- st = xs_dict_append(st, "favourites_count", ixc);
- st = xs_dict_append(st, "favourited",
- xs_list_in(idx, snac->md5) != -1 ? t : f);
- xs_free(idx);
- xs_free(ixc);
- idx = object_announces(id);
- ixc = xs_number_new(xs_list_len(idx));
- st = xs_dict_append(st, "reblogs_count", ixc);
- st = xs_dict_append(st, "reblogged",
- xs_list_in(idx, snac->md5) != -1 ? t : f);
- xs_free(idx);
- xs_free(ixc);
- idx = object_children(id);
- ixc = xs_number_new(xs_list_len(idx));
- st = xs_dict_append(st, "replies_count", ixc);
-
- st = xs_dict_append(st, "in_reply_to_id", n);
- st = xs_dict_append(st, "in_reply_to_account_id", n);
- tmp = xs_dict_get(msg, "inReplyTo");
- if (!xs_is_null(tmp)) {
- xs *irto = NULL;
- if (valid_status(object_get(tmp, &irto))) {
- xs *irt_mid = mastoapi_id(irto);
- st = xs_dict_set(st, "in_reply_to_id", irt_mid);
- char *at = NULL;
- if (!xs_is_null(at = xs_dict_get(irto, "attributedTo"))) {
- xs *at_md5 = xs_md5_hex(at, strlen(at));
- st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
- }
- }
- }
- st = xs_dict_append(st, "reblog", n);
- st = xs_dict_append(st, "poll", n);
- st = xs_dict_append(st, "card", n);
- st = xs_dict_append(st, "language", n);
- tmp = xs_dict_get(msg, "sourceContent");
- if (xs_is_null(tmp))
- tmp = "";
- st = xs_dict_append(st, "text", tmp);
- tmp = xs_dict_get(msg, "updated");
- if (xs_is_null(tmp))
- tmp = n;
- st = xs_dict_append(st, "edited_at", tmp);
- return st;
- }
- int process_auth_token(snac *snac, const xs_dict *req)
- {
- int logged_in = 0;
- char *v;
-
- if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
- xs *tokid = xs_replace(v, "Bearer ", "");
- xs *token = token_get(tokid);
- if (token != NULL) {
- const char *uid = xs_dict_get(token, "uid");
- if (!xs_is_null(uid) && user_open(snac, uid)) {
- logged_in = 1;
- srv_debug(2, xs_fmt("mastoapi auth: valid token for user %s", uid));
- }
- else
- srv_log(xs_fmt("mastoapi auth: corrupted token %s", tokid));
- }
- else
- srv_log(xs_fmt("mastoapi auth: invalid token %s", tokid));
- }
- return logged_in;
- }
- int mastoapi_get_handler(const xs_dict *req, const char *q_path,
- char **body, int *b_size, char **ctype)
- {
- if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
- return 0;
- srv_debug(1, xs_fmt("mastoapi_get_handler %s", q_path));
- int status = 404;
- xs_dict *args = xs_dict_get(req, "q_vars");
- xs *cmd = xs_replace(q_path, "/api", "");
- snac snac1 = {0};
- int logged_in = process_auth_token(&snac1, req);
- if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) {
- if (logged_in) {
- xs *acct = xs_dict_new();
- acct = xs_dict_append(acct, "id", xs_dict_get(snac1.config, "uid"));
- acct = xs_dict_append(acct, "username", xs_dict_get(snac1.config, "uid"));
- acct = xs_dict_append(acct, "acct", xs_dict_get(snac1.config, "uid"));
- acct = xs_dict_append(acct, "display_name", xs_dict_get(snac1.config, "name"));
- acct = xs_dict_append(acct, "created_at", xs_dict_get(snac1.config, "published"));
- acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio"));
- acct = xs_dict_append(acct, "url", snac1.actor);
- acct = xs_dict_append(acct, "header", "");
- xs *avatar = NULL;
- char *av = xs_dict_get(snac1.config, "avatar");
- if (xs_is_null(av) || *av == '\0')
- avatar = xs_fmt("%s/susie.png", srv_baseurl);
- else
- avatar = xs_dup(av);
- acct = xs_dict_append(acct, "avatar", avatar);
- *body = xs_json_dumps_pp(acct, 4);
- *ctype = "application/json";
- status = 200;
- }
- else {
- status = 422;
- }
- }
- else
- if (strcmp(cmd, "/v1/accounts/relationships") == 0) {
-
-
-
- if (logged_in) {
- *body = xs_dup("[]");
- *ctype = "application/json";
- status = 200;
- }
- }
- else
- if (xs_startswith(cmd, "/v1/accounts/")) {
-
- xs *l = xs_split(cmd, "/");
- const char *uid = xs_list_get(l, 3);
- const char *opt = xs_list_get(l, 4);
- if (uid != NULL) {
- snac snac2;
- xs *out = NULL;
- xs *actor = NULL;
-
- if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
- if (opt == NULL) {
-
- actor = msg_actor(&snac2);
- out = mastoapi_account(actor);
- }
- else
- if (strcmp(opt, "statuses") == 0) {
-
- xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
- xs_list *p = timeline;
- xs_str *v;
- out = xs_list_new();
- while (xs_list_iter(&p, &v)) {
- xs *msg = NULL;
- if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
-
- if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
- xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
- xs *st = mastoapi_status(&snac2, msg);
- out = xs_list_append(out, st);
- }
- }
- }
- }
- user_free(&snac2);
- }
- else {
-
- if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
- if (opt == NULL) {
-
- out = mastoapi_account(actor);
- }
- else
- if (strcmp(opt, "statuses") == 0) {
-
- out = xs_list_new();
- }
- }
- }
- if (out != NULL) {
- *body = xs_json_dumps_pp(out, 4);
- *ctype = "application/json";
- status = 200;
- }
- }
- }
- else
- if (strcmp(cmd, "/v1/timelines/home") == 0) {
-
- if (logged_in) {
- const char *max_id = xs_dict_get(args, "max_id");
- const char *since_id = xs_dict_get(args, "since_id");
- const char *min_id = xs_dict_get(args, "min_id");
- const char *limit_s = xs_dict_get(args, "limit");
- int limit = 0;
- int cnt = 0;
- if (!xs_is_null(limit_s))
- limit = atoi(limit_s);
- if (limit == 0)
- limit = 20;
- xs *timeline = timeline_simple_list(&snac1, "private", 0, XS_ALL);
- xs *out = xs_list_new();
- xs_list *p = timeline;
- xs_str *v;
- while (xs_list_iter(&p, &v) && cnt < limit) {
- xs *msg = NULL;
-
- if (max_id) {
- if (strcmp(v, max_id) == 0)
- max_id = NULL;
- continue;
- }
-
- if (since_id) {
- if (strcmp(v, since_id) == 0)
- break;
- }
-
-
- if (min_id) {
- if (strcmp(v, min_id) == 0)
- break;
- }
-
- if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
- continue;
-
- if (strcmp(xs_dict_get(msg, "type"), "Note") != 0)
- continue;
-
- xs *st = mastoapi_status(&snac1, msg);
- if (st != NULL)
- out = xs_list_append(out, st);
- cnt++;
- }
- *body = xs_json_dumps_pp(out, 4);
- *ctype = "application/json";
- status = 200;
- {
- xs *j = xs_json_loads(*body);
- if (j == NULL) {
- srv_log(xs_fmt("mastoapi timeline: bad JSON"));
- srv_archive_error("mastoapi_timeline", "bad JSON", req, *body);
- }
- }
- srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
- }
- else {
- status = 401;
- }
- }
- else
- if (strcmp(cmd, "/v1/timelines/public") == 0) {
-
-
- *body = xs_dup("[]");
- *ctype = "application/json";
- status = 200;
- }
- else
- if (strcmp(cmd, "/v1/conversations") == 0) {
-
- *body = xs_dup("[]");
- *ctype = "application/json";
- status = 200;
- }
- else
- if (strcmp(cmd, "/v1/notifications") == 0) {
- if (logged_in) {
- xs *l = notify_list(&snac1, 0);
- xs *out = xs_list_new();
- xs_list *p = l;
- xs_dict *v;
- while (xs_list_iter(&p, &v)) {
- xs *noti = notify_get(&snac1, v);
- if (noti == NULL)
- continue;
- const char *type = xs_dict_get(noti, "type");
- const char *objid = xs_dict_get(noti, "objid");
- xs *actor = NULL;
- xs *entry = NULL;
- if (!valid_status(object_get(xs_dict_get(noti, "actor"), &actor)))
- continue;
- if (objid != NULL && !valid_status(object_get(objid, &entry)))
- continue;
- if (is_hidden(&snac1, objid))
- continue;
-
- if (strcmp(type, "Like") == 0)
- type = "favourite";
- else
- if (strcmp(type, "Announce") == 0)
- type = "reblog";
- else
- if (strcmp(type, "Follow") == 0)
- type = "follow";
- else
- if (strcmp(type, "Create") == 0)
- type = "mention";
- else
- continue;
- xs *mn = xs_dict_new();
- mn = xs_dict_append(mn, "type", type);
- xs *id = xs_replace(xs_dict_get(noti, "id"), ".", "");
- mn = xs_dict_append(mn, "id", id);
- mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
- xs *acct = mastoapi_account(actor);
- mn = xs_dict_append(mn, "account", acct);
- if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
- xs *st = mastoapi_status(&snac1, entry);
- mn = xs_dict_append(mn, "status", st);
- }
- out = xs_list_append(out, mn);
- }
- *body = xs_json_dumps_pp(out, 4);
- *ctype = "application/json";
- status = 200;
- }
- else
- status = 401;
- }
- else
- if (strcmp(cmd, "/v1/filters") == 0) {
-
- *body = xs_dup("[]");
- *ctype = "application/json";
- status = 200;
- }
- else
- if (strcmp(cmd, "/v1/favourites") == 0) {
-
- *body = xs_dup("[]");
- *ctype = "application/json";
- status = 200;
- }
- else
- if (strcmp(cmd, "/v1/bookmarks") == 0) {
-
- *body = xs_dup("[]");
- *ctype = "application/json";
- status = 200;
- }
- else
- if (strcmp(cmd, "/v1/lists") == 0) {
-
- *body = xs_dup("[]");
- *ctype = "application/json";
- status = 200;
- }
- else
- if (strcmp(cmd, "/v1/scheduled_statuses") == 0) {
-
- *body = xs_dup("[]");
- *ctype = "application/json";
- status = 200;
- }
- else
- if (strcmp(cmd, "/v1/follow_requests") == 0) {
-
- *body = xs_dup("[]");
- *ctype = "application/json";
- status = 200;
- }
- else
- if (strcmp(cmd, "/v1/announcements") == 0) {
-
- *body = xs_dup("[]");
- *ctype = "application/json";
- status = 200;
- }
- else
- if (strcmp(cmd, "/v1/custom_emojis") == 0) {
-
- *body = xs_dup("[]");
- *ctype = "application/json";
- status = 200;
- }
- else
- if (strcmp(cmd, "/v1/instance") == 0) {
-
- xs *ins = xs_dict_new();
- const char *host = xs_dict_get(srv_config, "host");
- ins = xs_dict_append(ins, "uri", host);
- ins = xs_dict_append(ins, "domain", host);
- ins = xs_dict_append(ins, "title", host);
- ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
- ins = xs_dict_append(ins, "source_url", "https:/" "/comam.es/what-is-snac");
- ins = xs_dict_append(ins, "description", host);
- ins = xs_dict_append(ins, "short_description", host);
- xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
- ins = xs_dict_append(ins, "thumbnail", susie);
- const char *v = xs_dict_get(srv_config, "admin_email");
- if (xs_is_null(v) || *v == '\0')
- v = "admin@localhost";
- ins = xs_dict_append(ins, "email", v);
- xs *l1 = xs_list_new();
- ins = xs_dict_append(ins, "rules", l1);
- l1 = xs_list_append(l1, "en");
- ins = xs_dict_append(ins, "languages", l1);
- xs *d1 = xs_dict_new();
- ins = xs_dict_append(ins, "urls", d1);
- ins = xs_dict_append(ins, "configuration", d1);
- xs *z = xs_number_new(0);
- d1 = xs_dict_append(d1, "user_count", z);
- d1 = xs_dict_append(d1, "status_count", z);
- d1 = xs_dict_append(d1, "domain_count", z);
- ins = xs_dict_append(ins, "stats", d1);
- xs *f = xs_val_new(XSTYPE_FALSE);
- ins = xs_dict_append(ins, "registrations", f);
- ins = xs_dict_append(ins, "approval_required", f);
- ins = xs_dict_append(ins, "invites_enabled", f);
- *body = xs_json_dumps_pp(ins, 4);
- *ctype = "application/json";
- status = 200;
- }
- else
- if (xs_startswith(cmd, "/v1/statuses/")) {
-
- xs *l = xs_split(cmd, "/");
- const char *id = xs_list_get(l, 3);
- const char *op = xs_list_get(l, 4);
- if (!xs_is_null(id)) {
- xs *msg = NULL;
- xs *out = NULL;
-
- id = MID_TO_MD5(id);
- if (valid_status(timeline_get_by_md5(&snac1, id, &msg))) {
- if (op == NULL) {
-
- out = mastoapi_status(&snac1, msg);
- }
- else
- if (strcmp(op, "context") == 0) {
-
- xs *anc = xs_list_new();
- xs *des = xs_list_new();
- xs_list *p;
- xs_str *v;
- char pid[64];
-
- strncpy(pid, id, sizeof(pid));
- while (object_parent(pid, pid, sizeof(pid))) {
- xs *m2 = NULL;
- if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
- xs *st = mastoapi_status(&snac1, m2);
- anc = xs_list_insert(anc, 0, st);
- }
- else
- break;
- }
-
- xs *children = object_children(xs_dict_get(msg, "id"));
- p = children;
- while (xs_list_iter(&p, &v)) {
- xs *m2 = NULL;
- if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
- xs *st = mastoapi_status(&snac1, m2);
- des = xs_list_append(des, st);
- }
- }
- out = xs_dict_new();
- out = xs_dict_append(out, "ancestors", anc);
- out = xs_dict_append(out, "descendants", des);
- }
- else
- if (strcmp(op, "reblogged_by") == 0 ||
- strcmp(op, "favourited_by") == 0) {
-
- out = xs_list_new();
- xs *l = NULL;
- if (op[0] == 'r')
- l = object_announces(xs_dict_get(msg, "id"));
- else
- l = object_likes(xs_dict_get(msg, "id"));
- xs_list *p = l;
- xs_str *v;
- while (xs_list_iter(&p, &v)) {
- xs *actor2 = NULL;
- if (valid_status(object_get_by_md5(v, &actor2))) {
- xs *acct2 = mastoapi_account(actor2);
- out = xs_list_append(out, acct2);
- }
- }
- }
- }
- else
- srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
- if (out != NULL) {
- *body = xs_json_dumps_pp(out, 4);
- *ctype = "application/json";
- status = 200;
- }
- }
- }
- else
- if (strcmp(cmd, "/v1/filters") == 0) {
- *body = xs_dup("[]");
- *ctype = "application/json";
- status = 200;
- }
- else
- if (strcmp(cmd, "/v1/preferences") == 0) {
- *body = xs_dup("{}");
- *ctype = "application/json";
- status = 200;
- }
- else
- if (strcmp(cmd, "/v1/markers") == 0) {
- *body = xs_dup("{}");
- *ctype = "application/json";
- status = 200;
- }
- else
- if (strcmp(cmd, "/v1/followed_tags") == 0) {
- *body = xs_dup("[]");
- *ctype = "application/json";
- status = 200;
- }
-
- if (logged_in)
- user_free(&snac1);
- return status;
- }
- int mastoapi_post_handler(const xs_dict *req, const char *q_path,
- const char *payload, int p_size,
- char **body, int *b_size, char **ctype)
- {
- if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
- return 0;
- srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
- int status = 404;
- xs *args = NULL;
- char *i_ctype = xs_dict_get(req, "content-type");
- if (i_ctype && xs_startswith(i_ctype, "application/json"))
- args = xs_json_loads(payload);
- else
- args = xs_dup(xs_dict_get(req, "p_vars"));
- if (args == NULL)
- return 400;
- xs *cmd = xs_replace(q_path, "/api", "");
- snac snac = {0};
- int logged_in = process_auth_token(&snac, req);
- if (strcmp(cmd, "/v1/apps") == 0) {
- const char *name = xs_dict_get(args, "client_name");
- const char *ruri = xs_dict_get(args, "redirect_uris");
- const char *scope = xs_dict_get(args, "scope");
- if (xs_type(ruri) == XSTYPE_LIST)
- ruri = xs_dict_get(ruri, 0);
- if (name && ruri) {
- xs *app = xs_dict_new();
- xs *id = xs_replace_i(tid(0), ".", "");
- xs *cid = random_str();
- xs *csec = random_str();
- xs *vkey = random_str();
- app = xs_dict_append(app, "name", name);
- app = xs_dict_append(app, "redirect_uri", ruri);
- app = xs_dict_append(app, "client_id", cid);
- app = xs_dict_append(app, "client_secret", csec);
- app = xs_dict_append(app, "vapid_key", vkey);
- app = xs_dict_append(app, "id", id);
- *body = xs_json_dumps_pp(app, 4);
- *ctype = "application/json";
- status = 200;
- app = xs_dict_append(app, "code", "");
- if (scope)
- app = xs_dict_append(app, "scope", scope);
- app_add(cid, app);
- srv_debug(0, xs_fmt("mastoapi apps: new app %s", cid));
- }
- }
- else
- if (strcmp(cmd, "/v1/statuses") == 0) {
- if (logged_in) {
-
- const char *content = xs_dict_get(args, "status");
- const char *mid = xs_dict_get(args, "in_reply_to_id");
- const char *visibility = xs_dict_get(args, "visibility");
- const char *summary = xs_dict_get(args, "spoiler_text");
- const char *media_ids = xs_dict_get(args, "media_ids");
- if (xs_is_null(media_ids))
- media_ids = xs_dict_get(args, "media_ids[]");
- xs *attach_list = xs_list_new();
- xs *irt = NULL;
-
- if (mid != NULL) {
- xs *r_msg = NULL;
- const char *md5 = MID_TO_MD5(mid);
- if (valid_status(object_get_by_md5(md5, &r_msg)))
- irt = xs_dup(xs_dict_get(r_msg, "id"));
- }
-
- if (!xs_is_null(media_ids)) {
- xs *mi = NULL;
- if (xs_type(media_ids) == XSTYPE_LIST)
- mi = xs_dup(media_ids);
- else {
- mi = xs_list_new();
- mi = xs_list_append(mi, media_ids);
- }
- xs_list *p = mi;
- xs_str *v;
- while (xs_list_iter(&p, &v)) {
- xs *l = xs_list_new();
- xs *url = xs_fmt("%s/s/%s", snac.actor, v);
- xs *desc = static_get_meta(&snac, v);
- l = xs_list_append(l, url);
- l = xs_list_append(l, desc);
- attach_list = xs_list_append(attach_list, l);
- }
- }
-
- xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
- strcmp(visibility, "public") == 0 ? 0 : 1);
- if (!xs_is_null(summary) && *summary) {
- xs *t = xs_val_new(XSTYPE_TRUE);
- msg = xs_dict_set(msg, "sensitive", t);
- msg = xs_dict_set(msg, "summary", summary);
- }
-
- timeline_add(&snac, xs_dict_get(msg, "id"), msg);
-
- xs *c_msg = msg_create(&snac, msg);
- enqueue_message(&snac, c_msg);
- timeline_touch(&snac);
-
- xs *st = mastoapi_status(&snac, msg);
- *body = xs_json_dumps_pp(st, 4);
- *ctype = "application/json";
- status = 200;
- }
- else
- status = 401;
- }
- else
- if (xs_startswith(cmd, "/v1/statuses")) {
- if (logged_in) {
-
- xs *l = xs_split(cmd, "/");
- const char *mid = xs_list_get(l, 3);
- const char *op = xs_list_get(l, 4);
- if (!xs_is_null(mid)) {
- xs *msg = NULL;
- xs *out = NULL;
-
- mid = MID_TO_MD5(mid);
- if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
- char *id = xs_dict_get(msg, "id");
- if (op == NULL) {
-
- }
- else
- if (strcmp(op, "favourite") == 0) {
- xs *n_msg = msg_admiration(&snac, id, "Like");
- if (n_msg != NULL) {
- enqueue_message(&snac, n_msg);
- timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
- out = mastoapi_status(&snac, msg);
- }
- }
- else
- if (strcmp(op, "unfavourite") == 0) {
-
- }
- else
- if (strcmp(op, "reblog") == 0) {
- xs *n_msg = msg_admiration(&snac, id, "Announce");
- if (n_msg != NULL) {
- enqueue_message(&snac, n_msg);
- timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
- out = mastoapi_status(&snac, msg);
- }
- }
- else
- if (strcmp(op, "unreblog") == 0) {
-
- }
- else
- if (strcmp(op, "bookmark") == 0) {
-
- }
- else
- if (strcmp(op, "unbookmark") == 0) {
-
- }
- else
- if (strcmp(op, "pin") == 0) {
-
- }
- else
- if (strcmp(op, "unpin") == 0) {
-
- }
- else
- if (strcmp(op, "mute") == 0) {
-
- }
- else
- if (strcmp(op, "unmute") == 0) {
-
- }
- }
- if (out != NULL) {
- *body = xs_json_dumps_pp(out, 4);
- *ctype = "application/json";
- status = 200;
- }
- }
- }
- else
- status = 401;
- }
- else
- if (strcmp(cmd, "/v1/notifications/clear") == 0) {
- if (logged_in) {
- notify_clear(&snac);
- timeline_touch(&snac);
- *body = xs_dup("{}");
- *ctype = "application/json";
- status = 200;
- }
- else
- status = 401;
- }
- else
- if (strcmp(cmd, "/v1/push/subscription") == 0) {
-
- if (logged_in) {
- char *v;
- xs *wpush = xs_dict_new();
- wpush = xs_dict_append(wpush, "id", "1");
- v = xs_dict_get(args, "data");
- v = xs_dict_get(v, "alerts");
- wpush = xs_dict_append(wpush, "alerts", v);
- v = xs_dict_get(args, "subscription");
- v = xs_dict_get(v, "endpoint");
- wpush = xs_dict_append(wpush, "endpoint", v);
- xs *server_key = random_str();
- wpush = xs_dict_append(wpush, "server_key", server_key);
- *body = xs_json_dumps_pp(wpush, 4);
- *ctype = "application/json";
- status = 200;
- }
- else
- status = 401;
- }
- else
- if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) {
- if (logged_in) {
- const xs_list *file = xs_dict_get(args, "file");
- const char *desc = xs_dict_get(args, "description");
- if (xs_is_null(desc))
- desc = "";
- status = 400;
- if (xs_type(file) == XSTYPE_LIST) {
- const char *fn = xs_list_get(file, 0);
- if (*fn != '\0') {
- char *ext = strrchr(fn, '.');
- xs *hash = xs_md5_hex(fn, strlen(fn));
- xs *id = xs_fmt("%s%s", hash, ext);
- xs *url = xs_fmt("%s/s/%s", snac.actor, id);
- int fo = xs_number_get(xs_list_get(file, 1));
- int fs = xs_number_get(xs_list_get(file, 2));
-
- static_put(&snac, id, payload + fo, fs);
- static_put_meta(&snac, id, desc);
-
- xs *rsp = xs_dict_new();
- rsp = xs_dict_append(rsp, "id", id);
- rsp = xs_dict_append(rsp, "type", "image");
- rsp = xs_dict_append(rsp, "url", url);
- rsp = xs_dict_append(rsp, "preview_url", url);
- rsp = xs_dict_append(rsp, "remote_url", url);
- rsp = xs_dict_append(rsp, "description", desc);
- *body = xs_json_dumps_pp(rsp, 4);
- *ctype = "application/json";
- status = 200;
- }
- }
- }
- else
- status = 401;
- }
-
- if (logged_in)
- user_free(&snac);
- return status;
- }
- int mastoapi_put_handler(const xs_dict *req, const char *q_path,
- const char *payload, int p_size,
- char **body, int *b_size, char **ctype)
- {
- if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
- return 0;
- srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
- int status = 404;
- xs *args = NULL;
- char *i_ctype = xs_dict_get(req, "content-type");
- if (i_ctype && xs_startswith(i_ctype, "application/json"))
- args = xs_json_loads(payload);
- else
- args = xs_dup(xs_dict_get(req, "p_vars"));
- if (args == NULL)
- return 400;
- xs *cmd = xs_replace(q_path, "/api", "");
- snac snac = {0};
- int logged_in = process_auth_token(&snac, req);
- if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) {
- if (logged_in) {
- xs *l = xs_split(cmd, "/");
- const char *stid = xs_list_get(l, 3);
- if (!xs_is_null(stid)) {
- const char *desc = xs_dict_get(args, "description");
-
- static_put_meta(&snac, stid, desc);
-
- xs *rsp = xs_dict_new();
- xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
- rsp = xs_dict_append(rsp, "id", stid);
- rsp = xs_dict_append(rsp, "type", "image");
- rsp = xs_dict_append(rsp, "url", url);
- rsp = xs_dict_append(rsp, "preview_url", url);
- rsp = xs_dict_append(rsp, "remote_url", url);
- rsp = xs_dict_append(rsp, "description", desc);
- *body = xs_json_dumps_pp(rsp, 4);
- *ctype = "application/json";
- status = 200;
- }
- }
- else
- status = 401;
- }
-
- if (logged_in)
- user_free(&snac);
- return status;
- }
- #endif
|