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
45
46
47
48
49 public class RejectAction implements MailAction {
50
51 public void execute(Action action, Mail mail, ActionContext context)
52 throws MessagingException {
53 if (action instanceof ActionReject) {
54 final ActionReject actionReject = (ActionReject) action;
55 execute(actionReject, mail, context);
56 }
57
58 }
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 public void execute(ActionReject anAction, Mail aMail, ActionContext context) throws MessagingException
75 {
76 ActionUtils.detectAndHandleLocalLooping(aMail, context, "reject");
77
78
79 StringBuilder humanText = new StringBuilder(128);
80 humanText.append("This message was refused by the recipient's mail filtering program.");
81 humanText.append("\r\n");
82 humanText.append("The reason given was:");
83 humanText.append("\r\n");
84 humanText.append("\r\n");
85 humanText.append(anAction.getMessage());
86
87 String reporting_UA_name = null;
88 try
89 {
90 reporting_UA_name = InetAddress.getLocalHost()
91 .getCanonicalHostName();
92 }
93 catch (UnknownHostException ex)
94 {
95 reporting_UA_name = "localhost";
96 }
97
98 String reporting_UA_product = context.getServerInfo();
99
100 String[] originalRecipients = aMail.getMessage().getHeader(
101 "Original-Recipient");
102 String original_recipient = null;
103 if (null != originalRecipients && originalRecipients.length > 0)
104 {
105 original_recipient = originalRecipients[0];
106 }
107
108 MailAddress soleRecipient = ActionUtils.getSoleRecipient(aMail);
109 String final_recipient = soleRecipient.toString();
110
111 String original_message_id = aMail.getMessage().getMessageID();
112
113 DispositionModifier modifiers[] = {new ModifierError()};
114 Disposition disposition = new Disposition(new ActionModeAutomatic(),
115 new SendingModeAutomatic(), new TypeDeleted(), modifiers);
116
117 MimeMultipart multiPart = MDNFactory.create(humanText.toString(),
118 reporting_UA_name, reporting_UA_product, original_recipient,
119 final_recipient, original_message_id, disposition);
120
121
122 MimeMessage reply = (MimeMessage) aMail.getMessage().reply(false);
123 reply.setFrom(soleRecipient.toInternetAddress());
124 reply.setContent(multiPart);
125 reply.saveChanges();
126 Address[] recipientAddresses = reply.getAllRecipients();
127 if (null != recipientAddresses)
128 {
129 Collection<MailAddress> recipients = new ArrayList<MailAddress>(recipientAddresses.length);
130 for (int i = 0; i < recipientAddresses.length; i++)
131 {
132 recipients.add(new MailAddress(
133 (InternetAddress) recipientAddresses[i]));
134 }
135 context.post(null, recipients, reply);
136 }
137 else
138 {
139 context.getLog().info("Unable to send reject MDN. Could not determine the recipient.");
140 }
141
142 aMail.setState(Mail.GHOST);
143 }
144
145 }