소스 검색

Core support for protocol description objects

Fixes issue 3560

Change-Id: I7e1bdc1c936608acf1d98e8629ff62cec4b3e810
Reviewed-on: http://gerrit.dmdirc.com/1182
Automatic-Compile: Gregory Holmes <greg@dmdirc.com>
Reviewed-by: Gregory Holmes <greg@dmdirc.com>
tags/0.6.4rc1
Chris Smith 14 년 전
부모
커밋
857c1aeaca

+ 1
- 1
src/com/dmdirc/Channel.java 파일 보기

@@ -542,7 +542,7 @@ public class Channel extends MessageTarget<ChannelWindow> implements ConfigChang
542 542
             // Format topics
543 543
 
544 544
             args.add("");
545
-            args.addAll(Arrays.asList(server.getParser().parseHostmask(((Topic) arg).getClient())));
545
+            args.addAll(Arrays.asList(server.parseHostmask(((Topic) arg).getClient())));
546 546
             args.add(((Topic) arg).getTopic());
547 547
             args.add(((Topic) arg).getTime() * 1000);
548 548
 

+ 3
- 2
src/com/dmdirc/Invite.java 파일 보기

@@ -22,6 +22,7 @@
22 22
 
23 23
 package com.dmdirc;
24 24
 
25
+import com.dmdirc.parser.common.ChannelJoinRequest;
25 26
 import java.util.Date;
26 27
 
27 28
 /**
@@ -90,14 +91,14 @@ public class Invite {
90 91
      * @return This invite's source
91 92
      */
92 93
     public String[] getSource() {
93
-        return server.getParser().parseHostmask(source);
94
+        return server.parseHostmask(source);
94 95
     }
95 96
     
96 97
     /**
97 98
      * Join the channel that belongs to this invite.
98 99
      */
99 100
     public void accept() {
100
-        server.getParser().joinChannel(channel);
101
+        server.join(new ChannelJoinRequest(channel));
101 102
         
102 103
         server.removeInvite(this);
103 104
     }

+ 48
- 9
src/com/dmdirc/ParserFactory.java 파일 보기

@@ -26,7 +26,7 @@ import com.dmdirc.logger.ErrorLevel;
26 26
 import com.dmdirc.logger.Logger;
27 27
 import com.dmdirc.parser.interfaces.Parser;
28 28
 import com.dmdirc.parser.common.MyInfo;
29
-import com.dmdirc.plugins.ExportedService;
29
+import com.dmdirc.parser.interfaces.ProtocolDescription;
30 30
 import com.dmdirc.plugins.NoSuchProviderException;
31 31
 import com.dmdirc.plugins.PluginManager;
32 32
 import com.dmdirc.plugins.Service;
@@ -62,6 +62,51 @@ public class ParserFactory {
62 62
             }
63 63
         }
64 64
 
65
+        final Object obj = getExportResult(address, "getParser", myInfo, address);
66
+
67
+        if (obj instanceof Parser) {
68
+            return (Parser) obj;
69
+        } else {
70
+            Logger.userError(ErrorLevel.MEDIUM,
71
+                    "Unable to create parser for: " + address.getScheme());
72
+        }
73
+
74
+        return null;
75
+    }
76
+
77
+    /**
78
+     * Retrieves a protocol description.
79
+     *
80
+     * @param address The address to retrieve a description for
81
+     * @return A corresponding protocol description
82
+     * @since 0.6.4
83
+     */
84
+    public ProtocolDescription getDescription(final URI address) {
85
+        final Object obj = getExportResult(address, "getProtocolDescription");
86
+
87
+        if (obj instanceof ProtocolDescription) {
88
+            return (ProtocolDescription) obj;
89
+        } else {
90
+            Logger.userError(ErrorLevel.MEDIUM,
91
+                    "Unable to create protocol description for: " + address.getScheme());
92
+        }
93
+
94
+        return null;
95
+    }
96
+
97
+    /**
98
+     * Retrieves and executes an exported service with the specified name from a
99
+     * {@link ServiceProvider} which can provide a parser for the specified
100
+     * address. If no such provider or service is found, null is returned.
101
+     *
102
+     * @param address The address a provider is required for
103
+     * @param serviceName The name of the service to be retrieved
104
+     * @param args The arguments to be passed to the exported service
105
+     * @return The result from a relevant exported service, or null
106
+     * @since 0.6.4
107
+     */
108
+    protected Object getExportResult(final URI address,
109
+            final String serviceName, final Object ... args) {
65 110
         // TODO: Move default scheme to a setting
66 111
         final String scheme = address.getScheme() == null ? "irc" : address.getScheme();
67 112
 
@@ -74,14 +119,8 @@ public class ParserFactory {
74 119
                 if (provider != null) {
75 120
                     provider.activateServices();
76 121
 
77
-                    final ExportedService exportService = provider.getExportedService("getParser");
78
-                    final Object obj = exportService.execute(myInfo, address);
79
-                    if (obj != null && obj instanceof Parser) {
80
-                        return (Parser) obj;
81
-                    } else {
82
-                        Logger.userError(ErrorLevel.MEDIUM,
83
-                                "Unable to create parser for: " + address.getScheme());
84
-                    }
122
+                    return provider.getExportedService(serviceName)
123
+                            .execute(args);
85 124
                 }
86 125
             }
87 126
         } catch (NoSuchProviderException nspe) {

+ 6
- 6
src/com/dmdirc/Query.java 파일 보기

@@ -70,14 +70,14 @@ public class Query extends MessageTarget<QueryWindow> implements PrivateActionLi
70 70
      * @param newServer The server object that this Query belongs to
71 71
      */
72 72
     public Query(final Server newServer, final String newHost) {
73
-        super("query", newServer.getParser().parseHostmask(newHost)[0],
74
-                newServer.getParser().parseHostmask(newHost)[0],
73
+        super("query", newServer.parseHostmask(newHost)[0],
74
+                newServer.parseHostmask(newHost)[0],
75 75
                 QueryWindow.class, newServer.getConfigManager(),
76 76
                 new QueryCommandParser());
77 77
 
78 78
         this.server = newServer;
79 79
         this.host = newHost;
80
-        this.nickname = server.getParser().parseHostmask(host)[0];
80
+        this.nickname = server.parseHostmask(host)[0];
81 81
 
82 82
         WindowManager.addWindow(server, this,
83 83
                 !getConfigManager().getOptionBool("general", "hidequeries"));
@@ -163,7 +163,7 @@ public class Query extends MessageTarget<QueryWindow> implements PrivateActionLi
163 163
                     getTranscoder().encode(action));
164 164
 
165 165
             doNotification("querySelfAction", CoreActionType.QUERY_SELF_ACTION,
166
-                    server.getParser().getLocalClient(), action);
166
+                    client, action);
167 167
         } else {
168 168
             addLine("actionTooLong", action.length());
169 169
         }
