flamethrower 0.3.0 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -4,6 +4,7 @@ gem 'eventmachine'
4
4
  gem 'twitter-stream'
5
5
  gem 'json'
6
6
  gem 'em-http-request'
7
+ gem 'daemons'
7
8
 
8
9
  group :test do
9
10
  gem 'rspec'
@@ -3,6 +3,7 @@ GEM
3
3
  specs:
4
4
  addressable (2.2.2)
5
5
  crack (0.1.8)
6
+ daemons (1.1.0)
6
7
  diff-lcs (1.1.2)
7
8
  em-http-request (0.2.15)
8
9
  addressable (>= 2.0.0)
@@ -29,6 +30,7 @@ PLATFORMS
29
30
  ruby
30
31
 
31
32
  DEPENDENCIES
33
+ daemons
32
34
  em-http-request
33
35
  eventmachine
34
36
  json
@@ -1,5 +1,7 @@
1
1
  = Flamethrower: An irc to campfire gateway
2
2
 
3
+ * Now with ASCII art conversion! (Oooo, shiney!)
4
+
3
5
  Flamethrower gives you the power to use your awesome irc client to
4
6
  talk in your campfire rooms.
5
7
 
@@ -23,14 +25,18 @@ Then start flamethrower like so:
23
25
 
24
26
  flamethrower -c ~/myconfig.yml
25
27
 
26
- === A work in progress
28
+ By default, flamethrower will use the config file located at ~/.flamethrower/config.yml.
29
+
30
+ === Contributing
27
31
 
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
32
+ Flamethrower is a constant work in progress. If you find it useful and would like to see it do something, please submit a patch (with tests please)!. Bug reports are
31
33
  also highly appreciated.
32
34
 
33
- === Author
35
+ === Authors
34
36
 
35
37
  Flamethrower is written by Blake Smith <blakesmith0@gmail.com>
36
38
 
39
+ Contributions by:
40
+
41
+ Alexander Rakoczy[https://github.com/toothrot]
42
+
data/Rakefile CHANGED
@@ -22,6 +22,7 @@ begin
22
22
  gemspec.add_dependency('json')
23
23
  gemspec.add_dependency('em-http-request')
24
24
  gemspec.add_dependency('twitter-stream')
25
+ gemspec.add_dependency('daemons')
25
26
  end
26
27
  rescue LoadError
27
28
  puts "Jeweler not available. Install it with: gem install jeweler"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.3.2
@@ -60,7 +60,7 @@ end
60
60
 
61
61
  FLAMETHROWER_LOGGER = Logger.new(options['logger'] || STDOUT)
62
62
 
63
- server = Flamethrower::EventServer.new(options['host'], options['port'], options['domain'], options['token'])
63
+ server = Flamethrower::EventServer.new(options)
64
64
 
65
65
  trap("INT") do
66
66
  FLAMETHROWER_LOGGER.info("Received shutdown signal, killing connections")
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'daemons'
5
+
6
+ flamethrower_dir = File.join(ENV["HOME"], '.flamethrower')
7
+ config = File.join(flamethrower_dir, 'config.yml')
8
+ options = {
9
+ :dir => flamethrower_dir,
10
+ :dir_mode => :normal,
11
+ :log_output => true,
12
+ :backtrace => true
13
+ }
14
+
15
+ if File.exists?(config)
16
+ script = File.join(File.dirname(__FILE__), 'flamethrower')
17
+ Daemons.run(script, options)
18
+ else
19
+ puts "Unable to find config file! Please create $HOME/.flamethrower/config.yml and specify your domain and campfire API token"
20
+ end
@@ -5,15 +5,14 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{flamethrower}
8
- s.version = "0.3.0"
8
+ s.version = "0.3.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Blake Smith"]
12
- s.date = %q{2011-01-20}
13
- s.default_executable = %q{flamethrower}
12
+ s.date = %q{2012-04-29}
14
13
  s.description = %q{Flamethrower gives you the power to use your awesome irc client to talk in your campfire rooms.}
