mirror of
https://codeberg.org/grunfink/snac2.git
synced 2024-11-21 21:05:05 +00:00
[data.c] new file.
This commit is contained in:
parent
368cf465fc
commit
c88d4f1e15
5 changed files with 141 additions and 127 deletions
8
Makefile
8
Makefile
|
@ -2,16 +2,18 @@ CFLAGS=-g -Wall
|
|||
|
||||
all: snac
|
||||
|
||||
snac: snac.o main.o
|
||||
snac: snac.o main.o data.o
|
||||
$(CC) -L/usr/local/lib *.o -lcurl -lcrypto -o $@
|
||||
|
||||
.c.o:
|
||||
$(CC) $(CFLAGS) -I/usr/local/include -c $<
|
||||
|
||||
clean:
|
||||
rm -rf *.o *.core snac
|
||||
|
||||
snac.o: snac.c snac.h \
|
||||
xs.h xs_io.h xs_encdec.h xs_json.h xs_curl.h xs_openssl.h xs_socket.h xs_httpd.h
|
||||
|
||||
main.o: main.c snac.h xs.h
|
||||
|
||||
clean:
|
||||
rm -rf *.o *.core snac
|
||||
data.o: data.c snac.h xs.h xs_json.h
|
||||
|
|
133
data.c
Normal file
133
data.c
Normal file
|
@ -0,0 +1,133 @@
|
|||
/* snac - A simple, minimalistic ActivityPub instance */
|
||||
/* copyright (c) 2022 grunfink - MIT license */
|
||||
|
||||
#include "xs.h"
|
||||
#include "xs_io.h"
|
||||
#include "xs_json.h"
|
||||
|
||||
#include "snac.h"
|
||||
|
||||
int srv_open(char *basedir)
|
||||
/* opens a server */
|
||||
{
|
||||
int ret = 0;
|
||||
xs *cfg_file = NULL;
|
||||
FILE *f;
|
||||
|
||||
srv_basedir = xs_str_new(basedir);
|
||||
|
||||
cfg_file = xs_fmt("%s/server.json", basedir);
|
||||
|
||||
if ((f = fopen(cfg_file, "r")) == NULL)
|
||||
srv_log(xs_fmt("error opening '%s'", cfg_file));
|
||||
else {
|
||||
xs *cfg_data;
|
||||
|
||||
/* read full config file */
|
||||
cfg_data = xs_readall(f);
|
||||
|
||||
/* parse */
|
||||
srv_config = xs_json_loads(cfg_data);
|
||||
|
||||
if (srv_config == NULL)
|
||||
srv_log(xs_fmt("cannot parse '%s'", cfg_file));
|
||||
else {
|
||||
char *host;
|
||||
char *prefix;
|
||||
char *dbglvl;
|
||||
|
||||
host = xs_dict_get(srv_config, "host");
|
||||
prefix = xs_dict_get(srv_config, "prefix");
|
||||
dbglvl = xs_dict_get(srv_config, "dbglevel");
|
||||
|
||||
if (host == NULL || prefix == NULL)
|
||||
srv_log(xs_str_new("cannot get server data"));
|
||||
else {
|
||||
srv_baseurl = xs_fmt("https://%s%s", host, prefix);
|
||||
|
||||
dbglevel = (int) xs_number_get(dbglvl);
|
||||
|
||||
if ((dbglvl = getenv("DEBUG")) != NULL) {
|
||||
dbglevel = atoi(dbglvl);
|
||||
srv_log(xs_fmt("DEBUG level set to %d from environment", dbglevel));
|
||||
}
|
||||
|
||||
ret = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
void snac_free(snac *snac)
|
||||
/* frees a user snac */
|
||||
{
|
||||
free(snac->uid);
|
||||
free(snac->basedir);
|
||||
free(snac->config);
|
||||
free(snac->key);
|
||||
free(snac->actor);
|
||||
}
|
||||
|
||||
|
||||
int snac_open(snac *snac, char *uid)
|
||||
/* opens a user */
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
memset(snac, '\0', sizeof(struct _snac));
|
||||
|
||||
if (validate_uid(uid)) {
|
||||
xs *cfg_file;
|
||||
FILE *f;
|
||||
|
||||
snac->uid = xs_str_new(uid);
|
||||
|
||||
snac->basedir = xs_fmt("%s/user/%s", srv_basedir, uid);
|
||||
|
||||
cfg_file = xs_fmt("%s/user.json", snac->basedir);
|
||||
|
||||
if ((f = fopen(cfg_file, "r")) != NULL) {
|
||||
xs *cfg_data;
|
||||
|
||||
/* read full config file */
|
||||
cfg_data = xs_readall(f);
|
||||
fclose(f);
|
||||
|
||||
if ((snac->config = xs_json_loads(cfg_data)) != NULL) {
|
||||
xs *key_file = xs_fmt("%s/key.json", snac->basedir);
|
||||
|
||||
if ((f = fopen(key_file, "r")) != NULL) {
|
||||
xs *key_data;
|
||||
|
||||
key_data = xs_readall(f);
|
||||
fclose(f);
|
||||
|
||||
if ((snac->key = xs_json_loads(key_data)) != NULL) {
|
||||
snac->actor = xs_fmt("%s/%s", srv_baseurl, uid);
|
||||
ret = 1;
|
||||
}
|
||||
else
|
||||
srv_log(xs_fmt("cannot parse '%s'", key_file));
|
||||
}
|
||||
else
|
||||
srv_log(xs_fmt("error opening '%s'", key_file));
|
||||
}
|
||||
else
|
||||
srv_log(xs_fmt("cannot parse '%s'", cfg_file));
|
||||
}
|
||||
else
|
||||
srv_log(xs_fmt("error opening '%s'", cfg_file));
|
||||
}
|
||||
else
|
||||
srv_log(xs_fmt("invalid user '%s'", uid));
|
||||
|
||||
if (!ret)
|
||||
snac_free(snac);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
124
snac.c
124
snac.c
|
@ -66,60 +66,6 @@ void srv_debug(int level, d_char *str)
|
|||
}
|
||||
|
||||
|
||||
int srv_open(char *basedir)
|
||||
/* opens a server */
|
||||
{
|
||||
int ret = 0;
|
||||
xs *cfg_file = NULL;
|
||||
FILE *f;
|
||||
|
||||
srv_basedir = xs_str_new(basedir);
|
||||
|
||||
cfg_file = xs_fmt("%s/server.json", basedir);
|
||||
|
||||
if ((f = fopen(cfg_file, "r")) == NULL)
|
||||
srv_log(xs_fmt("error opening '%s'", cfg_file));
|
||||
else {
|
||||
xs *cfg_data;
|
||||
|
||||
/* read full config file */
|
||||
cfg_data = xs_readall(f);
|
||||
|
||||
/* parse */
|
||||
srv_config = xs_json_loads(cfg_data);
|
||||
|
||||
if (srv_config == NULL)
|
||||
srv_log(xs_fmt("cannot parse '%s'", cfg_file));
|
||||
else {
|
||||
char *host;
|
||||
char *prefix;
|
||||
char *dbglvl;
|
||||
|
||||
host = xs_dict_get(srv_config, "host");
|
||||
prefix = xs_dict_get(srv_config, "prefix");
|
||||
dbglvl = xs_dict_get(srv_config, "dbglevel");
|
||||
|
||||
if (host == NULL || prefix == NULL)
|
||||
srv_log(xs_str_new("cannot get server data"));
|
||||
else {
|
||||
srv_baseurl = xs_fmt("https://%s%s", host, prefix);
|
||||
|
||||
dbglevel = (int) xs_number_get(dbglvl);
|
||||
|
||||
if ((dbglvl = getenv("DEBUG")) != NULL) {
|
||||
dbglevel = atoi(dbglvl);
|
||||
srv_log(xs_fmt("DEBUG level set to %d from environment", dbglevel));
|
||||
}
|
||||
|
||||
ret = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int validate_uid(char *uid)
|
||||
/* returns if uid is a valid identifier */
|
||||
{
|
||||
|
@ -134,76 +80,6 @@ int validate_uid(char *uid)
|
|||
}
|
||||
|
||||
|
||||
void snac_free(snac *snac)
|
||||
/* frees a user snac */
|
||||
{
|
||||
free(snac->uid);
|
||||
free(snac->basedir);
|
||||
free(snac->config);
|
||||
free(snac->key);
|
||||
free(snac->actor);
|
||||
}
|
||||
|
||||
|
||||
int snac_open(snac *snac, char *uid)
|
||||
/* opens a user */
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
memset(snac, '\0', sizeof(struct _snac));
|
||||
|
||||
if (validate_uid(uid)) {
|
||||
xs *cfg_file;
|
||||
FILE *f;
|
||||
|
||||
snac->uid = xs_str_new(uid);
|
||||
|
||||
snac->basedir = xs_fmt("%s/user/%s", srv_basedir, uid);
|
||||
|
||||
cfg_file = xs_fmt("%s/user.json", snac->basedir);
|
||||
|
||||
if ((f = fopen(cfg_file, "r")) != NULL) {
|
||||
xs *cfg_data;
|
||||
|
||||
/* read full config file */
|
||||
cfg_data = xs_readall(f);
|
||||
fclose(f);
|
||||
|
||||
if ((snac->config = xs_json_loads(cfg_data)) != NULL) {
|
||||
xs *key_file = xs_fmt("%s/key.json", snac->basedir);
|
||||
|
||||
if ((f = fopen(key_file, "r")) != NULL) {
|
||||
xs *key_data;
|
||||
|
||||
key_data = xs_readall(f);
|
||||
fclose(f);
|
||||
|
||||
if ((snac->key = xs_json_loads(key_data)) != NULL) {
|
||||
snac->actor = xs_fmt("%s/%s", srv_baseurl, uid);
|
||||
ret = 1;
|
||||
}
|
||||
else
|
||||
srv_log(xs_fmt("cannot parse '%s'", key_file));
|
||||
}
|
||||
else
|
||||
srv_log(xs_fmt("error opening '%s'", key_file));
|
||||
}
|
||||
else
|
||||
srv_log(xs_fmt("cannot parse '%s'", cfg_file));
|
||||
}
|
||||
else
|
||||
srv_log(xs_fmt("error opening '%s'", cfg_file));
|
||||
}
|
||||
else
|
||||
srv_log(xs_fmt("invalid user '%s'", uid));
|
||||
|
||||
if (!ret)
|
||||
snac_free(snac);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
void snac_debug(snac *snac, int level, d_char *str)
|
||||
/* prints a user debugging information */
|
||||
{
|
||||
|
|
2
snac.h
2
snac.h
|
@ -31,3 +31,5 @@ void snac_free(snac *snac);
|
|||
|
||||
void snac_debug(snac *snac, int level, d_char *str);
|
||||
#define snac_log(snac, str) snac_debug(snac, 0, str)
|
||||
|
||||
int validate_uid(char *uid);
|
||||
|
|
1
xs.h
1
xs.h
|
@ -37,6 +37,7 @@ typedef char d_char;
|
|||
#define _XS_BLK_SIZE 16
|
||||
#define _xs_blk_size(sz) ((((sz) + _XS_BLK_SIZE) / _XS_BLK_SIZE) * _XS_BLK_SIZE)
|
||||
|
||||
void _xs_destroy(char **var);
|
||||
#define xs_debug() kill(getpid(), 5)
|
||||
xstype xs_type(const char *data);
|
||||
int xs_size(const char *data);
|
||||
|
|
Loading…
Reference in a new issue