Exemple #1
0
static void _xode_stream_cleanup(void *arg)
{
    xode_stream xs = (xode_stream)arg;

    xode_free(xs->node); /* cleanup anything left over */
    XML_ParserFree(xs->parser);
}
/**
 * send a message through a JABBER connection
 * params are pairs (buffer, len)
 */
int xj_jcon_send_msg(xj_jcon jbc, char *to, int tol, char *msg, 
		int msgl, int type)
{
	char msg_buff[4096], *p;
	int n;
	xode x;
	
	if(jbc == NULL)
		return -1;
	
	x = xode_new_tag("body");
	if(!x)
		return -1;
	
	xode_insert_cdata(x, msg, msgl);
	x = xode_wrap(x, "message");
	strncpy(msg_buff, to, tol);
	msg_buff[tol] = 0;
	xode_put_attrib(x, "to", msg_buff);
	switch(type)
	{
		case XJ_JMSG_CHAT:
			xode_put_attrib(x, "type", "chat");
			break;
		case XJ_JMSG_GROUPCHAT:
			xode_put_attrib(x, "type", "groupchat");
			break;
		default:
			xode_put_attrib(x, "type", "normal");
	}

	p = xode_to_str(x);
	n = strlen(p);
#ifdef XJ_EXTRA_DEBUG
	DBG("XJAB:xj_jcon_send_msg: jabber msg:\n%s\n", p);
#endif	
	if(send(jbc->sock, p, n, 0) != n)
	{
		DBG("XJAB:xj_jcon_send_msg: error - message not sent\n");
		goto error;
	}
	xode_free(x);
	return 0;
error:
	xode_free(x);
	return -1;
}
Exemple #3
0
xode xode_from_file(char *file)
{
    XML_Parser p;
    xode *x, node; /* pointer to an xmlnode */
    char buf[BUFSIZ];
    int done, fd, len;
    char _file[1000];

    if(NULL == file)
        return NULL;

    /* perform tilde expansion */
    if(*file == '~')
    {
        char *env = getenv("HOME");
        if(env != NULL)
            snprintf((char*)_file, 1000, "%s%s", env, file + 1);
        else
            snprintf((char*)_file, 1000, "%s", file);
    }
    else
    {
        snprintf((char*)_file, 1000, "%s", file);
    }

    fd = open((char*)&_file,O_RDONLY);
    if(fd < 0)
        return NULL;

    x = malloc(sizeof(void *));

    *x = NULL; /* pointer to NULL */
    p = XML_ParserCreate(NULL);
    XML_SetUserData(p, x);
    XML_SetElementHandler(p, _xode_expat_startElement, _xode_expat_endElement);
    XML_SetCharacterDataHandler(p, _xode_expat_charData);
    do{
        len = read(fd, buf, BUFSIZ);
        done = len < BUFSIZ;
        if(!XML_Parse(p, buf, len, done))
        {
            /*            jdebug(ZONE,"xmlnode_file_parseerror: %s",(char *)XML_ErrorString(XML_GetErrorCode(p)));*/
            xode_free(*x);
            *x = NULL;
            done = 1;
        }
    }while(!done);

    node = *x;
    XML_ParserFree(p);
    free(x);
    close(fd);
    return node; /* return the xmlnode x points to */
}
/**
 * add a new contact in user's roster
 */
int xj_jcon_set_roster(xj_jcon jbc, char* jid, char *type)
{
	xode x;
	char *p;
	int n;
	char buff[16];
	
	if(!jbc || !jid)
		return -1;
	
	x = xode_new_tag("item");
	if(!x)
		return -1;
	xode_put_attrib(x, "jid", jid);
	if(type != NULL)
		xode_put_attrib(x, "subscription", type);
	
	x = xode_wrap(x, "query");
	xode_put_attrib(x, "xmlns", "jabber:iq:roster");

	x = xode_wrap(x, "iq");
	
	xode_put_attrib(x, "type", "set");
	jbc->seq_nr++;
	sprintf(buff, "%08X", jbc->seq_nr);
	xode_put_attrib(x, "id", buff);

	p = xode_to_str(x);
	n = strlen(p);
	
	if(send(jbc->sock, p, n, 0) != n)
	{
		DBG("XJAB:xj_jcon_set_roster: Error - item not sent\n");
		goto error;
	}
	xode_free(x);
	return 0;
error:
	xode_free(x);
	return -1;
}
Exemple #5
0
static int xode_send_domain(char *domain, xode x)
{
	struct xmpp_connection *conn;

	if ((conn = conn_find_domain(domain, CONN_OUTBOUND))) {
		xode_send(conn->fd, x);
		xode_free(x);
	} else {
		if((conn = conn_new(CONN_OUTBOUND, -1, domain))==0)
			return -1;
		xode_insert_node(conn->todo, x);
	}
	return 1;
}
Exemple #6
0
tt_user *users_save(const char *jid) {
	tt_user **user, *user0;
	xode cfg, x;
	char *file;

	if (jid == NULL)
		return NULL;

	if ((file = malloc(strlen(config_dir) + strlen(jid) + 2)) == NULL) {
		perror("malloc()");
		exit(1);
	}
	sprintf(file, "%s/%s", config_dir, jid);

	my_debug(0, "users: Zapisuje plik %s", file);

	user = jid_jid2user(jid);
	user0 = *user;

	cfg = xode_new("user");

	x = xode_insert_tag(cfg, "jid");
	xode_insert_cdata(x, user0->jid, -1);

	x = xode_insert_tag(cfg, "tid");
	xode_insert_cdata(x, user0->tid_short, -1);

	x = xode_insert_tag(cfg, "password");
	xode_insert_cdata(x, user0->password, -1);

	x = xode_insert_tag(cfg, "notify");
	xode_put_attrib(x, "mail", (user0->mailnotify ? "1" : "0"));
	xode_put_attrib(x, "typing", (user0->typingnotify ? "1" : "0"));
	xode_put_attrib(x, "alarm", (user0->alarmnotify ? "1" : "0"));

	xode_to_file(file, cfg);
	xode_free(cfg);
	t_free(file);

	return user0;
}
/*!
 *
 */
