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

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