summaryrefslogtreecommitdiffstats
path: root/tsclient.h
blob: ccc32b0e90d7af207bad2a74db35f59e6b3fec65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Authors: Manuel Mausz (manuel@mausz.at)
 *          Christian Raschko (c.raschko@netcore.at)
 */

//Header guard
#ifndef TSCLIENT_H
#define TSCLIENT_H

//Libraries
#include <wx/wx.h>
#include <wx/thread.h>

#include "tsheaders.h"
#include "tsserver.h"

//! Thread response timeout
#define THREAD_TIMEOUT  4000

//! Maximum time to leave
#define MAX_TTL 5

// better than void *
union TSPtr
{
  TSPlayer  *pPlayer;
  TSChannel *pChannel;
  TSServer  *pServer;
  TSClient  *pClient;
  wxString  *pString;
};

//! Only for internal use
struct TSCmd : wxObject
{
  DECLARE_CLASS(TSCmd)
  //CTor
  TSCmd()
  {
    id             = 0;
    executed       = false;
    error          = false;
    ttl            = MAX_TTL;
    pktid          = 0;
    oldid          = 0;
    client         = NULL;
    param1.pPlayer = NULL;
    param1.pPlayer = NULL;
  }

  TSCmd(wxUint32 c)
  {
    TSCmd();
    id = c;
  }

  TSCmd(wxUint32 c, TSCmd *old)
  {
    TSCmd();
    id     = c;
    pktid  = old->pktid;
    oldid  = old->id;
    client = old->client;
  }

  TSCmd(const TSCmd &cmd)
    : wxObject()
  {
    id        = cmd.id;
    oldid     = cmd.oldid;
    executed  = cmd.executed;
    error     = cmd.error;
    ttl       = cmd.ttl;
    pktid     = cmd.pktid;
    client    = cmd.client;
    param1    = cmd.param1;
    param2    = cmd.param2;
    lasterror = cmd.lasterror;
  }

  wxUint32  id;
  wxUint32  oldid;
  bool      executed;
  bool      error;
  wxUint32  ttl;
  wxUint32  pktid;
  TSClient *client;
  TSPtr     param1;
  TSPtr     param2;
  wxString  lasterror;
};

//! TeamSpeak client, main class.
/*! TSClient is the main class to connect to a
 *  TeamSpeak server. These stepps are necessary
 *  to create and use a TSClient object.
 *  - Create a TSClient object.
 *    - Change the attributes.
 *  - Create a TSPlayer object.
 *    - Change the attributes.
 *    - Link it to the TSClient object.
 *  - Create a TSServer object.
 *    - Change the attributes.
 *    - Link it to the TSClient object.
 *  - Call Connect().
 *
 *  Example:
 *  \code
 *
 *  TSClient *pTSC = new TSClient;
 *  pTSC->SetPlatform(_T("Windows XP"));
 *  pTSC->SetVersionNumber(_T("2.0.32.60"));
 *
 *  TSPlayer *pTSP = new TSPlayer;
 *  pTSP->SetLoginName(_T("admin"));
 *  pTSP->SetLoginPassword(_T("testpassword"));
 *  pTSP->SetNickname(_T("penner"));
 *   //now the TSClient takes care of the pointer.
 *  pTSC->SetPlayer(pTSP);
 *
 *  TSServer *pTSS = new TSServer;
 *  pTSS->SetServerAddress(_T("game3.clan-server.at"));
 *  pTSS->SetPort(8767);
 *  //now the TSClient takes care of the pointer.
 *  pTSC->SetServer(pTSS);
 *
 *  if(!pTSC->Connect())
 *  {
 *    wxLogError(_T("%s, terminating.\n"), pTSC->GetLastError().c_str());
 *    return EXIT_FAILURE;
 *  }
 *
 *  pTSC->Disconnect();
 *  delete pTSC;
 *  \endcode
 */
class TSClient : public wxObject
{
  DECLARE_CLASS(TSClient)

  public:

   /*! Default CTor, Initializes the object.
    *  Creates all necessary member objects.
    */
    TSClient();

   /*! Default DTor.
    */
    ~TSClient();

   /*! Connects to a Teasmpeak server.
    *  \return Returns false if fails, check GetLastError for details.
    *  \sa GetLastError()
    */
    bool Connect();

   /*! Disconnects from a Teasmpeak server.
    *  \return Returns false if fails, check GetLastError for details.
    *  \sa GetLastError()
    */
    bool Disconnect(bool reconnect = true);

