#include "jsm.h"
mreturn mod_register_new(mapi m, void *arg)
{
xmlnode reg, x;
if((reg = js_config(m->si, "register")) == NULL) return M_PASS;
log_debug("mod_register","checking");
switch(jpacket_subtype(m->packet))
{
case JPACKET__GET:
xmlnode_insert_node(m->packet->iq,xmlnode_get_firstchild(reg));
break;
case JPACKET__SET:
log_debug(ZONE,"processing valid registration for %s",jid_full(m->packet->to));
jutil_delay(m->packet->iq,"registered");
xmlnode_hide(xmlnode_get_tag(m->packet->iq,"password"));
xdb_set(m->si->xc, jid_user(m->packet->to), NS_REGISTER, m->packet->iq);
if(xmlnode_get_attrib(reg,"notify") != NULL)
{
x = jutil_msgnew(NULL,
m->packet->to->server,
"Registration Notice",
spools(m->packet->p,"The user ",jid_full(m->packet->to)," was just created with the following registration data: ",xmlnode2str(m->packet->iq),m->packet->p));
xmlnode_put_attrib(x, "from", m->packet->to->server);
js_deliver(m->si,jpacket_new(x));
}
if((reg = js_config(m->si, "welcome")) != NULL)
{
x = xmlnode_new_tag("message");
xmlnode_put_attrib(x, "from", m->packet->to->server);
xmlnode_put_attrib(x, "to", jid_full(m->packet->to));
xmlnode_insert_node(x, xmlnode_get_firstchild(reg));
js_deliver(m->si,jpacket_new(x));
}
jutil_iqresult(m->packet->x);
break;
default:
return M_PASS;
}
return M_HANDLED;
}
mreturn mod_register_server(mapi m, void *arg)
{
xmlnode reg, cur, check;
if(m->packet->type != JPACKET_IQ) return M_IGNORE;
if(!NSCHECK(m->packet->iq,NS_REGISTER)) return M_PASS;
if(m->user == NULL) return M_PASS;
if(js_config(m->si,"register") == NULL) return M_PASS;
log_debug("mod_register","updating server: %s, user %s",m->user->id->server,jid_full(m->user->id));
reg = xdb_get(m->si->xc, m->user->id, NS_REGISTER);
switch(jpacket_subtype(m->packet))
{
case JPACKET__GET:
xmlnode_put_attrib(m->packet->x,"type","result");
jutil_tofrom(m->packet->x);
xmlnode_insert_node(m->packet->iq,xmlnode_get_firstchild(js_config(m->si,"register")));
xmlnode_insert_cdata(xmlnode_insert_tag(m->packet->iq,"key"),jutil_regkey(NULL,"foobar"),-1);
for(cur = xmlnode_get_firstchild(m->packet->iq); cur != NULL; cur = xmlnode_get_nextsibling(cur))
{
if(xmlnode_get_type(cur) != NTYPE_TAG) continue;
check = xmlnode_get_tag(reg,xmlnode_get_name(cur));
if(check == NULL) continue;
xmlnode_insert_node(cur,xmlnode_get_firstchild(check));
}
xmlnode_insert_tag(m->packet->iq,"registered");
break;
case JPACKET__SET:
if(xmlnode_get_tag(m->packet->iq,"remove") != NULL)
{
log_notice(m->user->id->server,"User Unregistered: %s",m->user->user);
xdb_set(m->si->xc, m->user->id, NS_REGISTER, NULL);
xdb_set(m->si->xc, m->user->id, NS_AUTH, NULL);
xdb_set(m->si->xc, m->user->id, NS_AUTH_CRYPT, NULL);
xdb_set(m->si->xc, m->user->id, NS_PRIVATE, NULL);
xdb_set(m->si->xc, m->user->id, NS_ROSTER, NULL);
xdb_set(m->si->xc, m->user->id, NS_VCARD, NULL);
xdb_set(m->si->xc, m->user->id, NS_OFFLINE, NULL);
xdb_set(m->si->xc, m->user->id, NS_FILTER, NULL);
}else{
log_debug(ZONE,"updating registration for %s",jid_full(m->user->id));
xmlnode_hide(xmlnode_get_tag(m->packet->iq,"username"));
xmlnode_hide(xmlnode_get_tag(m->packet->iq,"password"));
jutil_delay(m->packet->iq,"updated");
xdb_set(m->si->xc, m->user->id, NS_REGISTER, m->packet->iq);
}
jutil_iqresult(m->packet->x);
break;
default:
xmlnode_free(reg);
return M_PASS;
}
xmlnode_free(reg);
js_deliver(m->si, jpacket_reset(m->packet));
return M_HANDLED;
}
void mod_register(jsmi si)
{
log_debug("mod_register","init");
js_mapi_register(si, e_REGISTER, mod_register_new, NULL);
js_mapi_register(si, e_SERVER, mod_register_server, NULL);
}