Search in sources :

Example 1 with ChatManager

use of org.jivesoftware.spark.ChatManager in project Spark by igniterealtime.

the class BroadcastPlugin method userToUserBroadcast.

/**
 * Handles Broadcasts made from a user to another user
 *
 * @param message
 *            the message
 * @param type
 *            the message type
 * @param from
 *            the sender
 */
private void userToUserBroadcast(Message message, Type type, String from) {
    String jid = XmppStringUtils.parseBareJid(from);
    String nickname = SparkManager.getUserManager().getUserNicknameFromJID(jid);
    ChatManager chatManager = SparkManager.getChatManager();
    ChatContainer container = chatManager.getChatContainer();
    ChatRoomImpl chatRoom;
    try {
        chatRoom = (ChatRoomImpl) container.getChatRoom(jid);
    } catch (ChatRoomNotFoundException e) {
        chatRoom = new ChatRoomImpl(jid, nickname, nickname);
        SparkManager.getChatManager().getChatContainer().addChatRoom(chatRoom);
    }
    Message m = new Message();
    m.setBody(message.getBody());
    m.setTo(message.getTo());
    String broadcasttype = type == Message.Type.normal ? Res.getString("broadcast") : Res.getString("message.alert.notify");
    // m.setFrom(name +" "+broadcasttype);
    m.setFrom(nickname + " - " + broadcasttype);
    chatRoom.getTranscriptWindow().insertMessage(m.getFrom(), message, ChatManager.FROM_COLOR);
    chatRoom.addToTranscript(m, true);
    chatRoom.increaseUnreadMessageCount();
    broadcastRooms.add(chatRoom);
    LocalPreferences pref = SettingsManager.getLocalPreferences();
    if (pref.getShowToasterPopup()) {
        SparkToaster toaster = new SparkToaster();
        toaster.setDisplayTime(30000);
        toaster.setBorder(BorderFactory.createBevelBorder(0));
        toaster.setTitle(nickname + " - " + broadcasttype);
        toaster.showToaster(message.getBody());
    }
    SparkManager.getChatManager().fireGlobalMessageReceievedListeners(chatRoom, message);
    DelayInformation inf = message.getExtension("delay", "urn:xmpp:delay");
    if (inf == null) {
        SoundPreference soundPreference = (SoundPreference) SparkManager.getPreferenceManager().getPreference(new SoundPreference().getNamespace());
        SoundPreferences preferences = soundPreference.getPreferences();
        if (preferences.isPlayIncomingSound()) {
            File incomingFile = new File(preferences.getIncomingSound());
            SparkManager.getSoundManager().playClip(incomingFile);
        }
    }
    chatRoom.addMessageListener(new MessageListener() {

        boolean waiting = true;

        public void messageReceived(ChatRoom room, Message message) {
            removeAsBroadcast(room);
        }

        public void messageSent(ChatRoom room, Message message) {
            removeAsBroadcast(room);
        }

        private void removeAsBroadcast(ChatRoom room) {
            if (waiting) {
                broadcastRooms.remove(room);
                // Notify decorators
                SparkManager.getChatManager().notifySparkTabHandlers(room);
                waiting = false;
            }
        }
    });
}
Also used : SoundPreference(org.jivesoftware.sparkimpl.preference.sounds.SoundPreference) Message(org.jivesoftware.smack.packet.Message) SoundPreferences(org.jivesoftware.sparkimpl.preference.sounds.SoundPreferences) MessageListener(org.jivesoftware.spark.ui.MessageListener) ChatRoomImpl(org.jivesoftware.spark.ui.rooms.ChatRoomImpl) ChatContainer(org.jivesoftware.spark.ui.ChatContainer) DelayInformation(org.jivesoftware.smackx.delay.packet.DelayInformation) ChatRoom(org.jivesoftware.spark.ui.ChatRoom) ChatRoomNotFoundException(org.jivesoftware.spark.ui.ChatRoomNotFoundException) LocalPreferences(org.jivesoftware.sparkimpl.settings.local.LocalPreferences) ChatManager(org.jivesoftware.spark.ChatManager) File(java.io.File)

Example 2 with ChatManager

use of org.jivesoftware.spark.ChatManager in project Spark by igniterealtime.

the class GatewayPlugin method registerPresenceListener.

