package org.ibex.mail;
import org.ibex.util.*;
+import org.ibex.io.*;
import org.ibex.mail.target.*;
+import org.ibex.mail.protocol.*;
import java.util.*;
import java.io.*;
-//import org.prevayler.*;
-//import org.prevayler.Query;
+import org.prevayler.*;
+import org.prevayler.Query;
// FEATURE: umbrella structure to mailing lists
-public class List implements Serializable {
+public class List extends Target implements Serializable {
- // disabled until we figure out how to cope with the Prevayler dependency
- /*
public static enum UserType { Administrator, Moderator, Member }
public static enum SubscriptionType { All, None, Digest, MimeDigest }
public static enum Visibility { Members, Public, Nobody }
public static enum Action { Accept, Hold, Reject }
public Address address;
- public Mailbox pending;
public Mailbox archive;
private final long secret;
- private List(Address a, Mailbox p, Mailbox ar, long s) {this.address=a;this.pending=p;this.archive=ar;this.secret=s;}
+ private List(Address a, Mailbox ar, long s) { this.address=a; this.archive=ar; this.secret=s; }
public Hashtable subscribers = new Hashtable();
public Filter[] filters = new Filter[0];
public int bounceThreshhold = 10;
public static List getList(Object all, String listName) { return (List)((Hashtable)all).get(listName); }
+ public static List getList(final String listName) {
+ try {
+ return (List)p.execute(new Query() { public Object query(Object o, Date now) { return getList(o, listName); } });
+ } catch (Exception e) {
+ Log.error(List.class, e);
+ return null;
+ }
+ }
+
public synchronized Subscriber getSubscriber(Address subscriber) {
Subscriber s = (Subscriber)subscribers.get(subscriber.toString(false));
if (s == null) subscribers.put(subscriber, s = new Subscriber());
// public class AnonymizeSender { public boolean uncorrelated; }
//}
+
+ public void accept(Message m) throws IOException, MailException {
+ try {
+ m = new Message(new Stream("List-Id: " + one_line_description + "<"+address+">\r\n" +
+ m.toString() +
+ "--\r\n" +
+ message_footer + "\r\n" +
+ "to unsubscribe, go to " + homepage + "\r\n"),
+ null);
+ } catch (Exception e2) {
+ Log.error("[list]", e2);
+ throw new IOException(e2.toString());
+ }
+ Log.warn(List.class, "got message " + m.subject);
+ archive.accept(m);
+ try {
+ String[] subscribers = (String[])p.execute(subscribers());
+ Log.warn("**", "length is " + subscribers.length);
+ for(int i=0; i<subscribers.length; i++) {
+ String s = subscribers[i];
+ try {
+ Log.warn(List.class, " trying " + s);
+ SMTP.Outgoing.accept(new Message(new Stream(m.toString()), new Message.Envelope(address, Address.parse(s), new Date())));
+ Log.warn("[list]", "successfully sent to " + s);
+ } catch (Exception e2) {
+ Log.error("[list]", e2);
+ }
+ }
+ } catch (Exception e2) {
+ Log.error("[list]", e2);
+ }
+ }
+
+
// Transactions ///////////////////////////////////////////////////////////////////////////
new AlterSubscription(user,
now.getTime() + 1000*60*60*24,
list,
- SubscriptionType.All).signAndSend(getList(o, list).secret);
+ SubscriptionType.All).signAndSend(getList(o, list).secret, now);
} catch (Exception e) {
Log.error(List.class, e);
}
} }; }
- public static Transaction create(final Address address, final Mailbox pending, final Mailbox archive) {
+ /*
+ static {
+ try {
+ if (getList("test") == null) {
+ Mailbox archive = FileBasedMailbox.getFileBasedMailbox("/var/org.ibex.mail/lists/test@testing.megacz.com", true);
+ p.execute(create(Address.parse("test@testing.megacz.com"), archive));
+ p.execute(new Transaction() { public void executeOn(Object all, Date now) {
+ getList(all, "test@testing.megacz.com").getSubscriber(Address.parse("megacz@gmail.com")).subscription = SubscriptionType.All;
+ }});
+ }
+ } catch (Exception e) {
+ Log.error(List.class, e);
+ }
+ }
+ */
+
+ public static Transaction create(final Address address, final Mailbox archive) {
final long random = new Random().nextLong();
return new Transaction() { public void executeOn(Object all, Date now) {
- ((Hashtable)all).put(address.toString(false), new List(address, pending, archive, random)); } };
+ ((Hashtable)all).put(address.toString(false), new List(address, archive, random)); } };
}
public static Transaction delete(final Address address) {
return ret;
} }; }
+ public Query subscribers() { return new Query() { public Object query(Object o, Date now) {
+ Hashtable all = (Hashtable)o;
+ String[] ret = new String[subscribers.size()];
+ Enumeration e = subscribers.keys();
+ for(int i=0; i<ret.length; i++) ret[i] = e.nextElement().toString();
+ return ret;
+ } }; }
+
public static Query forAddress(final Address a) { return new Query() {
public Object query(Object o, Date now) {
return ((Hashtable)o).get(a.toString(false)); } }; }
public String getDescription() { return "change your subscription"; }
public void executeOn(Object all, Date now) { getList(all, list).getSubscriber(who).subscription = newType; }
}
- */
+
}