1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
crate::ix!();
/**
| JSON key under which the data is stored
| in the json database.
|
*/
pub const JSON_KEY: &'static str = "banned_nets";
/**
| Access to the banlist database (banlist.json)
|
*/
pub struct BanDB {
banlist_dat: PathBuf,
banlist_json: PathBuf,
}
impl BanDB {
pub fn new(ban_list_path: PathBuf) -> Self {
let mut dat = ban_list_path.clone();
dat.push(".dat");
let mut json = ban_list_path.clone();
json.push(".json");
Self {
banlist_dat: dat,
banlist_json: json,
}
}
pub fn write(&mut self, ban_set: &BanMap) -> bool {
let mut errors = Vec::<String>::default();
let map = {
let mut x = HashMap::new();
x.insert(JSON_KEY.to_string(), SettingsValue(ban_map_to_json(ban_set)));
x
};
if write_settings(
&self.banlist_json,
&map,
&mut errors)
{
return true;
}
for err in errors.iter() {
eprintln!("{}", err);
}
false
}
/**
| Read the banlist from disk.
|
| -----------
| @param[out] banSet
|
| The loaded list. Set if `true` is returned,
| otherwise it is left in an undefined
| state.
|
| -----------
| @return
|
| true on success
|
*/
pub fn read(&mut self, ban_set: &mut BanMap) -> bool {
if self.banlist_dat.exists() {
log_printf!(
"banlist.dat ignored because it can only be read by {} version 22.x. Remove {} to silence this warning.\n",
PACKAGE_NAME,
quoted(banlist_dat.to_string())
);
}
// If the JSON banlist does not exist,
// then recreate it
if !self.banlist_json.exists() {
return false;
}
let mut settings = HashMap::<String,SettingsValue>::default();
let mut errors = Vec::<String>::default();
if !read_settings(&self.banlist_json,&mut settings,&mut errors) {
for err in errors.iter() {
log_printf!(
"Cannot load banlist {}: {}\n",
self.banlist_json.to_string(),
err
);
}
return false;
}
let mut try_block = || -> TryBlockResult::<_,&'static str> {
ban_map_from_json(&settings[JSON_KEY].0, ban_set);
TryBlockResult::Success
};
match try_block() {
TryBlockResult::Return(v) => return v,
TryBlockResult::Err(e) => {
log_printf!(
"Cannot parse banlist {}: {}\n",
self.banlist_json.to_string(),
e.what()
);
return false;
},
TryBlockResult::Break => { }
TryBlockResult::Success => { }
}
true
}
}