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.

Styliser.java 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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.ui.messages;
  23. import com.dmdirc.interfaces.Connection;
  24. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  25. import com.dmdirc.interfaces.config.ConfigChangeListener;
  26. import com.dmdirc.util.colours.Colour;
  27. import com.google.common.annotations.VisibleForTesting;
  28. import java.util.Locale;
  29. import java.util.regex.Pattern;
  30. import javax.annotation.Nullable;
  31. import static com.google.common.base.Preconditions.checkArgument;
  32. /**
  33. * The styliser applies IRC styles to text. Styles are indicated by various control codes which are
  34. * a de-facto IRC standard.
  35. */
  36. public class Styliser implements ConfigChangeListener {
  37. /** Character used to indicate hyperlinks. */
  38. public static final char CODE_HYPERLINK = 5;
  39. /** Character used to indicate channel links. */
  40. public static final char CODE_CHANNEL = 6;
  41. /** Character used to indicate smilies. */
  42. public static final char CODE_SMILIE = 7;
  43. /** Character used to indicate nickname links. */
  44. public static final char CODE_NICKNAME = 16;
  45. /** The character used for tooltips. */
  46. public static final char CODE_TOOLTIP = 19;
  47. /** Internal chars. */
  48. private static final String INTERNAL_CHARS = String.valueOf(CODE_HYPERLINK)
  49. + CODE_NICKNAME + CODE_CHANNEL + CODE_SMILIE + CODE_TOOLTIP;
  50. /** Characters used for hyperlinks. */
  51. private static final String HYPERLINK_CHARS = Character.toString(CODE_HYPERLINK) + CODE_CHANNEL;
  52. /** Regexp to match characters which shouldn't be used in channel links. */
  53. private static final String RESERVED_CHARS = "[^\\s" + IRCControlCodes.BOLD + IRCControlCodes.COLOUR
  54. + IRCControlCodes.STOP + IRCControlCodes.COLOUR_HEX + IRCControlCodes.FIXED + IRCControlCodes.ITALIC
  55. + IRCControlCodes.UNDERLINE + CODE_CHANNEL + CODE_NICKNAME + IRCControlCodes.NEGATE
  56. + "\",]";
  57. /** Defines all characters treated as trailing punctuation that are illegal in URLs. */
  58. private static final String URL_PUNCT_ILLEGAL = "\"";
  59. /** Defines all characters treated as trailing punctuation that're legal in URLs. */
  60. private static final String URL_PUNCT_LEGAL = "';:!,\\.\\?";
  61. /** Defines all trailing punctuation. */
  62. private static final String URL_PUNCT = URL_PUNCT_ILLEGAL + URL_PUNCT_LEGAL;
  63. /** Defines all characters allowed in URLs that aren't treated as trailing punct. */
  64. private static final String URL_NOPUNCT = "a-z0-9$\\-_@&\\+\\*\\(\\)=/#%~\\|";
  65. /** Defines all characters allowed in URLs per W3C specs. */
  66. private static final String URL_CHARS = '[' + URL_PUNCT_LEGAL + URL_NOPUNCT
  67. + "]*[" + URL_NOPUNCT + "]+[" + URL_PUNCT_LEGAL + URL_NOPUNCT + "]*";
  68. /** The regular expression to use for marking up URLs. */
  69. private static final String URL_REGEXP = "(?i)((?>(?<!" + IRCControlCodes.COLOUR_HEX
  70. + "[a-f0-9]{5})[a-f]|[g-z+])+://" + URL_CHARS
  71. + "|(?<![a-z0-9:/])www\\." + URL_CHARS + ')';
  72. /** Regular expression for intelligent handling of closing brackets. */
  73. private static final String URL_INT1 = "(\\([^\\)" + HYPERLINK_CHARS
  74. + "]*(?:[" + HYPERLINK_CHARS + "][^" + HYPERLINK_CHARS + "]*["
  75. + HYPERLINK_CHARS + "])?[^\\)" + HYPERLINK_CHARS + "]*[" + HYPERLINK_CHARS
  76. + "][^" + HYPERLINK_CHARS + "]+)(\\)['\";:!,\\.\\)]*)([" + HYPERLINK_CHARS + "])";
  77. /** Regular expression for intelligent handling of trailing single and double quotes. */
  78. private static final String URL_INT2 = "(^(?:[^" + HYPERLINK_CHARS + "]+|["
  79. + HYPERLINK_CHARS + "][^" + HYPERLINK_CHARS + "][" + HYPERLINK_CHARS
  80. + "]))(['\"])([^" + HYPERLINK_CHARS + "]*?[" + HYPERLINK_CHARS + "][^"
  81. + HYPERLINK_CHARS + "]+)(\\1[" + URL_PUNCT + "]*)([" + HYPERLINK_CHARS + "])";
  82. /** Regular expression for intelligent handling of surrounding quotes. */
  83. private static final String URL_INT3 = "(['\"])([" + HYPERLINK_CHARS
  84. + "][^" + HYPERLINK_CHARS + "]+?)(\\1[^" + HYPERLINK_CHARS + "]*)(["
  85. + HYPERLINK_CHARS + "])";
  86. /** Regular expression for intelligent handling of trailing punctuation. */
  87. private static final String URL_INT4 = "([" + HYPERLINK_CHARS + "][^"
  88. + HYPERLINK_CHARS + "]+?)([" + URL_PUNCT + "]?)([" + HYPERLINK_CHARS + "])";
  89. /** The regular expression to use for marking up channels. */
  90. private static final String URL_CHANNEL = "(?i)(?<![^\\s\\+@\\-<>\\(\"',])([\\Q%s\\E]"
  91. + RESERVED_CHARS + "+)";
  92. /** Whether or not we should style links. */
  93. private boolean styleURIs;
  94. /** Whether or not we should style channel names. */
  95. private boolean styleChannels;
  96. /** Colour to use for URIs. */
  97. private Colour uriColour;
  98. /** Colour to use for channel names. */
  99. private Colour channelColour;
  100. /** Connection to get channel prefixes from, or null if not applicable. */
  101. @Nullable
  102. private final Connection connection;
  103. /** Config manager to retrieve settings from. */
  104. private final AggregateConfigProvider configManager;
  105. /** Colour manager to use to parse colours. */
  106. private final ColourManager colourManager;
  107. /**
  108. * Creates a new instance of Styliser.
  109. *
  110. * @param connection The {@link Connection} that this styliser is for. May be {@code null}.
  111. * @param configManager the {@link AggregateConfigProvider} to get settings from.
  112. * @param colourManager The {@link ColourManager} to get colours from.
  113. *
  114. * @since 0.6.3
  115. */
  116. public Styliser(@Nullable final Connection connection,
  117. final AggregateConfigProvider configManager,
  118. final ColourManager colourManager) {
  119. this.connection = connection;
  120. this.configManager = configManager;
  121. this.colourManager = colourManager;
  122. configManager.addChangeListener("ui", "linkcolour", this);
  123. configManager.addChangeListener("ui", "channelcolour", this);
  124. configManager.addChangeListener("ui", "stylelinks", this);
  125. configManager.addChangeListener("ui", "stylechannels", this);
  126. styleURIs = configManager.getOptionBool("ui", "stylelinks");
  127. styleChannels = configManager.getOptionBool("ui", "stylechannels");
  128. uriColour = colourManager.getColourFromString(
  129. configManager.getOptionString("ui", "linkcolour"), null);
  130. channelColour = colourManager.getColourFromString(
  131. configManager.getOptionString("ui", "channelcolour"), null);
  132. }
  133. /**
  134. * Stylises the specified strings and adds them to the specified maker.
  135. *
  136. * @param maker The message maker to add styling to.
  137. * @param strings The lines to be stylised
  138. */
  139. public void addStyledString(final StyledMessageMaker<?> maker, final String... strings) {
  140. maker.resetAllStyles();
  141. for (String string : strings) {
  142. final char[] chars = string.toCharArray();
  143. for (int j = 0; j < chars.length; j++) {
  144. if (chars[j] == 65533) {
  145. chars[j] = '?';
  146. }
  147. }
  148. int position = 0;
  149. final String target =
  150. doSmilies(doLinks(new String(chars).replaceAll(INTERNAL_CHARS, "")));
  151. final StyliserState state = new StyliserState();
  152. while (position < target.length()) {
  153. final String next = readUntilControl(target.substring(position));
  154. maker.appendString(next);
  155. position += next.length();
  156. if (position < target.length()) {
  157. position += readControlChars(target.substring(position), state, maker,
  158. position == 0);
  159. }
  160. }
  161. }
  162. }
  163. /**
  164. * Retrieves the styled String contained within the unstyled offsets specified. That is, the
  165. * <code>from</code> and <code>to</code> arguments correspond to indexes in an unstyled version
  166. * of the <code>styled</code> string. The unstyled indices are translated to offsets within the
  167. * styled String, and the return value includes all text and control codes between those
  168. * indices.
  169. * <p>
  170. * The index translation is left-biased; that is, the indices are translated to be as far left
  171. * as they possibly can be. This means that the start of the string will include any control
  172. * codes immediately preceding the desired text, and the end will not include any trailing
  173. * codes.
  174. * <p>
  175. * This method will NOT include "internal" control codes in the output.
  176. *
  177. * @param styled The styled String to be operated on
  178. * @param from The starting index in the unstyled string
  179. * @param to The ending index in the unstyled string
  180. *
  181. * @return The corresponding text between the two indices
  182. *
  183. * @since 0.6.3
  184. * @deprecated Use {@link StyledMessageUtils}
  185. */
  186. @Deprecated
  187. public static String getStyledText(final String styled, final int from, final int to) {
  188. checkArgument(from < to, "'from' (" + from + ") must be less than 'to' (" + to + ')');
  189. checkArgument(from >= 0, "'from' (" + from + ") must be non-negative");
  190. final String unstyled = stipControlCodes(styled);
  191. checkArgument(to <= unstyled.length(), "'to' (" + to + ") must be less than or equal to "
  192. + "the unstyled length (" + unstyled.length() + ')');
  193. final String startBit = unstyled.substring(0, from);
  194. final String middleBit = unstyled.substring(from, to);
  195. final String sanitised = stipInternalControlCodes(styled);
  196. int start = from;
  197. while (!stipControlCodes(sanitised.substring(0, start)).equals(startBit)) {
  198. start++;
  199. }
  200. int end = to + start - from;
  201. while (!stipControlCodes(sanitised.substring(start, end)).equals(middleBit)) {
  202. end++;
  203. }
  204. return sanitised.substring(start, end);
  205. }
  206. /**
  207. * Applies the hyperlink styles and intelligent linking regexps to the target.
  208. *
  209. * @param string The string to be linked
  210. *
  211. * @return A copy of the string with hyperlinks marked up
  212. */
  213. public String doLinks(final String string) {
  214. String target = string;
  215. final String prefixes = connection == null ? null
  216. : connection.getGroupChatManager().getChannelPrefixes();
  217. String target2 = target;
  218. target = target.replaceAll(URL_REGEXP, CODE_HYPERLINK + "$0" + CODE_HYPERLINK);
  219. if (prefixes != null) {
  220. target = target.replaceAll(String.format(URL_CHANNEL, prefixes),
  221. CODE_CHANNEL + "$0" + CODE_CHANNEL);
  222. }
  223. for (int j = 0; j < 5 && !target.equals(target2); j++) {
  224. target2 = target;
  225. target = target
  226. .replaceAll(URL_INT1, "$1$3$2")
  227. .replaceAll(URL_INT2, "$1$2$3$5$4")
  228. .replaceAll(URL_INT3, "$1$2$4$3")
  229. .replaceAll(URL_INT4, "$1$3$2");
  230. }
  231. return target;
  232. }
  233. /**
  234. * Applies the smilie styles to the target.
  235. *
  236. * @param string The string to be smilified
  237. *
  238. * @return A copy of the string with smilies marked up
  239. *
  240. * @since 0.6.3m1
  241. */
  242. private String doSmilies(final String string) {
  243. // TODO: Check if they're enabled.
  244. // TODO: Store the list instead of building it every line
  245. final StringBuilder smilies = new StringBuilder();
  246. configManager.getOptions("icon").entrySet().stream()
  247. .filter(icon -> icon.getKey().startsWith("smilie-")).forEach(icon -> {
  248. if (smilies.length() > 0) {
  249. smilies.append('|');
  250. }
  251. smilies.append(Pattern.quote(icon.getKey().substring(7)));
  252. });
  253. return string.replaceAll("(\\s|^)(" + smilies + ")(?=\\s|$)",
  254. "$1" + CODE_SMILIE + "$2" + CODE_SMILIE);
  255. }
  256. /**
  257. * Strips all recognised control codes from the input string.
  258. *
  259. * @param input the String to be stripped
  260. *
  261. * @return a copy of the input with control codes removed
  262. * @deprecated Use {@link StyledMessageUtils}
  263. */
  264. @Deprecated
  265. public static String stipControlCodes(final String input) {
  266. return input.replaceAll("[" + IRCControlCodes.BOLD + CODE_CHANNEL + IRCControlCodes.FIXED
  267. + CODE_HYPERLINK + IRCControlCodes.ITALIC + IRCControlCodes.NEGATE + CODE_NICKNAME
  268. + CODE_SMILIE + IRCControlCodes.STOP + IRCControlCodes.UNDERLINE + "]|"
  269. + IRCControlCodes.COLOUR_HEX + "([A-Za-z0-9]{6}(,[A-Za-z0-9]{6})?)?|"
  270. + IRCControlCodes.COLOUR + "([0-9]{1,2}(,[0-9]{1,2})?)?", "")
  271. .replaceAll(CODE_TOOLTIP + ".*?" + CODE_TOOLTIP + "(.*?)" + CODE_TOOLTIP, "$1");
  272. }
  273. /**
  274. * St(r)ips all recognised internal control codes from the input string.
  275. *
  276. * @param input the String to be stripped
  277. *
  278. * @return a copy of the input with control codes removed
  279. *
  280. * @since 0.6.5
  281. */
  282. @Deprecated
  283. private static String stipInternalControlCodes(final String input) {
  284. return input.replaceAll("[" + CODE_CHANNEL + CODE_HYPERLINK + CODE_NICKNAME
  285. + CODE_SMILIE + IRCControlCodes.STOP + IRCControlCodes.UNDERLINE + ']', "")
  286. .replaceAll(CODE_TOOLTIP + ".*?" + CODE_TOOLTIP + "(.*?)"
  287. + CODE_TOOLTIP, "$1");
  288. }
  289. /**
  290. * Returns a substring of the input string such that no control codes are present in the output.
  291. * If the returned value isn't the same as the input, then the character immediately after is a
  292. * control character.
  293. *
  294. * @param input The string to read from
  295. *
  296. * @return A substring of the input containing no control characters
  297. */
  298. @VisibleForTesting
  299. static String readUntilControl(final String input) {
  300. int pos = input.length();
  301. pos = checkChar(pos, input.indexOf(IRCControlCodes.BOLD));
  302. pos = checkChar(pos, input.indexOf(IRCControlCodes.UNDERLINE));
  303. pos = checkChar(pos, input.indexOf(IRCControlCodes.STOP));
  304. pos = checkChar(pos, input.indexOf(IRCControlCodes.COLOUR));
  305. pos = checkChar(pos, input.indexOf(IRCControlCodes.COLOUR_HEX));
  306. pos = checkChar(pos, input.indexOf(IRCControlCodes.ITALIC));
  307. pos = checkChar(pos, input.indexOf(IRCControlCodes.FIXED));
  308. pos = checkChar(pos, input.indexOf(CODE_HYPERLINK));
  309. pos = checkChar(pos, input.indexOf(CODE_NICKNAME));
  310. pos = checkChar(pos, input.indexOf(CODE_CHANNEL));
  311. pos = checkChar(pos, input.indexOf(CODE_SMILIE));
  312. pos = checkChar(pos, input.indexOf(IRCControlCodes.NEGATE));
  313. pos = checkChar(pos, input.indexOf(CODE_TOOLTIP));
  314. return input.substring(0, pos);
  315. }
  316. /**
  317. * Helper function used in readUntilControl. Checks if i is a valid index of the string (i.e.,
  318. * it's not -1), and then returns the minimum of pos and i.
  319. *
  320. * @param pos The current position in the string
  321. * @param i The index of the first occurrence of some character
  322. *
  323. * @return The new position (see implementation)
  324. */
  325. private static int checkChar(final int pos, final int i) {
  326. if (i < pos && i != -1) {
  327. return i;
  328. }
  329. return pos;
  330. }
  331. /**
  332. * Reads the first control character from the input string (and any arguments it takes), and
  333. * applies it to the specified attribute set.
  334. *
  335. * @return The number of characters read as control characters
  336. *@param string The string to read from
  337. * @param maker The attribute set that new attributes will be applied to
  338. * @param isStart Whether this is at the start of the string or not
  339. */
  340. private int readControlChars(final String string,
  341. final StyliserState state,
  342. final StyledMessageMaker<?> maker, final boolean isStart) {
  343. final boolean isNegated = state.isNegated;
  344. // Bold
  345. if (string.charAt(0) == IRCControlCodes.BOLD) {
  346. if (!isNegated) {
  347. maker.toggleBold();
  348. }
  349. return 1;
  350. }
  351. // Underline
  352. if (string.charAt(0) == IRCControlCodes.UNDERLINE) {
  353. if (!isNegated) {
  354. maker.toggleUnderline();
  355. }
  356. return 1;
  357. }
  358. // Italic
  359. if (string.charAt(0) == IRCControlCodes.ITALIC) {
  360. if (!isNegated) {
  361. maker.toggleItalic();
  362. }
  363. return 1;
  364. }
  365. // Hyperlinks
  366. if (string.charAt(0) == CODE_HYPERLINK) {
  367. if (!isNegated && styleURIs) {
  368. maker.toggleHyperlinkStyle(uriColour);
  369. }
  370. if (state.isInLink) {
  371. maker.endHyperlink();
  372. } else {
  373. maker.startHyperlink(readUntilControl(string.substring(1)));
  374. }
  375. state.isInLink = !state.isInLink;
  376. return 1;
  377. }
  378. // Channel links
  379. if (string.charAt(0) == CODE_CHANNEL) {
  380. if (!isNegated && styleChannels) {
  381. maker.toggleChannelLinkStyle(channelColour);
  382. }
  383. if (state.isInLink) {
  384. maker.endChannelLink();
  385. } else {
  386. maker.startChannelLink(readUntilControl(string.substring(1)));
  387. }
  388. state.isInLink = !state.isInLink;
  389. return 1;
  390. }
  391. // Nickname links
  392. if (string.charAt(0) == CODE_NICKNAME) {
  393. if (state.isInLink) {
  394. maker.endNicknameLink();
  395. } else {
  396. maker.startNicknameLink(readUntilControl(string.substring(1)));
  397. }
  398. state.isInLink = !state.isInLink;
  399. return 1;
  400. }
  401. // Fixed pitch
  402. if (string.charAt(0) == IRCControlCodes.FIXED) {
  403. if (!isNegated) {
  404. maker.toggleFixedWidth();
  405. }
  406. return 1;
  407. }
  408. // Stop formatting
  409. if (string.charAt(0) == IRCControlCodes.STOP) {
  410. if (!isNegated) {
  411. maker.resetAllStyles();
  412. }
  413. return 1;
  414. }
  415. // Colours
  416. if (string.charAt(0) == IRCControlCodes.COLOUR) {
  417. int count = 1;
  418. // This isn't too nice!
  419. if (string.length() > count && isInt(string.charAt(count))) {
  420. int foreground = string.charAt(count) - '0';
  421. count++;
  422. if (string.length() > count && isInt(string.charAt(count))) {
  423. foreground = foreground * 10 + string.charAt(count) - '0';
  424. count++;
  425. }
  426. foreground %= 16;
  427. if (!isNegated) {
  428. maker.setForeground(colourManager.getColourFromString(
  429. String.valueOf(foreground), Colour.WHITE));
  430. if (isStart) {
  431. maker.setDefaultForeground(colourManager
  432. .getColourFromString(String.valueOf(foreground), Colour.WHITE));
  433. }
  434. }
  435. // Now background
  436. if (string.length() > count && string.charAt(count) == ','
  437. && string.length() > count + 1
  438. && isInt(string.charAt(count + 1))) {
  439. int background = string.charAt(count + 1) - '0';
  440. count += 2; // Comma and first digit
  441. if (string.length() > count && isInt(string.charAt(count))) {
  442. background = background * 10 + string.charAt(count) - '0';
  443. count++;
  444. }
  445. background %= 16;
  446. if (!isNegated) {
  447. maker.setBackground(colourManager
  448. .getColourFromString(String.valueOf(background), Colour.WHITE));
  449. if (isStart) {
  450. maker.setDefaultBackground(colourManager
  451. .getColourFromString(String.valueOf(background), Colour.WHITE));
  452. }
  453. }
  454. }
  455. } else if (!isNegated) {
  456. maker.resetColours();
  457. }
  458. return count;
  459. }
  460. // Hex colours
  461. if (string.charAt(0) == IRCControlCodes.COLOUR_HEX) {
  462. int count = 1;
  463. if (hasHexString(string, 1)) {
  464. if (!isNegated) {
  465. maker.setForeground(
  466. colourManager.getColourFromString(string.substring(1, 7).toUpperCase(),
  467. Colour.WHITE));
  468. if (isStart) {
  469. maker.setDefaultForeground(
  470. colourManager.getColourFromString(
  471. string.substring(1, 7).toUpperCase(), Colour.WHITE));
  472. }
  473. }
  474. count += 6;
  475. if (string.length() == count) {
  476. return count;
  477. }
  478. // Now for background
  479. if (string.charAt(count) == ',' && hasHexString(string, count + 1)) {
  480. count++;
  481. if (!isNegated) {
  482. maker.setBackground(colourManager.getColourFromString(
  483. string.substring(count, count + 6).toUpperCase(), Colour.WHITE));
  484. if (isStart) {
  485. maker.setDefaultBackground(colourManager.getColourFromString(
  486. string.substring(count, count + 6).toUpperCase(), Colour.WHITE));
  487. }
  488. }
  489. count += 6;
  490. }
  491. } else if (!isNegated) {
  492. maker.resetColours();
  493. }
  494. return count;
  495. }
  496. // Control code negation
  497. if (string.charAt(0) == IRCControlCodes.NEGATE) {
  498. state.isNegated = !state.isNegated;
  499. return 1;
  500. }
  501. // Smilies!!
  502. if (string.charAt(0) == CODE_SMILIE) {
  503. if (state.isInSmilie) {
  504. maker.endSmilie();
  505. } else {
  506. maker.startSmilie("smilie-" + readUntilControl(string.substring(1)));
  507. }
  508. state.isInSmilie = !state.isInSmilie;
  509. return 1;
  510. }
  511. // Tooltips
  512. if (string.charAt(0) == CODE_TOOLTIP) {
  513. if (state.isInToolTip) {
  514. maker.endToolTip();
  515. } else {
  516. final int index = string.indexOf(CODE_TOOLTIP, 1);
  517. if (index == -1) {
  518. // Doesn't make much sense, let's ignore it!
  519. return 1;
  520. }
  521. final String tooltip = string.substring(1, index);
  522. maker.startToolTip(tooltip);
  523. state.isInToolTip = !state.isInToolTip;
  524. return tooltip.length() + 2;
  525. }
  526. state.isInToolTip = !state.isInToolTip;
  527. return 1;
  528. }
  529. return 0;
  530. }
  531. /**
  532. * Determines if the specified character represents a single integer (i.e. 0-9).
  533. *
  534. * @param c The character to check
  535. *
  536. * @return True iff the character is in the range [0-9], false otherwise
  537. */
  538. private static boolean isInt(final char c) {
  539. return c >= '0' && c <= '9';
  540. }
  541. /**
  542. * Determines if the specified character represents a single hex digit (i.e., 0-F).
  543. *
  544. * @param c The character to check
  545. *
  546. * @return True iff the character is in the range [0-F], false otherwise
  547. */
  548. private static boolean isHex(final char c) {
  549. return isInt(c) || c >= 'A' && c <= 'F';
  550. }
  551. /**
  552. * Determines if the specified string has a 6-digit hex string starting at the specified offset.
  553. *
  554. * @param input The string to check
  555. * @param offset The offset to start at
  556. *
  557. * @return True iff there is a hex string preset at the offset
  558. */
  559. private static boolean hasHexString(final String input, final int offset) {
  560. // If the string's too short, it can't have a hex string
  561. if (input.length() < offset + 6) {
  562. return false;
  563. }
  564. boolean res = true;
  565. for (int i = offset; i < 6 + offset; i++) {
  566. res = res && isHex(input.toUpperCase(Locale.getDefault()).charAt(i));
  567. }
  568. return res;
  569. }
  570. @Override
  571. public void configChanged(final String domain, final String key) {
  572. switch (key) {
  573. case "stylelinks":
  574. styleURIs = configManager.getOptionBool("ui", "stylelinks");
  575. break;
  576. case "stylechannels":
  577. styleChannels = configManager.getOptionBool("ui", "stylechannels");
  578. break;
  579. case "linkcolour":
  580. uriColour = colourManager.getColourFromString(
  581. configManager.getOptionString("ui", "linkcolour"), null);
  582. break;
  583. case "channelcolour":
  584. channelColour = colourManager.getColourFromString(
  585. configManager.getOptionString("ui", "channelcolour"), null);
  586. break;
  587. }
  588. }
  589. private static class StyliserState {
  590. boolean isNegated;
  591. boolean isInLink;
  592. boolean isInSmilie;
  593. boolean isInToolTip;
  594. }
  595. }