|
@@ -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
|
+}
|