The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
blacklist.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2016 by Ignacio Riquelme Morelle <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
16 
17 #include "log.hpp"
20 
21 static lg::log_domain log_campaignd_bl("campaignd/blacklist");
22 #define LOG_BL LOG_STREAM(err, log_campaignd_bl)
23 
24 namespace campaignd
25 {
26 
28  : names_()
29  , titles_()
30  , descriptions_()
31  , authors_()
32  , ips_()
33  , emails_()
34 {
35 }
36 
38  : names_()
39  , titles_()
40  , descriptions_()
41  , authors_()
42  , ips_()
43  , emails_()
44 {
45  this->read(cfg);
46 }
47 
49 {
50  names_.clear();
51  titles_.clear();
52  descriptions_.clear();
53 
54  authors_.clear();
55  ips_.clear();
56  emails_.clear();
57 }
58 
59 void blacklist::read(const config& cfg)
60 {
61  parse_str_to_globlist(cfg["name"], names_);
62  parse_str_to_globlist(cfg["title"], titles_);
63  parse_str_to_globlist(cfg["description"], descriptions_);
64 
65  parse_str_to_globlist(cfg["author"], authors_);
66  parse_str_to_globlist(cfg["ip"], ips_);
67  parse_str_to_globlist(cfg["email"], emails_);
68 }
69 
71 {
72  glist = utils::split(str);
73 }
74 
76  const std::string& title,
77  const std::string& description,
78  const std::string& author,
79  const std::string& ip,
80  const std::string& email) const
81 {
82  // Checks done in increasing order of performance impact and decreasing
83  // order of relevance.
84  return is_in_ip_masklist(ip, ips_) ||
85  is_in_globlist(email, emails_) ||
86  is_in_globlist(name, names_) ||
87  is_in_globlist(title, titles_) ||
88  is_in_globlist(author, authors_) ||
89  is_in_globlist(description, descriptions_);
90 }
91 
92 bool blacklist::is_in_globlist(const std::string& str, const blacklist::globlist& glist) const
93 {
94  if (!str.empty())
95  {
96  const std::string& lc_str = utf8::lowercase(str);
97  for(const std::string& glob : glist)
98  {
99  const std::string& lc_glob = utf8::lowercase(glob);
100  if (utils::wildcard_string_match(lc_str, lc_glob)) {
101  LOG_BL << "Blacklisted field found: " << str << " (" << glob << ")\n";
102  return true;
103  }
104  }
105  }
106 
107  return false;
108 }
109 
111 {
112  if (!ip.empty())
113  {
114  for(const std::string& ip_mask : mlist)
115  {
116  if (ip_matches(ip, ip_mask)) {
117  LOG_BL << "Blacklisted IP found: " << ip << " (" << ip_mask << ")\n";
118  return true;
119  }
120  }
121  }
122 
123  return false;
124 }
125 
127 {
128  // TODO: we want CIDR subnet mask matching here, not glob matching!
129  return utils::wildcard_string_match(ip, ip_mask);
130 }
131 
132 }
bool is_in_globlist(const std::string &str, const globlist &glist) const
Definition: blacklist.cpp:92
bool wildcard_string_match(const std::string &str, const std::string &match)
Match using '*' as any number of characters (including none), and '?' as any one character.
utf8::string lowercase(const utf8::string &s)
Returns a lowercased version of the string.
Definition: unicode.cpp:53
std::vector< glob > globlist
Definition: blacklist.hpp:48
std::pair< unsigned int, unsigned int > ip_mask
Definition: ban.hpp:54
std::string glob
Definition: blacklist.hpp:47
bool is_in_ip_masklist(const std::string &ip, const globlist &mlist) const
Definition: blacklist.cpp:110
bool is_blacklisted(const std::string &name, const std::string &title, const std::string &description, const std::string &author, const std::string &ip, const std::string &email) const
Whether an add-on described by these fields is blacklisted.
Definition: blacklist.cpp:75
void parse_str_to_globlist(const std::string &str, globlist &glist)
Definition: blacklist.cpp:70
globlist descriptions_
Definition: blacklist.hpp:85
GLuint const GLchar * name
Definition: glew.h:1782
static lg::log_domain log_campaignd_bl("campaignd/blacklist")
Standard logging facilities (interface).
void read(const config &cfg)
Initializes the blacklist from WML.
Definition: blacklist.cpp:59
std::vector< std::string > split(std::string const &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
#define LOG_BL
Definition: blacklist.cpp:22
GLsizei const GLcharARB ** string
Definition: glew.h:4503
bool ip_matches(const std::string &ip, const glob &ip_mask) const
Definition: blacklist.cpp:126