RosterManager.java 31.5 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
Matt Tucker's avatar
Matt Tucker committed
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
Matt Tucker's avatar
Matt Tucker committed
8 9
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
Matt Tucker's avatar
Matt Tucker committed
10
 */
Matt Tucker's avatar
Matt Tucker committed
11

Matt Tucker's avatar
Matt Tucker committed
12
package org.jivesoftware.messenger.roster;
Matt Tucker's avatar
Matt Tucker committed
13

Matt Tucker's avatar
Matt Tucker committed
14
import org.xmpp.packet.JID;
Matt Tucker's avatar
Matt Tucker committed
15 16 17
import org.jivesoftware.util.Cache;
import org.jivesoftware.util.CacheManager;
import org.jivesoftware.messenger.container.BasicModule;
Matt Tucker's avatar
Matt Tucker committed
18
import org.jivesoftware.messenger.user.UserNotFoundException;
19 20
import org.jivesoftware.messenger.user.User;
import org.jivesoftware.messenger.user.UserManager;
21
import org.jivesoftware.messenger.SharedGroupException;
22 23
import org.jivesoftware.messenger.event.GroupEventListener;
import org.jivesoftware.messenger.event.GroupEventDispatcher;
24
import org.jivesoftware.messenger.group.Group;
25 26
import org.jivesoftware.messenger.group.GroupManager;
import org.jivesoftware.messenger.group.GroupNotFoundException;
27

28
import java.util.*;
Matt Tucker's avatar
Matt Tucker committed
29

Matt Tucker's avatar
Matt Tucker committed
30 31 32 33 34 35 36 37 38 39 40
/**
 * A simple service that allows components to retrieve a roster based solely on the ID
 * of the owner. Users have convenience methods for obtaining a roster associated with
 * the owner. However there are many components that need to retrieve the roster
 * based solely on the generic ID owner key. This interface defines a service that can
 * do that. This allows classes that generically manage resource for resource owners
 * (such as presence updates) to generically offer their services without knowing or
 * caring if the roster owner is a user, chatbot, etc.
 *
 * @author Iain Shigeoka
 */
41
public class RosterManager extends BasicModule implements GroupEventListener {
Matt Tucker's avatar
Matt Tucker committed
42 43 44

