org/sonews/mlgw/SMTPTransport.java
author cli
Sat May 01 14:27:30 2010 +0200 (2010-05-01)
changeset 28 15d14b110240
parent 12 bb6990c0dd1a
permissions -rw-r--r--
Make mailinglist gateway more reliable, probably.
chris@3
     1
/*
chris@3
     2
 *   SONEWS News Server
chris@3
     3
 *   see AUTHORS for the list of contributors
chris@3
     4
 *
chris@3
     5
 *   This program is free software: you can redistribute it and/or modify
chris@3
     6
 *   it under the terms of the GNU General Public License as published by
chris@3
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@3
     8
 *   (at your option) any later version.
chris@3
     9
 *
chris@3
    10
 *   This program is distributed in the hope that it will be useful,
chris@3
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@3
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@3
    13
 *   GNU General Public License for more details.
chris@3
    14
 *
chris@3
    15
 *   You should have received a copy of the GNU General Public License
chris@3
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@3
    17
 */
chris@3
    18
chris@3
    19
package org.sonews.mlgw;
chris@3
    20
chris@3
    21
import java.io.BufferedOutputStream;
chris@3
    22
import java.io.BufferedReader;
chris@3
    23
import java.io.IOException;
chris@3
    24
import java.io.InputStreamReader;
chris@3
    25
import java.net.Socket;
chris@3
    26
import java.net.UnknownHostException;
chris@3
    27
import org.sonews.config.Config;
chris@3
    28
import org.sonews.storage.Article;
chris@3
    29
import org.sonews.util.io.ArticleInputStream;
chris@3
    30
chris@3
    31
/**
chris@3
    32
 * Connects to a SMTP server and sends a given Article to it.
chris@3
    33
 * @author Christian Lins
cli@28
    34
 * @since sonews/1.0
chris@3
    35
 */
chris@3
    36
class SMTPTransport
chris@3
    37
{
chris@3
    38
cli@28
    39
  protected BufferedReader       in;
cli@28
    40
  protected BufferedOutputStream out;
cli@28
    41
  protected Socket               socket;
chris@3
    42
chris@3
    43
  public SMTPTransport(String host, int port)
chris@3
    44
    throws IOException, UnknownHostException
chris@3
    45
  {
chris@3
    46
    socket = new Socket(host, port);
chris@3
    47
    this.in  = new BufferedReader(new InputStreamReader(socket.getInputStream()));
cli@28
    48
    this.out = new BufferedOutputStream(socket.getOutputStream());
chris@3
    49
chris@3
    50
    // Read helo from server
chris@3
    51
    String line = this.in.readLine();
chris@3
    52
    if(line == null || !line.startsWith("220 "))
chris@3
    53
    {
chris@3
    54
      throw new IOException("Invalid helo from server: " + line);
chris@3
    55
    }
chris@3
    56
chris@3
    57
    // Send HELO to server
cli@28
    58
    this.out.write(
cli@28
    59
      ("HELO " + Config.inst().get(Config.HOSTNAME, "localhost") + "\r\n").getBytes("UTF-8"));
chris@3
    60
    this.out.flush();
chris@3
    61
    line = this.in.readLine();
chris@3
    62
    if(line == null || !line.startsWith("250 "))
chris@3
    63
    {
chris@3
    64
      throw new IOException("Unexpected reply: " + line);
chris@3
    65
    }
chris@3
    66
  }
chris@3
    67
chris@3
    68
  public SMTPTransport(String host)
chris@3
    69
    throws IOException
chris@3
    70
  {
chris@3
    71
    this(host, 25);
chris@3
    72
  }
chris@3
    73
chris@3
    74
  public void close()
chris@3
    75
    throws IOException
chris@3
    76
  {
cli@28
    77
    this.out.write("QUIT".getBytes("UTF-8"));
chris@3
    78
    this.out.flush();
chris@3
    79
    this.in.readLine();
chris@3
    80
chris@3
    81
    this.socket.close();
chris@3
    82
  }
chris@3
    83
chris@3
    84
  public void send(Article article, String mailFrom, String rcptTo)
chris@3
    85
    throws IOException
chris@3
    86
  {
cli@12
    87
    assert(article != null);
cli@12
    88
    assert(mailFrom != null);
cli@12
    89
    assert(rcptTo != null);
cli@12
    90
cli@28
    91
    this.out.write(("MAIL FROM: " + mailFrom).getBytes("UTF-8"));
chris@3
    92
    this.out.flush();
chris@3
    93
    String line = this.in.readLine();
chris@3
    94
    if(line == null || !line.startsWith("250 "))
chris@3
    95
    {
chris@3
    96
      throw new IOException("Unexpected reply: " + line);
chris@3
    97
    }
chris@3
    98
cli@28
    99
    this.out.write(("RCPT TO: " + rcptTo).getBytes("UTF-8"));
chris@3
   100
    this.out.flush();
chris@3
   101
    line  = this.in.readLine();
chris@3
   102
    if(line == null || !line.startsWith("250 "))
chris@3
   103
    {
chris@3
   104
      throw new IOException("Unexpected reply: " + line);
chris@3
   105
    }
chris@3
   106
cli@28
   107
    this.out.write("DATA".getBytes("UTF-8"));
chris@3
   108
    this.out.flush();
chris@3
   109
    line = this.in.readLine();
chris@3
   110
    if(line == null || !line.startsWith("354 "))
chris@3
   111
    {
chris@3
   112
      throw new IOException("Unexpected reply: " + line);
chris@3
   113
    }
chris@3
   114
chris@3
   115
    ArticleInputStream   artStream = new ArticleInputStream(article);
chris@3
   116
    for(int b = artStream.read(); b >= 0; b = artStream.read())
chris@3
   117
    {
cli@28
   118
      this.out.write(b);
chris@3
   119
    }
chris@3
   120
chris@3
   121
    // Flush the binary stream; important because otherwise the output
chris@3
   122
    // will be mixed with the PrintWriter.
cli@28
   123
    this.out.flush();
cli@28
   124
    this.out.write("\r\n.\r\n".getBytes("UTF-8"));
chris@3
   125
    this.out.flush();
chris@3
   126
    line = this.in.readLine();
chris@3
   127
    if(line == null || !line.startsWith("250 "))
chris@3
   128
    {
chris@3
   129
      throw new IOException("Unexpected reply: " + line);
chris@3
   130
    }
chris@3
   131
  }
chris@3
   132
chris@3
   133
}