org/sonews/mlgw/SMTPTransport.java
author cli
Wed, 26 Aug 2009 17:04:04 +0200
changeset 24 2ff819fa5be1
parent 12 bb6990c0dd1a
child 28 15d14b110240
permissions -rw-r--r--
Some changes to storage interface.
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.FileOutputStream;
chris@3
    24
import java.io.IOException;
chris@3
    25
import java.io.InputStreamReader;
chris@3
    26
import java.io.PrintWriter;
chris@3
    27
import java.net.Socket;
chris@3
    28
import java.net.UnknownHostException;
chris@3
    29
import org.sonews.config.Config;
chris@3
    30
import org.sonews.storage.Article;
chris@3
    31
import org.sonews.util.io.ArticleInputStream;
chris@3
    32
chris@3
    33
/**
chris@3
    34
 * Connects to a SMTP server and sends a given Article to it.
chris@3
    35
 * @author Christian Lins
chris@3
    36
 */
chris@3
    37
class SMTPTransport
chris@3
    38
{
chris@3
    39
chris@3
    40
  protected BufferedReader in;
chris@3
    41
  protected PrintWriter    out;
chris@3
    42
  protected Socket         socket;
chris@3
    43
chris@3
    44
  public SMTPTransport(String host, int port)
chris@3
    45
    throws IOException, UnknownHostException
chris@3
    46
  {
chris@3
    47
    socket = new Socket(host, port);
chris@3
    48
    this.in  = new BufferedReader(new InputStreamReader(socket.getInputStream()));
chris@3
    49
    this.out = new PrintWriter(socket.getOutputStream());
chris@3
    50
chris@3
    51
    // Read helo from server
chris@3
    52
    String line = this.in.readLine();
chris@3
    53
    if(line == null || !line.startsWith("220 "))
chris@3
    54
    {
chris@3
    55
      throw new IOException("Invalid helo from server: " + line);
chris@3
    56
    }
chris@3
    57
chris@3
    58
    // Send HELO to server
chris@3
    59
    this.out.println("HELO " + Config.inst().get(Config.HOSTNAME, "localhost"));
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
  {
chris@3
    77
    this.out.println("QUIT");
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
chris@3
    91
    this.out.println("MAIL FROM: " + mailFrom);
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
chris@3
    99
    this.out.println("RCPT TO: " + rcptTo);
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
chris@3
   107
    this.out.println("DATA");
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
    BufferedOutputStream outStream = new BufferedOutputStream(socket.getOutputStream());
chris@3
   117
    FileOutputStream     fileStream = new FileOutputStream("smtp.dump");
chris@3
   118
    for(int b = artStream.read(); b >= 0; b = artStream.read())
chris@3
   119
    {
chris@3
   120
      outStream.write(b);
chris@3
   121
      fileStream.write(b);
chris@3
   122
    }
chris@3
   123
chris@3
   124
    // Flush the binary stream; important because otherwise the output
chris@3
   125
    // will be mixed with the PrintWriter.
chris@3
   126
    outStream.flush();
chris@3
   127
    fileStream.flush();
chris@3
   128
    fileStream.close();
chris@3
   129
    this.out.print("\r\n.\r\n");
chris@3
   130
    this.out.flush();
chris@3
   131
    line = this.in.readLine();
chris@3
   132
    if(line == null || !line.startsWith("250 "))
chris@3
   133
    {
chris@3
   134
      throw new IOException("Unexpected reply: " + line);
chris@3
   135
    }
chris@3
   136
  }
chris@3
   137
chris@3
   138
}