001 package com.exedio.pop3serv.spam;
002
003 import com.exedio.pop3serv.Mail;
004 import com.exedio.pop3serv.MailDrop;
005 import com.exedio.pop3serv.Server;
006 import com.exedio.pop3serv.SingleAccountManager;
007 import java.io.ByteArrayInputStream;
008 import java.io.IOException;
009 import java.io.InputStream;
010 import java.util.ArrayList;
011 import java.util.List;
012
013 /**
014 * The SpamServer is a very simple mail server which always delivers the same mails to the client.
015 * After building pop3serv using "ant all" you can start this server using
016 * "java -cp classes;exampleclasses com.exedio.pop3serv.spam.SpamServer -user:username -pass:password".
017 * It will then listen on the default POP3 port (110). You may then use any mail client to connect using
018 * "username" and "password" as username and password.
019 * @author ebert
020 */
021 public class SpamServer extends Server
022 {
023 /**
024 * Here the filenames of the saved spam mails are defined.
025 * Those files need to be in the same directory as this class.
026 */
027 private final String[] theSpamMailFileNames = new String[] {
028 "positive1.eml", "positive2.eml", "negative.eml"
029 };
030
031 /**
032 * The global list of mails. Each session will get a copy of this list.
033 * If a client chooses to delete messages from the server (most do) than
034 * this list will not be affected.
035 */
036 private static List<Mail> theMails = new ArrayList<Mail>();
037
038 /**
039 * Creates a new instance. The given userName and password are used to authenticate
040 * users and allow access to the single maildrop.
041 */
042 public SpamServer(String userName, String password) throws IOException
043 {
044 super(new SpamMailDropManager(userName, password));
045 for (int i=0; i<theSpamMailFileNames.length; i++)
046 {
047 byte[] buffer = new byte[0];
048 InputStream stream = getClass().getResourceAsStream(theSpamMailFileNames[i]);
049 byte[] readBuffer = new byte[1024];
050 for (int size = stream.read(readBuffer); size!=-1; size=stream.read(readBuffer))
051 {
052 byte[] newBuffer = new byte[buffer.length+size];
053 System.arraycopy(buffer, 0, newBuffer, 0, buffer.length);
054 System.arraycopy(readBuffer, 0, newBuffer, buffer.length, size);
055 buffer = newBuffer;
056 }
057 theMails.add(new SpamMail(buffer));
058 }
059 }
060
061 /**
062 * The most easy implementation of a {@see com.exedio.pop3serv.MailDropManager}.
063 */
064 private static class SpamMailDropManager extends SingleAccountManager
065 {
066 SpamMailDropManager(String userName, String password)
067 {
068 super(userName, password);
069 }
070
071 protected MailDrop createMailDrop()
072 {
073 return new SpamMailDrop();
074 }
075 }
076
077 /**
078 * A very simple {@see MailDrop}. It uses a copy of the global mail list.
079 */
080 private static class SpamMailDrop extends MailDrop
081 {
082 private List<Mail> theMails;
083
084 public SpamMailDrop()
085 {
086 theMails = new ArrayList<Mail>(SpamServer.theMails);
087 }
088
089 public List<Mail> getMails()
090 {
091 return theMails;
092 }
093 }
094
095 /**
096 * This {@see Mail} subclass gets the content directly from the file.
097 */
098 private class SpamMail extends Mail
099 {
100 private final byte[] theContent;
101
102 public SpamMail(byte[] content)
103 {
104 theContent = content;
105 }
106
107 public void remove()
108 {
109 theMails.remove(this);
110 }
111
112 public InputStream getContent()
113 {
114 return new ByteArrayInputStream(theContent);
115 }
116
117 public long getContentLength()
118 {
119 return theContent.length;
120 }
121 }
122
123 public final static void main(String[] args)
124 {
125 String userName = "username";
126 String password = "password";
127 int port = 110;
128
129 for (int i=0; i<args.length; i++)
130 {
131 if (args[i].startsWith("-user:"))
132 {
133 userName = args[i].substring(6);
134 }
135 else if (args[i].startsWith("-pass:"))
136 {
137 password = args[i].substring(6);
138 }
139 else if (args[i].startsWith("-port:"))
140 {
141 port = Integer.parseInt(args[i].substring(6));
142 }
143 else
144 {
145 System.out.println("Usage: SpamServer [-user:<user>] [-pass:<password>] [-port:<port]");
146 return;
147 }
148 }
149 try
150 {
151 new SpamServer(userName, password).start(port);
152 }
153 catch(IOException e)
154 {
155 e.printStackTrace();
156 }
157 }
158 }
|