2022-09-19 19:32:36 +00:00
|
|
|
/* snac - A simple, minimalistic ActivityPub instance */
|
|
|
|
/* copyright (c) 2022 grunfink - MIT license */
|
|
|
|
|
|
|
|
#include "xs.h"
|
2022-09-21 07:31:05 +00:00
|
|
|
#include "xs_encdec.h"
|
|
|
|
#include "xs_json.h"
|
2022-09-19 19:32:36 +00:00
|
|
|
|
|
|
|
#include "snac.h"
|
|
|
|
|
2022-09-21 16:27:30 +00:00
|
|
|
int usage(void)
|
|
|
|
{
|
|
|
|
printf("usage:\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-19 19:32:36 +00:00
|
|
|
int main(int argc, char *argv[])
|
2022-09-21 16:27:30 +00:00
|
|
|
{
|
|
|
|
char *cmd;
|
|
|
|
char *basedir;
|
2022-09-22 09:28:13 +00:00
|
|
|
char *user;
|
2022-09-23 15:33:33 +00:00
|
|
|
char *url;
|
2022-09-21 16:27:30 +00:00
|
|
|
int argi = 1;
|
2022-09-23 16:15:59 +00:00
|
|
|
snac snac;
|
2022-09-21 16:27:30 +00:00
|
|
|
|
|
|
|
argc--;
|
|
|
|
if (argc < argi)
|
|
|
|
return usage();
|
|
|
|
|
|
|
|
cmd = argv[argi++];
|
|
|
|
|
|
|
|
if (strcmp(cmd, "init") == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc < argi)
|
|
|
|
return usage();
|
|
|
|
|
|
|
|
basedir = argv[argi++];
|
|
|
|
|
|
|
|
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-22 09:28:13 +00:00
|
|
|
if (argc < argi)
|
|
|
|
return usage();
|
|
|
|
|
|
|
|
user = argv[argi++];
|
|
|
|
|
|
|
|
if (strcmp(cmd, "webfinger") == 0) {
|
|
|
|
xs *actor = NULL;
|
|
|
|
xs *uid = NULL;
|
|
|
|
int status;
|
|
|
|
|
2022-09-23 15:36:40 +00:00
|
|
|
status = webfinger_request(user, &actor, &uid);
|
2022-09-22 09:28:13 +00: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 15:33:33 +00:00
|
|
|
if (argc < argi)
|
|
|
|
return usage();
|
|
|
|
|
|
|
|
url = argv[argi++];
|
|
|
|
|
2022-09-23 16:15:59 +00:00
|
|
|
if (!user_open(&snac, user)) {
|
|
|
|
printf("error in user '%s'\n", user);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-09-23 15:33:33 +00:00
|
|
|
if (strcmp(cmd, "request") == 0) {
|
2022-09-23 16:15:59 +00: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 16:46:30 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(cmd, "actor") == 0) {
|
|
|
|
int status;
|
|
|
|
xs *data = NULL;
|
|
|
|
|
|
|
|
status = actor_request(&snac, url, &data);
|
|
|
|
|
|
|
|
printf("status: %d\n", status);
|
|
|
|
|
|
|
|
return 0;
|
2022-09-23 15:33:33 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 16:27:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
2022-09-19 19:32:36 +00:00
|
|
|
{
|
2022-09-19 20:19:14 +00:00
|
|
|
snac snac;
|
|
|
|
|
2022-09-20 10:00:13 +00:00
|
|
|
printf("%s\n", tid(0));
|
2022-09-19 20:19:14 +00:00
|
|
|
|
2022-09-20 07:39:28 +00:00
|
|
|
srv_open("/home/angel/lib/snac/comam.es/");
|
2022-09-19 19:32:36 +00:00
|
|
|
|
2022-09-19 21:03:18 +00:00
|
|
|
user_open(&snac, "mike");
|
2022-09-20 07:48:13 +00:00
|
|
|
|
2022-09-21 06:57:02 +00:00
|
|
|
xs *headers = xs_dict_new();
|
2022-09-20 19:00:16 +00:00
|
|
|
int status;
|
|
|
|
d_char *payload;
|
|
|
|
int p_size;
|
2022-09-21 07:31:05 +00:00
|
|
|
xs *response;
|
|
|
|
|
|
|
|
response = http_signed_request(&snac, "GET", "https://mastodon.social/users/VictorMoral",
|
2022-09-20 19:00:16 +00:00
|
|
|
headers, NULL, 0, &status, &payload, &p_size);
|
|
|
|
|
2022-09-21 07:31:05 +00:00
|
|
|
{
|
|
|
|
xs *j1 = xs_json_dumps_pp(response, 4);
|
|
|
|
printf("response:\n%s\n", j1);
|
|
|
|
printf("payload:\n%s\n", payload);
|
|
|
|
}
|
|
|
|
|
2022-09-20 10:43:49 +00:00
|
|
|
{
|
|
|
|
xs *list = queue(&snac);
|
|
|
|
char *p, *fn;
|
|
|
|
|
|
|
|
p = list;
|
|
|
|
while (xs_list_iter(&p, &fn)) {
|
2022-09-20 10:50:37 +00:00
|
|
|
xs *obj;
|
|
|
|
|
|
|
|
obj = dequeue(&snac, fn);
|
|
|
|
printf("%s\n", xs_dict_get(obj, "actor"));
|
2022-09-20 10:43:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2022-09-20 07:48:13 +00: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 20:19:14 +00:00
|
|
|
|
2022-09-20 08:49:24 +00: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-19 21:33:11 +00:00
|
|
|
{
|
|
|
|
xs *list = user_list();
|
|
|
|
char *p, *uid;
|
|
|
|
|
|
|
|
p = list;
|
|
|
|
while (xs_list_iter(&p, &uid)) {
|
2022-09-20 07:39:28 +00:00
|
|
|
if (user_open(&snac, uid)) {
|
|
|
|
printf("%s (%s)\n", uid, xs_dict_get(snac.config, "name"));
|
|
|
|
user_free(&snac);
|
|
|
|
}
|
2022-09-19 21:33:11 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-20 10:43:49 +00:00
|
|
|
#endif
|
2022-09-19 21:33:11 +00:00
|
|
|
|
2022-09-19 19:32:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2022-09-21 16:27:30 +00:00
|
|
|
#endif
|