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

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