    private Cache rosterCache = null;

Matt Tucker's avatar
Matt Tucker committed
45
    public RosterManager() {
Matt Tucker's avatar
Matt Tucker committed
46
        super("Roster Manager");
47 48
        // Add the new instance as a listener of group events
        GroupEventDispatcher.addListener(this);
Matt Tucker's avatar
Matt Tucker committed
49 50
    }

Matt Tucker's avatar
Matt Tucker committed
51 52 53 54 55
    /**
     * Returns the roster for the given username.
     *
     * @param username the username to search for.
     * @return the roster associated with the ID.
56 57
     * @throws org.jivesoftware.messenger.user.UserNotFoundException if the ID does not correspond
     *         to a known entity on the server.
Matt Tucker's avatar
Matt Tucker committed
58
     */
59
    public Roster getRoster(String username) throws UserNotFoundException {
Matt Tucker's avatar
Matt Tucker committed
60
        if (rosterCache == null) {
61
            rosterCache = CacheManager.getCache("username2roster");
Matt Tucker's avatar
Matt Tucker committed
62 63 64 65
        }
        if (rosterCache == null) {
            throw new UserNotFoundException("Could not load caches");
        }
66
        Roster roster = (Roster)rosterCache.get(username);
Matt Tucker's avatar
Matt Tucker committed
67
        if (roster == null) {
68
            // Not in cache so load a new one:
69
            roster = new Roster(username);
70
            rosterCache.put(username, roster);
Matt Tucker's avatar
Matt Tucker committed
71 72
        }
        if (roster == null) {
73
            throw new UserNotFoundException(username);
Matt Tucker's avatar
Matt Tucker committed
74 75 76
        }
        return roster;
    }
77

Matt Tucker's avatar
Matt Tucker committed
78 79 80 81 82 83
    /**
     * Removes the entire roster of a given user. This is necessary when a user
     * account is being deleted from the server.
     *
     * @param user the user.
     */
Matt Tucker's avatar
Matt Tucker committed
84
    public void deleteRoster(JID user) {
85
        try {
Matt Tucker's avatar
Matt Tucker committed
86
            String username = user.getNode();
87 88 89 90
            // Get the roster of the deleted user
            Roster roster = (Roster)CacheManager.getCache("username2roster").get(username);
            if (roster == null) {
                // Not in cache so load a new one:
91
                roster = new Roster(username);
92 93
            }
            // Remove each roster item from the user's roster
Gaston Dombiak's avatar
Gaston Dombiak committed
94
            for (RosterItem item : roster.getRosterItems()) {
95 96 97 98 99 100
                try {
                    roster.deleteRosterItem(item.getJid(), false);
                }
                catch (SharedGroupException e) {
                    // Do nothing. We shouldn't have this exception since we disabled the checkings
                }
101 102 103 104 105
            }
            // Remove the cached roster from memory
            CacheManager.getCache("username2roster").remove(username);

            // Get the rosters that have a reference to the deleted user
Matt Tucker's avatar
Matt Tucker committed
106
            RosterItemProvider rosteItemProvider = RosterItemProvider.getInstance();
Matt Tucker's avatar
Matt Tucker committed
107
            Iterator<String> usernames = rosteItemProvider.getUsernames(user.toBareJID());
108 109 110 111 112 113
            while (usernames.hasNext()) {
                username = usernames.next();
                // Get the roster that has a reference to the deleted user
                roster = (Roster)CacheManager.getCache("username2roster").get(username);
                if (roster == null) {
                    // Not in cache so load a new one:
114
                    roster = new Roster(username);
115 116
                }
                // Remove the deleted user reference from this roster
117 118 119 120 121 122
                try {
                    roster.deleteRosterItem(user, false);
                }
                catch (SharedGroupException e) {
                    // Do nothing. We shouldn't have this exception since we disabled the checkings
                }
123 124 125 126 127 128
            }
        }
        catch (UnsupportedOperationException e) {
            // Do nothing
        }
    }
129

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    /**
     * Returns a collection with all the groups that the user may include in his roster. The
     * following criteria will be used to select the groups: 1) Groups that are configured so that
     * everybody can include in his roster, 2) Groups that are configured so that its users may
     * include the group in their rosters and the user is a group user of the group and 3) User
     * belongs to a Group that may see a Group that whose members may include the Group in their
     * rosters.
     *
     * @param user the user to return his shared groups.
     * @return a collection with all the groups that the user may include in his roster.
     */
    public Collection<Group> getSharedGroups(User user) {
        Collection<Group> answer = new HashSet<Group>();
        Collection<Group> groups = GroupManager.getInstance().getGroups();
        for (Group group : groups) {
            String showInRoster = group.getProperties().get("sharedRoster.showInRoster");
            if ("onlyGroup".equals(showInRoster)) {
                if (group.isUser(user.getUsername())) {
                    // The user belongs to the group so add the group to the answer
                    answer.add(group);
                }
                else {
                    // 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.getUsername())) {
156
                            answer.add(group);
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
                        }
                    }
                }
            }
            else if ("everybody".equals(showInRoster)) {
                // Anyone can see this group so add the group to the answer
                answer.add(group);
            }
        }
        return answer;
    }

    /**
     * Returns a collection of Groups obtained by parsing a comma delimited String with the name
     * of groups.
     *
     * @param groupNames a comma delimited string with group names.
     * @return a collection of Groups obtained by parsing a comma delimited String with the name
     *         of groups.
     */
    private Collection<Group> parseGroups(String groupNames) {
        Collection<Group> answer = new HashSet<Group>();
        if (groupNames != null) {
            StringTokenizer tokenizer = new StringTokenizer(groupNames, ",");
            while (tokenizer.hasMoreTokens()) {
                String groupName = tokenizer.nextToken();
                try {
                    answer.add(GroupManager.getInstance().getGroup(groupName));
                }
                catch (GroupNotFoundException e) {
                    // Do nothing. Silently ignore the invalid reference to the group
                }
            }
        }
        return answer;
    }

