Class: DaemonKit::Jabber
- Inherits:
-
Object
- Object
- DaemonKit::Jabber
- Defined in:
- lib/daemon_kit/jabber.rb
Overview
Thin wrapper around xmpp4r-simple, specifically designed to ease configuration of a jabber daemon and provide some added simplicity.
Constant Summary collapse
- @@instance =
nil
- @@message_handler =
nil
- @@presence_handler =
nil
- @@subscription_handler =
nil
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Jabber connection.
Class Method Summary collapse
-
.deliver(jid, message) ⇒ Object
Deliver a message to the specified jid.
-
.instance ⇒ Object
Use this instead of initializing, keeps it singleton.
- .presence_updates(&block) ⇒ Object
- .process_messages ⇒ Object
- .process_subscriptions ⇒ Object
- .process_updates ⇒ Object
- .received_messages(&block) ⇒ Object
- .run ⇒ Object
- .subscription_requests(&block) ⇒ Object
Instance Method Summary collapse
- #busy(&block) ⇒ Object
- #contacts ⇒ Object
-
#initialize ⇒ Jabber
constructor
A new instance of Jabber.
- #shutdown! ⇒ Object
- #startup! ⇒ Object
- #status_line ⇒ Object
- #valid_master?(jid) ⇒ Boolean
- #valid_messages(&block) ⇒ Object
Constructor Details
#initialize ⇒ Jabber
Returns a new instance of Jabber.
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/daemon_kit/jabber.rb', line 84 def initialize = DaemonKit::Config.load( 'jabber' ) @jabber_id = .delete("jabber_id") @password = .delete("password") @resource = .delete("resource") || 'daemon_kit' @masters = .delete("masters") || [] @supporters = .delete("supporters") || [] raise ArgumentError if [ @jabber_id, @password ].any? { |a| a.nil? } end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Jabber connection
9 10 11 |
# File 'lib/daemon_kit/jabber.rb', line 9 def connection @connection end |
Class Method Details
.deliver(jid, message) ⇒ Object
Deliver a message to the specified jid.
19 20 21 |
# File 'lib/daemon_kit/jabber.rb', line 19 def deliver( jid, ) instance.connection.deliver( jid, ) end |
.instance ⇒ Object
Use this instead of initializing, keeps it singleton
24 25 26 27 |
# File 'lib/daemon_kit/jabber.rb', line 24 def instance @instance ||= new @instance.startup! end |
.presence_updates(&block) ⇒ Object
74 75 76 |
# File 'lib/daemon_kit/jabber.rb', line 74 def presence_updates(&block) @presence_handler = block end |
.process_messages ⇒ Object
47 48 49 50 51 |
# File 'lib/daemon_kit/jabber.rb', line 47 def @message_handler ||= Proc.new { |m| DaemonKit.logger.info "Received message from #{m.from}: #{m.body}" } instance. { |m| @message_handler.call(m) } end |
.process_subscriptions ⇒ Object
64 65 66 67 68 |
# File 'lib/daemon_kit/jabber.rb', line 64 def process_subscriptions @subscription_handler ||= Proc.new { |friend,presence| DaemonKit.logger.debug "Received presence update from #{friend}: #{presence}" } instance.connection.subscription_requests { |friend,presence| @subscription_handler.call(friend,presence) } end |
.process_updates ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/daemon_kit/jabber.rb', line 53 def process_updates @presence_handler ||= Proc.new { |friend, old_presence, new_presence| DaemonKit.logger.debug "Received presence update: #{friend} went from #{old_presence} to #{new_presence}" } instance.connection.presence_updates { |friend, old_presence, new_presence| @presence_handler.call(friend, old_presence, new_presence) } end |
.received_messages(&block) ⇒ Object
70 71 72 |
# File 'lib/daemon_kit/jabber.rb', line 70 def (&block) @message_handler = block end |
.run ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/daemon_kit/jabber.rb', line 30 def run DaemonKit.logger.info "Starting jabber loop" loop do process_updates process_subscriptions begin sleep 1 rescue Interrupt DaemonKit.logger.warn "Jabber loop interrupted" break end end end |
.subscription_requests(&block) ⇒ Object
78 79 80 |
# File 'lib/daemon_kit/jabber.rb', line 78 def subscription_requests(&block) @subscription_handler = block end |
Instance Method Details
#busy(&block) ⇒ Object
133 134 135 136 137 |
# File 'lib/daemon_kit/jabber.rb', line 133 def busy(&block) self.connection.status(:dnd, "Working...") yield self.connection.status(:chat, self.status_line ) end |
#contacts ⇒ Object
115 116 117 |
# File 'lib/daemon_kit/jabber.rb', line 115 def contacts @masters + @supporters end |
#shutdown! ⇒ Object
110 111 112 113 |
# File 'lib/daemon_kit/jabber.rb', line 110 def shutdown! DaemonKit.logger.warn "Disconnecting jabber connection" self.connection.disconnect end |
#startup! ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/daemon_kit/jabber.rb', line 96 def startup! return self if @booted connect! setup_roster! DaemonKit.trap( 'INT', Proc.new { self.shutdown! } ) DaemonKit.trap( 'TERM', Proc.new { self.shutdown! } ) @booted = true self end |
#status_line ⇒ Object
139 140 141 |
# File 'lib/daemon_kit/jabber.rb', line 139 def status_line "#{DaemonKit.configuration.daemon_name} ready for instructions" end |
#valid_master?(jid) ⇒ Boolean
129 130 131 |
# File 'lib/daemon_kit/jabber.rb', line 129 def valid_master?( jid ) @masters.include?( jid.strip.to_s ) end |
#valid_messages(&block) ⇒ Object
119 120 121 122 123 124 125 126 127 |
# File 'lib/daemon_kit/jabber.rb', line 119 def (&block) self.connection..each do || next unless valid_master?( .from ) busy do block.call end end end |