coffeemaker 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/coffeemaker.gemspec CHANGED
@@ -18,6 +18,6 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
- s.add_runtime_dependency "eventmachine", "~> 1.0.0.beta.3"
21
+ s.add_runtime_dependency "eventmachine", "~> 1.0.0"
22
22
  s.add_runtime_dependency "activesupport"
23
23
  end
@@ -13,19 +13,22 @@ module Coffeemaker
13
13
  include ::EM::Protocols::LineText2
14
14
  include ::Coffeemaker::Bot::Irc::Commands
15
15
 
16
- attr_accessor :port, :host, :nick, :on_message, :on_connect
16
+ attr_accessor :port, :host, :nick, :on_message, :on_connect, :logger, :ssl, :user, :pass
17
17
 
18
18
  def connection_completed
19
- @reconnecting = false
20
- @connected = true
21
- _send_command :user, [@nick] * 4
22
- _send_command :nick, @nick
23
- on_connect.call if on_connect
24
- succeed
19
+ return complete_connection unless @ssl
20
+ start_tls
21
+ end
22
+
23
+ def ssl_handshake_completed
24
+ @logger.info "ssl handshake completed"
25
+ complete_connection
25
26
  end
26
27
 
27
28
  def receive_line(data)
28
29
  msg = ::Coffeemaker::Bot::Irc::Message.new(data)
30
+ @logger.debug "received #{data}"
31
+
29
32
  case msg.command
30
33
  when :ping
31
34
  send_command :pong
@@ -35,9 +38,12 @@ module Coffeemaker
35
38
  end
36
39
 
37
40
  def unbind
41
+ @logger.info "diconnected"
42
+
38
43
  @deferred_status = nil
39
44
  if @connected or @reconnecting
40
45
  EM.add_timer(1) do
46
+ @logger.info "reconnecting"
41
47
  reconnect(@host, @port)
42
48
  end
43
49
  @connected = false
@@ -49,13 +55,32 @@ module Coffeemaker
49
55
 
50
56
  private
51
57
  def _send_command(name, *args)
52
- cmd = [name.to_s.upcase] + args
53
- send_data("#{cmd.flatten.join(' ')}\r\n")
58
+ cmd = [name.to_s.upcase] + args
59
+ data = "#{cmd.flatten.join(' ')}\r\n"
60
+
61
+ @logger.debug "sending #{data}"
62
+ send_data(data)
54
63
  end
55
64
 
56
65
  def send_command(name, *args)
57
66
  callback { _send_command(name, *args) }
58
67
  end
68
+
69
+ def complete_connection
70
+ @reconnecting = false
71
+ @connected = true
72
+ @logger.info "connected"
73
+
74
+ @logger.info "authenticating"
75
+ _send_command :user, [@user] * 4
76
+ _send_command :pass, @pass if @pass
77
+ _send_command :nick, @nick
78
+
79
+ EM.add_timer(1) do
80
+ on_connect.call if on_connect
81
+ succeed
82
+ end
83
+ end
59
84
  end
60
85
  end
61
86
  end
@@ -6,7 +6,7 @@ module Coffeemaker
6
6
  class Bot
7
7
  class Irc
8
8
  attr_accessor :on_message, :on_connect
9
- attr_reader :host, :port, :nick, :connection
9
+ attr_reader :host, :port, :nick, :connection, :logger, :user, :pass
10
10
  private :connection
11
11
 
12
12
  delegate :join, :part, :msg, :privmsg, to: :connection
@@ -15,16 +15,24 @@ module Coffeemaker
15
15
  @host = options.delete(:irc_host)
16
16
  @port = options.delete(:irc_port)
17
17
  @nick = options.delete(:nick)
18
+ @pass = options.delete(:pass)
19
+ @user = options.delete(:user)
18
20
  @on_message = options.delete(:on_message)
21
+ @logger = options.delete(:logger)
22
+ @ssl = options.delete(:ssl)
19
23
  end
20
24
 
21
25
  def start
22
26
  @connection = EM.connect(@host, @port, Connection) do |c|
23
27
  c.host = @host
24
28
  c.port = @port
29
+ c.pass = @pass
30
+ c.user = @user
25
31
  c.nick = @nick
26
32
  c.on_message = @on_message
27
33
  c.on_connect = @on_connect
34
+ c.logger = @logger
35
+ c.ssl = @ssl
28
36
  end
29
37
  end
30
38
 
@@ -8,16 +8,14 @@ module Coffeemaker
8
8
  def initialize(argv)
9
9
  @options = default_options
10
10
  parse_options!(argv)
11
-
12
- if logfile = @options.delete(:logfile)
13
- @logger = Logger.new(logfile =~ /stdout/i ? STDOUT : logfile)
14
- end
11
+ @logger = Logger.new(@options.delete(:logfile))
12
+ @logger.level = @options.delete(:log_level)
15
13
  @channels = @options.delete(:channels)
16
14
  end
17
15
 
18
16
  def start
19
17
  EM.run do
20
- bot = Coffeemaker::Bot.new(@options)
18
+ bot = Coffeemaker::Bot.new(@options.merge(:logger => @logger))
21
19
  bot.start do |irc|
22
20
  @channels.each { |channel| irc.join(channel) }
23
21
  end
@@ -35,8 +33,11 @@ module Coffeemaker
35
33
  irc_host: 'localhost',
