on_irc 2.0.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.0.0
1
+ 2.0.1
data/examples/bot.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
  require File.join(File.dirname(__FILE__), '..', 'lib', 'on_irc')
3
3
 
4
- IRC.configure do
4
+ bot = IRC.new do
5
5
  nick 'on_irc'
6
6
  ident 'on_irc'
7
7
  realname 'on_irc Ruby IRC library'
@@ -9,41 +9,32 @@ IRC.configure do
9
9
  server :eighthbit do
10
10
  address 'irc.eighthbit.net'
11
11
  end
12
-
13
- server :freenode do
14
- address 'irc.freenode.org'
15
- end
16
12
  end
17
13
 
18
14
 
19
- IRC[:freenode].on :'001' do |e|
20
- IRC.send(e.server, :join, '#botters')
21
- end
22
-
23
- IRC[:eighthbit].on :'001' do |e|
24
- IRC.send(e.server, :join, '#offtopic')
15
+ bot[:eighthbit].on '001' do
16
+ join '#bots'
25
17
  end
26
18
 
27
- IRC.on :privmsg do |e|
28
- case e.params[1]
19
+ bot.on :privmsg do
20
+ case params[1]
29
21
  when '!ping'
30
- IRC.send(e.server, :privmsg, e.params[0], e.prefix.split('!').first + ': pong')
22
+ msg(params[0], prefix.split('!').first + ': pong')
31
23
  when /^!echo (.*)/
32
- s = $1
33
- IRC.send(e.server, :privmsg, e.params[0], e.prefix.split('!').first + ': ' + s)
24
+ msg(params[0], prefix.split('!').first + ': ' + $1)
34
25
  when /^!join (.*)/
35
- IRC.send(e.server, :join, $1)
26
+ join $1
36
27
  end
37
28
  end
38
29
 
39
- IRC.on :ping do |e|
40
- IRC.send(e.server, :pong, e.params[0])
30
+ bot.on :ping do
31
+ pong params[0]
41
32
  end
42
33
 
43
- IRC.on :all do |e|
44
- prefix = "(#{e.prefix}) " unless e.prefix.empty?
45
- puts "#{e.server}: #{prefix}#{e.command} #{e.params.inspect}"
34
+ bot.on :all do
35
+ p = "(#{prefix}) " unless prefix.empty?
36
+ puts "#{server.name}: #{p}#{command} #{params.inspect}"
46
37
  end
47
38
 
48
- IRC.connect
39
+ bot.connect
49
40
 
@@ -5,26 +5,26 @@ MAX_BANGS = 3
5
5
  CH_USER_MEMORY = {}
6
6
  CHANNEL_MEMORY = {}
7
7
 
8
- IRC.configure do
8
+ bot = IRC.new do
9
9
  nick 'reggie'
10
10
  ident 'reggie'
11
11
  realname 'uses on_irc Ruby IRC library'
12
-
12
+
13
13
  server :eighthbit do
14
14
  address 'irc.eighthbit.net'
15
15
  end
16
-
16
+
17
17
  # server :freenode do
18
18
  # address 'irc.freenode.org'
19
19
  # end
20
20
  end
21
21
 
22
- IRC[:eighthbit].on :'001' do
22
+ bot[:eighthbit].on :'001' do
23
23
  join '#programming'
24
24
  join '#offtopic'
25
25
  end
26
26
 
27
- IRC.on :privmsg do
27
+ bot.on :privmsg do
28
28
  next unless params[0][0,1] == '#' # make sure regex replace only happens in channels
29
29
  channel = params[0]
30
30
  nick = prefix.split('!').first
@@ -85,14 +85,14 @@ IRC.on :privmsg do
85
85
  end
86
86
  end
87
87
 
88
- IRC.on :ping do
88
+ bot.on :ping do
89
89
  pong params[0]
90
90
  end
91
91
 
92
- IRC.on :all do
92
+ bot.on :all do
93
93
  prefix_str = "(#{prefix}) " unless prefix.empty?
94
94
  puts "#{server}: #{prefix_str}#{command} #{params.inspect}"
