pmatcher.h

00001 /***************************************************************************
00002  *   Copyright (C) 2004 by Michael Moritz                                  *
00003  *   mimo@restoel.net                                                      *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020  
00021 #ifndef __PMATCHER_H 
00022 #define __PMATCHER_H
00023 
00024 #include <string>
00025 //getnameinfo
00026 #include <netdb.h>
00027 //
00028 #include "regexcpp.h"
00029 #include "triplet.h"
00030 
00031 class PatternMatcher
00032 {
00033 public:
00034         PatternMatcher(const Triplet& triplet) 
00035         :       _regex(),_bHaveHostname(false),
00036                 _triplet(triplet)
00037         {
00038                 _tripletkey = "s=" + triplet.getSender().substr(1,triplet.getSender().size()-2)+"\n";
00039                 _tripletkey += "r=" + triplet.getRecipient().substr(1, triplet.getRecipient().size()-2)+"\n";
00040                 _tripletkey += "c=" + triplet.getClientAddress().substr(1, triplet.getClientAddress().size()-2)+"\n";
00041         }
00042         ~PatternMatcher() {}
00043         bool match(const std::string& pattern) throw (std::exception)
00044         {
00045                 //check if the pattern contains the host (h=) token
00046                 if( !_bHaveHostname && (pattern.find("h=") != std::string::npos) ) {
00047                         _bHaveHostname = true;
00048                         _tripletkey += "h=" + _triplet.getResolved() + "\n";
00049                 }
00050                 //remove leading and trailing quote added in constructor...
00051                 //std::cout << "pattern: " << pattern << std::endl;
00052                 _regex.setPattern(pattern);
00053                 if(_regex.match(_tripletkey)) {
00054                         //std::cout << "MATCH   " << _tripletkey << std::endl;
00055                         return true;
00056                 }
00057                 //std::cout << "nomatch " << _tripletkey << std::endl;
00058                 return false;
00059         }
00060         void reset() { _bHaveHostname = false; }
00061 private:
00062         RegEx _regex;//REG_ICASE (REG_EXTENDED|REG_ICASE)
00063         bool _bHaveHostname;
00064         const Triplet& _triplet;
00065         std::string _tripletkey;
00066 };
00067 #endif

Generated on Tue Jul 24 16:36:53 2007 for gps by  doxygen 1.5.1