franta-hg@72: /* franta-hg@72: * SONEWS News Server franta-hg@72: * see AUTHORS for the list of contributors franta-hg@72: * franta-hg@72: * This program is free software: you can redistribute it and/or modify franta-hg@72: * it under the terms of the GNU General Public License as published by franta-hg@72: * the Free Software Foundation, either version 3 of the License, or franta-hg@72: * (at your option) any later version. franta-hg@72: * franta-hg@72: * This program is distributed in the hope that it will be useful, franta-hg@72: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@72: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@72: * GNU General Public License for more details. franta-hg@72: * franta-hg@72: * You should have received a copy of the GNU General Public License franta-hg@72: * along with this program. If not, see . franta-hg@72: */ franta-hg@72: package org.sonews.storage; franta-hg@72: franta-hg@75: import java.io.BufferedReader; franta-hg@102: import java.io.ByteArrayInputStream; franta-hg@72: import java.io.ByteArrayOutputStream; franta-hg@72: import java.io.IOException; franta-hg@75: import java.io.InputStream; franta-hg@75: import java.io.InputStreamReader; franta-hg@75: import java.io.PrintStream; franta-hg@74: import java.io.StringReader; franta-hg@74: import java.io.StringWriter; franta-hg@72: import java.io.UnsupportedEncodingException; franta-hg@72: import java.sql.ResultSet; franta-hg@72: import java.sql.SQLException; franta-hg@72: import java.util.ArrayList; franta-hg@72: import java.util.Date; franta-hg@72: import java.util.Enumeration; franta-hg@74: import java.util.logging.Level; franta-hg@74: import java.util.logging.Logger; franta-hg@72: import javax.mail.Header; franta-hg@72: import javax.mail.MessagingException; franta-hg@72: import javax.mail.Multipart; franta-hg@72: import javax.mail.Session; franta-hg@72: import javax.mail.internet.InternetAddress; franta-hg@72: import javax.mail.internet.MimeBodyPart; franta-hg@72: import javax.mail.internet.MimeMessage; franta-hg@72: import javax.mail.internet.MimeMultipart; franta-hg@74: import javax.xml.transform.Transformer; franta-hg@74: import javax.xml.transform.TransformerFactory; franta-hg@74: import javax.xml.transform.stream.StreamResult; franta-hg@74: import javax.xml.transform.stream.StreamSource; franta-hg@102: import org.sonews.daemon.NNTPConnection; franta-hg@74: import org.sonews.util.io.Resource; franta-hg@72: franta-hg@72: /** franta-hg@72: * This is MimeMessage which enables custom Message-ID header franta-hg@72: * (this header will not be overwritten by the default one like in MimeMessage). franta-hg@72: * franta-hg@72: * Also add header and body separate serialization. franta-hg@72: * franta-hg@72: * And can be deserialized from SQL ResultSet franta-hg@72: * franta-hg@72: * @author František Kučera (frantovo.cz) franta-hg@72: */ franta-hg@72: public class DrupalMessage extends MimeMessage { franta-hg@72: franta-hg@74: private static final Logger log = Logger.getLogger(DrupalMessage.class.getName()); franta-hg@72: private static final String MESSAGE_ID_HEADER = "Message-ID"; franta-hg@72: private static final String CRLF = "\r\n"; franta-hg@72: public static final String CHARSET = "UTF-8"; franta-hg@72: private static final String XHTML_CONTENT_TYPE = "text/html; charset=" + CHARSET; franta-hg@100: private static final String ZNAKČKA_KONCE_ŘÁDKU = "◆"; franta-hg@72: private String messageID; franta-hg@102: private Long parentID; franta-hg@102: private Long groupID; franta-hg@72: franta-hg@72: /** franta-hg@72: * Constructs MIME message from SQL result. franta-hg@72: * @param rs ResultSet containing message data. No {@link ResultSet#next()} will be called, just values from current row will be read. franta-hg@72: * @param constructBody true if whole message should be constructed | false if we need only message headers (body will be dummy). franta-hg@72: */ franta-hg@72: public DrupalMessage(ResultSet rs, String myDomain, boolean constructBody) throws SQLException, UnsupportedEncodingException, MessagingException { franta-hg@72: super(Session.getDefaultInstance(System.getProperties())); franta-hg@72: franta-hg@102: groupID = rs.getLong("group_id"); franta-hg@102: addHeader("Message-id", constructMessageId(rs.getInt("id"), groupID, rs.getString("group_name"), myDomain)); franta-hg@72: addHeader("Newsgroups", rs.getString("group_name")); franta-hg@74: setFrom(new InternetAddress(rs.getString("sender_email"), rs.getString("sender_name"))); franta-hg@72: setSubject(rs.getString("subject")); franta-hg@72: setSentDate(new Date(rs.getLong("created"))); franta-hg@74: franta-hg@102: parentID = rs.getLong("parent_id"); franta-hg@74: if (parentID > 0) { franta-hg@72: String parentMessageID = constructMessageId(parentID, rs.getInt("group_id"), rs.getString("group_name"), myDomain); franta-hg@72: addHeader("In-Reply-To", parentMessageID); franta-hg@72: addHeader("References", parentMessageID); franta-hg@72: } franta-hg@72: franta-hg@72: if (constructBody) { franta-hg@72: Multipart multipart = new MimeMultipart("alternative"); franta-hg@72: setContent(multipart); franta-hg@72: franta-hg@82: /** XHTML part */ franta-hg@82: MimeBodyPart htmlPart = new MimeBodyPart(); franta-hg@82: String xhtmlText = readXhtmlText(rs); franta-hg@82: htmlPart.setContent(xhtmlText, XHTML_CONTENT_TYPE); franta-hg@84: franta-hg@74: /** Plain text part */ franta-hg@72: MimeBodyPart textPart = new MimeBodyPart(); franta-hg@89: String plainText = readPlainText(rs, xhtmlText); franta-hg@89: textPart.setText(plainText); franta-hg@89: //addHeader("Lines", String.valueOf(plainText.split("\n").length)); franta-hg@87: franta-hg@87: /** franta-hg@87: * Thunderbirdu záleží, v jakém pořadí části jsou franta-hg@87: * (když je prostý text druhý, html se nezobrazí), franta-hg@87: * KNode zobrazuje HTML správně, i když je na prvním místě. franta-hg@87: */ franta-hg@72: multipart.addBodyPart(textPart); franta-hg@87: multipart.addBodyPart(htmlPart); franta-hg@72: } else { franta-hg@82: /** empty body, just headers */ franta-hg@72: setText(""); franta-hg@72: } franta-hg@72: } franta-hg@72: franta-hg@102: /** franta-hg@102: * Constructs MIME message from article posted by user. franta-hg@102: * @param article article that came through NNTP. franta-hg@102: * @throws MessagingException franta-hg@102: */ franta-hg@102: public DrupalMessage(Article article) throws MessagingException { franta-hg@102: super(Session.getDefaultInstance(System.getProperties()), serializeArticle(article)); franta-hg@102: franta-hg@102: String[] parentHeaders = getHeader("In-Reply-To"); franta-hg@102: if (parentHeaders.length == 1) { franta-hg@102: String parentMessageID = parentHeaders[0]; franta-hg@102: parentID = parseArticleID(parentMessageID); franta-hg@102: groupID = parseGroupID(parentMessageID); franta-hg@102: } else { franta-hg@102: throw new MessagingException("Message posted by user must have exactly one In-Reply-To header."); franta-hg@102: } franta-hg@102: } franta-hg@102: franta-hg@102: private static InputStream serializeArticle(Article a) { franta-hg@102: byte articleHeaders[] = a.getHeaderSource().getBytes(); franta-hg@102: byte delimiter[] = (NNTPConnection.NEWLINE + NNTPConnection.NEWLINE).getBytes(); franta-hg@102: byte body[] = a.getBody(); franta-hg@102: franta-hg@102: byte message[] = new byte[articleHeaders.length + delimiter.length + body.length]; franta-hg@102: franta-hg@102: System.arraycopy(articleHeaders, 0, message, 0, articleHeaders.length); franta-hg@102: System.arraycopy(delimiter, 0, message, articleHeaders.length, delimiter.length); franta-hg@102: System.arraycopy(body, 0, message, articleHeaders.length + delimiter.length, body.length); franta-hg@102: franta-hg@102: return new ByteArrayInputStream(message); franta-hg@102: } franta-hg@102: franta-hg@82: private String readPlainText(ResultSet rs, String xhtmlText) { franta-hg@89: try { franta-hg@89: TransformerFactory tf = TransformerFactory.newInstance(); franta-hg@89: Transformer textTransformer = tf.newTransformer(new StreamSource(Resource.getAsStream("helpers/mimeTextPart.xsl"))); franta-hg@89: franta-hg@89: StringReader input = new StringReader(xhtmlText); franta-hg@89: StringWriter output = new StringWriter(xhtmlText.length()); franta-hg@89: textTransformer.transform(new StreamSource(input), new StreamResult(output)); franta-hg@89: franta-hg@89: return output.toString(); franta-hg@89: } catch (Exception e) { franta-hg@89: /** franta-hg@89: * TODO: lepší ošetření chyby franta-hg@89: */ franta-hg@89: log.log(Level.WARNING, "Error while transforming article to plain text", e); franta-hg@89: return makeSimpleXHTML("Při transformaci příspěvku bohužel došlo k chybě."); franta-hg@89: } franta-hg@72: } franta-hg@72: franta-hg@72: private String readXhtmlText(ResultSet rs) { franta-hg@72: /** franta-hg@82: * TODO: franta-hg@82: * - znovupoužívat XSL transformér franta-hg@82: * - používat cache, ukládat si vygenerované články franta-hg@72: */ franta-hg@74: try { franta-hg@84: String inputText = makeSimpleXHTML(rs.getString("text")); franta-hg@75: franta-hg@82: TransformerFactory tf = TransformerFactory.newInstance(); franta-hg@82: Transformer paragraphTransformer = tf.newTransformer(new StreamSource(Resource.getAsStream("helpers/mimeXhtmlPart-make-paragraphs.xsl"))); franta-hg@75: franta-hg@82: String paragraphedText; franta-hg@82: boolean tidyWasUsed = false; franta-hg@82: try { franta-hg@82: StringReader input = new StringReader(inputText); franta-hg@82: StringWriter output = new StringWriter(2 * inputText.length()); franta-hg@82: paragraphTransformer.transform(new StreamSource(input), new StreamResult(output)); franta-hg@82: paragraphedText = output.toString(); franta-hg@82: } catch (Exception e) { franta-hg@82: log.log(Level.FINER, "HTML input was shitty – Tidy had to be called.", e); franta-hg@82: StringReader input = new StringReader(tidyXhtml(inputText)); franta-hg@82: StringWriter output = new StringWriter(2 * inputText.length()); franta-hg@82: paragraphTransformer.transform(new StreamSource(input), new StreamResult(output)); franta-hg@82: paragraphedText = output.toString(); franta-hg@82: tidyWasUsed = true; franta-hg@82: } franta-hg@75: franta-hg@82: Transformer xhtmlTransformer = tf.newTransformer(new StreamSource(Resource.getAsStream("helpers/mimeXhtmlPart.xsl"))); franta-hg@82: xhtmlTransformer.setParameter("isRoot", (rs.getInt("parent_id") == 0)); franta-hg@82: xhtmlTransformer.setParameter("title", rs.getString("subject")); franta-hg@82: xhtmlTransformer.setParameter("urlBase", rs.getString("urlBase")); franta-hg@82: xhtmlTransformer.setParameter("wwwRead", rs.getString("wwwRead")); franta-hg@82: xhtmlTransformer.setParameter("wwwPost", rs.getString("wwwPost")); franta-hg@82: xhtmlTransformer.setParameter("headComment", String.format("Drupal-NNTP bridge. Transformed: %1$tc. Tidy had to be used: %2$b", new Date(), tidyWasUsed)); franta-hg@82: StringReader input = new StringReader(paragraphedText); franta-hg@82: StringWriter output = new StringWriter(2 * paragraphedText.length()); franta-hg@82: xhtmlTransformer.transform(new StreamSource(input), new StreamResult(output)); franta-hg@75: franta-hg@74: return output.toString(); franta-hg@74: } catch (Exception e) { franta-hg@74: /** franta-hg@74: * TODO: lepší ošetření chyby franta-hg@74: */ franta-hg@74: log.log(Level.WARNING, "Error while transforming article to XHTML", e); franta-hg@84: return makeSimpleXHTML("

Při transformaci příspěvku bohužel došlo k chybě.

"); franta-hg@74: } franta-hg@72: } franta-hg@72: franta-hg@84: private static String makeSimpleXHTML(String body) { franta-hg@84: return "" + body + ""; franta-hg@84: } franta-hg@84: franta-hg@75: /** franta-hg@75: * TODO: refaktorovat, přesunout franta-hg@75: */ franta-hg@75: private static String tidyXhtml(String inputText) throws IOException { franta-hg@89: /* franta-hg@89: * Viz https://sourceforge.net/tracker/index.php?func=detail&aid=3424437&group_id=27659&atid=390966 franta-hg@89: * franta-hg@89: * TODO: franta-hg@89: * - použít delší zástupný řetězec, ne jen jeden znak franta-hg@89: * - umísťovat ho jen tam, kde už nějaký text je (ne mezi >\s*<) franta-hg@89: */ franta-hg@100: inputText = označKonceŘádků(inputText); franta-hg@82: franta-hg@75: Runtime r = Runtime.getRuntime(); franta-hg@82: Process p = r.exec(new String[]{"tidy", // http://tidy.sourceforge.net franta-hg@82: "-asxml", // well formed XHTML franta-hg@82: "-numeric", // číselné entity franta-hg@82: "-utf8", // kódování franta-hg@82: "--show-warnings", "false", // žádná varování nás nezajímají franta-hg@82: "--show-errors", "0", // ani chyby franta-hg@82: "--doctype", "omit", // doctype nepotřebujeme (doplníme si případně vlastní v XSLT) franta-hg@82: "--logical-emphasis", "true", // em a strong místo i a b franta-hg@82: "--literal-attributes", "true", // zachovat mezery a konce řádků v atributech franta-hg@82: "--force-output", "true" // neznámé značky zahodíme, vložíme jen jejich obsah franta-hg@82: }); franta-hg@75: franta-hg@75: PrintStream vstupProcesu = new PrintStream(p.getOutputStream()); franta-hg@75: vstupProcesu.print(inputText); franta-hg@75: vstupProcesu.close(); franta-hg@75: franta-hg@75: String outputText = streamToString(p.getInputStream()); franta-hg@75: franta-hg@100: outputText = vraťKonceŘádků(outputText); franta-hg@82: franta-hg@75: return outputText; franta-hg@75: } franta-hg@75: franta-hg@100: private static String označKonceŘádků(String text) { franta-hg@100: text = text.replaceAll(">\\s+<", "> <"); franta-hg@100: text = text.replaceAll("\\n", ZNAKČKA_KONCE_ŘÁDKU + "\n"); franta-hg@100: return text; franta-hg@100: } franta-hg@100: franta-hg@100: private static String vraťKonceŘádků(String text) { franta-hg@100: text = text.replaceAll(ZNAKČKA_KONCE_ŘÁDKU + "\\n", "\n"); franta-hg@100: text = text.replaceAll(ZNAKČKA_KONCE_ŘÁDKU, "\n"); franta-hg@100: return text; franta-hg@100: } franta-hg@100: franta-hg@75: /** franta-hg@75: * TODO: refaktorovat, přesunout franta-hg@75: */ franta-hg@75: private static String streamToString(InputStream proud) throws IOException { franta-hg@75: StringBuilder výsledek = new StringBuilder(); franta-hg@75: BufferedReader buf = new BufferedReader(new InputStreamReader(proud)); franta-hg@75: while (true) { franta-hg@75: String radek = buf.readLine(); franta-hg@75: if (radek == null) { franta-hg@75: break; franta-hg@75: } else { franta-hg@75: výsledek.append(radek); franta-hg@75: výsledek.append("\n"); franta-hg@75: } franta-hg@75: } franta-hg@75: return výsledek.toString(); franta-hg@75: } franta-hg@75: franta-hg@102: public static String constructMessageId(long articleID, long groupID, String groupName, String domainName) { franta-hg@72: StringBuilder sb = new StringBuilder(); franta-hg@72: sb.append("<"); franta-hg@72: sb.append(articleID); franta-hg@72: sb.append("-"); franta-hg@72: sb.append(groupID); franta-hg@72: sb.append("-"); franta-hg@72: sb.append(groupName); franta-hg@72: sb.append("@"); franta-hg@72: sb.append(domainName); franta-hg@72: sb.append(">"); franta-hg@72: return sb.toString(); franta-hg@72: } franta-hg@72: franta-hg@102: /** franta-hg@102: * @return article ID of parent of this message | or null, if this is root article and not reply to another one franta-hg@102: */ franta-hg@102: public Long getParentID() { franta-hg@102: return parentID; franta-hg@102: } franta-hg@102: franta-hg@102: /** franta-hg@102: * @return group ID of this message | or null, if this message is not reply to any other one – which is wrong because we have to know the group franta-hg@102: */ franta-hg@102: public Long getGroupID() { franta-hg@102: return groupID; franta-hg@102: } franta-hg@102: franta-hg@102: /** franta-hg@102: * franta-hg@102: * @param messageID <{0}-{1}-{2}@domain.tld> where {0} is nntp_id and {1} is group_id and {2} is group_name franta-hg@102: * @return array where [0] = nntp_id and [1] = group_id and [2] = group_name or returns null if messageID is invalid franta-hg@102: */ franta-hg@102: private static String[] parseMessageID(String messageID) { franta-hg@102: if (messageID.matches("<[0-9]+\\-[0-9]+\\-[a-z0-9\\.]+@.+>")) { franta-hg@102: return messageID.substring(1).split("@")[0].split("\\-"); franta-hg@102: } else { franta-hg@102: return null; franta-hg@102: } franta-hg@102: } franta-hg@102: franta-hg@102: public static Long parseArticleID(String messageID) { franta-hg@102: String[] localPart = parseMessageID(messageID); franta-hg@102: if (localPart == null) { franta-hg@102: return null; franta-hg@102: } else { franta-hg@102: return Long.parseLong(localPart[0]); franta-hg@102: } franta-hg@102: } franta-hg@102: franta-hg@102: public static Long parseGroupID(String messageID) { franta-hg@102: String[] localPart = parseMessageID(messageID); franta-hg@102: if (localPart == null) { franta-hg@102: return null; franta-hg@102: } else { franta-hg@102: return Long.parseLong(localPart[1]); franta-hg@102: // If needed: franta-hg@102: // parseGroupName() will be same as this method, just with: franta-hg@102: // return localPart[2]; franta-hg@102: } franta-hg@102: } franta-hg@102: franta-hg@72: @Override franta-hg@72: public void setHeader(String name, String value) throws MessagingException { franta-hg@72: super.setHeader(name, value); franta-hg@72: franta-hg@72: if (MESSAGE_ID_HEADER.equalsIgnoreCase(name)) { franta-hg@72: messageID = value; franta-hg@72: } franta-hg@72: } franta-hg@72: franta-hg@72: @Override franta-hg@72: public final void addHeader(String name, String value) throws MessagingException { franta-hg@72: super.addHeader(name, value); franta-hg@72: franta-hg@72: if (MESSAGE_ID_HEADER.equalsIgnoreCase(name)) { franta-hg@72: messageID = value; franta-hg@72: } franta-hg@72: } franta-hg@72: franta-hg@72: @Override franta-hg@72: public void removeHeader(String name) throws MessagingException { franta-hg@72: super.removeHeader(name); franta-hg@72: franta-hg@72: if (MESSAGE_ID_HEADER.equalsIgnoreCase(name)) { franta-hg@72: messageID = null; franta-hg@72: } franta-hg@72: } franta-hg@72: franta-hg@72: public void setMessageID(String messageID) { franta-hg@72: this.messageID = messageID; franta-hg@72: } franta-hg@72: franta-hg@72: @Override franta-hg@72: protected void updateMessageID() throws MessagingException { franta-hg@72: if (messageID == null) { franta-hg@72: super.updateMessageID(); franta-hg@72: } else { franta-hg@72: setHeader(MESSAGE_ID_HEADER, messageID); franta-hg@72: } franta-hg@72: } franta-hg@72: franta-hg@72: /** franta-hg@72: * Call {@link #saveChanges()} before this method, if you want all headers including such ones like: franta-hg@72: * franta-hg@72: *
MIME-Version: 1.0
franta-hg@72: 	 *Content-Type: multipart/alternative;
franta-hg@72: * franta-hg@72: * @return serialized headers franta-hg@72: * @throws MessagingException if getAllHeaders() fails franta-hg@72: */ franta-hg@72: public String getHeaders() throws MessagingException { franta-hg@72: StringBuilder sb = new StringBuilder(); franta-hg@72: for (Enumeration eh = getAllHeaderLines(); eh.hasMoreElements();) { franta-hg@72: sb.append(eh.nextElement()); franta-hg@72: sb.append(CRLF); franta-hg@72: } franta-hg@72: return sb.toString(); franta-hg@72: } franta-hg@72: franta-hg@72: public byte[] getBody() throws IOException, MessagingException { franta-hg@72: saveChanges(); franta-hg@72: franta-hg@72: ArrayList skipHeaders = new ArrayList(); franta-hg@72: for (Enumeration eh = getAllHeaders(); eh.hasMoreElements();) { franta-hg@72: Header h = (Header) eh.nextElement(); franta-hg@72: skipHeaders.add(h.getName()); franta-hg@72: } franta-hg@72: franta-hg@72: ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); franta-hg@72: writeTo(baos, skipHeaders.toArray(new String[skipHeaders.size()])); franta-hg@72: return baos.toByteArray(); franta-hg@72: } franta-hg@72: }