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 javax.servlet.*;
14 import javax.servlet.http.*;
16 public class MailingList implements Target, Iterable<MailingList.Subscriber> {
18 // DO NOT move this below the stuff underneath it
19 private MailingList(File path) throws IOException {
21 archive = FileBasedMailbox.getFileBasedMailbox(path.getCanonicalPath(), true);
22 properties = new PropertiesFile(new File(path.getCanonicalPath() + File.separatorChar + "properties"));
23 address = new Address(properties.get("address"));
24 homepage = properties.get("homepage");
25 one_line_description = properties.get("description");
26 message_footer = properties.get("footer");
29 public void banner(HttpServletRequest request, HttpServletResponse response) throws IOException {
30 String basename = request.getRequestURL()+"";
31 PrintWriter pw = new PrintWriter(response.getWriter());
34 pw.println(" <head>");
35 pw.println(" <style>");
36 pw.println(" TH, TD, P, LI, BODY {");
37 pw.println(" font-family: helvetica, verdana, arial, sans-serif;");
38 pw.println(" font-size: 12px; ");
39 pw.println(" text-decoration:none; ");
41 pw.println(" </style>");
42 pw.println(" </head>");
43 pw.println(" <body>");
45 String confirm = request.getParameter("confirm");
46 if (confirm != null) {
47 Subscribe sub = (Subscribe)Confirmation.decode(confirm, secret, new Date());
48 String email = sub.email;
50 String path = this.path.getAbsolutePath() + File.separatorChar + "subscribers" + File.separatorChar+sub.email;
51 if (sub.un) new File(path).delete();
53 Log.warn(null, "creating " + path);
54 new FileOutputStream(path).close();
57 pw.println(" <b>successfully "+sub.adj+"d " + email + " to " + properties.get("address"));
59 String action = request.getParameter("action");
60 String email = request.getParameter("email");
62 Subscribe sub = new Subscribe(email, request.getRequestURL().toString(), action.equals("unsubscribe"));
63 sub.signAndSend(new Address(properties.get("owner")),
65 pw.println("a confirmation email has been sent to " + email +
66 "; click the enclosed link to confirm your request to " + action);
68 pw.println("<table width=100% border=0 cellpadding=0 cellspacing=0><tr><td>");
69 pw.println(" <b>"+properties.get("address")+"</b><br> ");
70 pw.println(" <i>"+properties.get("description")+"</i> ");
71 pw.println("</td><td align=right>");
72 pw.println("<i>access via:</i> ");
73 if (path.getAbsolutePath().startsWith("/afs/"))
74 pw.println("<a target=_top href='file:" + path.getAbsolutePath() +"'>[AFS]</a> ");
75 pw.println("<a target=_top href="+properties.get("nntp")+">[NNTP]</a></tt>");
76 pw.println("<a target=_top href='mailto:"+properties.get("address")+"'>[SMTP]</a></tt>");
77 pw.println("<a target=_top href='"+request.getRequestURL()+"'>[HTTP]</a></tt>");
78 pw.println("</td></tr><tr><td colspan=2 align=right>");
79 pw.println(" <form action='"+basename+"' method=post name=form1 target=_top>");
80 pw.println(" <input type=text width=100 value='your@email.com' name=email>");
81 pw.println(" <input type=hidden name=frame value=banner>");
82 pw.println(" <select name=action onchange='form1.submit()'>");
83 pw.println(" <option>--choose action--</option>");
84 pw.println(" <option>subscribe</option>");
85 pw.println(" <option>unsubscribe</option>");
86 pw.println(" <option>preferences</option>");
87 pw.println(" </select>");
88 pw.println(" </form>");
89 pw.println("</td></tr></table>");
92 //pw.println(" </body>");
93 //pw.println("</html>");
97 public class Subscribe extends Confirmation {
99 public String basename;
102 public Subscribe(String email, String basename, boolean un) {
103 super(new Address(email), new Date().getTime() + (1000 * 60 * 60 * 24));
105 this.basename = basename;
107 this.adj = un ? "unsubscribe" : "subscribe";
109 public String getDescription() { return adj + " " + email + " to " + properties.get("address"); }
110 public String getURL(String tail) { return basename + "?frame=banner&confirm="+URLEncoder.encode(tail); }
113 private final File path;
114 private final FileBasedMailbox archive;
115 private final PropertiesFile properties;
117 private final long secret = new Random().nextLong();
118 public final Address address;
119 public String homepage;
121 public String one_line_description;
122 public String message_footer;
124 public int bounceThreshhold = 10;
126 public static class Subscriber {
127 public Subscriber(Address a) { this.address = a; }
128 public Address address;
132 // Pooling //////////////////////////////////////////////////////////////////////////////
135 public Iterator<Subscriber> iterator() {
136 final File subdir = new File(path.getAbsolutePath() + File.separatorChar + "subscribers");
137 if (!subdir.exists()) subdir.mkdirs();
138 final String[] subs = !subdir.isDirectory() ? new String[0] : subdir.list();
139 return new Iterator<Subscriber>() {
141 Subscriber prep = null;
142 public void remove() {
144 new File(subdir.getAbsolutePath() + File.separatorChar + subs[i++]).delete();
145 } catch (Exception e) {
146 Log.error(MailingList.class, e);
149 public boolean hasNext() { if (prep==null) prep = next(); return prep!=null; }
150 public Subscriber next() {
151 if (prep!=null) { Subscriber ret = prep; prep = null; return ret; }
152 while(i<subs.length) {
153 if (subs[i].indexOf('@')==-1) i++;
155 return new Subscriber(new Address(subs[i++]));
156 } catch (Exception e) {
157 Log.warn(MailingList.class, e);
166 private static HashMap<String,MailingList> cache = new HashMap<String,MailingList>();
167 public static MailingList getMailingList(String path) throws IOException { return getMailingList(new File(path)); }
168 public static MailingList getMailingList(File path) throws IOException {
169 if (!path.exists()) path.mkdirs();
170 MailingList ret = cache.get(path.getCanonicalPath());
171 if (ret==null) cache.put(path.getCanonicalPath(), ret = new MailingList(path));
175 // Methods //////////////////////////////////////////////////////////////////////////////
177 public Mailbox getArchive() throws IOException { return archive; }
179 public void accept(Message m) throws IOException, MailException {
180 StringBuffer buf = new StringBuffer();
181 m.getBody().getStream().transcribe(buf);
182 Headers head = new Headers.Original(m.headers.getStream());
183 head = head.set("List-Id", one_line_description + "<"+address+">");
184 head = head.set("Subject", properties.get("prefix") + " " + head.get("Subject"));
186 m = Message.newMessage(new Fountain.StringFountain(head.getString()+"\r\n"+buf.toString()));
187 Log.warn(MailingList.class, "archiving list message " + m.subject);
188 getArchive().accept(m);
190 for(Subscriber s : this) try {
191 Log.warn(MailingList.class, " trying " + s.address);
192 SMTP.accept(Message.newMessage(m, m.envelopeFrom, s.address));
193 Log.warn("[list]", "successfully sent to " + s);
194 } catch (Exception e2) { Log.error("[list]", e2); }
197 //public Filter[] filters = new Filter[0];
198 //public static class Filter {
199 // public class EmergencyModerationFilter { }
200 // public class MaximumLengthFilter { }
201 // public class SpamFilter { }
202 // public class MIMETypes { }
203 // public class MungeReplyTo { }
204 // public class AnonymizeSender { public boolean uncorrelated; }
208 //public static enum UserType { Administrator, Moderator, Member }
209 //public static enum SubscriptionType { All, None, Digest, MimeDigest }
210 //public static enum Visibility { Members, Public, Nobody }
211 //public static enum Action { Accept, Hold, Reject }
212 //public Visibility listVisibility = Visibility.Nobody;
213 //public Visibility membershipVisibility = Visibility.Nobody;
214 //public Visibility archiveVisibility = Visibility.Members;
215 //public Action defaultPostingType = Action.Hold;
217 //public Action posting = Action.Accept;
218 //public UserType type = UserType.Member;
219 //public SubscriptionType subscription = SubscriptionType.All;
220 //public boolean send_copy_of_own_post = false;
221 //public boolean filter_duplicates_when_ccd = true;