You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IRCParser.java 82KB

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