gloox  1.0.1
clientbase.h
1 /*
2  Copyright (c) 2005-2012 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 
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 
682  // reimplemented from ParserHandler
683  virtual void handleTag( Tag* tag );
684 
685  // reimplemented from CompressionDataHandler
686  virtual void handleCompressedData( const std::string& data );
687 
688  // reimplemented from CompressionDataHandler
689  virtual void handleDecompressedData( const std::string& data );
690 
691  // reimplemented from ConnectionDataHandler
692  virtual void handleReceivedData( const ConnectionBase* connection, const std::string& data );
693 
694  // reimplemented from ConnectionDataHandler
695  virtual void handleConnect( const ConnectionBase* connection );
696 
697  // reimplemented from ConnectionDataHandler
698  virtual void handleDisconnect( const ConnectionBase* connection, ConnectionError reason );
699 
700  // reimplemented from TLSHandler
701  virtual void handleEncryptedData( const TLSBase* base, const std::string& data );
702 
703  // reimplemented from TLSHandler
704  virtual void handleDecryptedData( const TLSBase* base, const std::string& data );
705 
706  // reimplemented from TLSHandler
707  virtual void handleHandshakeResult( const TLSBase* base, bool success, CertInfo &certinfo );
708 
709  protected:
715  void notifyOnResourceBindError( const Error* error );
716 
721  void notifyOnResourceBind( const std::string& resource );
722 
728  void notifyOnSessionCreateError( const Error* error );
729 
737  bool notifyOnTLSConnect( const CertInfo& info );
738 
742  void notifyOnConnect();
743 
748  void notifyStreamEvent( StreamEvent event );
749 
754  virtual void disconnect( ConnectionError reason );
755 
759  void header();
760 
765  void setAuthed( bool authed ) { m_authed = authed; }
766 
772  void setAuthFailure( AuthenticationError e ) { m_authError = e; }
773 
781  virtual bool checkStreamVersion( const std::string& version );
782 
787  void startSASL( SaslMechanism type );
788 
792  void processSASLSuccess();
793 
798  void processSASLChallenge( const std::string& challenge );
799 
804  void processSASLError( Tag* tag );
805 
810  void setNTLMDomain( const std::string& domain ) { m_ntlmDomain = domain; }
811 
815  void startTls();
816 
821  bool hasTls();
822 
828  void send( const std::string& xml );
829 
832  std::string m_authcid;
840 
841  GLOOX_DEPRECATED std::string m_selectedResource;
845  std::string m_clientCerts;
846  std::string m_clientKey;
847  std::string m_namespace;
848  std::string m_password;
849  std::string m_xmllang;
850  std::string m_server;
852  std::string m_sid;
853  bool m_compressionActive;
855  bool m_encryptionActive;
857  bool m_compress;
859  bool m_authed;
860  bool m_block;
861  bool m_sasl;
863  int m_port;
868  private:
869 #ifdef CLIENTBASE_TEST
870  public:
871 #endif
872 
878  class Ping : public StanzaExtension
879  {
880 
881  public:
885  Ping();
886 
890  virtual ~Ping();
891 
892  // reimplemented from StanzaExtension
893  virtual const std::string& filterString() const;
894 
895  // reimplemented from StanzaExtension
896  virtual StanzaExtension* newInstance( const Tag* tag ) const
897  {
898  (void)tag;
899  return new Ping();
900  }
901 
902  // reimplemented from StanzaExtension
903  virtual Tag* tag() const
904  {
905  return new Tag( "ping", "xmlns", XMLNS_XMPP_PING );
906  }
907 
908  // reimplemented from StanzaExtension
909  virtual StanzaExtension* clone() const
910  {
911  return new Ping();
912  }
913 
914  };
915 
916  ClientBase( const ClientBase& );
917  ClientBase& operator=( const ClientBase& );
918 
924  virtual void handleStartNode( const Tag* start ) = 0;
925 
931  virtual bool handleNormalNode( Tag* tag ) = 0;
932  virtual void rosterFilled() = 0;
933  virtual void cleanup() {}
934  virtual void handleIqIDForward( const IQ& iq, int context ) { (void) iq; (void) context; }
935 
936  void parse( const std::string& data );
937  void init();
938  void handleStreamError( Tag* tag );
939  TLSBase* getDefaultEncryption();
940  CompressionBase* getDefaultCompression();
941 
942  void notifyIqHandlers( IQ& iq );
943  void notifyMessageHandlers( Message& msg );
944  void notifyPresenceHandlers( Presence& presence );
945  void notifySubscriptionHandlers( Subscription& s10n );
946  void notifyTagHandlers( Tag* tag );
947  void notifyOnDisconnect( ConnectionError e );
948  void addFrom( Tag* tag );
949  void addNamespace( Tag* tag );
950 
951  // reimplemented from IqHandler
952  virtual bool handleIq( const IQ& iq );
953 
954  // reimplemented from IqHandler
955  virtual void handleIqID( const IQ& iq, int context );
956 
957  struct TrackStruct
958  {
959  IqHandler* ih;
960  int context;
961  bool del;
962  };
963 
964  struct TagHandlerStruct
965  {
966  TagHandler* th;
967  std::string xmlns;
968  std::string tag;
969  };
970 
971  struct JidPresHandlerStruct
972  {
973  JID* jid;
974  PresenceHandler* ph;
975  };
976 
977  enum TrackContext
978  {
979  XMPPPing
980  };
981 
982  typedef std::list<ConnectionListener*> ConnectionListenerList;
983  typedef std::multimap<const std::string, IqHandler*> IqHandlerMapXmlns;
984  typedef std::multimap<const int, IqHandler*> IqHandlerMap;
985  typedef std::map<const std::string, TrackStruct> IqTrackMap;
986  typedef std::map<const std::string, MessageHandler*> MessageHandlerMap;
987  typedef std::list<MessageSession*> MessageSessionList;
988  typedef std::list<MessageHandler*> MessageHandlerList;
989  typedef std::list<PresenceHandler*> PresenceHandlerList;
990  typedef std::list<JidPresHandlerStruct> PresenceJidHandlerList;
991  typedef std::list<SubscriptionHandler*> SubscriptionHandlerList;
992  typedef std::list<TagHandlerStruct> TagHandlerList;
993 
994  ConnectionListenerList m_connectionListeners;
995  IqHandlerMapXmlns m_iqNSHandlers;
996  IqHandlerMap m_iqExtHandlers;
997  IqTrackMap m_iqIDHandlers;
998  MessageSessionList m_messageSessions;
999  MessageHandlerList m_messageHandlers;
1000  PresenceHandlerList m_presenceHandlers;
1001  PresenceJidHandlerList m_presenceJidHandlers;
1002  SubscriptionHandlerList m_subscriptionHandlers;
1003  TagHandlerList m_tagHandlers;
1004  StringList m_cacerts;
1005  StatisticsHandler * m_statisticsHandler;
1006  MUCInvitationHandler * m_mucInvitationHandler;
1007  MessageSessionHandler * m_messageSessionHandlerChat;
1008  MessageSessionHandler * m_messageSessionHandlerGroupchat;
1009  MessageSessionHandler * m_messageSessionHandlerHeadline;
1010  MessageSessionHandler * m_messageSessionHandlerNormal;
1011 
1012  util::Mutex m_iqHandlerMapMutex;
1013  util::Mutex m_iqExtHandlerMapMutex;
1014 
1015  Parser m_parser;
1016  LogSink m_logInstance;
1017  StanzaExtensionFactory* m_seFactory;
1018  EventDispatcher m_dispatcher;
1019 
1020  AuthenticationError m_authError;
1021  StreamError m_streamError;
1022  StringMap m_streamErrorText;
1023  std::string m_streamErrorCData;
1024  Tag* m_streamErrorAppCondition;
1025 
1026  StatisticsStruct m_stats;
1027 
1028  SaslMechanism m_selectedSaslMech;
1029 
1030  std::string m_ntlmDomain;
1031  bool m_autoMessageSession;
1032  bool m_customConnection;
1033 
1034  int m_uniqueBaseId;
1035  util::AtomicRefCount m_nextId;
1036 
1037 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
1038  CredHandle m_credHandle;
1039  CtxtHandle m_ctxtHandle;
1040 #endif
1041 
1042  };
1043 
1044 }
1045 
1046 #endif // CLIENTBASE_H__