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,3 @@
1
+ *.swp
2
+ *.swo
3
+ *.swn
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ -b
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source :gemcutter
2
+
3
+ gem 'eventmachine'
4
+ gem 'twitter-stream'
5
+ gem 'json'
6
+
7
+ group :test do
8
+ gem 'rspec'
9
+ gem 'fakeweb'
10
+ end
@@ -0,0 +1,29 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ eventmachine (0.12.10)
6
+ fakeweb (1.3.0)
7
+ json (1.4.6)
8
+ roauth (0.0.3)
9
+ rspec (2.1.0)
10
+ rspec-core (~> 2.1.0)
11
+ rspec-expectations (~> 2.1.0)
12
+ rspec-mocks (~> 2.1.0)
13
+ rspec-core (2.1.0)
14
+ rspec-expectations (2.1.0)
15
+ diff-lcs (~> 1.1.2)
16
+ rspec-mocks (2.1.0)
17
+ twitter-stream (0.1.10)
18
+ eventmachine (>= 0.12.8)
19
+ roauth (>= 0.0.2)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ eventmachine
26
+ fakeweb
27
+ json
28
+ rspec
29
+ twitter-stream
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Blake Smith
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,36 @@
1
+ = Flamethrower: An irc to campfire gateway
2
+
3
+ Flamethrower gives you the power to use your awesome irc client to
4
+ talk in your campfire rooms.
5
+
6
+ = Usage:
7
+
8
+ === Getting Started
9
+
10
+ gem install flamethrower
11
+ flamethrower -d <campfire_domain> -t <campfire_api_token>
12
+
13
+ Then fire up your irc client and point it to localhost:6667. If your
14
+ domain and API token are valid, you should see a list of channels appear
15
+ in the MOTD message. /join anyone of these and start chatting.
16
+
17
+ You can also create a YAML file with domain and token specified like so:
18
+
19
+ domain: mydomain
20
+ token: aoeu1234
21
+
22
+ Then start flamethrower like so:
23
+
24
+ flamethrower -c ~/myconfig.yml
25
+
26
+ === A work in progress
27
+
28
+ Flamethrower is a work in progress. Right now basic messaging works, but
29
+ it still needs a lot of love. If you find it useful and would like to see
30
+ it do something, please submit a patch (with tests please)!. Bug reports are
31
+ also highly appreciated.
32
+
33
+ === Author
34
+
35
+ Flamethrower is written by Blake Smith <blakesmith0@gmail.com>
36
+
@@ -0,0 +1,28 @@
1
+ require 'rubygems'
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc 'Default: run specs.'
5
+ task :default => :spec
6
+
7
+ desc "Run specs"
8
+ RSpec::Core::RakeTask.new do |t|
9
+ t.pattern = "./spec/**/*_spec.rb"
10
+ end
11
+
12
+ begin
13
+ require 'jeweler'
14
+ Jeweler::Tasks.new do |gemspec|
15
+ gemspec.name = "flamethrower"
16
+ gemspec.summary = "Campfire IRC gateway"
17
+ gemspec.description = "Flamethrower gives you the power to use your awesome irc client to talk in your campfire rooms."
18
+ gemspec.email = "blakesmith0@gmail.com"
19
+ gemspec.homepage = "http://github.com/blakesmith/flamethrower"
20
+ gemspec.authors = ["Blake Smith"]
21
+ gemspec.add_dependency('eventmachine', '>=0.12.10')
22
+ gemspec.add_dependency('json')
23
+ gemspec.add_dependency('twitter-stream')
24
+ end
25
+ rescue LoadError
26
+ puts "Jeweler not available. Install it with: gem install jeweler"
27
+ end
28
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,59 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), "../lib")
4
+
5
+ help = <<HELP
6
+ Flamethrower is a campfire irc gateway that allows you to talk in campfire with irc.
7
+
8
+ Basic command line usage:
9
+ flamethrower -c /path/to/config.yml
10
+ flamethrower -t <campfire_api_token> -d campfiredomain
11
+
12
+ HELP
13
+
14
+ require 'rubygems'
15
+ require 'flamethrower'
16
+ require 'optparse'
17
+
18
+ options = {}
19
+ opts = OptionParser.new do |opts|
20
+ opts.banner = help
21
+
22
+ opts.on("-c", "--config [CONFIG]", "Set the config.yml file") do |config|
23
+ options['config'] = config
24
+ end
25
+
26
+ opts.on("-t", "--token [TOKEN]", "Set the campfire API token value") do |token|
27
+ options['token'] = token
28
+ end
29
+
30
+ opts.on("-d", "--domain [DOMAIN]", "Set the campfire domain") do |domain|
31
+ options['domain'] = domain
32
+ end
33
+
34
+ opts.on("-h", "--host [HOST]", "Sets the listening host") do |host|
35
+ options['host'] = host
36
+ end
37
+
38
+ opts.on("-p", "--port [PORT]", "Sets the listening port") do |port|
39
+ options['port'] = port.to_i
40
+ end
41
+ end
42
+
43
+ opts.parse!
44
+
45
+ if options['config']
46
+ case File.exists?(options['config'])
47
+ when true
48
+ require 'yaml'
49
+ yaml = YAML.load_file(options['config'])
50
+ yaml.each {|k, v| options[k] = v}
51
+ when false
52
+ puts "Unable to find the specified file! Check the path?"
53
+ end
54
+ end
55
+
56
+ FLAMETHROWER_LOGGER = Logger.new(options['logger'] || STDOUT)
57
+
58
+ server = Flamethrower::EventServer.new(options['host'], options['port'], options['domain'], options['token'])
59
+ server.start
@@ -0,0 +1,106 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{flamethrower}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Blake Smith"]
12
+ s.date = %q{2010-12-22}
13
+ s.default_executable = %q{flamethrower}
14
+ s.description = %q{Flamethrower gives you the power to use your awesome irc client to talk in your campfire rooms.}
15
+ s.email = %q{blakesmith0@gmail.com}
16
+ s.executables = ["flamethrower"]
17
+ s.extra_rdoc_files = [
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".gitignore",
22
+ ".rspec",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "MIT-LICENSE",
26
+ "README.rdoc",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "bin/flamethrower",
30
+ "flamethrower.gemspec",
31
+ "lib/flamethrower.rb",
32
+ "lib/flamethrower/campfire/connection.rb",
33
+ "lib/flamethrower/campfire/message.rb",
34
+ "lib/flamethrower/campfire/rest_api.rb",
35
+ "lib/flamethrower/campfire/room.rb",
36
+ "lib/flamethrower/campfire/user.rb",
37
+ "lib/flamethrower/dispatcher.rb",
38
+ "lib/flamethrower/irc/channel.rb",
39
+ "lib/flamethrower/irc/codes.rb",
40
+ "lib/flamethrower/irc/commands.rb",
41
+ "lib/flamethrower/irc/message.rb",
42
+ "lib/flamethrower/irc/user.rb",
43
+ "lib/flamethrower/server.rb",
44
+ "lib/flamethrower/server/event_server.rb",
45
+ "lib/flamethrower/server/mock_server.rb",
46
+ "spec/fixtures/enter_message.json",
47
+ "spec/fixtures/kick_message.json",
48
+ "spec/fixtures/leave_message.json",
49
+ "spec/fixtures/message.json",
50
+ "spec/fixtures/paste_message.json",
51
+ "spec/fixtures/room.json",
52
+ "spec/fixtures/room_update.json",
53
+ "spec/fixtures/rooms.json",
54
+ "spec/fixtures/speak_message.json",
55
+ "spec/fixtures/streaming_message.json",
56
+ "spec/spec_helper.rb",
57
+ "spec/unit/campfire/connection_spec.rb",
58
+ "spec/unit/campfire/message_spec.rb",
59
+ "spec/unit/campfire/room_spec.rb",
60
+ "spec/unit/campfire/user_spec.rb",
61
+ "spec/unit/dispatcher_spec.rb",
62
+ "spec/unit/irc/channel_spec.rb",
63
+ "spec/unit/irc/message_spec.rb",
64
+ "spec/unit/irc/user_spec.rb",
65
+ "spec/unit/server/event_server_spec.rb",
66
+ "spec/unit/server_spec.rb"
67
+ ]
68
+ s.homepage = %q{http://github.com/blakesmith/flamethrower}
69
+ s.rdoc_options = ["--charset=UTF-8"]
70
+ s.require_paths = ["lib"]
71
+ s.rubygems_version = %q{1.3.7}
72
+ s.summary = %q{Campfire IRC gateway}
73
+ s.test_files = [
74
+ "spec/spec_helper.rb",
75
+ "spec/unit/campfire/connection_spec.rb",
76
+ "spec/unit/campfire/message_spec.rb",
77
+ "spec/unit/campfire/room_spec.rb",
78
+ "spec/unit/campfire/user_spec.rb",
79
+ "spec/unit/dispatcher_spec.rb",
80
+ "spec/unit/irc/channel_spec.rb",
81
+ "spec/unit/irc/message_spec.rb",
82
+ "spec/unit/irc/user_spec.rb",
83
+ "spec/unit/server/event_server_spec.rb",
84
+ "spec/unit/server_spec.rb"
85
+ ]
86
+
87
+ if s.respond_to? :specification_version then
88
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
89
+ s.specification_version = 3
90
+
91
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
92
+ s.add_runtime_dependency(%q<eventmachine>, [">= 0.12.10"])
93
+ s.add_runtime_dependency(%q<json>, [">= 0"])
94
+ s.add_runtime_dependency(%q<twitter-stream>, [">= 0"])
95
+ else
96
+ s.add_dependency(%q<eventmachine>, [">= 0.12.10"])
97
+ s.add_dependency(%q<json>, [">= 0"])
98
+ s.add_dependency(%q<twitter-stream>, [">= 0"])
99
+ end
100
+ else
101
+ s.add_dependency(%q<eventmachine>, [">= 0.12.10"])
102
+ s.add_dependency(%q<json>, [">= 0"])
103
+ s.add_dependency(%q<twitter-stream>, [">= 0"])
104
+ end
105
+ end
106
+
@@ -0,0 +1,25 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "flamethrower")
2
+
3
+ require 'rubygems'
4
+
5
+ require 'net/https'
6
+ require 'eventmachine'
7
+ require 'twitter/json_stream'
8
+ require 'logger'
9
+ require 'json'
10
+
11
+ require 'irc/codes'
12
+ require 'irc/commands'
13
+ require 'irc/user'
14
+ require 'irc/channel'
15
+ require 'irc/message'
16
+
17
+ require 'campfire/rest_api'
18
+ require 'campfire/user'
19
+ require 'campfire/message'
20
+ require 'campfire/connection'
21
+ require 'campfire/room'
22
+
23
+ require 'server'
24
+ require 'server/event_server'
25
+ require 'dispatcher'
@@ -0,0 +1,38 @@
1
+ module Flamethrower
2
+ module Campfire
3
+ class Connection
4
+ attr_reader :token, :domain
5
+
6
+ include Flamethrower::Campfire::RestApi
7
+
8
+ def initialize(domain, token, server)
9
+ @domain = domain
10
+ @token = token
11
+ @server = server
12
+ end
13
+
14
+ def rooms
15
+ begin
16
+ @rooms ||= Array.new.tap do |rooms|
17
+ response = campfire_get("/rooms.json")
18
+ case response
19
+ when Net::HTTPSuccess
20
+ json = JSON.parse(response.body)
21
+ json['rooms'].each do |room|
22
+ rooms << Room.new(@domain, @token, room).tap do |r|
23
+ r.server = @server
24
+ end
25
+ end
26
+ else
27
+ ::FLAMETHROWER_LOGGER.debug response.body
28
+ end
29
+ end
30
+ rescue SocketError
31
+ @server.send_message @server.reply(Flamethrower::Irc::Codes::RPL_MOTD, ":ERROR: Unable to fetch room list! Check your connection?")
32
+ []
33
+ end
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,42 @@
1
+ module Flamethrower
2
+ module Campfire
3
+ class Message
4
+ attr_accessor :body, :user, :room, :message_type, :status, :retry_at
5
+
6
+ RETRY_SECONDS = 15
7
+
8
+ def initialize(params = {})
9
+ @body = params['body']
10
+ @user = params['user']
11
+ @room = params['room']
12
+ @message_type = params['type']
13
+ @status = "pending"
14
+ end
15
+
16
+ def mark_delivered!
17
+ @status = "delivered"
18
+ end
19
+
20
+ def mark_failed!
21
+ @status = "failed"
22
+ @retry_at = Time.now + RETRY_SECONDS
23
+ end
24
+
25
+ def to_irc
26
+ case message_type
27
+ when "TextMessage"
28
+ irc_string = ":#{@user.to_irc.to_s} PRIVMSG #{@room.to_irc.name} :#{@body}"
29
+ when "EnterMessage"
30
+ irc_string = ":#{@user.to_irc.to_s} JOIN #{@room.to_irc.name}"
31
+ when "KickMessage"
32
+ irc_string = ":#{@user.to_irc.to_s} PART #{@room.to_irc.name}"
33
+ when "LeaveMessage"
34
+ irc_string = ":#{@user.to_irc.to_s} PART #{@room.to_irc.name}"
35
+ when "PasteMessage"
36
+ irc_string = ":#{@user.to_irc.to_s} PRIVMSG #{@room.to_irc.name} :#{@body}"
37
+ end
38
+ Flamethrower::Irc::Message.new(irc_string)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,40 @@
1
+ module Flamethrower
2
+ module Campfire
3
+ module RestApi
4
+
5
+ def host
6
+ "#{@domain}.campfirenow.com"
7
+ end
8
+
9
+ private
10
+ def http
11
+ Net::HTTP.new(host, 443).tap do |connection|
12
+ connection.use_ssl = true
13
+ end
14
+ end
15
+
16
+ def campfire_get(path)
17
+ get = Net::HTTP::Get.new(path)
18
+ get.basic_auth @token, 'x'
19
+ http.request(get)
20
+ end
21
+
22
+ def campfire_post(path, json=nil)
23
+ put_or_post(Net::HTTP::Post, path, json)
24
+ end
25
+
26
+ def campfire_put(path, json=nil)
27
+ put_or_post(Net::HTTP::Put, path, json)
28
+ end
29
+
30
+ def put_or_post(request_type, path, json)
31
+ action = request_type.new(path)
32
+ action.basic_auth @token, 'x'
33
+ action.body = json if json
34
+ action.add_field "Content-Type", "application/json"
35
+ http.request(action)
36
+ end
37
+
38
+ end
39
+ end
40
+ end