Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

IRCParser.java 82KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421
  1. /*
  2. * Copyright (c) 2006-2013 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;
  23. import com.dmdirc.parser.common.BaseParser;
  24. import com.dmdirc.parser.common.ChannelJoinRequest;
  25. import com.dmdirc.parser.common.ChildImplementations;
  26. import com.dmdirc.parser.common.CompositionState;
  27. import com.dmdirc.parser.common.IgnoreList;
  28. import com.dmdirc.parser.common.MyInfo;
  29. import com.dmdirc.parser.common.ParserError;
  30. import com.dmdirc.parser.common.QueuePriority;
  31. import com.dmdirc.parser.common.SRVRecord;
  32. import com.dmdirc.parser.common.SystemEncoder;
  33. import com.dmdirc.parser.interfaces.Encoder;
  34. import com.dmdirc.parser.interfaces.EncodingParser;
  35. import com.dmdirc.parser.interfaces.SecureParser;
  36. import com.dmdirc.parser.interfaces.callbacks.ConnectErrorListener;
  37. import com.dmdirc.parser.interfaces.callbacks.DataInListener;
  38. import com.dmdirc.parser.interfaces.callbacks.DataOutListener;
  39. import com.dmdirc.parser.interfaces.callbacks.DebugInfoListener;
  40. import com.dmdirc.parser.interfaces.callbacks.ErrorInfoListener;
  41. import com.dmdirc.parser.interfaces.callbacks.PingFailureListener;
  42. import com.dmdirc.parser.interfaces.callbacks.PingSentListener;
  43. import com.dmdirc.parser.interfaces.callbacks.PingSuccessListener;
  44. import com.dmdirc.parser.interfaces.callbacks.ServerErrorListener;
  45. import com.dmdirc.parser.interfaces.callbacks.ServerReadyListener;
  46. import com.dmdirc.parser.interfaces.callbacks.SocketCloseListener;
  47. import com.dmdirc.parser.irc.IRCReader.ReadLine;
  48. import com.dmdirc.parser.irc.outputqueue.OutputQueue;
  49. import java.io.IOException;
  50. import java.net.Inet4Address;
  51. import java.net.Inet6Address;
  52. import java.net.InetAddress;
  53. import java.net.InetSocketAddress;
  54. import java.net.Proxy;
  55. import java.net.Socket;
  56. import java.net.URI;
  57. import java.net.URISyntaxException;
  58. import java.net.UnknownHostException;
  59. import java.security.KeyManagementException;
  60. import java.security.NoSuchAlgorithmException;
  61. import java.security.cert.X509Certificate;
  62. import java.util.ArrayList;
  63. import java.util.Arrays;
  64. import java.util.Collection;
  65. import java.util.Collections;
  66. import java.util.Date;
  67. import java.util.Deque;
  68. import java.util.HashMap;
  69. import java.util.LinkedList;
  70. import java.util.List;
  71. import java.util.Map;
  72. import java.util.Queue;
  73. import java.util.Timer;
  74. import java.util.concurrent.Semaphore;
  75. import java.util.concurrent.atomic.AtomicBoolean;
  76. import javax.net.ssl.KeyManager;
  77. import javax.net.ssl.SSLContext;
  78. import javax.net.ssl.SSLSocket;
  79. import javax.net.ssl.SSLSocketFactory;
  80. import javax.net.ssl.TrustManager;
  81. import javax.net.ssl.X509TrustManager;
  82. /**
  83. * IRC Parser.
  84. */
  85. @ChildImplementations({
  86. IRCChannelClientInfo.class,
  87. IRCChannelInfo.class,
  88. IRCClientInfo.class
  89. })
  90. public class IRCParser extends BaseParser implements SecureParser, EncodingParser {
  91. /** Max length an outgoing line should be (NOT including \r\n). */
  92. public static final int MAX_LINELENGTH = 510;
  93. /** General Debug Information. */
  94. public static final int DEBUG_INFO = 1;
  95. /** Socket Debug Information. */
  96. public static final int DEBUG_SOCKET = 2;
  97. /** Processing Manager Debug Information. */
  98. public static final int DEBUG_PROCESSOR = 4;
  99. /** List Mode Queue Debug Information. */
  100. public static final int DEBUG_LMQ = 8;
  101. /** Attempt to update user host all the time, not just on Who/Add/NickChange. */
  102. static final boolean ALWAYS_UPDATECLIENT = true;
  103. /** Byte used to show that a non-boolean mode is a list (b). */
  104. static final byte MODE_LIST = 1;
  105. /** Byte used to show that a non-boolean mode is not a list, and requires a parameter to set (lk). */
  106. static final byte MODE_SET = 2;
  107. /** Byte used to show that a non-boolean mode is not a list, and requires a parameter to unset (k). */
  108. static final byte MODE_UNSET = 4;
  109. /**
  110. * Default channel prefixes if none are specified by the IRCd.
  111. *
  112. * <p>These are the RFC 2811 specified prefixes: '#', '&amp;', '!' and '+'.
  113. */
  114. private static final String DEFAULT_CHAN_PREFIX = "#&!+";
  115. /**
  116. * This is what the user wants settings to be.
  117. * Nickname here is *not* always accurate.<br><br>
  118. * ClientInfo variable tParser.getMyself() should be used for accurate info.
  119. */
  120. private MyInfo me = new MyInfo();
  121. /** Should PINGs be sent to the server to check if its alive? */
  122. private boolean checkServerPing = true;
  123. /** Timer for server ping. */
  124. private Timer pingTimer = null;
  125. /** Semaphore for access to pingTimer. */
  126. private final Semaphore pingTimerSem = new Semaphore(1);
  127. /** Is a ping needed? */
  128. private final AtomicBoolean pingNeeded = new AtomicBoolean(false);
  129. /** Time last ping was sent at. */
  130. private long pingTime;
  131. /** Current Server Lag. */
  132. private long serverLag;
  133. /** Last value sent as a ping argument. */
  134. private String lastPingValue = "";
  135. /**
  136. * Count down to next ping.
  137. * The timer fires every 10 seconds, this value is decreased every time the
  138. * timer fires.<br>
  139. * Once it reaches 0, we send a ping, and reset it to 6, this means we ping
  140. * the server every minute.
  141. *
  142. * @see setPingCountDownLength
  143. */
  144. private int pingCountDown;
  145. /** Network name. This is "" if no network name is provided */
  146. String networkName;
  147. /** This is what we think the nickname should be. */
  148. String thinkNickname;
  149. /** When using inbuilt pre-001 NickInUse handler, have we tried our AltNick. */
  150. boolean triedAlt;
  151. /** Have we received the 001. */
  152. boolean got001;
  153. /** Have we fired post005? */
  154. boolean post005;
  155. /** Has the thread started execution yet, (Prevents run() being called multiple times). */
  156. boolean hasBegan;
  157. /** Connect timeout. */
  158. private int connectTimeout = 5000;
  159. /** Hashtable storing known prefix modes (ohv). */
  160. final Map<Character, Long> prefixModes = new HashMap<>();
  161. /**
  162. * Hashtable maping known prefix modes (ohv) to prefixes (@%+) - Both ways.
  163. * Prefix map contains 2 pairs for each mode. (eg @ => o and o => @)
  164. */
  165. final Map<Character, Character> prefixMap = new HashMap<>();
  166. /** Integer representing the next avaliable integer value of a prefix mode. */
  167. long nextKeyPrefix = 1;
  168. /** Hashtable storing known user modes (owxis etc). */
  169. final Map<Character, Long> userModes = new HashMap<>();
  170. /** Integer representing the next avaliable integer value of a User mode. */
  171. long nNextKeyUser = 1;
  172. /**
  173. * Hashtable storing known boolean chan modes (cntmi etc).
  174. * Valid Boolean Modes are stored as Hashtable.pub('m',1); where 'm' is the mode and 1 is a numeric value.<br><br>
  175. * Numeric values are powers of 2. This allows up to 32 modes at present (expandable to 64)<br><br>
  176. * ChannelInfo/ChannelClientInfo etc provide methods to view the modes in a human way.<br><br>
  177. * <br>
  178. * Channel modes discovered but not listed in 005 are stored as boolean modes automatically (and a ERROR_WARNING Error is called)
  179. */
  180. final Map<Character, Long> chanModesBool = new HashMap<>();
  181. /** Integer representing the next avaliable integer value of a Boolean mode. */
  182. long nextKeyCMBool = 1;
  183. /**
  184. * Hashtable storing known non-boolean chan modes (klbeI etc).
  185. * Non Boolean Modes (for Channels) are stored together in this hashtable, the value param
  186. * is used to show the type of variable. (List (1), Param just for set (2), Param for Set and Unset (2+4=6))<br><br>
  187. * <br>
  188. * see MODE_LIST<br>
  189. * see MODE_SET<br>
  190. * see MODE_UNSET<br>
  191. */
  192. final Map<Character, Byte> chanModesOther = new HashMap<>();
  193. /** The last line of input received from the server */
  194. private ReadLine lastLine = null;
  195. /** Should the lastline (where given) be appended to the "data" part of any onErrorInfo call? */
  196. private boolean addLastLine = false;
  197. /** Channel Prefixes (ie # + etc). */
  198. private String chanPrefix = DEFAULT_CHAN_PREFIX;
  199. /** Hashtable storing all known clients based on nickname (in lowercase). */
  200. private final Map<String, IRCClientInfo> clientList = new HashMap<>();
  201. /** Hashtable storing all known channels based on chanel name (inc prefix - in lowercase). */
  202. private final Map<String, IRCChannelInfo> channelList = new HashMap<>();
  203. /** Reference to the ClientInfo object that references ourself. */
  204. private IRCClientInfo myself = new IRCClientInfo(this, "myself").setFake(true);
  205. /** Hashtable storing all information gathered from 005. */
  206. final Map<String, String> h005Info = new HashMap<>();
  207. /** difference in ms between our time and the servers time (used for timestampedIRC). */
  208. long tsdiff;
  209. /** Reference to the Processing Manager. */
  210. private final ProcessingManager myProcessingManager = new ProcessingManager(this);
  211. /** Should we automatically disconnect on fatal errors?. */
  212. private boolean disconnectOnFatal = true;
  213. /** Current Socket State. */
  214. protected SocketState currentSocketState = SocketState.NULL;
  215. /** This is the socket used for reading from/writing to the IRC server. */
  216. private Socket socket;
  217. /**
  218. * The underlying socket used for reading/writing to the IRC server.
  219. * For normal sockets this will be the same as {@link #socket} but for SSL
  220. * connections this will be the underlying {@link Socket} while
  221. * {@link #socket} will be an {@link SSLSocket}.
  222. */
  223. private Socket rawSocket;
  224. /** Used for writing to the server. */
  225. private OutputQueue out;
  226. /** The encoder to use to encode incoming lines. */
  227. private Encoder encoder = new SystemEncoder();
  228. /** Used for reading from the server. */
  229. private IRCReader in;
  230. /** This is the default TrustManager for SSL Sockets, it trusts all ssl certs. */
  231. private final TrustManager[] trustAllCerts = {
  232. new X509TrustManager() {
  233. @Override
  234. public X509Certificate[] getAcceptedIssuers() {
  235. return null;
  236. }
  237. @Override
  238. public void checkClientTrusted(final X509Certificate[] certs, final String authType) {
  239. }
  240. @Override
  241. public void checkServerTrusted(final X509Certificate[] certs, final String authType) {
  242. }
  243. },};
  244. /** Should channels automatically request list modes? */
  245. private boolean autoListMode = true;
  246. /** Should part/quit/kick callbacks be fired before removing the user internally? */
  247. private boolean removeAfterCallback = true;
  248. /** This is the TrustManager used for SSL Sockets. */
  249. private TrustManager[] myTrustManager = trustAllCerts;
  250. /** The KeyManagers used for client certificates for SSL sockets. */
  251. private KeyManager[] myKeyManagers;
  252. /** This is list containing 001 - 005 inclusive. */
  253. private final List<String> serverInformationLines = new LinkedList<>();
  254. /** Map of capabilities and their state. */
  255. private final Map<String, CapabilityState> capabilities = new HashMap<>();
  256. /**
  257. * Default constructor, ServerInfo and MyInfo need to be added separately (using IRC.me and IRC.server).
  258. */
  259. public IRCParser() {
  260. this((MyInfo) null);
  261. }
  262. /**
  263. * Constructor with ServerInfo, MyInfo needs to be added separately (using IRC.me).
  264. *
  265. * @param uri The URI to connect to
  266. */
  267. public IRCParser(final URI uri) {
  268. this(null, uri);
  269. }
  270. /**
  271. * Constructor with MyInfo, ServerInfo needs to be added separately (using IRC.server).
  272. *
  273. * @param myDetails Client information.
  274. */
  275. public IRCParser(final MyInfo myDetails) {
  276. this(myDetails, null);
  277. }
  278. /**
  279. * Creates a new IRCParser with the specified client details which will
  280. * connect to the specified URI.
  281. *
  282. * @since 0.6.3
  283. * @param myDetails The client details to use
  284. * @param uri The URI to connect to
  285. */
  286. public IRCParser(final MyInfo myDetails, final URI uri) {
  287. super(uri);
  288. out = new OutputQueue();
  289. if (myDetails != null) {
  290. this.me = myDetails;
  291. }
  292. setIgnoreList(new IgnoreList());
  293. setPingTimerInterval(10000);
  294. setPingTimerFraction(6);
  295. resetState();
  296. }
  297. /**
  298. * Get the current OutputQueue
  299. *
  300. * @return the current OutputQueue
  301. */
  302. public OutputQueue getOutputQueue() {
  303. return out;
  304. }
  305. /** {@inheritDoc} */
  306. @Override
  307. public boolean compareURI(final URI uri) {
  308. // Get the old URI.
  309. final URI oldURI = getURI();
  310. final URI newURI = uri;
  311. // Check that protocol, host and port are the same.
  312. // Anything else won't change the server we connect to just what we
  313. // would do after connecting, so is not relevent.
  314. return newURI.getScheme().equalsIgnoreCase(oldURI.getScheme())
  315. && newURI.getHost().equalsIgnoreCase(oldURI.getHost())
  316. && ((newURI.getUserInfo() == null || newURI.getUserInfo().isEmpty()) || newURI.getUserInfo().equalsIgnoreCase((oldURI.getUserInfo() == null ? "" : oldURI.getUserInfo())))
  317. && newURI.getPort() == oldURI.getPort();
  318. }
  319. /**
  320. * From the given URI, get a URI to actually connect to.
  321. * This function will check for DNS SRV records for the given URI and use
  322. * those if found.
  323. * If no SRV records exist, then fallback to using the URI as-is but with
  324. * a default port specified if none is given.
  325. *
  326. * @param uri Requested URI.
  327. * @return A connectable version of the given URI.
  328. */
  329. private URI getConnectURI(final URI uri) {
  330. if (uri == null) { return null; }
  331. final boolean isSSL = uri.getScheme().endsWith("s");
  332. final int defaultPort = isSSL ? 6697 : 6667;
  333. // Default to what the URI has already..
  334. int port = uri.getPort();
  335. String host = uri.getHost();
  336. // Look for SRV records if no port is specified.
  337. if (port == -1) {
  338. List<SRVRecord> recordList = new ArrayList<>();
  339. if (isSSL) {
  340. // There are a few possibilities for ssl...
  341. final String[] protocols = {"_ircs._tcp.", "_irc._tls."};
  342. for (final String protocol : protocols) {
  343. recordList = SRVRecord.getRecords(protocol + host);
  344. if (!recordList.isEmpty()) {
  345. break;
  346. }
  347. }
  348. } else {
  349. recordList = SRVRecord.getRecords("_irc._tcp." + host);
  350. }
  351. if (!recordList.isEmpty()) {
  352. host = recordList.get(0).getHost();
  353. port = recordList.get(0).getPort();
  354. }
  355. }
  356. // Fix the port if required.
  357. if (port == -1) { port = defaultPort; }
  358. // Return the URI to connect to based on the above.
  359. try {
  360. return new URI(uri.getScheme(), uri.getUserInfo(), host, port, uri.getPath(), uri.getQuery(), uri.getFragment());
  361. } catch (URISyntaxException ex) {
  362. // Shouldn't happen - but return the URI as-is if it does.
  363. return uri;
  364. }
  365. }
  366. /** {@inheritDoc} */
  367. @Override
  368. public Collection<? extends ChannelJoinRequest> extractChannels(final URI uri) {
  369. if (uri == null) {
  370. return Collections.<ChannelJoinRequest>emptyList();
  371. }
  372. String channelString = uri.getPath();
  373. if (uri.getRawQuery() != null && !uri.getRawQuery().isEmpty()) {
  374. channelString += "?" + uri.getRawQuery();
  375. }
  376. if (uri.getRawFragment() != null && !uri.getRawFragment().isEmpty()) {
  377. channelString += "#" + uri.getRawFragment();
  378. }
  379. if (!channelString.isEmpty() && channelString.charAt(0) == '/') {
  380. channelString = channelString.substring(1);
  381. }
  382. return extractChannels(channelString);
  383. }
  384. /**
  385. * Extracts a set of channels and optional keys from the specified String.
  386. * Channels are separated by commas, and keys are separated from their
  387. * channels by a space.
  388. *
  389. * @since 0.6.4
  390. * @param channels The string of channels to parse
  391. * @return A corresponding collection of join request objects
  392. */
  393. protected Collection<? extends ChannelJoinRequest> extractChannels(final String channels) {
  394. final List<ChannelJoinRequest> res = new ArrayList<>();
  395. for (String channel : channels.split(",")) {
  396. final String[] parts = channel.split(" ", 2);
  397. if (parts.length == 2) {
  398. res.add(new ChannelJoinRequest(parts[0], parts[1]));
  399. } else {
  400. res.add(new ChannelJoinRequest(parts[0]));
  401. }
  402. }
  403. return res;
  404. }
  405. /**
  406. * Get the current Value of autoListMode.
  407. *
  408. * @return Value of autoListMode (true if channels automatically ask for list modes on join, else false)
  409. */
  410. public boolean getAutoListMode() {
  411. return autoListMode;
  412. }
  413. /**
  414. * Set the current Value of autoListMode.
  415. *
  416. * @param newValue New value to set autoListMode
  417. */
  418. public void setAutoListMode(final boolean newValue) {
  419. autoListMode = newValue;
  420. }
  421. /**
  422. * Get the current Value of removeAfterCallback.
  423. *
  424. * @return Value of removeAfterCallback (true if kick/part/quit callbacks are fired before internal removal)
  425. */
  426. public boolean getRemoveAfterCallback() {
  427. return removeAfterCallback;
  428. }
  429. /**
  430. * Get the current Value of removeAfterCallback.
  431. *
  432. * @param newValue New value to set removeAfterCallback
  433. */
  434. public void setRemoveAfterCallback(final boolean newValue) {
  435. removeAfterCallback = newValue;
  436. }
  437. /**
  438. * Get the current Value of addLastLine.
  439. *
  440. * @return Value of addLastLine (true if lastLine info will be automatically
  441. * added to the errorInfo data line). This should be true if lastLine
  442. * isn't handled any other way.
  443. */
  444. public boolean getAddLastLine() {
  445. return addLastLine;
  446. }
  447. /**
  448. * Get the current Value of addLastLine.
  449. *
  450. * @param newValue New value to set addLastLine
  451. */
  452. public void setAddLastLine(final boolean newValue) {
  453. addLastLine = newValue;
  454. }
  455. /**
  456. * Get the current Value of connectTimeout.
  457. *
  458. * @return The value of getConnectTimeout.
  459. */
  460. public int getConnectTimeout() {
  461. return connectTimeout;
  462. }
  463. /**
  464. * Set the Value of connectTimeout.
  465. *
  466. * @param newValue new value for value of getConnectTimeout.
  467. */
  468. public void setConnectTimeout(final int newValue) {
  469. connectTimeout = newValue;
  470. }
  471. /**
  472. * Get the current socket State.
  473. *
  474. * @since 0.6.3m1
  475. * @return Current {@link SocketState}
  476. */
  477. public SocketState getSocketState() {
  478. return currentSocketState;
  479. }
  480. /**
  481. * Get a reference to the Processing Manager.
  482. *
  483. * @return Reference to the CallbackManager
  484. */
  485. public ProcessingManager getProcessingManager() {
  486. return myProcessingManager;
  487. }
  488. /**
  489. * Get a reference to the default TrustManager for SSL Sockets.
  490. *
  491. * @return a reference to trustAllCerts
  492. */
  493. public TrustManager[] getDefaultTrustManager() {
  494. return Arrays.copyOf(trustAllCerts, trustAllCerts.length);
  495. }
  496. /**
  497. * Get a reference to the current TrustManager for SSL Sockets.
  498. *
  499. * @return a reference to myTrustManager;
  500. */
  501. public TrustManager[] getTrustManager() {
  502. return Arrays.copyOf(myTrustManager, myTrustManager.length);
  503. }
  504. /** {@inheritDoc} */
  505. @Override
  506. public void setTrustManagers(final TrustManager[] managers) {
  507. myTrustManager = managers == null ? null : Arrays.copyOf(managers, managers.length);
  508. }
  509. /** {@inheritDoc} */
  510. @Override
  511. public void setKeyManagers(final KeyManager[] managers) {
  512. myKeyManagers = managers == null ? null : Arrays.copyOf(managers, managers.length);
  513. }
  514. //---------------------------------------------------------------------------
  515. // Start Callbacks
  516. //---------------------------------------------------------------------------
  517. /**
  518. * Callback to all objects implementing the ServerError Callback.
  519. *
  520. * @see com.dmdirc.parser.interfaces.callbacks.ServerErrorListener
  521. * @param message The error message
  522. */
  523. protected void callServerError(final String message) {
  524. getCallback(ServerErrorListener.class).onServerError(null, null, message);
  525. }
  526. /**
  527. * Callback to all objects implementing the DataIn Callback.
  528. *
  529. * @see com.dmdirc.parser.interfaces.callbacks.DataInListener
  530. * @param data Incoming Line.
  531. */
  532. protected void callDataIn(final String data) {
  533. getCallback(DataInListener.class).onDataIn(null, null, data);
  534. }
  535. /**
  536. * Callback to all objects implementing the DataOut Callback.
  537. *
  538. * @param data Outgoing Data
  539. * @param fromParser True if parser sent the data, false if sent using .sendLine
  540. * @see com.dmdirc.parser.interfaces.callbacks.DataOutListener
  541. */
  542. protected void callDataOut(final String data, final boolean fromParser) {
  543. getCallback(DataOutListener.class).onDataOut(null, null, data, fromParser);
  544. }
  545. /**
  546. * Callback to all objects implementing the DebugInfo Callback.
  547. *
  548. * @see com.dmdirc.parser.interfaces.callbacks.DebugInfoListener
  549. * @param level Debugging Level (DEBUG_INFO, DEBUG_SOCKET etc)
  550. * @param data Debugging Information as a format string
  551. * @param args Formatting String Options
  552. */
  553. protected void callDebugInfo(final int level, final String data, final Object... args) {
  554. callDebugInfo(level, String.format(data, args));
  555. }
  556. /**
  557. * Callback to all objects implementing the DebugInfo Callback.
  558. *
  559. * @see com.dmdirc.parser.interfaces.callbacks.DebugInfoListener
  560. * @param level Debugging Level (DEBUG_INFO, DEBUG_SOCKET etc)
  561. * @param data Debugging Information
  562. */
  563. protected void callDebugInfo(final int level, final String data) {
  564. getCallback(DebugInfoListener.class).onDebugInfo(null, null, level, data);
  565. }
  566. /**
  567. * Callback to all objects implementing the IErrorInfo Interface.
  568. *
  569. * @see com.dmdirc.parser.interfaces.callbacks.ErrorInfoListener
  570. * @param errorInfo ParserError object representing the error.
  571. */
  572. protected void callErrorInfo(final ParserError errorInfo) {
  573. getCallback(ErrorInfoListener.class).onErrorInfo(null, null, errorInfo);
  574. }
  575. /**
  576. * Callback to all objects implementing the IConnectError Interface.
  577. *
  578. * @see com.dmdirc.parser.interfaces.callbacks.ConnectErrorListener
  579. * @param errorInfo ParserError object representing the error.
  580. */
  581. protected void callConnectError(final ParserError errorInfo) {
  582. getCallback(ConnectErrorListener.class).onConnectError(null, null, errorInfo);
  583. }
  584. /**
  585. * Callback to all objects implementing the SocketClosed Callback.
  586. *
  587. * @see com.dmdirc.parser.interfaces.callbacks.SocketCloseListener
  588. */
  589. protected void callSocketClosed() {
  590. getCallback(SocketCloseListener.class).onSocketClosed(null, null);
  591. }
  592. /**
  593. * Callback to all objects implementing the PingFailed Callback.
  594. *
  595. * @see com.dmdirc.parser.interfaces.callbacks.PingFailureListener
  596. * @return True if any callback was called, false otherwise.
  597. */
  598. protected boolean callPingFailed() {
  599. return getCallbackManager().getCallbackType(PingFailureListener.class).call();
  600. }
  601. /**
  602. * Callback to all objects implementing the PingSent Callback.
  603. *
  604. * @see com.dmdirc.parser.interfaces.callbacks.PingSentListener
  605. */
  606. protected void callPingSent() {
  607. getCallback(PingSentListener.class).onPingSent(null, null);
  608. }
  609. /**
  610. * Callback to all objects implementing the PingSuccess Callback.
  611. *
  612. * @see com.dmdirc.parser.interfaces.callbacks.PingSuccessListener
  613. */
  614. protected void callPingSuccess() {
  615. getCallback(PingSuccessListener.class).onPingSuccess(null, null);
  616. }
  617. /**
  618. * Callback to all objects implementing the Post005 Callback.
  619. *
  620. * @see com.dmdirc.parser.interfaces.callbacks.ServerReadyListener
  621. */
  622. protected synchronized void callPost005() {
  623. if (post005) {
  624. return;
  625. }
  626. post005 = true;
  627. if (!h005Info.containsKey("PREFIX")) {
  628. parsePrefixModes();
  629. }
  630. if (!h005Info.containsKey("USERMODES")) {
  631. parseUserModes();
  632. }
  633. if (!h005Info.containsKey("CHANMODES")) {
  634. parseChanModes();
  635. }
  636. getCallback(ServerReadyListener.class).onServerReady(null, null);
  637. }
  638. //---------------------------------------------------------------------------
  639. // End Callbacks
  640. //---------------------------------------------------------------------------
  641. /** Reset internal state (use before doConnect). */
  642. private void resetState() {
  643. // Reset General State info
  644. triedAlt = false;
  645. got001 = false;
  646. post005 = false;
  647. // Clear the hash tables
  648. channelList.clear();
  649. clientList.clear();
  650. h005Info.clear();
  651. prefixModes.clear();
  652. prefixMap.clear();
  653. chanModesOther.clear();
  654. chanModesBool.clear();
  655. userModes.clear();
  656. chanPrefix = DEFAULT_CHAN_PREFIX;
  657. // Clear output queue.
  658. if (out != null) {
  659. out.clearQueue();
  660. }
  661. // Reset the mode indexes
  662. nextKeyPrefix = 1;
  663. nextKeyCMBool = 1;
  664. nNextKeyUser = 1;
  665. setServerName("");
  666. networkName = "";
  667. lastLine = null;
  668. myself = new IRCClientInfo(this, "myself").setFake(true);
  669. synchronized (serverInformationLines) {
  670. serverInformationLines.clear();
  671. }
  672. stopPingTimer();
  673. currentSocketState = SocketState.CLOSED;
  674. setEncoding(IRCEncoding.RFC1459);
  675. }
  676. /**
  677. * Called after other error callbacks.
  678. * CallbackOnErrorInfo automatically calls this *AFTER* any registered callbacks
  679. * for it are called.
  680. *
  681. * @param errorInfo ParserError object representing the error.
  682. * @param called True/False depending on the the success of other callbacks.
  683. */
  684. public void onPostErrorInfo(final ParserError errorInfo, final boolean called) {
  685. if (errorInfo.isFatal() && disconnectOnFatal) {
  686. disconnect("Fatal Parser Error");
  687. }
  688. }
  689. /**
  690. * Get the current Value of disconnectOnFatal.
  691. *
  692. * @return Value of disconnectOnFatal (true if the parser automatically disconnects on fatal errors, else false)
  693. */
  694. public boolean getDisconnectOnFatal() {
  695. return disconnectOnFatal;
  696. }
  697. /**
  698. * Set the current Value of disconnectOnFatal.
  699. *
  700. * @param newValue New value to set disconnectOnFatal
  701. */
  702. public void setDisconnectOnFatal(final boolean newValue) {
  703. disconnectOnFatal = newValue;
  704. }
  705. private Socket newSocket(final URI target, final URI proxy) throws IOException {
  706. if (target.getPort() > 65535 || target.getPort() <= 0) {
  707. throw new IOException("Server port (" + target.getPort() + ") is invalid.");
  708. }
  709. Socket mySocket;
  710. if (proxy == null) {
  711. callDebugInfo(DEBUG_SOCKET, "Not using Proxy");
  712. mySocket = new Socket();
  713. if (getBindIP() != null && !getBindIP().isEmpty()) {
  714. callDebugInfo(DEBUG_SOCKET, "Binding to IP: " + getBindIP());
  715. try {
  716. mySocket.bind(new InetSocketAddress(InetAddress.getByName(getBindIP()), 0));
  717. } catch (IOException e) {
  718. callDebugInfo(DEBUG_SOCKET, "Binding failed: " + e.getMessage());
  719. }
  720. }
  721. mySocket.connect(new InetSocketAddress(target.getHost(), target.getPort()), connectTimeout);
  722. } else {
  723. callDebugInfo(DEBUG_SOCKET, "Using Proxy");
  724. if (getBindIP() != null && !getBindIP().isEmpty()) {
  725. callDebugInfo(DEBUG_SOCKET, "IP Binding is not possible when using a proxy.");
  726. }
  727. final String proxyHost = proxy.getHost();
  728. final int proxyPort = proxy.getPort();
  729. if (proxyPort > 65535 || proxyPort <= 0) {
  730. throw new IOException("Proxy port (" + proxyPort + ") is invalid.");
  731. }
  732. final Proxy.Type proxyType = Proxy.Type.valueOf(proxy.getScheme().toUpperCase());
  733. mySocket = new Socket(new Proxy(proxyType, new InetSocketAddress(proxyHost, proxyPort)));
  734. final IRCAuthenticator ia = IRCAuthenticator.getIRCAuthenticator();
  735. try {
  736. try {
  737. ia.getSemaphore().acquire();
  738. } catch (InterruptedException ex) {
  739. }
  740. ia.addAuthentication(target, proxy);
  741. mySocket.connect(new InetSocketAddress(target.getHost(), target.getPort()), connectTimeout);
  742. } finally {
  743. ia.removeAuthentication(target, proxy);
  744. ia.getSemaphore().release();
  745. }
  746. }
  747. return mySocket;
  748. }
  749. /**
  750. * Find a socket to connect using, this will where possible attempt IPv6
  751. * first, before falling back to IPv4.
  752. *
  753. * This will:
  754. * - Try using v6 first before v4.
  755. * - If there are any failures at all with v6 (including, not having a
  756. * v6 address...) then fall through to v4.
  757. * - If there is no v4 address, throw the v6 exception
  758. * - Otherwise, try v4
  759. * - If there are failures with v4, throw the exception.
  760. *
  761. * If we have both IPv6 and IPv4, and a target host has both IPv6 and IPv4
  762. * addresses, but does not listen on the requested port on either, this
  763. * will cause a double-length connection timeout.
  764. *
  765. * @param target Target URI to connect to.
  766. * @param proxy Proxy URI to use
  767. * @return Socket to use in future connections.
  768. * @throws IOException if there is an error.
  769. */
  770. private Socket findSocket(final URI target, final URI proxy) throws IOException {
  771. URI target6 = null;
  772. URI target4 = null;
  773. // Get the v6 and v4 addresses if appropriate..
  774. try {
  775. for (InetAddress i : InetAddress.getAllByName(target.getHost())) {
  776. if (target6 == null && i instanceof Inet6Address) {
  777. target6 = new URI(target.getScheme(), target.getUserInfo(), i.getHostAddress(), target.getPort(), target.getPath(), target.getQuery(), target.getFragment());
  778. } else if (target4 == null && i instanceof Inet4Address) {
  779. target4 = new URI(target.getScheme(), target.getUserInfo(), i.getHostAddress(), target.getPort(), target.getPath(), target.getQuery(), target.getFragment());
  780. }
  781. }
  782. } catch (final URISyntaxException use) { /* Won't happen. */ }
  783. // Now try and connect.
  784. // Try using v6 first before v4.
  785. // If there are any failures at all with v6 (including, not having a
  786. // v6 address...) then fall through to v4.
  787. // If there is no v4 address, throw the v6 exception
  788. // Otherwise, try v4
  789. // If there are failures with v4, throw the exception.
  790. //
  791. // If we have both IPv6 and IPv4, and a target host has both IPv6 and
  792. // IPv4 addresses, but does not listen on the requested port on either,
  793. // this will cause a double-length connection timeout.
  794. //
  795. // In future this may want to be rewritten to perform a happy-eyeballs
  796. // race rather than trying one then the other.
  797. Exception v6Exception = null;
  798. if (target6 != null) {
  799. try {
  800. return newSocket(target6, proxy);
  801. } catch (final IOException ioe) {
  802. if (target4 == null) {
  803. throw ioe;
  804. }
  805. } catch (final Exception e) {
  806. v6Exception = e;
  807. callDebugInfo(DEBUG_SOCKET, "Exception trying to use IPv6: " + e);
  808. }
  809. }
  810. if (target4 != null) {
  811. try {
  812. return newSocket(target4, proxy);
  813. } catch (final IOException e2) {
  814. callDebugInfo(DEBUG_SOCKET, "Exception trying to use IPv4: " + e2);
  815. throw e2;
  816. }
  817. } else if (v6Exception != null) {
  818. throw new IOException("Error connecting to: " + target + " (IPv6 failure, with no IPv4 fallback)", v6Exception);
  819. } else {
  820. // We should never get here as if both target4 and target6 were null
  821. // getAllByName would have thrown an exception instead.
  822. throw new IOException("Error connecting to: " + target + " (General connectivity failure)");
  823. }
  824. }
  825. /**
  826. * Connect to IRC.
  827. *
  828. * @throws IOException if the socket can not be connected
  829. * @throws NoSuchAlgorithmException if SSL is not available
  830. * @throws KeyManagementException if the trustManager is invalid
  831. */
  832. private void doConnect() throws IOException, NoSuchAlgorithmException, KeyManagementException {
  833. if (getURI() == null || getURI().getHost() == null) {
  834. throw new UnknownHostException("Unspecified host.");
  835. }
  836. resetState();
  837. callDebugInfo(DEBUG_SOCKET, "Connecting to " + getURI().getHost() + ":" + getURI().getPort());
  838. currentSocketState = SocketState.OPENING;
  839. socket = findSocket(getConnectURI(getURI()), getProxy());
  840. rawSocket = socket;
  841. if (getURI().getScheme().endsWith("s")) {
  842. callDebugInfo(DEBUG_SOCKET, "Server is SSL.");
  843. if (myTrustManager == null) {
  844. myTrustManager = trustAllCerts;
  845. }
  846. final SSLContext sc = SSLContext.getInstance("SSL");
  847. sc.init(myKeyManagers, myTrustManager, new java.security.SecureRandom());
  848. final SSLSocketFactory socketFactory = sc.getSocketFactory();
  849. socket = socketFactory.createSocket(socket, getURI().getHost(), getURI().getPort(), false);
  850. // Manually start a handshake so we get proper SSL errors here,
  851. // and so that we can control the connection timeout
  852. final int timeout = socket.getSoTimeout();
  853. socket.setSoTimeout(10000);
  854. ((SSLSocket) socket).startHandshake();
  855. socket.setSoTimeout(timeout);
  856. currentSocketState = SocketState.OPENING;
  857. }
  858. callDebugInfo(DEBUG_SOCKET, "\t-> Opening socket output stream PrintWriter");
  859. out.setOutputStream(socket.getOutputStream());
  860. out.setQueueEnabled(true);
  861. currentSocketState = SocketState.OPEN;
  862. callDebugInfo(DEBUG_SOCKET, "\t-> Opening socket input stream BufferedReader");
  863. in = new IRCReader(socket.getInputStream(), encoder);
  864. callDebugInfo(DEBUG_SOCKET, "\t-> Socket Opened");
  865. }
  866. /**
  867. * Send server connection strings (NICK/USER/PASS).
  868. */
  869. protected void sendConnectionStrings() {
  870. sendString("CAP LS");
  871. if (getURI().getUserInfo() != null && !getURI().getUserInfo().isEmpty()) {
  872. sendString("PASS " + getURI().getUserInfo());
  873. }
  874. sendString("NICK " + me.getNickname());
  875. thinkNickname = me.getNickname();
  876. String localhost;
  877. try {
  878. localhost = InetAddress.getLocalHost().getHostAddress();
  879. } catch (UnknownHostException uhe) {
  880. localhost = "*";
  881. }
  882. sendString("USER " + me.getUsername() + " " + localhost + " " + getURI().getHost() + " :" + me.getRealname());
  883. }
  884. /**
  885. * Handle an onConnect error.
  886. *
  887. * @param e Exception to handle
  888. * @param isUserError Is this a user error?
  889. */
  890. private void handleConnectException(final Exception e, final boolean isUserError) {
  891. callDebugInfo(DEBUG_SOCKET, "Error Connecting (" + e.getMessage() + "), Aborted");
  892. final ParserError ei = new ParserError(ParserError.ERROR_ERROR + (isUserError ? ParserError.ERROR_USER : 0), "Exception with server socket", getLastLine());
  893. ei.setException(e);
  894. callConnectError(ei);
  895. if (currentSocketState != SocketState.CLOSED) {
  896. currentSocketState = SocketState.CLOSED;
  897. callSocketClosed();
  898. }
  899. resetState();
  900. }
  901. /**
  902. * Begin execution.
  903. * Connect to server, and start parsing incoming lines
  904. */
  905. @Override
  906. public void run() {
  907. callDebugInfo(DEBUG_INFO, "Begin Thread Execution");
  908. if (hasBegan) {
  909. return;
  910. } else {
  911. hasBegan = true;
  912. }
  913. try {
  914. doConnect();
  915. } catch (UnknownHostException e) {
  916. handleConnectException(e, true);
  917. return;
  918. } catch (IOException e) {
  919. handleConnectException(e, true);
  920. return;
  921. } catch (NoSuchAlgorithmException e) {
  922. handleConnectException(e, false);
  923. return;
  924. } catch (KeyManagementException e) {
  925. handleConnectException(e, false);
  926. return;
  927. }
  928. callDebugInfo(DEBUG_SOCKET, "Socket Connected");
  929. sendConnectionStrings();
  930. while (true) {
  931. try {
  932. lastLine = in.readLine(); // Blocking :/
  933. if (lastLine == null) {
  934. if (currentSocketState != SocketState.CLOSED) {
  935. currentSocketState = SocketState.CLOSED;
  936. callSocketClosed();
  937. }
  938. resetState();
  939. break;
  940. } else if (currentSocketState != SocketState.CLOSING) {
  941. processLine(lastLine);
  942. }
  943. } catch (IOException e) {
  944. callDebugInfo(DEBUG_SOCKET, "Exception in main loop (" + e.getMessage() + "), Aborted");
  945. if (currentSocketState != SocketState.CLOSED) {
  946. currentSocketState = SocketState.CLOSED;
  947. callSocketClosed();
  948. }
  949. resetState();
  950. break;
  951. }
  952. }
  953. callDebugInfo(DEBUG_INFO, "End Thread Execution");
  954. }
  955. /** {@inheritDoc} */
  956. @Override
  957. public int getLocalPort() {
  958. if (currentSocketState == SocketState.OPENING || currentSocketState == SocketState.OPEN) {
  959. return socket.getLocalPort();
  960. } else {
  961. return 0;
  962. }
  963. }
  964. /** Close socket on destroy. */
  965. @Override
  966. protected void finalize() throws Throwable {
  967. try {
  968. // See note at disconnect() method for why we close rawSocket.
  969. if (rawSocket != null) {
  970. rawSocket.close();
  971. }
  972. } catch (IOException e) {
  973. callDebugInfo(DEBUG_SOCKET, "Could not close socket");
  974. }
  975. super.finalize();
  976. }
  977. /**
  978. * Get the trailing parameter for a line.
  979. * The parameter is everything after the first occurrence of " :" to the last token in the line after a space.
  980. *
  981. * @param line Line to get parameter for
  982. * @return Parameter of the line
  983. */
  984. public static String getParam(final String line) {
  985. String[] params = line.split(" :", 2);
  986. return params[params.length - 1];
  987. }
  988. /**
  989. * Tokenise a line.
  990. * splits by " " up to the first " :" everything after this is a single token
  991. *
  992. * @param line Line to tokenise
  993. * @return Array of tokens
  994. */
  995. public static String[] tokeniseLine(final String line) {
  996. if (line == null) {
  997. return new String[]{""};
  998. }
  999. final int lastarg = line.indexOf(" :");
  1000. String[] tokens;
  1001. if (lastarg > -1) {
  1002. final String[] temp = line.substring(0, lastarg).split(" ");
  1003. tokens = new String[temp.length + 1];
  1004. System.arraycopy(temp, 0, tokens, 0, temp.length);
  1005. tokens[temp.length] = line.substring(lastarg + 2);
  1006. } else {
  1007. tokens = line.split(" ");
  1008. }
  1009. if (tokens.length < 1) {
  1010. tokens = new String[]{""};
  1011. }
  1012. return tokens;
  1013. }
  1014. /** {@inheritDoc} */
  1015. @Override
  1016. public IRCClientInfo getClient(final String details) {
  1017. final String sWho = getStringConverter().toLowerCase(IRCClientInfo.parseHost(details));
  1018. if (clientList.containsKey(sWho)) {
  1019. return clientList.get(sWho);
  1020. } else {
  1021. return new IRCClientInfo(this, details).setFake(true);
  1022. }
  1023. }
  1024. public boolean isKnownClient(final String host) {
  1025. final String sWho = getStringConverter().toLowerCase(IRCClientInfo.parseHost(host));
  1026. return clientList.containsKey(sWho);
  1027. }
  1028. /** {@inheritDoc} */
  1029. @Override
  1030. public IRCChannelInfo getChannel(final String channel) {
  1031. synchronized (channelList) {
  1032. return channelList.get(getStringConverter().toLowerCase(channel));
  1033. }
  1034. }
  1035. /** {@inheritDoc} */
  1036. @Override
  1037. public void sendInvite(final String channel, final String user) {
  1038. sendRawMessage("INVITE " + user + " " + channel);
  1039. }
  1040. /** {@inheritDoc} */
  1041. @Override
  1042. public void sendRawMessage(final String message) {
  1043. doSendString(message, QueuePriority.NORMAL, false);
  1044. }
  1045. /** {@inheritDoc} */
  1046. @Override
  1047. public void sendRawMessage(final String message, final QueuePriority priority) {
  1048. doSendString(message, priority, false);
  1049. }
  1050. /**
  1051. * Send a line to the server and add proper line ending.
  1052. *
  1053. * @param line Line to send (\r\n termination is added automatically)
  1054. * @return True if line was sent, else false.
  1055. */
  1056. protected boolean sendString(final String line) {
  1057. return doSendString(line, QueuePriority.NORMAL, true);
  1058. }
  1059. /**
  1060. * Send a line to the server and add proper line ending.
  1061. * If a non-empty argument is given, it is appended as a trailing argument
  1062. * (i.e., separated by " :"); otherwise, the line is sent as-is.
  1063. *
  1064. * @param line Line to send
  1065. * @param argument Trailing argument for the command, if any
  1066. * @return True if line was sent, else false.
  1067. */
  1068. protected boolean sendString(final String line, final String argument) {
  1069. return sendString(argument.isEmpty() ? line : line + " :" + argument);
  1070. }
  1071. /**
  1072. * Send a line to the server and add proper line ending.
  1073. *
  1074. * @param line Line to send (\r\n termination is added automatically)
  1075. * @param priority Priority of this line.
  1076. * @return True if line was sent, else false.
  1077. */
  1078. protected boolean sendString(final String line, final QueuePriority priority) {
  1079. return doSendString(line, priority, true);
  1080. }
  1081. /**
  1082. * Send a line to the server and add proper line ending.
  1083. *
  1084. * @param line Line to send (\r\n termination is added automatically)
  1085. * @param priority Priority of this line.
  1086. * @param fromParser is this line from the parser? (used for callDataOut)
  1087. * @return True if line was sent, else false.
  1088. */
  1089. protected boolean doSendString(final String line, final QueuePriority priority, final boolean fromParser) {
  1090. if (out == null || getSocketState() != SocketState.OPEN) {
  1091. return false;
  1092. }
  1093. callDataOut(line, fromParser);
  1094. out.sendLine(line, priority);
  1095. final String[] newLine = tokeniseLine(line);
  1096. if (newLine[0].equalsIgnoreCase("away") && newLine.length > 1) {
  1097. myself.setAwayReason(newLine[newLine.length - 1]);
  1098. } else if (newLine[0].equalsIgnoreCase("mode") && newLine.length == 3) {
  1099. final IRCChannelInfo channel = getChannel(newLine[1]);
  1100. if (channel != null) {
  1101. // This makes sure we don't add the same item to the LMQ twice,
  1102. // even if its requested twice, as the ircd will only reply once
  1103. final Deque<Character> foundModes = new LinkedList<>();
  1104. final Queue<Character> listModeQueue = channel.getListModeQueue();
  1105. for (int i = 0; i < newLine[2].length(); ++i) {
  1106. final Character mode = newLine[2].charAt(i);
  1107. callDebugInfo(DEBUG_LMQ, "Intercepted mode request for " + channel + " for mode " + mode);
  1108. if (chanModesOther.containsKey(mode) && chanModesOther.get(mode) == MODE_LIST) {
  1109. if (foundModes.contains(mode)) {
  1110. callDebugInfo(DEBUG_LMQ, "Already added to LMQ");
  1111. } else {
  1112. listModeQueue.offer(mode);
  1113. foundModes.offer(mode);
  1114. callDebugInfo(DEBUG_LMQ, "Added to LMQ");
  1115. }
  1116. }
  1117. }
  1118. }
  1119. }
  1120. return true;
  1121. }
  1122. /** {@inheritDoc} */
  1123. @Override
  1124. public String getNetworkName() {
  1125. return networkName;
  1126. }
  1127. /** {@inheritDoc} */
  1128. @Override
  1129. public String getLastLine() {
  1130. return lastLine == null ? "" : lastLine.getLine();
  1131. }
  1132. /** {@inheritDoc} */
  1133. @Override
  1134. public List<String> getServerInformationLines() {
  1135. synchronized (serverInformationLines) {
  1136. return new LinkedList<>(serverInformationLines);
  1137. }
  1138. }
  1139. /**
  1140. * Process a line and call relevant methods for handling.
  1141. *
  1142. * @param line Line read from the IRC server
  1143. */
  1144. @SuppressWarnings("fallthrough")
  1145. protected void processLine(final ReadLine line) {
  1146. callDataIn(line.getLine());
  1147. String[] token = line.getTokens();
  1148. Date lineTS = new Date();
  1149. if (!token[0].isEmpty() && token[0].charAt(0) == '@') {
  1150. try {
  1151. final int tsEnd = token[0].indexOf('@', 1);
  1152. final long ts = Long.parseLong(token[0].substring(1, tsEnd));
  1153. token[0] = token[0].substring(tsEnd + 1);
  1154. lineTS = new Date(ts - tsdiff);
  1155. } catch (final NumberFormatException nfe) { /* Do nothing. */ }
  1156. }
  1157. int nParam;
  1158. setPingNeeded(false);
  1159. if (token.length < 2) {
  1160. return;
  1161. }
  1162. try {
  1163. final String sParam = token[1];
  1164. if (token[0].equalsIgnoreCase("PING") || token[1].equalsIgnoreCase("PING")) {
  1165. sendString("PONG :" + sParam, QueuePriority.HIGH);
  1166. } else if (token[0].equalsIgnoreCase("PONG") || token[1].equalsIgnoreCase("PONG")) {
  1167. if (!lastPingValue.isEmpty() && lastPingValue.equals(token[token.length - 1])) {
  1168. lastPingValue = "";
  1169. serverLag = System.currentTimeMillis() - pingTime;
  1170. callPingSuccess();
  1171. }
  1172. } else if (token[0].equalsIgnoreCase("ERROR")) {
  1173. final StringBuilder errorMessage = new StringBuilder();
  1174. for (int i = 1; i < token.length; ++i) {
  1175. errorMessage.append(token[i]);
  1176. }
  1177. callServerError(errorMessage.toString());
  1178. } else if (token[1].equalsIgnoreCase("TSIRC") && token.length > 3) {
  1179. if (token[2].equals("1")) {
  1180. try {
  1181. final long ts = Long.parseLong(token[3]);
  1182. tsdiff = ts - System.currentTimeMillis();
  1183. } catch (final NumberFormatException nfe) { /* Do nothing. */ }
  1184. }
  1185. } else {
  1186. if (got001) {
  1187. // Freenode sends a random notice in a stupid place, others might do aswell
  1188. // These shouldn't cause post005 to be fired, so handle them here.
  1189. if (token[0].equalsIgnoreCase("NOTICE") || (token.length > 2 && token[2].equalsIgnoreCase("NOTICE"))) {
  1190. try {
  1191. myProcessingManager.process(lineTS, "Notice Auth", token);
  1192. } catch (ProcessorNotFoundException e) {
  1193. // ???
  1194. }
  1195. return;
  1196. }
  1197. if (!post005) {
  1198. try {
  1199. nParam = Integer.parseInt(token[1]);
  1200. } catch (NumberFormatException e) {
  1201. nParam = -1;
  1202. }
  1203. if (nParam < 0 || nParam > 5) {
  1204. callPost005();
  1205. } else {
  1206. // Store 001 - 005 for informational purposes.
  1207. synchronized (serverInformationLines) {
  1208. serverInformationLines.add(line.getLine());
  1209. }
  1210. }
  1211. }
  1212. // After 001 we potentially care about everything!
  1213. try {
  1214. myProcessingManager.process(lineTS, sParam, token);
  1215. } catch (ProcessorNotFoundException e) {
  1216. // ???
  1217. }
  1218. } else {
  1219. // Before 001 we don't care about much.
  1220. try {
  1221. nParam = Integer.parseInt(token[1]);
  1222. } catch (NumberFormatException e) {
  1223. nParam = -1;
  1224. }
  1225. switch (nParam) {
  1226. case 1: // 001 - Welcome to IRC
  1227. synchronized (serverInformationLines) {
  1228. serverInformationLines.add(line.getLine());
  1229. }
  1230. // Fallthrough
  1231. case 464: // Password Required
  1232. case 433: // Nick In Use
  1233. try {
  1234. myProcessingManager.process(sParam, token);
  1235. } catch (ProcessorNotFoundException e) {
  1236. }
  1237. break;
  1238. default: // Unknown - Send to Notice Auth
  1239. // Some networks send a CTCP during the auth process, handle it
  1240. if (token.length > 3 && !token[3].isEmpty() && token[3].charAt(0) == (char) 1 && token[3].charAt(token[3].length() - 1) == (char) 1) {
  1241. try {
  1242. myProcessingManager.process(lineTS, sParam, token);
  1243. } catch (ProcessorNotFoundException e) {
  1244. }
  1245. break;
  1246. }
  1247. // Some networks may send a NICK message if you nick change before 001
  1248. // Eat it up so that it isn't treated as a notice auth.
  1249. if (token[1].equalsIgnoreCase("NICK")) {
  1250. break;
  1251. }
  1252. // CAP also happens here, so try that.
  1253. if (token[1].equalsIgnoreCase("CAP")) {
  1254. myProcessingManager.process(lineTS, sParam, token);
  1255. }
  1256. // Otherwise, send to Notice Auth
  1257. try {
  1258. myProcessingManager.process(lineTS, "Notice Auth", token);
  1259. } catch (ProcessorNotFoundException e) {
  1260. }
  1261. break;
  1262. }
  1263. }
  1264. }
  1265. } catch (Exception e) {
  1266. final ParserError ei = new ParserError(ParserError.ERROR_FATAL, "Fatal Exception in Parser.", getLastLine());
  1267. ei.setException(e);
  1268. callErrorInfo(ei);
  1269. }
  1270. }
  1271. /** The IRCStringConverter for this parser */
  1272. private IRCStringConverter stringConverter = null;
  1273. /** {@inheritDoc} */
  1274. @Override
  1275. public IRCStringConverter getStringConverter() {
  1276. if (stringConverter == null) {
  1277. stringConverter = new IRCStringConverter(IRCEncoding.RFC1459);
  1278. }
  1279. return stringConverter;
  1280. }
  1281. /**
  1282. * Sets the encoding that this parser's string converter should use.
  1283. *
  1284. * @param encoding The encoding to use
  1285. */
  1286. protected void setEncoding(final IRCEncoding encoding) {
  1287. stringConverter = new IRCStringConverter(encoding);
  1288. }
  1289. /**
  1290. * Check the state of the requested capability.
  1291. *
  1292. * @param capability The capability to check the state of.
  1293. * @return State of the requested capability.
  1294. */
  1295. public CapabilityState getCapabilityState(final String capability) {
  1296. synchronized (capabilities) {
  1297. if (capabilities.containsKey(capability.toLowerCase())) {
  1298. return capabilities.get(capability.toLowerCase());
  1299. } else {
  1300. return CapabilityState.INVALID;
  1301. }
  1302. }
  1303. }
  1304. /**
  1305. * Set the state of the requested capability.
  1306. *
  1307. * @param capability Requested capability
  1308. * @param state State to set for capability
  1309. */
  1310. public void setCapabilityState(final String capability, final CapabilityState state) {
  1311. synchronized (capabilities) {
  1312. if (capabilities.containsKey(capability.toLowerCase())) {
  1313. capabilities.put(capability.toLowerCase(), state);
  1314. }
  1315. }
  1316. }
  1317. /**
  1318. * Add the given capability as a supported capability by the server.
  1319. *
  1320. * @param capability Requested capability
  1321. */
  1322. public void addCapability(final String capability) {
  1323. synchronized (capabilities) {
  1324. capabilities.put(capability.toLowerCase(), CapabilityState.DISABLED);
  1325. }
  1326. }
  1327. /**
  1328. * Get the server capabilities and their current state.
  1329. *
  1330. * @return Server capabilities and their current state.
  1331. */
  1332. public Map<String, CapabilityState> getCapabilities() {
  1333. synchronized (capabilities) {
  1334. return new HashMap<>(capabilities);
  1335. }
  1336. }
  1337. /**
  1338. * Process CHANMODES from 005.
  1339. */
  1340. public void parseChanModes() {
  1341. final StringBuilder sDefaultModes = new StringBuilder("b,k,l,");
  1342. String[] bits;
  1343. String modeStr;
  1344. if (h005Info.containsKey("USERCHANMODES")) {
  1345. if (getServerType() == ServerType.DANCER) {
  1346. sDefaultModes.insert(0, "dqeI");
  1347. } else if (getServerType() == ServerType.AUSTIRC) {
  1348. sDefaultModes.insert(0, "e");
  1349. }
  1350. modeStr = h005Info.get("USERCHANMODES");
  1351. char mode;
  1352. for (int i = 0; i < modeStr.length(); ++i) {
  1353. mode = modeStr.charAt(i);
  1354. if (!prefixModes.containsKey(mode) && sDefaultModes.indexOf(Character.toString(mode)) < 0) {
  1355. sDefaultModes.append(mode);
  1356. }
  1357. }
  1358. } else {
  1359. sDefaultModes.append("imnpstrc");
  1360. }
  1361. if (h005Info.containsKey("CHANMODES")) {
  1362. modeStr = h005Info.get("CHANMODES");
  1363. } else {
  1364. modeStr = sDefaultModes.toString();
  1365. h005Info.put("CHANMODES", modeStr);
  1366. }
  1367. bits = modeStr.split(",", 5);
  1368. if (bits.length < 4) {
  1369. modeStr = sDefaultModes.toString();
  1370. callErrorInfo(new ParserError(ParserError.ERROR_ERROR, "CHANMODES String not valid. Using default string of \"" + modeStr + "\"", getLastLine()));
  1371. h005Info.put("CHANMODES", modeStr);
  1372. bits = modeStr.split(",", 5);
  1373. }
  1374. // resetState
  1375. chanModesOther.clear();
  1376. chanModesBool.clear();
  1377. nextKeyCMBool = 1;
  1378. // List modes.
  1379. for (int i = 0; i < bits[0].length(); ++i) {
  1380. final Character cMode = bits[0].charAt(i);
  1381. callDebugInfo(DEBUG_INFO, "Found List Mode: %c", cMode);
  1382. if (!chanModesOther.containsKey(cMode)) {
  1383. chanModesOther.put(cMode, MODE_LIST);
  1384. }
  1385. }
  1386. // Param for Set and Unset.
  1387. final Byte nBoth = MODE_SET + MODE_UNSET;
  1388. for (int i = 0; i < bits[1].length(); ++i) {
  1389. final Character cMode = bits[1].charAt(i);
  1390. callDebugInfo(DEBUG_INFO, "Found Set/Unset Mode: %c", cMode);
  1391. if (!chanModesOther.containsKey(cMode)) {
  1392. chanModesOther.put(cMode, nBoth);
  1393. }
  1394. }
  1395. // Param just for Set
  1396. for (int i = 0; i < bits[2].length(); ++i) {
  1397. final Character cMode = bits[2].charAt(i);
  1398. callDebugInfo(DEBUG_INFO, "Found Set Only Mode: %c", cMode);
  1399. if (!chanModesOther.containsKey(cMode)) {
  1400. chanModesOther.put(cMode, MODE_SET);
  1401. }
  1402. }
  1403. // Boolean Mode
  1404. for (int i = 0; i < bits[3].length(); ++i) {
  1405. final Character cMode = bits[3].charAt(i);
  1406. callDebugInfo(DEBUG_INFO, "Found Boolean Mode: %c [%d]", cMode, nextKeyCMBool);
  1407. if (!chanModesBool.containsKey(cMode)) {
  1408. chanModesBool.put(cMode, nextKeyCMBool);
  1409. nextKeyCMBool *= 2;
  1410. }
  1411. }
  1412. }
  1413. /** {@inheritDoc} */
  1414. @Override
  1415. public String getChannelUserModes() {
  1416. if (h005Info.containsKey("PREFIXSTRING")) {
  1417. return h005Info.get("PREFIXSTRING");
  1418. } else {
  1419. return "";
  1420. }
  1421. }
  1422. /** {@inheritDoc} */
  1423. @Override
  1424. public String getBooleanChannelModes() {
  1425. final char[] modes = new char[chanModesBool.size()];
  1426. int i = 0;
  1427. for (char mode : chanModesBool.keySet()) {
  1428. modes[i] = mode;
  1429. i++;
  1430. }
  1431. // Alphabetically sort the array
  1432. Arrays.sort(modes);
  1433. return new String(modes);
  1434. }
  1435. /** {@inheritDoc} */
  1436. @Override
  1437. public String getListChannelModes() {
  1438. return getOtherModeString(MODE_LIST);
  1439. }
  1440. /** {@inheritDoc} */
  1441. @Override
  1442. public String getParameterChannelModes() {
  1443. return getOtherModeString(MODE_SET);
  1444. }
  1445. /** {@inheritDoc} */
  1446. @Override
  1447. public String getDoubleParameterChannelModes() {
  1448. return getOtherModeString((byte) (MODE_SET + MODE_UNSET));
  1449. }
  1450. /** {@inheritDoc} */
  1451. @Override
  1452. public String getChannelPrefixes() {
  1453. return chanPrefix;
  1454. }
  1455. /**
  1456. * Get modes from hChanModesOther that have a specific value.
  1457. * Modes are returned in alphabetical order
  1458. *
  1459. * @param value Value mode must have to be included
  1460. * @return All the currently known Set-Unset modes
  1461. */
  1462. protected String getOtherModeString(final byte value) {
  1463. final char[] modes = new char[chanModesOther.size()];
  1464. Byte nTemp;
  1465. int i = 0;
  1466. for (char cTemp : chanModesOther.keySet()) {
  1467. nTemp = chanModesOther.get(cTemp);
  1468. if (nTemp == value) {
  1469. modes[i] = cTemp;
  1470. i++;
  1471. }
  1472. }
  1473. // Alphabetically sort the array
  1474. Arrays.sort(modes);
  1475. return new String(modes).trim();
  1476. }
  1477. /** {@inheritDoc} */
  1478. @Override
  1479. public String getUserModes() {
  1480. if (h005Info.containsKey("USERMODES")) {
  1481. return h005Info.get("USERMODES");
  1482. } else {
  1483. return "";
  1484. }
  1485. }
  1486. /**
  1487. * Process USERMODES from 004.
  1488. */
  1489. protected void parseUserModes() {
  1490. final String sDefaultModes = "nwdoi";
  1491. String modeStr;
  1492. if (h005Info.containsKey("USERMODES")) {
  1493. modeStr = h005Info.get("USERMODES");
  1494. } else {
  1495. modeStr = sDefaultModes;
  1496. h005Info.put("USERMODES", sDefaultModes);
  1497. }
  1498. // resetState
  1499. userModes.clear();
  1500. nNextKeyUser = 1;
  1501. // Boolean Mode
  1502. for (int i = 0; i < modeStr.length(); ++i) {
  1503. final Character cMode = modeStr.charAt(i);
  1504. callDebugInfo(DEBUG_INFO, "Found User Mode: %c [%d]", cMode, nNextKeyUser);
  1505. if (!userModes.containsKey(cMode)) {
  1506. userModes.put(cMode, nNextKeyUser);
  1507. nNextKeyUser *= 2;
  1508. }
  1509. }
  1510. }
  1511. /**
  1512. * Resets the channel prefix property to the default, RFC specified value.
  1513. */
  1514. protected void resetChanPrefix() {
  1515. chanPrefix = DEFAULT_CHAN_PREFIX;
  1516. }
  1517. /**
  1518. * Sets the set of possible channel prefixes to those in the given value.
  1519. *
  1520. * @param value The new set of channel prefixes.
  1521. */
  1522. protected void setChanPrefix(final String value) {
  1523. chanPrefix = value;
  1524. }
  1525. /**
  1526. * Process PREFIX from 005.
  1527. */
  1528. public void parsePrefixModes() {
  1529. final String sDefaultModes = "(ohv)@%+";
  1530. String[] bits;
  1531. String modeStr;
  1532. if (h005Info.containsKey("PREFIX")) {
  1533. modeStr = h005Info.get("PREFIX");
  1534. } else {
  1535. modeStr = sDefaultModes;
  1536. }
  1537. if (modeStr.substring(0, 1).equals("(")) {
  1538. modeStr = modeStr.substring(1);
  1539. } else {
  1540. modeStr = sDefaultModes.substring(1);
  1541. h005Info.put("PREFIX", sDefaultModes);
  1542. }
  1543. bits = modeStr.split("\\)", 2);
  1544. if (bits.length != 2 || bits[0].length() != bits[1].length()) {
  1545. modeStr = sDefaultModes;
  1546. callErrorInfo(new ParserError(ParserError.ERROR_ERROR, "PREFIX String not valid. Using default string of \"" + modeStr + "\"", getLastLine()));
  1547. h005Info.put("PREFIX", modeStr);
  1548. modeStr = modeStr.substring(1);
  1549. bits = modeStr.split("\\)", 2);
  1550. }
  1551. // resetState
  1552. prefixModes.clear();
  1553. prefixMap.clear();
  1554. nextKeyPrefix = 1;
  1555. for (int i = bits[0].length() - 1; i > -1; --i) {
  1556. final Character cMode = bits[0].charAt(i);
  1557. final Character cPrefix = bits[1].charAt(i);
  1558. callDebugInfo(DEBUG_INFO, "Found Prefix Mode: %c => %c [%d]", cMode, cPrefix, nextKeyPrefix);
  1559. if (!prefixModes.containsKey(cMode)) {
  1560. prefixModes.put(cMode, nextKeyPrefix);
  1561. prefixMap.put(cMode, cPrefix);
  1562. prefixMap.put(cPrefix, cMode);
  1563. nextKeyPrefix *= 2;
  1564. }
  1565. }
  1566. h005Info.put("PREFIXSTRING", bits[0]);
  1567. }
  1568. /** {@inheritDoc} */
  1569. @Override
  1570. public void joinChannels(final ChannelJoinRequest... channels) {
  1571. // We store a map from key->channels to allow intelligent joining of
  1572. // channels using as few JOIN commands as needed.
  1573. final Map<String, StringBuffer> joinMap = new HashMap<>();
  1574. for (ChannelJoinRequest channel : channels) {
  1575. // Make sure we have a list to put stuff in.
  1576. StringBuffer list = joinMap.get(channel.getPassword());
  1577. if (list == null) {
  1578. list = new StringBuffer();
  1579. joinMap.put(channel.getPassword(), list);
  1580. }
  1581. // Add the channel to the list. If the name is invalid and
  1582. // autoprefix is off we will just skip this channel.
  1583. if (!channel.getName().isEmpty()) {
  1584. if (list.length() > 0) {
  1585. list.append(',');
  1586. }
  1587. if (!isValidChannelName(channel.getName())) {
  1588. if (chanPrefix.isEmpty()) {
  1589. // TODO: This is wrong - empty chan prefix means the
  1590. // IRCd supports no channels.
  1591. list.append('#');
  1592. } else {
  1593. list.append(chanPrefix.charAt(0));
  1594. }
  1595. }
  1596. list.append(channel.getName());
  1597. }
  1598. }
  1599. for (Map.Entry<String, StringBuffer> entrySet : joinMap.entrySet()) {
  1600. final String thisKey = entrySet.getKey();
  1601. final String channelString = entrySet.getValue().toString();
  1602. if (!channelString.isEmpty()) {
  1603. if (thisKey == null || thisKey.isEmpty()) {
  1604. sendString("JOIN " + channelString);
  1605. } else {
  1606. sendString("JOIN " + channelString + " " + thisKey);
  1607. }
  1608. }
  1609. }
  1610. }
  1611. /**
  1612. * Leave a Channel.
  1613. *
  1614. * @param channel Name of channel to part
  1615. * @param reason Reason for leaving (Nothing sent if sReason is "")
  1616. */
  1617. public void partChannel(final String channel, final String reason) {
  1618. if (getChannel(channel) == null) {
  1619. return;
  1620. }
  1621. sendString("PART " + channel, reason);
  1622. }
  1623. /**
  1624. * Set Nickname.
  1625. *
  1626. * @param nickname New nickname wanted.
  1627. */
  1628. public void setNickname(final String nickname) {
  1629. if (getSocketState() == SocketState.OPEN) {
  1630. if (!myself.isFake() && myself.getRealNickname().equals(nickname)) {
  1631. return;
  1632. }
  1633. sendString("NICK " + nickname);
  1634. } else {
  1635. me.setNickname(nickname);
  1636. }
  1637. thinkNickname = nickname;
  1638. }
  1639. /** {@inheritDoc} */
  1640. @Override
  1641. public int getMaxLength(final String type, final String target) {
  1642. // If my host is "nick!user@host" and we are sending "#Channel"
  1643. // a "PRIVMSG" this will find the length of ":nick!user@host PRIVMSG #channel :"
  1644. // and subtract it from the MAX_LINELENGTH. This should be sufficient in most cases.
  1645. // Lint = the 2 ":" at the start and end and the 3 separating " "s
  1646. int length = 0;
  1647. if (type != null) {
  1648. length += type.length();
  1649. }
  1650. if (target != null) {
  1651. length += target.length();
  1652. }
  1653. return getMaxLength(length);
  1654. }
  1655. /**
  1656. * Get the max length a message can be.
  1657. *
  1658. * @param length Length of stuff. (Ie "PRIVMSG"+"#Channel")
  1659. * @return Max Length message should be.
  1660. */
  1661. public int getMaxLength(final int length) {
  1662. final int lineLint = 5;
  1663. if (myself.isFake()) {
  1664. callErrorInfo(new ParserError(ParserError.ERROR_ERROR + ParserError.ERROR_USER, "getMaxLength() called, but I don't know who I am?", getLastLine()));
  1665. return MAX_LINELENGTH - length - lineLint;
  1666. } else {
  1667. return MAX_LINELENGTH - myself.toString().length() - length - lineLint;
  1668. }
  1669. }
  1670. /** {@inheritDoc} */
  1671. @Override
  1672. public int getMaxListModes(final char mode) {
  1673. // MAXLIST=bdeI:50
  1674. // MAXLIST=b:60,e:60,I:60
  1675. // MAXBANS=30
  1676. int result = -2;
  1677. callDebugInfo(DEBUG_INFO, "Looking for maxlistmodes for: " + mode);
  1678. // Try in MAXLIST
  1679. if (h005Info.get("MAXLIST") != null) {
  1680. if (h005Info.get("MAXBANS") == null) {
  1681. result = 0;
  1682. }
  1683. final String maxlist = h005Info.get("MAXLIST");
  1684. callDebugInfo(DEBUG_INFO, "Found maxlist (" + maxlist + ")");
  1685. final String[] bits = maxlist.split(",");
  1686. for (String bit : bits) {
  1687. final String[] parts = bit.split(":", 2);
  1688. callDebugInfo(DEBUG_INFO, "Bit: " + bit + " | parts.length = " + parts.length + " (" + parts[0] + " -> " + parts[0].indexOf(mode) + ")");
  1689. if (parts.length == 2 && parts[0].indexOf(mode) > -1) {
  1690. callDebugInfo(DEBUG_INFO, "parts[0] = '" + parts[0] + "' | parts[1] = '" + parts[1] + "'");
  1691. try {
  1692. result = Integer.parseInt(parts[1]);
  1693. break;
  1694. } catch (NumberFormatException nfe) {
  1695. result = -1;
  1696. }
  1697. }
  1698. }
  1699. }
  1700. // If not in max list, try MAXBANS
  1701. if (result == -2 && h005Info.get("MAXBANS") != null) {
  1702. callDebugInfo(DEBUG_INFO, "Trying max bans");
  1703. try {
  1704. result = Integer.parseInt(h005Info.get("MAXBANS"));
  1705. } catch (NumberFormatException nfe) {
  1706. result = -1;
  1707. }
  1708. } else if (result == -2 && getServerType() == ServerType.WEIRCD) {
  1709. result = 50;
  1710. } else if (result == -2 && getServerType() == ServerType.OTHERNET) {
  1711. result = 30;
  1712. } else if (result == -2) {
  1713. result = -1;
  1714. callDebugInfo(DEBUG_INFO, "Failed");
  1715. callErrorInfo(new ParserError(ParserError.ERROR_ERROR + ParserError.ERROR_USER, "Unable to discover max list modes.", getLastLine()));
  1716. }
  1717. callDebugInfo(DEBUG_INFO, "Result: " + result);
  1718. return result;
  1719. }
  1720. /** {@inheritDoc} */
  1721. @Override
  1722. public void sendMessage(final String target, final String message) {
  1723. if (target == null || message == null) {
  1724. return;
  1725. }
  1726. if (target.isEmpty()) {
  1727. return;
  1728. }
  1729. sendString("PRIVMSG " + target, message);
  1730. }
  1731. /** {@inheritDoc} */
  1732. @Override
  1733. public void sendNotice(final String target, final String message) {
  1734. if (target == null || message == null) {
  1735. return;
  1736. }
  1737. if (target.isEmpty()) {
  1738. return;
  1739. }
  1740. sendString("NOTICE " + target, message);
  1741. }
  1742. /** {@inheritDoc} */
  1743. @Override
  1744. public void sendAction(final String target, final String message) {
  1745. sendCTCP(target, "ACTION", message);
  1746. }
  1747. /** {@inheritDoc} */
  1748. @Override
  1749. public void sendCTCP(final String target, final String type, final String message) {
  1750. if (target == null || message == null) {
  1751. return;
  1752. }
  1753. if (target.isEmpty() || type.isEmpty()) {
  1754. return;
  1755. }
  1756. final char char1 = (char) 1;
  1757. sendString("PRIVMSG " + target, char1 + type.toUpperCase() + " " + message + char1);
  1758. }
  1759. /** {@inheritDoc} */
  1760. @Override
  1761. public void sendCTCPReply(final String target, final String type, final String message) {
  1762. if (target == null || message == null) {
  1763. return;
  1764. }
  1765. if (target.isEmpty() || type.isEmpty()) {
  1766. return;
  1767. }
  1768. final char char1 = (char) 1;
  1769. sendString("NOTICE " + target, char1 + type.toUpperCase() + " " + message + char1);
  1770. }
  1771. /** {@inheritDoc} */
  1772. @Override
  1773. public void requestGroupList(final String searchTerms) {
  1774. sendString("LIST", searchTerms);
  1775. }
  1776. /** {@inheritDoc} */
  1777. @Override
  1778. public void quit(final String reason) {
  1779. sendString("QUIT", reason);
  1780. }
  1781. /** {@inheritDoc} */
  1782. @Override
  1783. public void disconnect(final String message) {
  1784. super.disconnect(message);
  1785. if (currentSocketState == SocketState.OPENING || currentSocketState == SocketState.OPEN) {
  1786. currentSocketState = SocketState.CLOSING;
  1787. if (got001) {
  1788. quit(message);
  1789. }
  1790. }
  1791. try {
  1792. // SSLSockets try to close nicely and read data from the socket,
  1793. // which seems to hang indefinitely in some circumstances. We don't
  1794. // like indefinite hangs, so just close the underlying socket
  1795. // direct.
  1796. if (rawSocket != null) {
  1797. rawSocket.close();
  1798. }
  1799. } catch (IOException e) {
  1800. /* Do Nothing */
  1801. } finally {
  1802. if (currentSocketState != SocketState.CLOSED) {
  1803. currentSocketState = SocketState.CLOSED;
  1804. callSocketClosed();
  1805. }
  1806. resetState();
  1807. }
  1808. }
  1809. /** {@inheritDoc}
  1810. *
  1811. * - Before channel prefixes are known (005/noMOTD/MOTDEnd), this checks
  1812. * that the first character is either #, &amp;, ! or +
  1813. * - Assumes that any channel that is already known is valid, even if
  1814. * 005 disagrees.
  1815. */
  1816. @Override
  1817. public boolean isValidChannelName(final String name) {
  1818. // Check sChannelName is not empty or null
  1819. if (name == null || name.isEmpty()) {
  1820. return false;
  1821. }
  1822. // Check its not ourself (PM recieved before 005)
  1823. if (getStringConverter().equalsIgnoreCase(getMyNickname(), name)) {
  1824. return false;
  1825. }
  1826. // Check if we are already on this channel
  1827. if (getChannel(name) != null) {
  1828. return true;
  1829. }
  1830. // Otherwise return true if:
  1831. // Channel equals "0"
  1832. // first character of the channel name is a valid channel prefix.
  1833. return chanPrefix.indexOf(name.charAt(0)) >= 0 || "0".equals(name);
  1834. }
  1835. /** {@inheritDoc} */
  1836. @Override
  1837. public boolean isUserSettable(final char mode) {
  1838. String validmodes;
  1839. if (h005Info.containsKey("USERCHANMODES")) {
  1840. validmodes = h005Info.get("USERCHANMODES");
  1841. } else {
  1842. validmodes = "bklimnpstrc";
  1843. }
  1844. return validmodes.matches(".*" + mode + ".*");
  1845. }
  1846. /**
  1847. * Get the 005 info.
  1848. *
  1849. * @return 005Info hashtable.
  1850. */
  1851. public Map<String, String> get005() {
  1852. return Collections.unmodifiableMap(h005Info);
  1853. }
  1854. /**
  1855. * Get the ServerType for this IRCD.
  1856. *
  1857. * @return The ServerType for this IRCD.
  1858. */
  1859. public ServerType getServerType() {
  1860. return ServerType.findServerType(h005Info.get("004IRCD"), networkName, h005Info.get("003IRCD"), h005Info.get("002IRCD"));
  1861. }
  1862. /** {@inheritDoc} */
  1863. @Override
  1864. public String getServerSoftware() {
  1865. final String version = h005Info.get("004IRCD");
  1866. return version == null ? "" : version;
  1867. }
  1868. /** {@inheritDoc} */
  1869. @Override
  1870. public String getServerSoftwareType() {
  1871. return getServerType().getType();
  1872. }
  1873. /**
  1874. * Get the value of checkServerPing.
  1875. *
  1876. * @return value of checkServerPing.
  1877. * @see setCheckServerPing
  1878. */
  1879. public boolean getCheckServerPing() {
  1880. return checkServerPing;
  1881. }
  1882. /**
  1883. * Set the value of checkServerPing.
  1884. *
  1885. * @param newValue New value to use.
  1886. * @see setCheckServerPing
  1887. */
  1888. public void setCheckServerPing(final boolean newValue) {
  1889. checkServerPing = newValue;
  1890. if (checkServerPing) {
  1891. startPingTimer();
  1892. } else {
  1893. stopPingTimer();
  1894. }
  1895. }
  1896. /** {@inheritDoc} */
  1897. @Override
  1898. public void setEncoder(final Encoder encoder) {
  1899. this.encoder = encoder;
  1900. }
  1901. /** {@inheritDoc} */
  1902. @Override
  1903. public void setPingTimerInterval(final long newValue) {
  1904. super.setPingTimerInterval(newValue);
  1905. startPingTimer();
  1906. }
  1907. /**
  1908. * Start the pingTimer.
  1909. */
  1910. protected void startPingTimer() {
  1911. pingTimerSem.acquireUninterruptibly();
  1912. try {
  1913. setPingNeeded(false);
  1914. if (pingTimer != null) {
  1915. pingTimer.cancel();
  1916. }
  1917. pingTimer = new Timer("IRCParser pingTimer");
  1918. pingTimer.schedule(new PingTimer(this, pingTimer), 0, getPingTimerInterval());
  1919. pingCountDown = 1;
  1920. } finally {
  1921. pingTimerSem.release();
  1922. }
  1923. }
  1924. /**
  1925. * Stop the pingTimer.
  1926. */
  1927. protected void stopPingTimer() {
  1928. pingTimerSem.acquireUninterruptibly();
  1929. if (pingTimer != null) {
  1930. pingTimer.cancel();
  1931. pingTimer = null;
  1932. }
  1933. pingTimerSem.release();
  1934. }
  1935. /**
  1936. * This is called when the ping Timer has been executed.
  1937. * As the timer is restarted on every incomming message, this will only be
  1938. * called when there has been no incomming line for 10 seconds.
  1939. *
  1940. * @param timer The timer that called this.
  1941. */
  1942. protected void pingTimerTask(final Timer timer) {
  1943. // If user no longer wants server ping to be checked, or the socket is
  1944. // closed then cancel the time and do nothing else.
  1945. if (!getCheckServerPing() || getSocketState() != SocketState.OPEN) {
  1946. pingTimerSem.acquireUninterruptibly();
  1947. if (pingTimer != null && pingTimer.equals(timer)) {
  1948. pingTimer.cancel();
  1949. }
  1950. pingTimerSem.release();
  1951. return;
  1952. }
  1953. if (getPingNeeded()) {
  1954. if (!callPingFailed()) {
  1955. pingTimerSem.acquireUninterruptibly();
  1956. if (pingTimer != null && pingTimer.equals(timer)) {
  1957. pingTimer.cancel();
  1958. }
  1959. pingTimerSem.release();
  1960. disconnect("Server not responding.");
  1961. }
  1962. } else {
  1963. --pingCountDown;
  1964. if (pingCountDown < 1) {
  1965. pingTime = System.currentTimeMillis();
  1966. setPingNeeded(true);
  1967. pingCountDown = getPingTimerFraction();
  1968. lastPingValue = String.valueOf(System.currentTimeMillis());
  1969. if (sendString("PING " + lastPingValue, QueuePriority.HIGH)) {
  1970. callPingSent();
  1971. }
  1972. }
  1973. }
  1974. }
  1975. /** {@inheritDoc} */
  1976. @Override
  1977. public long getServerLatency() {
  1978. return serverLag;
  1979. }
  1980. /**
  1981. * Updates the name of the server that this parser is connected to.
  1982. *
  1983. * @param serverName The discovered server name
  1984. */
  1985. protected void updateServerName(final String serverName) {
  1986. setServerName(serverName);
  1987. }
  1988. /**
  1989. * Get the current server lag.
  1990. *
  1991. * @param actualTime if True the value returned will be the actual time the ping was sent
  1992. * else it will be the amount of time sinse the last ping was sent.
  1993. * @return Time last ping was sent
  1994. */
  1995. public long getPingTime(final boolean actualTime) {
  1996. if (actualTime) {
  1997. return pingTime;
  1998. } else {
  1999. return System.currentTimeMillis() - pingTime;
  2000. }
  2001. }
  2002. /** {@inheritDoc} */
  2003. @Override
  2004. public long getPingTime() {
  2005. return getPingTime(false);
  2006. }
  2007. /**
  2008. * Set if a ping is needed or not.
  2009. *
  2010. * @param newStatus new value to set pingNeeded to.
  2011. */
  2012. private void setPingNeeded(final boolean newStatus) {
  2013. pingNeeded.set(newStatus);
  2014. }
  2015. /**
  2016. * Get if a ping is needed or not.
  2017. *
  2018. * @return value of pingNeeded.
  2019. */
  2020. private boolean getPingNeeded() {
  2021. return pingNeeded.get();
  2022. }
  2023. /** {@inheritDoc} */
  2024. @Override
  2025. public IRCClientInfo getLocalClient() {
  2026. return myself;
  2027. }
  2028. /**
  2029. * Get the current nickname.
  2030. * After 001 this returns the exact same as getLocalClient().getRealNickname();
  2031. * Before 001 it returns the nickname that the parser Thinks it has.
  2032. *
  2033. * @return Current nickname.
  2034. */
  2035. public String getMyNickname() {
  2036. if (myself.isFake()) {
  2037. return thinkNickname;
  2038. } else {
  2039. return myself.getRealNickname();
  2040. }
  2041. }
  2042. /**
  2043. * Retrieves the local user information that this parser was configured
  2044. * with.
  2045. *
  2046. * @return This parser's local user configuration
  2047. */
  2048. public MyInfo getMyInfo() {
  2049. return me;
  2050. }
  2051. /**
  2052. * Get the current username (Specified in MyInfo on construction).
  2053. * Get the username given in MyInfo
  2054. *
  2055. * @return My username.
  2056. */
  2057. public String getMyUsername() {
  2058. return me.getUsername();
  2059. }
  2060. /**
  2061. * Add a client to the ClientList.
  2062. *
  2063. * @param client Client to add
  2064. */
  2065. public void addClient(final IRCClientInfo client) {
  2066. clientList.put(getStringConverter().toLowerCase(client.getRealNickname()), client);
  2067. }
  2068. /**
  2069. * Remove a client from the ClientList.
  2070. * This WILL NOT allow cMyself to be removed from the list.
  2071. *
  2072. * @param client Client to remove
  2073. */
  2074. public void removeClient(final IRCClientInfo client) {
  2075. if (client != myself) {
  2076. forceRemoveClient(client);
  2077. }
  2078. }
  2079. /**
  2080. * Remove a client from the ClientList.
  2081. * This WILL allow cMyself to be removed from the list
  2082. *
  2083. * @param client Client to remove
  2084. */
  2085. protected void forceRemoveClient(final IRCClientInfo client) {
  2086. clientList.remove(getStringConverter().toLowerCase(client.getRealNickname()));
  2087. }
  2088. /**
  2089. * Get the number of known clients.
  2090. *
  2091. * @return Count of known clients
  2092. */
  2093. public int knownClients() {
  2094. return clientList.size();
  2095. }
  2096. /**
  2097. * Get the known clients as a collection.
  2098. *
  2099. * @return Known clients as a collection
  2100. */
  2101. public Collection<IRCClientInfo> getClients() {
  2102. return clientList.values();
  2103. }
  2104. /**
  2105. * Clear the client list.
  2106. */
  2107. public void clearClients() {
  2108. clientList.clear();
  2109. addClient(getLocalClient());
  2110. }
  2111. /**
  2112. * Add a channel to the ChannelList.
  2113. *
  2114. * @param channel Channel to add
  2115. */
  2116. public void addChannel(final IRCChannelInfo channel) {
  2117. synchronized (channelList) {
  2118. channelList.put(getStringConverter().toLowerCase(channel.getName()), channel);
  2119. }
  2120. }
  2121. /**
  2122. * Remove a channel from the ChannelList.
  2123. *
  2124. * @param channel Channel to remove
  2125. */
  2126. public void removeChannel(final IRCChannelInfo channel) {
  2127. synchronized (channelList) {
  2128. channelList.remove(getStringConverter().toLowerCase(channel.getName()));
  2129. }
  2130. }
  2131. /**
  2132. * Get the number of known channel.
  2133. *
  2134. * @return Count of known channel
  2135. */
  2136. public int knownChannels() {
  2137. synchronized (channelList) {
  2138. return channelList.size();
  2139. }
  2140. }
  2141. /** {@inheritDoc} */
  2142. @Override
  2143. public Collection<IRCChannelInfo> getChannels() {
  2144. synchronized (channelList) {
  2145. return channelList.values();
  2146. }
  2147. }
  2148. /**
  2149. * Clear the channel list.
  2150. */
  2151. public void clearChannels() {
  2152. synchronized (channelList) {
  2153. channelList.clear();
  2154. }
  2155. }
  2156. /** {@inheritDoc} */
  2157. @Override
  2158. public String[] parseHostmask(final String hostmask) {
  2159. return IRCClientInfo.parseHostFull(hostmask);
  2160. }
  2161. /** {@inheritDoc} */
  2162. @Override
  2163. public int getMaxTopicLength() {
  2164. if (h005Info.containsKey("TOPICLEN")) {
  2165. try {
  2166. return Integer.parseInt(h005Info.get("TOPICLEN"));
  2167. } catch (NumberFormatException ex) {
  2168. // Do nothing
  2169. }
  2170. }
  2171. return 0;
  2172. }
  2173. /** {@inheritDoc} */
  2174. @Override
  2175. public int getMaxLength() {
  2176. return MAX_LINELENGTH;
  2177. }
  2178. /** {@inheritDoc} */
  2179. @Override
  2180. public void setCompositionState(final String host, final CompositionState state) {
  2181. // Do nothing
  2182. }
  2183. }