3 * see AUTHORS for the list of contributors
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package org.sonews.mlgw;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26 import javax.mail.Address;
27 import javax.mail.Authenticator;
28 import javax.mail.Message;
29 import javax.mail.MessagingException;
30 import javax.mail.PasswordAuthentication;
31 import javax.mail.internet.InternetAddress;
32 import org.sonews.config.Config;
33 import org.sonews.storage.Article;
34 import org.sonews.storage.Group;
35 import org.sonews.storage.Headers;
36 import org.sonews.storage.StorageBackendException;
37 import org.sonews.storage.StorageManager;
38 import org.sonews.util.Log;
39 import org.sonews.util.Stats;
42 * Dispatches messages from mailing list to newsserver or vice versa.
43 * @author Christian Lins
46 public class Dispatcher
49 static class PasswordAuthenticator extends Authenticator
53 public PasswordAuthentication getPasswordAuthentication()
55 final String username =
56 Config.inst().get(Config.MLSEND_USER, "user");
57 final String password =
58 Config.inst().get(Config.MLSEND_PASSWORD, "mysecret");
60 return new PasswordAuthentication(username, password);
66 * Chunks out the email address of the full List-Post header field.
67 * @param listPostValue
68 * @return The matching email address or null
70 private static String chunkListPost(String listPostValue)
72 // listPostValue is of form "<mailto:dev@openoffice.org>"
73 Pattern mailPattern = Pattern.compile("(\\w+[-|.])*\\w+@(\\w+.)+\\w+");
74 Matcher mailMatcher = mailPattern.matcher(listPostValue);
75 if(mailMatcher.find())
77 return listPostValue.substring(mailMatcher.start(), mailMatcher.end());
86 * This method inspects the header of the given message, trying
87 * to find the most appropriate recipient.
89 * @param fallback If this is false only List-Post and X-List-Post headers
91 * @return null or fitting group name for the given message.
93 private static List<String> getGroupFor(final Message msg, final boolean fallback)
94 throws MessagingException, StorageBackendException
96 List<String> groups = null;
98 // Is there a List-Post header?
99 String[] listPost = msg.getHeader(Headers.LIST_POST);
100 InternetAddress listPostAddr;
102 if(listPost == null || listPost.length == 0 || "".equals(listPost[0]))
104 // Is there a X-List-Post header?
105 listPost = msg.getHeader(Headers.X_LIST_POST);
108 if(listPost != null && listPost.length > 0
109 && !"".equals(listPost[0]) && chunkListPost(listPost[0]) != null)
111 // listPost[0] is of form "<mailto:dev@openoffice.org>"
112 listPost[0] = chunkListPost(listPost[0]);
113 listPostAddr = new InternetAddress(listPost[0], false);
114 groups = StorageManager.current().getGroupsForList(listPostAddr.getAddress());
118 Log.get().info("Using fallback recipient discovery for: " + msg.getSubject());
119 groups = new ArrayList<String>();
120 // Fallback to TO/CC/BCC addresses
121 Address[] to = msg.getAllRecipients();
122 for(Address toa : to) // Address can have '<' '>' around
124 if(toa instanceof InternetAddress)
126 List<String> g = StorageManager.current()
127 .getGroupsForList(((InternetAddress)toa).getAddress());
137 * Posts a message that was received from a mailing list to the
138 * appropriate newsgroup.
139 * If the message already exists in the storage, this message checks
140 * if it must be posted in an additional group. This can happen for
141 * crosspostings in different mailing lists.
144 public static boolean toGroup(final Message msg)
148 // Create new Article object
149 Article article = new Article(msg);
150 boolean posted = false;
152 // Check if this mail is already existing the storage
154 StorageManager.current().isArticleExisting(article.getMessageID());
156 List<String> newsgroups = getGroupFor(msg, !updateReq);
157 List<String> oldgroups = new ArrayList<String>();
160 // Check for duplicate entries of the same group
161 Article oldArticle = StorageManager.current().getArticle(article.getMessageID());
162 List<Group> oldGroups = oldArticle.getGroups();
163 for(Group oldGroup : oldGroups)
165 if(!newsgroups.contains(oldGroup.getName()))
167 oldgroups.add(oldGroup.getName());
172 if(newsgroups.size() > 0)
174 newsgroups.addAll(oldgroups);
175 StringBuilder groups = new StringBuilder();
176 for(int n = 0; n < newsgroups.size(); n++)
178 groups.append(newsgroups.get(n));
179 if (n + 1 != newsgroups.size())
184 Log.get().info("Posting to group " + groups.toString());
186 article.setGroup(groups.toString());
187 //article.removeHeader(Headers.REPLY_TO);
188 //article.removeHeader(Headers.TO);
190 // Write article to database
193 Log.get().info("Updating " + article.getMessageID()
194 + " with additional groups");
195 StorageManager.current().delete(article.getMessageID());
196 StorageManager.current().addArticle(article);
200 Log.get().info("Gatewaying " + article.getMessageID() + " to "
201 + article.getHeader(Headers.NEWSGROUPS)[0]);
202 StorageManager.current().addArticle(article);
203 Stats.getInstance().mailGatewayed(
204 article.getHeader(Headers.NEWSGROUPS)[0]);
210 StringBuilder buf = new StringBuilder();
211 for (Address toa : msg.getAllRecipients())
214 buf.append(toa.toString());
216 buf.append(" " + article.getHeader(Headers.LIST_POST)[0]);
217 Log.get().warning("No group for" + buf.toString());
223 ex.printStackTrace();
229 * Mails a message received through NNTP to the appropriate mailing list.
230 * This method MAY be called several times by PostCommand for the same
233 public static void toList(Article article, String group)
234 throws IOException, MessagingException, StorageBackendException
236 // Get mailing lists for the group of this article
237 List<String> rcptAddresses = StorageManager.current().getListsForGroup(group);
239 if(rcptAddresses == null || rcptAddresses.size() == 0)
241 Log.get().warning("No ML-address for " + group + " found.");
245 for(String rcptAddress : rcptAddresses)
247 // Compose message and send it via given SMTP-Host
248 String smtpHost = Config.inst().get(Config.MLSEND_HOST, "localhost");
249 int smtpPort = Config.inst().get(Config.MLSEND_PORT, 25);
250 String smtpUser = Config.inst().get(Config.MLSEND_USER, "user");
251 String smtpPw = Config.inst().get(Config.MLSEND_PASSWORD, "mysecret");
252 String smtpFrom = Config.inst().get(
253 Config.MLSEND_ADDRESS, article.getHeader(Headers.FROM)[0]);
255 // TODO: Make Article cloneable()
256 article.getMessageID(); // Make sure an ID is existing
257 article.removeHeader(Headers.NEWSGROUPS);
258 article.removeHeader(Headers.PATH);
259 article.removeHeader(Headers.LINES);
260 article.removeHeader(Headers.BYTES);
262 article.setHeader("To", rcptAddress);
263 //article.setHeader("Reply-To", listAddress);
265 if (Config.inst().get(Config.MLSEND_RW_SENDER, false))
267 rewriteSenderAddress(article); // Set the SENDER address
270 SMTPTransport smtpTransport = new SMTPTransport(smtpHost, smtpPort);
271 smtpTransport.send(article, smtpFrom, rcptAddress);
272 smtpTransport.close();
274 Stats.getInstance().mailGatewayed(group);
275 Log.get().info("MLGateway: Mail " + article.getHeader("Subject")[0]
276 + " was delivered to " + rcptAddress + ".");
281 * Sets the SENDER header of the given MimeMessage. This might be necessary
282 * for moderated groups that does not allow the "normal" FROM sender.
284 * @throws javax.mail.MessagingException
286 private static void rewriteSenderAddress(Article msg)
287 throws MessagingException
289 String mlAddress = Config.inst().get(Config.MLSEND_ADDRESS, null);
291 if(mlAddress != null)
293 msg.setHeader(Headers.SENDER, mlAddress);
297 throw new MessagingException("Cannot rewrite SENDER header!");