gloox  1.0
clientbase.h
1 /*
2  Copyright (c) 2005-2009 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 
32 #include <string>
33 #include <list>
34 #include <map>
35 
36 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
37 #include <windows.h>
38 #define SECURITY_WIN32
39 #include <security.h>
40 #endif
41 
42 namespace gloox
43 {
44 
45  class Disco;
46  class EventHandler;
47  class Event;
48  class Tag;
49  class IQ;
50  class Message;
51  class Presence;
52  class Subscription;
53  class MessageSessionHandler;
54  class ConnectionListener;
55  class MessageHandler;
56  class MessageSession;
57  class PresenceHandler;
58  class SubscriptionHandler;
59  class MUCInvitationHandler;
60  class TagHandler;
61  class TLSBase;
62  class ConnectionBase;
63  class CompressionBase;
64  class StanzaExtensionFactory;
65 
75  class GLOOX_API ClientBase : public TagHandler, public ConnectionDataHandler,
76  public CompressionDataHandler, public TLSHandler,
77  public IqHandler
78  {
79 
80  friend class RosterManager;
81 
82  public:
91  ClientBase( const std::string& ns, const std::string& server, int port = -1 );
92 
102  ClientBase( const std::string& ns, const std::string& password,
103  const std::string& server, int port = -1 );
104 
108  virtual ~ClientBase();
109 
122  bool connect( bool block = true );
123 
131  ConnectionError recv( int timeout = -1 );
132 
137  virtual const std::string& username() const { return m_jid.username(); }
138 
146  const JID& jid() { return m_authzid ? m_authzid : m_jid; }
147 
153  void setSasl( bool sasl ) { m_sasl = sasl; }
154 
160  void setTls( TLSPolicy tls ) { m_tls = tls; }
161 
167  void setCompression( bool compression ) { m_compress = compression; }
168 
174  void setPort( int port ) { m_port = port; }
175 
183  void setServer( const std::string &server );
184 
189  void setPassword( const std::string &password ) { m_password = password; }
190 
195  const std::string& server() const { return m_server; }
196 
201  bool sasl() const { return m_sasl; }
202 
207  TLSPolicy tls() const { return m_tls; }
208 
213  bool compression() const { return m_compress; }
214 
220  int port() const { return m_port; }
221 
226  virtual const std::string& password() const { return m_password; }
227 
232  virtual Disco* disco() const { return m_disco; }
233 
239  const std::string getID();
240 
248  void send( Tag* tag );
249 
259  void send( IQ& iq, IqHandler* ih, int context, bool del = false );
260 
265  void send( const IQ& iq );
266 
271  void send( const Message& msg );
272 
277  void send( const Subscription& sub );
278 
283  void send( Presence& pres );
284 
289  bool authed() const { return m_authed; }
290 
295  ConnectionState state() const;
296 
301  const std::string& xmlLang() const { return m_xmllang; }
302 
309  void setXmlLang( const std::string& xmllang ) { m_xmllang = xmllang; }
310 
316  ConnectionBase* connectionImpl() const { return m_connection; }
317 
325  void setConnectionImpl( ConnectionBase* cb );
326 
332  TLSBase* encryptionImpl() const { return m_encryption; }
333 
341  void setEncryptionImpl( TLSBase* tb );
342 
348  CompressionBase* compressionImpl() const { return m_compression; }
349 
357  void setCompressionImpl( CompressionBase* cb );
358 
363  void whitespacePing();
364 
371  void xmppPing( const JID& to, EventHandler* eh );
372 
380  void setAuthzid( const JID& authzid ) { m_authzid = authzid; }
381 
390  void setAuthcid( const std::string& authcid ) { m_authcid = authcid; }
391 
402  void setSASLMechanisms( int mechanisms ) { m_availableSaslMechs = mechanisms; }
403 
408  void registerStanzaExtension( StanzaExtension* ext );
409 
415  bool removeStanzaExtension( int ext );
416 
421  void registerConnectionListener( ConnectionListener* cl );
422 
432  void registerIqHandler( IqHandler* ih, int exttype );
433 
441  void removeIDHandler( IqHandler* ih );
442 
447  void registerMessageHandler( MessageHandler* mh );
448 
453  void removeMessageHandler( MessageHandler* mh );
454 
464  void registerMessageSession( MessageSession* session );
465 
470  void disposeMessageSession( MessageSession* session );
471 
476  void registerPresenceHandler( PresenceHandler* ph );
477 
487  void registerPresenceHandler( const JID& jid, PresenceHandler* ph );
488 
493  void registerSubscriptionHandler( SubscriptionHandler* sh );
494 
502  void registerTagHandler( TagHandler* th, const std::string& tag,
503  const std::string& xmlns );
504 
511  void registerStatisticsHandler( StatisticsHandler* sh );
512 
517  void removeConnectionListener( ConnectionListener* cl );
518 
526  void removeIqHandler( IqHandler* ih, int exttype );
527 
532  void removePresenceHandler( PresenceHandler* ph );
533 
540  void removePresenceHandler( const JID& jid, PresenceHandler* ph );
541 
546  void removeSubscriptionHandler( SubscriptionHandler* sh );
547 
554  void removeTagHandler( TagHandler* th, const std::string& tag,
555  const std::string& xmlns );
556 
560  void removeStatisticsHandler();
561 
567  void setCACerts( const StringList& cacerts ) { m_cacerts = cacerts; }
568 
580  void setClientCert( const std::string& clientKey, const std::string& clientCerts );
581 
592  void registerMessageSessionHandler( MessageSessionHandler* msh, int types = 0 );
593 
598  LogSink& logInstance() { return m_logInstance; }
599 
606  StreamError streamError() const { return m_streamError; }
607 
617  const std::string& streamErrorText( const std::string& lang = "default" ) const;
618 
625  const std::string& streamErrorCData() const { return m_streamErrorCData; }
626 
632  const Tag* streamErrorAppCondition() const { return m_streamErrorAppCondition; }
633 
639  AuthenticationError authError() const { return m_authError; }
640 
646  StatisticsStruct getStatistics();
647 
652  void registerMUCInvitationHandler( MUCInvitationHandler* mih );
653 
657  void removeMUCInvitationHandler();
658 
666  void addPresenceExtension( StanzaExtension* se );
667 
673  bool removePresenceExtension( int type );
674 
679  const StanzaExtensionList& presenceExtensions() const { return m_presenceExtensions; }
680 
681  // reimplemented from ParserHandler
682  virtual void handleTag( Tag* tag );
683 
684  // reimplemented from CompressionDataHandler
685  virtual void handleCompressedData( const std::string& data );
686 
687  // reimplemented from CompressionDataHandler
688  virtual void handleDecompressedData( const std::string& data );
689 
690  // reimplemented from ConnectionDataHandler
691  virtual void handleReceivedData( const ConnectionBase* connection, const std::string& data );
692 
693  // reimplemented from ConnectionDataHandler
694  virtual void handleConnect( const ConnectionBase* connection );
695 
696  // reimplemented from ConnectionDataHandler
697  virtual void handleDisconnect( const ConnectionBase* connection, ConnectionError reason );
698 
699  // reimplemented from TLSHandler
700  virtual void handleEncryptedData( const TLSBase* base, const std::string& data );
701 
702  // reimplemented from TLSHandler
703  virtual void handleDecryptedData( const TLSBase* base, const std::string& data );
704 
705  // reimplemented from TLSHandler
706  virtual void handleHandshakeResult( const TLSBase* base, bool success, CertInfo &certinfo );
707 
708  protected:
714  void notifyOnResourceBindError( const Error* error );
715 
720  void notifyOnResourceBind( const std::string& resource );
721 
727  void notifyOnSessionCreateError( const Error* error );
728 
736  bool notifyOnTLSConnect( const CertInfo& info );
737 
741  void notifyOnConnect();
742 
747  void notifyStreamEvent( StreamEvent event );
748 
753  virtual void disconnect( ConnectionError reason );
754 
758  void header();
759 
764  void setAuthed( bool authed ) { m_authed = authed; }
765 
771  void setAuthFailure( AuthenticationError e ) { m_authError = e; }
772 
780  virtual bool checkStreamVersion( const std::string& version );
781 
786  void startSASL( SaslMechanism type );
787 
791  void processSASLSuccess();
792 
797  void processSASLChallenge( const std::string& challenge );
798 
803  void processSASLError( Tag* tag );
804 
809  void setNTLMDomain( const std::string& domain ) { m_ntlmDomain = domain; }
810 
814  void startTls();
815 
820  bool hasTls();
821 
824  std::string m_authcid;
832 
833  std::string m_selectedResource;
835  std::string m_clientCerts;
836  std::string m_clientKey;
837  std::string m_namespace;
838  std::string m_password;
839  std::string m_xmllang;
840  std::string m_server;
842  std::string m_sid;
843  bool m_compressionActive;
845  bool m_encryptionActive;
847  bool m_compress;
849  bool m_authed;
850  bool m_block;
851  bool m_sasl;
853  int m_port;
858  private:
859 #ifdef CLIENTBASE_TEST
860  public:
861 #endif
862 
868  class Ping : public StanzaExtension
869  {
870 
871  public:
875  Ping();
876 
880  virtual ~Ping();
881 
882  // reimplemented from StanzaExtension
883  virtual const std::string& filterString() const;
884 
885  // reimplemented from StanzaExtension
886  virtual StanzaExtension* newInstance( const Tag* tag ) const
887  {
888  (void)tag;
889  return new Ping();
890  }
891 
892  // reimplemented from StanzaExtension
893  virtual Tag* tag() const
894  {
895  return new Tag( "ping", "xmlns", XMLNS_XMPP_PING );
896  }
897 
898  // reimplemented from StanzaExtension
899  virtual StanzaExtension* clone() const
900  {
901  return new Ping();
902  }
903 
904  };
905 
906  ClientBase( const ClientBase& );
907  ClientBase& operator=( const ClientBase& );
908 
912  virtual void handleStartNode() = 0;
913 
919  virtual bool handleNormalNode( Tag* tag ) = 0;
920  virtual void rosterFilled() = 0;
921  virtual void cleanup() {}
922  virtual void handleIqIDForward( const IQ& iq, int context ) { (void) iq; (void) context; }
923 
924  void parse( const std::string& data );
925  void init();
926  void handleStreamError( Tag* tag );
927  TLSBase* getDefaultEncryption();
928  CompressionBase* getDefaultCompression();
929 
930  void notifyIqHandlers( IQ& iq );
931  void notifyMessageHandlers( Message& msg );
932  void notifyPresenceHandlers( Presence& presence );
933  void notifySubscriptionHandlers( Subscription& s10n );
934  void notifyTagHandlers( Tag* tag );
935  void notifyOnDisconnect( ConnectionError e );
936  void send( const std::string& xml );
937  void addFrom( Tag* tag );
938  void addNamespace( Tag* tag );
939 
940  // reimplemented from IqHandler
941  virtual bool handleIq( const IQ& iq );
942 
943  // reimplemented from IqHandler
944  virtual void handleIqID( const IQ& iq, int context );
945 
946  struct TrackStruct
947  {
948  IqHandler* ih;
949  int context;
950  bool del;
951  };
952 
953  struct TagHandlerStruct
954  {
955  TagHandler* th;
956  std::string xmlns;
957  std::string tag;
958  };
959 
960  struct JidPresHandlerStruct
961  {
962  JID* jid;
963  PresenceHandler* ph;
964  };
965 
966  enum TrackContext
967  {
968  XMPPPing
969  };
970 
971  typedef std::list<ConnectionListener*> ConnectionListenerList;
972  typedef std::multimap<const std::string, IqHandler*> IqHandlerMapXmlns;
973  typedef std::multimap<const int, IqHandler*> IqHandlerMap;
974  typedef std::map<const std::string, TrackStruct> IqTrackMap;
975  typedef std::map<const std::string, MessageHandler*> MessageHandlerMap;
976  typedef std::list<MessageSession*> MessageSessionList;
977  typedef std::list<MessageHandler*> MessageHandlerList;
978  typedef std::list<PresenceHandler*> PresenceHandlerList;
979  typedef std::list<JidPresHandlerStruct> PresenceJidHandlerList;
980  typedef std::list<SubscriptionHandler*> SubscriptionHandlerList;
981  typedef std::list<TagHandlerStruct> TagHandlerList;
982 
983  ConnectionListenerList m_connectionListeners;
984  IqHandlerMapXmlns m_iqNSHandlers;
985  IqHandlerMap m_iqExtHandlers;
986  IqTrackMap m_iqIDHandlers;
987  MessageSessionList m_messageSessions;
988  MessageHandlerList m_messageHandlers;
989  PresenceHandlerList m_presenceHandlers;
990  PresenceJidHandlerList m_presenceJidHandlers;
991  SubscriptionHandlerList m_subscriptionHandlers;
992  TagHandlerList m_tagHandlers;
993  StringList m_cacerts;
994  StatisticsHandler * m_statisticsHandler;
995  MUCInvitationHandler * m_mucInvitationHandler;
996  MessageSessionHandler * m_messageSessionHandlerChat;
997  MessageSessionHandler * m_messageSessionHandlerGroupchat;
998  MessageSessionHandler * m_messageSessionHandlerHeadline;
999  MessageSessionHandler * m_messageSessionHandlerNormal;
1000 
1001  util::Mutex m_iqHandlerMapMutex;
1002 
1003  Parser m_parser;
1004  LogSink m_logInstance;
1005  StanzaExtensionFactory* m_seFactory;
1006  EventDispatcher m_dispatcher;
1007 
1008  AuthenticationError m_authError;
1009  StreamError m_streamError;
1010  StringMap m_streamErrorText;
1011  std::string m_streamErrorCData;
1012  Tag* m_streamErrorAppCondition;
1013 
1014  StatisticsStruct m_stats;
1015 
1016  SaslMechanism m_selectedSaslMech;
1017 
1018  std::string m_ntlmDomain;
1019  bool m_autoMessageSession;
1020 
1021 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
1022  CredHandle m_credHandle;
1023  CtxtHandle m_ctxtHandle;
1024 #endif
1025 
1026  };
1027 
1028 }
1029 
1030 #endif // CLIENTBASE_H__