|
@@ -1,157 +0,0 @@
|
1
|
|
-/*
|
2
|
|
- * Copyright (c) 2006-2015 DMDirc Developers
|
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.ui.messages;
|
24
|
|
-
|
25
|
|
-import com.dmdirc.ClientModule;
|
26
|
|
-import com.dmdirc.DMDircMBassador;
|
27
|
|
-import com.dmdirc.events.ServerDisconnectedEvent;
|
28
|
|
-import com.dmdirc.events.ServerNumericEvent;
|
29
|
|
-import com.dmdirc.interfaces.Connection;
|
30
|
|
-import com.dmdirc.interfaces.SystemLifecycleComponent;
|
31
|
|
-import com.dmdirc.interfaces.config.ConfigProvider;
|
32
|
|
-
|
33
|
|
-import java.util.HashMap;
|
34
|
|
-import java.util.Map;
|
35
|
|
-
|
36
|
|
-import javax.inject.Inject;
|
37
|
|
-import javax.inject.Singleton;
|
38
|
|
-
|
39
|
|
-import net.engio.mbassy.listener.Handler;
|
40
|
|
-
|
41
|
|
-/**
|
42
|
|
- * Listens for whois-like numeric events and automatically formats them.
|
43
|
|
- *
|
44
|
|
- * @since 0.6.3
|
45
|
|
- */
|
46
|
|
-@Singleton
|
47
|
|
-public class WhoisNumericFormatter implements SystemLifecycleComponent {
|
48
|
|
-
|
49
|
|
- /** The name of the target of any current whois requests. */
|
50
|
|
- private final Map<Connection, String> targets = new HashMap<>();
|
51
|
|
- /** The identity to add formatters to. */
|
52
|
|
- private final ConfigProvider identity;
|
53
|
|
- /** Event bus to subscribe to events on. */
|
54
|
|
- private final DMDircMBassador eventBus;
|
55
|
|
-
|
56
|
|
- /**
|
57
|
|
- * Creates a new whois numeric formatter that will add automatic formats to the specified
|
58
|
|
- * identity. This will normally be a temporary global identity.
|
59
|
|
- *
|
60
|
|
- * @param identity The identity to write formatters to
|
61
|
|
- * @param eventBus The event bus to subscribe to events on
|
62
|
|
- */
|
63
|
|
- @Inject
|
64
|
|
- public WhoisNumericFormatter(
|
65
|
|
- @ClientModule.AddonConfig final ConfigProvider identity,
|
66
|
|
- final DMDircMBassador eventBus) {
|
67
|
|
- this.identity = identity;
|
68
|
|
- this.eventBus = eventBus;
|
69
|
|
- }
|
70
|
|
-
|
71
|
|
- /**
|
72
|
|
- * Handles a server disconnected event. This clears any entry for that server in the
|
73
|
|
- * {@link #targets} map.
|
74
|
|
- *
|
75
|
|
- * @param event The server disconnected event to process
|
76
|
|
- */
|
77
|
|
- @Handler
|
78
|
|
- public void handleServerDisconnected(final ServerDisconnectedEvent event) {
|
79
|
|
- targets.remove(event.getConnection());
|
80
|
|
- }
|
81
|
|
-
|
82
|
|
- /**
|
83
|
|
- * Handles a received numeric event. This method has special handling for numerics 311 and 318,
|
84
|
|
- * used to signal the start and end of a WHOIS request. It then monitors any other numerics
|
85
|
|
- * without formatters for events which look like WHOIS information, and formats them
|
86
|
|
- * automatically.
|
87
|
|
- *
|
88
|
|
- * @param event The server numeric event to process
|
89
|
|
- */
|
90
|
|
- @Handler
|
91
|
|
- public void handleNumeric(final ServerNumericEvent event) {
|
92
|
|
- final Connection server = event.getConnection();
|
93
|
|
- final int numeric = event.getNumeric();
|
94
|
|
- final String[] arguments = event.getArgs();
|
95
|
|
- switch (numeric) {
|
96
|
|
- case 311: // RPL_WHOISUSER
|
97
|
|
- targets.put(server, arguments[3]);
|
98
|
|
- break;
|
99
|
|
- case 318: // RPL_ENDOFWHOIS
|
100
|
|
- targets.remove(server);
|
101
|
|
- break;
|
102
|
|
- default:
|
103
|
|
- if (arguments.length > 4
|
104
|
|
- && targets.containsKey(server)
|
105
|
|
- && arguments[3].equals(targets.get(server))) {
|
106
|
|
- // This numeric should be automatically formatted.
|
107
|
|
-
|
108
|
|
- if (event.getDisplayFormat().isEmpty()) {
|
109
|
|
- // No custom formatter, switch it to an auto whois
|
110
|
|
- // format and target.
|
111
|
|
- final String target = "numeric_autowhois_" + (arguments.length - 4);
|
112
|
|
- ensureExists(target, arguments.length);
|
113
|
|
- event.setDisplayFormat(target);
|
114
|
|
- } else {
|
115
|
|
- // There's a custom format. We'll see if we need to
|
116
|
|
- // add a formatter or notification settings for it
|
117
|
|
- // anyway.
|
118
|
|
- ensureExists(event.getDisplayFormat(), arguments.length);
|
119
|
|
- }
|
120
|
|
- }
|
121
|
|
- break;
|
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
|
|
- }
|
142
|
|
-
|
143
|
|
- if (!identity.hasOptionString("notifications", target)) {
|
144
|
|
- identity.setOption("notifications", target, "group:whois");
|
145
|
|
- }
|
146
|
|
- }
|
147
|
|
-
|
148
|
|
- @Override
|
149
|
|
- public void startUp() {
|
150
|
|
- eventBus.subscribe(this);
|
151
|
|
- }
|
152
|
|
-
|
153
|
|
- @Override
|
154
|
|
- public void shutDown() {
|
155
|
|
- eventBus.unsubscribe(this);
|
156
|
|
- }
|
157
|
|
-}
|