1 package org.cacert.gigi.testUtils;
3 import java.io.DataInputStream;
4 import java.io.DataOutputStream;
5 import java.io.IOException;
6 import java.net.SocketAddress;
7 import java.net.Socket;
8 import java.util.concurrent.LinkedBlockingQueue;
9 import java.util.concurrent.TimeUnit;
10 import java.util.regex.Matcher;
11 import java.util.regex.Pattern;
13 import org.cacert.gigi.email.EmailProvider;
15 public class TestEmailReciever extends EmailProvider implements Runnable {
17 public class TestMail {
29 public TestMail(String to, String subject, String message, String from, String replyto) {
31 this.subject = subject;
32 this.message = message;
34 this.replyto = replyto;
37 public String getTo() {
41 public String getSubject() {
45 public String getMessage() {
49 public String getFrom() {
53 public String getReplyto() {
57 public String extractLink() {
58 Pattern link = Pattern.compile("https?://[^\\s]+(?=\\s)");
59 Matcher m = link.matcher(getMessage());
68 private DataInputStream dis;
70 private DataOutputStream dos;
72 public TestEmailReciever(SocketAddress target) throws IOException {
76 s.setSoTimeout(1000 * 60 * 60);
77 dis = new DataInputStream(s.getInputStream());
78 dos = new DataOutputStream(s.getOutputStream());
79 new Thread(this).start();
83 LinkedBlockingQueue<TestMail> mails = new LinkedBlockingQueue<TestEmailReciever.TestMail>();
85 public TestMail recieve() throws InterruptedException {
86 return mails.poll(5, TimeUnit.SECONDS);
93 String type = dis.readUTF();
94 if (type.equals("mail")) {
95 String to = dis.readUTF();
96 String subject = dis.readUTF();
97 String message = dis.readUTF();
98 String from = dis.readUTF();
99 String replyto = dis.readUTF();
100 mails.add(new TestMail(to, subject, message, from, replyto));
101 } else if (type.equals("challengeAddrBox")) {
102 String email = dis.readUTF();
103 dos.writeUTF(quickEmailCheck(email));
105 } else if (type.equals("ping")) {
107 System.err.println("Unknown type: " + type);
110 } catch (IOException e) {
118 private String quickEmailCheck(String email) throws IOException {
119 if (approveRegex.matcher(email).matches()) {
126 String error = "FAIL";
128 public void setEmailCheckError(String error) {
132 Pattern approveRegex = Pattern.compile(".*");
134 public void setApproveRegex(Pattern approveRegex) {
135 this.approveRegex = approveRegex;
138 public void clearMails() {
142 public void reset() {
145 approveRegex = Pattern.compile(".*");
148 boolean closed = false;
150 public void destroy() {
154 } catch (IOException e) {
160 public String checkEmailServer(int forUid, String address) throws IOException {
161 return quickEmailCheck(address);
165 public void sendmail(String to, String subject, String message, String from, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
166 mails.add(new TestMail(to, subject, message, from, replyto));