diff options
Diffstat (limited to 'lib/mauve/notifiers')
-rw-r--r-- | lib/mauve/notifiers/templates/email.html.erb | 4 | ||||
-rw-r--r-- | lib/mauve/notifiers/templates/email.txt.erb | 2 | ||||
-rw-r--r-- | lib/mauve/notifiers/templates/xmpp.html.erb | 21 | ||||
-rw-r--r-- | lib/mauve/notifiers/templates/xmpp.txt.erb | 8 | ||||
-rw-r--r-- | lib/mauve/notifiers/xmpp.rb | 75 |
5 files changed, 74 insertions, 36 deletions
diff --git a/lib/mauve/notifiers/templates/email.html.erb b/lib/mauve/notifiers/templates/email.html.erb index f0ce1be..24b5e7b 100644 --- a/lib/mauve/notifiers/templates/email.html.erb +++ b/lib/mauve/notifiers/templates/email.html.erb @@ -1,12 +1,12 @@ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head><title></title></head><body> -<p><strong><%= alert.update_type.upcase %>:</strong> <% +<p><a href="<%= WebInterface.url_for(alert) %>"><%= alert.update_type.upcase %></a>:<% case alert.update_type when "cleared" %><%= alert.cleared_at.to_s_relative %><% when "acknowledged" -%><%= alert.acknowledged_at.to_s_relative %> by <%= alert.acknowledged_by%><% +%><%= alert.acknowledged_at.to_s_relative %> by <%= alert.acknowledged_by%> until <%= alert.will_unacknowledge_at.to_s_human %><% else %><%= alert.raised_at.to_s_relative %><% end diff --git a/lib/mauve/notifiers/templates/email.txt.erb b/lib/mauve/notifiers/templates/email.txt.erb index dc5762b..aab44a8 100644 --- a/lib/mauve/notifiers/templates/email.txt.erb +++ b/lib/mauve/notifiers/templates/email.txt.erb @@ -3,7 +3,7 @@ case alert.update_type when "cleared" %><%= alert.cleared_at.to_s_relative %><% when "acknowledged" -%><%= alert.acknowledged_at.to_s_relative %> by <%= alert.acknowledged_by %><% +%><%= alert.acknowledged_at.to_s_relative %> by <%= alert.acknowledged_by %> until <%= alert.will_unacknowledge_at.to_s_human %><% else %><%= alert.raised_at.to_s_relative %><% end diff --git a/lib/mauve/notifiers/templates/xmpp.html.erb b/lib/mauve/notifiers/templates/xmpp.html.erb new file mode 100644 index 0000000..c6bfaed --- /dev/null +++ b/lib/mauve/notifiers/templates/xmpp.html.erb @@ -0,0 +1,21 @@ + <html xmlns="http://jabber.org/protocol/xhtml-im"><body xmlns="OAhttp://www.w3.org/1999/xhtml"> +<a href="<%=WebInterface.url_for(alert)%>"><%= alert.update_type.upcase %></a>: <% +case alert.update_type +when "cleared" +%><%= alert.cleared_at.to_s_relative %><% +when "acknowledged" +%><%= alert.acknowledged_at.to_s_relative %> by <%= alert.acknowledged_by %> until <%= alert.will_unacknowledge_at.to_s_human %><% +else +%><%= alert.raised_at.to_s_relative %><% +end +%>: <%= alert.subject %>: <%= alert.summary %><% +if alert.source != alert.subject +%> -- from <%= alert.source %><% +end +%>.<% +if was_suppressed and not is_suppressed +%><em> Normal service has resumed.</em><% +elsif is_suppressed and not was_suppressed +%><em> Further alerts suppressed until things calm down.</em><% +end +%></body></html> diff --git a/lib/mauve/notifiers/templates/xmpp.txt.erb b/lib/mauve/notifiers/templates/xmpp.txt.erb index 837fd67..a73f41f 100644 --- a/lib/mauve/notifiers/templates/xmpp.txt.erb +++ b/lib/mauve/notifiers/templates/xmpp.txt.erb @@ -3,7 +3,7 @@ case alert.update_type when "cleared" %><%= alert.cleared_at.to_s_relative %><% when "acknowledged" -%><%= alert.acknowledged_at.to_s_relative %> by <%= alert.acknowledged_by %><% +%><%= alert.acknowledged_at.to_s_relative %> by <%= alert.acknowledged_by %> until <%= alert.will_unacknowledge_at.to_s_human %><% else %><%= alert.raised_at.to_s_relative %><% end @@ -11,10 +11,10 @@ end if alert.source != alert.subject %> -- from <%= alert.source %><% end -%>.<% +%>. <%=WebInterface.url_for(alert)%><% if was_suppressed and not is_suppressed -%> Normal service has resumed.<% +%> (Normal service has resumed.)<% elsif is_suppressed and not was_suppressed -%> Further alerts suppressed until things calm down.<% +%> (Further alerts suppressed until things calm down.)<% end %> diff --git a/lib/mauve/notifiers/xmpp.rb b/lib/mauve/notifiers/xmpp.rb index 7fe1e39..991194d 100644 --- a/lib/mauve/notifiers/xmpp.rb +++ b/lib/mauve/notifiers/xmpp.rb @@ -34,9 +34,11 @@ module Jabber # mean the other parts of the method fail to execute. # That would be bad. So kill parser_thread last @tbcbmutex.synchronize { @processing = 0 } - @fd.close if @fd and !@fd.closed? + if @fd and !@fd.closed? + @fd.close + stop + end @status = DISCONNECTED - stop end end end @@ -66,15 +68,15 @@ module Mauve include Jabber # Atrtribute. - attr_reader :name, :jid + attr_reader :name # Atrtribute. attr_accessor :password def initialize(name) Jabber::logger = self.logger - #Jabber::debug = true - #Jabber::warnings = true +# Jabber::debug = true +# Jabber::warnings = true @name = name @mucs = {} @@ -138,10 +140,11 @@ module Mauve # unless ex.nil? or @closing logger.warn(["Caught",ex.class,ex.to_s,"during XMPP",where].join(" ")) + @closing = true connect @mucs.each do |jid, muc| @mucs.delete(jid) - join_muc(jid) + join_muc(muc[:jid], muc[:password]) end end end @@ -156,11 +159,13 @@ module Mauve def close @closing = true - if @client and @client.is_connected? - @mucs.each do |jid, muc| - muc.exit("Goodbye!") if muc.active? - end - @client.send(Presence.new(nil, "Goodbye!").set_type(:unavailable)) + if @client + if @client.is_connected? + @mucs.each do |jid, muc| + muc[:client].exit("Goodbye!") if muc[:client].active? + end + @client.send(Presence.new(nil, "Goodbye!").set_type(:unavailable)) + end @client.close! end end @@ -202,7 +207,16 @@ module Mauve alert.to_s end - send_message(destination_jid, txt) + template_file = File.join(File.dirname(__FILE__),"templates","xmpp.html.erb") + + xhtml = 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, txt, xhtml) end # Sends a message to the destionation. @@ -210,20 +224,23 @@ module Mauve # @param [String] destination The (full) JID to send to. # @param [String] msg The (formatted) message to send. # @return [NIL] nada. - def send_message(jid, msg) + def send_message(jid, msg, html_msg=nil) jid = JID.new(jid) unless jid.is_a?(JID) message = Message.new(jid) - - #if msg.is_a?(XHTML::HTML) - # message.add_element(msg) - #else message.body = msg - #end + if html_msg + begin + html_msg = REXML::Document.new(html_msg) unless html_msg.is_a?(REXML::Document) + message.add_element(html_msg) + rescue REXML::ParseException + logger.error "Bad XHTML: #{html_msg.inspect}" + end + end if is_muc?(jid) jid = join_muc(jid.strip) - muc = @mucs[jid] + muc = @mucs[jid][:client] if muc message.to = muc.jid @@ -272,28 +289,28 @@ module Mauve logger.debug("Adding new MUC client for #{jid}") - @mucs[jid.strip] = Jabber::MUC::MUCClient.new(@client) + @mucs[jid.strip] = {:jid => jid, :password => password, :client => Jabber::MUC::MUCClient.new(@client)} # Add some callbacks - @mucs[jid.strip].add_message_callback do |m| + @mucs[jid.strip][:client].add_message_callback do |m| receive_message(m) end - @mucs[jid.strip].add_private_message_callback do |m| + @mucs[jid.strip][:client].add_private_message_callback do |m| receive_message(m) end end - if !@mucs[jid.strip].active? + if !@mucs[jid.strip][:client].active? # # Make sure we have a resource. # - @mucs[jid.strip].join(jid, password) + @mucs[jid.strip][:client].join(jid, password) - logger.info("Joined #{jid}") + logger.info("Joined #{jid.strip}") else - logger.debug("Already joined #{jid}.") + logger.debug("Already joined #{jid.strip}.") end # @@ -410,10 +427,10 @@ module Mauve # that we've not sent ourselves, that are not historical, and that # match our resource or node in the body. # - if @mucs[msg.from.strip].is_a?(MUC::MUCClient) and - msg.from != @mucs[msg.from.strip].jid and + if @mucs[msg.from.strip][:client].is_a?(MUC::MUCClient) and + msg.from != @mucs[msg.from.strip][:client].jid and msg.x("jabber:x:delay") == nil and - (msg.body =~ /\b#{Regexp.escape(@mucs[msg.from.strip].jid.resource)}\b/i or + (msg.body =~ /\b#{Regexp.escape(@mucs[msg.from.strip][:client].jid.resource)}\b/i or msg.body =~ /\b#{Regexp.escape(@client.jid.node)}\b/i) receive_normal_message(msg) end |