on_irc 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +3 -0
- data/LICENSE +27 -0
- data/Rakefile +22 -0
- data/VERSION +1 -0
- data/examples/bot.rb +49 -0
- data/examples/regex_bot.rb +98 -0
- data/examples/relay.rb +47 -0
- data/lib/on_irc.rb +47 -0
- data/lib/on_irc/callback.rb +66 -0
- data/lib/on_irc/config.rb +34 -0
- data/lib/on_irc/config_accessor.rb +17 -0
- data/lib/on_irc/connection.rb +52 -0
- data/lib/on_irc/dsl_accessor.rb +31 -0
- data/lib/on_irc/event.rb +30 -0
- data/lib/on_irc/parser.rb +33 -0
- data/lib/on_irc/server.rb +41 -0
- data/on_irc.gemspec +61 -0
- metadata +82 -0
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
BSD License
|
2
|
+
|
3
|
+
Copyright (c) 2008 Scott Olson
|
4
|
+
All rights reserved.
|
5
|
+
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
7
|
+
modification, are permitted provided that the following conditions
|
8
|
+
are met:
|
9
|
+
1. Redistributions of source code must retain the above copyright
|
10
|
+
notice, this list of conditions and the following disclaimer.
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright
|
12
|
+
notice, this list of conditions and the following disclaimer in the
|
13
|
+
documentation and/or other materials provided with the distribution.
|
14
|
+
3. The name of the author may not be used to endorse or promote products
|
15
|
+
derived from this software without specific prior written permission.
|
16
|
+
|
17
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
18
|
+
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
19
|
+
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
20
|
+
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
21
|
+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
22
|
+
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
23
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
24
|
+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
25
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
26
|
+
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
27
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift('lib')
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'jeweler'
|
7
|
+
Jeweler::Tasks.new do |gemspec|
|
8
|
+
gemspec.name = "on_irc"
|
9
|
+
gemspec.summary = "An event driven IRC library with an easy to use DSL"
|
10
|
+
gemspec.description = "An event driven IRC library with an easy to use DSL"
|
11
|
+
gemspec.email = "scott@scott-olson.org"
|
12
|
+
gemspec.homepage = "http://github.com/tsion/on_irc"
|
13
|
+
gemspec.authors = ["Scott Olson"]
|
14
|
+
|
15
|
+
gemspec.add_dependency "eventmachine"
|
16
|
+
end
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler not available. Install it with: sudo gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
Jeweler::GemcutterTasks.new
|
22
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.0.0
|
data/examples/bot.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'on_irc')
|
3
|
+
|
4
|
+
IRC.configure do
|
5
|
+
nick 'on_irc'
|
6
|
+
ident 'on_irc'
|
7
|
+
realname 'on_irc Ruby IRC library'
|
8
|
+
|
9
|
+
server :eighthbit do
|
10
|
+
address 'irc.eighthbit.net'
|
11
|
+
end
|
12
|
+
|
13
|
+
server :freenode do
|
14
|
+
address 'irc.freenode.org'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
|
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')
|
25
|
+
end
|
26
|
+
|
27
|
+
IRC.on :privmsg do |e|
|
28
|
+
case e.params[1]
|
29
|
+
when '!ping'
|
30
|
+
IRC.send(e.server, :privmsg, e.params[0], e.prefix.split('!').first + ': pong')
|
31
|
+
when /^!echo (.*)/
|
32
|
+
s = $1
|
33
|
+
IRC.send(e.server, :privmsg, e.params[0], e.prefix.split('!').first + ': ' + s)
|
34
|
+
when /^!join (.*)/
|
35
|
+
IRC.send(e.server, :join, $1)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
IRC.on :ping do |e|
|
40
|
+
IRC.send(e.server, :pong, e.params[0])
|
41
|
+
end
|
42
|
+
|
43
|
+
IRC.on :all do |e|
|
44
|
+
prefix = "(#{e.prefix}) " unless e.prefix.empty?
|
45
|
+
puts "#{e.server}: #{prefix}#{e.command} #{e.params.inspect}"
|
46
|
+
end
|
47
|
+
|
48
|
+
IRC.connect
|
49
|
+
|
@@ -0,0 +1,98 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'on_irc')
|
3
|
+
|
4
|
+
MAX_BANGS = 3
|
5
|
+
CH_USER_MEMORY = {}
|
6
|
+
CHANNEL_MEMORY = {}
|
7
|
+
|
8
|
+
IRC.configure do
|
9
|
+
nick 'reggie'
|
10
|
+
ident 'reggie'
|
11
|
+
realname 'uses on_irc Ruby IRC library'
|
12
|
+
|
13
|
+
server :eighthbit do
|
14
|
+
address 'irc.eighthbit.net'
|
15
|
+
end
|
16
|
+
|
17
|
+
# server :freenode do
|
18
|
+
# address 'irc.freenode.org'
|
19
|
+
# end
|
20
|
+
end
|
21
|
+
|
22
|
+
IRC[:eighthbit].on :'001' do
|
23
|
+
join '#programming'
|
24
|
+
join '#offtopic'
|
25
|
+
end
|
26
|
+
|
27
|
+
IRC.on :privmsg do
|
28
|
+
next unless params[0][0,1] == '#' # make sure regex replace only happens in channels
|
29
|
+
channel = params[0]
|
30
|
+
nick = prefix.split('!').first
|
31
|
+
message = params[1]
|
32
|
+
|
33
|
+
CHANNEL_MEMORY[channel] ||= []
|
34
|
+
CH_USER_MEMORY[channel] ||= {}
|
35
|
+
CH_USER_MEMORY[channel][nick] ||= []
|
36
|
+
|
37
|
+
if params[1] =~ %r"^(!*)s/((?:[^\\/]|\\.)*)/((?:[^\\/]|\\.)*)/(?:(\w*))?"
|
38
|
+
bangs = $1
|
39
|
+
match = $2
|
40
|
+
replace = $3
|
41
|
+
flags = $4
|
42
|
+
|
43
|
+
if bangs.length > MAX_BANGS
|
44
|
+
privmsg params[0], "#{nick}: I only support up to #{MAX_BANGS} !'s."
|
45
|
+
privmsg params[0], 'in bed' if rand(1000) == 42
|
46
|
+
next
|
47
|
+
end
|
48
|
+
|
49
|
+
begin
|
50
|
+
match = Regexp.new match
|
51
|
+
rescue RegexpError => err
|
52
|
+
privmsg params[0], "RegexpError: #{err.message}"
|
53
|
+
next
|
54
|
+
end
|
55
|
+
|
56
|
+
target = if bangs.length == 0
|
57
|
+
CHANNEL_MEMORY[channel][1] || ''
|
58
|
+
else
|
59
|
+
CH_USER_MEMORY[channel][nick][-bangs.length] || ''
|
60
|
+
end
|
61
|
+
|
62
|
+
if flags.chars.include? 'g'
|
63
|
+
answer = target.gsub(match, replace)
|
64
|
+
else
|
65
|
+
answer = target.sub(match, replace)
|
66
|
+
end
|
67
|
+
|
68
|
+
if bangs.length > 0 || CHANNEL_MEMORY[channel][0] == nick
|
69
|
+
privmsg(params[0], nick + ' meant: ' + answer)
|
70
|
+
else
|
71
|
+
privmsg(params[0], nick + ' thinks ' + CHANNEL_MEMORY[channel][0] + ' meant: ' + answer)
|
72
|
+
end
|
73
|
+
|
74
|
+
else
|
75
|
+
if message =~ /^\x01(\S+) (.*)\x01$/
|
76
|
+
next unless $1 == 'ACTION'
|
77
|
+
|
78
|
+
message = "* #{nick} #{$2}"
|
79
|
+
end
|
80
|
+
|
81
|
+
CH_USER_MEMORY[channel][nick] << message
|
82
|
+
CH_USER_MEMORY[channel][nick].unshift if CH_USER_MEMORY[channel][nick].length > MAX_BANGS
|
83
|
+
|
84
|
+
CHANNEL_MEMORY[channel] = [nick, message]
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
IRC.on :ping do
|
89
|
+
pong params[0]
|
90
|
+
end
|
91
|
+
|
92
|
+
IRC.on :all do
|
93
|
+
prefix_str = "(#{prefix}) " unless prefix.empty?
|
94
|
+
puts "#{server}: #{prefix_str}#{command} #{params.inspect}"
|
95
|
+
end
|
96
|
+
|
97
|
+
IRC.connect
|
98
|
+
|
data/examples/relay.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'on_irc')
|
3
|
+
|
4
|
+
IRC.configure do
|
5
|
+
nick 'on_irc-relay'
|
6
|
+
ident 'on_irc'
|
7
|
+
realname 'on_irc Ruby IRC library - relay example'
|
8
|
+
|
9
|
+
server :eighthbit do
|
10
|
+
address 'irc.eighthbit.net'
|
11
|
+
end
|
12
|
+
|
13
|
+
server :freenode do
|
14
|
+
address 'irc.freenode.org'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
|
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')
|
25
|
+
end
|
26
|
+
|
27
|
+
IRC.on :privmsg do |e|
|
28
|
+
case e.params[1]
|
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
|
32
|
+
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
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
IRC.on :ping do |e|
|
39
|
+
IRC.send(e.server, :pong, e.params[0])
|
40
|
+
end
|
41
|
+
|
42
|
+
IRC.on :all do |e|
|
43
|
+
prefix = "(#{e.prefix}) " unless e.prefix.empty?
|
44
|
+
puts "#{e.server}: #{prefix}#{e.command} #{e.params.inspect}"
|
45
|
+
end
|
46
|
+
|
47
|
+
IRC.connect
|
data/lib/on_irc.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
%w[rubygems eventmachine socket strscan].each { |lib| require lib }
|
2
|
+
%w[event parser dsl_accessor config_accessor server config connection callback].each do |lib|
|
3
|
+
require File.join(File.dirname(__FILE__), 'on_irc', lib)
|
4
|
+
end
|
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
|
15
|
+
|
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)
|
28
|
+
end
|
29
|
+
|
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
|
35
|
+
end
|
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
|
+
end
|
46
|
+
end
|
47
|
+
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module IRC
|
2
|
+
class Callback
|
3
|
+
def initialize(block)
|
4
|
+
@block = block
|
5
|
+
end
|
6
|
+
|
7
|
+
def call(event)
|
8
|
+
CallbackDSL.run(event, @block)
|
9
|
+
end
|
10
|
+
|
11
|
+
class CallbackDSL
|
12
|
+
def self.run(event, block)
|
13
|
+
callbackdsl = self.new(event)
|
14
|
+
block.arity < 1 ? callbackdsl.instance_eval(&block) : block.call(callbackdsl)
|
15
|
+
end
|
16
|
+
|
17
|
+
def initialize(event)
|
18
|
+
@event = event
|
19
|
+
end
|
20
|
+
|
21
|
+
# @event accessors
|
22
|
+
def prefix
|
23
|
+
@event.prefix
|
24
|
+
end
|
25
|
+
|
26
|
+
def command
|
27
|
+
@event.command
|
28
|
+
end
|
29
|
+
|
30
|
+
def server
|
31
|
+
@event.server
|
32
|
+
end
|
33
|
+
|
34
|
+
def params
|
35
|
+
@event.params
|
36
|
+
end
|
37
|
+
|
38
|
+
# 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
|
45
|
+
end
|
46
|
+
|
47
|
+
alias raw send
|
48
|
+
|
49
|
+
def privmsg(target, message)
|
50
|
+
send(:privmsg, target, message)
|
51
|
+
end
|
52
|
+
|
53
|
+
alias msg privmsg
|
54
|
+
|
55
|
+
def join(channel)
|
56
|
+
send(:join, channel)
|
57
|
+
end
|
58
|
+
|
59
|
+
def pong(msg)
|
60
|
+
send(:pong, msg)
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module IRC
|
2
|
+
class ConfigError < StandardError; end
|
3
|
+
|
4
|
+
Config = Struct.new(:nick, :ident, :realname, :servers)
|
5
|
+
|
6
|
+
class ConfigDSL
|
7
|
+
dsl_accessor :nick, :ident, :realname
|
8
|
+
attr_accessor :servers
|
9
|
+
|
10
|
+
def self.run(&block)
|
11
|
+
confdsl = self.new
|
12
|
+
block.arity < 1 ? confdsl.instance_eval(&block) : block.call(confdsl)
|
13
|
+
|
14
|
+
raise ConfigError, 'no nick' unless confdsl.nick
|
15
|
+
raise ConfigError, 'no servers' unless confdsl.servers
|
16
|
+
|
17
|
+
conf = Config.new
|
18
|
+
|
19
|
+
conf.nick = confdsl.nick
|
20
|
+
conf.ident = confdsl.ident || confdsl.nick
|
21
|
+
conf.realname = confdsl.realname || confdsl.nick
|
22
|
+
conf.servers = confdsl.servers
|
23
|
+
|
24
|
+
conf
|
25
|
+
end
|
26
|
+
|
27
|
+
def server(id, &block)
|
28
|
+
@servers ||= {}
|
29
|
+
@servers[id] = Server.new Server::ConfigDSL.run(&block)
|
30
|
+
@servers[id]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module IRC
|
2
|
+
class Connection < EventMachine::Connection
|
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
|
29
|
+
end
|
30
|
+
|
31
|
+
## EventMachine callbacks
|
32
|
+
def post_init
|
33
|
+
command "USER #{config.ident || IRC.config.ident} * * :#{config.realname || IRC.config.realname}"
|
34
|
+
command "NICK #{config.nick || IRC.config.nick}"
|
35
|
+
end
|
36
|
+
|
37
|
+
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)
|
42
|
+
end
|
43
|
+
|
44
|
+
def unbind
|
45
|
+
EM.add_timer(3) do
|
46
|
+
reconnect(config.address, config.port)
|
47
|
+
post_init
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class Module
|
2
|
+
# taken from http://www.artima.com/rubycs/articles/ruby_as_dsl3.html
|
3
|
+
# used for the IRC.new config DSL
|
4
|
+
def dsl_accessor(*symbols)
|
5
|
+
symbols.each do |sym|
|
6
|
+
class_eval %{
|
7
|
+
def #{sym}(*val)
|
8
|
+
if val.empty?
|
9
|
+
@#{sym}
|
10
|
+
else
|
11
|
+
@#{sym} = val.size == 1 ? val[0] : val
|
12
|
+
end
|
13
|
+
end
|
14
|
+
}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def bool_dsl_accessor(*symbols)
|
19
|
+
symbols.each do |sym|
|
20
|
+
class_eval %{
|
21
|
+
def #{sym}(val = true)
|
22
|
+
@#{sym} = !!val
|
23
|
+
end
|
24
|
+
|
25
|
+
def #{sym}?
|
26
|
+
!!@#{sym}
|
27
|
+
end
|
28
|
+
}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/on_irc/event.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
module IRC
|
2
|
+
class Event
|
3
|
+
attr_accessor :server, :prefix, :command, :params
|
4
|
+
|
5
|
+
def initialize(server, prefix, command, params)
|
6
|
+
@server = server
|
7
|
+
@prefix = prefix
|
8
|
+
@command = command
|
9
|
+
@params = params
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
module Kernel
|
18
|
+
# Like instance_eval but allows parameters to be passed.
|
19
|
+
def instance_exec(*args, &block)
|
20
|
+
mname = "__instance_exec_#{Thread.current.object_id.abs}_#{object_id.abs}"
|
21
|
+
Object.class_eval{ define_method(mname, &block) }
|
22
|
+
begin
|
23
|
+
ret = send(mname, *args)
|
24
|
+
ensure
|
25
|
+
Object.class_eval{ undef_method(mname) } rescue nil
|
26
|
+
end
|
27
|
+
ret
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module IRC
|
2
|
+
module Parser
|
3
|
+
def self.parse(line)
|
4
|
+
prefix = ''
|
5
|
+
command = ''
|
6
|
+
params = []
|
7
|
+
msg = StringScanner.new(line)
|
8
|
+
|
9
|
+
if msg.peek(1) == ':'
|
10
|
+
msg.pos += 1
|
11
|
+
prefix = msg.scan /\S+/
|
12
|
+
msg.skip /\s+/
|
13
|
+
end
|
14
|
+
|
15
|
+
command = msg.scan /\S+/
|
16
|
+
|
17
|
+
until msg.eos?
|
18
|
+
msg.skip /\s+/
|
19
|
+
|
20
|
+
if msg.peek(1) == ':'
|
21
|
+
msg.pos += 1
|
22
|
+
params << msg.rest
|
23
|
+
msg.terminate
|
24
|
+
else
|
25
|
+
params << msg.scan(/\S+/)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
{:prefix => prefix, :command => command, :params => params}
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module IRC
|
2
|
+
class Server
|
3
|
+
attr_accessor :config, :connection, :handlers
|
4
|
+
config_accessor :address, :port, :nick, :ident, :realname, :ssl
|
5
|
+
|
6
|
+
def initialize(config)
|
7
|
+
@config = config
|
8
|
+
@handlers = {}
|
9
|
+
end
|
10
|
+
|
11
|
+
def on(event, &block)
|
12
|
+
@handlers[event.to_s.downcase.to_sym] = Callback.new(block)
|
13
|
+
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
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/on_irc.gemspec
ADDED
@@ -0,0 +1,61 @@
|
|
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{on_irc}
|
8
|
+
s.version = "2.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Scott Olson"]
|
12
|
+
s.date = %q{2009-12-13}
|
13
|
+
s.description = %q{An event driven IRC library with an easy to use DSL}
|
14
|
+
s.email = %q{scott@scott-olson.org}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".gitignore",
|
20
|
+
"LICENSE",
|
21
|
+
"Rakefile",
|
22
|
+
"VERSION",
|
23
|
+
"examples/bot.rb",
|
24
|
+
"examples/regex_bot.rb",
|
25
|
+
"examples/relay.rb",
|
26
|
+
"lib/on_irc.rb",
|
27
|
+
"lib/on_irc/callback.rb",
|
28
|
+
"lib/on_irc/config.rb",
|
29
|
+
"lib/on_irc/config_accessor.rb",
|
30
|
+
"lib/on_irc/connection.rb",
|
31
|
+
"lib/on_irc/dsl_accessor.rb",
|
32
|
+
"lib/on_irc/event.rb",
|
33
|
+
"lib/on_irc/parser.rb",
|
34
|
+
"lib/on_irc/server.rb",
|
35
|
+
"on_irc.gemspec"
|
36
|
+
]
|
37
|
+
s.homepage = %q{http://github.com/tsion/on_irc}
|
38
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
39
|
+
s.require_paths = ["lib"]
|
40
|
+
s.rubygems_version = %q{1.3.5}
|
41
|
+
s.summary = %q{An event driven IRC library with an easy to use DSL}
|
42
|
+
s.test_files = [
|
43
|
+
"examples/regex_bot.rb",
|
44
|
+
"examples/relay.rb",
|
45
|
+
"examples/bot.rb"
|
46
|
+
]
|
47
|
+
|
48
|
+
if s.respond_to? :specification_version then
|
49
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
50
|
+
s.specification_version = 3
|
51
|
+
|
52
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
53
|
+
s.add_runtime_dependency(%q<eventmachine>, [">= 0"])
|
54
|
+
else
|
55
|
+
s.add_dependency(%q<eventmachine>, [">= 0"])
|
56
|
+
end
|
57
|
+
else
|
58
|
+
s.add_dependency(%q<eventmachine>, [">= 0"])
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: on_irc
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Scott Olson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-13 00:00:00 -06:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: eventmachine
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: An event driven IRC library with an easy to use DSL
|
26
|
+
email: scott@scott-olson.org
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
files:
|
34
|
+
- .gitignore
|
35
|
+
- LICENSE
|
36
|
+
- Rakefile
|
37
|
+
- VERSION
|
38
|
+
- examples/bot.rb
|
39
|
+
- examples/regex_bot.rb
|
40
|
+
- examples/relay.rb
|
41
|
+
- lib/on_irc.rb
|
42
|
+
- lib/on_irc/callback.rb
|
43
|
+
- lib/on_irc/config.rb
|
44
|
+
- lib/on_irc/config_accessor.rb
|
45
|
+
- lib/on_irc/connection.rb
|
46
|
+
- lib/on_irc/dsl_accessor.rb
|
47
|
+
- lib/on_irc/event.rb
|
48
|
+
- lib/on_irc/parser.rb
|
49
|
+
- lib/on_irc/server.rb
|
50
|
+
- on_irc.gemspec
|
51
|
+
has_rdoc: true
|
52
|
+
homepage: http://github.com/tsion/on_irc
|
53
|
+
licenses: []
|
54
|
+
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options:
|
57
|
+
- --charset=UTF-8
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: "0"
|
71
|
+
version:
|
72
|
+
requirements: []
|
73
|
+
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 1.3.5
|
76
|
+
signing_key:
|
77
|
+
specification_version: 3
|
78
|
+
summary: An event driven IRC library with an easy to use DSL
|
79
|
+
test_files:
|
80
|
+
- examples/regex_bot.rb
|
81
|
+
- examples/relay.rb
|
82
|
+
- examples/bot.rb
|