Class: Jabber::Robot

Inherits:
Object
  • Object
show all
Defined in:
lib/xmpp4r/robot.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, opts = {}, &errback) ⇒ Robot

Returns a new instance of Robot.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/xmpp4r/robot.rb', line 12

def initialize username, password, opts={}, &errback
  @username = username
  @password = password
  @errback  = errback
  @roster   = {:available   => Set.new,
               :away        => Set.new,
               :unavailable => Set.new}

  @retry_time               = 1
  @auto_accept_subscription = opts[:auto_accept_subscription]

  @roster_mutex = Mutex.new
  @helper_mutex = Mutex.new
  @client_mutex = Mutex.new
  @client   = nil # eliminate warning
  @sleeping = false
end

Instance Attribute Details

#auto_accept_subscriptionObject

Returns the value of attribute auto_accept_subscription.



9
10
11
# File 'lib/xmpp4r/robot.rb', line 9

def auto_accept_subscription
  @auto_accept_subscription
end

#clientObject

Returns the value of attribute client.



9
10
11
# File 'lib/xmpp4r/robot.rb', line 9

def client
  @client
end

#errbackObject

Returns the value of attribute errback.



9
10
11
# File 'lib/xmpp4r/robot.rb', line 9

def errback
  @errback
end

#passwordObject

Returns the value of attribute password.



9
10
11
# File 'lib/xmpp4r/robot.rb', line 9

def password
  @password
end

#retry_timeObject

Returns the value of attribute retry_time.



9
10
11
# File 'lib/xmpp4r/robot.rb', line 9

def retry_time
  @retry_time
end

#usernameObject

Returns the value of attribute username.



9
10
11
# File 'lib/xmpp4r/robot.rb', line 9

def username
  @username
end

Instance Method Details

#availableObject

actions #####



86
87
88
# File 'lib/xmpp4r/robot.rb', line 86

def available
  client.send(Jabber::Presence.new.set_type(:available))
end

#connectObject



67
68
69
70
# File 'lib/xmpp4r/robot.rb', line 67

def connect
  client.connect
  self
end

#helperObject



38
39
40
# File 'lib/xmpp4r/robot.rb', line 38

def helper
  @helper ||= Jabber::Roster::Helper.new(client, false)
end

#inspectObject



30
31
32
# File 'lib/xmpp4r/robot.rb', line 30

def inspect
  "#<#{self.class.name} username=#{username.inspect}>"
end

#loginObject



72
73
74
75
# File 'lib/xmpp4r/robot.rb', line 72

def 
  client.auth(password)
  self
end

#message(to_jid, body) ⇒ Object

e.g. robot.message(‘[email protected]’, ‘Hi!’)



91
92
93
# File 'lib/xmpp4r/robot.rb', line 91

def message to_jid, body
  client.send(Jabber::Message::new(to_jid, body).set_type(:chat))
end

#notify_messageObject

e.g. robot.notify_message{ |from, body| puts “#from: #body” }



124
125
126
127
128
129
130
131
132
133
# File 'lib/xmpp4r/robot.rb', line 124

def notify_message
  client.add_message_callback do |message|
    protect_yield do
      if message.body
        yield(jid_to_username(message.from), message.body.strip)
      end
      false
    end
  end
end

#notify_presenceObject

e.g. robot.notify_presence{ |from, status| puts “#from is #status” } The status could be one of :available, :away, :unavailable



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/xmpp4r/robot.rb', line 104

def notify_presence
  client.add_presence_callback do |presence|
    status = presence.type ||
               case presence.show # http://xmpp.org/rfcs/rfc3921.html
               when nil, :chat
                 :available
               when :away, :dnd, :xa
                 :away
               else
                 raise "What's this show? #{presence.show}"
               end

    protect_yield do
      yield(jid_to_username(presence.from), status)
      false
    end
  end
end

#roster(sync = false) ⇒ Object

getters #####



79
80
81
82
# File 'lib/xmpp4r/robot.rb', line 79

def roster sync=false
  sync_roster if sync || @roster[:unknown].nil?
  @roster_mutex.synchronize{ @roster }
end

#startObject

Start the robot. This is not thread safe.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/xmpp4r/robot.rb', line 45

def start
  @client_mutex.synchronize do
    @sleeping = false
    if @client # restart
      stop
      @client = nil
    end
    initialize_callbacks
    connect
    
    available
    roster
    @retry_time = 1 # reset retry time after successfully connected
    self
  end
end

#stopObject



62
63
64
65
# File 'lib/xmpp4r/robot.rb', line 62

def stop
  client.close
  self
end

#subscribe(to_jid) ⇒ Object

e.g. robot.subscribe(‘[email protected]’)



96
97
98
# File 'lib/xmpp4r/robot.rb', line 96

def subscribe to_jid
  client.send(Jabber::Presence.new.set_type(:subscribe).set_to(to_jid))
end