Search in sources :

Example 6 with UserAlreadyExistsException

use of org.jivesoftware.openfire.user.UserAlreadyExistsException in project Openfire by igniterealtime.

the class LocalMUCUser method process.

public void process(Presence packet) {
    // Ignore presences of type ERROR sent to a room
    if (Presence.Type.error == packet.getType()) {
        return;
    }
    lastPacketTime = System.currentTimeMillis();
    JID recipient = packet.getTo();
    String group = recipient.getNode();
    if (group != null) {
        MUCRole role = roles.get(group);
        Element mucInfo = packet.getChildElement("x", "http://jabber.org/protocol/muc");
        if (role == null || mucInfo != null) {
            // Alternative is that mucInfo is not null, in which case the client thinks it isn't in the room, so we should join anyway.
            if (recipient.getResource() != null && recipient.getResource().trim().length() > 0) {
                if (packet.isAvailable()) {
                    try {
                        // Get or create the room
                        MUCRoom room = server.getChatRoom(group, packet.getFrom());
                        // User must support MUC in order to create a room
                        HistoryRequest historyRequest = null;
                        String password = null;
                        // Check for password & requested history if client supports MUC
                        if (mucInfo != null) {
                            password = mucInfo.elementTextTrim("password");
                            if (mucInfo.element("history") != null) {
                                historyRequest = new HistoryRequest(mucInfo);
                            }
                        }
                        // The user joins the room
                        role = room.joinRoom(recipient.getResource().trim(), password, historyRequest, this, packet.createCopy());
                        // unlock the room thus creating an "instant" room
                        if (mucInfo == null && room.isLocked() && !room.isManuallyLocked()) {
                            room.unlock(role);
                        }
                    } catch (UnauthorizedException e) {
                        sendErrorPacket(packet, PacketError.Condition.not_authorized);
                    } catch (ServiceUnavailableException e) {
                        sendErrorPacket(packet, PacketError.Condition.service_unavailable);
                    } catch (UserAlreadyExistsException | ConflictException e) {
                        sendErrorPacket(packet, PacketError.Condition.conflict);
                    } catch (RoomLockedException e) {
                        // If a user attempts to enter a room while it is "locked" (i.e., before the room creator provides an initial configuration and therefore before the room officially exists), the service MUST refuse entry and return an <item-not-found/> error to the user
                        sendErrorPacket(packet, PacketError.Condition.item_not_found);
                    } catch (ForbiddenException e) {
                        sendErrorPacket(packet, PacketError.Condition.forbidden);
                    } catch (RegistrationRequiredException e) {
                        sendErrorPacket(packet, PacketError.Condition.registration_required);
                    } catch (NotAcceptableException e) {
                        sendErrorPacket(packet, PacketError.Condition.not_acceptable);
                    } catch (NotAllowedException e) {
                        sendErrorPacket(packet, PacketError.Condition.not_allowed);
                    }
                } else {
                // TODO: send error message to user (can't send presence to group you
                // haven't joined)
                }
            } else {
                if (packet.isAvailable()) {
                    // A resource is required in order to join a room
                    // http://xmpp.org/extensions/xep-0045.html#enter
                    // If the user does not specify a room nickname (note the bare JID on the 'from' address in the following example), the service MUST return a <jid-malformed/> error
                    sendErrorPacket(packet, PacketError.Condition.jid_malformed);
                }
            // TODO: send error message to user (can't send packets to group you haven't
            // joined)
            }
        } else {
            // In other words, another user already has this nickname
            if (!role.getUserAddress().equals(packet.getFrom())) {
                sendErrorPacket(packet, PacketError.Condition.conflict);
            } else {
                if (Presence.Type.unavailable == packet.getType()) {
                    try {
                        // TODO Consider that different nodes can be creating and processing this presence at the same time (when remote node went down)
                        removeRole(group);
                        role.getChatRoom().leaveRoom(role);
                    } catch (Exception e) {
                        Log.error(e.getMessage(), e);
                    }
                } else {
                    try {
                        String resource = (recipient.getResource() == null || recipient.getResource().trim().length() == 0 ? null : recipient.getResource().trim());
                        if (resource == null || role.getNickname().equalsIgnoreCase(resource)) {
                            // Occupant has changed his availability status
                            role.getChatRoom().presenceUpdated(role, packet);
                        } else {
                            // Check if occupants are allowed to change their nicknames
                            if (!role.getChatRoom().canChangeNickname()) {
                                sendErrorPacket(packet, PacketError.Condition.not_acceptable);
                            } else // Answer a conflic error if the new nickname is taken
                            if (role.getChatRoom().hasOccupant(resource)) {
                                sendErrorPacket(packet, PacketError.Condition.conflict);
                            } else {
                                // Send "unavailable" presence for the old nickname
                                Presence presence = role.getPresence().createCopy();
                                // Switch the presence to OFFLINE
                                presence.setType(Presence.Type.unavailable);
                                presence.setStatus(null);
                                // Add the new nickname and status 303 as properties
                                Element frag = presence.getChildElement("x", "http://jabber.org/protocol/muc#user");
                                frag.element("item").addAttribute("nick", resource);
                                frag.addElement("status").addAttribute("code", "303");
                                role.getChatRoom().send(presence);
                                // Send availability presence for the new nickname
                                String oldNick = role.getNickname();
                                role.getChatRoom().nicknameChanged(role, packet, oldNick, resource);
                            }
                        }
                    } catch (Exception e) {
                        Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
                    }
                }
            }
        }
    } else {
        // Packets to the groupchat server. This should not occur (should be handled by MultiUserChatServiceImpl instead)
        Log.warn(LocaleUtils.getLocalizedString("muc.error.not-supported") + " " + packet.toString());
    }
}
Also used : Element(org.dom4j.Element) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) PacketException(org.jivesoftware.openfire.PacketException) UnauthorizedException(org.jivesoftware.openfire.auth.UnauthorizedException) NotFoundException(org.jivesoftware.util.NotFoundException) UnauthorizedException(org.jivesoftware.openfire.auth.UnauthorizedException)