static int do_send_message_component(struct xmpp_private_data *priv,
		struct xmpp_pipe_cmd *cmd)
{
	xode x;

	LM_DBG("do_send_message_component from=[%s] to=[%s] body=[%s]\n",
			cmd->from, cmd->to, cmd->body);

	x = xode_new_tag("message");
	xode_put_attrib(x, "id", cmd->id); // XXX
	xode_put_attrib(x, "from", encode_uri_sip_xmpp(cmd->from));
	xode_put_attrib(x, "to", decode_uri_sip_xmpp(cmd->to));
	xode_put_attrib(x, "type", "chat");
	xode_insert_cdata(xode_insert_tag(x, "body"), cmd->body, -1);
			
	xode_send(priv->fd, x);
	xode_free(x);

	/* missing error handling here ?!?!*/
	return 0;
}
Exemple #8
0
static void conn_free(struct xmpp_connection *conn)
{
	struct xmpp_connection **last_p, *link;
	
	last_p = &conn_list;
	for (link = conn_list; link; link = link->next) {
		if (link == conn) {
			*last_p = link->next;
			break;
		}
		last_p = &link->next;
	}

	if (conn->todo)
		xode_free(conn->todo);
	xode_pool_free(conn->pool);
	if (conn->fd != -1)
		close(conn->fd);
	if (conn->stream_id)
		free(conn->stream_id);
	if (conn->domain)
		free(conn->domain);
	free(conn);
}
Exemple #9
0
/**
 * parse incoming message from Jabber server
 */
