You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LoggingManager.java 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.logging;
  23. import com.dmdirc.Query;
  24. import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
  25. import com.dmdirc.config.GlobalConfig;
  26. import com.dmdirc.config.prefs.PluginPreferencesCategory;
  27. import com.dmdirc.config.prefs.PreferencesCategory;
  28. import com.dmdirc.config.prefs.PreferencesDialogModel;
  29. import com.dmdirc.config.prefs.PreferencesSetting;
  30. import com.dmdirc.config.prefs.PreferencesType;
  31. import com.dmdirc.events.BaseChannelActionEvent;
  32. import com.dmdirc.events.BaseChannelMessageEvent;
  33. import com.dmdirc.events.BaseQueryActionEvent;
  34. import com.dmdirc.events.BaseQueryMessageEvent;
  35. import com.dmdirc.events.ChannelClosedEvent;
  36. import com.dmdirc.events.ChannelGotTopicEvent;
  37. import com.dmdirc.events.ChannelJoinEvent;
  38. import com.dmdirc.events.ChannelKickEvent;
  39. import com.dmdirc.events.ChannelModeChangeEvent;
  40. import com.dmdirc.events.ChannelNickChangeEvent;
  41. import com.dmdirc.events.ChannelOpenedEvent;
  42. import com.dmdirc.events.ChannelPartEvent;
  43. import com.dmdirc.events.ChannelQuitEvent;
  44. import com.dmdirc.events.ChannelTopicChangeEvent;
  45. import com.dmdirc.events.ClientPrefsOpenedEvent;
  46. import com.dmdirc.events.QueryClosedEvent;
  47. import com.dmdirc.events.QueryOpenedEvent;
  48. import com.dmdirc.interfaces.EventBus;
  49. import com.dmdirc.interfaces.GroupChat;
  50. import com.dmdirc.interfaces.GroupChatUser;
  51. import com.dmdirc.interfaces.PrivateChat;
  52. import com.dmdirc.interfaces.User;
  53. import com.dmdirc.interfaces.WindowModel;
  54. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  55. import com.dmdirc.interfaces.config.ConfigChangeListener;
  56. import com.dmdirc.plugins.PluginDomain;
  57. import com.dmdirc.plugins.PluginInfo;
  58. import com.dmdirc.ui.WindowManager;
  59. import com.dmdirc.ui.messages.BackBufferFactory;
  60. import com.dmdirc.ui.messages.IRCControlCodes;
  61. import com.dmdirc.ui.messages.StyledMessageUtils;
  62. import com.dmdirc.ui.messages.Styliser;
  63. import com.dmdirc.util.io.ReverseFileReader;
  64. import com.dmdirc.util.io.StreamUtils;
  65. import java.awt.Color;
  66. import java.io.BufferedWriter;
  67. import java.io.File;
  68. import java.io.FileWriter;
  69. import java.io.IOException;
  70. import java.nio.file.Files;
  71. import java.nio.file.Path;
  72. import java.nio.file.Paths;
  73. import java.text.DateFormat;
  74. import java.text.SimpleDateFormat;
  75. import java.time.format.DateTimeFormatter;
  76. import java.util.ArrayList;
  77. import java.util.Collection;
  78. import java.util.Collections;
  79. import java.util.Date;
  80. import java.util.HashMap;
  81. import java.util.Map;
  82. import java.util.Stack;
  83. import java.util.Timer;
  84. import java.util.TimerTask;
  85. import javax.inject.Inject;
  86. import javax.inject.Provider;
  87. import javax.inject.Singleton;
  88. import net.engio.mbassy.listener.Handler;
  89. import org.slf4j.Logger;
  90. import org.slf4j.LoggerFactory;
  91. import static com.dmdirc.util.LogUtils.USER_ERROR;
  92. /**
  93. * Manages logging activities.
  94. */
  95. @Singleton
  96. public class LoggingManager implements ConfigChangeListener {
  97. private static final Logger LOG = LoggerFactory.getLogger(LoggingManager.class);
  98. /** Date format used for "File Opened At" log. */
  99. private static final DateFormat OPENED_AT_FORMAT = new SimpleDateFormat(
  100. "EEEE MMMM dd, yyyy - HH:mm:ss");
  101. /** Object for synchronising access to the date forma.t */
  102. private static final Object FORMAT_LOCK = new Object();
  103. private static final DateFormat LOG_FORMAT = new SimpleDateFormat("[dd/MM/yyyy HH:mm:ss]");
  104. /** This plugin's plugin info. */
  105. private final String domain;
  106. private final PluginInfo pluginInfo;
  107. /** Global config. */
  108. private final AggregateConfigProvider config;
  109. /** The manager to add history windows to. */
  110. private final WindowManager windowManager;
  111. /** Map of open files. */
  112. private final Map<String, OpenFile> openFiles = Collections.synchronizedMap(new HashMap<>());
  113. private final EventBus eventBus;
  114. private final Provider<String> directoryProvider;
  115. private final BackBufferFactory backBufferFactory;
  116. private final LogFileLocator locator;
  117. private final StyledMessageUtils styleUtils;
  118. /** Timer used to close idle files. */
  119. private Timer idleFileTimer;
  120. /** Cached boolean settings. */
  121. private boolean addtime;
  122. private boolean stripcodes;
  123. private boolean channelmodeprefix;
  124. private boolean autobackbuffer;
  125. private String colour;
  126. /** Cached int settings. */
  127. private int historyLines;
  128. private int backbufferLines;
  129. @Inject
  130. public LoggingManager(
  131. @PluginDomain(LoggingPlugin.class) final String domain,
  132. @PluginDomain(LoggingPlugin.class) final PluginInfo pluginInfo,
  133. @GlobalConfig final AggregateConfigProvider globalConfig,
  134. final WindowManager windowManager, final EventBus eventBus,
  135. @Directory(LoggingModule.LOGS_DIRECTORY) final Provider<String> directoryProvider,
  136. final BackBufferFactory backBufferFactory,
  137. final LogFileLocator locator,
  138. final StyledMessageUtils styleUtils) {
  139. this.domain = domain;
  140. this.pluginInfo = pluginInfo;
  141. this.config = globalConfig;
  142. this.windowManager = windowManager;
  143. this.eventBus = eventBus;
  144. this.directoryProvider = directoryProvider;
  145. this.backBufferFactory = backBufferFactory;
  146. this.locator = locator;
  147. this.styleUtils = styleUtils;
  148. }
  149. public void load() {
  150. setCachedSettings();
  151. final File dir = new File(directoryProvider.get());
  152. if (dir.exists()) {
  153. if (!dir.isDirectory()) {
  154. LOG.info(USER_ERROR, "Unable to create logging dir (file exists instead)");
  155. }
  156. } else {
  157. if (!dir.mkdirs()) {
  158. LOG.info(USER_ERROR, "Unable to create logging dir");
  159. }
  160. }
  161. config.addChangeListener(domain, this);
  162. // Close idle files every hour.
  163. idleFileTimer = new Timer("LoggingPlugin Timer");
  164. idleFileTimer.schedule(new TimerTask() {
  165. @Override
  166. public void run() {
  167. timerTask();
  168. }
  169. }, 3600000);
  170. eventBus.subscribe(this);
  171. }
  172. public void unload() {
  173. if (idleFileTimer != null) {
  174. idleFileTimer.cancel();
  175. idleFileTimer.purge();
  176. }
  177. synchronized (openFiles) {
  178. for (OpenFile file : openFiles.values()) {
  179. StreamUtils.close(file.writer);
  180. }
  181. openFiles.clear();
  182. }
  183. eventBus.unsubscribe(this);
  184. }
  185. /**
  186. * What to do every hour when the timer fires.
  187. */
  188. protected void timerTask() {
  189. // Oldest time to allow
  190. final long oldestTime = System.currentTimeMillis() - 3480000;
  191. synchronized (openFiles) {
  192. final Collection<String> old = new ArrayList<>(openFiles.size());
  193. openFiles.entrySet().stream()
  194. .filter(entry -> entry.getValue().lastUsedTime < oldestTime)
  195. .forEach(entry -> {
  196. StreamUtils.close(entry.getValue().writer);
  197. old.add(entry.getKey());
  198. });
  199. openFiles.keySet().removeAll(old);
  200. }
  201. }
  202. @Handler
  203. public void handleQueryOpened(final QueryOpenedEvent event) {
  204. final String filename = locator.getLogFile(event.getQuery().getUser());
  205. if (autobackbuffer) {
  206. showBackBuffer(event.getQuery().getWindowModel(), filename);
  207. }
  208. synchronized (FORMAT_LOCK) {
  209. appendLine(filename, "*** Query opened at: %s", OPENED_AT_FORMAT.format(new Date()));
  210. appendLine(filename, "*** Query with User: %s", event.getQuery().getUser().getNickname()
  211. + '!' + event.getQuery().getUser().getUsername().orElse("")
  212. + '@' + event.getQuery().getUser().getHostname().orElse(""));
  213. appendLine(filename, "");
  214. }
  215. }
  216. @Handler
  217. public void handleQueryClosed(final QueryClosedEvent event) {
  218. final String filename = locator.getLogFile(event.getQuery().getUser());
  219. synchronized (FORMAT_LOCK) {
  220. appendLine(filename, "*** Query closed at: %s", OPENED_AT_FORMAT.format(new Date()));
  221. }
  222. if (openFiles.containsKey(filename)) {
  223. StreamUtils.close(openFiles.get(filename).writer);
  224. openFiles.remove(filename);
  225. }
  226. }
  227. @Handler
  228. public void handleQueryActions(final BaseQueryActionEvent event) {
  229. final User user = event.getQuery().getUser();
  230. final String filename = locator.getLogFile(user);
  231. appendLine(filename, "* %s %s", user.getNickname(), event.getMessage());
  232. }
  233. @Handler
  234. public void handleQueryMessages(final BaseQueryMessageEvent event) {
  235. final User user = event.getQuery().getUser();
  236. final String filename = locator.getLogFile(user);
  237. appendLine(filename, "<%s> %s", user.getNickname(), event.getMessage());
  238. }
  239. @Handler
  240. public void handleChannelMessage(final BaseChannelMessageEvent event) {
  241. final String filename = locator.getLogFile(event.getChannel());
  242. appendLine(filename, "<%s> %s", getDisplayName(event.getClient()), event.getMessage());
  243. }
  244. @Handler
  245. public void handleChannelAction(final BaseChannelActionEvent event) {
  246. final String filename = locator.getLogFile(event.getChannel());
  247. appendLine(filename, "* %s %s", getDisplayName(event.getClient()), event.getMessage());
  248. }
  249. @Handler
  250. public void handleChannelGotTopic(final ChannelGotTopicEvent event) {
  251. final String filename = locator.getLogFile(event.getChannel());
  252. appendLine(filename, "*** Topic is: %s", event.getTopic().getTopic());
  253. appendLine(filename, "*** Set at: %s on %s by %s",
  254. event.getTopic().getDate().format(DateTimeFormatter.ofPattern("HH:mm:ss")),
  255. event.getTopic().getDate().format(DateTimeFormatter.ofPattern("dd/MM/yyyy")),
  256. event.getTopic().getClient()
  257. .map(GroupChatUser::getNickname).orElse("Unknown"));
  258. }
  259. @Handler
  260. public void handleChannelTopicChange(final ChannelTopicChangeEvent event) {
  261. final String filename = locator.getLogFile(event.getChannel());
  262. appendLine(filename, "*** %s Changed the topic to: %s",
  263. event.getTopic().getClient().map(this::getDisplayName).orElse(""), event.getTopic());
  264. }
  265. @Handler
  266. public void handleChannelJoin(final ChannelJoinEvent event) {
  267. final String filename = locator.getLogFile(event.getChannel());
  268. final GroupChatUser channelClient = event.getClient();
  269. appendLine(filename, "*** %s (%s) joined the channel", getDisplayName(channelClient),
  270. channelClient.getNickname());
  271. }
  272. @Handler
  273. public void handleChannelPart(final ChannelPartEvent event) {
  274. final String filename = locator.getLogFile(event.getChannel());
  275. final String message = event.getMessage();
  276. final GroupChatUser channelClient = event.getClient();
  277. if (message.isEmpty()) {
  278. appendLine(filename, "*** %s (%s) left the channel", getDisplayName(channelClient),
  279. channelClient.getNickname());
  280. } else {
  281. appendLine(filename, "*** %s (%s) left the channel (%s)",
  282. getDisplayName(channelClient), channelClient.getNickname(), message);
  283. }
  284. }
  285. @Handler
  286. public void handleChannelQuit(final ChannelQuitEvent event) {
  287. final String filename = locator.getLogFile(event.getChannel());
  288. final String reason = event.getMessage();
  289. final GroupChatUser channelClient = event.getClient();
  290. if (reason.isEmpty()) {
  291. appendLine(filename, "*** %s (%s) Quit IRC",
  292. getDisplayName(channelClient), channelClient.getNickname());
  293. } else {
  294. appendLine(filename, "*** %s (%s) Quit IRC (%s)",
  295. getDisplayName(channelClient), channelClient.getNickname(), reason);
  296. }
  297. }
  298. @Handler
  299. public void handleChannelKick(final ChannelKickEvent event) {
  300. final GroupChatUser victim = event.getVictim();
  301. final GroupChatUser perpetrator = event.getClient();
  302. final String reason = event.getReason();
  303. final String filename = locator.getLogFile(event.getChannel());
  304. if (reason.isEmpty()) {
  305. appendLine(filename, "*** %s was kicked by %s",
  306. getDisplayName(victim), getDisplayName(perpetrator));
  307. } else {
  308. appendLine(filename, "*** %s was kicked by %s (%s)",
  309. getDisplayName(victim), getDisplayName(perpetrator), reason);
  310. }
  311. }
  312. @Handler
  313. public void handleNickChange(final ChannelNickChangeEvent event) {
  314. final String filename = locator.getLogFile(event.getChannel());
  315. appendLine(filename, "*** %s is now %s", getDisplayName(event.getClient(),
  316. event.getOldNick()), getDisplayName(event.getClient()));
  317. }
  318. @Handler
  319. public void handleModeChange(final ChannelModeChangeEvent event) {
  320. final String filename = locator.getLogFile(event.getChannel());
  321. if (event.getClient().getNickname().isEmpty()) {
  322. appendLine(filename, "*** Channel modes are: %s", event.getModes());
  323. } else {
  324. appendLine(filename, "*** %s set modes: %s",
  325. getDisplayName(event.getClient()), event.getModes());
  326. }
  327. }
  328. @Override
  329. public void configChanged(final String domain, final String key) {
  330. setCachedSettings();
  331. }
  332. @Handler
  333. public void handleChannelOpened(final ChannelOpenedEvent event) {
  334. final String filename = locator.getLogFile(event.getChannel().getName());
  335. if (autobackbuffer) {
  336. showBackBuffer(event.getChannel().getWindowModel(), filename);
  337. }
  338. synchronized (FORMAT_LOCK) {
  339. appendLine(filename, "*** Channel opened at: %s", OPENED_AT_FORMAT.format(new Date()));
  340. appendLine(filename, "");
  341. }
  342. }
  343. @Handler
  344. public void handleChannelClosed(final ChannelClosedEvent event) {
  345. final String filename = locator.getLogFile(event.getChannel().getName());
  346. synchronized (FORMAT_LOCK) {
  347. appendLine(filename, "*** Channel closed at: %s", OPENED_AT_FORMAT.format(new Date()));
  348. }
  349. if (openFiles.containsKey(filename)) {
  350. StreamUtils.close(openFiles.get(filename).writer);
  351. openFiles.remove(filename);
  352. }
  353. }
  354. /**
  355. * Add a backbuffer to a frame.
  356. *
  357. * @param frame The frame to add the backbuffer lines to
  358. * @param filename File to get backbuffer from
  359. */
  360. protected void showBackBuffer(final WindowModel frame, final String filename) {
  361. if (frame == null) {
  362. LOG.info(USER_ERROR, "Unable to show back buffer, frame was null");
  363. return;
  364. }
  365. final Path testFile = Paths.get(filename);
  366. if (Files.exists(testFile)) {
  367. try (final ReverseFileReader file = new ReverseFileReader(testFile)) {
  368. // Because the file includes a newline char at the end, an empty line
  369. // is returned by getLines. To counter this, we call getLines(1) and do
  370. // nothing with the output.
  371. file.getLines(1);
  372. final Stack<String> lines = file.getLines(backbufferLines);
  373. while (!lines.empty()) {
  374. frame.getEventBus().publishAsync(new HistoricalLineRestoredEvent(frame,
  375. getColouredString(colour, lines.pop())));
  376. }
  377. file.close();
  378. frame.getEventBus().publishAsync(new HistoricalLineRestoredEvent(frame,
  379. getColouredString(colour, "--- End of backbuffer\n")));
  380. } catch (IOException | SecurityException e) {
  381. LOG.info(USER_ERROR, "Unable to show backbuffer (Filename: {}): {}", filename,
  382. e.getMessage(), e);
  383. }
  384. }
  385. }
  386. /**
  387. * Get a coloured String. If colour is invalid, IRC Colour 14 will be used.
  388. *
  389. * @param colour The colour the string should be (IRC Colour or 6-digit hex colour)
  390. * @param line the line to colour
  391. *
  392. * @return The given line with the appropriate irc codes appended/prepended to colour it.
  393. */
  394. protected static String getColouredString(final String colour, final String line) {
  395. String res = null;
  396. if (colour.length() < 3) {
  397. int num;
  398. try {
  399. num = Integer.parseInt(colour);
  400. } catch (NumberFormatException ex) {
  401. num = -1;
  402. }
  403. if (num >= 0 && num <= 15) {
  404. res = String.format("%c%02d%s%1$c", IRCControlCodes.COLOUR, num, line);
  405. }
  406. } else if (colour.length() == 6) {
  407. try {
  408. Color.decode('#' + colour);
  409. res = String.format("%c%s%s%1$c", IRCControlCodes.COLOUR_HEX, colour, line);
  410. } catch (NumberFormatException ex) { /* Do Nothing */ }
  411. }
  412. if (res == null) {
  413. res = String.format("%c%02d%s%1$c", IRCControlCodes.COLOUR, 14, line);
  414. }
  415. return res;
  416. }
  417. /**
  418. * Add a line to a file.
  419. *
  420. * @param filename Name of file to write to
  421. * @param format Format of line to add. (NewLine will be added Automatically)
  422. * @param args Arguments for format
  423. *
  424. * @return true on success, else false.
  425. */
  426. protected boolean appendLine(final String filename, final String format, final Object... args) {
  427. return appendLine(filename, String.format(format, args));
  428. }
  429. /**
  430. * Add a line to a file.
  431. *
  432. * @param filename Name of file to write to
  433. * @param line Line to add. (NewLine will be added Automatically)
  434. *
  435. * @return true on success, else false.
  436. */
  437. protected boolean appendLine(final String filename, final String line) {
  438. final StringBuilder finalLine = new StringBuilder();
  439. if (addtime) {
  440. final String dateString = LOG_FORMAT.format(new Date()).trim();
  441. finalLine.append(dateString);
  442. finalLine.append(' ');
  443. }
  444. if (stripcodes) {
  445. finalLine.append(styleUtils.stripControlCodes(line));
  446. } else {
  447. finalLine.append(line);
  448. }
  449. try {
  450. final BufferedWriter out;
  451. if (openFiles.containsKey(filename)) {
  452. final OpenFile of = openFiles.get(filename);
  453. of.lastUsedTime = System.currentTimeMillis();
  454. out = of.writer;
  455. } else {
  456. out = new BufferedWriter(new FileWriter(filename, true));
  457. openFiles.put(filename, new OpenFile(out));
  458. }
  459. out.write(finalLine.toString());
  460. out.newLine();
  461. out.flush();
  462. return true;
  463. } catch (IOException e) {
  464. /*
  465. * Do Nothing
  466. *
  467. * Makes no sense to keep adding errors to the logger when we can't write to the file,
  468. * as chances are it will happen on every incoming line.
  469. */
  470. }
  471. return false;
  472. }
  473. /**
  474. * Get name to display for channelClient (Taking into account the channelmodeprefix setting).
  475. *
  476. * @param channelClient The client to get the display name for
  477. *
  478. * @return name to display
  479. */
  480. protected String getDisplayName(final GroupChatUser channelClient) {
  481. return getDisplayName(channelClient, "");
  482. }
  483. /**
  484. * Get name to display for channelClient (Taking into account the channelmodeprefix setting).
  485. *
  486. * @param channelClient The client to get the display name for
  487. * @param overrideNick Nickname to display instead of real nickname
  488. *
  489. * @return name to display
  490. */
  491. protected String getDisplayName(final GroupChatUser channelClient, final String overrideNick) {
  492. if (channelClient == null) {
  493. return overrideNick.isEmpty() ? "Unknown Client" : overrideNick;
  494. } else if (overrideNick.isEmpty()) {
  495. return channelmodeprefix ? channelClient.getModePrefixedNickname()
  496. : channelClient.getNickname();
  497. } else {
  498. return channelmodeprefix ? channelClient.getImportantMode() + overrideNick :
  499. overrideNick;
  500. }
  501. }
  502. /**
  503. * Shows the history window for the specified target, if available.
  504. *
  505. * @param target The window whose history we're trying to open
  506. *
  507. * @return True if the history is available, false otherwise
  508. */
  509. protected boolean showHistory(final WindowModel target) {
  510. final String descriptor;
  511. if (target instanceof GroupChat) {
  512. descriptor = target.getName();
  513. } else if (target instanceof Query) {
  514. descriptor = ((PrivateChat) target).getNickname();
  515. } else {
  516. // Unknown component
  517. return false;
  518. }
  519. final Path log = Paths.get(locator.getLogFile(descriptor));
  520. if (!Files.exists(log)) {
  521. // File doesn't exist
  522. return false;
  523. }
  524. windowManager.addWindow(target, new HistoryWindow("History", log, target,
  525. eventBus, backBufferFactory, historyLines));
  526. return true;
  527. }
  528. /** Updates cached settings. */
  529. public void setCachedSettings() {
  530. addtime = config.getOptionBool(domain, "general.addtime");
  531. stripcodes = config.getOptionBool(domain, "general.stripcodes");
  532. channelmodeprefix = config.getOptionBool(domain, "general.channelmodeprefix");
  533. autobackbuffer = config.getOptionBool(domain, "backbuffer.autobackbuffer");
  534. historyLines = config.getOptionInt(domain, "history.lines");
  535. colour = config.getOption(domain, "backbuffer.colour");
  536. backbufferLines = config.getOptionInt(domain, "backbuffer.lines");
  537. }
  538. @Handler
  539. public void showConfig(final ClientPrefsOpenedEvent event) {
  540. final PreferencesDialogModel manager = event.getModel();
  541. final PreferencesCategory general = new PluginPreferencesCategory(
  542. pluginInfo, "Logging", "General configuration for Logging plugin.");
  543. final PreferencesCategory backbuffer = new PluginPreferencesCategory(
  544. pluginInfo, "Back Buffer", "Options related to the automatic backbuffer");
  545. final PreferencesCategory advanced = new PluginPreferencesCategory(
  546. pluginInfo, "Advanced",
  547. "Advanced configuration for Logging plugin. You shouldn't need to edit this unless you know what you are doing.");
  548. general.addSetting(new PreferencesSetting(PreferencesType.DIRECTORY,
  549. pluginInfo.getDomain(), "general.directory", "Directory",
  550. "Directory for log files", manager.getConfigManager(),
  551. manager.getIdentity()));
  552. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  553. pluginInfo.getDomain(), "general.networkfolders",
  554. "Separate logs by network",
  555. "Should the files be stored in a sub-dir with the networks name?",
  556. manager.getConfigManager(), manager.getIdentity()));
  557. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  558. pluginInfo.getDomain(), "general.addtime", "Timestamp logs",
  559. "Should a timestamp be added to the log files?",
  560. manager.getConfigManager(), manager.getIdentity()));
  561. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  562. pluginInfo.getDomain(), "general.stripcodes", "Strip Control Codes",
  563. "Remove known irc control codes from lines before saving?",
  564. manager.getConfigManager(), manager.getIdentity()));
  565. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  566. pluginInfo.getDomain(), "general.channelmodeprefix",
  567. "Show channel mode prefix", "Show the @,+ etc next to nicknames",
  568. manager.getConfigManager(), manager.getIdentity()));
  569. backbuffer.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  570. pluginInfo.getDomain(), "backbuffer.autobackbuffer", "Automatically display",
  571. "Automatically display the backbuffer when a channel is joined",
  572. manager.getConfigManager(), manager.getIdentity()));
  573. backbuffer.addSetting(new PreferencesSetting(PreferencesType.COLOUR,
  574. pluginInfo.getDomain(), "backbuffer.colour", "Colour to use for display",
  575. "Colour used when displaying the backbuffer",
  576. manager.getConfigManager(), manager.getIdentity()));
  577. backbuffer.addSetting(new PreferencesSetting(PreferencesType.INTEGER,
  578. pluginInfo.getDomain(), "backbuffer.lines", "Number of lines to show",
  579. "Number of lines used when displaying backbuffer",
  580. manager.getConfigManager(), manager.getIdentity()));
  581. advanced.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  582. pluginInfo.getDomain(), "advanced.filenamehash", "Add Filename hash",
  583. "Add the MD5 hash of the channel/client name to the filename. "
  584. + "(This is used to allow channels with similar names "
  585. + "(ie a _ not a -) to be logged separately)",
  586. manager.getConfigManager(), manager.getIdentity()));
  587. advanced.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  588. pluginInfo.getDomain(), "advanced.usedate", "Use Date directories",
  589. "Should the log files be in separate directories based on the date?",
  590. manager.getConfigManager(), manager.getIdentity()));
  591. advanced.addSetting(new PreferencesSetting(PreferencesType.TEXT,
  592. pluginInfo.getDomain(), "advanced.usedateformat", "Archive format",
  593. "The String to pass to 'SimpleDateFormat' to format the "
  594. + "directory name(s) for archiving",
  595. manager.getConfigManager(), manager.getIdentity()));
  596. general.addSubCategory(backbuffer.setInline());
  597. general.addSubCategory(advanced.setInline());
  598. manager.getCategory("Plugins").addSubCategory(general.setInlineAfter());
  599. }
  600. /** Open File. */
  601. private static class OpenFile {
  602. /** Last used time. */
  603. public long lastUsedTime = System.currentTimeMillis();
  604. /** Open file's writer. */
  605. public final BufferedWriter writer;
  606. /**
  607. * Creates a new open file.
  608. *
  609. * @param writer Writer that has file open
  610. */
  611. protected OpenFile(final BufferedWriter writer) {
  612. this.writer = writer;
  613. }
  614. }
  615. }