00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef SESSION_HH
00034 #define SESSION_HH
00035
00036 #include <judo.hpp>
00037 #include <sigc++/object.h>
00038 #include <sigc++/signal.h>
00039 #include <sigc++/slot.h>
00040
00041 #include <discoDB.hh>
00042 #include <roster.hh>
00043 #include <presenceDB.hh>
00044
00045 namespace jabberoo {
00046
00047 typedef SigC::Slot1<void, const judo::Element&> ElementCallbackFunc;
00048
00053 class Session :
00054 public judo::ElementStreamEventListener,
00055 public judo::ElementStream,
00056 public SigC::Object
00057 {
00058 public:
00062 enum AuthType
00063 {
00064 atPlaintextAuth,
00065 atDigestAuth,
00066 atAutoAuth,
00067 at0kAuth
00068 };
00069
00073 enum ConnectionState
00074 {
00075 csNotConnected,
00076 csCreateUser,
00077 csAuthReq,
00078 csAwaitingAuth,
00079 csConnected
00080 };
00081
00082
00089 Session();
00094 virtual ~Session();
00095
00096
00101 Session& operator>>(const char* buffer) { push(buffer, strlen(buffer)); return *this; }
00106 Session& operator<<(const Packet& p);
00111 Session& operator<<(const char* buffer) { evtTransmitXML(buffer); return *this;}
00112 public:
00113
00129 void connect(const std::string& server, AuthType atype,
00130 const std::string& username, const std::string& resource, const std::string& password,
00131 bool createuser = false, bool should_auth = true);
00132
00137 bool disconnect();
00138
00143 ConnectionState getConnState() { return _ConnState; }
00144
00145
00152 virtual void push(const char* data, int datasz);
00159 void registerIQ(const std::string& id, ElementCallbackFunc f);
00160
00169 judo::XPath::Query* registerXPath(const std::string& query,
00170 ElementCallbackFunc f, bool incoming=true);
00171
00177 void unregisterXPath(judo::XPath::Query* id, bool incoming=true);
00178
00186 void queryNamespace(const std::string& nspace, ElementCallbackFunc f, const std::string& to = "");
00187
00188
00194 const Roster& roster() const;
00200 Roster& roster();
00201
00207 const DiscoDB& discoDB() const;
00208
00214 DiscoDB& discoDB();
00215
00221 const PresenceDB& presenceDB() const;
00227 PresenceDB& presenceDB();
00228
00229
00234 AuthType getAuthType() const;
00239 const std::string& getUserName() const;
00240
00241
00247 std::string getNextID();
00252 std::string getDigest();
00253
00254 public:
00255
00265 SigC::Signal2<void, int, const std::string&, SigC::Marshal<void> > evtXMLParserError;
00271 SigC::Signal1<void, const char*, SigC::Marshal<void> > evtTransmitXML;
00276 SigC::Signal1<void, const char*, SigC::Marshal<void> > evtRecvXML;
00281 SigC::Signal1<void, const Packet&, SigC::Marshal<void> > evtTransmitPacket;
00282
00288 SigC::Signal1<void, const judo::Element&, SigC::Marshal<void> > evtConnected;
00294 SigC::Signal0<void, SigC::Marshal<void> > evtDisconnected;
00295
00301 SigC::Signal1<void, const Message&, SigC::Marshal<void> > evtMessage;
00307 SigC::Signal1<void, const judo::Element&, SigC::Marshal<void> > evtIQ;
00314 SigC::Signal1<void, const Presence&, SigC::Marshal<void> > evtMyPresence;
00321 SigC::Signal2<void, const Presence&, Presence::Type, SigC::Marshal<void> > evtPresence;
00327 SigC::Signal1<void, const Presence&, SigC::Marshal<void> > evtPresenceRequest;
00328
00334 SigC::Signal1<void, const judo::Element&, SigC::Marshal<void> > evtUnknownPacket;
00340 SigC::Signal2<void, int, const char*, SigC::Marshal<void> > evtAuthError;
00349 SigC::Signal0<void, SigC::Marshal<void> > evtOnRoster;
00356 SigC::Signal3<void, std::string&, std::string&, std::string&, SigC::Marshal<void> > evtOnVersion;
00361 SigC::Signal1<void, std::string&, SigC::Marshal<void> > evtOnLast;
00362
00368 SigC::Signal2<void, std::string&, std::string&, SigC::Marshal<void> > evtOnTime;
00369 protected:
00370
00371 void authenticate();
00372 void OnAuthTypeReceived(const judo::Element& t);
00373 void sendLogin(Session::AuthType atype, const judo::Element* squery);
00374
00375
00376 virtual void onDocumentStart(judo::Element* t);
00377 virtual void onElement(judo::Element* t);
00378 virtual void onCDATA(judo::CDATA* c);
00379 virtual void onDocumentEnd();
00380
00381
00382 void handleMessage(judo::Element& t);
00383 void handlePresence(judo::Element& t);
00384 void handleIQ(judo::Element& t);
00385
00386 private:
00387
00388 std::string _ServerID;
00389 std::string _Username;
00390 std::string _Password;
00391 std::string _Resource;
00392 std::string _SessionID;
00393 long _ID;
00394
00395 AuthType _AuthType;
00396 ConnectionState _ConnState;
00397
00398 judo::Element* _StreamElement;
00399
00400 bool _StreamStart;
00401
00402 bool _Authenticate;
00403
00404 std::multimap<std::string, ElementCallbackFunc> _Callbacks;
00405 typedef std::map<judo::XPath::Query*, ElementCallbackFunc> XPCBMap;
00406 typedef std::list<judo::XPath::Query*> XPQueryList;
00407 XPCBMap _incoming_XPaths;
00408 XPQueryList _incoming_queries;
00409 XPCBMap _outgoing_XPaths;
00410 XPQueryList _outgoing_queries;
00411
00412 Roster _Roster;
00413 DiscoDB _DDB;
00414 PresenceDB _PDB;
00415
00416
00417 void IQHandler_Auth(const judo::Element& t);
00418 void IQHandler_CreateUser(const judo::Element& t);
00419 };
00420 }
00421
00422 #endif // SESSION_HH