194 195 196 197 198
    public void groupCreated(Group group, Map params) {
        //Do nothing
    }

    public void groupDeleting(Group group, Map params) {
199
        // Iterate on all the group users and update their rosters
200
        for (String deletedUser : getAffectedUsers(group)) {
201 202 203 204
            groupUserDeleted(group, deletedUser);
        }
    }

205
    public void groupModified(Group group, Map params) {
206 207 208 209
        // Do nothing if no group property has been modified
        if (!"propertyModified".equals(params.get("type"))) {
             return;
        }
210
        String keyChanged = (String) params.get("propertyKey");
211 212
        String originalValue = (String) params.get("originalValue");

213 214 215

        if ("sharedRoster.showInRoster".equals(keyChanged)) {
            String currentValue = group.getProperties().get("sharedRoster.showInRoster");
216 217 218 219
            // Nothing has changed so do nothing.
            if (currentValue.equals(originalValue)) {
                return;
            }
220 221 222 223 224
            // Get the users of the group
            Collection<String> users = new HashSet<String>(group.getMembers());
            users.addAll(group.getAdmins());
            // Get the users whose roster will be affected
            Collection<String> affectedUsers = getAffectedUsers(group, originalValue,
225
                    group.getProperties().get("sharedRoster.groupList"));
226
            // Remove the group members from the affected rosters
227
            for (String deletedUser : users) {
228
                groupUserDeleted(group, affectedUsers, deletedUser);
229
            }
230 231 232

            // Simulate that the group users has been added to the group. This will cause to push
            // roster items to the "affected" users for the group users
233
            //Collection<Group> visibleGroups = getVisibleGroups(group);
234 235
            for (String user : users) {
                groupUserAdded(group, user);
236 237 238
                /*for (Group visibleGroup : visibleGroups) {
                    addSharedGroupToRoster(visibleGroup, user);
                }*/
239 240
            }
        }
241 242
        else if ("sharedRoster.groupList".equals(keyChanged)) {
            String currentValue = group.getProperties().get("sharedRoster.groupList");
243 244 245 246
            // Nothing has changed so do nothing.
            if (currentValue.equals(originalValue)) {
                return;
            }
247 248 249 250 251
            // Get the users of the group
            Collection<String> users = new HashSet<String>(group.getMembers());
            users.addAll(group.getAdmins());
            // Get the users whose roster will be affected
            Collection<String> affectedUsers = getAffectedUsers(group,
252
                    group.getProperties().get("sharedRoster.showInRoster"), originalValue);
253
            // Remove the group members from the affected rosters
254
            for (String deletedUser : users) {
255
                groupUserDeleted(group, affectedUsers, deletedUser);
256
            }
257 258 259

            // Simulate that the group users has been added to the group. This will cause to push
            // roster items to the "affected" users for the group users
260
            //Collection<Group> visibleGroups = getVisibleGroups(group);
261 262
            for (String user : users) {
                groupUserAdded(group, user);
263 264 265
                /*for (Group visibleGroup : visibleGroups) {
                    addSharedGroupToRoster(visibleGroup, user);
                }*/
266 267 268 269 270 271
            }
        }
        else if ("sharedRoster.displayName".equals(keyChanged)) {
            String currentValue = group.getProperties().get("sharedRoster.displayName");
            // Nothing has changed so do nothing.
            if (currentValue.equals(originalValue)) {
272 273
                return;
            }
274 275 276 277 278
            // Do nothing if the group is not being shown in users' rosters
            if (!isSharedGroup(group)) {
                return;
            }
            // Get all the affected users
279
            Collection<String> users = getAffectedUsers(group);
280
            // Iterate on all the affected users and update their rosters
281 282 283 284 285
            for (String updatedUser : users) {
                // Get the roster to update.
                Roster roster = (Roster) CacheManager.getCache("username2roster").get(updatedUser);
                if (roster != null) {
                    // Update the roster with the new group display name
286
                    roster.shareGroupRenamed(users);
287 288 289
                }
            }
        }
290 291
    }

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
    /**
     * Returns true if the specified Group may be included in a user roster. The decision is made
     * based on the group properties that are configurable through the Admin Console.
     *
     * @param group the group to check if it may be considered a shared group.
     * @return true if the specified Group may be included in a user roster.
     */
    public boolean isSharedGroup(Group group) {
        String showInRoster = group.getProperties().get("sharedRoster.showInRoster");
        if ("onlyGroup".equals(showInRoster) || "everybody".equals(showInRoster)) {
            return true;
        }
        return false;
    }

