Package vicazh.hyperpool.stream.net.smtp

Source Code of vicazh.hyperpool.stream.net.smtp.SwitchStream

package vicazh.hyperpool.stream.net.smtp;

import java.io.*;
import java.net.Socket;
import java.net.*;
import java.util.*;
import java.util.logging.*;
import org.xbill.DNS.*;
import vicazh.hyperpool.Start;
import vicazh.hyperpool.stream.*;
import vicazh.hyperpool.stream.net.*;

class SwitchStream extends ClientStream {

  SwitchStream(Connection connection) {
    super(connection, new NullStream());
    msg(220, "Welcome to " + Start.SYSTEM_NAME + "/" + Start.VERSION);
  }

  void msg(final int code, final String message) {
    new Thread() {
      public void run() {
        try {
          ((ServerStream) connection.getServer()).msg(code, message);
          connection.getServer().flush();
          return;
        } catch (SocketException e) {
          Start.logger.fine(e.getMessage());
        } catch (SocketTimeoutException e) {
          Start.logger.fine(e.getMessage());
        } catch (UnknownHostException e) {
          Start.logger.fine(e.getMessage());
        } catch (BreakException e) {
        } catch (Exception e) {
          Start.logger.log(Level.SEVERE, e.getMessage(), e);
        }
        try {
          connection.getServer().close();
        } catch (Exception e) {
        }
      }
    }.start();
  }

  private static final String OK = "OK";

  private String from;

  public void from(String s) throws IOException {
    super.from(s);
    from = s;
    msg(250, OK);
  }

  private String helo;

  public void helo(String s) throws IOException {
    super.helo(s);
    helo = s;
    msg(250, "Hello " + s);
  }

  private String getDomain(String rcpt) {
    return rcpt.substring(rcpt.indexOf('@') + 1);
  }

  private Socket connect(String rcpt) throws IOException {
    Record[] records = new Lookup(getDomain(rcpt), Type.MX).run();
    if (records == null)
      throw new IOException("Lookup failed");
    TreeMap<Integer, String> map = new TreeMap<Integer, String>();
    for (Record r : records)
      map.put(((MXRecord) r).getPriority(), ((MXRecord) r).getTarget()
          .toString());
    while (!map.isEmpty()) {
      String s = map.remove(map.firstKey());
      int port = 25;
      int i = s.indexOf(":");
      if (i >= 0) {
        try {
          port = Integer.parseInt(s.substring(i + 1));
        } catch (Exception e) {
          continue;
        }
        if (i == 0)
          s = "localhost";
        else
          s = s.substring(0, i);
      }
      try {
        return new Socket(s, port);
      } catch (IOException e) {
      }
    }
    throw new IOException("Could not connect to any SMTP server");
  }

  private Socket socket;

  public void rcpt(String s) throws IOException {
    if (socket == null) {
      try {
        socket = connect(s.replace("<", "").replace(">", "")
            .toLowerCase());
        socket
            .setSoTimeout(((SwitchService) connection.element).timeout);
        outputstream = new BufferedOutputStream(socket
            .getOutputStream());
        BufferedReader r = new BufferedReader(new InputStreamReader(
            socket.getInputStream()));
        receive(r);
        super.helo(helo);
        flush();
        receive(r);
        super.from(from);
        flush();
        receive(r);
        Transfer.start(
            new BufferedInputStream(socket.getInputStream()),
            connection.getServer());
      } catch (IOException e) {
        try {
          connection.getServer().close();
        } catch (Exception e1) {
        }
        throw e;
      }
    }
    super.rcpt(s);
  }

  public vicazh.hyperpool.stream.net.Socket getSocket() {
    return new vicazh.hyperpool.stream.net.Socket(socket);
  }

  public void close() throws IOException {
    super.close();
    try {
      socket.close();
    } catch (Exception e) {
    }
    new Thread() {
      public void run() {
        try {
          connection.getServer().close();
        } catch (Exception e) {
        }
      }
    }.start();
  }

  private void receive(BufferedReader r) throws IOException {
    String result;
    while ((result = r.readLine()).charAt(0) == ' '
        || result.charAt(3) != ' ') {
      Start.logger.finer(result);
    }
    Start.logger.finer(result);
  }

}
TOP

Related Classes of vicazh.hyperpool.stream.net.smtp.SwitchStream

TOP
Copyright © 2018 www.massapi.com. 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.