static int roster_hook(int x, struct xmpp *xmpp) { struct xml_data *d; char *jid, *name, *sub; for (d = xml_node_data(xml_node_find(x, "query", &xmpp->xml.mem), &xmpp->xml.mem); d; d = xml_data_next(d, &xmpp->xml.mem)) { if (d->type != XML_NODE) continue; jid = xml_node_find_attr(d->value, "jid", &xmpp->xml.mem); name = xml_node_find_attr(d->value, "name", &xmpp->xml.mem); sub = xml_node_find_attr(d->value, "subscription", &xmpp->xml.mem); print_msg(0, "", "* %s - %s - [%s]\n", name ? name : "", jid, sub); } print_msg(0, "", "End of /R list.\n"); for (d = xml_node_data(xml_node_find(x, "query", &xmpp->xml.mem), &xmpp->xml.mem); d; d = xml_data_next(d, &xmpp->xml.mem)) { if (d->type != XML_NODE) continue; jid = xml_node_find_attr(d->value, "jid", &xmpp->xml.mem); request_presence(xmpp, jid); } return 0; }
static void roster_handler(int x, struct xmpp *xmpp) { struct xml_data *d; char *jid, *name, *sub; for (d = xml_node_data(xml_node_find(x, "query", &xmpp->xml.mem), &xmpp->xml.mem); d; d = xml_data_next(d, &xmpp->xml.mem)) { if (d->type != XML_NODE) continue; jid = xml_node_find_attr(d->value, "jid", &xmpp->xml.mem); name = xml_node_find_attr(d->value, "name", &xmpp->xml.mem); sub = xml_node_find_attr(d->value, "subscription", &xmpp->xml.mem); print_msg("* %s - %s - [%s]\n", name ? name : "", jid, sub); } print_msg("End of roster\n"); for (d = xml_node_data(xml_node_find(x, "query", &xmpp->xml.mem), &xmpp->xml.mem); d; d = xml_data_next(d, &xmpp->xml.mem)) { if (d->type != XML_NODE) continue; jid = xml_node_find_attr(d->value, "jid", &xmpp->xml.mem); if (jid) xmpp_printf(xmpp, "<presence type='probe' to='%s'/>", jid); } }
static int node_handler(int x, void *user) { struct xmpp *xmpp = (struct xmpp *)user; int r; char *name; r = xmpp_default_node_hook(x, xmpp, user); if (r < 0) return -1; if (r) return 0; name = xml_node_name(x, &xmpp->xml.mem); if (!name) return -1; if (!strcmp(name, "message")) return msg_hook(x, xmpp); if (!strcmp(name, "presence")) return presence_hook(x, xmpp); if (!strcmp(name, "iq")) { name = xml_node_find_attr(x, "id", &xmpp->xml.mem); if (name && !strcmp(name, "roster")) return roster_hook(x, xmpp); } return 0; }
static int node_handler(int x, void *user) { struct xmpp *xmpp = (struct xmpp *)user; char *name, *msg, *from, *show, *status, *type; int r, n; r = xmpp_default_node_hook(x, xmpp, user); if (r < 0) { if (!xmpp->is_authorized) fprintf(stderr, "Authorization error.\n"); return -1; } if (r) return 0; name = xml_node_name(x, &xmpp->xml.mem); if (!name) return -1; from = xml_node_find_attr(x, "from", &xmpp->xml.mem); if (!strcmp(name, "message")) { from = from ? jid_partial(from, &n) : 0; if (!from) return -1; snprintf(jid_from, sizeof(jid_from), "%.*s", n, from); msg = xml_node_find_text(x, "body", &xmpp->xml.mem); if (!msg) return -1; print_msg("<%.*s> %s\n", n, from, msg); } else if (!strcmp(name, "presence")) { if (!from) return -1; show = xml_node_find_text(x, "show", &xmpp->xml.mem); status = xml_node_find_text(x, "status", &xmpp->xml.mem); type = xml_node_find_attr(x, "type", &xmpp->xml.mem); if (type) print_msg("-!- %s sends %s\n", from, type); print_msg("-!- %s is %s (%s)\n", from, show ? show : (type ? type : "online"), status ? status : ""); } else if (!strcmp(name, "iq")) { name = xml_node_find_attr(x, "id", &xmpp->xml.mem); if (name && !strcmp(name, "roster")) roster_handler(x, xmpp); } return 0; }
static int presence_hook(int x, struct xmpp *xmpp) { char *show, *s, *status, *from, *part, *type; struct contact *u; int npart; show = xml_node_find_text(x, "show", &xmpp->xml.mem); status = xml_node_find_text(x, "status", &xmpp->xml.mem); type = xml_node_find_attr(x, "type", &xmpp->xml.mem); from = xml_node_find_attr(x, "from", &xmpp->xml.mem); if (!from) return 0; show = (!show && type && !strcmp("unavailable", type)) ? type : show; show = show ? show : STR_ONLINE; status = status ? status : ""; for (s = status; *s; ++s) if (*s == '\n') *s = '\\'; part = jid_partial(from, &npart); find_contact(u, npart, part); if (type && type[0] && (!u || strcmp(u->type, "groupchat"))) print_msg(0, "", "-!- %s sends %s\n", from, type); if (!u || strcmp(u->type, "groupchat")) print_msg(0, "", "-!- %s is %s (%s)\n", from, show, status); if (u) { if (!strcmp(u->type, "groupchat")) { if (!strcasecmp(show, STR_ONLINE) || !strcasecmp(show, STR_OFFLINE)) print_msg(npart, part, "-!- %s is %s\n", from, show); } else { print_msg(npart, part, "-!- %s is %s (%s)\n", from, show, status); snprintf(u->show, sizeof(u->show), "%s", show); snprintf(u->status, sizeof(u->status), "%s", status); } } return 0; }
static int msg_hook(int x, struct xmpp *xmpp) { char *s, *from, *part, *n, *type; int npart, len; struct contact *u; s = xml_node_find_text(x, "body", &xmpp->xml.mem); from = xml_node_find_attr(x, "from", &xmpp->xml.mem); part = from ? jid_partial(from, &npart) : 0; if (!part || !s) return 0; u = add_contact(npart, part); type = xml_node_find_attr(x, "type", &xmpp->xml.mem); if (type && !strcmp(type, "groupchat")) { u->type = "groupchat"; n = jid_resource(from, &len); } else n = jid_name(from, &len); print_msg(npart, from, "<%.*s> %s\n", len, n, s); notify(u->type, npart, from, len, n, s); return 0; }