コード例 #1
0
ファイル: stanza.c プロジェクト: apophys/libstrophe
/** Release a stanza object and all of its children.
 *  This function releases a stanza object and potentially all of its
 *  children, which may cause the object(s) to be freed.
 *
 *  @param stanza a Strophe stanza object
 *
 *  @return TRUE if the object was freed and FALSE otherwise
 *
 *  @ingroup Stanza
 */
int xmpp_stanza_release(xmpp_stanza_t * const stanza)
{
    int released = 0;
    xmpp_stanza_t *child, *tchild;

    /* release stanza */
    if (stanza->ref > 1)
        stanza->ref--;
    else {
        /* release all children */
        child = stanza->children;
        while (child) {
            tchild = child;
            child = child->next;
            xmpp_stanza_release(tchild);
        }

        if (stanza->attributes) hash_release(stanza->attributes);
        if (stanza->data) xmpp_free(stanza->ctx, stanza->data);
        xmpp_free(stanza->ctx, stanza);
        released = 1;
    }

    return released;
}
コード例 #2
0
ファイル: handler.c プロジェクト: 1Project/SafeBoardMessenger
/** Delete a stanza handler.
 *
 *  @param conn a Strophe connection object
 *  @param handler a function pointer to a stanza handler
 *
 *  @ingroup Handlers
 */
void xmpp_handler_delete(xmpp_conn_t * const conn,
			 xmpp_handler handler)
{
    xmpp_handlist_t *prev, *item;

    if (!conn->handlers) return;

    prev = NULL;
    item = conn->handlers;
    while (item) {
	if (item->handler == (void *)handler)
	    break;
	
	prev = item;
	item = item->next;
    }

    if (item) {
	if (prev)
	    prev->next = item->next;
	else
	    conn->handlers = item->next;

	if (item->ns) xmpp_free(conn->ctx, item->ns);
	if (item->name) xmpp_free(conn->ctx, item->name);
	if (item->type) xmpp_free(conn->ctx, item->type);
	xmpp_free(conn->ctx, item);
    }
}
コード例 #3
0
ファイル: hash.c プロジェクト: pasis/libmesode
/** delete a key from a hash table */
int hash_drop(hash_t *table, const char *key)
{
   xmpp_ctx_t *ctx = table->ctx;
   hashentry_t *entry, *prev;
   int table_index = _hash_key(table, key);

   /* look up the hash entry */
   entry = table->entries[table_index];
   prev = NULL;
   while (entry != NULL) {
	/* traverse the linked list looking for the key */
	if (!strcmp(key, entry->key)) {
	  /* match, remove the entry */
	  xmpp_free(ctx, entry->key);
	  if (table->free) table->free(ctx, entry->value);
	  if (prev == NULL) {
	    table->entries[table_index] = entry->next;
	  } else {
	    prev->next = entry->next;
	  }
	  xmpp_free(ctx, entry);
	  table->num_keys--;
	  return 0;
	}
	prev = entry;
	entry = entry->next;
   }
   /* no match */
   return -1;
}
コード例 #4
0
ファイル: handler.c プロジェクト: 1Project/SafeBoardMessenger
/** Delete an id based stanza handler.
 *
 *  @param conn a Strophe connection object
 *  @param handler a function pointer to a stanza handler
 *  @param id a string containing the id the handler is for
 *
 *  @ingroup Handlers
 */
