Exemple #1
0
const char*
connection_get_fulljid(void)
{
    const char *jid = xmpp_conn_get_bound_jid(conn.xmpp_conn);
    if (jid) {
        return jid;
    } else {
        return xmpp_conn_get_jid(conn.xmpp_conn);
    }
}
Exemple #2
0
static int alarmqueue_handler(xmpp_conn_t * const conn, void * const userdata)
{
    conflate_handle_t *handle = (conflate_handle_t*) userdata;
    alarm_t alarm;
    const char* myjid = xmpp_conn_get_bound_jid(conn);
    char id[262];
    char open[2] = { 0, 0 };
    char amsg[256];
    char body[1500];
    char num[255];
    while (handle->alarms->size > 0)
        {
            alarm = get_alarm(handle->alarms);
            open[0] = alarm.open ? '1' : '2';
            snprintf(amsg, sizeof(amsg), "%s", alarm.msg);
            /* if we got a legitimate alarm, send off alert */
            if(alarm.open == 1)
                {
                    snprintf(id, sizeof(id), "_alarm%d", alarm.num);
                    //handler_add_id(conn, alarm_response_handler, id, handle);
                    //handler_add_timed(conn, alarm_missing_handler, 120000, handle);
                    xmpp_stanza_t* msg = xmpp_stanza_new(handle->ctx);
                    assert(msg);
                    xmpp_stanza_set_name(msg, "message");
                    //xmpp_stanza_set_type(iq, "set");
                    xmpp_stanza_set_id(msg, id);
                    //xmpp_stanza_set_attribute(iq, "to", xmpp_stanza_get_attribute(stanza, "from"));
                    /* TODO: This needs to have a config on where to report to */
                    xmpp_stanza_set_attribute(msg, "to", "*****@*****.**");
                    xmpp_stanza_set_attribute(msg, "from", myjid);

                    xmpp_stanza_t* mbody = xmpp_stanza_new(handle->ctx);
                    assert(mbody);
                    xmpp_stanza_set_name(mbody, "body");
                    snprintf(body, sizeof(body), "Alert\n%s", amsg);
                    snprintf(num, sizeof(num), "%d", alarm.num);
                    xmpp_stanza_set_text(mbody, body);
                    add_and_release(msg, mbody);

                    xmpp_stanza_t* alert = xmpp_stanza_new(handle->ctx);
                    assert(alert);
                    xmpp_stanza_set_name(alert, "alert");
                    xmpp_stanza_set_attribute(alert, "xmlns", "http://northscale.net/protocol/alerts");
                    xmpp_stanza_set_attribute(alert, "open", open);
                    xmpp_stanza_set_attribute(alert, "msg", amsg);
                    xmpp_stanza_set_attribute(alert, "num", num);
                    xmpp_stanza_set_attribute(alert, "name", alarm.name);
                    add_and_release(msg, alert);

                    xmpp_send(conn, msg);
                    xmpp_stanza_release(msg);
                }
        }
    return 1;
}
Exemple #3
0
static void conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status,
                         const int error, xmpp_stream_error_t * const stream_error,
                         void * const userdata)
{
    conflate_handle_t *handle = (conflate_handle_t *)userdata;

    if (status == XMPP_CONN_CONNECT) {
        xmpp_stanza_t* pres = NULL, *priority = NULL, *pri_text = NULL;
        CONFLATE_LOG(handle, LOG_LVL_INFO, "Connected.");
        xmpp_handler_add(conn, version_handler, "jabber:iq:version", "iq", NULL, handle);
        xmpp_handler_add(conn, command_handler, "http://jabber.org/protocol/commands",
                         "iq", NULL, handle);
        xmpp_handler_add(conn, disco_items_handler,
                         "http://jabber.org/protocol/disco#items", "iq", NULL, handle);
        xmpp_handler_add(conn, message_handler, NULL, "message", NULL, handle);
        xmpp_timed_handler_add(conn, keepalive_handler, 60000, handle);
        xmpp_timed_handler_add(conn, alarmqueue_handler, 10000, handle);

        /* Send initial <presence/> so that we appear online to contacts */
        pres = xmpp_stanza_new(handle->ctx);
        assert(pres);
        xmpp_stanza_set_name(pres, "presence");

        priority = xmpp_stanza_new(handle->ctx);
        assert(priority);
        xmpp_stanza_set_name(priority, "priority");
        add_and_release(pres, priority);

        pri_text = xmpp_stanza_new(handle->ctx);
        assert(pri_text);
        xmpp_stanza_set_text(pri_text, "5");
        add_and_release(priority, pri_text);

        xmpp_send(conn, pres);
        xmpp_stanza_release(pres);

        /* Store the bound jid */
        if (!conflate_save_private(handle, STORED_JID_KEY,
                                   xmpp_conn_get_bound_jid(conn),
                                   handle->conf->save_path)) {
            CONFLATE_LOG(handle, LOG_LVL_WARN, "Failed to save the bound jid");
        }
    }
    else {
        CONFLATE_LOG(handle, LOG_LVL_INFO, "disconnected.");
        xmpp_stop(handle->ctx);
    }
}
Exemple #4
0
xmpp_ibb_session_t *xmpp_ibb_open(xmpp_conn_t * const conn, char * const peer, char * const sid)
{
    xmpp_ibb_session_t *sess;
    xmpp_stanza_t *iq, *open;
    xmpp_ctx_t *ctx;
    const char *jid = xmpp_conn_get_bound_jid(conn);
    char sizetemp[6] = "";
    int size;

    if (peer == NULL || strlen(peer) == 0) {
        return NULL;
    }
    sess = _ibb_session_init(conn, peer, sid);
    if (sess == NULL) {
        return NULL;
    }
    size = snprintf(sizetemp, sizeof(sizetemp), "%d", sess->block_size);
    if (size < sizeof(sizetemp)) {
        sizetemp[size] = '\0';
    }
    nmtoken_generate(sess->id, 8);

    ctx = xmpp_conn_get_context(conn);
    iq = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(iq, "iq");
    xmpp_stanza_set_type(iq, "set");
    xmpp_stanza_set_id(iq, sess->id);
    xmpp_stanza_set_attribute(iq, "from", jid);
    xmpp_stanza_set_attribute(iq, "to", sess->peer);

    open = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(open, "open");
    xmpp_stanza_set_ns(open, XMLNS_IBB);
    xmpp_stanza_set_attribute(open, "block-size", sizetemp);
    xmpp_stanza_set_attribute(open, "sid", sess->sid);
    xmpp_stanza_set_attribute(open, "stanza", "iq");

    xmpp_stanza_add_child(iq, open);
    xmpp_send(conn, iq);
    xmpp_stanza_release(open);
    xmpp_stanza_release(iq);

    ilist_add(g_list, sess);

    return sess;
}
Exemple #5
0
int xmpp_ibb_close(xmpp_ibb_session_t *sess)
{
    xmpp_stanza_t *iq, *close;
    xmpp_ctx_t *ctx;
    const char *jid;

    if (sess == NULL) { return -1; }

    if (!ilist_foundinlist(g_list, sess)) {
        fprintf(stderr, "session is not in handle, may be closed.\n");
        return -1;
    }

    jid = xmpp_conn_get_bound_jid(sess->conn);
    ctx = xmpp_conn_get_context(sess->conn);

    iq = xmpp_stanza_new(ctx);
    close = xmpp_stanza_new(ctx);
    nmtoken_generate(sess->id, 8);

    xmpp_stanza_set_name(iq, "iq");
    xmpp_stanza_set_type(iq, "set");
    xmpp_stanza_set_id(iq, sess->id);
    xmpp_stanza_set_attribute(iq, "to", sess->peer);
    xmpp_stanza_set_attribute(iq, "from", jid);

    xmpp_stanza_set_name(close, "close");
    xmpp_stanza_set_ns(close, XMLNS_IBB);
    xmpp_stanza_set_attribute(close, "sid", sess->sid);

    xmpp_stanza_add_child(iq, close);
    xmpp_send(sess->conn, iq);
    xmpp_stanza_release(close);
    xmpp_stanza_release(iq);

    sess->state = STATE_CLOSING;

    return 0;
}
Exemple #6
0
static int disco_items_handler(xmpp_conn_t * const conn,
                               xmpp_stanza_t * const stanza,
                               void * const userdata)
{
    xmpp_stanza_t *reply, *query;
    const char* myjid = xmpp_conn_get_bound_jid(conn);
    conflate_handle_t *handle = (conflate_handle_t*) userdata;

    assert(conn);
    assert(myjid);
    assert(stanza);
    assert(userdata);

    reply = xmpp_stanza_new(handle->ctx);
    assert(reply);
    xmpp_stanza_set_name(reply, "iq");
    xmpp_stanza_set_type(reply, "result");
    xmpp_stanza_set_id(reply, xmpp_stanza_get_id(stanza));
    xmpp_stanza_set_attribute(reply, "to",
                              xmpp_stanza_get_attribute(stanza, "from"));
    xmpp_stanza_set_attribute(reply, "from", myjid);

    query = xmpp_stanza_new(handle->ctx);
    assert(query);
    xmpp_stanza_set_name(query, "query");
    xmpp_stanza_set_attribute(query, "xmlns", XMPP_NS_DISCO_ITEMS);
    xmpp_stanza_set_attribute(query, "node", "http://jabber.org/protocol/commands");
    add_and_release(reply, query);

    for (struct command_def *p = commands; p; p = p->next) {
        add_disco_item(handle->ctx, query, myjid, p->name, p->description);
    }

    xmpp_send(conn, reply);
    xmpp_stanza_release(reply);

    return 1;
}
Exemple #7
0
int xmpp_ibb_send_data(xmpp_ibb_session_t *sess, xmppdata_t *xdata)
{
    int i;
    xmpp_stanza_t *iq, *data, *text;
    char *encode, seqchar[16] = "";
    xmpp_ctx_t *ctx;
    if (sess == NULL || xdata == NULL) {
        return -1;
    }
    if (xdata->size > (sess->block_size / 4 * 3)) {
        return -1;
    }
    const char *jid = xmpp_conn_get_bound_jid(sess->conn);

    for (i = 0; i < 50; i++) {
        if (sess->state == STATE_READY) {
            break ;
        } else if (sess->state == STATE_SENDING) {
            fprintf(stderr, "skip invalid state(%d).\n", sess->state);
            break ;
        } else if (sess->state == STATE_OPENING) {
            usleep(100000);
        } else {
            fprintf(stderr, "invalid state(%d).\n", sess->state);
            break ;
        }
    }

    if (sess->state != STATE_SENDING && sess->state != STATE_READY) {
        fprintf(stderr, "xmpp_ibb_send_data() failed. state(%d) not ready.\n", sess->state);
        return -1;
    }

    if (!ilist_foundinlist(g_list, sess)) {
        fprintf(stderr, "session is not in handle, may be closed.\n");
        return -1;
    }

    ctx = xmpp_conn_get_context(sess->conn);

    iq = xmpp_stanza_new(ctx);
    data = xmpp_stanza_new(ctx);
    text = xmpp_stanza_new(ctx);
    nmtoken_generate(sess->id, 8);

    xmpp_stanza_set_name(iq, "iq");
    xmpp_stanza_set_type(iq, "set");
    xmpp_stanza_set_id(iq, sess->id);
    xmpp_stanza_set_attribute(iq, "to", sess->peer);
    xmpp_stanza_set_attribute(iq, "from", jid);

    xmpp_stanza_set_name(data, "data");
    xmpp_stanza_set_ns(data, XMLNS_IBB);
    xmpp_stanza_set_attribute(data, "sid", sess->sid);
    snprintf(seqchar, sizeof(seqchar), "%d", ++(sess->send_seq));
    xmpp_stanza_set_attribute(data, "seq", seqchar);

    xmpp_b64encode(xdata->data, xdata->size, &encode);
    if (encode == NULL) {
        xmpp_stanza_set_text_with_size(text, "", 0);
    } else {
        xmpp_stanza_set_text_with_size(text, encode, strlen(encode));
    }

    xmpp_stanza_add_child(data, text);
    xmpp_stanza_add_child(iq, data);
    sess->state = STATE_SENDING;
    xmpp_send(sess->conn, iq);

    xmpp_stanza_release(text);
    xmpp_stanza_release(data);
    xmpp_stanza_release(iq);
    xmpp_b64free(encode);

    return sess->send_seq;
}