1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Added function to create message stanzas

This commit is contained in:
James Booth 2012-11-08 23:31:21 +00:00
parent dac4cf3c3c
commit 29b5abfe06
3 changed files with 52 additions and 37 deletions

View File

@ -144,44 +144,26 @@ jabber_process_events(void)
void
jabber_send(const char * const msg, const char * const recipient)
{
char *encoded_xml = encode_xml(msg);
if (prefs_get_states()) {
if (!chat_session_exists(recipient)) {
chat_session_start(recipient, TRUE);
}
}
char *encoded_xml = encode_xml(msg);
xmpp_stanza_t *reply, *body, *text, *active;
reply = xmpp_stanza_new(jabber_conn.ctx);
xmpp_stanza_set_name(reply, "message");
xmpp_stanza_set_type(reply, "chat");
xmpp_stanza_set_attribute(reply, "to", recipient);
body = xmpp_stanza_new(jabber_conn.ctx);
xmpp_stanza_set_name(body, "body");
text = xmpp_stanza_new(jabber_conn.ctx);
xmpp_stanza_set_text(text, encoded_xml);
if (prefs_get_states()) {
// always send <active/> with messages when recipient supports chat states
if (chat_session_get_recipient_supports(recipient)) {
xmpp_stanza_t *message;
if (prefs_get_states() && chat_session_get_recipient_supports(recipient)) {
chat_session_set_active(recipient);
active = xmpp_stanza_new(jabber_conn.ctx);
xmpp_stanza_set_name(active, "active");
xmpp_stanza_set_ns(active, "http://jabber.org/protocol/chatstates");
xmpp_stanza_add_child(reply, active);
}
message = stanza_create_message(jabber_conn.ctx, recipient, "chat",
encoded_xml, "active");
} else {
message = stanza_create_message(jabber_conn.ctx, recipient, "chat",
encoded_xml, NULL);
}
xmpp_stanza_add_child(body, text);
xmpp_stanza_add_child(reply, body);
xmpp_send(jabber_conn.conn, reply);
xmpp_stanza_release(reply);
xmpp_send(jabber_conn.conn, message);
xmpp_stanza_release(message);
free(encoded_xml);
}

View File

@ -26,18 +26,48 @@ xmpp_stanza_t *
stanza_create_chat_state(xmpp_ctx_t *ctx, const char * const recipient,
const char * const state)
{
xmpp_stanza_t *message, *chat_state;
xmpp_stanza_t *msg, *chat_state;
message = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(message, "message");
xmpp_stanza_set_type(message, "chat");
xmpp_stanza_set_attribute(message, "to", recipient);
msg = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(msg, "message");
xmpp_stanza_set_type(msg, "chat");
xmpp_stanza_set_attribute(msg, "to", recipient);
chat_state = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(chat_state, state);
xmpp_stanza_set_ns(chat_state, "http://jabber.org/protocol/chatstates");
xmpp_stanza_add_child(message, chat_state);
xmpp_stanza_add_child(msg, chat_state);
return message;
return msg;
}
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)
{
xmpp_stanza_t *msg, *body, *text;
msg = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(msg, "message");
xmpp_stanza_set_type(msg, "chat");
xmpp_stanza_set_attribute(msg, "to", recipient);
body = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(body, "body");
text = xmpp_stanza_new(ctx);
xmpp_stanza_set_text(text, message);
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, "http://jabber.org/protocol/chatstates");
xmpp_stanza_add_child(msg, chat_state);
}
return msg;
}

View File

@ -28,4 +28,7 @@
xmpp_stanza_t* stanza_create_chat_state(xmpp_ctx_t *ctx,
const char * const recipient, const char * const state);
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);
#endif