disable everything that requires extraneous third-party jarfiles
[org.ibex.mail.git] / src / org / ibex / mail / MailingList.java
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.
4
5 package org.ibex.mail;
6 import org.ibex.util.*;
7 import org.ibex.io.*;
8 import org.ibex.mail.target.*;
9 import org.ibex.mail.protocol.*;
10 import java.util.*;
11 import java.io.*;
12 import java.net.*;
13 //import org.prevayler.*;
14 //import org.prevayler.Query;
15 import javax.servlet.*;
16 import javax.servlet.http.*;
17
18 public class MailingList implements Target, Iterable<MailingList.Subscriber> {
19
20     // DO NOT move this below the stuff underneath it
21     private MailingList(File path) throws IOException {
22         this.path = path;
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");
29     }
30
31     public void banner(HttpServletRequest request, HttpServletResponse response) throws IOException {
32         String basename = request.getServletPath();
33         PrintWriter pw = new PrintWriter(response.getWriter());
34         pw.println("<html>");
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;
40             synchronized(this) {
41                 String path = this.path.getAbsolutePath() + File.separatorChar + "subscribers" + File.separatorChar + sub.email;
42                 if (sub.un) new File(path).delete();
43                 else {
44                     Log.warn(null, "creating " + path);
45                     new FileOutputStream(path).close();
46                 }
47             }
48             pw.println("    <b>successfully "+sub.adj+"d " + email + " to " + properties.get("address"));
49         } else {
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");
54             if (action != null) {
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);
58             } else {
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>");
68             }
69         }
70         pw.println("  </body>");
71         pw.println("</html>");
72         pw.flush();
73         pw.close();
74     }
75
76     public class Subscribe extends Confirmation {
77         public String email;
78         public String basename;
79         public boolean un;
80         public String adj;
81         public Subscribe(String email, String basename, boolean un) {
82             super(new Address(email), new Date().getTime() + (1000 * 60 * 60 * 24));
83             this.email = email;
84             this.basename = basename;
85             this.un = un;
86             this.adj = un ? "unsubscribe" : "subscribe";
87         }
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); }
90     }
91
92     private final File             path;
93     private final FileBasedMailbox archive;
94     private final PropertiesFile   properties;
95
96     private final long             secret               = new Random().nextLong();
97     public  final Address          address;
98     public        String           homepage;
99
100     public        String           one_line_description;
101     public        String           message_footer;
102
103     public        int              bounceThreshhold     = 10;
104
105     public static class Subscriber {
106         public Subscriber(Address a) { this.address = a; }
107         public  Address          address;
108     }
109
110
111     // Pooling //////////////////////////////////////////////////////////////////////////////
112
113     
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>() {
119             int i=0;
120             Subscriber prep = null;
121             public void remove() {
122                 try {
123                     new File(subdir.getAbsolutePath() + File.separatorChar + subs[i++]).delete();
124                 } catch (Exception e) {
125                     Log.error(MailingList.class, e);
126                 }
127             }
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++;
133                     else try {
134                         return new Subscriber(new Address(subs[i++]));
135                     } catch (Exception e) {
136                         Log.warn(MailingList.class, e);
137                         continue;
138                     }
139                 }
140                 return null;
141             }
142         };
143     }
144
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));
151         return ret;
152     }
153
154     // Methods //////////////////////////////////////////////////////////////////////////////
155
156     public Mailbox getArchive() throws IOException { return archive; }
157
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"));
164         
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);
168
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); }
174     }
175
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; }
184     //}
185 }    
186
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;
195
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;