소스 검색

Abstract server software stuff nicely.

Fixes issue 2260.

Change-Id: Ib74eaca55eeeca83e17a3d78fb15de42e552c8f2
Reviewed-on: http://gerrit.dmdirc.com/427
Reviewed-by: Chris Smith <chris@dmdirc.com>
Tested-by: Chris Smith <chris@dmdirc.com>
tags/0.6.3
Shane Mc Cormack 14 년 전
부모
커밋
32801b4a09
3개의 변경된 파일272개의 추가작업 그리고 57개의 파일을 삭제
  1. 5
    10
      src/com/dmdirc/parser/irc/IRCChannelInfo.java
  2. 13
    47
      src/com/dmdirc/parser/irc/IRCParser.java
  3. 254
    0
      src/com/dmdirc/parser/irc/ServerType.java

+ 5
- 10
src/com/dmdirc/parser/irc/IRCChannelInfo.java 파일 보기

@@ -140,20 +140,15 @@ public class IRCChannelInfo implements ChannelInfo {
140 140
         
141 141
         askedForListModes = true;
142 142
         
143
-        final String thisIRCD = myParser.getIRCD(true).toLowerCase();
144
-        final boolean isFreenode = (thisIRCD.equals("hyperion") || thisIRCD.equals("dancer"));
145
-        final boolean isUnreal = thisIRCD.equals("unreal");
146
-        final boolean isStarChat = thisIRCD.equals("starchat");
147
-        final boolean isHybrid = thisIRCD.equals("hybrid") || thisIRCD.equals("oftc-hybrid");
148
-        final boolean isCharybdis = thisIRCD.equals("charybdis");
149
-        
143
+        final ServerType serverType = myParser.getServerType();
144
+                
150 145
         // We are considered opped if we have a mode higher than voice (or if we have any modes if voice doesn't exist)
151 146
         long voiceValue = 0;
152 147
         if (myParser.prefixModes.get('v') != null) { voiceValue = myParser.prefixModes.get('v');}
153 148
         final boolean isOpped = me.getImportantModeValue() > voiceValue;
154 149
         
155 150
         int modecount = 1;
156
-        if (!isUnreal && myParser.h005Info.containsKey("MODES")) {
151
+        if (serverType != ServerType.UNREAL && myParser.h005Info.containsKey("MODES")) {
157 152
             try {
158 153
                 modecount = Integer.parseInt(myParser.h005Info.get("MODES"));
159 154
             } catch (NumberFormatException e) { /* use default modecount */}
@@ -170,10 +165,10 @@ public class IRCChannelInfo implements ChannelInfo {
170 165
         for (Character cTemp : myParser.chanModesOther.keySet()) {
171 166
             final int nTemp = myParser.chanModesOther.get(cTemp);
172 167
             if (nTemp == IRCParser.MODE_LIST) {
173
-                if ((isFreenode || isHybrid || isCharybdis) && (cTemp == 'e' || cTemp == 'I') && !isOpped) {
168
+                if (!isOpped && serverType.isOpOnly(cTemp)) {
174 169
                     // IRCD doesn't allow non-ops to ask for these modes.
175 170
                     continue;
176
-                } else if (isStarChat && cTemp == 'H') {
171
+                } else if (serverType == serverType.STARCHAT && cTemp == 'H') {
177 172
                     // IRCD Denies the mode exists
178 173
                     continue;
179 174
                 }

+ 13
- 47
src/com/dmdirc/parser/irc/IRCParser.java 파일 보기

@@ -1765,6 +1765,15 @@ public class IRCParser implements SecureParser, Runnable {
1765 1765
      */
1766 1766
     public Map<String, String> get005() { return h005Info; }
1767 1767
 
1768
+    /**
1769
+     * Get the ServerType for this IRCD.
1770
+     *
1771
+     * @return The ServerType for this IRCD.
1772
+     */
1773
+    public ServerType getServerType() {
1774
+        return ServerType.findServerType(h005Info.get("004IRCD"), networkName, h005Info.get("003IRCD"), h005Info.get("002IRCD"));
1775
+    }
1776
+
1768 1777
     /**
1769 1778
      * Get the name of the ircd.
1770 1779
      *
@@ -1772,54 +1781,11 @@ public class IRCParser implements SecureParser, Runnable {
1772 1781
      * @return IRCD Version or Type
1773 1782
      */
1774 1783
     public String getIRCD(final boolean getType) {
1775
-        if (h005Info.containsKey("004IRCD")) {
1776
-            final String version = h005Info.get("004IRCD");
1777
-            if (getType) {
1778
-                // This ilst is vaugly based on http://searchirc.com/ircd-versions,
1779
-                // but keeping groups of ircd's together (ie hybrid-based, ircu-based)
1780
-                if (version.matches("(?i).*unreal[^4-9].*")) { return "unreal"; }
1781
-                else if (version.matches("(?i).*unreal[4-9].*")) { return "unreal4"; }
1782
-                else if (version.matches("(?i).*bahamut.*")) { return "bahamut"; }
1783
-                else if (version.matches("(?i).*nefarious.*")) { return "nefarious"; }
1784
-                else if (version.matches("(?i).*asuka.*")) { return "asuka"; }
1785
-                else if (version.matches("(?i).*snircd.*")) { return "snircd"; }
1786
-                else if (version.matches("(?i).*beware.*")) { return "bircd"; }
1787
-                else if (version.matches("(?i).*u2\\.[0-9]+\\.H\\..*")) { return "irchispano"; }
1788
-                else if (version.matches("(?i).*u2\\.[0-9]+\\..*")) { return "ircu"; }
1789
-                else if (version.matches("(?i).*ircu.*")) { return "ircu"; }
1790
-                else if (version.matches("(?i).*plexus.*")) { return "plexus"; }
1791
-                else if (version.matches("(?i).*hybrid.*oftc.*")) { return "oftc-hybrid"; }
1792
-                else if (version.matches("(?i).*ircd.hybrid.*")) { return "hybrid7"; }
1793
-                else if (version.matches("(?i).*hybrid.*")) { return "hybrid"; }
1794
-                else if (version.matches("(?i).*charybdis.*")) { return "charybdis"; }
1795
-                else if (version.matches("(?i).*inspircd.*")) { return "inspircd"; }
1796
-                else if (version.matches("(?i).*ultimateircd.*")) { return "ultimateircd"; }
1797
-                else if (version.matches("(?i).*critenircd.*")) { return "critenircd"; }
1798
-                else if (version.matches("(?i).*fqircd.*")) { return "fqircd"; }
1799
-                else if (version.matches("(?i).*conferenceroom.*")) { return "conferenceroom"; }
1800
-                else if (version.matches("(?i).*hyperion.*")) { return "hyperion"; }
1801
-                else if (version.matches("(?i).*dancer.*")) { return "dancer"; }
1802
-                else if (version.matches("(?i).*austhex.*")) { return "austhex"; }
1803
-                else if (version.matches("(?i).*austirc.*")) { return "austirc"; }
1804
-                else if (version.matches("(?i).*ratbox.*")) { return "ratbox"; }
1805
-                else if (version.matches("(?i).*euircd.*")) { return "euircd"; }
1806
-                else if (version.matches("(?i).*weircd.*")) { return "weircd"; }
1807
-                else if (version.matches("(?i).*swiftirc.*")) { return "swiftirc"; }
1808
-                else {
1809
-                    // Stupid networks/ircds go here...
1810
-                    if (networkName.equalsIgnoreCase("ircnet")) { return "ircnet"; }
1811
-                    else if (networkName.equalsIgnoreCase("starchat")) { return "starchat"; }
1812
-                    else if (networkName.equalsIgnoreCase("bitlbee")) { return "bitlbee"; }
1813
-                    else if (h005Info.containsKey("003IRCD") && h005Info.get("003IRCD").matches("(?i).*bitlbee.*")) { return "bitlbee"; } // Older bitlbee
1814
-                    else if (h005Info.containsKey("002IRCD") && h005Info.get("002IRCD").matches("(?i).*pastiche.*")) { return "ircd-pastiche"; }
1815
-                    else { return "generic"; }
1816
-                }
1817
-            } else {
1818
-                return version;
1819
-            }
1784
+        if (getType) {
1785
+            return getServerType().getType();
1820 1786
         } else {
1821
-            if (getType) { return "generic"; }
1822
-            else { return ""; }
1787
+            final String version = h005Info.get("004IRCD");
1788
+            return (version == null) ? "" : version;
1823 1789
         }
1824 1790
     }
1825 1791
 

+ 254
- 0
src/com/dmdirc/parser/irc/ServerType.java 파일 보기

@@ -0,0 +1,254 @@
1
+/*
2
+ *  Copyright (c) 2006-2009 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.parser.irc;
24
+
25
+/**
26
+ * This defines server types.
27
+ * ServerTypes define a regex that will be matched against a lower-cased verison
28
+ * of the input, an array of modes that only ops can request, and what they are
29
+ * matching against.
30
+ * Lowercaseing will be handled using javas built in toLowercase() rather than
31
+ * usig the Server-Specific one.
32
+ *
33
+ * @author shane
34
+ */
35
+public enum ServerType {
36
+    /** Unreal IRCD prior to version 4. */
37
+    UNREAL("unreal", ".*unreal[^4-9].*"),
38
+    /** Unreal IRCD after version 4 (inspircd based). */
39
+    UNREAL4("unreal4", ".*unreal[4-9].*"),
40
+    /** Bahamut. */
41
+    BAHAMUT("bahamut", ".*bahamut.*"),
42
+    /** Some IRCU Fork. */
43
+    NEFARIOUS("nefarious", ".*nefarious.*"),
44
+    /** Quakenets old IRCU Fork. */
45
+    ASUKA("asuka", ".*asuka.*"),
46
+    /** Quakenets current IRCU Fork. */
47
+    SNIRCD("snircd", ".*snircd.*"),
48
+    /** Windows IRCU-Based IRCD. */
49
+    BIRCD("bircd", ".*beware.*"),
50
+    /** Spanish IRCU Fork. */
51
+    IRCHISPANO("irchispano", ".*u2\\.[0-9]+\\.H\\..*"),
52
+    /** IRCU. */
53
+    IRCU2("ircu", ".*u2\\.[0-9]+\\..*"),
54
+    /** Some kind of IRCU Fork. */
55
+    IRCU_GENERIC("ircu", ".*ircu.*"),
56
+    /** Plexus. */
57
+    PLEXUS("plexus", ".*plexus.*"),
58
+    /** OFTCs hybrid fork. */
59
+    OFTC_HYBRID("oftc-hybrid", ".*hybrid.*oftc.*"),
60
+    /** hybrid7. */
61
+    HYBRID7("hybrid7", ".*ircd.hybrid.*", "eI"),
62
+    /** Older versions of Hybrid. */
63
+    HYBRID("hybrid", ".*hybrid.*", "eI"),
64
+    /** Freenodes new IRCD. */
65
+    CHARYBDIS("charybdis", ".*charybdis.*", "eI"),
66
+    /** Freenodes Current IRCD. */
67
+    HYPERION("hyperion", ".*hyperion.*", "eI"),
68
+    /** Freenodes Old IRCD. */
69
+    DANCER("dancer", ".*dancer.*", "eI"),
70
+    /** Inspircd. */
71
+    INSPIRCD("inspircd", ".*inspircd.*"),
72
+    /** Ultimate IRCD. */
73
+    ULTIMATEIRCD("ultimateircd", ".*ultimateircd.*"),
74
+    /** Criten IRCD. */
75
+    CRITENIRCD("critenircd", ".*critenircd.*"),
76
+    /** fqircd. */
77
+    FQIRCD("fqircd", ".*fqircd.*"),
78
+    /** Microsoft's Conference Room "IRCD". */
79
+    CONFERENCEROOM("conferenceroom", ".*conferenceroom.*"),
80
+    /** AustHex custom IRCD. */
81
+    AUSTHEX("austhex", ".*austhex.*"),
82
+    /** AustIRC custom IRCD. */
83
+    AUSTIRC("austirc", ".*austirc.*"),
84
+    /** Ratbox. */
85
+    RATBOX("ratbox", ".*ratbox.*"),
86
+    /** euircd. */
87
+    EUIRCD("euircd", ".*euircd.*"),
88
+    /** weircd. */
89
+    WEIRCD("weircd", ".*weircd.*"),
90
+    /** swiftirc's ircd. */
91
+    SWIFTIRC("swiftirc", ".*swiftirc.*"),
92
+    /** IRCNet. */
93
+    IRCNET("ircnet", "ircnet", null, MatchType.NETWORK),
94
+    /** Star Chat. */
95
+    STARCHAT("starchat", "starchat", null, MatchType.NETWORK),
96
+    /** Newer Bitlbee. */
97
+    BITLBEE("bitlbee", "bitlbee", null, MatchType.NETWORK),
98
+    /** Older Bitlbee. */
99
+    BITLBEE_OLD("bitlbee", "bitlbee", null, MatchType.RAW003),
100
+    /** Pastiche. */
101
+    PASTICHE("bitlbee", "ircd-pastiche", null, MatchType.RAW002),
102
+    /** Generic IRCD. */
103
+    GENERIC("generic", "", null, MatchType.NEVER);
104
+
105
+    /** Define what this ServerType should match on. */
106
+    private enum MatchType {
107
+        /** Match using the ircd version from 004. */
108
+        VERSION,
109
+        /** Match using the network name. */
110
+        NETWORK,
111
+        /** Match using raw 003. */
112
+        RAW003,
113
+        /** Match using raw 002. */
114
+        RAW002,
115
+        /** Never Match (Used by generic). */
116
+        NEVER;
117
+    }
118
+
119
+    /** Type of for this ServerType. */
120
+    private final String type;
121
+
122
+    /** Regex for this ServerType. */
123
+    private final String regex;
124
+
125
+    /** String of chars that only ops can access. */
126
+    private final String opOnly;
127
+
128
+    /** What does this ServerType match? */
129
+    private final MatchType matchType;
130
+
131
+    /**
132
+     * Create a new server type.
133
+     *
134
+     * @param type The name for this type.
135
+     * @param regex The regex for this type.
136
+     */
137
+    ServerType(final String type, final String regex) {
138
+        this(type, regex, null);
139
+    }
140
+
141
+
142
+    /**
143
+     * Create a new server type.
144
+     *
145
+     * @param type The name for this type.
146
+     * @param regex The regex for this type.
147
+     * @param opOnly Any mode chars that are op-only.
148
+     */
149
+    ServerType(final String type, final String regex, final String opOnly) {
150
+        this(type, regex, opOnly, MatchType.VERSION);
151
+    }
152
+
153
+    /**
154
+     * Create a new server type.
155
+     *
156
+     * @param type The name for this type.
157
+     * @param regex The regex for this type.
158
+     * @param opOnly Any mode chars that are op-only.
159
+     * @param matchType What information should we match on?
160
+     */
161
+    ServerType(final String type, final String regex, final String opOnly, final MatchType matchType) {
162
+        this.type = type;
163
+        this.regex = regex;
164
+        this.opOnly = (opOnly == null) ? "" : opOnly;
165
+        this.matchType = matchType;
166
+    }
167
+
168
+    /**
169
+     * Get a string that defines what type this server is.
170
+     *
171
+     * @return String that defines what type this server is.
172
+     */
173
+    public String getType() {
174
+        return type;
175
+    }
176
+
177
+    /**
178
+     * Get the MatchType of this ServerType
179
+     *
180
+     * @return The MatchType of this ServerType
181
+     */
182
+    private MatchType getMatchType() {
183
+        return matchType;
184
+    }
185
+
186
+    /**
187
+     * Get the Regex used by this ServerType.
188
+     *
189
+     * @return The Regex used by this ServerType.
190
+     */
191
+    private String getRegex() {
192
+        return regex;
193
+    }
194
+
195
+    /**
196
+     * Get the modes that are op-only.
197
+     *
198
+     * @return the modes that are op-only.
199
+     */
200
+    public String getOpOnly() {
201
+        return opOnly;
202
+    }
203
+
204
+    /**
205
+     * Check if the given mode is op-only.
206
+     *
207
+     * @param mode
208
+     * @return
209
+     */
210
+    public boolean isOpOnly(final char mode) {
211
+        return (opOnly.indexOf(mode) != -1);
212
+    }
213
+
214
+    /**
215
+     * Find the first ServerType that matches the given details.
216
+     *
217
+     * @param versionInput Version from 004
218
+     * @param networkInput Network Name
219
+     * @param raw003Input 003 line
220
+     * @param raw002Input 002 line
221
+     * @return The Server type that matches the given details.
222
+     */
223
+    public static ServerType findServerType(final String versionInput, final String networkInput, final String raw003Input, final String raw002Input) {
224
+        final String version = (versionInput == null) ? "" : versionInput.toLowerCase();
225
+        final String network = (networkInput == null) ? "" : networkInput.toLowerCase();
226
+        final String raw003 = (raw003Input == null) ? "" : raw003Input.toLowerCase();
227
+        final String raw002 = (raw002Input == null) ? "" : raw002Input.toLowerCase();
228
+
229
+        for (ServerType type : ServerType.values()) {
230
+            switch (type.getMatchType()) {
231
+                case VERSION:
232
+                    if (version.matches(type.getRegex())) { return type; }
233
+                    break;
234
+                case NETWORK:
235
+                    if (network.matches(type.getRegex())) { return type; }
236
+                    break;
237
+                case RAW003:
238
+                    if (raw003.matches(type.getRegex())) { return type; }
239
+                    break;
240
+                case RAW002:
241
+                    if (raw002.matches(type.getRegex())) { return type; }
242
+                    break;
243
+                case NEVER:
244
+                    break;
245
+                default:
246
+                    /* Won't happen. */
247
+                    break;
248
+            }
249
+        }
250
+
251
+        // Return Generic IRCD.
252
+        return ServerType.GENERIC;
253
+    }
254
+}

Loading…
취소
저장