updates to MailingList code
[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 org.prevayler.*;
13 import org.prevayler.Query;
14
15 public class MailingList extends Persistent implements Target {
16
17     public static enum UserType         { Administrator, Moderator, Member }
18     public static enum SubscriptionType { All, None, Digest, MimeDigest }
19     public static enum Visibility       { Members, Public, Nobody }
20     public static enum Action           { Accept, Hold, Reject }
21
22     public        Address     address;
23     private final long        secret               = new Random().nextLong();
24
25     public        Hashtable   subscribers          = new Hashtable();
26
27     public        String      homepage             = "";
28     public        String      one_line_description = "";
29     public        String      long_description     = "";
30     public        String      message_footer       = "";
31
32     public        Visibility  listVisibility       = Visibility.Nobody;
33     public        Visibility  membershipVisibility = Visibility.Nobody;
34     public        Visibility  archiveVisibility    = Visibility.Members;
35     public        Action      defaultPostingType   = Action.Hold;
36
37     public        int         bounceThreshhold     = 10;
38
39     public static class Subscriber {
40         public Subscriber(Address a) { this.address = a; }
41         public  Address          address;
42         public  Action           posting = Action.Accept;
43         public  UserType         type = UserType.Member;
44         public  SubscriptionType subscription = SubscriptionType.All;
45         public  boolean          send_copy_of_own_post = false;
46         public  boolean          filter_duplicates_when_ccd = true;
47     }
48
49
50     // Pooling //////////////////////////////////////////////////////////////////////////////
51
52     private MailingList(File path) { super(path); }
53     public static MailingList getMailingList(String path) { return getMailingList(new File(path)); }
54     public static MailingList getMailingList(File path) {
55         if (!path.exists()) path.mkdirs();
56         path = new File(path.getAbsolutePath() + File.separatorChar + ".mailinglist");
57         try {
58             if (path.exists()) return (MailingList)Persistent.read(path);
59             MailingList ret = new MailingList(path);
60             ret.write();
61             return ret;
62         } catch (Exception e) {
63             Log.error(MailingList.class, e);
64             return null;
65         }
66     }
67
68     // Methods //////////////////////////////////////////////////////////////////////////////
69
70     public Mailbox getArchive() { return FileBasedMailbox.getFileBasedMailbox(file.getParent(), true); }
71
72     public void accept(Message m) throws IOException, MailException {
73         StringBuffer buf = new StringBuffer();
74         m.getBody().getStream().transcribe(buf);
75         Headers head = new Headers(m.headers.getStream());
76         head.put("list-id", one_line_description + "<"+address+">");
77         
78         m = Message.newMessage(new Fountain.StringFountain(head.getString()+"\r\n"+buf.toString()));
79         Log.warn(MailingList.class, "archiving list message " + m.subject);
80         getArchive().accept(m);
81         
82         for(java.util.Enumeration e = subscribers.elements(); e.hasMoreElements();) try {
83             Subscriber s = (Subscriber)e.nextElement();
84             Log.warn(MailingList.class, "  trying " + s.address);
85             SMTP.accept(Message.newMessage(m, m.envelopeFrom, s.address));
86             Log.warn("[list]", "successfully sent to " + s);
87         } catch (Exception e2) { Log.error("[list]", e2); }
88     }
89
90     //public Filter[]     filters  = new Filter[0];
91     //public static class Filter {
92     //    public class EmergencyModerationFilter { }
93     //    public class MaximumLengthFilter { }
94     //    public class SpamFilter { }
95     //    public class MIMETypes { }
96     //    public class MungeReplyTo { }
97     //    public class AnonymizeSender { public boolean uncorrelated; }
98     //}
99 }