Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ircclient.connection;
- import java.net.*;
- import java.io.*;
- import javax.swing.*;
- import ircclient.windows.*;
- import ircclient.infos.IRCClient;
- public class IRC implements Runnable{
- private winClient wClient;
- private Socket sock=null;
- private DataOutputStream oStream=null;
- private BufferedReader bReader=null;
- private String buffer="";
- private Thread thread;
- private String IrcServer="";
- private int IrcPort=-1;
- public String IrcNick="";
- private String IrcSuffix="";
- public String IrcRoom="";
- public StringBuilder sBuilderOne=new StringBuilder();
- private StringBuilder sBuilderTwo=new StringBuilder();
- private boolean isFirstConnection=true;
- private boolean requestNames=false;
- private boolean isNickInUsed=false;
- public IRC(String irc_server,int irc_port,String irc_nick,String irc_suffix,String irc_room,winClient win) {
- IrcServer=irc_server;
- IrcPort=irc_port;
- IrcNick=irc_nick;
- IrcSuffix=irc_suffix;
- IrcRoom=irc_room;
- wClient=win;
- try {
- sBuilderOne.append("<font color=\"red\">Connecting...</font><br />");
- wClient.editorPaneOne.setText(sBuilderOne.toString());
- sock=new Socket(IrcServer,IrcPort);
- oStream = new DataOutputStream(sock.getOutputStream());
- bReader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
- thread=new Thread(this);
- thread.start();
- }catch(UnknownHostException e) {
- JOptionPane.showMessageDialog(wClient,"An Unknown Host Exception Occurred. The Application Will Exit.",IRCClient.APPLICATION_TITLE + " " + IRCClient.VERSION_NUMBER,JOptionPane.ERROR_MESSAGE);
- System.exit(1);
- }catch(IOException e) {
- JOptionPane.showMessageDialog(wClient,"An Input/Output Exception Occurred. The Application Will Exit.",IRCClient.APPLICATION_TITLE + " " + IRCClient.VERSION_NUMBER,JOptionPane.ERROR_MESSAGE);
- System.exit(1);
- }
- }
- public void closeIRC() {
- if(sock != null) {
- try {
- sock.close();
- }catch(IOException e) {
- //catch exception
- }
- }
- if(oStream != null) {
- try {
- oStream.close();
- }catch(IOException e) {
- //catch exception
- }
- oStream=null;
- }
- if(bReader != null) {
- try {
- bReader.close();
- }catch(IOException e) {
- //catch exception
- }
- bReader=null;
- }
- thread=null;
- }
- public void send(String msg) {
- try {
- oStream.writeBytes(msg);
- }catch(IOException e) {
- }catch(NullPointerException e) {
- }
- }
- //method to override (Runnable)
- public void run() {
- String[] str=null;
- String msg="";
- String nck="";
- boolean isEnd = false;
- int Posi = 0;
- while(true) {
- try {
- if(buffer != null) {
- buffer=bReader.readLine();
- System.out.println(buffer); //for debugging
- if(buffer.contains("Checking Ident")) {
- sBuilderOne.append("<font color=\"red\">Registering...</font><br />");
- wClient.editorPaneOne.setText(sBuilderOne.toString());
- send("NICK " + IrcNick + "\r\n"); //send nickname
- }else if(buffer.contains("No Ident response")) {
- send("USER " + IrcNick + " \"" + IrcNick + ".com\" \"" + IrcServer + "\" :" + IrcNick + " robot\r\n");
- }else if(buffer.contains("End of /MOTD command")) {
- sBuilderOne.append("<font color=\"red\">Joining...</font><br />");
- wClient.editorPaneOne.setText(sBuilderOne.toString());
- send("JOIN #" + IrcRoom + "\r\n"); //join channel
- }else if(buffer.contains("End of /NAMES list")) {
- if(isFirstConnection) {
- isFirstConnection=false;
- sBuilderOne.append("<font color=\"red\">Connected...</font><br />");
- wClient.editorPaneOne.setText(sBuilderOne.toString());
- wClient.txtMsg.setEditable(true);
- wClient.txtMsg.requestFocus();
- }
- }else if(buffer.contains("353") && requestNames == true) {
- isEnd=false;
- Posi=0;
- sBuilderTwo.setLength(0);
- wClient.editorPaneTwo.setText("");
- while(!isEnd) {
- try {
- str=buffer.split(" = ");
- str=str[1].split("#" + IrcRoom + " :");
- str=str[1].split(" ");
- }catch(ArrayIndexOutOfBoundsException e) {
- str=buffer.split(" @ ");
- str=str[1].split("#" + IrcRoom + " :");
- str=str[1].split(" ");
- }
- for(int n=0;n<str.length;n++) {
- sBuilderTwo.append("<a href=\"" + str[n] + "\">" + str[n] + "</a><br />");
- }
- Posi+=str.length;
- str=null;
- buffer=bReader.readLine();
- if(buffer.contains("366")) {
- isEnd=true;
- Posi=0;
- }
- }
- wClient.editorPaneTwo.setText(sBuilderTwo.toString());
- requestNames=false;
- }else if(buffer.contains("JOIN :#" + IrcRoom)) {
- send("NAMES #" + IrcRoom + "\r\n");
- requestNames = true;
- //dito ilalagay ang autoresponder na welcome to channel
- //get the nick
- str = buffer.split(":");
- str=str[1].split("!");
- nck=str[0];
- str=null;
- if(!(nck.equals(IrcNick) || nck.equals(IrcNick + IrcSuffix))) {
- send("PRIVMSG #" + IrcRoom + " :Magandang " + IRCClient.getMeridiem() + " sa'yo " + nck + "." + wClient.getQuitLog(nck.trim()) + "\r\n"); //the welcome autoresponder
- }
- //update the quit log
- wClient.updateQuitLog(nck.trim());
- }else if(buffer.contains("QUIT") || buffer.contains("NICK :")) {
- send("NAMES #" + IrcRoom + "\r\n");
- requestNames = true;
- }else if(buffer.contains("PRIVMSG " + IrcNick)) {
- str = buffer.split(":");
- str=str[1].split("!");
- nck=str[0];
- if(isNickInUsed) {
- str=buffer.split("PRIVMSG " + IrcNick + IrcSuffix + " :");
- }else {
- str=buffer.split("PRIVMSG " + IrcNick + " :");
- }
- msg=str[1];
- str = null;
- if(nck.contains("frigg")) {
- if(msg.contains("VERSION")) {
- send("NOTICE frigg :.VERSION " + IRCClient.APPLICATION_TITLE + " " + IRCClient.VERSION_NUMBER + "\r\n");
- }
- }else{
- sBuilderOne.append("<font color=\"blue\"><b>" + nck + "</b>: " + msg + "</font><br />");
- wClient.editorPaneOne.setText(sBuilderOne.toString());
- }
- }else if(buffer.contains("PRIVMSG #" + IrcRoom)) {
- str = buffer.split(":");
- str=str[1].split("!");
- nck=str[0];
- str=buffer.split("PRIVMSG #" + IrcRoom + " :");
- msg=str[1];
- str = null;
- sBuilderOne.append("<font color=\"f26522\"><b>" + nck + "</b>: " + msg + "</font><br />");
- wClient.editorPaneOne.setText(sBuilderOne.toString());
- //set the bot command here
- //eto yung channel chat
- if(msg.trim().equals("!oras")) {
- send("PRIVMSG #" + IrcRoom + " :Ang oras ay " + IRCClient.getTime() + ".\r\n");
- }else if(msg.trim().equals("!petsa")) {
- send("PRIVMSG #" + IrcRoom + " :Ang petsa ay " + IRCClient.getDate() + ".\r\n");
- }else if(msg.contains("!php")) {
- wClient.sendPHPFunction(msg.trim().replace("!php", "").trim());
- }
- }else if(buffer.contains("PING :")) {
- send(buffer.replace("PING","PONG") + "\r\n");
- System.out.println("Ponging");
- }else if(buffer.contains("Nickname is already in use")) {
- isNickInUsed=true;
- send("NICK " + IrcNick + IrcSuffix + "\r\n");
- sBuilderOne.append("<font color=\"red\">Nick in used..Registering Again...</font><br />");
- wClient.editorPaneOne.setText(sBuilderOne.toString());
- }else if(buffer.contains("Erroneous Nickname")) {
- JOptionPane.showMessageDialog(wClient,"Invalid Nickname. The Application Will Exit.",IRCClient.APPLICATION_TITLE + " " + IRCClient.VERSION_NUMBER,JOptionPane.ERROR_MESSAGE);
- System.exit(1);
- }else if(buffer.contains("This nickname is registered")) {
- //hindi pa supported ang paglologin ng nick
- JOptionPane.showMessageDialog(wClient,"NickName Already Registered. The Application Will Exit.",IRCClient.APPLICATION_TITLE + " " + IRCClient.VERSION_NUMBER,JOptionPane.ERROR_MESSAGE);
- System.exit(1);
- }
- }
- }catch(IOException e) {
- }
- }
- }
- //end of method to override (Runnable)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement