]> jfr.im git - irc/UndernetIRC/gnuworld.git/commitdiff
Added nickUserCidr class, which is based on the xCIDR class.
authorMrBean <redacted>
Wed, 24 Jun 2009 17:39:44 +0000 (17:39 +0000)
committerMrBean <redacted>
Wed, 24 Jun 2009 17:39:44 +0000 (17:39 +0000)
This class handles all nick!user@cidr matching

include/cidr.h
include/nickUserCidr.h [new file with mode: 0755]
src/Makefile.am
src/Makefile.in
src/cidr.cc
src/nickUserCidr.cc [new file with mode: 0755]

index 4cf22c299aebfacc54f5fdb715055108030c6312..e17a5af2e4ef4a09a940c71de9399a9289057bc7 100755 (executable)
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
  * USA.
  *
- * $Id: cidr.h,v 1.1 2009/06/20 17:36:52 mrbean_ Exp $
+ * $Id: cidr.h,v 1.2 2009/06/24 17:39:44 mrbean_ Exp $
  */
 
 #ifndef __CIDR_H
-#define __CIDR_H "$Id: cidr.h,v 1.1 2009/06/20 17:36:52 mrbean_ Exp $"
+#define __CIDR_H "$Id: cidr.h,v 1.2 2009/06/24 17:39:44 mrbean_ Exp $"
 
 #include       <string>
 #include       <sys/types.h>
@@ -70,6 +70,9 @@ public:
        inline const xIP& GetPrefix() const 
                { return prefix; }
 
+       inline const std::string& AsString() const
+               { return str; }
+               
        bool matches(const std::string& IP) ;   
                
 protected:
@@ -89,6 +92,8 @@ protected:
         */
        xIP prefix ;
        
+       std::string     str; 
+       
 private:
        
        bool ExtractMask(const std::string&);
diff --git a/include/nickUserCidr.h b/include/nickUserCidr.h
new file mode 100755 (executable)
index 0000000..998cd8a
--- /dev/null
@@ -0,0 +1,120 @@
+/**
+ * ip.h
+ * This stuff should probably be moved to a static class.
+ * xIP provides mostly utilitarian functionality for IP
+ * addresses.
+ * This class originally created by Orlando Bassotto.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
+ * USA.
+ *
+ * $Id: nickUserCidr.h,v 1.1 2009/06/24 17:39:44 mrbean_ Exp $
+ */
+
+#ifndef __NICK_USER_CIDR_H
+#define __NICK_USER_CIDR_H "$Id: nickUserCidr.h,v 1.1 2009/06/24 17:39:44 mrbean_ Exp $"
+
+#include       <string>
+#include       <sys/types.h>
+#include       <sys/socket.h>
+#include       <netinet/in.h>
+#include       <netdb.h>
+#include       <arpa/inet.h>
+#include       "cidr.h"
+
+namespace gnuworld
+{
+
+/**
+ * A utility class that is used to retrieve information about
+ * CIDR addresses.
+ */
+class xNickUserCIDR
+{
+
+public:
+
+       /**
+        * Construct an xNickUserCIDR instance given an address in std::string
+        * format.
+        */
+       xNickUserCIDR( const std::string& nickUserCIDR) ;
+
+       /**
+        * Checks whether or not the CIDR is valid.
+        */
+       inline const bool& GetValid() const 
+               { return valid; }
+               
+       /**
+        * Retrieve the mask of this CIDR (the part after the /).
+        */
+       inline const unsigned int& GetMask() const 
+               { return cidr.GetMask(); }
+
+       /**
+        * Retrieve the prefix of this CIDR (the part before the /).
+        */
+       inline const xIP& GetPrefix() const 
+               { return cidr.GetPrefix(); }
+
+       /**
+        * Retrieve the nick 
+        */
+       inline const std::string& GetNick() const 
+               { return nick; }
+
+       /**
+        * Retrieve the user
+        */
+       inline const std::string& GetUser() const 
+               { return user; }
+       
+       inline const std::string& AsString() const
+               { return str; }
+
+               
+       bool matches(const std::string& IP) ;   
+               
+protected:
+
+       
+       /**
+        * CIDR part 
+        */
+       xCIDR cidr;
+       
+       /**
+        * The nick
+        */
+       std::string nick ;
+       
+       /**
+        * user
+        */
+       std::string     user ;
+       
+       std::string     str ;
+       
+       bool valid;
+private:
+       
+       bool ExtractNickUser(const std::string&);
+
+} ;
+
+} // namespace gnuworld
+
+#endif 
index fcf08f1998b11b1a6311e6290ee5f4512836657a..3e04584820cc386592a29ae08dfe28abfd995a31 100644 (file)
@@ -1,5 +1,5 @@
 ## Process this file with automake to produce Makefile.in
