Add “SHODAN” to blacklist.
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / filters / BlacklistFilter.java
1 /*
2  * rhynodge - BlacklistFilter.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.rhynodge.filters;
19
20 import static com.google.common.base.Preconditions.checkState;
21 import static com.google.common.collect.FluentIterable.from;
22 import static java.util.Arrays.asList;
23
24 import java.util.List;
25
26 import net.pterodactylus.rhynodge.Filter;
27 import net.pterodactylus.rhynodge.State;
28 import net.pterodactylus.rhynodge.states.FailedState;
29 import net.pterodactylus.rhynodge.states.TorrentState;
30 import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile;
31
32 import com.google.common.base.Predicate;
33
34 /**
35  * Filter for {@link TorrentState}s that removes all {@link TorrentFile}s whose
36  * names match a list of bad words.
37  *
38  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39  */
40 public class BlacklistFilter implements Filter {
41
42         private final Iterable<String> filterWords;
43
44         public BlacklistFilter(List<String> filterWords) {
45                 this.filterWords = filterWords;
46         }
47
48         @Override
49         public State filter(State state) {
50                 if (!state.success()) {
51                         return FailedState.from(state);
52                 }
53                 checkState(state instanceof TorrentState, "state is not a TorrentState but a %s!", state.getClass());
54
55                 TorrentState torrentState = (TorrentState) state;
56                 return new TorrentState(from(torrentState.torrentFiles()).filter(new Predicate<TorrentFile>() {
57                         @Override
58                         public boolean apply(TorrentFile torrentFile) {
59                                 return (torrentFile != null) && nameDoesNotMatchAFilterWord(torrentFile.name());
60                         }
61
62                         private boolean nameDoesNotMatchAFilterWord(final String name) {
63                                 return !from(filterWords).anyMatch(new Predicate<String>() {
64                                         @Override
65                                         public boolean apply(String word) {
66                                                 return name.toLowerCase().contains(word.toLowerCase());
67                                         }
68                                 });
69                         }
70                 }).toList());
71         }
72
73         public static BlacklistFilter createDefaultBlacklistFilter() {
74                 return new BlacklistFilter(asList(
75             "-3LT0N",
76             "-ADTRG",
77             "-AMIABLE",
78             "-AN0NYM0US",
79             "-AQOS",
80             "-AXED",
81             "-CM",
82             "-DiRTYMARY",
83             "-EDAW",
84             "-EVOLVE",
85             "-FooKaS",
86             "-Haggebulle",
87             "-HELLRAZ0R",
88             "-juggs",
89             "-KILLERS",
90             "-LEGi0N",
91             "-MAX",
92             "-MiLLENiUM",
93             "-NoGRP",
94             "-NYDIC",
95             "-P2P",
96             "-PLAYNOW",
97             "-S4A",
98             "-SHODAN",
99             "-SPARKS",
100             "-SUFFiCE",
101             "-TAMILROCKERS",
102             "-TASTE",
103             "-THC",
104             "-UnKnOwN",
105             "-UNiQUE",
106             "-W00D"
107         ));
108         }
109
110 }