Last active
August 29, 2015 14:13
-
-
Save Maome/b6f7c739461f22ddd274 to your computer and use it in GitHub Desktop.
how do i shot web?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package minirc; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.PrintWriter; | |
import java.net.Socket; | |
import java.net.UnknownHostException; | |
import javax.swing.JFrame; | |
import javax.swing.JOptionPane; | |
import javax.swing.JScrollPane; | |
import javax.swing.JTextArea; | |
import javax.swing.JTextField; | |
import javax.swing.text.DefaultCaret; | |
public class Application { | |
String server = "irc.freenode.net"; | |
Integer port = 6667; | |
String channel = "Genkiklaus"; | |
String username = ""; | |
BufferedReader in; | |
PrintWriter out; | |
JFrame frame = new JFrame("Danchat"); | |
JTextField textField = new JTextField(40); | |
JTextArea messageArea = new JTextArea(8, 40); | |
public Application() { | |
textField.setEditable(false); | |
messageArea.setEditable(false); | |
frame.getContentPane().add(textField, "South"); | |
frame.getContentPane().add(new JScrollPane(messageArea), "Center"); | |
frame.pack(); | |
DefaultCaret caret = (DefaultCaret)messageArea.getCaret(); | |
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); | |
messageArea.setLineWrap(true); | |
messageArea.setWrapStyleWord(true); | |
textField.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent e) { | |
String text = textField.getText(); | |
String command_text = "PRIVMSG #" + channel + " :" + text; | |
out.println(command_text); | |
messageArea.append("<" + username + "> " + text + "\n"); | |
textField.setText(""); | |
} | |
}); | |
} | |
private String parsePrivateMessage(String message){ | |
Integer idx = null; | |
message = message.substring(1); | |
idx = message.indexOf("!"); | |
String from = message.substring(0, idx); | |
idx = message.indexOf(":"); | |
String contents = message.substring(idx+1); | |
return "<" + from + "> " + contents; | |
} | |
private void run() throws UnknownHostException, IOException{ | |
username = getName(); | |
messageArea.setText("Connecting please wait...\n"); | |
Socket socket = new Socket(server, port); | |
in = new BufferedReader(new InputStreamReader(socket.getInputStream())); | |
out = new PrintWriter(socket.getOutputStream(), true); | |
//connect and join channel | |
out.println("USER" + " " + username + " none none " + username); | |
out.println("NICK" + " " + username); | |
out.println("JOIN #" + channel); | |
String line = ""; | |
while(!line.contains(":End of /NAMES list.")){ | |
line = in.readLine(); | |
} | |
messageArea.append("Connected!\n"); | |
textField.setEditable(true); | |
while(true) | |
{ | |
line = in.readLine(); | |
if(line.startsWith("PING")){ | |
out.println(line.replace("PING", "PONG")); | |
continue; | |
} | |
if(line.contains("!")) | |
line = parsePrivateMessage(line); | |
messageArea.append(line + "\n"); | |
} | |
} | |
private String getName() { | |
return JOptionPane.showInputDialog( | |
frame, | |
"Choose a screen name:", | |
"Screen name selection", | |
JOptionPane.PLAIN_MESSAGE); | |
} | |
public static void main(String[] args) { | |
Application client = new Application(); | |
client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
client.frame.setVisible(true); | |
try { | |
client.run(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment