Java Code Examples for org.jxmpp.jid.EntityFullJid#getResourcepart()

The following examples show how to use org.jxmpp.jid.EntityFullJid#getResourcepart() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractDebugger.java    From Smack with Apache License 2.0 6 votes vote down vote up
@Override
public void userHasLogged(EntityFullJid user) {
    String localpart = user.getLocalpart().toString();
    boolean isAnonymous = "".equals(localpart);
    String title =
            "User logged (" + connection.getConnectionCounter() + "): "
            + (isAnonymous ? "" : localpart)
            + "@"
            + connection.getXMPPServiceDomain()
            + ":"
            + connection.getPort();
    title += "/" + user.getResourcepart();
    log(title);
    // Add the connection listener to the connection so that the debugger can be notified
    // whenever the connection is closed.
    connection.addConnectionListener(connListener);
}
 
Example 2
Source File: Occupant.java    From Smack with Apache License 2.0 6 votes vote down vote up
Occupant(Presence presence) {
    MUCUser mucUser = (MUCUser) presence.getExtensionElement("x",
            "http://jabber.org/protocol/muc#user");
    MUCItem item = mucUser.getItem();
    this.jid = item.getJid();
    this.affiliation = item.getAffiliation();
    this.role = item.getRole();
    // Get the nickname from the FROM attribute of the presence
    EntityFullJid from = presence.getFrom().asEntityFullJidIfPossible();
    if (from == null) {
        LOGGER.warning("Occupant presence without resource: " + presence.getFrom());
        this.nick = null;
    } else {
        this.nick = from.getResourcepart();
    }
}
 
Example 3
Source File: GroupChatRoom.java    From Spark with Apache License 2.0 6 votes vote down vote up
@Override
protected void sendChatState(ChatState state) throws SmackException.NotConnectedException, InterruptedException
{
    if (!chatStatEnabled || !SparkManager.getConnection().isConnected() )
    {
        return;
    }

    // XEP-0085: SHOULD NOT send 'gone' in a MUC.
    if ( state == ChatState.gone )
    {
        return;
    }

    for ( final EntityFullJid occupant : chat.getOccupants() )
    {
        final Resourcepart occupantNickname = occupant.getResourcepart();
        final Resourcepart myNickname = chat.getNickname();
        if (occupantNickname != null && !occupantNickname.equals(myNickname))
        {
            SparkManager.getMessageEventManager().sendComposingNotification(occupant, "djn");
        }
    }
}
 
Example 4
Source File: GroupChatParticipantList.java    From Spark with Apache License 2.0 6 votes vote down vote up
protected void startChat(ChatRoom groupChat, EntityFullJid groupJID) {
	Resourcepart userNickname = groupJID.getResourcepart();
	String roomTitle = userNickname + " - "
			+ groupChat.getRoomJid();

	// TODO: Remove duplicate variable userNickname and nicknameOfUser.
	Resourcepart nicknameOfUser = userNickname;
	Resourcepart nickname = groupChat.getNickname();

	if (nicknameOfUser.equals(nickname)) {
		return;
	}

	ChatRoom chatRoom;
	try {
		chatRoom = chatManager.getChatContainer().getChatRoom(groupJID);
	} catch (ChatRoomNotFoundException e) {
		Log.debug("Could not find chat room - " + groupJID);

		// Create new room
		chatRoom = new ChatRoomImpl(groupJID.asEntityBareJid(), nicknameOfUser, roomTitle);
		chatManager.getChatContainer().addChatRoom(chatRoom);
	}

	chatManager.getChatContainer().activateChatRoom(chatRoom);
}
 
Example 5
Source File: JidCreateTest.java    From jxmpp with Apache License 2.0 5 votes vote down vote up
@Test
public void entityFullFromComplexTest() throws XmppStringprepException {
	EntityFullJid entityFullJid = JidCreate.entityFullFrom("foo@[email protected]/bar@baz");

	Domainpart domainpart = entityFullJid.getDomain();
	assertEquals(Domainpart.from("[email protected]"), domainpart);

	Localpart localpart = entityFullJid.getLocalpart();
	assertEquals(Localpart.from("foo"), localpart);

	Resourcepart resourcepart = entityFullJid.getResourcepart();
	assertEquals(Resourcepart.from("bar@baz"), resourcepart);
}
 
