X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Femail%2FTestEmailProvider.java;h=35c4b3fe81351af1aaf6df7a44a983899a92ee30;hb=28e686114d3a851a8f3163862b0c4ec4de3eacce;hp=e37e802a079e9f69f22f0aaa5c9ea191ffb6208f;hpb=634b7f75c8fc2ed8799bad74731278fb59198c48;p=gigi.git diff --git a/src/org/cacert/gigi/email/TestEmailProvider.java b/src/org/cacert/gigi/email/TestEmailProvider.java index e37e802a..35c4b3fe 100644 --- a/src/org/cacert/gigi/email/TestEmailProvider.java +++ b/src/org/cacert/gigi/email/TestEmailProvider.java @@ -1,5 +1,6 @@ package org.cacert.gigi.email; +import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.InetAddress; @@ -11,38 +12,66 @@ class TestEmailProvider extends EmailProvider { ServerSocket servs; Socket client; DataOutputStream out; + DataInputStream in; + protected TestEmailProvider(Properties props) { try { - servs = new ServerSocket(Integer.parseInt(props - .getProperty("emailProvider.port")), 10, - InetAddress.getByName("127.0.0.1")); + servs = new ServerSocket(Integer.parseInt(props.getProperty("emailProvider.port")), 10, + InetAddress.getByName("127.0.0.1")); } catch (IOException e) { e.printStackTrace(); } } + @Override - public synchronized void sendmail(String to, String subject, - String message, String from, String replyto, String toname, - String fromname, String errorsto, boolean extra) throws IOException { - boolean sent = false; - while (!sent) { - if (client == null || client.isClosed()) { - client = servs.accept(); - out = new DataOutputStream(client.getOutputStream()); - } + public synchronized void sendmail(String to, String subject, String message, String from, String replyto, + String toname, String fromname, String errorsto, boolean extra) throws IOException { + while (true) { + assureLocalConnection(); try { + out.writeUTF("mail"); write(to); write(subject); write(message); write(from); write(replyto); out.flush(); - sent = true; + return; + } catch (IOException e) { + client = null; + } + } + } + + private void assureLocalConnection() throws IOException { + if (out != null) { + try { + out.writeUTF("ping"); + } catch (IOException e) { + client = null; + } + } + if (client == null || client.isClosed()) { + client = servs.accept(); + out = new DataOutputStream(client.getOutputStream()); + in = new DataInputStream(client.getInputStream()); + } + } + + @Override + public synchronized String checkEmailServer(int forUid, String address) throws IOException { + while (true) { + assureLocalConnection(); + try { + out.writeUTF("challengeAddrBox"); + out.writeUTF(address); + return in.readUTF(); } catch (IOException e) { client = null; } } } + private void write(String to) throws IOException { if (to == null) { out.writeUTF("");