From 0f01f30b9c6cefcb0b6e79e4e5f59652bf50eb73 Mon Sep 17 00:00:00 2001 From: James Booth Date: Tue, 29 Jan 2013 00:37:50 +0000 Subject: [PATCH] Moved message functions to xmpp_message.c --- src/command.c | 6 +- src/profanity.c | 2 +- src/ui_windows.c | 6 +- src/xmpp.h | 13 +- src/xmpp_conn.c | 314 +++++++-------------------------------------- src/xmpp_message.c | 240 ++++++++++++++++++++++++++++++++++ 6 files changed, 298 insertions(+), 283 deletions(-) diff --git a/src/command.c b/src/command.c index 59f13a2b..52b909f5 100644 --- a/src/command.c +++ b/src/command.c @@ -900,7 +900,7 @@ cmd_execute_default(const char * const inp) win_current_show("You are not currently connected."); } else { char *recipient = win_current_get_recipient(); - jabber_send_groupchat(inp, recipient); + message_send_groupchat(inp, recipient); free(recipient); } } else if (win_current_is_chat() || win_current_is_private()) { @@ -1898,7 +1898,7 @@ _cmd_tiny(gchar **args, struct cmd_help_t help) free(recipient); } else { // groupchat char *recipient = win_current_get_recipient(); - jabber_send_groupchat(tiny, recipient); + message_send_groupchat(tiny, recipient); free(recipient); } free(tiny); @@ -1936,7 +1936,7 @@ _cmd_close(gchar **args, struct cmd_help_t help) // send chat state before closing if (chat_session_get_recipient_supports(recipient)) { chat_session_set_gone(recipient); - jabber_send_gone(recipient); + message_send_gone(recipient); chat_session_end(recipient); } } diff --git a/src/profanity.c b/src/profanity.c index 741f9b81..2e2aa456 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -398,7 +398,7 @@ prof_handle_activity(void) chat_session_set_composing(recipient); if (!chat_session_get_sent(recipient) || chat_session_is_paused(recipient)) { - jabber_send_composing(recipient); + message_send_composing(recipient); } } } diff --git a/src/ui_windows.c b/src/ui_windows.c index 6c762e9b..cb92bfaf 100644 --- a/src/ui_windows.c +++ b/src/ui_windows.c @@ -314,14 +314,14 @@ ui_idle(void) if (chat_session_is_gone(recipient) && !chat_session_get_sent(recipient)) { - jabber_send_gone(recipient); + message_send_gone(recipient); } else if (chat_session_is_inactive(recipient) && !chat_session_get_sent(recipient)) { - jabber_send_inactive(recipient); + message_send_inactive(recipient); } else if (prefs_get_outtype() && chat_session_is_paused(recipient) && !chat_session_get_sent(recipient)) { - jabber_send_paused(recipient); + message_send_paused(recipient); } } } diff --git a/src/xmpp.h b/src/xmpp.h index 3a179141..a050dd03 100644 --- a/src/xmpp.h +++ b/src/xmpp.h @@ -146,11 +146,6 @@ jabber_conn_status_t jabber_connect_with_account(ProfAccount *account, const char * const passwd); void jabber_disconnect(void); void jabber_process_events(void); -void jabber_send_groupchat(const char * const msg, const char * const recipient); -void jabber_send_inactive(const char * const recipient); -void jabber_send_composing(const char * const recipient); -void jabber_send_paused(const char * const recipient); -void jabber_send_gone(const char * const recipient); const char * jabber_get_jid(void); jabber_conn_status_t jabber_get_connection_status(void); int jabber_get_priority(void); @@ -168,7 +163,15 @@ void jabber_conn_set_status(const char * const message); char* jabber_get_account_name(void); // message functions + +int message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, + void * const userdata); void message_send(const char * const msg, const char * const recipient); +void message_send_groupchat(const char * const msg, const char * const recipient); +void message_send_inactive(const char * const recipient); +void message_send_composing(const char * const recipient); +void message_send_paused(const char * const recipient); +void message_send_gone(const char * const recipient); // iq functions void iq_add_handlers(void); diff --git a/src/xmpp_conn.c b/src/xmpp_conn.c index 626ed63d..36f9973b 100644 --- a/src/xmpp_conn.c +++ b/src/xmpp_conn.c @@ -75,16 +75,9 @@ static void _jabber_reconnect(void); static void _jabber_roster_request(void); -// XMPP event handlers static void _connection_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); - -static int _message_handler(xmpp_conn_t * const conn, - xmpp_stanza_t * const stanza, void * const userdata); -static int _groupchat_message_handler(xmpp_stanza_t * const stanza); -static int _chat_message_handler(xmpp_stanza_t * const stanza); - static int _ping_timed_handler(xmpp_conn_t * const conn, void * const userdata); void @@ -106,46 +99,6 @@ jabber_restart(void) FREE_SET_NULL(jabber_conn.status); } -static jabber_conn_status_t -_jabber_connect(const char * const fulljid, const char * const passwd, - const char * const altdomain) -{ - Jid *jid = jid_create(fulljid); - - if (jid == NULL) { - log_error("Malformed JID not able to connect: %s", fulljid); - jabber_conn.conn_status = JABBER_DISCONNECTED; - return jabber_conn.conn_status; - } else if (jid->fulljid == NULL) { - log_error("Full JID required to connect, received: %s", fulljid); - jabber_conn.conn_status = JABBER_DISCONNECTED; - return jabber_conn.conn_status; - } - - jid_destroy(jid); - - log_info("Connecting as %s", fulljid); - xmpp_initialize(); - jabber_conn.log = _xmpp_get_file_logger(); - jabber_conn.ctx = xmpp_ctx_new(NULL, jabber_conn.log); - jabber_conn.conn = xmpp_conn_new(jabber_conn.ctx); - xmpp_conn_set_jid(jabber_conn.conn, fulljid); - xmpp_conn_set_pass(jabber_conn.conn, passwd); - - if (jabber_conn.tls_disabled) - xmpp_conn_disable_tls(jabber_conn.conn); - - int connect_status = xmpp_connect_client(jabber_conn.conn, altdomain, 0, - _connection_handler, jabber_conn.ctx); - - if (connect_status == 0) - jabber_conn.conn_status = JABBER_CONNECTING; - else - jabber_conn.conn_status = JABBER_DISCONNECTED; - - return jabber_conn.conn_status; -} - jabber_conn_status_t jabber_connect_with_account(ProfAccount *account, const char * const passwd) { @@ -241,60 +194,6 @@ jabber_process_events(void) } -void -jabber_send_groupchat(const char * const msg, const char * const recipient) -{ - xmpp_stanza_t *message = stanza_create_message(jabber_conn.ctx, recipient, - STANZA_TYPE_GROUPCHAT, msg, NULL); - - xmpp_send(jabber_conn.conn, message); - xmpp_stanza_release(message); -} - -void -jabber_send_composing(const char * const recipient) -{ - xmpp_stanza_t *stanza = stanza_create_chat_state(jabber_conn.ctx, recipient, - STANZA_NAME_COMPOSING); - - xmpp_send(jabber_conn.conn, stanza); - xmpp_stanza_release(stanza); - chat_session_set_sent(recipient); -} - -void -jabber_send_paused(const char * const recipient) -{ - xmpp_stanza_t *stanza = stanza_create_chat_state(jabber_conn.ctx, recipient, - STANZA_NAME_PAUSED); - - xmpp_send(jabber_conn.conn, stanza); - xmpp_stanza_release(stanza); - chat_session_set_sent(recipient); -} - -void -jabber_send_inactive(const char * const recipient) -{ - xmpp_stanza_t *stanza = stanza_create_chat_state(jabber_conn.ctx, recipient, - STANZA_NAME_INACTIVE); - - xmpp_send(jabber_conn.conn, stanza); - xmpp_stanza_release(stanza); - chat_session_set_sent(recipient); -} - -void -jabber_send_gone(const char * const recipient) -{ - xmpp_stanza_t *stanza = stanza_create_chat_state(jabber_conn.ctx, recipient, - STANZA_NAME_GONE); - - xmpp_send(jabber_conn.conn, stanza); - xmpp_stanza_release(stanza); - chat_session_set_sent(recipient); -} - void jabber_set_autoping(int seconds) { @@ -345,7 +244,6 @@ jabber_get_presence(void) return jabber_conn.presence; } -// returns pointer to status, owned by connection char * jabber_get_status(void) { @@ -395,101 +293,6 @@ jabber_free_resources(void) xmpp_shutdown(); } -static void -_jabber_roster_request(void) -{ - xmpp_stanza_t *iq = stanza_create_roster_iq(jabber_conn.ctx); - xmpp_send(jabber_conn.conn, iq); - xmpp_stanza_release(iq); -} - -static int -_message_handler(xmpp_conn_t * const conn, - xmpp_stanza_t * const stanza, void * const userdata) -{ - gchar *type = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_TYPE); - - if (type == NULL) { - log_error("Message stanza received with no type attribute"); - return 1; - } else if (strcmp(type, STANZA_TYPE_ERROR) == 0) { - return error_handler(stanza); - } else if (strcmp(type, STANZA_TYPE_GROUPCHAT) == 0) { - return _groupchat_message_handler(stanza); - } else if (strcmp(type, STANZA_TYPE_CHAT) == 0) { - return _chat_message_handler(stanza); - } else { - log_error("Message stanza received with unknown type: %s", type); - return 1; - } -} - -static int -_groupchat_message_handler(xmpp_stanza_t * const stanza) -{ - char *message = NULL; - char *room_jid = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); - Jid *jid = jid_create(room_jid); - - // handle room broadcasts - if (jid->resourcepart == NULL) { - xmpp_stanza_t *subject = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_SUBJECT); - - // handle subject - if (subject != NULL) { - message = xmpp_stanza_get_text(subject); - if (message != NULL) { - prof_handle_room_subject(jid->barejid, message); - } - - return 1; - - // handle other room broadcasts - } else { - xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_BODY); - if (body != NULL) { - message = xmpp_stanza_get_text(body); - if (message != NULL) { - prof_handle_room_broadcast(room_jid, message); - } - } - - return 1; - } - } - - - if (!jid_is_valid_room_form(jid)) { - log_error("Invalid room JID: %s", jid->str); - return 1; - } - - // room not active in profanity - if (!muc_room_is_active(jid)) { - log_error("Message recieved for inactive chat room: %s", jid->str); - return 1; - } - - // determine if the notifications happened whilst offline - GTimeVal tv_stamp; - gboolean delayed = stanza_get_delay(stanza, &tv_stamp); - xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_BODY); - - // check for and deal with message - if (body != NULL) { - char *message = xmpp_stanza_get_text(body); - if (delayed) { - prof_handle_room_history(jid->barejid, jid->resourcepart, tv_stamp, message); - } else { - prof_handle_room_message(jid->barejid, jid->resourcepart, message); - } - } - - jid_destroy(jid); - - return 1; -} - int error_handler(xmpp_stanza_t * const stanza) { @@ -529,83 +332,44 @@ error_handler(xmpp_stanza_t * const stanza) return 1; } -static int -_chat_message_handler(xmpp_stanza_t * const stanza) +static jabber_conn_status_t +_jabber_connect(const char * const fulljid, const char * const passwd, + const char * const altdomain) { - gchar *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); - Jid *jid = jid_create(from); + Jid *jid = jid_create(fulljid); - // private message from chat room use full jid (room/nick) - if (muc_room_is_active(jid)) { - // determine if the notifications happened whilst offline - GTimeVal tv_stamp; - gboolean delayed = stanza_get_delay(stanza, &tv_stamp); - - // check for and deal with message - xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_BODY); - if (body != NULL) { - char *message = xmpp_stanza_get_text(body); - if (delayed) { - prof_handle_delayed_message(jid->str, message, tv_stamp, TRUE); - } else { - prof_handle_incoming_message(jid->str, message, TRUE); - } - } - - free(jid); - return 1; - - // standard chat message, use jid without resource - } else { - // determine chatstate support of recipient - gboolean recipient_supports = FALSE; - if (stanza_contains_chat_state(stanza)) { - recipient_supports = TRUE; - } - - // create or update chat session - if (!chat_session_exists(jid->barejid)) { - chat_session_start(jid->barejid, recipient_supports); - } else { - chat_session_set_recipient_supports(jid->barejid, recipient_supports); - } - - // determine if the notifications happened whilst offline - GTimeVal tv_stamp; - gboolean delayed = stanza_get_delay(stanza, &tv_stamp); - - // deal with chat states if recipient supports them - if (recipient_supports && (!delayed)) { - if (xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_COMPOSING) != NULL) { - if (prefs_get_notify_typing() || prefs_get_intype()) { - prof_handle_typing(jid->barejid); - } - } else if (xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_GONE) != NULL) { - prof_handle_gone(jid->barejid); - } else if (xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_PAUSED) != NULL) { - // do something - } else if (xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_INACTIVE) != NULL) { - // do something - } else { // handle - // do something - } - } - - // check for and deal with message - xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_BODY); - if (body != NULL) { - char *message = xmpp_stanza_get_text(body); - if (delayed) { - prof_handle_delayed_message(jid->barejid, message, tv_stamp, FALSE); - } else { - prof_handle_incoming_message(jid->barejid, message, FALSE); - } - } - - free(jid); - return 1; + if (jid == NULL) { + log_error("Malformed JID not able to connect: %s", fulljid); + jabber_conn.conn_status = JABBER_DISCONNECTED; + return jabber_conn.conn_status; + } else if (jid->fulljid == NULL) { + log_error("Full JID required to connect, received: %s", fulljid); + jabber_conn.conn_status = JABBER_DISCONNECTED; + return jabber_conn.conn_status; } + jid_destroy(jid); + + log_info("Connecting as %s", fulljid); + xmpp_initialize(); + jabber_conn.log = _xmpp_get_file_logger(); + jabber_conn.ctx = xmpp_ctx_new(NULL, jabber_conn.log); + jabber_conn.conn = xmpp_conn_new(jabber_conn.ctx); + xmpp_conn_set_jid(jabber_conn.conn, fulljid); + xmpp_conn_set_pass(jabber_conn.conn, passwd); + + if (jabber_conn.tls_disabled) + xmpp_conn_disable_tls(jabber_conn.conn); + + int connect_status = xmpp_connect_client(jabber_conn.conn, altdomain, 0, + _connection_handler, jabber_conn.ctx); + + if (connect_status == 0) + jabber_conn.conn_status = JABBER_CONNECTING; + else + jabber_conn.conn_status = JABBER_DISCONNECTED; + + return jabber_conn.conn_status; } static void @@ -639,7 +403,7 @@ _connection_handler(xmpp_conn_t * const conn, chat_sessions_init(); - xmpp_handler_add(conn, _message_handler, NULL, STANZA_NAME_MESSAGE, NULL, ctx); + xmpp_handler_add(conn, message_handler, NULL, STANZA_NAME_MESSAGE, NULL, ctx); presence_add_handlers(); iq_add_handlers(); @@ -692,6 +456,14 @@ _connection_handler(xmpp_conn_t * const conn, } } +static void +_jabber_roster_request(void) +{ + xmpp_stanza_t *iq = stanza_create_roster_iq(jabber_conn.ctx); + xmpp_send(jabber_conn.conn, iq); + xmpp_stanza_release(iq); +} + static int _ping_timed_handler(xmpp_conn_t * const conn, void * const userdata) { diff --git a/src/xmpp_message.c b/src/xmpp_message.c index f018470c..532b7c1b 100644 --- a/src/xmpp_message.c +++ b/src/xmpp_message.c @@ -20,12 +20,21 @@ * */ +#include +#include + #include #include "chat_session.h" +#include "log.h" +#include "muc.h" #include "preferences.h" +#include "profanity.h" #include "xmpp.h" +static int _groupchat_message_handler(xmpp_stanza_t * const stanza); +static int _chat_message_handler(xmpp_stanza_t * const stanza); + void message_send(const char * const msg, const char * const recipient) { @@ -50,3 +59,234 @@ message_send(const char * const msg, const char * const recipient) xmpp_send(conn, message); xmpp_stanza_release(message); } + +void +message_send_groupchat(const char * const msg, const char * const recipient) +{ + xmpp_conn_t * const conn = jabber_get_conn(); + xmpp_ctx_t * const ctx = jabber_get_ctx(); + xmpp_stanza_t *message = stanza_create_message(ctx, recipient, + STANZA_TYPE_GROUPCHAT, msg, NULL); + + xmpp_send(conn, message); + xmpp_stanza_release(message); +} + +void +message_send_composing(const char * const recipient) +{ + xmpp_conn_t * const conn = jabber_get_conn(); + xmpp_ctx_t * const ctx = jabber_get_ctx(); + xmpp_stanza_t *stanza = stanza_create_chat_state(ctx, recipient, + STANZA_NAME_COMPOSING); + + xmpp_send(conn, stanza); + xmpp_stanza_release(stanza); + chat_session_set_sent(recipient); +} + +void +message_send_paused(const char * const recipient) +{ + xmpp_conn_t * const conn = jabber_get_conn(); + xmpp_ctx_t * const ctx = jabber_get_ctx(); + xmpp_stanza_t *stanza = stanza_create_chat_state(ctx, recipient, + STANZA_NAME_PAUSED); + + xmpp_send(conn, stanza); + xmpp_stanza_release(stanza); + chat_session_set_sent(recipient); +} + +void +message_send_inactive(const char * const recipient) +{ + xmpp_conn_t * const conn = jabber_get_conn(); + xmpp_ctx_t * const ctx = jabber_get_ctx(); + xmpp_stanza_t *stanza = stanza_create_chat_state(ctx, recipient, + STANZA_NAME_INACTIVE); + + xmpp_send(conn, stanza); + xmpp_stanza_release(stanza); + chat_session_set_sent(recipient); +} + +void +message_send_gone(const char * const recipient) +{ + xmpp_conn_t * const conn = jabber_get_conn(); + xmpp_ctx_t * const ctx = jabber_get_ctx(); + xmpp_stanza_t *stanza = stanza_create_chat_state(ctx, recipient, + STANZA_NAME_GONE); + + xmpp_send(conn, stanza); + xmpp_stanza_release(stanza); + chat_session_set_sent(recipient); +} + +int +message_handler(xmpp_conn_t * const conn, + xmpp_stanza_t * const stanza, void * const userdata) +{ + gchar *type = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_TYPE); + + if (type == NULL) { + log_error("Message stanza received with no type attribute"); + return 1; + } else if (strcmp(type, STANZA_TYPE_ERROR) == 0) { + return error_handler(stanza); + } else if (strcmp(type, STANZA_TYPE_GROUPCHAT) == 0) { + return _groupchat_message_handler(stanza); + } else if (strcmp(type, STANZA_TYPE_CHAT) == 0) { + return _chat_message_handler(stanza); + } else { + log_error("Message stanza received with unknown type: %s", type); + return 1; + } +} + +static int +_groupchat_message_handler(xmpp_stanza_t * const stanza) +{ + char *message = NULL; + char *room_jid = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); + Jid *jid = jid_create(room_jid); + + // handle room broadcasts + if (jid->resourcepart == NULL) { + xmpp_stanza_t *subject = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_SUBJECT); + + // handle subject + if (subject != NULL) { + message = xmpp_stanza_get_text(subject); + if (message != NULL) { + prof_handle_room_subject(jid->barejid, message); + } + + return 1; + + // handle other room broadcasts + } else { + xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_BODY); + if (body != NULL) { + message = xmpp_stanza_get_text(body); + if (message != NULL) { + prof_handle_room_broadcast(room_jid, message); + } + } + + return 1; + } + } + + + if (!jid_is_valid_room_form(jid)) { + log_error("Invalid room JID: %s", jid->str); + return 1; + } + + // room not active in profanity + if (!muc_room_is_active(jid)) { + log_error("Message recieved for inactive chat room: %s", jid->str); + return 1; + } + + // determine if the notifications happened whilst offline + GTimeVal tv_stamp; + gboolean delayed = stanza_get_delay(stanza, &tv_stamp); + xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_BODY); + + // check for and deal with message + if (body != NULL) { + char *message = xmpp_stanza_get_text(body); + if (delayed) { + prof_handle_room_history(jid->barejid, jid->resourcepart, tv_stamp, message); + } else { + prof_handle_room_message(jid->barejid, jid->resourcepart, message); + } + } + + jid_destroy(jid); + + return 1; +} + +static int +_chat_message_handler(xmpp_stanza_t * const stanza) +{ + gchar *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); + Jid *jid = jid_create(from); + + // private message from chat room use full jid (room/nick) + if (muc_room_is_active(jid)) { + // determine if the notifications happened whilst offline + GTimeVal tv_stamp; + gboolean delayed = stanza_get_delay(stanza, &tv_stamp); + + // check for and deal with message + xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_BODY); + if (body != NULL) { + char *message = xmpp_stanza_get_text(body); + if (delayed) { + prof_handle_delayed_message(jid->str, message, tv_stamp, TRUE); + } else { + prof_handle_incoming_message(jid->str, message, TRUE); + } + } + + free(jid); + return 1; + + // standard chat message, use jid without resource + } else { + // determine chatstate support of recipient + gboolean recipient_supports = FALSE; + if (stanza_contains_chat_state(stanza)) { + recipient_supports = TRUE; + } + + // create or update chat session + if (!chat_session_exists(jid->barejid)) { + chat_session_start(jid->barejid, recipient_supports); + } else { + chat_session_set_recipient_supports(jid->barejid, recipient_supports); + } + + // determine if the notifications happened whilst offline + GTimeVal tv_stamp; + gboolean delayed = stanza_get_delay(stanza, &tv_stamp); + + // deal with chat states if recipient supports them + if (recipient_supports && (!delayed)) { + if (xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_COMPOSING) != NULL) { + if (prefs_get_notify_typing() || prefs_get_intype()) { + prof_handle_typing(jid->barejid); + } + } else if (xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_GONE) != NULL) { + prof_handle_gone(jid->barejid); + } else if (xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_PAUSED) != NULL) { + // do something + } else if (xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_INACTIVE) != NULL) { + // do something + } else { // handle + // do something + } + } + + // check for and deal with message + xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_BODY); + if (body != NULL) { + char *message = xmpp_stanza_get_text(body); + if (delayed) { + prof_handle_delayed_message(jid->barejid, message, tv_stamp, FALSE); + } else { + prof_handle_incoming_message(jid->barejid, message, FALSE); + } + } + + free(jid); + return 1; + } + +} +