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