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 {
20 public TestMail(String to, String subject, String message, String from,
23 this.subject = subject;
24 this.message = message;
26 this.replyto = replyto;
28 public String getTo() {
31 public String getSubject() {
34 public String getMessage() {
37 public String getFrom() {
40 public String getReplyto() {
43 public String extractLink() {
44 Pattern link = Pattern.compile("https?://[^\\s]+(?=\\s)");
45 Matcher m = link.matcher(getMessage());
52 private DataInputStream dis;
53 private DataOutputStream dos;
55 public TestEmailReciever(SocketAddress target) throws IOException {
59 s.setSoTimeout(1000 * 60 * 60);
60 dis = new DataInputStream(s.getInputStream());
61 dos = new DataOutputStream(s.getOutputStream());
62 new Thread(this).start();
64 LinkedBlockingQueue<TestMail> mails = new LinkedBlockingQueue<TestEmailReciever.TestMail>();
66 public TestMail recieve() throws InterruptedException {
67 return mails.poll(5, TimeUnit.SECONDS);
73 String type = dis.readUTF();
74 if (type.equals("mail")) {
75 String to = dis.readUTF();
76 String subject = dis.readUTF();
77 String message = dis.readUTF();
78 String from = dis.readUTF();
79 String replyto = dis.readUTF();
80 mails.add(new TestMail(to, subject, message, from, replyto));
81 } else if (type.equals("challengeAddrBox")) {
82 String email = dis.readUTF();
83 if (approveRegex.matcher(email).matches()) {
84 System.out.println("approving mbox: " + email);
87 System.out.println("rejecting mbox: " + email);
91 } else if (type.equals("ping")) {
93 System.err.println("Unknown type: " + type);
96 } catch (IOException e) {
101 Pattern approveRegex = Pattern.compile(".*");
102 public void setApproveRegex(Pattern approveRegex) {
103 this.approveRegex = approveRegex;
106 public void clearMails() {
109 public void reset() {
111 approveRegex = Pattern.compile(".*");