Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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