@@ -179,7 +179,7 @@ public class Query extends MessageTarget<QueryWindow> implements PrivateActionLi
179 179
     @Override
180 180
     public void onPrivateMessage(final Parser parser, final String message,
181 181
             final String remoteHost) {
182
-        final String[] parts = parser.parseHostmask(host);
182
+        final String[] parts = server.parseHostmask(host);
183 183
 
184 184
         final StringBuffer buff = new StringBuffer("queryMessage");
185 185
 
@@ -199,7 +199,7 @@ public class Query extends MessageTarget<QueryWindow> implements PrivateActionLi
199 199
     @Override
200 200
     public void onPrivateAction(final Parser parser, final String message,
201 201
             final String remoteHost) {
202
-        final String[] parts = parser.parseHostmask(host);
202
+        final String[] parts = server.parseHostmask(host);
203 203
 
204 204
         final StringBuffer buff = new StringBuffer("queryAction");
205 205
 

+ 21
- 29
src/com/dmdirc/Server.java 파일 보기

@@ -46,6 +46,7 @@ import com.dmdirc.parser.interfaces.Parser;
46 46
 import com.dmdirc.parser.interfaces.SecureParser;
47 47
 import com.dmdirc.parser.interfaces.StringConverter;
48 48
 import com.dmdirc.parser.common.MyInfo;
49
+import com.dmdirc.parser.interfaces.ProtocolDescription;
49 50
 import com.dmdirc.ui.WindowManager;
50 51
 import com.dmdirc.ui.input.TabCompleter;
51 52
 import com.dmdirc.ui.input.TabCompletionType;
@@ -104,6 +105,8 @@ public class Server extends WritableFrameContainer<ServerWindow> implements Conf
104 105
     private transient Parser parser;
105 106
     /** The Parser instance that used to be handling this server. */
106 107
     private transient Parser oldParser;
108
+    /** The parser-supplied protocol description object. */
109
+    private ProtocolDescription protocolDescription;
107 110
 
108 111
     /**
109 112
      * Object used to synchronoise access to parser. This object should be
@@ -181,6 +184,7 @@ public class Server extends WritableFrameContainer<ServerWindow> implements Conf
181 184
                 new ServerCommandParser());
182 185
 
183 186
         this.address = uri;
187
+        this.protocolDescription = new ParserFactory().getDescription(address);
184 188
         this.profile = profile;
185 189
 
186 190
         ServerManager.getServerManager().registerServer(this);
@@ -277,6 +281,7 @@ public class Server extends WritableFrameContainer<ServerWindow> implements Conf
277 281
                 getConfigManager().migrate(address.getScheme(), "", "", address.getHost());
278 282
 
279 283
                 this.address = address;
284
+                this.protocolDescription = new ParserFactory().getDescription(address);
280 285
                 this.profile = profile;
281 286
 
282 287
                 updateTitle();
@@ -481,21 +486,7 @@ public class Server extends WritableFrameContainer<ServerWindow> implements Conf
481 486
      * @return True iff the query is known, false otherwise
482 487
      */
483 488
     public boolean hasQuery(final String host) {
484
-        final String nick;
485
-
486
-        try {
487
-            parserLock.readLock().lock();
488
-
489
-            if (parser == null) {
490
-                return false;
491
-            }
492
-
493
-            nick = converter.toLowerCase(parser.parseHostmask(host)[0]);
494
-        } finally {
495
-            parserLock.readLock().unlock();
496
-        }
497
-
498
-        return queries.containsKey(nick);
489
+        return queries.containsKey(converter.toLowerCase(parseHostmask(host)[0]));
499 490
     }
500 491
 
501 492
     /**
@@ -513,20 +504,8 @@ public class Server extends WritableFrameContainer<ServerWindow> implements Conf
513 504
             }
514 505
         }
515 506
 
516
-        final String nick, lnick;
517
-
518
-        try {
519
-            parserLock.readLock().lock();
520
-
521
-            if (parser == null) {
522
-                throw new IllegalStateException("Can't retrieve query while disconnected");
523
-            }
524
-
525
-            nick = parser.parseHostmask(host)[0];
526
-            lnick = converter.toLowerCase(nick);
527
-        } finally {
528
-            parserLock.readLock().unlock();
529
-        }
507
+        final String nick = parseHostmask(host)[0];
508
+        final String lnick = converter.toLowerCase(nick);
530 509
 
531 510
         if (!queries.containsKey(lnick)) {
532 511
             final Query newQuery = new Query(this, host);
@@ -791,6 +770,19 @@ public class Server extends WritableFrameContainer<ServerWindow> implements Conf
791 770
         return false;
792 771
     }
793 772
 
773
+    /**
774
+     * Parses the specified hostmask in a manner prescribed by the protocol
775
+     * currently used by this server.
776
+     *
777
+     * @see ProtocolDescription#parseHostmask(java.lang.String)
778
+     * @param hostmask The hostmask to be parsed
779
+     * @return An array containing the nickname, username and hostname
780
+     * @since 0.6.4
781
+     */
782
+    public String[] parseHostmask(final String hostmask) {
783
+        return protocolDescription.parseHostmask(hostmask);
784
+    }
785
+
794 786
     /**
795 787
      * Retrieves the MyInfo object used for the IRC Parser.
796 788
      *

+ 1
- 1
src/com/dmdirc/ServerEventHandler.java 파일 보기

@@ -154,7 +154,7 @@ public final class ServerEventHandler extends EventHandler implements
154 154
 
155 155
         if (owner.doNotification("privateCTCP", CoreActionType.SERVER_CTCP,
156 156
                 owner.getParser().getClient(sHost), sType, sMessage)) {
157
-            owner.sendCTCPReply(tParser.parseHostmask(sHost)[0], sType, sMessage);
157
+            owner.sendCTCPReply(owner.parseHostmask(sHost)[0], sType, sMessage);
158 158
         }
159 159
     }
160 160
 

+ 1
- 1
src/com/dmdirc/commandparser/commands/channel/ShowTopic.java 파일 보기

@@ -56,7 +56,7 @@ public final class ShowTopic extends ChannelCommand implements ExternalCommand {
56 56
             if (cChannel.getTopic().isEmpty()) {
57 57
                 sendLine(origin, isSilent, "channelNoTopic", cChannel);
58 58
             } else {
59
-                final String[] parts = server.getParser().parseHostmask(cChannel.getTopicSetter());
59
+                final String[] parts = server.parseHostmask(cChannel.getTopicSetter());
60 60
 
61 61
                 sendLine(origin, isSilent, "channelTopicDiscovered",
62 62
                         "", parts[0], parts[1], parts[2], cChannel.getTopic(),

+ 1
- 6
test/com/dmdirc/InviteTest.java 파일 보기

@@ -25,8 +25,6 @@ package com.dmdirc;
25 25
 import com.dmdirc.config.IdentityManager;
26 26
 import com.dmdirc.addons.ui_dummy.DummyController;
27 27
 
28
-import com.dmdirc.parser.interfaces.Parser;
29
-import java.net.URI;
30 28
 import java.util.Date;
31 29
 
32 30
 import org.junit.BeforeClass;
@@ -37,7 +35,6 @@ import static org.mockito.Mockito.*;
37 35
 
38 36
 public class InviteTest {
39 37
 
40
-    private static Parser parser;
41 38
     private static Server server;
42 39
     private static Invite test;
43 40
     private static long ts;
@@ -48,10 +45,8 @@ public class InviteTest {
48 45
         IdentityManager.load();
49 46
         
50 47
         server = mock(Server.class);
51
-        parser = mock(Parser.class);
52 48
 
53
-        when(server.getParser()).thenReturn(parser);
54
-        when(parser.parseHostmask("nick!ident@host"))
49
+        when(server.parseHostmask("nick!ident@host"))
55 50
                 .thenReturn(new String[] { "nick", "ident", "host"});
56 51
 
57 52
         test = new Invite(server, "#channel", "nick!ident@host");

Loading…
취소
저장