int xj_manage_jab(char *buf, int len, int *pos, xj_jalias als, xj_jcon jbc)
{
    int j, err=0;
    char *p, *to, *from, *msg, *type, *emsg, *ecode, lbuf[4096], fbuf[128];
    xj_jconf jcf = NULL;
    str ts, tf;
    xode x, y, z;
    str *sid;
    xj_pres_cell prc = NULL;

    if(!jbc)
        return -1;

    sid = jbc->jkey->id;
    x = xode_from_strx(buf, len, &err, &j);
#ifdef XJ_EXTRA_DEBUG
    DBG("XJAB:xj_parse_jab: XODE ret:%d pos:%d\n", err, j);
#endif
    if(err && pos != NULL)
        *pos= j;
    if(x == NULL)
        return -1;
    lbuf[0] = 0;
    ecode = NULL;

    /******************** XMPP 'MESSAGE' HANDLING **********************/

    if(!strncasecmp(xode_get_name(x), "message", 7))
    {
#ifdef XJ_EXTRA_DEBUG
        DBG("XJAB:xj_manage_jab: jabber [message] received\n");
#endif
        if((to = xode_get_attrib(x, "to")) == NULL)
        {
#ifdef XJ_EXTRA_DEBUG
            DBG("XJAB:xj_manage_jab: missing 'to' attribute\n");
#endif
            err = -1;
            goto ready;
        }
        if((from = xode_get_attrib(x, "from")) == NULL)
        {
#ifdef XJ_EXTRA_DEBUG
            DBG("XJAB:xj_manage_jab: missing 'from' attribute\n");
#endif
            err = -1;
            goto ready;
        }
        if((y = xode_get_tag(x, "body")) == NULL
                || (msg = xode_get_data(y)) == NULL)
        {
#ifdef XJ_EXTRA_DEBUG
            DBG("XJAB:xj_manage_jab: missing 'body' of message\n");
#endif
            err = -1;
            goto ready;
        }
        type = xode_get_attrib(x, "type");
        if(type != NULL && !strncasecmp(type, "error", 5))
        {
            if((y = xode_get_tag(x, "error")) == NULL
                    || (emsg = xode_get_data(y)) == NULL)
                strcpy(lbuf, "{Error sending following message} - ");
            else
            {
                ecode = xode_get_attrib(y, "code");
                strcpy(lbuf, "{Error (");
                if(ecode != NULL)
                {
                    strcat(lbuf, ecode);
                    strcat(lbuf, " - ");
                }
                strcat(lbuf, emsg);
                strcat(lbuf, ") when trying to send following message}");
            }

        }

        // is from a conference?!?!
        if((jcf=xj_jcon_check_jconf(jbc, from))!=NULL)
        {
            if(lbuf[0] == 0)
            {
                p = from + strlen(from);
                while(p>from && *p != '/')
                    p--;
                if(*p == '/')
                {
                    if(jcf->nick.len>0
                            && strlen(p+1) == jcf->nick.len
                            && !strncasecmp(p+1, jcf->nick.s, jcf->nick.len))
                    {
#ifdef XJ_EXTRA_DEBUG
                        DBG("XJAB:xj_manage_jab: message sent by myself\n");
#endif
                        goto ready;
                    }
                    lbuf[0] = '[';
                    lbuf[1] = 0;
                    strcat(lbuf, p+1);
                    strcat(lbuf, "] ");
                }
            }
            else
            {
                jcf->status = XJ_JCONF_NULL;
                xj_jcon_jconf_presence(jbc,jcf,NULL,"online");
            }
            strcat(lbuf, msg);
            ts.s = lbuf;
            ts.len = strlen(lbuf);

            if(xj_send_sip_msg(als->proxy, sid, &jcf->uri, &ts,
                               &jbc->jkey->flag)<0)
                DBG("XJAB:xj_manage_jab: ERROR SIP MESSAGE was not sent!\n");
#ifdef XJ_EXTRA_DEBUG
            else
                DBG("XJAB:xj_manage_jab: SIP MESSAGE was sent!\n");
#endif
            goto ready;
        }

        strcat(lbuf, msg);
        ts.s = from;
        ts.len = strlen(from);
        tf.s = fbuf;
        tf.len = 0;
        if(xj_address_translation(&ts, &tf, als, XJ_ADDRTR_J2S) == 0)
        {
            ts.s = lbuf;
            ts.len = strlen(lbuf);

            if(xj_send_sip_msg(als->proxy, sid, &tf, &ts, &jbc->jkey->flag)<0)
                DBG("XJAB:xj_manage_jab: ERROR SIP MESSAGE was not sent ...\n");
#ifdef XJ_EXTRA_DEBUG
            else
                DBG("XJAB:xj_manage_jab: SIP MESSAGE was sent.\n");
#endif
        }
        goto ready;
    }
    /*------------------- END 'MESSAGE' HANDLING ----------------------*/

    /******************** XMPP 'PRESENCE' HANDLING *********************/
    if(!strncasecmp(xode_get_name(x), "presence", 8))
    {
#ifdef XJ_EXTRA_DEBUG
        DBG("XJAB:xj_manage_jab: jabber [presence] received\n");
#endif
        type = xode_get_attrib(x, "type");
        from = xode_get_attrib(x, "from");
        if(from == NULL)
            goto ready;
        ts.s = from;
        p = from;
        while(p<from + strlen(from) && *p != '/')
            p++;
        if(*p == '/')
            ts.len = p - from;
        else
            ts.len = strlen(from);

        if(type == NULL || !strncasecmp(type, "online", 6)
                || !strncasecmp(type, "available", 9))
        {
            if(strchr(from, '@') == NULL)
            {
                if(!strncasecmp(from, XJ_AIM_NAME, XJ_AIM_LEN))
                {
                    jbc->ready |= XJ_NET_AIM;
#ifdef XJ_EXTRA_DEBUG
                    DBG("XJAB:xj_manage_jab: AIM network ready\n");
#endif
                }
                else if(!strncasecmp(from, XJ_ICQ_NAME, XJ_ICQ_LEN))
                {
                    jbc->ready |= XJ_NET_ICQ;
#ifdef XJ_EXTRA_DEBUG
                    DBG("XJAB:xj_manage_jab: ICQ network ready\n");
#endif
                }
                else if(!strncasecmp(from, XJ_MSN_NAME, XJ_MSN_LEN))
                {
                    jbc->ready |= XJ_NET_MSN;
#ifdef XJ_EXTRA_DEBUG
                    DBG("XJAB:xj_manage_jab: MSN network ready\n");
#endif
                }
                else if(!strncasecmp(from, XJ_YAH_NAME, XJ_YAH_LEN))
                {
                    jbc->ready |= XJ_NET_YAH;
#ifdef XJ_EXTRA_DEBUG
                    DBG("XJAB:xj_manage_jab: YAHOO network ready\n");
#endif
                }
            }
            else if((jcf=xj_jcon_check_jconf(jbc, from))!=NULL)
            {
                jcf->status = XJ_JCONF_READY;
#ifdef XJ_EXTRA_DEBUG
                DBG("XJAB:xj_manage_jab: %s conference ready\n", from);
#endif
            }
            else
            {
#ifdef XJ_EXTRA_DEBUG
                DBG("XJAB:xj_manage_jab: user <%.*s> is online\n",ts.len,ts.s);
#endif
                prc = xj_pres_list_check(jbc->plist, &ts);
                if(prc)
                {
                    if(prc->state != XJ_PS_ONLINE)
                    {
                        prc->state = XJ_PS_ONLINE;
                        goto call_pa_cbf;
                    }
                }
                else
                {
#ifdef XJ_EXTRA_DEBUG
                    DBG("XJAB:xj_manage_jab: user state received - creating"
                        " presence cell for [%.*s]\n", ts.len, ts.s);
#endif
                    prc = xj_pres_cell_new();
                    if(prc == NULL)
                    {
                        DBG("XJAB:xj_manage_jab: cannot create presence"
                            " cell for [%s]\n", from);
                        goto ready;
                    }
                    if(xj_pres_cell_init(prc, &ts, NULL, NULL)<0)
                    {
                        DBG("XJAB:xj_manage_jab: cannot init presence"
                            " cell for [%s]\n", from);
                        xj_pres_cell_free(prc);
                        goto ready;
                    }
                    prc = xj_pres_list_add(jbc->plist, prc);
                    if(prc)
                    {
                        prc->state = XJ_PS_ONLINE;
                        goto call_pa_cbf;
                    }
                }
            }
            goto ready;
        }

        if(strchr(from, '@') == NULL)
            goto ready;


        if(!strncasecmp(type, "error", 5))
        {
            if((jcf=xj_jcon_check_jconf(jbc, from))!=NULL)
            {
                tf.s = from;
                tf.len = strlen(from);
                if((y = xode_get_tag(x, "error")) == NULL)
                    goto ready;
                if ((p = xode_get_attrib(y, "code")) != NULL
                        && atoi(p) == 409)
                {
                    xj_send_sip_msgz(als->proxy, sid, &tf,
                                     XJ_DMSG_ERR_JCONFNICK, &jbc->jkey->flag);
                    goto ready;
                }
                xj_send_sip_msgz(als->proxy,sid,&tf,XJ_DMSG_ERR_JCONFREFUSED,
                                 &jbc->jkey->flag);
            }
            goto ready;
        }
        if(type!=NULL && !strncasecmp(type, "subscribe", 9))
        {
            xj_jcon_send_presence(jbc, from, "subscribed", NULL, NULL);
            goto ready;
        }

        prc = xj_pres_list_check(jbc->plist, &ts);
        if(!prc)
            goto ready;

        if(!strncasecmp(type, "unavailable", 11))
        {
#ifdef XJ_EXTRA_DEBUG
            DBG("XJAB:xj_manage_jab: user <%s> is offline\n", from);
#endif
            if(prc->state != XJ_PS_OFFLINE)
            {
                prc->state = XJ_PS_OFFLINE;
                goto call_pa_cbf;
            }
            goto ready;
        }

        if(!strncasecmp(type, "unsubscribed", 12))
        {
#ifdef XJ_EXTRA_DEBUG
            DBG("XJAB:xj_manage_jab: user <%s> does not allow to see his"
                " presence status\n", from);
#endif
            if(prc->state != XJ_PS_REFUSED)
            {
                prc->state = XJ_PS_REFUSED;
                goto call_pa_cbf;
            }
        }

        // ignoring unknown types
        goto ready;
    }
    /*------------------- END XMPP 'PRESENCE' HANDLING ----------------*/

    /******************** XMPP 'IQ' HANDLING ***************************/
    if(!strncasecmp(xode_get_name(x), "iq", 2))
    {
#ifdef XJ_EXTRA_DEBUG
        DBG("XJAB:xj_manage_jab: jabber [iq] received\n");
#endif
        if(!strncasecmp(xode_get_attrib(x, "type"), "result", 6))
        {
            if((y = xode_get_tag(x, "query?xmlns=jabber:iq:roster")) == NULL)
                goto ready;
            z = xode_get_firstchild(y);
            while(z)
            {
                if(!strncasecmp(xode_get_name(z), "item", 5)
                        && (from = xode_get_attrib(z, "jid")) != NULL)
                {
                    if(strchr(from, '@') == NULL)
                    {   // transports
                        if(!strncasecmp(from, XJ_AIM_NAME, XJ_AIM_LEN))
                        {
                            jbc->allowed |= XJ_NET_AIM;
#ifdef XJ_EXTRA_DEBUG
                            DBG("XJAB:xj_manage_jab:AIM network available\n");
#endif
                        }
                        else if(!strncasecmp(from, XJ_ICQ_NAME, XJ_ICQ_LEN))
                        {
                            jbc->allowed |= XJ_NET_ICQ;
#ifdef XJ_EXTRA_DEBUG
                            DBG("XJAB:xj_manage_jab:ICQ network available\n");
#endif
                        }
                        else if(!strncasecmp(from, XJ_MSN_NAME, XJ_MSN_LEN))
                        {
                            jbc->allowed |= XJ_NET_MSN;
#ifdef XJ_EXTRA_DEBUG
                            DBG("XJAB:xj_manage_jab:MSN network available\n");
#endif
                        }
                        else if(!strncasecmp(from, XJ_YAH_NAME, XJ_YAH_LEN))
                        {
                            jbc->allowed |= XJ_NET_YAH;
#ifdef XJ_EXTRA_DEBUG
                            DBG("XJAB:xj_manage_jab:YAHOO network available\n");
#endif
                        }
                        goto next_sibling;
                    }
                }
next_sibling:
                z = xode_get_nextsibling(z);
            }
        }

        goto ready;
    }
    /*------------------- END XMPP 'IQ' HANDLING ----------------------*/

call_pa_cbf:
    if(prc && prc->cbf)
    {
        // call the PA callback function
        tf.s = fbuf;
        tf.len = 0;
        if(xj_address_translation(&ts,&tf,als,XJ_ADDRTR_J2S)==0)
        {
#ifdef XJ_EXTRA_DEBUG
            DBG("XJAB:xj_manage_jab: calling CBF(%.*s,%d)\n",
                tf.len, tf.s, prc->state);
#endif
            (*(prc->cbf))(&tf, &tf, prc->state, prc->cbp);
        }
    }
ready:
    xode_free(x);
    return err;
}
static void stream_node_callback(int type, xode node, void *arg) 
{
	struct xmpp_private_data *priv = (struct xmpp_private_data *) arg;
	char *id, *hash, *tag;
	char buf[4096];
	xode x;

	LM_DBG("stream callback: %d: %s\n", type, node ? xode_get_name(node) : "n/a");
	switch (type) {
	case XODE_STREAM_ROOT:
		id = xode_get_attrib(node, "id");
		snprintf(buf, sizeof(buf), "%s%s", id, xmpp_password);
		hash = shahash(buf);
		
		x = xode_new_tag("handshake");
		xode_insert_cdata(x, hash, -1);
		xode_send(priv->fd, x);
		xode_free(x);
		break;
	case XODE_STREAM_NODE:
		tag = xode_get_name(node);
		if (!strcmp(tag, "handshake")) {
			LM_DBG("handshake succeeded\n");
		} else if (!strcmp(tag, "message")) {
			LM_DBG("XMPP IM received\n");
			char *from = xode_get_attrib(node, "from");
			char *to = xode_get_attrib(node, "to");
			char *type = xode_get_attrib(node, "type");
			xode body = xode_get_tag(node, "body");
			char *msg;
			
			if (!type)
				type = "chat";
			if (!strcmp(type, "error")) {	
				LM_DBG("received message error stanza\n");
				goto out;
			}
			
			if (!from || !to || !body) {
				LM_DBG("invalid <message/> attributes\n");
				goto out;
			}

			if (!(msg = xode_get_data(body)))
				msg = "";
			xmpp_send_sip_msg(
				encode_uri_xmpp_sip(from),
				decode_uri_xmpp_sip(to),
				msg);
		} else if (!strcmp(tag, "presence")) {
			/* call presence callbacks */
			LM_DBG("XMPP Presence received\n");
			run_xmpp_callbacks(XMPP_RCV_PRESENCE, xode_to_str(node));
		}else if (!strcmp(tag, "iq")) {
			/* call presence callbacks */
			LM_DBG("XMPP IQ received\n");
			run_xmpp_callbacks(XMPP_RCV_IQ, xode_to_str(node));
		}
		break;
	case XODE_STREAM_ERROR:
		LM_ERR("stream error\n");
		/* fall-through */
	case XODE_STREAM_CLOSE:
		priv->running = 0;
		break;
	}
out:
	xode_free(node);
}
Exemple #11
0
tt_user *users_read(const char *jid) {
	tt_user *user;
	xode cfg, x;
	char *file, *temp;

	if (jid == NULL)
		return NULL;

	if ((file = malloc(strlen(config_dir) + strlen(jid) + 2)) == NULL) {
		perror("malloc()");
		exit(1);
	}
	sprintf(file, "%s/%s", config_dir, jid);

	my_debug(0, "users: Wczytuje plik %s", file);

	if ((cfg = xode_from_file(file)) == NULL) {
		my_debug(0, "users: Nie moge wczytac usera");
		return NULL;
	}

	user = (tt_user*)malloc(sizeof(tt_user));
	memset(user, 0, sizeof(tt_user));

	x = xode_get_tag(cfg, "jid");
	temp = xode_get_data(x);
	my_strcpy(user->jid, temp);

	x = xode_get_tag(cfg, "tid");
	temp = xode_get_data(x);
	my_strcpy(user->tid_short, temp);

	user->tid = malloc(strlen(user->tid_short) + strlen("@tlen.pl") + 1);
	sprintf(user->tid, "*****@*****.**", user->tid_short);

	x = xode_get_tag(cfg, "password");
	temp = xode_get_data(x);
	my_strcpy(user->password, temp);

	user->tlen_sesja = NULL;
	user->tlen_loged = 0;
	user->last_ping_time = 0;

	user->roster = NULL;
	user->search = NULL;
	user->search_id = NULL;

	x = xode_get_tag(cfg, "notify");

	temp = xode_get_attrib(x, "mail");
	if (temp != NULL) {
		if (strcmp(temp, "1") == 0) {
			user->mailnotify = 1;
		} else if (strcmp(temp, "0") == 0) {
			user->mailnotify = 0;
		} else {
			user->mailnotify = 0;
			my_debug(0, "Blad w pliku");
		}
	} else {
		user->mailnotify = 0;
	}

	temp = xode_get_attrib(x, "typing");
	if (temp != NULL) {
		if (strcmp(temp, "1") == 0) {
			user->typingnotify = 1;
		} else if (strcmp(temp, "0") == 0) {
			user->typingnotify = 0;
		} else {
			user->typingnotify = 0;
			my_debug(0, "Blad w pliku");
		}
	} else {
		user->typingnotify = 0;
	}

	temp = xode_get_attrib(x, "alarm");
	if (temp != NULL) {
		if (strcmp(temp, "1") == 0) {
			user->alarmnotify = 1;
		} else if (strcmp(temp, "0") == 0) {
			user->alarmnotify = 0;
		} else {
			user->alarmnotify = 0;
			my_debug(0, "Blad w pliku");
		}
	} else {
		user->alarmnotify = 0;
	}

	user->status = NULL;
	user->status_type = 0;

	user->jabber_status = NULL;
	user->jabber_status_type = NULL;

	xode_free(cfg);
	t_free(file);

	return user;
}
Exemple #12
0
static void in_stream_node_callback(int type, xode node, void *arg) 
{
	struct xmpp_connection *conn = (struct xmpp_connection *) arg;
	char *tag;
	xode x;

	LM_DBG("instream callback: %d: %s\n",
			type, node ? xode_get_name(node) : "n/a");
	switch (type) {
	case XODE_STREAM_ROOT:
		conn->stream_id = strdup(random_secret());
		net_printf(conn->fd,
			"<?xml version='1.0'?>"
			"<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:server' version='1.0'"
			" xmlns:db='jabber:server:dialback' id='%s' from='%s'>", conn->stream_id, xmpp_domain);
		net_printf(conn->fd,"<stream:features xmlns:stream='http://etherx.jabber.org/streams'/>");
		break;
	case XODE_STREAM_NODE:
		tag = xode_get_name(node);

		if (!strcmp(tag, "db:result")) {
			char *from = xode_get_attrib(node, "from");
			char *to = xode_get_attrib(node, "to");
			/* char *id = xode_get_attrib(node, "id"); */
			char *type = xode_get_attrib(node, "type");
			char *cdata = xode_get_data(node);
			
			if (!type) {
				if (conn->domain) {
					LM_DBG("connection %d has old domain '%s'\n",conn->fd,
							conn->domain);
					free(conn->domain);
				}
				conn->domain = strdup(from);
				LM_DBG("connection %d set domain '%s'\n",
						conn->fd, conn->domain);

				/* it's a request; send verification over outgoing connection */
				x = xode_new_tag("db:verify");
				xode_put_attrib(x, "xmlns:db", "jabber:server:dialback");
				xode_put_attrib(x, "from", to);
				xode_put_attrib(x, "to", from);
				//xode_put_attrib(x, "id", "someid"); /* XXX fix ID */
				xode_put_attrib(x, "id", conn->stream_id);
				xode_insert_cdata(x, cdata, -1);
				xode_send_domain(from, x);
			}			
		} else if (!strcmp(tag, "db:verify")) {
			char *from = xode_get_attrib(node, "from");
			char *to = xode_get_attrib(node, "to");
			char *id = xode_get_attrib(node, "id");
			char *type = xode_get_attrib(node, "type");
			char *cdata = xode_get_data(node);
			
			if (!type) {
				/* it's a request */
				x = xode_new_tag("db:verify");
				xode_put_attrib(x, "xmlns:db", "jabber:server:dialback");
				xode_put_attrib(x, "from", to);
				xode_put_attrib(x, "to", from);
				xode_put_attrib(x, "id", id);
				//if (cdata && !strcmp(cdata, DB_KEY)) {
				if (cdata && !strcmp(cdata, db_key(local_secret, from, id))) {
					xode_put_attrib(x, "type", "valid");
				} else {
					xode_put_attrib(x, "type", "invalid");
				}
				xode_send(conn->fd, x);
				xode_free(x);
			}			
		} else if (!strcmp(tag, "message")) {
			char *from = xode_get_attrib(node, "from");
			char *to = xode_get_attrib(node, "to");
			char *type = xode_get_attrib(node, "type");
			xode body = xode_get_tag(node, "body");
			char *msg;
			
			if (!type)
				type = "chat";
			if (!strcmp(type, "error")) {	
				LM_DBG("received message error stanza\n");
				goto out;
			}
			
			if (!from || !to || !body) {
				LM_DBG("invalid <message/> attributes\n");
				goto out;
			}

			if (!(msg = xode_get_data(body)))
				msg = "";
			xmpp_send_sip_msg(
				encode_uri_xmpp_sip(from),
				decode_uri_xmpp_sip(to),
				msg);
		} else if (!strcmp(tag, "presence")) {
			/* run presence callbacks */
		}
		break;

		break;
	case XODE_STREAM_ERROR:
		LM_ERR("instream error\n");
		/* fall-through */
	case XODE_STREAM_CLOSE:
		conn->type = CONN_DEAD;
		break;
	}
out:
	xode_free(node);
}
Exemple #13
0
static void out_stream_node_callback(int type, xode node, void *arg)
{
	struct xmpp_connection *conn = (struct xmpp_connection *) arg;
	struct xmpp_connection *in_conn = NULL;
	char *tag;
	xode x;

	LM_DBG("outstream callback: %d: %s\n", type, 
			node?xode_get_name(node):"n/a");

	if (conn->domain)
		in_conn = conn_find_domain(conn->domain, CONN_INBOUND);

	switch (type) {
	case XODE_STREAM_ROOT:
		x = xode_new_tag("db:result");
		xode_put_attrib(x, "xmlns:db", "jabber:server:dialback");
		xode_put_attrib(x, "from", xmpp_domain);
		xode_put_attrib(x, "to", conn->domain);
		//xode_insert_cdata(x, DB_KEY, -1);
		xode_insert_cdata(x, db_key(local_secret, conn->domain,
					xode_get_attrib(node, "id")), -1);
		xode_send(conn->fd, x);
		xode_free(x);

		break;
	case XODE_STREAM_NODE:
		tag = xode_get_name(node);

		if (!strcmp(tag, "db:verify")) {
			char *from = xode_get_attrib(node, "from");
			char *to = xode_get_attrib(node, "to");
			char *id = xode_get_attrib(node, "id");
			char *type = xode_get_attrib(node, "type");
			/* char *cdata = xode_get_data(node); */
			
			if (!strcmp(type, "valid") || !strcmp(type, "invalid")) {
				/* got a reply, report it */
				x = xode_new_tag("db:result");
				xode_put_attrib(x, "xmlns:db", "jabber:server:dialback");
				xode_put_attrib(x, "from", to);
				xode_put_attrib(x, "to", from);
				xode_put_attrib(x, "id", id);
				xode_put_attrib(x, "type", type);
				if (in_conn)
					xode_send(in_conn->fd, x);
				else
					LM_ERR("need to send reply to domain '%s', but no inbound"
							" connection found\n", from);
				xode_free(x);
			}
		} else if (!strcmp(tag, "db:result")) {
			char *type = xode_get_attrib(node, "type");
			
			if (type && !strcmp(type, "valid")) {
				/* the remote server has successfully authenticated us,
				 * we can now send data */
				for (x = xode_get_firstchild(conn->todo); x;
						x = xode_get_nextsibling(x)) {
					LM_DBG("sending todo tag '%s'\n", xode_get_name(x));
					xode_send(conn->fd, x);
				}
				xode_free(conn->todo);
				conn->todo = NULL;
			}
		}
		break;
	case XODE_STREAM_ERROR:
		LM_ERR("outstream error\n");
		/* fall-through */
	case XODE_STREAM_CLOSE:
		conn->type = CONN_DEAD;
		break;
	}
	xode_free(node);
}
/**
 * authentication to the JABBER server
 */
