aboutsummaryrefslogtreecommitdiff
path: root/lib/mauve/notifier.rb
blob: e62f7f49ba54f6e9c05ecb8daa27ed6b2fbf7f55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
require 'mauve/mauve_thread'
require 'mauve/notifiers'
require 'mauve/notifiers/xmpp'

module Mauve

  class Notifier < MauveThread

    DEFAULT_XMPP_MESSAGE = "Mauve server started."
    
    include Singleton

    attr_accessor :sleep_interval

    def initialize
      super
    end

    def main_loop
      # 
      # Cycle through the buffer.
      #
      sz = Server.notification_buffer_size

      return if sz == 0
 
      my_threads = []
      sz.times do
        person, *args = Server.notification_pop
        
        #
        # Nil person.. that's craaazy too!
        #
        break if person.nil?
        my_threads << Thread.new {
          person.do_send_alert(*args) 
        }
      end

      my_threads.each do |t|
        begin
          t.join
        rescue StandardError => ex
          logger.error ex.to_s
          logger.debug ex.backtrace.join("\n")
        end
      end
    end

    def start
      super

      if Configuration.current.notification_methods['xmpp']
        #
        # Connect to XMPP server
        #
        xmpp = Configuration.current.notification_methods['xmpp']
        xmpp.connect 

        Configuration.current.people.each do |username, person|
          # 
          # Ignore people without XMPP stanzas.
          #
          next unless person.xmpp

          #
          # For each JID, either ensure they're on our roster, or that we're in
          # that chat room.
          #
          jid = if xmpp.is_muc?(person.xmpp)
            xmpp.join_muc(person.xmpp) 
          else
            xmpp.ensure_roster_and_subscription!(person.xmpp)
          end

          Configuration.current.people[username].xmpp = jid unless jid.nil?
        end
      end
    end

    def stop
      if Configuration.current.notification_methods['xmpp']
        Configuration.current.notification_methods['xmpp'].close
      end

      super

      # 
      # flush the queue
      #
      main_loop
    end

  end

end