Class: Jabot::Jabber
Instance Attribute Summary collapse
-
#is_listen ⇒ Object
readonly
Returns the value of attribute is_listen.
-
#remote_clients ⇒ Object
readonly
Returns the value of attribute remote_clients.
Instance Method Summary collapse
- #add_remote_client(id) ⇒ Object
- #connect ⇒ Object
- #disconnect ⇒ Object
-
#initialize(options) ⇒ Jabber
constructor
A new instance of Jabber.
- #listen ⇒ Object
- #send(to, text) ⇒ Object
- #send_to_all(text) ⇒ Object
Constructor Details
#initialize(options) ⇒ Jabber
Returns a new instance of Jabber.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/jabot/jabber.rb', line 14 def initialize() @client_id = [:client_id] @client_password = [:client_password] @remote_clients = [] @is_listen = false @client = Client::new JID::new(@client_id + '/bot') #@client.on_exception do # puts 'Reconnect JabberClient'; # connect #end connect end |
Instance Attribute Details
#is_listen ⇒ Object (readonly)
Returns the value of attribute is_listen.
12 13 14 |
# File 'lib/jabot/jabber.rb', line 12 def is_listen @is_listen end |
#remote_clients ⇒ Object (readonly)
Returns the value of attribute remote_clients.
11 12 13 |
# File 'lib/jabot/jabber.rb', line 11 def remote_clients @remote_clients end |
Instance Method Details
#add_remote_client(id) ⇒ Object
43 44 45 |
# File 'lib/jabot/jabber.rb', line 43 def add_remote_client(id) @remote_clients << id unless @remote_clients.include?(id) end |
#connect ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/jabot/jabber.rb', line 30 def connect @client.connect @client.auth @client_password presence = Presence.new(:dnd, 'server running') @client.send presence end |
#disconnect ⇒ Object
38 39 40 41 |
# File 'lib/jabot/jabber.rb', line 38 def disconnect @is_listen = false @client.close end |
#listen ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/jabot/jabber.rb', line 58 def listen @is_listen = true @message_callback = @client. 0, @message_callback do |m| if m.type != :error sender_id = "#{m.from.node}@#{m.from.domain}" #puts' "Message received from "' + sender_id# + ": " + m.body unless m.body.nil? if @remote_clients.include?(sender_id) yield(m.body, sender_id) if block_given? else #puts 'access denied' end end end end end |
#send(to, text) ⇒ Object
53 54 55 56 |
# File 'lib/jabot/jabber.rb', line 53 def send(to, text) = Message::new to, text @client.send end |
#send_to_all(text) ⇒ Object
47 48 49 50 51 |
# File 'lib/jabot/jabber.rb', line 47 def send_to_all(text) @remote_clients.each do |to| send to, text end end |