95
95
  end
96
96
 
97
- IRC.connect
97
+ bot.connect
98
98
 
data/examples/relay.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
  require File.join(File.dirname(__FILE__), '..', 'lib', 'on_irc')
3
3
 
4
- IRC.configure do
4
+ bot = IRC.new do
5
5
  nick 'on_irc-relay'
6
6
  ident 'on_irc'
7
7
  realname 'on_irc Ruby IRC library - relay example'
@@ -16,32 +16,31 @@ IRC.configure do
16
16
  end
17
17
 
18
18
 
19
- IRC[:freenode].on :'001' do |e|
20
- IRC.send(e.server, :join, '#botters')
19
+ bot[:freenode].on '001' do
20
+ join '#botters'
21
21
  end
22
22
 
23
- IRC[:eighthbit].on :'001' do |e|
24
- IRC.send(e.server, :join, '#offtopic')
23
+ bot[:eighthbit].on '001' do
24
+ join '#offtopic'
25
25
  end
26
26
 
27
- IRC.on :privmsg do |e|
28
- case e.params[1]
27
+ bot.on :privmsg do
28
+ case params[1]
29
29
  when /^fn> (.*)/
30
- msg = $1
31
- IRC.send(:freenode, :privmsg, '#botters', "<8b:#{e.prefix.split('!').first}> #{msg}") if e.params[0] == '#offtopic' && e.server == :eighthbit
30
+ bot[:freenode].send_cmd(:privmsg, '#botters', "<8b:#{prefix.split('!').first}> #{$1}") if params[0] == '#offtopic' && server.name == :eighthbit
32
31
  when /^8b> (.*)/
33
- msg = $1
34
- IRC.send(:eighthbit, :privmsg, '#offtopic', "<fn:#{e.prefix.split('!').first}> #{msg}") if e.params[0] == '#botters' && e.server == :freenode
32
+ bot[:eighthbit].send_cmd(:privmsg, '#offtopic', "<fn:#{prefix.split('!').first}> #{$1}") if params[0] == '#botters' && server.name == :freenode
35
33
  end
36
34
  end
37
35
 
38
- IRC.on :ping do |e|
39
- IRC.send(e.server, :pong, e.params[0])
36
+ bot.on :ping do
37
+ pong params[0]
40
38
  end
41
39
 
42
- IRC.on :all do |e|
43
- prefix = "(#{e.prefix}) " unless e.prefix.empty?
44
- puts "#{e.server}: #{prefix}#{e.command} #{e.params.inspect}"
40
+ bot.on :all do
41
+ p = "(#{prefix}) " unless prefix.empty?
42
+ puts "#{server.name}: #{p}#{command} #{params.inspect}"
45
43
  end
46
44
 
47
- IRC.connect
45
+ bot.connect
46
+
data/lib/on_irc.rb CHANGED
@@ -3,45 +3,45 @@
3
3
  require File.join(File.dirname(__FILE__), 'on_irc', lib)
4
4
  end
5
5
 
6
- module IRC
7
- class << self
8
- attr_accessor :config, :handlers
9
- config_accessor :nick, :ident, :realname, :servers
10
-
11
- def configure(&block)
12
- @config = ConfigDSL.run(&block)
13
- @handlers = {}
14
- end
6
+ class IRC
7
+ attr_accessor :config, :handlers, :servers
8
+ config_accessor :nick, :ident, :realname
15
9
 
16
- def on(event, &block)
17
- @handlers[event.to_s.downcase.to_sym] = Callback.new(block)
18
- end
19
-
20
- def [](server_id)
21
- servers[server_id]
22
- end
23
-
24
- def send(server, cmd, *args)
25
- cmd = cmd.to_s.upcase
26
- args[-1] = ':' + args[-1]
27
- IRC[server].connection.command(cmd, *args)
10
+ def initialize(&block)
11
+ @config = ConfigDSL.run(&block)
12
+ @servers = {}
13
+ @config.servers.each do |server_id, server_conf|
14
+ @servers[server_id] = Server.new(self, server_id, server_conf)
28
15
  end
