示例#1
0
int zkmuc_broadcast_message(zkmuc_ctx_t *ctx, const char *msg)
{
	xmpp_stanza_t *stanza_msg = xmpp_stanza_new(_xmpp_ctx);
	xmpp_stanza_set_name(stanza_msg, "message");
	xmpp_stanza_set_attribute(stanza_msg, "to", ctx->room_id);
	xmpp_stanza_set_type(stanza_msg, "groupchat");

	xmpp_stanza_t *stanza_body = xmpp_stanza_new(_xmpp_ctx);
	xmpp_stanza_set_name(stanza_body, "zonekey");
	xmpp_stanza_t *stanza_jid = xmpp_stanza_new(_xmpp_ctx);
	xmpp_stanza_set_name(stanza_jid, "jid");
	xmpp_stanza_t *stanza_jid_value = xmpp_stanza_new(_xmpp_ctx);
	xmpp_stanza_set_text(stanza_jid_value, xmpp_ua_get_jid(ctx->ua));
	xmpp_stanza_add_child(stanza_jid, stanza_jid_value);
	xmpp_stanza_release(stanza_jid_value);
	xmpp_stanza_add_child(stanza_body, stanza_jid);
	xmpp_stanza_release(stanza_jid);
	xmpp_stanza_t *stanza_txt = xmpp_stanza_new(_xmpp_ctx);
	xmpp_stanza_set_text(stanza_txt, msg);
	xmpp_stanza_add_child(stanza_body, stanza_txt);
	xmpp_stanza_release(stanza_txt);
	xmpp_stanza_add_child(stanza_msg, stanza_body);
	xmpp_stanza_release(stanza_body);
	xmpp_ua_send(ctx->ua, stanza_msg);
	xmpp_stanza_release(stanza_msg);
	return 0;
}
示例#2
0
xmpp_stanza_t *
stanza_create_presence(xmpp_ctx_t *ctx, const char * const show,
    const char * const status)
{
    xmpp_stanza_t *presence = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(presence, STANZA_NAME_PRESENCE);

    if (show != NULL) {
        xmpp_stanza_t *show_stanza = xmpp_stanza_new(ctx);
        xmpp_stanza_set_name(show_stanza, STANZA_NAME_SHOW);
        xmpp_stanza_t *text = xmpp_stanza_new(ctx);
        xmpp_stanza_set_text(text, show);
        xmpp_stanza_add_child(show_stanza, text);
        xmpp_stanza_add_child(presence, show_stanza);
        xmpp_stanza_release(text);
        xmpp_stanza_release(show_stanza);
    }

    if (status != NULL) {
        xmpp_stanza_t *status_stanza = xmpp_stanza_new(ctx);
        xmpp_stanza_set_name(status_stanza, STANZA_NAME_STATUS);
        xmpp_stanza_t *text = xmpp_stanza_new(ctx);
        xmpp_stanza_set_text(text, status);
        xmpp_stanza_add_child(status_stanza, text);
        xmpp_stanza_add_child(presence, status_stanza);
        xmpp_stanza_release(text);
        xmpp_stanza_release(status_stanza);
    }

    return presence;
}
示例#3
0
int register_user(mio_conn_t *conn, char *user, char *pass)
{
	int err;
	mio_response_t *response = mio_response_new();
    xmpp_stanza_t *iq = NULL, *query = NULL, *username = NULL, *username_tag = NULL, *password = NULL, *password_tag = NULL;
    mio_stanza_t *stanza = mio_stanza_new(conn);
    // Check if connection is active
	if (!conn->xmpp_conn->authenticated) {
		return MIO_ERROR_DISCONNECTED;
	}

    // Create a new user stanza
	iq = xmpp_stanza_new(conn->xmpp_conn->ctx);
	xmpp_stanza_set_name(iq, "iq");
	xmpp_stanza_set_attribute(iq, "id", "reg2");
	xmpp_stanza_set_type(iq, "set");

    // Creqte query stanza
    query = xmpp_stanza_new(conn -> xmpp_conn -> ctx);
    xmpp_stanza_set_name(query,"query");
    xmpp_stanza_set_ns(query,"jabber:iq:register");
    
    // create username stanza 
    username = xmpp_stanza_new(conn -> xmpp_conn -> ctx);
    username_tag = xmpp_stanza_new(conn -> xmpp_conn -> ctx);
    xmpp_stanza_set_name(username,"username");
    xmpp_stanza_set_text(username_tag, user);



    // create password stanza
    password = xmpp_stanza_new(conn -> xmpp_conn -> ctx);
    password_tag = xmpp_stanza_new(conn -> xmpp_conn -> ctx);
    xmpp_stanza_set_name(password, "password");
    xmpp_stanza_set_text(password_tag,pass);

    // create password stanza
    password = xmpp_stanza_new(conn -> xmpp_conn -> ctx);
    password_tag = xmpp_stanza_new(conn -> xmpp_conn -> ctx);
    xmpp_stanza_set_name(password, "password");
    xmpp_stanza_set_text(password_tag,pass);
    // Build xmpp message
	xmpp_stanza_add_child(password, password_tag);
	xmpp_stanza_add_child(username, username_tag);
	xmpp_stanza_add_child(query, username);
	xmpp_stanza_add_child(query, password);
	xmpp_stanza_add_child(iq, query);
    stanza -> xmpp_stanza = iq;
    
    // Send out the stanza
	err = _mio_send_nonblocking(conn, stanza);
    
    // Release the stanzas
	mio_stanza_free(stanza);
    
	mio_response_free(response);
	return 0;
}
示例#4
0
文件: basic.c 项目: nazda/wippien
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; 
} 
示例#5
0
void send_stdin_once(xmpp_conn_t * const conn, xmpp_ctx_t *ctx, char *jid_to)
{
    int n;
    char buf[1024], *stdin_b64;
    xmpp_stanza_t *message, *body, *text;

    while (n = fread(buf, sizeof(char), sizeof buf, stdin)){
        stdin_b64 = to_base64(buf, n);

        message = xmpp_stanza_new(ctx);
        xmpp_stanza_set_name(message, "message");
        xmpp_stanza_set_type(message, "chat");
        xmpp_stanza_set_attribute(message, "to", jid_to);

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

        text = xmpp_stanza_new(ctx);
        xmpp_stanza_set_text(text, stdin_b64);
        xmpp_stanza_add_child(body, text);
        xmpp_stanza_add_child(message, body);

        xmpp_send(conn, message);
        xmpp_stanza_release(message);
        free(stdin_b64);
    }
}
示例#6
0
xmpp_stanza_t *
stanza_create_message(xmpp_ctx_t *ctx, const char * const recipient,
    const char * const type, const char * const message,
    const char * const state)
{
    char *encoded_xml = encode_xml(message);

    xmpp_stanza_t *msg, *body, *text;

    msg = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(msg, STANZA_NAME_MESSAGE);
    xmpp_stanza_set_type(msg, type);
    xmpp_stanza_set_attribute(msg, STANZA_ATTR_TO, recipient);

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

    text = xmpp_stanza_new(ctx);
    xmpp_stanza_set_text(text, encoded_xml);
    xmpp_stanza_add_child(body, text);
    xmpp_stanza_add_child(msg, body);

    if (state != NULL) {
        xmpp_stanza_t *chat_state = xmpp_stanza_new(ctx);
        xmpp_stanza_set_name(chat_state, state);
        xmpp_stanza_set_ns(chat_state, STANZA_NS_CHATSTATES);
        xmpp_stanza_add_child(msg, chat_state);
    }

    g_free(encoded_xml);

    return msg;
}
示例#7
0
文件: main.c 项目: jasjeetIM/PingMe
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;
}
示例#8
0
int xmppchat_send_message(xmpp_conn_t *conn, xmppdata_t *xdata)
{
    xmpp_stanza_t *szmsg, *szbody, *sztext;
    xmpp_ctx_t *ctx;

    ctx = xmpp_conn_get_context(conn);

    sztext = xmpp_stanza_new(ctx);
    xmpp_stanza_set_text(sztext, xdata->data);

    szbody = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(szbody, "body");
    xmpp_stanza_add_child(szbody, sztext);

    szmsg = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(szmsg, "message");
    xmpp_stanza_set_type(szmsg, "chat");
    xmpp_stanza_set_attribute(szmsg, "to", xdata->tojid);
    xmpp_stanza_add_child(szmsg, szbody);

    xmpp_send(conn, szmsg);
    xmpp_stanza_release(szmsg);

    return 0;
}
示例#9
0
/** Add <body/> child element to a <message/> stanza with the given text.
 *
 *  @param msg a <message> stanza object without <body/> child element.
 *
 *  @return 0 on success (XMPP_EOK), and a number less than 0 on failure
 *      (XMPP_EMEM, XMPP_EINVOP)
 *
 *  @ingroup Stanza
 */
