2022-09-19 22:32:36 +03:00
|
|
|
/* snac - A simple, minimalistic ActivityPub instance */
|
|
|
|
/* copyright (c) 2022 grunfink - MIT license */
|
|
|
|
|
|
|
|
#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_encdec.h"
|
|
|
|
#include "xs_json.h"
|
2022-09-19 22:32:36 +03:00
|
|
|
|
|
|
|
#include "snac.h"
|
|
|
|
|
2022-09-21 19:27:30 +03:00
|
|
|
int usage(void)
|
|
|
|
{
|
2022-09-25 10:07:43 +03:00
|
|
|
printf("snac - 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("httpd {basedir} Starts the HTTPD daemon\n");
|
|
|
|
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-09-25 10:07:43 +03:00
|
|
|
// printf("check {basedir} [{uid}] Checks the database\n");
|
|
|
|
// printf("purge {basedir} [{uid}] Purges old data\n");
|
|
|
|
// printf("adduser {basedir} [{uid}] Adds a new user\n");
|
|
|
|
|
|
|
|
// printf("update {basedir} {uid} Sends a user update to followers\n");
|
|
|
|
// printf("passwd {basedir} {uid} Sets the password for {uid}\n");
|
|
|
|
// printf("unfollow {basedir} {uid} {actor} Unfollows an actor\n");
|
|
|
|
// printf("mute {basedir} {uid} {actor} Mutes an actor\n");
|
|
|
|
// printf("unmute {basedir} {uid} {actor} Unmutes an actor\n");
|
|
|
|
// printf("like {basedir} {uid} {url} Likes an url\n");
|
|
|
|
// printf("announce {basedir} {uid} {url} Announces (boosts) an url\n");
|
|
|
|
// printf("note {basedir} {uid} {'text'} Sends a note to followers\n");
|
|
|
|
|
|
|
|
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-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-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
|
|
|
|
2022-09-25 10:07:43 +03:00
|
|
|
if ((cmd = GET_ARGV()) == NULL)
|
2022-09-21 19:27:30 +03:00
|
|
|
return usage();
|
|
|
|
|
|
|
|
if (strcmp(cmd, "init") == 0) {
|
2022-09-25 10:07:43 +03:00
|
|
|
/* initialize the database */
|
|
|
|
/* ... */
|
|
|
|
basedir = GET_ARGV();
|
|
|
|
|
2022-09-21 19:27:30 +03:00
|
|
|
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();
|
|
|
|
|
|
|
|
if (!srv_open(basedir)) {
|
|
|
|
srv_log(xs_fmt("error opening database at %s", basedir));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(cmd, "httpd") == 0) {
|
|
|
|
httpd();
|
|
|
|
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();
|
|
|
|
|
|
|
|
if (strcmp(cmd, "webfinger") == 0) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-09-25 10:07:43 +03:00
|
|
|
if (strcmp(cmd, "queue") == 0) {
|
|
|
|
process_queue(&snac);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((url = GET_ARGV()) == NULL)
|
|
|
|
return usage();
|
|
|
|
|
2022-09-26 11:08:14 +03:00
|
|
|
if (strcmp(cmd, "announce") == 0) {
|
|
|
|
xs *msg = msg_admiration(&snac, url, "Announce");
|
|
|
|
|
2022-09-26 12:19:45 +03:00
|
|
|
if (msg != NULL) {
|
|
|
|
post(&snac, msg);
|
|
|
|
|
2022-09-27 16:28:08 +03:00
|
|
|
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(&snac, msg, actor, 0);
|
|
|
|
|
|
|
|
if (dbglevel) {
|
|
|
|
xs *j = xs_json_dumps_pp(msg, 4);
|
|
|
|
printf("%s\n", j);
|
|
|
|
}
|
2022-09-26 11:08:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-09-23 18:33:33 +03:00
|
|
|
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);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(cmd, "actor") == 0) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-09-27 10:51:39 +03:00
|
|
|
if (strcmp(cmd, "note") == 0) {
|
|
|
|
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
|
|
|
|
|
|
|
if (strcmp(url, "-") == 0) {
|
|
|
|
/* get the content from an editor */
|
|
|
|
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);
|
|
|
|
|
2022-09-27 15:50:13 +03:00
|
|
|
msg = msg_note(&snac, content, NULL, in_reply_to);
|
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-09-27 15:07:36 +03:00
|
|
|
post(&snac, c_msg);
|
2022-09-27 10:51:39 +03:00
|
|
|
|
2022-09-27 15:50:13 +03:00
|
|
|
timeline_add(&snac, xs_dict_get(msg, "id"), msg, in_reply_to, NULL);
|
|
|
|
|
2022-09-27 10:51:39 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:27:30 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
2022-09-19 22:32:36 +03:00
|
|
|
{
|
2022-09-19 23:19:14 +03:00
|
|
|
snac snac;
|
|
|
|
|
2022-09-20 13:00:13 +03:00
|
|
|
printf("%s\n", tid(0));
|
2022-09-19 23:19:14 +03:00
|
|
|
|
2022-09-20 10:39:28 +03:00
|
|
|
srv_open("/home/angel/lib/snac/comam.es/");
|
2022-09-19 22:32:36 +03:00
|
|
|
|
2022-09-20 00:03:18 +03:00
|
|
|
user_open(&snac, "mike");
|
2022-09-20 10:48:13 +03:00
|
|
|
|
2022-09-21 09:57:02 +03:00
|
|
|
xs *headers = xs_dict_new();
|
2022-09-20 22:00:16 +03:00
|
|
|
int status;
|
|
|
|
d_char *payload;
|
|
|
|
int p_size;
|
2022-09-21 10:31:05 +03:00
|
|
|
xs *response;
|
|
|
|
|
|
|
|
response = http_signed_request(&snac, "GET", "https://mastodon.social/users/VictorMoral",
|
2022-09-20 22:00:16 +03:00
|
|
|
headers, NULL, 0, &status, &payload, &p_size);
|
|
|
|
|
2022-09-21 10:31:05 +03:00
|
|
|
{
|
|
|
|
xs *j1 = xs_json_dumps_pp(response, 4);
|
|
|
|
printf("response:\n%s\n", j1);
|
|
|
|
printf("payload:\n%s\n", payload);
|
|
|
|
}
|
|
|
|
|
2022-09-20 13:43:49 +03:00
|
|
|
{
|
|
|
|
xs *list = queue(&snac);
|
|
|
|
char *p, *fn;
|
|
|
|
|
|
|
|
p = list;
|
|
|
|
while (xs_list_iter(&p, &fn)) {
|
2022-09-20 13:50:37 +03:00
|
|
|
xs *obj;
|
|
|
|
|
|
|
|
obj = dequeue(&snac, fn);
|
|
|
|
printf("%s\n", xs_dict_get(obj, "actor"));
|
2022-09-20 13:43:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2022-09-20 10:48:13 +03:00
|
|
|
{
|
|
|
|
xs *list = follower_list(&snac);
|
|
|
|
char *p, *obj;
|
|
|
|
|
|
|
|
p = list;
|
|
|
|
while (xs_list_iter(&p, &obj)) {
|
|
|
|
char *actor = xs_dict_get(obj, "actor");
|
|
|
|
printf("%s\n", actor);
|
|
|
|
}
|
|
|
|
}
|
2022-09-19 23:19:14 +03:00
|
|
|
|
2022-09-20 11:49:24 +03:00
|
|
|
{
|
|
|
|
xs *list = timeline_list(&snac);
|
|
|
|
char *p, *fn;
|
|
|
|
|
|
|
|
p = list;
|
|
|
|
while (xs_list_iter(&p, &fn)) {
|
|
|
|
xs *tle = timeline_get(&snac, fn);
|
|
|
|
|
|
|
|
printf("%s\n", xs_dict_get(tle, "id"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 00:33:11 +03:00
|
|
|
{
|
|
|
|
xs *list = user_list();
|
|
|
|
char *p, *uid;
|
|
|
|
|
|
|
|
p = list;
|
|
|
|
while (xs_list_iter(&p, &uid)) {
|
2022-09-20 10:39:28 +03:00
|
|
|
if (user_open(&snac, uid)) {
|
|
|
|
printf("%s (%s)\n", uid, xs_dict_get(snac.config, "name"));
|
|
|
|
user_free(&snac);
|
|
|
|
}
|
2022-09-20 00:33:11 +03:00
|
|
|
}
|
|
|
|
}
|
2022-09-20 13:43:49 +03:00
|
|
|
#endif
|
2022-09-20 00:33:11 +03:00
|
|
|
|
2022-09-19 22:32:36 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2022-09-21 19:27:30 +03:00
|
|
|
#endif
|