flamethrower 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/.gitignore +3 -0
  2. data/.rspec +2 -0
  3. data/Gemfile +10 -0
  4. data/Gemfile.lock +29 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.rdoc +36 -0
  7. data/Rakefile +28 -0
  8. data/VERSION +1 -0
  9. data/bin/flamethrower +59 -0
  10. data/flamethrower.gemspec +106 -0
  11. data/lib/flamethrower.rb +25 -0
  12. data/lib/flamethrower/campfire/connection.rb +38 -0
  13. data/lib/flamethrower/campfire/message.rb +42 -0
  14. data/lib/flamethrower/campfire/rest_api.rb +40 -0
  15. data/lib/flamethrower/campfire/room.rb +142 -0
  16. data/lib/flamethrower/campfire/user.rb +17 -0
  17. data/lib/flamethrower/dispatcher.rb +102 -0
  18. data/lib/flamethrower/irc/channel.rb +55 -0
  19. data/lib/flamethrower/irc/codes.rb +17 -0
  20. data/lib/flamethrower/irc/commands.rb +57 -0
  21. data/lib/flamethrower/irc/message.rb +41 -0
  22. data/lib/flamethrower/irc/user.rb +33 -0
  23. data/lib/flamethrower/server.rb +43 -0
  24. data/lib/flamethrower/server/event_server.rb +28 -0
  25. data/lib/flamethrower/server/mock_server.rb +13 -0
  26. data/spec/fixtures/enter_message.json +1 -0
  27. data/spec/fixtures/kick_message.json +1 -0
  28. data/spec/fixtures/leave_message.json +1 -0
  29. data/spec/fixtures/message.json +1 -0
  30. data/spec/fixtures/paste_message.json +1 -0
  31. data/spec/fixtures/room.json +1 -0
  32. data/spec/fixtures/room_update.json +1 -0
  33. data/spec/fixtures/rooms.json +1 -0
  34. data/spec/fixtures/speak_message.json +1 -0
  35. data/spec/fixtures/streaming_message.json +1 -0
  36. data/spec/spec_helper.rb +13 -0
  37. data/spec/unit/campfire/connection_spec.rb +37 -0
  38. data/spec/unit/campfire/message_spec.rb +85 -0
  39. data/spec/unit/campfire/room_spec.rb +296 -0
  40. data/spec/unit/campfire/user_spec.rb +32 -0
  41. data/spec/unit/dispatcher_spec.rb +255 -0
  42. data/spec/unit/irc/channel_spec.rb +56 -0
  43. data/spec/unit/irc/message_spec.rb +32 -0
  44. data/spec/unit/irc/user_spec.rb +63 -0
  45. data/spec/unit/server/event_server_spec.rb +16 -0
  46. data/spec/unit/server_spec.rb +164 -0
  47. metadata +165 -0