-## $Id: Makefile.am,v 1.30 2009/06/20 17:36:52 mrbean_ Exp $
+## $Id: Makefile.am,v 1.31 2009/06/24 17:39:44 mrbean_ Exp $
 
 lib_LTLIBRARIES = libgnuworldcore.la
 libgnuworldcore_la_SOURCES = Channel.cc \
@@ -13,6 +13,7 @@ libgnuworldcore_la_SOURCES = Channel.cc \
        iClient.cc \
        iServer.cc \
        ip.cc \
+       nickUserCidr.cc \
        server.cc \
        server_connection.cc \
        server_events.cc \
index a7f4c4026ddf11a5ce6b33444b41f48880af913b..55e806a2658b86ae7c142f1f4002eeda4a78f562 100644 (file)
@@ -62,7 +62,8 @@ am_libgnuworldcore_la_OBJECTS = libgnuworldcore_la-Channel.lo \
        libgnuworldcore_la-UnloadClientTimerHandler.lo \
        libgnuworldcore_la-cidr.lo libgnuworldcore_la-client.lo \
        libgnuworldcore_la-iClient.lo libgnuworldcore_la-iServer.lo \
-       libgnuworldcore_la-ip.lo libgnuworldcore_la-server.lo \
+       libgnuworldcore_la-ip.lo libgnuworldcore_la-nickUserCidr.lo \
+       libgnuworldcore_la-server.lo \
        libgnuworldcore_la-server_connection.lo \
        libgnuworldcore_la-server_events.lo \
        libgnuworldcore_la-server_glines.lo \
@@ -226,6 +227,7 @@ libgnuworldcore_la_SOURCES = Channel.cc \
        iClient.cc \
        iServer.cc \
        ip.cc \
+       nickUserCidr.cc \
        server.cc \
        server_connection.cc \
        server_events.cc \
@@ -355,6 +357,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libgnuworldcore_la-iClient.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libgnuworldcore_la-iServer.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libgnuworldcore_la-ip.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libgnuworldcore_la-nickUserCidr.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libgnuworldcore_la-server.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libgnuworldcore_la-server_connection.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libgnuworldcore_la-server_events.Plo@am__quote@
@@ -459,6 +462,13 @@ libgnuworldcore_la-ip.lo: ip.cc
 @AMDEP_TRUE@@am__fastdepCXX_FALSE@     DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
 @am__fastdepCXX_FALSE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libgnuworldcore_la_CXXFLAGS) $(CXXFLAGS) -c -o libgnuworldcore_la-ip.lo `test -f 'ip.cc' || echo '$(srcdir)/'`ip.cc
 
+libgnuworldcore_la-nickUserCidr.lo: nickUserCidr.cc
+@am__fastdepCXX_TRUE@  $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libgnuworldcore_la_CXXFLAGS) $(CXXFLAGS) -MT libgnuworldcore_la-nickUserCidr.lo -MD -MP -MF $(DEPDIR)/libgnuworldcore_la-nickUserCidr.Tpo -c -o libgnuworldcore_la-nickUserCidr.lo `test -f 'nickUserCidr.cc' || echo '$(srcdir)/'`nickUserCidr.cc
+@am__fastdepCXX_TRUE@  mv -f $(DEPDIR)/libgnuworldcore_la-nickUserCidr.Tpo $(DEPDIR)/libgnuworldcore_la-nickUserCidr.Plo
+@AMDEP_TRUE@@am__fastdepCXX_FALSE@     source='nickUserCidr.cc' object='libgnuworldcore_la-nickUserCidr.lo' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCXX_FALSE@     DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCXX_FALSE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libgnuworldcore_la_CXXFLAGS) $(CXXFLAGS) -c -o libgnuworldcore_la-nickUserCidr.lo `test -f 'nickUserCidr.cc' || echo '$(srcdir)/'`nickUserCidr.cc
+
 libgnuworldcore_la-server.lo: server.cc
 @am__fastdepCXX_TRUE@  $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libgnuworldcore_la_CXXFLAGS) $(CXXFLAGS) -MT libgnuworldcore_la-server.lo -MD -MP -MF $(DEPDIR)/libgnuworldcore_la-server.Tpo -c -o libgnuworldcore_la-server.lo `test -f 'server.cc' || echo '$(srcdir)/'`server.cc
 @am__fastdepCXX_TRUE@  mv -f $(DEPDIR)/libgnuworldcore_la-server.Tpo $(DEPDIR)/libgnuworldcore_la-server.Plo
