fd9ef0964e2e42e221e5b58c135cc6c466327210
[org.ibex.mail.git] / src / org / ibex / mail / List.java
1 package org.ibex.mail;
2 import org.ibex.util.*;
3 import org.ibex.mail.target.*;
4 import java.util.*;
5 import java.io.*;
6 import org.prevayler.*;
7 import org.prevayler.Query;
8
9 // FEATURE: umbrella structure to mailing lists
10 public class List implements Serializable {
11
12     public static enum UserType         { Administrator, Moderator, Member }
13     public static enum SubscriptionType { All, None, Digest, MimeDigest }
14     public static enum Visibility       { Members, Public, Nobody }
15     public static enum Action           { Accept, Hold, Reject }
16
17     public Address      address;
18     public Mailbox      pending;
19     public Mailbox      archive;
20     private final long  secret;
21     private List(Address a, Mailbox p, Mailbox ar, long s) {this.address=a;this.pending=p;this.archive=ar;this.secret=s;}
22
23     public Hashtable subscribers = new Hashtable();
24     public Filter[]     filters  = new Filter[0];
25
26     public String       homepage             = "";
27     public String       one_line_description = "";
28     public String       long_description     = "";
29     public String       message_footer       = "";
30
31     public Visibility   listVisibility       = Visibility.Nobody;
32     public Visibility   membershipVisibility = Visibility.Nobody;
33     public Visibility   archiveVisibility    = Visibility.Members;
34     public Action       defaultPostingType   = Action.Hold;
35
36     public int          bounceThreshhold     = 10;
37
38     public static List getList(Object all, String listName) { return (List)((Hashtable)all).get(listName); }
39     public synchronized Subscriber getSubscriber(Address subscriber) {
40         Subscriber s = (Subscriber)subscribers.get(subscriber.toString(false));
41         if (s == null) subscribers.put(subscriber, s = new Subscriber());
42         return s;
43     }
44
45     public static class Subscriber implements Serializable {
46         public  Address          address;
47         public  Action           posting;
48         public  UserType         type;
49         public  SubscriptionType subscription;
50         public  boolean          send_copy_of_own_post;
51         public  boolean          filter_duplicates_when_ccd;
52     }
53     
54     //public static class Filter {
55     //    public class EmergencyModerationFilter { }
56     //    public class MaximumLengthFilter { }
57     //    public class SpamFilter { }
58     //    public class MIMETypes { }
59     //    public class MungeReplyTo { }
60     //    public class AnonymizeSender { public boolean uncorrelated; }
61     //}
62
63     // Transactions ///////////////////////////////////////////////////////////////////////////
64
65
66     //////////////////////////////////////////////////////////////////////////////
67
68     public static final String ROOT   = System.getProperty("ibex.mail.list.root", Mailbox.STORAGE_ROOT+File.separatorChar+"lists");
69     public static Prevayler p;
70     static { try { p = PrevaylerFactory.createPrevayler(new Hashtable(), ROOT); }
71     catch (Exception e) { Log.error(List.class, e); } }
72
73     public static Transaction subscribeNewUser(final Address user, final String list) {
74         return new Transaction() { public void executeOn(final Object o, final Date now) {        
75             try {
76                 new AlterSubscription(user,
77                                       now.getTime() + 1000*60*60*24,
78                                       list,
79                                       SubscriptionType.All).signAndSend(getList(o, list).secret);
80             } catch (Exception e) {
81                 Log.error(List.class, e);
82             }
83         } }; }
84
85     public static Transaction create(final Address address, final Mailbox pending, final Mailbox archive) {
86         final long random = new Random().nextLong();
87         return new Transaction() { public void executeOn(Object all, Date now) {
88             ((Hashtable)all).put(address.toString(false), new List(address, pending, archive, random)); } };
89     }
90
91     public static Transaction delete(final Address address) {
92         return new Transaction() { public void executeOn(Object o,Date now) {
93             ((Hashtable)o).remove(address.toString(false)); } }; }
94
95     public static Query all() { return new Query() { public Object query(Object o, Date now) {
96         Hashtable all = (Hashtable)o;
97         List[] ret = new List[all.size()];
98         Enumeration e = all.elements();
99         for(int i=0; i<ret.length; i++) ret[i] = (List)e.nextElement();
100         return ret;
101     } }; }
102
103     public static Query forAddress(final Address a) { return new Query() {
104             public Object query(Object o, Date now) {
105                 return ((Hashtable)o).get(a.toString(false)); } }; }
106
107     public static class AlterSubscription extends Confirmation {
108         public transient SubscriptionType newType = SubscriptionType.All;
109         public String list;
110         protected AlterSubscription(Address who, long expiration, String list, SubscriptionType newType) {
111             super(who, expiration); this.newType = newType; this.list = list; }
112         public String getDescription() { return "change your subscription"; }
113         public void executeOn(Object all, Date now) { getList(all, list).getSubscriber(who).subscription = newType; }
114     }
115
116 }