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 public class TestEmailReciever implements Runnable {
14 public class TestMail {
21 public TestMail(String to, String subject, String message, String from, String replyto) {
23 this.subject = subject;
24 this.message = message;
26 this.replyto = replyto;
29 public String getTo() {
33 public String getSubject() {
37 public String getMessage() {
41 public String getFrom() {
45 public String getReplyto() {
49 public String extractLink() {
50 Pattern link = Pattern.compile("https?://[^\\s]+(?=\\s)");
51 Matcher m = link.matcher(getMessage());
59 private DataInputStream dis;
60 private DataOutputStream dos;
62 public TestEmailReciever(SocketAddress target) throws IOException {
66 s.setSoTimeout(1000 * 60 * 60);
67 dis = new DataInputStream(s.getInputStream());
68 dos = new DataOutputStream(s.getOutputStream());
69 new Thread(this).start();
72 LinkedBlockingQueue<TestMail> mails = new LinkedBlockingQueue<TestEmailReciever.TestMail>();
74 public TestMail recieve() throws InterruptedException {
75 return mails.poll(5, TimeUnit.SECONDS);
82 String type = dis.readUTF();
83 if (type.equals("mail")) {
84 String to = dis.readUTF();
85 String subject = dis.readUTF();
86 String message = dis.readUTF();
87 String from = dis.readUTF();
88 String replyto = dis.readUTF();
89 mails.add(new TestMail(to, subject, message, from, replyto));
90 } else if (type.equals("challengeAddrBox")) {
91 String email = dis.readUTF();
92 if (approveRegex.matcher(email).matches()) {
98 } else if (type.equals("ping")) {
100 System.err.println("Unknown type: " + type);
103 } catch (IOException e) {
111 Pattern approveRegex = Pattern.compile(".*");
113 public void setApproveRegex(Pattern approveRegex) {
114 this.approveRegex = approveRegex;
117 public void clearMails() {
121 public void reset() {
123 approveRegex = Pattern.compile(".*");
126 boolean closed = false;
128 public void destroy() {
132 } catch (IOException e) {