123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- #include "xs.h"
- #include "xs_io.h"
- #include "xs_encdec.h"
- #include "xs_json.h"
- #include "snac.h"
- int usage(void)
- {
- printf("snac " VERSION " - A simple, minimalistic ActivityPub instance\n");
- printf("Copyright (c) 2022 grunfink - MIT license\n");
- printf("\n");
- printf("Commands:\n");
- printf("\n");
- printf("init [{basedir}] Initializes the database\n");
- printf("upgrade {basedir} Upgrade to a new version\n");
- printf("adduser {basedir} [{uid}] Adds a new user\n");
- printf("httpd {basedir} Starts the HTTPD daemon\n");
- printf("purge {basedir} Purges old data\n");
- printf("webfinger {basedir} {user} Queries about a @user@host or actor\n");
- printf("queue {basedir} {uid} Processes a user queue\n");
- printf("follow {basedir} {uid} {actor} Follows an actor\n");
- printf("unfollow {basedir} {uid} {actor} Unfollows an actor\n");
- printf("request {basedir} {uid} {url} Requests an object\n");
- printf("actor {basedir} {uid} {url} Requests an actor\n");
- printf("note {basedir} {uid} {'text'} Sends a note to followers\n");
- printf("resetpwd {basedir} {uid} Resets the password of a user\n");
- return 1;
- }
- char *get_argv(int *argi, int argc, char *argv[])
- {
- if (*argi < argc)
- return argv[(*argi)++];
- else
- return NULL;
- }
- #define GET_ARGV() get_argv(&argi, argc, argv)
- d_char *html_timeline(snac *snac, char *list, int local);
- int main(int argc, char *argv[])
- {
- char *cmd;
- char *basedir;
- char *user;
- char *url;
- int argi = 1;
- snac snac;
- if ((cmd = GET_ARGV()) == NULL)
- return usage();
- if (strcmp(cmd, "init") == 0) {
-
-
- basedir = GET_ARGV();
- return initdb(basedir);
- }
- if (strcmp(cmd, "upgrade") == 0) {
- int ret;
-
- if ((basedir = GET_ARGV()) == NULL)
- return usage();
- if ((ret = srv_open(basedir, 1)) == 1)
- srv_log(xs_dup("OK"));
- return ret;
- }
- if (strcmp(cmd, "markdown") == 0) {
-
- xs *c = xs_readall(stdin);
- xs *fc = not_really_markdown(c);
- printf("<html>\n%s\n</html>\n", fc);
- return 0;
- }
- if ((basedir = GET_ARGV()) == NULL)
- return usage();
- if (!srv_open(basedir, 0)) {
- srv_log(xs_fmt("error opening database at %s", basedir));
- return 1;
- }
- if (strcmp(cmd, "adduser") == 0) {
- user = GET_ARGV();
- return adduser(user);
- return 0;
- }
- if (strcmp(cmd, "httpd") == 0) {
- httpd();
- srv_free();
- return 0;
- }
- if (strcmp(cmd, "purge") == 0) {
- purge_all();
- return 0;
- }
- if ((user = GET_ARGV()) == NULL)
- return usage();
- if (strcmp(cmd, "webfinger") == 0) {
- xs *actor = NULL;
- xs *uid = NULL;
- int status;
- status = webfinger_request(user, &actor, &uid);
- printf("status: %d\n", status);
- if (actor != NULL)
- printf("actor: %s\n", actor);
- if (uid != NULL)
- printf("uid: %s\n", uid);
- return 0;
- }
- if (!user_open(&snac, user)) {
- printf("error in user '%s'\n", user);
- return 1;
- }
- if (strcmp(cmd, "resetpwd") == 0) {
- return resetpwd(&snac);
- }
- if (strcmp(cmd, "queue") == 0) {
- process_queue(&snac);
- return 0;
- }
- if (strcmp(cmd, "timeline") == 0) {
- #if 0
- xs *list = local_list(&snac, XS_ALL);
- xs *body = html_timeline(&snac, list, 1);
- printf("%s\n", body);
- user_free(&snac);
- srv_free();
- #endif
- xs *idx = xs_fmt("%s/private.idx", snac.basedir);
- xs *list = index_list_desc(idx, 0, 256);
- xs *tl = timeline_top_level(list);
- xs *j = xs_json_dumps_pp(tl, 4);
- printf("%s\n", j);
- return 0;
- }
- if ((url = GET_ARGV()) == NULL)
- return usage();
- if (strcmp(cmd, "announce") == 0) {
- xs *msg = msg_admiration(&snac, url, "Announce");
- if (msg != NULL) {
- enqueue_message(&snac, msg);
- if (dbglevel) {
- xs *j = xs_json_dumps_pp(msg, 4);
- printf("%s\n", j);
- }
- }
- return 0;
- }
- if (strcmp(cmd, "follow") == 0) {
- xs *msg = msg_follow(&snac, url);
- if (msg != NULL) {
- char *actor = xs_dict_get(msg, "object");
- following_add(&snac, actor, msg);
- enqueue_output_by_actor(&snac, msg, actor, 0);
- if (dbglevel) {
- xs *j = xs_json_dumps_pp(msg, 4);
- printf("%s\n", j);
- }
- }
- return 0;
- }
- if (strcmp(cmd, "unfollow") == 0) {
- xs *object = NULL;
- if (valid_status(following_get(&snac, url, &object))) {
- xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
- following_del(&snac, url);
- enqueue_output_by_actor(&snac, msg, url, 0);
- snac_log(&snac, xs_fmt("unfollowed actor %s", url));
- }
- else
- snac_log(&snac, xs_fmt("actor is not being followed %s", url));
- return 0;
- }
- if (strcmp(cmd, "request") == 0) {
- int status;
- xs *data = NULL;
- status = activitypub_request(&snac, url, &data);
- printf("status: %d\n", status);
- if (valid_status(status)) {
- xs *j = xs_json_dumps_pp(data, 4);
- printf("%s\n", j);
- }
- return 0;
- }
- if (strcmp(cmd, "actor") == 0) {
- int status;
- xs *data = NULL;
- status = actor_request(&snac, url, &data);
- printf("status: %d\n", status);
- if (valid_status(status)) {
- xs *j = xs_json_dumps_pp(data, 4);
- printf("%s\n", j);
- }
- return 0;
- }
- if (strcmp(cmd, "note") == 0) {
- xs *content = NULL;
- xs *msg = NULL;
- xs *c_msg = NULL;
- char *in_reply_to = GET_ARGV();
- if (strcmp(url, "-") == 0) {
-
- FILE *f;
- system("$EDITOR /tmp/snac-edit.txt");
- if ((f = fopen("/tmp/snac-edit.txt", "r")) != NULL) {
- content = xs_readall(f);
- fclose(f);
- unlink("/tmp/snac-edit.txt");
- }
- else {
- printf("Nothing to send\n");
- return 1;
- }
- }
- else
- content = xs_dup(url);
- msg = msg_note(&snac, content, NULL, in_reply_to, NULL);
- c_msg = msg_create(&snac, msg);
- if (dbglevel) {
- xs *j = xs_json_dumps_pp(c_msg, 4);
- printf("%s\n", j);
- }
- enqueue_message(&snac, c_msg);
- timeline_add(&snac, xs_dict_get(msg, "id"), msg);
- return 0;
- }
- return 0;
- }
|