001/*
002 * Trident - A Multithreaded Server Alternative
003 * Copyright 2014 The TridentSDK Team
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package net.tridentsdk.server.service;
018
019import com.google.common.collect.Sets;
020import net.tridentsdk.Trident;
021import net.tridentsdk.registry.PlayerStatus;
022
023import java.io.*;
024import java.net.InetSocketAddress;
025import java.nio.file.Files;
026import java.nio.file.Path;
027import java.util.Iterator;
028import java.util.Set;
029import java.util.UUID;
030
031/**
032 * Loads the Trident status files for bans, ops, and whitelists
033 *
034 * @author The TridentSDK Team
035 */
036public class Statuses implements PlayerStatus {
037    @Override
038    public boolean isBanned(UUID uuid) {
039        return banList.contains(uuid.toString());
040    }
041
042    @Override
043    public boolean isIpBanned(InetSocketAddress address) {
044        return banList.contains("ip: " + address.getAddress().getHostAddress());
045    }
046
047    @Override
048    public boolean isOpped(UUID uuid) {
049        return opsList.contains(uuid.toString());
050    }
051
052    @Override
053    public boolean isWhitelisted(UUID uuid) {
054        return whiteList.contains(uuid.toString());
055    }
056
057    @Override
058    public void ban(UUID uuid) {
059        banList.add(uuid.toString());
060    }
061
062    @Override
063    public void ipBan(InetSocketAddress address) {
064        banList.add("ip: " + address.getAddress().getHostAddress());
065    }
066
067    @Override
068    public void op(UUID uuid) {
069        opsList.add(uuid.toString());
070    }
071
072    @Override
073    public void whitelist(UUID uuid) {
074        whiteList.add(uuid.toString());
075    }
076
077    private final Path bans = Trident.fileContainer().resolve("bans.txt");
078    private final Path ops = Trident.fileContainer().resolve("ops.txt");
079    private final Path whitelist = Trident.fileContainer().resolve("whitelist.txt");
080
081    private final Set<String> banList = Sets.newConcurrentHashSet();
082    private final Set<String> opsList = Sets.newConcurrentHashSet();
083    private final Set<String> whiteList = Sets.newConcurrentHashSet();
084
085    public void loadAll() {
086        ensureExists(bans);
087        ensureExists(ops);
088        ensureExists(whitelist);
089
090        for (Iterator<String> iterator = loadFromReader(readFile(bans)); iterator.hasNext(); ) {
091            banList.add(iterator.next());
092        }
093
094        for (Iterator<String> iterator = loadFromReader(readFile(ops)); iterator.hasNext(); ) {
095            opsList.add(iterator.next());
096        }
097
098        for (Iterator<String> iterator = loadFromReader(readFile(whitelist)); iterator.hasNext(); ) {
099            whiteList.add(iterator.next());
100        }
101    }
102
103    private void ensureExists(Path path) {
104        if (!Files.exists(path)) {
105            try {
106                Files.createFile(path);
107            } catch (IOException e) {
108                e.printStackTrace();
109            }
110        }
111    }
112
113    private BufferedReader readFile(Path path) {
114        try {
115            return new BufferedReader(new FileReader(path.toFile()));
116        } catch (FileNotFoundException e) {
117            e.printStackTrace();
118        }
119
120        return null;
121    }
122
123    private BufferedWriter writeFile(Path path) {
124        try {
125            return new BufferedWriter(new FileWriter(path.toFile()));
126        } catch (IOException e) {
127            e.printStackTrace();
128        }
129
130        return null;
131    }
132
133    private Iterator<String> loadFromReader(BufferedReader reader) {
134        return new Iterator<String>() {
135            String line;
136
137            @Override
138            public boolean hasNext() {
139                try {
140                    return (line = reader.readLine()) != null;
141                } catch (IOException e) {
142                    e.printStackTrace();
143                }
144
145                return false;
146            }
147
148            @Override
149            public String next() {
150                if (line == null) {
151                    try {
152                        return reader.readLine();
153                    } catch (IOException e) {
154                        e.printStackTrace();
155                    }
156                } else {
157                    return line;
158                }
159
160                return null;
161            }
162        };
163    }
164
165    public void saveAll() throws IOException {
166        ensureExists(bans);
167        BufferedWriter banWriter = writeFile(bans);
168        for (String aBanList : banList) {
169            banWriter.write(aBanList);
170            banWriter.newLine();
171        }
172        banWriter.close();
173
174        ensureExists(ops);
175        BufferedWriter opsWriter = writeFile(ops);
176        for (String op : opsList) {
177            opsWriter.write(op);
178            opsWriter.newLine();
179        }
180        opsWriter.close();
181
182        ensureExists(whitelist);
183        BufferedWriter whitelistWriter = writeFile(whitelist);
184        for (String list : whiteList) {
185            whitelistWriter.write(list);
186            whitelistWriter.newLine();
187        }
188        whitelistWriter.close();
189    }
190}