# tutorial_8.cpp

## C++ Tutorial Step 8: Adding, Deleting and Listing PSTN (Telephone Number) Contacts

In this example, we'll write a command-line utility for adding, deleting, and listing PSTN (telephone number) contacts. The program will take five arguments:

- your skypename
- your password
- contact's phone number (to be added or removed)
- contact's displayname
- one of the following commands: -a (add a contact); -d (delete contact); -l (list all PSTN contacts)

For example, if you are skypename and you want to add JohnDoe's phone number, we would run the program as follows:

    $ ./tutorial_8 skypename password1 2122255631 JohnDoe -a

For simplicity's sake, we'll hardcode in the assumption that the phone number we're adding is in Estonia (country code 372).

### Code Walkthrough

Adding a Contact

Before adding a new contact, we need to normalize the contact's phone number (3rd argument to our program) to "+<country-code><number>" format (E.164 standard). Thankfully, the SkypeKit method ''NormalizePSTNWithCountry'' performs this task. The method takes two arguments - a phone number string and a country code - and returns the normalized string in ''&normalized'':

    bool NormalizePSTNWithCountry (
      const Sid::String &original,
      NORMALIZERESULT &result,
      Sid::String &normalized,
      const uint countryPrefix=0);

NormalizePSTNWithCountry checks if the phone number starts with "+" - if it doesn't, it prefixes the output with "+XXX" (where XXX is the country code). It also converts letters to numbers based on the standard phone keypad, so that the phone number string "212CALLME1" with country code "372" (Estonia) would be normalized to "[skype:+12122255631 +3722122255631]". If the method cannot normalize the phone number (because it's too long, too short, etc.), it returns an error code in &result. The following code returns the error types in English:

    Skype::NORMALIZERESULT normalizationResult;
    SEString normalizeResultStr = tostring(normalizationResult);

Putting it all together, here's how we normalize the contact's phone number and handle errors:

    SEString normalizedPSTN;
    Skype::NORMALIZERESULT normalizeResult;
    Skype::NORMALIZERESULT normalizationResult;
    skype->NormalizePSTNWithCountry (newPSTN, normalizationResult, normalizedPSTN, countryPrefix);

    if (normalizeResult != Skype::IDENTITY_OK)
    {
      SEString normalizeResultStr = tostring(normalizationResult);
      printf("Error: %s - %s\n", (const char*)newPSTN, (const char*)normalizeResultStr);
    }
    else
    {
      // newPSTN was ok.
    };

Now that we've got a valid number, we want to check if the contact we're trying to add (or remove) is already in the contact list.

    Contact::Ref soContact;
    skype->GetContact(normalizedPSTN, soContact);
    ContactGroup::Ref soContactGroup;
    skype->GetHardwiredContactGroup(ContactGroup::SKYPEOUT_BUDDIES , soContactGroup);
    ContactRefs soContactList;
    soContactGroup->GetContacts(soContactList);
    bool contactAlreadyListed = soContactList.contains(soContact);

Now we're ready to add the contact. First, we request a new contact object with Skype::GetContact. Then we set the contact's display name to the one we got from the command line (4th argument) using Contact::GiveDisplayName. To "save" the contact, we need to add it to a ContactGroup. Luckily, we have the one we created (soContactGroup) when we checked if the contact already existed, so we can use that one and call SOContactGroup->AddContact(newContact):

    Contact::Ref newContact;
    skype->GetContact(normalizedPSTN, newContact);
    newContact->GiveDisplayName(newName);
    if (soContactGroup->AddContact(newContact))
    {
      printf("Contact %s %s added.\n", (const char*)normalizedPSTN, (const char*)newName);
    }
    else
    {
      printf("Contact %s %s was NOT added. Possibly the PSTN number format is too odd?\n",
        (const char*)normalizedPSTN,
        (const char*)newName);
    };

Deleting a Contact

The code for deleting a contact is almost exactly the same, except we use SOContactGroup->RemoveContact:

    Contact::Ref removableContact;
    skype->GetContact(normalizedPSTN, removableContact);
    if (soContactGroup->RemoveContact(removableContact))
    {
      printf("Contact %s %s removed.\n", (const char*)normalizedPSTN, (const char*)newName);
    }
    else
    {
      printf("Contact %s %s was NOT removed.\n",
        (const char*)normalizedPSTN,
        (const char*)newName);
    };

