Ejemplo n.º 1
0
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;
}
Ejemplo n.º 2
0
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;
}
Ejemplo n.º 3
0
static int
_version_result_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
    void * const userdata)
{
    char *id = xmpp_stanza_get_id(stanza);

    if (id != NULL) {
        log_debug("IQ version result handler fired, id: %s.", id);
    } else {
        log_debug("IQ version result handler fired.");
    }

    const char *jid = xmpp_stanza_get_attribute(stanza, "from");

    xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY);
    if (query == NULL) {
        return 1;
    }

    char *ns = xmpp_stanza_get_ns(query);
    if (g_strcmp0(ns, STANZA_NS_VERSION) != 0) {
        return 1;
    }

    char *name_str = NULL;
    char *version_str = NULL;
    char *os_str = NULL;
    xmpp_stanza_t *name = xmpp_stanza_get_child_by_name(query, "name");
    xmpp_stanza_t *version = xmpp_stanza_get_child_by_name(query, "version");
    xmpp_stanza_t *os = xmpp_stanza_get_child_by_name(query, "os");

    if (name != NULL) {
        name_str = xmpp_stanza_get_text(name);
    }
    if (version != NULL) {
        version_str = xmpp_stanza_get_text(version);
    }
    if (os != NULL) {
        os_str = xmpp_stanza_get_text(os);
    }

    PContact contact;
    Jid *jidp = jid_create(jid);
    if (muc_room_is_active(jidp->barejid)) {
        contact = muc_get_participant(jidp->barejid, jidp->resourcepart);
    } else {
        contact = roster_get_contact(jidp->barejid);
    }

    Resource *resource = p_contact_get_resource(contact, jidp->resourcepart);
    const char *presence = string_from_resource_presence(resource->presence);
    handle_software_version_result(jid, presence, name_str, version_str, os_str);

    jid_destroy(jidp);

    return 1;
}
Ejemplo n.º 4
0
/**
 * Creates the parameter structure for the Read callback
 * @param[in] params This stanza is the params element in the XML-RPC format
 * @return The structure or null if an error were found.
 */