15
14
  s.email = %q{blakesmith0@gmail.com}
16
- s.executables = ["flamethrower"]
15
+ s.executables = ["flamethrower", "flamethrowerd"]
17
16
  s.extra_rdoc_files = [
18
17
  "README.rdoc"
19
18
  ]
@@ -27,34 +26,40 @@ Gem::Specification.new do |s|
27
26
  "Rakefile",
28
27
  "VERSION",
29
28
  "bin/flamethrower",
29
+ "bin/flamethrowerd",
30
30
  "flamethrower.gemspec",
31
+ "flamethrower.yml.example",
31
32
  "lib/flamethrower.rb",
33
+ "lib/flamethrower/ascii_imager.rb",
32
34
  "lib/flamethrower/campfire/connection.rb",
33
35
  "lib/flamethrower/campfire/message.rb",
34
36
  "lib/flamethrower/campfire/rest_api.rb",
35
37
  "lib/flamethrower/campfire/room.rb",
36
38
  "lib/flamethrower/campfire/user.rb",
39
+ "lib/flamethrower/connection.rb",
37
40
  "lib/flamethrower/dispatcher.rb",
38
41
  "lib/flamethrower/irc/channel.rb",
39
42
  "lib/flamethrower/irc/codes.rb",
40
43
  "lib/flamethrower/irc/commands.rb",
41
44
  "lib/flamethrower/irc/message.rb",
42
45
  "lib/flamethrower/irc/user.rb",
43
- "lib/flamethrower/server.rb",
44
46
  "lib/flamethrower/server/event_server.rb",
45
- "lib/flamethrower/server/mock_server.rb",
47
+ "lib/flamethrower/server/mock_connection.rb",
46
48
  "spec/fixtures/enter_message.json",
47
49
  "spec/fixtures/kick_message.json",
48
50
  "spec/fixtures/leave_message.json",
49
51
  "spec/fixtures/message.json",
50
52
  "spec/fixtures/paste_message.json",
51
53
  "spec/fixtures/paste_message_with_pound.json",
54
+ "spec/fixtures/recent_messages.json",
52
55
  "spec/fixtures/room.json",
53
56
  "spec/fixtures/room_update.json",
54
57
  "spec/fixtures/rooms.json",
55
58
  "spec/fixtures/speak_message.json",
59
+ "spec/fixtures/streaming_image_message.json",
56
60
  "spec/fixtures/streaming_message.json",
57
61
  "spec/fixtures/timestamp_message.json",
62
+ "spec/fixtures/tweet_message.json",
58
63
  "spec/fixtures/upload_message.json",
59
64
  "spec/fixtures/upload_url_sample",
60
65
  "spec/fixtures/user.json",
@@ -63,12 +68,12 @@ Gem::Specification.new do |s|
63
68
  "spec/unit/campfire/message_spec.rb",
64
69
  "spec/unit/campfire/room_spec.rb",
65
70
  "spec/unit/campfire/user_spec.rb",
71
+ "spec/unit/connection_spec.rb",
66
72
  "spec/unit/dispatcher_spec.rb",
67
73
  "spec/unit/irc/channel_spec.rb",
68
74
  "spec/unit/irc/message_spec.rb",
69
75
  "spec/unit/irc/user_spec.rb",
70
- "spec/unit/server/event_server_spec.rb",
71
- "spec/unit/server_spec.rb"
76
+ "spec/unit/server/event_server_spec.rb"
72
77
  ]
73
78
  s.homepage = %q{http://github.com/blakesmith/flamethrower}
74
79
  s.rdoc_options = ["--charset=UTF-8"]
@@ -81,12 +86,12 @@ Gem::Specification.new do |s|
81
86
  "spec/unit/campfire/message_spec.rb",
82
87
  "spec/unit/campfire/room_spec.rb",
83
88
  "spec/unit/campfire/user_spec.rb",
89
+ "spec/unit/connection_spec.rb",
84
90
  "spec/unit/dispatcher_spec.rb",
85
91
  "spec/unit/irc/channel_spec.rb",
86
92
  "spec/unit/irc/message_spec.rb",
87
93
  "spec/unit/irc/user_spec.rb",
88
- "spec/unit/server/event_server_spec.rb",
89
- "spec/unit/server_spec.rb"
94
+ "spec/unit/server/event_server_spec.rb"
90
95
  ]
