1 /************************************************************************
2 * Copyright (c) 2000-2006 The Apache Software Foundation. *
3 * All rights reserved. *
4 * ------------------------------------------------------------------- *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you *
6 * may not use this file except in compliance with the License. You *
7 * may obtain a copy of the License at: *
8 * *
9 * http://www.apache.org/licenses/LICENSE-2.0 *
10 * *
11 * Unless required by applicable law or agreed to in writing, software *
12 * distributed under the License is distributed on an "AS IS" BASIS, *
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14 * implied. See the License for the specific language governing *
15 * permissions and limitations under the License. *
16 ***********************************************************************/
17
18 package org.apache.james.transport.mailets;
19
20 import org.apache.mailet.GenericMailet;
21 import org.apache.mailet.Mail;
22 import org.apache.mailet.MailAddress;
23
24 import javax.mail.MessagingException;
25
26 /***
27 * An abstract implementation of a listserv manager. This mailet reads the
28 * address to find the command.
29 */
30 public abstract class GenericListservManager extends GenericMailet {
31
32 /***
33 * Adds an address to the listserv. Returns whether command was
34 * successful.
35 */
36 public abstract boolean addAddress(MailAddress address);
37
38 /***
39 * Removes an address from the listserv. Returns whether command
40 * was successful.
41 */
42 public abstract boolean removeAddress(MailAddress address);
43
44
45 /***
46 * Indicates whether an address already exists on the listserv. Returns
47 * whether the address exists.
48 */
49 public abstract boolean existsAddress(MailAddress address);
50
51 /***
52 * Processes the message. Checks which command was sent based on the
53 * recipient address, and does the appropriate action.
54 */
55 public final void service(Mail mail) throws MessagingException {
56 if (mail.getRecipients().size() != 1) {
57 getMailetContext().bounce(mail, "You can only send one command at a time to this listserv manager.");
58 return;
59 }
60 MailAddress address = (MailAddress)mail.getRecipients().iterator().next();
61 if (address.getUser().endsWith("-off")) {
62 if (existsAddress(mail.getSender())) {
63 if (removeAddress(mail.getSender())) {
64 getMailetContext().bounce(mail, "Successfully removed from listserv.");
65 } else {
66 getMailetContext().bounce(mail, "Unable to remove you from listserv for some reason.");
67 }
68 } else {
69 getMailetContext().bounce(mail, "You are not subscribed to this listserv.");
70 }
71 } else if (address.getUser().endsWith("-on")) {
72 if (existsAddress(mail.getSender())) {
73 getMailetContext().bounce(mail, "You are already subscribed to this listserv.");
74 } else {
75 if (addAddress(mail.getSender())) {
76 getMailetContext().bounce(mail, "Successfully added to listserv.");
77 } else {
78 getMailetContext().bounce(mail, "Unable to add you to the listserv for some reason");
79 }
80 }
81 } else {
82 getMailetContext().bounce(mail, "Could not understand the command you sent to this listserv manager.\r\n"
83 + "Valid commands are <listserv>-on@domain.com and <listserv>-off@domain.com");
84 }
85
86 mail.setState(Mail.GHOST);
87 }
88 }