sr-shout-bot 0.0.1 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/shout-bot.rb +131 -83
- metadata +3 -2
data/shout-bot.rb
CHANGED
@@ -5,7 +5,7 @@ ShoutBot
|
|
5
5
|
|
6
6
|
EXAMPLE
|
7
7
|
|
8
|
-
ShoutBot.shout('irc://irc.freenode.net:6667
|
8
|
+
ShoutBot.shout('irc://shoutbot:password@irc.freenode.net:6667/#github') do |channel|
|
9
9
|
channel.say "check me out! http://github.com/sr/shout-bot"
|
10
10
|
end
|
11
11
|
|
@@ -14,8 +14,8 @@ LICENSE
|
|
14
14
|
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
15
15
|
Version 2, December 2004
|
16
16
|
|
17
|
-
Copyright (C) 2008 Simon Rozet <http://purl.org/net/sr/>
|
18
|
-
Copyright (C) 2008 Harry Vangberg <http://trueaffection.net>
|
17
|
+
Copyright (C) 2008, 2009 Simon Rozet <http://purl.org/net/sr/>
|
18
|
+
Copyright (C) 2008, 2009 Harry Vangberg <http://trueaffection.net>
|
19
19
|
|
20
20
|
Everyone is permitted to copy and distribute verbatim or modified
|
21
21
|
copies of this license document, and changing it is allowed as long
|
@@ -32,21 +32,30 @@ require "addressable/uri"
|
|
32
32
|
require "socket"
|
33
33
|
|
34
34
|
class ShoutBot
|
35
|
-
def self.shout(uri,
|
35
|
+
def self.shout(uri, &block)
|
36
36
|
raise ArgumentError unless block_given?
|
37
37
|
|
38
38
|
uri = Addressable::URI.parse(uri)
|
39
|
-
irc = new(uri.host, uri.port,
|
40
|
-
|
39
|
+
irc = new(uri.host, uri.port, uri.user, uri.password) do |irc|
|
40
|
+
if channel = uri.fragment
|
41
|
+
irc.join(channel, &block)
|
42
|
+
else
|
43
|
+
irc.channel = uri.path[1..-1]
|
44
|
+
yield irc
|
45
|
+
end
|
41
46
|
end
|
42
47
|
end
|
43
48
|
|
44
|
-
|
49
|
+
attr_accessor :channel
|
50
|
+
|
51
|
+
def initialize(server, port, nick, password=nil)
|
45
52
|
raise ArgumentError unless block_given?
|
46
53
|
|
47
|
-
@socket = TCPSocket.open(server, port)
|
54
|
+
@socket = TCPSocket.open(server, port || 6667)
|
55
|
+
@socket.puts "PASSWORD #{password}" if password
|
48
56
|
@socket.puts "NICK #{nick}"
|
49
57
|
@socket.puts "USER #{nick} #{nick} #{nick} :#{nick}"
|
58
|
+
sleep 1
|
50
59
|
yield self
|
51
60
|
@socket.puts "QUIT"
|
52
61
|
@socket.gets until @socket.eof?
|
@@ -67,115 +76,154 @@ class ShoutBot
|
|
67
76
|
end
|
68
77
|
end
|
69
78
|
|
70
|
-
if $0 == __FILE__
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
79
|
+
if $0 == __FILE__ || $0 == "-e"
|
80
|
+
require "test/unit"
|
81
|
+
require "context"
|
82
|
+
require "rr"
|
83
|
+
|
84
|
+
class MockSocket
|
85
|
+
attr_accessor :in, :out
|
86
|
+
def gets() @in.gets end
|
87
|
+
def puts(m) @out.puts(m) end
|
88
|
+
def eof?() true end
|
75
89
|
end
|
76
90
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
91
|
+
class ShoutBot
|
92
|
+
include Test::Unit::Assertions
|
93
|
+
end
|
94
|
+
|
95
|
+
class Test::Unit::TestCase
|
96
|
+
include RR::Adapters::TestUnit
|
81
97
|
|
82
|
-
setup
|
83
|
-
@socket
|
84
|
-
|
98
|
+
def setup
|
99
|
+
@socket, @server = MockSocket.new, MockSocket.new
|
100
|
+
@socket.in, @server.out = IO.pipe
|
101
|
+
@server.in, @socket.out = IO.pipe
|
102
|
+
|
103
|
+
stub(TCPSocket).open(anything, anything) {@socket}
|
85
104
|
end
|
105
|
+
end
|
86
106
|
|
87
|
-
|
88
|
-
|
107
|
+
class TestShoutBot < Test::Unit::TestCase
|
108
|
+
def create_shoutbot(&block)
|
109
|
+
ShoutBot.new("irc.freenode.net", 6667, "john", &block || lambda {})
|
89
110
|
end
|
90
111
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
112
|
+
def create_shoutbot_and_register(&block)
|
113
|
+
create_shoutbot &block
|
114
|
+
2.times { @server.gets } # NICK + USER
|
115
|
+
end
|
95
116
|
|
96
|
-
|
97
|
-
|
117
|
+
test "raises error if no block given" do
|
118
|
+
assert_raises ArgumentError do
|
119
|
+
ShoutBot.new("irc.freenode.net", 6667, "john")
|
98
120
|
end
|
121
|
+
end
|
99
122
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
123
|
+
test "registers to the irc server" do
|
124
|
+
create_shoutbot
|
125
|
+
assert_equal "NICK john\n", @server.gets
|
126
|
+
assert_equal "USER john john john :john\n", @server.gets
|
127
|
+
end
|
104
128
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
129
|
+
test "sends password if specified" do
|
130
|
+
# hey, retarded test!
|
131
|
+
ShoutBot.new("irc.freenode.net", 6667, "john", "malbec") {@socket}
|
132
|
+
assert_equal "PASSWORD malbec\n", @server.gets
|
133
|
+
end
|
134
|
+
|
135
|
+
test "falls back to port 6667 if not specified" do
|
136
|
+
# talk about retarded test
|
137
|
+
mock(TCPSocket).open("irc.freenode.net", 6667) {@socket}
|
138
|
+
ShoutBot.new("irc.freenode.net", nil, "john") {}
|
139
|
+
end
|
110
140
|
|
111
|
-
|
112
|
-
|
113
|
-
|
141
|
+
test "quits after doing its job" do
|
142
|
+
create_shoutbot_and_register {}
|
143
|
+
assert_equal "QUIT\n", @server.gets
|
114
144
|
end
|
115
145
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
create_shouter(nil)
|
120
|
-
end.should raise_error(ArgumentError)
|
146
|
+
test "raises error if no block is given to join" do
|
147
|
+
create_shoutbot do |bot|
|
148
|
+
assert_raises(ArgumentError) {bot.join "integrity"}
|
121
149
|
end
|
150
|
+
end
|
122
151
|
|
123
|
-
|
124
|
-
|
125
|
-
|
152
|
+
test "joins channel" do
|
153
|
+
create_shoutbot_and_register do |bot|
|
154
|
+
bot.join("integrity") {}
|
126
155
|
end
|
156
|
+
assert_equal "JOIN #integrity\n", @server.gets
|
157
|
+
end
|
127
158
|
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
end
|
159
|
+
test "doesn't do anything until receiving RPL_MYINFO / 004" do
|
160
|
+
# pending
|
161
|
+
end
|
132
162
|
|
133
|
-
|
134
|
-
|
163
|
+
test "joins channel and says something" do
|
164
|
+
create_shoutbot_and_register do |bot|
|
165
|
+
bot.join "integrity" do |c|
|
166
|
+
c.say "foo bar!"
|
167
|
+
end
|
135
168
|
end
|
169
|
+
@server.gets # JOIN
|
170
|
+
assert_equal "PRIVMSG #integrity :foo bar!\n", @server.gets
|
171
|
+
end
|
136
172
|
|
137
|
-
|
138
|
-
|
139
|
-
|
173
|
+
test "sends private message to user" do
|
174
|
+
create_shoutbot_and_register do |bot|
|
175
|
+
bot.channel = "sr"
|
176
|
+
bot.say "Look Ma, new tests!"
|
140
177
|
end
|
178
|
+
assert_equal "PRIVMSG sr :Look Ma, new tests!\n", @server.gets
|
141
179
|
end
|
180
|
+
end
|
142
181
|
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
182
|
+
class TestShouter < Test::Unit::TestCase
|
183
|
+
def create_shouter(&block)
|
184
|
+
shouter = ShoutBot.new("irc.freenode.net", 6667, "shouter") {}
|
185
|
+
mock(ShoutBot).new(anything, anything, anything, anything).yields(shouter) {shouter}
|
186
|
+
shouter
|
187
|
+
end
|
147
188
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
end.should raise_error(ArgumentError)
|
189
|
+
test "raises error unless block is given" do
|
190
|
+
assert_raises ArgumentError do
|
191
|
+
ShoutBot.shout("irc://shouter@irc.freenode.net:6667/foo")
|
152
192
|
end
|
193
|
+
end
|
153
194
|
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
195
|
+
test "creates a new instance of shoutbot" do
|
196
|
+
mock(ShoutBot).new("irc.freenode.net", 6667, "shouter", nil)
|
197
|
+
ShoutBot.shout("irc://shouter@irc.freenode.net:6667/foo") {}
|
198
|
+
end
|
158
199
|
|
159
|
-
|
160
|
-
|
161
|
-
|
200
|
+
test "creates a new instance of shoutbot with password" do
|
201
|
+
mock(ShoutBot).new("irc.freenode.net", 6667, "shouter", "badass")
|
202
|
+
ShoutBot.shout("irc://shouter:badass@irc.freenode.net:6667/foo") {}
|
203
|
+
end
|
162
204
|
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
205
|
+
test "joins channel" do
|
206
|
+
shouter = create_shouter
|
207
|
+
mock(shouter).join("integrity")
|
208
|
+
ShoutBot.shout("irc://shouter@irc.freenode.net:6667/#integrity") {}
|
167
209
|
end
|
168
210
|
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
211
|
+
test "says stuff in channel" do
|
212
|
+
shouter = create_shouter
|
213
|
+
mock(shouter).say("foo bar!")
|
214
|
+
ShoutBot.shout("irc://shouter@irc.freenode.net:6667/#integrity") do |bot|
|
215
|
+
bot.say "foo bar!"
|
173
216
|
end
|
217
|
+
assert_equal "#integrity", shouter.channel
|
218
|
+
end
|
174
219
|
|
175
|
-
|
176
|
-
|
177
|
-
|
220
|
+
test "sends private message to nick" do
|
221
|
+
shouter = create_shouter
|
222
|
+
mock(shouter).say("foo bar!")
|
223
|
+
ShoutBot.shout("irc://shouter@irc.freenode.net:6667/harry") do |bot|
|
224
|
+
bot.say "foo bar!"
|
178
225
|
end
|
226
|
+
assert_equal "harry", shouter.channel
|
179
227
|
end
|
180
228
|
end
|
181
229
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sr-shout-bot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Simon Rozet
|
@@ -10,11 +10,12 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date:
|
13
|
+
date: 2009-03-02 00:00:00 -08:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: addressable
|
18
|
+
type: :runtime
|
18
19
|
version_requirement:
|
19
20
|
version_requirements: !ruby/object:Gem::Requirement
|
20
21
|
requirements:
|