   /*! Sets the platform string (e.g. Windows XP, Linux).
    *  \param str Platform string.
    *  \return Returns false if fails, check GetLastError for details.
    *  \sa GetLastError()
    */
    bool SetPlatform(wxString const &str);

   /*! Sets the TeamSpeak player.
    *  The class takes care of the pointer, no delete call.
    *  \param pPlayer TeamSpeak Player.
    *  \return Returns false if fails, check GetLastError for details.
    *  \sa TSPlayer GetLastError()
    */
    bool SetPlayer(TSPlayer *pPlayer);

   /*! Sets the TeamSpeak player channel.
    *  The class takes care of the pointer, no delete call.
    *  \param pChannel player channel.
    *  \return Returns false if fails, check GetLastError for details.
    *  \sa TSChannel GetLastError()
    */
    bool SetChannel(TSChannel *pChannel);

   /*! Sets the TeamSpeak server.
    *  The class takes care of the pointer, no delete call.
    *  \param pServer Server to use for connection.
    *  \return Returns false if fails, check GetLastError for details.
    *  \sa TSServer GetLastError()
    */
    bool SetServer(TSServer *pServer);

   /*! Sets the TeamSpeak connection.
    *  The class takes care of the pointer, no delete call.
    *  \param pConnection Connection object.
    *  \return Returns false if fails, check GetLastError for details.
    *  \sa TSConnection GetLastError()
    */
    bool SetConnection(TSConnectionThread *pConnection);

   /*! Sets the TeamSpeak client version number (e.g. "2.0.32.60").
    *  \param str Client version number.
    *  \return Returns false if fails, check GetLastError for details.
    *  \sa TSConnection GetLastError()
    */
    bool SetVersionNumber(wxString const &str);

   /*! Sets the session id.
    *  \param id Session id.
    *  \return Returns false if fails, check GetLastError for details.
    *  \sa GetLastError()
    */
    bool SetSessionId(wxUint32 const id);

   /*! Check if connected.
    *  \return Returns true if connected.
    *  \sa GetLastError()
    */
    bool IsConnected();

   /*! Returns the state of the reconnectflag
    *  \return Returns false if fails, check GetLastError for details.
    *  \sa GetLastError()
    */
    bool IsReconnectActive()
    {
      return m_pReconnectAct;
    }

   /*! Sets Sync onject, for thread sync.
    *  \param sync Sync onject.
    *  \return Returns false if fails, check GetLastError for details.
    *  \sa GetLastError()
    */
    bool SetSync(TSCmd *sync);

   /*! Gets the TeamSpeak client Platform string.
    *  \return Client Platform string.
    */
    wxString const &GetPlatform() const
    {
      return m_Platform;
    }

   /*! Gets the LastError message, call this method
    *  to get more information for an error.
    *  \return LastError message.
    */
    wxString const &GetLastError() const
    {
      return m_LastError;
    }

   /*! Gets the version number
    *  \return Version number.
    */
    wxString const &GetVersionNumber() const
    {
      return m_VersionNumber;
    }

   /*! Gets the TeamSpeak player.
    *  \return TeamSpeak player.
    *  \sa TSPlayer
    */
    TSPlayer *GetPlayer() const
    {
      return m_pPlayer;
    }

   /*! Gets all TeamSpeak players on the server.
    *  \return TeamSpeak players.
    *  \sa TSPlayer
    */
    TSpPlayerArray *GetPlayers() const
    {
      return m_ppPlayers;
    }

   /*! Gets the TeamSpeak channel.
    *  \return TeamSpeak channel.
    *  \sa TSChannel
    */
    TSChannel *GetChannel() const
    {
      return m_pChannel;
    }

   /*! Gets all TeamSpeak channels on the server.
    *  \return TeamSpeak channels.
    *  \sa TSChannel
    */
    TSpChannelArray *GetChannels() const
    {
      return m_ppChannels;
    }

   /*! Gets the TeamSpeak server.
    *  \return TeamSpeak server.
    *  \sa TSServer
    */
    TSServer *GetServer() const
    {
      return m_pServer;
    }

   /*! Gets the TeamSpeak connection.
    *  \return TeamSpeak connection.
    *  \sa TSConnection
    */
    TSConnectionThread *GetConnection() const
    {
      return m_pConnection;
    }

   /*! Gets the session id.
    *  \return Session id.
    */
    wxUint32 GetSessionId() const
    {
      return m_SessionId;
    }

   /*! Gets Sync object, for thread sync
    *  \return Sync object.
    */
    TSCmd *GetSync() const
    {
      return m_pSync;
    }

