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 MailingList 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 MailingList(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 MailingList getList(Object all, String listName) { return (MailingList)((Hashtable)all).get(listName); }
44 public static MailingList getList(final String listName) {
46 return (MailingList)p.execute(new Query() { public Object query(Object o, Date now) { return getList(o, listName); } });
47 } catch (Exception e) {
48 Log.error(MailingList.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 Fountain.StringFountain("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(MailingList.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(MailingList.class, " trying " + s);
99 SMTP.Outgoing.accept(Message.newMessage(new Fountain.StringFountain(m.toString()),
100 address, Address.parse(s)));
102 Log.warn("[list]", "successfully sent to " + s);
103 } catch (Exception e2) {
104 Log.error("[list]", e2);
107 } catch (Exception e2) {
108 Log.error("[list]", e2);
113 // Transactions ///////////////////////////////////////////////////////////////////////////
116 //////////////////////////////////////////////////////////////////////////////
118 public static final String ROOT = System.getProperty("ibex.mail.list.root", Mailbox.STORAGE_ROOT+File.separatorChar+"lists");
119 public static Prevayler p;
120 static { try { p = PrevaylerFactory.createPrevayler(new Hashtable(), ROOT); }
121 catch (Exception e) { Log.error(MailingList.class, e); } }
123 public static Transaction subscribeNewUser(final Address user, final String list) {
124 return new Transaction() { public void executeOn(final Object o, final Date now) {
126 new AlterSubscription(user,
127 now.getTime() + 1000*60*60*24,
129 SubscriptionType.All).signAndSend(getList(o, list).secret, now);
130 } catch (Exception e) {
131 Log.error(MailingList.class, e);
138 if (getList("test") == null) {
139 Mailbox archive = FileBasedMailbox.getFileBasedMailbox("/var/org.ibex.mail/lists/test@testing.megacz.com", true);
140 p.execute(create(Address.parse("test@testing.megacz.com"), archive));
141 p.execute(new Transaction() { public void executeOn(Object all, Date now) {
142 getList(all, "test@testing.megacz.com").getSubscriber(Address.parse("megacz@gmail.com")).subscription = SubscriptionType.All;
145 } catch (Exception e) {
146 Log.error(List.class, e);
151 public static Transaction create(final Address address, final Mailbox archive) {
152 final long random = new Random().nextLong();
153 return new Transaction() { public void executeOn(Object all, Date now) {
154 ((Hashtable)all).put(address.toString(false), new MailingList(address, archive, random)); } };
157 public static Transaction delete(final Address address) {
158 return new Transaction() { public void executeOn(Object o,Date now) {
159 ((Hashtable)o).remove(address.toString(false)); } }; }
161 public static Query all() { return new Query() { public Object query(Object o, Date now) {
162 Hashtable all = (Hashtable)o;
163 MailingList[] ret = new MailingList[all.size()];
164 java.util.Enumeration e = all.elements();
165 for(int i=0; i<ret.length; i++) ret[i] = (MailingList)e.nextElement();
169 public Query subscribers() { return new Query() { public Object query(Object o, Date now) {
170 Hashtable all = (Hashtable)o;
171 String[] ret = new String[subscribers.size()];
172 java.util.Enumeration e = subscribers.keys();
173 for(int i=0; i<ret.length; i++) ret[i] = e.nextElement().toString();
177 public static Query forAddress(final Address a) { return new Query() {
178 public Object query(Object o, Date now) {
179 return ((Hashtable)o).get(a.toString(false)); } }; }
181 public static class AlterSubscription extends Confirmation {
182 public transient SubscriptionType newType = SubscriptionType.All;
184 protected AlterSubscription(Address who, long expiration, String list, SubscriptionType newType) {
185 super(who, expiration); this.newType = newType; this.list = list; }
186 public String getDescription() { return "change your subscription"; }
187 public void executeOn(Object all, Date now) { getList(all, list).getSubscriber(who).subscription = newType; }