]> WPIA git - gigi.git/blob - src/org/cacert/gigi/email/TestEmailProvider.java
caf29661e4df6565bd1e70d4c813bd95e77b3ee6
[gigi.git] / src / org / cacert / gigi / email / TestEmailProvider.java
1 package org.cacert.gigi.email;
2
3 import java.io.DataInputStream;
4 import java.io.DataOutputStream;
5 import java.io.IOException;
6 import java.net.InetAddress;
7 import java.net.ServerSocket;
8 import java.net.Socket;
9 import java.util.Properties;
10
11 class TestEmailProvider extends EmailProvider {
12
13     ServerSocket servs;
14
15     Socket client;
16
17     DataOutputStream out;
18
19     DataInputStream in;
20
21     protected TestEmailProvider(Properties props) {
22         try {
23             servs = new ServerSocket(Integer.parseInt(props.getProperty("emailProvider.port")), 10, InetAddress.getByName("127.0.0.1"));
24         } catch (IOException e) {
25             e.printStackTrace();
26         }
27     }
28
29     @Override
30     public synchronized void sendmail(String to, String subject, String message, String from, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
31         while (true) {
32             assureLocalConnection();
33             try {
34                 out.writeUTF("mail");
35                 write(to);
36                 write(subject);
37                 write(message);
38                 write(from);
39                 write(replyto);
40                 out.flush();
41                 return;
42             } catch (IOException e) {
43                 client = null;
44             }
45         }
46     }
47
48     private void assureLocalConnection() throws IOException {
49         if (out != null) {
50             try {
51                 out.writeUTF("ping");
52             } catch (IOException e) {
53                 client = null;
54             }
55         }
56         if (client == null || client.isClosed()) {
57             client = servs.accept();
58             out = new DataOutputStream(client.getOutputStream());
59             in = new DataInputStream(client.getInputStream());
60         }
61     }
62
63     @Override
64     public synchronized String checkEmailServer(int forUid, String address) throws IOException {
65         while (true) {
66             assureLocalConnection();
67             try {
68                 out.writeUTF("challengeAddrBox");
69                 out.writeUTF(address);
70                 return in.readUTF();
71             } catch (IOException e) {
72                 client = null;
73             }
74         }
75     }
76
77     private void write(String to) throws IOException {
78         if (to == null) {
79             out.writeUTF("<null>");
80         } else {
81             out.writeUTF(to);
82         }
83     }
84
85 }