f5fade4ef402c625d652997aaba8c468e18f7e71
[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 // FEATURE: umbrella structure to mailing lists
16 public class MailingList extends Target implements Serializable {
17
18     public static enum UserType         { Administrator, Moderator, Member }
19     public static enum SubscriptionType { All, None, Digest, MimeDigest }
20     public static enum Visibility       { Members, Public, Nobody }
21     public static enum Action           { Accept, Hold, Reject }
22
23     public Address      address;
24     public Mailbox      archive;
25     private final long  secret;
26     private MailingList(Address a, Mailbox ar, long s) { this.address=a; this.archive=ar; this.secret=s; }
27
28     public Hashtable    subscribers = new Hashtable();
29     public Filter[]     filters  = new Filter[0];
30
31     public String       homepage             = "";
32     public String       one_line_description = "";
33     public String       long_description     = "";
34     public String       message_footer       = "";
35
36     public Visibility   listVisibility       = Visibility.Nobody;
37     public Visibility   membershipVisibility = Visibility.Nobody;
38     public Visibility   archiveVisibility    = Visibility.Members;
39     public Action       defaultPostingType   = Action.Hold;
40
41     public int          bounceThreshhold     = 10;
42
43     public static MailingList getList(Object all, String listName) { return (MailingList)((Hashtable)all).get(listName); }
44     public static MailingList getList(final String listName) {
45         try {
46             return (MailingList)p.execute(new Query() { public Object query(Object o, Date now) { return getList(o, listName); } });
47         } catch (Exception e) {
48             Log.error(MailingList.class, e);
49             return null;
50         }
51     }
52
53     public synchronized Subscriber getSubscriber(Address subscriber) {
54         Subscriber s = (Subscriber)subscribers.get(subscriber.toString(false));
55         if (s == null) subscribers.put(subscriber, s = new Subscriber());
56         return s;
57     }
58
59     public static class Subscriber implements Serializable {
60         public  Address          address;
61         public  Action           posting;
62         public  UserType         type;
63         public  SubscriptionType subscription;
64         public  boolean          send_copy_of_own_post;
65         public  boolean          filter_duplicates_when_ccd;
66     }
67     
68     //public static class Filter {
69     //    public class EmergencyModerationFilter { }
70     //    public class MaximumLengthFilter { }
71     //    public class SpamFilter { }
72     //    public class MIMETypes { }
73     //    public class MungeReplyTo { }
74     //    public class AnonymizeSender { public boolean uncorrelated; }
75     //}
76
77
78     public void accept(Message m) throws IOException, MailException {
79         try {
80             m = Message.newMessage(new Fountain.StringFountain("List-Id: " + one_line_description + "<"+address+">\r\n" +
81                                        m.toString() +
82                                        "--\r\n" +
83                                        message_footer + "\r\n" +
84                                        "to unsubscribe, go to " + homepage + "\r\n"));
85         } catch (Exception e2) {
86             Log.error("[list]", e2);
87             throw new IOException(e2.toString());
88         }
89         Log.warn(MailingList.class, "got message " + m.subject);
90         archive.accept(m);
91         try {
92             String[] subscribers = (String[])p.execute(subscribers());
93             Log.warn("**", "length is " + subscribers.length);
94             for(int i=0; i<subscribers.length; i++) {
95                 String s = subscribers[i];
96                 try {
97                     Log.warn(MailingList.class, "  trying " + s);
98                     /* FIXME
99                     SMTP.Outgoing.accept(Message.newMessage(new Fountain.StringFountain(m.toString()),
100                                                             address, Address.parse(s)));
101                     */
102                     Log.warn("[list]", "successfully sent to " + s);
103                 } catch (Exception e2) {
104                     Log.error("[list]", e2);
105                 }
106             }
107         } catch (Exception e2) {
108             Log.error("[list]", e2);
109         }
110     }
111
112
113     // Transactions ///////////////////////////////////////////////////////////////////////////
114
115
116     //////////////////////////////////////////////////////////////////////////////
117
118     public static final String ROOT   = System.getProperty("ibex.mail.list.root", Mailbox.STORAGE_ROOT+File.separatorChar+"lists");
119     public static Prevayler p;
120     static { try {
121         PrevaylerFactory pf = new PrevaylerFactory();
122         //pf.configureSnapshotManager(new org.prevayler.implementation.snapshot.XmlSnapshotManager(new Hashtable(), ROOT));
123         pf.configurePrevalenceBase(ROOT);
124         p = pf.create();
125     } catch (Exception e) { Log.error(MailingList.class, e); } }
126
127     public static Transaction subscribeNewUser(final Address user, final String list) {
128         return new Transaction() { public void executeOn(final Object o, final Date now) {        
129             try {
130                 new AlterSubscription(user,
131                                       now.getTime() + 1000*60*60*24,
132                                       list,
133                                       SubscriptionType.All).signAndSend(getList(o, list).secret, now);
134             } catch (Exception e) {
135                 Log.error(MailingList.class, e);
136             }
137         } }; }
138
139     /*
140     static {
141         try {
142             if (getList("test") == null) {
143                 Mailbox archive = FileBasedMailbox.getFileBasedMailbox("/var/org.ibex.mail/lists/test@testing.megacz.com", true);
144                 p.execute(create(Address.parse("test@testing.megacz.com"), archive));
145                 p.execute(new Transaction() { public void executeOn(Object all, Date now) {
146                     getList(all, "test@testing.megacz.com").getSubscriber(Address.parse("megacz@gmail.com")).subscription = SubscriptionType.All;
147                 }});
148             }
149         } catch (Exception e) {
150             Log.error(List.class, e);
151         }
152     }
153     */
154
155     public static Transaction create(final Address address, final Mailbox archive) {
156         final long random = new Random().nextLong();
157         return new Transaction() { public void executeOn(Object all, Date now) {
158             ((Hashtable)all).put(address.toString(false), new MailingList(address, archive, random)); } };
159     }
160
161     public static Transaction delete(final Address address) {
162         return new Transaction() { public void executeOn(Object o,Date now) {
163             ((Hashtable)o).remove(address.toString(false)); } }; }
164
165     public static Query all() { return new Query() { public Object query(Object o, Date now) {
166         Hashtable all = (Hashtable)o;
167         MailingList[] ret = new MailingList[all.size()];
168         java.util.Enumeration e = all.elements();
169         for(int i=0; i<ret.length; i++) ret[i] = (MailingList)e.nextElement();
170         return ret;
171     } }; }
172
173     public Query subscribers() { return new Query() { public Object query(Object o, Date now) {
174         Hashtable all = (Hashtable)o;
175         String[] ret = new String[subscribers.size()];
176         java.util.Enumeration e = subscribers.keys();
177         for(int i=0; i<ret.length; i++) ret[i] = e.nextElement().toString();
178         return ret;
179     } }; }
180
181     public static Query forAddress(final Address a) { return new Query() {
182             public Object query(Object o, Date now) {
183                 return ((Hashtable)o).get(a.toString(false)); } }; }
184
185     public static class AlterSubscription extends Confirmation {
186         public transient SubscriptionType newType = SubscriptionType.All;
187         public String list;
188         protected AlterSubscription(Address who, long expiration, String list, SubscriptionType newType) {
189             super(who, expiration); this.newType = newType; this.list = list; }
190         public String getDescription() { return "change your subscription"; }
191         public void executeOn(Object all, Date now) { getList(all, list).getSubscriber(who).subscription = newType; }
192     }
193
194 }