Guest User

Untitled

a guest
Jul 21st, 2024
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.10 KB | Source Code | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.regex.*;
  4. import java.util.*;
  5.  
  6. public class SmtpSink
  7. {
  8.     boolean finished = false;
  9.  
  10.     public static void main( String[] args )
  11.     {
  12.         new SmtpSink( 2000 );
  13.     }
  14.  
  15.     SmtpSink( int port )
  16.     {
  17.         int count = 0;
  18.         try
  19.         {
  20.             ServerSocket listener = new ServerSocket( port );
  21.             while ( ! finished )
  22.             {
  23.                 Socket client = listener.accept();
  24.                 new Connection( client, ++count );
  25.             }
  26.         }
  27.         catch (Exception e)
  28.         {
  29.         }
  30.     }
  31. }
  32.  
  33. class Connection
  34. {
  35.     Socket client;
  36.     int id;
  37.     BufferedReader in;
  38.     PrintWriter out;
  39.     Pattern p_DATA;
  40.     Pattern p_QUIT;
  41.  
  42.     Connection( Socket client, int id ) throws IOException
  43.     {
  44.         try
  45.         {
  46.             p_DATA = Pattern.compile( "data", Pattern.CASE_INSENSITIVE );
  47.             p_QUIT = Pattern.compile( "quit", Pattern.CASE_INSENSITIVE );
  48.         }
  49.         catch (Exception e)
  50.         {
  51.             System.out.println( "Error compiling pattern: " + e );
  52.         }
  53.         this.client = client;
  54.         this.id = id;
  55.         InputStream in_stream = client.getInputStream();
  56.         InputStreamReader in_stream_reader = new InputStreamReader( in_stream );
  57.         in = new BufferedReader( in_stream_reader );
  58.  
  59.         OutputStream out_stream = client.getOutputStream();
  60.         OutputStreamWriter out_stream_writer = new OutputStreamWriter( out_stream );
  61.         out = new PrintWriter( out_stream_writer, true );
  62.  
  63.         new Thread(
  64.             new Runnable()
  65.             {
  66.                 public void run()
  67.                 {
  68.                     handle_message();
  69.                 }
  70.             }
  71.         ).start();
  72.     }
  73.  
  74.     void handle_message()
  75.     {
  76.         int state = 0;  // 0 == processing commands
  77.                         // 1 == processing DATA
  78.  
  79.         String remote = client.getInetAddress().getHostAddress();
  80.         Calendar now = new GregorianCalendar();
  81.         File msgfile = new File( "msgs", "msg." + now.get(Calendar.YEAR) + "."
  82.                                                 + now.get(Calendar.MONTH) + "."
  83.                                                 + now.get(Calendar.DAY_OF_MONTH) + "-"
  84.                                                 + now.get(Calendar.HOUR_OF_DAY) + "."
  85.                                                 + now.get(Calendar.MINUTE) + "."
  86.                                                 + now.get(Calendar.SECOND) + "-"
  87.                                                 + id + "-" + remote );
  88.        
  89.         try
  90.         {
  91.             FileOutputStream fileout = new FileOutputStream( msgfile );
  92.             PrintWriter file = new PrintWriter( fileout );
  93.            
  94.             String line;
  95.             out.println( "220 hello " + remote );
  96.             while ( (line = in.readLine()) != null )
  97.             {
  98.                 file.println( line );
  99.  
  100.                 if ( state == 0 )
  101.                 {
  102.                     Matcher m = p_QUIT.matcher( line );
  103.                     if ( m.lookingAt() )
  104.                     {
  105.                         out.println( "221 bye" );
  106.                         client.close();
  107.                         client = null;
  108.                         break;
  109.                     }
  110.                     m = p_DATA.matcher( line );
  111.                     if ( m.lookingAt() )
  112.                     {
  113.                         state = 1;
  114.                         out.println( "354 send the message" );
  115.                     }
  116.                     else
  117.                         out.println( "250 OK" );
  118.                 }
  119.                 else
  120.                 {
  121.                     if ( line.equals( "." ) )
  122.                     {
  123.                         out.println( "250 OK" );
  124.                         state = 0;
  125.                     }
  126.                 }
  127.             }
  128.             if ( client != null )
  129.                 client.close();
  130.  
  131.             file.close();
  132.         }
  133.         catch (Exception e)
  134.         {
  135.             System.out.println( "handle_message exception: " + e );
  136.         }
  137.     }
  138. }
  139.  
Advertisement
Add Comment
Please, Sign In to add comment