Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members  

jabberoox-filter.cc

00001 /* jabberoox-filter.cc
00002  * Jabber filter support
00003  *
00004  * Original Code Copyright (C) 1999-2001 Dave Smith (dave@jabber.org)
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  * Contributor(s): Julian Missig
00021  *
00022  * This Original Code has been modified by IBM Corporation. Modifications 
00023  * made by IBM described herein are Copyright (c) International Business 
00024  * Machines Corporation, 2002.
00025  *
00026  * Date             Modified by     Description of modification
00027  * 01/20/2002       IBM Corp.       Updated to libjudo 1.1.1
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 // Filter::Action methods
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 // Filter::Condition methods
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 // Filter methods
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      // Retrieve the rule name
00156      _name = rule.getAttrib("name");
00157      if (_name == "")
00158           _name = "Default rule";
00159 
00160      // Walk the rule's child tags
00161      Element::const_iterator it = rule.begin();
00162      for (; it != rule.end(); it++)
00163      {
00164           if ((*it)->getType() != Node::ntElement)
00165                continue;
00166           // Cast the child element into a tag..
00167           Element& t = *static_cast<Element*>(*it);
00168           
00169           // See if this is an action
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           // See if this is a condition
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 // FilterList methods
00205 //
00206 // ---------------------------------------------------------
00207 FilterList::FilterList(const Element& query)
00208 {
00209      // Make sure this is the proper namespace
00210      if (!query.cmpAttrib("xmlns", "jabber:iq:filter"))
00211           return;
00212 
00213      // Walk each of the child <rule> tags and build rule objects
00214      Element::const_iterator it = query.begin();
00215      for (; it != query.end(); it++)
00216      {
00217           if ((*it)->getType() != Node::ntElement)
00218                continue;
00219           // Cast the child element to a tag.
00220           Element& t = *static_cast<Element*>(*it);
00221 
00222           // Ensure this is a <rule> tag
00223           if (t.getName() == "rule")
00224                // Construct and insert a rule
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 }

Generated at Tue Apr 16 17:09:06 2002 for jabberoo by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001