List can now send messages
[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 = new Message(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                             null);
82         } catch (Exception e2) {
83             Log.error("[list]", e2);
84             throw new IOException(e2.toString());
85         }
86         Log.warn(List.class, "got message " + m.subject);
87         archive.accept(m);
88         try {
89             String[] subscribers = (String[])p.execute(subscribers());
90             Log.warn("**", "length is " + subscribers.length);
91             for(int i=0; i<subscribers.length; i++) {
92                 String s = subscribers[i];
93                 try {
94                     Log.warn(List.class, "  trying " + s);
95                     SMTP.Outgoing.accept(new Message(new Stream(m.toString()), new Message.Envelope(address, Address.parse(s), new Date())));
96                     Log.warn("[list]", "successfully sent to " + s);
97                 } catch (Exception e2) {
98                     Log.error("[list]", e2);
99                 }
100             }
101         } catch (Exception e2) {
102             Log.error("[list]", e2);
103         }
104     }
105
106
107     // Transactions ///////////////////////////////////////////////////////////////////////////
108
109
110     //////////////////////////////////////////////////////////////////////////////
111
112     public static final String ROOT   = System.getProperty("ibex.mail.list.root", Mailbox.STORAGE_ROOT+File.separatorChar+"lists");
113     public static Prevayler p;
114     static { try { p = PrevaylerFactory.createPrevayler(new Hashtable(), ROOT); }
115     catch (Exception e) { Log.error(List.class, e); } }
116
117     public static Transaction subscribeNewUser(final Address user, final String list) {
118         return new Transaction() { public void executeOn(final Object o, final Date now) {        
119             try {
120                 new AlterSubscription(user,
121                                       now.getTime() + 1000*60*60*24,
122                                       list,
123                                       SubscriptionType.All).signAndSend(getList(o, list).secret, now);
124             } catch (Exception e) {
125                 Log.error(List.class, e);
126             }
127         } }; }
128
129     /*
130     static {
131         try {
132             if (getList("test") == null) {
133                 Mailbox archive = FileBasedMailbox.getFileBasedMailbox("/var/org.ibex.mail/lists/test@testing.megacz.com", true);
134                 p.execute(create(Address.parse("test@testing.megacz.com"), archive));
135                 p.execute(new Transaction() { public void executeOn(Object all, Date now) {
136                     getList(all, "test@testing.megacz.com").getSubscriber(Address.parse("megacz@gmail.com")).subscription = SubscriptionType.All;
137                 }});
138             }
139         } catch (Exception e) {
140             Log.error(List.class, e);
141         }
142     }
143     */
144
145     public static Transaction create(final Address address, final Mailbox archive) {
146         final long random = new Random().nextLong();
147         return new Transaction() { public void executeOn(Object all, Date now) {
148             ((Hashtable)all).put(address.toString(false), new List(address, archive, random)); } };
149     }
150
151     public static Transaction delete(final Address address) {
152         return new Transaction() { public void executeOn(Object o,Date now) {
153             ((Hashtable)o).remove(address.toString(false)); } }; }
154
155     public static Query all() { return new Query() { public Object query(Object o, Date now) {
156         Hashtable all = (Hashtable)o;
157         List[] ret = new List[all.size()];
158         Enumeration e = all.elements();
159         for(int i=0; i<ret.length; i++) ret[i] = (List)e.nextElement();
160         return ret;
161     } }; }
162
163     public Query subscribers() { return new Query() { public Object query(Object o, Date now) {
164         Hashtable all = (Hashtable)o;
165         String[] ret = new String[subscribers.size()];
166         Enumeration e = subscribers.keys();
167         for(int i=0; i<ret.length; i++) ret[i] = e.nextElement().toString();
168         return ret;
169     } }; }
170
171     public static Query forAddress(final Address a) { return new Query() {
172             public Object query(Object o, Date now) {
173                 return ((Hashtable)o).get(a.toString(false)); } }; }
174
175     public static class AlterSubscription extends Confirmation {
176         public transient SubscriptionType newType = SubscriptionType.All;
177         public String list;
178         protected AlterSubscription(Address who, long expiration, String list, SubscriptionType newType) {
179             super(who, expiration); this.newType = newType; this.list = list; }
180         public String getDescription() { return "change your subscription"; }
181         public void executeOn(Object all, Date now) { getList(all, list).getSubscriber(who).subscription = newType; }
182     }
183
184 }