91
96
 
92
97
  if s.respond_to? :specification_version then
@@ -98,17 +103,20 @@ Gem::Specification.new do |s|
98
103
  s.add_runtime_dependency(%q<json>, [">= 0"])
99
104
  s.add_runtime_dependency(%q<em-http-request>, [">= 0"])
100
105
  s.add_runtime_dependency(%q<twitter-stream>, [">= 0"])
106
+ s.add_runtime_dependency(%q<daemons>, [">= 0"])
101
107
  else
102
108
  s.add_dependency(%q<eventmachine>, [">= 0.12.10"])
103
109
  s.add_dependency(%q<json>, [">= 0"])
104
110
  s.add_dependency(%q<em-http-request>, [">= 0"])
105
111
  s.add_dependency(%q<twitter-stream>, [">= 0"])
112
+ s.add_dependency(%q<daemons>, [">= 0"])
106
113
  end
107
114
  else
108
115
  s.add_dependency(%q<eventmachine>, [">= 0.12.10"])
109
116
  s.add_dependency(%q<json>, [">= 0"])
110
117
  s.add_dependency(%q<em-http-request>, [">= 0"])
111
118
  s.add_dependency(%q<twitter-stream>, [">= 0"])
119
+ s.add_dependency(%q<daemons>, [">= 0"])
112
120
  end
113
121
  end
114
122
 
@@ -0,0 +1,6 @@
1
+ domain: flamethrower # eg, flamethrower.campfirenow.com
2
+ token: c9be61aeb7c1a311f8f5a928ac6a0d41c93c4a72 # your personal API token
3
+ ascii_conversion:
4
+ enabled: true
5
+ service: "http://skeeter.blakesmith.me"
6
+ scale_to_width: 80
@@ -9,6 +9,8 @@ require 'twitter/json_stream'
9
9
  require 'logger'
10
10
  require 'json'
11
11
 
12
+ require 'ascii_imager'
13
+
12
14
  require 'irc/codes'
13
15
  require 'irc/commands'
14
16
  require 'irc/user'
@@ -21,6 +23,6 @@ require 'campfire/message'
21
23
  require 'campfire/connection'
22
24
  require 'campfire/room'
23
25
 
24
- require 'server'
26
+ require 'connection'
25
27
  require 'server/event_server'
26
28
  require 'dispatcher'
@@ -0,0 +1,16 @@
1
+ module Flamethrower
2
+ module AsciiImager
3
+ DEFAULT_IMAGE_ASCII_SERVICE = "http://skeeter.blakesmith.me"
4
+ DEFAULT_IMAGE_WIDTH = 80
5
+
6
+ def image_get(url)
7
+ host = config['service'] || DEFAULT_IMAGE_ASCII_SERVICE
8
+ image_width = config['scale_to_width'] || DEFAULT_IMAGE_WIDTH
9
+ EventMachine::HttpRequest.new(host).get :query => {'image_url' => url, 'width' => image_width}
10
+ end
11
+
12
+ def config
13
+ @connection.server.ascii_conversion
14
+ end
15
+ end
16
+ end
@@ -5,10 +5,10 @@ module Flamethrower
5
5
 
6
6
  include Flamethrower::Campfire::RestApi
7
7
 
8
- def initialize(domain, token, server)
8
+ def initialize(domain, token, connection)
9
9
  @domain = domain
10
10
  @token = token
11
- @server = server
11
+ @connection = connection
12
12
  end
13
13
 
14
14
  def fetch_my_user
@@ -17,10 +17,10 @@ module Flamethrower
17
17
  case http.response_header.status
18
18
  when 200
19
19
  json = JSON.parse(http.response)
