Module: Xolo::Server::Helpers::Notification

Included in:
Title, Version
Defined in:
lib/xolo/server/helpers/notification.rb

Overview

This is mixed in to Xolo::Server::App (as a helper, available in route processing) and in Xolo::Server::Title and Xolo::Server::Version.

This holds methods and constants for sending alerts and emails.

Constant Summary collapse

DFT_EMAIL_FROM =

Constants

'xolo-server-do-not-reply'
ALERT_TOOL_EMAIL_PREFIX =
'mailto:'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(includer) ⇒ Object

when this module is included



37
38
39
# File 'lib/xolo/server/helpers/notification.rb', line 37

def self.included(includer)
  Xolo.verbose_include includer, self
end

Instance Method Details

#email_fromString

Returns the from address for emails.

Returns:

  • (String)

    the from address for emails



124
125
126
# File 'lib/xolo/server/helpers/notification.rb', line 124

def email_from
  @email_from ||= Xolo::Server.config.email_from || "#{DFT_EMAIL_FROM}@#{server_fqdn}"
end

#send_alert(msg, level) ⇒ void

This method returns an undefined value.

Send a message thru the alert_tool, if one is defined in the config.

Messages are prepended with “#level ALERT: ”

This should be called by passing ‘alert: true’ to one of the logging wrapper methods

Parameters:

  • msg (String)

    the message to send

  • level (Symbol)

    the log level of the message



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/xolo/server/helpers/notification.rb', line 57

def send_alert(msg, level)
  return unless Xolo::Server.config.alert_tool
  return if send_email_alert(msg, level)

  alerter = nil # just in case we need the ensure clause below.
  alerter = IO.popen(Xolo::Server.config.alert_tool, 'w')
  alerter.puts "#{level} ALERT: #{msg}"

# this catches the quitting of the alerter before expected
rescue Errno::EPIPE
  true
ensure
  # this flushes the pipe and makes the msg go
  alerter&.close
end

#send_email(to:, subject:, msg:, html: false) ⇒ void

This method returns an undefined value.

Send an email, if the smtp_server is defined in the config.

Parameters:

  • to (String)

    the email address to send to

  • subject (String)

    the subject of the email

  • msg (String)

    the body of the email

  • html (Boolean) (defaults to: false)

    should the email be sent as HTML?



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/xolo/server/helpers/notification.rb', line 100

def send_email(to:, subject:, msg:, html: false)
  return unless Xolo::Server.config.smtp_server

  headers = [
    "From: #{server_name} <#{email_from}>",
    "Date: #{Time.now.rfc2822}",
    "To: #{to} <#{to}>",
    "Subject: #{subject}"
  ]

  if html
    headers << 'MIME-Version: 1.0'
    headers << 'Content-type: text/html'
  end

  msg = "#{headers.join "\n"}\n\n#{msg}"

  Net::SMTP.start(Xolo::Server.config.smtp_server) do |smtp|
    smtp.send_message msg, email_from, to
  end
end

#send_email_alert(msg, level) ⇒ Boolean

Send an alert via email

Parameters:

  • msg (String)

    the message to send

  • level (Symbol)

    the log level of the message

Returns:

  • (Boolean)

    true if the email was sent, false otherwise



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/xolo/server/helpers/notification.rb', line 79

def send_email_alert(msg, level)
  return false unless Xolo::Server.config.smtp_server
  return false unless Xolo::Server.config.alert_tool.start_with? ALERT_TOOL_EMAIL_PREFIX

  send_email(
    to: Xolo::Server.config.alert_tool.delete_prefix(ALERT_TOOL_EMAIL_PREFIX).strip,
    subject: "#{level} ALERT from Xolo Server",
    msg: msg
  )
  true
end

#server_fqdnString

Returns the server’s fully qualified domain name.

Returns:

  • (String)

    the server’s fully qualified domain name



136
137
138
# File 'lib/xolo/server/helpers/notification.rb', line 136

def server_fqdn
  @server_fqdn ||= Addrinfo.getaddrinfo(Socket.gethostname, nil).first.getnameinfo.first
end

#server_nameString

Returns the human-readable name of the server for sending emails.

Returns:

  • (String)

    the human-readable name of the server for sending emails



130
131
132
# File 'lib/xolo/server/helpers/notification.rb', line 130

def server_name
  @server_name ||= "Xolo Server on #{server_fqdn}"
end