Sha256: 16c3b45a90e8c8dc38ca23b824d2d4fbe5d1a1e1d65ea59d1c2035b364ab8d7f

Contents?: true

Size: 1.81 KB

Versions: 2

Compression:

Stored size: 1.81 KB

Contents

module Malm
  class Message
    attr_reader :state, :email_body, :mail_from, :rcpt_to

    def initialize
      @email_body = ""
      @rcpt_to = []
      @state = :init_state
    end
  
    def process_line(line=nil)    
      if line =~ /QUIT/
        return quit!
      end
      
      # state machine pattern. Delegate to state handler for where we're at in SMTP conversation
      response = send(@state, line)
      return error! unless response
      
      # transition if state handler returned a new state
      new_state = response.delete(:state)
      @state = new_state if new_state
      
      response
    end
  
    def subject
      if @email_body =~ /^Subject\: (.+)$/
        $1.strip
      end
    end
  
    private
    def init_state(_ignore_line=nil)
      {:output => "220 hello\r\n", :state => :helo_state}
    end
    
    def helo_state(line)
      if (line =~ /^(HELO|EHLO)/)
        ok!(:mail_state)
      end
    end
    
    def mail_state(line)
      if (line =~ /^MAIL FROM\:<(.+)>.*$/)
        @mail_from = $1
        ok!(:rcpt_state)
      end
    end
    
    def rcpt_state(line)
      if (line =~ /^RCPT TO\:<(.+)>.*$/)
        @rcpt_to << $1
        ok!
      elsif (line =~ /^DATA/)
        {:output => "354 Enter message, ending with \".\" on a line by itself\r\n", :state => :data_state}
      end
    end
    
    def data_state(line)
      if (line.chomp =~ /^\.$/)
        ok!(:quit_state)
      else
        @email_body << line
        {}
      end
    end
  
    def quit_state(_ignore)
      error!
    end
  
    def quit!
      {:output => "221 bye\r\n", :state => :quit_state, :done => true}
    end
  
    def error!
      {:output => "500 ERROR\r\n", :state => :quit_state, :done => true}
    end
  
    def ok!(state=nil)
      {:output => "250 OK\r\n", :state => state}
    end
    
    
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
malm-0.1.1 lib/malm/message.rb
malm-0.1.0 lib/malm/message.rb