mack-notifier 0.8.0.101 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/mack-notifier.rb +4 -1
- data/lib/mack-notifier/adapters/xmpp_msg.rb +61 -0
- data/lib/mack-notifier/delivery_handlers/sendmail.rb +1 -1
- data/lib/mack-notifier/delivery_handlers/smtp.rb +1 -1
- data/lib/mack-notifier/delivery_handlers/xmpp_transport.rb +78 -0
- data/lib/mack-notifier/errors.rb +44 -0
- data/lib/mack-notifier/loader.rb +4 -2
- data/lib/mack-notifier/settings.rb +14 -5
- data/lib/mack-notifier/validations.rb +4 -0
- metadata +24 -2
    
        data/lib/mack-notifier.rb
    CHANGED
    
    | @@ -1,6 +1,9 @@ | |
| 1 1 | 
             
            require 'rubygems'
         | 
| 2 2 | 
             
            require 'validatable'
         | 
| 3 3 | 
             
            require 'tmail'
         | 
| 4 | 
            +
            # require 'xmpp4r/client'
         | 
| 5 | 
            +
            # require 'xmpp4r/roster'
         | 
| 6 | 
            +
            require 'xmpp4r-simple'
         | 
| 4 7 |  | 
| 5 8 | 
             
            fl = File.join(File.dirname(__FILE__), "mack-notifier")
         | 
| 6 9 |  | 
| @@ -22,4 +25,4 @@ require File.join(fl, "validations") | |
| 22 25 |  | 
| 23 26 | 
             
            require File.join(fl, "testing")
         | 
| 24 27 |  | 
| 25 | 
            -
            require File.join(fl, "loader")
         | 
| 28 | 
            +
            require File.join(fl, "loader")
         | 
| @@ -0,0 +1,61 @@ | |
| 1 | 
            +
            module Mack
         | 
| 2 | 
            +
              module Notifier
         | 
| 3 | 
            +
                module Adapters # :nodoc:
         | 
| 4 | 
            +
                  
         | 
| 5 | 
            +
                  class XmppMsgContainer 
         | 
| 6 | 
            +
                    attr_accessor :messages
         | 
| 7 | 
            +
                    attr_accessor :recipients
         | 
| 8 | 
            +
                    def initialize
         | 
| 9 | 
            +
                      self.messages = []
         | 
| 10 | 
            +
                      self.recipients = []
         | 
| 11 | 
            +
                    end
         | 
| 12 | 
            +
                    
         | 
| 13 | 
            +
                    def find_message_by_recipient(rcpt)
         | 
| 14 | 
            +
                      self.messages.find { |x| x.to.to_s == rcpt.to_s }
         | 
| 15 | 
            +
                    end
         | 
| 16 | 
            +
                    
         | 
| 17 | 
            +
                  end
         | 
| 18 | 
            +
                  
         | 
| 19 | 
            +
                  # All mail adapters need to extend this class.
         | 
| 20 | 
            +
                  class Xmpp < Mack::Notifier::Adapters::Base
         | 
| 21 | 
            +
                    include Jabber
         | 
| 22 | 
            +
                    
         | 
| 23 | 
            +
                    # The transformed (ie, converted, object)
         | 
| 24 | 
            +
                    def transformed
         | 
| 25 | 
            +
                      raise Mack::Errors::UnconvertedNotifier.new if @xmpp_msg.nil?
         | 
| 26 | 
            +
                      return @xmpp_container
         | 
| 27 | 
            +
                    end
         | 
| 28 | 
            +
                    
         | 
| 29 | 
            +
                    # Convert the Mack::Notifier object to the adapted object.
         | 
| 30 | 
            +
                    def convert
         | 
| 31 | 
            +
                      settings = configatron.mack.notifier.xmpp_settings
         | 
| 32 | 
            +
                      arr = [mack_notifier.to].flatten
         | 
| 33 | 
            +
                      @xmpp_container = XmppMsgContainer.new
         | 
| 34 | 
            +
                      @xmpp_container.recipients = arr
         | 
| 35 | 
            +
                      
         | 
| 36 | 
            +
                      arr.each do |rcpt|
         | 
| 37 | 
            +
                        xmpp_msg = Message::new
         | 
| 38 | 
            +
                        xmpp_msg.set_type(:normal)
         | 
| 39 | 
            +
                        xmpp_msg.set_to(rcpt)
         | 
| 40 | 
            +
                        xmpp_msg.set_from(mack_notifier.from)
         | 