void xmpp_id_handler_delete(xmpp_conn_t * const conn,
			    xmpp_handler handler,
			    const char * const id)
{
    xmpp_handlist_t *item, *prev;

    prev = NULL;
    item = (xmpp_handlist_t *)hash_get(conn->id_handlers, id);
    if (!item) return;

    while (item) {
	if (item->handler == (void *)handler)
	    break;

	prev = item;
	item = item->next;
    }

    if (item) {
	if (prev)
	    prev->next = item->next;
	else {
	    hash_drop(conn->id_handlers, id);
	    hash_add(conn->id_handlers, id, item->next);
	}
	xmpp_free(conn->ctx, item->id);
	xmpp_free(conn->ctx, item);
    }
}
コード例 #5
0
void tls_free(tls_t *tls)
{
    if (tls->recvbuffer) {
	xmpp_free(tls->ctx, tls->recvbuffer);
    }

    if (tls->readybuffer) {
	xmpp_free(tls->ctx, tls->readybuffer);
    }

    if (tls->sendbuffer) {
	xmpp_free(tls->ctx, tls->sendbuffer);
    }

    if (tls->init) {
	tls->sft->FreeCredentialsHandle(&(tls->hcred));
    }

    tls->sft = NULL;

    if (tls->hsec32) {
	FreeLibrary(tls->hsec32);
	tls->hsec32 = NULL;
    }

    xmpp_free(tls->ctx, tls);
    return;
}
コード例 #6
0
ファイル: zksignal.cpp プロジェクト: FihlaTV/conference
static int zkmuc_group_msg_handler(xmpp_ua_t *ua, xmpp_stanza_t *stanza, void *userdata)
{
	char *type = xmpp_stanza_get_type(stanza);
	if (type && !strcmp(type, "groupchat"))
	{
		zkmuc_ctx_t *ctx = (zkmuc_ctx_t *)userdata;
	//	char *from = xmpp_stanza_get_attribute(stanza, "from");
		xmpp_stanza_t *stanza_body = xmpp_stanza_get_child_by_name(stanza, "zonekey");
		if (!stanza_body)
		{
			return 0;
		}
		xmpp_stanza_t *stanza_jid = xmpp_stanza_get_child_by_name(stanza_body, "jid");
		char *jid_value = xmpp_stanza_get_text(stanza_jid);
		char *text = xmpp_stanza_get_text(stanza_body);
		if (text && ctx->room_cbs.on_broadcast_message)
		{
			ctx->room_cbs.on_broadcast_message(ctx, /*from*/jid_value, text, ctx->room_data);
		}
		xmpp_free(_xmpp_ctx, jid_value);
		xmpp_free(_xmpp_ctx, text);
		return 1;
	}
	
	return 0;
}
コード例 #7
0
ファイル: sasl.c プロジェクト: boothj5/libmesode
/* split key, value pairs into a hash */
static hash_t *_parse_digest_challenge(xmpp_ctx_t *ctx, const char *msg)
{
    hash_t *result;
    unsigned char *text;
    char *key, *value;
    unsigned char *s, *t;

    text = (unsigned char *)xmpp_base64_decode_str(ctx, msg, strlen(msg));
    if (text == NULL) {
	xmpp_error(ctx, "SASL", "couldn't Base64 decode challenge!");
	return NULL;
    }

    result = hash_new(ctx, 10, xmpp_free);
    if (result != NULL) {
	s = text;
	while (*s != '\0') {
	    /* skip any leading commas and spaces */
	    while ((*s == ',') || (*s == ' ')) s++;
	    /* accumulate a key ending at '=' */
	    t = s;
	    while ((*t != '=') && (*t != '\0')) t++;
	    if (*t == '\0') break; /* bad string */
	    key = _make_string(ctx, (char *)s, (t-s));
	    if (key == NULL) break;
            /* advance our start pointer past the key */
	    s = t + 1;
	    t = s;
	    /* if we see quotes, grab the string in between */
	    if ((*s == '\'') || (*s == '"')) {
		t++;
		while ((*t != *s) && (*t != '\0'))
		    t++;
		value = _make_string(ctx, (char *)s+1, (t-s-1));
		if (*t == *s) {
		    s = t + 1;
		} else {
		    s = t;
		}
	    /* otherwise, accumulate a value ending in ',' or '\0' */
	    } else {
		while ((*t != ',') && (*t != '\0')) t++;
		value = _make_string(ctx, (char *)s, (t-s));
		s = t;
	    }
	    if (value == NULL) {
		xmpp_free(ctx, key);
		break;
	    }
	    /* TODO: check for collisions per spec */
	    hash_add(result, key, value);
	    /* hash table now owns the value, free the key */
	    xmpp_free(ctx, key);
	}
    }
    xmpp_free(ctx, text);

    return result;
}
コード例 #8
0
ファイル: parser_expat.c プロジェクト: EmuxEvans/libstrophe
static void _start_element(void *userdata,
                           const XML_Char *nsname,
                           const XML_Char **attrs)
{
    parser_t *parser = (parser_t *)userdata;
    xmpp_stanza_t *child;
    char *ns, *name;

    ns = _xml_namespace(parser->ctx, nsname);
    name = _xml_name(parser->ctx, nsname);

    if (parser->depth == 0) {
        /* notify the owner */
        if (parser->startcb)
            parser->startcb((char *)name, (char **)attrs, 
                            parser->userdata);
    } else {
	/* build stanzas at depth 1 */
	if (!parser->stanza && parser->depth != 1) {
	    /* something terrible happened */
	    /* FIXME: shutdown disconnect */
	    xmpp_error(parser->ctx, "parser", "oops, where did our stanza go?");
	} else if (!parser->stanza) {
	    /* starting a new toplevel stanza */
	    parser->stanza = xmpp_stanza_new(parser->ctx);
	    if (!parser->stanza) {
		/* FIXME: can't allocate, disconnect */
	    }
	    xmpp_stanza_set_name(parser->stanza, name);
	    _set_attributes(parser->stanza, attrs);
	    if (ns)
		xmpp_stanza_set_ns(parser->stanza, ns);
	} else {
	    /* starting a child of parser->stanza */
	    child = xmpp_stanza_new(parser->ctx);
	    if (!child) {
		/* FIXME: can't allocate, disconnect */
	    }
	    xmpp_stanza_set_name(child, name);
	    _set_attributes(child, attrs);
	    if (ns)
		xmpp_stanza_set_ns(child, ns);

	    /* add child to parent */
	    xmpp_stanza_add_child(parser->stanza, child);
	    
	    /* the child is owned by the toplevel stanza now */
	    xmpp_stanza_release(child);

	    /* make child the current stanza */
	    parser->stanza = child;
	}
    }

    if (ns) xmpp_free(parser->ctx, ns);
    if (name) xmpp_free(parser->ctx, name);

    parser->depth++;
}
コード例 #9
0
ファイル: message.c プロジェクト: 0xPoly/profanity
static gboolean
_handle_carbons(xmpp_stanza_t *const stanza)
{
    xmpp_stanza_t *carbons = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_CARBONS);
    if (!carbons) {
        return FALSE;
    }

    char *name = xmpp_stanza_get_name(carbons);
    if ((g_strcmp0(name, "received") == 0) || (g_strcmp0(name, "sent")) == 0) {
        xmpp_stanza_t *forwarded = xmpp_stanza_get_child_by_ns(carbons, STANZA_NS_FORWARD);
        xmpp_stanza_t *message = xmpp_stanza_get_child_by_name(forwarded, STANZA_NAME_MESSAGE);

        xmpp_ctx_t *ctx = connection_get_ctx();

        gchar *to = xmpp_stanza_get_attribute(message, STANZA_ATTR_TO);
        gchar *from = xmpp_stanza_get_attribute(message, STANZA_ATTR_FROM);

        // happens when receive a carbon of a self sent message
        if (!to) to = from;

        Jid *jid_from = jid_create(from);
        Jid *jid_to = jid_create(to);
        Jid *my_jid = jid_create(jabber_get_fulljid());

        // check for and deal with message
        xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(message, STANZA_NAME_BODY);
        if (body) {
            char *message_txt = xmpp_stanza_get_text(body);
            if (message_txt) {
                // check for pgp encrypted message
                char *enc_message = NULL;
                xmpp_stanza_t *x = xmpp_stanza_get_child_by_ns(message, STANZA_NS_ENCRYPTED);
                if (x) {
                    enc_message = xmpp_stanza_get_text(x);
                }

                // if we are the recipient, treat as standard incoming message
                if(g_strcmp0(my_jid->barejid, jid_to->barejid) == 0){
                    sv_ev_incoming_carbon(jid_from->barejid, jid_from->resourcepart, message_txt, enc_message);

                // else treat as a sent message
                } else {
                    sv_ev_outgoing_carbon(jid_to->barejid, message_txt, enc_message);
                }
                xmpp_free(ctx, message_txt);
                xmpp_free(ctx, enc_message);
            }
        }

        jid_destroy(jid_from);
        jid_destroy(jid_to);
        jid_destroy(my_jid);

        return TRUE;
    }

    return FALSE;
}
コード例 #10
0
ファイル: user.c プロジェクト: hnakagawa/libantis
void xmpp_user_release(xmpp_user_t * const user)
{
	xmpp_ctx_t *ctx = user->conn->ctx;

	if (user->name) xmpp_free(ctx, user->name);
	if (user->jid) xmpp_free(ctx, user->jid);
	xmpp_free(ctx, user);
}
コード例 #11
0
int XMPP_IBB_Data_Process(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata)
{

    char* intext;
    unsigned char *result;
    xmpp_ctx_t *ctx = (xmpp_ctx_t*)userdata;
    char *szSid, szSeq;	
//    xmpp_ibb_session_t* ibb_ssn;
    

    szSid = \
	xmpp_stanza_get_attribute(xmpp_stanza_get_child_by_name(stanza, "data"), "sid");
 
    szSeq = \ 
	xmpp_stanza_get_attribute(xmpp_stanza_get_child_by_name(stanza, "data"), "seq");
 
    intext = xmpp_stanza_get_text(xmpp_stanza_get_child_by_name(stanza, "data"));

    printf("[Sid=%s][Seq=%s][Raw Data=%s]\n", szSid, szSeq, intext);
    result = base64_decode(ctx, intext, strlen(intext));
    printf("Decode result=%s\n", result);

    gRecv = malloc(strlen(result)+1);
    strcpy(gRecv, result);
    
    if(gStanza == NULL)
        gStanza = xmpp_stanza_copy(stanza);

#if 0  //data queue function has not been verified.  
	            
    ibb_ssn = XMPP_Get_IBB_Session_Handle(szSid);

    if( ibb_ssn == NULL)		
    {
        printf("Opened Session ID not found\n"); 
	goto error;
    }

    xmpp_ibb_data_t* ibb_data_new = malloc(sizeof(xmpp_ibb_data_t));
    ibb_data_new->seq_num = malloc(strlen(szSeq)+1);
    ibb_data_new->recv_data =  malloc(strlen(result)+1);
    
    strcpy(ibb_data_new->seq_num, szSeq);   
    strcpy(ibb_data_new->recv_data, result);
   
    XMPP_IBB_Add_Session_Data_Queue(ibb_ssn, ibb_data_new);
#endif

error:
    xmpp_free(ctx, szSid);
    xmpp_free(ctx, szSeq);
    xmpp_free(ctx, intext);
    xmpp_free(ctx, result);

    return 1;
}
コード例 #12
0
ファイル: auth.c プロジェクト: mcanthony/libstrophe
/* handle the challenge phase of digest auth */
static int _handle_digestmd5_challenge(xmpp_conn_t * const conn,
			      xmpp_stanza_t * const stanza,
			      void * const userdata)
{
    char *text;
    char *response;
    xmpp_stanza_t *auth, *authdata;
    char *name;

    name = xmpp_stanza_get_name(stanza);
    xmpp_debug(conn->ctx, "xmpp",\
	"handle digest-md5 (challenge) called for %s", name);

    if (strcmp(name, "challenge") == 0) {
	text = xmpp_stanza_get_text(stanza);
	response = sasl_digest_md5(conn->ctx, text, conn->jid, conn->pass);
	if (!response) {
	    disconnect_mem_error(conn);
	    return 0;
	}
	xmpp_free(conn->ctx, text);

	auth = xmpp_stanza_new(conn->ctx);
	if (!auth) {
	    disconnect_mem_error(conn);
	    return 0;
	}
	xmpp_stanza_set_name(auth, "response");
	xmpp_stanza_set_ns(auth, XMPP_NS_SASL);

	authdata = xmpp_stanza_new(conn->ctx);
	if (!authdata) {
	    disconnect_mem_error(conn);
	    return 0;
	}

	xmpp_stanza_set_text(authdata, response);
	xmpp_free(conn->ctx, response);

	xmpp_stanza_add_child(auth, authdata);
	xmpp_stanza_release(authdata);

	handler_add(conn, _handle_digestmd5_rspauth,
		    XMPP_NS_SASL, NULL, NULL, NULL);

	xmpp_send(conn, auth);
	xmpp_stanza_release(auth);

    } else {
	return _handle_sasl_result(conn, stanza, "DIGEST-MD5");
    }

    /* remove ourselves */
    return 0;
}
コード例 #13
0
ファイル: conn.c プロジェクト: apophys/libstrophe
static void _conn_attributes_destroy(xmpp_conn_t *conn, char **attributes,
                                     size_t attributes_len)
{
    size_t i;

    if (attributes) {
        for (i = 0; i < attributes_len; ++i)
            xmpp_free(conn->ctx, attributes[i]);
        xmpp_free(conn->ctx, attributes);
    }
}
コード例 #14
0
ファイル: message.c プロジェクト: KThand1/profanity
static int
_muc_user_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata)
{
    xmpp_ctx_t *ctx = connection_get_ctx();
    xmpp_stanza_t *xns_muc_user = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_MUC_USER);
    char *room = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);

    if (!room) {
        log_warning("Message received with no from attribute, ignoring");
        return 1;
    }

    // XEP-0045
    xmpp_stanza_t *invite = xmpp_stanza_get_child_by_name(xns_muc_user, STANZA_NAME_INVITE);
    if (!invite) {
        return 1;
    }

    char *invitor_jid = xmpp_stanza_get_attribute(invite, STANZA_ATTR_FROM);
    if (!invitor_jid) {
        log_warning("Chat room invite received with no from attribute");
        return 1;
    }

    Jid *jidp = jid_create(invitor_jid);
    if (!jidp) {
        return 1;
    }
    char *invitor = jidp->barejid;

    char *reason = NULL;
    xmpp_stanza_t *reason_st = xmpp_stanza_get_child_by_name(invite, STANZA_NAME_REASON);
    if (reason_st) {
        reason = xmpp_stanza_get_text(reason_st);
    }

    char *password = NULL;
    xmpp_stanza_t *password_st = xmpp_stanza_get_child_by_name(xns_muc_user, STANZA_NAME_PASSWORD);
    if (password_st) {
        password = xmpp_stanza_get_text(password_st);
    }

    sv_ev_room_invite(INVITE_MEDIATED, invitor, room, reason, password);
    jid_destroy(jidp);
    if (reason) {
        xmpp_free(ctx, reason);
    }
    if (password) {
        xmpp_free(ctx, password);
    }

    return 1;
}
コード例 #15
0
ファイル: test_ctx.c プロジェクト: boothj5/libmesode
int main(int argc, char **argv)
{
    xmpp_ctx_t *ctx;
    xmpp_mem_t mymem;
    xmpp_log_t mylog;
    char my_str[5] = "asdf";
    void *testptr1, *testptr2;

    ctx = xmpp_ctx_new(NULL, NULL);
    if (ctx == NULL) return 1;

    /* destroy context */
    xmpp_ctx_free(ctx);

    /* setup our memory handler */
    mymem.alloc = my_alloc;
    mymem.free = my_free;
    mymem.realloc = my_realloc;

    /* setup our logger */
    mylog.handler = my_logger;
    mylog.userdata = my_str;

    ctx = xmpp_ctx_new(&mymem, &mylog);
    xmpp_debug(ctx, "test", "hello");

    testptr1 = xmpp_alloc(ctx, 1024);
    if (testptr1 == NULL) {
	xmpp_ctx_free(ctx);
	return 1;
    }

    testptr2 = xmpp_realloc(ctx, testptr1, 2048);
    if (testptr2 == NULL) {
	xmpp_free(ctx, testptr1);
	xmpp_ctx_free(ctx);
	return 1;
    }

    xmpp_free(ctx, testptr2);

    xmpp_ctx_free(ctx);

    /* check for test failure */
    if (!(log_called && mem_alloc_called && mem_realloc_called && 
	  mem_free_called))
	return 1;
    if (mem_alloc_called != mem_free_called)
        return 1;
    
    return 0;
}
コード例 #16
0
static void _free_cbattrs(parser_t *parser, char **attrs)
{
    int i;

    if (!attrs)
        return;

    for (i = 0; attrs[i]; i += 2) {
        if (attrs[i]) xmpp_free(parser->ctx, attrs[i]);
        if (attrs[i+1]) xmpp_free(parser->ctx, attrs[i+1]);
    }

    xmpp_free(parser->ctx, attrs);
}
コード例 #17
0
ファイル: test_base64.c プロジェクト: Aversiste/libstrophe
int main(int argc, char *argv[])
{
    xmpp_ctx_t *ctx;
    unsigned char *udec;
    char *dec;
    char *enc;
    size_t len;
    int i;

    printf("BASE64 tests.\n");

    ctx = xmpp_ctx_new(NULL, NULL);
    if (ctx == NULL) {
        fprintf(stderr, "failed to create context\n");
        return 1;
    }

    for (i = 0; i < ARRAY_SIZE(tests); ++i) {
        printf("Test #%d: ", (int)i + 1);
        enc = xmpp_base64_encode(ctx, (unsigned char *)tests[i].raw,
                                 strlen(tests[i].raw));
        assert(enc != NULL);
        COMPARE(tests[i].base64, enc);
        xmpp_free(ctx, enc);

        dec = xmpp_base64_decode_str(ctx, tests[i].base64,
                                     strlen(tests[i].base64));
        assert(dec != NULL);
        COMPARE_BUF(tests[i].raw, strlen(tests[i].raw), dec, strlen(dec));
        xmpp_free(ctx, dec);
        printf("ok\n");
    }

    printf("Test with binary data: ");
    enc = xmpp_base64_encode(ctx, bin_data, sizeof(bin_data));
    assert(enc != NULL);
    xmpp_base64_decode_bin(ctx, enc, strlen(enc), &udec, &len);
    assert(udec != NULL);
    assert(len != 0);
    assert(len == sizeof(bin_data));
    COMPARE_BUF(bin_data, sizeof(bin_data), udec, len);
    xmpp_free(ctx, udec);
    xmpp_free(ctx, enc);
    printf("ok\n");

    xmpp_ctx_free(ctx);

    return 0;
}
コード例 #18
0
ファイル: conn.c プロジェクト: CAOJINGYOU/libstrophe
/** Send raw bytes to the XMPP server.
 *  This function is a convenience function to send raw bytes to the 
 *  XMPP server.  It is usedly primarly by xmpp_send_raw_string.  This 
 *  function should be used with care as it does not validate the bytes and
 *  invalid data may result in stream termination by the XMPP server.
 *
 *  @param conn a Strophe connection object
 *  @param data a buffer of raw bytes
 *  @param len the length of the data in the buffer
 */
