2022-09-19 19:13:40 +00:00
|
|
|
/* snac - A simple, minimalistic ActivityPub instance */
|
2022-09-19 18:47:22 +00:00
|
|
|
/* copyright (c) 2022 grunfink - MIT license */
|
|
|
|
|
|
|
|
#define XS_IMPLEMENTATION
|
|
|
|
|
|
|
|
#include "xs.h"
|
|
|
|
#include "xs_io.h"
|
|
|
|
#include "xs_encdec.h"
|
|
|
|
#include "xs_json.h"
|
|
|
|
#include "xs_curl.h"
|
|
|
|
#include "xs_openssl.h"
|
|
|
|
#include "xs_socket.h"
|
|
|
|
#include "xs_httpd.h"
|
|
|
|
|
|
|
|
#include "snac.h"
|
|
|
|
|
2022-09-19 20:19:14 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
|
2022-09-19 19:13:40 +00:00
|
|
|
d_char *srv_basedir = NULL;
|
|
|
|
d_char *srv_config = NULL;
|
|
|
|
d_char *srv_baseurl = NULL;
|
|
|
|
|
|
|
|
int dbglevel = 0;
|
|
|
|
|
|
|
|
|
2022-09-19 19:24:55 +00:00
|
|
|
d_char *xs_time(char *fmt, int local)
|
|
|
|
/* returns a d_char with a formated time */
|
|
|
|
{
|
|
|
|
time_t t = time(NULL);
|
|
|
|
struct tm tm;
|
|
|
|
char tmp[64];
|
|
|
|
|
|
|
|
if (local)
|
|
|
|
localtime_r(&t, &tm);
|
|
|
|
else
|
|
|
|
gmtime_r(&t, &tm);
|
|
|
|
|
|
|
|
strftime(tmp, sizeof(tmp), fmt, &tm);
|
|
|
|
|
|
|
|
return xs_str_new(tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-19 20:19:14 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-19 19:30:19 +00:00
|
|
|
void srv_debug(int level, d_char *str)
|
|
|
|
/* logs a debug message */
|
2022-09-19 19:13:40 +00:00
|
|
|
{
|
|
|
|
xs *msg = str;
|
|
|
|
|
2022-09-19 19:30:19 +00:00
|
|
|
if (dbglevel >= level) {
|
|
|
|
xs *tm = xs_local_time("%H:%M:%S");
|
|
|
|
fprintf(stderr, "%s %s\n", tm, msg);
|
|
|
|
}
|
2022-09-19 19:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-19 20:19:14 +00:00
|
|
|
int validate_uid(char *uid)
|
|
|
|
/* returns if uid is a valid identifier */
|
|
|
|
{
|
|
|
|
while (*uid) {
|
|
|
|
if (!(isalnum(*uid) || *uid == '_'))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
uid++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-19 20:23:33 +00:00
|
|
|
void snac_debug(snac *snac, int level, d_char *str)
|
|
|
|
/* prints a user debugging information */
|
|
|
|
{
|
|
|
|
xs *msg = str;
|
|
|
|
|
|
|
|
if (dbglevel >= level) {
|
|
|
|
xs *tm = xs_local_time("%H:%M:%S");
|
|
|
|
fprintf(stderr, "%s [%s] %s\n", tm, snac->uid, msg);
|
|
|
|
}
|
|
|
|
}
|