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 #include "jabberoox.hh"
00032 #include <judo.hpp>
00033 #include <session.hh>
00034 #include <sigc++/object_slot.h>
00035 #include <sigc++/slot.h>
00036
00037 namespace jabberoo {
00038
00039
00040
00041
00042
00043
00044 Agent::Agent(const Agent& a)
00045 : _session(a._session), _jid(a._jid), _name(a._name),
00046 _desc(a._desc), _service(a._service), _transport(a._transport),
00047 _registerable(a._registerable), _searchable(a._searchable),
00048 _subagents(a._subagents)
00049 {
00050 if (_name.empty())
00051 _name = "No name available (" + a._jid + ")";
00052 }
00053
00054 Agent::Agent(const std::string& jid, const judo::Element& baseElement, Session& s)
00055 : _jid(jid), _session(s)
00056 {
00057 _name = baseElement.getChildCData("name");
00058 _desc = baseElement.getChildCData("description");
00059 _service = baseElement.getChildCData("service");
00060 _transport = baseElement.getChildCData("transport");
00061 _registerable = (baseElement.findElement("register") != NULL);
00062 _searchable = (baseElement.findElement("search") != NULL);
00063 _subagents = (baseElement.findElement("agents") != NULL);
00064 _gc_capable = (baseElement.findElement("groupchat") != NULL);
00065 _fetchrequested = false;
00066 }
00067
00068 Agent::~Agent()
00069 {}
00070
00071 void Agent::requestRegister(ElementCallbackFunc f)
00072 {
00073 _session.queryNamespace("jabber:iq:register", f, _jid);
00074 }
00075
00076 void Agent::requestSearch(ElementCallbackFunc f)
00077 {
00078 _session.queryNamespace("jabber:iq:search", f, _jid);
00079 }
00080
00081 void Agent::fetch()
00082 {
00083 if ( _subagents && empty() && !_fetchrequested )
00084 {
00085
00086 clear();
00087
00088 _fetchrequested = true;
00089
00090 _session.queryNamespace("jabber:iq:agents", SigC::slot(*this, &Agent::on_fetch), _jid);
00091 }
00092 }
00093
00094 void Agent::on_fetch(const judo::Element& iq)
00095 {
00096
00097 _fetchrequested = false;
00098
00099
00100
00101
00102 const judo::Element* query = iq.findElement("query");
00103 if (query == NULL)
00104 return;
00105
00106 if (iq.cmpAttrib("type", "result"))
00107 {
00108
00109 judo::Element::const_iterator it = query->begin();
00110 for (; it != query->end(); it++)
00111 {
00112 if ((*it)->getType() != judo::Node::ntElement)
00113 continue;
00114
00115 judo::Element& t = *static_cast<judo::Element*>(*it);
00116
00117
00118 if (t.getName() == "agent")
00119 push_back(agentPtr(new Agent(t.getAttrib("jid"), t, _session)) );
00120 }
00121
00122
00123 evtFetchComplete(true);
00124 }
00125 else
00126 {
00127
00128 evtFetchComplete(false);
00129 }
00130
00131
00132 evtFetchComplete.clear();
00133 }
00134
00135 AgentList::AgentList(const judo::Element& query, Session& s)
00136 {
00137 load(query, s);
00138 }
00139
00140 void AgentList::load(const judo::Element& query, Session& s)
00141 {
00142
00143 clear();
00144
00145 if (!query.cmpAttrib("xmlns", "jabber:iq:agents"))
00146 return;
00147
00148
00149 judo::Element::const_iterator it = query.begin();
00150 for (; it != query.end(); it++)
00151 {
00152 if ((*it)->getType() != judo::Node::ntElement)
00153 continue;
00154
00155 judo::Element& t = *static_cast<judo::Element*>(*it);
00156
00157
00158 if (t.getName() == "agent")
00159
00160 push_back(Agent(t.getAttrib("jid"), t, s));
00161 }
00162 }
00163
00164 }