void xmpp_send_raw(xmpp_conn_t * const conn,
		   const char * const data, const size_t len)
{
    xmpp_send_queue_t *item;

    if (conn->state != XMPP_STATE_CONNECTED) return;

    /* create send queue item for queue */
    item = xmpp_alloc(conn->ctx, sizeof(xmpp_send_queue_t));
    if (!item) return;

    item->data = xmpp_alloc(conn->ctx, len);
    if (!item->data) {
	xmpp_free(conn->ctx, item);
	return;
    }
    memcpy(item->data, data, len);
    item->len = len;
    item->next = NULL;
    item->written = 0;

    /* add item to the send queue */
    if (!conn->send_queue_tail) {
	/* first item, set head and tail */
	conn->send_queue_head = item;
	conn->send_queue_tail = item;
    } else {
	/* add to the tail */
	conn->send_queue_tail->next = item;
	conn->send_queue_tail = item;
    }
    conn->send_queue_len++;
}
コード例 #19
0
ファイル: hash.c プロジェクト: pasis/libmesode
/** add a key, value pair to a hash table.
 *  each key can appear only once; the value of any
 *  identical key will be replaced
 */
int hash_add(hash_t *table, const char * const key, void *data)
{
   xmpp_ctx_t *ctx = table->ctx;
   hashentry_t *entry = NULL;
   int table_index = _hash_key(table, key);

   /* drop existing entry, if any */
   hash_drop(table, key);

   /* allocate and fill a new entry */
   entry = xmpp_alloc(ctx, sizeof(hashentry_t));
   if (!entry) return -1;
   entry->key = xmpp_strdup(ctx, key);
   if (!entry->key) {
       xmpp_free(ctx, entry);
       return -1;
   }
   entry->value = data;
   /* insert ourselves in the linked list */
   /* TODO: this leaks duplicate keys */
   entry->next = table->entries[table_index];
   table->entries[table_index] = entry;
   table->num_keys++;

   return 0;
}
コード例 #20
0
ファイル: connection.c プロジェクト: klement/profanity
void
connection_free_uuid(char *uuid)
{
    if (uuid) {
        xmpp_free(conn.xmpp_ctx, uuid);
    }
}
コード例 #21
0
ファイル: stanza.c プロジェクト: apophys/libstrophe
/** Set an attribute for a stanza object.
 *  
 *  @param stanza a Strophe stanza object
 *  @param key a string with the attribute name
 *  @param value a string with the attribute value
 *
 *  @return XMPP_EOK (0) on success or a number less than 0 on failure
 *
 *  @ingroup Stanza
 */