307
    public void memberAdded(Group group, Map params) {
308
        String addedUser = (String) params.get("member");
Gaston Dombiak's avatar
Gaston Dombiak committed
309 310 311 312
        // Do nothing if the user was an admin that became a member
        if (group.getAdmins().contains(addedUser)) {
            return;
        }
313
        if (!isSharedGroup(group)) {
314 315 316 317 318 319
            for (Group visibleGroup : getVisibleGroups(group)) {
                addSharedGroupToRoster(visibleGroup, addedUser);
            }
        }
        else {
            groupUserAdded(group, addedUser);
320
        }
321 322 323
    }

    public void memberRemoved(Group group, Map params) {
324
        String deletedUser = (String) params.get("member");
Gaston Dombiak's avatar
Gaston Dombiak committed
325 326 327 328
        // Do nothing if the user is still an admin
        if (group.getAdmins().contains(deletedUser)) {
            return;
        }
329
        if (!isSharedGroup(group)) {
330 331 332 333 334 335
            for (Group visibleGroup : getVisibleGroups(group)) {
                removeSharedGroupFromRoster(visibleGroup, deletedUser);
            }
        }
        else {
            groupUserDeleted(group, deletedUser);
336
        }
337 338 339
    }

    public void adminAdded(Group group, Map params) {
340
        String addedUser = (String) params.get("admin");
Gaston Dombiak's avatar
Gaston Dombiak committed
341 342 343 344
        // Do nothing if the user was a member that became an admin
        if (group.getMembers().contains(addedUser)) {
            return;
        }
345
        if (!isSharedGroup(group)) {
346 347 348 349 350 351
            for (Group visibleGroup : getVisibleGroups(group)) {
                addSharedGroupToRoster(visibleGroup, addedUser);
            }
        }
        else {
            groupUserAdded(group, addedUser);
352
        }
353 354 355
    }

    public void adminRemoved(Group group, Map params) {
356
        String deletedUser = (String) params.get("admin");
Gaston Dombiak's avatar
Gaston Dombiak committed
357 358 359 360
        // Do nothing if the user is still a member
        if (group.getMembers().contains(deletedUser)) {
            return;
        }
361
        // Do nothing if the group is not being shown in group members' rosters
362
        if (!isSharedGroup(group)) {
363 364 365 366 367 368
            for (Group visibleGroup : getVisibleGroups(group)) {
                removeSharedGroupFromRoster(visibleGroup, deletedUser);
            }
        }
        else {
            groupUserDeleted(group, deletedUser);
369
        }
370 371
    }

372 373 374 375 376 377
    /**
     * Notification that a Group user has been added. Update the group users' roster accordingly.
     *
     * @param group the group where the user was added.
     * @param addedUser the username of the user that has been added to the group.
     */
