xmpp4r-robot 0.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1546d0ef0a762f29b06026368758036e5675b5a8
4
- data.tar.gz: 9de0ef3dba793af53cfefbc37260ed778354781e
3
+ metadata.gz: 62c1f2e10c7d3b9bbff8750f1c122e67e6e20665
4
+ data.tar.gz: eca785d782872d6076e81d2d872e9a8a5f0059bd
5
5
  SHA512:
6
- metadata.gz: 31c4b5c1728638a547f2a1b4cd7da0867e062a21c36ebe03c580510e9923374ba4a5cbaa02b72386360ae94de791da82fe233a5446fcb206095ae181ed93fc78
7
- data.tar.gz: d78ffd1965952d2cddf91ec99516be7579a3428caa22dfe7bd9a6d52ba22f900ddaba5906740919ba5968fdc915ba78183cb5ae8bf1fd4bf9d0b560a31a41503
6
+ metadata.gz: e11bfd191df92527c20cd4a1765e53336955af0c9878d121bfe9393e611c6fd56a455b915093f69e915b15cf95c062449249fb5e6c68780f74b6104e4bdb9e28
7
+ data.tar.gz: 3af9add9f15286f162cb96c5c250e4ff624d2f40f9ab9b106f7ac03a89f79dcdcf007ccb93473688c32b5d590378eb35081a353434c0ae1c28484cb5029bddd9
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ /pkg/
data/CHANGES.md ADDED
@@ -0,0 +1,12 @@
1
+ # CHANGES
2
+
3
+ ## xmpp4r-robot 0.2.0 -- 2014-01-23
4
+
5
+ * Jabber::Robot#start would now act as restart.
6
+ * Jabber::Robot#roster added which would return a list of roster,
7
+ with the state of :available, :away, :unavailable, and :unknown.
8
+ * Now we never consume any events.
9
+
10
+ ## xmpp4r-robot 0.1 -- 2014-01-22
11
+
12
+ * Birthday!
data/README.md CHANGED
@@ -35,9 +35,9 @@ robot = Jabber::Robot.new('someone@gmail.com', 'awesome password',
35
35
  :retry_time => 10,
36
36
  :auto_accept_subscription => true)
37
37
 
38
- robot.notify_presence do |from, reason|
39
- # reason could be one of :available, :away, :unavailable
40
- puts "#{from} is #{reason}"
38
+ robot.notify_presence do |from, status|
39
+ # status could be one of :available, :away, :unavailable
40
+ puts "#{from} is #{status}"
41
41
  end
42
42
 
43
43
  robot.notify_message do |from, body|
@@ -47,10 +47,10 @@ robot.notify_message do |from, body|
47
47
  robot.subscribe(from) if body == 'subscribe' # demonstrate how we subscribe
48
48
  end
49
49
 
50
- robot.start
50
+ p robot.start.roster
51
51
 
52
52
  rd, wr = IO.pipe
