]> granicus.if.org Git - icinga2/blobdiff - lib/livestatus/attributefilter.cpp
Merge branch 'support/2.8'
[icinga2] / lib / livestatus / attributefilter.cpp
index 118981c59034407e1117a59c5d0718f943196ba0..35ed40cf7acb7f7a93f5b0527c315d6974e35c01 100644 (file)
@@ -1,6 +1,6 @@
 /******************************************************************************
  * Icinga 2                                                                   *
- * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
+ * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
  *                                                                            *
  * This program is free software; you can redistribute it and/or              *
  * modify it under the terms of the GNU General Public License                *
 #include "base/convert.hpp"
 #include "base/array.hpp"
 #include "base/objectlock.hpp"
-#include "base/logger_fwd.hpp"
-#include <boost/foreach.hpp>
+#include "base/logger.hpp"
 #include <boost/regex.hpp>
+#include <boost/algorithm/string/predicate.hpp>
 
 using namespace icinga;
 
-AttributeFilter::AttributeFilter(const String& column, const String& op, const String& operand)
-       : m_Column(column), m_Operator(op), m_Operand(operand)
+AttributeFilter::AttributeFilter(String column, String op, String operand)
+       : m_Column(std::move(column)), m_Operator(std::move(op)), m_Operand(std::move(operand))
 { }
 
 bool AttributeFilter::Apply(const Table::Ptr& table, const Value& row)
@@ -44,7 +44,7 @@ bool AttributeFilter::Apply(const Table::Ptr& table, const Value& row)
                        bool negate = (m_Operator == "<");
 
                        ObjectLock olock(array);
-                       BOOST_FOREACH(const String& item, array) {
+                       for (const String& item : array) {
                                if (item == m_Operand)
                                        return !negate; /* Item found in list. */
                        }
@@ -57,7 +57,7 @@ bool AttributeFilter::Apply(const Table::Ptr& table, const Value& row)
                }
        } else {
                if (m_Operator == "=") {
-                       if (value.GetType() == ValueNumber)
+                       if (value.GetType() == ValueNumber || value.GetType() == ValueBoolean)
                                return (static_cast<double>(value) == Convert::ToDouble(m_Operand));
                        else
                                return (static_cast<String>(value) == m_Operand);
@@ -69,16 +69,28 @@ bool AttributeFilter::Apply(const Table::Ptr& table, const Value& row)
                                boost::smatch what;
                                ret = boost::regex_search(operand.GetData(), what, expr);
                        } catch (boost::exception&) {
-                               Log(LogWarning, "AttributeFilter", "Regex '" + m_Operand + " " + m_Operator + " " + Convert::ToString(value) + "' error.");
+                               Log(LogWarning, "AttributeFilter")
+                                       << "Regex '" << m_Operand << " " << m_Operator << " " << value << "' error.";
                                ret = false;
                        }
 
-                       //Log(LogDebug, "LivestatusListener/AttributeFilter", "Attribute filter '" + m_Operand + " " + m_Operator + " " +
-                       //    static_cast<String>(value) + "' " + (ret ? "matches" : "doesn't match") + "." );
+                       //Log(LogDebug, "LivestatusListener/AttributeFilter")
+                       //    << "Attribute filter '" << m_Operand + " " << m_Operator << " "
+                       //    << value << "' " << (ret ? "matches" : "doesn't match") << ".";
 
                        return ret;
                } else if (m_Operator == "=~") {
-                       return string_iless()(value, m_Operand);
+                       bool ret;
+                       try {
+                               String operand = value;
+                               ret = boost::iequals(operand, m_Operand.GetData());
+                       } catch (boost::exception&) {
+                               Log(LogWarning, "AttributeFilter")
+                                       << "Case-insensitive equality '" << m_Operand << " " << m_Operator << " " << value << "' error.";
+                               ret = false;
+                       }
+
+                       return ret;
                } else if (m_Operator == "~~") {
                        bool ret;
                        try {
@@ -87,12 +99,14 @@ bool AttributeFilter::Apply(const Table::Ptr& table, const Value& row)
                                boost::smatch what;
                                ret = boost::regex_search(operand.GetData(), what, expr);
                        } catch (boost::exception&) {
-                               Log(LogWarning, "AttributeFilter", "Regex '" + m_Operand + " " + m_Operator + " " + Convert::ToString(value) + "' error.");
+                               Log(LogWarning, "AttributeFilter")
+                                       << "Regex '" << m_Operand << " " << m_Operator << " " << value << "' error.";
                                ret = false;
                        }
 
-                       //Log(LogDebug, "LivestatusListener/AttributeFilter", "Attribute filter '" + m_Operand + " " + m_Operator + " " +
-                       //    static_cast<String>(value) + "' " + (ret ? "matches" : "doesn't match") + "." );
+                       //Log(LogDebug, "LivestatusListener/AttributeFilter")
+                       //    << "Attribute filter '" << m_Operand << " " << m_Operator << " "
+                       //    << value << "' " << (ret ? "matches" : "doesn't match") << ".";
 
                        return ret;
                } else if (m_Operator == "<") {