20
- old_user = @server.current_user.nickname
20
+ old_user = @connection.current_user.nickname
21
21
  new_user = Flamethrower::Campfire::User.new(json['user']).to_irc
22
- @server.current_user = new_user
23
- @server.send_rename(old_user, new_user.nickname)
22
+ @connection.current_user = new_user
23
+ @connection.send_rename(old_user, new_user.nickname)
24
24
  end
25
25
  end
26
26
  end
@@ -34,18 +34,15 @@ module Flamethrower
34
34
  json = JSON.parse(http.response)
35
35
  json['rooms'].each do |room|
36
36
  rooms << Room.new(@domain, @token, room).tap do |r|
37
- r.server = @server
37
+ r.connection = @connection
38
38
  end
39
39
  end
40
- @server.irc_channels = rooms.map(&:to_irc)
41
- @server.send_channel_list
40
+ @connection.irc_channels = rooms.map(&:to_irc)
41
+ @connection.send_channel_list
42
42
  else
43
43
  ::FLAMETHROWER_LOGGER.debug http.response
44
44
  end
45
45
  end
46
- http.errback do
47
- @server.send_message @server.reply(Flamethrower::Irc::Codes::RPL_MOTD, ":ERROR: Unable to fetch room list! Check your connection?")
48
- end
49
46
  end
50
47
  end
51
48
  end
@@ -1,7 +1,7 @@
1
1
  module Flamethrower
2
2
  module Campfire
3
3
  class Message
4
- attr_accessor :body, :user, :room, :message_type, :status, :retry_at, :user_id
4
+ attr_accessor :body, :user, :room, :direction, :message_type, :status, :retry_at, :image_converted, :user_id
5
5
 
6
6
  RETRY_SECONDS = 15
7
7
 
@@ -9,9 +9,11 @@ module Flamethrower
9
9
  @body = params['body']
10
10
  @user = params['user']
11
11
  @room = params['room']
12
- @user_id = params['user_id']
12
+ @user_id = set_user_id(params)
13
13
  @message_type = params['type']
14
+ @direction = params['direction']
14
15
  @status = "pending"
16
+ @image_converted = false
15
17
  end
16
18
 
17
19
  def mark_delivered!
@@ -23,6 +25,48 @@ module Flamethrower
23
25
  @retry_at = Time.now + RETRY_SECONDS
24
26
  end
25
27
 
28
+ def mark_pending!
29
+ @status = "pending"
30
+ end
31
+
32
+ def failed?
33
+ @status == "failed"
34
+ end
35
+
36
+ def pending?
37
+ @status == "pending"
38
+ end
39
+
40
+ def outbound?
41
+ @direction == "outbound"
42
+ end
43
+
44
+ def inbound?
45
+ @direction == "inbound"
46
+ end
47
+
48
+ def needs_image_conversion?
49
+ has_images? && !@image_converted
50
+ end
51
+
52
+ def image_urls
53
+ if @body
54
+ @body.scan(/(https?:\/\/.+?\.(?:jpg|jpeg|gif|png))/i).flatten
55
+ else
56
+ []
57
+ end
58
+ end
59
+
60
+ def has_images?
61
+ image_urls.size > 0
62
+ end
63
+
64
+ def set_ascii_image(str)
65
+ @body << "\n#{str}"
66
+ @message_type = "PasteMessage"
67
+ @image_converted = true
68
+ end
69
+
26
70
  def to_irc
27
71
  case message_type
28
72
  when "TextMessage"
@@ -50,6 +94,18 @@ module Flamethrower
50
94
  end
51
95
  message.join("\r\n")
52
96
  end
97
+
98
+ def set_user_id(params)
99
+ if params['user_id']
100
+ params['user_id']
101
+ elsif params['user'].is_a?(User)
102
+ params['user'].number
103
+ elsif params['user'] && params['user'].is_a?(Hash)
104
+ params['user']['id']
105
+ else
106
+ nil
107
+ end
108
+ end
53
109
  end
54
110
  end
55
111
  end