   /*! Send a command to a Teasmpeak server.
    *  \param cmd Command to send.
    *  \return Returns false if fails, check GetLastError for details.
    *  \sa GetLastError() TSCommand for commands
    */
    bool SendCommand(wxUint32 cmd);

   /*! Send a command to a Teasmpeak server.
    *  \param cmd Command to send.
    *  \return Returns false if fails, check GetLastError for details.
    *  \sa GetLastError() TSCommand for commands
    */
    bool SendCommand(TSCmd *cmd);

   /*! Finds a player
    *  \param id Player id.
    *  \return Returns NULL if fails, check GetLastError for details.
    *  \sa GetLastError()
    */
    TSPlayer *FindPlayer(wxUint32 id);

   /*! Finds a player
    *  \param str Player nick.
    *  \return Returns NULL if fails, check GetLastError for details.
    *  \sa GetLastError()
    */
    TSPlayer *FindPlayer(wxString const &str);

   /*! Finds a channel
    *  \param id Channel id.
    *  \return Returns NULL if fails, check GetLastError for details.
    *  \sa GetLastError()
    */
    TSChannel *FindChannel(wxUint32 id);

   /*! Finds a channel
    *  \param id Channel id.
    *  \return Returns NULL if fails, check GetLastError for details.
    *  \sa GetLastError()
    */
    TSChannel *FindChannel(wxString const &str);

   /*! Finds default channel
    *  \return Returns NULL if fails, check GetLastError for details.
    *  \sa GetLastError()
    */
    TSChannel *FindDefaultChannel();

   /*! Finds all channel with this parent id.
    *  \param id Channel id.
    *  \param channels Channel array
    *  \sa TSpChannelArray
    */
    bool FindChannelsByParent(wxUint32 id, TSpChannelArray *channels);

   /*! Finds all channels per name, regex.
    *  \param str Channel name.
    *  \param channels Channel array
    *  \sa TSpChannelArray
    */
    bool FindChannelsByName(wxString const &str, TSpChannelArray *channels);

   /*! Finds all players per name, regex.
    *  \param str Player name.
    *  \param players Player array
    *  \sa TSpPlayerArray
    */
    bool FindPlayersByName(wxString const &str, TSpPlayerArray *players);

   /*! Finds all players in channel.
    *  \param channel Channel.
    *  \param players Player array
    *  \sa TSpPlayerArray
    */
    bool FindPlayersByChannel(TSChannel *channel, TSpPlayerArray *players);

   /*! Creates a channel.
    *  \param channel Channel object.
    *  \return Returns false if fails, check GetLastError for details.
    *  \sa GetLastError() TSChannel
    */
    bool CreateChannel(TSChannel *channel);

   /*! Deletes a channel.
    *  \param channel Channel object.
    *  \return Returns false if fails, check GetLastError for details.
    *  \sa GetLastError() TSChannel
    */
    bool DeleteChannel(TSChannel *channel);

   /*! Modify a channel.
    *  \param channel Channel object.
    *  \return Returns false if fails, check GetLastError for details.
    *  \sa GetLastError() TSChannel
    */
    bool ModifyChannel(TSChannel *channel);

   /*! Moves a Player.
    *  \param player Player object.
    *  \return Returns false if fails, check GetLastError for details.
    *  \sa GetLastError() TSChannel
    */
    bool MovePlayer(TSPlayer *player);

   /*! Kick a Player.
    *  \param player Player object.
    *  \param msg Kick message.
    *  \return Returns false if fails, check GetLastError for details.
    *  \sa GetLastError() TSChannel
    */
    bool KickPlayer(TSPlayer *player, wxString msg = _T(""));

   /*! Dumps object.
    *  \param ostrm Stream to write.
    */
    void Dump(wxOutputStream &ostrm) const;

  private:
    //Sets the LastError message.
    void SetLastError(wxString const &str)
    {
      m_LastError = str;
    }

    //Members
    TSPlayer           *m_pPlayer;
    TSpPlayerArray     *m_ppPlayers;
    TSChannel          *m_pChannel;
    TSpChannelArray    *m_ppChannels;
    TSServer           *m_pServer;
    TSConnectionThread *m_pConnection;
    wxString            m_LastError;
    wxString            m_VersionNumber;
    wxString            m_Platform;
    wxUint32            m_SessionId;
    wxMutex            *m_pMutex;
    TSCmd              *m_pSync;
    bool                m_pReconnectAct;
  public:
    bool                m_Lock;
};

#endif