| 41 | 
            +
                        xmpp_msg.set_subject(mack_notifier.subject)
         | 
| 42 | 
            +
                        xmpp_msg.set_type(settings.message_type)
         | 
| 43 | 
            +
                        unless mack_notifier.body(:plain).blank?
         | 
| 44 | 
            +
                          xmpp_msg.set_body(mack_notifier.body(:plain))
         | 
| 45 | 
            +
                        end
         | 
| 46 | 
            +
             | 
| 47 | 
            +
                        @xmpp_container.messages << xmpp_msg
         | 
| 48 | 
            +
                      end
         | 
| 49 | 
            +
                      
         | 
| 50 | 
            +
                      return @xmpp_container
         | 
| 51 | 
            +
                    end
         | 
| 52 | 
            +
                    
         | 
| 53 | 
            +
                    # The RAW encoded String ready for delivery via SMTP, Sendmail, etc...
         | 
| 54 | 
            +
                    def deliverable
         | 
| 55 | 
            +
                      return @xmpp_container
         | 
| 56 | 
            +
                    end
         | 
| 57 | 
            +
                    
         | 
| 58 | 
            +
                  end # Base
         | 
| 59 | 
            +
                end # Adapters
         | 
| 60 | 
            +
              end # Notifier
         | 
| 61 | 
            +
            end # Mack
         | 
| @@ -5,7 +5,7 @@ module Mack | |
| 5 5 | 
             
                  module Sendmail
         | 
| 6 6 |  | 
| 7 7 | 
             
                    def self.deliver(mail)
         | 
| 8 | 
            -
                      sendmail_settings = configatron.mack.notifier. | 
| 8 | 
            +
                      sendmail_settings = configatron.mack.notifier.sendmail
         | 
| 9 9 | 
             
                      sendmail_args = sendmail_settings.arguments
         | 
| 10 10 | 
             
                      sendmail_args += " -f \"#{mail.reply_to}\"" if mail.reply_to
         | 
| 11 11 | 
             
                      IO.popen("#{sendmail_settings.location} #{sendmail_args}","w+") do |sm|
         | 
| @@ -6,7 +6,7 @@ module Mack | |
| 6 6 | 
             
                  module Smtp
         | 
| 7 7 |  | 
| 8 8 | 
             
                    def self.deliver(mail)
         | 
| 9 | 
            -
                      smtp_settings = configatron.mack.notifier. | 
| 9 | 
            +
                      smtp_settings = configatron.mack.notifier.smtp
         | 
| 10 10 | 
             
                      Net::SMTP.start(smtp_settings.address, smtp_settings.port, 
         | 
| 11 11 | 
             
                                      smtp_settings.domain, smtp_settings.retrieve(:user_name, nil), 
         | 
| 12 12 | 
             
                                      smtp_settings.retrieve(:password, nil), smtp_settings.retrieve(:authentication, nil)) do |smtp|
         | 
| @@ -0,0 +1,78 @@ | |
| 1 | 
            +
            module Mack
         | 
| 2 | 
            +
              module Notifier
         | 
| 3 | 
            +
                module DeliveryHandlers # :nodoc:
         | 
| 4 | 
            +
                  # Delivers Mack::Notifier objects using XMPP (Jabber)
         | 
| 5 | 
            +
                  module XmppTransport
         | 
| 6 | 
            +
                    
         | 
| 7 | 
            +
                    def self.check_availability(client, recipients)
         | 
| 8 | 
            +
                      buddies = {}
         | 
| 9 | 
            +
                      recipients.each do |rcpt|
         | 
| 10 | 
            +
                        buddies[rcpt] = :offline
         | 
| 11 | 
            +
                      end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                      # try to get the presence update
         | 
| 14 | 
            +
                      sleep(1)
         | 
| 15 | 
            +
                      client.presence_updates do |update|
         | 
| 16 | 
            +
                        from = update[0].to_s
         | 
| 17 | 
            +
                        presence = update[1].to_s
         | 
| 18 | 
            +
                        if buddies.has_key?(from)
         | 
| 19 | 
            +
                          buddies[from] = presence
         | 
| 20 | 
            +
                        end
         | 
| 21 | 
            +
                        Mack.logger.info "#{from} --> #{presence}"
         | 
| 22 | 
            +
                      end
         | 
| 23 | 
            +
                      return buddies
         | 
| 24 | 
            +
                    end
         | 
| 25 | 
            +
                    
         | 
