public
Last active

some old little IRC client

  • Download Gist
IRCClient.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package communication;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
/**
*
* @author rEaCH
*/
public class IRCClient {
private final BufferedWriter out;
private final BufferedReader in;
 
private String channel, host;
private final Socket server;
 
public IRCClient(String host, int port) {
this.host = host;
 
try {
server = new Socket(host, port);
} catch (UnknownHostException ex) {
} catch (IOException ex) {
}
try {
out = new BufferedWriter(new OutputStreamWriter(server.getOutputStream()));
in = new BufferedReader(new InputStreamReader(server.getInputStream()));
} catch (IOException ex) {
ex.printStackTrace();
}
}
 
public void connect(String myNick) {
try {
writeMsg("USER", myNick + " host irc " + myNick);
writeMsg("NICK", myNick);
String line;
while ((line = in.readLine()) != null) {
if (line.indexOf("004") >= 0) {
break;
}
parsePing(line);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
 
public void disconnect() {
try {
server.close();
} catch (IOException ex) {
}
}
 
private void writeMsg(String cmd, String msg) {
writeIrc(cmd + " " + msg + "\r\n");
}
 
public void joinChannel(String channel) {
String this.channel = channel;
writeMsg("JOIN", channel + " blubber");
}
 
public void sendPong() {
writeIrc("PONG " + host);
}
 
private void writeIrc(String msg) {
try {
out.write(msg);
out.flush();
} catch (IOException ex) {
}
}
 
public void writePrivMsg(String msg) {
writeMsg("PRIVMSG " + channel, ":" + msg);
}
 
public String recvMsg() throws IOException {
String line = in.readLine();
if (line != null) {
parsePing(line);
} else {
line = "";
}
 
return line;
}
 
private void parsePing(String line) {
if (line.startsWith("PING ")) {
writeMsg("PONG", line.substring(6));
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.