Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Process004005.java 11KB

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