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.

UserLevelPlugin.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.userlevel;
  23. import com.dmdirc.actions.ActionManager;
  24. import com.dmdirc.actions.CoreActionType;
  25. import com.dmdirc.actions.interfaces.ActionComponent;
  26. import com.dmdirc.actions.interfaces.ActionType;
  27. import com.dmdirc.config.IdentityManager;
  28. import com.dmdirc.interfaces.ActionListener;
  29. import com.dmdirc.interfaces.ConfigChangeListener;
  30. import com.dmdirc.parser.interfaces.ChannelClientInfo;
  31. import com.dmdirc.parser.interfaces.ClientInfo;
  32. import com.dmdirc.plugins.Plugin;
  33. import java.util.HashMap;
  34. import java.util.Map;
  35. /**
  36. * Allows the client to assign user levels to users (based on hostname matches),
  37. * and for actions/plugins to check those levels.
  38. *
  39. * @author chris
  40. */
  41. public class UserLevelPlugin extends Plugin implements ActionListener,
  42. ConfigChangeListener {
  43. /** The domain used for userlevels. */
  44. private static final String DOMAIN = "userlevels";
  45. /** A map of hostmasks to associated level numbers. */
  46. private static final Map<String, Integer> LEVELS = new HashMap<String, Integer>();
  47. /** {@inheritDoc} */
  48. @Override
  49. public void onLoad() {
  50. ActionManager.addListener(this, CoreActionType.CHANNEL_JOIN);
  51. ActionManager.registerActionComponents(
  52. new ActionComponent[]{
  53. new AccessLevelComponent(),
  54. new ChannelAccessLevelComponent()
  55. });
  56. IdentityManager.getGlobalConfig().addChangeListener(DOMAIN, this);
  57. loadLevels();
  58. }
  59. /** {@inheritDoc} */
  60. @Override
  61. public void onUnload() {
  62. ActionManager.removeListener(this);
  63. IdentityManager.getGlobalConfig().removeListener(this);
  64. }
  65. /** {@inheritDoc} */
  66. @Override
  67. public void processEvent(final ActionType type, final StringBuffer format,
  68. final Object... arguments) {
  69. switch ((CoreActionType) type) {
  70. case CHANNEL_JOIN:
  71. doChannelLevel((ChannelClientInfo) arguments[1]);
  72. break;
  73. }
  74. }
  75. /**
  76. * Updates the specified channel client's channel user level.
  77. *
  78. * @param client The client whose user level is to be updated
  79. */
  80. protected static void doChannelLevel(final ChannelClientInfo client) {
  81. doGlobalLevel(client.getClient());
  82. }
  83. /**
  84. * Updates the specified client's global user level.
  85. *
  86. * @param client The client whose user level is to be updated
  87. */
  88. @SuppressWarnings("unchecked")
  89. protected static void doGlobalLevel(final ClientInfo client) {
  90. final String host = client.getNickname() + "!" + client.getUsername()
  91. + "@" + client.getHostname();
  92. int level = 0;
  93. synchronized (LEVELS) {
  94. for (Map.Entry<String, Integer> entry : LEVELS.entrySet()) {
  95. if (host.matches(entry.getKey())) {
  96. level = Math.max(level, entry.getValue());
  97. }
  98. }
  99. }
  100. client.getMap().put("level", level);
  101. }
  102. /** {@inheritDoc} */
  103. @Override
  104. public void configChanged(final String domain, final String key) {
  105. if (DOMAIN.equals(domain)) {
  106. loadLevels();
  107. }
  108. }
  109. /**
  110. * Loads all levels from the config file into our map.
  111. */
  112. private void loadLevels() {
  113. LEVELS.clear();
  114. for (Map.Entry<String, String> item
  115. : IdentityManager.getGlobalConfig().getOptions(DOMAIN).entrySet()) {
  116. try {
  117. LEVELS.put(item.getKey(), Integer.parseInt(item.getValue()));
  118. } catch (NumberFormatException ex) {
  119. LEVELS.put(item.getKey(), 0);
  120. }
  121. }
  122. }
  123. }