irc 0.0.2 → 0.0.3

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/README.markdown CHANGED
@@ -1,7 +1,16 @@
1
+ Installation
2
+ ============
3
+
4
+ Simply install the gem:
5
+
6
+ `gem install irc`
7
+
1
8
  Usage
2
9
  =====
3
10
 
4
- ## Quickstart
11
+ Quickstart
12
+ ----------
13
+
5
14
  ``` ruby
6
15
  require 'irc'
7
16
 
@@ -16,17 +25,27 @@ end
16
25
 
17
26
  # Tell the time and date:
18
27
  match /^!(?:time|now)/ do
19
- reply Time.now.strftime("it is %l:%M %P on %A, %B %-d, %Y.").gsub(/[ ]+/, ' ' )
28
+ reply Time.now
29
+ end
30
+
31
+ # mention_match requires messages to start with the name of the bot
32
+ mention_match /join (?<chan>.+)/ do
33
+ channels = chan.split(/[, ]+/)
34
+ connection.join channels
35
+ reply "I joined #{channels.to_sentence}."
20
36
  end
21
37
 
22
38
  start!
23
39
  ```
24
- Then, `ruby newbot.rb`
40
+ Then, run with `ruby bot.rb`
41
+
25
42
 
43
+ Subclass
44
+ --------
26
45
 
27
- ## Subclass
46
+ Polluting the global namespace is bad.
47
+ Instead, you can `require 'irc/base'` and subclass `IRC::Bot`:
28
48
 
29
- You can also `require 'irc/base'` and subclass `IRC::Bot`:
30
49
  ``` ruby
31
50
  require 'irc/base'
32
51
 
@@ -40,8 +59,8 @@ end
40
59
  ```
41
60
 
42
61
  You can re-load the bot to update its callbacks without disconnecting:
62
+
43
63
  ``` ruby
44
- # mention_match requires messages to start with the name of the bot
45
64
  mention_match /reload!/ do
46
65
  self.class.reset!
47
66
 
data/lib/irc.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'irc/base'
2
2
 
3
3
  class NewBot < IRC::Bot
4
-
4
+ on(:ping) { connection.pong params}
5
5
  end
6
6
 
7
7
  module IRC
data/lib/irc/base.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require 'irc/extensions'
2
+ require 'irc/helpers'
2
3
 
4
+ require 'irc/job'
3
5
  require 'irc/connection'
4
6
  require 'irc/callback'
5
7
  require 'irc/message'
data/lib/irc/callback.rb CHANGED
@@ -4,32 +4,32 @@ module IRC
4
4
 
5
5
  def initialize action, regex = nil, factory = nil, &block
6
6
  @action = action
7
- @regex = regex || //
7
+ @regex = regex || //
8
8
  @block = block
9
9
  @factory = factory
10
10
  end
11
11
 
12
12
  def call! message
13
- if match = @regex.match(message.content)
14
- (class << message; self; end).send :attr_accessor, *@regex.names
15
- @regex.names.each do |name|
16
- message.instance_variable_set("@#{name}", match[name])
17
- end
13
+ match = regex.match(message.content)
14
+ return if !match
15
+
16
+ (class << message; self; end).send :attr_accessor, *@regex.names
18
17
 
19
- Thread.new do
20
- # begin
21
- if @factory
22
- @factory.new(message).instance_eval(&@block)
23
- else
24
- puts "calling #{@block}"
25
- @block.call(message, match)
26
- end
18
+ match.names.each do |name|
19
+ message.instance_variable_set("@#{name}", match[name])
20
+ end
21
+
22
+ Job.schedule do
23
+ if @factory
24
+ @factory.new(message).instance_eval(&@block)
25
+ else
26
+ @block.call(message, match)
27
27
  end
28
28
  end
29
29
  end
30
30
 
31
31
  class << self
32
- attr_accessor :callbacks
32
+ attr_accessor :callbacks, :filters
33
33
 
34
34
  def handle message
35
35
  callbacks[message.action].each do |callback|
@@ -43,12 +43,11 @@ module IRC
43
43
  callbacks[action] << callback
44
44
  callbacks[:all] << callback
45
45
  end
46
-
47
46
  def callbacks
48
47
  @callbacks ||= Hash.new {|hash,key| hash[key] = []}
49
48
  end
50
49
 
51
- def reset!
50
+ def clear!
52
51
  @callbacks = nil
53
52
  callbacks
54
53
  end
@@ -1,17 +1,17 @@
1
1
  require 'socket'
2
2
  require 'thread'
3
+ require 'monitor'
3
4
 
4
5
  module IRC
5
6
  class Connection
7
+ include MonitorMixin
8
+
9
+ attr_accessor :host, :port
6
10
  def initialize host = nil, port = 6667
7
11
  @host = host
8
12
  @port = port
9
13
  end
10
14
 
11
- def mutex
12
- @mutex ||= Mutex.new
13
- end
14
-
15
15
  def disconnected?
16
16
  !@socket || @socket.closed?
17
17
  end
@@ -21,12 +21,10 @@ module IRC
21
21
  end
22
22
 
23
23
  def write *strings
24
- # mutex do
25
- strings.each do |string|
26
- puts '-> ' + string
27
- socket.print string + "\r\n"
28
- end
29
- # end
24
+ strings.each do |string|
25
+ puts '-> ' + string
26
+ socket.print string + "\r\n"
27
+ end
30
28
  end
31
29
 
32
30
  def join *channels
@@ -0,0 +1,12 @@
1
+ module IRC
2
+ module Helpers
3
+ def inflect noun, lookup = nil
4
+ lookup ||= {
5
+ 'me' => nick,
6
+ 'you' => self.class.nick,
7
+ nick => 'you'
8
+ }
9
+ noun.gsub /[\w]+/i, lookup
10
+ end
11
+ end
12
+ end
data/lib/irc/job.rb ADDED
@@ -0,0 +1,55 @@
1
+ require 'thread'
2
+
3
+ module IRC
4
+ class Job
5
+ def initialize *args, &block
6
+ @args, @block = args, block
7
+ end
8
+
9
+ def call
10
+ @block.call(*@args)
11
+ end
12
+
13
+ class << self
14
+ attr_accessor :safe_mode
15
+ def schedule *args, &block
16
+ run!
17
+ jobs << new(*args, &block)
18
+ end
19
+
20
+ def size
21
+ @size ||= 1
22
+ end
23
+
24
+ def size= _size
25
+ _size = _size.to_i
26
+ _size = 1 if _size < 1
27
+
28
+ @size = _size
29
+ end
30
+
31
+ def jobs
32
+ @jobs ||= Queue.new
33
+ end
34
+
35
+ def run!
36
+ @pool ||= Array.new(size) do |i|
37
+ Thread.new do
38
+ catch :exit do
39
+ loop do
40
+ jobs.pop.call
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end # def run!
46
+
47
+ def shutdown
48
+ size.times do
49
+ schedule { throw :exit }
50
+ end
51
+ end # def shutdown
52
+ end # class << Job
53
+ at_exit { shutdown }
54
+ end # class Job
55
+ end # module IRC
data/lib/irc/message.rb CHANGED
@@ -1,13 +1,14 @@
1
1
  module IRC
2
2
  class Message
3
+ special_characters = Regexp.escape('-[]\^{}|`_')
3
4
  REGEX = /^
