Exemple #1
0
/** insert a roster item into this pkt, starting at elem */
static void _roster_insert_item(pkt_t pkt, item_t item, int elem)
{
    int ns, i;
    char *sub;

    ns = nad_add_namespace(pkt->nad, uri_CLIENT, NULL);
    elem = nad_insert_elem(pkt->nad, elem, ns, "item", NULL);
    nad_set_attr(pkt->nad, elem, -1, "jid", jid_full(item->jid), 0);

    if(item->to && item->from)
        sub = "both";
    else if(item->to)
        sub = "to";
    else if(item->from)
        sub = "from";
    else
        sub = "none";

    nad_set_attr(pkt->nad, elem, -1, "subscription", sub, 0);

    if(item->ask == 1)
        nad_set_attr(pkt->nad, elem, -1, "ask", "subscribe", 9);
    else if(item->ask == 2)
        nad_set_attr(pkt->nad, elem, -1, "ask", "unsubscribe", 11);

    if(item->name != NULL)
        nad_set_attr(pkt->nad, elem, -1, "name", item->name, 0);

    for(i = 0; i < item->ngroups; i++)
        nad_insert_elem(pkt->nad, elem, NAD_ENS(pkt->nad, elem), "group", item->groups[i]);
}
Exemple #2
0
static mod_ret_t _vacation_pkt_user(mod_instance_t mi, user_t user, pkt_t pkt) {
    module_t mod = mi->mod;
    vacation_t v = user->module_data[mod->index];
    time_t t;
    pkt_t res;

    if(v->msg == NULL)
        return mod_PASS;

    /* only want messages, and only if they're offline */
    if(!(pkt->type & pkt_MESSAGE) || user->top != NULL)
        return mod_PASS;

    /* reply only to real, human users - they always have full JIDs in 'from' */
    jid_expand(pkt->from);
    if(pkt->from->node[0] == '\0' || pkt->from->resource[0] == '\0') {
        pkt_free(pkt);
        return mod_HANDLED;
    }

    t = time(NULL);

    if(v->start < t && (t < v->end || v->end == 0)) {
        res = pkt_create(mod->mm->sm, "message", NULL, jid_full(pkt->from), mod->mm->sm->id);
        nad_insert_elem(res->nad, 1, NAD_ENS(res->nad, 1), "subject", "Automated reply");
        nad_insert_elem(res->nad, 1, NAD_ENS(res->nad, 1), "body", v->msg);
        pkt_router(res);

        /* !!! remember that we sent this */
    }

    return mod_PASS;
}
Exemple #3
0
/** error the packet */
nad_t stanza_error(nad_t nad, int elem, int err) {
    int ns;

    assert((int) (nad != NULL));
    assert((int) (elem >= 0));
    assert((int) (err >= stanza_err_BAD_REQUEST && err < stanza_err_LAST));

    err = err - stanza_err_BAD_REQUEST;

    nad_set_attr(nad, elem, -1, "type", "error", 5);

    elem = nad_insert_elem(nad, elem, NAD_ENS(nad, elem), "error", NULL);
    if(_stanza_errors[err].code != NULL)
    nad_set_attr(nad, elem, -1, "code", _stanza_errors[err].code, 0);
    if(_stanza_errors[err].type != NULL)
        nad_set_attr(nad, elem, -1, "type", _stanza_errors[err].type, 0);

    if(_stanza_errors[err].name != NULL) {
        ns = nad_add_namespace(nad, uri_STANZA_ERR, NULL);
        nad_insert_elem(nad, elem, ns, _stanza_errors[err].name, NULL);
    }

    return nad;
}
Exemple #4
0
static mod_ret_t _vacation_in_sess(mod_instance_t mi, sess_t sess, pkt_t pkt) {
    module_t mod = mi->mod;
    vacation_t v = sess->user->module_data[mod->index];
    int ns, start, end, msg;
    char dt[30];
    pkt_t res;
    os_t os;
    os_object_t o;

    /* we only want to play with vacation iq packets */
    if((pkt->type != pkt_IQ && pkt->type != pkt_IQ_SET) || pkt->ns != ns_VACATION)
        return mod_PASS;

    /* if it has a to, throw it out */
    if(pkt->to != NULL)
        return -stanza_err_BAD_REQUEST;

    /* get */
    if(pkt->type == pkt_IQ) {
        if(v->msg == NULL) {
            res = pkt_create(mod->mm->sm, "iq", "result", NULL, NULL);
            pkt_id(pkt, res);
            pkt_free(pkt);

            pkt_sess(res, sess);

            return mod_HANDLED;
        }

        ns = nad_find_scoped_namespace(pkt->nad, uri_VACATION, NULL);

        if(v->start != 0) {
            datetime_out(v->start, dt_DATETIME, dt, 30);
            nad_insert_elem(pkt->nad, 2, ns, "start", dt);
        } else
            nad_insert_elem(pkt->nad, 2, ns, "start", NULL);

        if(v->end != 0) {
            datetime_out(v->end, dt_DATETIME, dt, 30);
            nad_insert_elem(pkt->nad, 2, ns, "end", dt);
        } else
            nad_insert_elem(pkt->nad, 2, ns, "end", NULL);

        nad_insert_elem(pkt->nad, 2, ns, "message", v->msg);

        pkt_tofrom(pkt);
        nad_set_attr(pkt->nad, 1, -1, "type", "result", 6);
            
        pkt_sess(pkt, sess);

        return mod_HANDLED;
    }

    /* set */
    ns = nad_find_scoped_namespace(pkt->nad, uri_VACATION, NULL);

    start = nad_find_elem(pkt->nad, 2, ns, "start", 1);
    end = nad_find_elem(pkt->nad, 2, ns, "end", 1);
    msg = nad_find_elem(pkt->nad, 2, ns, "message", 1);

    if(start < 0 || end < 0 || msg < 0) {
        /* forget */
        if(v->msg != NULL) {
            free(v->msg);
            v->msg = NULL;
        }
        v->start = 0;
        v->end = 0;

        storage_delete(mi->sm->st, "vacation-settings", jid_user(sess->jid), NULL);

        res = pkt_create(mod->mm->sm, "iq", "result", NULL, NULL);
        pkt_id(pkt, res);
        pkt_free(pkt);

        pkt_sess(res, sess);

        return mod_HANDLED;
    }

    if(NAD_CDATA_L(pkt->nad, start) > 0) {
        strncpy(dt, NAD_CDATA(pkt->nad, start), (30 < NAD_CDATA_L(pkt->nad, start) ? 30 : NAD_CDATA_L(pkt->nad, start)));
        v->start = datetime_in(dt);
    } else
        v->start = 0;

    if(NAD_CDATA_L(pkt->nad, end) > 0) {
        strncpy(dt, NAD_CDATA(pkt->nad, end), (30 < NAD_CDATA_L(pkt->nad, end) ? 30 : NAD_CDATA_L(pkt->nad, end)));
        v->end = datetime_in(dt);
    } else
        v->end = 0;

    v->msg = (char *) malloc(sizeof(char) * (NAD_CDATA_L(pkt->nad, msg) + 1));
    strncpy(v->msg, NAD_CDATA(pkt->nad, msg), NAD_CDATA_L(pkt->nad, msg));
    v->msg[NAD_CDATA_L(pkt->nad, msg)] = '\0';

    os = os_new();
    o = os_object_new(os);
    os_object_put(o, "start", &v->start, os_type_INTEGER);
    os_object_put(o, "end", &v->end, os_type_INTEGER);
    os_object_put(o, "message", v->msg, os_type_STRING);

    if(storage_replace(mod->mm->sm->st, "vacation-settings", jid_user(sess->user->jid), NULL, os) != st_SUCCESS) {
        free(v->msg);
        v->msg = NULL;
        v->start = 0;
        v->end = 0;
        return -stanza_err_INTERNAL_SERVER_ERROR;
    }
    
    res = pkt_create(mod->mm->sm, "iq", "result", NULL, NULL);
    pkt_id(pkt, res);
    pkt_free(pkt);

    pkt_sess(res, sess);

    return mod_HANDLED;
}
Exemple #5
0
/**
 * Saves packet into database.
 * @param direct Direction of the message - 2 = incomming, 1 = sent
 */
