Search in sources :

Example 1 with RosterEntry

use of org.jivesoftware.smack.roster.RosterEntry in project xabber-android by redsolution.

the class RosterManager method setName.

public void setName(String account, String bareAddress, final String name) {
    final Roster roster = getRoster(account);
    if (roster == null) {
        return;
    }
    final RosterEntry entry = roster.getEntry(bareAddress);
    if (entry == null) {
        return;
    }
    try {
        entry.setName(name.trim());
    } catch (SmackException.NotConnectedException e) {
        Application.getInstance().onError(R.string.NOT_CONNECTED);
    } catch (SmackException.NoResponseException e) {
        Application.getInstance().onError(R.string.CONNECTION_FAILED);
    } catch (XMPPException.XMPPErrorException e) {
        Application.getInstance().onError(R.string.XMPP_EXCEPTION);
    }
}
Also used : Roster(org.jivesoftware.smack.roster.Roster) SmackException(org.jivesoftware.smack.SmackException) RosterEntry(org.jivesoftware.smack.roster.RosterEntry) XMPPException(org.jivesoftware.smack.XMPPException)

Example 2 with RosterEntry

use of org.jivesoftware.smack.roster.RosterEntry in project Spark by igniterealtime.

the class ChatRoomImpl method processPacket.

/**
 * Process incoming packets.
 *
 * @param stanza - the packet to process
 */
public void processPacket(final Stanza stanza) {
    final Runnable runnable = () -> {
        try {
            if (stanza instanceof Presence) {
                Presence.Type oldType = presence.getType();
                presence = (Presence) stanza;
                final Presence presence1 = (Presence) stanza;
                ContactList list = SparkManager.getWorkspace().getContactList();
                ContactItem contactItem = list.getContactItemByJID(getParticipantJID());
                String time = DateFormat.getTimeInstance(DateFormat.SHORT).format(new Date());
                if (presence1.getType() == Presence.Type.unavailable && contactItem != null) {
                    getTranscriptWindow().insertNotificationMessage("*** " + Res.getString("message.went.offline", participantNickname, time), ChatManager.NOTIFICATION_COLOR);
                } else if (oldType == Presence.Type.unavailable && presence1.getType() == Presence.Type.available) {
                    getTranscriptWindow().insertNotificationMessage("*** " + Res.getString("message.came.online", participantNickname, time), ChatManager.NOTIFICATION_COLOR);
                }
            } else if (stanza instanceof Message) {
                lastActivity = System.currentTimeMillis();
                // Do something with the incoming packet here.
                final Message message = (Message) stanza;
                fireReceivingIncomingMessage(message);
                if (message.getError() != null) {
                    if (message.getError().getCondition() == XMPPError.Condition.item_not_found) {
                        // Check to see if the user is online to recieve this message.
                        RosterEntry entry = roster.getEntry(participantJID);
                        if (!presence.isAvailable() && !offlineSent && entry != null) {
                            getTranscriptWindow().insertNotificationMessage(Res.getString("message.offline.error"), ChatManager.ERROR_COLOR);
                            offlineSent = true;
                        }
                    }
                    return;
                }
                // Check to see if the user is online to recieve this message.
                RosterEntry entry = roster.getEntry(participantJID);
                if (!presence.isAvailable() && !offlineSent && entry != null) {
                    getTranscriptWindow().insertNotificationMessage(Res.getString("message.offline"), ChatManager.ERROR_COLOR);
                    offlineSent = true;
                }
                if (threadID == null) {
                    threadID = message.getThread();
                    if (threadID == null) {
                        threadID = StringUtils.randomString(6);
                    }
                }
                final JivePropertiesExtension extension = ((JivePropertiesExtension) message.getExtension(JivePropertiesExtension.NAMESPACE));
                final boolean broadcast = extension != null && extension.getProperty("broadcast") != null;
                // If this is a group chat message, discard
                if (message.getType() == Message.Type.groupchat || broadcast || message.getType() == Message.Type.normal || message.getType() == Message.Type.headline) {
                    return;
                }
                // Do not accept Administrative messages.
                final String host = SparkManager.getSessionManager().getServerAddress();
                if (host.equals(message.getFrom())) {
                    return;
                }
                final CarbonExtension carbon = (CarbonExtension) message.getExtension(CarbonExtension.NAMESPACE);
                if (carbon != null) {
                    // Is the a carbon copy?
                    final Message forwardedStanza = (Message) carbon.getForwarded().getForwardedPacket();
                    if (forwardedStanza.getBody() != null) {
                        if (carbon.getDirection() == CarbonExtension.Direction.received) {
                            // This is a stanza that we received from someone on one of our other clients.
                            participantJID = forwardedStanza.getFrom();
                            insertMessage(forwardedStanza);
                        } else {
                            // This is a stanza that one of our own clients sent.
                            participantJID = forwardedStanza.getTo();
                            displaySendMessage(forwardedStanza);
                        }
                        showTyping(false);
                    }
                } else if (message.getBody() != null) {
                    // If the message is not from the current agent. Append to chat.
                    participantJID = message.getFrom();
                    insertMessage(message);
                    showTyping(false);
                }
            }
        } catch (Exception e) {
            Log.error("An exception occurred while processing this incoming stanza: " + stanza, e);
        }
    };
    SwingUtilities.invokeLater(runnable);
}
Also used : HistoryMessage(org.jivesoftware.sparkimpl.plugin.transcripts.HistoryMessage) CarbonExtension(org.jivesoftware.smackx.carbons.packet.CarbonExtension) Date(java.util.Date) JivePropertiesExtension(org.jivesoftware.smackx.jiveproperties.packet.JivePropertiesExtension) SmackException(org.jivesoftware.smack.SmackException) XMPPException(org.jivesoftware.smack.XMPPException) RosterEntry(org.jivesoftware.smack.roster.RosterEntry)

