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 #include "jabberoox.hh"
00031 #include <judo.hpp>
00032 #include <iostream>
00033
00034 namespace jabberoo
00035 {
00036
00037 const bool Filter::Action::ParamReq[5] = { true, true, true, false, false };
00038 const bool Filter::Condition::ParamReq[7] = { false, true, true, true, true, true, true };
00039
00040
00041
00042
00043
00044
00045 std::string Filter::Action::toXML() const
00046 {
00047 std::string name;
00048 switch (_value)
00049 {
00050 case SetType:
00051 name = "settype"; break;
00052 case ForwardTo:
00053 name = "forward"; break;
00054 case ReplyWith:
00055 name = "reply"; break;
00056 case StoreOffline:
00057 name = "offline"; break;
00058 case Continue:
00059 name = "continue"; break;
00060 default:
00061 return "";
00062 }
00063 if (_param.empty())
00064 return "<" + name + "/>";
00065 else
00066 return "<" + name + ">" + judo::escape(_param) + "</" + name + ">";
00067 }
00068
00069 Filter::Action::Value Filter::Action::translate(const std::string& s)
00070 {
00071 if (s == "settype")
00072 return Action::SetType;
00073 else if (s == "forward")
00074 return Action::ForwardTo;
00075 else if (s == "reply")
00076 return Action::ReplyWith;
00077 else if (s == "offline")
00078 return Action::StoreOffline;
00079 else if (s == "continue")
00080 return Action::Continue;
00081 else
00082 return Action::Invalid;
00083 }
00084
00085
00086
00087
00088
00089
00090 std::string Filter::Condition::toXML() const
00091 {
00092 std::string name;
00093 switch(_value)
00094 {
00095 case Unavailable:
00096 name = "unavailable"; break;
00097 case From:
00098 name = "from"; break;
00099 case MyResourceEquals:
00100 name = "resource"; break;
00101 case SubjectEquals:
00102 name = "subject"; break;
00103 case BodyEquals:
00104 name = "body"; break;
00105 case ShowEquals:
00106 name = "show"; break;
00107 case TypeEquals:
00108 name = "type"; break;
00109 default:
00110 return "";
00111 }
00112 if (_param.empty())
00113 return "<" + name + "/>";
00114 else
00115 return "<" + name + ">" + judo::escape(_param) + "</" + name + ">";
00116 }
00117
00118 Filter::Condition::Value Filter::Condition::translate(const std::string& s)
00119 {
00120 if (s == "unavailable")
00121 return Condition::Unavailable;
00122 else if (s == "from")
00123 return Condition::From;
00124 else if (s == "resource")
00125 return Condition::MyResourceEquals;
00126 else if (s == "subject")
00127 return Condition::SubjectEquals;
00128 else if (s == "body")
00129 return Condition::BodyEquals;
00130 else if (s == "show")
00131 return Condition::ShowEquals;
00132 else if (s == "type")
00133 return Condition::TypeEquals;
00134 else
00135 return Condition::Invalid;
00136 }
00137
00138
00139
00140
00141
00142
00143 Filter::Filter(const std::string& name)
00144 : _name(name)
00145 {}
00146
00147 void dumpFilter(Filter& f)
00148 {
00149 std::cerr << "Filter(" << f.Name() <<") @ " << &f << "\tActions: " << f.Actions().size() << "\tConditions: " << f.Conditions().size() << std::endl;
00150 }
00151
00152 Filter::Filter(const Filter& f)
00153 : _name(f._name), _actions(f._actions), _conditions(f._conditions)
00154 {}
00155
00156 Filter::Filter(const judo::Element& rule)
00157 {
00158
00159 _name = rule.getAttrib("name");
00160 if (_name == "")
00161 _name = "Default rule";
00162
00163
00164 judo::Element::const_iterator it = rule.begin();
00165 for (; it != rule.end(); it++)
00166 {
00167 if ((*it)->getType() != judo::Node::ntElement)
00168 continue;
00169
00170 judo::Element& t = *static_cast<judo::Element*>(*it);
00171
00172
00173 Action::Value aval = Action::translate(t.getName());
00174 if (aval != Action::Invalid)
00175 {
00176 _actions.push_back(Action(aval, t.getCDATA()));
00177 }
00178
00179
00180 Condition::Value cval = Condition::translate(t.getName());
00181 if (cval != Condition::Invalid)
00182 {
00183 _conditions.push_back(Condition(cval, t.getCDATA()));
00184 }
00185 }
00186 }
00187
00188
00189 std::string Filter::toXML() const
00190 {
00191 std::string result = "<rule name='" + _name;
00192 if (!_actions.empty() || !_conditions.empty())
00193 {
00194 result += "'>";
00195 for (ActionList::const_iterator it = _actions.begin(); it != _actions.end(); it++)
00196 result += it->toXML();
00197 for (ConditionList::const_iterator it = _conditions.begin(); it != _conditions.end(); it++)
00198 result += it->toXML();
00199 return result + "</rule>";
00200 }
00201 else
00202 return result + "'/>";
00203 }
00204
00205
00206
00207
00208
00209
00210 FilterList::FilterList(const judo::Element& query)
00211 {
00212
00213 if (!query.cmpAttrib("xmlns", "jabber:iq:filter"))
00214 return;
00215
00216
00217 judo::Element::const_iterator it = query.begin();
00218 for (; it != query.end(); it++)
00219 {
00220 if ((*it)->getType() != judo::Node::ntElement)
00221 continue;
00222
00223 judo::Element& t = *static_cast<judo::Element*>(*it);
00224
00225
00226 if (t.getName() == "rule")
00227
00228 push_back(Filter(t));
00229 }
00230 }
00231
00232 std::string FilterList::toXML() const
00233 {
00234 std::string result = "<query xmlns='jabber:iq:filter'";
00235 if (!empty())
00236 {
00237 result += ">";
00238 for (const_iterator it = begin(); it != end(); it++)
00239 result += it->toXML();
00240 return result + "</query>";
00241 }
00242 else
00243 return result + "/>";
00244 }
00245
00246 }