Search in sources :

Example 1 with ChatStateExtension

use of org.jivesoftware.smackx.packet.ChatStateExtension in project Openfire by igniterealtime.

the class XMPPSession method sendChatState.

/**
     * @see net.sf.kraken.session.TransportSession#sendChatState(org.xmpp.packet.JID, net.sf.kraken.type.ChatStateType)
     */
@Override
public void sendChatState(JID jid, ChatStateType chatState) {
    final Presence presence = conn.getRoster().getPresence(jid.toString());
    if (presence == null || presence.getType().equals(Presence.Type.unavailable)) {
        // don't send chat state to contacts that are offline.
        return;
    }
    Chat chat = conn.getChatManager().createChat(getTransport().convertJIDToID(jid), listener);
    try {
        ChatState state = ChatState.active;
        switch(chatState) {
            case active:
                state = ChatState.active;
                break;
            case composing:
                state = ChatState.composing;
                break;
            case paused:
                state = ChatState.paused;
                break;
            case inactive:
                state = ChatState.inactive;
                break;
            case gone:
                state = ChatState.gone;
                break;
        }
        Message message = new Message();
        message.addExtension(new ChatStateExtension(state));
        chat.sendMessage(message);
    } catch (XMPPException e) {
    // Ignore
    }
}
Also used : ChatState(org.jivesoftware.smackx.ChatState) Message(org.jivesoftware.smack.packet.Message) ChatStateExtension(org.jivesoftware.smackx.packet.ChatStateExtension) Presence(org.jivesoftware.smack.packet.Presence)

Example 2 with ChatStateExtension

use of org.jivesoftware.smackx.packet.ChatStateExtension in project Openfire by igniterealtime.

the class XMPPListener method processMessage.

/**
     * Handles incoming messages.
     *
     * @param chat Chat instance this message is associated with.
     * @param message Message received.
     */
public void processMessage(Chat chat, org.jivesoftware.smack.packet.Message message) {
    Log.debug("Received " + getSession().getTransport().getType().name() + " message: " + message.toXML());
    try {
        final BaseTransport<XMPPBuddy> transport = getSession().getTransport();
        final JID legacyJID = transport.convertIDToJID(message.getFrom());
        final JID localJID = getSession().getJID();
        final PacketExtension pe = message.getExtension("x", NameSpace.X_DELAY);
        final PacketExtension attExt = message.getExtension(AttentionExtension.ELEMENT_NAME, AttentionExtension.NAMESPACE);
        if (pe != null && pe instanceof DelayInformation) {
            DelayInformation di = (DelayInformation) pe;
            transport.sendOfflineMessage(localJID, legacyJID, message.getBody(), di.getStamp(), di.getReason());
        } else if (attExt != null && (attExt instanceof AttentionExtension)) {
            transport.sendAttentionNotification(localJID, legacyJID, message.getBody());
        } else {
            // see if we got sent chat state notifications
            final PacketExtension cse = message.getExtension("http://jabber.org/protocol/chatstates");
            if (cse != null && cse instanceof ChatStateExtension) {
                final String chatState = cse.getElementName();
                try {
                    final ChatStateType cst = ChatStateType.valueOf(ChatStateType.class, chatState);
                    switch(cst) {
                        case active:
                            // included with that chat message below.
                            if (message.getBody() == null || message.getBody().trim().length() == 0) {
                                transport.sendChatActiveNotification(localJID, legacyJID);
                            }
                            break;
                        case composing:
                            transport.sendComposingNotification(localJID, legacyJID);
                            break;
                        case gone:
                            transport.sendChatGoneNotification(localJID, legacyJID);
                            break;
                        case inactive:
                            transport.sendChatInactiveNotification(localJID, legacyJID);
                            break;
                        case paused:
                            transport.sendComposingPausedNotification(localJID, legacyJID);
                            break;
                        default:
                            Log.debug("Unexpected chat state recieved: " + cst);
                            break;
                    }
                } catch (IllegalArgumentException ex) {
                    Log.warn("Illegal chat state notification " + "received from legacy domain: " + chatState);
                }
            }
            if (message.getType() == Type.error) {
                Log.debug("Received an error message! Message: " + message.toXML());
                transport.sendMessage(localJID, legacyJID, message.getBody(), Message.Type.error);
            } else {
                transport.sendMessage(localJID, legacyJID, message.getBody());
            }
        }
    //            if (message.getProperty("time") == null || message.getProperty("time").equals("")) {
    //            }
    //            else {
    //                getSession().getTransport().sendOfflineMessage(
    //                        getSession().getJID(),
    //                        getSession().getTransport().convertIDToJID(message.getFrom()),
    //                        message.getBody(),
    //                        Message.Type.chat,
    //                        message.getProperty("time").toString()
    //                );
    //            }
    } catch (Exception ex) {
        Log.debug("E001:" + ex.getMessage(), ex);
    }
}
Also used : PacketExtension(org.jivesoftware.smack.packet.PacketExtension) IQWithPacketExtension(net.sf.kraken.protocols.xmpp.packet.IQWithPacketExtension) ChatStateType(net.sf.kraken.type.ChatStateType) JID(org.xmpp.packet.JID) DelayInformation(org.jivesoftware.smackx.packet.DelayInformation) ChatStateExtension(org.jivesoftware.smackx.packet.ChatStateExtension) AttentionExtension(net.sf.kraken.protocols.xmpp.packet.AttentionExtension)

Aggregations

ChatStateExtension (org.jivesoftware.smackx.packet.ChatStateExtension)2 AttentionExtension (net.sf.kraken.protocols.xmpp.packet.AttentionExtension)1 IQWithPacketExtension (net.sf.kraken.protocols.xmpp.packet.IQWithPacketExtension)1 ChatStateType (net.sf.kraken.type.ChatStateType)1 Message (org.jivesoftware.smack.packet.Message)1 PacketExtension (org.jivesoftware.smack.packet.PacketExtension)1 Presence (org.jivesoftware.smack.packet.Presence)1 ChatState (org.jivesoftware.smackx.ChatState)1 DelayInformation (org.jivesoftware.smackx.packet.DelayInformation)1 JID (org.xmpp.packet.JID)1