53
- Signal.trap('INT'){ wr.puts
53
+ Signal.trap('INT'){ wr.puts }
54
54
  rd.gets
55
55
 
56
56
  robot.stop
data/Rakefile CHANGED
@@ -8,6 +8,6 @@ end
8
8
 
9
9
  Gemgem.init(dir) do |s|
10
10
  s.name = 'xmpp4r-robot'
11
- s.version = '0.1'
12
- %w[xmpp4r].each{ |g| s.add_runtime_dependency(g) }
11
+ s.version = '0.2.0'
12
+ s.add_runtime_dependency('xmpp4r', '~> 0.5')
13
13
  end
data/lib/xmpp4r/robot.rb CHANGED
@@ -1,4 +1,6 @@
1
1
 
2
+ require 'set'
3
+
2
4
  require 'xmpp4r'
3
5
  require 'xmpp4r/roster'
4
6
 
@@ -10,36 +12,58 @@ class Jabber::Robot
10
12
  @username = username
11
13
  @password = password
12
14
  @errback = errback
15
+ @roster = {:available => Set.new,
16
+ :away => Set.new,
17
+ :unavailable => Set.new}
13
18
 
14
19
  @retry_time = Float(opts[:retry_time] || 0)
15
20
  @auto_accept_subscription = opts[:auto_accept_subscription]
16
21
  end
17
22
 
18
- # So that you could reset the client by calling:
19
- # robot.client = nil
20
- # robot.client
21
- # This would remove all previously set callbacks.
23
+ def inspect
24
+ "#<#{self.class.name} username=#{username.inspect}>"
25
+ end
26
+
22
27
  def client
23
28
  @client ||= Jabber::Client.new(Jabber::JID::new(username))
24
29
  end
25
30
 
31
+ def helper
32
+ @helper ||= Jabber::Roster::Helper.new(client, false)
33
+ end
34
+
26
35
  def start
36
+ if @client # restart
37
+ stop
38
+ @client = nil
39
+ end
40
+ initialize_callbacks
27
41
  connect
28
42
  login
29
- initialize_callbacks
30
43
  available
44
+ self
31
45
  end
32
46
 
33
47
  def stop
34
48
  client.close
49
+ self
35
50
  end
36
51
 
37
52
  def connect
38
53
  client.connect
54
+ self
39
55
  end
40
56
 
41
57
  def login
42
58
  client.auth(password)
59
+ self
60
+ end
61
+
62
+ ##### getters #####
63
+
64
+ def roster sync=false
65
+ sync_roster if sync || @roster[:unknown].nil?
66
+ @roster
43
67
  end
44
68
 
45
69
  ##### actions #####
@@ -49,33 +73,34 @@ class Jabber::Robot
49
73
  end
50
74
 
51
75
  # e.g. robot.message('someone@example.com', 'Hi!')
52
- def message to, body
53
- client.send(Jabber::Message::new(to, body).set_type(:chat))
76
+ def message to_jid, body
77
+ client.send(Jabber::Message::new(to_jid, body).set_type(:chat))
54
78
  end
55
79
 
56
80
  # e.g. robot.subscribe('someone@example.com')
57
- def subscribe to
58
- roster.accept_subscription(to)
81
+ def subscribe to_jid
82
+ client.send(Jabber::Presence.new.set_type(:subscribe).set_to(to_jid))
59
83
  end
60
84
 
61
85
  ##### callbacks #####
62
86
 
63
- # e.g. robot.notify_presence{ |from, reason| puts "#{from} is #{reason}" }
87
+ # e.g. robot.notify_presence{ |from, status| puts "#{from} is #{status}" }
88
+ # The status could be one of :available, :away, :unavailable
64
89
  def notify_presence
65
90
  client.add_presence_callback do |presence|
66
- # :available, :away, :unavailable
67
- show = presence.type ||
68
- case presence.show # http://xmpp.org/rfcs/rfc3921.html
69
- when nil, :chat
70
- :available
71
- when :away, :dnd, :xa
72
- :away
73
- else
74
- raise "What's this show? #{presence.show}"
75
- end
91
+ status = presence.type ||
92
+ case presence.show # http://xmpp.org/rfcs/rfc3921.html
93
+ when nil, :chat
94
+ :available
95
+ when :away, :dnd, :xa
96
+ :away
97
+ else
98
+ raise "What's this show? #{presence.show}"
99
+ end
76
100
 
77
101
  protect_yield do
78
- yield(jid_to_username(presence.from), show)
102
+ yield(jid_to_username(presence.from), status)
103
+ false
79
104
  end
80
105
  end
81
106
  end
@@ -84,8 +109,10 @@ class Jabber::Robot
84
109
  def notify_message
85
110
  client.add_message_callback do |message|
86
111
  protect_yield do
87
- yield(jid_to_username(message.from), message.body.strip) if
88
- message.body
112
+ if message.body
113
+ yield(jid_to_username(message.from), message.body.strip)
114
+ end
115
+ false
89
116
  end
90
117
  end
91
118
  end
@@ -93,10 +120,6 @@ class Jabber::Robot
93
120
  ##### private #####
94
121
 
95
122
  private
96
- def roster
97
- @roster ||= Jabber::Roster::Helper.new(client)
98
- end
99
-
100
123
  def initialize_callbacks
101
124
  client.on_exception do |exp|
102
125
  errback.call(exp) if errback
@@ -109,8 +132,23 @@ class Jabber::Robot
109
132
  start
110
133
  end
111
134
 
112
- roster.add_subscription_request_callback do |_, presence|
113
- subscribe(presence.from) if auto_accept_subscription
135
+ client.add_presence_callback do |presence|
136
+ if auto_accept_subscription && presence.type == :subscribe
137
+ subscribe(presence.from)
138
+ end
139
+ false
140
+ end
141
+
142
+ notify_presence do |jid, status|
143
+ @roster[:unknown].delete(jid) if @roster[:unknown]
144
+
145
+ [:available, :away, :unavailable].each do |type|
146
+ if type == status
147
+ @roster[type].add(jid)
148
+ else
149
+ @roster[type].delete(jid)
150
+ end
151
+ end
114
152
  end
115
153
  end
116
154
 
@@ -123,4 +161,19 @@ class Jabber::Robot
123
161
  rescue => e
124
162
  errback.call(e) if errback
125
163
  end
164
+
165
+ def sync_roster
166
+ clear_roster_semaphore
167
+ helper.get_roster
168
+ helper.wait_for_roster
169
+ @roster[:unknown] =
170
+ Set.new(helper.items.keys.map(&method(:jid_to_username))) -
171
+ @roster[:available] - @roster[:away] - @roster[:unavailable]
172
+ end
173
+
174
+ # a hack to let us always get the latest roster
175
+ def clear_roster_semaphore
176
+ helper.instance_variable_get(:@roster_wait).
177
+ instance_variable_set(:@tickets, 0)
178
+ end
126
179
  end
data/xmpp4r-robot.gemspec CHANGED
@@ -1,19 +1,21 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: xmpp4r-robot 0.1 ruby lib
2
+ # stub: xmpp4r-robot 0.2.0 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "xmpp4r-robot"
6
- s.version = "0.1"
6
+ s.version = "0.2.0"
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib"]
10
10
  s.authors = ["Lin Jen-Shin (godfat)"]
11
- s.date = "2014-01-22"
11
+ s.date = "2014-01-23"
12
12
  s.description = "Simple XMPP client built upon xmpp4r. Intended for building simple robots."
13
13
  s.email = ["godfat (XD) godfat.org"]
14
14
  s.files = [
15
+ ".gitignore",
15
16
  ".gitmodules",
16
17
  ".travis.yml",
18
+ "CHANGES.md",
17
19
  "Gemfile",
18
20
  "LICENSE",
19
21
  "README.md",
@@ -32,11 +34,11 @@ Gem::Specification.new do |s|
32
34
  s.specification_version = 4
33
35
 
34
36
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
35
- s.add_runtime_dependency(%q<xmpp4r>, [">= 0"])
37
+ s.add_runtime_dependency(%q<xmpp4r>, ["~> 0.5"])
36
38
  else
37
- s.add_dependency(%q<xmpp4r>, [">= 0"])
39
+ s.add_dependency(%q<xmpp4r>, ["~> 0.5"])
38
40
  end
39
41
  else
40
- s.add_dependency(%q<xmpp4r>, [">= 0"])
42
+ s.add_dependency(%q<xmpp4r>, ["~> 0.5"])
41
43
  end
42
44
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xmpp4r-robot
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lin Jen-Shin (godfat)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-22 00:00:00.000000000 Z
11
+ date: 2014-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: xmpp4r
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '0.5'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '0.5'
27
27
  description: Simple XMPP client built upon xmpp4r. Intended for building simple robots.
28
28
  email:
29
29
  - godfat (XD) godfat.org
@@ -31,8 +31,10 @@ executables: []
31
31
  extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
+ - ".gitignore"
34
35
  - ".gitmodules"
35
36
  - ".travis.yml"
37
+ - CHANGES.md
36
38
  - Gemfile
37
39
  - LICENSE
38
40
  - README.md