Ruby-IRC 1.0.10 → 1.0.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/IRCEvent.rb +2 -2
  2. data/lib/IRCEvent.rb.~1.8.~ +90 -0
  3. metadata +3 -2
@@ -25,8 +25,8 @@ class IRCEvent
25
25
  # mess_parts[0] is server info
26
26
  # mess_parts[1] is the message that was sent
27
27
  @message = mess_parts[1]
28
- @stats = mess_parts[0].scan(/[-\w.\#\@\+]+/)
29
-
28
+ @stats = mess_parts[0].scan(/[-`\^\{\}\[\]\w.\#\@\+]+/)
29
+
30
30
  if @stats[0].match(/^PING/)
31
31
  @event_type = 'ping'
32
32
  elsif @stats[1] && @stats[1].match(/^\d+/)
@@ -0,0 +1,90 @@
1
+ require 'yaml'
2
+
3
+ # This is a lookup class for IRC event name mapping
4
+ class EventLookup
5
+ @@lookup = YAML.load_file("#{File.dirname(__FILE__)}/eventmap.yml")
6
+
7
+ # returns the event name, given a number
8
+ def EventLookup::find_by_number(num)
9
+ return @@lookup[num.to_i]
10
+ end
11
+ end
12
+
13
+
14
+ # Handles an IRC generated event.
15
+ # Handlers are for the IRC framework to use
16
+ # Callbacks are for users to add.
17
+ # Both handlers and callbacks can be called for the same event.
18
+ class IRCEvent
19
+ @@handlers = { 'ping' => lambda {|event| IRCConnection.send_to_server("PONG #{event.message}") } }
20
+ @@callbacks = Hash.new()
21
+ attr_reader :hostmask, :message, :event_type, :from, :channel, :target, :mode, :stats
22
+ def initialize (line)
23
+ line.sub!(/^:/, '')
24
+ mess_parts = line.split(':', 2);
25
+ # mess_parts[0] is server info
26
+ # mess_parts[1] is the message that was sent
27
+ @message = mess_parts[1]
28
+ @stats = mess_parts[0].scan(/[-\w.\#\@\+]+/)
29
+
30
+ if @stats[0].match(/^PING/)
31
+ @event_type = 'ping'
32
+ elsif @stats[1] && @stats[1].match(/^\d+/)
33
+ @event_type = EventLookup::find_by_number(@stats[1]);
34
+ @channel = @stats[3]
35
+ else
36
+ @event_type = @stats[2].downcase if @stats[2]
37
+ end
38
+
39
+ if @event_type != 'ping'
40
+ @from = @stats[0]
41
+ @user = IRCUser.create_user(@from)
42
+ end
43
+ # FIXME: this list would probably be more accurate to exclude commands than to include them
44
+ @hostmask = @stats[1] if %W(topic privmsg join).include? @event_type
45
+ @channel = @stats[3] if @stats[3] && !@channel
46
+ @target = @stats[5] if @stats[5]
47
+ @mode = @stats[4] if @stats[4]
48
+
49
+
50
+
51
+ # Unfortunatly, not all messages are created equal. This is our
52
+ # special exceptions section
53
+ if @event_type == 'join'
54
+ @channel = @message
55
+ end
56
+
57
+ end
58
+
59
+ # Adds a callback for the specified irc message.
60
+ def IRCEvent.add_callback(message_id, &callback)
61
+ @@callbacks[message_id] = callback
62
+ end
63
+
64
+ # Adds a handler to the handler function hash.
65
+ def IRCEvent.add_handler(message_id, proc=nil, &handler)
66
+ if block_given?
67
+ @@handlers[message_id] = handler
68
+ elsif proc
69
+ @@handlers[message_id] = proc
70
+ end
71
+ end
72
+
73
+ # Process this event, preforming which ever handler and callback is specified
74
+ # for this event.
75
+ def process
76
+ handled = nil
77
+ if @@handlers[@event_type]
78
+ @@handlers[@event_type].call(self)
79
+ handled = 1
80
+ end
81
+ if @@callbacks[@event_type]
82
+ @@callbacks[@event_type].call(self)
83
+ handled = 1
84
+ end
85
+ if !handled
86
+ # puts "No handler for event type #@event_type in #{self.class}"
87
+ end
88
+ end
89
+ end
90
+
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: Ruby-IRC
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.0.10
7
- date: 2007-11-07 00:00:00 -08:00
6
+ version: 1.0.11
7
+ date: 2007-11-08 00:00:00 -08:00
8
8
  summary: An IRC Client library
9
9
  require_paths:
10
10
  - lib
@@ -38,6 +38,7 @@ files:
38
38
  - lib/IRCUtil.rb
39
39
  - lib/eventmap.yml
40
40
  - lib/IRCConnection.rb.~1.4.~
41
+ - lib/IRCEvent.rb.~1.8.~
41
42
  - README
42
43
  test_files: []
43
44