Example 7 with UserAlreadyExistsException

use of org.jivesoftware.openfire.user.UserAlreadyExistsException in project Openfire by igniterealtime.

the class NativeAuthProvider method authenticate.

@Override
public void authenticate(String username, String password) throws UnauthorizedException {
    if (username.contains("@")) {
        // Check that the specified domain matches the server's domain
        int index = username.indexOf("@");
        String domain = username.substring(index + 1);
        if (domain.equals(XMPPServer.getInstance().getServerInfo().getXMPPDomain())) {
            username = username.substring(0, index);
        } else {
            // Unknown domain. Return authentication failed.
            throw new UnauthorizedException();
        }
    }
    try {
        // very well. Therefore, synchronize access to Shaj to throttle auth checks.
        synchronized (this) {
            if (!Shaj.checkPassword(domain, username, password)) {
                throw new UnauthorizedException();
            }
        }
    } catch (UnauthorizedException ue) {
        throw ue;
    } catch (Exception e) {
        throw new UnauthorizedException(e);
    }
    // See if the user exists in the database. If not, automatically create them.
    UserManager userManager = UserManager.getInstance();
    try {
        userManager.getUser(username);
    } catch (UserNotFoundException unfe) {
        try {
            Log.debug("Automatically creating new user account for " + username);
            // Create user; use a random password for better safety in the future.
            // Note that we have to go to the user provider directly -- because the
            // provider is read-only, UserManager will usually deny access to createUser.
            UserProvider provider = UserManager.getUserProvider();
            if (!(provider instanceof NativeUserProvider)) {
                Log.error("Error: not using NativeUserProvider so authentication with " + "NativeAuthProvider will likely fail. Using: " + provider.getClass().getName());
            }
            UserManager.getUserProvider().createUser(username, StringUtils.randomString(8), null, null);
        } catch (UserAlreadyExistsException uaee) {
        // Ignore.
        }
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) UserProvider(org.jivesoftware.openfire.user.UserProvider) NativeUserProvider(org.jivesoftware.openfire.user.NativeUserProvider) UserManager(org.jivesoftware.openfire.user.UserManager) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) NativeUserProvider(org.jivesoftware.openfire.user.NativeUserProvider)

Example 8 with UserAlreadyExistsException

use of org.jivesoftware.openfire.user.UserAlreadyExistsException in project Openfire by igniterealtime.

the class DefaultRosterItemProvider method createItem.

/* (non-Javadoc)
	 * @see org.jivesoftware.openfire.roster.RosterItemProvider#createItem(java.lang.String, org.jivesoftware.openfire.roster.RosterItem)
	 */
@Override
public RosterItem createItem(String username, RosterItem item) throws UserAlreadyExistsException {
    Connection con = null;
    PreparedStatement pstmt = null;
    try {
        long rosterID = SequenceManager.nextID(JiveConstants.ROSTER);
        con = DbConnectionManager.getConnection();
        pstmt = con.prepareStatement(CREATE_ROSTER_ITEM);
        pstmt.setString(1, username);
        pstmt.setLong(2, rosterID);
        pstmt.setString(3, item.getJid().toBareJID());
        pstmt.setInt(4, item.getSubStatus().getValue());
        pstmt.setInt(5, item.getAskStatus().getValue());
        pstmt.setInt(6, item.getRecvStatus().getValue());
        pstmt.setString(7, item.getNickname());
        pstmt.executeUpdate();
        item.setID(rosterID);
        insertGroups(rosterID, item.getGroups().iterator(), con);
    } catch (SQLException e) {
        Log.warn("Error trying to insert a new row in ofRoster", e);
        throw new UserAlreadyExistsException(item.getJid().toBareJID());
    } finally {
        DbConnectionManager.closeConnection(pstmt, con);
    }
    return item;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException)

