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.
6 import org.ibex.util.*;
8 import org.ibex.mail.target.*;
9 import org.ibex.mail.protocol.*;
12 import org.prevayler.*;
13 import org.prevayler.Query;
15 // FEATURE: umbrella structure to mailing lists
16 public class List extends Target implements Serializable {
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 }
23 public Address address;
24 public Mailbox archive;
25 private final long secret;
26 private List(Address a, Mailbox ar, long s) { this.address=a; this.archive=ar; this.secret=s; }
28 public Hashtable subscribers = new Hashtable();
29 public Filter[] filters = new Filter[0];
31 public String homepage = "";
32 public String one_line_description = "";
33 public String long_description = "";
34 public String message_footer = "";
36 public Visibility listVisibility = Visibility.Nobody;
37 public Visibility membershipVisibility = Visibility.Nobody;
38 public Visibility archiveVisibility = Visibility.Members;
39 public Action defaultPostingType = Action.Hold;
41 public int bounceThreshhold = 10;
43 public static List getList(Object all, String listName) { return (List)((Hashtable)all).get(listName); }
44 public static List getList(final String listName) {
46 return (List)p.execute(new Query() { public Object query(Object o, Date now) { return getList(o, listName); } });
47 } catch (Exception e) {
48 Log.error(List.class, e);
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());
59 public static class Subscriber implements Serializable {
60 public Address address;
61 public Action posting;
63 public SubscriptionType subscription;
64 public boolean send_copy_of_own_post;
65 public boolean filter_duplicates_when_ccd;
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; }
78 public void accept(Message m) throws IOException, MailException {
80 m = Message.newMessage(new Stream("List-Id: " + one_line_description + "<"+address+">\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());
89 Log.warn(List.class, "got message " + m.subject);
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];
97 Log.warn(List.class, " trying " + s);
98 SMTP.Outgoing.accept(Message.newMessage(new Stream(m.toString()), address, Address.parse(s)));
99 Log.warn("[list]", "successfully sent to " + s);
100 } catch (Exception e2) {
101 Log.error("[list]", e2);
104 } catch (Exception e2) {
105 Log.error("[list]", e2);
110 // Transactions ///////////////////////////////////////////////////////////////////////////
113 //////////////////////////////////////////////////////////////////////////////
115 public static final String ROOT = System.getProperty("ibex.mail.list.root", Mailbox.STORAGE_ROOT+File.separatorChar+"lists");
116 public static Prevayler p;
117 static { try { p = PrevaylerFactory.createPrevayler(new Hashtable(), ROOT); }
118 catch (Exception e) { Log.error(List.class, e); } }
120 public static Transaction subscribeNewUser(final Address user, final String list) {
121 return new Transaction() { public void executeOn(final Object o, final Date now) {
123 new AlterSubscription(user,
124 now.getTime() + 1000*60*60*24,
126 SubscriptionType.All).signAndSend(getList(o, list).secret, now);
127 } catch (Exception e) {
128 Log.error(List.class, e);
135 if (getList("test") == null) {
136 Mailbox archive = FileBasedMailbox.getFileBasedMailbox("/var/org.ibex.mail/lists/test@testing.megacz.com", true);
137 p.execute(create(Address.parse("test@testing.megacz.com"), archive));
138 p.execute(new Transaction() { public void executeOn(Object all, Date now) {
139 getList(all, "test@testing.megacz.com").getSubscriber(Address.parse("megacz@gmail.com")).subscription = SubscriptionType.All;
142 } catch (Exception e) {
143 Log.error(List.class, e);
148 public static Transaction create(final Address address, final Mailbox archive) {
149 final long random = new Random().nextLong();
150 return new Transaction() { public void executeOn(Object all, Date now) {
151 ((Hashtable)all).put(address.toString(false), new List(address, archive, random)); } };
154 public static Transaction delete(final Address address) {
155 return new Transaction() { public void executeOn(Object o,Date now) {
156 ((Hashtable)o).remove(address.toString(false)); } }; }
158 public static Query all() { return new Query() { public Object query(Object o, Date now) {
159 Hashtable all = (Hashtable)o;
160 List[] ret = new List[all.size()];
161 Enumeration e = all.elements();
162 for(int i=0; i<ret.length; i++) ret[i] = (List)e.nextElement();
166 public Query subscribers() { return new Query() { public Object query(Object o, Date now) {
167 Hashtable all = (Hashtable)o;
168 String[] ret = new String[subscribers.size()];
169 Enumeration e = subscribers.keys();
170 for(int i=0; i<ret.length; i++) ret[i] = e.nextElement().toString();
174 public static Query forAddress(final Address a) { return new Query() {
175 public Object query(Object o, Date now) {
176 return ((Hashtable)o).get(a.toString(false)); } }; }
178 public static class AlterSubscription extends Confirmation {
179 public transient SubscriptionType newType = SubscriptionType.All;
181 protected AlterSubscription(Address who, long expiration, String list, SubscriptionType newType) {
182 super(who, expiration); this.newType = newType; this.list = list; }
183 public String getDescription() { return "change your subscription"; }
184 public void executeOn(Object all, Date now) { getList(all, list).getSubscriber(who).subscription = newType; }