Example 3 with RosterEntry

use of org.jivesoftware.smack.roster.RosterEntry in project Spark by igniterealtime.

the class ContactListAssistantPlugin method addContactItem.

/**
 * Copies or moves a new <code>ContactItem</code> into the <code>ContactGroup</code>.
 *
 * @param contactGroup the ContactGroup.
 * @param item         the ContactItem to move.
 * @param move         true if the ContactItem should be moved, otherwise false.
 */
private void addContactItem(final ContactGroup contactGroup, final ContactItem item, final boolean move) {
    ContactItem newContact = UIComponentRegistry.createContactItem(item.getAlias(), item.getNickname(), item.getJID());
    newContact.setPresence(item.getPresence());
    newContact.setIcon(item.getIcon());
    newContact.getNicknameLabel().setFont(item.getNicknameLabel().getFont());
    boolean groupHadAvailableContacts = false;
    // Do not copy/move a contact item only if it is not already in the Group.
    if (contactGroup.getContactItemByJID(item.getJID(), true) != null) {
        return;
    }
    if (!PresenceManager.isOnline(item.getJID())) {
        contactGroup.addOfflineContactItem(item.getAlias(), item.getNickname(), item.getJID(), null);
    } else {
        groupHadAvailableContacts = contactGroup.hasAvailableContacts();
        contactGroup.addContactItem(newContact);
    }
    contactGroup.clearSelection();
    // Updating group title
    contactGroup.fireContactGroupUpdated();
    final ContactGroup oldGroup = getContactGroup(item.getGroupName());
    final boolean groupAvailableContacts = groupHadAvailableContacts;
    SwingWorker worker = new SwingWorker() {

        @Override
        public Object construct() {
            Roster roster = Roster.getInstanceFor(SparkManager.getConnection());
            RosterEntry entry = roster.getEntry(item.getJID());
            RosterGroup groupFound = null;
            for (RosterGroup group : roster.getGroups()) {
                if (group.getName().equals(contactGroup.getGroupName())) {
                    try {
                        groupFound = group;
                        if (!groupAvailableContacts) {
                            SparkManager.getContactList().toggleGroupVisibility(groupFound.getName(), true);
                        }
                        group.addEntry(entry);
                    } catch (XMPPException | SmackException e1) {
                        Log.error(e1);
                        return false;
                    }
                }
            }
            // This is a new group
            if (groupFound == null) {
                groupFound = roster.createGroup(contactGroup.getGroupName());
                try {
                    groupFound.addEntry(entry);
                    if (!groupAvailableContacts) {
                        SparkManager.getContactList().toggleGroupVisibility(groupFound.getName(), true);
                    }
                } catch (XMPPException | SmackException e) {
                    Log.error(e);
                }
            }
            return true;
        }

        @Override
        public void finished() {
            if ((Boolean) get()) {
                // Now try and remove the group from the old one.
                if (move) {
                    removeContactItem(oldGroup, item);
                    if (!localPreferences.isEmptyGroupsShown() && !oldGroup.hasAvailableContacts()) {
                        SparkManager.getContactList().toggleGroupVisibility(oldGroup.getGroupName(), false);
                    }
                }
            }
        }
    };
    worker.start();
}
Also used : Roster(org.jivesoftware.smack.roster.Roster) ContactItem(org.jivesoftware.spark.ui.ContactItem) SmackException(org.jivesoftware.smack.SmackException) SwingWorker(org.jivesoftware.spark.util.SwingWorker) RosterEntry(org.jivesoftware.smack.roster.RosterEntry) ContactGroup(org.jivesoftware.spark.ui.ContactGroup) XMPPException(org.jivesoftware.smack.XMPPException) RosterGroup(org.jivesoftware.smack.roster.RosterGroup)

