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