Search in sources :

Example 1 with DeleteItemsCmd

use of net.kano.joscar.snaccmd.ssi.DeleteItemsCmd in project Openfire by igniterealtime.

the class SSIHierarchy method syncContactGroupsAndNickname.

/**
     * Synchronizes the basic characteristics of one contact, including:
     * <ul>
     * <li>the list of groups a contact is a member of</li>
     * <li>nicknames</li>
     * </ul>
     * 
     * As an OSCAR contact must be in at least one group, a default group is
     * used if the provided group list is empty or <tt>null</tt>.
     * 
     * @param contact
     *            Screen name/UIN of the contact.
     * @param nickname
     *            Nickname of the contact (should not be <tt>null</tt>)
     * @param grouplist
     *            List of groups the contact should be a member of.
     * @see SSIHierarchy#getDefaultGroup()
     */
public void syncContactGroupsAndNickname(String contact, String nickname, List<String> grouplist) {
    if (grouplist == null) {
        grouplist = new ArrayList<String>();
    }
    if (grouplist.isEmpty()) {
        Log.debug("No groups provided for the sync of contact " + contact + ". Using default group.");
        grouplist.add(getDefaultGroup());
    }
    Log.debug("Syncing contact = " + contact + ", nickname = " + nickname + ", grouplist = " + grouplist);
    OSCARBuddy oscarBuddy = null;
    try {
        final JID jid = parent.getTransport().convertIDToJID(contact);
        oscarBuddy = parent.getBuddyManager().getBuddy(jid);
        Log.debug("Found related oscarbuddy: " + oscarBuddy);
    } catch (NotFoundException e) {
        Log.debug("Didn't find related oscarbuddy. One will be created.");
    }
    //TODO: Should we do a premodcmd here and postmodcmd at the end and not have separate ones?
    // Stored 'removed' list of buddy items for later use
    final List<BuddyItem> freeBuddyItems = new ArrayList<BuddyItem>();
    // We'll keep the buddy items around for potential modification instead of deletion.
    if (oscarBuddy != null) {
        for (BuddyItem buddy : oscarBuddy.getBuddyItems()) {
            //                if (buddy.getScreenname().equalsIgnoreCase(contact)) {
            if (!groups.containsKey(buddy.getGroupId())) {
                // Well this is odd, a group we don't know about?  Nuke it.
                Log.debug("Removing " + buddy + " because of unknown group");
                freeBuddyItems.add(buddy);
            //                        request(new DeleteItemsCmd(buddy.toSsiItem()));
            //                        oscarBuddy.removeBuddyItem(buddy.getGroupId(), true);
            } else if (!grouplist.contains(groups.get(buddy.getGroupId()).getGroupName())) {
                Log.debug("Removing " + buddy + " because not in list of groups");
                freeBuddyItems.add(buddy);
            //                        request(new DeleteItemsCmd(buddy.toSsiItem()));
            //                        oscarBuddy.removeBuddyItem(buddy.getGroupId(), true);
            } else {
                // nothing to delete? lets update Aliases then.
                if (buddy.getAlias() == null || !buddy.getAlias().equals(nickname)) {
                    Log.debug("Updating alias for " + buddy);
                    buddy.setAlias(nickname);
                    request(new PreModCmd());
                    request(new ModifyItemsCmd(buddy.toSsiItem()));
                    request(new PostModCmd());
                    updateHighestId(buddy);
                    oscarBuddy.tieBuddyItem(buddy, true);
                }
            }
        //                }
        }
    }
    // Now, lets take the known good list of groups and add whatever is missing on the server.
    for (String group : grouplist) {
        Integer groupId = getGroupIdOrCreateNew(group);
        if (isMemberOfGroup(groupId, contact)) {
            // Already a member, moving on
            continue;
        }
        Integer newBuddyId = 1;
        if (highestBuddyIdPerGroup.containsKey(groupId)) {
            newBuddyId = getNextBuddyId(groupId);
        }
        if (freeBuddyItems.size() > 0) {
            // Moving a freed buddy item
            // TODO: This isn't working.. why?  Returns RESULT_ID_TAKEN
            //                BuddyItem buddy = freeBuddyItems.remove(0);
            //                if (oscarBuddy != null) {
            //                    oscarBuddy.removeBuddyItem(buddy.getGroupId(), false);
            //                }
            //                buddy.setGroupid(groupId);
            //                buddy.setId(newBuddyId);
            //                buddy.setAlias(nickname);
            //                request(new ModifyItemsCmd(buddy.toSsiItem()));
            //                if (oscarBuddy == null) {
            //                    oscarBuddy = new OSCARBuddy(getBuddyManager(), buddy);
            //                    // TODO: translate this
            //                    request(new BuddyAuthRequest(contact, "Automated add request on behalf of user."));
            //                }
            //                else {
            //                    oscarBuddy.tieBuddyItem(buddy, false);
            //                }
            request(new PreModCmd());
            BuddyItem buddy = freeBuddyItems.remove(0);
            BuddyItem newBuddy = new BuddyItem(buddy);
            newBuddy.setGroupid(groupId);
            newBuddy.setId(newBuddyId);
            newBuddy.setAlias(nickname);
            request(new DeleteItemsCmd(buddy.toSsiItem()));
            if (oscarBuddy != null) {
                oscarBuddy.removeBuddyItem(buddy.getGroupId(), false);
            }
            request(new CreateItemsCmd(newBuddy.toSsiItem()));
            if (oscarBuddy == null) {
                oscarBuddy = new OSCARBuddy(parent.getBuddyManager(), newBuddy);
                // TODO: translate this
                request(new BuddyAuthRequest(contact, "Automated add request on behalf of user."));
            } else {
                oscarBuddy.tieBuddyItem(newBuddy, false);
            }
            request(new PostModCmd());
        } else {
            // Creating a new buddy item
            final BuddyItem newBuddy = new BuddyItem(contact, groupId, newBuddyId);
            newBuddy.setAlias(nickname);
            updateHighestId(newBuddy);
            //  TODO: Should we be doing this for AIM too?
            if (parent.getTransport().getType().equals(TransportType.icq)) {
                newBuddy.setAwaitingAuth(true);
            }
            request(new PreModCmd());
            request(new CreateItemsCmd(newBuddy.toSsiItem()));
            request(new PostModCmd());
            if (oscarBuddy == null) {
                oscarBuddy = new OSCARBuddy(parent.getBuddyManager(), newBuddy);
                // TODO: translate this
                request(new BuddyAuthRequest(contact, "Automated add request on behalf of user."));
            } else {
                oscarBuddy.tieBuddyItem(newBuddy, true);
            }
        }
    }
    // Now, lets remove any leftover buddy items that we're no longer using.
    for (BuddyItem buddy : freeBuddyItems) {
        request(new DeleteItemsCmd(buddy.toSsiItem()));
        if (oscarBuddy != null) {
            oscarBuddy.removeBuddyItem(buddy.getGroupId(), false);
        }
    }
    // Lastly, lets store the final buddy item after we've modified it, making sure to update groups first.
    if (oscarBuddy != null) {
        //            oscarBuddy.populateGroupList();
        parent.getBuddyManager().storeBuddy(oscarBuddy);
    }
}
Also used : DeleteItemsCmd(net.kano.joscar.snaccmd.ssi.DeleteItemsCmd) JID(org.xmpp.packet.JID) ArrayList(java.util.ArrayList) NotFoundException(org.jivesoftware.util.NotFoundException) BuddyAuthRequest(net.kano.joscar.snaccmd.ssi.BuddyAuthRequest) PostModCmd(net.kano.joscar.snaccmd.ssi.PostModCmd) BuddyItem(net.kano.joscar.ssiitem.BuddyItem) PreModCmd(net.kano.joscar.snaccmd.ssi.PreModCmd) CreateItemsCmd(net.kano.joscar.snaccmd.ssi.CreateItemsCmd) ModifyItemsCmd(net.kano.joscar.snaccmd.ssi.ModifyItemsCmd)

Aggregations

ArrayList (java.util.ArrayList)1 BuddyAuthRequest (net.kano.joscar.snaccmd.ssi.BuddyAuthRequest)1 CreateItemsCmd (net.kano.joscar.snaccmd.ssi.CreateItemsCmd)1 DeleteItemsCmd (net.kano.joscar.snaccmd.ssi.DeleteItemsCmd)1 ModifyItemsCmd (net.kano.joscar.snaccmd.ssi.ModifyItemsCmd)1 PostModCmd (net.kano.joscar.snaccmd.ssi.PostModCmd)1 PreModCmd (net.kano.joscar.snaccmd.ssi.PreModCmd)1 BuddyItem (net.kano.joscar.ssiitem.BuddyItem)1 NotFoundException (org.jivesoftware.util.NotFoundException)1 JID (org.xmpp.packet.JID)1