00001
00002
00003
00004
00005
00006
00007 #pragma once
00008
00009 #include <Swiften/Queries/IQHandler.h>
00010 #include <Swiften/Queries/IQRouter.h>
00011 #include <Swiften/Elements/ErrorPayload.h>
00012
00013 namespace Swift {
00023 template<typename PAYLOAD_TYPE>
00024 class Responder : public IQHandler {
00025 public:
00026 Responder(IQRouter* router) : router_(router) {
00027 }
00028
00029 ~Responder() {
00030 }
00031
00039 void start() {
00040 router_->addHandler(this);
00041 }
00042
00050 void stop() {
00051 router_->removeHandler(this);
00052 }
00053
00054 protected:
00060 virtual bool handleGetRequest(const JID& from, const JID& to, const std::string& id, boost::shared_ptr<PAYLOAD_TYPE> payload) = 0;
00061
00067 virtual bool handleSetRequest(const JID& from, const JID& to, const std::string& id, boost::shared_ptr<PAYLOAD_TYPE> payload) = 0;
00068
00072 void sendResponse(const JID& to, const std::string& id, boost::shared_ptr<PAYLOAD_TYPE> payload) {
00073 router_->sendIQ(IQ::createResult(to, id, payload));
00074 }
00075
00079 void sendResponse(const JID& to, const JID& from, const std::string& id, boost::shared_ptr<PAYLOAD_TYPE> payload) {
00080 router_->sendIQ(IQ::createResult(to, from, id, payload));
00081 }
00082
00086 void sendError(const JID& to, const std::string& id, ErrorPayload::Condition condition, ErrorPayload::Type type, Payload::ref payload = Payload::ref()) {
00087 router_->sendIQ(IQ::createError(to, id, condition, type, payload));
00088 }
00089
00093 void sendError(const JID& to, const JID& from, const std::string& id, ErrorPayload::Condition condition, ErrorPayload::Type type, Payload::ref payload = Payload::ref()) {
00094 router_->sendIQ(IQ::createError(to, from, id, condition, type, payload));
00095 }
00096
00097 IQRouter* getIQRouter() const {
00098 return router_;
00099 }
00100
00101 private:
00102 virtual bool handleIQ(boost::shared_ptr<IQ> iq) {
00103 if (iq->getType() == IQ::Set || iq->getType() == IQ::Get) {
00104 boost::shared_ptr<PAYLOAD_TYPE> payload(iq->getPayload<PAYLOAD_TYPE>());
00105 if (payload) {
00106 bool result;
00107 if (iq->getType() == IQ::Set) {
00108 result = handleSetRequest(iq->getFrom(), iq->getTo(), iq->getID(), payload);
00109 }
00110 else {
00111 result = handleGetRequest(iq->getFrom(), iq->getTo(), iq->getID(), payload);
00112 }
00113 if (!result) {
00114 router_->sendIQ(IQ::createError(iq->getFrom(), iq->getID(), ErrorPayload::NotAllowed, ErrorPayload::Cancel));
00115 }
00116 return true;
00117 }
00118 }
00119 return false;
00120 }
00121
00122 private:
00123 IQRouter* router_;
00124 };
00125 }