| 26 | 
            +
                    def self.deliver_message(client, xmpp_msg, wait_for_response, xmpp_errors)
         | 
| 27 | 
            +
                      Mack.logger.info "#{xmpp_msg.to} is online, delivering message"
         | 
| 28 | 
            +
                      client.deliver(xmpp_msg.to, xmpp_msg)
         | 
| 29 | 
            +
                      loop do
         | 
| 30 | 
            +
                        client.received_messages { |ret_msg| @msg_received = true if ret_msg.body == "ack" }
         | 
| 31 | 
            +
                        break if @msg_received or @error
         | 
| 32 | 
            +
                      end if wait_for_response
         | 
| 33 | 
            +
                      return xmpp_errors
         | 
| 34 | 
            +
                    end
         | 
| 35 | 
            +
                    
         | 
| 36 | 
            +
                    def self.deliver(xmpp_msg)
         | 
| 37 | 
            +
                      @ex ||= Mack::Errors::XmppError.new("xmpp error")
         | 
| 38 | 
            +
                      xmpp_settings = configatron.mack.notifier.xmpp
         | 
| 39 | 
            +
                      jid_str = xmpp_settings.jid
         | 
| 40 | 
            +
                      jid_str += ("/" + xmpp_settings.jid_resource) if !jid_str.index("/")
         | 
| 41 | 
            +
                      password = xmpp_settings.password
         | 
| 42 | 
            +
                      client = Jabber::Simple.new(jid_str, password)
         | 
| 43 | 
            +
                      thr = Thread.new {
         | 
| 44 | 
            +
                        begin
         | 
| 45 | 
            +
                          xmpp_msg = xmpp_msg.deliverable
         | 
| 46 | 
            +
                          Timeout::timeout(20) do
         | 
| 47 | 
            +
                            buddies = check_availability(client, xmpp_msg.recipients)
         | 
| 48 | 
            +
             | 
| 49 | 
            +
                            online_buddies = buddies.keys.reject { |x| buddies[x].to_sym == :offline }
         | 
| 50 | 
            +
                            offline_buddies = buddies.keys.reject { |x| buddies[x].to_sym == :online }
         | 
| 51 | 
            +
             | 
| 52 | 
            +
                            if !offline_buddies.empty?
         | 
| 53 | 
            +
                              # there are offline buddies, log the errors
         | 
| 54 | 
            +
                              offline_buddies.each do |buddy|
         | 
| 55 | 
            +
                                err = Mack::Errors::XmppUserNotOnline.new(buddy)
         | 
| 56 | 
            +
                                @ex.add_error(:offline, err)
         | 
| 57 | 
            +
                              end
         | 
| 58 | 
            +
                            end
         | 
| 59 | 
            +
                            
         | 
| 60 | 
            +
                            online_buddies.each do |buddy|
         | 
| 61 | 
            +
                              deliver_message(client, xmpp_msg.find_message_by_recipient(buddy), xmpp_settings.wait_for_response, @ex)
         | 
| 62 | 
            +
                            end
         | 
| 63 | 
            +
                            
         | 
| 64 | 
            +
                            raise @ex if !@ex.empty?
         | 
| 65 | 
            +
                          end # timeout
         | 
| 66 | 
            +
                        rescue Timeout::Error => ex
         | 
| 67 | 
            +
                          @error = ex
         | 
| 68 | 
            +
                        rescue Exception => ex
         | 
| 69 | 
            +
                        end
         | 
| 70 | 
            +
                      }
         | 
| 71 | 
            +
                      
         | 
| 72 | 
            +
                      thr.join
         | 
| 73 | 
            +
                      raise @ex if !@ex.empty?
         | 
| 74 | 
            +
                    end
         | 
| 75 | 
            +
                  end # XmppTransport
         | 
| 76 | 
            +
                end # DeliveryHandlers
         | 
| 77 | 
            +
              end # Notifier
         | 
| 78 | 
            +
            end # Mack
         | 
    
        data/lib/mack-notifier/errors.rb
    CHANGED
    
    | @@ -7,5 +7,49 @@ module Mack | |
| 7 7 | 
             
                  end
         | 
| 8 8 | 
             
                end # UnconvertedNotifier
         | 
| 9 9 |  | 
| 10 | 
            +
                class XmppError < StandardError
         | 
| 11 | 
            +
                  def initialize(msg)
         | 
| 12 | 
            +
                    super(msg)
         | 
| 13 | 
            +
                    self.error_hash = {}
         | 
