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 {
16 public class TestMail {
23 public TestMail(String to, String subject, String message, String from, String replyto) {
25 this.subject = subject;
26 this.message = message;
28 this.replyto = replyto;
31 public String getTo() {
35 public String getSubject() {
39 public String getMessage() {
43 public String getFrom() {
47 public String getReplyto() {
51 public String extractLink() {
52 Pattern link = Pattern.compile("https?://[^\\s]+(?=\\s)");
53 Matcher m = link.matcher(getMessage());
61 private DataInputStream dis;
62 private DataOutputStream dos;
64 public TestEmailReciever(SocketAddress target) throws IOException {
68 s.setSoTimeout(1000 * 60 * 60);
69 dis = new DataInputStream(s.getInputStream());
70 dos = new DataOutputStream(s.getOutputStream());
71 new Thread(this).start();
75 LinkedBlockingQueue<TestMail> mails = new LinkedBlockingQueue<TestEmailReciever.TestMail>();
77 public TestMail recieve() throws InterruptedException {
78 return mails.poll(5, TimeUnit.SECONDS);
85 String type = dis.readUTF();
86 if (type.equals("mail")) {
87 String to = dis.readUTF();
88 String subject = dis.readUTF();
89 String message = dis.readUTF();
90 String from = dis.readUTF();
91 String replyto = dis.readUTF();
92 mails.add(new TestMail(to, subject, message, from, replyto));
93 } else if (type.equals("challengeAddrBox")) {
94 String email = dis.readUTF();
95 dos.writeUTF(quickEmailCheck(email));
97 } else if (type.equals("ping")) {
99 System.err.println("Unknown type: " + type);
102 } catch (IOException e) {
110 private String quickEmailCheck(String email) throws IOException {
111 if (approveRegex.matcher(email).matches()) {
118 String error = "FAIL";
120 public void setEmailCheckError(String error) {
124 Pattern approveRegex = Pattern.compile(".*");
126 public void setApproveRegex(Pattern approveRegex) {
127 this.approveRegex = approveRegex;
130 public void clearMails() {
134 public void reset() {
137 approveRegex = Pattern.compile(".*");
140 boolean closed = false;
142 public void destroy() {
146 } catch (IOException e) {
152 public String checkEmailServer(int forUid, String address) throws IOException {
153 return quickEmailCheck(address);
157 public void sendmail(String to, String subject, String message, String from, String replyto, String toname,
158 String fromname, String errorsto, boolean extra) throws IOException {
159 mails.add(new TestMail(to, subject, message, from, replyto));