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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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.ClientModule.GlobalConfig;
  24. import com.dmdirc.DMDircMBassador;
  25. import com.dmdirc.Query;
  26. import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
  27. import com.dmdirc.config.prefs.PluginPreferencesCategory;
  28. import com.dmdirc.config.prefs.PreferencesCategory;
  29. import com.dmdirc.config.prefs.PreferencesDialogModel;
  30. import com.dmdirc.config.prefs.PreferencesSetting;
  31. import com.dmdirc.config.prefs.PreferencesType;
  32. import com.dmdirc.events.BaseChannelActionEvent;
  33. import com.dmdirc.events.BaseChannelMessageEvent;
  34. import com.dmdirc.events.BaseQueryActionEvent;
  35. import com.dmdirc.events.BaseQueryMessageEvent;
  36. import com.dmdirc.events.ChannelClosedEvent;
  37. import com.dmdirc.events.ChannelGotTopicEvent;
  38. import com.dmdirc.events.ChannelJoinEvent;
  39. import com.dmdirc.events.ChannelKickEvent;
  40. import com.dmdirc.events.ChannelModeChangeEvent;
  41. import com.dmdirc.events.ChannelNickChangeEvent;
  42. import com.dmdirc.events.ChannelOpenedEvent;
  43. import com.dmdirc.events.ChannelPartEvent;
  44. import com.dmdirc.events.ChannelQuitEvent;
  45. import com.dmdirc.events.ChannelTopicChangeEvent;
  46. import com.dmdirc.events.ClientPrefsOpenedEvent;
  47. import com.dmdirc.events.QueryClosedEvent;
  48. import com.dmdirc.events.QueryOpenedEvent;
  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.Styliser;
  61. import com.dmdirc.util.io.ReverseFileReader;
  62. import com.dmdirc.util.io.StreamUtils;
  63. import java.awt.Color;
  64. import java.io.BufferedWriter;
  65. import java.io.File;
  66. import java.io.FileWriter;
  67. import java.io.IOException;
  68. import java.nio.file.Files;
  69. import java.nio.file.Path;
  70. import java.nio.file.Paths;
  71. import java.text.DateFormat;
  72. import java.text.SimpleDateFormat;
  73. import java.util.ArrayList;
  74. import java.util.Collection;
  75. import java.util.Collections;
  76. import java.util.Date;
  77. import java.util.HashMap;
  78. import java.util.Map;
  79. import java.util.Stack;
  80. import java.util.Timer;
  81. import java.util.TimerTask;
  82. import javax.inject.Inject;
  83. import javax.inject.Provider;
  84. import javax.inject.Singleton;
  85. import org.slf4j.Logger;
  86. import org.slf4j.LoggerFactory;
  87. import net.engio.mbassy.listener.Handler;
  88. import static com.dmdirc.util.LogUtils.USER_ERROR;
  89. /**
  90. * Manages logging activities.
  91. */
  92. @Singleton
  93. public class LoggingManager implements ConfigChangeListener {
  94. private static final Logger LOG = LoggerFactory.getLogger(LoggingManager.class);
  95. /** Date format used for "File Opened At" log. */
  96. private static final DateFormat OPENED_AT_FORMAT = new SimpleDateFormat(
  97. "EEEE MMMM dd, yyyy - HH:mm:ss");
  98. /** Object for synchronising access to the date forma.t */
  99. private static final Object FORMAT_LOCK = new Object();
  100. private static final DateFormat LOG_FORMAT = new SimpleDateFormat("[dd/MM/yyyy HH:mm:ss]");
  101. /** This plugin's plugin info. */
  102. private final String domain;
  103. private final PluginInfo pluginInfo;
  104. /** Global config. */
  105. private final AggregateConfigProvider config;
  106. /** The manager to add history windows to. */
  107. private final WindowManager windowManager;
  108. /** Map of open files. */
  109. private final Map<String, OpenFile> openFiles = Collections.synchronizedMap(
  110. new HashMap<>());
  111. private final DMDircMBassador eventBus;
  112. private final Provider<String> directoryProvider;
  113. private final BackBufferFactory backBufferFactory;
  114. private final LogFileLocator locator;
  115. /** Timer used to close idle files. */
  116. private Timer idleFileTimer;
  117. /** Cached boolean settings. */
  118. private boolean addtime;
  119. private boolean stripcodes;
  120. private boolean channelmodeprefix;
  121. private boolean autobackbuffer;
  122. private String colour;
  123. /** Cached int settings. */
  124. private int historyLines;
  125. private int backbufferLines;
  126. @Inject
  127. public LoggingManager(@PluginDomain(LoggingPlugin.class) final String domain,
  128. @PluginDomain(LoggingPlugin.class) final PluginInfo pluginInfo,
  129. @GlobalConfig final AggregateConfigProvider globalConfig,
  130. final WindowManager windowManager, final DMDircMBassador eventBus,
  131. @Directory(LoggingModule.LOGS_DIRECTORY) final Provider<String> directoryProvider,
  132. final BackBufferFactory backBufferFactory,
  133. final LogFileLocator locator) {
  134. this.domain = domain;
  135. this.pluginInfo = pluginInfo;
  136. this.config = globalConfig;
  137. this.windowManager = windowManager;
  138. this.eventBus = eventBus;
  139. this.directoryProvider = directoryProvider;
  140. this.backBufferFactory = backBufferFactory;
  141. this.locator = locator;
  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(), 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.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.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. final DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
  247. final DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
  248. appendLine(filename, "*** Topic is: %s", event.getTopic().getTopic());
  249. appendLine(filename, "*** Set at: %s on %s by %s",
  250. timeFormat.format(event.getTopic().getDate()),
  251. dateFormat.format(event.getTopic().getDate()),
  252. event.getTopic().getClient()
  253. .map(GroupChatUser::getNickname).orElse("Unknown"));
  254. }
  255. @Handler
  256. public void handleChannelTopicChange(final ChannelTopicChangeEvent event) {
  257. final String filename = locator.getLogFile(event.getChannel());
  258. appendLine(filename, "*** %s Changed the topic to: %s",
  259. event.getTopic().getClient().map(this::getDisplayName).orElse(""), event.getTopic());
  260. }
  261. @Handler
  262. public void handleChannelJoin(final ChannelJoinEvent event) {
  263. final String filename = locator.getLogFile(event.getChannel());
  264. final GroupChatUser channelClient = event.getClient();
  265. appendLine(filename, "*** %s (%s) joined the channel", getDisplayName(channelClient),
  266. channelClient.getNickname());
  267. }
  268. @Handler
  269. public void handleChannelPart(final ChannelPartEvent event) {
  270. final String filename = locator.getLogFile(event.getChannel());
  271. final String message = event.getMessage();
  272. final GroupChatUser channelClient = event.getClient();
  273. if (message.isEmpty()) {
  274. appendLine(filename, "*** %s (%s) left the channel", getDisplayName(channelClient),
  275. channelClient.getNickname());
  276. } else {
  277. appendLine(filename, "*** %s (%s) left the channel (%s)",
  278. getDisplayName(channelClient), channelClient.getNickname(), message);
  279. }
  280. }
  281. @Handler
  282. public void handleChannelQuit(final ChannelQuitEvent event) {
  283. final String filename = locator.getLogFile(event.getChannel());
  284. final String reason = event.getMessage();
  285. final GroupChatUser channelClient = event.getClient();
  286. if (reason.isEmpty()) {
  287. appendLine(filename, "*** %s (%s) Quit IRC",
  288. getDisplayName(channelClient), channelClient.getNickname());
  289. } else {
  290. appendLine(filename, "*** %s (%s) Quit IRC (%s)",
  291. getDisplayName(channelClient), channelClient.getNickname(), reason);
  292. }
  293. }
  294. @Handler
  295. public void handleChannelKick(final ChannelKickEvent event) {
  296. final GroupChatUser victim = event.getVictim();
  297. final GroupChatUser perpetrator = event.getClient();
  298. final String reason = event.getReason();
  299. final String filename = locator.getLogFile(event.getChannel());
  300. if (reason.isEmpty()) {
  301. appendLine(filename, "*** %s was kicked by %s",
  302. getDisplayName(victim), getDisplayName(perpetrator));
  303. } else {
  304. appendLine(filename, "*** %s was kicked by %s (%s)",
  305. getDisplayName(victim), getDisplayName(perpetrator), reason);
  306. }
  307. }
  308. @Handler
  309. public void handleNickChange(final ChannelNickChangeEvent event) {
  310. final String filename = locator.getLogFile(event.getChannel());
  311. appendLine(filename, "*** %s is now %s", getDisplayName(event.getClient(),
  312. event.getOldNick()), getDisplayName(event.getClient()));
  313. }
  314. @Handler
  315. public void handleModeChange(final ChannelModeChangeEvent event) {
  316. final String filename = locator.getLogFile(event.getChannel());
  317. if (event.getClient().getNickname().isEmpty()) {
  318. appendLine(filename, "*** Channel modes are: %s", event.getModes());
  319. } else {
  320. appendLine(filename, "*** %s set modes: %s",
  321. getDisplayName(event.getClient()), event.getModes());
  322. }
  323. }
  324. @Override
  325. public void configChanged(final String domain, final String key) {
  326. setCachedSettings();
  327. }
  328. @Handler
  329. public void handleChannelOpened(final ChannelOpenedEvent event) {
  330. final String filename = locator.getLogFile(event.getChannel().getName());
  331. if (autobackbuffer) {
  332. showBackBuffer(event.getChannel().getWindowModel(), filename);
  333. }
  334. synchronized (FORMAT_LOCK) {
  335. appendLine(filename, "*** Channel opened at: %s", OPENED_AT_FORMAT.format(new Date()));
  336. appendLine(filename, "");
  337. }
  338. }
  339. @Handler
  340. public void handleChannelClosed(final ChannelClosedEvent event) {
  341. final String filename = locator.getLogFile(event.getChannel().getName());
  342. synchronized (FORMAT_LOCK) {
  343. appendLine(filename, "*** Channel closed at: %s", OPENED_AT_FORMAT.format(new Date()));
  344. }
  345. if (openFiles.containsKey(filename)) {
  346. StreamUtils.close(openFiles.get(filename).writer);
  347. openFiles.remove(filename);
  348. }
  349. }
  350. /**
  351. * Add a backbuffer to a frame.
  352. *
  353. * @param frame The frame to add the backbuffer lines to
  354. * @param filename File to get backbuffer from
  355. */
  356. protected void showBackBuffer(final WindowModel frame, final String filename) {
  357. if (frame == null) {
  358. LOG.info(USER_ERROR, "Unable to show back buffer, frame was null");
  359. return;
  360. }
  361. final Path testFile = Paths.get(filename);
  362. if (Files.exists(testFile)) {
  363. try (final ReverseFileReader file = new ReverseFileReader(testFile)) {
  364. // Because the file includes a newline char at the end, an empty line
  365. // is returned by getLines. To counter this, we call getLines(1) and do
  366. // nothing with the output.
  367. file.getLines(1);
  368. final Stack<String> lines = file.getLines(backbufferLines);
  369. while (!lines.empty()) {
  370. frame.getEventBus().publishAsync(new HistoricalLineRestoredEvent(frame,
  371. getColouredString(colour, lines.pop())));
  372. }
  373. file.close();
  374. frame.getEventBus().publishAsync(new HistoricalLineRestoredEvent(frame,
  375. getColouredString(colour, "--- End of backbuffer\n")));
  376. } catch (IOException | SecurityException e) {
  377. LOG.info(USER_ERROR, "Unable to show backbuffer (Filename: {}): {}", filename,
  378. e.getMessage(), e);
  379. }
  380. }
  381. }
  382. /**
  383. * Get a coloured String. If colour is invalid, IRC Colour 14 will be used.
  384. *
  385. * @param colour The colour the string should be (IRC Colour or 6-digit hex colour)
  386. * @param line the line to colour
  387. *
  388. * @return The given line with the appropriate irc codes appended/prepended to colour it.
  389. */
  390. protected static String getColouredString(final String colour, final String line) {
  391. String res = null;
  392. if (colour.length() < 3) {
  393. int num;
  394. try {
  395. num = Integer.parseInt(colour);
  396. } catch (NumberFormatException ex) {
  397. num = -1;
  398. }
  399. if (num >= 0 && num <= 15) {
  400. res = String.format("%c%02d%s%1$c", Styliser.CODE_COLOUR, num, line);
  401. }
  402. } else if (colour.length() == 6) {
  403. try {
  404. Color.decode('#' + colour);
  405. res = String.format("%c%s%s%1$c", Styliser.CODE_HEXCOLOUR, colour, line);
  406. } catch (NumberFormatException ex) { /* Do Nothing */ }
  407. }
  408. if (res == null) {
  409. res = String.format("%c%02d%s%1$c", Styliser.CODE_COLOUR, 14, line);
  410. }
  411. return res;
  412. }
  413. /**
  414. * Add a line to a file.
  415. *
  416. * @param filename Name of file to write to
  417. * @param format Format of line to add. (NewLine will be added Automatically)
  418. * @param args Arguments for format
  419. *
  420. * @return true on success, else false.
  421. */
  422. protected boolean appendLine(final String filename, final String format, final Object... args) {
  423. return appendLine(filename, String.format(format, args));
  424. }
  425. /**
  426. * Add a line to a file.
  427. *
  428. * @param filename Name of file to write to
  429. * @param line Line to add. (NewLine will be added Automatically)
  430. *
  431. * @return true on success, else false.
  432. */
  433. protected boolean appendLine(final String filename, final String line) {
  434. final StringBuilder finalLine = new StringBuilder();
  435. if (addtime) {
  436. final String dateString = LOG_FORMAT.format(new Date()).trim();
  437. finalLine.append(dateString);
  438. finalLine.append(' ');
  439. }
  440. if (stripcodes) {
  441. finalLine.append(Styliser.stipControlCodes(line));
  442. } else {
  443. finalLine.append(line);
  444. }
  445. try {
  446. final BufferedWriter out;
  447. if (openFiles.containsKey(filename)) {
  448. final OpenFile of = openFiles.get(filename);
  449. of.lastUsedTime = System.currentTimeMillis();
  450. out = of.writer;
  451. } else {
  452. out = new BufferedWriter(new FileWriter(filename, true));
  453. openFiles.put(filename, new OpenFile(out));
  454. }
  455. out.write(finalLine.toString());
  456. out.newLine();
  457. out.flush();
  458. return true;
  459. } catch (IOException e) {
  460. /*
  461. * Do Nothing
  462. *
  463. * Makes no sense to keep adding errors to the logger when we can't write to the file,
  464. * as chances are it will happen on every incomming line.
  465. */
  466. }
  467. return false;
  468. }
  469. /**
  470. * Get name to display for channelClient (Taking into account the channelmodeprefix setting).
  471. *
  472. * @param channelClient The client to get the display name for
  473. *
  474. * @return name to display
  475. */
  476. protected String getDisplayName(final GroupChatUser channelClient) {
  477. return getDisplayName(channelClient, "");
  478. }
  479. /**
  480. * Get name to display for channelClient (Taking into account the channelmodeprefix setting).
  481. *
  482. * @param channelClient The client to get the display name for
  483. * @param overrideNick Nickname to display instead of real nickname
  484. *
  485. * @return name to display
  486. */
  487. protected String getDisplayName(final GroupChatUser channelClient, final String overrideNick) {
  488. if (channelClient == null) {
  489. return overrideNick.isEmpty() ? "Unknown Client" : overrideNick;
  490. } else if (overrideNick.isEmpty()) {
  491. return channelmodeprefix ? channelClient.getModePrefixedNickname()
  492. : channelClient.getNickname();
  493. } else {
  494. return channelmodeprefix ? channelClient.getImportantMode() + overrideNick :
  495. overrideNick;
  496. }
  497. }
  498. /**
  499. * Shows the history window for the specified target, if available.
  500. *
  501. * @param target The window whose history we're trying to open
  502. *
  503. * @return True if the history is available, false otherwise
  504. */
  505. protected boolean showHistory(final WindowModel target) {
  506. final String descriptor;
  507. if (target instanceof GroupChat) {
  508. descriptor = target.getName();
  509. } else if (target instanceof Query) {
  510. descriptor = ((PrivateChat) target).getNickname();
  511. } else {
  512. // Unknown component
  513. return false;
  514. }
  515. final Path log = Paths.get(locator.getLogFile(descriptor));
  516. if (!Files.exists(log)) {
  517. // File doesn't exist
  518. return false;
  519. }
  520. windowManager.addWindow(target, new HistoryWindow("History", log, target,
  521. eventBus, backBufferFactory, historyLines));
  522. return true;
  523. }
  524. /** Updates cached settings. */
  525. public void setCachedSettings() {
  526. addtime = config.getOptionBool(domain, "general.addtime");
  527. stripcodes = config.getOptionBool(domain, "general.stripcodes");
  528. channelmodeprefix = config.getOptionBool(domain, "general.channelmodeprefix");
  529. autobackbuffer = config.getOptionBool(domain, "backbuffer.autobackbuffer");
  530. historyLines = config.getOptionInt(domain, "history.lines");
  531. colour = config.getOption(domain, "backbuffer.colour");
  532. backbufferLines = config.getOptionInt(domain, "backbuffer.lines");
  533. }
  534. @Handler
  535. public void showConfig(final ClientPrefsOpenedEvent event) {
  536. final PreferencesDialogModel manager = event.getModel();
  537. final PreferencesCategory general = new PluginPreferencesCategory(
  538. pluginInfo, "Logging", "General configuration for Logging plugin.");
  539. final PreferencesCategory backbuffer = new PluginPreferencesCategory(
  540. pluginInfo, "Back Buffer", "Options related to the automatic backbuffer");
  541. final PreferencesCategory advanced = new PluginPreferencesCategory(
  542. pluginInfo, "Advanced",
  543. "Advanced configuration for Logging plugin. You shouldn't need to edit this unless you know what you are doing.");
  544. general.addSetting(new PreferencesSetting(PreferencesType.DIRECTORY,
  545. pluginInfo.getDomain(), "general.directory", "Directory",
  546. "Directory for log files", manager.getConfigManager(),
  547. manager.getIdentity()));
  548. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  549. pluginInfo.getDomain(), "general.networkfolders",
  550. "Separate logs by network",
  551. "Should the files be stored in a sub-dir with the networks name?",
  552. manager.getConfigManager(), manager.getIdentity()));
  553. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  554. pluginInfo.getDomain(), "general.addtime", "Timestamp logs",
  555. "Should a timestamp be added to the log files?",
  556. manager.getConfigManager(), manager.getIdentity()));
  557. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  558. pluginInfo.getDomain(), "general.stripcodes", "Strip Control Codes",
  559. "Remove known irc control codes from lines before saving?",
  560. manager.getConfigManager(), manager.getIdentity()));
  561. general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  562. pluginInfo.getDomain(), "general.channelmodeprefix",
  563. "Show channel mode prefix", "Show the @,+ etc next to nicknames",
  564. manager.getConfigManager(), manager.getIdentity()));
  565. backbuffer.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  566. pluginInfo.getDomain(), "backbuffer.autobackbuffer", "Automatically display",
  567. "Automatically display the backbuffer when a channel is joined",
  568. manager.getConfigManager(), manager.getIdentity()));
  569. backbuffer.addSetting(new PreferencesSetting(PreferencesType.COLOUR,
  570. pluginInfo.getDomain(), "backbuffer.colour", "Colour to use for display",
  571. "Colour used when displaying the backbuffer",
  572. manager.getConfigManager(), manager.getIdentity()));
  573. backbuffer.addSetting(new PreferencesSetting(PreferencesType.INTEGER,
  574. pluginInfo.getDomain(), "backbuffer.lines", "Number of lines to show",
  575. "Number of lines used when displaying backbuffer",
  576. manager.getConfigManager(), manager.getIdentity()));
  577. advanced.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  578. pluginInfo.getDomain(), "advanced.filenamehash", "Add Filename hash",
  579. "Add the MD5 hash of the channel/client name to the filename. "
  580. + "(This is used to allow channels with similar names "
  581. + "(ie a _ not a -) to be logged separately)",
  582. manager.getConfigManager(), manager.getIdentity()));
  583. advanced.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  584. pluginInfo.getDomain(), "advanced.usedate", "Use Date directories",
  585. "Should the log files be in separate directories based on the date?",
  586. manager.getConfigManager(), manager.getIdentity()));
  587. advanced.addSetting(new PreferencesSetting(PreferencesType.TEXT,
  588. pluginInfo.getDomain(), "advanced.usedateformat", "Archive format",
  589. "The String to pass to 'SimpleDateFormat' to format the "
  590. + "directory name(s) for archiving",
  591. manager.getConfigManager(), manager.getIdentity()));
  592. general.addSubCategory(backbuffer.setInline());
  593. general.addSubCategory(advanced.setInline());
  594. manager.getCategory("Plugins").addSubCategory(general.setInlineAfter());
  595. }
  596. /** Open File. */
  597. private static class OpenFile {
  598. /** Last used time. */
  599. public long lastUsedTime = System.currentTimeMillis();
  600. /** Open file's writer. */
  601. public final BufferedWriter writer;
  602. /**
  603. * Creates a new open file.
  604. *
  605. * @param writer Writer that has file open
  606. */
  607. protected OpenFile(final BufferedWriter writer) {
  608. this.writer = writer;
  609. }
  610. }
  611. }