lib/ronin/network/mixins/smtp.rb in ronin-support-0.3.0 vs lib/ronin/network/mixins/smtp.rb in ronin-support-0.4.0.rc1

- old
+ new

@@ -15,16 +15,13 @@ # # You should have received a copy of the GNU Lesser General Public License # along with Ronin Support. If not, see <http://www.gnu.org/licenses/>. # +require 'ronin/network/mixins/mixin' require 'ronin/network/smtp' -require 'ronin/ui/output/helpers' -require 'ronin/mixin' -require 'parameters' - module Ronin module Network module Mixins # # Adds SMTP convenience methods and connection parameters to a class. @@ -36,48 +33,35 @@ # * `smtp_login` (`String`) - SMTP authentication method. # * `smtp_user` (`String`) - SMTP user to login as. # * `smtp_password` (`String`) - SMTP password to login with. # module SMTP - include Mixin + include Mixin, Network::SMTP - mixin UI::Output::Helpers, Parameters + # SMTP host + parameter :host, :type => String, + :description => 'SMTP host' - mixin do - # SMTP host - parameter :host, :type => String, - :description => 'SMTP host' + # SMTP port + parameter :port, :type => Integer, + :description => 'SMTP port' - # SMTP port - parameter :port, :type => Integer, - :description => 'SMTP port' + # SMTP authentication method + parameter :smtp_login, :type => String, + :description => 'SMTP authentication method' - # SMTP authentication method - parameter :smtp_login, :type => String, - :description => 'SMTP authentication method' + # SMTP user to login as + parameter :smtp_user, :type => String, + :description => 'SMTP user to login as' - # SMTP user to login as - parameter :smtp_user, :type => String, - :description => 'SMTP user to login as' + # SMTP user to login with + parameter :smtp_password, :type => String, + :description => 'SMTP password to login with' - # SMTP user to login with - parameter :smtp_password, :type => String, - :description => 'SMTP password to login with' - end - protected # - # @see Ronin::Network::SMTP.message - # - # @api public - # - def smtp_message(options={},&block) - Network::SMTP.message(options,&block) - end - - # # Creates a connection to the SMTP server. The `host`, `port`, # `smtp_login`, `smtp_user` and `smtp_password` parameters # will also be used to connect to the server. # # @param [Hash] options @@ -109,22 +93,13 @@ # The SMTP session. # # @api public # def smtp_connect(options={},&block) - options[:port] ||= self.port - options[:login] ||= self.smtp_login - options[:user] ||= self.smtp_user - options[:password] ||= self.smtp_password + print_info "Connecting to #{host_port} ..." - if self.port - print_info "Connecting to #{self.host}:#{self.port} ..." - else - print_info "Connecting to #{self.host} ..." - end - - return ::Net.smtp_connect(self.host,options,&block) + return super(self.host,smtp_merge_options(options),&block) end # # Starts a session with the SMTP server. The `host`, `port`, # `smtp_login`, `smtp_user` and `smtp_password` parameters @@ -140,19 +115,42 @@ # @see smtp_connect # # @api public # def smtp_session(options={},&block) - smtp_connect(options) do |sess| + super(smtp_merge_options(options)) do |sess| yield sess if block_given? - sess.close - if self.port - print_info "Disconnecting to #{self.host}:#{self.port}" - else - print_info "Disconnecting to #{self.host}" - end + print_info "Logging out ..." end + + print_info "Disconnected to #{host_port}" + return nil + end + + private + + # + # Merges the SMTP parameters into the options for {Network::SMTP} + # methods. + # + # @param [Hash] options + # The original options. + # + # @return [Hash] + # The merged options. + # + # @since 0.4.0 + # + # @api private + # + def smtp_merge_options(options={}) + options[:port] ||= self.port + options[:login] ||= self.smtp_login + options[:user] ||= self.smtp_user + options[:password] ||= self.smtp_password + + return options end end end end end