private void registerPresenceListener() {
    StanzaFilter orFilter = new OrFilter(new StanzaTypeFilter(Presence.class), new StanzaTypeFilter(Message.class));
    SparkManager.getConnection().addAsyncStanzaListener(stanza -> {
        if (stanza instanceof Presence) {
            Presence presence = (Presence) stanza;
            Transport transport = TransportUtils.getTransport(stanza.getFrom());
            if (transport != null) {
                boolean registered = true;
                if (presence.getType() == Presence.Type.unavailable) {
                    registered = false;
                }
                GatewayItem button = uiMap.get(transport);
                button.signedIn(registered);
                SwingWorker worker = new SwingWorker() {

                    @Override
                    public Object construct() {
                        transferTab.revalidate();
                        transferTab.repaint();
                        return 41;
                    }
                };
                worker.start();
            }
        } else if (stanza instanceof Message) {
            Message message = (Message) stanza;
            String from = message.getFrom();
            boolean hasError = message.getType() == Message.Type.error;
            String body = message.getBody();
            if (from != null && hasError) {
                Transport transport = TransportUtils.getTransport(from);
                if (transport != null) {
                    String title = "Alert from " + transport.getName();
                    // Show error
                    MessageDialog.showAlert(body, title, "Information", SparkRes.getImageIcon(SparkRes.INFORMATION_IMAGE));
                }
            }
        }
    }, orFilter);
    ChatManager chatManager = SparkManager.getChatManager();
    chatManager.addContactItemHandler(this);
    // Iterate through Contacts and check for
    final ContactList contactList = SparkManager.getWorkspace().getContactList();
    for (ContactGroup contactGroup : contactList.getContactGroups()) {
        for (ContactItem contactItem : contactGroup.getContactItems()) {
            Presence presence = contactItem.getPresence();
            if (presence.isAvailable()) {
                String domain = XmppStringUtils.parseDomain(presence.getFrom());
                Transport transport = TransportUtils.getTransport(domain);
                if (transport != null) {
                    handlePresence(contactItem, presence);
                    contactGroup.fireContactGroupUpdated();
                }
            }
        }
    }
    SparkManager.getSessionManager().addPresenceListener(presence -> {
        for (Transport transport : TransportUtils.getTransports()) {
            GatewayItem button = uiMap.get(transport);
            if (button.isLoggedIn()) {
                if (!presence.isAvailable()) {
                    return;
                }
                // Create new presence
                Presence p = new Presence(presence.getType(), presence.getStatus(), presence.getPriority(), presence.getMode());
                p.setTo(transport.getServiceName());
                try {
                    SparkManager.getConnection().sendStanza(p);
                } catch (SmackException.NotConnectedException e) {
                    Log.warning("Unable to forward presence change to transport.", e);
                }
            }
        }
    });
}
Also used : StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) Message(org.jivesoftware.smack.packet.Message) SmackException(org.jivesoftware.smack.SmackException) OrFilter(org.jivesoftware.smack.filter.OrFilter) StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) Presence(org.jivesoftware.smack.packet.Presence) SwingWorker(org.jivesoftware.spark.util.SwingWorker) ChatManager(org.jivesoftware.spark.ChatManager)

Example 3 with ChatManager

use of org.jivesoftware.spark.ChatManager in project Spark by igniterealtime.

the class UriManager method handleMessage.

/**
 * handles the ?message URI
 *
 * @param uri
 *            the decoded uri
 */
public void handleMessage(URI uri) {
    String query = uri.getQuery();
    int bodyIndex = query.indexOf("body=");
    String jid = retrieveJID(uri);
    String body = null;
    // Find body
    if (bodyIndex != -1) {
        body = query.substring(bodyIndex + 5);
    }
    body = org.jivesoftware.spark.util.StringUtils.unescapeFromXML(body);
    UserManager userManager = SparkManager.getUserManager();
    String nickname = userManager.getUserNicknameFromJID(jid);
    if (nickname == null) {
        nickname = jid;
    }
    ChatManager chatManager = SparkManager.getChatManager();
    ChatRoom chatRoom = chatManager.createChatRoom(jid, nickname, nickname);
    if (body != null) {
        Message message = new Message();
        message.setBody(body);
        chatRoom.sendMessage(message);
    }
    chatManager.getChatContainer().activateChatRoom(chatRoom);
}
Also used : Message(org.jivesoftware.smack.packet.Message) ChatRoom(org.jivesoftware.spark.ui.ChatRoom) UserManager(org.jivesoftware.spark.UserManager) ChatManager(org.jivesoftware.spark.ChatManager)

Example 4 with ChatManager

use of org.jivesoftware.spark.ChatManager in project Spark by igniterealtime.

the class ConferenceUtils method createPrivateConference.

/**
 * Creates a private conference.
 *
 * @param serviceName the service name to use for the private conference.
 * @param message     the message sent to each individual invitee.
 * @param roomName    the name of the room to create.
 * @param jids        a collection of the user JIDs to invite.
 * @throws SmackException thrown if an error occurs during room creation.
 */
