Procházet zdrojové kódy

Auto numeric formatting

Fixes issue 3678

Squashed commit of the following:

commit 896c815005
Author: Chris Smith <chris@dmdirc.com>
Date:   Sat Jan 30 21:05:09 2010 +0000

    Add whois numeric formatter

    Change-Id: I474fe3778a9314212d69f6b0d4d95aec7ad3ecee

commit 11f13651ad
Author: Chris Smith <chris@dmdirc.com>
Date:   Fri Oct 24 19:02:05 2008 +0100

    No longer check for formatter.numeric_<ircd>_<xxx> settings

commit cc078694da
Author: Chris Smith <chris@dmdirc.com>
Date:   Fri Oct 24 18:59:29 2008 +0100

    Numerics with no applicable formatters can now be formatted by actions/plugins

Change-Id: I7c574c1f06f2d0bb77a4fe1ec855153f7e3d347e
Reviewed-on: http://gerrit.dmdirc.com/737
Reviewed-by: Gregory Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Local Commits <dmdirc@googlemail.com>
tags/0.6.3b1
Chris Smith před 14 roky
rodič
revize
dca79bf057

+ 3
- 8
src/com/dmdirc/Server.java Zobrazit soubor

@@ -1279,13 +1279,10 @@ public class Server extends WritableFrameContainer implements
1279 1279
             snumeric = "0" + snumeric;
1280 1280
         }
1281 1281
 
1282
-        final String withIrcd = "numeric_" + parser.getServerSoftwareType() + "_" + snumeric;
1283 1282
         final String sansIrcd = "numeric_" + snumeric;
1284
-        StringBuffer target = null;
1283
+        StringBuffer target = new StringBuffer("");
1285 1284
 
