2702de73b01841b7fe9bc039a35ed555fc356f30
[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 { p = PrevaylerFactory.createPrevayler(new Hashtable(), ROOT); }
121     catch (Exception e) { Log.error(MailingList.class, e); } }
122
123     public static Transaction subscribeNewUser(final Address user, final String list) {
124         return new Transaction() { public void executeOn(final Object o, final Date now) {        
125             try {
126                 new AlterSubscription(user,
127                                       now.getTime() + 1000*60*60*24,
128                                       list,
129                                       SubscriptionType.All).signAndSend(getList(o, list).secret, now);
130             } catch (Exception e) {
131                 Log.error(MailingList.class, e);
132             }
133         } }; }
134
135     /*
136     static {
137         try {
138             if (getList("test") == null) {
139                 Mailbox archive = FileBasedMailbox.getFileBasedMailbox("/var/org.ibex.mail/lists/test@testing.megacz.com", true);
140                 p.execute(create(Address.parse("test@testing.megacz.com"), archive));
141                 p.execute(new Transaction() { public void executeOn(Object all, Date now) {
142                     getList(all, "test@testing.megacz.com").getSubscriber(Address.parse("megacz@gmail.com")).subscription = SubscriptionType.All;
143                 }});
144             }
145         } catch (Exception e) {
146             Log.error(List.class, e);
147         }
148     }
149     */
150
151     public static Transaction create(final Address address, final Mailbox archive) {
152         final long random = new Random().nextLong();
153         return new Transaction() { public void executeOn(Object all, Date now) {
154             ((Hashtable)all).put(address.toString(false), new MailingList(address, archive, random)); } };
155     }
156
157     public static Transaction delete(final Address address) {
158         return new Transaction() { public void executeOn(Object o,Date now) {
159             ((Hashtable)o).remove(address.toString(false)); } }; }
160
161     public static Query all() { return new Query() { public Object query(Object o, Date now) {
162         Hashtable all = (Hashtable)o;
163         MailingList[] ret = new MailingList[all.size()];
164         java.util.Enumeration e = all.elements();
165         for(int i=0; i<ret.length; i++) ret[i] = (MailingList)e.nextElement();
166         return ret;
167     } }; }
168
169     public Query subscribers() { return new Query() { public Object query(Object o, Date now) {
170         Hashtable all = (Hashtable)o;
171         String[] ret = new String[subscribers.size()];
172         java.util.Enumeration e = subscribers.keys();
173         for(int i=0; i<ret.length; i++) ret[i] = e.nextElement().toString();
174         return ret;
175     } }; }
176
177     public static Query forAddress(final Address a) { return new Query() {
178             public Object query(Object o, Date now) {
179                 return ((Hashtable)o).get(a.toString(false)); } }; }
180
181     public static class AlterSubscription extends Confirmation {
182         public transient SubscriptionType newType = SubscriptionType.All;
183         public String list;
184         protected AlterSubscription(Address who, long expiration, String list, SubscriptionType newType) {
185             super(who, expiration); this.newType = newType; this.list = list; }
186         public String getDescription() { return "change your subscription"; }
187         public void executeOn(Object all, Date now) { getList(all, list).getSubscriber(who).subscription = newType; }
188     }
189
190 }