int xmpp_stanza_set_attribute(xmpp_stanza_t * const stanza,
                              const char * const key,
                              const char * const value)
{
    char *val;
    int rc;

    if (stanza->type != XMPP_STANZA_TAG) return XMPP_EINVOP;

    if (!stanza->attributes) {
        stanza->attributes = hash_new(stanza->ctx, 8, xmpp_free);
        if (!stanza->attributes) return XMPP_EMEM;
    }

    val = xmpp_strdup(stanza->ctx, value);
    if (!val) {
        hash_release(stanza->attributes);
        return XMPP_EMEM;
    }

    rc = hash_add(stanza->attributes, key, val);
    if (rc < 0) {
        xmpp_free(stanza->ctx, val);
        return XMPP_EMEM;
    }

    return XMPP_EOK;
}
コード例 #22
0
ファイル: conn.c プロジェクト: 1Project/SafeBoardMessenger
static void _log_open_tag(xmpp_conn_t *conn, char **attrs)
{
    char buf[4096];
    size_t pos;
    int len;
    int i;
    char *attr;

    if (!attrs) return;

    pos = 0;
    len = xmpp_snprintf(buf, 4096, "<stream:stream");
    if (len < 0) return;

    pos += len;
    for (i = 0; attrs[i]; i += 2) {
        attr = parser_attr_name(conn->ctx, attrs[i]);
        len = xmpp_snprintf(&buf[pos], 4096 - pos, " %s='%s'",
                            attr, attrs[i+1]);
        xmpp_free(conn->ctx, attr);
        if (len < 0) return;
        pos += len;
    }

    len = xmpp_snprintf(&buf[pos], 4096 - pos, ">");
    if (len < 0) return;

    xmpp_debug(conn->ctx, "xmpp", "RECV: %s", buf);
}
コード例 #23
0
int message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata)
{
    xmpp_ctx_t *ctx = (xmpp_ctx_t*)userdata;
    xmpp_stanza_t *reply;
    char *intext, *replytext;

    if (!xmpp_stanza_get_child_by_name(stanza, "body"))
        return 1;
    if (xmpp_stanza_get_type(stanza) != NULL && !strcmp(xmpp_stanza_get_type(stanza), "error"))
        return 1;

    intext = xmpp_stanza_get_text(xmpp_stanza_get_child_by_name(stanza, "body"));

    printf("Incoming message from %s: %s\n", xmpp_stanza_get_from(stanza), intext);

    reply = xmpp_stanza_reply(stanza);
    if (xmpp_stanza_get_type(reply) == NULL)
        xmpp_stanza_set_type(reply, "chat");

    replytext = (char *) malloc(strlen(" to you too!") + strlen(intext) + 1);
    strcpy(replytext, intext);
    strcat(replytext, " to you too!");
    xmpp_free(ctx, intext);
    xmpp_message_set_body(reply, replytext);

    xmpp_send(conn, reply);
    xmpp_stanza_release(reply);
    free(replytext);
    return 1;
}
コード例 #24
0
ファイル: ctx.c プロジェクト: 1Project/SafeBoardMessenger
/** Create and initialize a Strophe context object.
 *  If mem is NULL, a default allocation setup will be used which
 *  wraps malloc(), free(), and realloc() from the standard library.
 *  If log is NULL, a default logger will be used which does no
 *  logging.  Basic filtered logging to stderr can be done with the
 *  xmpp_get_default_logger() convenience function.
 *
 *  @param mem a pointer to an xmpp_mem_t structure or NULL
 *  @param log a pointer to an xmpp_log_t structure or NULL
 *
 *  @return the allocated Strophe context object or NULL on an error
 *
 *  @ingroup Context
 */
