Class: DaemonKit::Jabber
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.
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/daemon_kit/jabber.rb', line 85 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
75 76 77 |
# File 'lib/daemon_kit/jabber.rb', line 75 def presence_updates(&block) @presence_handler = block end |
.process_messages ⇒ Object
48 49 50 51 52 |
# File 'lib/daemon_kit/jabber.rb', line 48 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
65 66 67 68 69 |
# File 'lib/daemon_kit/jabber.rb', line 65 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
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/daemon_kit/jabber.rb', line 54 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
71 72 73 |
# File 'lib/daemon_kit/jabber.rb', line 71 def (&block) @message_handler = block end |
.run ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/daemon_kit/jabber.rb', line 30 def run DaemonKit.logger.warn "Please use the new XMPP daemons, this class is deprecated" 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
79 80 81 |
# File 'lib/daemon_kit/jabber.rb', line 79 def subscription_requests(&block) @subscription_handler = block end |
Instance Method Details
#busy(&block) ⇒ Object
134 135 136 137 138 |
# File 'lib/daemon_kit/jabber.rb', line 134 def busy(&block) self.connection.status(:dnd, "Working...") yield self.connection.status(:chat, self.status_line ) end |
#contacts ⇒ Object
116 117 118 |
# File 'lib/daemon_kit/jabber.rb', line 116 def contacts @masters + @supporters end |
#shutdown! ⇒ Object
111 112 113 114 |
# File 'lib/daemon_kit/jabber.rb', line 111 def shutdown! DaemonKit.logger.warn "Disconnecting jabber connection" self.connection.disconnect end |
#startup! ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/daemon_kit/jabber.rb', line 97 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
140 141 142 |
# File 'lib/daemon_kit/jabber.rb', line 140 def status_line "#{DaemonKit.configuration.daemon_name} ready for instructions" end |
#valid_master?(jid) ⇒ Boolean
130 131 132 |
# File 'lib/daemon_kit/jabber.rb', line 130 def valid_master?( jid ) @masters.include?( jid.strip.to_s ) end |
#valid_messages(&block) ⇒ Object
120 121 122 123 124 125 126 127 128 |
# File 'lib/daemon_kit/jabber.rb', line 120 def (&block) self.connection..each do || next unless valid_master?( .from ) busy do block.call end end end |