36
34
  irc_port: 6667,
37
35
  nick: 'coffeemaker',
36
+ user: 'coffeemaker',
38
37
  channels: [],
39
- on_message: Proc.new { |msg| @logger.info(msg) if @logger }
38
+ on_message: Proc.new { |msg| @logger.info(msg) if @logger },
39
+ logfile: STDOUT,
40
+ log_level: Logger::INFO
40
41
  }
41
42
  end
42
43
 
@@ -45,11 +46,15 @@ module Coffeemaker
45
46
  option.default_argv = argv
46
47
  option.banner = "Usage: coffeemaker [options]"
47
48
  option.on('-n NICK') { |nick| @options[:nick] = nick }
49
+ option.on('-u USER') { |user| @options[:user] = user }
50
+ option.on('-w PASS') { |pass| @options[:pass] = pass }
48
51
  option.on('-p PORT', Numeric) { |port| @options[:irc_port] = port }
49
52
  option.on('-s HOST') { |host| @options[:irc_host] = host }
50
53
  option.on('-c CHANNELS', 'comma-separated list of channels') { |channels| @options[:channels] = channels.split }
51
- option.on('-l LOG_FILE', 'path to log file or STDOUT') { |logfile| @options[:logfile] = logfile }
52
- option.on('--help') { puts option; exit }
54
+ option.on('-l LOG_FILE', 'path to log file') { |logfile| @options[:logfile] = logfile }
55
+ option.on('-d', '--debug') { |debug| @options[:log_level] = Logger::DEBUG }
56
+ option.on('--ssl') { |ssl| @options[:ssl] = true }
57
+ option.on_tail('--help') { puts option; exit }
53
58
  begin
54
59
  option.parse!
55
60
  rescue OptionParser::RequiredArgument, OptionParser::InvalidOption
@@ -1,3 +1,3 @@
1
1
  module Coffeemaker
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coffeemaker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-26 00:00:00.000000000 Z
12
+ date: 2013-02-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: eventmachine
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 1.0.0.beta.3
21
+ version: 1.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: 1.0.0.beta.3
29
+ version: 1.0.0
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: activesupport
32
32
  requirement: !ruby/object:Gem::Requirement
@@ -57,8 +57,6 @@ files:
57
57
  - Rakefile
58
58
  - bin/coffeemaker
59
59
  - coffeemaker.gemspec
60
- - goliath.rb
61
- - http.rb
62
60
  - lib/coffeemaker.rb
63
61
  - lib/coffeemaker/bot.rb
64
62
  - lib/coffeemaker/bot/irc.rb
@@ -87,8 +85,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
85
  version: '0'
88
86
  requirements: []
89
87
  rubyforge_project: coffeemaker
90
- rubygems_version: 1.8.24
88
+ rubygems_version: 1.8.23
91
89
  signing_key:
92
90
  specification_version: 3
93
91
  summary: IRC bot, how unexpected!
94
92
  test_files: []
93
+ has_rdoc:
data/goliath.rb DELETED
@@ -1,18 +0,0 @@
1
- require 'goliath/connection'
2
- require 'goliath/api'
3
-
4
- module Goliath
5
- module Constants
6
- IRC_CONNECTION = 'IRC_CONNECTION'
7
- end
8
-
9
- class Request
10
- alias_method :initialize_without_irc, :initialize
11
-
12
- def initialize(app, conn, env)
13
- initialize_without_irc(app, conn, env)
14
- env[IRC_CONNECTION] = conn.irc
15
- end
16
- end
17
- end
18
-
data/http.rb DELETED
@@ -1,57 +0,0 @@
1
- require 'coffeemaker'
2
- require 'coffeemaker/goliath'
3
- require 'json'
4
-
5
- module Coffeemaker
6
- class HTTP
7
- class Connection < Goliath::Connection
8
- attr_accessor :irc
9
- end
10
-
11
- class Router < Goliath::API
12
- get '/:channel' do
13
- run Stream.new
14
- end
15
- end
16
-
17
- class Stream < Goliath::API
18
- def on_close(env)
19
- env[IRC_CONNECTION].part(env[:channel])
20
- end
21
-
22
- def response(env)
23
- env[:channel] = params[:channel]
24
- env[IRC_CONNECTION].join(env[:channel])
25
-
26
- env[IRC_CONNECTION].on_message = Proc.new do |msg|
27
- env.stream_send("#{msg.to_json}\n")
28
- end
29
- streaming_response(202, {'X-Stream' => 'Goliath'})
30
- end
31
- end
32
-
33
- def initialize(options)
34
- @host = options.delete(:http_host)
35
- @port = options.delete(:http_port)
36
- @options = options
37
- end
38
-
39
- def start(irc_connection)
40
- Goliath.env = :production
41
- @http = EM.start_server(@host, @port, Coffeemaker::HTTP::Connection) do |c|
42
- c.port = 8080
43
- c.irc = irc_connection
44
- c.status = {}
45
- c.config = {}
46
- c.options = {}
47
- c.logger = Logger.new(STDOUT)
48
- c.app = Goliath::Rack::Builder.build(Coffeemaker::HTTP::Router, Coffeemaker::HTTP::Router.new)
49
- c.api = Coffeemaker::HTTP::Stream.new
50
- end
51
- end
52
-
53
- def stop
54
- EM.stop_server(@http)
55
- end
56
- end
57
- end