ichverstehe-shout-bot 0.0.2

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.
Files changed (2) hide show
  1. data/shout-bot.rb +212 -0
  2. metadata +63 -0
data/shout-bot.rb ADDED
@@ -0,0 +1,212 @@
1
+ =begin
2
+ ShoutBot
3
+ Ridiculously simple library to quickly say something on IRC
4
+ <http://github.com/sr/shout-bot>
5
+
6
+ EXAMPLE
7
+
8
+ ShoutBot.shout('irc://shoutbot:password@irc.freenode.net:6667/#github') do |channel|
9
+ channel.say "check me out! http://github.com/sr/shout-bot"
10
+ end
11
+
12
+ LICENSE
13
+
14
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
15
+ Version 2, December 2004
16
+
17
+ Copyright (C) 2008, 2009 Simon Rozet <http://purl.org/net/sr/>
18
+ Copyright (C) 2008, 2009 Harry Vangberg <http://trueaffection.net>
19
+
20
+ Everyone is permitted to copy and distribute verbatim or modified
21
+ copies of this license document, and changing it is allowed as long
22
+ as the name is changed.
23
+
24
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
25
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
26
+
27
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
28
+ =end
29
+
30
+ require "rubygems"
31
+ require "addressable/uri"
32
+ require "socket"
33
+
34
+ class ShoutBot
35
+ def self.shout(uri, &block)
36
+ raise ArgumentError unless block_given?
37
+
38
+ uri = Addressable::URI.parse(uri)
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
46
+ end
47
+ end
48
+
49
+ attr_accessor :channel
50
+
51
+ def initialize(server, port, nick, password=nil)
52
+ raise ArgumentError unless block_given?
53
+
54
+ @socket = TCPSocket.open(server, port || 6667)
55
+ @socket.puts "PASSWORD #{password}" if password
56
+ @socket.puts "NICK #{nick}"
57
+ @socket.puts "USER #{nick} #{nick} #{nick} :#{nick}"
58
+ sleep 1
59
+ yield self
60
+ @socket.puts "QUIT"
61
+ @socket.gets until @socket.eof?
62
+ end
63
+
64
+ def join(channel)
65
+ raise ArgumentError unless block_given?
66
+
67
+ @channel = "##{channel}"
68
+ @socket.puts "JOIN #{@channel}"
69
+ yield self
70
+ @socket.puts "PART #{@channel}"
71
+ end
72
+
73
+ def say(message)
74
+ return unless @channel
75
+ @socket.puts "PRIVMSG #{@channel} :#{message}"
76
+ end
77
+ end
78
+
79
+ if $0 == __FILE__ || $0 == "-e"
80
+ require "test/unit"
81
+ require "context"
82
+ require "rr"
83
+
84
+ class ShoutBot
85
+ include Test::Unit::Assertions
86
+ end
87
+
88
+ class Test::Unit::TestCase
89
+ include RR::Adapters::TestUnit
90
+
91
+ def setup
92
+ @socket = StringIO.new
93
+ stub(TCPSocket).open(anything, anything) {@socket}
94
+ end
95
+ end
96
+
97
+ class TestShoutBot < Test::Unit::TestCase
98
+ def create_shoutbot(&block)
99
+ ShoutBot.new("irc.freenode.net", 6667, "john", &block || lambda {})
100
+ end
101
+
102
+ def create_shoutbot_and_register(&block)
103
+ create_shoutbot &block
104
+ @socket.rewind
105
+ 2.times { @socket.gets }
106
+ end
107
+
108
+ test "raises error if no block given" do
109
+ assert_raises ArgumentError do
110
+ ShoutBot.new("irc.freenode.net", 6667, "john")
111
+ end
112
+ end
113
+
114
+ test "registers to the irc server" do
115
+ create_shoutbot
116
+ @socket.rewind
117
+ assert_equal "NICK john\n", @socket.gets
118
+ assert_equal "USER john john john :john\n", @socket.gets
119
+ end
120
+
121
+ test "sends password if specified" do
122
+ ShoutBot.new("irc.freenode.net", 6667, "john", "malbec") {}
123
+ @socket.rewind
124
+ assert_equal "PASSWORD malbec\n", @socket.gets
125
+ end
126
+
127
+ test "falls back to port 6667 if not specified" do
128
+ # talk about retarded test
129
+ mock(TCPSocket).open("irc.freenode.net", 6667) {StringIO.new}
130
+ ShoutBot.new("irc.freenode.net", nil, "john") {}
131
+ end
132
+
133
+ test "raises error if no block is given to join" do
134
+ create_shoutbot do |bot|
135
+ assert_raises(ArgumentError) {bot.join "integrity"}
136
+ end
137
+ end
138
+
139
+ test "joins channel" do
140
+ create_shoutbot_and_register do |bot|
141
+ bot.join("integrity") {}
142
+ end
143
+ assert_equal "JOIN #integrity\n", @socket.gets
144
+ end
145
+
146
+ test "joins channel and says something" do
147
+ create_shoutbot_and_register do |bot|
148
+ bot.join "integrity" do |c|
149
+ c.say "foo bar!"
150
+ end
151
+ end
152
+ @socket.gets # JOIN
153
+ assert_equal "PRIVMSG #integrity :foo bar!\n", @socket.gets
154
+ end
155
+
156
+ test "sends private message to user" do
157
+ create_shoutbot_and_register do |bot|
158
+ bot.channel = "sr"
159
+ bot.say "Look Ma, new tests!"
160
+ end
161
+ assert_equal "PRIVMSG sr :Look Ma, new tests!\n", @socket.gets
162
+ end
163
+ end
164
+
165
+ class TestShouter < Test::Unit::TestCase
166
+ def create_shouter(&block)
167
+ shouter = ShoutBot.new("irc.freenode.net", 6667, "shouter") {}
168
+ mock(ShoutBot).new(anything, anything, anything, anything).yields(shouter) {shouter}
169
+ shouter
170
+ end
171
+
172
+ test "raises error unless block is given" do
173
+ assert_raises ArgumentError do
174
+ ShoutBot.shout("irc://shouter@irc.freenode.net:6667/foo")
175
+ end
176
+ end
177
+
178
+ test "creates a new instance of shoutbot" do
179
+ mock(ShoutBot).new("irc.freenode.net", 6667, "shouter", nil)
180
+ ShoutBot.shout("irc://shouter@irc.freenode.net:6667/foo") {}
181
+ end
182
+
183
+ test "creates a new instance of shoutbot with password" do
184
+ mock(ShoutBot).new("irc.freenode.net", 6667, "shouter", "badass")
185
+ ShoutBot.shout("irc://shouter:badass@irc.freenode.net:6667/foo") {}
186
+ end
187
+
188
+ test "joins channel" do
189
+ shouter = create_shouter
190
+ mock(shouter).join("integrity")
191
+ ShoutBot.shout("irc://shouter@irc.freenode.net:6667/#integrity") {}
192
+ end
193
+
194
+ test "says stuff in channel" do
195
+ shouter = create_shouter
196
+ mock(shouter).say("foo bar!")
197
+ ShoutBot.shout("irc://shouter@irc.freenode.net:6667/#integrity") do |bot|
198
+ bot.say "foo bar!"
199
+ end
200
+ assert_equal "#integrity", shouter.channel
201
+ end
202
+
203
+ test "sends private message to nick" do
204
+ shouter = create_shouter
205
+ mock(shouter).say("foo bar!")
206
+ ShoutBot.shout("irc://shouter@irc.freenode.net:6667/harry") do |bot|
207
+ bot.say "foo bar!"
208
+ end
209
+ assert_equal "harry", shouter.channel
210
+ end
211
+ end
212
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ichverstehe-shout-bot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Simon Rozet
8
+ - Harry Vangberg
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-03-02 00:00:00 -08:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: addressable
18
+ type: :runtime
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ version:
26
+ description: Ridiculously simple library to quickly say something on IRC
27
+ email: simon@rozet.name
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files: []
33
+
34
+ files:
35
+ - shout-bot.rb
36
+ has_rdoc: false
37
+ homepage: http://github.com/sr/shout-bot
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - .
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.2.0
59
+ signing_key:
60
+ specification_version: 2
61
+ summary: Ridiculously simple library to quickly say something on IRC
62
+ test_files: []
63
+