16
+ @handlers = {}
17
+ end
18
+
19
+ def on(event, &block)
20
+ @handlers[event.to_s.downcase.to_sym] = Callback.new(block)
21
+ end
29
22
 
30
- def connect
31
- EventMachine.run do
32
- servers.each do |id, server|
33
- server.connection = EM.connect(server.address, server.port, Connection, id)
34
- end
23
+ def [](server_id)
24
+ servers[server_id]
25
+ end
26
+
27
+ def send_cmd(server_id, cmd, *args)
28
+ servers[server_id].send_cmd(cmd, *args)
29
+ end
30
+
31
+ def connect
32
+ EventMachine.run do
33
+ servers.values.each do |server|
34
+ server.connection = EM.connect(server.address, server.port, Connection, server)
35
35
  end
36
36
  end
37
-
38
- # for ssl
39
- # require 'openssl'
40
- # ssl_context = OpenSSL::SSL::SSLContext.new
41
- # ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
42
- # @socket = OpenSSL::SSL::SSLSocket.new(socket, ssl_context)
43
- # @socket.sync = true
44
- # @socket.connect
45
37
  end
38
+
39
+ # for ssl
40
+ # require 'openssl'
41
+ # ssl_context = OpenSSL::SSL::SSLContext.new
42
+ # ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
43
+ # @socket = OpenSSL::SSL::SSLSocket.new(socket, ssl_context)
44
+ # @socket.sync = true
45
+ # @socket.connect
46
46
  end
47
47
 
@@ -1,21 +1,22 @@
1
- module IRC
1
+ class IRC
2
2
  class Callback
3
3
  def initialize(block)
4
4
  @block = block
5
5
  end
6
6
 
7
- def call(event)
8
- CallbackDSL.run(event, @block)
7
+ def call(irc, event)
8
+ CallbackDSL.run(irc, event, @block)
9
9
  end
10
10
 
11
11
  class CallbackDSL
12
- def self.run(event, block)
13
- callbackdsl = self.new(event)
12
+ def self.run(irc, event, block)
13
+ callbackdsl = self.new(irc, event)
14
14
  block.arity < 1 ? callbackdsl.instance_eval(&block) : block.call(callbackdsl)
15
15
  end
16
16
 
17
- def initialize(event)
17
+ def initialize(irc, event)
18
18
  @event = event
19
+ @irc = irc
19
20
  end
20
21
 
21
22
  # @event accessors
@@ -36,28 +37,22 @@ module IRC
36
37
  end
37
38
 
38
39
  # commands
39
- def send(*args)
40
- if args[0].is_a?(Symbol) && args[1].is_a?(String)
41
- IRC.send(@event.server, *args) # now we don't have to do send(e.server, ...) all the time
42
- else
43
- IRC.send(*args)
44
- end
40
+ def send_cmd(cmd, *args)
41
+ @event.server.send_cmd(cmd, *args)
45
42
  end
46
43
 
47
- alias raw send
48
-
49
44
  def privmsg(target, message)
50
- send(:privmsg, target, message)
45
+ send_cmd(:privmsg, target, message)
51
46
  end
52
47
 
53
48
  alias msg privmsg
54
49
 
55
50
  def join(channel)
56
- send(:join, channel)
51
+ send_cmd(:join, channel)
57
52
  end
58
53
 
59
54
  def pong(msg)
60
- send(:pong, msg)
55
+ send_cmd(:pong, msg)
61
56
  end
62
57
 
63
58
  end
data/lib/on_irc/config.rb CHANGED
@@ -1,4 +1,4 @@
1
- module IRC
1
+ class IRC
2
2
  class ConfigError < StandardError; end
3
3
 
4
4
  Config = Struct.new(:nick, :ident, :realname, :servers)
@@ -6,29 +6,55 @@ module IRC
6
6
  class ConfigDSL
7
7
  dsl_accessor :nick, :ident, :realname
8
8
  attr_accessor :servers
9
-
9
+
10
10
  def self.run(&block)
11
11
  confdsl = self.new