int xmpp_message_set_body(xmpp_stanza_t *msg, const char * const text)
{
    xmpp_ctx_t *ctx = msg->ctx;
    xmpp_stanza_t *body;
    xmpp_stanza_t *text_stanza;
    const char *name;
    int ret;

    /* check that msg is a <message/> stanza and doesn't contain <body/> */
    name = xmpp_stanza_get_name(msg);
    body = xmpp_stanza_get_child_by_name(msg, "body");
    if (!name || strcmp(name, "message") != 0 || body)
        return XMPP_EINVOP;

    body = xmpp_stanza_new(ctx);
    text_stanza = xmpp_stanza_new(ctx);

    ret = body && text_stanza ? XMPP_EOK : XMPP_EMEM;
    if (ret == XMPP_EOK)
        ret = xmpp_stanza_set_name(body, "body");
    if (ret == XMPP_EOK)
        ret = xmpp_stanza_set_text(text_stanza, text);
    if (ret == XMPP_EOK)
        ret = xmpp_stanza_add_child(body, text_stanza);
    if (ret == XMPP_EOK)
        ret = xmpp_stanza_add_child(msg, body);

    if (text_stanza)
        xmpp_stanza_release(text_stanza);
    if (body)
        xmpp_stanza_release(body);

    return ret;
}
示例#10
0
int version_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata)
{
    xmpp_stanza_t *reply, *query, *name, *version, *text;
    const char *ns;
    xmpp_ctx_t *ctx = (xmpp_ctx_t*)userdata;

    printf("Received version request from %s\n", xmpp_stanza_get_from(stanza));

    reply = xmpp_stanza_reply(stanza);
    xmpp_stanza_set_type(reply, "result");

    query = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(query, "query");
    ns = xmpp_stanza_get_ns(xmpp_stanza_get_children(stanza));
    if (ns) {
        xmpp_stanza_set_ns(query, ns);
    }

    name = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(name, "name");
    xmpp_stanza_add_child(query, name);
    xmpp_stanza_release(name);

    text = xmpp_stanza_new(ctx);
    xmpp_stanza_set_text(text, "libstrophe example bot");
    xmpp_stanza_add_child(name, text);
    xmpp_stanza_release(text);

    version = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(version, "version");
    xmpp_stanza_add_child(query, version);
    xmpp_stanza_release(version);

    text = xmpp_stanza_new(ctx);
    xmpp_stanza_set_text(text, "1.0");
    xmpp_stanza_add_child(version, text);
    xmpp_stanza_release(text);

    xmpp_stanza_add_child(reply, query);
    xmpp_stanza_release(query);

    xmpp_send(conn, reply);
    xmpp_stanza_release(reply);
    return 1;
}
示例#11
0
char*
message_send_chat_pgp(const char *const barejid, const char *const msg)
{
    xmpp_conn_t * const conn = connection_get_conn();
    xmpp_ctx_t * const ctx = connection_get_ctx();

    char *state = _session_state(barejid);
    char *jid = _session_jid(barejid);
    char *id = create_unique_id("msg");

    xmpp_stanza_t *message = NULL;
#ifdef HAVE_LIBGPGME
    char *account_name = jabber_get_account_name();
    ProfAccount *account = accounts_get_account(account_name);
    if (account->pgp_keyid) {
        Jid *jidp = jid_create(jid);
        char *encrypted = p_gpg_encrypt(jidp->barejid, msg);
        if (encrypted) {
            message = stanza_create_message(ctx, id, jid, STANZA_TYPE_CHAT, "This message is encrypted.");
            xmpp_stanza_t *x = xmpp_stanza_new(ctx);
            xmpp_stanza_set_name(x, STANZA_NAME_X);
            xmpp_stanza_set_ns(x, STANZA_NS_ENCRYPTED);
            xmpp_stanza_t *enc_st = xmpp_stanza_new(ctx);
            xmpp_stanza_set_text(enc_st, encrypted);
            xmpp_stanza_add_child(x, enc_st);
            xmpp_stanza_release(enc_st);
            xmpp_stanza_add_child(message, x);
            xmpp_stanza_release(x);
            free(encrypted);
        } else {
            message = stanza_create_message(ctx, id, jid, STANZA_TYPE_CHAT, msg);
        }
        jid_destroy(jidp);
    } else {
        message = stanza_create_message(ctx, id, jid, STANZA_TYPE_CHAT, msg);
    }
    account_free(account);
#else
    message = stanza_create_message(ctx, id, jid, STANZA_TYPE_CHAT, msg);
#endif
    free(jid);

    if (state) {
        stanza_attach_state(ctx, message, state);
    }

    stanza_attach_carbons_private(ctx, message);

    if (prefs_get_boolean(PREF_RECEIPTS_REQUEST)) {
        stanza_attach_receipt_request(ctx, message);
    }

    xmpp_send(conn, message);
    xmpp_stanza_release(message);

    return id;
}
示例#12
0
文件: mood.c 项目: Aversiste/mood
void
mood_publish(mood_callback_t callback, const char * const usermood,
    const char * const usertext)
{
	xmpp_stanza_t *iq, *pubsub, *publish, *item, *mood, *stanza, *text;

	iq = xmpp_stanza_new(ctx);
	xmpp_stanza_set_name(iq, "iq");
	xmpp_stanza_set_type(iq, "set");
	xmpp_stanza_set_id(iq, "mood1");	/* FIXME */

	pubsub = xmpp_stanza_new(ctx);
	xmpp_stanza_set_name(pubsub, "pubsub");
	xmpp_stanza_set_ns(pubsub, NS_PUBSUB);
	xmpp_stanza_add_child(iq, pubsub);
	xmpp_stanza_release(pubsub);

	publish = xmpp_stanza_new(ctx);
	xmpp_stanza_set_name(publish, "publish");
	xmpp_stanza_set_attribute(publish, "node", NS_MOOD);
	xmpp_stanza_add_child(pubsub, publish);
	xmpp_stanza_release(publish);

	item = xmpp_stanza_new(ctx);
	xmpp_stanza_set_name(item, "item");
	xmpp_stanza_add_child(publish, item);
	xmpp_stanza_release(item);

	mood = xmpp_stanza_new(ctx);
	xmpp_stanza_set_name(mood, "mood");
	xmpp_stanza_set_ns(mood, NS_MOOD);
	xmpp_stanza_add_child(item, mood);
	xmpp_stanza_release(mood);

	if (usermood != NULL) {
		stanza = xmpp_stanza_new(ctx);
		xmpp_stanza_set_name(stanza, usermood);
		xmpp_stanza_add_child(mood, stanza);
		xmpp_stanza_release(stanza);
	}

	if (usertext != NULL) {
		text = xmpp_stanza_new(ctx);
		xmpp_stanza_set_name(text, "text");
		xmpp_stanza_set_text(text, usertext);
		xmpp_stanza_add_child(mood, text);
		xmpp_stanza_release(text);
	}

	xmpp_id_handler_add(conn, mood_result_handler,
	    xmpp_stanza_get_id(iq), callback);

	xmpp_send(conn, iq);
	xmpp_stanza_release(iq);
}
示例#13
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;
}
示例#14
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;
}
示例#15
0
void xmpp_send_presence(xmpp_conn_t * const conn, xmpp_presence_show_t sw, const char *sv)
{
	static const char *show_table[] = {
		"chat",
		"away",
		"xa",
		"dnd"
	};
	xmpp_stanza_t *presence, *show, *status, *text;

	presence = xmpp_stanza_new(conn->ctx);
	xmpp_stanza_set_name(presence, "presence");

	show = xmpp_stanza_new(conn->ctx);
	xmpp_stanza_set_name(show, "show");
	xmpp_stanza_add_child(presence, show);

	text = xmpp_stanza_new(conn->ctx);
	xmpp_stanza_set_text(text, show_table[sw]);
	xmpp_stanza_add_child(show, text);
	xmpp_stanza_release(text);

	xmpp_stanza_release(show);

	status = xmpp_stanza_new(conn->ctx);
	xmpp_stanza_set_name(status, "status");
	xmpp_stanza_add_child(presence, status);

	text = xmpp_stanza_new(conn->ctx);
	xmpp_stanza_set_text(text, sv);
	xmpp_stanza_add_child(status, text);
	xmpp_stanza_release(text);

	xmpp_stanza_release(status);

	xmpp_send(conn, presence);
	xmpp_stanza_release(presence);
}
示例#16
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);
    }
}
示例#17
0
static void zkmuc_unlock_room(zkmuc_ctx_t *ctx)
{
	xmpp_stanza_t *iq = xmpp_stanza_new(_xmpp_ctx);
	char id[128];
	xmpp_ua_get_unique_string(ctx->ua, id);

	xmpp_stanza_set_name(iq, "iq");
	xmpp_stanza_set_id(iq, id);
	xmpp_stanza_set_type(iq, "set");
	xmpp_stanza_set_attribute(iq, "to", ctx->room_id);

	xmpp_stanza_t *query = xmpp_stanza_new(_xmpp_ctx);
	xmpp_stanza_set_name(query, "query");
	xmpp_stanza_set_ns(query, XMPP_NS_MUC_OWNER);

	xmpp_stanza_t *x = xmpp_stanza_new(_xmpp_ctx);
	xmpp_stanza_set_name(x, "x");
	xmpp_stanza_set_ns(x, XMPP_NS_X);
	xmpp_stanza_set_type(x, "submit");

	xmpp_stanza_t *field = xmpp_stanza_new(_xmpp_ctx);
	xmpp_stanza_set_name(field, "field");
	xmpp_stanza_set_attribute(field, "var", "muc#roomconfig_roomname");

	xmpp_stanza_t *stanza_value = xmpp_stanza_new(_xmpp_ctx);
	xmpp_stanza_set_name(stanza_value, "value");

	xmpp_stanza_t *stanza_text = xmpp_stanza_new(_xmpp_ctx);
	xmpp_stanza_set_text(stanza_text, ctx->room_desc);
	xmpp_stanza_add_child(stanza_value, stanza_text);
	xmpp_stanza_release(stanza_text);

	xmpp_stanza_add_child(field, stanza_value);
	xmpp_stanza_release(stanza_value);

	xmpp_stanza_add_child(x, field);
	xmpp_stanza_release(field);

	xmpp_stanza_add_child(query, x);
	xmpp_stanza_release(x);
	xmpp_stanza_add_child(iq, query);
	xmpp_stanza_release(query);

	xmpp_ua_id_handler_add(ctx->ua, zkmuc_unlock_room_result, id, ctx);
	xmpp_ua_send(ctx->ua, iq);
	xmpp_stanza_release(iq);
}
示例#18
0
文件: main.c 项目: jasjeetIM/PingMe
int presence_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata)
{
	xmpp_ctx_t *ctx = (xmpp_ctx_t*)userdata;

	if (!strcmp(xmpp_stanza_get_name(stanza), "presence"))
	{

		xmpp_stanza_t *message, *text, *body;


		char username[1024];
		char sendtext[1024];
		printf("Enter the username you would like to send this message to: ");
		scanf("%[^\n]", username);

		char c = getchar();

		printf("Type the message that you would like to send: ");
		scanf("%[^\n]", sendtext);
		message = xmpp_stanza_new(ctx);
		xmpp_stanza_set_name(message, "message");
		xmpp_stanza_set_type(message, "chat");
		xmpp_stanza_set_attribute(message, "to", username);


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

		text = xmpp_stanza_new(ctx);
		xmpp_stanza_set_text(text, sendtext);
		xmpp_stanza_add_child(body, text);
		xmpp_stanza_add_child(message, body);

		xmpp_send(conn, message);
		xmpp_stanza_release(message);

	}


}
示例#19
0
int xmpp_ua_mse_send_cmd(xmpp_ua_t *ua, const char *remote_jid, const char *cmd, const char *option, 
	xmpp_ua_mse_receive_rsp cb, void *userdata)
{
	int cmd_id = -1;
	if (cb)
		cmd_id = xmpp_ua_get_unique_id();
	xmpp_stanza_t *msg = xmpp_stanza_new(_xmpp_ctx);
	xmpp_stanza_set_name(msg, "message");
	xmpp_stanza_set_type(msg, "chat");
	xmpp_stanza_set_attribute(msg, "to", remote_jid);

	xmpp_stanza_t *body = xmpp_stanza_new(_xmpp_ctx);
	xmpp_stanza_set_name(body,"body");

	xmpp_stanza_t *text = xmpp_stanza_new(_xmpp_ctx);
	char *option2;
	xmpp_ua_change_string(option, &option2);
	char *msg_text = (char *)malloc(32+strlen(cmd)+strlen(option2));
	sprintf(msg_text, "ReqCmd|%d|%s|%s", cmd_id, cmd, option2);
	xmpp_stanza_set_text(text, msg_text);
	free(option2);
	free(msg_text);
	
	xmpp_stanza_add_child(body, text);
	xmpp_stanza_release(text);
	xmpp_stanza_add_child(msg, body);
	xmpp_stanza_release(body);

	if (cb)
	{
		mse_cmd_data data = {cb, userdata};
		WaitForSingleObject(ua->mutex_4_ua, INFINITE);
		ua->mse_map[cmd_id] = data;
		ReleaseMutex(ua->mutex_4_ua);
	}
	int rc = xmpp_ua_send(ua, msg);
	xmpp_stanza_release(msg);
	return rc;
}
示例#20
0
int zkmuc_invite(zkmuc_ctx_t *ctx, const char *remote_id)
{
	xmpp_stanza_t *stanza_msg = xmpp_stanza_new(_xmpp_ctx);
	xmpp_stanza_set_name(stanza_msg, "message");
	xmpp_stanza_set_attribute(stanza_msg, "to", remote_id);
	xmpp_stanza_set_ns(stanza_msg, XMPP_NS_CONFERECE);

	xmpp_stanza_t *stanza_body = xmpp_stanza_new(_xmpp_ctx);
	xmpp_stanza_set_name(stanza_body, "body");

	xmpp_stanza_t *stanza_txt = xmpp_stanza_new(_xmpp_ctx);
	xmpp_stanza_set_text(stanza_txt, ctx->room_id);

	xmpp_stanza_add_child(stanza_body,stanza_txt);
	xmpp_stanza_release(stanza_txt);
	xmpp_stanza_add_child(stanza_msg, stanza_body);
	xmpp_stanza_release(stanza_body);

	xmpp_ua_send(ctx->ua, stanza_msg);
	xmpp_stanza_release(stanza_msg);
	return 0;
}
示例#21
0
static void add_form_values(xmpp_ctx_t* ctx, xmpp_stanza_t *parent,
                            const char *key, const char **values)
{
    xmpp_stanza_t* field = xmpp_stanza_new(ctx);
    assert(field);

    xmpp_stanza_set_name(field, "field");
    xmpp_stanza_set_attribute(field, "var", key);
    add_and_release(parent, field);

    for (int i = 0; values[i]; i++) {
        xmpp_stanza_t* value = xmpp_stanza_new(ctx);
        xmpp_stanza_t* text = xmpp_stanza_new(ctx);
        assert(value);
        assert(text);

        xmpp_stanza_set_name(value, "value");
        add_and_release(field, value);

        xmpp_stanza_set_text(text, values[i]);
        add_and_release(value, text);
    }
}
示例#22
0
static gboolean
_bookmark_add(const char *jid, const char *nick, gboolean autojoin)
{
    gboolean added = TRUE;
    if (autocomplete_contains(bookmark_ac, jid)) {
        added = FALSE;
    }

    xmpp_conn_t *conn = connection_get_conn();
    xmpp_ctx_t *ctx = connection_get_ctx();

    /* TODO: send request */
    xmpp_stanza_t *stanza = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(stanza, STANZA_NAME_IQ);
    char *id = generate_unique_id("bookmark_add");
    xmpp_stanza_set_id(stanza, id);
    xmpp_stanza_set_type(stanza, STANZA_TYPE_SET);

    xmpp_stanza_t *pubsub = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(pubsub, STANZA_NAME_PUBSUB);
    xmpp_stanza_set_ns(pubsub, STANZA_NS_PUBSUB);
    xmpp_stanza_add_child(stanza, pubsub);

    xmpp_stanza_t *publish = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(publish, STANZA_NAME_PUBLISH);
    xmpp_stanza_set_attribute(publish, STANZA_ATTR_NODE, "storage:bookmarks");
    xmpp_stanza_add_child(pubsub, publish);

    xmpp_stanza_t *item = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(item, STANZA_NAME_ITEM);
    xmpp_stanza_add_child(publish, item);

    xmpp_stanza_t *storage = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(storage, STANZA_NAME_STORAGE);
    xmpp_stanza_set_ns(storage, "storage;bookmarks");
    xmpp_stanza_add_child(item, storage);

    xmpp_stanza_t *conference = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(conference, STANZA_NAME_CONFERENCE);
    xmpp_stanza_set_attribute(conference, STANZA_ATTR_JID, jid);

    if (autojoin) {
        xmpp_stanza_set_attribute(conference, STANZA_ATTR_AUTOJOIN, "true");
    } else {
        xmpp_stanza_set_attribute(conference, STANZA_ATTR_AUTOJOIN, "false");
    }

    xmpp_stanza_add_child(storage, conference);

    if (nick != NULL) {
        xmpp_stanza_t *nick_st = xmpp_stanza_new(ctx);
        xmpp_stanza_set_name(nick_st, STANZA_NAME_NICK);
        xmpp_stanza_set_text(nick_st, nick);
        xmpp_stanza_add_child(conference, nick_st);
    }

    xmpp_stanza_t *publish_options = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(publish_options, STANZA_NAME_PUBLISH_OPTIONS);
    xmpp_stanza_add_child(pubsub, publish_options);

    xmpp_stanza_t *x = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(x, STANZA_NAME_X);
    xmpp_stanza_set_ns(x, STANZA_NS_DATA);
    xmpp_stanza_set_attribute(x, STANZA_ATTR_TYPE, "submit");
    xmpp_stanza_add_child(publish_options, x);

    xmpp_stanza_t *form_type = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(form_type, STANZA_NAME_FIELD);
    xmpp_stanza_set_attribute(form_type, STANZA_ATTR_VAR, "FORM_TYPE");
    xmpp_stanza_set_attribute(form_type, STANZA_ATTR_TYPE, "hidden");
    xmpp_stanza_t *form_type_value = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(form_type_value, STANZA_NAME_VALUE);
    xmpp_stanza_t *form_type_value_text = xmpp_stanza_new(ctx);
    xmpp_stanza_set_text(form_type_value_text, "http://jabber.org/protocol/pubsub#publish-options");
    xmpp_stanza_add_child(form_type_value, form_type_value_text);
    xmpp_stanza_add_child(form_type, form_type_value);
    xmpp_stanza_add_child(x, form_type);

    xmpp_stanza_t *persist_items = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(persist_items, STANZA_NAME_FIELD);
    xmpp_stanza_set_attribute(persist_items, STANZA_ATTR_VAR, "pubsub#persist_items");
    xmpp_stanza_t *persist_items_value = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(persist_items_value, STANZA_NAME_VALUE);
    xmpp_stanza_t *persist_items_value_text = xmpp_stanza_new(ctx);
    xmpp_stanza_set_text(persist_items_value_text, "true");
    xmpp_stanza_add_child(persist_items_value, persist_items_value_text);
    xmpp_stanza_add_child(persist_items, persist_items_value);
    xmpp_stanza_add_child(x, persist_items);

    xmpp_stanza_t *access_model = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(access_model, STANZA_NAME_FIELD);
    xmpp_stanza_set_attribute(access_model, STANZA_ATTR_VAR, "pubsub#access_model");
    xmpp_stanza_t *access_model_value = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(access_model_value, STANZA_NAME_VALUE);
    xmpp_stanza_t *access_model_value_text = xmpp_stanza_new(ctx);
    xmpp_stanza_set_text(access_model_value_text, "whitelist");
    xmpp_stanza_add_child(access_model_value, access_model_value_text);
    xmpp_stanza_add_child(access_model, access_model_value);
    xmpp_stanza_add_child(x, access_model);

    xmpp_send(conn, stanza);
    xmpp_stanza_release(stanza);

    /* TODO: manage bookmark_list */

    /* this may be command for modifying */
    autocomplete_remove(bookmark_ac, jid);
    autocomplete_add(bookmark_ac, jid);

    return added;
}
示例#23
0
static void
_send_bookmarks(void)
{
    xmpp_conn_t *conn = connection_get_conn();
    xmpp_ctx_t *ctx = connection_get_ctx();

    xmpp_stanza_t *iq = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(iq, STANZA_NAME_IQ);
    char *id = create_unique_id("bookmarks_update");
    xmpp_stanza_set_id(iq, id);
    free(id);
    xmpp_stanza_set_type(iq, STANZA_TYPE_SET);

    xmpp_stanza_t *query = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(query, STANZA_NAME_QUERY);
    xmpp_stanza_set_ns(query, "jabber:iq:private");

    xmpp_stanza_t *storage = xmpp_stanza_new(ctx);
    xmpp_stanza_set_name(storage, STANZA_NAME_STORAGE);
    xmpp_stanza_set_ns(storage, "storage:bookmarks");

    GList *curr = bookmark_list;
    while (curr) {
        Bookmark *bookmark = curr->data;
        xmpp_stanza_t *conference = xmpp_stanza_new(ctx);
        xmpp_stanza_set_name(conference, STANZA_NAME_CONFERENCE);
        xmpp_stanza_set_attribute(conference, STANZA_ATTR_JID, bookmark->jid);

        Jid *jidp = jid_create(bookmark->jid);
        if (jidp->localpart) {
            xmpp_stanza_set_attribute(conference, STANZA_ATTR_NAME, jidp->localpart);
        }
        jid_destroy(jidp);

        if (bookmark->autojoin) {
            xmpp_stanza_set_attribute(conference, STANZA_ATTR_AUTOJOIN, "true");
        } else {
            xmpp_stanza_set_attribute(conference, STANZA_ATTR_AUTOJOIN, "false");
        }

        if (bookmark->nick) {
            xmpp_stanza_t *nick_st = xmpp_stanza_new(ctx);
            xmpp_stanza_set_name(nick_st, STANZA_NAME_NICK);
            xmpp_stanza_t *nick_text = xmpp_stanza_new(ctx);
            xmpp_stanza_set_text(nick_text, bookmark->nick);
            xmpp_stanza_add_child(nick_st, nick_text);
            xmpp_stanza_add_child(conference, nick_st);

            xmpp_stanza_release(nick_text);
            xmpp_stanza_release(nick_st);
        }

        if (bookmark->password) {
            xmpp_stanza_t *password_st = xmpp_stanza_new(ctx);
            xmpp_stanza_set_name(password_st, STANZA_NAME_PASSWORD);
            xmpp_stanza_t *password_text = xmpp_stanza_new(ctx);
            xmpp_stanza_set_text(password_text, bookmark->password);
            xmpp_stanza_add_child(password_st, password_text);
            xmpp_stanza_add_child(conference, password_st);

            xmpp_stanza_release(password_text);
            xmpp_stanza_release(password_st);
        }

        xmpp_stanza_add_child(storage, conference);
        xmpp_stanza_release(conference);

        curr = curr->next;
    }

    xmpp_stanza_add_child(query, storage);
    xmpp_stanza_add_child(iq, query);
    xmpp_stanza_release(storage);
    xmpp_stanza_release(query);

    xmpp_send(conn, iq);
    xmpp_stanza_release(iq);
}
示例#24
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;
}
示例#25
0
static int _handle_features_sasl(xmpp_conn_t * const conn,
				 xmpp_stanza_t * const stanza,
				 void * const userdata)
{
    xmpp_stanza_t *bind, *session, *iq, *res, *text;
    char *resource;

    /* remove missing features handler */
    xmpp_timed_handler_delete(conn, _handle_missing_features_sasl);

    /* we are expecting <bind/> and <session/> since this is a
       XMPP style connection */

    bind = xmpp_stanza_get_child_by_name(stanza, "bind");
    if (bind && strcmp(xmpp_stanza_get_ns(bind), XMPP_NS_BIND) == 0) {
	/* resource binding is required */
	conn->bind_required = 1;
    }

    session = xmpp_stanza_get_child_by_name(stanza, "session");
    if (session && strcmp(xmpp_stanza_get_ns(session), XMPP_NS_SESSION) == 0) {
	/* session establishment required */
	conn->session_required = 1;
    }

    /* if bind is required, go ahead and start it */
    if (conn->bind_required) {
	/* bind resource */

	/* setup response handlers */
	handler_add_id(conn, _handle_bind, "_xmpp_bind1", NULL);
	handler_add_timed(conn, _handle_missing_bind,
			  BIND_TIMEOUT, NULL);

	/* send bind 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_bind1");

	bind = xmpp_stanza_copy(bind);
	if (!bind) {
	    xmpp_stanza_release(iq);
	    disconnect_mem_error(conn);
	    return 0;
	}

	/* request a specific resource if we have one */
        resource = xmpp_jid_resource(conn->ctx, conn->jid);
	if ((resource != NULL) && (strlen(resource) == 0)) {
	    /* jabberd2 doesn't handle an empty resource */
	    xmpp_free(conn->ctx, resource);
	    resource = NULL;
	}

	/* if we have a resource to request, do it. otherwise the
	   server will assign us one */
	if (resource) {
	    res = xmpp_stanza_new(conn->ctx);
	    if (!res) {
		xmpp_stanza_release(bind);
		xmpp_stanza_release(iq);
		disconnect_mem_error(conn);
		return 0;
	    }
	    xmpp_stanza_set_name(res, "resource");
	    text = xmpp_stanza_new(conn->ctx);
	    if (!text) {
		xmpp_stanza_release(res);
		xmpp_stanza_release(bind);
		xmpp_stanza_release(iq);
		disconnect_mem_error(conn);
		return 0;
	    }
	    xmpp_stanza_set_text(text, resource);
	    xmpp_stanza_add_child(res, text);
            xmpp_stanza_release(text);
	    xmpp_stanza_add_child(bind, res);
            xmpp_stanza_release(res);
	    xmpp_free(conn->ctx, resource);
	}

	xmpp_stanza_add_child(iq, bind);
	xmpp_stanza_release(bind);

	/* send bind request */
	xmpp_send(conn, iq);
	xmpp_stanza_release(iq);
    } else {
	/* can't bind, disconnect */
	xmpp_error(conn->ctx, "xmpp", "Stream features does not allow "\
		   "resource bind.");
	xmpp_disconnect(conn);
    }

    return 0;
}
示例#26
0
/* authenticate the connection
 * this may get called multiple times.  if any auth method fails,
 * this will get called again until one auth method succeeds or every
 * method fails
 */
