Browse Source

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 years ago
parent
commit
857c1aeaca

+ 1
- 1
src/com/dmdirc/Channel.java View File

542
             // Format topics
542
             // Format topics
543
 
543
 
544
             args.add("");
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
             args.add(((Topic) arg).getTopic());
546
             args.add(((Topic) arg).getTopic());
547
             args.add(((Topic) arg).getTime() * 1000);
547
             args.add(((Topic) arg).getTime() * 1000);
548
 
548
 

+ 3
- 2
src/com/dmdirc/Invite.java View File

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

+ 48
- 9
src/com/dmdirc/ParserFactory.java View File

26
 import com.dmdirc.logger.Logger;
26
 import com.dmdirc.logger.Logger;
27
 import com.dmdirc.parser.interfaces.Parser;
27
 import com.dmdirc.parser.interfaces.Parser;
28
 import com.dmdirc.parser.common.MyInfo;
28
 import com.dmdirc.parser.common.MyInfo;
29
-import com.dmdirc.plugins.ExportedService;
29
+import com.dmdirc.parser.interfaces.ProtocolDescription;
30
 import com.dmdirc.plugins.NoSuchProviderException;
30
 import com.dmdirc.plugins.NoSuchProviderException;
31
 import com.dmdirc.plugins.PluginManager;
31
 import com.dmdirc.plugins.PluginManager;
32
 import com.dmdirc.plugins.Service;
32
 import com.dmdirc.plugins.Service;
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
         // TODO: Move default scheme to a setting
110
         // TODO: Move default scheme to a setting
66
         final String scheme = address.getScheme() == null ? "irc" : address.getScheme();
111
         final String scheme = address.getScheme() == null ? "irc" : address.getScheme();
67
 
112
 
74
                 if (provider != null) {
119
                 if (provider != null) {
75
                     provider.activateServices();
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
         } catch (NoSuchProviderException nspe) {
126
         } catch (NoSuchProviderException nspe) {

+ 6
- 6
src/com/dmdirc/Query.java View File

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

+ 21
- 29
src/com/dmdirc/Server.java View File

46
 import com.dmdirc.parser.interfaces.SecureParser;
46
 import com.dmdirc.parser.interfaces.SecureParser;
47
 import com.dmdirc.parser.interfaces.StringConverter;
47
 import com.dmdirc.parser.interfaces.StringConverter;
48
 import com.dmdirc.parser.common.MyInfo;
48
 import com.dmdirc.parser.common.MyInfo;
49
+import com.dmdirc.parser.interfaces.ProtocolDescription;
49
 import com.dmdirc.ui.WindowManager;
50
 import com.dmdirc.ui.WindowManager;
50
 import com.dmdirc.ui.input.TabCompleter;
51
 import com.dmdirc.ui.input.TabCompleter;
51
 import com.dmdirc.ui.input.TabCompletionType;
52
 import com.dmdirc.ui.input.TabCompletionType;
104
     private transient Parser parser;
105
     private transient Parser parser;
105
     /** The Parser instance that used to be handling this server. */
106
     /** The Parser instance that used to be handling this server. */
106
     private transient Parser oldParser;
107
     private transient Parser oldParser;
108
+    /** The parser-supplied protocol description object. */
109
+    private ProtocolDescription protocolDescription;
107
 
110
 
108
     /**
111
     /**
109
      * Object used to synchronoise access to parser. This object should be
112
      * Object used to synchronoise access to parser. This object should be
181
                 new ServerCommandParser());
184
                 new ServerCommandParser());
182
 
185
 
183
         this.address = uri;
186
         this.address = uri;
187
+        this.protocolDescription = new ParserFactory().getDescription(address);
184
         this.profile = profile;
188
         this.profile = profile;
185
 
189
 
186
         ServerManager.getServerManager().registerServer(this);
190
         ServerManager.getServerManager().registerServer(this);
277
                 getConfigManager().migrate(address.getScheme(), "", "", address.getHost());
281
                 getConfigManager().migrate(address.getScheme(), "", "", address.getHost());
278
 
282
 
279
                 this.address = address;
283
                 this.address = address;
284
+                this.protocolDescription = new ParserFactory().getDescription(address);
280
                 this.profile = profile;
285
                 this.profile = profile;
281
 
286
 
282
                 updateTitle();
287
                 updateTitle();
481
      * @return True iff the query is known, false otherwise
486
      * @return True iff the query is known, false otherwise
482
      */
487
      */
483
     public boolean hasQuery(final String host) {
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
             }
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
         if (!queries.containsKey(lnick)) {
510
         if (!queries.containsKey(lnick)) {
532
             final Query newQuery = new Query(this, host);
511
             final Query newQuery = new Query(this, host);
791
         return false;
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
      * Retrieves the MyInfo object used for the IRC Parser.
787
      * Retrieves the MyInfo object used for the IRC Parser.
796
      *
788
      *

+ 1
- 1
src/com/dmdirc/ServerEventHandler.java View File

154
 
154
 
155
         if (owner.doNotification("privateCTCP", CoreActionType.SERVER_CTCP,
155
         if (owner.doNotification("privateCTCP", CoreActionType.SERVER_CTCP,
156
                 owner.getParser().getClient(sHost), sType, sMessage)) {
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 View File

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

+ 1
- 6
test/com/dmdirc/InviteTest.java View File

25
 import com.dmdirc.config.IdentityManager;
25
 import com.dmdirc.config.IdentityManager;
26
 import com.dmdirc.addons.ui_dummy.DummyController;
26
 import com.dmdirc.addons.ui_dummy.DummyController;
27
 
27
 
28
-import com.dmdirc.parser.interfaces.Parser;
29
-import java.net.URI;
30
 import java.util.Date;
28
 import java.util.Date;
31
 
29
 
32
 import org.junit.BeforeClass;
30
 import org.junit.BeforeClass;
37
 
35
 
38
 public class InviteTest {
36
 public class InviteTest {
39
 
37
 
40
-    private static Parser parser;
41
     private static Server server;
38
     private static Server server;
42
     private static Invite test;
39
     private static Invite test;
43
     private static long ts;
40
     private static long ts;
48
         IdentityManager.load();
45
         IdentityManager.load();
49
         
46
         
50
         server = mock(Server.class);
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
                 .thenReturn(new String[] { "nick", "ident", "host"});
50
                 .thenReturn(new String[] { "nick", "ident", "host"});
56
 
51
 
57
         test = new Invite(server, "#channel", "nick!ident@host");
52
         test = new Invite(server, "#channel", "nick!ident@host");

Loading…
Cancel
Save