🚧 Add event for first start of Sone
[Sone.git] / src / main / java / net / pterodactylus / sone / main / SonePlugin.java
1 /*
2  * Sone - SonePlugin.java - Copyright Â© 2010–2019 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.sone.main;
19
20 import static java.util.logging.Logger.*;
21
22 import java.util.logging.Logger;
23 import java.util.logging.*;
24
25 import net.pterodactylus.sone.core.*;
26 import net.pterodactylus.sone.core.event.*;
27 import net.pterodactylus.sone.fcp.*;
28 import net.pterodactylus.sone.freenet.wot.*;
29 import net.pterodactylus.sone.web.*;
30 import net.pterodactylus.sone.web.notification.NotificationHandler;
31 import net.pterodactylus.sone.web.notification.NotificationHandlerModule;
32
33 import freenet.l10n.BaseL10n.*;
34 import freenet.l10n.*;
35 import freenet.pluginmanager.*;
36 import freenet.support.*;
37 import freenet.support.api.*;
38
39 import com.google.common.annotations.*;
40 import com.google.common.eventbus.*;
41 import com.google.common.cache.*;
42 import com.google.inject.*;
43 import com.google.inject.Module;
44 import com.google.inject.name.*;
45 import kotlin.jvm.functions.*;
46
47 /**
48  * This class interfaces with Freenet. It is the class that is loaded by the
49  * node and starts up the whole Sone system.
50  */
51 public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, FredPluginBaseL10n, FredPluginThreadless, FredPluginVersioned {
52
53         private static final Logger soneLogger = getLogger("net.pterodactylus.sone");
54
55         static {
56                 /* initialize logging. */
57                 soneLogger.setUseParentHandlers(false);
58                 soneLogger.setLevel(Level.ALL);
59                 soneLogger.addHandler(new Handler() {
60                         private final LoadingCache<String, Class<?>> classCache = CacheBuilder.newBuilder()
61                                         .build(new CacheLoader<String, Class<?>>() {
62                                                 @Override
63                                                 public Class<?> load(String key) throws Exception {
64                                                         return SonePlugin.class.getClassLoader().loadClass(key);
65                                                 }
66                                         });
67
68                         @Override
69                         public void publish(LogRecord logRecord) {
70                                 int recordLevel = logRecord.getLevel().intValue();
71                                 Class<?> loggingClass = classCache.getUnchecked(logRecord.getLoggerName());
72                                 if (recordLevel < Level.FINE.intValue()) {
73                                         freenet.support.Logger.debug(loggingClass, logRecord.getMessage(), logRecord.getThrown());
74                                 } else if (recordLevel < Level.INFO.intValue()) {
75                                         freenet.support.Logger.minor(loggingClass, logRecord.getMessage(), logRecord.getThrown());
76                                 } else if (recordLevel < Level.WARNING.intValue()) {
77                                         freenet.support.Logger.normal(loggingClass, logRecord.getMessage(), logRecord.getThrown());
78                                 } else if (recordLevel < Level.SEVERE.intValue()) {
79                                         freenet.support.Logger.warning(loggingClass, logRecord.getMessage(), logRecord.getThrown());
80                                 } else {
81                                         freenet.support.Logger.error(loggingClass, logRecord.getMessage(), logRecord.getThrown());
82                                 }
83                         }
84
85                         @Override
86                         public void flush() {
87                         }
88
89                         @Override
90                         public void close() {
91                         }
92                 });
93         }
94
95         /** The current year at time of release. */
96         private static final int YEAR = 2019;
97         private static final String SONE_HOMEPAGE = "USK@nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI,DuQSUZiI~agF8c-6tjsFFGuZ8eICrzWCILB60nT8KKo,AQACAAE/sone/";
98         private static final int LATEST_EDITION = 79;
99
100         /** The logger. */
101         private static final Logger logger = getLogger(SonePlugin.class.getName());
102
103         private final Function1<Module[], Injector> injectorCreator;
104
105         /** The plugin respirator. */
106         private PluginRespirator pluginRespirator;
107
108         /** The core. */
109         private Core core;
110
111         /** The web interface. */
112         private WebInterface webInterface;
113
114         /** The FCP interface. */
115         private FcpInterface fcpInterface;
116
117         /** The l10n helper. */
118         private PluginL10n l10n;
119
120         /** The web of trust connector. */
121         private WebOfTrustConnector webOfTrustConnector;
122
123         public SonePlugin() {
124                 this(Guice::createInjector);
125         }
126
127         @VisibleForTesting
128         public SonePlugin(Function1<Module[], Injector> injectorCreator) {
129                 this.injectorCreator = injectorCreator;
130         }
131
132         //
133         // ACCESSORS
134         //
135
136         /**
137          * Returns the plugin respirator for this plugin.
138          *
139          * @return The plugin respirator
140          */
141         public PluginRespirator pluginRespirator() {
142                 return pluginRespirator;
143         }
144
145         /**
146          * Returns the core started by this plugin.
147          *
148          * @return The core
149          */
150         public Core core() {
151                 return core;
152         }
153
154         /**
155          * Returns the plugin’s l10n helper.
156          *
157          * @return The plugin’s l10n helper
158          */
159         public PluginL10n l10n() {
160                 return l10n;
161         }
162
163         public static String getPluginVersion() {
164                 net.pterodactylus.sone.main.Version version = VersionParserKt.getParsedVersion();
165                 return (version == null) ? "unknown" : version.getNice();
166         }
167
168         public int getYear() {
169                 return YEAR;
170         }
171
172         public String getHomepage() {
173                 return SONE_HOMEPAGE + LATEST_EDITION;
174         }
175
176         public static long getLatestEdition() {
177                 return LATEST_EDITION;
178         }
179
180         //
181         // FREDPLUGIN METHODS
182         //
183
184         /**
185          * {@inheritDoc}
186          */
187         @Override
188         public void runPlugin(PluginRespirator pluginRespirator) {
189                 this.pluginRespirator = pluginRespirator;
190
191                 Injector injector = createInjector();
192                 core = injector.getInstance(Core.class);
193
194                 /* create web of trust connector. */
195                 webOfTrustConnector = injector.getInstance(WebOfTrustConnector.class);
196
197                 /* create FCP interface. */
198                 fcpInterface = injector.getInstance(FcpInterface.class);
199
200                 /* create the web interface. */
201                 webInterface = injector.getInstance(WebInterface.class);
202
203                 /* we need to request this to install all notification handlers. */
204                 injector.getInstance(NotificationHandler.class);
205
206                 /* start core! */
207                 core.start();
208
209                 /* start the web interface! */
210                 webInterface.start();
211                 webInterface.setFirstStart(injector.getInstance(Key.get(Boolean.class, Names.named("FirstStart"))));
212                 webInterface.setNewConfig(injector.getInstance(Key.get(Boolean.class, Names.named("NewConfig"))));
213
214                 /* first start? */
215                 if (injector.getInstance(Key.get(Boolean.class, Names.named("FirstStart")))) {
216                         injector.getInstance(EventBus.class).post(new FirstStart());
217                 }
218         }
219
220         @VisibleForTesting
221         protected Injector createInjector() {
222                 FreenetModule freenetModule = new FreenetModule(pluginRespirator);
223                 AbstractModule soneModule = new SoneModule(this, new EventBus());
224                 Module webInterfaceModule = new WebInterfaceModule();
225                 Module notificationHandlerModule = new NotificationHandlerModule();
226
227                 return createInjector(freenetModule, soneModule, webInterfaceModule, notificationHandlerModule);
228         }
229
230         @VisibleForTesting
231         protected Injector createInjector(Module... modules) {
232                 return injectorCreator.invoke(modules);
233         }
234
235         /**
236          * {@inheritDoc}
237          */
238         @Override
239         public void terminate() {
240                 try {
241                         /* stop the web interface. */
242                         webInterface.stop();
243
244                         /* stop the core. */
245                         core.stop();
246
247                         /* stop the web of trust connector. */
248                         webOfTrustConnector.stop();
249                 } catch (Throwable t1) {
250                         logger.log(Level.SEVERE, "Error while shutting down!", t1);
251                 } finally {
252                         deregisterLoggerHandlers();
253                 }
254         }
255
256         private void deregisterLoggerHandlers() {
257                 for (Handler handler : soneLogger.getHandlers()) {
258                         soneLogger.removeHandler(handler);
259                 }
260         }
261
262         //
263         // INTERFACE FredPluginFCP
264         //
265
266         /**
267          * {@inheritDoc}
268          */
269         @Override
270         public void handle(PluginReplySender pluginReplySender, SimpleFieldSet parameters, Bucket data, int accessType) {
271                 fcpInterface.handle(pluginReplySender, parameters, data, accessType);
272         }
273
274         //
275         // INTERFACE FredPluginL10n
276         //
277
278         /**
279          * {@inheritDoc}
280          */
281         @Override
282         public String getString(String key) {
283                 return l10n.getBase().getString(key);
284         }
285
286         /**
287          * {@inheritDoc}
288          */
289         @Override
290         public void setLanguage(LANGUAGE newLanguage) {
291                 l10n = new PluginL10n(this, newLanguage);
292         }
293
294         //
295         // INTERFACE FredPluginBaseL10n
296         //
297
298         /**
299          * {@inheritDoc}
300          */
301         @Override
302         public String getL10nFilesBasePath() {
303                 return "i18n";
304         }
305
306         /**
307          * {@inheritDoc}
308          */
309         @Override
310         public String getL10nFilesMask() {
311                 return "sone.${lang}.properties";
312         }
313
314         /**
315          * {@inheritDoc}
316          */
317         @Override
318         public String getL10nOverrideFilesMask() {
319                 return "sone.${lang}.override.properties";
320         }
321
322         /**
323          * {@inheritDoc}
324          */
325         @Override
326         public ClassLoader getPluginClassLoader() {
327                 return SonePlugin.class.getClassLoader();
328         }
329
330         //
331         // INTERFACE FredPluginVersioned
332         //
333
334         /**
335          * {@inheritDoc}
336          */
337         @Override
338         public String getVersion() {
339                 return getPluginVersion();
340         }
341
342 }