static void _auth(xmpp_conn_t * const conn)
{
    xmpp_stanza_t *auth, *authdata, *query, *child, *iq;
    char *str, *authid;
    char *scram_init;
    int anonjid;

    /* if there is no node in conn->jid, we assume anonymous connect */
    str = xmpp_jid_node(conn->ctx, conn->jid);
    if (str == NULL) {
	anonjid = 1;
    } else {
	xmpp_free(conn->ctx, str);
	anonjid = 0;
    }

    if (conn->tls_support)
    {
	tls_t *tls = tls_new(conn->ctx, conn->sock);

	/* If we couldn't init tls, it isn't there, so go on */
	if (!tls)
	{
	    conn->tls_support = 0;
	    _auth(conn);
	    return;
	}
	else
	{
	    tls_free(tls);
	}

	auth = _make_starttls(conn);

	if (!auth) {
	    disconnect_mem_error(conn);
	    return;
	}

	handler_add(conn, _handle_proceedtls_default,
		    XMPP_NS_TLS, NULL, NULL, NULL);

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

	/* TLS was tried, unset flag */
	conn->tls_support = 0;
    } else if (anonjid && conn->sasl_support & SASL_MASK_ANONYMOUS) {
	/* some crap here */
	auth = _make_sasl_auth(conn, "ANONYMOUS");
	if (!auth) {
	    disconnect_mem_error(conn);
	    return;
	}

	handler_add(conn, _handle_sasl_result, XMPP_NS_SASL,
	            NULL, NULL, "ANONYMOUS");

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

	/* SASL ANONYMOUS was tried, unset flag */
	conn->sasl_support &= ~SASL_MASK_ANONYMOUS;
    } else if (anonjid) {
	xmpp_error(conn->ctx, "auth",
		   "No node in JID, and SASL ANONYMOUS unsupported.");
	xmpp_disconnect(conn);
    } else if (conn->sasl_support & SASL_MASK_SCRAMSHA1) {
        auth = _make_sasl_auth(conn, "SCRAM-SHA-1");
        if (!auth) {
            disconnect_mem_error(conn);
            return;
        }

        /* don't free scram_init on success */
        scram_init = _make_scram_sha1_init_msg(conn);
        if (!scram_init) {
            xmpp_stanza_release(auth);
            disconnect_mem_error(conn);
            return;
        }

        str = (char *)base64_encode(conn->ctx, (unsigned char *)scram_init,
                                    strlen(scram_init));
        if (!str) {
            xmpp_free(conn->ctx, scram_init);
            xmpp_stanza_release(auth);
            disconnect_mem_error(conn);
            return;
        }

        authdata = xmpp_stanza_new(conn->ctx);
        if (!authdata) {
            xmpp_free(conn->ctx, str);
            xmpp_free(conn->ctx, scram_init);
            xmpp_stanza_release(auth);
            disconnect_mem_error(conn);
            return;
        }
        xmpp_stanza_set_text(authdata, str);
        xmpp_free(conn->ctx, str);
        xmpp_stanza_add_child(auth, authdata);
        xmpp_stanza_release(authdata);

        handler_add(conn, _handle_scram_sha1_challenge,
                    XMPP_NS_SASL, NULL, NULL, (void *)scram_init);

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

        /* SASL SCRAM-SHA-1 was tried, unset flag */
        conn->sasl_support &= ~SASL_MASK_SCRAMSHA1;
    } else if (conn->sasl_support & SASL_MASK_DIGESTMD5) {
	auth = _make_sasl_auth(conn, "DIGEST-MD5");
	if (!auth) {
	    disconnect_mem_error(conn);
	    return;

	}

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

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

	/* SASL DIGEST-MD5 was tried, unset flag */
	conn->sasl_support &= ~SASL_MASK_DIGESTMD5;
    } else if (conn->sasl_support & SASL_MASK_PLAIN) {
	auth = _make_sasl_auth(conn, "PLAIN");
	if (!auth) {
	    disconnect_mem_error(conn);
	    return;
	}
	authdata = xmpp_stanza_new(conn->ctx);
	if (!authdata) {
	    disconnect_mem_error(conn);
	    return;
	}
	authid = _get_authid(conn);
	if (!authid) {
	    disconnect_mem_error(conn);
	    return;
	}
	str = sasl_plain(conn->ctx, authid, conn->pass);
	if (!str) {
	    disconnect_mem_error(conn);
	    return;
	}
	xmpp_stanza_set_text(authdata, str);
	xmpp_free(conn->ctx, str);
	xmpp_free(conn->ctx, authid);

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

	handler_add(conn, _handle_sasl_result,
		    XMPP_NS_SASL, NULL, NULL, "PLAIN");

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

	/* SASL PLAIN was tried */
	conn->sasl_support &= ~SASL_MASK_PLAIN;
    } else if (conn->type == XMPP_CLIENT) {
	/* legacy client authentication */

	iq = xmpp_stanza_new(conn->ctx);
	if (!iq) {
	    disconnect_mem_error(conn);
	    return;
	}
	xmpp_stanza_set_name(iq, "iq");
	xmpp_stanza_set_type(iq, "set");
	xmpp_stanza_set_id(iq, "_xmpp_auth1");

	query = xmpp_stanza_new(conn->ctx);
	if (!query) {
	    xmpp_stanza_release(iq);
	    disconnect_mem_error(conn);
	    return;
	}
	xmpp_stanza_set_name(query, "query");
	xmpp_stanza_set_ns(query, XMPP_NS_AUTH);
	xmpp_stanza_add_child(iq, query);
	xmpp_stanza_release(query);

	child = xmpp_stanza_new(conn->ctx);
	if (!child) {
	    xmpp_stanza_release(iq);
	    disconnect_mem_error(conn);
	    return;
	}
	xmpp_stanza_set_name(child, "username");
	xmpp_stanza_add_child(query, child);
	xmpp_stanza_release(child);

	authdata = xmpp_stanza_new(conn->ctx);
	if (!authdata) {
	    xmpp_stanza_release(iq);
	    disconnect_mem_error(conn);
	    return;
	}
	str = xmpp_jid_node(conn->ctx, conn->jid);
	xmpp_stanza_set_text(authdata, str);
	xmpp_free(conn->ctx, str);
	xmpp_stanza_add_child(child, authdata);
	xmpp_stanza_release(authdata);

	child = xmpp_stanza_new(conn->ctx);
	if (!child) {
	    xmpp_stanza_release(iq);
	    disconnect_mem_error(conn);
	    return;
	}
	xmpp_stanza_set_name(child, "password");
	xmpp_stanza_add_child(query, child);
	xmpp_stanza_release(child);

	authdata = xmpp_stanza_new(conn->ctx);
	if (!authdata) {
	    xmpp_stanza_release(iq);
	    disconnect_mem_error(conn);
	    return;
	}
	xmpp_stanza_set_text(authdata, conn->pass);
	xmpp_stanza_add_child(child, authdata);
	xmpp_stanza_release(authdata);

	child = xmpp_stanza_new(conn->ctx);
	if (!child) {
	    xmpp_stanza_release(iq);
	    disconnect_mem_error(conn);
	    return;
	}
	xmpp_stanza_set_name(child, "resource");
	xmpp_stanza_add_child(query, child);
	xmpp_stanza_release(child);

	authdata = xmpp_stanza_new(conn->ctx);
	if (!authdata) {
	    xmpp_stanza_release(iq);
	    disconnect_mem_error(conn);
	    return;
	}
	str = xmpp_jid_resource(conn->ctx, conn->jid);
	if (str) {
	    xmpp_stanza_set_text(authdata, str);
	    xmpp_free(conn->ctx, str);
	} else {
	    xmpp_stanza_release(authdata);
	    xmpp_stanza_release(iq);
	    xmpp_error(conn->ctx, "auth",
		       "Cannot authenticate without resource");
	    xmpp_disconnect(conn);
	    return;
	}
	xmpp_stanza_add_child(child, authdata);
	xmpp_stanza_release(authdata);

	handler_add_id(conn, _handle_legacy, "_xmpp_auth1", NULL);
	handler_add_timed(conn, _handle_missing_legacy,
			  LEGACY_TIMEOUT, NULL);

	xmpp_send(conn, iq);
	xmpp_stanza_release(iq);
    }
}
示例#27
0
/* handle the challenge phase of SCRAM-SHA-1 auth */
static int _handle_scram_sha1_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;
    char *challenge;
    char *scram_init = (char *)userdata;

    name = xmpp_stanza_get_name(stanza);
    xmpp_debug(conn->ctx, "xmpp",
               "handle SCRAM-SHA-1 (challenge) called for %s", name);

    if (strcmp(name, "challenge") == 0) {
        text = xmpp_stanza_get_text(stanza);
        if (!text)
            goto err;

        challenge = (char *)base64_decode(conn->ctx, text, strlen(text));
        xmpp_free(conn->ctx, text);
        if (!challenge)
            goto err;

        response = sasl_scram_sha1(conn->ctx, challenge, scram_init,
                                   conn->jid, conn->pass);
        xmpp_free(conn->ctx, challenge);
        if (!response)
            goto err;

        auth = xmpp_stanza_new(conn->ctx);
        if (!auth)
            goto err_free_response;
        xmpp_stanza_set_name(auth, "response");
        xmpp_stanza_set_ns(auth, XMPP_NS_SASL);

        authdata = xmpp_stanza_new(conn->ctx);
        if (!authdata)
            goto err_release_auth;
        xmpp_stanza_set_text(authdata, response);
        xmpp_free(conn->ctx, response);

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

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

    } else {
        xmpp_free(conn->ctx, scram_init);
        return _handle_sasl_result(conn, stanza, "SCRAM-SHA-1");
    }

    return 1;