Example 4 with RosterEntry

use of org.jivesoftware.smack.roster.RosterEntry in project Spark by igniterealtime.

the class UriManager method handleRemove.

/**
 * Handles the ?remove URI
 *
 * @param uri
 *            the decoded uri
 * @throws Exception
 */
public void handleRemove(URI uri) throws Exception {
    // xmpp:romeo@montague.net?remove
    String jid = retrieveJID(uri);
    Roster roster = Roster.getInstanceFor(SparkManager.getConnection());
    RosterEntry entry = roster.getEntry(jid);
    roster.removeEntry(entry);
}
Also used : Roster(org.jivesoftware.smack.roster.Roster) RosterEntry(org.jivesoftware.smack.roster.RosterEntry)

Example 5 with RosterEntry

use of org.jivesoftware.smack.roster.RosterEntry in project Spark by igniterealtime.

the class UriManager method handleRoster.

/**
 * Handles the ?roster URI<br>
 * with name= and group=
 *
 * @param uri
 *            the decoded uri
 * @throws Exception
 */
public void handleRoster(URI uri) throws Exception {
    // xmpp:romeo@montague.net?roster
    // xmpp:romeo@montague.net?roster;name=Romeo%20Montague
    // xmpp:romeo@montague.net?roster;group=Friends
    // xmpp:romeo@montague.net?roster;name=Romeo%20Montague;group=Friends
    String jid = retrieveJID(uri);
    String name = "";
    String query = uri.getQuery();
    if (query.contains("name=")) {
        StringBuilder buf = new StringBuilder();
        int x = query.indexOf("name=") + 5;
        while (x < query.length() && query.charAt(x) != ';') {
            buf.append(query.charAt(x));
            x++;
        }
    }
    String group = "";
    if (query.contains("group=")) {
        StringBuilder buf = new StringBuilder();
        int x = query.indexOf("group=") + 6;
        while (x < query.length() && query.charAt(x) != ';') {
            buf.append(query.charAt(x));
            x++;
        }
    }
    Roster roster = Roster.getInstanceFor(SparkManager.getConnection());
    RosterEntry userEntry = roster.getEntry(jid);
    roster.createEntry(jid, name, new String[] { group });
    RosterGroup rosterGroup = roster.getGroup(group);
    if (rosterGroup == null) {
        rosterGroup = roster.createGroup(group);
    }
    if (userEntry == null) {
        roster.createEntry(jid, name, new String[] { group });
        userEntry = roster.getEntry(jid);
    } else {
        userEntry.setName(name);
        rosterGroup.addEntry(userEntry);
    }
    userEntry = roster.getEntry(jid);
}
Also used : Roster(org.jivesoftware.smack.roster.Roster) RosterEntry(org.jivesoftware.smack.roster.RosterEntry) RosterGroup(org.jivesoftware.smack.roster.RosterGroup)

Aggregations

RosterEntry (org.jivesoftware.smack.roster.RosterEntry)45 Roster (org.jivesoftware.smack.roster.Roster)35 RosterGroup (org.jivesoftware.smack.roster.RosterGroup)17 SmackException (org.jivesoftware.smack.SmackException)14 Presence (org.jivesoftware.smack.packet.Presence)13 XMPPException (org.jivesoftware.smack.XMPPException)12 IOException (java.io.IOException)6 SwingWorker (org.jivesoftware.spark.util.SwingWorker)4 RosterPacket (org.jivesoftware.smack.roster.packet.RosterPacket)3 AccountItem (com.xabber.android.data.account.AccountItem)2 ConnectionItem (com.xabber.android.data.connection.ConnectionItem)2 java.awt (java.awt)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 KeyEvent (java.awt.event.KeyEvent)2 java.util (java.util)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 javax.swing (javax.swing)2 Icon (javax.swing.Icon)2