Browse Source

Merge pull request #618 from csmith/tidying

Improve formatting of whois events.
pull/619/head
Greg Holmes 8 years ago
parent
commit
2bfd0b0fa3

+ 2
- 8
res/com/dmdirc/ui/messages/format.yml View File

@@ -211,8 +211,8 @@ UnknownCommandEvent:
211 211
 UserInfoResponseEvent:
212 212
   before: "---------- User info for {{user.nickname}} ----------"
213 213
   after: "--------- End of info for {{user.nickname}} ---------"
214
-  iterate: "entries"
215
-  format: "{{key}}: {{value}}"
214
+  iterate: "properties"
215
+  format: "{{friendlyName}}: {{rawValue}}"
216 216
   colour: 10
217 217
 
218 218
 ################## TODO ############################################################################
@@ -237,12 +237,6 @@ UserInfoResponseEvent:
237 237
 #  numeric_290=%4$s
238 238
 #  numeric_292=%4$s
239 239
 #  numeric_294=%4$s
240
-#  numeric_301=%4$s is away: %5$s
241
-#  numeric_311=-\n%4$s is %5$s@%6$s (%8$s).
242
-#  numeric_312=%4$s is connected to %5$s (%6$s).
243
-#  numeric_317=%4$s has been idle for %5$u; signed on at %6$TT on %6$TF.
244
-#  numeric_318=End of WHOIS info for %4$s.\n-
245
-#  numeric_319=%4$s is on: %5$s
246 240
 #  numeric_401=6A7000%4$s: %5$s
247 241
 #  numeric_404=6A7000%5$s
248 242
 #  numeric_405=6A7000%4$s: %5$s

+ 0
- 8
src/com/dmdirc/ClientModule.java View File

@@ -36,14 +36,12 @@ import com.dmdirc.interfaces.CommandController;
36 36
 import com.dmdirc.interfaces.ConnectionFactory;
37 37
 import com.dmdirc.interfaces.ConnectionManager;
38 38
 import com.dmdirc.interfaces.LifecycleController;
39
-import com.dmdirc.interfaces.SystemLifecycleComponent;
40 39
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
41 40
 import com.dmdirc.interfaces.config.IdentityController;
42 41
 import com.dmdirc.plugins.PluginModule;
43 42
 import com.dmdirc.ui.messages.ColourManager;
44 43
 import com.dmdirc.ui.messages.ColourManagerFactory;
45 44
 import com.dmdirc.ui.messages.UiMessagesModule;
46
-import com.dmdirc.ui.messages.WhoisNumericFormatter;
47 45
 import com.dmdirc.ui.messages.sink.MessagesModule;
48 46
 import com.dmdirc.ui.themes.ThemeManager;
49 47
 import com.dmdirc.updater.UpdaterModule;
@@ -182,10 +180,4 @@ public class ClientModule {
182 180
         return objectGraph;
183 181
     }
184 182
 
185
-    @Provides(type = Provides.Type.SET)
186
-    public SystemLifecycleComponent getWhoisNumericFormatter(
187
-            final WhoisNumericFormatter formatter) {
188
-        return formatter;
189
-    }
190
-
191 183
 }

+ 32
- 7
src/com/dmdirc/events/UserInfoResponseEvent.java View File

@@ -37,13 +37,14 @@ import java.util.Optional;
37 37
 public class UserInfoResponseEvent extends ServerDisplayableEvent {
38 38
 
39 39
     private final User user;
40
-    private final Map<UserInfoEvent.UserInfoType, String> info;
40
+    private final Map<UserInfoEvent.UserInfoType, UserInfoProperty> info;
41 41
 
42 42
     public UserInfoResponseEvent(final Connection connection, final long date,
43 43
             final User user, final Map<UserInfoEvent.UserInfoType, String> info) {
44 44
         super(date, connection);
45 45
         this.user = user;
46
-        this.info = new EnumMap<>(info);
46
+        this.info = new EnumMap<>(UserInfoEvent.UserInfoType.class);
47
+        info.forEach((key, value) -> this.info.put(key, new UserInfoProperty(key, value)));
47 48
     }
48 49
 
49 50
     /**
@@ -63,16 +64,40 @@ public class UserInfoResponseEvent extends ServerDisplayableEvent {
63 64
      * @return An optional containing the information, if it was provided.
64 65
      */
65 66
     public Optional<String> getInfo(final UserInfoEvent.UserInfoType type) {
66
-        return Optional.ofNullable(info.get(type));
67
+        return Optional.ofNullable(info.get(type)).map(UserInfoProperty::getRawValue);
67 68
     }
68 69
 
69 70
     /**
70
-     * Gets a collection of all info entries in the response.
71
+     * Gets a collection of all info properties in the response.
71 72
      *
72
-     * @return A collection of all user info entries.
73
+     * @return A collection of all user info properties.
73 74
      */
74
-    public Collection<Map.Entry<UserInfoEvent.UserInfoType, String>> getEntries() {
75
-        return info.entrySet();
75
+    public Collection<UserInfoProperty> getProperties() {
76
+        return info.values();
77
+    }
78
+
79
+    public static class UserInfoProperty {
80
+
81
+        private final UserInfoEvent.UserInfoType type;
82
+        private final String rawValue;
83
+
84
+        public UserInfoProperty(final UserInfoEvent.UserInfoType type, final String rawValue) {
85
+            this.type = type;
86
+            this.rawValue = rawValue;
87
+        }
88
+
89
+        public UserInfoEvent.UserInfoType getType() {
90
+            return type;
91
+        }
92
+
93
+        public String getRawValue() {
94
+            return rawValue;
95
+        }
96
+
97
+        public String getFriendlyName() {
98
+            return type.name().charAt(0) + type.name().substring(1).toLowerCase().replace('_', ' ');
99
+        }
100
+
76 101
     }
77 102
 
78 103
 }

+ 0
- 157
src/com/dmdirc/ui/messages/WhoisNumericFormatter.java View File

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

Loading…
Cancel
Save