err_release_auth:
    xmpp_stanza_release(auth);
err_free_response:
    xmpp_free(conn->ctx, response);
err:
    xmpp_free(conn->ctx, scram_init);
    disconnect_mem_error(conn);
    return 0;
}
示例#28
0
文件: auth.c 项目: catap/libstrophe
/** registration at connections
 *
 *  @param conn a Strophe connection object
 */
static void _register(xmpp_conn_t * const conn)
{
    char *str;

    xmpp_stanza_t *iq, *q, *child, *regdata;

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

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

    q = xmpp_stanza_new(conn->ctx);
    if (!q) {
	xmpp_stanza_release(iq);
	disconnect_mem_error(conn);
	return;
    }
    xmpp_stanza_set_name(q, "query");
    xmpp_stanza_set_ns(q, XMPP_NS_REGISTER);
    xmpp_stanza_add_child(iq, q);
    xmpp_stanza_release(q);

    child = xmpp_stanza_new(conn->ctx);
    if (!child) {
	xmpp_stanza_release(iq);
	disconnect_mem_error(conn);
	return;
    }
    xmpp_stanza_set_name(child, "registered");
    xmpp_stanza_add_child(q, child);
    xmpp_stanza_release(child);

    child = xmpp_stanza_new(conn->ctx);
    if (!child) {
	xmpp_stanza_release(iq);
	disconnect_mem_error(conn);
	return;
    }
    xmpp_stanza_set_name(child, "username");
    xmpp_stanza_add_child(q, child);
    xmpp_stanza_release(child);

    regdata = xmpp_stanza_new(conn->ctx);
    if (!regdata) {
	xmpp_stanza_release(iq);
	disconnect_mem_error(conn);
	return;
    }
    str = xmpp_jid_node(conn->ctx, conn->jid);
    xmpp_stanza_set_text(regdata, str);
    xmpp_free(conn->ctx, str);
    xmpp_stanza_add_child(child, regdata);
    xmpp_stanza_release(regdata);

    child = xmpp_stanza_new(conn->ctx);
    if (!child) {
	xmpp_stanza_release(iq);
	disconnect_mem_error(conn);
	return;
    }
    xmpp_stanza_set_name(child, "password");
    xmpp_stanza_add_child(q, child);
    xmpp_stanza_release(child);

    regdata = xmpp_stanza_new(conn->ctx);
    if (!regdata) {
	xmpp_stanza_release(iq);
	disconnect_mem_error(conn);
	return;
    }
    xmpp_stanza_set_text(regdata, conn->pass);
    xmpp_stanza_add_child(child, regdata);
    xmpp_stanza_release(regdata);

    child = xmpp_stanza_new(conn->ctx);
    if (!child) {
	xmpp_stanza_release(iq);
	disconnect_mem_error(conn);
	return;
    }
    xmpp_stanza_set_name(child, "email");
    xmpp_stanza_add_child(q, child);
    xmpp_stanza_release(child);

    regdata = xmpp_stanza_new(conn->ctx);
    if (!regdata) {
	xmpp_stanza_release(iq);
	disconnect_mem_error(conn);
	return;
    }
    str = xmpp_jid_node(conn->ctx, conn->jid);
    xmpp_stanza_set_text(regdata, str);
    xmpp_free(conn->ctx, str);
    xmpp_stanza_add_child(child, regdata);
    xmpp_stanza_release(regdata);

    handler_add_id(conn, _handle_register, "_xmpp_reg1", NULL);
    handler_add_timed(conn, _handle_missing_register, 
		      LAZY_REGISTRATION_TIMEOUT, NULL);

    xmpp_send(conn, iq);
    xmpp_stanza_release(iq);
}
示例#29
0
/** Create an <stream:error/> stanza object with given type and error text.
 *  The error text is optional and may be NULL.
 *
 *  @param ctx a Strophe context object
 *  @param type enum of xmpp_error_type_t
 *  @param text content of a 'text'
 *
 *  @return a new Strophe stanza object
 *
 *  @todo Handle errors in this function
 *
 *  @ingroup Stanza
 */
