on_irc 2.0.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -11,6 +11,7 @@ begin
11
11
  gemspec.email = "scott@scott-olson.org"
12
12
  gemspec.homepage = "http://github.com/tsion/on_irc"
13
13
  gemspec.authors = ["Scott Olson"]
14
+ gemspec.files = FileList["[A-Z]*", "lib/**/*"]
14
15
 
15
16
  gemspec.add_dependency "eventmachine"
16
17
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.0.1
1
+ 2.1.0
data/examples/bot.rb CHANGED
@@ -5,7 +5,7 @@ bot = IRC.new do
5
5
  nick 'on_irc'
6
6
  ident 'on_irc'
7
7
  realname 'on_irc Ruby IRC library'
8
-
8
+
9
9
  server :eighthbit do
10
10
  address 'irc.eighthbit.net'
11
11
  end
@@ -19,9 +19,9 @@ end
19
19
  bot.on :privmsg do
20
20
  case params[1]
21
21
  when '!ping'
22
- msg(params[0], prefix.split('!').first + ': pong')
22
+ respond "#{sender.nick}: pong"
23
23
  when /^!echo (.*)/
24
- msg(params[0], prefix.split('!').first + ': ' + $1)
24
+ respond "#{sender.nick}: #{$1}"
25
25
  when /^!join (.*)/
26
26
  join $1
27
27
  end
@@ -32,7 +32,7 @@ bot.on :ping do
32
32
  end
33
33
 
34
34
  bot.on :all do
35
- p = "(#{prefix}) " unless prefix.empty?
35
+ p = "(#{sender}) " unless sender.empty?
36
36
  puts "#{server.name}: #{p}#{command} #{params.inspect}"
37
37
  end
38
38
 
@@ -27,12 +27,11 @@ end
27
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
- nick = prefix.split('!').first
31
30
  message = params[1]
32
31
 
33
32
  CHANNEL_MEMORY[channel] ||= []
34
33
  CH_USER_MEMORY[channel] ||= {}
35
- CH_USER_MEMORY[channel][nick] ||= []
34
+ CH_USER_MEMORY[channel][sender.nick] ||= []
36
35
 
37
36
  if params[1] =~ %r"^(!*)s/((?:[^\\/]|\\.)*)/((?:[^\\/]|\\.)*)/(?:(\w*))?"
38
37
  bangs = $1
@@ -41,22 +40,22 @@ bot.on :privmsg do
41
40
  flags = $4
42
41
 
43
42
  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
43
+ respond "#{sender.nick}: I only support up to #{MAX_BANGS} !'s."
44
+ respond 'in bed' if rand(1000) == 42
46
45
  next
47
46
  end
48
47
 
49
48
  begin
50
49
  match = Regexp.new match
51
50
  rescue RegexpError => err
52
- privmsg params[0], "RegexpError: #{err.message}"
51
+ respond "RegexpError: #{err.message}"
53
52
  next
54
53
  end
55
54
 
56
55
  target = if bangs.length == 0
57
56
  CHANNEL_MEMORY[channel][1] || ''
58
57
  else
59
- CH_USER_MEMORY[channel][nick][-bangs.length] || ''
58
+ CH_USER_MEMORY[channel][sender.nick][-bangs.length] || ''
60
59
  end
61
60
 
62
61
  if flags.chars.include? 'g'
@@ -65,23 +64,23 @@ bot.on :privmsg do
65
64
  answer = target.sub(match, replace)
66
65
  end
67
66
 
68
- if bangs.length > 0 || CHANNEL_MEMORY[channel][0] == nick
69
- privmsg(params[0], nick + ' meant: ' + answer)
67
+ if bangs.length > 0 || CHANNEL_MEMORY[channel][0] == sender.nick
68
+ respond "#{sender.nick} meant: #{answer}"
70
69
  else
71
- privmsg(params[0], nick + ' thinks ' + CHANNEL_MEMORY[channel][0] + ' meant: ' + answer)
70
+ respond "#{sender.nick} thinks #{CHANNEL_MEMORY[channel][0]} meant: #{answer}"
72
71
  end
73
72
 
74
73
  else
75
74
  if message =~ /^\x01(\S+) (.*)\x01$/
76
75
  next unless $1 == 'ACTION'
77
76
 
78
- message = "* #{nick} #{$2}"
77
+ message = "* #{sender.nick} #{$2}"
79
78
  end
