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

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