gloox  1.0
client.h
1 /*
2  Copyright (c) 2004-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 #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 
98  class GLOOX_API Client : public ClientBase
99  {
100  public:
101 
102  friend class NonSaslAuth;
103  friend class Parser;
104 
111  Client( const std::string& server );
112 
123  Client( const JID& jid, const std::string& password, int port = -1 );
124 
128  virtual ~Client();
129 
145  bool bindResource( const std::string& resource )
146  { return bindOperation( resource, true ); }
147 
157  bool selectResource( const std::string& resource );
158 
165  bool hasResourceBind() const { return ((m_streamFeatures & StreamFeatureUnbind) == StreamFeatureUnbind); }
166 
175  bool unbindResource( const std::string& resource )
176  { return bindOperation( resource, false ); }
177 
182  const std::string& resource() const { return m_jid.resource(); }
183 
188  int priority() const { return m_presence.priority(); }
189 
194  void setUsername( const std::string &username );
195 
200  void setResource( const std::string &resource ) { m_jid.setResource( resource ); }
201 
212  void setPresence( const JID& to, Presence::PresenceType pres, int priority,
213  const std::string& status = EmptyString );
214 
228  void setPresence( Presence::PresenceType pres, int priority,
229  const std::string& status = EmptyString );
230 
242  void setPresence() { sendPresence( m_presence ); }
243 
248  Presence& presence() { return m_presence; }
249 
255  GLOOX_DEPRECATED void setForceNonSasl( bool force = true ) { m_forceNonSasl = force; }
256 
262  void disableRoster();
263 
268  RosterManager* rosterManager() { return m_rosterManager; }
269 
273  void disconnect();
274 
282  bool login();
283 
284  protected:
288  void nonSaslLogin();
289 
290  private:
297  class ResourceBind : public StanzaExtension
298  {
299 
300  public:
307  ResourceBind( const std::string& resource, bool bind = true );
308 
313  ResourceBind( const Tag* tag );
314 
318  ~ResourceBind();
319 
324  const std::string& resource() const { return m_resource; }
325 
330  const JID& jid() const { return m_jid; }
331 
337  bool unbind() const { return !m_bind; }
338 
339  // reimplemented from StanzaExtension
340  virtual const std::string& filterString() const;
341 
342  // reimplemented from StanzaExtension
343  virtual StanzaExtension* newInstance( const Tag* tag ) const
344  {
345  return new ResourceBind( tag );
346  }
347 
348  // reimplemented from StanzaExtension
349  virtual Tag* tag() const;
350 
351  // reimplemented from StanzaExtension
352  virtual StanzaExtension* clone() const
353  {
354  return new ResourceBind( *this );
355  }
356 
357  private:
358  std::string m_resource;
359  JID m_jid;
360  bool m_bind;
361  };
362 
369  class SessionCreation : public StanzaExtension
370  {
371 
372  public:
376  SessionCreation() : StanzaExtension( ExtSessionCreation ) {}
377 
381  ~SessionCreation() {}
382 
383  // reimplemented from StanzaExtension
384  virtual const std::string& filterString() const { return EmptyString; }
385 
386  // reimplemented from StanzaExtension
387  virtual StanzaExtension* newInstance( const Tag* tag ) const
388  { (void)tag; return 0; }
389 
390  // reimplemented from StanzaExtension
391  virtual Tag* tag() const;
392 
393  // reimplemented from StanzaExtension
394  virtual StanzaExtension* clone() const
395  { return 0; }
396 
397  };
398 
399  virtual void handleStartNode() {}
400  virtual bool handleNormalNode( Tag* tag );
401  virtual void disconnect( ConnectionError reason );
402  virtual void handleIqIDForward( const IQ& iq, int context );
403 
404  int getStreamFeatures( Tag* tag );
405  int getSaslMechs( Tag* tag );
406  int getCompressionMethods( Tag* tag );
407  void processResourceBind( const IQ& iq );
408  void processCreateSession( const IQ& iq );
409  void sendPresence( Presence& pres );
410  void createSession();
411  void negotiateCompression( StreamFeature method );
412  void connected();
413  virtual void rosterFilled();
414  virtual void cleanup();
415  bool bindOperation( const std::string& resource, bool bind );
416 
417  void init();
418 
419  enum TrackContext
420  {
421  CtxResourceBind = 1000, // must be higher than the last element in ClientBase's TrackContext
422  CtxResourceUnbind,
423  CtxSessionEstablishment
424  };
425 
426  RosterManager* m_rosterManager;
427  NonSaslAuth* m_auth;
428 
429  Presence m_presence;
430 
431  bool m_resourceBound;
432  bool m_forceNonSasl;
433  bool m_manageRoster;
434 
435  int m_streamFeatures;
436 
437  };
438 
439 }
440 
441 #endif // CLIENT_H__