636fa4e6668816ee50338591706e92d4801864c4
[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  Address          address;
41         public  Action           posting;
42         public  UserType         type;
43         public  SubscriptionType subscription;
44         public  boolean          send_copy_of_own_post;
45         public  boolean          filter_duplicates_when_ccd;
46     }
47
48
49     // Pooling //////////////////////////////////////////////////////////////////////////////
50
51     private MailingList(File path) { super(path); }
52     public static MailingList getMailingList(String path) { return getMailingList(new File(path)); }
53     public static MailingList getMailingList(File path) {
54         if (!path.exists()) path.mkdirs();
55         File f = new File(path.getAbsolutePath() + File.separatorChar + ".mailinglist");
56         try {
57             if (f.exists()) return (MailingList)Persistent.read(f);
58             MailingList ret = new MailingList(path);
59             ret.write();
60             return ret;
61         } catch (Exception e) {
62             Log.error(MailingList.class, e);
63             return null;
64         }
65     }
66
67     // Methods //////////////////////////////////////////////////////////////////////////////
68
69     public Mailbox getArchive() { return FileBasedMailbox.getFileBasedMailbox(path, true); }
70
71     public void accept(Message m) throws IOException, MailException {
72         StringBuffer buf = new StringBuffer();
73         m.getBody().getStream().transcribe(buf);
74         Headers head = new Headers(m.headers.getStream());
75         head.put("list-id", one_line_description + "<"+address+">");
76         
77         m = Message.newMessage(new Fountain.StringFountain(head.getString()+"\r\n"+buf.toString()));
78         Log.warn(MailingList.class, "archiving list message " + m.subject);
79         getArchive().accept(m);
80         
81         for(java.util.Enumeration e = subscribers.elements(); e.hasMoreElements();) try {
82             Subscriber s = (Subscriber)e.nextElement();
83             Log.warn(MailingList.class, "  trying " + s.address);
84             SMTP.accept(Message.newMessage(m, m.envelopeFrom, s.address));
85             Log.warn("[list]", "successfully sent to " + s);
86         } catch (Exception e2) { Log.error("[list]", e2); }
87     }
88
89     //public Filter[]     filters  = new Filter[0];
90     //public static class Filter {
91     //    public class EmergencyModerationFilter { }
92     //    public class MaximumLengthFilter { }
93     //    public class SpamFilter { }
94     //    public class MIMETypes { }
95     //    public class MungeReplyTo { }
96     //    public class AnonymizeSender { public boolean uncorrelated; }
97     //}
98 }