4
5
  (:
5
6
  (?<prefix>
6
7
  (
7
- (?<servername>(?<nick>[a-z][a-z0-9_\-\[\]\\`\^\{\}\.\|]*))
8
+ (?<servername>(?<nick>[a-z#{special_characters}][a-z0-9#{special_characters}]*))
8
9
  )
9
10
  (!(?<user>[a-z0-9~\.]+))?
10
- (@(?<host>[a-z0-9\.]+))?
11
+ (@(?<host>[a-z0-9\.\-_]{1,255}))?
11
12
  )
12
13
  [\ ]+
13
14
  )?
@@ -23,7 +24,7 @@ module IRC
23
24
  attr_accessor :connection
24
25
  attr_accessor *REGEX.names
25
26
 
26
- def initialize message_string, connection
27
+ def initialize message_string, connection = nil
27
28
  @raw = message_string
28
29
 
29
30
  @connection = connection
@@ -39,6 +40,10 @@ module IRC
39
40
  @action ||= (command || '').downcase.intern
40
41
  end
41
42
 
43
+ def target
44
+ middle
45
+ end
46
+
42
47
  def content
43
48
  @content ||= trailing || middle
44
49
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: irc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-02 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2013-01-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: serialport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.1.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 1.1.0
14
30
  description: a simple ruby irc (bot) framework
15
31
  email: jeff@petersonj.com
16
32
  executables: []
@@ -24,6 +40,8 @@ files:
24
40
  - lib/irc/callback.rb
25
41
  - lib/irc/connection.rb
26
42
  - lib/irc/extensions.rb
43
+ - lib/irc/helpers.rb
44
+ - lib/irc/job.rb
27
45
  - lib/irc/message.rb
28
46
  - lib/irc/store.rb
29
47
  homepage: http://github.com/jeffpeterson/irc