Module: Ronin::Support::Network::IMAP::Mixin

Defined in:
lib/ronin/support/network/imap/mixin.rb

Overview

Provides helper methods for communicating with IMAP services.

Constant Summary collapse

DEFAULT_PORT =

Default IMAP port

143
AUTH_TYPES =

Authentication types.

{
  login:    'LOGIN',
  cram_md5: 'CRAM_MD5'
}

Instance Method Summary collapse

Instance Method Details

#imap_connect(host, user:, password:, port: DEFAULT_PORT, ssl: nil, auth: :login) {|imap| ... } ⇒ Net::IMAP?

Creates a connection to the IMAP server.

Parameters:

  • host (String)

    The host to connect to.

  • user (String, nil)

    The user to authenticate as when connecting to the server.

  • password (String, nil)

    The password to authenticate with when connecting to the server.

  • port (Integer) (defaults to: DEFAULT_PORT)

    The port the IMAP server is running on.

  • ssl (Boolean, Hash, nil) (defaults to: nil)

    Additional SSL options.

  • auth (:login, :cram_md5) (defaults to: :login)

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

Options Hash (ssl:):

  • :verify (Boolean)

    Specifies that the SSL certificate should be verified.

  • :certs (String)

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

Yields:

  • (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.

Yield Parameters:

  • imap (Net::IMAP)

    The newly created IMAP session object.

Returns:

  • (Net::IMAP, nil)

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

Raises:

  • (ArgumentType)

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# 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