xmpp_stanza_t *xmpp_error_new(xmpp_ctx_t *ctx, xmpp_error_type_t const type,
                              const char * const text)
{
    xmpp_stanza_t *error = _stanza_new_with_attrs(ctx, "stream:error", NULL, NULL, NULL);
    xmpp_stanza_t *error_type = xmpp_stanza_new(ctx);

    switch(type) {
        case XMPP_SE_BAD_FORMAT:
            xmpp_stanza_set_name(error_type, "bad-format");
            break;
        case XMPP_SE_BAD_NS_PREFIX:
            xmpp_stanza_set_name(error_type, "bad-namespace-prefix");
            break;
        case XMPP_SE_CONFLICT:
            xmpp_stanza_set_name(error_type, "conflict");
            break;
        case XMPP_SE_CONN_TIMEOUT:
            xmpp_stanza_set_name(error_type, "connection-timeout");
            break;
        case XMPP_SE_HOST_GONE:
            xmpp_stanza_set_name(error_type, "host-gone");
            break;
        case XMPP_SE_HOST_UNKNOWN:
            xmpp_stanza_set_name(error_type, "host-unknown");
            break;
        case XMPP_SE_IMPROPER_ADDR:
            xmpp_stanza_set_name(error_type, "improper-addressing");
            break;
        case XMPP_SE_INTERNAL_SERVER_ERROR:
            xmpp_stanza_set_name(error_type, "internal-server-error");
            break;
        case XMPP_SE_INVALID_FROM:
            xmpp_stanza_set_name(error_type, "invalid-from");
            break;
        case XMPP_SE_INVALID_ID:
            xmpp_stanza_set_name(error_type, "invalid-id");
            break;
        case XMPP_SE_INVALID_NS:
            xmpp_stanza_set_name(error_type, "invalid-namespace");
            break;
        case XMPP_SE_INVALID_XML:
            xmpp_stanza_set_name(error_type, "invalid-xml");
            break;
        case XMPP_SE_NOT_AUTHORIZED:
            xmpp_stanza_set_name(error_type, "not-authorized");
            break;
        case XMPP_SE_POLICY_VIOLATION:
            xmpp_stanza_set_name(error_type, "policy-violation");
            break;
        case XMPP_SE_REMOTE_CONN_FAILED:
            xmpp_stanza_set_name(error_type, "remote-connection-failed");
            break;
        case XMPP_SE_RESOURCE_CONSTRAINT:
            xmpp_stanza_set_name(error_type, "resource-constraint");
            break;
        case XMPP_SE_RESTRICTED_XML:
            xmpp_stanza_set_name(error_type, "restricted-xml");
            break;
        case XMPP_SE_SEE_OTHER_HOST:
            xmpp_stanza_set_name(error_type, "see-other-host");
            break;
        case XMPP_SE_SYSTEM_SHUTDOWN:
            xmpp_stanza_set_name(error_type, "system-shutdown");
            break;
        case XMPP_SE_UNDEFINED_CONDITION:
            xmpp_stanza_set_name(error_type, "undefined-condition");
            break;
        case XMPP_SE_UNSUPPORTED_ENCODING:
            xmpp_stanza_set_name(error_type, "unsupported-encoding");
            break;
        case XMPP_SE_UNSUPPORTED_STANZA_TYPE:
            xmpp_stanza_set_name(error_type, "unsupported-stanza-type");
            break;
        case XMPP_SE_UNSUPPORTED_VERSION:
            xmpp_stanza_set_name(error_type, "unsupported-version");
            break;
        case XMPP_SE_XML_NOT_WELL_FORMED:
            xmpp_stanza_set_name(error_type, "xml-not-well-formed");
            break;
        default:
            xmpp_stanza_set_name(error_type, "internal-server-error");
            break;
    }

    xmpp_stanza_set_ns(error_type, XMPP_NS_STREAMS_IETF);
    xmpp_stanza_add_child(error, error_type);
    xmpp_stanza_release(error_type);

    if (text) {
        xmpp_stanza_t *error_text = xmpp_stanza_new(ctx);
        xmpp_stanza_t *content = xmpp_stanza_new(ctx);

        xmpp_stanza_set_name(error_text, "text");
        xmpp_stanza_set_ns(error_text, XMPP_NS_STREAMS_IETF);

        xmpp_stanza_set_text(content, text);
        xmpp_stanza_add_child(error_text, content);
        xmpp_stanza_release(content);

        xmpp_stanza_add_child(error, error_text);
        xmpp_stanza_release(error_text);
    }

    return error;
}
示例#30
0
/**
 * Handles the service
 */