Example 9 with UserAlreadyExistsException

use of org.jivesoftware.openfire.user.UserAlreadyExistsException in project Openfire by igniterealtime.

the class JustMarriedPlugin method addNewUserToOthersRoster.

private static void addNewUserToOthersRoster(User newUser, RosterItem otherItem, String currentUser) {
    otherItem.getJid();
    UserManager userManager = UserManager.getInstance();
    // Is this user registered with our OF server?
    String username = otherItem.getJid().getNode();
    if (username != null && username.length() > 0 && userManager.isRegisteredUser(username) && XMPPServer.getInstance().isLocal(XMPPServer.getInstance().createJID(currentUser, null))) {
        try {
            User otherUser = userManager.getUser(username);
            Roster otherRoster = otherUser.getRoster();
            RosterItem oldUserOnOthersRoster = otherRoster.getRosterItem(XMPPServer.getInstance().createJID(currentUser, null));
            try {
                if (!oldUserOnOthersRoster.isOnlyShared()) {
                    RosterItem justCreated = otherRoster.createRosterItem(XMPPServer.getInstance().createJID(newUser.getUsername(), null), oldUserOnOthersRoster.getNickname(), oldUserOnOthersRoster.getGroups(), true, true);
                    justCreated.setAskStatus(oldUserOnOthersRoster.getAskStatus());
                    justCreated.setRecvStatus(oldUserOnOthersRoster.getRecvStatus());
                    justCreated.setSubStatus(oldUserOnOthersRoster.getSubStatus());
                    otherRoster.updateRosterItem(justCreated);
                }
            } catch (UserAlreadyExistsException e) {
                Log.error("Could not create roster item for user " + newUser.getUsername(), e);
            } catch (SharedGroupException e) {
                Log.error(e);
            }
        } catch (UserNotFoundException e) {
            Log.error("Could not create roster item for user " + newUser.getUsername() + " because it is a contact from a shared group", e);
        }
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) RosterItem(org.jivesoftware.openfire.roster.RosterItem) User(org.jivesoftware.openfire.user.User) Roster(org.jivesoftware.openfire.roster.Roster) UserManager(org.jivesoftware.openfire.user.UserManager) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) SharedGroupException(org.jivesoftware.openfire.SharedGroupException)

Example 10 with UserAlreadyExistsException

use of org.jivesoftware.openfire.user.UserAlreadyExistsException in project Openfire by igniterealtime.

the class JustMarriedPlugin method changeName.

public static boolean changeName(String currentUserName, String newUserName, boolean deleteOldUser, String newEmail, String newRealName) {
    UserManager userManager = UserManager.getInstance();
    try {
        User currentUser = userManager.getUser(currentUserName);
        // Old user found, create new one
        String password = AuthFactory.getPassword(currentUserName);
        String newName = (newRealName == null || newRealName.length() == 0) ? currentUser.getName() : newRealName;
        String newMail = (newEmail == null || newEmail.length() == 0) ? currentUser.getEmail() : newEmail;
        User newUser = userManager.createUser(newUserName, password, currentUser.getName(), newMail);
        newUser.setName(newName);
        newUser.setNameVisible(currentUser.isNameVisible());
        newUser.setEmailVisible(currentUser.isEmailVisible());
        newUser.setCreationDate(currentUser.getCreationDate());
        copyRoster(currentUser, newUser, currentUserName);
        copyProperties(currentUser, newUser);
        copyToGroups(currentUserName, newUserName);
        copyVCard(currentUserName, newUserName);
        if (deleteOldUser) {
            deleteUser(currentUser);
        }
    } catch (UserNotFoundException e) {
        Log.error("Could not find user " + currentUserName, e);
        return false;
    } catch (UserAlreadyExistsException e) {
        Log.error("Could not create user " + newUserName, e);
        return false;
    }
    return true;
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) User(org.jivesoftware.openfire.user.User) UserManager(org.jivesoftware.openfire.user.UserManager) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException)

Aggregations

UserAlreadyExistsException (org.jivesoftware.openfire.user.UserAlreadyExistsException)23 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)19 RosterItem (org.jivesoftware.openfire.roster.RosterItem)10 Roster (org.jivesoftware.openfire.roster.Roster)9 UserManager (org.jivesoftware.openfire.user.UserManager)7 JID (org.xmpp.packet.JID)7 SharedGroupException (org.jivesoftware.openfire.SharedGroupException)6 Element (org.dom4j.Element)5 User (org.jivesoftware.openfire.user.User)5 ArrayList (java.util.ArrayList)4 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)4 ServiceException (org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException)4 StringprepException (gnu.inet.encoding.StringprepException)3 IOException (java.io.IOException)3 NotFoundException (org.jivesoftware.util.NotFoundException)3 PrintWriter (java.io.PrintWriter)2 List (java.util.List)2 StringTokenizer (java.util.StringTokenizer)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2