rostermanager.java
来自「基于Jabber协议的即时消息服务器」· Java 代码 · 共 924 行 · 第 1/3 页
JAVA
924 行
sendSubscribeRequest(addedUser, userToUpdate, true);
}
}
}
}
/**
* Notification that a Group user has been deleted. Update the group users' roster accordingly.
*
* @param group the group from where the user was deleted.
* @param deletedUser the username of the user that has been deleted from the group.
*/
private void groupUserDeleted(Group group, JID deletedUser) {
groupUserDeleted(group, getAffectedUsers(group), deletedUser);
}
/**
* Notification that a Group user has been deleted. Update the group users' roster accordingly.
*
* @param group the group from where the user was deleted.
* @param users the users to update their rosters
* @param deletedUser the username of the user that has been deleted from the group.
*/
private void groupUserDeleted(Group group, Collection<JID> users, JID deletedUser) {
// Get the roster of the deleted user.
Roster deletedUserRoster = null;
if (server.isLocal(deletedUser)) {
deletedUserRoster = (Roster) CacheManager.getCache("Roster").get(deletedUser.getNode());
}
// Iterate on all the affected users and update their rosters
for (JID userToUpdate : users) {
// Get the roster to update
Roster roster = null;
if (server.isLocal(userToUpdate)) {
// Check that the user exists, if not then continue with the next user
try {
UserManager.getInstance().getUser(userToUpdate.getNode());
}
catch (UserNotFoundException e) {
continue;
}
roster = (Roster) CacheManager.getCache("Roster").get(userToUpdate.getNode());
}
// Only update rosters in memory
if (roster != null) {
roster.deleteSharedUser(group, deletedUser);
}
// Check if the roster is still not in memory
if (deletedUserRoster == null && server.isLocal(deletedUser)) {
deletedUserRoster =
(Roster) CacheManager.getCache("Roster").get(deletedUser.getNode());
}
// Update the roster of the newly deleted group user.
if (deletedUserRoster != null) {
deletedUserRoster.deleteSharedUser(userToUpdate, group);
}
if (!server.isLocal(deletedUser)) {
// Unsusbcribe from the presence of the remote user. This is only necessary for
// remote users and may only work with remote users that **automatically**
// accept presence subscription requests
sendSubscribeRequest(userToUpdate, deletedUser, false);
}
if (!server.isLocal(userToUpdate)) {
// Unsusbcribe from the presence of the remote user. This is only necessary for
// remote users and may only work with remote users that **automatically**
// accept presence subscription requests
sendSubscribeRequest(deletedUser, userToUpdate, false);
}
}
}
private void sendSubscribeRequest(JID sender, JID recipient, boolean isSubscribe) {
Presence presence = new Presence();
presence.setFrom(sender);
presence.setTo(recipient);
if (isSubscribe) {
presence.setType(Presence.Type.subscribe);
}
else {
presence.setType(Presence.Type.unsubscribe);
}
try {
ChannelHandler handler = routingTable.getRoute(recipient);
if (handler != null) {
handler.process(presence);
}
}
catch (UnauthorizedException e) {
// Do nothing
}
}
private Collection<Group> getVisibleGroups(Group groupToCheck) {
Collection<Group> answer = new HashSet<Group>();
Collection<Group> groups = GroupManager.getInstance().getSharedGroups();
for (Group group : groups) {
if (group.equals(groupToCheck)) {
continue;
}
String showInRoster = group.getProperties().get("sharedRoster.showInRoster");
if ("onlyGroup".equals(showInRoster)) {
// Check if the user belongs to a group that may see this group
Collection<String> groupList =
parseGroupNames(group.getProperties().get("sharedRoster.groupList"));
if (groupList.contains(groupToCheck.getName())) {
answer.add(group);
}
}
else if ("everybody".equals(showInRoster)) {
answer.add(group);
}
}
return answer;
}
/**
* Returns true if a given group is visible to a given user. That means, if the user can
* see the group in his roster.
*
* @param group the group to check if the user can see.
* @param user the JID of the user to check if he may see the group.
* @return true if a given group is visible to a given user.
*/
boolean isGroupVisible(Group group, JID user) {
String showInRoster = group.getProperties().get("sharedRoster.showInRoster");
if ("everybody".equals(showInRoster)) {
return true;
}
else if ("onlyGroup".equals(showInRoster)) {
if (group.isUser(user)) {
return true;
}
// Check if the user belongs to a group that may see this group
Collection<Group> groupList = parseGroups(group.getProperties().get(
"sharedRoster.groupList"));
for (Group groupInList : groupList) {
if (groupInList.isUser(user)) {
return true;
}
}
}
return false;
}
/**
* Returns all the users that are related to a shared group. This is the logic that we are
* using: 1) If the group visiblity is configured as "Everybody" then all users in the system or
* all logged users in the system will be returned (configurable thorugh the "filterOffline"
* flag), 2) if the group visiblity is configured as "onlyGroup" then all the group users will
* be included in the answer and 3) if the group visiblity is configured as "onlyGroup" and
* the group allows other groups to include the group in the groups users' roster then all
* the users of the allowed groups will be included in the answer.
*/
private Collection<JID> getAffectedUsers(Group group) {
return getAffectedUsers(group, group.getProperties().get("sharedRoster.showInRoster"),
group.getProperties().get("sharedRoster.groupList"));
}
/**
* This method is similar to {@link #getAffectedUsers(Group)} except that it receives
* some group properties. The group properties are passed as parameters since the called of this
* method may want to obtain the related users of the group based in some properties values.
*
* This is useful when the group is being edited and some properties has changed and we need to
* obtain the related users of the group based on the previous group state.
*/
private Collection<JID> getAffectedUsers(Group group, String showInRoster, String groupNames) {
// Answer an empty collection if the group is not being shown in users' rosters
if (!"onlyGroup".equals(showInRoster) && !"everybody".equals(showInRoster)) {
return new ArrayList<JID>();
}
// Add the users of the group
Collection<JID> users = new HashSet<JID>(group.getMembers());
users.addAll(group.getAdmins());
// Check if anyone can see this shared group
if ("everybody".equals(showInRoster)) {
// Add all users in the system
for (String username : UserManager.getInstance().getUsernames()) {
users.add(server.createJID(username, null));
}
// Add all logged users. We don't need to add all users in the system since only the
// logged ones will be affected.
//users.addAll(SessionManager.getInstance().getSessionUsers());
}
else {
// Add the users that may see the group
Collection<Group> groupList = parseGroups(groupNames);
for (Group groupInList : groupList) {
users.addAll(groupInList.getMembers());
users.addAll(groupInList.getAdmins());
}
}
return users;
}
Collection<JID> getSharedUsersForRoster(Group group, Roster roster) {
String showInRoster = group.getProperties().get("sharedRoster.showInRoster");
String groupNames = group.getProperties().get("sharedRoster.groupList");
// Answer an empty collection if the group is not being shown in users' rosters
if (!"onlyGroup".equals(showInRoster) && !"everybody".equals(showInRoster)) {
return new ArrayList<JID>();
}
// Add the users of the group
Collection<JID> users = new HashSet<JID>(group.getMembers());
users.addAll(group.getAdmins());
// If the user of the roster belongs to the shared group then we should return
// users that need to be in the roster with subscription "from"
if (group.isUser(roster.getUsername())) {
// Check if anyone can see this shared group
if ("everybody".equals(showInRoster)) {
// Add all users in the system
for (String username : UserManager.getInstance().getUsernames()) {
users.add(server.createJID(username, null));
}
}
else {
// Add the users that may see the group
Collection<Group> groupList = parseGroups(groupNames);
for (Group groupInList : groupList) {
users.addAll(groupInList.getMembers());
users.addAll(groupInList.getAdmins());
}
}
}
return users;
}
/**
* Returns true if a group in the first collection may mutually see a group of the
* second collection. More precisely, return true if both collections contain a public
* group (i.e. anybody can see the group) or if both collection have a group that may see
* each other and the users are members of those groups or if one group is public and the
* other group allowed the public group to see it.
*
* @param user the name of the user associated to the first collection of groups. This is always a local user.
* @param groups a collection of groups to check against the other collection of groups.
* @param otherUser the JID of the user associated to the second collection of groups.
* @param otherGroups the other collection of groups to check against the first collection.
* @return true if a group in the first collection may mutually see a group of the
* second collection.
*/
boolean hasMutualVisibility(String user, Collection<Group> groups, JID otherUser,
Collection<Group> otherGroups) {
for (Group group : groups) {
for (Group otherGroup : otherGroups) {
// Skip this groups if the users are not group users of the groups
if (!group.isUser(user) || !otherGroup.isUser(otherUser)) {
continue;
}
if (group.equals(otherGroup)) {
return true;
}
String showInRoster = group.getProperties().get("sharedRoster.showInRoster");
String otherShowInRoster = otherGroup.getProperties().get("sharedRoster.showInRoster");
// Return true if both groups are public groups (i.e. anybody can see them)
if ("everybody".equals(showInRoster) && "everybody".equals(otherShowInRoster)) {
return true;
}
else if ("onlyGroup".equals(showInRoster) && "onlyGroup".equals(otherShowInRoster)) {
String groupNames = group.getProperties().get("sharedRoster.groupList");
String otherGroupNames = otherGroup.getProperties().get("sharedRoster.groupList");
// Return true if each group may see the other group
if (groupNames != null && otherGroupNames != null) {
if (groupNames.contains(otherGroup.getName()) &&
otherGroupNames.contains(group.getName())) {
return true;
}
}
}
else if ("everybody".equals(showInRoster) && "onlyGroup".equals(otherShowInRoster)) {
// Return true if one group is public and the other group allowed the public
// group to see him
String otherGroupNames = otherGroup.getProperties().get("sharedRoster.groupList");
if (otherGroupNames != null && otherGroupNames.contains(group.getName())) {
return true;
}
}
else if ("onlyGroup".equals(showInRoster) && "everybody".equals(otherShowInRoster)) {
// Return true if one group is public and the other group allowed the public
// group to see him
String groupNames = group.getProperties().get("sharedRoster.groupList");
// Return true if each group may see the other group
if (groupNames != null && groupNames.contains(otherGroup.getName())) {
return true;
}
}
}
}
return false;
}
public void start() throws IllegalStateException {
super.start();
// Add this module as a user event listener so we can update
// rosters when users are created or deleted
UserEventDispatcher.addListener(this);
}
public void stop() {
super.stop();
// Remove this module as a user event listener
UserEventDispatcher.removeListener(this);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?