use of net.kano.joscar.snaccmd.ssi.CreateItemsCmd in project Openfire by igniterealtime.
the class SSIHierarchy method setIcon.
/**
* Updates the avatar of the entity owning this instance on the network.
*/
public void setIcon(final String type, final byte[] data) {
this.pendingAvatar = data;
try {
final MessageDigest digest = MessageDigest.getInstance("MD5");
digest.update(data);
final ExtraInfoData eid = new ExtraInfoData(ExtraInfoData.FLAG_HASH_PRESENT, ByteBlock.wrap(digest.digest()));
final SsiCommand request;
final IconItem newIconItem;
if (icon != null) {
newIconItem = new IconItem(icon);
newIconItem.setIconInfo(eid);
request = new ModifyItemsCmd(newIconItem.toSsiItem());
} else {
newIconItem = new IconItem(IconItem.NAME_DEFAULT, this.getNextBuddyId(SsiItem.GROUP_ROOT), eid);
request = new CreateItemsCmd(newIconItem.toSsiItem());
}
request(new PreModCmd());
request(request);
request(new PostModCmd());
this.icon = newIconItem;
} catch (NoSuchAlgorithmException e) {
Log.error("No algorithm found for MD5 checksum??");
}
}
use of net.kano.joscar.snaccmd.ssi.CreateItemsCmd in project Openfire by igniterealtime.
the class SSIHierarchy method getGroupIdOrCreateNew.
/**
* Finds the id number of a group specified or creates a new one and returns
* that id.
*
* @param groupName
* Name of the group we are looking for.
* @return Id number of the group.
*/
public int getGroupIdOrCreateNew(String groupName) {
for (final GroupItem g : groups.values()) {
if (groupName.equalsIgnoreCase(g.getGroupName())) {
return g.getId();
}
}
// Group doesn't exist, lets create a new one.
final int newGroupId = getNextBuddyId(0);
final GroupItem newGroup = new GroupItem(groupName, newGroupId);
request(new CreateItemsCmd(newGroup.toSsiItem()));
gotGroup(newGroup);
return newGroupId;
}
use of net.kano.joscar.snaccmd.ssi.CreateItemsCmd 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);
}
}
use of net.kano.joscar.snaccmd.ssi.CreateItemsCmd in project Openfire by igniterealtime.
the class SSIHierarchy method setVisibilityFlag.
/**
* Adds a visibility settings flag, if that flag had not been set
* previously.
*
* @param flag
* the that will be added to the existing settings.
* @see VisibilityItem
*/
public void setVisibilityFlag(long flag) {
if (visibility != null) {
if ((visibility.getVisFlags() & flag) == 0) {
visibility.setVisFlags((visibility.getVisFlags() | flag));
request(new ModifyItemsCmd(visibility.toSsiItem()));
}
} else {
final VisibilityItem newItem = new VisibilityItem(getNextBuddyId(SsiItem.GROUP_ROOT), SsiItem.GROUP_ROOT);
newItem.setVisFlags(flag);
this.visibility = newItem;
request(new CreateItemsCmd(newItem.toSsiItem()));
}
}
Aggregations