cinch 0.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,9 +10,6 @@ plugin, defining a rule, and watching your profits flourish.
10
10
  Cinch will do all of the hard work for you, so you can spend time creating cool plugins
11
11
  and extensions to wow your internet peers.
12
12
 
13
- Cinch is not a fully fledged IRC framework, its base is extremely small. If you're looking
14
- for a more controlled library check out {silverplatter-irc}[http://github.com/apeiros/silverplatter-irc].
15
-
16
13
  == Installation
17
14
  The latest version of Cinch is 0.2
18
15
 
@@ -102,9 +99,14 @@ could pass the 'nick' option to the hash.
102
99
  bot.join #{m.args[:channel]}
103
100
  end
104
101
 
102
+ This method also works for arrays, to only reply to a message sent in the foo and bar channels
103
+
104
+ bot.plugin :hello, :channel => ['#foo', '#bar'] do |m|
105
+ m.reply "Hello"
106
+ end
107
+
105
108
  == Named Parameter Types
106
- Since version 0.2, Cinch supports named parameter types. Although at the moment you're constrained
107
- to the <i>digit</i>, <i>string</i> and <i>word</i> types. It means stuff like the this works:
109
+ Since version 0.2, Cinch supports named parameter types. It means stuff like the this works:
108
110
 
109
111
  bot.plugin("say :n-digit :text") do |m|
110
112
  m.args[:n].to_i.times {
@@ -119,7 +121,24 @@ This would provide the following output on IRC
119
121
  Cinch> foo bar
120
122
  Cinch> foo bar
121
123
 
122
- * See Cinch::Base#compile for more information
124
+ * See Cinch::Base#compile for more information and the available types
125
+
126
+ Cinch also supports custom types. That's right, you can define you own type. Just like this:
127
+
128
+ bot.add_custom_type(:friends, "(injekt|lee|john|bob)")
129
+
130
+ bot.plugin("I like :friend-friends", :prefix => false) do |m|
131
+ m.reply "I like #{m.args[:friend]} too!"
132
+ end
133
+
134
+ Which would provide the following output on IRC:
135
+
136
+ * Cinch has joined #cinch
137
+ injekt> I like spongebob
138
+ injekt> I like bob
139
+ Cinch> I like bob too!
140
+
141
+ Note though that you must wrap your type regexp in capturing parenthesis and escape it yourself
123
142
 
124
143
  == Authors
125
144
  Just me at the moment, sad poor lonely me...
@@ -0,0 +1,20 @@
1
+ require 'cinch'
2
+
3
+ bot = Cinch.setup do
4
+ server "irc.freenode.org"
5
+ channels %w{ #cinch }
6
+ end
7
+
8
+ bot.add_custom_type(:friends, "(injekt|lee|john|bob)")
9
+ bot.add_custom_type(:hex, "([\\dA-Fa-f]+?)")
10
+
11
+ bot.plugin("I like :person-friends", :prefix => false) do |m|
12
+ m.reply "I like #{m.args[:person]} too!"
13
+ end
14
+
15
+ bot.plugin("checkhex :n-hex") do |m|
16
+ m.answer "Yes, #{m.args[:n]} is indeed hex."
17
+ end
18
+
19
+ bot.run
20
+
@@ -23,7 +23,7 @@ bot.on :privmsg do |m|
23
23
  end
24
24
  end
25
25
 
26
- bot.plugin("seen :nick foo") do |m|
26
+ bot.plugin("seen :nick") do |m|
27
27
  nick = m.args[:nick]
28
28
 
29
29
  if nick == bot.nick
@@ -9,7 +9,7 @@ require 'cinch/rules'
9
9
  require 'cinch/base'
10
10
 
11
11
  module Cinch
12
- VERSION = '0.2'
12
+ VERSION = '0.2.3'
13
13
 
14
14
  # Setup bot options and return a new Cinch::Base instance
15
15
  def self.setup(ops={}, &blk)
@@ -62,6 +62,7 @@ module Cinch
62
62
 
63
63
  @rules = Rules.new
64
64
  @listeners = {}
65
+ @custom_types = {}
65
66
 
66
67
  @irc = IRC::Socket.new(options[:server], options[:port])
67
68
  @parser = IRC::Parser.new
@@ -138,9 +139,11 @@ module Cinch
138
139
  #
139
140
  # So far 3 types are supported:
140
141
  #
141
- # * word - matches [a-zA-Z]+
142
+ # * word - matches [a-zA-Z_]+
142
143
  # * string - matches \w+
143
144
  # * digit - matches \d+
145
+ # * lower - matches [a-z]+
146
+ # * upper - matches [A-Z]+
144
147
  #
145
148
  # == Examples
146
149
  # For capturing individual words
@@ -189,10 +192,16 @@ module Cinch
189
192
 
190
193
  case type
191
194
  when 'digit'; "(\\d+?)"
192
- when 'word'; "([a-zA-Z]+?)"
195
+ when 'word'; "([a-zA-Z_]+?)"
193
196
  when 'string'; "(\\w+?)"
197
+ when 'upper'; "([A-Z]+?)"
198
+ when 'lower'; "([a-z]+?)"
194
199
  else
195
- "([^\x00\r\n]+?)"
200
+ if @custom_types.include?(type)
201
+ @custom_types[type]
202
+ else
203
+ "([^\x00\r\n]+?)"
204
+ end
196
205
  end
197
206
  else
198
207
  keys << k[1..-1]
@@ -203,6 +212,24 @@ module Cinch
203
212
  ["^#{pattern}$", keys]
204
213
  end
205
214
 
215
+ # Add a custom 'type', for rule validation
216
+ #
217
+ # == Example
218
+ # bot = Cinch.setup do
219
+ # server 'irc.freenode.org'
220
+ # port 6667
221
+ # end
222
+ #
223
+ # bot.add_custom_type(:number, "([0-9])")
224
+ #
225
+ # bot.plugin("getnum :foo-number") do |m|
226
+ # m.reply "Your number was: #{m.args[:foo]}"
227
+ # end
228
+ def add_custom_type(name, pattern)
229
+ @custom_types[name.to_s] = pattern.to_s
230
+ end
231
+ alias :add_type :add_custom_type
232
+
206
233
  # Run run run
207
234
  def run
208
235
  @irc.connect options.server, options.port
@@ -230,6 +257,11 @@ module Cinch
230
257
  end
231
258
 
232
259
  if [:privmsg].include?(message.symbol)
260
+
261
+ # At the moment we must traverse all possibly rules, which
262
+ # could get clunky with a lot of rules. This is because each
263
+ # rule can be unique in what prefix it uses, in future some kind
264
+ # of loose checking should be put in place
233
265
  rules.each do |rule|
234
266
  unless rule.options.key?(:prefix) || options.prefix == false
235
267
  rule.to_s.insert(1, options.prefix) unless rule.to_s[1].chr == options.prefix
@@ -86,16 +86,20 @@ module Cinch
86
86
  @data[:host] = host
87
87
  end
88
88
 
89
+ # Reply to a channel or user, probably the most commonly used helper
90
+ # method
89
91
  def reply(text)
90
92
  recipient = data[:channel] || data[:nick]
91
93
  @irc.privmsg(recipient, text)
92
94
  end
93
95
 
96
+ # Same as reply but prefixes the users nickname
94
97
  def answer(text)
95
98
  return unless data[:channel]
96
99
  @irc.privmsg(data[:channel], "#{data[:nick]}: #{text}")
97
100
  end
98
101
 
102
+ # The deadly /me action
99
103
  def action(text)
100
104
  reply("\001ACTION #{text}\001")
101
105
  end
@@ -20,15 +20,15 @@ module Cinch
20
20
  def execute(message)
21
21
  options.keys.each do |key|
22
22
  case key
23
- when :nick
24
- return unless options[:nick] == message.nick
25
- when :host
26
- return unless options[:host] == message.host
27
- when :user
28
- return unless options[:user] == message.user
29
- when :channel
23
+ when :nick, :nicks
24
+ return unless validate(options[:nick], message.nick)
25
+ when :host, :hosts
26
+ return unless validate(options[:host], message.host)
27
+ when :user, :users
28
+ return unless validate(options[:user], message.user)
29
+ when :channel, :channels
30
30
  if message.channel
31
- return unless options[:channel] == message.channel
31
+ return unless validate(options[:channel], message.channel)
32
32
  end
33
33
  end
34
34
  end
@@ -38,6 +38,16 @@ module Cinch
38
38
  end
39
39
  end
40
40
 
41
+ # Validate rule attributes
42
+ def validate(option, attr)
43
+ if option.is_a?(Array)
44
+ return unless option.any?{|o| o == attr }
45
+ else
46
+ return unless options.to_s == attr
47
+ end
48
+ true
49
+ end
50
+
41
51
  # The rule as a String
42
52
  def to_s
43
53
  rule
metadata CHANGED
@@ -5,7 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- version: "0.2"
8
+ - 3
9
+ version: 0.2.3
9
10
  platform: ruby
10
11
  authors:
11
12
  - Lee 'injekt' Jarvis
@@ -13,7 +14,7 @@ autorequire:
13
14
  bindir: bin
14
15
  cert_chain: []
15
16
 
16
- date: 2010-04-26 00:00:00 +01:00
17
+ date: 2010-04-27 00:00:00 +01:00
17
18
  default_executable:
18
19
  dependencies:
19
20
  - !ruby/object:Gem::Dependency
@@ -58,6 +59,7 @@ files:
58
59
  - lib/cinch/irc.rb
59
60
  - examples/join_part.rb
60
61
  - examples/msg.rb
62
+ - examples/custom_types.rb
61
63
  - examples/seen.rb
62
64
  - examples/hello.rb
63
65
  - examples/named-param-types.rb