The Battle for Wesnoth  1.19.0-dev
blacklist.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2024
3  by Iris Morelle <shadowm2006@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include "config.hpp"
19 
20 namespace campaignd
21 {
22 
23 /**
24  * Add-on blacklist table.
25  *
26  * A path to a blacklist WML file may be provided in the campaignd
27  * configuration. The file's contents are used to maintain a blacklist to
28  * check certain add-on metadata fields against it every time a new or
29  * existing add-on is uploaded ([upload] request).
30  *
31  * Blacklist entries are glob patterns accepting the '*' and '?' wildcards for
32  * matching any number of characters and a single character, respectively. The
33  * lists are expected to be comma-delimited.
34  *
35  * ip = (net address masks)
36  * email = (email address patterns)
37  * name = (add-on id/dirname patterns)
38  * title = (add-on title patterns)
39  * author = (add-on author patterns)
40  * description = (add-on description patterns)
41  */
42 class blacklist
43 {
44 public:
45  typedef std::vector<std::string> globlist;
46 
47  blacklist(const blacklist&) = delete;
48  blacklist& operator=(const blacklist&) = delete;
49 
50  blacklist();
51  explicit blacklist(const config& cfg);
52 
53  void clear();
54 
55  /**
56  * Initializes the blacklist from WML.
57  *
58  * @param cfg WML node object with the contents of the [blacklist] tag.
59  */
60  void read(const config& cfg);
61 
62  /**
63  * Writes the blacklist to a WML node.
64  *
65  * @param cfg WML node object to write to. Any existing contents are
66  * erased by this method.
67  */
68  void write(config& cfg) const;
69 
70  /**
71  * Whether an add-on described by these fields is blacklisted.
72  *
73  * Empty parameters are ignored.
74  */
75  bool is_blacklisted(const std::string& name,
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 private:
86 
90 
91  void parse_str_to_globlist(const std::string& str, globlist& glist);
92 
93  bool is_in_globlist(const std::string& str, const globlist& glist) const;
94 
95  bool is_in_ip_masklist(const std::string& ip, const globlist& mlist) const;
96  bool ip_matches(const std::string& ip, const std::string& ip_mask) const;
97 };
98 
99 }
Add-on blacklist table.
Definition: blacklist.hpp:43
blacklist(const blacklist &)=delete
bool ip_matches(const std::string &ip, const std::string &ip_mask) const
Definition: blacklist.cpp:127
globlist descriptions_
Definition: blacklist.hpp:85
void read(const config &cfg)
Initializes the blacklist from WML.
Definition: blacklist.cpp:60
void parse_str_to_globlist(const std::string &str, globlist &glist)
Definition: blacklist.cpp:71
std::vector< std::string > globlist
Definition: blacklist.hpp:45
bool is_in_globlist(const std::string &str, const globlist &glist) const
Definition: blacklist.cpp:93
blacklist & operator=(const blacklist &)=delete
bool is_in_ip_masklist(const std::string &ip, const globlist &mlist) const
Definition: blacklist.cpp:111
void write(config &cfg) const
Writes the blacklist to a WML node.
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:76
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
std::pair< unsigned int, unsigned int > ip_mask
Definition: ban.hpp:56