Example 6
Source File: JidCreateTest.java    From jxmpp with Apache License 2.0 5 votes vote down vote up
@Test
public void entityFullFromUnsecapedComplexTest() throws XmppStringprepException {
	EntityFullJid entityFullJid = JidCreate.entityFullFromUnescaped("foo@[email protected]/bar@baz");

	Domainpart domainpart = entityFullJid.getDomain();
	assertEquals(Domainpart.from("[email protected]"), domainpart);

	Localpart localpart = entityFullJid.getLocalpart();
	assertEquals(Localpart.from("foo"), localpart);

	Resourcepart resourcepart = entityFullJid.getResourcepart();
	assertEquals(Resourcepart.from("bar@baz"), resourcepart);
}
 
Example 7
Source File: MultiUserChat.java    From Smack with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the nickname that was used to join the room, or <code>null</code> if not
 * currently joined.
 *
 * @return the nickname currently being used.
 */
public Resourcepart getNickname() {
    final EntityFullJid myRoomJid = this.myRoomJid;
    if (myRoomJid == null) {
        return null;
    }
    return myRoomJid.getResourcepart();
}
 
Example 8
Source File: GroupChatRoom.java    From Spark with Apache License 2.0 4 votes vote down vote up
/**
 * Handle all presence packets being sent to this Group Chat Room.
 *
 * @param stanza the presence packet.
 */
private void handlePresencePacket( Stanza stanza )
{
    final Presence presence = (Presence) stanza;
    if ( presence.getError() != null )
    {
        return;
    }

    final EntityFullJid from = presence.getFrom().asEntityFullJidIfPossible();
    if (from == null) {
       return;
    }

    final Resourcepart nickname = from.getResourcepart();

    final MUCUser mucUser = stanza.getExtension( "x", "http://jabber.org/protocol/muc#user" );
    final Set<MUCUser.Status> status = new HashSet<>();
    if ( mucUser != null )
    {
        status.addAll( mucUser.getStatus() );
        final Destroy destroy = mucUser.getDestroy();
        if ( destroy != null )
        {
            UIManager.put( "OptionPane.okButtonText", Res.getString( "ok" ) );
            JOptionPane.showMessageDialog( this,
                    Res.getString( "message.room.destroyed", destroy.getReason() ),
                    Res.getString( "title.room.destroyed" ),
                    JOptionPane.INFORMATION_MESSAGE );
            leaveChatRoom();
            return;
        }
    }

    if ( presence.getType() == Presence.Type.unavailable && !status.contains( MUCUser.Status.NEW_NICKNAME_303 ) )
    {
        if ( currentUserList.contains( from ) )
        {
            if ( pref.isShowJoinLeaveMessagesEnabled() )
            {
                getTranscriptWindow().insertNotificationMessage( Res.getString( "message.user.left.room", nickname ), ChatManager.NOTIFICATION_COLOR );
                scrollToBottom();
            }
            currentUserList.remove( from );
        }
    }
    else
    {
        if ( !currentUserList.contains( from ) )
        {
            currentUserList.add( from );
            getChatInputEditor().setEnabled( true );
            if ( pref.isShowJoinLeaveMessagesEnabled() )
            {
                getTranscriptWindow().insertNotificationMessage(
                        Res.getString( "message.user.joined.room", nickname ),
                        ChatManager.NOTIFICATION_COLOR );
                scrollToBottom();
            }
        }
    }
}
 
Example 9
Source File: GroupChatParticipantList.java    From Spark with Apache License 2.0 4 votes vote down vote up
protected ImageIcon getImageIcon(EntityFullJid participantJID) {
	Resourcepart displayName = participantJID.getResourcepart();
	ImageIcon icon = SparkRes.getImageIcon(SparkRes.GREEN_BALL);
	icon.setDescription(displayName.toString());
	return icon;
}
 