@@ -6,15 +6,14 @@ module Flamethrower
6
6
  "https://#{@domain}.campfirenow.com"
7
7
  end
8
8
 
9
- private
10
- def http
11
- EventMachine::HttpRequest.new(host)
12
- end
13
-
14
- def campfire_get(path)
9
+ def campfire_get(path, args = {})
15
10
  action_log("get", path, nil)
16
11
  full_path = host << path
17
- EventMachine::HttpRequest.new(full_path).get :head => {'authorization' => [@token, 'x']}
12
+ http = EventMachine::HttpRequest.new(full_path).get(
13
+ :head => {'authorization' => [@token, 'x']},
14
+ :query => args)
15
+ http.errback { on_connection_error("get", path) }
16
+ http
18
17
  end
19
18
 
20
19
  def campfire_post(path, json=nil)
@@ -23,6 +22,8 @@ module Flamethrower
23
22
  params = {:head => {'Content-Type' => 'application/json', 'authorization' => [@token, 'x']}}
24
23
  params[:body] = json if json
25
24
  http = EventMachine::HttpRequest.new(full_path).post params
25
+ http.errback { on_connection_error("post", path) }
26
+ http
26
27
  end
27
28
 
28
29
  def campfire_put(path, json=nil)
@@ -30,7 +31,9 @@ module Flamethrower
30
31
  full_path = host << path
31
32
  params = {:head => {'Content-Type' => 'application/json', 'authorization' => [@token, 'x']}}
32
33
  params[:body] = json if json
33
- EventMachine::HttpRequest.new(full_path).put params
34
+ http = EventMachine::HttpRequest.new(full_path).put params
35
+ http.errback { on_connection_error("put", path) }
36
+ http
34
37
  end
35
38
 
36
39
  private
@@ -39,6 +42,10 @@ module Flamethrower
39
42
  ::FLAMETHROWER_LOGGER.debug "Sending #{action.upcase} #{path} with #{json || 'no'} JSON"
40
43
  end
41
44
 
45
+ def on_connection_error(action, path)
46
+ @connection.send_message @connection.reply(Flamethrower::Irc::Codes::RPL_MOTD, ":ERROR: Unable to make API call #{action.upcase} #{path}. Check your connection?")
47
+ end
48
+
42
49
  end
43
50
  end
44
51
  end
@@ -1,14 +1,16 @@
1
1
  module Flamethrower
2
2
  module Campfire
3
3
  class Room
4
+ MAX_RECONNECT_TIMOUT_SECONDS = 20
4
5
  POLL_SECONDS = 0.5
5
6
  PERIODIC_UPDATE_SECONDS = 60 * 10
6
7
 
7
8
  include Flamethrower::Campfire::RestApi
9
+ include Flamethrower::AsciiImager
8
10
 
9
11
  attr_reader :stream, :token
10
12
  attr_writer :topic
11
- attr_accessor :inbound_messages, :outbound_messages, :thread_messages, :number, :name, :users, :server
13
+ attr_accessor :inbound_messages, :outbound_messages, :thread_messages, :number, :name, :users, :connection
12
14
  attr_accessor :failed_messages, :joined
13
15
 
14
16
  def initialize(domain, token, params = {})
@@ -17,6 +19,7 @@ module Flamethrower
17
19
  @inbound_messages = Queue.new
18
20
  @outbound_messages = Queue.new
19
21
  @users_to_fetch = Queue.new
22
+ @images_to_fetch = Queue.new
20
23
  @failed_messages = []
21
24
  @number = params['id']
22
25
  @name = params['name']
@@ -39,8 +42,8 @@ module Flamethrower
39
42
  end
40
43
 
41
44
  def send_info
42
- @server.send_topic(to_irc)
43
- @server.send_userlist(to_irc)
45
+ @connection.send_topic(to_irc)
46
+ @connection.send_userlist(to_irc)
44
47
  end
45
48
 
46
49
  def fetch_room_info
@@ -55,24 +58,42 @@ module Flamethrower
55
58
  @users << Flamethrower::Campfire::User.new(user)
