org/sonews/mlgw/SMTPTransport.java
author cli
Thu, 06 Aug 2009 18:41:34 +0200
changeset 6 dcc7e491fbc9
parent 3 2fdc9cc89502
child 12 bb6990c0dd1a
permissions -rw-r--r--
Removing doc subdir which resides now in sonews-doc repository.
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
  {
chris@3
    87
    this.out.println("MAIL FROM: " + mailFrom);
chris@3
    88
    this.out.flush();
chris@3
    89
    String line = this.in.readLine();
chris@3
    90
    if(line == null || !line.startsWith("250 "))
chris@3
    91
    {
chris@3
    92
      throw new IOException("Unexpected reply: " + line);
chris@3
    93
    }
chris@3
    94
chris@3
    95
    this.out.println("RCPT TO: " + rcptTo);
chris@3
    96
    this.out.flush();
chris@3
    97
    line  = this.in.readLine();
chris@3
    98
    if(line == null || !line.startsWith("250 "))
chris@3
    99
    {
chris@3
   100
      throw new IOException("Unexpected reply: " + line);
chris@3
   101
    }
chris@3
   102
chris@3
   103
    this.out.println("DATA");
chris@3
   104
    this.out.flush();
chris@3
   105
    line = this.in.readLine();
chris@3
   106
    if(line == null || !line.startsWith("354 "))
chris@3
   107
    {
chris@3
   108
      throw new IOException("Unexpected reply: " + line);
chris@3
   109
    }
chris@3
   110
chris@3
   111
    ArticleInputStream   artStream = new ArticleInputStream(article);
chris@3
   112
    BufferedOutputStream outStream = new BufferedOutputStream(socket.getOutputStream());
chris@3
   113
    FileOutputStream     fileStream = new FileOutputStream("smtp.dump");
chris@3
   114
    for(int b = artStream.read(); b >= 0; b = artStream.read())
chris@3
   115
    {
chris@3
   116
      outStream.write(b);
chris@3
   117
      fileStream.write(b);
chris@3
   118
    }
chris@3
   119
chris@3
   120
    // Flush the binary stream; important because otherwise the output
chris@3
   121
    // will be mixed with the PrintWriter.
chris@3
   122
    outStream.flush();
chris@3
   123
    fileStream.flush();
chris@3
   124
    fileStream.close();
chris@3
   125
    this.out.print("\r\n.\r\n");
chris@3
   126
    this.out.flush();
chris@3
   127
    line = this.in.readLine();
chris@3
   128
    if(line == null || !line.startsWith("250 "))
chris@3
   129
    {
chris@3
   130
      throw new IOException("Unexpected reply: " + line);
chris@3
   131
    }
chris@3
   132
  }
chris@3
   133
chris@3
   134
}