public static void createPrivateConference(String serviceName, String message, String roomName, Collection<String> jids) throws SmackException {
    final String roomJID = XmppStringUtils.escapeLocalpart(roomName) + "@" + serviceName;
    final MultiUserChat multiUserChat = MultiUserChatManager.getInstanceFor(SparkManager.getConnection()).getMultiUserChat(roomJID);
    final LocalPreferences pref = SettingsManager.getLocalPreferences();
    final GroupChatRoom room = UIComponentRegistry.createGroupChatRoom(multiUserChat);
    try {
        // Attempt to create room.
        multiUserChat.create(pref.getNickname());
    } catch (XMPPException | SmackException e) {
        throw new SmackException(e);
    }
    try {
        // Since this is a private room, make the room not public and set user as owner of the room.
        Form submitForm = multiUserChat.getConfigurationForm().createAnswerForm();
        submitForm.setAnswer("muc#roomconfig_publicroom", false);
        submitForm.setAnswer("muc#roomconfig_roomname", roomName);
        final List<String> owners = new ArrayList<>();
        owners.add(SparkManager.getSessionManager().getBareAddress());
        submitForm.setAnswer("muc#roomconfig_roomowners", owners);
        multiUserChat.sendConfigurationForm(submitForm);
    } catch (XMPPException | SmackException e1) {
        Log.error("Unable to send conference room chat configuration form.", e1);
    }
    ChatManager chatManager = SparkManager.getChatManager();
    // Check if room already is open
    try {
        chatManager.getChatContainer().getChatRoom(room.getRoomname());
    } catch (ChatRoomNotFoundException e) {
        chatManager.getChatContainer().addChatRoom(room);
        chatManager.getChatContainer().activateChatRoom(room);
    }
    for (String jid : jids) {
        multiUserChat.invite(jid, message);
        room.getTranscriptWindow().insertNotificationMessage(Res.getString("message.waiting.for.user.to.join", jid), ChatManager.NOTIFICATION_COLOR);
    }
}
Also used : GroupChatRoom(org.jivesoftware.spark.ui.rooms.GroupChatRoom) MultiUserChat(org.jivesoftware.smackx.muc.MultiUserChat) DataForm(org.jivesoftware.smackx.xdata.packet.DataForm) Form(org.jivesoftware.smackx.xdata.Form) ChatRoomNotFoundException(org.jivesoftware.spark.ui.ChatRoomNotFoundException) SmackException(org.jivesoftware.smack.SmackException) LocalPreferences(org.jivesoftware.sparkimpl.settings.local.LocalPreferences) XMPPException(org.jivesoftware.smack.XMPPException) ChatManager(org.jivesoftware.spark.ChatManager) MultiUserChatManager(org.jivesoftware.smackx.muc.MultiUserChatManager)

Example 5 with ChatManager

use of org.jivesoftware.spark.ChatManager in project Spark by igniterealtime.

the class ConversationInvitation method actionPerformed.

public void actionPerformed(ActionEvent actionEvent) {
    final Object obj = actionEvent.getSource();
    if (obj == joinButton) {
        String name = XmppStringUtils.parseLocalpart(roomName);
        ConferenceUtils.enterRoomOnSameThread(name, roomName, password);
    } else {
        try {
            MultiUserChatManager.getInstanceFor(SparkManager.getConnection()).decline(roomName, inviter, "No thank you");
        } catch (SmackException.NotConnectedException e) {
            Log.warning("unable to decline invatation from " + inviter + " to join room " + roomName, e);
        }
    }
    // Close Container
    ChatManager chatManager = SparkManager.getChatManager();
    chatManager.getChatContainer().closeTab(this);
}
Also used : SmackException(org.jivesoftware.smack.SmackException) ChatManager(org.jivesoftware.spark.ChatManager) MultiUserChatManager(org.jivesoftware.smackx.muc.MultiUserChatManager)

Aggregations

ChatManager (org.jivesoftware.spark.ChatManager)19 ChatRoom (org.jivesoftware.spark.ui.ChatRoom)8 ChatRoomImpl (org.jivesoftware.spark.ui.rooms.ChatRoomImpl)7 SmackException (org.jivesoftware.smack.SmackException)6 ChatContainer (org.jivesoftware.spark.ui.ChatContainer)5 MultiUserChatManager (org.jivesoftware.smackx.muc.MultiUserChatManager)4 GroupChatRoom (org.jivesoftware.spark.ui.rooms.GroupChatRoom)4 ActionEvent (java.awt.event.ActionEvent)3 Message (org.jivesoftware.smack.packet.Message)3 ChatRoomListenerAdapter (org.jivesoftware.spark.ui.ChatRoomListenerAdapter)3 ChatRoomNotFoundException (org.jivesoftware.spark.ui.ChatRoomNotFoundException)3 LocalPreferences (org.jivesoftware.sparkimpl.settings.local.LocalPreferences)3 BorderLayout (java.awt.BorderLayout)2 File (java.io.File)2 ImageIcon (javax.swing.ImageIcon)2 JButton (javax.swing.JButton)2 JComboBox (javax.swing.JComboBox)2 JLabel (javax.swing.JLabel)2 JPanel (javax.swing.JPanel)2 Presence (org.jivesoftware.smack.packet.Presence)2