xmpp_ctx_t *xmpp_ctx_new(const xmpp_mem_t * const mem, 
			 const xmpp_log_t * const log)
{
    xmpp_ctx_t *ctx = NULL;

    if (mem == NULL)
	ctx = xmpp_default_mem.alloc(sizeof(xmpp_ctx_t), NULL);
    else
	ctx = mem->alloc(sizeof(xmpp_ctx_t), mem->userdata);

    if (ctx != NULL) {
	if (mem != NULL) 
	    ctx->mem = mem;
	else 
	    ctx->mem = &xmpp_default_mem;

	if (log == NULL)
	    ctx->log = &xmpp_default_log;
	else
	    ctx->log = log;

	ctx->connlist = NULL;
	ctx->loop_status = XMPP_LOOP_NOTSTARTED;
	ctx->rand = xmpp_rand_new(ctx);
	if (ctx->rand == NULL) {
	    xmpp_free(ctx, ctx);
	    ctx = NULL;
	}
    }

    return ctx;
}
コード例 #25
0
ファイル: test_jid.c プロジェクト: pasis/libmesode
int test_jid_new(xmpp_ctx_t *ctx)
{
    char *jid;

    jid = xmpp_jid_new(ctx, "node", "domain", "resource");
    printf("new jid: '%s'\n", jid);
    if (strcmp(jid, "node@domain/resource")) return 1;
    xmpp_free(ctx, jid);

    jid = xmpp_jid_new(ctx, "foo", "bar.com", NULL);
    printf("new jid: '%s'\n", jid);
    if (strcmp(jid, "*****@*****.**")) return 1;
    xmpp_free(ctx, jid);

    return 0;
}
コード例 #26
0
ファイル: auth.c プロジェクト: mcanthony/libstrophe
/* Check if the received stanza is <handshake/> and set auth to true
 * and fire connection handler.
 */
