/** * ContactGroupListener Override: Tutorial Handler - ContactGroup TYPE. * <br /><br /> * This handler fires for all ContactGroups. If it's <em>not</em> * a pending authorization request, log and ignore it. * <br /><br /> * Tutorial 9 - Process authorization requests * * @since 1.0 * * @see com.skype.api.ContactGroupListener#onChange(com.skype.api.ContactGroup, com.skype.api.Contact) */ public void onChange(com.skype.api.ContactGroup obj, Contact contact) { if (contact != null) { String contactSkypeName = contact.getSkypeName(); // Find out who it's from String contactDisplayName = contact.getDisplayName(); ContactGroup waitingAuth = mySession.mySkype.getHardwiredContactGroup(ContactGroup.Type.CONTACTS_WAITING_MY_AUTHORIZATION); Contact[] waitingAuthMembers = waitingAuth.getContacts(); int waitingAuthMembersCnt = waitingAuthMembers.Length; if (waitingAuthMembersCnt == 0) { MySession.myConsole.printf("%s: Ignoring ContactGroup change for %s (%s); no Contacts awaiting authorization %n", mySession.myTutorialTag, contactSkypeName, contactDisplayName); return; } if (contact.getReceivedAuthRequest().Length == 0) { MySession.myConsole.printf("%s: Ignoring ContactGroup change; Contact %s (%s) is not awaiting authorization %n", mySession.myTutorialTag, contactSkypeName, contactDisplayName); return; } String authRequestText = contact.getReceivedAuthRequest(); // Get any intro text... if ((authRequestText == null) || (authRequestText.Length == 0)) { // ...and default it if missing authRequestText = "-- NO INTRODUCTORY TEXT --"; } MySession.myConsole.printf("%s: Authorization request from: %s (%s):%n\t%s", mySession.myTutorialTag, contactSkypeName, contactDisplayName, authRequestText); contact.setBuddyStatus(true, true); /* * SetBuddyStatus should really return a boolean... * If and when it does, replace the above SetBuddyStatus invocation with the following... * if (contact.setBuddyStatus(true, true)) { * MySession.myConsole.printf("%s: %s is now authorized%n", mySession.myTutorialTag, contactSkypeName); * } * else { * MySession.myConsole.printf("%s: Authorization failed.%n", mySession.myTutorialTag); * } */ MySession.myConsole.printf("%s: %s is now authorized!%n", mySession.myTutorialTag, contactSkypeName); } }
/** * List, add, or delete Contacts. * * @param mySession * Populated session object. * @param pstn * Properly formatted phone number of the target PSTN Contact. * @param displayName * Display name of the target PSTN Contact (used by add <em>only</em>). * @param commandOpt * Command string, which must be one of: * <ul> * <li>{@link #ADD_CONTACT}</li> * <li>{@link #DELETE_CONTACT}</li> * <li>{@link #LIST_CONTACTS}</li> * </ul> * * @since 1.0 */ static void doPstnContact(MySession mySession, String pstn, String displayName, String commandOpt) { // Verify that our command option is valid -- something our invoker should have already done! if ((!(commandOpt.Equals(LIST_CONTACTS))) && (!(commandOpt.Equals(ADD_CONTACT))) && (!(commandOpt.Equals(DELETE_CONTACT)))) { // We shouldn't get here -- the invoker should have already validated the command. MySession.myConsole.printf("%s: Unrecognized command option: %s%n", mySession.myTutorialTag, commandOpt); return; } int contactIdx; Skype.NormalizeIdentityResponse nrmlResponse = null; ContactGroup soContactGroup = mySession.mySkype.getHardwiredContactGroup(ContactGroup.Type.SKYPEOUT_BUDDIES); Contact[] soContactList = soContactGroup.getContacts(); int contactCount = soContactList.Length; // Handle list operations... if (commandOpt.Equals(LIST_CONTACTS)) { // Make sure there's something to list! if (contactCount == 0) { MySession.myConsole.printf("%s: There are no PSTN contacts.%n", mySession.myTutorialTag); return; } MySession.myConsole.printf("%s: Current list of PSTN contacts:%n", mySession.myTutorialTag); for (contactIdx = 0; contactIdx < contactCount; contactIdx++) { MySession.myConsole.printf("%s: %d. %s (%s)%n", mySession.myTutorialTag, (contactIdx + 1), soContactList[contactIdx].getPstnNumber(), soContactList[contactIdx].getDisplayName()); } return; } //Handle add & delete operations... String contactPstn; bool contactAlreadyListed = false; // Ensure that the pstn argument contains a valid contact identity nrmlResponse = getNormalizationStr(pstn); if (nrmlResponse.result != Skype.NormalizeResult.IDENTITY_OK) { MySession.myConsole.printf("%s: Cannot normalize pstn %s using %s%n", mySession.myTutorialTag, pstn, mySession.mySkype.getIsoCountryInfo().countryPrefixList[0]); return; } // Check whether the PSTN contact already exists, which is relevant to both // adding and removing contacts. In current wrapper version, the only way to do this // is to loop over a contact group. for (contactIdx = 0; contactIdx < contactCount; contactIdx++) { contactPstn = soContactList[contactIdx].getPstnNumber(); if (contactPstn.Equals(nrmlResponse.normalized)) { contactAlreadyListed = true; } } // Handle adding a Contact. The Contact must not exist in that group! if (commandOpt.Equals(ADD_CONTACT)) { if (contactAlreadyListed) { MySession.myConsole.printf("%s: Error: %s already present in ContactGroup.%n", mySession.myTutorialTag, nrmlResponse.normalized); return; } MySession.myConsole.printf("%s: Adding PSTN Contact...%n", mySession.myTutorialTag); Contact newContact = mySession.mySkype.getContact(nrmlResponse.normalized); if ((newContact != null) && (soContactGroup.canAddContact(newContact))) { newContact.giveDisplayName(displayName); soContactGroup.addContact(newContact); MySession.myConsole.printf("%s: Contact %s (%s) added.%n", mySession.myTutorialTag, nrmlResponse.normalized, displayName); } else { ContactGroup.Type soContactGroupType = soContactGroup.getType(); MySession.myConsole.printf("%s: Cannot add Contact %s (%s) to ContactGroup %s (\"%s\") using AddContact():%n", mySession.myTutorialTag, nrmlResponse.normalized, displayName, soContactGroupType.toString(), soContactGroup.getGivenDisplayName()); if (newContact == null) { MySession.myConsole.println("\tCould not create new Contact (normalized PSTN likely invalid)"); } else if (!(soContactGroup.canAddContact(newContact))) { MySession.myConsole.println("\tCannot add Contacts to target ContactGroup"); } else { MySession.myConsole.println("\tReason unknown?!?%n"); } } return; } // Handle deleting a Contact. The Contact must exist in that group! if (!contactAlreadyListed) { MySession.myConsole.printf("%s: PSTN Contact %s not present in ContactGroup.%n", mySession.myTutorialTag, nrmlResponse.normalized); return; } MySession.myConsole.printf("%s: Removing PSTN Contact...%n", mySession.myTutorialTag); Contact removableContact = mySession.mySkype.getContact(nrmlResponse.normalized); if ((removableContact != null) && (soContactGroup.canRemoveContact())) { String removableDisplayName = removableContact.getDisplayName(); soContactGroup.removeContact(removableContact); // Can't include any Contact-specific identifying information in the message that we haven't already // extracted since RemoveContact leaves the target Contact instance in an undefined (mostly nulled-out) state! MySession.myConsole.printf("%s: Removed PSTN Contact %s (\"%s\").%n", mySession.myTutorialTag, nrmlResponse.normalized, removableDisplayName); } else { ContactGroup.Type soContactGroupType = soContactGroup.getType(); MySession.myConsole.printf("%s: Cannot remove Contact %s from ContactGroup %s (\"%s\") using RemoveContact():%n", mySession.myTutorialTag, nrmlResponse.normalized, soContactGroupType.toString(), soContactGroup.getGivenDisplayName()); if (removableContact == null) { MySession.myConsole.println("\tCould not remove Contact (normalized PSTN likely invalid)"); } else if (!(soContactGroup.canRemoveContact())) { MySession.myConsole.println("\tCannot remove Contacts from target ContactGroup"); } else { MySession.myConsole.println("\tReason unknown?!?%n"); } } return; }