Don’t start a command reader on stdin/stdout.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / main / Main.java
1 /*
2  * XdccDownloader - Main.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.xdcc.main;
19
20 import static com.lexicalscope.jewel.cli.CliFactory.parseArguments;
21
22 import java.io.File;
23 import java.util.Collection;
24 import java.util.HashSet;
25 import java.util.concurrent.CopyOnWriteArraySet;
26 import java.util.concurrent.Executors;
27 import java.util.logging.ConsoleHandler;
28 import java.util.logging.Formatter;
29 import java.util.logging.Level;
30 import java.util.logging.LogRecord;
31 import java.util.logging.Logger;
32
33 import net.pterodactylus.irc.Connection;
34 import net.pterodactylus.xdcc.core.Core;
35 import net.pterodactylus.xdcc.data.Channel;
36 import net.pterodactylus.xdcc.data.Download;
37 import net.pterodactylus.xdcc.data.Network;
38 import net.pterodactylus.xdcc.data.Network.NetworkBuilder;
39 import net.pterodactylus.xdcc.data.Network.ServerBuilder;
40 import net.pterodactylus.xdcc.ui.stdin.CommandReader;
41 import net.pterodactylus.xdcc.ui.stdin.NetworkAdapter;
42
43 import com.google.common.eventbus.AsyncEventBus;
44 import com.google.common.eventbus.EventBus;
45 import com.lexicalscope.jewel.cli.Option;
46 import org.codehaus.jackson.map.ObjectMapper;
47
48 /**
49  * Xudocci main class.
50  *
51  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
52  */
53 public class Main {
54
55         private static final String VERSION = "0.1-SNAPSHOT";
56
57         public static String version() {
58                 return VERSION;
59         }
60
61         public static void main(String... arguments) throws Exception {
62
63                 ConsoleHandler consoleHandler = new ConsoleHandler();
64                 consoleHandler.setFormatter(new Formatter() {
65
66                         @Override
67                         public String format(LogRecord logRecord) {
68                                 return String.format("%1$tF %1$tT %2$s %3$s\n", logRecord.getMillis(), logRecord.getLoggerName(), logRecord.getMessage());
69                         }
70                 });
71                 consoleHandler.setLevel(Level.ALL);
72                 Logger.getLogger("").removeHandler(Logger.getLogger("").getHandlers()[0]);
73                 Logger.getLogger("").addHandler(consoleHandler);
74                 Logger.getLogger("").setLevel(Level.ALL);
75                 Logger.getLogger(Connection.class.getName()).setLevel(Level.INFO);
76                 Logger.getLogger(Core.class.getName()).setLevel(Level.INFO);
77
78                 Parameters parameters = parseArguments(Parameters.class, arguments);
79
80                 EventBus eventBus = new AsyncEventBus(Executors.newSingleThreadExecutor());
81                 Configuration configuration = new ObjectMapper().readValue(new File(parameters.getConfiguration()), Configuration.class);
82
83                 Core core = new Core(eventBus, configuration.getTemporaryDirectory(), configuration.getFinalDirectory());
84                 eventBus.register(core);
85
86                 for (Configuration.Network network : configuration.getNetworks()) {
87                         NetworkBuilder networkBuilder = Network.builder(network.getName());
88                         for (Configuration.Network.Server server : network.getServers()) {
89                                 ServerBuilder serverBuilder = networkBuilder.addServer();
90                                 serverBuilder.at(server.getHostname());
91                                 for (int unencryptedPort : server.getUnencryptedPorts()) {
92                                         serverBuilder.port(unencryptedPort);
93                                 }
94                                 for (int encryptedPort : server.getEncryptedPorts()) {
95                                         serverBuilder.sslPort(encryptedPort);
96                                 }
97                                 serverBuilder.endServer();
98                         }
99                         Network createdNetwork = networkBuilder.build();
100                         for (String channel : network.getChannels()) {
101                                 core.addChannel(new Channel(createdNetwork, channel));
102                         }
103                 }
104
105                 Collection<Download> failedDownloads = new CopyOnWriteArraySet<>();
106
107                 NetworkAdapter networkAcceptor = new NetworkAdapter(eventBus, (reader, writer) -> new CommandReader(core, reader, writer, failedDownloads), configuration.getTelnetPort());
108                 networkAcceptor.start();
109
110                 core.start();
111         }
112
113         /**
114          * Defines the command-line parameters.
115          *
116          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
117          */
118         private interface Parameters {
119
120                 @Option(longName = "configuration", description = "The configuration file", defaultValue = "configuration.json")
121                 String getConfiguration();
122
123         }
124
125 }