From 32c188ae8fc662cf32ed16bf1d38a5391094ddda Mon Sep 17 00:00:00 2001 From: default Date: Mon, 19 Sep 2022 22:19:14 +0200 Subject: [PATCH] New functions snac_open() and snac_free(). --- main.c | 6 ++++ snac.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ snac.h | 12 ++++++++ 3 files changed, 114 insertions(+) diff --git a/main.c b/main.c index 9a3f71c..d047a64 100644 --- a/main.c +++ b/main.c @@ -7,7 +7,13 @@ int main(int argc, char *argv[]) { + snac snac; + + printf("%s\n", tid()); + srv_open("/home/angel/lib/snac/comam.es"); + snac_open(&snac, "mike"); + return 0; } diff --git a/snac.c b/snac.c index 95d2814..fa84c03 100644 --- a/snac.c +++ b/snac.c @@ -14,6 +14,9 @@ #include "snac.h" +#include + + d_char *srv_basedir = NULL; d_char *srv_config = NULL; d_char *srv_baseurl = NULL; @@ -39,6 +42,18 @@ d_char *xs_time(char *fmt, int local) } +d_char *tid(void) +/* returns a time-based Id */ +{ + struct timeval tv; + struct timezone tz; + + gettimeofday(&tv, &tz); + + return xs_fmt("%10d.%06d", tv.tv_sec, tv.tv_usec); +} + + void srv_debug(int level, d_char *str) /* logs a debug message */ { @@ -103,3 +118,84 @@ int srv_open(char *basedir) return ret; } + + +int validate_uid(char *uid) +/* returns if uid is a valid identifier */ +{ + while (*uid) { + if (!(isalnum(*uid) || *uid == '_')) + return 0; + + uid++; + } + + return 1; +} + + +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)); + + 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); +} diff --git a/snac.h b/snac.h index ef14425..c14fd79 100644 --- a/snac.h +++ b/snac.h @@ -11,8 +11,20 @@ d_char *xs_time(char *fmt, int local); #define xs_local_time(fmt) xs_time(fmt, 1) #define xs_utc_time(fmt) xs_time(fmt, 0) +d_char *tid(void); + void srv_debug(int level, d_char *str); #define srv_log(str) srv_debug(0, str) int srv_open(char *basedir); +typedef struct _snac { + d_char *uid; /* uid */ + d_char *basedir; /* user base directory */ + d_char *config; /* user configuration */ + d_char *key; /* keypair */ + d_char *actor; /* actor url */ +} snac; + +int snac_open(snac *snac, char *uid); +void snac_free(snac *snac);