17:39
[skypekit:skypekit.git] / skypekit_2.0.0.190_29281_CB30 / examples / cpp / tutorial / step8 / tutorial_8.cpp
1 /****************************************************************************
2
3 Getting Started With SkypeKit. Tutorial Application, Step 8.
4
5 In this example, we'll write a command-line utility for adding, deleting, and 
6 listing PSTN (telephone number) contacts. The program will take five arguments:
7
8  1. your skypename
9  2. your password
10  3. contact's phone number (to be added or removed)
11  4. contact's displayname
12  5. one of the following commands: -a (add a contact);  -d (delete contact); -l  (list all PSTN contacts)
13
14 **/
15
16 #ifdef _WIN32
17 #include "Windows.h"
18 #else
19 #include <unistd.h>
20 #endif
21
22 #include "skype-embedded_2.h"
23 #include "skype-tcp-transport.h"
24 #include "app_token.h"
25
26 using namespace Sid;
27
28 SEString inetAddr           = "127.0.0.1";
29 uint portNum                = 8963;
30
31 bool LoggedIn               = false;
32 bool LoggedOut              = false;
33
34 SEString MyAccountName;
35 SEString MyAccountPsw;
36
37 int CountryPrefix = 372;
38
39 SETCPTransport* transport   = 0;
40
41
42 void debugOut(const char* out)
43 {
44     //printf("%s", out);
45 }
46
47 void Delay(int Ms)
48 {
49         #ifdef _WIN32
50                 Sleep(Ms);
51         #else
52                 sleep(Ms);
53         #endif
54 };
55
56
57 //---------------------------------------------------------------------------------------
58 // Subclassing Account
59
60 class MyAccount : public Account
61 {
62 public:
63     typedef DRef<MyAccount, Account> Ref;
64     MyAccount(unsigned int oid, SERootObject* root);
65     ~MyAccount() {};
66
67     void OnChange(int prop);
68 };
69
70 MyAccount::MyAccount(unsigned int oid, SERootObject* root) : Account(oid, root) {};
71
72 void MyAccount::OnChange(int prop)
73 {
74     if (prop == Account::P_STATUS)
75     {
76         Account::STATUS LoginStatus;
77         GetPropStatus(LoginStatus);
78         if (LoginStatus == Account::LOGGED_IN)      { LoggedIn  = true; };
79         if (LoginStatus == Account::LOGGED_OUT)         { LoggedOut = true; };
80     };
81 };
82
83 //---------------------------------------------------------------------------------------
84 // Subclassing Skype
85
86 class MySkype : public Skype
87 {
88 public:
89     MySkype(SETransport* transport) : Skype(transport) {}
90     ~MySkype() {}
91     Account* newAccount(int oid) {return new MyAccount(oid, this);}
92     NORMALIZERESULT GetNormalizationStr (const SEString PSTN, SEString& NormalizedPSTN, SEString& NormalizeResultStr);
93 };
94
95
96 Skype::NORMALIZERESULT MySkype::GetNormalizationStr (SEString PSTN, SEString& NormalizedPSTN, SEString& NormalizeResultStr)
97 {
98         Skype::NORMALIZERESULT NormalizationResult = Skype::IDENTITY_EMPTY;
99
100         this->NormalizePSTNWithCountry (PSTN, NormalizationResult, NormalizedPSTN, CountryPrefix);
101
102         // You would use this instead of NormalizePSTNWithCountry for non-PSTN contacts:
103         // this->NormalizeIdentity(PSTN, NormalizationResult, NormalizedPSTN, false);
104
105         NormalizeResultStr = "Unexpected normalization result";
106         switch (NormalizationResult) {
107         case Skype::IDENTITY_OK :
108                         NormalizeResultStr = "Normalization OK";
109                 break;
110
111                 case Skype::IDENTITY_EMPTY :
112                         NormalizeResultStr = "Input was empty";
113                 break;
114
115                 case Skype::IDENTITY_TOO_LONG  :
116                         NormalizeResultStr = "Input string too long";
117                 break;
118
119                 case Skype::IDENTITY_CONTAINS_INVALID_CHAR  :
120                         NormalizeResultStr = "Input contained illegal characters";
121                 break;
122
123                 case Skype::PSTN_NUMBER_TOO_SHORT :
124                         NormalizeResultStr = "Submitted PSTN number was too short";
125                 break;
126
127                 case Skype::PSTN_NUMBER_HAS_INVALID_PREFIX  :
128                         NormalizeResultStr = "Input started with invalid character(s)";
129                 break;
130
131                 case Skype::SKYPENAME_STARTS_WITH_NONALPHA :
132                         NormalizeResultStr = "Input started with non-alphanumeric character.";
133                 break;
134
135                 case Skype::SKYPENAME_SHORTER_THAN_6_CHARS  :
136                         NormalizeResultStr = "Submitted skypename was too short";
137                 break;
138         }
139         return NormalizationResult;
140 };
141
142
143 //---------------------------------------------------------------------------------------
144 // Main
145
146 MySkype* skype = 0;
147
148 int main(int argc, char * argv[])
149 {
150     printf("*****************************************************************\n");
151     printf(" SkypeKit Tutorial, Step 8. - Adding PSTN contact.\n");
152     printf("*****************************************************************\n");
153
154     if (argc < 6)
155     {
156         printf("usage: tutorial_8 <skypename> <password> <phoneno> <contact name> <-a|-d|-l>\n");
157         printf(" -a  - Add PSTN contact.\n");
158         printf(" -d  - Delete PSTN contact.\n");
159         printf(" -l  - List PSTNcontacts.\n");
160         return 0;
161     };
162
163     MyAccountName = argv[1];
164     MyAccountPsw = argv[2];
165     SEString NewPSTN = argv[3];
166     SEString NewName = argv[4];
167         SEString Command = argv[5];
168
169     printf("Creating transport ..\n");
170     transport = new SETCPTransport(inetAddr, portNum);
171     printf("Creating skype ..\n");
172     skype = new MySkype(transport);
173     skype->log = &(debugOut);
174
175     skype->init();
176     skype->start();
177
178     printf("Submitting application token..\n");
179     skype->SetApplicationToken(AppToken);
180
181     printf("Retrieving account ..\n");
182     MyAccount::Ref A;
183
184     if (skype->GetAccount(MyAccountName, A))
185     {
186         printf("Logging in..\n");
187         A->LoginWithPassword(MyAccountPsw, false, true);
188         while (!LoggedIn) {Delay(1);};
189         printf("Loggin complete.\n");
190
191
192                 // First we will check if the NewPSTN argument contains a valid contact identity.
193                 // If not, our GetNormalizationStr will return us the error string in NormalizeResultStr
194                 SEString NormalizedPSTN = "";
195                 SEString NormalizeResultStr = "";
196                 Skype::NORMALIZERESULT NormalizeResult;
197                 NormalizeResult = skype->GetNormalizationStr (NewPSTN, NormalizedPSTN, NormalizeResultStr);
198
199                 if (NormalizeResult != Skype::IDENTITY_OK)
200                 {
201                         printf("Error: %s - %s\n", (const char*)NewPSTN, (const char*)NormalizeResultStr);
202                 }
203                 else
204                 {
205                         // Next, lets check if the PSTN contact actually exists. This is relevant for both
206                         // adding and removing contacts. In current wrapper version, the only way to do this
207                         // is to loop over a contact group.
208                 ContactGroup::Ref SOContactGroup;
209                 skype->GetHardwiredContactGroup(ContactGroup::SKYPEOUT_BUDDIES , SOContactGroup);
210
211                         ContactRefs SOContactList;
212                         SOContactGroup->GetContacts(SOContactList);
213
214                         Contact::Ref SOContact;
215                         SEString ContactPSTN = "";
216
217                         bool ContactAlreadyListed = false;
218                         int ContactCount = SOContactList.size();
219
220                         if (Command == "-l")
221                         {
222                                 if (ContactCount != 0)
223                                 {
224                                         printf("Current list of PSTN contacts:\n");
225                                 }
226                                 else
227                                 {
228                                         printf("There are no PSTN contacts.\n");
229                                 };
230                         };
231
232                 for (int I = 0; I < ContactCount; I++)
233                 {
234                                 SOContact = SOContactList[I];
235                 ContactPSTN = SOContact->GetProp(Contact::P_PSTNNUMBER);
236
237                                 if (Command == "-l")
238                                 {
239                         printf("%d. %s\n", I+1, (const char*)ContactPSTN);
240                                 };
241
242                 if (ContactPSTN == NormalizedPSTN)
243                 {
244                         ContactAlreadyListed = true;
245                 };
246                 };
247
248                         // Removing PSTN contact.
249                         if (Command == "-d")
250                         {
251                                 if (!ContactAlreadyListed)
252                                 {
253                                         printf("PSTN contact %s not found.\n", (const char*)NormalizedPSTN);
254                                 }
255                                 else
256                                 {
257                                         printf("Removing PSTN contact..\n");
258                                         Contact::Ref RemovableContact;
259                                         skype->GetContact(NormalizedPSTN, RemovableContact);
260
261                                         if (SOContactGroup->RemoveContact(RemovableContact)) 
262                                         {
263                                                 printf("Contact %s %s removed.\n", (const char*)NormalizedPSTN, (const char*)NewName);
264                                         }
265                                         else
266                                         {
267                                                 printf("Contact %s %s was NOT removed.\n", (const char*)NormalizedPSTN, (const char*)NewName);
268                                         };
269                                 };
270                         };
271
272                         // Adding PSTN contact.
273                         if (Command == "-a")
274                         {
275                                 if (ContactAlreadyListed)
276                                 {
277                                         printf("Error: %s already present in contact list.\n", (const char*)NormalizedPSTN);
278                                 }
279                                 else
280                                 {
281                                         printf("Adding PSTN contact..\n");
282                                         Contact::Ref NewContact;
283                                         skype->GetContact(NormalizedPSTN, NewContact);
284                                         NewContact->GiveDisplayName(NewName);
285                                         if (SOContactGroup->AddContact(NewContact))
286                                         {
287                                                 printf("Contact %s %s added.\n", (const char*)NormalizedPSTN, (const char*)NewName);
288                                         }
289                                         else
290                                         {
291                                                 printf("Contact %s %s was NOT added. Possibly the PSTN number format is too odd?\n", (const char*)NormalizedPSTN, (const char*)NewName);
292                                         };
293                                 };
294                         };
295                 };
296
297         printf("Logging out..\n");
298         A->Logout(false);
299
300         while (!LoggedOut) {Delay(1);};
301         printf("Logout complete.\n");
302     }
303     else
304     {
305         printf("Account does not exist\n");
306     };
307
308     printf("Cleaning up.\n");
309     skype->stop();
310     skype->cleanup();
311     delete skype;
312     delete transport;
313     printf("Done.\n");
314     return 0;
315 };