aboutsummaryrefslogtreecommitdiff
path: root/lib/mauve/notifiers
diff options
context:
space:
mode:
authorPatrick J Cherry <patrick@bytemark.co.uk>2011-06-13 11:02:37 +0100
committerPatrick J Cherry <patrick@bytemark.co.uk>2011-06-13 11:02:37 +0100
commitb22cbc87927553f6dbb5754281e95fe9bad2eed1 (patch)
tree5575791fe84492648a8cc92433c267815056507a /lib/mauve/notifiers
parent495c44445642cfae8f23fadde299ad5307f5be58 (diff)
* Tidied up mauveserver to handle HUP restarts
* Added HTML santizing to the alert class, so bad HTML is stripped as part of processing. * Alert#cleared? now means "not raised" * Better error handling in the Timer class, making sure that the timer never gets permanently frozen. * Moved notification and packet buffers to the Server class, meaning that if the Processor or Notifier threads crash, we don't lose all the items waiting to be processed/notified. * XMPP/Email Alerts now use templates, instead of instance methods. * Emails now get sent as multipart with HTML to allow detail fields to be shown as nature intended.
Diffstat (limited to 'lib/mauve/notifiers')
-rw-r--r--lib/mauve/notifiers/email.rb70
-rw-r--r--lib/mauve/notifiers/templates/email.html.erb28
-rw-r--r--lib/mauve/notifiers/templates/email.txt.erb24
l---------[-rw-r--r--]lib/mauve/notifiers/templates/sms.txt.erb1
-rw-r--r--lib/mauve/notifiers/templates/xmpp.html.erb0
-rw-r--r--lib/mauve/notifiers/templates/xmpp.txt.erb15
-rw-r--r--lib/mauve/notifiers/xmpp.rb23
7 files changed, 94 insertions, 67 deletions
diff --git a/lib/mauve/notifiers/email.rb b/lib/mauve/notifiers/email.rb
index f3b9a0f..168a9d6 100644
--- a/lib/mauve/notifiers/email.rb
+++ b/lib/mauve/notifiers/email.rb
@@ -66,70 +66,30 @@ module Mauve
@suppressed_changed = conditions[:suppressed_changed]
end
- other_alerts = all_alerts - [alert]
-
m = RMail::Message.new
- m.header.subject = subject_prefix +
- case @suppressed_changed
- when true
- "Suppressing notifications (#{all_alerts.length} total)"
-
- else
- alert.summary_one_line.to_s
- end
+ m.header.subject = "Arse"
m.header.to = destination
m.header.from = @from
m.header.date = MauveTime.now
+ m.header['Content-Type'] = "multipart/alternative"
- summary_formatted = " * "+alert.summary_two_lines.join("\n ")
-
- case alert.update_type.to_sym
- when :cleared
- m.body = "An alert has been cleared:\n"+summary_formatted+"\n\n"
- when :raised
- m.body = "An alert has been raised:\n"+summary_formatted+"\n\n"
- when :acknowledged
- m.body = "An alert has been acknowledged by #{alert.acknowledged_by}:\n"+summary_formatted+"\n\n"
- when :changed
- m.body = "An alert has changed in nature:\n"+summary_formatted+"\n\n"
- else
- raise ArgumentError.new("Unknown update_type #{alert.update_type}")
+ txt_template = File.join(File.dirname(__FILE__), "templates", "email.txt.erb")
+ if File.exists?(txt_template)
+ txt = RMail::Message.new
+ txt.header['Content-Type'] = "text/plain; charset=\"utf-8\""
+ txt.body = ERB.new(File.read(txt_template)).result(binding).chomp
+ m.add_part(txt)
end
-
- # FIXME: include alert.detail as multipart mime
- ##Thread.abort_on_exception = true
- m.body += "\n" + '-'*10 + " This is the detail field " + '-'*44 + "\n\n"
- m.body += alert.detail.to_s
-#' m.body += alert.get_details_plain_text()
- m.body += "\n" + '-'*80 + "\n\n"
-
- if @suppressed_changed == true
- m.body += <<-END
-IMPORTANT: I've been configured to suppress notification of individual changes
-to alerts until their rate decreases. If you still need notification of evrey
-single alert, you must watch the web front-end instead.
- END
- elsif @suppressed_changed == false
- m.body += "(Notifications have slowed down - you will now be notified of every change)\n\n"
- end
-
- if other_alerts.empty?
- m.body += (alert.update_type == :cleared ? "That was" : "This is")+
- " currently the only alert outstanding\n\n"
- else
- m.body += other_alerts.length == 1 ?
- "There is currently one other alert outstanding:\n\n" :
- "There are currently #{other_alerts.length} other alerts outstanding:\n\n"
-
- other_alerts.each do |other|
- m.body += " * "+other.summary_two_lines.join("\n ")+"\n\n"
- end
+ html_template = File.join(File.dirname(__FILE__), "templates", "email.html.erb")
+ if File.exists?(html_template)
+ html = RMail::Message.new
+ html.header['Content-Type'] = "text/html; charset=\"utf-8\""
+ html.body = ERB.new(File.read(html_template)).result(binding).chomp
+ m.add_part(html)
end
-
- m.body += "-- \n"+@signature
-
+
m.to_s
end
include Debug
diff --git a/lib/mauve/notifiers/templates/email.html.erb b/lib/mauve/notifiers/templates/email.html.erb
index e69de29..fcf0620 100644
--- a/lib/mauve/notifiers/templates/email.html.erb
+++ b/lib/mauve/notifiers/templates/email.html.erb
@@ -0,0 +1,28 @@
+<p><strong><%= alert.update_type.upcase %></strong> <%
+case alert.update_type
+when :cleared
+%><%= MauveTime.now.to_s_relative(alert.cleared_at.to_time) %><%
+when :acknowleged
+%><%= MauveTime.now.to_s_relative(alert.acknowledged_at.to_time) %><%
+else
+%><%= MauveTime.now.to_s_relative(alert.raised_at.to_time) %><%
+end
+%>: <%= alert.summary %><%
+if alert.source != alert.subject
+%> -- <em>from <%= alert.source %></em><%
+end
+%>.
+
+<hr />
+
+<div>
+<%=alert.detail %>
+</div>
+
+<hr />
+
+<address>
+--<br />
+Love mauve<br />
+xxx.
+</address>
diff --git a/lib/mauve/notifiers/templates/email.txt.erb b/lib/mauve/notifiers/templates/email.txt.erb
index e69de29..8f7e9a4 100644
--- a/lib/mauve/notifiers/templates/email.txt.erb
+++ b/lib/mauve/notifiers/templates/email.txt.erb
@@ -0,0 +1,24 @@
+<%= alert.update_type.upcase %>: <%
+case alert.update_type
+when :cleared
+%><%= MauveTime.now.to_s_relative(alert.cleared_at.to_time) %><%
+when :acknowleged
+%><%= MauveTime.now.to_s_relative(alert.acknowledged_at.to_time) %><%
+else
+%><%= MauveTime.now.to_s_relative(alert.raised_at.to_time) %><%
+end
+%>: <%= alert.summary %><%
+if alert.source != alert.subject
+%> -- from <%= alert.source %><%
+end
+%>.
+
+-- Details ------------------------------------------------------------
+
+<%= Sanitize.clean(alert.detail) %>
+
+-----------------------------------------------------------------------
+
+--
+Love mauve.
+xx
diff --git a/lib/mauve/notifiers/templates/sms.txt.erb b/lib/mauve/notifiers/templates/sms.txt.erb
index e69de29..802c711 100644..120000
--- a/lib/mauve/notifiers/templates/sms.txt.erb
+++ b/lib/mauve/notifiers/templates/sms.txt.erb
@@ -0,0 +1 @@
+xmpp.txt.erb \ No newline at end of file
diff --git a/lib/mauve/notifiers/templates/xmpp.html.erb b/lib/mauve/notifiers/templates/xmpp.html.erb
deleted file mode 100644
index e69de29..0000000
--- a/lib/mauve/notifiers/templates/xmpp.html.erb
+++ /dev/null
diff --git a/lib/mauve/notifiers/templates/xmpp.txt.erb b/lib/mauve/notifiers/templates/xmpp.txt.erb
index 881c197..22e2d8c 100644
--- a/lib/mauve/notifiers/templates/xmpp.txt.erb
+++ b/lib/mauve/notifiers/templates/xmpp.txt.erb
@@ -1 +1,14 @@
-<%=arse %>
+<%= alert.update_type.upcase %>: <%
+case alert.update_type
+when :cleared
+%><%= MauveTime.now.to_s_relative(alert.cleared_at.to_time) %><%
+when :acknowleged
+%><%= MauveTime.now.to_s_relative(alert.acknowledged_at.to_time) %><%
+else
+%><%= MauveTime.now.to_s_relative(alert.raised_at.to_time) %><%
+end
+%>: <%= alert.summary %><%
+if alert.source != alert.subject
+%> -- from <%= alert.source %><%
+end
+%>.
diff --git a/lib/mauve/notifiers/xmpp.rb b/lib/mauve/notifiers/xmpp.rb
index fbc9640..4c30643 100644
--- a/lib/mauve/notifiers/xmpp.rb
+++ b/lib/mauve/notifiers/xmpp.rb
@@ -165,18 +165,10 @@ module Mauve
end
end
+ #
# Takes an alert and converts it into a message.
#
- # @param [Alert] alert The alert to convert.
- # @return [String] The message, either as HTML.
def convert_alert_to_message(alert)
- arr = alert.summary_three_lines
- str = arr[0] + ": " + arr[1]
- str += " -- " + arr[2] if false == arr[2].nil?
- str += "."
- return str
- #return alert.summary_two_lines.join(" -- ")
- #return "<p>" + alert.summary_two_lines.join("<br />") + "</p>"
end
# Attempt to send an alert using XMPP.
@@ -201,8 +193,17 @@ module Mauve
logger.info("Alert conditions not met, not sending XMPP alert to #{destination_jid}")
return false
end
+
+ template_file = File.join(File.dirname(__FILE__),"templates","xmpp.txt.erb")
+
+ txt = if File.exists?(template_file)
+ ERB.new(File.read(template_file)).result(binding).chomp
+ else
+ logger.error("Could not find xmpp.txt.erb template")
+ alert.to_s
+ end
- send_message(destination_jid, convert_alert_to_message(alert))
+ send_message(destination_jid, txt)
end
# Sends a message to the destionation.
@@ -413,7 +414,7 @@ module Mauve
if @mucs[msg.from.strip].is_a?(MUC::MUCClient) and
msg.from != @mucs[msg.from.strip].jid and
msg.x("jabber:x:delay") == nil and
- (msg.body =~ /\b#{Regexp.escape(@client.jid.resource)}\b/i or
+ (msg.body =~ /\b#{Regexp.escape(@mucs[msg.from.strip].jid.resource)}\b/i or
msg.body =~ /\b#{Regexp.escape(@client.jid.node)}\b/i)
receive_normal_message(msg)
end