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

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