Example 10
Source File: GroupChatParticipantList.java    From Spark with Apache License 2.0 4 votes vote down vote up
protected void addParticipant(final EntityFullJid participantJID, Presence presence) {
// Remove reference to invitees
for (CharSequence displayName : invitees.keySet()) {
    EntityFullJid jid = SparkManager.getUserManager().getJIDFromDisplayName(
	    displayName);

    Occupant occ = chat.getOccupant(jid);
    if (occ != null) {
	String actualJID = occ.getJid().toString();
	if (actualJID.equals(jid)) {
	    removeUser(displayName);
	}
    }
}

Resourcepart nickname = participantJID.getResourcepart();

MUCAffiliation affiliation = null;
MUCRole role = null;
final MUCUser extension = (MUCUser) presence.getExtension( MUCUser.NAMESPACE );
if ( extension != null && extension.getItem() != null )
{
	affiliation = extension.getItem().getAffiliation();
	role = extension.getItem().getRole();
}

if ( affiliation == null ) {
	affiliation = MUCAffiliation.none;
}
if ( role == null ) {
	role = MUCRole.none;
}

usersToRoles.put(participantJID, role);
usersToAffiliation.put(participantJID, affiliation);

Icon icon;
if (_localPreferences.isShowingRoleIcons()) {
    icon = getIconForRole(role, affiliation);
} else {
    icon = PresenceManager.getIconFromPresence(presence);
    if (icon == null) {
		icon = SparkRes.getImageIcon(SparkRes.GREEN_BALL);
	}
}

if (!exists(nickname)) {
    addUser(icon, nickname);
} else {
    int index = getIndex(nickname);
    if (index != -1) {
	final JLabel userLabel = new JLabel(nickname.toString(), icon,
		JLabel.HORIZONTAL);
	model.setElementAt(userLabel, index);
    }
}
   }
 
Example 11
Source File: ChatRoom.java    From Spark with Apache License 2.0 4 votes vote down vote up
/**
 * Handles the Nickname Completion dialog, when Pressing CTRL + SPACE<br>
 * it searches for matches in the current GroupchatList and also in the
 * Roster
 *
 * @throws ChatRoomNotFoundException
 *             when for some reason the GroupChatRoom cannot be found, this
 *             should <u>not</u> happen, since we retrieve it from the
 *             ActiveWindowTab and thus <u>can be ignored</u>
 */
private void handleNickNameCompletion() throws ChatRoomNotFoundException
{
    // Search for a name that starts with the same word as the last word in the chat input editor.
    final String text = getChatInputEditor().getText();
    if ( text == null || text.isEmpty() )
    {
        return;
    }

    final int lastSpaceCharacterIndex = text.lastIndexOf( ' ' ); // -1 when space does not occur.
    final String needle = text.substring( lastSpaceCharacterIndex + 1 );

    final Set<String> matches = new TreeSet<>( String::compareToIgnoreCase );

    if ( SparkManager.getChatManager().getChatContainer().getActiveChatRoom() instanceof GroupChatRoom )
    {
        final GroupChatRoom activeChatRoom = (GroupChatRoom) SparkManager.getChatManager().getChatContainer().getActiveChatRoom();
        for ( EntityFullJid participant : activeChatRoom.getParticipants() )
        {
            final Resourcepart nickname = participant.getResourcepart();
            if ( nickname.toString().toLowerCase().startsWith( needle.toLowerCase() ) )
            {
                matches.add( nickname.toString() );
            }
        }
    }
    else
    {
        for ( RosterEntry re : Roster.getInstanceFor( SparkManager.getConnection() ).getEntries() )
        {
            // Use the name if available, otherwise the localpart of the JID.
            final String username;
            if ( re.getName() != null )
            {
                username = re.getName();
            }
            else
            {
                username = re.getUser().substring( 0, re.getUser().indexOf( '@' ) );
            }

            if ( username.toLowerCase().startsWith( needle.toLowerCase() ) )
            {
                matches.add( username );
            }
        }
    }

    if ( matches.size() == 1 )
    {
        // If we only have 1 match, that match can be used immediately.
        getChatInputEditor().appendText( matches.iterator().next().substring( needle.length() ) );
    }
    else
    {
        // More than one match: create Popupmenu and let the user select one.
        final JPopupMenu popup = new JPopupMenu();
        for ( final String match : matches )
        {
            final JMenuItem menuItem = new JMenuItem( match );
            popup.add( menuItem );
            menuItem.addActionListener( new AbstractAction()
            {
                @Override
                public void actionPerformed( ActionEvent e )
                {
                    getChatInputEditor().appendText( match.substring( needle.length() ) );
                    popup.setVisible( false );
                }
            } );
        }

        popup.show( SparkManager.getChatManager().getChatContainer(),
                    getChatInputEditor().getCaret().getMagicCaretPosition().x,
                    SparkManager.getChatManager().getChatContainer().getHeight() - 20 );
    }
}