static sdvp_ReadParam_t* _CreateReadParam (xmpp_stanza_t * params) {
    const int expectedParams = 1;
    sdvp_ReadParam_t* rv = NULL;
    int i = 0;
    bool elementNotFund = false;
    xmpp_stanza_t *structure = NULL, *member = NULL, *value = NULL,
            *type = NULL, *name = NULL;
    char *text, *textname;

    structure = _GetRpcStructureElementFromParams(params);
    if (structure == NULL ) {
        return NULL ;
    }
    rv = _InitiateReadParameters();
    for (member = xmpp_stanza_get_children(structure);
            member && !elementNotFund && (i < expectedParams); member =
                    xmpp_stanza_get_next(member)) {
        if (xmpp_stanza_get_name(member)
                && strcmp(xmpp_stanza_get_name(member), "member") == 0) {

            if (!(name = xmpp_stanza_get_child_by_name(member, "name"))) {
                _FreeReadParameters(rv);
                // TODO: Signal error back
                return NULL ;
            }
            name = xmpp_stanza_get_children(name);

			textname = xmpp_stanza_get_text(name);
            if (strcmp(textname, "VariableAccessSpecification") == 0) {
                if (!(value = xmpp_stanza_get_child_by_name(member, "value"))) {
                    _FreeReadParameters(rv);
                    // TODO: Signal error back
					free(textname);
                    return NULL ;
                }
                if ((type = xmpp_stanza_get_child_by_name(value, "string"))) {
                    if (xmpp_stanza_get_children(type)) {
                        text = xmpp_stanza_get_text(type);
                        rv->reference = text;
                    } else {
                        rv->reference = NULL;
                    }
                } else {
                    elementNotFund = true;
                }
            } else {
//                if (!type) {
//                    _FreeReadParameters(rv);
//                    return NULL ;
//                }
            }
			free(textname);
        }
    }
    return rv;
}
Ejemplo n.º 5
0
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;
}
Ejemplo n.º 6
0
static void files_read(xmpp_stanza_t *stanza) {
  int rc;

  rc = pthread_mutex_lock(&mutex);
  wsyserr(rc != 0, "pthread_mutex_lock");

  char *text = xmpp_stanza_get_text(stanza);
  if(text == NULL) {
    werr("xmpp_stanza_get_text returned NULL (%s, error %s)", xmpp_stanza_get_attribute(stanza, "path"), xmpp_stanza_get_attribute(stanza, "error"));
    read_data = strdup ("");
  }
  else
  {
    int dec_size = strlen(text) * 3 / 4 + 1;
    uint8_t *dec_text = (uint8_t *)calloc(dec_size, sizeof(uint8_t));
    rc = base64_decode(dec_text, text, dec_size);
    wfatal(rc < 0, "base64_decode");

    if (read_data != NULL) {
      free(read_data);
    }
    read_data = strdup((char *)dec_text);
    wsyserr(read_data == NULL, "strdup");

    free(text);
    free(dec_text);
  }

  signal_read = true;
  rc = pthread_cond_signal(&cond);
  wsyserr(rc != 0, "pthread_cond_signal");

  rc = pthread_mutex_unlock(&mutex);
  wsyserr(rc != 0, "pthread_mutex_unlock");
}
Ejemplo n.º 7
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;
}
Ejemplo n.º 8
0
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;
}
Ejemplo n.º 9
0
int message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata)
{
	xmpp_stanza_t *reply, *body, *text;
	char *intext;
	xmpp_ctx_t *ctx = (xmpp_ctx_t*)userdata;


	if (!xmpp_stanza_get_child_by_name(stanza, "body")) return 1;
	if (xmpp_stanza_get_attribute(stanza, "type") != NULL && !strcmp(xmpp_stanza_get_attribute(stanza, "type"), "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_attribute(stanza, "from"), intext);

	reply = xmpp_stanza_new(ctx);
	xmpp_stanza_set_name(reply, "message");
	xmpp_stanza_set_type(reply, xmpp_stanza_get_type(stanza) ? xmpp_stanza_get_type(stanza) : "chat");
	xmpp_stanza_set_attribute(reply, "to", xmpp_stanza_get_attribute(stanza, "from"));

	body = xmpp_stanza_new(ctx);
	xmpp_stanza_set_name(body, "body");

	char replytext[1024];

	scanf("%[^\n]", replytext);

	text = xmpp_stanza_new(ctx);
	xmpp_stanza_set_text(text, replytext);
	xmpp_stanza_add_child(body, text);
	xmpp_stanza_add_child(reply, body);

	xmpp_send(conn, reply);
	xmpp_stanza_release(reply);
	return 1;
}
Ejemplo n.º 10
0
int message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) 
{ 
  xmpp_stanza_t *reply, *body, *text; 
  char *intext, *replytext; 
  xmpp_ctx_t *ctx = (xmpp_ctx_t*)userdata; 

  if(!xmpp_stanza_get_child_by_name(stanza, "body")) 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_attribute(stanza, "from"), intext); 

  reply = xmpp_stanza_new(ctx); 
  xmpp_stanza_set_name(reply, "message"); 
  xmpp_stanza_set_type(reply, xmpp_stanza_get_type(stanza)?xmpp_stanza_get_type(stanza):"chat"); 
  xmpp_stanza_set_attribute(reply, "to", xmpp_stanza_get_attribute(stanza, "from")); 

  body = xmpp_stanza_new(ctx); 
  xmpp_stanza_set_name(body, "body"); 

  replytext = malloc(strlen(" to you too!") + strlen(intext) + 1); 
  strcpy(replytext, intext); 
  strcat(replytext, " to you too!"); 

  text = xmpp_stanza_new(ctx); 
  xmpp_stanza_set_text(text, replytext); 
  xmpp_stanza_add_child(body, text); 
  xmpp_stanza_add_child(reply, body); 

  xmpp_send(conn, reply); 
  xmpp_stanza_release(reply); 
  free(replytext); 
  return 1; 
} 
Ejemplo n.º 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;
}
Ejemplo n.º 12
0
/* 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;
}
Ejemplo n.º 13
0
int message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata)
{
    char *intext, *decoded_intext;
    if(!xmpp_stanza_get_child_by_name(stanza, "body")) return 1;
    if(!strcmp(xmpp_stanza_get_attribute(stanza, "type"), "error")) return 1;
    intext = xmpp_stanza_get_text(xmpp_stanza_get_child_by_name(stanza, "body"));

    decoded_intext = from_base64(intext, strlen(intext));
    fwrite((const void*)decoded_intext, 1, strlen(intext), stdout);
    free(decoded_intext);
    return 1;
}
Ejemplo n.º 14
0
/** Get text from <body/> child element.
 *  This function returns new allocated string. The caller is responsible
 *  for freeing this string with xmpp_free().
 *
 *  @param msg well formed <message/> stanza
 *
 *  @return allocated string or NULL on failure (no <body/> element or
 *      memory allocation error)
 *
 *  @ingroup Stanza
 */