56
59
  end
57
60
  resolve_renames(old_users, @users)
58
- send_info unless @room_info_sent
61
+ unless @room_info_sent
62
+ send_info
63
+ fetch_recent_messages
64
+ end
59
65
  @room_info_sent = true
60
66
  end
61
67
  end
62
68
  end
63
69
 
70
+ def fetch_recent_messages
71
+ http = campfire_get("/room/#{@number}/recent.json", :limit => 10)
72
+ http.callback do
73
+ case http.response_header.status
74
+ when 200
75
+ json = JSON.parse(http.response)
76
+ json['messages'].each do |json_message|
77
+ process_inbound_json_message(json_message)
78
+ end
79
+ end
80
+ end
81
+ end
82
+
64
83
  def resolve_renames(old_users, new_users)
65
84
  old_users.each do |old_user|
66
85
  user = new_users.detect {|new_user| new_user.number == old_user.number}
67
- unless old_user.name == user.name
68
- @server.send_rename(old_user.to_irc.nickname, user.to_irc.nickname)
86
+ if user
87
+ unless old_user.name == user.name
88
+ @connection.send_rename(old_user.to_irc.nickname, user.to_irc.nickname)
89
+ end
69
90
  end
70
91
  end
71
92
  end
72
93
 
73
94
  def say(body, message_type='TextMessage')
74
- params = {'body' => translate_nicknames(body), 'type' => message_type}
75
- @outbound_messages << Flamethrower::Campfire::Message.new(params)
95
+ params = {'body' => translate_nicknames(body), 'type' => message_type, 'direction' => 'outbound'}
96
+ sort_and_dispatch_message(Flamethrower::Campfire::Message.new(params))
76
97
  end
77
98
 
78
99
  def start
@@ -88,18 +109,20 @@ module Flamethrower
88
109
  EventMachine.cancel_timer(@polling_timer)
89
110
  EventMachine.cancel_timer(@periodic_timer)
90
111
  @room_alive = false
112
+ @room_info_sent = false
91
113
  end
92
114
 
93
115
  def poll
94
116
  unless dead?
117
+ requeue_failed_messages
95
118
  fetch_messages
96
119
  post_messages
97
- requeue_failed_messages
98
120
  fetch_users
121
+ fetch_images
99
122
  messages_to_send = to_irc.retrieve_irc_messages
100
123
  messages_to_send.each do |m|
101
124
  ::FLAMETHROWER_LOGGER.debug "Sending irc message #{m.to_s}"
102
- @server.send_message(m.to_s)
125
+ @connection.send_message(m.to_s)
103
126
  end
104
127
  end
105
128
  end
@@ -124,20 +147,29 @@ module Flamethrower
124
147
  @stream = Twitter::JSONStream.connect(:path => "/room/#{@number}/live.json",
125
148
  :host => "streaming.campfirenow.com",
126
149
  :auth => "#{@token}:x")
150
+ setup_stream_callbacks
127
151
  end
128
152
 
129
153
  def fetch_messages
130
154
  @stream.each_item do |item|
131
155
  ::FLAMETHROWER_LOGGER.debug "Got json message #{item.inspect}"
132
- params = JSON.parse(item)
133
- params['user'] = @users.find {|u| u.number == params['user_id'] }
134
- params['room'] = self
135
- message = Flamethrower::Campfire::Message.new(params)
136
- unless message.message_type == "TimestampMessage"
137
- unless message.user
138
- @users_to_fetch << message
139
- else
140
- @inbound_messages << message
156
+ process_inbound_json_message(JSON.parse(item))
157
+ end
158
+ end
159
+
160
+ def fetch_images
161
+ until @images_to_fetch.empty?
162
+ message = @images_to_fetch.pop
163
+ message.image_urls.each do |url|
164
+ http = image_get(url)
165
+ http.callback do
166
+ case http.response_header.status
167
+ when 200
168
+ message.set_ascii_image(http.response)
169
+ else
170
+ message.mark_failed!
171
+ end
172
+ sort_and_dispatch_message(message)
141
173
  end
