123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- /*
- * Copyright (c) 2006-2014 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.actions.internal;
-
- import com.dmdirc.actions.ActionManager;
- import com.dmdirc.actions.CoreActionType;
- import com.dmdirc.interfaces.ActionListener;
- import com.dmdirc.interfaces.Connection;
- import com.dmdirc.interfaces.actions.ActionType;
- import com.dmdirc.interfaces.config.ConfigProvider;
-
- import java.util.HashMap;
- import java.util.Map;
-
- /**
- * Listens for whois-like numeric events and automatically formats them.
- *
- * @since 0.6.3
- */
- public class WhoisNumericFormatter implements ActionListener {
-
- /** The name of the target of any current whois requests. */
- private final Map<Connection, String> targets = new HashMap<>();
- /** The identity to add formatters to. */
- private final ConfigProvider identity;
-
- /**
- * Creates a new whois numeric formatter that will add automatic formats to the specified
- * identity. This will normally be a temporary global identity.
- *
- * @param identity The identity to write formatters to
- */
- public WhoisNumericFormatter(final ConfigProvider identity) {
- this.identity = identity;
- }
-
- /**
- * Registers this this whois numeric formatter with the global actions manager.
- */
- public void register() {
- ActionManager.getActionManager().registerListener(this,
- CoreActionType.SERVER_NUMERIC,
- CoreActionType.SERVER_DISCONNECTED);
- }
-
- /** {@inheritDoc} */
- @Override
- public void processEvent(final ActionType type, final StringBuffer format,
- final Object... arguments) {
- if (CoreActionType.SERVER_DISCONNECTED == type) {
- handleServerDisconnected((Connection) arguments[0]);
- } else {
- handleNumeric((Connection) arguments[0], (Integer) arguments[1],
- (String[]) arguments[2], format);
- }
- }
-
- /**
- * Handles a server disconnected event. This clears any entry for that server in the
- * {@link #targets} map.
- *
- * @param server The server that was disconnected
- */
- private void handleServerDisconnected(final Connection server) {
- targets.remove(server);
- }
-
- /**
- * Handles a received numeric event. This method has special handling for numerics 311 and 318,
- * used to signal the start and end of a WHOIS request. It then monitors any other numerics
- * without formatters for events which look like WHOIS information, and formats them
- * automatically.
- *
- * @param server The server on which the event was received
- * @param numeric The numeric code of the event
- * @param arguments The arguments to the numeric event
- * @param format The format that should be used to display the event
- */
- private void handleNumeric(final Connection server, final int numeric,
- final String[] arguments, final StringBuffer format) {
- switch (numeric) {
- case 311: // RPL_WHOISUSER
- targets.put(server, arguments[3]);
- break;
- case 318: // RPL_ENDOFWHOIS
- targets.remove(server);
- break;
- default:
- if (arguments.length > 4
- && targets.containsKey(server)
- && arguments[3].equals(targets.get(server))) {
- // This numeric should be automatically formatted.
-
- if (format.length() > 0) {
- // There's a custom format. We'll see if we need to
- // add a formatter or notification settings for it
- // anyway.
- ensureExists(format.toString(), arguments.length);
- } else {
- // No custom formatter, switch it to an auto whois
- // format and target.
- final String target = "numeric_autowhois_" + (arguments.length - 4);
- ensureExists(target, arguments.length);
- format.replace(0, format.length(), target);
- }
- }
- break;
- }
- }
-
- /**
- * Ensures that the specified formatter exists in our identity.
- *
- * @param target The target to be checked and added if necessary
- * @param arguments The number of arguments for the numeric
- */
- private void ensureExists(final String target, final int arguments) {
- if (!identity.hasOptionString("formatter", target)) {
- final StringBuilder builder = new StringBuilder("%4$s %" + arguments + "$s");
- for (int i = 5; i < arguments; i++) {
- builder.append(" %");
- builder.append(i);
- builder.append("$s");
- }
-
- identity.setOption("formatter", target, builder.toString());
- }
-
- if (!identity.hasOptionString("notifications", target)) {
- identity.setOption("notifications", target, "group:whois");
- }
- }
-
- }
|