Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.bbw2.ircBot;;
- import javax.swing.*;
- import java.io.*;
- import java.net.Socket;
- /**
- * Created on 10/14/2015.
- */
- public class Server {
- private String serverName, nick, login, pass;
- private Channel[] channels;
- private Socket socket;
- private BufferedWriter writer;
- private BufferedReader reader;
- private JTabbedPane serverTabs;
- public Server(String serverName, String nick, String login, String pass, Channel... channels){
- this.serverName = serverName;
- this.nick = nick;
- this.login = login;
- this.pass = pass;
- this.channels = channels;
- this.serverTabs = new JTabbedPane();
- }
- public JTabbedPane getServerTabs() {return serverTabs;}
- public String getServerName() {return serverName;}
- public void init() throws IOException {
- socket = new Socket(serverName,6667);
- writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
- reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- }
- public boolean start() throws IOException {
- writer.write("NICK " + nick + "\r\n");
- writer.write("USER " + login + " . . .\r\n");
- writer.flush();
- String line;
- while ((line = reader.readLine()) != null){
- System.out.println(line);
- if (line.startsWith("PING ")){
- writer.write("PONG " + line.substring(5) + "\r\n");
- writer.flush();
- } else if (line.contains("004")){
- if (pass!=null) {
- writer.write("PRIVMSG NickServ IDENTIFY " + pass);
- writer.flush();
- }
- return true;
- } else if (line.contains("443")){
- System.out.println("Nick already in use.");
- return false;
- }
- }
- return false;
- }
- public void joinChannels() throws IOException {
- for (Channel channel : channels) {
- channel.setServer(this);
- writer.write("JOIN " + channel.getChannelName() + "\r\n");
- }
- writer.flush();
- String line;
- Bot bot = new Bot(this);
- while ((line = reader.readLine())!=null){
- if (line.startsWith("PING ")){
- writer.write("PONG " + line.substring(5) + "\r\n");
- writer.flush();
- } else {
- System.out.println(line);
- String[] split0 = line.split(":");
- String[] split1 = split0[1].split(" ");
- String code = split1[1];
- if (!"005 251 252 254 255 265 266 353 366 372 375 376 NOTICE MODE JOIN".contains(code) && !code.contains(" "))
- bot.translate(line);
- }
- }
- }
- public String getNick() {
- return nick;
- }
- public Channel[] getChannels() {return channels;}
- public BufferedWriter getWriter() {return writer;}
- }
Advertisement
Add Comment
Please, Sign In to add comment