gloox  1.0.28
clientbase.h
1 /*
2  Copyright (c) 2005-2023 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 
15 #ifndef CLIENTBASE_H__
16 #define CLIENTBASE_H__
17 
18 #include "macros.h"
19 #include "gloox.h"
20 #include "eventdispatcher.h"
21 #include "iqhandler.h"
22 #include "jid.h"
23 #include "logsink.h"
24 #include "mutex.h"
25 #include "taghandler.h"
26 #include "statisticshandler.h"
27 #include "tlshandler.h"
28 #include "compressiondatahandler.h"
29 #include "connectiondatahandler.h"
30 #include "parser.h"
31 #include "atomicrefcount.h"
32 
33 #include <string>
34 #include <list>
35 #include <map>
36 
37 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
38 #include <windows.h>
39 #define SECURITY_WIN32
40 #include <security.h>
41 #endif
42 
43 namespace gloox
44 {
45 
46  class Disco;
47  class EventHandler;
48  class Event;
49  class Tag;
50  class IQ;
51  class Message;
52  class Presence;
53  class Subscription;
54  class MessageSessionHandler;
55  class ConnectionListener;
56  class MessageHandler;
57  class MessageSession;
58  class PresenceHandler;
59  class SubscriptionHandler;
60  class MUCInvitationHandler;
61  class TagHandler;
62  class TLSBase;
63  class ConnectionBase;
64  class CompressionBase;
65  class StanzaExtensionFactory;
66 
76  class GLOOX_API ClientBase : public TagHandler, public ConnectionDataHandler,
77  public CompressionDataHandler, public TLSHandler,
78  public IqHandler
79  {
80 
81  friend class RosterManager;
82 
83  public:
92  ClientBase( const std::string& ns, const std::string& server, int port = -1 );
93 
103  ClientBase( const std::string& ns, const std::string& password,
104  const std::string& server, int port = -1 );
105 
109  virtual ~ClientBase();
110 
123  bool connect( bool block = true );
124 
132  virtual ConnectionError recv( int timeout = -1 );
133 
138  virtual const std::string& username() const { return m_jid.username(); }
139 
147  const JID& jid() { return m_authzid ? m_authzid : m_jid; }
148 
154  void setSasl( bool sasl ) { m_sasl = sasl; }
155 
161  void setTls( TLSPolicy tls ) { m_tls = tls; }
162 
168  void setCompression( bool compression ) { m_compress = compression; }
169 
175  void setPort( int port ) { m_port = port; }
176 
184  void setServer( const std::string &server );
185 
190  void setPassword( const std::string &password ) { m_password = password; }
191 
196  const std::string& server() const { return m_server; }
197 
202  bool sasl() const { return m_sasl; }
203 
208  TLSPolicy tls() const { return m_tls; }
209 
214  bool compression() const { return m_compress; }
215 
221  int port() const { return m_port; }
222 
227  virtual const std::string& password() const { return m_password; }
228 
233  virtual Disco* disco() const { return m_disco; }
234 
240  const std::string getID();
241 
249  void send( Tag* tag );
250 
260  void send( IQ& iq, IqHandler* ih, int context, bool del = false );
261 
266  void send( const IQ& iq );
267 
272  void send( const Message& msg );
273 
278  void send( const Subscription& sub );
279 
284  void send( const Presence& pres );
285 
290  bool authed() const { return m_authed; }
291 
296  ConnectionState state() const;
297 
302  const std::string& xmlLang() const { return m_xmllang; }
303 
310  void setXmlLang( const std::string& xmllang ) { m_xmllang = xmllang; }
311 
317  ConnectionBase* connectionImpl() const { return m_connection; }
318 
326  void setConnectionImpl( ConnectionBase* cb );
327 
333  TLSBase* encryptionImpl() const { return m_encryption; }
334 
342  void setEncryptionImpl( TLSBase* tb );
343 
349  CompressionBase* compressionImpl() const { return m_compression; }
350 
358  void setCompressionImpl( CompressionBase* cb );
359 
364  void whitespacePing();
365 
372  void xmppPing( const JID& to, EventHandler* eh );
373 
381  void setAuthzid( const JID& authzid ) { m_authzid = authzid; }
382 
391  void setAuthcid( const std::string& authcid ) { m_authcid = authcid; }
392 
403  void setSASLMechanisms( int mechanisms ) { m_availableSaslMechs = mechanisms; }
404 
409  void registerStanzaExtension( StanzaExtension* ext );
410 
416  bool removeStanzaExtension( int ext );
417 
422  void registerConnectionListener( ConnectionListener* cl );
423 
433  void registerIqHandler( IqHandler* ih, int exttype );
434 
442  void removeIDHandler( IqHandler* ih );
443 
448  void registerMessageHandler( MessageHandler* mh );
449 
454  void removeMessageHandler( MessageHandler* mh );
455 
465  void registerMessageSession( MessageSession* session );
466 
471  void disposeMessageSession( MessageSession* session );
472 
477  void registerPresenceHandler( PresenceHandler* ph );
478 
488  void registerPresenceHandler( const JID& jid, PresenceHandler* ph );
489 
494  void registerSubscriptionHandler( SubscriptionHandler* sh );
495 
503  void registerTagHandler( TagHandler* th, const std::string& tag,
504  const std::string& xmlns );
505 
512  void registerStatisticsHandler( StatisticsHandler* sh );
513 
518  void removeConnectionListener( ConnectionListener* cl );
519 
527  void removeIqHandler( IqHandler* ih, int exttype );
528 
533  void removePresenceHandler( PresenceHandler* ph );
534 
541  void removePresenceHandler( const JID& jid, PresenceHandler* ph );
542 
547  void removeSubscriptionHandler( SubscriptionHandler* sh );
548 
555  void removeTagHandler( TagHandler* th, const std::string& tag,
556  const std::string& xmlns );
557 
561  void removeStatisticsHandler();
562 
568  void setCACerts( const StringList& cacerts ) { m_cacerts = cacerts; }
569 
581  void setClientCert( const std::string& clientKey, const std::string& clientCerts );
582 
593  void registerMessageSessionHandler( MessageSessionHandler* msh, int types = 0 );
594 
599  LogSink& logInstance() { return m_logInstance; }
600 
607  StreamError streamError() const { return m_streamError; }
608 
618  const std::string& streamErrorText( const std::string& lang = "default" ) const;
619 
626  const std::string& streamErrorCData() const { return m_streamErrorCData; }
627 
633  const Tag* streamErrorAppCondition() const { return m_streamErrorAppCondition; }
634 
640  AuthenticationError authError() const { return m_authError; }
641 
647  StatisticsStruct getStatistics();
648 
653  void registerMUCInvitationHandler( MUCInvitationHandler* mih );
654 
658  void removeMUCInvitationHandler();
659 
667  void addPresenceExtension( StanzaExtension* se );
668 
674  bool removePresenceExtension( int type );
675 
680  const StanzaExtensionList& presenceExtensions() const { return m_presenceExtensions; }
681 
689  const TagList sendQueue();
690 
691  // reimplemented from ParserHandler
692  virtual void handleTag( Tag* tag );
693 
694  // reimplemented from CompressionDataHandler
695  virtual void handleCompressedData( const std::string& data );
696 
697  // reimplemented from CompressionDataHandler
698  virtual void handleDecompressedData( const std::string& data );
699 
700  // reimplemented from ConnectionDataHandler
701  virtual void handleReceivedData( const ConnectionBase* connection, const std::string& data );
702 
703  // reimplemented from ConnectionDataHandler
704  virtual void handleConnect( const ConnectionBase* connection );
705 
706  // reimplemented from ConnectionDataHandler
707  virtual void handleDisconnect( const ConnectionBase* connection, ConnectionError reason );
708 
709  // reimplemented from TLSHandler
710  virtual void handleEncryptedData( const TLSBase* base, const std::string& data );
711 
712  // reimplemented from TLSHandler
713  virtual void handleDecryptedData( const TLSBase* base, const std::string& data );
714 
715  // reimplemented from TLSHandler
716  virtual void handleHandshakeResult( const TLSBase* base, bool success, CertInfo &certinfo );
717 
718  protected:
719 #ifdef CLIENTBASE_TEST
720  public:
721 #endif
727  void notifyOnResourceBindError( const Error* error );
728 
733  void notifyOnResourceBind( const std::string& resource );
734 
740  void notifyOnSessionCreateError( const Error* error );
741 
749  bool notifyOnTLSConnect( const CertInfo& info );
750 
754  void notifyOnConnect();
755 
760  void notifyStreamEvent( StreamEvent event );
761 
766  virtual void disconnect( ConnectionError reason );
767 
771  void header();
772 
777  void setAuthed( bool authed ) { m_authed = authed; }
778 
784  void setAuthFailure( AuthenticationError e ) { m_authError = e; }
785 
793  virtual bool checkStreamVersion( const std::string& version );
794 
799  void startSASL( SaslMechanism type );
800 
808  bool processSASLSuccess( const std::string& payload );
809 
814  void processSASLChallenge( const std::string& challenge );
815 
820  void processSASLError( Tag* tag );
821 
826  void setNTLMDomain( const std::string& domain ) { m_ntlmDomain = domain; }
827 
831  void startTls();
832 
837  bool hasTls();
838 
844  void send( const std::string& xml );
845 
854  void checkQueue( int handled, bool resend );
855 
860  int stanzasSent() const { return m_smSent; }
861 
866  std::string getRandom();
867 
870  std::string m_authcid;
878 
879  GLOOX_DEPRECATED std::string m_selectedResource;
883  std::string m_clientCerts;
884  std::string m_clientKey;
885  std::string m_namespace;
886  std::string m_password;
887  std::string m_xmllang;
888  std::string m_server;
890  std::string m_sid;
895  bool m_compress;
897  bool m_authed;
899  bool m_block;
900  bool m_sasl;
902  int m_port;
911  {
917  CtxSMResumed
918  };
919 
924  private:
925 #ifdef CLIENTBASE_TEST
926  public:
927 #endif
934  class Ping : public StanzaExtension
935  {
936 
937  public:
941  Ping();
942 
946  virtual ~Ping();
947 
948  // reimplemented from StanzaExtension
949  virtual const std::string& filterString() const;
950 
951  // reimplemented from StanzaExtension
952  virtual StanzaExtension* newInstance( const Tag* tag ) const
953  {
954  (void)tag;
955  return new Ping();
956  }
957 
958  // reimplemented from StanzaExtension
959  virtual Tag* tag() const
960  {
961  return new Tag( "ping", "xmlns", XMLNS_XMPP_PING );
962  }
963 
964  // reimplemented from StanzaExtension
965  virtual StanzaExtension* clone() const
966  {
967  return new Ping();
968  }
969 
970  };
971 
972  ClientBase( const ClientBase& );
973  ClientBase& operator=( const ClientBase& );
974 
980  virtual void handleStartNode( const Tag* start ) = 0;
981 
988  virtual bool handleNormalNode( Tag* tag ) = 0;
989  virtual void rosterFilled() = 0;
990  virtual void cleanup() {}
991  virtual void handleIqIDForward( const IQ& iq, int context ) { (void) iq; (void) context; }
992  void send( Tag* tag, bool queue, bool del );
993  std::string hmac( const std::string& str, const std::string& key );
994  std::string hi( const std::string& str, const std::string& key, int iter );
995 
996  void parse( const std::string& data );
997  void init();
998  void handleStreamError( Tag* tag );
999  TLSBase* getDefaultEncryption();
1000  CompressionBase* getDefaultCompression();
1001 
1002  void notifyIqHandlers( IQ& iq );
1003  void notifyMessageHandlers( Message& msg );
1004  void notifyPresenceHandlers( Presence& presence );
1005  void notifySubscriptionHandlers( Subscription& s10n );
1006  void notifyTagHandlers( Tag* tag );
1007  void notifyOnDisconnect( ConnectionError e );
1008  void addFrom( Tag* tag );
1009  void addNamespace( Tag* tag );
1010 
1011  // reimplemented from IqHandler
1012  virtual bool handleIq( const IQ& iq );
1013 
1014  // reimplemented from IqHandler
1015  virtual void handleIqID( const IQ& iq, int context );
1016 
1017  struct TrackStruct
1018  {
1019  IqHandler* ih;
1020  int context;
1021  bool del;
1022  };
1023 
1024  struct TagHandlerStruct
1025  {
1026  TagHandler* th;
1027  std::string xmlns;
1028  std::string tag;
1029  };
1030 
1031  struct JidPresHandlerStruct
1032  {
1033  JID* jid;
1034  PresenceHandler* ph;
1035  };
1036 
1037  enum TrackContext
1038  {
1039  XMPPPing
1040  };
1041 
1042  typedef std::list<ConnectionListener*> ConnectionListenerList;
1043  typedef std::multimap<const std::string, IqHandler*> IqHandlerMapXmlns;
1044  typedef std::multimap<const int, IqHandler*> IqHandlerMap;
1045  typedef std::map<const std::string, TrackStruct> IqTrackMap;
1046  typedef std::map<const std::string, MessageHandler*> MessageHandlerMap;
1047  typedef std::map<int, Tag*> SMQueueMap;
1048  typedef std::list<MessageSession*> MessageSessionList;
1049  typedef std::list<MessageHandler*> MessageHandlerList;
1050  typedef std::list<PresenceHandler*> PresenceHandlerList;
1051  typedef std::list<JidPresHandlerStruct> PresenceJidHandlerList;
1052  typedef std::list<SubscriptionHandler*> SubscriptionHandlerList;
1053  typedef std::list<TagHandlerStruct> TagHandlerList;
1054 
1055  ConnectionListenerList m_connectionListeners;
1056  IqHandlerMapXmlns m_iqNSHandlers;
1057  IqHandlerMap m_iqExtHandlers;
1058  IqTrackMap m_iqIDHandlers;
1059  SMQueueMap m_smQueue;
1060  MessageSessionList m_messageSessions;
1061  MessageHandlerList m_messageHandlers;
1062  PresenceHandlerList m_presenceHandlers;
1063  PresenceJidHandlerList m_presenceJidHandlers;
1064  SubscriptionHandlerList m_subscriptionHandlers;
1065  TagHandlerList m_tagHandlers;
1066  StringList m_cacerts;
1067  StatisticsHandler * m_statisticsHandler;
1068  MUCInvitationHandler * m_mucInvitationHandler;
1069  MessageSessionHandler * m_messageSessionHandlerChat;
1070  MessageSessionHandler * m_messageSessionHandlerGroupchat;
1071  MessageSessionHandler * m_messageSessionHandlerHeadline;
1072  MessageSessionHandler * m_messageSessionHandlerNormal;
1073 
1074  util::Mutex m_iqHandlerMapMutex;
1075  util::Mutex m_iqExtHandlerMapMutex;
1076  util::Mutex m_queueMutex;
1077 
1078  Parser m_parser;
1079  LogSink m_logInstance;
1080  StanzaExtensionFactory* m_seFactory;
1081  EventDispatcher m_dispatcher;
1082 
1083  AuthenticationError m_authError;
1084  StreamError m_streamError;
1085  StringMap m_streamErrorText;
1086  std::string m_streamErrorCData;
1087  Tag* m_streamErrorAppCondition;
1088 
1089  StatisticsStruct m_stats;
1090 
1091  SaslMechanism m_selectedSaslMech;
1092 
1093  std::string m_clientFirstMessageBare;
1094  std::string m_serverSignature;
1095  std::string m_gs2Header;
1096  std::string m_ntlmDomain;
1097  bool m_customConnection;
1098 
1099  std::string m_uniqueBaseId;
1100  util::AtomicRefCount m_nextId;
1101 
1102  int m_smSent;
1103 
1104 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
1105  CredHandle m_credHandle;
1106  CtxtHandle m_ctxtHandle;
1107 #endif
1108 
1109  };
1110 
1111 }
1112 
1113 #endif // CLIENTBASE_H__
This is the common base class for a Jabber/XMPP Client and a Jabber Component.
Definition: clientbase.h:79
const Tag * streamErrorAppCondition() const
Definition: clientbase.h:633
SMContext m_smContext
Definition: clientbase.h:920
const std::string & server() const
Definition: clientbase.h:196
bool compression() const
Definition: clientbase.h:214
void setCompression(bool compression)
Definition: clientbase.h:168
std::string m_sid
Definition: clientbase.h:890
int port() const
Definition: clientbase.h:221
std::string m_clientCerts
Definition: clientbase.h:883
std::string m_authcid
Definition: clientbase.h:870
CompressionBase * compressionImpl() const
Definition: clientbase.h:349
void setAuthFailure(AuthenticationError e)
Definition: clientbase.h:784
void setAuthed(bool authed)
Definition: clientbase.h:777
void setCACerts(const StringList &cacerts)
Definition: clientbase.h:568
TLSPolicy tls() const
Definition: clientbase.h:208
void setAuthcid(const std::string &authcid)
Definition: clientbase.h:391
virtual Disco * disco() const
Definition: clientbase.h:233
TLSBase * m_encryption
Definition: clientbase.h:872
virtual const std::string & username() const
Definition: clientbase.h:138
StanzaExtensionList m_presenceExtensions
Definition: clientbase.h:877
void setPassword(const std::string &password)
Definition: clientbase.h:190
std::string m_xmllang
Definition: clientbase.h:887
bool m_compressionActive
Definition: clientbase.h:891
std::string m_clientKey
Definition: clientbase.h:884
const JID & jid()
Definition: clientbase.h:147
const std::string & xmlLang() const
Definition: clientbase.h:302
CompressionBase * m_compression
Definition: clientbase.h:873
AuthenticationError authError() const
Definition: clientbase.h:640
std::string m_password
Definition: clientbase.h:886
const StanzaExtensionList & presenceExtensions() const
Definition: clientbase.h:680
std::string m_server
Definition: clientbase.h:888
GLOOX_DEPRECATED std::string m_selectedResource
Definition: clientbase.h:879
void setSasl(bool sasl)
Definition: clientbase.h:154
LogSink & logInstance()
Definition: clientbase.h:599
std::string m_namespace
Definition: clientbase.h:885
TLSPolicy m_tls
Definition: clientbase.h:901
virtual const std::string & password() const
Definition: clientbase.h:227
void setSASLMechanisms(int mechanisms)
Definition: clientbase.h:403
ConnectionBase * connectionImpl() const
Definition: clientbase.h:317
bool sasl() const
Definition: clientbase.h:202
void setNTLMDomain(const std::string &domain)
Definition: clientbase.h:826
void setAuthzid(const JID &authzid)
Definition: clientbase.h:381
void setXmlLang(const std::string &xmllang)
Definition: clientbase.h:310
const std::string & streamErrorCData() const
Definition: clientbase.h:626
ConnectionBase * m_connection
Definition: clientbase.h:871
int stanzasSent() const
Definition: clientbase.h:860
void setTls(TLSPolicy tls)
Definition: clientbase.h:161
TLSBase * encryptionImpl() const
Definition: clientbase.h:333
bool authed() const
Definition: clientbase.h:290
StreamError streamError() const
Definition: clientbase.h:607
void setPort(int port)
Definition: clientbase.h:175
This is an abstract base class for stream compression implementations.
An abstract base class used to receive de/compressed data from a CompressionBase-derived object.
An abstract base class for a connection.
This is an abstract base class to receive events from a ConnectionBase-derived object.
Derived classes can be registered as ConnectionListeners with the Client.
This class implements XEP-0030 (Service Discovery) and XEP-0092 (Software Version).
Definition: disco.h:46
A stanza error abstraction implemented as a StanzaExtension.
Definition: error.h:35
An base class for event handlers.
Definition: eventhandler.h:29
An abstraction of an IQ stanza.
Definition: iq.h:34
A virtual interface which can be reimplemented to receive IQ stanzas.
Definition: iqhandler.h:32
An abstraction of a JID.
Definition: jid.h:31
An implementation of log sink and source.
Definition: logsink.h:39
A handler that can be used to receive invitations to MUC rooms.
A virtual interface which can be reimplemented to receive incoming message stanzas.
A virtual interface which can be reimplemented to receive incoming message sessions.
An abstraction of a message session between any two entities.
An abstraction of a message stanza.
Definition: message.h:34
A virtual interface which can be reimplemented to receive presence stanzas.
An abstraction of a presence stanza.
Definition: presence.h:33
This class implements Jabber/XMPP roster handling in the jabber:iq:roster namespace.
Definition: rostermanager.h:49
This class abstracts a stanza extension, which is usually an XML child element in a specific namespac...
A virtual interface which can be reimplemented to receive connection statistics.
A virtual interface which can be reimplemented to receive incoming subscription stanzas.
An abstraction of a subscription stanza.
Definition: subscription.h:32
An abstract base class for TLS implementations.
Definition: tlsbase.h:32
An interface that allows for interacting with TLS implementations derived from TLSBase.
Definition: tlshandler.h:35
A virtual interface which can be reimplemented to receive non-XMPP Core stanzas.
Definition: taghandler.h:33
This is an abstraction of an XML element.
Definition: tag.h:47
The namespace for the gloox library.
Definition: adhoc.cpp:28
std::list< Tag * > TagList
Definition: tag.h:26
const std::string XMLNS_XMPP_PING
Definition: gloox.cpp:62
ConnectionError
Definition: gloox.h:684
std::list< const StanzaExtension * > StanzaExtensionList
Definition: gloox.h:1268
std::list< std::string > StringList
Definition: gloox.h:1251
AuthenticationError
Definition: gloox.h:1011
StreamEvent
Definition: gloox.h:653
TLSPolicy
Definition: gloox.h:722
StreamError
Definition: gloox.h:775
SaslMechanism
Definition: gloox.h:757
ConnectionState
Definition: gloox.h:641
std::map< std::string, std::string > StringMap
Definition: gloox.h:1261