uppercut 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +151 -0
- data/VERSION.yml +4 -0
- data/examples/basic_agent.rb +28 -0
- data/examples/personal.rb +42 -0
- data/lib/uppercut.rb +8 -0
- data/lib/uppercut/agent.rb +223 -0
- data/lib/uppercut/base.rb +90 -0
- data/lib/uppercut/conversation.rb +28 -0
- data/lib/uppercut/message.rb +18 -0
- data/lib/uppercut/notifier.rb +64 -0
- data/spec/agent_spec.rb +278 -0
- data/spec/conversation_spec.rb +14 -0
- data/spec/jabber_stub.rb +121 -0
- data/spec/notifier_spec.rb +85 -0
- data/spec/spec_helper.rb +49 -0
- metadata +78 -0
data/README.textile
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
h1. Uppercut
|
2
|
+
|
3
|
+
|
4
|
+
h2. Overview
|
5
|
+
|
6
|
+
Uppercut is a little DSL for writing agents and notifiers which you interact with via your Jabber client.
|
7
|
+
|
8
|
+
|
9
|
+
h2. Making an Agent
|
10
|
+
|
11
|
+
You could put together a very simple agent as follows:
|
12
|
+
|
13
|
+
<pre>
|
14
|
+
<code>
|
15
|
+
class BasicAgent < Uppercut::Agent
|
16
|
+
command 'date' do |c|
|
17
|
+
m.send `date`
|
18
|
+
end
|
19
|
+
|
20
|
+
command /^cat (.*)/ do |c,rest|
|
21
|
+
m.send File.read(rest)
|
22
|
+
end
|
23
|
+
|
24
|
+
command 'report' do |c|
|
25
|
+
m.send 'Hostname: ' + `hostname`
|
26
|
+
m.send 'Running as: ' + ENV['USER']
|
27
|
+
end
|
28
|
+
end
|
29
|
+
</code>
|
30
|
+
</pre>
|
31
|
+
|
32
|
+
With the above code, we've created an Agent template of sorts. It responds to three commands: {date cat report}.
|
33
|
+
The block which is passed to "command" should always have at least one paramater. The first parameter will
|
34
|
+
always be an Uppercut::Conversation object, which can be used to respond to the user who sent the input. When passing
|
35
|
+
a regular expression as the first parameter to "command", all of the _captures_ which the pattern match generates
|
36
|
+
will also be passed to the block. This can be seen in the "cat" example above. There is one capture and it is
|
37
|
+
passed in as _rest_.
|
38
|
+
|
39
|
+
Then to actually put the Agent to work...
|
40
|
+
|
41
|
+
<pre>
|
42
|
+
<code>
|
43
|
+
BasicAgent.new('user@server/resource','password').listen
|
44
|
+
</code>
|
45
|
+
</pre>
|
46
|
+
|
47
|
+
This creates a new BasicAgent instance and sends it _listen_ to make it start accepting messages.
|
48
|
+
Note that by default when an agent is listening, it ignores all errors. (In the future, I'll have it log them.)
|
49
|
+
|
50
|
+
There are also event callbacks for agents. These events are based on XMPP presence messages. So, as of this writing,
|
51
|
+
the list of allowed callbacks is: signon, signoff, subscribe, unsubscribe, subscription_approval, subscription_denial,
|
52
|
+
status_change, and status_message_change.
|
53
|
+
|
54
|
+
You can use these callbacks as follows:
|
55
|
+
|
56
|
+
<pre>
|
57
|
+
<code>
|
58
|
+
class CoolAgent < Uppercut::Agent
|
59
|
+
on :subscribe do |c|
|
60
|
+
c.send 'Hey dude, thanks for subscribing!'
|
61
|
+
end
|
62
|
+
|
63
|
+
on :signoff do |c|
|
64
|
+
puts "LOG: #{c.to} signed off."
|
65
|
+
end
|
66
|
+
end
|
67
|
+
</code>
|
68
|
+
</pre>
|
69
|
+
|
70
|
+
Some callbacks only work if the user allows the agent to subscribe to their presence notifications. This happens
|
71
|
+
automatically when a user subscribes to your agent. (Depending on their client) they'll be presented with a choice
|
72
|
+
to authorize or deny the subscription. You can catch their answer with the 'subscription_approval' and 'subscription_denial'
|
73
|
+
callbacks. Without their approving the subscription, your 'signon', 'signoff', 'status_change', and 'status_message_change'
|
74
|
+
callbacks will not be fired by them. I suggest using the 'subscription_denial' callback to inform them of that.
|
75
|
+
|
76
|
+
|
77
|
+
h2. Making a Notifier
|
78
|
+
|
79
|
+
<pre>
|
80
|
+
<code>
|
81
|
+
class BasicNotifier < Uppercut::Notifier
|
82
|
+
notifier :error do |m,data|
|
83
|
+
m.to 'tbmcmullen@gmail.com'
|
84
|
+
m.send "Something in your app blew up: #{data}"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
</code>
|
88
|
+
</pre>
|
89
|
+
|
90
|
+
So, we make a new Notifier class and call a _notifier_ block within it. This makes a notifier with the name :error, which sends a message to tbmcmullen@gmail.com when it fires.
|
91
|
+
|
92
|
+
<pre>
|
93
|
+
<code>
|
94
|
+
notifier = BasicNotifier.new('user@server/resource','password').connect
|
95
|
+
|
96
|
+
notifier.notify :error, 'Sprocket Error!'
|
97
|
+
</code>
|
98
|
+
</pre>
|
99
|
+
|
100
|
+
The purpose of a Notifier is just to make a multi-purpose event-driven notification system. You could use it notify yourself of errors in your Rails app, or ping you when a new comment arrives on your blog, or ... I dunno, something else.
|
101
|
+
|
102
|
+
The way I intend a Notifier to be used though is with Starling. Take the following example...
|
103
|
+
|
104
|
+
<pre>
|
105
|
+
<code>
|
106
|
+
notifier = BasicNotifier.new('user@server','password', :starling => 'localhost:22122', :queue => 'basic')
|
107
|
+
notifier.listen
|
108
|
+
sleep
|
109
|
+
</code>
|
110
|
+
</pre>
|
111
|
+
|
112
|
+
<pre>
|
113
|
+
<code>
|
114
|
+
require 'starling'
|
115
|
+
starling = Starling.new('localhost:22122')
|
116
|
+
starling.set 'basic', :error
|
117
|
+
</code>
|
118
|
+
</pre>
|
119
|
+
|
120
|
+
In that example, we have the same BasicNotifier. Except this time, we provide it with information on which Starling queue on which Starling server to monitor. Meanwhile, in the separate program, we attach to that same Starling server and push a symbol onto that same queue.
|
121
|
+
|
122
|
+
In this case, what happens is BasicNotifier sees that symbol and begins processing it, just as if it had been passed in via the #notify method.
|
123
|
+
|
124
|
+
|
125
|
+
|
126
|
+
h2. Todo
|
127
|
+
|
128
|
+
|
129
|
+
h3. Security
|
130
|
+
|
131
|
+
If you intend to use this on an open Jabber network (read: Google Talk) take some precautions. Agent#new takes an optional list of JIDs that the agent will respond to.
|
132
|
+
|
133
|
+
<pre>
|
134
|
+
<code>
|
135
|
+
BasicAgent.new('user@server/res', 'pw', :roster => ['you@server'])
|
136
|
+
</code>
|
137
|
+
</pre>
|
138
|
+
|
139
|
+
The agent created with the above statement will not respond to messages or subscription requests from anyone other than 'you@server'.
|
140
|
+
|
141
|
+
h3. Features
|
142
|
+
|
143
|
+
Uppercut is currently very thin on features, as it's in its infancy. Here's a brief list of where I'm
|
144
|
+
currently intending to take the project:
|
145
|
+
|
146
|
+
* send files to and receive files from agents
|
147
|
+
* improving the way one writes executables (using the Daemons library is best for the moment)
|
148
|
+
* auto-updating of agents
|
149
|
+
* I swear I'm going to find a use for MUC
|
150
|
+
* allow agents to establish communications on their own, rather than being reactionary (think RSS updates)
|
151
|
+
* ... other stuff that sounds fun to code up ...
|
data/VERSION.yml
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
class BasicNotifier < Uppercut::Notifier
|
2
|
+
notifier :basic do |n,data|
|
3
|
+
n.to 'tyler@codehallow.com'
|
4
|
+
n.send 'Hey kid.'
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
class BasicAgent < Uppercut::Agent
|
9
|
+
command 'date' do |m|
|
10
|
+
m.send `date`
|
11
|
+
end
|
12
|
+
|
13
|
+
command /^cat (.*)/ do |m,rest|
|
14
|
+
m.send File.read(rest)
|
15
|
+
end
|
16
|
+
|
17
|
+
command 'report' do |m|
|
18
|
+
m.send 'Hostname: ' + `hostname`
|
19
|
+
m.send 'Running as: ' + ENV['USER']
|
20
|
+
end
|
21
|
+
|
22
|
+
command 'dangerous' do |c|
|
23
|
+
c.send "Are you sure?!"
|
24
|
+
c.wait_for do |reply|
|
25
|
+
c.send %w(yes y).include?(reply.downcase) ? "Okay! Done boss!" : "Cancelled!"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'uppercut'
|
3
|
+
require 'yahoo-weather'
|
4
|
+
require 'feed_tools'
|
5
|
+
|
6
|
+
class PersonalAgent < Uppercut::Agent
|
7
|
+
def get_weather
|
8
|
+
@weather_client ||= YahooWeather::Client.new
|
9
|
+
@weather_client.lookup_location('94102') # lookup by zipcode
|
10
|
+
end
|
11
|
+
|
12
|
+
def get_news
|
13
|
+
FeedTools::Feed.open('feed://www.nytimes.com/services/xml/rss/nyt/HomePage.xml')
|
14
|
+
end
|
15
|
+
|
16
|
+
command 'weather' do |c,args|
|
17
|
+
weather = get_weather
|
18
|
+
c.send "#{weather.title}\n#{weather.condition.temp} degrees\n#{weather.condition.text}"
|
19
|
+
end
|
20
|
+
|
21
|
+
command 'forecast' do |c,args|
|
22
|
+
response = get_weather
|
23
|
+
msg = "#{response.forecasts[0].day} - #{response.forecasts[0].text}. "
|
24
|
+
msg << "High: #{response.forecasts[0].high} Low: #{response.forecasts[0].low}\n"
|
25
|
+
msg << "#{response.forecasts[1].day} - #{response.forecasts[1].text}. "
|
26
|
+
msg << "High: #{response.forecasts[1].high} Low: #{response.forecasts[1].low}\n"
|
27
|
+
c.send msg
|
28
|
+
end
|
29
|
+
|
30
|
+
command 'news' do |c,args|
|
31
|
+
msg = get_news.items[0,5].map { |item|
|
32
|
+
"#{item.title}\n#{item.link}"
|
33
|
+
}.join("\n\n")
|
34
|
+
c.send msg
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
if $0 == __FILE__
|
39
|
+
agent = PersonalAgent.new('reminder@drmcawesome.com/PersonalAgent','password')
|
40
|
+
agent.listen
|
41
|
+
sleep
|
42
|
+
end
|
data/lib/uppercut.rb
ADDED
@@ -0,0 +1,223 @@
|
|
1
|
+
class Uppercut
|
2
|
+
class Agent < Base
|
3
|
+
VALID_CALLBACKS = [:signon, :signoff, :subscribe, :unsubscribe, :subscription_approval,
|
4
|
+
:subscription_denial, :status_change, :status_message_change]
|
5
|
+
|
6
|
+
class << self
|
7
|
+
# Define a new command for the agent.
|
8
|
+
#
|
9
|
+
# The pattern can be a String or a Regexp. If a String is passed, it
|
10
|
+
# will dispatch this command only on an exact match. A Regexp simply
|
11
|
+
# must match.
|
12
|
+
#
|
13
|
+
# There is always at least one argument sent to the block. The first
|
14
|
+
# is a always an Uppercut::Message object, which can be used to reply
|
15
|
+
# to the sender. The rest of the arguments to the block correspond to
|
16
|
+
# any captures in the pattern Regexp. (Does not apply to String
|
17
|
+
# patterns).
|
18
|
+
def command(pattern,&block)
|
19
|
+
@@patterns ||= []
|
20
|
+
g = gensym
|
21
|
+
@@patterns << [pattern,g]
|
22
|
+
define_method(g, &block)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Define a callback for specific presence events.
|
26
|
+
#
|
27
|
+
# At the moment this is only confirmed to work with :subscribe and :unsubscribe, but it may work with other types as well.
|
28
|
+
# Example:
|
29
|
+
#
|
30
|
+
# on :subscribe do |conversation|
|
31
|
+
# conversation.send "Welcome! Send 'help' for instructions."
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
def on(type, &block)
|
35
|
+
raise 'Not a valid callback' unless VALID_CALLBACKS.include?(type)
|
36
|
+
define_method("__on_#{type.to_s}") { |conversation| block[conversation] }
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def gensym
|
42
|
+
('__uc' + (self.instance_methods.grep(/^__uc/).size).to_s.rjust(8,'0')).intern
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
DEFAULT_OPTIONS = { :connect => true }
|
47
|
+
|
48
|
+
# Create a new instance of an Agent, possibly connecting to the server.
|
49
|
+
#
|
50
|
+
# user should be a String in the form: "user@server/Resource". pw is
|
51
|
+
# simply the password for this account. The final, and optional, argument
|
52
|
+
# is a boolean which controls whether or not it will attempt to connect to
|
53
|
+
# the server immediately. Defaults to true.
|
54
|
+
def initialize(user,pw,options={})
|
55
|
+
options = DEFAULT_OPTIONS.merge(options)
|
56
|
+
|
57
|
+
@user = user
|
58
|
+
@pw = pw
|
59
|
+
connect if options[:connect]
|
60
|
+
listen if options[:listen]
|
61
|
+
|
62
|
+
@allowed_roster = options[:roster]
|
63
|
+
@redirects = {}
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
def inspect #:nodoc:
|
68
|
+
"<Uppercut::Agent #{@user} " +
|
69
|
+
"#{listening? ? 'Listening' : 'Not Listening'}:" +
|
70
|
+
"#{connected? ? 'Connected' : 'Disconnected'}>"
|
71
|
+
end
|
72
|
+
|
73
|
+
# Makes an Agent instance begin listening for incoming messages and
|
74
|
+
# subscription requests.
|
75
|
+
#
|
76
|
+
# Current listen simply eats any errors that occur, in the interest of
|
77
|
+
# keeping the remote agent alive. These should be logged at some point
|
78
|
+
# in the future. Pass debug as true to prevent this behaviour.
|
79
|
+
#
|
80
|
+
# Calling listen fires off a new Thread whose sole purpose is to listen
|
81
|
+
# for new incoming messages and then fire off a new Thread which dispatches
|
82
|
+
# the message to the proper handler.
|
83
|
+
def listen
|
84
|
+
connect unless connected?
|
85
|
+
|
86
|
+
@listen_thread = Thread.new {
|
87
|
+
@client.add_message_callback do |message|
|
88
|
+
log_and_continue do
|
89
|
+
next if message.body.nil?
|
90
|
+
next unless allowed_roster_includes?(message.from)
|
91
|
+
dispatch message
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
@roster ||= Jabber::Roster::Helper.new(@client)
|
96
|
+
@roster.add_presence_callback do |item, old_presence, new_presence|
|
97
|
+
# Callbacks:
|
98
|
+
# post-subscribe initial stuff (oldp == nil)
|
99
|
+
# status change: (oldp.show != newp.show)
|
100
|
+
# status message change: (oldp.status != newp.status)
|
101
|
+
|
102
|
+
log_and_continue do
|
103
|
+
if old_presence.nil? && new_presence.type == :unavailable
|
104
|
+
dispatch_presence :signoff, new_presence
|
105
|
+
elsif old_presence.nil?
|
106
|
+
# do nothing, we don't care
|
107
|
+
elsif old_presence.type == :unavailable && new_presence
|
108
|
+
dispatch_presence :signon, new_presence
|
109
|
+
elsif old_presence.show != new_presence.show
|
110
|
+
dispatch_presence :status_change, new_presence
|
111
|
+
elsif old_presence.status != new_presence.status
|
112
|
+
dispatch_presence :status_message_change, new_presence
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
@roster.add_subscription_request_callback do |item,presence|
|
117
|
+
# Callbacks:
|
118
|
+
# someone tries to subscribe (presence.type == 'subscribe')
|
119
|
+
|
120
|
+
log_and_continue do
|
121
|
+
case presence.type
|
122
|
+
when :subscribe
|
123
|
+
next unless allowed_roster_includes?(presence.from)
|
124
|
+
@roster.accept_subscription(presence.from)
|
125
|
+
@roster.add(presence.from, nil, true)
|
126
|
+
dispatch_presence :subscribe, presence
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
@roster.add_subscription_callback do |item, presence|
|
131
|
+
# Callbacks:
|
132
|
+
# user allows agent to subscribe to them (presence.type == 'subscribed')
|
133
|
+
# user denies agent subscribe request (presence.type == 'unsubscribed')
|
134
|
+
# user unsubscribes from agent (presence.type == 'unsubscribe')
|
135
|
+
|
136
|
+
log_and_continue do
|
137
|
+
case presence.type
|
138
|
+
when :subscribed
|
139
|
+
dispatch_presence :subscription_approval, presence
|
140
|
+
when :unsubscribed
|
141
|
+
# if item.subscription != :from, it's not a denial... it's just an unsub
|
142
|
+
dispatch_presence(:subscription_denial, presence) if item.subscription == :from
|
143
|
+
when :unsubscribe
|
144
|
+
dispatch_presence :unsubscribe, presence
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
sleep
|
149
|
+
}
|
150
|
+
end
|
151
|
+
|
152
|
+
# Stops the Agent from listening to incoming messages.
|
153
|
+
#
|
154
|
+
# Simply kills the thread if it is running.
|
155
|
+
def stop
|
156
|
+
@listen_thread.kill if listening?
|
157
|
+
end
|
158
|
+
|
159
|
+
# True if the Agent is currently listening for incoming messages.
|
160
|
+
def listening?
|
161
|
+
@listen_thread && @listen_thread.alive?
|
162
|
+
end
|
163
|
+
|
164
|
+
def redirect_from(contact,&block)
|
165
|
+
@redirects[contact] ||= []
|
166
|
+
@redirects[contact].push block
|
167
|
+
end
|
168
|
+
|
169
|
+
attr_accessor :allowed_roster, :roster
|
170
|
+
|
171
|
+
private
|
172
|
+
|
173
|
+
def log_and_continue
|
174
|
+
yield
|
175
|
+
rescue => e
|
176
|
+
log e
|
177
|
+
raise if @debug
|
178
|
+
end
|
179
|
+
|
180
|
+
def dispatch(msg)
|
181
|
+
bare_from = msg.from.bare
|
182
|
+
block = @redirects[bare_from].respond_to?(:shift) && @redirects[bare_from].shift
|
183
|
+
return block[msg.body] if block
|
184
|
+
|
185
|
+
captures = nil
|
186
|
+
pair = @@patterns.detect { |pattern,method| captures = matches?(pattern,msg.body) }
|
187
|
+
if pair
|
188
|
+
pattern, method = pair if pair
|
189
|
+
send method, Conversation.new(msg.from,self), captures
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
def dispatch_presence(type, presence)
|
194
|
+
handler = "__on_#{type}"
|
195
|
+
self.send(handler, Conversation.new(presence.from, self), presence) if respond_to?(handler)
|
196
|
+
end
|
197
|
+
|
198
|
+
def __ucDefault(msg)
|
199
|
+
Message.new(msg.from,self).send("I don't know what \"#{msg.body}\" means.")
|
200
|
+
end
|
201
|
+
|
202
|
+
def matches?(pattern,msg)
|
203
|
+
captures = nil
|
204
|
+
case pattern
|
205
|
+
when String
|
206
|
+
captures = [] if pattern == msg
|
207
|
+
when Regexp
|
208
|
+
match_data = pattern.match(msg)
|
209
|
+
captures = match_data.captures if match_data
|
210
|
+
end
|
211
|
+
captures
|
212
|
+
end
|
213
|
+
|
214
|
+
def allowed_roster_includes?(jid)
|
215
|
+
return true unless @allowed_roster
|
216
|
+
|
217
|
+
jid = jid.to_s
|
218
|
+
return true if @allowed_roster.include?(jid)
|
219
|
+
return true if @allowed_roster.include?(jid.sub(/\/[^\/]+$/,''))
|
220
|
+
end
|
221
|
+
|
222
|
+
end
|
223
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
class Uppercut
|
2
|
+
class Base
|
3
|
+
def stanza(msg) #:nodoc:
|
4
|
+
return false unless connected?
|
5
|
+
send! msg
|
6
|
+
end
|
7
|
+
|
8
|
+
# Attempt to connect to the server, if not already connected.
|
9
|
+
#
|
10
|
+
# Raises a simple RuntimeError if it fails to connect. This should be
|
11
|
+
# changed eventually to be more useful.
|
12
|
+
def connect
|
13
|
+
return if connected?
|
14
|
+
connect!
|
15
|
+
raise 'Failed to connected' unless connected?
|
16
|
+
present!
|
17
|
+
end
|
18
|
+
|
19
|
+
# Disconnects from the server if it is connected.
|
20
|
+
def disconnect
|
21
|
+
disconnect! if connected?
|
22
|
+
end
|
23
|
+
|
24
|
+
# Disconnects and connects to the server.
|
25
|
+
def reconnect
|
26
|
+
disconnect
|
27
|
+
connect
|
28
|
+
end
|
29
|
+
|
30
|
+
attr_reader :client, :roster
|
31
|
+
|
32
|
+
# True if the Agent is currently connected to the Jabber server.
|
33
|
+
def connected?
|
34
|
+
@client.respond_to?(:is_connected?) && @client.is_connected?
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def connect!
|
40
|
+
@connect_lock ||= Mutex.new
|
41
|
+
return if @connect_lock.locked?
|
42
|
+
|
43
|
+
client = Jabber::Client.new(@user)
|
44
|
+
|
45
|
+
@connect_lock.lock
|
46
|
+
|
47
|
+
client.connect
|
48
|
+
client.auth(@pw)
|
49
|
+
@client = client
|
50
|
+
|
51
|
+
@connect_lock.unlock
|
52
|
+
end
|
53
|
+
|
54
|
+
def disconnect!
|
55
|
+
@client.close if connected?
|
56
|
+
@client = nil
|
57
|
+
end
|
58
|
+
|
59
|
+
def present!
|
60
|
+
send! Jabber::Presence.new(nil,"Available")
|
61
|
+
end
|
62
|
+
|
63
|
+
# Taken directly from xmpp4r-simple (thanks Blaine!)
|
64
|
+
def send!(msg)
|
65
|
+
attempts = 0
|
66
|
+
begin
|
67
|
+
attempts += 1
|
68
|
+
@client.send(msg)
|
69
|
+
rescue Errno::EPIPE, IOError => e
|
70
|
+
sleep 1
|
71
|
+
disconnect!
|
72
|
+
connect!
|
73
|
+
retry unless attempts > 3
|
74
|
+
raise e
|
75
|
+
rescue Errno::ECONNRESET => e
|
76
|
+
sleep (attempts^2) * 60 + 60
|
77
|
+
disconnect!
|
78
|
+
connect!
|
79
|
+
retry unless attempts > 3
|
80
|
+
raise e
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def log(error)
|
85
|
+
# todo
|
86
|
+
p error
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class Uppercut
|
2
|
+
class Conversation < Message
|
3
|
+
attr_reader :to
|
4
|
+
|
5
|
+
def initialize(to,base) #:nodoc:
|
6
|
+
@to = to
|
7
|
+
super base
|
8
|
+
end
|
9
|
+
|
10
|
+
# Wait for another message from this contact.
|
11
|
+
#
|
12
|
+
# Expects a block which should receive one parameter, which will be a
|
13
|
+
# String.
|
14
|
+
#
|
15
|
+
# One common use of _wait_for_ is for confirmation of a sensitive action.
|
16
|
+
#
|
17
|
+
# command('foo') do |c|
|
18
|
+
# c.send 'Are you sure?'
|
19
|
+
# c.wait_for do |reply|
|
20
|
+
# do_it if reply.downcase == 'yes'
|
21
|
+
# end
|
22
|
+
# end
|
23
|
+
def wait_for(&block)
|
24
|
+
@base.redirect_from(@to.bare,&block)
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class Uppercut
|
2
|
+
class Message
|
3
|
+
attr_accessor :to, :message
|
4
|
+
|
5
|
+
def initialize(base) #:nodoc:
|
6
|
+
@base = base
|
7
|
+
end
|
8
|
+
|
9
|
+
# Send a blob of text.
|
10
|
+
def send(body=nil)
|
11
|
+
msg = Jabber::Message.new(@to)
|
12
|
+
msg.type = :chat
|
13
|
+
msg.body = body || @message
|
14
|
+
@base.stanza(msg)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
@@ -0,0 +1,64 @@
|
|
1
|
+
class Uppercut
|
2
|
+
class Notifier < Base
|
3
|
+
class << self
|
4
|
+
@@notifiers = []
|
5
|
+
|
6
|
+
def notifier(name,&block)
|
7
|
+
@@notifiers << name
|
8
|
+
define_method(name, &block)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def notify(name,data=nil)
|
13
|
+
return false unless connected?
|
14
|
+
return nil unless @@notifiers.include?(name)
|
15
|
+
|
16
|
+
send(name,Message.new(self),data)
|
17
|
+
end
|
18
|
+
|
19
|
+
def initialize(user,pw,options={})
|
20
|
+
options = DEFAULT_OPTIONS.merge(options)
|
21
|
+
|
22
|
+
initialize_queue options[:starling], options[:queue]
|
23
|
+
|
24
|
+
@user = user
|
25
|
+
@pw = pw
|
26
|
+
connect if options[:connect]
|
27
|
+
listen if options[:listen]
|
28
|
+
end
|
29
|
+
|
30
|
+
DEFAULT_OPTIONS = { :connect => true }
|
31
|
+
|
32
|
+
def listen
|
33
|
+
connect unless connected?
|
34
|
+
|
35
|
+
@listen_thread = Thread.new {
|
36
|
+
loop { notify @starling.get(@queue) }
|
37
|
+
}
|
38
|
+
end
|
39
|
+
|
40
|
+
def stop
|
41
|
+
@listen_thread.kill if listening?
|
42
|
+
end
|
43
|
+
|
44
|
+
def listening?
|
45
|
+
@listen_thread && @listen_thread.alive?
|
46
|
+
end
|
47
|
+
|
48
|
+
def inspect #:nodoc:
|
49
|
+
"<Uppercut::Notifier #{@user} " +
|
50
|
+
"#{listening? ? 'Listening' : 'Not Listening'} " +
|
51
|
+
"#{connected? ? 'Connected' : 'Disconnected'}>"
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def initialize_queue(server,queue)
|
57
|
+
return unless queue && server
|
58
|
+
require 'starling'
|
59
|
+
@queue = queue
|
60
|
+
@starling = Starling.new(server)
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
data/spec/agent_spec.rb
ADDED
@@ -0,0 +1,278 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
|
3
|
+
describe Uppercut::Agent do
|
4
|
+
before :each do
|
5
|
+
@agent = TestAgent.new('test@foo.com', 'pw', :connect => false)
|
6
|
+
end
|
7
|
+
|
8
|
+
describe :new do
|
9
|
+
it "connects by default" do
|
10
|
+
agent = Uppercut::Agent.new('test@foo','pw')
|
11
|
+
agent.should be_connected
|
12
|
+
end
|
13
|
+
|
14
|
+
it "does not connect by default with :connect = false" do
|
15
|
+
agent = Uppercut::Agent.new('test@foo','pw', :connect => false)
|
16
|
+
agent.should_not be_connected
|
17
|
+
end
|
18
|
+
|
19
|
+
it "starts to listen with :listen = true" do
|
20
|
+
agent = Uppercut::Agent.new('test@foo','pw', :listen => true)
|
21
|
+
agent.should be_listening
|
22
|
+
end
|
23
|
+
|
24
|
+
it "initializes @redirects with a blank hash" do
|
25
|
+
agent = Uppercut::Agent.new('test@foo','pw', :connect => false)
|
26
|
+
agent.instance_eval { @redirects }.should == {}
|
27
|
+
end
|
28
|
+
|
29
|
+
it "populates @pw and @user" do
|
30
|
+
agent = Uppercut::Agent.new('test@foo','pw')
|
31
|
+
agent.instance_eval { @pw }.should == 'pw'
|
32
|
+
agent.instance_eval { @user }.should == 'test@foo'
|
33
|
+
end
|
34
|
+
|
35
|
+
it "populates @allowed_roster with :roster option" do
|
36
|
+
jids = %w(bob@foo fred@foo)
|
37
|
+
agent = Uppercut::Agent.new('test@foo','pw', :roster => jids)
|
38
|
+
agent.instance_eval { @allowed_roster }.should == jids
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe :connect do
|
43
|
+
it "does not try to connect if already connected" do
|
44
|
+
@agent.connect
|
45
|
+
old_client = @agent.client
|
46
|
+
|
47
|
+
@agent.connect
|
48
|
+
(@agent.client == old_client).should == true
|
49
|
+
end
|
50
|
+
|
51
|
+
it "connects if disconnected" do
|
52
|
+
@agent.should_not be_connected
|
53
|
+
|
54
|
+
old_client = @agent.client
|
55
|
+
|
56
|
+
@agent.connect
|
57
|
+
(@agent.client == old_client).should_not == true
|
58
|
+
end
|
59
|
+
|
60
|
+
it "sends a Presence notification" do
|
61
|
+
@agent.connect
|
62
|
+
@agent.client.sent.first.class.should == Jabber::Presence
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe :disconnect do
|
67
|
+
it "does not try to disconnect if not connected" do
|
68
|
+
@agent.client.should be_nil
|
69
|
+
@agent.instance_eval { @client = :foo }
|
70
|
+
|
71
|
+
@agent.disconnect
|
72
|
+
@agent.client.should == :foo
|
73
|
+
end
|
74
|
+
|
75
|
+
it "sets @client to nil" do
|
76
|
+
@agent.connect
|
77
|
+
@agent.client.should_not be_nil
|
78
|
+
|
79
|
+
@agent.disconnect
|
80
|
+
@agent.client.should be_nil
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe :reconnect do
|
85
|
+
it "calls disconnect then connect" do
|
86
|
+
@agent.should_receive(:disconnect).once.ordered
|
87
|
+
@agent.should_receive(:connect).once.ordered
|
88
|
+
|
89
|
+
@agent.reconnect
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe :connected? do
|
94
|
+
it "returns true if client#is_connected? is true" do
|
95
|
+
@agent.connect
|
96
|
+
@agent.client.instance_eval { @connected = true }
|
97
|
+
@agent.should be_connected
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
describe :listen do
|
102
|
+
it "connects if not connected" do
|
103
|
+
@agent.listen
|
104
|
+
@agent.should be_connected
|
105
|
+
end
|
106
|
+
|
107
|
+
it "spins off a new thread in @listen_thread" do
|
108
|
+
@agent.listen
|
109
|
+
@agent.instance_eval { @listen_thread.class }.should == Thread
|
110
|
+
end
|
111
|
+
|
112
|
+
it "creates a receive message callback" do
|
113
|
+
@agent.listen
|
114
|
+
@agent.client.on_message.class.should == Proc
|
115
|
+
end
|
116
|
+
|
117
|
+
it "creates a subscription request callback" do
|
118
|
+
@agent.listen
|
119
|
+
@agent.roster.on_subscription_request.class.should == Proc
|
120
|
+
end
|
121
|
+
|
122
|
+
it "calls dispatch when receving a message" do
|
123
|
+
@agent.listen
|
124
|
+
@agent.should_receive(:dispatch)
|
125
|
+
@agent.client.receive_message("foo@bar.com","test")
|
126
|
+
end
|
127
|
+
|
128
|
+
describe 'presence callbacks' do
|
129
|
+
it 'processes :signon presence callback' do
|
130
|
+
@agent.listen
|
131
|
+
@agent.should_receive :__on_signon
|
132
|
+
new_presence = Jabber::Presence.new(nil,nil)
|
133
|
+
old_presence = Jabber::Presence.new(nil,nil)
|
134
|
+
old_presence.type = :unavailable
|
135
|
+
|
136
|
+
@agent.roster.receive_presence(Jabber::Roster::Helper::RosterItem.new, old_presence, new_presence)
|
137
|
+
end
|
138
|
+
|
139
|
+
it 'processes :signoff presence callback' do
|
140
|
+
@agent.listen
|
141
|
+
@agent.should_receive :__on_signoff
|
142
|
+
presence = Jabber::Presence.new(nil,nil)
|
143
|
+
presence.type = :unavailable
|
144
|
+
|
145
|
+
@agent.roster.receive_presence(Jabber::Roster::Helper::RosterItem.new, nil, presence)
|
146
|
+
end
|
147
|
+
|
148
|
+
it 'processes :status_change presence callback' do
|
149
|
+
@agent.listen
|
150
|
+
@agent.should_receive :__on_status_change
|
151
|
+
|
152
|
+
old_presence = Jabber::Presence.new(nil,nil)
|
153
|
+
new_presence = Jabber::Presence.new(nil,nil)
|
154
|
+
new_presence.show = :away
|
155
|
+
|
156
|
+
@agent.roster.receive_presence(Jabber::Roster::Helper::RosterItem.new, old_presence, new_presence)
|
157
|
+
end
|
158
|
+
|
159
|
+
it 'processes :status_message_change presence callback' do
|
160
|
+
@agent.listen
|
161
|
+
@agent.should_receive :__on_status_message_change
|
162
|
+
|
163
|
+
old_presence = Jabber::Presence.new(nil,nil)
|
164
|
+
old_presence.status = 'chicka chicka yeaaaaah'
|
165
|
+
|
166
|
+
new_presence = Jabber::Presence.new(nil,nil)
|
167
|
+
new_presence.status = 'thom yorke is the man'
|
168
|
+
|
169
|
+
@agent.roster.receive_presence(Jabber::Roster::Helper::RosterItem.new, old_presence, new_presence)
|
170
|
+
end
|
171
|
+
|
172
|
+
it 'processes :subscribe presence callback' do
|
173
|
+
@agent.listen
|
174
|
+
@agent.should_receive :__on_subscribe
|
175
|
+
@agent.roster.should_receive :add
|
176
|
+
@agent.roster.should_receive :accept_subscription
|
177
|
+
|
178
|
+
presence = Jabber::Presence.new(nil,nil)
|
179
|
+
presence.type = :subscribe
|
180
|
+
|
181
|
+
@agent.roster.receive_subscription_request(Jabber::Roster::Helper::RosterItem.new, presence)
|
182
|
+
end
|
183
|
+
|
184
|
+
it 'processes :subscription_approval presence callback' do
|
185
|
+
@agent.listen
|
186
|
+
@agent.should_receive :__on_subscription_approval
|
187
|
+
|
188
|
+
presence = Jabber::Presence.new(nil,nil)
|
189
|
+
presence.type = :subscribed
|
190
|
+
|
191
|
+
@agent.roster.receive_subscription(Jabber::Roster::Helper::RosterItem.new, presence)
|
192
|
+
end
|
193
|
+
|
194
|
+
it 'processes :subscription_denial presence callback' do
|
195
|
+
@agent.listen
|
196
|
+
@agent.should_receive :__on_subscription_denial
|
197
|
+
|
198
|
+
presence = Jabber::Presence.new(nil,nil)
|
199
|
+
presence.type = :unsubscribed
|
200
|
+
|
201
|
+
item = Jabber::Roster::Helper::RosterItem.new
|
202
|
+
item.subscription = :from
|
203
|
+
|
204
|
+
@agent.roster.receive_subscription(item, presence)
|
205
|
+
end
|
206
|
+
|
207
|
+
it 'processes :unsubscribe presence callback' do
|
208
|
+
@agent.listen
|
209
|
+
@agent.should_receive :__on_unsubscribe
|
210
|
+
|
211
|
+
presence = Jabber::Presence.new(nil,nil)
|
212
|
+
presence.type = :unsubscribe
|
213
|
+
|
214
|
+
@agent.roster.receive_subscription(Jabber::Roster::Helper::RosterItem.new, presence)
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
describe :stop do
|
220
|
+
it "kills the @listen_thread" do
|
221
|
+
@agent.listen
|
222
|
+
@agent.instance_eval { @listen_thread.alive? }.should == true
|
223
|
+
|
224
|
+
@agent.stop
|
225
|
+
@agent.instance_eval { @listen_thread.alive? }.should_not == true
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
describe :listening? do
|
230
|
+
it "returns true if @listen_thread is alive" do
|
231
|
+
@agent.listen
|
232
|
+
@agent.instance_eval { @listen_thread.alive? }.should == true
|
233
|
+
@agent.should be_listening
|
234
|
+
end
|
235
|
+
|
236
|
+
it "returns false if @listen_thread is not alive" do
|
237
|
+
@agent.listen
|
238
|
+
@agent.stop
|
239
|
+
@agent.should_not be_listening
|
240
|
+
end
|
241
|
+
|
242
|
+
it "returns false if @listen_thread has not been set" do
|
243
|
+
@agent.should_not be_listening
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
describe :dispatch_presence do
|
248
|
+
it 'calls the correct callback' do
|
249
|
+
@agent.listen
|
250
|
+
@agent.should_receive(:__on_subscribe)
|
251
|
+
|
252
|
+
presence = Jabber::Presence.new(nil,nil)
|
253
|
+
@agent.send(:dispatch_presence, :subscribe, presence)
|
254
|
+
end
|
255
|
+
end
|
256
|
+
|
257
|
+
describe :dispatch do
|
258
|
+
it "calls the first matching command" do
|
259
|
+
msg = Jabber::Message.new(nil)
|
260
|
+
msg.body = 'hi'
|
261
|
+
msg.from = Jabber::JID.fake_jid
|
262
|
+
|
263
|
+
@agent.send(:dispatch, msg)
|
264
|
+
@agent.instance_eval { @called_hi_regex }.should_not == true
|
265
|
+
@agent.instance_eval { @called_hi }.should == true
|
266
|
+
end
|
267
|
+
|
268
|
+
it "matches by regular expression" do
|
269
|
+
msg = Jabber::Message.new(nil)
|
270
|
+
msg.body = 'high'
|
271
|
+
msg.from = Jabber::JID.fake_jid
|
272
|
+
|
273
|
+
@agent.send(:dispatch, msg)
|
274
|
+
@agent.instance_eval { @called_hi }.should_not == true
|
275
|
+
@agent.instance_eval { @called_hi_regex }.should == true
|
276
|
+
end
|
277
|
+
end
|
278
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
|
3
|
+
|
4
|
+
describe Uppercut::Conversation do
|
5
|
+
before(:each) do
|
6
|
+
@conv = Uppercut::Conversation.new('test@foo.com', nil)
|
7
|
+
end
|
8
|
+
|
9
|
+
describe :contact do
|
10
|
+
it "should have a contact method" do
|
11
|
+
@conv.should.respond_to?(:contact)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/spec/jabber_stub.rb
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
Object.send(:remove_const, :Jabber)
|
2
|
+
class Jabber
|
3
|
+
class Client
|
4
|
+
def initialize(user)
|
5
|
+
@user = user
|
6
|
+
end
|
7
|
+
|
8
|
+
def connect
|
9
|
+
@connected = true
|
10
|
+
end
|
11
|
+
|
12
|
+
def auth(pw)
|
13
|
+
@pw = pw
|
14
|
+
end
|
15
|
+
|
16
|
+
def is_connected?
|
17
|
+
@connected
|
18
|
+
end
|
19
|
+
|
20
|
+
def close
|
21
|
+
@connected = nil
|
22
|
+
end
|
23
|
+
|
24
|
+
attr_reader :sent
|
25
|
+
def send(msg)
|
26
|
+
@sent ||= []
|
27
|
+
@sent << msg
|
28
|
+
end
|
29
|
+
|
30
|
+
attr_reader :on_message
|
31
|
+
def add_message_callback(&block)
|
32
|
+
@on_message = block
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
# TESTING HELPER METHODS
|
38
|
+
|
39
|
+
def receive_message(from,body,type=:chat)
|
40
|
+
msg = Message.new(nil)
|
41
|
+
msg.type = type
|
42
|
+
msg.body = body
|
43
|
+
msg.from = from
|
44
|
+
@on_message[msg]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class Presence
|
49
|
+
attr_accessor :from, :type, :show, :status
|
50
|
+
|
51
|
+
def initialize(a,b)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class Message
|
56
|
+
attr_accessor :type, :body, :from
|
57
|
+
def initialize(to)
|
58
|
+
@to = to
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
class Roster
|
63
|
+
class Helper
|
64
|
+
class RosterItem
|
65
|
+
attr_accessor :subscription
|
66
|
+
end
|
67
|
+
|
68
|
+
def initialize(client)
|
69
|
+
@client = client
|
70
|
+
end
|
71
|
+
|
72
|
+
def accept_subscription(a)
|
73
|
+
end
|
74
|
+
|
75
|
+
def add(a)
|
76
|
+
end
|
77
|
+
|
78
|
+
attr_reader :on_subscription_request
|
79
|
+
def add_subscription_request_callback(&block)
|
80
|
+
@on_subscription_request = block
|
81
|
+
end
|
82
|
+
|
83
|
+
def add_presence_callback(&block)
|
84
|
+
@on_presence = block
|
85
|
+
end
|
86
|
+
|
87
|
+
def add_subscription_callback(&block)
|
88
|
+
@on_subscription = block
|
89
|
+
end
|
90
|
+
|
91
|
+
# TESTING HELPER METHODS
|
92
|
+
|
93
|
+
def receive_presence(item, old_presence, new_presence)
|
94
|
+
@on_presence[item, old_presence, new_presence]
|
95
|
+
end
|
96
|
+
|
97
|
+
def receive_subscription(item, presence)
|
98
|
+
@on_subscription[item, presence]
|
99
|
+
end
|
100
|
+
|
101
|
+
def receive_subscription_request(item, presence)
|
102
|
+
@on_subscription_request[item, presence]
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
class JID
|
108
|
+
def self.fake_jid
|
109
|
+
new 'foo', 'bar.com', 'baz'
|
110
|
+
end
|
111
|
+
|
112
|
+
def initialize(node,domain,res)
|
113
|
+
@node, @domain, @res = node, domain, res
|
114
|
+
end
|
115
|
+
|
116
|
+
def bare
|
117
|
+
self.class.new @node, @domain, nil
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
|
3
|
+
describe Uppercut::Notifier do
|
4
|
+
before :each do
|
5
|
+
@notifier = TestNotifier.new('test@foo.com', 'pw', :connect => false)
|
6
|
+
end
|
7
|
+
|
8
|
+
describe :new do
|
9
|
+
it "connects by default" do
|
10
|
+
notifier = Uppercut::Notifier.new('test@foo','pw')
|
11
|
+
notifier.should be_connected
|
12
|
+
end
|
13
|
+
|
14
|
+
it "does not connect by default with :connect = false" do
|
15
|
+
notifier = Uppercut::Notifier.new('test@foo','pw', :connect => false)
|
16
|
+
notifier.should_not be_connected
|
17
|
+
end
|
18
|
+
|
19
|
+
it "populates @pw and @user" do
|
20
|
+
notifier = Uppercut::Notifier.new('test@foo','pw')
|
21
|
+
notifier.instance_eval { @pw }.should == 'pw'
|
22
|
+
notifier.instance_eval { @user }.should == 'test@foo'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe :connect do
|
27
|
+
it "does not try to connect if already connected" do
|
28
|
+
@notifier.connect
|
29
|
+
old_client = @notifier.client
|
30
|
+
|
31
|
+
@notifier.connect
|
32
|
+
(@notifier.client == old_client).should == true
|
33
|
+
end
|
34
|
+
|
35
|
+
it "connects if disconnected" do
|
36
|
+
@notifier.should_not be_connected
|
37
|
+
|
38
|
+
old_client = @notifier.client
|
39
|
+
|
40
|
+
@notifier.connect
|
41
|
+
(@notifier.client == old_client).should_not == true
|
42
|
+
end
|
43
|
+
|
44
|
+
it "sends a Presence notification" do
|
45
|
+
@notifier.connect
|
46
|
+
@notifier.client.sent.first.class.should == Jabber::Presence
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe :disconnect do
|
51
|
+
it "does not try to disconnect if not connected" do
|
52
|
+
@notifier.client.should be_nil
|
53
|
+
@notifier.instance_eval { @client = :foo }
|
54
|
+
|
55
|
+
@notifier.disconnect
|
56
|
+
@notifier.client.should == :foo
|
57
|
+
end
|
58
|
+
|
59
|
+
it "sets @client to nil" do
|
60
|
+
@notifier.connect
|
61
|
+
@notifier.client.should_not be_nil
|
62
|
+
|
63
|
+
@notifier.disconnect
|
64
|
+
@notifier.client.should be_nil
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe :reconnect do
|
69
|
+
it "calls disconnect then connect" do
|
70
|
+
@notifier.should_receive(:disconnect).once.ordered
|
71
|
+
@notifier.should_receive(:connect).once.ordered
|
72
|
+
|
73
|
+
@notifier.reconnect
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe :connected? do
|
78
|
+
it "returns true if client#is_connected? is true" do
|
79
|
+
@notifier.connect
|
80
|
+
@notifier.client.instance_eval { @connected = true }
|
81
|
+
@notifier.should be_connected
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spec'
|
3
|
+
require 'set'
|
4
|
+
|
5
|
+
$: << File.dirname(__FILE__)
|
6
|
+
$: << File.join(File.dirname(__FILE__),'../lib')
|
7
|
+
|
8
|
+
# Loads uppercut and jabber
|
9
|
+
require 'uppercut'
|
10
|
+
|
11
|
+
# Unloads jabber, replacing it with a stub
|
12
|
+
require 'jabber_stub'
|
13
|
+
|
14
|
+
class TestAgent < Uppercut::Agent
|
15
|
+
command 'hi' do |c,args|
|
16
|
+
c.instance_eval { @base.instance_eval { @called_hi = true } }
|
17
|
+
c.send 'called hi'
|
18
|
+
end
|
19
|
+
|
20
|
+
command /^hi/ do |c,args|
|
21
|
+
c.instance_eval { @base.instance_eval { @called_hi_regex = true } }
|
22
|
+
c.send 'called high regex'
|
23
|
+
end
|
24
|
+
|
25
|
+
command /(good)?bye/ do |c,args|
|
26
|
+
@called_goodbye = true
|
27
|
+
c.send args.first ? "Good bye to you as well!" : "Rot!"
|
28
|
+
end
|
29
|
+
|
30
|
+
command 'wait' do |c,args|
|
31
|
+
@called_wait = true
|
32
|
+
c.send 'Waiting...'
|
33
|
+
c.wait_for do |reply|
|
34
|
+
@called_wait_block = true
|
35
|
+
c.send 'Hooray!'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
Uppercut::Agent::VALID_CALLBACKS.each do |cb|
|
40
|
+
on(cb) { }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
class TestNotifier < Uppercut::Notifier
|
45
|
+
notifier :foo do |m,data|
|
46
|
+
m.to = 'foo@bar.com'
|
47
|
+
m.send 'Foo happened!'
|
48
|
+
end
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: uppercut
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.7.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tyler McMullen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-12-27 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: xmpp4r
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: A DSL for writing agents and notifiers for Jabber.
|
26
|
+
email: tbmcmullen@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files: []
|
32
|
+
|
33
|
+
files:
|
34
|
+
- README.textile
|
35
|
+
- VERSION.yml
|
36
|
+
- lib/uppercut/agent.rb
|
37
|
+
- lib/uppercut/base.rb
|
38
|
+
- lib/uppercut/conversation.rb
|
39
|
+
- lib/uppercut/message.rb
|
40
|
+
- lib/uppercut/notifier.rb
|
41
|
+
- lib/uppercut.rb
|
42
|
+
- spec/agent_spec.rb
|
43
|
+
- spec/conversation_spec.rb
|
44
|
+
- spec/jabber_stub.rb
|
45
|
+
- spec/notifier_spec.rb
|
46
|
+
- spec/spec_helper.rb
|
47
|
+
- examples/basic_agent.rb
|
48
|
+
- examples/personal.rb
|
49
|
+
has_rdoc: true
|
50
|
+
homepage: http://github.com/tyler/uppercut
|
51
|
+
licenses: []
|
52
|
+
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
version:
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
version:
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.3.5
|
74
|
+
signing_key:
|
75
|
+
specification_version: 2
|
76
|
+
summary: A DSL for writing agents and notifiers for Jabber.
|
77
|
+
test_files: []
|
78
|
+
|