## Full code of this example

    /****************************************************************************

    Getting Started With SkypeKit. Tutorial Application, Step 8.

    In this example, we'll write a command-line utility for adding, deleting, and
    listing PSTN (telephone number) contacts. The program will take five arguments:

     1. your skypename
     2. your password
     3. contact's phone number (to be added or removed)
     4. contact's displayname
     5. one of the following commands: -a (add a contact);  -d (delete contact); -l  (list all PSTN contacts)

    **/

    #include "skype-embedded_2.h"
    #include "keypair.h"
    #include "tutorial_common.h"

    using namespace Sid;

    SEString myAccountName;
    SEString myAccountPsw;

    int countryPrefix = 372;

    class MySkype : public Skype
    {
    public:
      MySkype() : Skype() {}
      Account* newAccount(int oid) {return new MyAccount(oid, this);}
    };

    //----------------------------------------------------------------------------
    // Main

    MySkype* skype = 0;

    int main(int argc, char * argv[])
    {
      printf("*****************************************************************\n");
      printf(" SkypeKit Tutorial, Step 8. - Adding PSTN contact.\n");
      printf("*****************************************************************\n");

      if (argc < 6)
      {
        printf("usage: tutorial_8 <skypename> <password> <phoneno> <contact name> <-a|-d|-l>\n");
        printf(" -a  - Add PSTN contact.\n");
        printf(" -d  - Delete PSTN contact.\n");
        printf(" -l  - List PSTNcontacts.\n");
        return 0;
      };

      myAccountName     = argv[1];
      myAccountPsw      = argv[2];
      SEString newPSTN  = argv[3];
      SEString newName  = argv[4];
      SEString command  = argv[5];

      printf("Creating skype ..\n");
      skype = new MySkype();

      printf("Starting Skype and submitting application token..\n");
      getKeyPair ();
      skype->init(keyBuf, inetAddr, portNum, "streamlog.txt");
      skype->start();

      printf("Retrieving account ..\n");
      MyAccount::Ref account;

      if (skype->GetAccount(myAccountName, account))
      {
        printf("Logging in..\n");
        account->LoginWithPassword(myAccountPsw, false, true);
        account->BlockWhileLoggingIn();
        printf("Loggin complete.\n");

        // First we will check if the newPSTN argument contains a valid PSTN number.
        SEString normalizedPSTN;
        Skype::NORMALIZERESULT normalizationResult;
        skype->NormalizePSTNWithCountry (newPSTN, normalizationResult, normalizedPSTN, countryPrefix);

        if (normalizationResult != Skype::IDENTITY_OK)
        {
          SEString normalizeResultStr = tostring(normalizationResult);
          printf("Error: %s - %s\n", (const char*)newPSTN, (const char*)normalizeResultStr);
        }
        else
        {
          // Next, lets check if the PSTN contact actually exists. This is relevant for both
          // adding and removing contacts. In current wrapper version, the only way to do this
          // is to loop over a contact group.

          Contact::Ref soContact;
          skype->GetContact(normalizedPSTN, soContact);

          ContactGroup::Ref soContactGroup;
          skype->GetHardwiredContactGroup(ContactGroup::SKYPEOUT_BUDDIES , soContactGroup);

          ContactRefs soContactList;
          soContactGroup->GetContacts(soContactList);

          bool contactAlreadyListed = soContactList.contains(soContact);

          // Listing PSTN contacts
          if (command == "-l")
          {
            if (soContactList.size() != 0)
            {
              printf("Current list of PSTN contacts:\n");
              SEString contactPSTN;
              SEString contactDisplayName;
              for (uint i = 0; i < soContactList.size(); i++)
              {
                soContact = soContactList[i];
                contactPSTN = soContact->GetProp(Contact::P_PSTNNUMBER);
                contactDisplayName = soContact->GetProp(Contact::P_DISPLAYNAME);
                printf("%d. %s (%s)\n", i+1, (const char*)contactPSTN, (const char*)contactDisplayName);
              };
            }
            else
            {
              printf("There are no PSTN contacts.\n");
            };
          };
          // end of listing PSTN contacts

          // Removing PSTN contact.
          if (command == "-d")
          {
            if (!contactAlreadyListed)
            {
              printf("PSTN contact %s not found.\n", (const char*)normalizedPSTN);
            }
            else
            {
              printf("Removing PSTN contact..\n");
              Contact::Ref removableContact;
              skype->GetContact(normalizedPSTN, removableContact);

              if (soContactGroup->RemoveContact(removableContact))
              {
                printf("Contact %s %s removed.\n", (const char*)normalizedPSTN, (const char*)newName);
              }
              else
              {
                printf("Contact %s %s was NOT removed.\n", (const char*)normalizedPSTN, (const char*)newName);
              };
            };
          };
          // end of removing PSTN contact.

          // Adding PSTN contact.
          if (command == "-a")
          {
            if (contactAlreadyListed)
            {
              printf("Error: %s already present in contact list.\n", (const char*)normalizedPSTN);
            }
            else
            {
              printf("Adding PSTN contact..\n");
              Contact::Ref newContact;
              skype->GetContact(normalizedPSTN, newContact);
              newContact->GiveDisplayName(newName);
              if (soContactGroup->AddContact(newContact))
              {
                printf("Contact %s %s added.\n", (const char*)normalizedPSTN, (const char*)newName);
              }
              else
              {
                printf("Contact %s %s was NOT added. Possibly the PSTN number format is too odd?\n",
                  (const char*)normalizedPSTN,
                  (const char*)newName);
              };
            };
          };
          // end of Adding PSTN contact.
        };

        printf("Logging out..\n");
        account->Logout(false);
        account->BlockWhileLoggingOut();
        printf("Logout complete.\n");
      }
      else
      {
        printf("Account does not exist\n");
      };

      printf("Cleaning up.\n");
      skype->stop();
      delete skype;
      printf("Done.\n");
      return 0;
    };

All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

(c) Skype Technologies S.A. Confidential/Proprietary

Last updated: Fri Mar 16 2012