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.

NickColourManager.java 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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.nickcolours;
  23. import com.dmdirc.ClientModule.GlobalConfig;
  24. import com.dmdirc.DMDircMBassador;
  25. import com.dmdirc.events.ChannelGotnamesEvent;
  26. import com.dmdirc.events.ChannelJoinEvent;
  27. import com.dmdirc.events.DisplayProperty;
  28. import com.dmdirc.interfaces.GroupChatUser;
  29. import com.dmdirc.interfaces.User;
  30. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  31. import com.dmdirc.interfaces.config.ConfigChangeListener;
  32. import com.dmdirc.interfaces.config.ReadOnlyConfigProvider;
  33. import com.dmdirc.parser.interfaces.StringConverter;
  34. import com.dmdirc.plugins.PluginDomain;
  35. import com.dmdirc.ui.messages.ColourManager;
  36. import com.dmdirc.util.colours.Colour;
  37. import java.util.ArrayList;
  38. import java.util.Collection;
  39. import java.util.List;
  40. import javax.inject.Inject;
  41. import javax.inject.Singleton;
  42. import net.engio.mbassy.listener.Handler;
  43. /**
  44. * Provides various features related to nickname colouring.
  45. */
  46. @Singleton
  47. public class NickColourManager implements ConfigChangeListener {
  48. /** Manager to parse colours with. */
  49. private final ColourManager colourManager;
  50. /** Config to read settings from. */
  51. private final AggregateConfigProvider globalConfig;
  52. /** Plugin's setting domain. */
  53. private final String domain;
  54. /** Event bus to subscribe to events on . */
  55. private final DMDircMBassador eventBus;
  56. /** "Random" colours to use to colour nicknames. */
  57. private String[] randColours = {
  58. "E90E7F", "8E55E9", "B30E0E", "18B33C", "58ADB3", "9E54B3", "B39875", "3176B3",};
  59. private boolean useowncolour;
  60. private String owncolour;
  61. private boolean userandomcolour;
  62. @Inject
  63. public NickColourManager(@GlobalConfig final ColourManager colourManager,
  64. @PluginDomain(NickColourPlugin.class) final String domain,
  65. @GlobalConfig final AggregateConfigProvider globalConfig, final DMDircMBassador eventBus) {
  66. this.domain = domain;
  67. this.globalConfig = globalConfig;
  68. this.colourManager = colourManager;
  69. this.eventBus = eventBus;
  70. }
  71. @Handler
  72. public void handleChannelNames(final ChannelGotnamesEvent event) {
  73. final String network = event.getChannel().getConnection().get().getNetwork();
  74. event.getChannel().getUsers().forEach(client -> colourClient(network, client));
  75. }
  76. @Handler
  77. public void handleChannelJoin(final ChannelJoinEvent event) {
  78. final String network = event.getChannel().getConnection().get().getNetwork();
  79. colourClient(network, event.getClient());
  80. }
  81. /**
  82. * Colours the specified client according to the user's config.
  83. *
  84. * @param network The network to use for the colouring
  85. * @param client The client to be coloured
  86. */
  87. private void colourClient(final String network, final GroupChatUser client) {
  88. final StringConverter sc = client.getUser().getConnection().getParser().get()
  89. .getStringConverter();
  90. final User myself = client.getUser();
  91. final String nickOption1 = "color:" + sc.toLowerCase(network + ':' + client.getNickname());
  92. final String nickOption2 = "color:" + sc.toLowerCase("*:" + client.getNickname());
  93. if (useowncolour && client.getUser().equals(myself)) {
  94. final Colour color = colourManager.getColourFromString(owncolour, null);
  95. putColour(client, color);
  96. } else if (userandomcolour) {
  97. putColour(client, getColour(client.getNickname()));
  98. }
  99. String[] parts = null;
  100. if (globalConfig.hasOptionString(domain, nickOption1)) {
  101. parts = getParts(globalConfig, domain, nickOption1);
  102. } else if (globalConfig.hasOptionString(domain, nickOption2)) {
  103. parts = getParts(globalConfig, domain, nickOption2);
  104. }
  105. if (parts != null) {
  106. Colour textColor = null;
  107. if (parts[0] != null) {
  108. textColor = colourManager.getColourFromString(parts[0], null);
  109. }
  110. putColour(client, textColor);
  111. }
  112. }
  113. /**
  114. * Puts the specified colour into the given map. The keys are determined by config settings.
  115. *
  116. * @param user The map to colour
  117. * @param colour Text colour to be inserted
  118. */
  119. private void putColour(final GroupChatUser user, final Colour colour) {
  120. user.setDisplayProperty(DisplayProperty.FOREGROUND_COLOUR, colour);
  121. }
  122. /**
  123. * Retrieves a pseudo-random colour for the specified nickname.
  124. *
  125. * @param nick The nickname of the client whose colour we're determining
  126. *
  127. * @return Colour of the specified nickname
  128. */
  129. private Colour getColour(final CharSequence nick) {
  130. int count = 0;
  131. for (int i = 0; i < nick.length(); i++) {
  132. count += nick.charAt(i);
  133. }
  134. count %= randColours.length;
  135. return colourManager.getColourFromString(randColours[count], null);
  136. }
  137. /**
  138. * Reads the nick colour data from the config.
  139. *
  140. * @param config Config to read settings from
  141. * @param domain Config domain
  142. *
  143. * @return A multi-dimensional array of nick colour info.
  144. */
  145. public static Object[][] getData(final ReadOnlyConfigProvider config, final String domain) {
  146. final Collection<Object[]> data = new ArrayList<>();
  147. config.getOptions(domain).keySet().stream().filter(key -> key.startsWith("color:"))
  148. .forEach(key -> {
  149. final String network = key.substring(6, key.indexOf(':', 6));
  150. final String user = key.substring(1 + key.indexOf(':', 6));
  151. final String[] parts = getParts(config, domain, key);
  152. data.add(new Object[]{network, user, parts[0], parts[1]});
  153. });
  154. final Object[][] res = new Object[data.size()][4];
  155. int i = 0;
  156. for (Object[] row : data) {
  157. res[i] = row;
  158. i++;
  159. }
  160. return res;
  161. }
  162. /**
  163. * Retrieves the config option with the specified key, and returns an array of the colours that
  164. * should be used for it.
  165. *
  166. * @param config Config to read settings from
  167. * @param domain Config domain
  168. * @param key The config key to look up
  169. *
  170. * @return The colours specified by the given key
  171. */
  172. private static String[] getParts(final ReadOnlyConfigProvider config, final String domain,
  173. final String key) {
  174. String[] parts = config.getOption(domain, key).split(":");
  175. if (parts.length == 0) {
  176. parts = new String[]{null, null};
  177. } else if (parts.length == 1) {
  178. parts = new String[]{parts[0], null};
  179. } else if (parts.length == 2) {
  180. parts = new String[]{parts[0], parts[1]};
  181. }
  182. return parts;
  183. }
  184. /**
  185. * Loads this plugin.
  186. */
  187. public void onLoad() {
  188. setCachedSettings();
  189. eventBus.subscribe(this);
  190. }
  191. /**
  192. * Unloads this plugin.
  193. */
  194. public void onUnload() {
  195. eventBus.unsubscribe(this);
  196. }
  197. /**
  198. * Updates cached settings.
  199. */
  200. private void setCachedSettings() {
  201. useowncolour = globalConfig.getOptionBool(domain, "useowncolour");
  202. owncolour = globalConfig.getOption(domain, "owncolour");
  203. userandomcolour = globalConfig.getOptionBool(domain, "userandomcolour");
  204. if (globalConfig.hasOptionString(domain, "randomcolours")) {
  205. final List<String> list = globalConfig.getOptionList(domain, "randomcolours");
  206. randColours = list.toArray(new String[list.size()]);
  207. }
  208. }
  209. @Override
  210. public void configChanged(final String domain, final String key) {
  211. setCachedSettings();
  212. }
  213. }