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 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. /*
  2. * Copyright (c) 2006-2014 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.Channel;
  24. import com.dmdirc.ClientModule.GlobalConfig;
  25. import com.dmdirc.DMDircMBassador;
  26. import com.dmdirc.FrameContainer;
  27. import com.dmdirc.Query;
  28. import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
  29. import com.dmdirc.events.BaseChannelActionEvent;
  30. import com.dmdirc.events.BaseChannelMessageEvent;
  31. import com.dmdirc.events.BaseQueryActionEvent;
  32. import com.dmdirc.events.BaseQueryMessageEvent;
  33. import com.dmdirc.events.ChannelClosedEvent;
  34. import com.dmdirc.events.ChannelGottopicEvent;
  35. import com.dmdirc.events.ChannelJoinEvent;
  36. import com.dmdirc.events.ChannelKickEvent;
  37. import com.dmdirc.events.ChannelModechangeEvent;
  38. import com.dmdirc.events.ChannelNickchangeEvent;
  39. import com.dmdirc.events.ChannelOpenedEvent;
  40. import com.dmdirc.events.ChannelPartEvent;
  41. import com.dmdirc.events.ChannelQuitEvent;
  42. import com.dmdirc.events.ChannelTopicChangeEvent;
  43. import com.dmdirc.events.QueryClosedEvent;
  44. import com.dmdirc.events.QueryOpenedEvent;
  45. import com.dmdirc.events.UserErrorEvent;
  46. import com.dmdirc.interfaces.PrivateChat;
  47. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  48. import com.dmdirc.interfaces.config.ConfigChangeListener;
  49. import com.dmdirc.logger.ErrorLevel;
  50. import com.dmdirc.parser.interfaces.ChannelClientInfo;
  51. import com.dmdirc.parser.interfaces.ChannelInfo;
  52. import com.dmdirc.parser.interfaces.ClientInfo;
  53. import com.dmdirc.parser.interfaces.Parser;
  54. import com.dmdirc.plugins.PluginDomain;
  55. import com.dmdirc.ui.WindowManager;
  56. import com.dmdirc.ui.messages.BackBufferFactory;
  57. import com.dmdirc.ui.messages.Styliser;
  58. import com.dmdirc.util.URLBuilder;
  59. import com.dmdirc.util.io.ReverseFileReader;
  60. import com.dmdirc.util.io.StreamUtils;
  61. import java.awt.Color;
  62. import java.io.BufferedWriter;
  63. import java.io.File;
  64. import java.io.FileWriter;
  65. import java.io.IOException;
  66. import java.nio.file.Files;
  67. import java.nio.file.Path;
  68. import java.nio.file.Paths;
  69. import java.text.DateFormat;
  70. import java.text.SimpleDateFormat;
  71. import java.util.ArrayList;
  72. import java.util.Collection;
  73. import java.util.Collections;
  74. import java.util.Date;
  75. import java.util.HashMap;
  76. import java.util.Map;
  77. import java.util.Stack;
  78. import java.util.Timer;
  79. import java.util.TimerTask;
  80. import javax.inject.Inject;
  81. import javax.inject.Provider;
  82. import javax.inject.Singleton;
  83. import net.engio.mbassy.listener.Handler;
  84. /**
  85. * Manages logging activities.
  86. */
  87. @Singleton
  88. public class LoggingManager implements ConfigChangeListener {
  89. /** Date format used for "File Opened At" log. */
  90. private static final DateFormat OPENED_AT_FORMAT = new SimpleDateFormat(
  91. "EEEE MMMM dd, yyyy - HH:mm:ss");
  92. /** Object for synchronising access to the date forma.t */
  93. private static final Object FORMAT_LOCK = new Object();
  94. /** This plugin's plugin info. */
  95. private final String domain;
  96. /** Global config. */
  97. private final AggregateConfigProvider config;
  98. /** The manager to add history windows to. */
  99. private final WindowManager windowManager;
  100. /** Map of open files. */
  101. private final Map<String, OpenFile> openFiles = Collections.synchronizedMap(
  102. new HashMap<>());
  103. private final URLBuilder urlBuilder;
  104. private final DMDircMBassador eventBus;
  105. private final Provider<String> directoryProvider;
  106. private final BackBufferFactory backBufferFactory;
  107. private final LogFileLocator locator;
  108. /** Timer used to close idle files. */
  109. private Timer idleFileTimer;
  110. /** Cached boolean settings. */
  111. private boolean addtime;
  112. private boolean stripcodes;
  113. private boolean channelmodeprefix;
  114. private boolean autobackbuffer;
  115. private boolean backbufferTimestamp;
  116. /** Cached string settings. */
  117. private String timestamp;
  118. private String colour;
  119. /** Cached int settings. */
  120. private int historyLines;
  121. private int backbufferLines;
  122. @Inject
  123. public LoggingManager(@PluginDomain(LoggingPlugin.class) final String domain,
  124. @GlobalConfig final AggregateConfigProvider globalConfig,
  125. final WindowManager windowManager, final URLBuilder urlBuilder, final DMDircMBassador eventBus,
  126. @Directory(LoggingModule.LOGS_DIRECTORY) final Provider<String> directoryProvider,
  127. final BackBufferFactory backBufferFactory,
  128. final LogFileLocator locator) {
  129. this.domain = domain;
  130. this.config = globalConfig;
  131. this.windowManager = windowManager;
  132. this.urlBuilder = urlBuilder;
  133. this.eventBus = eventBus;
  134. this.directoryProvider = directoryProvider;
  135. this.backBufferFactory = backBufferFactory;
  136. this.locator = locator;
  137. }
  138. public void load() {
  139. setCachedSettings();
  140. final File dir = new File(directoryProvider.get());
  141. if (dir.exists()) {
  142. if (!dir.isDirectory()) {
  143. eventBus.publishAsync(new UserErrorEvent(ErrorLevel.LOW, null,
  144. "Unable to create logging dir (file exists instead)", ""));
  145. }
  146. } else {
  147. if (!dir.mkdirs()) {
  148. eventBus.publishAsync(new UserErrorEvent(ErrorLevel.LOW, null,
  149. "Unable to create logging dir", ""));
  150. }
  151. }
  152. config.addChangeListener(domain, this);
  153. // Close idle files every hour.
  154. idleFileTimer = new Timer("LoggingPlugin Timer");
  155. idleFileTimer.schedule(new TimerTask() {
  156. @Override
  157. public void run() {
  158. timerTask();
  159. }
  160. }, 3600000);
  161. eventBus.subscribe(this);
  162. }
  163. public void unload() {
  164. if (idleFileTimer != null) {
  165. idleFileTimer.cancel();
  166. idleFileTimer.purge();
  167. }
  168. synchronized (openFiles) {
  169. for (OpenFile file : openFiles.values()) {
  170. StreamUtils.close(file.writer);
  171. }
  172. openFiles.clear();
  173. }
  174. eventBus.unsubscribe(this);
  175. }
  176. /**
  177. * What to do every hour when the timer fires.
  178. */
  179. protected void timerTask() {
  180. // Oldest time to allow
  181. final long oldestTime = System.currentTimeMillis() - 3480000;
  182. synchronized (openFiles) {
  183. final Collection<String> old = new ArrayList<>(openFiles.size());
  184. for (Map.Entry<String, OpenFile> entry : openFiles.entrySet()) {
  185. if (entry.getValue().lastUsedTime < oldestTime) {
  186. StreamUtils.close(entry.getValue().writer);
  187. old.add(entry.getKey());
  188. }
  189. }
  190. openFiles.keySet().removeAll(old);
  191. }
  192. }
  193. @Handler
  194. public void handleQueryOpened(final QueryOpenedEvent event) {
  195. final Parser parser = event.getQuery().getConnection().get().getParser();
  196. final ClientInfo client = parser.getClient(event.getQuery().getHost());
  197. final String filename = locator.getLogFile(client);
  198. if (autobackbuffer) {
  199. showBackBuffer(event.getQuery(), filename);
  200. }
  201. synchronized (FORMAT_LOCK) {
  202. appendLine(filename, "*** Query opened at: %s", OPENED_AT_FORMAT.format(new Date()));
  203. appendLine(filename, "*** Query with User: %s", event.getQuery().getHost());
  204. appendLine(filename, "");
  205. }
  206. }
  207. @Handler
  208. public void handleQueryClosed(final QueryClosedEvent event) {
  209. final Parser parser = event.getQuery().getConnection().get().getParser();
  210. final ClientInfo client = parser.getClient(event.getQuery().getHost());
  211. final String filename = locator.getLogFile(client);
  212. synchronized (FORMAT_LOCK) {
  213. appendLine(filename, "*** Query closed at: %s", OPENED_AT_FORMAT.format(new Date()));
  214. }
  215. if (openFiles.containsKey(filename)) {
  216. StreamUtils.close(openFiles.get(filename).writer);
  217. openFiles.remove(filename);
  218. }
  219. }
  220. @Handler
  221. public void handleQueryActions(final BaseQueryActionEvent event) {
  222. final ClientInfo client = event.getClient();
  223. final String filename = locator.getLogFile(client);
  224. appendLine(filename, "* %s %s", client.getNickname(), event.getMessage());
  225. }
  226. @Handler
  227. public void handleQueryMessages(final BaseQueryMessageEvent event) {
  228. final ClientInfo client = event.getClient();
  229. final String filename = locator.getLogFile(client);
  230. appendLine(filename, "<%s> %s", client.getNickname(), event.getMessage());
  231. }
  232. @Handler
  233. public void handleChannelMessage(final BaseChannelMessageEvent event) {
  234. final String filename = locator.getLogFile(event.getChannel().getChannelInfo());
  235. appendLine(filename, "<%s> %s", getDisplayName(event.getClient()), event.getMessage());
  236. }
  237. @Handler
  238. public void handleChannelAction(final BaseChannelActionEvent event) {
  239. final String filename = locator.getLogFile(event.getChannel().getChannelInfo());
  240. appendLine(filename, "* %s %s", getDisplayName(event.getClient()), event.getMessage());
  241. }
  242. @Handler
  243. public void handleChannelGotTopic(final ChannelGottopicEvent event) {
  244. final String filename = locator.getLogFile(event.getChannel().getChannelInfo());
  245. final ChannelInfo channel = event.getChannel().getChannelInfo();
  246. final DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
  247. final DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
  248. appendLine(filename, "*** Topic is: %s", channel.getTopic());
  249. appendLine(filename, "*** Set at: %s on %s by %s", timeFormat.format(1000 * channel.
  250. getTopicTime()), dateFormat.format(1000 * channel.getTopicTime()), channel.
  251. getTopicSetter());
  252. }
  253. @Handler
  254. public void handleChannelTopicChange(final ChannelTopicChangeEvent event) {
  255. final String filename = locator.getLogFile(event.getChannel().getChannelInfo());
  256. final ChannelClientInfo channelClient = event.getClient();
  257. appendLine(filename, "*** %s Changed the topic to: %s",
  258. getDisplayName(channelClient), event.getTopic());
  259. }
  260. @Handler
  261. public void handleChannelJoin(final ChannelJoinEvent event) {
  262. final String filename = locator.getLogFile(event.getChannel().getChannelInfo());
  263. final ChannelClientInfo channelClient = event.getClient();
  264. final ClientInfo client = channelClient.getClient();
  265. appendLine(filename, "*** %s (%s) joined the channel",
  266. getDisplayName(channelClient), client.toString());
  267. }
  268. @Handler
  269. public void handleChannelPart(final ChannelPartEvent event) {
  270. final String filename = locator.getLogFile(event.getChannel().getChannelInfo());
  271. final String message = event.getMessage();
  272. final ChannelClientInfo channelClient = event.getClient();
  273. final ClientInfo client = channelClient.getClient();
  274. if (message.isEmpty()) {
  275. appendLine(filename, "*** %s (%s) left the channel",
  276. getDisplayName(channelClient), client.toString());
  277. } else {
  278. appendLine(filename, "*** %s (%s) left the channel (%s)",
  279. getDisplayName(channelClient), client.toString(), message);
  280. }
  281. }
  282. @Handler
  283. public void handleChannelQuit(final ChannelQuitEvent event) {
  284. final String filename = locator.getLogFile(event.getChannel().getChannelInfo());
  285. final String reason = event.getMessage();
  286. final ChannelClientInfo channelClient = event.getClient();
  287. final ClientInfo client = channelClient.getClient();
  288. if (reason.isEmpty()) {
  289. appendLine(filename, "*** %s (%s) Quit IRC",
  290. getDisplayName(channelClient), client.toString());
  291. } else {
  292. appendLine(filename, "*** %s (%s) Quit IRC (%s)",
  293. getDisplayName(channelClient), client.toString(), reason);
  294. }
  295. }
  296. @Handler
  297. public void handleChannelKick(final ChannelKickEvent event) {
  298. final ChannelClientInfo victim = event.getVictim();
  299. final ChannelClientInfo perpetrator = event.getClient();
  300. final String reason = event.getReason();
  301. final String filename = locator.getLogFile(event.getChannel().getChannelInfo());
  302. if (reason.isEmpty()) {
  303. appendLine(filename, "*** %s was kicked by %s",
  304. getDisplayName(victim), getDisplayName(perpetrator));
  305. } else {
  306. appendLine(filename, "*** %s was kicked by %s (%s)",
  307. getDisplayName(victim), getDisplayName(perpetrator), reason);
  308. }
  309. }
  310. @Handler
  311. public void handleNickChange(final ChannelNickchangeEvent event) {
  312. final String filename = locator.getLogFile(event.getChannel().getChannelInfo());
  313. appendLine(filename, "*** %s is now %s", getDisplayName(event.getClient(),
  314. event.getOldNick()), getDisplayName(event.getClient()));
  315. }
  316. @Handler
  317. public void handleModeChange(final ChannelModechangeEvent event) {
  318. final String filename = locator.getLogFile(event.getChannel().getChannelInfo());
  319. if (event.getClient().getClient().getNickname().isEmpty()) {
  320. appendLine(filename, "*** Channel modes are: %s", event.getModes());
  321. } else {
  322. appendLine(filename, "*** %s set modes: %s",
  323. getDisplayName(event.getClient()), event.getModes());
  324. }
  325. }
  326. @Override
  327. public void configChanged(final String domain, final String key) {
  328. setCachedSettings();
  329. }
  330. @Handler
  331. public void handleChannelOpened(final ChannelOpenedEvent event) {
  332. final String filename = locator.getLogFile(event.getChannel().getName());
  333. if (autobackbuffer) {
  334. showBackBuffer(event.getChannel(), filename);
  335. }
  336. synchronized (FORMAT_LOCK) {
  337. appendLine(filename, "*** Channel opened at: %s", OPENED_AT_FORMAT.format(new Date()));
  338. appendLine(filename, "");
  339. }
  340. }
  341. @Handler
  342. public void handleChannelClosed(final ChannelClosedEvent event) {
  343. final String filename = locator.getLogFile(event.getChannel().getName());
  344. synchronized (FORMAT_LOCK) {
  345. appendLine(filename, "*** Channel closed at: %s", OPENED_AT_FORMAT.format(new Date()));
  346. }
  347. if (openFiles.containsKey(filename)) {
  348. StreamUtils.close(openFiles.get(filename).writer);
  349. openFiles.remove(filename);
  350. }
  351. }
  352. /**
  353. * Add a backbuffer to a frame.
  354. *
  355. * @param frame The frame to add the backbuffer lines to
  356. * @param filename File to get backbuffer from
  357. */
  358. protected void showBackBuffer(final FrameContainer frame, final String filename) {
  359. if (frame == null) {
  360. eventBus.publishAsync(new UserErrorEvent(ErrorLevel.LOW, null, "Given a null frame", ""));
  361. return;
  362. }
  363. final Path testFile = Paths.get(filename);
  364. if (Files.exists(testFile)) {
  365. try {
  366. final ReverseFileReader file = new ReverseFileReader(testFile);
  367. // Because the file includes a newline char at the end, an empty line
  368. // is returned by getLines. To counter this, we call getLines(1) and do
  369. // nothing with the output.
  370. file.getLines(1);
  371. final Stack<String> lines = file.getLines(backbufferLines);
  372. while (!lines.empty()) {
  373. frame.addLine(getColouredString(colour, lines.pop()), backbufferTimestamp);
  374. }
  375. file.close();
  376. frame.addLine(getColouredString(colour, "--- End of backbuffer\n"),
  377. backbufferTimestamp);
  378. } catch (IOException | SecurityException e) {
  379. eventBus.publishAsync(new UserErrorEvent(ErrorLevel.LOW, e,
  380. "Unable to show backbuffer (Filename: " + filename + "): " + e.getMessage(),
  381. ""));
  382. }
  383. }
  384. }
  385. /**
  386. * Get a coloured String. If colour is invalid, IRC Colour 14 will be used.
  387. *
  388. * @param colour The colour the string should be (IRC Colour or 6-digit hex colour)
  389. * @param line the line to colour
  390. *
  391. * @return The given line with the appropriate irc codes appended/prepended to colour it.
  392. */
  393. protected static String getColouredString(final String colour, final String line) {
  394. String res = null;
  395. if (colour.length() < 3) {
  396. int num;
  397. try {
  398. num = Integer.parseInt(colour);
  399. } catch (NumberFormatException ex) {
  400. num = -1;
  401. }
  402. if (num >= 0 && num <= 15) {
  403. res = String.format("%c%02d%s%1$c", Styliser.CODE_COLOUR, num, line);
  404. }
  405. } else if (colour.length() == 6) {
  406. try {
  407. Color.decode('#' + colour);
  408. res = String.format("%c%s%s%1$c", Styliser.CODE_HEXCOLOUR, colour, line);
  409. } catch (NumberFormatException ex) { /* Do Nothing */ }
  410. }
  411. if (res == null) {
  412. res = String.format("%c%02d%s%1$c", Styliser.CODE_COLOUR, 14, line);
  413. }
  414. return res;
  415. }
  416. /**
  417. * Add a line to a file.
  418. *
  419. * @param filename Name of file to write to
  420. * @param format Format of line to add. (NewLine will be added Automatically)
  421. * @param args Arguments for format
  422. *
  423. * @return true on success, else false.
  424. */
  425. protected boolean appendLine(final String filename, final String format, final Object... args) {
  426. return appendLine(filename, String.format(format, args));
  427. }
  428. /**
  429. * Add a line to a file.
  430. *
  431. * @param filename Name of file to write to
  432. * @param line Line to add. (NewLine will be added Automatically)
  433. *
  434. * @return true on success, else false.
  435. */
  436. protected boolean appendLine(final String filename, final String line) {
  437. final StringBuilder finalLine = new StringBuilder();
  438. if (addtime) {
  439. String dateString;
  440. try {
  441. final DateFormat dateFormat = new SimpleDateFormat(timestamp);
  442. dateString = dateFormat.format(new Date()).trim();
  443. } catch (IllegalArgumentException iae) {
  444. // Default to known good format
  445. final DateFormat dateFormat = new SimpleDateFormat("[dd/MM/yyyy HH:mm:ss]");
  446. dateString = dateFormat.format(new Date()).trim();
  447. eventBus.publishAsync(new UserErrorEvent(ErrorLevel.LOW, iae,
  448. "Dateformat String '" + timestamp + "' is invalid. For more information: "
  449. + "http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html",
  450. ""));
  451. }
  452. finalLine.append(dateString);
  453. finalLine.append(' ');
  454. }
  455. if (stripcodes) {
  456. finalLine.append(Styliser.stipControlCodes(line));
  457. } else {
  458. finalLine.append(line);
  459. }
  460. try {
  461. final BufferedWriter out;
  462. if (openFiles.containsKey(filename)) {
  463. final OpenFile of = openFiles.get(filename);
  464. of.lastUsedTime = System.currentTimeMillis();
  465. out = of.writer;
  466. } else {
  467. out = new BufferedWriter(new FileWriter(filename, true));
  468. openFiles.put(filename, new OpenFile(out));
  469. }
  470. out.write(finalLine.toString());
  471. out.newLine();
  472. out.flush();
  473. return true;
  474. } catch (IOException e) {
  475. /*
  476. * Do Nothing
  477. *
  478. * Makes no sense to keep adding errors to the logger when we can't write to the file,
  479. * as chances are it will happen on every incomming line.
  480. */
  481. }
  482. return false;
  483. }
  484. /**
  485. * Get name to display for channelClient (Taking into account the channelmodeprefix setting).
  486. *
  487. * @param channelClient The client to get the display name for
  488. *
  489. * @return name to display
  490. */
  491. protected String getDisplayName(final ChannelClientInfo channelClient) {
  492. return getDisplayName(channelClient, "");
  493. }
  494. /**
  495. * Get name to display for channelClient (Taking into account the channelmodeprefix setting).
  496. *
  497. * @param channelClient The client to get the display name for
  498. * @param overrideNick Nickname to display instead of real nickname
  499. *
  500. * @return name to display
  501. */
  502. protected String getDisplayName(final ChannelClientInfo channelClient, final String overrideNick) {
  503. if (channelClient == null) {
  504. return overrideNick.isEmpty() ? "Unknown Client" : overrideNick;
  505. } else if (overrideNick.isEmpty()) {
  506. return channelmodeprefix ? channelClient.toString() : channelClient.getClient().
  507. getNickname();
  508. } else {
  509. return channelmodeprefix ? channelClient.getImportantModePrefix() + overrideNick
  510. : overrideNick;
  511. }
  512. }
  513. /**
  514. * Shows the history window for the specified target, if available.
  515. *
  516. * @param target The window whose history we're trying to open
  517. *
  518. * @return True if the history is available, false otherwise
  519. */
  520. protected boolean showHistory(final FrameContainer target) {
  521. final String descriptor;
  522. if (target instanceof Channel) {
  523. descriptor = target.getName();
  524. } else if (target instanceof Query) {
  525. final Parser parser = target.getConnection().get().getParser();
  526. descriptor = parser.getClient(((PrivateChat) target).getHost()).getNickname();
  527. } else {
  528. // Unknown component
  529. return false;
  530. }
  531. final Path log = Paths.get(locator.getLogFile(descriptor));
  532. if (!Files.exists(log)) {
  533. // File doesn't exist
  534. return false;
  535. }
  536. windowManager.addWindow(target, new HistoryWindow("History", log, target, urlBuilder,
  537. eventBus, backBufferFactory, historyLines));
  538. return true;
  539. }
  540. /** Updates cached settings. */
  541. public void setCachedSettings() {
  542. addtime = config.getOptionBool(domain, "general.addtime");
  543. stripcodes = config.getOptionBool(domain, "general.stripcodes");
  544. channelmodeprefix = config.getOptionBool(domain, "general.channelmodeprefix");
  545. autobackbuffer = config.getOptionBool(domain, "backbuffer.autobackbuffer");
  546. backbufferTimestamp = config.getOptionBool(domain, "backbuffer.timestamp");
  547. timestamp = config.getOption(domain, "general.timestamp");
  548. historyLines = config.getOptionInt(domain, "history.lines");
  549. colour = config.getOption(domain, "backbuffer.colour");
  550. backbufferLines = config.getOptionInt(domain, "backbuffer.lines");
  551. }
  552. /** Open File. */
  553. private static class OpenFile {
  554. /** Last used time. */
  555. public long lastUsedTime = System.currentTimeMillis();
  556. /** Open file's writer. */
  557. public final BufferedWriter writer;
  558. /**
  559. * Creates a new open file.
  560. *
  561. * @param writer Writer that has file open
  562. */
  563. protected OpenFile(final BufferedWriter writer) {
  564. this.writer = writer;
  565. }
  566. }
  567. }