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

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