mirror of
https://codeberg.org/grunfink/snac2.git
synced 2024-11-25 14:35:04 +00:00
New functions snac_open() and snac_free().
This commit is contained in:
parent
8fbe3199df
commit
32c188ae8f
3 changed files with 114 additions and 0 deletions
6
main.c
6
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;
|
||||
}
|
||||
|
|
96
snac.c
96
snac.c
|
@ -14,6 +14,9 @@
|
|||
|
||||
#include "snac.h"
|
||||
|
||||
#include <sys/time.h>
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
12
snac.h
12
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);
|
||||
|
|
Loading…
Reference in a new issue