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 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack
  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 dmdirc.parser;
  23. import java.io.*;
  24. import java.net.*;
  25. import java.util.ArrayList;
  26. public class IRCParser implements Runnable {
  27. class MyInfo {
  28. String sNickname = "IRCParser";
  29. String sRealname = "Java Test IRCParser";
  30. String sUsername = "IRCParser";
  31. }
  32. private Socket socket = null;
  33. private PrintWriter out = null;
  34. private BufferedReader in = null;
  35. public MyInfo me = new MyInfo();
  36. private boolean HasBegan = false;
  37. private boolean IsFirst = true;
  38. // Events
  39. public interface IMOTDEnd { public void onMOTDEnd(IRCParser tParser); }
  40. public interface IDataIn { public void onDataIn(IRCParser tParser, String sData); }
  41. public interface IDataOut { public void onDataOut(IRCParser tParser, String sData); }
  42. class AllEvents {
  43. ArrayList<IMOTDEnd> EndOfMOTD = new ArrayList<IMOTDEnd>();
  44. ArrayList<IDataIn> DataIn = new ArrayList<IDataIn>();
  45. ArrayList<IDataOut> DataOut = new ArrayList<IDataOut>();
  46. }
  47. public AllEvents cb = new AllEvents();
  48. public void AddMOTDEnd(Object eMethod) { cb.EndOfMOTD.add((IMOTDEnd)eMethod); }
  49. public void DelMOTDEnd(Object eMethod) {
  50. for (int i = 0; i < cb.EndOfMOTD.size(); i++) {
  51. if (eMethod.equals((Object)cb.EndOfMOTD.get(i))) { cb.EndOfMOTD.remove(i); break; }
  52. }
  53. }
  54. private void CallMOTDEnd() {
  55. for (int i = 0; i < cb.EndOfMOTD.size(); i++) {
  56. // IMOTDEnd(cb.EndOfMOTD.get(i)).OnMOTDEnd(this);
  57. cb.EndOfMOTD.get(i).onMOTDEnd(this);
  58. }
  59. }
  60. public void AddDataIn(Object eMethod) { cb.DataIn.add((IDataIn)eMethod); }
  61. public void DelDataIn(Object eMethod) {
  62. for (int i = 0; i < cb.DataIn.size(); i++) {
  63. if (eMethod.equals((Object)cb.DataIn.get(i))) { cb.DataIn.remove(i); break; }
  64. }
  65. }
  66. private void CallDataIn(String data) {
  67. for (int i = 0; i < cb.DataIn.size(); i++) {
  68. cb.DataIn.get(i).onDataIn(this, data);
  69. }
  70. }
  71. public void AddDataOut(Object eMethod) { cb.DataOut.add((IDataOut)eMethod); }
  72. public void DelDataOut(Object eMethod) {
  73. for (int i = 0; i < cb.DataOut.size(); i++) {
  74. if (eMethod.equals((Object)cb.DataOut.get(i))) { cb.DataOut.remove(i); break; }
  75. }
  76. }
  77. private void CallDataOut(String data) {
  78. for (int i = 0; i < cb.DataOut.size(); i++) {
  79. cb.DataOut.get(i).onDataOut(this, data);
  80. }
  81. }
  82. // Constructor.
  83. IRCParser () { }
  84. public void connect(String sHost) throws Exception {
  85. try {
  86. connect(sHost,6667);
  87. } catch (Exception e) {
  88. throw e;
  89. }
  90. }
  91. public void connect(String sHost, int nPort) throws Exception {
  92. if (HasBegan) { return; }
  93. try {
  94. socket = new Socket(sHost,nPort);
  95. out = new PrintWriter(socket.getOutputStream(), true);
  96. in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  97. } catch (UnknownHostException e) {
  98. throw new Exception("Socket Exception");
  99. } catch (Exception e) {
  100. throw new Exception("General Exception");
  101. }
  102. }
  103. public void run() {
  104. if (HasBegan) { return; } else { HasBegan = true; }
  105. // :HACK: While true loops really really suck.
  106. while(true){
  107. String line = "";
  108. try {
  109. line = in.readLine(); // Blocking :/
  110. if (IsFirst) {
  111. SendString("NICK "+me.sNickname);
  112. SendString("USER "+me.sUsername.toLowerCase()+" * * :"+me.sRealname);
  113. IsFirst = false;
  114. }
  115. ProcessLine(line);
  116. } catch (IOException e) {
  117. System.out.println("Socket read failed");
  118. System.exit(-1);
  119. }
  120. }
  121. }
  122. protected void finalize(){
  123. try{
  124. socket.close();
  125. } catch (IOException e) {
  126. System.out.println("Could not close socket");
  127. System.exit(-1);
  128. }
  129. }
  130. private String GetParam(String line) {
  131. String[] params = null;
  132. params = line.split(" :",2);
  133. return params[params.length-1];
  134. }
  135. private String[] IRCTokenise(String line) {
  136. String[] params = null;
  137. String[] tokens = null;
  138. params = line.split(" :",2);
  139. tokens = params[0].split(" ");
  140. String[] temp = new String[tokens.length+1];
  141. System.arraycopy(tokens, 0, temp, 0, tokens.length);
  142. tokens = temp;
  143. if (params.length == 2) { tokens[tokens.length-1] = params[1]; }
  144. return tokens;
  145. }
  146. public void SendLine(String line) {SendString(line);} // This should do some checks on stuff possible, public event!
  147. // Our Method
  148. private void SendString(String line) {
  149. CallDataOut(line);
  150. out.printf("%s\r\n",line);
  151. }
  152. private void ProcessLine(String line) {
  153. String[] token = IRCTokenise(line);
  154. String sParam = token[token.length-1];
  155. int nParam;
  156. CallDataIn(line);
  157. try {nParam = Integer.parseInt(token[1]);} catch (Exception e) { nParam = -1;}
  158. if (token[0].equals("PING") || token[1].equals("PING")) { SendString("PONG :"+sParam); }
  159. else {
  160. if (token[0].substring(0,1).equals(":")) {
  161. // Post Connect
  162. switch (nParam) {
  163. case -1:
  164. ProcessStringParam(sParam,token);
  165. break;
  166. case 1: // 001
  167. break;
  168. case 422: // No MOTD
  169. case 376: // End of MOTD
  170. ProcessEndOfMOTD(sParam,token);
  171. break;
  172. default: // Unknown
  173. break;
  174. }
  175. } else {
  176. // Pre Connect
  177. }
  178. }
  179. }
  180. private void ProcessStringParam(String sParam,String token[]) {
  181. // Process a line where the parameter is a string (IE PRIVMSG, NOTICE etc - Not including PING!)
  182. }
  183. private void ProcessEndOfMOTD(String sParam,String token[]) {
  184. // Process EndOfMOTD
  185. CallMOTDEnd();
  186. }
  187. //-------------------------------------------------------------------------
  188. public void JoinChannel(String sChannelName) {
  189. SendLine("JOIN "+sChannelName);
  190. }
  191. }