module Ronin::Support::Network::IMAP::Mixin

Provides helper methods for communicating with IMAP services.

Constants

AUTH_TYPES

Authentication types.

DEFAULT_PORT

Default IMAP port

Public Instance Methods

imap_connect(host, user: , password: , port: DEFAULT_PORT, ssl: nil, auth: :login) { |imap| ... } click to toggle source

Creates a connection to the IMAP server.

@param [String] host

The host to connect to.

@param [String, nil] user

The user to authenticate as when connecting to the server.

@param [String, nil] password

The password to authenticate with when connecting to the server.

@param [Integer] port

The port the IMAP server is running on.

@param [Boolean, Hash, nil] ssl

Additional SSL options.

@option ssl [Boolean] :verify

Specifies that the SSL certificate should be verified.

@option ssl [String] :certs

The path to the file containing CA certs of the server.

@param [:login, :cram_md5] auth

The type of authentication to perform when connecting to the
server. May be either `:login` or `:cram_md5`.

@yield [imap]

If a block is given, it will be passed the newly created IMAP
session. Once the block returns, it will close the IMAP session.

@yieldparam [Net::IMAP] imap

The newly created IMAP session object.

@return [Net::IMAP, nil]

The newly created IMAP session object. If a block is given, `nil`
will be returned.

@raise [ArgumentType]

The `auth:` keyword argument must be either `:login` or
`:cram_md5`.

@api public

# File lib/ronin/support/network/imap/mixin.rb, line 91
def imap_connect(host, user: ,
                       password: ,
                       port:  DEFAULT_PORT,
                       ssl:   nil,
                       auth: :login)
  host = DNS::IDN.to_ascii(host)

  auth_type = AUTH_TYPES.fetch(auth) do
    raise(ArgumentError,"auth: must be either :login or :cram_md5")
  end

  case ssl
  when Hash
    ssl        = true
    ssl_certs  = ssl[:certs]
    ssl_verify = Network::SSL::VERIFY[ssl[:verify]]
  when TrueClass
    ssl        = true
    ssl_certs  = nil
    ssl_verify = nil
  else
    ssl        = false
    ssl_certs  = nil
    ssl_verify = false
  end

  imap = Net::IMAP.new(host,port,ssl,ssl_certs,ssl_verify)
  imap.authenticate(auth_type,user,passwd)

  if block_given?
    yield imap
    imap.logout
    imap.close
    imap.disconnect
  else
    return imap
  end
end