cinch 0.2.3 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -105,8 +105,8 @@ This method also works for arrays, to only reply to a message sent in the foo an
105
105
  m.reply "Hello"
106
106
  end
107
107
 
108
- == Named Parameter Types
109
- Since version 0.2, Cinch supports named parameter types. It means stuff like the this works:
108
+ == Named Parameter Patterns
109
+ Since version 0.2, Cinch supports named parameter patterns. It means stuff like the this works:
110
110
 
111
111
  bot.plugin("say :n-digit :text") do |m|
112
112
  m.args[:n].to_i.times {
@@ -121,11 +121,12 @@ This would provide the following output on IRC
121
121
  Cinch> foo bar
122
122
  Cinch> foo bar
123
123
 
124
- * See Cinch::Base#compile for more information and the available types
124
+ * See Cinch::Base#compile for more information and the available patterns
125
125
 
126
- Cinch also supports custom types. That's right, you can define you own type. Just like this:
126
+ Cinch also supports custom named parameter patterns. That's right, you can define you own
127
+ pattern. Just like this:
127
128
 
128
- bot.add_custom_type(:friends, "(injekt|lee|john|bob)")
129
+ bot.add_custom_pattern(:friends, "(injekt|lee|john|bob)")
129
130
 
130
131
  bot.plugin("I like :friend-friends", :prefix => false) do |m|
131
132
  m.reply "I like #{m.args[:friend]} too!"
@@ -0,0 +1,32 @@
1
+ require 'cinch'
2
+
3
+ # Give this bot ops in a channel and it'll auto voice
4
+ # visitors
5
+ #
6
+ # Enable with !autovoice on
7
+ # Disable with !autovoice off
8
+
9
+ bot = Cinch.setup do
10
+ server "irc.freenode.org"
11
+ channels %w( #cinch )
12
+ end
13
+
14
+ autovoice = true
15
+
16
+ bot.on :join do |m|
17
+ unless m.nick == bot.options.nick # We shouldn't attempt to voice ourselves
18
+ bot.mode(m.channel, '+v', m.nick) if autovoice
19
+ end
20
+ end
21
+
22
+ bot.add_custom_pattern(:onoff, '(on|off)')
23
+
24
+ bot.plugin "autovoice :option-onoff" do |m|
25
+ case m.args[:option]
26
+ when 'on'; autovoice = true
27
+ when 'off'; autovoice = false
28
+ end
29
+ m.answer "Autovoice is now #{autovoice ? 'enabled' : 'disabled'}"
30
+ end
31
+
32
+ bot.run
@@ -1,12 +1,12 @@
1
- require 'cinch'
1
+ require '/home/injekt/code/cinch/lib/cinch'
2
2
 
3
3
  bot = Cinch.setup do
4
4
  server "irc.freenode.org"
5
5
  channels %w{ #cinch }
6
6
  end
7
7
 
8
- bot.add_custom_type(:friends, "(injekt|lee|john|bob)")
9
- bot.add_custom_type(:hex, "([\\dA-Fa-f]+?)")
8
+ bot.add_custom_pattern(:friends, "(injekt|lee|john|bob)")
9
+ bot.add_custom_pattern(:hex, "([\\dA-Fa-f]+?)")
10
10
 
11
11
  bot.plugin("I like :person-friends", :prefix => false) do |m|
12
12
  m.reply "I like #{m.args[:person]} too!"
@@ -17,4 +17,3 @@ bot.plugin("checkhex :n-hex") do |m|
17
17
  end
18
18
 
19
19
  bot.run
20
-
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'cinch'
4
+
5
+ bot = Cinch.setup do
6
+ server "irc.freenode.org"
7
+ channels %w( #cinch )
8
+ end
9
+
10
+ class Memo < Struct.new(:nick, :channel, :text, :time)
11
+ def to_s
12
+ "[#{time.asctime}] <#{channel}/#{nick}> #{text}"
13
+ end
14
+ end
15
+
16
+ @memos = {}
17
+
18
+ bot.on :privmsg do |m|
19
+ if @memos.has_key?(m.nick)
20
+ bot.privmsg m.nick, @memos[m.nick].to_s
21
+ @memos.delete(m.nick)
22
+ end
23
+ end
24
+
25
+ bot.plugin("memo :nick-string :memo") do |m|
26
+ nick = m.args[:nick]
27
+
28
+ if @memos.key?(nick)
29
+ m.reply "There's already a memo #{nick}. You can only store one right now"
30
+ elsif nick == m.nick
31
+ m.reply "You can't leave memos for yourself.."
32
+ elsif nick == bot.options.nick
33
+ m.reply "You can't leave memos for me.."
34
+ else
35
+ @memos[nick] = Memo.new(m.nick, m.channel, m.args[:memo], Time.new)
36
+ m.reply "Added memo for #{nick}"
37
+ end
38
+ end
39
+
40
+ bot.run
@@ -9,7 +9,7 @@ require 'cinch/rules'
9
9
  require 'cinch/base'
10
10
 
11
11
  module Cinch
12
- VERSION = '0.2.3'
12
+ VERSION = '0.2.5'
13
13
 
14
14
  # Setup bot options and return a new Cinch::Base instance
15
15
  def self.setup(ops={}, &blk)
@@ -62,7 +62,7 @@ module Cinch
62
62
 
63
63
  @rules = Rules.new
64
64
  @listeners = {}
65
- @custom_types = {}
65
+ @custom_patterns = {}
66
66
 
67
67
  @irc = IRC::Socket.new(options[:server], options[:port])
68
68
  @parser = IRC::Parser.new
@@ -135,9 +135,9 @@ module Cinch
135
135
 
136
136
  # This method builds a regular expression from your rule
137
137
  # and defines all named parameters, as well as dealing with
138
- # types.
138
+ # patterns.
139
139
  #
140
- # So far 3 types are supported:
140
+ # So far 3 patterns are supported:
141
141
  #
142
142
  # * word - matches [a-zA-Z_]+
143
143
  # * string - matches \w+
@@ -197,8 +197,8 @@ module Cinch
197
197
  when 'upper'; "([A-Z]+?)"
198
198
  when 'lower'; "([a-z]+?)"
199
199
  else
200
- if @custom_types.include?(type)
201
- @custom_types[type]
200
+ if @custom_patterns.include?(type)
201
+ @custom_patterns[type]
202
202
  else
203
203
  "([^\x00\r\n]+?)"
204
204
  end
@@ -220,15 +220,16 @@ module Cinch
220
220
  # port 6667
221
221
  # end
222
222
  #
223
- # bot.add_custom_type(:number, "([0-9])")
223
+ # bot.add_custom_pattern(:number, "([0-9])")
224
224
  #
225
225
  # bot.plugin("getnum :foo-number") do |m|
226
226
  # m.reply "Your number was: #{m.args[:foo]}"
227
227
  # end
228
- def add_custom_type(name, pattern)
229
- @custom_types[name.to_s] = pattern.to_s
228
+ def add_custom_pattern(name, pattern)
229
+ @custom_patterns[name.to_s] = pattern.to_s
230
230
  end
231
- alias :add_type :add_custom_type
231
+ alias :add_custom_type :add_custom_pattern # backwards
232
+ alias :add_pattern :add_custom_pattern
232
233
 
233
234
  # Run run run
234
235
  def run
@@ -82,19 +82,35 @@ describe "Cinch::Base" do
82
82
  keys.should be_empty
83
83
  end
84
84
 
85
- it "should convert a digit type" do
85
+ it "should convert a digit pattern" do
86
86
  rule, keys = @base.compile(":foo-digit")
87
87
  rule.should == "^(\\d+?)$"
88
88
  end
89
89
 
90
- it "should convert a string type" do
90
+ it "should convert a string pattern" do
91
91
  rule, keys = @base.compile(":foo-string")
92
92
  rule.should == "^(\\w+?)$"
93
93
  end
94
94
 
95
- it "should convert a word type" do
95
+ it "should convert a word pattern" do
96
96
  rule, keys = @base.compile(":foo-word")
97
- rule.should == "^([a-zA-Z]+?)$"
97
+ rule.should == "^([a-zA-Z_]+?)$"
98
+ end
99
+
100
+ it "should convert a lowercase pattern" do
101
+ rule, keys = @base.compile(":foo-lower")
102
+ rule.should == "^([a-z]+?)$"
103
+ end
104
+
105
+ it "should convert an uppercase pattern" do
106
+ rule, keys = @base.compile(":foo-upper")
107
+ rule.should == "^([A-Z]+?)$"
108
+ end
109
+
110
+ it "should convert a custom pattern" do
111
+ @base.add_custom_pattern(:people, "(foo|bar|baz)")
112
+ rule, keys = @base.compile(":foo-people")
113
+ rule.should == "^(foo|bar|baz)$"
98
114
  end
99
115
 
100
116
  it "should automatically add start and end anchors" do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 3
9
- version: 0.2.3
8
+ - 5
9
+ version: 0.2.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Lee 'injekt' Jarvis
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-27 00:00:00 +01:00
17
+ date: 2010-04-28 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -59,9 +59,11 @@ files:
59
59
  - lib/cinch/irc.rb
60
60
  - examples/join_part.rb
61
61
  - examples/msg.rb
62
- - examples/custom_types.rb
62
+ - examples/memo.rb
63
+ - examples/custom_patterns.rb
63
64
  - examples/seen.rb
64
65
  - examples/hello.rb
66
+ - examples/autovoice.rb
65
67
  - examples/named-param-types.rb
66
68
  has_rdoc: true
67
69
  homepage: http://rdoc.injekt.net/cinch