Package net.bnubot.core

Examples of net.bnubot.core.Connection


      this.socket = socket;
    }

    @Override
    public void run() {
      Connection pri = null;
      do {
        try {
          sleep(1000);
        } catch (InterruptedException e) {}
        if(profile != null)
          pri = profile.getPrimaryConnection();
        if((pri != null) && (pri.getMyUser() == null))
          pri = null;
      } while(pri == null);

      try {
        InputStream is = socket.getInputStream();
        OutputStream os = socket.getOutputStream();
        bw = new BufferedWriter(new OutputStreamWriter(os));
        BufferedReader br = new BufferedReader(new InputStreamReader(is));

        while((is.available() == 0) && socket.isConnected())
          sleep(500);

        int loginAttempts = 0;
        while(!connected && (++loginAttempts < 3)) {
          if(loginAttempts == 1) {
            // This is the first login attempt; require them to shake hands
            int b = is.read();
            while(b == 255) {
              sleep(500);
              b = is.read();
            }
            if((b != 'c') && (b != 3))
              break;

            Out.debug(TelnetEventHandler.class, "Connection established from " + socket.getRemoteSocketAddress().toString());
          }

          sendInternal("Username: ");
          String username = br.readLine();
          if(username.length() == 0)
            username = br.readLine(); // Fool me once, shame on you
          if(username.length() == 0)
            continue; // Fool me twice, shame on me

          while(username.charAt(0) < 0x20)
            username = username.substring(1);

          ConnectionSettings cs = pri.getConnectionSettings();
          if(!username.equalsIgnoreCase(cs.username)) {
            sendInternal("1019 Error \"Invalid username\"");
            continue;
          }

          sendInternal("Password: ");
          String password = br.readLine();

          if(!password.equalsIgnoreCase(cs.password)) {
            sendInternal("1019 Error \"Invalid password\"");
            continue;
          }

          connected = true;
        }

        if(connected) {
          Out.debug(TelnetEventHandler.class, "Login accepted from " + socket.getRemoteSocketAddress().toString());
          dispatch(ChatEvent.NAME, pri.getMyUser().getShortLogonName());
          dispatch(ChatEvent.CHANNEL, quoteText(pri.getChannel()));
          for(BNetUser user : pri.getUsers())
            dispatchUserDetail(ChatEvent.USER, user);
        } else
          send("Login failed");

        try {
          while(connected) {
            if(!socket.isConnected())
              break;
            pri.sendChatInternal(br.readLine());
          }
        } catch(SocketException e) {
          Out.debug(TelnetEventHandler.class, socket.getRemoteSocketAddress().toString() + " " + e.getMessage());
        }
      } catch(Exception e) {
View Full Code Here


      // If there's text in the queue to send
      if(queue.size() > 0) {
        // Iterate through the connecitons
        Iterator<Connection> it = cons.iterator();
        while(it.hasNext()) {
          Connection con = it.next();
          // Check if the con can send text now
          if(con.canSendChat()) {
            if(!con.isOp()) {
              //Find a string we can send
              int queueIndex;
              findSendableText: for(queueIndex = 0; queueIndex < queue.size(); queueIndex++) {
                String text = queue.get(queueIndex);
               
                // Check if ops is required
                try {
                  if(text.charAt(0) == '/') {
                    String cmd = text.substring(1);
                    int i = cmd.indexOf(' ');
                    if(i != -1) {
                      cmd = cmd.substring(0, i).toLowerCase();
                     
                      if(cmd.equals("kick")
                      || cmd.equals("ban"))
                        continue findSendableText;
                    }
                  }
                } catch(Exception e) {}

                // Write the text out
                con.sendChatNow(queue.remove(queueIndex));
              }
            } else {
              // Write the text out
              con.sendChatNow(queue.remove());
            }
           
            // If the queue is empty, stop
            if(queue.size() == 0)
              break;
View Full Code Here

  private static HashMap<String, Session> sessions = new java.util.HashMap<String, Session>();
  private static Profile profile = null;
 
  public static void sendChat(String text) {
    if(profile != null) {
      Connection c = profile.getPrimaryConnection();
      if(c != null)
        c.sendChat(text, true);
    }
  }
View Full Code Here

TOP

Related Classes of net.bnubot.core.Connection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.