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 #include "jabberoo-component.hh"
00026 #include <sha.h>
00027
00028 using namespace jabberoo;
00029
00030 ComponentSession::ComponentSession() :
00031 judo::ElementStream(this), _connState(csNotConnected)
00032 { }
00033
00034 ComponentSession::~ComponentSession()
00035 {
00036 if (_connState != csNotConnected)
00037 {
00038 disconnect();
00039 }
00040
00041 while (!_XPaths.empty())
00042 {
00043 judo::XPath::Query* query = _XPaths.front();
00044 delete query;
00045 _XPaths.pop_front();
00046 }
00047 }
00048
00049 void ComponentSession::connect(const std::string& server, const std::string& componentID,
00050 const std::string& password)
00051 {
00052 _componentID = componentID;
00053 _server = server;
00054 _password = password;
00055
00056 assert(_connState == csNotConnected);
00057
00058 reset();
00059
00060 *this << "<stream::stream to='" << _componentID.c_str() << "' xmlns='jabber:component:accept' xmlns:stream='http://etherx.jabber.org/streams'>";
00061
00062 _connState = csAwaitingAuth;
00063 }
00064
00065 void ComponentSession::disconnect()
00066 {
00067 assert(_connState != csNotConnected);
00068
00069 *this << "</stream:stream>";
00070
00071 _connState = csNotConnected;
00072 }
00073
00074 void ComponentSession::push(const char* data, int datasz)
00075 {
00076 try
00077 {
00078 judo::ElementStream::push(data, datasz);
00079 }
00080 catch (const judo::ElementStream::exception::ParserError& error)
00081 {
00082 disconnect();
00083 }
00084 }
00085
00086 judo::XPath::Query* ComponentSession::registerXPath(const std::string& query,
00087 ElementCallbackFunc f)
00088 {
00089 judo::XPath::Query* xpq = new judo::XPath::Query(query);
00090 _XPaths.push_front(xpq);
00091 _XPCallbacks.insert(std::make_pair(xpq, f));
00092
00093 return xpq;
00094 }
00095
00096 void ComponentSession::unregisterXPath(judo::XPath::Query* id)
00097 {
00098 std::remove(_XPaths.begin(), _XPaths.end(), id);
00099 _XPCallbacks.erase(id);
00100 delete id;
00101 }
00102
00103 void ComponentSession::onDocumentStart(judo::Element* t)
00104 {
00105
00106 _SessionID = t->getAttrib("id");
00107
00108 std::string full = _SessionID + _password;
00109 *this << "<handshake>" << shahash(full.c_str()) << "</handshake>";
00110 }
00111
00112 void ComponentSession::onElement(judo::Element* t)
00113 {
00114 if (_connState != csConnected && t->getName() == "handshake")
00115 {
00116 _connState = csConnected;
00117 delete t;
00118 evtConnected();
00119 return;
00120 }
00121
00122 judo::Element& tref = *t;
00123
00124 typedef std::list<judo::XPath::Query*>::iterator IT;
00125 for (IT it = _XPaths.begin(); it != _XPaths.end(); it++)
00126 {
00127 if ((*it)->check(tref))
00128 {
00129 _XPCallbacks[(*it)](tref);
00130 }
00131 }
00132
00133 delete t;
00134 }
00135
00136 void ComponentSession::onCDATA(judo::CDATA* c)
00137 {
00138
00139
00140
00141 delete c;
00142 }
00143
00144 void ComponentSession::onDocumentEnd()
00145 {
00146 disconnect();
00147 }
00148