| 14 | 
            +
                  end
         | 
| 15 | 
            +
                  
         | 
| 16 | 
            +
                  def add_error(type, msg)
         | 
| 17 | 
            +
                    self.error_hash[type] ||= []
         | 
| 18 | 
            +
                    self.error_hash[type] << msg
         | 
| 19 | 
            +
                  end
         | 
| 20 | 
            +
                  
         | 
| 21 | 
            +
                  def get_error(type)
         | 
| 22 | 
            +
                    self.error_hash[type]
         | 
| 23 | 
            +
                  end
         | 
| 24 | 
            +
                  
         | 
| 25 | 
            +
                  def empty?
         | 
| 26 | 
            +
                    self.error_hash.empty?
         | 
| 27 | 
            +
                  end
         | 
| 28 | 
            +
                  
         | 
| 29 | 
            +
                  attr_accessor :error_hash
         | 
| 30 | 
            +
                end
         | 
| 31 | 
            +
                
         | 
| 32 | 
            +
                class XmppAuthenticationError < StandardError
         | 
| 33 | 
            +
                  def initialize(user)
         | 
| 34 | 
            +
                    super("Cannot authenticate: #{user} to xmpp server")
         | 
| 35 | 
            +
                  end
         | 
| 36 | 
            +
                end
         | 
| 37 | 
            +
                
         | 
| 38 | 
            +
                class XmppUserNotOnline < StandardError
         | 
| 39 | 
            +
                  def initialize(user)
         | 
| 40 | 
            +
                    super("user #{user} is not online")
         | 
| 41 | 
            +
                  end
         | 
| 42 | 
            +
                end
         | 
| 43 | 
            +
                
         | 
| 44 | 
            +
                class XmppSendError < StandardError
         | 
| 45 | 
            +
                  attr_reader :code
         | 
| 46 | 
            +
                  attr_reader :msg
         | 
| 47 | 
            +
                  
         | 
| 48 | 
            +
                  def initialize(code, msg)
         | 
| 49 | 
            +
                    super("Cannot send message. Code=#{code}, Msg=#{msg}")
         | 
| 50 | 
            +
                    @code = code
         | 
| 51 | 
            +
                    @msg = msg
         | 
| 52 | 
            +
                  end
         | 
| 53 | 
            +
                end
         | 
| 10 54 | 
             
              end # Errors
         | 
| 11 55 | 
             
            end # Mack
         | 
    
        data/lib/mack-notifier/loader.rb
    CHANGED
    
    | @@ -2,6 +2,8 @@ | |
| 2 2 | 
             
            FileUtils.mkdir_p(Mack::Paths.notifiers)
         | 
| 3 3 |  | 
| 4 4 | 
             
            # Require all notifiers
         | 
| 5 | 
            -
             | 
| 6 | 
            -
               | 
| 5 | 
            +
            Mack.search_path(:app).each do |path|
         | 
| 6 | 
            +
              Dir.glob(File.join(path, 'notifiers', "**/*.rb")).each do |notifier|
         | 
| 7 | 
            +
                require File.expand_path(notifier)
         | 
| 8 | 
            +
              end
         | 
| 7 9 | 
             
            end
         | 
| @@ -1,7 +1,16 @@ | |
| 1 | 
            -
            configatron.mack.notifier. | 
| 2 | 
            -
            configatron.mack.notifier. | 
| 3 | 
            -
            configatron.mack.notifier. | 
| 4 | 
            -
            configatron.mack.notifier. | 
| 5 | 
            -
            configatron.mack.notifier. | 
| 1 | 
            +
            configatron.mack.notifier.sendmail.set_default(:location, '/usr/sbin/sendmail')
         | 
| 2 | 
            +
            configatron.mack.notifier.sendmail.set_default(:arguments, '-i -t')
         | 
| 3 | 
            +
            configatron.mack.notifier.smtp.set_default(:address, 'localhost')
         | 
| 4 | 
            +
            configatron.mack.notifier.smtp.set_default(:port, 25)
         | 
| 5 | 
            +
            configatron.mack.notifier.smtp.set_default(:domain, 'localhost.localdomain')
         | 
| 6 6 | 
             
            configatron.mack.notifier.set_default(:deliver_with, (Mack.env == "test" ? "test" : "smtp"))
         | 
| 7 7 | 
             
            configatron.mack.notifier.set_default(:adapter, 'tmail')
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            # xmpp settings
         | 