80
79
 
81
- CH_USER_MEMORY[channel][nick] << message
82
- CH_USER_MEMORY[channel][nick].unshift if CH_USER_MEMORY[channel][nick].length > MAX_BANGS
80
+ CH_USER_MEMORY[channel][sender.nick] << message
81
+ CH_USER_MEMORY[channel][sender.nick].unshift if CH_USER_MEMORY[channel][sender.nick].length > MAX_BANGS
83
82
 
84
- CHANNEL_MEMORY[channel] = [nick, message]
83
+ CHANNEL_MEMORY[channel] = [sender.nick, message]
85
84
  end
86
85
  end
87
86
 
@@ -90,9 +89,8 @@ bot.on :ping do
90
89
  end
91
90
 
92
91
  bot.on :all do
93
- prefix_str = "(#{prefix}) " unless prefix.empty?
94
- puts "#{server}: #{prefix_str}#{command} #{params.inspect}"
92
+ p = "(#{sender}) " unless sender.empty?
93
+ puts "#{server.name}: #{p}#{command} #{params.inspect}"
95
94
  end
96
95
 
97
96
  bot.connect
98
-
data/examples/relay.rb CHANGED
@@ -5,11 +5,11 @@ 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'
8
-
8
+
9
9
  server :eighthbit do
10
10
  address 'irc.eighthbit.net'
11
11
  end
12
-
12
+
13
13
  server :freenode do
14
14
  address 'irc.freenode.org'
15
15
  end
@@ -27,9 +27,9 @@ end
27
27
  bot.on :privmsg do
28
28
  case params[1]
29
29
  when /^fn> (.*)/
30
- bot[:freenode].send_cmd(:privmsg, '#botters', "<8b:#{prefix.split('!').first}> #{$1}") if params[0] == '#offtopic' && server.name == :eighthbit
30
+ bot[:freenode].msg('#botters', "<8b:#{sender.nick}> #{$1}") if params[0] == '#offtopic' && server.name == :eighthbit
31
31
  when /^8b> (.*)/
32
- bot[:eighthbit].send_cmd(:privmsg, '#offtopic', "<fn:#{prefix.split('!').first}> #{$1}") if params[0] == '#botters' && server.name == :freenode
32
+ bot[:eighthbit].msg('#offtopic', "<fn:#{sender.nick}> #{$1}") if params[0] == '#botters' && server.name == :freenode
33
33
  end
34
34
  end
35
35
 
@@ -38,7 +38,7 @@ bot.on :ping do
38
38
  end
39
39
 
40
40
  bot.on :all do
41
- p = "(#{prefix}) " unless prefix.empty?
41
+ p = "(#{sender}) " unless sender.empty?
42
42
  puts "#{server.name}: #{p}#{command} #{params.inspect}"
43
43
  end
44
44
 
data/lib/on_irc.rb CHANGED
@@ -1,5 +1,5 @@
1
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|
2
+ %w[event commands parser dsl_accessor config_accessor server config connection sender callback].each do |lib|
3
3
  require File.join(File.dirname(__FILE__), 'on_irc', lib)
4
4
  end
5
5
 
@@ -20,8 +20,8 @@ class IRC
20
20
  end
21
21
 
22
22
  # @event accessors
23
- def prefix
24
- @event.prefix
23
+ def sender
24
+ @event.sender
25
25
  end
26
26
 
27
27
  def command
@@ -37,24 +37,19 @@ class IRC
37
37
  end
38
38
 
39
39
  # commands
40
+ include Commands
41
+
40
42
  def send_cmd(cmd, *args)
41
43
  @event.server.send_cmd(cmd, *args)
42
44
  end
43
45
 
44
- def privmsg(target, message)
45
- send_cmd(:privmsg, target, message)
46
- end
47
-
48
- alias msg privmsg
49
-
50
- def join(channel)
51
- send_cmd(:join, channel)
46
+ def respond(message)
47
+ if params[0].start_with? '#'
48
+ privmsg(params[0], message)
49
+ else
50
+ privmsg(sender.nick, message)
51
+ end
52
52
  end
53
-
54
- def pong(msg)
55
- send_cmd(:pong, msg)
56
- end
57
-
58
53
  end
59
54
  end
60
55
  end
