Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.91 KB | None | 0 0
  1. package ircclient.connection;
  2.  
  3. import java.net.*;
  4. import java.io.*;
  5. import javax.swing.*;
  6. import ircclient.windows.*;
  7. import ircclient.infos.IRCClient;
  8.  
  9. public class IRC implements Runnable{
  10.  
  11.     private winClient wClient;
  12.     private Socket sock=null;
  13.     private DataOutputStream oStream=null;
  14.     private BufferedReader bReader=null;
  15.     private String buffer="";
  16.     private Thread thread;
  17.     private String IrcServer="";
  18.     private int IrcPort=-1;
  19.     public String IrcNick="";
  20.     private String IrcSuffix="";
  21.     public String IrcRoom="";
  22.     public StringBuilder sBuilderOne=new StringBuilder();
  23.     private StringBuilder sBuilderTwo=new StringBuilder();
  24.     private boolean isFirstConnection=true;
  25.     private boolean requestNames=false;
  26.     private boolean isNickInUsed=false;
  27.    
  28.     public IRC(String irc_server,int irc_port,String irc_nick,String irc_suffix,String irc_room,winClient win) {
  29.  
  30.        IrcServer=irc_server;
  31.        IrcPort=irc_port;
  32.        IrcNick=irc_nick;
  33.        IrcSuffix=irc_suffix;
  34.        IrcRoom=irc_room;
  35.        wClient=win;
  36.  
  37.        try {
  38.            sBuilderOne.append("<font color=\"red\">Connecting...</font><br />");
  39.  
  40.            wClient.editorPaneOne.setText(sBuilderOne.toString());
  41.            sock=new Socket(IrcServer,IrcPort);
  42.  
  43.            oStream = new DataOutputStream(sock.getOutputStream());
  44.            bReader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
  45.  
  46.            thread=new Thread(this);
  47.            thread.start();
  48.  
  49.        }catch(UnknownHostException e) {
  50.             JOptionPane.showMessageDialog(wClient,"An Unknown Host Exception Occurred. The Application Will Exit.",IRCClient.APPLICATION_TITLE + " " + IRCClient.VERSION_NUMBER,JOptionPane.ERROR_MESSAGE);
  51.             System.exit(1);
  52.        }catch(IOException e) {
  53.             JOptionPane.showMessageDialog(wClient,"An Input/Output Exception Occurred. The Application Will Exit.",IRCClient.APPLICATION_TITLE + " " + IRCClient.VERSION_NUMBER,JOptionPane.ERROR_MESSAGE);
  54.             System.exit(1);
  55.        }
  56.  
  57.     }
  58.  
  59.     public void closeIRC() {
  60.  
  61.         if(sock != null) {
  62.             try {
  63.                 sock.close();
  64.             }catch(IOException e) {
  65.                     //catch exception
  66.             }
  67.         }
  68.  
  69.         if(oStream != null) {
  70.             try {
  71.                 oStream.close();
  72.             }catch(IOException e) {
  73.                 //catch exception
  74.             }
  75.  
  76.             oStream=null;
  77.         }
  78.  
  79.         if(bReader != null) {
  80.             try {
  81.                 bReader.close();
  82.             }catch(IOException e) {
  83.                 //catch exception
  84.             }
  85.  
  86.             bReader=null;
  87.         }
  88.  
  89.         thread=null;
  90.     }
  91.  
  92.     public void send(String msg) {
  93.         try {
  94.             oStream.writeBytes(msg);
  95.         }catch(IOException e) {
  96.         }catch(NullPointerException e) {    
  97.         }
  98.  
  99.     }
  100.  
  101.     //method to override (Runnable)
  102.     public void run() {
  103.        
  104.         String[] str=null;
  105.         String msg="";
  106.         String nck="";
  107.         boolean isEnd = false;
  108.         int Posi = 0;
  109.  
  110.         while(true) {
  111.             try {
  112.                 if(buffer != null) {
  113.                     buffer=bReader.readLine();
  114.                     System.out.println(buffer); //for debugging
  115.  
  116.                     if(buffer.contains("Checking Ident")) {
  117.                         sBuilderOne.append("<font color=\"red\">Registering...</font><br />");
  118.                         wClient.editorPaneOne.setText(sBuilderOne.toString());
  119.                         send("NICK " + IrcNick + "\r\n"); //send nickname
  120.                     }else if(buffer.contains("No Ident response")) {
  121.                         send("USER " + IrcNick + " \"" + IrcNick + ".com\" \"" + IrcServer + "\" :" + IrcNick + " robot\r\n");
  122.                     }else if(buffer.contains("End of /MOTD command")) {
  123.                         sBuilderOne.append("<font color=\"red\">Joining...</font><br />");
  124.                         wClient.editorPaneOne.setText(sBuilderOne.toString());
  125.                         send("JOIN #" + IrcRoom + "\r\n"); //join channel
  126.  
  127.                     }else if(buffer.contains("End of /NAMES list")) {
  128.                         if(isFirstConnection) {
  129.                             isFirstConnection=false;
  130.                             sBuilderOne.append("<font color=\"red\">Connected...</font><br />");
  131.                             wClient.editorPaneOne.setText(sBuilderOne.toString());
  132.                             wClient.txtMsg.setEditable(true);
  133.                             wClient.txtMsg.requestFocus();
  134.                            
  135.                         }
  136.                     }else if(buffer.contains("353") && requestNames == true) {
  137.                         isEnd=false;
  138.                         Posi=0;
  139.                         sBuilderTwo.setLength(0);
  140.                         wClient.editorPaneTwo.setText("");
  141.  
  142.                         while(!isEnd) {
  143.                             try {
  144.                                 str=buffer.split(" = ");
  145.                                 str=str[1].split("#" + IrcRoom + " :");
  146.                                 str=str[1].split(" ");
  147.                             }catch(ArrayIndexOutOfBoundsException e) {
  148.                                 str=buffer.split(" @ ");
  149.                                 str=str[1].split("#" + IrcRoom + " :");
  150.                                 str=str[1].split(" ");        
  151.                             }
  152.  
  153.                             for(int n=0;n<str.length;n++) {
  154.                                 sBuilderTwo.append("<a href=\"" + str[n] + "\">" + str[n] + "</a><br />");
  155.                             }
  156.  
  157.                             Posi+=str.length;
  158.                             str=null;
  159.                             buffer=bReader.readLine();
  160.  
  161.                             if(buffer.contains("366")) {
  162.                                 isEnd=true;
  163.                                 Posi=0;
  164.                             }
  165.  
  166.                         }
  167.                  
  168.                         wClient.editorPaneTwo.setText(sBuilderTwo.toString());
  169.                         requestNames=false;
  170.                     }else if(buffer.contains("JOIN :#" + IrcRoom)) {
  171.                         send("NAMES #" + IrcRoom + "\r\n");
  172.                         requestNames = true;
  173.                         //dito ilalagay ang autoresponder na welcome to channel
  174.                         //get the nick
  175.                         str = buffer.split(":");
  176.                         str=str[1].split("!");
  177.                         nck=str[0];
  178.                         str=null;
  179.                        
  180.                         if(!(nck.equals(IrcNick) || nck.equals(IrcNick + IrcSuffix))) {
  181.                             send("PRIVMSG #" + IrcRoom + " :Magandang " + IRCClient.getMeridiem() + " sa'yo " + nck + "." + wClient.getQuitLog(nck.trim()) + "\r\n"); //the welcome autoresponder
  182.                         }
  183.  
  184.                         //update the quit log
  185.                         wClient.updateQuitLog(nck.trim());
  186.                        
  187.                     }else if(buffer.contains("QUIT") || buffer.contains("NICK :")) {
  188.                         send("NAMES #" + IrcRoom + "\r\n");
  189.                         requestNames = true;    
  190.                     }else if(buffer.contains("PRIVMSG " + IrcNick)) {
  191.  
  192.                         str = buffer.split(":");
  193.                         str=str[1].split("!");
  194.                         nck=str[0];
  195.  
  196.                         if(isNickInUsed) {
  197.                             str=buffer.split("PRIVMSG " + IrcNick + IrcSuffix + " :");
  198.                         }else {
  199.                             str=buffer.split("PRIVMSG " + IrcNick + " :");
  200.                         }
  201.  
  202.                         msg=str[1];
  203.                         str = null;
  204.  
  205.                         if(nck.contains("frigg")) {
  206.                             if(msg.contains("VERSION")) {
  207.                                 send("NOTICE frigg :.VERSION " + IRCClient.APPLICATION_TITLE  + " " + IRCClient.VERSION_NUMBER + "\r\n");
  208.                             }
  209.                         }else{
  210.                             sBuilderOne.append("<font color=\"blue\"><b>" + nck + "</b>: " + msg + "</font><br />");
  211.                             wClient.editorPaneOne.setText(sBuilderOne.toString());
  212.                         }
  213.                        
  214.                     }else if(buffer.contains("PRIVMSG #" + IrcRoom)) {
  215.  
  216.                         str = buffer.split(":");
  217.                         str=str[1].split("!");
  218.                         nck=str[0];
  219.  
  220.                         str=buffer.split("PRIVMSG #" + IrcRoom + " :");
  221.                         msg=str[1];
  222.  
  223.                         str = null;
  224.                         sBuilderOne.append("<font color=\"f26522\"><b>" + nck + "</b>: " + msg + "</font><br />");
  225.                         wClient.editorPaneOne.setText(sBuilderOne.toString());
  226.  
  227.                         //set the bot command here
  228.                         //eto yung channel chat
  229.                         if(msg.trim().equals("!oras")) {
  230.                             send("PRIVMSG #" + IrcRoom + " :Ang oras ay " + IRCClient.getTime() + ".\r\n");
  231.                         }else if(msg.trim().equals("!petsa")) {
  232.                             send("PRIVMSG #" + IrcRoom + " :Ang petsa ay " + IRCClient.getDate() + ".\r\n");
  233.                         }else if(msg.contains("!php")) {
  234.                             wClient.sendPHPFunction(msg.trim().replace("!php", "").trim());
  235.                         }
  236.  
  237.                        
  238.                     }else if(buffer.contains("PING :")) {
  239.                         send(buffer.replace("PING","PONG") + "\r\n");
  240.                         System.out.println("Ponging");      
  241.                     }else if(buffer.contains("Nickname is already in use")) {
  242.                         isNickInUsed=true;
  243.                         send("NICK " + IrcNick + IrcSuffix + "\r\n");
  244.                         sBuilderOne.append("<font color=\"red\">Nick in used..Registering Again...</font><br />");
  245.                         wClient.editorPaneOne.setText(sBuilderOne.toString());
  246.                     }else if(buffer.contains("Erroneous Nickname")) {
  247.                         JOptionPane.showMessageDialog(wClient,"Invalid Nickname. The Application Will Exit.",IRCClient.APPLICATION_TITLE + " " + IRCClient.VERSION_NUMBER,JOptionPane.ERROR_MESSAGE);
  248.                         System.exit(1);
  249.                     }else if(buffer.contains("This nickname is registered")) {
  250.                         //hindi pa supported ang paglologin ng nick
  251.                         JOptionPane.showMessageDialog(wClient,"NickName Already Registered. The Application Will Exit.",IRCClient.APPLICATION_TITLE + " " + IRCClient.VERSION_NUMBER,JOptionPane.ERROR_MESSAGE);
  252.                         System.exit(1);
  253.                     }
  254.                 }
  255.             }catch(IOException e) {
  256.  
  257.             }
  258.         }
  259.  
  260.     }
  261.     //end of method to override (Runnable)
  262.  
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement