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 = Message.newMessage(new Stream("List-Id: " + one_line_description + "<"+address+">\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());
85 Log.warn(List.class, "got message " + m.subject);
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];
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);
100 } catch (Exception e2) {
101 Log.error("[list]", e2);
106 // Transactions ///////////////////////////////////////////////////////////////////////////
109 //////////////////////////////////////////////////////////////////////////////
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); } }
116 public static Transaction subscribeNewUser(final Address user, final String list) {
117 return new Transaction() { public void executeOn(final Object o, final Date now) {
119 new AlterSubscription(user,
120 now.getTime() + 1000*60*60*24,
122 SubscriptionType.All).signAndSend(getList(o, list).secret, now);
123 } catch (Exception e) {
124 Log.error(List.class, e);
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;
138 } catch (Exception e) {
139 Log.error(List.class, e);
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)); } };
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)); } }; }
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();
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();
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)); } }; }
174 public static class AlterSubscription extends Confirmation {
175 public transient SubscriptionType newType = SubscriptionType.All;
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; }