My Project
NPLJabberClient.h
1 #pragma once
2 #include "IParaWebService.h"
3 
4 #include "JC_Options.h"
5 #include "JabberEventBinding.h"
6 #include "ParaRingBuffer.h"
7 
8 #include "util/mutex.h"
9 #include <boost/bimap.hpp>
10 #include <boost/thread.hpp>
11 #include <boost/asio.hpp>
12 
13 // in gloox src folder.
14 #include <gloox.h>
15 #include <client.h>
16 #include <message.h>
17 #include <messagesessionhandler.h>
18 #include <messageeventhandler.h>
19 #include <messageeventfilter.h>
20 #include <chatstatehandler.h>
21 #include <chatstatefilter.h>
22 #include <connectionlistener.h>
23 #include <disco.h>
24 #include <lastactivity.h>
25 #include <loghandler.h>
26 #include <logsink.h>
27 #include <connectiontcpclient.h>
28 #include <connectionsocks5proxy.h>
29 #include <connectionhttpproxy.h>
30 #include <messagehandler.h>
31 #include <rostermanager.h>
32 #include <registration.h>
33 
34 using namespace gloox;
35 
36 namespace ParaEngine
37 {
38  using namespace std;
39 
42  class CNPLJabberClient : public MessageSessionHandler, ConnectionListener, LogHandler, RegistrationHandler,
43  MessageEventHandler, MessageHandler, ChatStateHandler, RosterListener, INPLJabberClient/*, MUCRoomHandler*/
44  {
45  public:
46  typedef boost::bimap<int, std::string> StringMap_Type;
47 
52  CNPLJabberClient(boost::asio::io_service& io_service, const char* sJID);
53  virtual ~CNPLJabberClient(void);
54 
55  public:
60  int ProcessMessage(int timeout = 10);
61 
62  //
63  // handlers
64  //
65  virtual void handleLog( LogLevel level, LogArea area, const std::string& message );
66 
67  virtual void onConnect();
68  virtual void onDisconnect( ConnectionError e );
69  virtual bool onTLSConnect( const CertInfo& info );
70  virtual void handleMessage( const gloox::Message & stanza, MessageSession * /*session*/ );
71  virtual void handleMessageEvent( const JID& from, MessageEventType event );
72  virtual void handleChatState( const JID& from, ChatStateType state );
73  virtual void handleMessageSession( MessageSession *session );
74 
75 
76  virtual void onResourceBindError( ResourceBindError error );
77  virtual void onSessionCreateError( SessionCreateError error );
78  virtual void handleItemSubscribed( const JID& jid );
79  virtual void handleItemAdded( const JID& jid );
80  virtual void handleItemUnsubscribed( const JID& jid );
81  virtual void handleItemRemoved( const JID& jid );
82  virtual void handleItemUpdated( const JID& jid );
83  virtual void handleRoster( const Roster& roster );
84  virtual void handleRosterError( const gloox::IQ & /*stanza*/ );
85  virtual void handleRosterPresence( const RosterItem& item, const std::string& resource,
86  gloox::Presence::PresenceType presence, const std::string& /*msg*/ );
87  virtual void handleSelfPresence( const RosterItem& item, const std::string& resource,
88  gloox::Presence::PresenceType presence, const std::string& /*msg*/ );
89  virtual bool handleSubscriptionRequest( const JID& jid, const std::string& /*msg*/ );
90  virtual bool handleUnsubscriptionRequest( const JID& jid, const std::string& /*msg*/ );
91  virtual void handleNonrosterPresence( const gloox::Presence & );
92 
93  virtual void handleRegistrationFields( const JID& /*from*/, int fields, std::string instructions );
94  virtual void handleRegistrationResult( const JID& /*from*/, RegistrationResult result );
95  virtual void handleAlreadyRegistered(const JID& /*from*/ );
96  virtual void handleDataForm( const JID& /*from*/, const DataForm& /*form*/ );
97  virtual void handleOOB( const JID& /*from*/, const OOB& oob );
98  public:
99  //
100  // exposed functions
101  //
102 
109  bool Connect();
110 
112  void Close();
113 
120  static void AddStringMap(int nID, const char* sString);
121 
123  static void ClearStringMap();
124 
130  bool WriteNPLMessage(const char* to, const char* neuronfile, const char* sCode);
131 
133  bool WriteRawString(const char* rawstring);
134 
142 
143  bool SendMessage(int t, const char* to, const char* body);
144 
150  bool SendMessage(const char* to, const char* body);
151 
153  bool Login();
154 
160 
165 
169 
173 
177 
181 
185 
189 
193 
198 
206 
207  bool SetPresence(int t,const char* status,const char* show,int priority);
208 
211  {
215  MsgType_normal = -1,
231  MsgType_headline
232  };
233 
243  const char* GetRoster();
244 
253  bool Subscribe(const char* jid, const char* name, const char* groups, const char* msg);
254 
262  void Unsubscribe(const char* to, const char* msg);
263 
264 
271  virtual void RegisterCreateAccount(const char* username, const char* password);
276  virtual void RegisterChangePassword(const char* password);
281  virtual void RegisterRemoveAccount();
282 
283 
288  virtual StatisticsJabber GetStatistics();
289 
291  // following are obsoleted methods
293 
295  void RemoveRosterItem(const char* to, const char* msg);
296 
302  bool AllowSubscription(const char* to, bool bAllow);
303 
304 
308  void GetAgents();
309 
315  bool Register(const char* jid);
316 
317 
328  const char* GetRosterItemDetail(const char* jid);
329 
334  const char* GetRosterGroups();
335 
340  const char* GetRosterItems();
341 
342 
344  //
345  // event functions
346  //
348 
354  void AddEventListener(int nListenerType, const char* callbackScript);
355  void AddEventListener1(const char* sListenerType, const char* callbackScript);
356  void RemoveEventListener(int nListenerType, const char* callbackScript);
357  void RemoveEventListener1(const char* sListenerType, const char* callbackScript);
358  void ClearEventListener(int nListenerType);
359  void ClearEventListener1(const char* sListenerType);
360  void ResetAllEventListeners();
361 
363  //
364  // misc
365  //
367 
372  const std::string GetID();
373 
375  const string& GetStatus() const;
376 
383  int GetConnectionStatus() const;
384 
399  int GetAuthError() const;
400 
401 
403  void WhitespacePing ();
404 
406  void XmppPing (const string & sTo);
407 
409  const std::string & GetXmlLang () const ;
410  void SetXmlLang (const std::string &xmllang);
411 
412 
424  void SetClientCert (const std::string &clientKey, const std::string &clientCerts);
425 
426  public:
427  //
428  // properties
429  //
430 
432  const char* GetUser();
433  void SetUser(const char* sValue);
434 
436  int GetPriority();
437  void SetPriority(int nValue){m_Options.PRIORITY = nValue;}
438 
442  const char* GetPassword();
443  void SetPassword(const char* sValue);
444 
445 
447  bool GetAutoLogin() {return m_Options.AUTO_LOGIN;}
448  void SetAutoLogin(bool nValue){m_Options.AUTO_LOGIN = nValue;}
449 
450 
452  bool GetAutoRoster(){return m_Options.AUTO_ROSTER;}
453  void SetAutoRoster(bool nValue){m_Options.AUTO_ROSTER = nValue;}
454 
456  bool GetAutoIQErrors() {return m_Options.AUTO_IQ_ERRORS;}
457  void SetAutoIQErrors(bool nValue){m_Options.AUTO_IQ_ERRORS = nValue;}
458 
460  bool GetAutoPresence(){return m_Options.AUTO_PRESENCE;}
461  void SetAutoPresence(bool nValue){m_Options.AUTO_PRESENCE = nValue;}
462 
464  const char* GetResource();
465  void SetResource(const char* sValue){m_Options.RESOURCE = sValue;}
466 
468  bool GetIsAuthenticated();
469  void SetIsAuthenticated(bool bValue);
470 
472  const char* GetServer();
473  void SetServer(const char* Server);
474 
479  const char* GetNetworkHost(){return m_Options.NETWORK_HOST.c_str();}
480  void SetNetworkHost(const char* sValue){m_Options.NETWORK_HOST = sValue;}
481 
483  int GetPort();
484  void SetPort(int nValue);
485 
487  bool GetPlaintextAuth(){return m_Options.PLAINTEXT;}
488  void SetPlaintextAuth(bool bValue){m_Options.PLAINTEXT = bValue;}
489 
491  bool IsSSLon(){return m_Options.SSL;}
492 
494  bool IsCompressionOn(){return m_Options.AUTO_COMPRESS;}
495 
497  bool GetSSL(){return m_Options.SSL;}
498  void SetSSL(bool bValue){m_Options.SSL = bValue;}
499 
501  bool GetAutoStartTLS(){return m_Options.AUTO_TLS;}
502  void SetAutoStartTLS(bool bValue);
503 
505  bool GetAutoStartCompression();
506  void SetAutoStartCompression(bool bValue);
507 
509  float GetKeepAlive(){return m_fKeepAlive; }
510  void SetKeepAlive(float KeepAlive){m_fKeepAlive = KeepAlive;}
511 
513  float GetAutoReconnect(){return m_fAutoReconnect;}
514  void SetAutoReconnect(float AutoReconnect){m_fAutoReconnect = AutoReconnect;}
515 
517  const char* GetServerVersion() { return GLOOX_VERSION.c_str();}
518 
520  bool GetRequiresSASL(){return m_Options.SASL;}
521  void SetRequiresSASL(bool bValue);
522 
523  protected:
533  virtual bool AddEvent(int nEventType, const char* precode=NULL, const char* postcode=NULL);
543  virtual bool CallEvent(int nEventType, const char* precode=NULL, const char* postcode=NULL);
544 
548  const char* GetStringByID(int nID);
552  int GetIDByString(const char* sString);
553 
557  const string& GetJIDKey() {return m_sThisJIDKey;}
559  void SetJIDKey(const string& sJIDKey) {m_sThisJIDKey = sJIDKey;}
560 
561  protected:
562  Client *m_client;
563  MessageSession *m_session;
564  MessageEventFilter *m_messageEventFilter;
565  ChatStateFilter *m_chatStateFilter;
566  Registration *m_reg;
567 
568  JCOptions m_Options;
569  bool m_bIsAuthenticated;
570  float m_fKeepAlive;
571  float m_fAutoReconnect;
572  CJabberEventBinding m_eventbinding;
573 
575  string m_sUsername;
576  string m_sPassword;
578  string m_sStatus;
586  static StringMap_Type g_mapStringMap;
587 
589  boost::asio::io_service * m_pIOService;
590 
591  ParaEngine::mutex m_mutex;
592  };
593 
594 }
MessageType
message type
Definition: NPLJabberClient.h:210
void SetResource(const char *sValue)
The connecting resource.
Definition: NPLJabberClient.h:465
bool IsCompressionOn()
Is the current connection XEP-138 compressed?
Definition: NPLJabberClient.h:494
different physics engine has different winding order.
Definition: EventBinding.h:32
float GetKeepAlive()
Time, in seconds, between keep-alive spaces.
Definition: NPLJabberClient.h:509
bool GetAutoIQErrors()
Automatically send back 501/feature-not-implemented to IQs that have not been handled.
Definition: NPLJabberClient.h:456
Jabber event that could be bound with NPL script.
Definition: JabberEventBinding.h:104
void SetAutoLogin(bool nValue)
Automatically log in on connection.
Definition: NPLJabberClient.h:448
static StringMap_Type g_mapStringMap
we will automatically encode NPL filename string if it is in this string map.
Definition: NPLJabberClient.h:586
void SetSSL(bool bValue)
Do SSL3/TLS1 on startup.
Definition: NPLJabberClient.h:498
const string & GetJIDKey()
this is the unique key of this JC instance.
Definition: NPLJabberClient.h:557
NPL jabber client.
Definition: NPLJabberClient.h:42
string m_sStatus
presence status
Definition: NPLJabberClient.h:578
void SetAutoPresence(bool nValue)
Automatically send presence on connection.
Definition: NPLJabberClient.h:461
NPL interface of a Jabber-XMPP client proxy.
Definition: IParaWebService.h:108
bool IsSSLon()
Is the current connection SSL/TLS protected?
Definition: NPLJabberClient.h:491
bool GetAutoRoster()
Automatically retrieve roster on connection.
Definition: NPLJabberClient.h:452
const char * GetNetworkHost()
The address to use on the "to" attribute of the stream:stream.
Definition: NPLJabberClient.h:479
void SetNetworkHost(const char *sValue)
The address to use on the "to" attribute of the stream:stream.
Definition: NPLJabberClient.h:480
float GetAutoReconnect()
Seconds before automatically reconnecting if the connection drops.
Definition: NPLJabberClient.h:513
void SetKeepAlive(float KeepAlive)
Time, in seconds, between keep-alive spaces.
Definition: NPLJabberClient.h:510
Chat (one-to-one) message
Definition: NPLJabberClient.h:223
const char * GetServerVersion()
The version string returned in the server&#39;s open stream element.
Definition: NPLJabberClient.h:517
void SetAutoRoster(bool nValue)
Automatically retrieve roster on connection.
Definition: NPLJabberClient.h:453
void SetAutoReconnect(float AutoReconnect)
Seconds before automatically reconnecting if the connection drops.
Definition: NPLJabberClient.h:514
bool GetAutoPresence()
Automatically send presence on connection.
Definition: NPLJabberClient.h:460
A structure describing the current jabber connection statistics.
Definition: IParaWebService.h:17
void SetJIDKey(const string &sJIDKey)
never call this function, unless you know why.
Definition: NPLJabberClient.h:559
Definition: error.hpp:46
void SetAutoIQErrors(bool nValue)
Automatically send back 501/feature-not-implemented to IQs that have not been handled.
Definition: NPLJabberClient.h:457
bool GetSSL()
Do SSL3/TLS1 on startup.
Definition: NPLJabberClient.h:497
string m_sUsername
only used for creating a new user account.
Definition: NPLJabberClient.h:575
bool GetPlaintextAuth()
Allow plaintext authentication? default to false.
Definition: NPLJabberClient.h:487
bool GetAutoLogin()
Automatically log in on connection.
Definition: NPLJabberClient.h:447
void SetPriority(int nValue)
Priority for this connection.
Definition: NPLJabberClient.h:437
boost::asio::io_service * m_pIOService
dispatcher IO service object for the connection socket.
Definition: NPLJabberClient.h:589
Error message
Definition: NPLJabberClient.h:219
cross platform mutex
Definition: mutex.h:95
string m_sThisJIDKey
this is the unique key of this JC instance.
Definition: NPLJabberClient.h:582
Groupchat
Definition: NPLJabberClient.h:227
bool GetAutoStartTLS()
Allow Start-TLS on connection, if the server supports it.
Definition: NPLJabberClient.h:501
Option names.
Definition: JC_Options.h:11
void SetPlaintextAuth(bool bValue)
Allow plaintext authentication? default to false.
Definition: NPLJabberClient.h:488
bool GetRequiresSASL()
Is SASL required? This will default to true in the future.
Definition: NPLJabberClient.h:520