12
12
  block.arity < 1 ? confdsl.instance_eval(&block) : block.call(confdsl)
13
-
13
+
14
14
  raise ConfigError, 'no nick' unless confdsl.nick
15
15
  raise ConfigError, 'no servers' unless confdsl.servers
16
-
16
+
17
17
  conf = Config.new
18
-
18
+
19
19
  conf.nick = confdsl.nick
20
20
  conf.ident = confdsl.ident || confdsl.nick
21
21
  conf.realname = confdsl.realname || confdsl.nick
22
22
  conf.servers = confdsl.servers
23
-
23
+
24
24
  conf
25
25
  end
26
-
26
+
27
27
  def server(id, &block)
28
28
  @servers ||= {}
29
- @servers[id] = Server.new Server::ConfigDSL.run(&block)
29
+ @servers[id] = ServerConfigDSL.run(&block)
30
30
  @servers[id]
31
31
  end
32
32
  end
33
+
34
+ ServerConfig = Struct.new(:address, :port, :nick, :ident, :realname, :ssl)
35
+
36
+ class ServerConfigDSL
37
+ dsl_accessor :address, :port, :nick, :ident, :realname
38
+ bool_dsl_accessor :ssl
39
+
40
+ def self.run(&block)
41
+ confdsl = self.new
42
+ block.arity < 1 ? confdsl.instance_eval(&block) : block.call(confdsl)
43
+
44
+ raise ConfigError, 'no address' unless confdsl.address
45
+
46
+ conf = ServerConfig.new
47
+
48
+ conf.address = confdsl.address
49
+ # If not supplied, the port defaults to 6667, or 6697 if ssl is used
50
+ conf.port = confdsl.port || (confdsl.ssl? ? 6697 : 6667)
51
+ conf.ssl = confdsl.ssl?
52
+ conf.nick = confdsl.nick
53
+ conf.ident = confdsl.ident
54
+ conf.realname = confdsl.realname
55
+
56
+ conf
57
+ end
58
+ end
33
59
  end
34
60
 
@@ -1,51 +1,25 @@
1
- module IRC
1
+ class IRC
2
2
  class Connection < EventMachine::Connection
3
3
  include EventMachine::Protocols::LineText2
4
-
5
- def initialize(server_id)
6
- @server = server_id
7
- end
8
-
9
- def config
10
- IRC[@server].config
11
- end
12
-
13
- def command(*cmd)
14
- send_data(cmd.join(' ') + "\r\n")
15
- end
16
-
17
- def handle_event(event)
18
- if IRC[@server].handlers[:all]
19
- IRC[@server].handlers[:all].call(event)
20
- elsif IRC.handlers[:all]
21
- IRC.handlers[:all].call(event)
22
- end
23
-
24
- if IRC[@server].handlers[event.command]
25
- IRC[@server].handlers[event.command].call(event)
26
- elsif IRC.handlers[event.command]
27
- IRC.handlers[event.command].call(event)
28
- end
4
+
5
+ def initialize(server)
6
+ @server = server
29
7
  end
30
-
8
+
31
9
  ## EventMachine callbacks
32
10
  def post_init
33
- command "USER #{config.ident || IRC.config.ident} * * :#{config.realname || IRC.config.realname}"
34
- command "NICK #{config.nick || IRC.config.nick}"
11
+ send_data("USER #{@server.ident || @server.irc.ident} * * #{@server.realname || @server.irc.realname}\r\n")
12
+ send_data("NICK #{@server.nick || @server.irc.nick}\r\n")
13
+ rescue => e
14
+ p e
35
15
  end
36
-
16
+
37
17
  def receive_line(line)
38
- parsed_line = Parser.parse(line)
39
- event = Event.new(@server, parsed_line[:prefix], parsed_line[:command].downcase.to_sym, parsed_line[:params])
40
-
41
- handle_event(event)
18
+ @server.receive_line(line)
42
19
  end
43
-
20
+
44
21
  def unbind
45
- EM.add_timer(3) do
46
- reconnect(config.address, config.port)
47
- post_init
48
- end
22
+ @server.unbind
49
23
  end
