gloox  1.0.10
client.h
1 /*
2  Copyright (c) 2004-2013 by Jakob Schroeter <js@camaya.net>
3  This file is part of the gloox library. http://camaya.net/gloox
4 
5  This software is distributed under a license. The full license
6  agreement can be found in the file LICENSE in this distribution.
7  This software may not be copied, modified, sold or distributed
8  other than expressed in the named license agreement.
9 
10  This software is distributed without any warranty.
11 */
12 
13 
14 #ifndef CLIENT_H__
15 #define CLIENT_H__
16 
17 #include "clientbase.h"
18 #include "presence.h"
19 
20 #include <string>
21 
22 namespace gloox
23 {
24 
25  class Capabilities;
26  class RosterManager;
27  class NonSaslAuth;
28  class IQ;
29 
116  class GLOOX_API Client : public ClientBase
117  {
118  public:
119 
120  friend class NonSaslAuth;
121  friend class Parser;
122 
129  Client( const std::string& server );
130 
141  Client( const JID& jid, const std::string& password, int port = -1 );
142 
146  virtual ~Client();
147 
163  bool bindResource( const std::string& resource )
164  { return bindOperation( resource, true ); }
165 
175  bool selectResource( const std::string& resource );
176 
183  bool hasResourceBind() const { return ((m_streamFeatures & StreamFeatureUnbind) == StreamFeatureUnbind); }
184 
193  bool unbindResource( const std::string& resource )
194  { return bindOperation( resource, false ); }
195 
200  const std::string& resource() const { return m_jid.resource(); }
201 
213  void setStreamManagement( bool enable = true, bool resume = true );
214 
224  void ackStreamManagement();
225 
233  void reqStreamManagement();
234 
239  int priority() const { return m_presence.priority(); }
240 
245  void setUsername( const std::string &username );
246 
251  void setResource( const std::string &resource ) { m_jid.setResource( resource ); }
252 
263  void setPresence( const JID& to, Presence::PresenceType pres, int priority,
264  const std::string& status = EmptyString );
265 
279  void setPresence( Presence::PresenceType pres, int priority,
280  const std::string& status = EmptyString );
281 
293  void setPresence() { sendPresence( m_presence ); }
294 
299  Presence& presence() { return m_presence; }
300 
306  GLOOX_DEPRECATED void setForceNonSasl( bool force = true ) { m_forceNonSasl = force; }
307 
313  void disableRoster();
314 
319  RosterManager* rosterManager() { return m_rosterManager; }
320 
324  void disconnect();
325 
333  bool login();
334 
335  protected:
339  void nonSaslLogin();
340 
341  private:
342 #ifdef CLIENT_TEST
343  public:
344 #endif
345 
351  class ResourceBind : public StanzaExtension
352  {
353 
354  public:
361  ResourceBind( const std::string& resource, bool bind = true );
362 
367  ResourceBind( const Tag* tag );
368 
372  ~ResourceBind();
373 
378  const std::string& resource() const { return m_resource; }
379 
384  const JID& jid() const { return m_jid; }
385 
391  bool unbind() const { return !m_bind; }
392 
393  // reimplemented from StanzaExtension
394  virtual const std::string& filterString() const;
395 
396  // reimplemented from StanzaExtension
397  virtual StanzaExtension* newInstance( const Tag* tag ) const
398  {
399  return new ResourceBind( tag );
400  }
401 
402  // reimplemented from StanzaExtension
403  virtual Tag* tag() const;
404 
405  // reimplemented from StanzaExtension
406  virtual StanzaExtension* clone() const
407  {
408  return new ResourceBind( *this );
409  }
410 
411  private:
412  std::string m_resource;
413  JID m_jid;
414  bool m_bind;
415  };
416 
423  class SessionCreation : public StanzaExtension
424  {
425 
426  public:
430  SessionCreation() : StanzaExtension( ExtSessionCreation ) {}
431 
435  ~SessionCreation() {}
436 
437  // reimplemented from StanzaExtension
438  virtual const std::string& filterString() const { return EmptyString; }
439 
440  // reimplemented from StanzaExtension
441  virtual StanzaExtension* newInstance( const Tag* tag ) const
442  { (void)tag; return 0; }
443 
444  // reimplemented from StanzaExtension
445  virtual Tag* tag() const;
446 
447  // reimplemented from StanzaExtension
448  virtual StanzaExtension* clone() const
449  { return 0; }
450 
451  };
452 
453  virtual void handleStartNode( const Tag* /*start*/ ) {}
454  virtual bool handleNormalNode( Tag* tag );
455  virtual void disconnect( ConnectionError reason );
456  virtual void handleIqIDForward( const IQ& iq, int context );
457 
458  int getStreamFeatures( Tag* tag );
459  int getSaslMechs( Tag* tag );
460  int getCompressionMethods( Tag* tag );
461  void processResourceBind( const IQ& iq );
462  void processCreateSession( const IQ& iq );
463  void sendPresence( Presence& pres );
464  void createSession();
465  void negotiateCompression( StreamFeature method );
466  void connected();
467  virtual void rosterFilled();
468  virtual void cleanup();
469  bool bindOperation( const std::string& resource, bool bind );
470  void sendStreamManagement();
471 
472  void init();
473 
474  enum TrackContext
475  {
476  CtxResourceBind = 1000, // must be higher than the last element in ClientBase's TrackContext
477  CtxResourceUnbind,
478  CtxSessionEstablishment
479  };
480 
481  RosterManager* m_rosterManager;
482  NonSaslAuth* m_auth;
483 
484  Presence m_presence;
485 
486  bool m_resourceBound;
487  bool m_forceNonSasl;
488  bool m_manageRoster;
489 
490  std::string m_smId;
491  std::string m_smLocation;
492  bool m_smResume;
493  bool m_smWanted;
494  int m_smMax;
495 
496  int m_streamFeatures;
497 
498  };
499 
500 }
501 
502 #endif // CLIENT_H__