1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.jsieve.mailet;
20
21 import java.net.InetAddress;
22 import java.net.UnknownHostException;
23 import java.util.ArrayList;
24 import java.util.Collection;
25
26 import javax.mail.Address;
27 import javax.mail.MessagingException;
28 import javax.mail.internet.InternetAddress;
29 import javax.mail.internet.MimeMessage;
30 import javax.mail.internet.MimeMultipart;
31
32 import org.apache.jsieve.mail.Action;
33 import org.apache.jsieve.mail.ActionReject;
34 import org.apache.mailet.Mail;
35 import org.apache.mailet.MailAddress;
36 import org.apache.jsieve.mailet.mdn.ActionModeAutomatic;
37 import org.apache.jsieve.mailet.mdn.Disposition;
38 import org.apache.jsieve.mailet.mdn.DispositionModifier;
39 import org.apache.jsieve.mailet.mdn.MDNFactory;
40 import org.apache.jsieve.mailet.mdn.ModifierError;
41 import org.apache.jsieve.mailet.mdn.SendingModeAutomatic;
42 import org.apache.jsieve.mailet.mdn.TypeDeleted;
43
44 public class RejectAction implements MailAction {
45
46 public void execute(Action action, Mail mail, ActionContext context)
47 throws MessagingException {
48 if (action instanceof ActionReject) {
49 final ActionReject actionReject = (ActionReject) action;
50 execute(actionReject, mail, context);
51 }
52
53 }
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public void execute(ActionReject anAction, Mail aMail, ActionContext context) throws MessagingException
70 {
71 ActionUtils.detectAndHandleLocalLooping(aMail, context, "reject");
72
73
74 StringBuffer humanText = new StringBuffer(128);
75 humanText
76 .append("This message was refused by the recipient's mail filtering program.");
77 humanText.append("\r\n");
78 humanText.append("The reason given was:");
79 humanText.append("\r\n");
80 humanText.append("\r\n");
81 humanText.append(anAction.getMessage());
82
83 String reporting_UA_name = null;
84 try
85 {
86 reporting_UA_name = InetAddress.getLocalHost()
87 .getCanonicalHostName();
88 }
89 catch (UnknownHostException ex)
90 {
91 reporting_UA_name = "localhost";
92 }
93
94 String reporting_UA_product = context.getServerInfo();
95
96 String[] originalRecipients = aMail.getMessage().getHeader(
97 "Original-Recipient");
98 String original_recipient = null;
99 if (null != originalRecipients && originalRecipients.length > 0)
100 {
101 original_recipient = originalRecipients[0];
102 }
103
104 MailAddress soleRecipient = ActionUtils.getSoleRecipient(aMail);
105 String final_recipient = soleRecipient.toString();
106
107 String original_message_id = aMail.getMessage().getMessageID();
108
109 DispositionModifier modifiers[] = {new ModifierError()};
110 Disposition disposition = new Disposition(new ActionModeAutomatic(),
111 new SendingModeAutomatic(), new TypeDeleted(), modifiers);
112
113 MimeMultipart multiPart = MDNFactory.create(humanText.toString(),
114 reporting_UA_name, reporting_UA_product, original_recipient,
115 final_recipient, original_message_id, disposition);
116
117
118 MimeMessage reply = (MimeMessage) aMail.getMessage().reply(false);
119 reply.setFrom(soleRecipient.toInternetAddress());
120 reply.setContent(multiPart);
121 reply.saveChanges();
122 Address[] recipientAddresses = reply.getAllRecipients();
123 if (null != recipientAddresses)
124 {
125 Collection recipients = new ArrayList(recipientAddresses.length);
126 for (int i = 0; i < recipientAddresses.length; i++)
127 {
128 recipients.add(new MailAddress(
129 (InternetAddress) recipientAddresses[i]));
130 }
131 context.post(null, recipients, reply);
132 }
133 else
134 {
135 context.getLog().info("Unable to send reject MDN. Could not determine the recipient.");
136 }
137
138 aMail.setState(Mail.GHOST);
139 }
140
141 }