@@ -0,0 +1,32 @@
1
+ require File.join(File.dirname(__FILE__), "../../spec_helper")
2
+
3
+ describe Flamethrower::Campfire::User do
4
+ before do
5
+ @user = Flamethrower::Campfire::User.new('name' => "Bob Jackson", 'id' => 1234)
6
+ end
7
+
8
+ it "should have name" do
9
+ @user.name.should == "Bob Jackson"
10
+ end
11
+
12
+ it "should have the user number (id)" do
13
+ @user.number.should == 1234
14
+ end
15
+
16
+ describe "#to_irc" do
17
+ it "sets the username and nickname to the campfire user's name" do
18
+ @user.name = "bob"
19
+ @user.to_irc.nickname.should == "bob"
20
+ @user.to_irc.username.should == "bob"
21
+ end
22
+
23
+ it "joins spaced names with underscores" do
24
+ @user.to_irc.nickname.should == "Bob_Jackson"
25
+ @user.to_irc.username.should == "Bob_Jackson"
26
+ end
27
+
28
+ it "sets the user hostname to campfirenow.com" do
29
+ @user.to_irc.hostname.should == "campfirenow.com"
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,255 @@
1
+ require File.join(File.dirname(__FILE__), "../spec_helper")
2
+
3
+ describe Flamethrower::Dispatcher do
4
+ before do
5
+ @server = Flamethrower::MockServer.new(:log => Logger.new("/dev/null"))
6
+ @room = Flamethrower::Campfire::Room.new('mydomain', 'mytoken', {'name' => 'a room', 'id' => 347348})
7
+ @channel = Flamethrower::Irc::Channel.new("#flamethrower", @room)
8
+ @server.irc_channels << @channel
9
+ @dispatcher = Flamethrower::Dispatcher.new(@server)
10
+ end
11
+
12
+ describe "#handle_message" do
13
+ it "sends the message to the right command handler method" do
14
+ message = Flamethrower::Irc::Message.new("USER stuff\r\n")
15
+ @dispatcher.should_receive(:handle_user).with(message)
16
+ @dispatcher.handle_message(message)
17
+ end
18
+
19
+ it "doesn't send the message to the handler method if the method doesn't exist" do
20
+ message = Flamethrower::Irc::Message.new("BOGUS stuff\r\n")
21
+ @dispatcher.should_not_receive(:BOGUS)
22
+ @dispatcher.handle_message(message)
23
+ end
24
+ end
25
+
26
+ describe "#user" do
27
+ it "sets the current session's user to the specified user" do
28
+ message = Flamethrower::Irc::Message.new("USER guest tolmoon tolsun :Ronnie Reagan\r\n")
29
+ @dispatcher.handle_message(message)
30
+ @dispatcher.server.current_user.username.should == "guest"
31
+ @dispatcher.server.current_user.hostname.should == "tolmoon"
32
+ @dispatcher.server.current_user.servername.should == "tolsun"
33
+ @dispatcher.server.current_user.realname.should == "Ronnie Reagan"
34
+ end
35
+
36
+ it "not set a second user request if a first has already been recieved" do
37
+ message = Flamethrower::Irc::Message.new("USER guest tolmoon tolsun :Ronnie Reagan\r\n")
38
+ message2 = Flamethrower::Irc::Message.new("USER guest2 tolmoon2 tolsun2 :Ronnie Reagan2\r\n")
39
+ @dispatcher.handle_message(message)
40
+ @dispatcher.handle_message(message2)
41
+ @dispatcher.server.current_user.username.should == "guest"
42
+ @dispatcher.server.current_user.hostname.should == "tolmoon"
43
+ @dispatcher.server.current_user.servername.should == "tolsun"
44
+ @dispatcher.server.current_user.realname.should == "Ronnie Reagan"
45
+ end
46
+ end
47
+
48
+ describe "#topic" do
49
+ before do
50
+ @user = Flamethrower::Irc::User.new :username => "user", :nickname => "nick", :hostname => "host", :realname => "realname", :servername => "servername"
51
+ @server.current_user = @user
52
+ end
53
+
54
+ context "retrieving the channel topic" do
55
+ it "should display the channel topic" do
56
+ message = Flamethrower::Irc::Message.new("TOPIC #flamethrower")
57
+ @dispatcher.server.should_receive(:send_topic).with(@channel)
58
+ @dispatcher.handle_message(message)
59
+ end
60
+
61
+ it "responds with an error if the channel can't be found" do
62
+ message = Flamethrower::Irc::Message.new("TOPIC #bogus")
63
+ @dispatcher.server.should_not_receive(:send_topic)
64
+ @dispatcher.server.should_receive(:send_message).with(":#{@user.hostname} 475")
65
+ @dispatcher.handle_message(message)
66
+ end
67
+ end
68
+
69
+ context "setting the channel topic" do
70
+ it "sets the channel topic to the specified topic" do
71
+ FakeWeb.register_uri(:put, "https://mytoken:x@mydomain.campfirenow.com/room/347348.json", :body => json_fixture("room_update"), :status => ["200", "OK"])
72
+ message = Flamethrower::Irc::Message.new("TOPIC #flamethrower :some awesome topic")
73
+ @dispatcher.server.should_receive(:send_topic).with(@channel)
74
+ @dispatcher.handle_message(message)
75
+ @channel.topic.should == "some awesome topic"
76
+ end
77
+ end
78
+ end
79
+
80
+ describe "#mode" do
81
+ before do
82
+ @user = Flamethrower::Irc::User.new :username => "user", :nickname => "nick", :hostname => "host", :realname => "realname", :servername => "servername"
83
+ @server.current_user = @user
84
+ end
85
+
86
+ context "channel mode" do
87
+ it "responds to mode with a static channel mode" do
88
+ message = Flamethrower::Irc::Message.new("MODE #flamethrower\r\n")
89
+ @dispatcher.server.should_receive(:send_channel_mode).with(@channel)
90
+ @dispatcher.handle_message(message)
91
+ end
92
+
93
+ it "responds with the user mode if the mode isn't for a channel" do
94
+ message = Flamethrower::Irc::Message.new("MODE #{@user.nickname} +i\r\n")
95
+ @dispatcher.server.should_receive(:send_user_mode)
96
+ @dispatcher.handle_message(message)
97
+ end
98
+
99
+ it "responds with unknown command if the mode is neither a server nor the current user" do
100
+ message = Flamethrower::Irc::Message.new("MODE foo\r\n")
101
+ @dispatcher.server.should_receive(:send_message).with(":#{@user.hostname} 421")
102
+ @dispatcher.handle_message(message)
103
+ end
104
+ end
105
+ end
106
+
107
+ describe "#nick" do
108
+ it "sets the current session's user nickname to the specified nick" do
109
+ message = Flamethrower::Irc::Message.new("NICK WiZ\r\n")
110
+ @dispatcher.handle_message(message)
111
+ @dispatcher.server.current_user.nickname.should == "WiZ"
112
+ end
113
+ end
114
+
115
+ describe "#privmsg" do
116
+ context "sent by me" do
117
+ it "generates and queues an outbound campfire message to the right room" do
118
+ message = Flamethrower::Irc::Message.new("PRIVMSG #test :Hello.\r\n")
119
+ room = Flamethrower::Campfire::Room.new("mydomain", "mytoken", {'name' => "test"})
120
+ irc_channel = room.to_irc
121
+ @dispatcher.server.irc_channels << irc_channel
122
+ @dispatcher.handle_message(message)
123
+ room.outbound_messages.size.should == 1
124
+ end
125
+
126
+ it "sends the right message parameters to the new outbound message" do
127
+ message = Flamethrower::Irc::Message.new("PRIVMSG #test :Hello.\r\n")
128
+ room = Flamethrower::Campfire::Room.new("mydomain", "mytoken", {'name' => "test"})
129
+ irc_channel = room.to_irc
130
+ @dispatcher.server.irc_channels << irc_channel
131
+ @dispatcher.handle_message(message)
132
+ campfire_message = room.outbound_messages.pop
133
+ campfire_message.body.should == "Hello."
134
+ campfire_message.user.should be_nil
135
+ campfire_message.message_type.should == "TextMessage"
136
+ end
137
+ end
138
+ end
139
+
140
+ describe "#ping" do
141
+ it "responds with pong of the same ping parameters" do
142
+ message = Flamethrower::Irc::Message.new("PING :something\r\n")
143
+ @server.should_receive(:send_message).with("PONG :something")
144
+ @dispatcher.handle_message(message)
145
+ end
146
+ end
147
+
148
+ describe "#part" do
149
+ it "stops the thread of the room being parted from" do
150
+ @room.instance_variable_set("@thread_running", true)
151
+ @room.should be_alive
152
+ message = Flamethrower::Irc::Message.new("PART #flamethrower")
153
+ @dispatcher.handle_message(message)
154
+ @room.should_not be_alive
155
+ end
156
+
157
+ it "responds with ERR_BADCHANNELKEY a channel that doesn't exist" do
158
+ user = Flamethrower::Irc::User.new :username => "user", :nickname => "nick", :hostname => "host", :realname => "realname", :servername => "servername"
159
+ @server.current_user = user
160
+ message = Flamethrower::Irc::Message.new("PART #foobar")
161
+ @server.should_receive(:send_message).with(":#{user.hostname} 475")
162
+ @dispatcher.handle_message(message)
163
+ end
164
+ end
165
+
166
+ describe "#quit" do
167
+ it "kills all the room threads on quit" do
168
+ @room.instance_variable_set("@thread_running", true)
169
+ @room.should be_alive
170
+ message = Flamethrower::Irc::Message.new("QUIT :leaving")
171
+ @dispatcher.handle_message(message)
172
+ @room.should_not be_alive
173
+ end
174
+ end
175
+
176
+ describe "#join" do
177
+ before do
178
+ @room.stub(:fetch_room_info)
179
+ @room.stub(:join)
180
+ end
181
+
182
+ it "responds with a topic and userlist if sent a join" do
183
+ message = Flamethrower::Irc::Message.new("JOIN #flamethrower\r\n")
184
+ @server.should_receive(:send_topic).with(@channel)
185
+ @server.should_receive(:send_userlist)
186
+ @dispatcher.handle_message(message)
187
+ end
188
+
189
+ it "adds the current user to the channel" do
190
+ user = Flamethrower::Irc::User.new :username => "user", :nickname => "nick", :hostname => "host", :realname => "realname", :servername => "servername"
191
+ @server.current_user = user
192
+ message = Flamethrower::Irc::Message.new("JOIN #flamethrower\r\n")
193
+ @dispatcher.handle_message(message)
194
+ @channel.users.should include(user)
195
+ end
196
+
197
+ it "fetches the room information on join" do
198
+ message = Flamethrower::Irc::Message.new("JOIN #flamethrower\r\n")
199
+ @room.should_receive(:fetch_room_info)
200
+ @dispatcher.handle_message(message)
201
+ end
202
+
203
+ it "responds with ERR_BADCHANNELKEY a channel that doesn't exist" do
204
+ user = Flamethrower::Irc::User.new :username => "user", :nickname => "nick", :hostname => "host", :realname => "realname", :servername => "servername"
205
+ @server.current_user = user
206
+ message = Flamethrower::Irc::Message.new("JOIN #foobar\r\n")
207
+ @server.should_receive(:send_message).with(":#{user.hostname} 475")
208
+ @server.should_not_receive(:send_topic)
209
+ @server.should_not_receive(:send_userlist)
210
+ @dispatcher.handle_message(message)
211
+ end
212
+
213
+ it "sends a join command to the API" do
214
+ message = Flamethrower::Irc::Message.new("JOIN #flamethrower\r\n")
215
+ @room.should_receive(:join)
216
+ @dispatcher.handle_message(message)
217
+ end
218
+ end
219
+
220
+ context "after a nick and user has been set" do
221
+ before do
222
+ @user_message = Flamethrower::Irc::Message.new("USER guest tolmoon tolsun :Ronnie Reagan\r\n")
223
+ @nick_message = Flamethrower::Irc::Message.new("NICK WiZ\r\n")
224
+ end
225
+
226
+ describe "nick sent first" do
227
+ it "sends the motd, join" do
228
+ @dispatcher.server.should_receive(:after_connect)
229
+ @dispatcher.handle_message(@nick_message)
230
+ @dispatcher.handle_message(@user_message)
231
+ end
232
+
233
+ it "doesn't send after_connect if only the nick has been sent" do
234
+ @dispatcher.server.should_not_receive(:after_connect)
235
+ @dispatcher.handle_message(@nick_message)
236
+ end
237
+
238
+ end
239
+
240
+ describe "user sent first" do
241
+ it "sends the motd" do
242
+ @dispatcher.server.should_receive(:after_connect)
243
+ @dispatcher.handle_message(@user_message)
244
+ @dispatcher.handle_message(@nick_message)
245
+ end
246
+
247
+ it "doesn't send after_connect if only the nick has been sent" do
248
+ @dispatcher.server.should_not_receive(:after_connect)
249
+ @dispatcher.handle_message(@user_message)
250
+ end
251
+
252
+ end
253
+ end
254
+
255
+ end
@@ -0,0 +1,56 @@
1
+ require File.join(File.dirname(__FILE__), "../../spec_helper")
2
+
3
+ describe Flamethrower::Irc::Channel do
4
+ before do
5
+ @room = Flamethrower::Campfire::Room.new("mydomain", "mytoken", {'name' => "flamethrower"})
6
+ @channel = Flamethrower::Irc::Channel.new("#flamethrower", @room)
7
+ @campfire_user = Flamethrower::Campfire::User.new('name' => "bob", 'id' => 734581)
8
+ @irc_user = @campfire_user.to_irc
9
+ end
10
+
11
+ it "returns the current channel name" do
12
+ @channel.name.should == "#flamethrower"
13
+ end
14
+
15
+ it "returns the current channel topic" do
16
+ @channel.topic = "The topic"
17
+ @channel.topic.should == "The topic"
18
+ end
19
+
20
+ it "returns the current user list" do
21
+ user = Flamethrower::Irc::User.new
22
+ @channel.users << user
23
+ @channel.users.should include(user)
24
+ end
25
+
26
+ it "returns the channel mode from an array of modes" do
27
+ @channel.mode.should == "+t"
28
+ end
29
+
30
+ describe "#to_campfire" do
31
+ it "returns the stored copy of the campfire room" do
32
+ @channel.to_campfire.should == @room
33
+ end
34
+ end
35
+
36
+ describe "#irc_messages" do
37
+ it "returns the irc messages to be sent to the client" do
38
+ message = Flamethrower::Campfire::Message.new('body' => 'Hello there', 'user' => @campfire_user, 'room' => @room, 'type' => 'TextMessage')
39
+ @room.inbound_messages << message
40
+ @channel.retrieve_irc_messages.map(&:to_s).should == [":#{@irc_user.to_s} PRIVMSG #{@channel.name} :Hello there"]
41
+ end
42
+
43
+ it "doesn't send TimeStamp messages" do
44
+ message = Flamethrower::Campfire::Message.new('type' => 'TimestampMessage', 'body' => 'Hello there', 'user' => @campfire_user, 'room' => @room)
45
+ @room.inbound_messages << message
46
+ @channel.retrieve_irc_messages.should be_empty
47
+ end
48
+
49
+ xit "doesn't send the message if the source is the current user" do
50
+ message = Flamethrower::Campfire::Message.new('body' => 'Hello there', 'user' => @campfire_user, 'room' => @room, 'type' => 'TextMessage')
51
+ @room.inbound_messages << message
52
+ @room.campfire_user = @current_user
53
+ @channel.retrieve_irc_messages.map(&:to_s).should be_empty
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,32 @@
1
+ require File.join(File.dirname(__FILE__), "../../spec_helper")
2
+
3
+ describe Flamethrower::Irc::Message do
4
+ it "returns the params from the @message hash" do
5
+ message = Flamethrower::Irc::Message.new("COMMAND param")
6
+ message.command.should == "COMMAND"
7
+ end
8
+
9
+ it "returns the command from the @message hash" do
10
+ message = Flamethrower::Irc::Message.new("COMMAND param")
11
+ message.parameters.should == ["param"]
12
+ end
13
+
14
+ describe "USER" do
15
+ it "should break a string into command and params" do
16
+ message = Flamethrower::Irc::Message.new("USER guest tolmoon tolsun :Ronnie Reagan\r\n")
17
+ message.parse.should == {:command => "USER", :params => ["guest", "tolmoon", "tolsun", "Ronnie Reagan"]}
18
+ end
19
+
20
+ it "strips out the prefix character from the message" do
21
+ message = Flamethrower::Irc::Message.new("COMMAND :Ronnie Reagan\r\n")
22
+ message.send(:strip_prefixes, [":Ronnie", "Reagan"]).should == ["Ronnie Reagan"]
23
+ end
24
+ end
25
+
26
+ describe "#to_s" do
27
+ it "returns the irc message" do
28
+ message = Flamethrower::Irc::Message.new("COMMAND param")
29
+ message.to_s.should == "COMMAND param"
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,63 @@
1
+ require File.join(File.dirname(__FILE__), "../../spec_helper")
2
+
3
+ describe Flamethrower::Irc::User do
4
+ before do
5
+ @user = Flamethrower::Irc::User.new
6
+ @initial_user = Flamethrower::Irc::User.new :username => "user", :nickname => "nick", :hostname => "host", :realname => "realname", :servername => "servername"
7
+ end
8
+
9
+ context "storing initial arguments" do
10
+ it "should have user" do
11
+ @initial_user.username.should == "user"
12
+ end
13
+
14
+ it "should have nick" do
15
+ @initial_user.nickname.should == "nick"
16
+ end
17
+
18
+ it "should have host" do
19
+ @initial_user.hostname.should == "host"
20
+ end
21
+
22
+ it "should have realname" do
23
+ @initial_user.realname.should == "realname"
24
+ end
25
+
26
+ it "should have servername" do
27
+ @initial_user.servername.should == "servername"
28
+ end
29
+ end
30
+
31
+ describe "#nick_set?" do
32
+ it "returns true if nickname is set" do
33
+ user = Flamethrower::Irc::User.new :nickname => "nick"
34
+ user.nick_set?.should be_true
35
+ end
36
+
37
+ it "returns false if nickname is not set" do
38
+ Flamethrower::Irc::User.new.nick_set?.should be_false
39
+ end
40
+ end
41
+
42
+ describe "#user_set?" do
43
+ it "returns true if username, hostname, realname, servername are set" do
44
+ user = Flamethrower::Irc::User.new :username => "user", :hostname => "host", :realname => "realname", :servername => "servername"
45
+ user.user_set?.should be_true
46
+ end
47
+
48
+ it "returns true if username, hostname, realname, servername are set" do
49
+ Flamethrower::Irc::User.new.user_set?.should be_false
50
+ end
51
+ end
52
+
53
+ describe "#to_s" do
54
+ it "should display the correct irc user string" do
55
+ @initial_user.to_s.should == "nick!user@host"
56
+ end
57
+ end
58
+
59
+ it "displays user mode given an array of modes" do
60
+ @user.mode.should == "+i"
61
+ end
62
+
63
+ end
@@ -0,0 +1,16 @@
1
+ require File.join(File.dirname(__FILE__), "../../spec_helper")
2
+
3
+ describe Flamethrower::EventServer do
4
+ before do
5
+ @event_server = Flamethrower::EventServer.new("0.0.0.0", 6667, "mydomain", "mytoken")
6
+ end
7
+
8
+ it "initializes the host" do
9
+ @event_server.host.should == "0.0.0.0"
10
+ end
11
+
12
+ it "initializes the port" do
13
+ @event_server.port.should == 6667
14
+ end
15
+
16
+ end