void message_send(const char * const msg, const char * const recipient) { const char * jid = NULL; if (roster_barejid_from_name(recipient) != NULL) { jid = roster_barejid_from_name(recipient); } else { jid = recipient; } if (prefs_get_boolean(PREF_STATES)) { if (!chat_session_exists(jid)) { chat_session_start(jid, TRUE); } } xmpp_stanza_t *message; xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); if (prefs_get_boolean(PREF_STATES) && chat_session_get_recipient_supports(jid)) { chat_session_set_active(jid); message = stanza_create_message(ctx, jid, STANZA_TYPE_CHAT, msg, STANZA_NAME_ACTIVE, NULL); } else { message = stanza_create_message(ctx, jid, STANZA_TYPE_CHAT, msg, NULL, NULL); } xmpp_send(conn, message); xmpp_stanza_release(message); }
// handle message stanza errors void handle_message_error(const char * const from, const char * const type, const char * const err_msg) { // handle errors from no recipient if (from == NULL) { ui_handle_error(err_msg); // handle recipient not found ('from' contains a value and type is 'cancel') } else if (type != NULL && (strcmp(type, "cancel") == 0)) { ui_handle_recipient_not_found(from, err_msg); if (prefs_get_boolean(PREF_STATES) && chat_session_exists(from)) { chat_session_set_recipient_supports(from, FALSE); } // handle any other error from recipient } else { ui_handle_recipient_error(from, err_msg); } }
static int _chat_message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) { xmpp_ctx_t *ctx = connection_get_ctx(); gchar *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); Jid *jid = jid_create(from); // handle ddg searches if (strcmp(jid->barejid, "*****@*****.**") == 0) { 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 (message != NULL) { prof_handle_duck_result(message); xmpp_free(ctx, message); } } jid_destroy(jid); return 1; // private message from chat room use full jid (room/nick) } else 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 (message != NULL) { if (delayed) { prof_handle_delayed_message(jid->str, message, tv_stamp, TRUE); } else { prof_handle_incoming_message(jid->str, message, TRUE); } xmpp_free(ctx, message); } } jid_destroy(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_boolean(PREF_NOTIFY_TYPING) || prefs_get_boolean(PREF_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 <active/> // 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 (message != NULL) { if (delayed) { prof_handle_delayed_message(jid->barejid, message, tv_stamp, FALSE); } else { prof_handle_incoming_message(jid->barejid, message, FALSE); } xmpp_free(ctx, message); } } jid_destroy(jid); return 1; } }