int xj_jcon_user_auth(xj_jcon jbc, char *username, char *passwd,
				char *resource)
{
	char msg_buff[4096];
	int n, i, err;
	char *p0, *p1;
	xode x, y, z;

	/*** send open stream tag **/
	sprintf(msg_buff, JB_CLIENT_OPEN_STREAM, jbc->hostname);
	if(send(jbc->sock, msg_buff, strlen(msg_buff), 0) != strlen(msg_buff))
		goto error;
	
	n = recv(jbc->sock, msg_buff, 4096, 0);
	msg_buff[n] = 0;
	if(strncasecmp(msg_buff, JB_START_STREAM, JB_START_STREAM_LEN))
		goto error;
	
	p0 = strstr(msg_buff + JB_START_STREAM_LEN, "id='");
	if(p0 == NULL)
		goto error;

	p0 += 4;
	p1 = strchr(p0, '\'');
	if(p1 == NULL)
		goto error;

	jbc->stream_id = (char*)_M_MALLOC(p1-p0+1);
	strncpy(jbc->stream_id, p0, p1-p0);
    jbc->stream_id[p1-p0] = 0;

	sprintf(msg_buff, "%08X", jbc->seq_nr);
	
	x = xode_new_tag("iq");
	if(!x)
		return -1;

	xode_put_attrib(x, "id", msg_buff);
	xode_put_attrib(x, "type", "get");
	y = xode_insert_tag(x, "query");
	xode_put_attrib(y, "xmlns", "jabber:iq:auth");
	z = xode_insert_tag(y, "username");
	xode_insert_cdata(z, username, -1);
	p0 = xode_to_str(x);
	n = strlen(p0);

	i = send(jbc->sock, p0, n, 0);
	if(i != n)
		goto errorx;
	
	xode_free(x);
	
	// receive  response
	// try 10 times
	i = 10;
	while(i)
	{
		if((n = recv(jbc->sock, msg_buff, 4096, 0)) > 0)
		{
			msg_buff[n] = 0;
			break;
		}
		usleep(1000);
		i--;
	}
	if(!i)
		goto error;

	x = xode_from_strx(msg_buff, n, &err, &i);
	p0 = msg_buff;
	if(err)
		p0 += i; 
	
	if(strncasecmp(xode_get_name(x), "iq", 2))
		goto errorx;
	
	if((x = xode_get_tag(x, "query?xmlns=jabber:iq:auth")) == NULL)
		goto errorx;
			
	y = xode_new_tag("query");
	xode_put_attrib(y, "xmlns", "jabber:iq:auth");
	z = xode_insert_tag(y, "username");
	xode_insert_cdata(z, username, -1);
	z = xode_insert_tag(y, "resource");
	xode_insert_cdata(z, resource, -1);
	
	if(xode_get_tag(x, "digest") != NULL)
	{ // digest authentication
			
		//sprintf(msg_buff, "%s%s", jbc->stream_id, passwd);
		strcpy(msg_buff, jbc->stream_id);
		strcat(msg_buff, passwd);
		//DBG("XJAB:xj_jcon_user_auth: [%s:%s]\n", jbc->stream_id, passwd);
		p1 = shahash(msg_buff);

		z = xode_insert_tag(y, "digest");
		xode_insert_cdata(z, p1, -1);
	}
	else
	{ // plaint text authentication
		z = xode_insert_tag(y, "password");
		xode_insert_cdata(z, passwd, -1);
	}

	y = xode_wrap(y, "iq");
	
	jbc->seq_nr++;
	sprintf(msg_buff, "%08X", jbc->seq_nr);
	
	xode_put_attrib(y, "id", msg_buff);
	xode_put_attrib(y, "type", "set");
	
	p1 = xode_to_str(y);
	n = strlen(p1);
	
	i = send(jbc->sock, p1, n, 0);
	if(i != n)
	{
		xode_free(y);
		goto errorx;
	}
	xode_free(x);
	xode_free(y);
	
	// receive  response
	// try 10 times
	i = 10;
	while(i)
	{
		if((n = recv(jbc->sock, msg_buff, 4096, 0)) > 0)
		{
			msg_buff[n] = 0;
			break;
		}
		usleep(1000);
		i--;
	}
	if(!i)
		goto error;

	x = xode_from_strx(msg_buff, n, &err, &i);
	p0 = msg_buff;
	if(err)
		p0 += i; 
	
	if(strncasecmp(xode_get_name(x), "iq", 2) || 
			strncasecmp(xode_get_attrib(x, "type"), "result", 6))
		goto errorx;
	
	jbc->resource = (char*)_M_MALLOC(strlen(resource)+1);
	strcpy(jbc->resource, resource);

	jbc->allowed = XJ_NET_ALL;
	jbc->ready = XJ_NET_JAB;	
	return 0;
	
errorx:
	xode_free(x);
error:
	return -1;
}