Quellcode durchsuchen

Intelligent notification targets

git-svn-id: http://svn.dmdirc.com/trunk@2252 00569f92-eb28-0410-84fd-f71c24880f
tags/0.5.1
Chris Smith vor 16 Jahren
Ursprung
Commit
9a20d62108

+ 23
- 0
src/com/dmdirc/Server.java Datei anzeigen

@@ -813,6 +813,29 @@ public final class Server extends WritableFrameContainer implements
813 813
             addLineToAll(messageType, args);
814 814
         } else if ("active".equals(target)) {
815 815
             addLineToActive(messageType, args);
816
+        } else if (target.startsWith("lastcommand:")) {
817
+            final String command = String.format(target.substring(12), args);
818
+            
819
+            WritableFrameContainer best = this;
820
+            long besttime = 0;
821
+            
822
+            final List<WritableFrameContainer> containers = new ArrayList<WritableFrameContainer>();
823
+            containers.addAll(channels.values());
824
+            containers.addAll(queries.values());
825
+            
826
+            if (raw != null) {
827
+                containers.add(raw);
828
+            }
829
+            
830
+            for (WritableFrameContainer container: containers) {
831
+                long time = container.getFrame().getCommandParser().getCommandTime(command);
832
+                if (time > besttime) {
833
+                    besttime = time;
834
+                    best = container;
835
+                }
836
+            }
837
+            
838
+            best.addLine(messageType, args);
816 839
         } else if (!"none".equals(target)) {
817 840
             Logger.userError(ErrorLevel.MEDIUM,
818 841
                     "Invalid notification target for type " + messageType + ": " + target);

+ 56
- 3
src/com/dmdirc/commandparser/CommandParser.java Datei anzeigen

@@ -29,7 +29,9 @@ import com.dmdirc.config.IdentityManager;
29 29
 import com.dmdirc.ui.interfaces.InputWindow;
30 30
 
31 31
 import java.io.Serializable;
32
+import java.util.ArrayList;
32 33
 import java.util.Hashtable;
34
+import java.util.List;
33 35
 import java.util.Map;
34 36
 
35 37
 /**
@@ -52,6 +54,11 @@ public abstract class CommandParser implements Serializable {
52 54
      */
53 55
     private final Map<String, Command> commands;
54 56
     
57
+    /**
58
+     * A history of commands that have been entered into this parser.
59
+     */
60
+    private final List<PreviousCommand> history = new ArrayList<PreviousCommand>();
61
+    
55 62
     /** Creates a new instance of CommandParser. */
56 63
     public CommandParser() {
57 64
         commands = new Hashtable<String, Command>();
@@ -90,12 +97,14 @@ public abstract class CommandParser implements Serializable {
90 97
             return;
91 98
         }
92 99
         
93
-        if (line.charAt(0) == IdentityManager.getGlobalConfig().getOption("general", "commandchar").charAt(0)) {
100
+        if (line.charAt(0) == IdentityManager.getGlobalConfig()
101
+                .getOption("general", "commandchar").charAt(0)) {
94 102
             int offset = 1;
95 103
             boolean silent = false;
96 104
             
97 105
             if (line.length() > offset
98
-                    && line.charAt(offset) == IdentityManager.getGlobalConfig().getOption("general", "silencechar").charAt(0)) {
106
+                    && line.charAt(offset) == IdentityManager.getGlobalConfig()
107
+                    .getOption("general", "silencechar").charAt(0)) {
99 108
                 silent = true;
100 109
                 offset++;
101 110
             }
@@ -106,7 +115,8 @@ public abstract class CommandParser implements Serializable {
106 115
             
107 116
             assert args.length > 0;
108 117
             
109
-            if (args.length >= 2 && parseChannel && origin != null && origin.getContainer().getServer() != null
118
+            if (args.length >= 2 && parseChannel && origin != null
119
+                    && origin.getContainer().getServer() != null
110 120
                     && origin.getContainer().getServer().getParser().isValidChannelName(args[1])
111 121
                     && CommandManager.isChannelCommand(command)) {
112 122
                 final Server server = origin.getContainer().getServer();
@@ -138,8 +148,10 @@ public abstract class CommandParser implements Serializable {
138 148
             // have error handlers if there are too few arguments (e.g., msg/0 and
139 149
             // msg/1 would return errors, so msg only gets called with 2+ args).
140 150
             if (commands.containsKey(signature.toLowerCase())) {
151
+                addHistory(command, comargs);
141 152
                 executeCommand(origin, silent, commands.get(signature.toLowerCase()), comargs);
142 153
             } else if (commands.containsKey(command.toLowerCase())) {
154
+                addHistory(command, comargs);
143 155
                 executeCommand(origin, silent, commands.get(command.toLowerCase()), comargs);
144 156
             } else {
145 157
                 handleInvalidCommand(origin, command, comargs);
@@ -149,6 +161,47 @@ public abstract class CommandParser implements Serializable {
149 161
         }
150 162
     }
151 163
     
164
+    /**
165
+     * Adds a command to this parser's history.
166
+     *
167
+     * @param command The command name that was used
168
+     * @param args The arguments for the command, if any
169
+     */
170
+    private void addHistory(final String command, final String... args) {
171
+        final StringBuilder builder = new StringBuilder(command);
172
+        
173
+        for (String arg : args) {
174
+            builder.append(' ');
175
+            builder.append(arg);
176
+        }
177
+        
178
+        history.add(new PreviousCommand(builder.toString()));
179
+        
180
+        while (history.size() >
181
+                IdentityManager.getGlobalConfig().getOptionInt("general", "commandhistory", 10)) {
182
+            history.remove(0);
183
+        }
184
+    }
185
+    
186
+    /**
187
+     * Retrieves the most recent time that the specified command was used.
188
+     * Commands should not include command or silence chars.
189
+     *
190
+     * @param command The command to search for
191
+     * @return The timestamp that the command was used, or 0 if it wasn't
192
+     */
193
+    public long getCommandTime(final String command) {
194
+        long res = 0;
195
+        
196
+        for (PreviousCommand pc : history) {
197
+            if (pc.getLine().equalsIgnoreCase(command)) {
198
+                res = Math.max(res, pc.getTime());
199
+            }
200
+        }
201
+        
202
+        return res;
203
+    }
204
+    
152 205
     /**
153 206
      * Parses the specified string as a command.
154 207
      * @param origin The window in which the command was typed

+ 45
- 0
src/com/dmdirc/commandparser/PreviousCommand.java Datei anzeigen

@@ -0,0 +1,45 @@
1
+/*
2
+ * Copyright (c) 2006-2007 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.commandparser;
24
+
25
+import java.util.Date;
26
+
27
+public final class PreviousCommand {
28
+    
29
+    private final String line;
30
+    private final long time;
31
+    
32
+    public PreviousCommand(final String line) {
33
+        this.line = line;
34
+        this.time = new Date().getTime();
35
+    }
36
+    
37
+    public long getTime() {
38
+        return time;
39
+    }
40
+    
41
+    public String getLine() {
42
+        return line;
43
+    }
44
+    
45
+}

+ 1
- 1
src/com/dmdirc/config/defaults/defaults Datei anzeigen

@@ -67,7 +67,7 @@ notifications.numeric_671=group:whois
67 67
 
68 68
 notifications.numeric_705=server
69 69
 
70
-notifications.whois=active
70
+notifications.whois=lastcommand:whois %4$s
71 71
         
72 72
 ui.backgroundcolour=0
73 73
 ui.foregroundcolour=1

Laden…
Abbrechen
Speichern