@@ -0,0 +1,25 @@
1
+ class IRC
2
+ module Commands
3
+ def privmsg(target, message)
4
+ send_cmd(:privmsg, target, message)
5
+ end
6
+
7
+ alias msg privmsg
8
+
9
+ def notice(target, message)
10
+ send_cmd(:notice, target, message)
11
+ end
12
+
13
+ def join(channel)
14
+ send_cmd(:join, channel)
15
+ end
16
+
17
+ def part(channel, message=nil)
18
+ send_cmd(:part, channel, message)
19
+ end
20
+
21
+ def pong(msg)
22
+ send_cmd(:pong, msg)
23
+ end
24
+ end
25
+ end
@@ -15,7 +15,7 @@ class IRC
15
15
  end
16
16
 
17
17
  def receive_line(line)
18
- @server.receive_line(line)
18
+ @server.receive_line(line.force_encoding('utf-8'))
19
19
  end
20
20
 
21
21
  def unbind
data/lib/on_irc/event.rb CHANGED
@@ -1,15 +1,15 @@
1
1
  class IRC
2
2
  class Event
3
- attr_accessor :server, :prefix, :command, :params
4
-
3
+ attr_accessor :server, :sender, :command, :params
4
+
5
5
  def initialize(server, prefix, command, params)
6
6
  @server = server
7
- @prefix = prefix
7
+ @sender = Sender.new(prefix)
8
8
  @command = command
9
9
  @params = params
10
10
  end
11
-
12
-
11
+
12
+
13
13
  end
14
14
  end
15
15
 
@@ -0,0 +1,31 @@
1
+ class IRC
2
+ class Sender
3
+ attr_accessor :nick, :user, :host
4
+
5
+ def initialize(string)
6
+ if string =~ /^([^!]+)!([^@]+)@(.+)$/
7
+ @nick, @user, @host = $1, $2, $3
8
+ @server = false
9
+ else
10
+ @host = string
11
+ @server = true
12
+ end
13
+ end
14
+
15
+ def server?
16
+ @server
17
+ end
18
+
19
+ def user?
20
+ !@server
21
+ end
22
+
23
+ def to_s
24
+ @server ? @host : @nick + '!' + @user + '@' + @host
25
+ end
26
+
27
+ def empty?
28
+ to_s.empty?
29
+ end
30
+ end
31
+ end
data/lib/on_irc/server.rb CHANGED
@@ -11,11 +11,16 @@ class IRC
11
11
  end
12
12
 
13
13
  def send_cmd(cmd, *args)
14
+ # remove nil entries
15
+ args.compact!
14
16
  # prepend last arg with : only if it exists. it's really ugly
15
17
  args[-1] = ":#{args[-1]}" if args[-1]
16
18
  connection.send_data(cmd.to_s.upcase + ' ' + args.join(' ') + "\r\n")
17
19
  end
18
20
 
21
+ # basic IRC commands
22
+ include Commands
23
+
19
24
  def on(event, &block)
20
25
  @handlers[event.to_s.downcase.to_sym] = Callback.new(block)
21
26
  end
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.1
4
+ version: 2.1.0
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-27 00:00:00 -06:00
12
+ date: 2010-01-19 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -31,23 +31,20 @@ extensions: []
31
31
  extra_rdoc_files:
32
32
  - LICENSE
33
33
  files:
34
- - .gitignore
35
34
  - LICENSE
36
35
  - Rakefile
37
36
  - VERSION
38
- - examples/bot.rb
39
- - examples/regex_bot.rb
40
- - examples/relay.rb
41
37
  - lib/on_irc.rb
42
38
  - lib/on_irc/callback.rb
39
+ - lib/on_irc/commands.rb
43
40
  - lib/on_irc/config.rb
44
41
  - lib/on_irc/config_accessor.rb
45
42
  - lib/on_irc/connection.rb
46
43
  - lib/on_irc/dsl_accessor.rb
47
44
  - lib/on_irc/event.rb
48
45
  - lib/on_irc/parser.rb
46
+ - lib/on_irc/sender.rb
49
47
  - lib/on_irc/server.rb
50
- - on_irc.gemspec
51
48
  has_rdoc: true
52
49
  homepage: http://github.com/tsion/on_irc
53
50
  licenses: []
data/.gitignore DELETED
@@ -1,3 +0,0 @@
1
- doc/
2
- pkg/
3
- .*.swp
data/on_irc.gemspec DELETED
@@ -1,61 +0,0 @@
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.1"
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-27}
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
-