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