require 'xmpp4r-simple' require 'htmlentities/string' module Whistle class Switchbox attr_reader :user def initialize(user,password,subscribers) @user, @password, @subscribers = user, password, subscribers @jabber = Jabber::Simple.new(@user, @password) end def deliver(messages) @subscribers.each do |to| Array(messages).each do |body| format_and_send :to => to, :body => body end end end # Sends message to primary (first) subscriber only def inform(message) if (primary = @subscribers.first) format_and_send :to => @subscribers.first, :body => message end end private # Processes html in the messages and sends it to a recipient def format_and_send(options) m = Jabber::Message::new(options[:to],options[:body]).set_type(:normal) r = REXML::Document.new(options[:body]) rescue nil if r && r.root && r.root.name == 'html' # We have a html message here, prepare for sending via jabber m.body = strip_tags(options[:body]) # Make a plaintext version r.root.add_namespace('http://jabber.org/protocol/xhtml-im') r.root.elements['body'].add_namespace('http://www.w3.org/1999/xhtml') m.add_element(r) end # Use deliver from xmpp4r-simple so it will try adding contact first and queue the delivery @jabber.deliver(options[:to],m) end def strip_tags(html) html.gsub(/<.+?>/,''). gsub(/&/,'&'). gsub(/"/,'"'). gsub(/</,'<'). gsub(/>/,'>'). gsub(/&ellip;/,'...'). gsub(/'/, "'"). gsub("\n",'') end end end