File Source: UserLevelPlugin.java
1 /*
2 * Copyright (c) 2006-2009 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
23 package com.dmdirc.addons.userlevel;
24
25 import com.dmdirc.actions.ActionManager;
26 import com.dmdirc.actions.CoreActionType;
27 import com.dmdirc.actions.interfaces.ActionComponent;
28 import com.dmdirc.actions.interfaces.ActionType;
29 import com.dmdirc.config.IdentityManager;
30 import com.dmdirc.interfaces.ActionListener;
31 import com.dmdirc.interfaces.ConfigChangeListener;
32 import com.dmdirc.parser.irc.ChannelClientInfo;
33 import com.dmdirc.parser.irc.ClientInfo;
34 import com.dmdirc.plugins.Plugin;
35
36 import java.util.HashMap;
37 import java.util.Map;
38
39 /**
40 * Allows the client to assign user levels to users (based on hostname matches),
41 * and for actions/plugins to check those levels.
42 *
43 * @author chris
44 */
/*
P/P * Method: void com.dmdirc.addons.userlevel.UserLevelPlugin()
*/
45 public class UserLevelPlugin extends Plugin implements ActionListener,
46 ConfigChangeListener {
47
48 /** The domain used for userlevels. */
49 private static final String DOMAIN = "userlevels";
50
51 /** A map of hostmasks to associated level numbers. */
/*
P/P * Method: com.dmdirc.addons.userlevel.UserLevelPlugin__static_init
*
* Postconditions:
* LEVELS == &new HashMap(UserLevelPlugin__static_init#1)
* new HashMap(UserLevelPlugin__static_init#1) num objects == 1
*/
52 private static final Map<String, Integer> LEVELS = new HashMap<String, Integer>();
53
54 /** {@inheritDoc} */
55 @Override
56 public void onLoad() {
/*
P/P * Method: void onLoad()
*
* Preconditions:
* init'ed(com/dmdirc/config/IdentityManager.globalconfig)
* (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
*
* Presumptions:
* init'ed(com.dmdirc.actions.CoreActionType.CHANNEL_JOIN)
* getGlobalConfig(...).listeners != null
*
* Postconditions:
* com/dmdirc/config/IdentityManager.globalconfig != null
* java.lang.StringBuilder:toString(...)._tainted == 0
* new ArrayList(getSources#1) num objects == 0
* new ConfigManager(getGlobalConfig#1) num objects == 0
* new MapList(ConfigManager#1) num objects == 0
* new ArrayList(getSources#1) num objects <= 1
* new ConfigManager(getGlobalConfig#1) num objects == new ArrayList(getSources#1) num objects
* new MapList(ConfigManager#1) num objects == new ArrayList(getSources#1) num objects
* new ConfigManager(getGlobalConfig#1).channel == &java.lang.StringBuilder:toString(...)
* not_init'ed(new ConfigManager(getGlobalConfig#1).channel)
* ...
*/
57 ActionManager.addListener(this, CoreActionType.CHANNEL_JOIN);
58 ActionManager.registerActionComponents(
59 new ActionComponent[]{
60 new AccessLevelComponent(),
61 new ChannelAccessLevelComponent()
62 });
63 IdentityManager.getGlobalConfig().addChangeListener(DOMAIN, this);
64 loadLevels();
65 }
66
67 /** {@inheritDoc} */
68 @Override
69 public void onUnload() {
/*
P/P * Method: void onUnload()
*
* Preconditions:
* init'ed(com/dmdirc/config/IdentityManager.globalconfig)
* (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
*
* Presumptions:
* getGlobalConfig(...).listeners != null
*
* Postconditions:
* com/dmdirc/config/IdentityManager.globalconfig == One-of{old com/dmdirc/config/IdentityManager.globalconfig, &new ConfigManager(getGlobalConfig#1)}
* com/dmdirc/config/IdentityManager.globalconfig != null
* java.lang.StringBuilder:toString(...)._tainted == 0
* new ArrayList(getSources#1) num objects <= 1
* new ConfigManager(getGlobalConfig#1) num objects == new ArrayList(getSources#1) num objects
* new MapList(ConfigManager#1) num objects == new ArrayList(getSources#1) num objects
* new ConfigManager(getGlobalConfig#1).channel == &java.lang.StringBuilder:toString(...)
* new ConfigManager(getGlobalConfig#1).ircd == &""
* new ConfigManager(getGlobalConfig#1).network == &""
* new ConfigManager(getGlobalConfig#1).server == &""
* ...
*/
70 ActionManager.removeListener(this);
71 IdentityManager.getGlobalConfig().removeListener(this);
72 }
73
74 /** {@inheritDoc} */
75 @Override
76 public void processEvent(final ActionType type, final StringBuffer format,
77 final Object... arguments) {
/*
P/P * Method: void processEvent(ActionType, StringBuffer, Object[])
*
* Preconditions:
* type != null
* (soft) arguments != null
* (soft) arguments.length >= 2
* (soft) arguments[1] != null
* (soft) init'ed(com.dmdirc.addons.userlevel.UserLevelPlugin$1__static_init.new int[](UserLevelPlugin$1__static_init#1)[...])
*
* Presumptions:
* com.dmdirc.actions.CoreActionType:ordinal(...)@78 >= 0
* com.dmdirc.actions.CoreActionType:values(...).length >= 1
* com.dmdirc.actions.CoreActionType:ordinal(...)@78 < com.dmdirc.actions.CoreActionType:values(...).length
*
* Test Vectors:
* com.dmdirc.addons.userlevel.UserLevelPlugin$1__static_init.new int[](UserLevelPlugin$1__static_init#1)[...]: {1}, {-231..0, 2..232-1}
*/
78 switch ((CoreActionType) type) {
79 case CHANNEL_JOIN:
80 doChannelLevel((ChannelClientInfo) arguments[1]);
81 break;
82 }
83 }
84
85 /**
86 * Updates the specified channel client's channel user level.
87 *
88 * @param client The client whose user level is to be updated
89 */
90 protected static void doChannelLevel(final ChannelClientInfo client) {
/*
P/P * Method: void doChannelLevel(ChannelClientInfo)
*
* Preconditions:
* client != null
*
* Presumptions:
* com.dmdirc.parser.irc.ChannelClientInfo:getClient(...)@91 != null
*/
91 doGlobalLevel(client.getClient());
92 }
93
94 /**
95 * Updates the specified client's global user level.
96 *
97 * @param client The client whose user level is to be updated
98 */
99 @SuppressWarnings("unchecked")
100 protected static void doGlobalLevel(final ClientInfo client) {
/*
P/P * Method: void doGlobalLevel(ClientInfo)
*
* Preconditions:
* client != null
*
* Presumptions:
* com.dmdirc.parser.irc.ClientInfo:getMap(...)@114 != null
* java.util.Iterator:next(...)@107 != null
* java.util.Map:entrySet(...)@107 != null
* java.util.Map_Entry:getValue(...)@109 != null
*
* Test Vectors:
* java.lang.String:matches(...)@108: {0}, {1}
* java.util.Iterator:hasNext(...)@107: {1}, {0}
*/
101 final String host = client.getNickname() + "!" + client.getIdent()
102 + "@" + client.getHost();
103
104 int level = 0;
105
106 synchronized (LEVELS) {
107 for (Map.Entry<String, Integer> entry : LEVELS.entrySet()) {
108 if (host.matches(entry.getKey())) {
109 level = Math.max(level, entry.getValue());
110 }
111 }
112 }
113
114 client.getMap().put("level", level);
115 }
116
117 /** {@inheritDoc} */
118 @Override
119 public void configChanged(final String domain, final String key) {
/*
P/P * Method: void configChanged(String, String)
*
* Preconditions:
* (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
* (soft) init'ed(com/dmdirc/config/IdentityManager.globalconfig)
*
* Postconditions:
* com/dmdirc/config/IdentityManager.globalconfig == One-of{old com/dmdirc/config/IdentityManager.globalconfig, &new ConfigManager(getGlobalConfig#1)}
* init'ed(com/dmdirc/config/IdentityManager.globalconfig)
* java.lang.StringBuilder:toString(...)._tainted == 0
* new ArrayList(getSources#1) num objects <= 1
* new ConfigManager(getGlobalConfig#1) num objects <= 1
* init'ed(new ConfigManager(getGlobalConfig#1).channel)
* init'ed(new ConfigManager(getGlobalConfig#1).ircd)
* init'ed(new ConfigManager(getGlobalConfig#1).listeners)
* init'ed(new ConfigManager(getGlobalConfig#1).network)
* init'ed(new ConfigManager(getGlobalConfig#1).server)
* ...
*
* Test Vectors:
* java.lang.String:equals(...)@120: {0}, {1}
*/
120 if (DOMAIN.equals(domain)) {
121 loadLevels();
122 }
123 }
124
125 /**
126 * Loads all levels from the config file into our map.
127 */
128 private void loadLevels() {
/*
P/P * Method: void loadLevels()
*
* Preconditions:
* init'ed(com/dmdirc/config/IdentityManager.globalconfig)
* (soft) init'ed(com.dmdirc.config.ConfigManager$1__static_init.new int[](ConfigManager$1__static_init#1)[...])
*
* Presumptions:
* getGlobalConfig(...).sources != null
* java.util.Iterator:next(...)@132 != null
* java.util.Map:entrySet(...)@132 != null
*
* Postconditions:
* com/dmdirc/config/IdentityManager.globalconfig == One-of{old com/dmdirc/config/IdentityManager.globalconfig, &new ConfigManager(getGlobalConfig#1)}
* com/dmdirc/config/IdentityManager.globalconfig != null
* java.lang.StringBuilder:toString(...)._tainted == 0
* new ArrayList(getSources#1) num objects <= 1
* new ConfigManager(getGlobalConfig#1) num objects == new ArrayList(getSources#1) num objects
* new MapList(ConfigManager#1) num objects == new ArrayList(getSources#1) num objects
* new ConfigManager(getGlobalConfig#1).channel == &java.lang.StringBuilder:toString(...)
* new ConfigManager(getGlobalConfig#1).ircd == &""
* new ConfigManager(getGlobalConfig#1).network == &""
* new ConfigManager(getGlobalConfig#1).server == &""
* ...
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@132: {0}, {1}
*/
129 LEVELS.clear();
130
131 for (Map.Entry<String, String> item
132 : IdentityManager.getGlobalConfig().getOptions(DOMAIN).entrySet()) {
133 try {
134 LEVELS.put(item.getKey(), Integer.parseInt(item.getValue()));
135 } catch (NumberFormatException ex) {
136 LEVELS.put(item.getKey(), 0);
137 }
138 }
139 }
140
141 }
SofCheck Inspector Build Version : 2.17854
| UserLevelPlugin.java |
2009-Jun-25 01:54:24 |
| UserLevelPlugin.class |
2009-Sep-02 17:04:16 |
| UserLevelPlugin$1.class |
2009-Sep-02 17:04:16 |