378
    private void groupUserAdded(Group group, String addedUser) {
379
        // Get all the affected users
380
        Collection<String> users = getAffectedUsers(group);
381 382 383
        // Get the roster of the added user.
        Roster addedUserRoster = (Roster) CacheManager.getCache("username2roster").get(addedUser);

384
        // Iterate on all the affected users and update their rosters
385
        for (String userToUpdate : users) {
386 387 388 389 390
            if (!addedUser.equals(userToUpdate)) {
                // Get the roster to update
                Roster roster = (Roster)CacheManager.getCache("username2roster").get(userToUpdate);
                // Only update rosters in memory
                if (roster != null) {
391
                    roster.addSharedUser(group, addedUser);
392
                }
393 394 395 396 397 398 399 400
                // Update the roster of the newly added group user.
                if (addedUserRoster != null) {
                    try {
                        User user = UserManager.getInstance().getUser(userToUpdate);
                        Collection<Group> groups = GroupManager.getInstance().getGroups(user);
                        addedUserRoster.addSharedUser(userToUpdate, groups, group);
                    }
                    catch (UserNotFoundException e) {}
401
                }
402 403 404 405 406 407 408 409 410 411
            }
        }
    }

    /**
     * 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.
     */
412
    private void groupUserDeleted(Group group, String deletedUser) {
413
        groupUserDeleted(group, getAffectedUsers(group), deletedUser);
414 415 416 417 418 419 420 421 422 423
    }

    /**
     * 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<String> users, String deletedUser) {
424 425 426
        // Get the roster of the deleted user.
        Roster deletedUserRoster = (Roster) CacheManager.getCache("username2roster").get(deletedUser);

427
        // Iterate on all the affected users and update their rosters
428
        for (String userToUpdate : users) {
429 430 431 432
            // Get the roster to update
            Roster roster = (Roster)CacheManager.getCache("username2roster").get(userToUpdate);
            // Only update rosters in memory
            if (roster != null) {
433
                roster.deleteSharedUser(group, deletedUser);
434
            }
435 436 437 438 439 440 441 442
            // Update the roster of the newly deleted group user.
            if (deletedUserRoster != null) {
                try {
                    User user = UserManager.getInstance().getUser(userToUpdate);
                    Collection<Group> groups = GroupManager.getInstance().getGroups(user);
                    deletedUserRoster.deleteSharedUser(userToUpdate, groups, group);
                }
                catch (UserNotFoundException e) {}
443 444 445
            }
        }
    }
446

447 448 449 450
    private Collection<Group> getVisibleGroups(Group groupToCheck) {
        Collection<Group> answer = new HashSet<Group>();
        Collection<Group> groups = GroupManager.getInstance().getGroups();
        for (Group group : groups) {
451 452 453
            if (groupToCheck == group) {
                continue;
            }
454 455 456 457 458 459 460 461 462
            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<Group> groupList = parseGroups(group.getProperties().get(
                        "sharedRoster.groupList"));
                if (groupList.contains(groupToCheck)) {
                    answer.add(group);
                }
            }
463 464 465
            else if ("everybody".equals(showInRoster)) {
                answer.add(group);
            }
466 467 468 469
        }
        return answer;
    }

470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
    /**
     * Returns true if a given group is visible to any user. That means, if any user can
     * see the group in his roster.
     *
     * @param group the group to check if the user can see.
     * @return true if a given group is visible by any user.
     */
    boolean isGroupPublic(Group group) {
        String showInRoster = group.getProperties().get("sharedRoster.showInRoster");
        if ("everybody".equals(showInRoster)) {
            return true;
        }
        return false;
    }

