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.

Process004005.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright (c) 2006-2015 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.processors;
  23. import com.dmdirc.parser.common.ParserError;
  24. import com.dmdirc.parser.common.QueuePriority;
  25. import com.dmdirc.parser.events.NetworkDetectedEvent;
  26. import com.dmdirc.parser.irc.CapabilityState;
  27. import com.dmdirc.parser.irc.IRCEncoding;
  28. import com.dmdirc.parser.irc.IRCParser;
  29. import com.dmdirc.parser.irc.ProcessorNotFoundException;
  30. import java.util.Date;
  31. import java.util.regex.Matcher;
  32. import java.util.regex.Pattern;
  33. /**
  34. * Process ISUPPORT lines.
  35. */
  36. public class Process004005 extends IRCProcessor {
  37. /**
  38. * Create a new instance of the IRCProcessor Object.
  39. *
  40. * @param parser IRCParser That owns this IRCProcessor
  41. */
  42. public Process004005(final IRCParser parser) {
  43. super(parser, "002", "003", "004", "005");
  44. }
  45. /**
  46. * Process ISUPPORT lines.
  47. *
  48. * @param sParam Type of line to process ("005", "004")
  49. * @param token IRCTokenised line to process
  50. */
  51. @Override
  52. public void process(final String sParam, final String... token) {
  53. switch (sParam) {
  54. case "002":
  55. process002();
  56. break;
  57. case "003":
  58. process003(token);
  59. break;
  60. case "004":
  61. process004(token);
  62. break;
  63. case "005":
  64. process005(token);
  65. break;
  66. }
  67. }
  68. /**
  69. * Callback to all objects implementing the GotNetwork Callback.
  70. * This takes no params of its own, but works them out itself.
  71. */
  72. private void callGotNetwork() {
  73. final String networkName = parser.networkName;
  74. final String ircdVersion = parser.getServerSoftware();
  75. final String ircdType = parser.getServerSoftwareType();
  76. getCallbackManager().publish(
  77. new NetworkDetectedEvent(parser, new Date(), networkName, ircdVersion, ircdType));
  78. }
  79. /**
  80. * Processes a 002 line.
  81. */
  82. private void process002() {
  83. final Pattern pattern = Pattern.compile("running(?: version)? (.*)$");
  84. final Matcher matcher = pattern.matcher(parser.getLastLine());
  85. if (matcher.find()) {
  86. parser.h005Info.put("002IRCD", matcher.group(1));
  87. }
  88. }
  89. /**
  90. * Processes a 003 line.
  91. *
  92. * @param token The tokenised line.
  93. */
  94. private void process003(final String... token) {
  95. parser.h005Info.put("003IRCD", token[token.length - 1]);
  96. }
  97. /**
  98. * Processes a 004 line.
  99. *
  100. * @param token The tokenised line.
  101. */
  102. private void process004(final String... token) {
  103. final boolean multiParam = token.length > 4;
  104. int i = multiParam ? 4 : 1;
  105. final String[] bits = multiParam ? token : token[3].split(" ");
  106. if (bits.length > i) {
  107. parser.h005Info.put("004IRCD", bits[i]);
  108. i++;
  109. }
  110. if (bits.length > i && bits[i].matches("^\\d+$")) {
  111. // some IRCDs put a timestamp where the usermodes should be
  112. // (issues 4140. 4181 and 4183) so check to see if this is
  113. // numeric only, and if so, skip it.
  114. i++;
  115. }
  116. if (bits.length > i + 1) {
  117. parser.h005Info.put("USERMODES", bits[i]);
  118. parser.h005Info.put("USERCHANMODES", bits[i + 1]);
  119. i += 2;
  120. }
  121. if (bits.length > i) {
  122. // INSPIRCD includes an extra param
  123. parser.h005Info.put("USERCHANPARAMMODES", bits[i]);
  124. }
  125. parser.parseUserModes();
  126. }
  127. /**
  128. * Processes a 005 line.
  129. *
  130. * @param token The tokenised line.
  131. */
  132. private void process005(final String... token) {
  133. for (int i = 3; i < token.length; i++) {
  134. final String[] bits = token[i].split("=", 2);
  135. if (bits[0].isEmpty()) {
  136. continue;
  137. }
  138. final boolean isNegation = bits[0].charAt(0) == '-';
  139. final String key = (isNegation ? bits[0].substring(1) : bits[0]).toUpperCase();
  140. final String value = bits.length == 2 ? bits[1] : "";
  141. callDebugInfo(IRCParser.DEBUG_INFO, "%s => %s", key, value);
  142. if (isNegation) {
  143. parser.h005Info.remove(key);
  144. } else {
  145. parser.h005Info.put(key, value);
  146. }
  147. switch (key) {
  148. case "NETWORK":
  149. processNetworkToken(isNegation, value);
  150. break;
  151. case "CASEMAPPING":
  152. processCaseMappingToken(isNegation, value);
  153. break;
  154. case "CHANTYPES":
  155. processChanTypesToken(isNegation, value);
  156. break;
  157. case "PREFIX":
  158. processPrefixToken();
  159. break;
  160. case "CHANMODES":
  161. processChanModesToken();
  162. break;
  163. case "NAMESX":
  164. processNamesxToken(key);
  165. break;
  166. case "UHNAMES":
  167. processUhnamesToken(key);
  168. break;
  169. case "LISTMODE":
  170. processListmodeToken(value);
  171. break;
  172. case "TIMESTAMPEDIRC":
  173. processTimestampedIrcToken();
  174. break;
  175. }
  176. }
  177. }
  178. /**
  179. * Processes a 'NETWORK' token received in a 005.
  180. *
  181. * @param isNegation Whether the token was negated or not.
  182. * @param value The value of the token.
  183. */
  184. private void processNetworkToken(final boolean isNegation, final String value) {
  185. if (!isNegation) {
  186. parser.networkName = value;
  187. callGotNetwork();
  188. }
  189. }
  190. /**
  191. * Processes a 'CASEMAPPING' token received in a 005.
  192. *
  193. * @param isNegation Whether the token was negated or not.
  194. * @param value The value of the token.
  195. */
  196. private void processCaseMappingToken(final boolean isNegation, final String value) {
  197. if (!isNegation) {
  198. IRCEncoding encoding = IRCEncoding.RFC1459;
  199. try {
  200. encoding = IRCEncoding.valueOf(value.toUpperCase().replace('-', '_'));
  201. } catch (IllegalArgumentException ex) {
  202. parser.callErrorInfo(new ParserError(ParserError.ERROR_WARNING,
  203. "Unknown casemapping: '" + value + "' - assuming rfc1459",
  204. parser.getLastLine()));
  205. }
  206. final boolean encodingChanged = parser.getStringConverter().getEncoding() != encoding;
  207. parser.setEncoding(encoding);
  208. if (encodingChanged && parser.knownClients() == 1) {
  209. // This means that the casemapping is not rfc1459
  210. // We have only added ourselves so far (from 001)
  211. // We can fix the hashtable easily.
  212. parser.removeClient(parser.getLocalClient());
  213. parser.addClient(parser.getLocalClient());
  214. }
  215. }
  216. }
  217. /**
  218. * Processes a 'CHANTYPES' token received in a 005.
  219. *
  220. * @param isNegation Whether the token was negated or not.
  221. * @param value The value of the token.
  222. */
  223. private void processChanTypesToken(final boolean isNegation, final String value) {
  224. if (isNegation) {
  225. parser.resetChanPrefix();
  226. } else {
  227. parser.setChanPrefix(value);
  228. }
  229. }
  230. /**
  231. * Processes a 'PREFIX' token received in a 005.
  232. */
  233. private void processPrefixToken() {
  234. parser.parsePrefixModes();
  235. }
  236. /**
  237. * Processes a 'CHANMODES' token received in a 005.
  238. */
  239. private void processChanModesToken() {
  240. parser.parseChanModes();
  241. }
  242. /**
  243. * Processes a 'NAMEX' token received in a 005.
  244. *
  245. * @param key The NAMEX token that was received.
  246. */
  247. private void processNamesxToken(final String key) {
  248. if (parser.getCapabilityState("multi-prefix") != CapabilityState.ENABLED) {
  249. parser.sendString("PROTOCTL " + key);
  250. }
  251. }
  252. /**
  253. * Processes a 'UHNAMES' token received in a 005.
  254. *
  255. * @param key The UHNAMES token that was received.
  256. */
  257. private void processUhnamesToken(final String key) {
  258. if (parser.getCapabilityState("userhost-in-names") != CapabilityState.ENABLED) {
  259. parser.sendString("PROTOCTL " + key);
  260. }
  261. }
  262. /**
  263. * Processes a 'LISTMODE' token received in a 005. The LISTMODE token
  264. * indicates support for a new way of describing list modes (such as +b).
  265. * See the proposal at http://shanemcc.co.uk/irc/#listmode.
  266. *
  267. * @param value The value of the token.
  268. */
  269. private void processListmodeToken(final String value) {
  270. final String[] handles = new String[2];
  271. handles[0] = value; // List mode item
  272. final String endValue = Integer.toString(Integer.parseInt(value) + 1);
  273. parser.h005Info.put("LISTMODEEND", endValue);
  274. handles[1] = endValue; // List mode end
  275. try {
  276. parser.getProcessingManager().addProcessor(handles,
  277. parser.getProcessingManager().getProcessor("__LISTMODE__"));
  278. } catch (ProcessorNotFoundException e) {
  279. }
  280. }
  281. /**
  282. * Processes a 'TIMESTAMPEDIRC' token received in a 005. The TIMESTAMPEDIRC
  283. * protocol allows servers (or proxies) to send historical events with a
  284. * corresponding timestamp, allowing the client to catch up on events that
  285. * happened prior to them connecting. See the proposal at
  286. * http://shanemcc.co.uk/irc/#timestamping.
  287. */
  288. private void processTimestampedIrcToken() {
  289. if (parser.getCapabilityState("dfbnc.com/tsirc") != CapabilityState.ENABLED) {
  290. parser.sendString("TIMESTAMPEDIRC ON", QueuePriority.HIGH);
  291. }
  292. }
  293. }