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

jabberoox-filter.cpp

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 "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 // Filter::Action methods
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 // Filter::Condition methods
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 // Filter methods
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      // Retrieve the rule name
00159      _name = rule.getAttrib("name");
00160      if (_name == "")
00161           _name = "Default rule";
00162 
00163      // Walk the rule's child tags
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           // Cast the child judo::element into a tag..
00170           judo::Element& t = *static_cast<judo::Element*>(*it);
00171           
00172           // See if this is an action
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           // See if this is a condition
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 // FilterList methods
00208 //
00209 // ---------------------------------------------------------
00210 FilterList::FilterList(const judo::Element& query)
00211 {
00212      // Make sure this is the proper namespace
00213      if (!query.cmpAttrib("xmlns", "jabber:iq:filter"))
00214           return;
00215 
00216      // Walk each of the child <rule> tags and build rule objects
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           // Cast the child judo::element to a tag.
00223           judo::Element& t = *static_cast<judo::Element*>(*it);
00224 
00225           // Ensure this is a <rule> tag
00226           if (t.getName() == "rule")
00227                // Construct and insert a rule
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 } // namespace jabberoo

Generated on Thu Jul 24 13:31:51 2003 for jabberoo by doxygen1.3-rc3