142
174
  end
143
175
  end
@@ -154,8 +186,10 @@ module Flamethrower
154
186
  user = Flamethrower::Campfire::User.new(json['user'])
155
187
  message.user = user
156
188
  @users << user
157
- @inbound_messages << message
189
+ else
190
+ message.mark_failed!
158
191
  end
192
+ sort_and_dispatch_message(message)
159
193
  end
160
194
  end
161
195
  end
@@ -173,7 +207,7 @@ module Flamethrower
173
207
  else
174
208
  ::FLAMETHROWER_LOGGER.debug "Failed to post to campfire API with code: #{http.response_header.status} body: #{http.response}"
175
209
  message.mark_failed!
176
- @failed_messages << message
210
+ sort_and_dispatch_message(message)
177
211
  end
178
212
  end
179
213
  end
@@ -184,7 +218,7 @@ module Flamethrower
184
218
  until @inbound_messages.empty?
185
219
  message = @inbound_messages.pop
186
220
  next unless message
187
- unless message.user.to_irc.nickname == @server.current_user.nickname
221
+ unless message.user.to_irc.nickname == @connection.current_user.nickname
188
222
  new_array << message
189
223
  end
190
224
  end
@@ -194,7 +228,8 @@ module Flamethrower
194
228
  def requeue_failed_messages
195
229
  @failed_messages.each do |m|
196
230
  if m.retry_at > Time.now
197
- @outbound_messages << m
231
+ m.mark_pending!
232
+ sort_and_dispatch_message(m)
198
233
  @failed_messages.delete(m)
199
234
  end
200
235
  end
@@ -209,8 +244,50 @@ module Flamethrower
209
244
  end
210
245
  end
211
246
 
247
+ def on_reconnect
248
+ ::FLAMETHROWER_LOGGER.debug "Reconnected to #{name} stream"
249
+ end
250
+
251
+ def on_error
252
+ ::FLAMETHROWER_LOGGER.debug "There was an error connecting to #{name} stream"
253
+ end
254
+
255
+ def on_max_reconnects
256
+ ::FLAMETHROWER_LOGGER.debug "Failed to reconnect to #{name}, restarting room in #{MAX_RECONNECT_TIMOUT_SECONDS} seconds"
257
+ end
258
+
212
259
  private
213
260
 
261
+ def sort_and_dispatch_message(message)
262
+ if message.failed?
263
+ @failed_messages << message
264
+ elsif message.inbound?
265
+ sort_and_dispatch_inbound_message(message)
266
+ else
267
+ @outbound_messages << message
268
+ end
269
+ end
270
+
271
+ def sort_and_dispatch_inbound_message(message)
272
+ if !message.user
273
+ @users_to_fetch << message
274
+ elsif @connection.server.ascii_conversion['enabled'] && message.needs_image_conversion?
275
+ @images_to_fetch << message
276
+ else
277
+ @inbound_messages << message
278
+ end
279
+ end
280
+
281
+ def process_inbound_json_message(json_message)
282
+ json_message['user'] = @users.find {|u| u.number == json_message['user_id'] }
283
+ json_message['room'] = self
284
+ json_message['direction'] = 'inbound'
285
+ message = Flamethrower::Campfire::Message.new(json_message)
286
+ unless message.message_type == "TimestampMessage"
287
+ sort_and_dispatch_message(message)
288
+ end
289
+ end
290
+
214
291
  def translate_nicknames(message_body)
215
292
  @users.each do |user|
216
293
  if message_body.include?(user.to_irc.nickname)
@@ -220,6 +297,12 @@ module Flamethrower
220
297
  message_body
221
298
  end
222
299
 
300
+ def setup_stream_callbacks
301
+ @stream.on_reconnect { self.on_reconnect }
302
+ @stream.on_error { self.on_error }
303
+ @stream.on_max_reconnects { self.on_max_reconnects }
304
+ end
305
+
223
306
  end
224
307
  end
225
308
  end