char *xmpp_message_get_body(xmpp_stanza_t *msg)
{
    xmpp_stanza_t *body;
    const char *name;
    char *text = NULL;

    name = xmpp_stanza_get_name(msg);
    body = xmpp_stanza_get_child_by_name(msg, "body");
    if (name && strcmp(name, "message") == 0 && body) {
        text = xmpp_stanza_get_text(body);
    }
    return text;
}
Ejemplo n.º 15
0
static int _zkmuc_on_rcv_invite(xmpp_ua_t *ua, xmpp_stanza_t *stanza, void *userdata)
{
	char *ns = xmpp_stanza_get_ns(stanza);
	if (ns != NULL && !strcmp(ns, XMPP_NS_CONFERECE))
	{
		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, "body");
		if (stanza_body)
		{
			char *room_id = xmpp_stanza_get_text(stanza_body);
			ctx->invite_cb(ctx, from, room_id, ctx->invite_data);
		}
		return 1;
	}
	return 0;
}
Ejemplo n.º 16
0
static char*
_get_property(xmpp_stanza_t *const stanza, const char *const property)
{
    char *result = NULL;
    xmpp_ctx_t *ctx = connection_get_ctx();

    xmpp_stanza_t *child = xmpp_stanza_get_child_by_name(stanza, property);
    if (child) {
        char *child_text = xmpp_stanza_get_text(child);
        if (child_text) {
            result = strdup(child_text);
            xmpp_free(ctx, child_text);
        }
    }

    return result;
}
Ejemplo n.º 17
0
static int _handle_features(xmpp_conn_t * const conn,
			    xmpp_stanza_t * const stanza,
			    void * const userdata)
{
    xmpp_stanza_t *child, *mech;
    char *text;

    /* remove the handler that detects missing stream:features */
    xmpp_timed_handler_delete(conn, _handle_missing_features);

    /* check for TLS */
    if (!conn->secured) {
        if (!conn->tls_disabled) {
            child = xmpp_stanza_get_child_by_name(stanza, "starttls");
            if (child && (strcmp(xmpp_stanza_get_ns(child), XMPP_NS_TLS) == 0))
                conn->tls_support = 1;
        } else {
            conn->tls_support = 0;
        }
    }

    /* check for SASL */
    child = xmpp_stanza_get_child_by_name(stanza, "mechanisms");
    if (child && (strcmp(xmpp_stanza_get_ns(child), XMPP_NS_SASL) == 0)) {
	for (mech = xmpp_stanza_get_children(child); mech;
	     mech = xmpp_stanza_get_next(mech)) {
	    if (xmpp_stanza_get_name(mech) && strcmp(xmpp_stanza_get_name(mech), "mechanism") == 0) {
		text = xmpp_stanza_get_text(mech);
		if (strcasecmp(text, "PLAIN") == 0)
		    conn->sasl_support |= SASL_MASK_PLAIN;
		else if (strcasecmp(text, "DIGEST-MD5") == 0)
		    conn->sasl_support |= SASL_MASK_DIGESTMD5;
                else if (strcasecmp(text, "SCRAM-SHA-1") == 0)
                    conn->sasl_support |= SASL_MASK_SCRAMSHA1;
		else if (strcasecmp(text, "ANONYMOUS") == 0)
		    conn->sasl_support |= SASL_MASK_ANONYMOUS;

		xmpp_free(conn->ctx, text);
	    }
	}
    }

    _auth(conn);

    return 0;
}
Ejemplo n.º 18
0
static int _zkmse_receive_rsp(xmpp_ua_t *ua, xmpp_stanza_t *stanza, void *userdata)
{
	char *type = xmpp_stanza_get_type(stanza);
	if (!type || strcmp(type, "chat"))
		return 0;
	char *from = xmpp_stanza_get_attribute(stanza, "from");
	if (!from)
		return 0;
	xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, "body");
	if (!body)
		return 0;
	char *str_body = xmpp_stanza_get_text(body);
	if (!str_body)
		return 0;
	char cmd_type[16];
	char result[128];
	char *option = (char *)malloc(strlen(str_body));
	int msg_id;

	int rc = sscanf(str_body, "%15[^|]|%d|%*[^|]|%127[^|]|%[^\0]",  cmd_type, &msg_id, result, option);
	if (rc < 4)
	{
		free(option);
		return 0;
	}
	if (!strcmp(cmd_type, "AckCmd"))
	{
		WaitForSingleObject(ua->mutex_4_ua, INFINITE);
		mse_cmd_map::iterator iter = ua->mse_map.find(msg_id);
		if (iter == ua->mse_map.end())
		{
			free(option);
			ReleaseMutex(ua->mutex_4_ua);
			return 0;
		}
		mse_cmd_data data = iter->second;
		data.cb(ua, from, result, option, data.userdata);
		free(option);
		ReleaseMutex(ua->mutex_4_ua);
		return 1;
	}

	free(option);
	return 0;
}
Ejemplo n.º 19
0
GSList *
_get_groups_from_item(xmpp_stanza_t *item)
{
    GSList *groups = NULL;
    xmpp_stanza_t *group_element = xmpp_stanza_get_children(item);

    while (group_element) {
        if (strcmp(xmpp_stanza_get_name(group_element), STANZA_NAME_GROUP) == 0) {
            char *groupname = xmpp_stanza_get_text(group_element);
            if (groupname) {
                groups = g_slist_append(groups, groupname);
            }
        }
        group_element = xmpp_stanza_get_next(group_element);
    }

    return groups;
}
Ejemplo n.º 20
0
//TODO: Create structure for holding the roster
static int _HandleRosterReply(xmpp_conn_t * const conn,
		xmpp_stanza_t * const stanza, void * const userdata) {
	xmpp_stanza_t *query, *item, *group;
	char *type, *name, *jid, *sub, groups[200], *groupName;
	char *g;
	int r_count = 0;

	sdvpIdleCounter = 0;
	sdvpPingsSent = 0;

	type = xmpp_stanza_get_type(stanza);
	if (strcmp(type, "error") == 0) {
		syslog(LOG_WARNING, "ERROR: query failed\n");
	} else {
		query = xmpp_stanza_get_child_by_name(stanza, "query");
		syslog(LOG_INFO, "Roster (Only the first group is shown!):\n----------\n");
		for (item = xmpp_stanza_get_children(query); item; item =
				xmpp_stanza_get_next(item)) {
			name = xmpp_stanza_get_attribute(item, "name");
			jid = xmpp_stanza_get_attribute(item, "jid");
			sub = xmpp_stanza_get_attribute(item, "subscription");

			// Get groups
			//
			strcpy(groups, "-");
			for (group = xmpp_stanza_get_children(item); group; group =
					xmpp_stanza_get_next(group)) {
				groupName = xmpp_stanza_get_name(group);
				if (strcmp(groupName, "group") == 0) {
				    g = xmpp_stanza_get_text(group);
					strcpy(groups, g);
					free(g);
				}
			}

			syslog(LOG_INFO, "#%d\tName=%s\n\tJID=%s\n\tsubscription=%s\n\tGroups=%s\n\n",
					r_count, name, jid, sub, groups);
			r_count++;
		}
		syslog(LOG_INFO, "----------\n");
	}
	return 1;
}
Ejemplo n.º 21
0
static int _chat_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata)
{
    xmppdata_t xdata;
    char *intext;

    xmppchat_userdata_t *udata = (xmppchat_userdata_t *) userdata;

    if (!xmpp_stanza_get_child_by_name(stanza, "body"))
        return 1;
    if (xmpp_stanza_get_attribute(stanza, "type") != NULL && !strcmp(xmpp_stanza_get_attribute(stanza, "type"), "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_attribute(stanza, "from"), intext);
    xdata.from = xmpp_stanza_get_attribute(stanza, "from");
    xdata.data = (void *) intext;
    if (udata != NULL && udata->handler != NULL)
        udata->handler(conn, &xdata, udata->userdata);
    return 1;
}
Ejemplo n.º 22
0
int message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata)
{
	char *intext, replytext[512];
    int ret;
	xmpp_ctx_t *ctx = (xmpp_ctx_t*)userdata;
	
	if(!xmpp_stanza_get_child_by_name(stanza, "body")) return 1;
	if(!strcmp(xmpp_stanza_get_attribute(stanza, "type"), "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_attribute(stanza, "from"), intext);
	
    /* exec command */
    ret = cs_exe(cs, intext, ctx, conn, stanza);

    sprintf(replytext, "%s ret:%d.", intext, ret);
    send_chattext(ctx, conn, stanza, replytext);

    xmpp_free(ctx, intext);
	return 1;
}
Ejemplo n.º 23
0
void
_private_chat_handler(xmpp_stanza_t *const stanza, const char *const fulljid)
{
    xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_BODY);
    if (!body) {
        return;
    }

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

    GDateTime *timestamp = stanza_get_delay(stanza);
    if (timestamp) {
        sv_ev_delayed_private_message(fulljid, message, timestamp);
        g_date_time_unref(timestamp);
    } else {
        sv_ev_incoming_private_message(fulljid, message);
    }

    xmpp_ctx_t *ctx = connection_get_ctx();
    xmpp_free(ctx, message);
}
Ejemplo n.º 24
0
static int
_bookmark_handle_result(xmpp_conn_t * const conn,
    xmpp_stanza_t * const stanza, void * const userdata)
{
    xmpp_ctx_t *ctx = connection_get_ctx();
    char *id = (char *)userdata;
    xmpp_stanza_t *ptr;
    xmpp_stanza_t *nick;
    char *name;
    char *jid;
    char *autojoin;
    gboolean autojoin_val;
    Jid *my_jid;
    Bookmark *item;

    xmpp_timed_handler_delete(conn, _bookmark_handle_delete);
    g_free(id);

    name = xmpp_stanza_get_name(stanza);
    if (!name || strcmp(name, STANZA_NAME_IQ) != 0) {
        return 0;
    }

    ptr = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY);
    if (!ptr) {
        return 0;
    }
    ptr = xmpp_stanza_get_child_by_name(ptr, STANZA_NAME_STORAGE);
    if (!ptr) {
        return 0;
    }

    if (bookmark_ac == NULL) {
        bookmark_ac = autocomplete_new();
    }
    my_jid = jid_create(jabber_get_fulljid());

    ptr = xmpp_stanza_get_children(ptr);
    while (ptr) {
        name = xmpp_stanza_get_name(ptr);
        if (!name || strcmp(name, STANZA_NAME_CONFERENCE) != 0) {
            ptr = xmpp_stanza_get_next(ptr);
            continue;
        }
        jid = xmpp_stanza_get_attribute(ptr, STANZA_ATTR_JID);
        if (!jid) {
            ptr = xmpp_stanza_get_next(ptr);
            continue;
        }

        log_debug("Handle bookmark for %s", jid);

        name = NULL;
        nick = xmpp_stanza_get_child_by_name(ptr, "nick");
        if (nick) {
            char *tmp;
            tmp = xmpp_stanza_get_text(nick);
            if (tmp) {
                name = strdup(tmp);
                xmpp_free(ctx, tmp);
            }
        }

        autojoin = xmpp_stanza_get_attribute(ptr, "autojoin");
        if (autojoin && (strcmp(autojoin, "1") == 0 || strcmp(autojoin, "true") == 0)) {
            autojoin_val = TRUE;
        } else {
            autojoin_val = FALSE;
        }

        autocomplete_add(bookmark_ac, jid);
        item = malloc(sizeof(*item));
        item->jid = strdup(jid);
        item->nick = name;
        item->autojoin = autojoin_val;
        bookmark_list = g_list_append(bookmark_list, item);


        /* TODO: preference whether autojoin */
        if (autojoin_val) {
            if (autojoin_count < BOOKMARK_AUTOJOIN_MAX) {
                Jid *room_jid;

                ++autojoin_count;

                if (name == NULL) {
                    name = my_jid->localpart;
                }

                log_debug("Autojoin %s with nick=%s", jid, name);
                room_jid = jid_create_from_bare_and_resource(jid, name);
                if (!muc_room_is_active(room_jid)) {
                    presence_join_room(room_jid);
                    /* TODO: this should be removed after fixing #195 */
                    ui_room_join(room_jid);
                }
                jid_destroy(room_jid);
            } else {
                log_debug("Rejected autojoin %s (maximum has been reached)", jid);
            }
        }

        ptr = xmpp_stanza_get_next(ptr);
    }

    jid_destroy(my_jid);

    return 0;
}
Ejemplo n.º 25
0
static sdvp_WriteParam_t* _CreateWriteParam (xmpp_stanza_t * params) {
    int expectedParams, nVarParams = 0;
    sdvp_WriteParam_t* rv = NULL;
    int i = 0, j = 0, expectedDataElementsInArray;
    bool elementNotFund = false;
    xmpp_stanza_t *structure = NULL, *member = NULL, *value = NULL,
            *type = NULL;
    xmpp_stanza_t *name = NULL, *array, *arrayEntry;
    char *text, *textname;

    structure = _GetRpcStructureElementFromParams(params);
    if (structure == NULL ) {
        return NULL ;
    }

    // Get the number of elements
    //
    expectedParams = _FindNumberOfParams(params);
    if (expectedParams > 1) {
        nVarParams = expectedParams - 1;
    } else {
        // TODO: This should result in a ServiceError as minimum two is expected
    }

    rv = _InitiateWriteParameters(nVarParams);
    for (member = xmpp_stanza_get_children(structure);
            member && !elementNotFund && (i < expectedParams); member =
                    xmpp_stanza_get_next(member)) {
        if (xmpp_stanza_get_name(member)
                && strcmp(xmpp_stanza_get_name(member), "member") == 0) {

            if (!(name = xmpp_stanza_get_child_by_name(member, "name"))) {
                _FreeWriteParameters(rv);
                // TODO: Signal error back
                return NULL ;
            }
            name = xmpp_stanza_get_children(name);

			textname = xmpp_stanza_get_text(name);
            if (strcmp(textname, "VariableAccessSpecification") == 0) {
                if (!(value = xmpp_stanza_get_child_by_name(member, "value"))) {
                    _FreeWriteParameters(rv);
                    // TODO: Signal error back
                    return NULL ;
                }
                if ((type = xmpp_stanza_get_child_by_name(value, "string"))) {
                    if (xmpp_stanza_get_children(type)) {
                        text = xmpp_stanza_get_text(type);
                        rv->params->reference = text;
                    } else {
                        rv->params->reference = NULL;
                    }
                } else {
                    elementNotFund = true;
                }
            } else if (strcmp(textname, "ListOfData") == 0) {
                if (!(value = xmpp_stanza_get_child_by_name(member, "value"))) {
                    _FreeWriteParameters(rv);
                    // TODO: Signal error back
                    return NULL ;
                }
                if (!(array = xmpp_stanza_get_child_by_name(value, "array"))) {
                    _FreeWriteParameters(rv);
                    // TODO: Signal error back
                    return NULL ;
                }

                expectedDataElementsInArray = _countDataElementsInArray(array);
                _InitiateWriteDataParameters(rv, expectedDataElementsInArray);

//                array = xmpp_stanza_get_children(array);
                arrayEntry = xmpp_stanza_get_children(array);
//HJP Why skip the first element?!
//                arrayEntry = xmpp_stanza_get_next(arrayEntry);

                for (j = 0; arrayEntry;
                     arrayEntry = xmpp_stanza_get_next(arrayEntry)) {

                    if (xmpp_stanza_is_tag(arrayEntry)) {
                        if (!(value = xmpp_stanza_get_child_by_name(arrayEntry,
                                "value"))) {
                            _FreeWriteParameters(rv);
                            // TODO: Signal error back
                            return NULL ;
                        }

                        // MISSING: base64
                        if ((type = xmpp_stanza_get_child_by_name(value,
                                "string"))) {
                            rv->params->dataAttributeValues[j]->dataType =
                                    IEC_VISIBLE_STRING;
                            rv->params->dataAttributeValues[j]->strValue =
                                    xmpp_stanza_get_text(type);
                        } else if ((type = xmpp_stanza_get_child_by_name(value,
                                "i4"))
                                ||
                                (type = xmpp_stanza_get_child_by_name(value,
                                        "int"))) {
                            rv->params->dataAttributeValues[j]->dataType =
                                    IEC_INT32;
							text = xmpp_stanza_get_text(type);
                            rv->params->dataAttributeValues[j]->intValue =
                                    strtol(text, NULL,
                                            10);
							free(text);
                        } else if ((type = xmpp_stanza_get_child_by_name(value,
                                "double"))) {
                            rv->params->dataAttributeValues[j]->dataType =
                                    IEC_FLOAT32;
							text = xmpp_stanza_get_text(type);
                            rv->params->dataAttributeValues[j]->f32Value =
                                    strtof(text, NULL );
							free(text);
                        } else if ((type = xmpp_stanza_get_child_by_name(value,
                                "boolean"))) {
                            rv->params->dataAttributeValues[j]->dataType =
                                    IEC_BOOLEAN;
							text = xmpp_stanza_get_text(type);
                            if (strcmp(text, "1") == 0) {
                                rv->params->dataAttributeValues[j]->boolValue =
                                        true;
                            } else {
                                rv->params->dataAttributeValues[j]->boolValue =
                                        false;
                            }
							free(text);
                        } else if ((type = xmpp_stanza_get_child_by_name(value,
                                "dateTime.iso8601"))) {
                            rv->params->dataAttributeValues[j]->dataType =
                                    IEC_TIME;
							text = xmpp_stanza_get_text(type);
                            rv->params->dataAttributeValues[j]->time =
                                    ne_iso8601_parse(text);
							free(text);
                        }
                        j++;
                    }
                }

            } else {
//                if (!type) {
//                    _FreeWriteParameters(rv);
//                    return NULL ;
//                }
            }
			free(textname);
        }
    }
    return rv;
}
Ejemplo n.º 26
0
/**
 * Creates the parameter structure for the GetNameList callback
 * @param[in] params This stanza is the params element in the XML-RPC format
 * @return The structure or null if an error were found.
 */
static sdvp_GetNameListParam_t* _CreateGetNameListParam (xmpp_stanza_t * params) {
    const int expectedParams = 4;
    sdvp_GetNameListParam_t* rv = NULL;
    int i = 0;
    bool elementNotFund = false;
    xmpp_stanza_t *structure = NULL, *member = NULL, *value = NULL,
            *type = NULL, *name = NULL;
    char *text, *textname;

    structure = _GetRpcStructureElementFromParams(params);
    if (structure == NULL ) {
        return NULL ;
    }
    rv = _InitiateGetNameListParameters();
    for (member = xmpp_stanza_get_children(structure);
            member && !elementNotFund && (i < expectedParams); member =
                    xmpp_stanza_get_next(member)) {
        if (xmpp_stanza_get_name(member)
                && strcmp(xmpp_stanza_get_name(member), "member") == 0) {

            if (!(name = xmpp_stanza_get_child_by_name(member, "name"))) {
                _FreeGetNameListParameters(rv);
                // TODO: Signal error back
                return NULL ;
            }
            name = xmpp_stanza_get_children(name);

			textname = xmpp_stanza_get_text(name);
            if (strcmp(textname, "ObjectClass") == 0) {
                if (!(value = xmpp_stanza_get_child_by_name(member, "value"))) {
                    _FreeGetNameListParameters(rv);
                    // TODO: Signal error back
                    return NULL ;
                }
                if ((type = xmpp_stanza_get_child_by_name(value, "string"))) {
                    if (xmpp_stanza_get_children(type)) {
                        text = xmpp_stanza_get_text(type);
                        rv->params.objectClass = text;
                    } else {
                        rv->params.objectClass = NULL;
                    }
                } else {
                    elementNotFund = true;
                }

            } else if (strcmp(textname, "ObjectScope") == 0) {
                if ((type = xmpp_stanza_get_child_by_name(value, "string"))) {
                    if (xmpp_stanza_get_children(type)) {
                        text = xmpp_stanza_get_text(type);
                        rv->params.objectScope = text;
                    } else {
                        rv->params.objectScope = NULL;
                    }
                } else {
                    elementNotFund = true;
                }
            } else if (strcmp(textname, "DomainName") == 0) {
                if ((type = xmpp_stanza_get_child_by_name(value, "string"))) {
                    if (xmpp_stanza_get_children(type)) {
                        text = xmpp_stanza_get_text(type);
                        rv->params.domainName = text;
                    } else {
                        rv->params.domainName = NULL;
                    }
                } else {
                    elementNotFund = true;
                }
            } else if (strcmp(textname, "ContinueAfter") == 0) {
                if ((type = xmpp_stanza_get_child_by_name(value, "string"))) {
                    if (xmpp_stanza_get_children(type)) {
                        text = xmpp_stanza_get_text(type);
                        rv->params.continueAfter = text;
                    } else {
                        rv->params.continueAfter = NULL;
                    }
                } else {
                    elementNotFund = true;
                }
            } else {
//                if (!type) {
//                    _FreeGetNameListParameters(rv);
//                    return NULL ;
//                }
            }
			free(textname);
        }
    }
    return rv;
}
Ejemplo n.º 27
0
static int
_bookmark_handle_result(xmpp_conn_t *const conn,
    xmpp_stanza_t *const stanza, void *const userdata)
{
    xmpp_ctx_t *ctx = connection_get_ctx();
    char *id = (char *)userdata;
    xmpp_stanza_t *ptr;
    xmpp_stanza_t *nick;
    xmpp_stanza_t *password_st;
    char *name;
    char *jid;
    char *autojoin;
    char *password;
    gboolean autojoin_val;
    Jid *my_jid;
    Bookmark *item;

    xmpp_timed_handler_delete(conn, _bookmark_handle_delete);
    g_free(id);

    name = xmpp_stanza_get_name(stanza);
    if (!name || strcmp(name, STANZA_NAME_IQ) != 0) {
        return 0;
    }

    ptr = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY);
    if (!ptr) {
        return 0;
    }
    ptr = xmpp_stanza_get_child_by_name(ptr, STANZA_NAME_STORAGE);
    if (!ptr) {
        return 0;
    }

    if (bookmark_ac == NULL) {
        bookmark_ac = autocomplete_new();
    }
    my_jid = jid_create(jabber_get_fulljid());

    ptr = xmpp_stanza_get_children(ptr);
    while (ptr) {
        name = xmpp_stanza_get_name(ptr);
        if (!name || strcmp(name, STANZA_NAME_CONFERENCE) != 0) {
            ptr = xmpp_stanza_get_next(ptr);
            continue;
        }
        jid = xmpp_stanza_get_attribute(ptr, STANZA_ATTR_JID);
        if (!jid) {
            ptr = xmpp_stanza_get_next(ptr);
            continue;
        }

        log_debug("Handle bookmark for %s", jid);

        name = NULL;
        nick = xmpp_stanza_get_child_by_name(ptr, "nick");
        if (nick) {
            char *tmp;
            tmp = xmpp_stanza_get_text(nick);
            if (tmp) {
                name = strdup(tmp);
                xmpp_free(ctx, tmp);
            }
        }

        password = NULL;
        password_st = xmpp_stanza_get_child_by_name(ptr, "password");
        if (password_st) {
            char *tmp;
            tmp = xmpp_stanza_get_text(password_st);
            if (tmp) {
                password = strdup(tmp);
                xmpp_free(ctx, tmp);
            }
        }

        autojoin = xmpp_stanza_get_attribute(ptr, "autojoin");
        if (autojoin && (strcmp(autojoin, "1") == 0 || strcmp(autojoin, "true") == 0)) {
            autojoin_val = TRUE;
        } else {
            autojoin_val = FALSE;
        }

        autocomplete_add(bookmark_ac, jid);
        item = malloc(sizeof(*item));
        item->jid = strdup(jid);
        item->nick = name;
        item->password = password;
        item->autojoin = autojoin_val;
        bookmark_list = g_list_append(bookmark_list, item);

        if (autojoin_val) {
            Jid *room_jid;

            char *account_name = jabber_get_account_name();
            ProfAccount *account = accounts_get_account(account_name);
            if (name == NULL) {
                name = account->muc_nick;
            }

            log_debug("Autojoin %s with nick=%s", jid, name);
            room_jid = jid_create_from_bare_and_resource(jid, name);
            if (!muc_active(room_jid->barejid)) {
                presence_join_room(jid, name, password);
                muc_join(jid, name, password, TRUE);
            }
            jid_destroy(room_jid);
            account_free(account);
        }

        ptr = xmpp_stanza_get_next(ptr);
    }

    jid_destroy(my_jid);

    return 0;
}
Ejemplo n.º 28
0
static int _ibb_set_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata)
{
    xmpp_stanza_t *child;
    xmpp_ibb_userdata_t * udata = (xmpp_ibb_userdata_t *) userdata;
    xmpp_ibb_session_t * sess;
    char *from, *id, *type;

    id = xmpp_stanza_get_id(stanza);
    from = xmpp_stanza_get_attribute(stanza, "from");
    type = xmpp_stanza_get_type(stanza);
    if ((child = xmpp_stanza_get_child_by_name(stanza, "open")) != NULL) {
        char *sid = xmpp_stanza_get_attribute(child, "sid");
        char *bsize = xmpp_stanza_get_attribute(child, "block-size");
        if (sid == NULL || bsize == NULL) {
            xmpp_iq_ack_error(conn, id, from, "cancel", "not-acceptable");
            return 1;
        } else {
            xmpp_iq_ack_result(conn, id, from);
        }
        sess = _ibb_session_init(conn, from, sid);
        strncpy(sess->id, id, sizeof(sess->id));
        strncpy(sess->peer, from, sizeof(sess->peer));
        sess->state = STATE_READY;
        sess->block_size = atoi(bsize);
        if (udata != NULL && udata->open_cb != NULL)
            udata->open_cb(sess, type);
        ilist_add(g_list, sess);
    } else if ((child = xmpp_stanza_get_child_by_name(stanza, "data")) != NULL) {
        char *sid = xmpp_stanza_get_attribute(child, "sid");
        sess = ilist_finditem_func(g_list, _find_sid, sid);
        if (sess != NULL) {
            xmppdata_t xdata;
            int seq = 0;
            char *intext = xmpp_stanza_get_text(child);
            xmpp_iq_ack_result(conn, id, from);
            xdata.from = from;
            strncpy(sess->id, id, sizeof(sess->id));
            seq = atoi(xmpp_stanza_get_attribute(child, "seq"));
            if (seq != (sess->recv_seq + 1)) {
                //printf("sequence number is not continue. new seq %d last seq %d\n", seq, sess->recv_seq);
            }
            sess->recv_seq = seq;
            xmpp_b64decode(intext, (char **) &xdata.data, (size_t *) &xdata.size);
            if (udata != NULL && udata->recv_cb != NULL)
                udata->recv_cb(sess, &xdata);
            xmpp_b64free(sess->recv_data);
            sess->recv_data = NULL;
        } else {
            //printf("unknown session is not in handle.\n");
            xmpp_iq_ack_error(conn, id, from, "cancel", "item-not-found");
        }
    } else if ((child = xmpp_stanza_get_child_by_name(stanza, "close")) != NULL) {
        char *sid = xmpp_stanza_get_attribute(child, "sid");
        sess = ilist_finditem_func(g_list, _find_sid, sid);
        if (sess != NULL) {
            xmpp_iq_ack_result(conn, id, from);
            strncpy(sess->id, id, sizeof(sess->id));
            sess->state = STATE_NONE;
            if (udata != NULL && udata->close_cb != NULL)
                udata->close_cb(sess, type);
            _ibb_session_release(sess);
        }
    }

    time(&glast_ping_time);

    return 1;
}
Ejemplo n.º 29
0
int message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata)
{
    xmpp_stanza_t *reply, *body, *text;
    char *intext, *replytext;
    xmpp_ctx_t *ctx = (xmpp_ctx_t*)userdata;

    if(!xmpp_stanza_get_child_by_name(stanza, "body")) return 1;
    if(!strcmp(xmpp_stanza_get_attribute(stanza, "type"), "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_attribute(stanza, "from"), intext);

    reply = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(reply, "message");
    xmpp_stanza_set_type(reply, xmpp_stanza_get_type(stanza)?xmpp_stanza_get_type(stanza):"chat");
    xmpp_stanza_set_attribute(reply, "to", xmpp_stanza_get_attribute(stanza, "from"));

    body = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(body, "body");

    //printf("intext char: %c\n", *intext);

    /* reply section */
    if(*intext == '!')
    {
        intext++;
        char command[32];
        int idx=0;
        while(idx<31 && *intext != '\0' && *intext != ' ')
        {
            command[idx] = *intext;
            idx++;
            intext++;
        }
        command[idx] = '\0';

        while(*intext == ' ' && *intext != '\0')
            intext++;
        char *args = intext;

        if(command[0]=='\0')
        {
            replytext = strdup("you're barking mad!");
        } else {
            // good command and args //
            if(!strcmp("google", command))
            {
                char *res = google(args);
                replytext = malloc(snprintf(NULL, 0 , "result: %s", res)+1);
                sprintf(replytext, "result: %s", res);
                free(res);
            } else if(!strcmp("d20", command)) {
                srand(time(NULL));
                int rand_int = (rand() % 20) + 1;
                if(rand_int == 20)
                {
                    replytext = malloc(snprintf(NULL, 0, "d20: %d CRITICAL!!!", rand_int)+1);
                    sprintf(replytext, "d20: %d CRITICAL!!!", rand_int);
                } else {
                    replytext = malloc(snprintf(NULL, 0, "d20: %d", rand_int)+1);
                    sprintf(replytext, "d20: %d", rand_int);
                }
            } else {
                replytext = malloc(snprintf(NULL, 0 , "command: %s args: %s", command, args)+1);
                sprintf(replytext, "command: %s args: %s", command, args);
            }
        }
    } else {

        if (strcmp(intext, "boob")==0)
        {
            replytext = malloc(strlen("Boobies!")+1);
            replytext = strcpy(replytext, "Boobies!");
        }
        else
        {
            replytext = malloc(strlen(" to you too!") + strlen(intext) + 1);
            strcpy(replytext, intext);
            strcat(replytext, " to you too!");
        }
    }
    /* end reply logic */

    text = xmpp_stanza_new(ctx);
    xmpp_stanza_set_text(text, replytext);
    xmpp_stanza_add_child(body, text);
    xmpp_stanza_add_child(reply, body);

    xmpp_send(conn, reply);
    xmpp_stanza_release(reply);
    free(replytext);
    return 1;
}
Ejemplo n.º 30
0
static int _handle_bind(xmpp_conn_t * const conn,
			xmpp_stanza_t * const stanza,
			void * const userdata)
{
    char *type;
    xmpp_stanza_t *iq, *session;

    /* delete missing bind handler */
    xmpp_timed_handler_delete(conn, _handle_missing_bind);

    /* server has replied to bind request */
    type = xmpp_stanza_get_type(stanza);
    if (type && strcmp(type, "error") == 0) {
	xmpp_error(conn->ctx, "xmpp", "Binding failed.");
	xmpp_disconnect(conn);
    } else if (type && strcmp(type, "result") == 0) {
        xmpp_stanza_t *binding = xmpp_stanza_get_child_by_name(stanza, "bind");
	xmpp_debug(conn->ctx, "xmpp", "Bind successful.");

        if (binding) {
            xmpp_stanza_t *jid_stanza = xmpp_stanza_get_child_by_name(binding,
                                                                      "jid");
            if (jid_stanza) {
                conn->bound_jid = xmpp_stanza_get_text(jid_stanza);
            }
        }

	/* establish a session if required */
	if (conn->session_required) {
	    /* setup response handlers */
	    handler_add_id(conn, _handle_session, "_xmpp_session1", NULL);
	    handler_add_timed(conn, _handle_missing_session,
			      SESSION_TIMEOUT, NULL);

	    /* send session request */
	    iq = xmpp_stanza_new(conn->ctx);
	    if (!iq) {
		disconnect_mem_error(conn);
		return 0;
	    }

	    xmpp_stanza_set_name(iq, "iq");
	    xmpp_stanza_set_type(iq, "set");
	    xmpp_stanza_set_id(iq, "_xmpp_session1");

	    session = xmpp_stanza_new(conn->ctx);
	    if (!session) {
		xmpp_stanza_release(iq);
		disconnect_mem_error(conn);
	    }

	    xmpp_stanza_set_name(session, "session");
	    xmpp_stanza_set_ns(session, XMPP_NS_SESSION);

	    xmpp_stanza_add_child(iq, session);
	    xmpp_stanza_release(session);

	    /* send session establishment request */
	    xmpp_send(conn, iq);
	    xmpp_stanza_release(iq);
	} else {
	    conn->authenticated = 1;

	    /* call connection handler */
	    conn->conn_handler(conn, XMPP_CONN_CONNECT, 0, NULL,
			       conn->userdata);
	}
    } else {
	xmpp_error(conn->ctx, "xmpp", "Server sent malformed bind reply.");
	xmpp_disconnect(conn);
    }

    return 0;
}