xmlnode jutil_iqresult(xmlnode x) { xmlnode cur; jutil_tofrom(x); xmlnode_put_attrib(x,"type","result"); /* hide all children of the iq, they go back empty */ for(cur = xmlnode_get_firstchild(x); cur != NULL; cur = xmlnode_get_nextsibling(cur)) xmlnode_hide(cur); return x; }
/** * A replacement for xdb_set * returns 0 if the set succeeded, 1 if not */ int xdb_set(void *noop, jid fn, char *ns, xmlnode x) { extern jcr_instance jcr; char buf[512]; xmlnode n, s, xdb; int rc; n = xdb_get(noop, fn, NULL); if (n == NULL) { memset(buf, 0, 512); snprintf(buf, 511, "%s/%s.xml", jcr->spool_dir, fn->user); xdb = xmlnode_new_tag("xdb"); if (ns != NULL) xmlnode_put_attrib(x, "xdbns", ns); // log_debug(JDBG, "node not found, creating %s", xmlnode2str(x)); xmlnode_insert_node(xdb, x); // log_debug(JDBG, "\nwriting '%s'\n\n", xmlnode2str(xdb)); rc = xmlnode2file(buf, xdb); xmlnode_free(xdb); return rc; } else { s = NULL; // log_debug(JDBG, "node found '%s'", xmlnode2str(n)); if (ns == NULL) { s = xmlnode_get_tag(n, xmlnode_get_name(x)); } else { memset(buf, 0, 512); snprintf(buf, 511, "%s?xdbns=%s", xmlnode_get_name(x), ns); // log_debug(JDBG, "search for '%s'", buf); s = xmlnode_get_tag(n, buf); } // log_debug(JDBG, "removing '%s'", xmlnode2str(s)); xmlnode_hide(s); if (ns != NULL) xmlnode_put_attrib(x, "xdbns", ns); xmlnode_insert_tag_node(n, x); } snprintf(buf, 511, "%s/%s.xml", jcr->spool_dir, fn->user); rc = xmlnode2file(buf, n); xmlnode_free(n); log_debug(JDBG, "xdb_set: rc == %d", rc); return 0; }
mreturn mod_browse_set(mapi m, void *arg) { xmlnode browse, cur; jid id, to; if (m->packet->type != JPACKET_IQ) return M_IGNORE; if (!NSCHECK(m->packet->iq, NS_BROWSE) || jpacket_subtype(m->packet) != JPACKET__SET) return M_PASS; if (m->packet->to != NULL) return M_PASS; /* if its to someone other than ourselves */ log_debug("handling set request %s", xmlnode2str(m->packet->iq)); /* no to implies to ourselves */ if (m->packet->to != NULL) to = m->packet->to; else to = m->user->id; /* if we set to a resource, we need to make sure that resource's browse is in the users browse */ if (to->resource != NULL) { browse = mod_browse_get(m, to); /* get our browse info */ xmlnode_hide_attrib(browse, "xmlns"); /* don't need a ns as a child */ for (cur = xmlnode_get_firstchild(browse); cur != NULL; cur = xmlnode_get_nextsibling(cur)) xmlnode_hide(cur); /* erase all children */ xdb_act(m->si->xc, m->user->id, NS_BROWSE, "insert", spools(m->packet->p, "?jid=", jid_full(to), m->packet->p), browse); /* insert and match replace */ xmlnode_free(browse); } /* get the id of the new browse item */ if ((cur = xmlnode_get_firstchild(m->packet->iq)) == NULL || (id = jid_new(m->packet->p, xmlnode_get_attrib(cur, "jid"))) == NULL) { js_bounce(m->si, m->packet->x, TERROR_NOTACCEPTABLE); return M_HANDLED; } /* insert the new item into the resource it was sent to */ xmlnode_hide_attrib(cur, "xmlns"); /* just in case, to make sure it inserts */ if (xdb_act (m->si->xc, to, NS_BROWSE, "insert", spools(m->packet->p, "?jid=", jid_full(id), m->packet->p), cur)) { js_bounce(m->si, m->packet->x, TERROR_UNAVAIL); return M_HANDLED; } /* if the new data we're inserting is to one of our resources, update that resource's browse */ if (jid_cmpx(m->user->id, id, JID_USER | JID_SERVER) == 0 && id->resource != NULL) { /* get the old */ browse = mod_browse_get(m, id); /* transform the new one into the old one */ xmlnode_put_attrib(cur, "xmlns", NS_BROWSE); xmlnode_insert_node(cur, xmlnode_get_firstchild(browse)); xdb_set(m->si->xc, id, NS_BROWSE, cur); /* replace the resource's browse w/ this one */ xmlnode_free(browse); } /* send response to the user */ jutil_iqresult(m->packet->x); jpacket_reset(m->packet); js_session_to(m->s, m->packet); return M_HANDLED; }