Class: Ronin::EmailAddress

Inherits:
Object
  • Object
show all
Includes:
Model
Defined in:
lib/ronin/email_address.rb

Overview

Represents email addresses that can be stored in the Database.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Model

included

Class Method Details

.extract(text) {|email| ... } ⇒ Array<EmailAddress>

Extracts email addresses from the given text.

Parameters:

  • text (String)

    The text to parse.

Yields:

  • (email)

    The given block will be passed each extracted email address.

Yield Parameters:

Returns:

  • (Array<EmailAddress>)

    If no block is given, an Array of email address will be returned.

See Also:

  • 11.31.3.0


77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ronin/email_address.rb', line 77

def self.extract(text)
  return enum_for(:extract,text).to_a unless block_given?

  scanner = StringScanner.new(text)

  while scanner.skip_until(Regexp::EMAIL_ADDR)
    yield parse(scanner.matched)
  end

  return nil
end

.parse(email) ⇒ EmailAddress

Parses an email address.

Parameters:

  • email (String)

    The email address to parse.

Returns:

  • (EmailAddress)

    A new or previously saved email address resource.

Raises:

  • (RuntimeError)

    The email address did not have a user name or a host name.

Since:

  • 1.0.0



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/ronin/email_address.rb', line 156

def EmailAddress.parse(email)
  user, host = email.split('@',2)

  user.strip!

  if user.empty?
    raise("email address #{email.dump} must have a user name")
  end

  host.strip!

  if host.empty?
    raise("email address #{email.dump} must have a host name")
  end

  return EmailAddress.first_or_new(
    :user_name => UserName.first_or_new(:name => user),
    :host_name => HostName.first_or_new(:address => host)
  )
end

.with_hosts(names) ⇒ Array<EmailAddress>

Searches for email addresses associated with the given host names.

Parameters:

  • names (Array<String>, String)

    The host name(s) to search for.

Returns:

Since:

  • 1.0.0



102
103
104
# File 'lib/ronin/email_address.rb', line 102

def self.with_hosts(names)
  all('host_name.address' => names)
end

.with_ips(ips) ⇒ Array<EmailAddress>

Searches for email addresses associated with the given IP address(es).

Parameters:

  • ips (Array<String>, String)

    The IP address(es) to search for.

Returns:

Since:

  • 1.0.0



119
120
121
# File 'lib/ronin/email_address.rb', line 119

def self.with_ips(ips)
  all('ip_addresses.address' => ips)
end

.with_users(names) ⇒ Array<EmailAddress>

Searches for email addresses associated with the given user names.

Parameters:

  • names (Array<String>, String)

    The user name(s) to search for.

Returns:

Since:

  • 1.0.0



136
137
138
# File 'lib/ronin/email_address.rb', line 136

def self.with_users(names)
  all('user_name.name' => names)
end

Instance Method Details

#hostString

The host of the email address.

Returns:

  • (String)

    The host name.

Since:

  • 1.0.0



201
202
203
# File 'lib/ronin/email_address.rb', line 201

def host
  self.host_name.address if self.host_name
end

#inspectString

Inspects the email address.

Returns:

  • (String)

    The inspected email address.

Since:

  • 1.0.0



229
230
231
# File 'lib/ronin/email_address.rb', line 229

def inspect
  "#<#{self.class}: #{self}>"
end

#to_aryArray

Splats the email address into multiple variables.

Examples:

email = EmailAddress.parse('[email protected]')
user, host = email

user
# => "alice"
host
# => "example.com"

Returns:

  • (Array)

    The user-name and the host-name within the email address.

Since:

  • 1.0.0



252
253
254
# File 'lib/ronin/email_address.rb', line 252

def to_ary
  [self.user_name.name, self.host_name.address]
end

#to_sString

Converts the email address into a String.

Returns:

  • (String)

    The raw email address.

Since:

  • 1.0.0



215
216
217
# File 'lib/ronin/email_address.rb', line 215

def to_s
  "#{self.user_name}@#{self.host_name}"
end

#userString

The user of the email address.

Returns:

  • (String)

    The user name.

Since:

  • 1.0.0



187
188
189
# File 'lib/ronin/email_address.rb', line 187

def user
  self.user_name.name if self.user_name
end