gloox  1.0.21
client.h
1 /*
2  Copyright (c) 2004-2017 by Jakob Schröter <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 
302  void setActive() { if( hasClientStateIndication() )
303  send( new Tag( "active", XMLNS, XMLNS_CLIENT_STATE_INDICATION ) ); }
304 
312  void setInactive() { if( hasClientStateIndication() )
313  send( new Tag( "inactive", XMLNS, XMLNS_CLIENT_STATE_INDICATION ) ); }
314 
322  return ((m_streamFeatures & StreamFeatureClientStateIndication) == StreamFeatureClientStateIndication); }
323 
328  Presence& presence() { return m_presence; }
329 
335  GLOOX_DEPRECATED void setForceNonSasl( bool force = true ) { m_forceNonSasl = force; }
336 
342  void disableRoster();
343 
348  RosterManager* rosterManager() { return m_rosterManager; }
349 
353  void disconnect();
354 
362  bool login();
363 
364  protected:
368  void nonSaslLogin();
369 
370  private:
371 #ifdef CLIENT_TEST
372  public:
373 #endif
374 
380  class ResourceBind : public StanzaExtension
381  {
382 
383  public:
390  ResourceBind( const std::string& resource, bool bind = true );
391 
396  ResourceBind( const Tag* tag );
397 
401  ~ResourceBind();
402 
407  const std::string& resource() const { return m_resource; }
408 
413  const JID& jid() const { return m_jid; }
414 
420  bool unbind() const { return !m_bind; }
421 
422  // reimplemented from StanzaExtension
423  virtual const std::string& filterString() const;
424 
425  // reimplemented from StanzaExtension
426  virtual StanzaExtension* newInstance( const Tag* tag ) const
427  {
428  return new ResourceBind( tag );
429  }
430 
431  // reimplemented from StanzaExtension
432  virtual Tag* tag() const;
433 
434  // reimplemented from StanzaExtension
435  virtual StanzaExtension* clone() const
436  {
437  return new ResourceBind( *this );
438  }
439 
440  private:
441  std::string m_resource;
442  JID m_jid;
443  bool m_bind;
444  };
445 
452  class SessionCreation : public StanzaExtension
453  {
454 
455  public:
459  SessionCreation() : StanzaExtension( ExtSessionCreation ) {}
460 
464  ~SessionCreation() {}
465 
466  // reimplemented from StanzaExtension
467  virtual const std::string& filterString() const { return EmptyString; }
468 
469  // reimplemented from StanzaExtension
470  virtual StanzaExtension* newInstance( const Tag* tag ) const
471  { (void)tag; return 0; }
472 
473  // reimplemented from StanzaExtension
474  virtual Tag* tag() const;
475 
476  // reimplemented from StanzaExtension
477  virtual StanzaExtension* clone() const
478  { return 0; }
479 
480  };
481 
482  virtual void handleStartNode( const Tag* /*start*/ ) {}
483  virtual bool handleNormalNode( Tag* tag );
484  virtual void disconnect( ConnectionError reason );
485  virtual void handleIqIDForward( const IQ& iq, int context );
486 
487  int getStreamFeatures( Tag* tag );
488  int getSaslMechs( Tag* tag );
489  int getCompressionMethods( Tag* tag );
490  void processResourceBind( const IQ& iq );
491  void processCreateSession( const IQ& iq );
492  void sendPresence( Presence& pres );
493  void createSession();
494  void negotiateCompression( StreamFeature method );
495  void connected();
496  virtual void rosterFilled();
497  virtual void cleanup();
498  bool bindOperation( const std::string& resource, bool bind );
499  void sendStreamManagement();
500 
501  void init();
502 
503  enum TrackContext
504  {
505  CtxResourceBind = 1000, // must be higher than the last element in ClientBase's TrackContext
506  CtxResourceUnbind,
507  CtxSessionEstablishment
508  };
509 
510  RosterManager* m_rosterManager;
511  NonSaslAuth* m_auth;
512 
513  Presence m_presence;
514 
515  bool m_forceNonSasl;
516  bool m_manageRoster;
517 
518  std::string m_smId;
519  std::string m_smLocation;
520  bool m_smResume;
521  bool m_smWanted;
522  int m_smMax;
523 
524  int m_streamFeatures;
525 
526  };
527 
528 }
529 
530 #endif // CLIENT_H__
Presence & presence()
Definition: client.h:328
GLOOX_DEPRECATED void setForceNonSasl(bool force=true)
Definition: client.h:335
void setInactive()
Definition: client.h:312
const std::string XMLNS
Definition: gloox.cpp:122
bool hasResourceBind() const
Definition: client.h:183
An abstraction of an IQ stanza.
Definition: iq.h:33
This class implements a basic Jabber/XMPP Client.
Definition: client.h:116
This class implements an XML parser.
Definition: parser.h:34
const std::string & resource() const
Definition: client.h:200
int priority() const
Definition: client.h:239
This class is an implementation of XEP-0078 (Non-SASL Authentication).
Definition: nonsaslauth.h:39
ConnectionError
Definition: gloox.h:683
RosterManager * rosterManager()
Definition: client.h:348
const std::string XMLNS_CLIENT_STATE_INDICATION
Definition: gloox.cpp:115
void setResource(const std::string &resource)
Definition: client.h:251
bool bindResource(const std::string &resource)
Definition: client.h:163
An abstraction of a presence stanza.
Definition: presence.h:32
bool hasClientStateIndication() const
Definition: client.h:321
The namespace for the gloox library.
Definition: adhoc.cpp:27
This class abstracts a stanza extension, which is usually an XML child element in a specific namespac...
This class implements Jabber/XMPP roster handling in the jabber:iq:roster namespace.
Definition: rostermanager.h:47
void setActive()
Definition: client.h:302
An abstraction of a JID.
Definition: jid.h:30
StreamFeature
Definition: gloox.h:733
void setPresence()
Definition: client.h:293
bool unbindResource(const std::string &resource)
Definition: client.h:193
const std::string EmptyString
Definition: gloox.cpp:124
This is the common base class for a Jabber/XMPP Client and a Jabber Component.
Definition: clientbase.h:76
This is an abstraction of an XML element.
Definition: tag.h:46