#include <jabberd.h>
#define FILES_PRIME 509
typedef struct cacher_struct
{
char *fname;
xmlnode file;
int lastset;
} *cacher, _cacher;
typedef struct xdbf_struct
{
char *spool;
instance i;
int timeout;
HASHTABLE cache;
} *xdbf, _xdbf;
int _xdb_file_purge(void *arg, const void *key, void *data)
{
xdbf xf = (xdbf)arg;
cacher c = (cacher)data;
int now = time(NULL);
if((now - c->lastset) > xf->timeout)
{
log_debug(ZONE,"purging %s",c->fname);
ghash_remove(xf->cache,c->fname);
xmlnode_free(c->file);
}
return 1;
}
result xdb_file_purge(void *arg)
{
xdbf xf = (xdbf)arg;
log_debug(ZONE,"purge check");
ghash_walk(xf->cache,_xdb_file_purge,(void *)xf);
return r_DONE;
}
xmlnode xdb_file_load(char *host, char *fname, HASHTABLE cache)
{
xmlnode data = NULL;
cacher c;
int fd;
log_debug(ZONE,"loading %s",fname);
if((c = ghash_get(cache,fname)) != NULL)
return c->file;
fd = open(fname,O_RDONLY);
if(fd < 0)
{
log_debug(host,"xdb_file check file %s: %s.",fname,strerror(errno));
if (ENOENT != errno)
log_warn(host,"xdb_file check file %s: %s.",fname,strerror(errno));
}else{
close(fd);
data = xmlnode_file(fname);
}
if(data == NULL)
data = xmlnode_new_tag("xdb");
log_debug(ZONE,"caching %s",fname);
c = pmalloco(xmlnode_pool(data),sizeof(_cacher));
c->fname = pstrdup(xmlnode_pool(data),fname);
c->lastset = time(NULL);
c->file = data;
ghash_put(cache,c->fname,c);
return data;
}
char *xdb_file_full(int create, pool p, char *spl, char *host, char *file, char *ext)
{
struct stat s;
spool sp = spool_new(p);
char *ret;
spooler(sp,spl,"/",host,sp);
ret = spool_print(sp);
if(create && stat(ret,&s) < 0 && mkdir(ret, S_IRWXU) < 0)
{
log_error(host,"xdb request failed, error accessing spool loaction %s: %s",ret,strerror(errno));
return NULL;
}
spooler(sp,"/",file,".",ext,sp);
ret = spool_print(sp);
return ret;
}
result xdb_file_phandler(instance i, dpacket p, void *arg)
{
char *full, *ns, *act, *match;
xdbf xf = (xdbf)arg;
xmlnode file, top, data;
int ret = 0, flag_set = 0;
log_debug(ZONE,"handling xdb request %s",xmlnode2str(p->x));
if((ns = xmlnode_get_attrib(p->x,"ns")) == NULL)
return r_ERR;
if(j_strcmp(xmlnode_get_attrib(p->x,"type"), "set") == 0)
flag_set = 1;
if(p->id->user != NULL)
full = xdb_file_full(flag_set, p->p, xf->spool, p->id->server, p->id->user, "xml");
else
full = xdb_file_full(flag_set, p->p, xf->spool, p->id->server, "global", "xdb");
if(full == NULL)
return r_ERR;
top = file = xdb_file_load(p->host, full, xf->cache);
if(p->id->resource != NULL)
{
if((top = xmlnode_get_tag(top,spools(p->p,"res?id=",p->id->resource,p->p))) == NULL)
{
top = xmlnode_insert_tag(file,"res");
xmlnode_put_attrib(top,"id",p->id->resource);
}
}
data = xmlnode_get_tag(top,spools(p->p,"?xdbns=",ns,p->p));
if(flag_set)
{
act = xmlnode_get_attrib(p->x,"action");
match = xmlnode_get_attrib(p->x,"match");
if(act != NULL)
{
switch(*act)
{
case 'i':
if(data == NULL)
{
data = xmlnode_insert_tag(top,"foo");
xmlnode_put_attrib(data,"xdbns",ns);
xmlnode_put_attrib(data,"xmlns",ns);
}
xmlnode_hide(xmlnode_get_tag(data,match));
xmlnode_insert_tag_node(data, xmlnode_get_firstchild(p->x));
break;
case 'c':
if(match != NULL)
data = xmlnode_get_tag(data,match);
if(j_strcmp(xmlnode_get_data(data),xmlnode_get_data(xmlnode_get_firstchild(p->x))) != 0)
{
log_debug(ZONE,"xdb check action returning error to signify unsuccessful check");
return r_ERR;
}
flag_set = 0;
break;
default:
log_warn("xdb_file","unable to handle unknown xdb action '%s'",act);
return r_ERR;
}
}else{
if(data != NULL)
xmlnode_hide(data);
data = xmlnode_insert_tag_node(top, xmlnode_get_firstchild(p->x));
xmlnode_put_attrib(data,"xdbns",ns);
}
if(flag_set && !xmlnode_has_children(file) && !xmlnode_has_attribs(file) && !xmlnode_get_datasz(file))
{
log_debug(ZONE,"refusing to create empty cache file %s",full);
ret = 1;
}else{
if(flag_set && xmlnode2file(full,file) < 0)
log_error(p->id->server,"xdb request failed, unable to save to file %s",full);
else
ret = 1;
}
}else{
ret = 1;
if(data != NULL)
{
xmlnode_hide_attrib(xmlnode_insert_tag_node(p->x, data),"xdbns");
}
}
if(ret)
{
xmlnode_put_attrib(p->x,"type","result");
xmlnode_put_attrib(p->x,"to",xmlnode_get_attrib(p->x,"from"));
xmlnode_put_attrib(p->x,"from",jid_full(p->id));
deliver(dpacket_new(p->x), NULL);
if(xf->timeout == 0 || flag_set)
{
log_debug(ZONE,"decaching %s",full);
ghash_remove(xf->cache,full);
xmlnode_free(file);
}
return r_DONE;
}else{
return r_ERR;
}
}
void xdb_file_cleanup(void *arg)
{
xdbf xf = (xdbf)arg;
ghash_destroy(xf->cache);
}
void xdb_file(instance i, xmlnode x)
{
char *spl, *to;
xmlnode config;
xdbcache xc;
xdbf xf;
int timeout = -1;
log_debug(ZONE,"xdb_file loading");
xc = xdb_cache(i);
config = xdb_get(xc, jid_new(xmlnode_pool(x),"config@-internal"),"jabber:config:xdb_file");
spl = xmlnode_get_tag_data(config,"spool");
if(spl == NULL)
{
log_error(NULL,"xdb_file: No filesystem spool location configured");
return;
}
to = xmlnode_get_tag_data(config,"timeout");
if(to != NULL)
timeout = atoi(to);
xf = pmalloco(i->p,sizeof(_xdbf));
xf->spool = pstrdup(i->p,spl);
xf->timeout = timeout;
xf->i = i;
xf->cache = ghash_create(j_atoi(xmlnode_get_tag_data(config,"maxfiles"),FILES_PRIME),(KEYHASHFUNC)str_hash_code,(KEYCOMPAREFUNC)j_strcmp);
register_phandler(i, o_DELIVER, xdb_file_phandler, (void *)xf);
if(timeout > 0)
register_beat(timeout, xdb_file_purge, (void *)xf);
xmlnode_free(config);
pool_cleanup(i->p, xdb_file_cleanup, (void*)xf);
}