| 10 | 
            +
            configatron.mack.notifier.xmpp.set_default(:jid, 'h_test@jabber80.com')
         | 
| 11 | 
            +
            configatron.mack.notifier.xmpp.set_default(:jid_resource, 'work')
         | 
| 12 | 
            +
            configatron.mack.notifier.xmpp.set_default(:password, 'test1234')
         | 
| 13 | 
            +
            configatron.mack.notifier.xmpp.set_default(:message_type, :chat)
         | 
| 14 | 
            +
            configatron.mack.notifier.xmpp.set_default(:wait_for_response, true)
         | 
| 15 | 
            +
            configatron.mack.notifier.xmpp.set_default(:response_wait_time, 20)
         | 
| 16 | 
            +
            configatron.mack.notifier.xmpp.set_default(:response_message, "ack")
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification 
         | 
| 2 2 | 
             
            name: mack-notifier
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version 
         | 
| 4 | 
            -
              version: 0.8. | 
| 4 | 
            +
              version: 0.8.1
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors: 
         | 
| 7 7 | 
             
            - Mark Bates
         | 
| @@ -9,7 +9,7 @@ autorequire: | |
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 11 |  | 
| 12 | 
            -
            date: 2008-10- | 
| 12 | 
            +
            date: 2008-10-26 00:00:00 -04:00
         | 
| 13 13 | 
             
            default_executable: 
         | 
| 14 14 | 
             
            dependencies: 
         | 
| 15 15 | 
             
            - !ruby/object:Gem::Dependency 
         | 
| @@ -32,6 +32,26 @@ dependencies: | |
| 32 32 | 
             
                  - !ruby/object:Gem::Version 
         | 
| 33 33 | 
             
                    version: 1.6.7
         | 
| 34 34 | 
             
                version: 
         | 
| 35 | 
            +
            - !ruby/object:Gem::Dependency 
         | 
| 36 | 
            +
              name: xmpp4r
         | 
| 37 | 
            +
              type: :runtime
         | 
| 38 | 
            +
              version_requirement: 
         | 
| 39 | 
            +
              version_requirements: !ruby/object:Gem::Requirement 
         | 
| 40 | 
            +
                requirements: 
         | 
| 41 | 
            +
                - - "="
         | 
| 42 | 
            +
                  - !ruby/object:Gem::Version 
         | 
| 43 | 
            +
                    version: "0.4"
         | 
| 44 | 
            +
                version: 
         | 
| 45 | 
            +
            - !ruby/object:Gem::Dependency 
         | 
| 46 | 
            +
              name: xmpp4r-simple
         | 
| 47 | 
            +
              type: :runtime
         | 
| 48 | 
            +
              version_requirement: 
         | 
| 49 | 
            +
              version_requirements: !ruby/object:Gem::Requirement 
         | 
| 50 | 
            +
                requirements: 
         | 
| 51 | 
            +
                - - "="
         | 
| 52 | 
            +
                  - !ruby/object:Gem::Version 
         | 
| 53 | 
            +
                    version: 0.8.8
         | 
| 54 | 
            +
                version: 
         | 
| 35 55 | 
             
            description: Notifier functionality for Mack applications.
         | 
| 36 56 | 
             
            email: mark@mackframework.com
         | 
| 37 57 | 
             
            executables: []
         | 
| @@ -43,10 +63,12 @@ extra_rdoc_files: [] | |
| 43 63 | 
             
            files: 
         | 
| 44 64 | 
             
            - lib/mack-notifier/adapters/base.rb
         | 
| 45 65 | 
             
            - lib/mack-notifier/adapters/tmail.rb
         | 
| 66 | 
            +
            - lib/mack-notifier/adapters/xmpp_msg.rb
         | 
| 46 67 | 
             
            - lib/mack-notifier/attachment.rb
         | 
| 47 68 | 
             
            - lib/mack-notifier/delivery_handlers/sendmail.rb
         | 
| 48 69 | 
             
            - lib/mack-notifier/delivery_handlers/smtp.rb
         | 
| 49 70 | 
             
            - lib/mack-notifier/delivery_handlers/test.rb
         | 
| 71 | 
            +
            - lib/mack-notifier/delivery_handlers/xmpp_transport.rb
         | 
| 50 72 | 
             
            - lib/mack-notifier/errors.rb
         | 
| 51 73 | 
             
            - lib/mack-notifier/loader.rb
         | 
| 52 74 | 
             
            - lib/mack-notifier/notifier.rb
         |