src/org/sonews/daemon/command/PostCommand.java
changeset 115 e5bfc969d41f
parent 113 a059aecd1794
child 117 79ce65d63cce
     1.1 --- a/src/org/sonews/daemon/command/PostCommand.java	Sun Oct 30 22:15:03 2011 +0100
     1.2 +++ b/src/org/sonews/daemon/command/PostCommand.java	Sat Nov 05 00:06:09 2011 +0100
     1.3 @@ -37,6 +37,7 @@
     1.4  import org.sonews.storage.StorageManager;
     1.5  import org.sonews.feed.FeedManager;
     1.6  import org.sonews.util.Stats;
     1.7 +import org.sonews.util.io.SMTPInputStream;
     1.8  
     1.9  /**
    1.10   * Implementation of the POST command. This command requires multiple lines
    1.11 @@ -103,6 +104,7 @@
    1.12  				if ("".equals(line) || ".".equals(line)) {
    1.13  					// we finally met the blank line
    1.14  					// separating headers from body
    1.15 +					// WTF: "."
    1.16  
    1.17  					try {
    1.18  						// Parse the header using the InternetHeader class from JavaMail API
    1.19 @@ -124,6 +126,7 @@
    1.20  
    1.21  					state = PostState.ReadingBody;
    1.22  
    1.23 +					// WTF: do we need articles without bodies?
    1.24  					if (".".equals(line)) {
    1.25  						// Post an article without body
    1.26  						postArticle(conn, article);
    1.27 @@ -138,7 +141,7 @@
    1.28  					headers.setHeader(Headers.LINES, Integer.toString(lineCount));
    1.29  					headers.setHeader(Headers.BYTES, Long.toString(bodySize));
    1.30  
    1.31 -					byte[] body = bufBody.toByteArray();
    1.32 +					byte[] body = unescapeDots(bufBody.toByteArray());
    1.33  					if (body.length >= 2) {
    1.34  						// Remove trailing CRLF
    1.35  						body = Arrays.copyOf(body, body.length - 2);
    1.36 @@ -213,7 +216,7 @@
    1.37  		if (conn.getUser() != null && conn.getUser().isAuthenticated()) {
    1.38  			article.setAuthenticatedUser(conn.getUser().getUserName());
    1.39  		}
    1.40 -		
    1.41 +
    1.42  		if (article.getHeader(Headers.CONTROL)[0].length() > 0) {
    1.43  			controlMessage(conn, article);
    1.44  		} else if (article.getHeader(Headers.SUPERSEDES)[0].length() > 0) {
    1.45 @@ -273,4 +276,27 @@
    1.46  			}
    1.47  		}
    1.48  	}
    1.49 +
    1.50 +	/**
    1.51 +	 * TODO: rework, integrate into NNTPConnection
    1.52 +	 * 
    1.53 +	 * @param body message body with doubled dots
    1.54 +	 * @return message body with unescaped dots (.. → .)
    1.55 +	 */
    1.56 +	private static byte[] unescapeDots(byte[] body) throws IOException {
    1.57 +		byte[] result = new byte[body.length];
    1.58 +		int resultLength = 0;
    1.59 +
    1.60 +		ByteArrayInputStream escapedInput = new ByteArrayInputStream(body);
    1.61 +		SMTPInputStream unescapedInput = new SMTPInputStream(escapedInput);
    1.62 +
    1.63 +		int ch = unescapedInput.read();
    1.64 +		while (ch >= 0) {
    1.65 +			result[resultLength] = (byte) ch;
    1.66 +			resultLength++;
    1.67 +			ch = unescapedInput.read();
    1.68 +		}
    1.69 +
    1.70 +		return Arrays.copyOfRange(result, 0, resultLength);
    1.71 +	}
    1.72  }