485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
    /**
     * 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 username 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, String username) {
        String showInRoster = group.getProperties().get("sharedRoster.showInRoster");
        if ("everybody".equals(showInRoster)) {
            return true;
        }
        else if ("onlyGroup".equals(showInRoster)) {
            if (group.isUser(username)) {
                 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(username)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Adds the group users of the given shared group to the roster of the specified user.
     *
     * @param group the shared group to add to the roster of a user.
     * @param username the name of the user to add a shared group to his roster.
     */
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
    private void addSharedGroupToRoster(Group group, String username) {
        // Get the group users to add to the user's roster
        Collection<String> users = new HashSet<String>(group.getMembers());
        users.addAll(group.getAdmins());

        // Get the roster of the user from which we need to add the shared group users
        Roster userRoster = (Roster) CacheManager.getCache("username2roster").get(username);

        // Iterate on all the group users and update their rosters
        for (String userToAdd : users) {
            // Get the roster to update
            Roster roster = (Roster)CacheManager.getCache("username2roster").get(userToAdd);
            // Only update rosters in memory
            if (roster != null) {
                roster.addSharedUser(group, username);
            }
            // Update the roster of the user
            if (userRoster != null) {
538 539 540 541 542 543
                try {
                    User user = UserManager.getInstance().getUser(userToAdd);
                    Collection<Group> groups = GroupManager.getInstance().getGroups(user);
                    userRoster.addSharedUser(userToAdd, groups, group);
                }
                catch (UserNotFoundException e) {}
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
            }
        }
    }

    private void removeSharedGroupFromRoster(Group group, String username) {
        // Get the group users to remove from the user's roster
        Collection<String> users = new HashSet<String>(group.getMembers());
        users.addAll(group.getAdmins());

        // Get the roster of the user from which we need to remove the shared group users
        Roster userRoster = (Roster) CacheManager.getCache("username2roster").get(username);

        // Iterate on all the group users and update their rosters
        for (String userToRemove : users) {
            // Get the roster to update
            Roster roster = (Roster)CacheManager.getCache("username2roster").get(userToRemove);
            // Only update rosters in memory
            if (roster != null) {
562
                roster.deleteSharedUser(group, username);
563 564 565
            }
            // Update the roster of the user
            if (userRoster != null) {
566 567 568 569 570 571
                try {
                    User user = UserManager.getInstance().getUser(userToRemove);
                    Collection<Group> groups = GroupManager.getInstance().getGroups(user);
                    userRoster.deleteSharedUser(userToRemove, groups, group);
                }
                catch (UserNotFoundException e) {}
572 573 574 575 576
            }
        }
    }

        /**
577 578 579 580 581 582 583 584
     * 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.
     */
585 586 587
    private Collection<String> getAffectedUsers(Group group) {
        return getAffectedUsers(group, group.getProperties().get("sharedRoster.showInRoster"),
                group.getProperties().get("sharedRoster.groupList"));
588 589 590
    }

    /**
591
     * This method is similar to {@link #getAffectedUsers(Group)} except that it receives
592 593 594 595 596 597
     * 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.
     */ 
598
    private Collection<String> getAffectedUsers(Group group, String showInRoster, String groupNames) {
599 600 601 602 603 604 605 606 607
        // Answer an empty collection if the group is not being shown in users' rosters
        if (!"onlyGroup".equals(showInRoster) && !"everybody".equals(showInRoster)) {
            return new ArrayList<String>();
        }
        // Add the users of the group
        Collection<String> users = new HashSet<String>(group.getMembers());
        users.addAll(group.getAdmins());
        // Check if anyone can see this shared group
        if ("everybody".equals(showInRoster)) {
608 609 610 611
            // Add all users in the system
            for (User user : UserManager.getInstance().getUsers()) {
                users.add(user.getUsername());
            }
612 613
            // Add all logged users. We don't need to add all users in the system since only the
            // logged ones will be affected.
614
            //users.addAll(SessionManager.getInstance().getSessionUsers());
615 616 617 618 619 620 621
        }
        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());
622
            }
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
        }
        return users;
    }
    
    Collection<String> 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<String>();
        }
        
        // Add the users of the group
        Collection<String> users = new HashSet<String>(group.getMembers());
        users.addAll(group.getAdmins());
        
        // Check if anyone can see this shared group
        if ("everybody".equals(showInRoster)) {
            // If the user of the roster belongs to the public group then we should return all users
            // in the system since they all need to be in the roster with subscription "from"
            if (group.isUser(roster.getUsername())) {
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
                // Add all users in the system
                for (User user : UserManager.getInstance().getUsers()) {
                    users.add(user.getUsername());
                }
            }
        }
        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;
    }
661 662 663 664 665

    /**
     * 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
666 667
     * 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.
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
     *
     * @param user the name of the user associated to the first collection of groups.
     * @param groups a collection of groups to check against the other collection of groups.
     * @param otherUser the name 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, String 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 == 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;
                        }
                    }
                }
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
                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;
                    }
                }
721 722 723 724
            }
        }
        return false;
    }
725
}