50
24
  end
51
25
  end
data/lib/on_irc/event.rb CHANGED
@@ -1,4 +1,4 @@
1
- module IRC
1
+ class IRC
2
2
  class Event
3
3
  attr_accessor :server, :prefix, :command, :params
4
4
 
data/lib/on_irc/parser.rb CHANGED
@@ -1,4 +1,4 @@
1
- module IRC
1
+ class IRC
2
2
  module Parser
3
3
  def self.parse(line)
4
4
  prefix = ''
data/lib/on_irc/server.rb CHANGED
@@ -1,40 +1,52 @@
1
- module IRC
1
+ class IRC
2
2
  class Server
3
- attr_accessor :config, :connection, :handlers
3
+ attr_accessor :config, :connection, :handlers, :name, :irc
4
4
  config_accessor :address, :port, :nick, :ident, :realname, :ssl
5
-
6
- def initialize(config)
5
+
6
+ def initialize(irc, name, config)
7
+ @irc = irc
8
+ @name = name
7
9
  @config = config
8
10
  @handlers = {}
9
11
  end
10
-
12
+
13
+ def send_cmd(cmd, *args)
14
+ # prepend last arg with : only if it exists. it's really ugly
15
+ args[-1] = ":#{args[-1]}" if args[-1]
16
+ connection.send_data(cmd.to_s.upcase + ' ' + args.join(' ') + "\r\n")
17
+ end
18
+
11
19
  def on(event, &block)
12
20
  @handlers[event.to_s.downcase.to_sym] = Callback.new(block)
13
21
  end
14
-
15
- Config = Struct.new(:address, :port, :nick, :ident, :realname, :ssl)
16
-
17
- class ConfigDSL
18
- dsl_accessor :address, :port, :nick, :ident, :realname
19
- bool_dsl_accessor :ssl
20
-
21
- def self.run(&block)
22
- confdsl = self.new
23
- block.arity < 1 ? confdsl.instance_eval(&block) : block.call(confdsl)
24
-
25
- raise ConfigError, 'no address' unless confdsl.address
26
-
27
- conf = Config.new
28
-
29
- conf.address = confdsl.address
30
- # If not supplied, the port defaults to 6667, or 6697 if ssl is used
31
- conf.port = confdsl.port || (confdsl.ssl? ? 6697 : 6667)
32
- conf.ssl = confdsl.ssl?
33
- conf.nick = confdsl.nick
34
- conf.ident = confdsl.ident
35
- conf.realname = confdsl.realname
36
-
37
- conf
22
+
23
+ def handle_event(event)
24
+ if @handlers[:all]
25
+ @handlers[:all].call(@irc, event)
26
+ elsif @irc.handlers[:all]
27
+ @irc.handlers[:all].call(@irc, event)
28
+ end
29
+
30
+ if @handlers[event.command]
31
+ @handlers[event.command].call(@irc, event)
32
+ elsif @irc.handlers[event.command]
33
+ @irc.handlers[event.command].call(@irc, event)
34
+ end
35
+ end
36
+
37
+ # Eventmachine callbacks
38
+ def receive_line(line)
39
+ parsed_line = Parser.parse(line)
40
+ event = Event.new(self, parsed_line[:prefix],
41
+ parsed_line[:command].downcase.to_sym,
42
+ parsed_line[:params])
43
+ handle_event(event)
44
+ end
45
+
46
+ def unbind
47
+ EM.add_timer(3) do
48
+ connection.reconnect(config.address, config.port)
49
+ connection.post_init
38
50
  end
39
51
  end
40
52
  end
data/on_irc.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{on_irc}
8
- s.version = "2.0.0"
8
+ s.version = "2.0.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Scott Olson"]
12
- s.date = %q{2009-12-13}
12
+ s.date = %q{2009-12-27}
13
13
  s.description = %q{An event driven IRC library with an easy to use DSL}
14
14
  s.email = %q{scott@scott-olson.org}
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: on_irc
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Olson
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-13 00:00:00 -06:00
12
+ date: 2009-12-27 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency