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

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