1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
6 import org.ibex.util.*;
8 import org.ibex.mail.target.*;
9 import org.ibex.mail.protocol.*;
13 //import org.prevayler.*;
14 //import org.prevayler.Query;
15 import javax.servlet.*;
16 import javax.servlet.http.*;
18 public class MailingList implements Target, Iterable<MailingList.Subscriber> {
20 // DO NOT move this below the stuff underneath it
21 private MailingList(File path) throws IOException {
23 archive = FileBasedMailbox.getFileBasedMailbox(path.getCanonicalPath(), true);
24 properties = new PropertiesFile(new File(path.getCanonicalPath() + File.separatorChar + "properties"));
25 address = new Address(properties.get("address"));
26 homepage = properties.get("homepage");
27 one_line_description = properties.get("description");
28 message_footer = properties.get("footer");
31 public void banner(HttpServletRequest request, HttpServletResponse response) throws IOException {
32 String basename = request.getServletPath();
33 PrintWriter pw = new PrintWriter(response.getWriter());
35 pw.println(" <body>");
36 String confirm = request.getParameter("confirm");
37 if (confirm != null) {
38 Subscribe sub = (Subscribe)Confirmation.decode(confirm, Long.parseLong(properties.get("secret")), new Date());
39 String email = sub.email;
41 String path = this.path.getAbsolutePath() + File.separatorChar + "subscribers" + File.separatorChar + sub.email;
42 if (sub.un) new File(path).delete();
44 Log.warn(null, "creating " + path);
45 new FileOutputStream(path).close();
48 pw.println(" <b>successfully "+sub.adj+"d " + email + " to " + properties.get("address"));
50 pw.println(" <b>"+properties.get("address")+"</b><br>");
51 pw.println(" <tt><a href="+properties.get("nntp")+">"+properties.get("nntp")+"</a></tt><br>");
52 String action = request.getParameter("action");
53 String email = request.getParameter("email");
55 Subscribe sub = new Subscribe(email, request.getRequestURL().toString(), action.equals("unsubscribe"));
56 sub.signAndSend(new Address(properties.get("owner")), Long.parseLong(properties.get("secret")), new Date());
57 pw.println("a confirmation email has been sent to " + email + "; click the enclosed link to confirm your request to " + action);
59 pw.println(" <form action='"+basename+"' method=post name=form1>");
60 pw.println(" <input type=text width=100 value='your@email.com' name=email>");
61 pw.println(" <input type=hidden name=frame value=banner>");
62 pw.println(" <select name=action onchange='form1.submit()'>");
63 pw.println(" <option>--choose action--</option>");
64 pw.println(" <option>subscribe</option>");
65 pw.println(" <option>unsubscribe</option>");
66 pw.println(" </select>");
67 pw.println(" </form>");
70 pw.println(" </body>");
71 pw.println("</html>");
76 public class Subscribe extends Confirmation {
78 public String basename;
81 public Subscribe(String email, String basename, boolean un) {
82 super(new Address(email), new Date().getTime() + (1000 * 60 * 60 * 24));
84 this.basename = basename;
86 this.adj = un ? "unsubscribe" : "subscribe";
88 public String getDescription() { return adj + " " + email + " to " + properties.get("address"); }
89 public String getURL(String tail) { return basename + "?frame=banner&confirm="+URLEncoder.encode(tail); }
92 private final File path;
93 private final FileBasedMailbox archive;
94 private final PropertiesFile properties;
96 private final long secret = new Random().nextLong();
97 public final Address address;
98 public String homepage;
100 public String one_line_description;
101 public String message_footer;
103 public int bounceThreshhold = 10;
105 public static class Subscriber {
106 public Subscriber(Address a) { this.address = a; }
107 public Address address;
111 // Pooling //////////////////////////////////////////////////////////////////////////////
114 public Iterator<Subscriber> iterator() {
115 final File subdir = new File(path.getAbsolutePath() + File.separatorChar + "subscribers");
116 if (!subdir.exists()) subdir.mkdirs();
117 final String[] subs = !subdir.isDirectory() ? new String[0] : subdir.list();
118 return new Iterator<Subscriber>() {
120 Subscriber prep = null;
121 public void remove() {
123 new File(subdir.getAbsolutePath() + File.separatorChar + subs[i++]).delete();
124 } catch (Exception e) {
125 Log.error(MailingList.class, e);
128 public boolean hasNext() { if (prep==null) prep = next(); return prep!=null; }
129 public Subscriber next() {
130 if (prep!=null) { Subscriber ret = prep; prep = null; return ret; }
131 while(i<subs.length) {
132 if (subs[i].indexOf('@')==-1) i++;
134 return new Subscriber(new Address(subs[i++]));
135 } catch (Exception e) {
136 Log.warn(MailingList.class, e);
145 private static HashMap<String,MailingList> cache = new HashMap<String,MailingList>();
146 public static MailingList getMailingList(String path) throws IOException { return getMailingList(new File(path)); }
147 public static MailingList getMailingList(File path) throws IOException {
148 if (!path.exists()) path.mkdirs();
149 MailingList ret = cache.get(path.getCanonicalPath());
150 if (ret==null) cache.put(path.getCanonicalPath(), ret = new MailingList(path));
154 // Methods //////////////////////////////////////////////////////////////////////////////
156 public Mailbox getArchive() throws IOException { return archive; }
158 public void accept(Message m) throws IOException, MailException {
159 StringBuffer buf = new StringBuffer();
160 m.getBody().getStream().transcribe(buf);
161 Headers head = new Headers.Original(m.headers.getStream());
162 head = head.set("List-Id", one_line_description + "<"+address+">");
163 head = head.set("Subject", properties.get("prefix") + " " + head.get("Subject"));
165 m = Message.newMessage(new Fountain.StringFountain(head.getString()+"\r\n"+buf.toString()));
166 Log.warn(MailingList.class, "archiving list message " + m.subject);
167 getArchive().accept(m);
169 for(Subscriber s : this) try {
170 Log.warn(MailingList.class, " trying " + s.address);
171 SMTP.accept(Message.newMessage(m, m.envelopeFrom, s.address));
172 Log.warn("[list]", "successfully sent to " + s);
173 } catch (Exception e2) { Log.error("[list]", e2); }
176 //public Filter[] filters = new Filter[0];
177 //public static class Filter {
178 // public class EmergencyModerationFilter { }
179 // public class MaximumLengthFilter { }
180 // public class SpamFilter { }
181 // public class MIMETypes { }
182 // public class MungeReplyTo { }
183 // public class AnonymizeSender { public boolean uncorrelated; }
187 //public static enum UserType { Administrator, Moderator, Member }
188 //public static enum SubscriptionType { All, None, Digest, MimeDigest }
189 //public static enum Visibility { Members, Public, Nobody }
190 //public static enum Action { Accept, Hold, Reject }
191 //public Visibility listVisibility = Visibility.Nobody;
192 //public Visibility membershipVisibility = Visibility.Nobody;
193 //public Visibility archiveVisibility = Visibility.Members;
194 //public Action defaultPostingType = Action.Hold;
196 //public Action posting = Action.Accept;
197 //public UserType type = UserType.Member;
198 //public SubscriptionType subscription = SubscriptionType.All;
199 //public boolean send_copy_of_own_post = false;
200 //public boolean filter_duplicates_when_ccd = true;