1286
-        if (getConfigManager().hasOptionString("formatter", withIrcd)) {
1287
-            target = new StringBuffer(withIrcd);
1288
-        } else if (getConfigManager().hasOptionString("formatter", sansIrcd)) {
1285
+        if (getConfigManager().hasOptionString("formatter", sansIrcd)) {
1289 1286
             target = new StringBuffer(sansIrcd);
1290 1287
         } else if (getConfigManager().hasOptionString("formatter", "numeric_unknown")) {
1291 1288
             target = new StringBuffer("numeric_unknown");
@@ -1294,9 +1291,7 @@ public class Server extends WritableFrameContainer implements
1294 1291
         ActionManager.processEvent(CoreActionType.SERVER_NUMERIC, target, this,
1295 1292
                 Integer.valueOf(numeric), tokens);
1296 1293
 
1297
-        if (target != null) {
1298
-            handleNotification(target.toString(), (Object[]) tokens);
1299
-        }
1294
+        handleNotification(target.toString(), (Object[]) tokens);
1300 1295
     }
1301 1296
 
1302 1297
     /**

+ 3
- 0
src/com/dmdirc/actions/ActionManager.java Zobrazit soubor

@@ -27,6 +27,7 @@ import com.dmdirc.Precondition;
27 27
 import com.dmdirc.actions.interfaces.ActionComparison;
28 28
 import com.dmdirc.actions.interfaces.ActionComponent;
29 29
 import com.dmdirc.actions.interfaces.ActionType;
30
+import com.dmdirc.actions.internal.WhoisNumericFormatter;
30 31
 import com.dmdirc.actions.wrappers.AliasWrapper;
31 32
 import com.dmdirc.actions.wrappers.PerformWrapper;
32 33
 import com.dmdirc.config.IdentityManager;
@@ -103,6 +104,8 @@ public final class ActionManager {
103 104
 
104 105
         registerGroup(AliasWrapper.getAliasWrapper());
105 106
         registerGroup(PerformWrapper.getPerformWrapper());
107
+
108
+        new WhoisNumericFormatter(IdentityManager.getAddonIdentity()).register();
106 109
        
107 110
         // Register a listener for the closing event, so we can save actions
108 111
         addListener(new ActionListener() {

+ 145
- 0
src/com/dmdirc/actions/internal/WhoisNumericFormatter.java Zobrazit soubor

@@ -0,0 +1,145 @@
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
+
23
+package com.dmdirc.actions.internal;
24
+
25
+import com.dmdirc.Server;
26
+import com.dmdirc.actions.ActionManager;
27
+import com.dmdirc.actions.CoreActionType;
28
+import com.dmdirc.actions.interfaces.ActionType;
29
+import com.dmdirc.config.Identity;
30
+import com.dmdirc.config.IdentityManager;
31
+import com.dmdirc.interfaces.ActionListener;
32
+import java.util.HashMap;
33
+import java.util.Map;
34
+
35
+/**
36
+ * Listens for whois-like numeric events and automatically formats them.
37
+ *
38
+ * @since 0.6.3
39
+ * @author chris
40
+ */
41
+public class WhoisNumericFormatter implements ActionListener {
42
+
43
+    /** The name of the target of any current whois requests. */
44
+    private final Map<Server, String> targets = new HashMap<Server, String>();
45
+
46
+    /** The identity to add formatters to. */
47
+    private final Identity identity;
48
+
49
+    /**
50
+     * Creates a new whois numeric formatter that will add automatic formats
51
+     * to the specified identity. This will normally be a temporary global
52
+     * identity, such as the one returned by
53
+     * {@link IdentityManager#getAddonIdentity()}.
54
+     *
55
+     * @param identity The identity to write formatters to
56
+     */
57
+    public WhoisNumericFormatter(final Identity identity) {
58
+        this.identity = identity;
59
+    }
60
+
61
+    /**
62
+     * Registers this this whois numeric formatter with the global actions
63
+     * manager.
64
+     */
65
+    public void register() {
66
+        ActionManager.addListener(this, CoreActionType.SERVER_NUMERIC,
67
+                CoreActionType.SERVER_DISCONNECTED);
68
+    }
69
+
70
+    /** {@inheritDoc} */
71
+    @Override
72
+    public void processEvent(final ActionType type, final StringBuffer format,
73
+            final Object... arguments) {
74
+        if (CoreActionType.SERVER_DISCONNECTED == type) {
75
+            handleServerDisconnected((Server) arguments[0]);
76
+        } else {
77
+            handleNumeric((Server) arguments[0], (Integer) arguments[1],
78
+                    (String[]) arguments[2], format);
79
+        }
80
+    }
81
+
82
+    /**
83
+     * Handles a server disconnected event. This clears any entry for that
84
+     * server in the <code>target</code> map.
85
+     *
86
+     * @param server The server that was disconnected
87
+     */
88
+    private void handleServerDisconnected(final Server server) {
89
+        targets.remove(server);
90
+    }
91
+
92
+    /**
93
+     * Handles a received numeric event. This method has special handling for
94
+     * numerics 311 and 318, used to signal the start and end of a WHOIS
95
+     * request. It then monitors any other numerics without formatters for
96
+     * events which look like WHOIS information, and formats them automatically.
97
+     *
98
+     * @param server The server on which the event was received
99
+     * @param numeric The numeric code of the event
100
+     * @param arguments The arguments to the numeric event
101
+     * @param format The format that should be used to display the event
102
+     */
103
+    private void handleNumeric(final Server server, final int numeric,
104
+            final String[] arguments, final StringBuffer format) {
105
+        switch (numeric) {
106
+            case 311: // RPL_WHOISUSER
107
+                targets.put(server, arguments[3]);
108
+                break;
109
+            case 318: // RPL_ENDOFWHOIS
110
+                targets.remove(server);
111
+                break;
112
+            default:
113
+                if (format.length() == 0 && arguments.length > 4
114
+                        && targets.containsKey(server)
115
+                        && arguments[3].equals(targets.get(server))) {
116
+                    // This numeric should be automatically formatted.
117
+
118
+                    final String target = "numeric_auto_" + (arguments.length - 4);
119
+                    ensureExists(target, arguments.length);
120
+                    format.replace(0, format.length(), target);
121
+                }
122
+        }
123
+    }
124
+
125
+    /**
126
+     * Ensures that the specified formatter exists in our identity.
127
+     *
128
+     * @param target The target to be checked and added if necessary
129
+     * @param arguments The number of arguments for the numeric
130
+     */
131
+    private void ensureExists(final String target, final int arguments) {
132
+        if (!identity.hasOptionString("formatter", target)) {
133
+            final StringBuilder builder = new StringBuilder("%4$s %" + arguments + "$s");
134
+            for (int i = 5; i < arguments; i++) {
135
+                builder.append(" %");
136
+                builder.append(i);
137
+                builder.append("$s");
138
+            }
139
+
140
+            identity.setOption("formatter", target, builder.toString());
141
+            identity.setOption("notification", target, "group:whois");
142
+        }
143
+    }
144
+
145
+}

Načítá se…
Zrušit
Uložit