int _handle_component_hs_response(xmpp_conn_t * const conn,
            xmpp_stanza_t * const stanza,
            void * const userdata)
{
    char *name;

    xmpp_timed_handler_delete(conn, _handle_missing_handshake);

    name = xmpp_stanza_get_name(stanza);
    if (strcmp(name, "handshake") != 0) {
        char *msg;
        size_t msg_size;
        xmpp_stanza_to_text(stanza, &msg, &msg_size);
        if (msg) {
            xmpp_debug(conn->ctx, "auth", "Handshake failed: %s", msg);
            xmpp_free(conn->ctx, msg);
        }
        xmpp_disconnect(conn);
        return XMPP_EINT;
    } else {
        conn->authenticated = 1;
        conn->conn_handler(conn, XMPP_CONN_CONNECT, 0, NULL, conn->userdata);
    }

    /* We don't need this handler anymore, return 0 so it can be deleted
     * from the list of handlers.
     */
    return 0;
}
コード例 #27
0
ファイル: conn.c プロジェクト: CAOJINGYOU/libstrophe
static void _handle_stream_start(char *name, char **attrs, 
                                 void * const userdata)
{
    xmpp_conn_t *conn = (xmpp_conn_t *)userdata;
    char *id;

    if (strcmp(name, "stream:stream") != 0) {
        printf("name = %s\n", name);
        xmpp_error(conn->ctx, "conn", "Server did not open valid stream.");
        conn_disconnect(conn);
    } else {
        _log_open_tag(conn, attrs);
        
        if (conn->stream_id) xmpp_free(conn->ctx, conn->stream_id);

        id = _get_stream_attribute(attrs, "id");
        if (id)
            conn->stream_id = xmpp_strdup(conn->ctx, id);

        if (!conn->stream_id) {
            xmpp_error(conn->ctx, "conn", "Memory allocation failed.");
            conn_disconnect(conn);
        }
    }
    
    /* call stream open handler */
    conn->open_handler(conn);
}
コード例 #28
0
ファイル: tls_openssl.c プロジェクト: anoek/libstrophe
void tls_free(tls_t *tls)
{
    SSL_free(tls->ssl);
    SSL_CTX_free(tls->ssl_ctx);
    xmpp_free(tls->ctx, tls);
    return;
}
コード例 #29
0
/* free a parser */
void parser_free(parser_t *parser)
{
    if (parser->expat)
        XML_ParserFree(parser->expat);

    xmpp_free(parser->ctx, parser);
}
コード例 #30
0
ファイル: message.c プロジェクト: KThand1/profanity
static int
_captcha_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata)
{
    xmpp_ctx_t *ctx = connection_get_ctx();
    char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);

    if (!from) {
        log_warning("Message received with no from attribute, ignoring");
        return 1;
    }

    // XEP-0158
    xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_BODY);
    if (!body) {
        return 1;
    }

    char *message = xmpp_stanza_get_text(body);
    if (!message) {
        return 1;
    }

    sv_ev_room_broadcast(from, message);
    xmpp_free(ctx, message);

    return 1;
}