2022-09-19 22:32:36 +03:00
|
|
|
/* snac - A simple, minimalistic ActivityPub instance */
|
2023-01-17 11:50:16 +03:00
|
|
|
/* copyright (c) 2022 - 2023 grunfink / MIT license */
|
2022-09-19 22:32:36 +03:00
|
|
|
|
|
|
|
#include "xs.h"
|
2022-09-27 10:51:39 +03:00
|
|
|
#include "xs_io.h"
|
2022-09-21 10:31:05 +03:00
|
|
|
#include "xs_json.h"
|
2022-09-19 22:32:36 +03:00
|
|
|
|
|
|
|
#include "snac.h"
|
|
|
|
|
2023-02-07 11:01:57 +03:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2022-09-21 19:27:30 +03:00
|
|
|
int usage(void)
|
|
|
|
{
|
2022-10-03 20:17:12 +03:00
|
|
|
printf("snac " VERSION " - A simple, minimalistic ActivityPub instance\n");
|
2023-01-17 11:50:16 +03:00
|
|
|
printf("Copyright (c) 2022 - 2023 grunfink / MIT license\n");
|
2022-09-25 10:07:43 +03:00
|
|
|
printf("\n");
|
|
|
|
printf("Commands:\n");
|
|
|
|
printf("\n");
|
2023-01-13 17:15:14 +03:00
|
|
|
printf("init [{basedir}] Initializes the data storage\n");
|
2022-11-25 12:53:16 +03:00
|
|
|
printf("upgrade {basedir} Upgrade to a new version\n");
|
2022-10-04 10:40:16 +03:00
|
|
|
printf("adduser {basedir} [{uid}] Adds a new user\n");
|
2022-09-25 10:07:43 +03:00
|
|
|
printf("httpd {basedir} Starts the HTTPD daemon\n");
|
2022-10-04 19:46:12 +03:00
|
|
|
printf("purge {basedir} Purges old data\n");
|
2022-09-25 10:07:43 +03:00
|
|
|
printf("webfinger {basedir} {user} Queries about a @user@host or actor\n");
|
|
|
|
printf("queue {basedir} {uid} Processes a user queue\n");
|
2022-09-27 16:28:08 +03:00
|
|
|
printf("follow {basedir} {uid} {actor} Follows an actor\n");
|
2022-10-13 16:44:38 +03:00
|
|
|
printf("unfollow {basedir} {uid} {actor} Unfollows an actor\n");
|
2022-09-25 10:07:43 +03:00
|
|
|
printf("request {basedir} {uid} {url} Requests an object\n");
|
|
|
|
printf("actor {basedir} {uid} {url} Requests an actor\n");
|
2022-09-27 10:51:39 +03:00
|
|
|
printf("note {basedir} {uid} {'text'} Sends a note to followers\n");
|
2022-12-04 23:26:24 +03:00
|
|
|
printf("resetpwd {basedir} {uid} Resets the password of a user\n");
|
2023-05-05 10:54:41 +03:00
|
|
|
printf("ping {basedir} {uid} {actor} Pings an actor\n");
|
2023-05-24 12:49:16 +03:00
|
|
|
/* printf("question {basedir} {uid} 'opts' Generates a poll (;-separated opts)\n");*/
|
2022-09-25 10:07:43 +03:00
|
|
|
|
2022-09-21 19:27:30 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-25 10:07:43 +03:00
|
|
|
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)
|
|
|
|
|
2022-10-26 07:43:47 +03:00
|
|
|
d_char *html_timeline(snac *snac, char *list, int local);
|
2022-09-25 22:02:47 +03:00
|
|
|
|
2022-09-19 22:32:36 +03:00
|
|
|
int main(int argc, char *argv[])
|
2022-09-21 19:27:30 +03:00
|
|
|
{
|
|
|
|
char *cmd;
|
|
|
|
char *basedir;
|
2022-09-22 12:28:13 +03:00
|
|
|
char *user;
|
2022-09-23 18:33:33 +03:00
|
|
|
char *url;
|
2022-09-21 19:27:30 +03:00
|
|
|
int argi = 1;
|
2022-09-23 19:15:59 +03:00
|
|
|
snac snac;
|
2022-09-21 19:27:30 +03:00
|
|
|
|
2023-02-07 11:01:57 +03:00
|
|
|
/* ensure group has write access */
|
|
|
|
umask(0007);
|
|
|
|
|
2022-09-25 10:07:43 +03:00
|
|
|
if ((cmd = GET_ARGV()) == NULL)
|
2022-09-21 19:27:30 +03:00
|
|
|
return usage();
|
|
|
|
|
2023-05-24 12:49:16 +03:00
|
|
|
if (strcmp(cmd, "init") == 0) { /** **/
|
2023-01-13 17:15:14 +03:00
|
|
|
/* initialize the data storage */
|
2022-09-25 10:07:43 +03:00
|
|
|
/* ... */
|
|
|
|
basedir = GET_ARGV();
|
|
|
|
|
2023-01-31 20:38:56 +03:00
|
|
|
return snac_init(basedir);
|
2022-09-21 19:27:30 +03:00
|
|
|
}
|
|
|
|
|
2023-05-24 12:49:16 +03:00
|
|
|
if (strcmp(cmd, "upgrade") == 0) { /** **/
|
2022-11-25 12:53:16 +03:00
|
|
|
int ret;
|
|
|
|
|
2023-01-13 17:15:14 +03:00
|
|
|
/* upgrade */
|
2022-11-25 12:53:16 +03:00
|
|
|
if ((basedir = GET_ARGV()) == NULL)
|
|
|
|
return usage();
|
|
|
|
|
|
|
|
if ((ret = srv_open(basedir, 1)) == 1)
|
|
|
|
srv_log(xs_dup("OK"));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:49:16 +03:00
|
|
|
if (strcmp(cmd, "markdown") == 0) { /** **/
|
2022-11-09 13:53:16 +03:00
|
|
|
/* undocumented, for testing only */
|
|
|
|
xs *c = xs_readall(stdin);
|
2023-05-21 21:32:23 +03:00
|
|
|
xs *fc = not_really_markdown(c, NULL);
|
2022-11-09 13:53:16 +03:00
|
|
|
|
|
|
|
printf("<html>\n%s\n</html>\n", fc);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-09-25 10:07:43 +03:00
|
|
|
if ((basedir = GET_ARGV()) == NULL)
|
2022-09-21 19:27:30 +03:00
|
|
|
return usage();
|
|
|
|
|
2022-11-25 12:53:16 +03:00
|
|
|
if (!srv_open(basedir, 0)) {
|
2023-01-13 17:15:14 +03:00
|
|
|
srv_log(xs_fmt("error opening data storage at %s", basedir));
|
2022-09-21 19:27:30 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:49:16 +03:00
|
|
|
if (strcmp(cmd, "adduser") == 0) { /** **/
|
2022-10-04 10:40:16 +03:00
|
|
|
user = GET_ARGV();
|
|
|
|
|
|
|
|
return adduser(user);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:49:16 +03:00
|
|
|
if (strcmp(cmd, "httpd") == 0) { /** **/
|
2022-09-21 19:27:30 +03:00
|
|
|
httpd();
|
2022-10-25 11:38:32 +03:00
|
|
|
srv_free();
|
2022-09-21 19:27:30 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:49:16 +03:00
|
|
|
if (strcmp(cmd, "purge") == 0) { /** **/
|
2022-10-17 12:00:34 +03:00
|
|
|
purge_all();
|
2022-10-04 19:46:12 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-09-25 10:07:43 +03:00
|
|
|
if ((user = GET_ARGV()) == NULL)
|
2022-09-22 12:28:13 +03:00
|
|
|
return usage();
|
|
|
|
|
2023-05-24 12:49:16 +03:00
|
|
|
if (strcmp(cmd, "webfinger") == 0) { /** **/
|
2022-09-22 12:28:13 +03:00
|
|
|
xs *actor = NULL;
|
|
|
|
xs *uid = NULL;
|
|
|
|
int status;
|
|
|
|
|
2022-09-23 18:36:40 +03:00
|
|
|
status = webfinger_request(user, &actor, &uid);
|
2022-09-22 12:28:13 +03:00
|
|
|
|
|
|
|
printf("status: %d\n", status);
|
|
|
|
if (actor != NULL)
|
|
|
|
printf("actor: %s\n", actor);
|
|
|
|
if (uid != NULL)
|
|
|
|
printf("uid: %s\n", uid);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-09-23 19:15:59 +03:00
|
|
|
if (!user_open(&snac, user)) {
|
|
|
|
printf("error in user '%s'\n", user);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-05-08 10:02:45 +03:00
|
|
|
lastlog_write(&snac, "cmdline");
|
|
|
|
|
2023-05-24 12:49:16 +03:00
|
|
|
if (strcmp(cmd, "resetpwd") == 0) { /** **/
|
2022-12-04 23:26:24 +03:00
|
|
|
return resetpwd(&snac);
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:49:16 +03:00
|
|
|
if (strcmp(cmd, "queue") == 0) { /** **/
|
2023-02-02 06:50:51 +03:00
|
|
|
process_user_queue(&snac);
|
2022-09-25 10:07:43 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:49:16 +03:00
|
|
|
if (strcmp(cmd, "timeline") == 0) { /** **/
|
2022-11-26 20:52:51 +03:00
|
|
|
#if 0
|
2022-11-25 14:21:26 +03:00
|
|
|
xs *list = local_list(&snac, XS_ALL);
|
2022-10-26 07:43:47 +03:00
|
|
|
xs *body = html_timeline(&snac, list, 1);
|
|
|
|
|
|
|
|
printf("%s\n", body);
|
|
|
|
user_free(&snac);
|
|
|
|
srv_free();
|
2022-11-26 20:52:51 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
xs *idx = xs_fmt("%s/private.idx", snac.basedir);
|
2022-12-04 23:28:29 +03:00
|
|
|
xs *list = index_list_desc(idx, 0, 256);
|
2023-02-08 15:21:44 +03:00
|
|
|
xs *tl = timeline_top_level(&snac, list);
|
2022-11-26 20:52:51 +03:00
|
|
|
|
|
|
|
xs *j = xs_json_dumps_pp(tl, 4);
|
|
|
|
printf("%s\n", j);
|
2022-10-26 07:43:47 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-09-25 10:07:43 +03:00
|
|
|
if ((url = GET_ARGV()) == NULL)
|
|
|
|
return usage();
|
|
|
|
|
2023-05-24 12:49:16 +03:00
|
|
|
if (strcmp(cmd, "announce") == 0) { /** **/
|
2022-09-26 11:08:14 +03:00
|
|
|
xs *msg = msg_admiration(&snac, url, "Announce");
|
|
|
|
|
2022-09-26 12:19:45 +03:00
|
|
|
if (msg != NULL) {
|
2022-12-16 09:16:00 +03:00
|
|
|
enqueue_message(&snac, msg);
|
2022-09-26 12:19:45 +03:00
|
|
|
|
2022-09-27 16:28:08 +03:00
|
|
|
if (dbglevel) {
|
|
|
|
xs *j = xs_json_dumps_pp(msg, 4);
|
|
|
|
printf("%s\n", j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:49:16 +03:00
|
|
|
if (strcmp(cmd, "follow") == 0) { /** **/
|
2022-09-27 16:28:08 +03:00
|
|
|
xs *msg = msg_follow(&snac, url);
|
|
|
|
|
|
|
|
if (msg != NULL) {
|
|
|
|
char *actor = xs_dict_get(msg, "object");
|
|
|
|
|
|
|
|
following_add(&snac, actor, msg);
|
|
|
|
|
2022-11-18 10:21:40 +03:00
|
|
|
enqueue_output_by_actor(&snac, msg, actor, 0);
|
2022-09-27 16:28:08 +03:00
|
|
|
|
|
|
|
if (dbglevel) {
|
|
|
|
xs *j = xs_json_dumps_pp(msg, 4);
|
|
|
|
printf("%s\n", j);
|
|
|
|
}
|
2022-09-26 11:08:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:49:16 +03:00
|
|
|
if (strcmp(cmd, "unfollow") == 0) { /** **/
|
2022-10-13 16:44:38 +03:00
|
|
|
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);
|
|
|
|
|
2022-11-18 10:21:40 +03:00
|
|
|
enqueue_output_by_actor(&snac, msg, url, 0);
|
2022-10-13 16:44:38 +03:00
|
|
|
|
|
|
|
snac_log(&snac, xs_fmt("unfollowed actor %s", url));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
snac_log(&snac, xs_fmt("actor is not being followed %s", url));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:49:16 +03:00
|
|
|
if (strcmp(cmd, "ping") == 0) { /** **/
|
2023-05-05 13:47:17 +03:00
|
|
|
xs *actor_o = NULL;
|
2023-05-05 10:54:41 +03:00
|
|
|
|
2023-05-05 13:47:17 +03:00
|
|
|
if (valid_status(actor_request(&snac, url, &actor_o))) {
|
|
|
|
xs *msg = msg_ping(&snac, url);
|
2023-05-05 10:54:41 +03:00
|
|
|
|
2023-05-05 13:47:17 +03:00
|
|
|
enqueue_output_by_actor(&snac, msg, url, 0);
|
|
|
|
|
|
|
|
if (dbglevel) {
|
|
|
|
xs *j = xs_json_dumps_pp(msg, 4);
|
|
|
|
printf("%s\n", j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
srv_log(xs_fmt("Error getting actor %s", url));
|
|
|
|
return 1;
|
2023-05-05 10:54:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:49:16 +03:00
|
|
|
if (strcmp(cmd, "question") == 0) { /** **/
|
2023-05-29 12:07:38 +03:00
|
|
|
int end_secs = 5 * 60;
|
2023-05-24 12:49:16 +03:00
|
|
|
xs *opts = xs_split(url, ";");
|
|
|
|
|
2023-05-30 06:54:45 +03:00
|
|
|
xs *msg = msg_question(&snac, "Poll", NULL, opts, 0, end_secs);
|
2023-05-24 12:49:16 +03:00
|
|
|
xs *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);
|
2023-05-29 12:07:38 +03:00
|
|
|
enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
|
2023-05-24 12:49:16 +03:00
|
|
|
|
|
|
|
timeline_add(&snac, xs_dict_get(msg, "id"), msg);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(cmd, "request") == 0) { /** **/
|
2022-09-23 19:15:59 +03:00
|
|
|
int status;
|
|
|
|
xs *data = NULL;
|
|
|
|
|
|
|
|
status = activitypub_request(&snac, url, &data);
|
|
|
|
|
|
|
|
printf("status: %d\n", status);
|
|
|
|
|
2023-06-07 14:09:19 +03:00
|
|
|
if (data != NULL) {
|
2022-09-23 19:15:59 +03:00
|
|
|
xs *j = xs_json_dumps_pp(data, 4);
|
|
|
|
printf("%s\n", j);
|
|
|
|
}
|
2022-09-23 19:46:30 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:49:16 +03:00
|
|
|
if (strcmp(cmd, "actor") == 0) { /** **/
|
2022-09-23 19:46:30 +03:00
|
|
|
int status;
|
|
|
|
xs *data = NULL;
|
|
|
|
|
|
|
|
status = actor_request(&snac, url, &data);
|
|
|
|
|
|
|
|
printf("status: %d\n", status);
|
|
|
|
|
2022-09-27 10:38:46 +03:00
|
|
|
if (valid_status(status)) {
|
|
|
|
xs *j = xs_json_dumps_pp(data, 4);
|
|
|
|
printf("%s\n", j);
|
|
|
|
}
|
|
|
|
|
2022-09-23 19:46:30 +03:00
|
|
|
return 0;
|
2022-09-23 18:33:33 +03:00
|
|
|
}
|
|
|
|
|
2023-05-24 12:49:16 +03:00
|
|
|
if (strcmp(cmd, "note") == 0) { /** **/
|
2022-09-27 10:51:39 +03:00
|
|
|
xs *content = NULL;
|
2022-09-27 15:07:36 +03:00
|
|
|
xs *msg = NULL;
|
|
|
|
xs *c_msg = NULL;
|
2022-09-27 15:50:13 +03:00
|
|
|
char *in_reply_to = GET_ARGV();
|
2022-09-27 10:51:39 +03:00
|
|
|
|
2023-02-05 21:54:41 +03:00
|
|
|
if (strcmp(url, "-e") == 0) {
|
2022-09-27 10:51:39 +03:00
|
|
|
/* get the content from an editor */
|
|
|
|
FILE *f;
|
|
|
|
|
2023-02-05 21:54:41 +03:00
|
|
|
unlink("/tmp/snac-edit.txt");
|
2022-09-27 10:51:39 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2023-02-05 21:54:41 +03:00
|
|
|
else
|
|
|
|
if (strcmp(url, "-") == 0) {
|
|
|
|
/* get the content from stdin */
|
|
|
|
content = xs_readall(stdin);
|
|
|
|
}
|
2022-09-27 10:51:39 +03:00
|
|
|
else
|
|
|
|
content = xs_dup(url);
|
|
|
|
|
2023-02-20 11:32:44 +03:00
|
|
|
msg = msg_note(&snac, content, NULL, in_reply_to, NULL, 0);
|
2022-09-27 15:07:36 +03:00
|
|
|
|
|
|
|
c_msg = msg_create(&snac, msg);
|
|
|
|
|
2022-09-27 16:28:08 +03:00
|
|
|
if (dbglevel) {
|
2022-09-27 15:07:36 +03:00
|
|
|
xs *j = xs_json_dumps_pp(c_msg, 4);
|
|
|
|
printf("%s\n", j);
|
|
|
|
}
|
2022-09-27 10:51:39 +03:00
|
|
|
|
2022-12-16 09:16:00 +03:00
|
|
|
enqueue_message(&snac, c_msg);
|
2022-09-27 10:51:39 +03:00
|
|
|
|
2023-01-11 22:40:13 +03:00
|
|
|
timeline_add(&snac, xs_dict_get(msg, "id"), msg);
|
2022-09-27 15:50:13 +03:00
|
|
|
|
2022-09-27 10:51:39 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:27:30 +03:00
|
|
|
return 0;
|
|
|
|
}
|