mod_ret_t savepkt(pkt_t pkt, int direct) {
    int body=-1, sub=-1, type=-1, arch=-1;
    char *mem=NULL;
    const *owner = NULL, *other = NULL;
    int sz = 0;
    int archive=default_mode;
    char filter[2060]; // 2048 is jid_user maximum length
    time_t t=0;
    jid_t own_jid, other_jid;
    os_t os, set_os=NULL;
    os_object_t o, set_o=NULL;

    // Is it a message?
    log_debug(ZONE, "Testing message...");
    if ( (pkt->type & pkt_MESSAGE) == 0)
        return mod_PASS;

    log_debug(ZONE, "It is a message...");

    // 0 element is route, 1st is message, we need subelement of message
    body = nad_find_elem(pkt->nad, 1, -1, "body",    1);
    log_debug(ZONE, "Body is at %d", body);
    sub  = nad_find_elem(pkt->nad, 1, -1, "subject", 1);
    log_debug(ZONE, "Subject is at %d", sub);
    type = nad_find_attr(pkt->nad, 1, -1, "type", NULL);
    log_debug(ZONE, "Type %d", type);

    // Are these parts really interesting?
    if( ( (body < 0) || (NAD_CDATA_L(pkt->nad, body) < 1) ) &&
        ( (sub  < 0) || (NAD_CDATA_L(pkt->nad, sub ) < 1) ) )
        return mod_PASS;

    log_debug(ZONE, "It's meaningful message!", pkt->from);

    // What direction are we talking about?
    if (direct == IN_D) {
        own_jid   = pkt->to;
        other_jid = pkt->from;
    } else {
        own_jid   = pkt->from;
        other_jid = pkt->to;
    }

    // Get JIDs
    if(own_jid != NULL) {
        owner=jid_user(own_jid);
    } else {
        return mod_PASS;
    }
    if(other_jid != NULL) {
        other=jid_user(other_jid);
    } else {
        return mod_PASS;
    }

    // Check settings

    // Load defaults
    snprintf(filter, 2060, "(jid=%s)", owner);
    if((storage_get(pkt->sm->st, tbl_name "_settings", owner, filter, &set_os) == st_SUCCESS) &&
        (os_iter_first(set_os)) && ((set_o=os_iter_object(set_os))!=NULL))
        os_object_get_int(set_os, set_o, "setting", &archive);

    // Cleanup
    if(set_o != NULL) {
        os_object_free(set_o);
        set_o=NULL;
    }
    if(set_os != NULL) {
        os_free(set_os);
        set_os=NULL;
    }

    // Load contact specific
    snprintf(filter, 2060, "(jid=%s)", other);
    if((storage_get(pkt->sm->st, tbl_name "_settings", owner, filter, &set_os) == st_SUCCESS) &&
        (os_iter_first(set_os)) && ((set_o=os_iter_object(set_os))!=NULL))
        os_object_get_int(set_os, set_o, "setting", &archive);

    // Cleanup
    if(set_o != NULL) {
        os_object_free(set_o);
        set_o=NULL;
    }
    if(set_os != NULL) {
        os_free(set_os);
        set_os=NULL;
    }

    // Do we need to check roster?
    if(archive==A_ROSTER) {
        snprintf(filter, 2060, "(jid=%s)", other);
        if(storage_get(pkt->sm->st, "roster-items", owner, filter, &set_os) == st_SUCCESS)
            archive=A_ALWAYS;
        else
            archive=A_NEVER;
        if(set_os != NULL) {
            os_free(set_os);
            set_os=NULL;
        }
    }

    // Decide
    if(archive==A_NEVER)
        return mod_PASS;

    // Prepare to store them
    os = os_new();
    if(os == NULL) return mod_PASS;
    o = os_object_new(os);
    if(o  == NULL) return mod_PASS;

    // Real storing
    log_debug(ZONE, "Saving message for %s (other party is %s)", owner, other);

    // Message

    // Buffer allocation
    if(body > 0) {
        sz = NAD_CDATA_L(pkt->nad, body) / 1024;
        log_debug(ZONE, "Body size %d", NAD_CDATA_L(pkt->nad, body));
    }
    if(sub > 0) {
        sz = max(sz, NAD_CDATA_L(pkt->nad, sub)  / 1024);
        log_debug(ZONE, "Subj size %d", NAD_CDATA_L(pkt->nad, sub));
    }
    log_debug(ZONE, "Creating buffer of size %d", sz);
    mem = (char*)malloc(1024 * (sz+1));
    if(mem == NULL) return mod_PASS;
    log_debug(ZONE, "We got past the buffer allocation.");

    // JID
    os_object_put(o, "other_jid", other, os_type_STRING);

    // Body
    mem[0]=0;
    if ( (body > 0) && (NAD_CDATA_L(pkt->nad, body) > 0) ) {
        strncpy(mem, NAD_CDATA(pkt->nad, body), NAD_CDATA_L(pkt->nad, body));
        mem[NAD_CDATA_L(pkt->nad, body)] = 0;
    }
    os_object_put(o, "message", mem,                        os_type_STRING);

    // Subject
    mem[0]=0;
    if ( (sub  > 0) && (NAD_CDATA_L(pkt->nad, sub ) > 0) ) {
        strncpy(mem, NAD_CDATA(pkt->nad, sub),  NAD_CDATA_L(pkt->nad, sub));
        mem[NAD_CDATA_L(pkt->nad, sub)] = 0;
    }
    os_object_put(o, "subject", mem,                        os_type_STRING);

    // Type
    mem[0]=0;
    if ( (type  > 0) && (NAD_AVAL_L(pkt->nad, type ) > 0) ) {
        strncpy(mem, NAD_AVAL(pkt->nad, type), NAD_AVAL_L(pkt->nad, type));
        mem[NAD_AVAL_L(pkt->nad, type)] = 0;
    }
    os_object_put(o, "type",    mem,                        os_type_STRING);

    // To and from resources
    os_object_put(o, "my_resource",    own_jid->resource,   os_type_STRING);
    os_object_put(o, "other_resource", other_jid->resource, os_type_STRING);

    // Time and direction
    t=time(NULL);
    os_object_put(o, "direct",  &direct,                    os_type_INTEGER);
    os_object_put_time(o, "time",   &t);

    // Message ID
    if (direct == IN_D) {
        arch = nad_insert_elem(pkt->nad, 1, -1, "archived", "");
        nad_set_attr(pkt->nad,arch,-1,"by",jid_user(own_jid), strlen(jid_user(own_jid)));
    }
    set_mid(&(pkt->sm->st), &o, pkt->nad, arch);

    // Save itself
    storage_put(pkt->sm->st, tbl_name, owner, os);

    // Cleanup
    os_object_free(o);
    os_free(os);
    free(mem);
    log_debug(ZONE, "Saved.");

    return mod_PASS;
}