2 import org.ibex.util.*;
4 import org.ibex.mail.target.*;
5 import org.ibex.mail.protocol.*;
8 import org.prevayler.*;
9 import org.prevayler.Query;
11 // FEATURE: umbrella structure to mailing lists
12 public class List extends Target implements Serializable {
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 }
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; }
24 public Hashtable subscribers = new Hashtable();
25 public Filter[] filters = new Filter[0];
27 public String homepage = "";
28 public String one_line_description = "";
29 public String long_description = "";
30 public String message_footer = "";
32 public Visibility listVisibility = Visibility.Nobody;
33 public Visibility membershipVisibility = Visibility.Nobody;
34 public Visibility archiveVisibility = Visibility.Members;
35 public Action defaultPostingType = Action.Hold;
37 public int bounceThreshhold = 10;
39 public static List getList(Object all, String listName) { return (List)((Hashtable)all).get(listName); }
40 public static List getList(final String listName) {
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);
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());
55 public static class Subscriber implements Serializable {
56 public Address address;
57 public Action posting;
59 public SubscriptionType subscription;
60 public boolean send_copy_of_own_post;
61 public boolean filter_duplicates_when_ccd;
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; }
74 public void accept(Message m) throws IOException, MailException {
76 m = new Message(new Stream("List-Id: " + one_line_description + "<"+address+">\r\n" +
79 message_footer + "\r\n" +
80 "to unsubscribe, go to " + homepage + "\r\n"),
82 } catch (Exception e2) {
83 Log.error("[list]", e2);
84 throw new IOException(e2.toString());
86 Log.warn(List.class, "got message " + m.subject);
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];
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);
101 } catch (Exception e2) {
102 Log.error("[list]", e2);
107 // Transactions ///////////////////////////////////////////////////////////////////////////
110 //////////////////////////////////////////////////////////////////////////////
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); } }
117 public static Transaction subscribeNewUser(final Address user, final String list) {
118 return new Transaction() { public void executeOn(final Object o, final Date now) {
120 new AlterSubscription(user,
121 now.getTime() + 1000*60*60*24,
123 SubscriptionType.All).signAndSend(getList(o, list).secret, now);
124 } catch (Exception e) {
125 Log.error(List.class, e);
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;
139 } catch (Exception e) {
140 Log.error(List.class, e);
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)); } };
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)); } }; }
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();
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();
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)); } }; }
175 public static class AlterSubscription extends Confirmation {
176 public transient SubscriptionType newType = SubscriptionType.All;
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; }