index 47217c8a92ea9e13f0b66472a6d655f5d87141a2..e7121f62b5c71182c91a8797d8062944a2657c14 100755 (executable)
@@ -18,7 +18,7 @@
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
  * USA.
  *
- * $Id: cidr.cc,v 1.1 2009/06/20 17:36:52 mrbean_ Exp $
+ * $Id: cidr.cc,v 1.2 2009/06/24 17:39:44 mrbean_ Exp $
  */
 
 #include       <string>
 #include       "StringTokenizer.h"
 #include       "gnuworld_config.h"
 
-RCSTAG( "$Id: cidr.cc,v 1.1 2009/06/20 17:36:52 mrbean_ Exp $" ) ;
+RCSTAG( "$Id: cidr.cc,v 1.2 2009/06/24 17:39:44 mrbean_ Exp $" ) ;
 
 namespace gnuworld
 {
 
 using std::string ;
 
-xCIDR::xCIDR( const std::string& IP ) : prefix(0)
+xCIDR::xCIDR( const std::string& IP ) : str(IP), prefix(0)
 {
        std::size_t pos = IP.find_first_of("/");
        if(pos == string::npos || pos == IP.length()) //Failed to find / or its at the end of the string
diff --git a/src/nickUserCidr.cc b/src/nickUserCidr.cc
new file mode 100755 (executable)
index 0000000..33edd53
--- /dev/null
@@ -0,0 +1,99 @@
+/**
+ * nickUserCidr.cc
+ * Copyright (C) 2002 Daniel Karrels <dan@karrels.com>
+ *                     Orlando Bassotto
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
+ * USA.
+ *
+ * $Id: nickUserCidr.cc,v 1.1 2009/06/24 17:39:44 mrbean_ Exp $
+ */
+
+#include       <string>
+#include       <sstream>
+
+#include       <cstdio>
+#include       <cstdlib>
+#include       <cstring>
+
+#include       <sys/types.h>
+#include       <sys/socket.h>
+#include       <netinet/in.h>
+#include       <netdb.h>
+#include       <arpa/inet.h>
+
+
+#include       "nickUserCidr.h"
+#include       "misc.h"
+#include       "StringTokenizer.h"
+#include       "gnuworld_config.h"
+#include       "match.h"
+
+RCSTAG( "$Id: nickUserCidr.cc,v 1.1 2009/06/24 17:39:44 mrbean_ Exp $" ) ;
+
+namespace gnuworld
+{
+
+using std::string ;
+
+xNickUserCIDR::xNickUserCIDR( const std::string& nickUserCidr ) : str("") , nick("*"),user("*"),cidr("")
+{
+       StringTokenizer st = StringTokenizer(nickUserCidr,'@');
+       if(st.size() == 1) //No @ , consider everything as a cidr
+       {
+               cidr = xCIDR(nickUserCidr);
+               valid = cidr.GetValid();
+       } else if(st.size() == 2) {
+               ExtractNickUser(st[0]);
+               cidr = xCIDR(st[1]);
+               valid = cidr.GetValid();
+               
+       }
+       str = nick + "!" + user + "@" + cidr.AsString();
+       
+}
+
+bool xNickUserCIDR::ExtractNickUser(const std::string& nickUser)
+{
+       if(nickUser[0] == '!') //Starts with ! ? user = everything after the !
+       {
+               user = nickUser.substr(1);
+               return true;
+       }
+       StringTokenizer st = StringTokenizer(nickUser,'!');
+       if(st.size() == 1)
+       {       //No ! user = everything
+               user = st[0];
+       } else {
+               //We have ! - nick = up til !, user = rest
+               nick = st[0];
+               user = st.assemble(1);
+       }
+       return true;
+}
+bool xNickUserCIDR::matches(const std::string& IP)
+{
+       StringTokenizer st = StringTokenizer(IP,'@');
+       if(st.size() == 1) 
+       {       // WTF No @ ?! 
+               return false;
+       }
+       if(!match(nick+"!"+user,st[0]))
+       { //User and nick match
+               return cidr.matches(st[1]);
+       }
+       return false;
+}
+} // namespace gnuworld