You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ServerType.java 9.5KB

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