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