xmpp_stanza_t * _HandleServiceRequest(xmpp_conn_t * const conn,
		xmpp_stanza_t * const stanza, void * const userdata) {
	xmpp_stanza_t *reply;
	xmpp_stanza_t *wd, *nodetext;
	xmpp_ctx_t *ctx = (xmpp_ctx_t*) userdata;
	sdvp_method_t methodType = SDVP_METHOD_UNDEFINED;
	sdvp_from_t* from;
	char *method;
	sdvp_reply_params_t* sdvpReplyParams = NULL;
    void* replyParams;
	void* paramStructure = NULL;
	char strbuf[20];
	static unsigned int watchdog = 1;

	//HJP: Hvorfor oprette reply her, når den næsten altid bliver overskrevet?
	reply = xmpp_stanza_new(ctx);
	xmpp_stanza_set_name(reply, "iq");

	from = _CreateFrom(xmpp_stanza_get_attribute(stanza, "from"), "TODO",
			"TODO");
	syslog(LOG_DEBUG, "Received a RPC Method call from %s\n", from->name);

	method = _GetRpcMethode(stanza);
	methodType = _GetDefinedMetodeFromMethod(method);

	syslog(LOG_DEBUG, "XML-RPC method name %s\n", method);

	free(method);

	if (methodType == SDVP_METHOD_UNDEFINED) {
		syslog(LOG_DEBUG, "Undefined method type\n");

		sdvp_InitiateReplyParameters(&sdvpReplyParams, 1);
		sdvpReplyParams->params[0].strValue =
				strdup("ERROR: Method is undefined. Methods are CasE SentItive.\n");
		sdvpReplyParams->params[0].type = IEC_VISIBLE_STRING;
		//HJP: Jeg releaser reply her, det ville være smartere kun at oprette den når nødvendigt...
		xmpp_stanza_release(reply);
        reply = _CreateReply(ctx, methodType, sdvpReplyParams);
		xmpp_stanza_set_type(reply, "error");
        sdvp_FreeReplyParameters(sdvpReplyParams);
	} else if (methodType == LIAB_WATCHDOG) {
		xmpp_stanza_release(reply);
		reply = xmpp_stanza_new(ctx);
		xmpp_stanza_set_name(reply, "query");
		xmpp_stanza_set_ns(reply, "jabber:iq:rpc");

		wd = xmpp_stanza_new(ctx);
		xmpp_stanza_set_name(wd, "watchdog");
		xmpp_stanza_add_child(reply, wd);
		xmpp_stanza_release(wd);

		nodetext = xmpp_stanza_new(ctx);
		sprintf(strbuf, "%u", watchdog++);
		xmpp_stanza_set_text(nodetext, strbuf);
		xmpp_stanza_add_child(wd, nodetext);
		xmpp_stanza_release(nodetext);

	} else{
		if (_CallbackIsRegistered(methodType)) {
			paramStructure = _CreateParamStructureFromParameters(methodType, stanza);
			replyParams = _CallTheRightCallback(methodType, paramStructure);
			_FreeParamStructure(methodType,paramStructure);
			xmpp_stanza_release(reply);
            reply = _CreateReply(ctx, methodType, replyParams);

			sdvp_FreeCallbackReply(methodType, replyParams);
        	sdvp_FreeReplyParameters(sdvpReplyParams);
		} else {
			// TODO: Send a error with method not implemented or alike
		}
	}

	_FreeFrom(from);


	return reply;
}