Skip to content

Commit

Permalink
simplified internal event handling
Browse files Browse the repository at this point in the history
added a MessageParser module to outsource event parsing
  • Loading branch information
tbuehlmann committed Aug 25, 2012
1 parent 848417b commit 4618ea9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 44 deletions.
12 changes: 6 additions & 6 deletions lib/ponder/callback.rb
Expand Up @@ -14,13 +14,13 @@ def initialize(event_type = :channel, match = //, options = {}, proc = Proc.new
@options = options
end

def call(event_type, event_data = {})
if (event_type == :channel) || (event_type == :query)
@proc.call(event_data) if event_data[:message] =~ @match
elsif event_type == :topic
@proc.call(event_data) if event_data[:topic] =~ @match
def call(event)
if (event[:type] == :channel) || (event[:type] == :query)
@proc.call(event) if event[:message] =~ @match
elsif event[:type] == :topic
@proc.call(event) if event[:topic] =~ @match
else
@proc.call(event_data)
@proc.call(event)
end
end

Expand Down
39 changes: 39 additions & 0 deletions lib/ponder/message_parser.rb
@@ -0,0 +1,39 @@
module Ponder
module MessageParser
class << self
def parse(message)
case message
when /^(?:\:\S+ )?(\d\d\d) /
number = $1.to_i
{:type => number, :params => $'}

when /^:(\S+)!(\S+)@(\S+) PRIVMSG #(\S+) :/
{:type => :channel, :nick => $1, :user => $2, :host => $3, :channel => "##{$4}", :message => $'}

when /^:(\S+)!(\S+)@(\S+) PRIVMSG \S+ :/
{:type => :query, :nick => $1, :user => $2, :host => $3, :message => $'}

when /^:(\S+)!(\S+)@(\S+) JOIN :*(\S+)$/
{:type => :join, :nick => $1, :user => $2, :host => $3, :channel => $4}

when /^:(\S+)!(\S+)@(\S+) PART (\S+)/
{:type => :part, :nick => $1, :user => $2, :host => $3, :channel => $4, :message => $'.sub(/ :/, '')}

when /^:(\S+)!(\S+)@(\S+) QUIT/
{:type => :quit, :nick => $1, :user => $2, :host => $3, :message => $'.sub(/ :/, '')}

when /^:(\S+)!(\S+)@(\S+) NICK :/
{:type => :nickchange, :nick => $1, :user => $2, :host => $3, :new_nick => $'}

when /^:(\S+)!(\S+)@(\S+) KICK (\S+) (\S+) :/
{:type => :kick, :nick => $1, :user => $2, :host => $3, :channel => $4, :victim => $5, :reason => $'}

when /^:(\S+)!(\S+)@(\S+) TOPIC (\S+) :/
{:type => :topic, :nick => $1, :user => $2, :host => $3, :channel => $4, :topic => $'}
else
nil
end
end
end
end
end
51 changes: 13 additions & 38 deletions lib/ponder/thaum.rb
Expand Up @@ -10,6 +10,7 @@
require 'ponder/irc'
require 'ponder/logger/twoflogger'
require 'ponder/logger/blind_io'
require 'ponder/message_parser'

module Ponder
class Thaum
Expand Down Expand Up @@ -115,45 +116,19 @@ def parse(message)
@logger.info "<< #{message}"
@console_logger.info "<< #{message}"

case message
when /^PING \S+$/
if message =~ /^PING \S+$/
raw message.sub(/PING/, 'PONG')

when /^(?:\:\S+ )?(\d\d\d) /
number = $1.to_i
parse_event(number, :type => number, :params => $')

when /^:(\S+)!(\S+)@(\S+) PRIVMSG #(\S+) :/
parse_event(:channel, :type => :channel, :nick => $1, :user => $2, :host => $3, :channel => "##{$4}", :message => $')

when /^:(\S+)!(\S+)@(\S+) PRIVMSG \S+ :/
parse_event(:query, :type => :query, :nick => $1, :user => $2, :host => $3, :message => $')

when /^:(\S+)!(\S+)@(\S+) JOIN :*(\S+)$/
parse_event(:join, :type => :join, :nick => $1, :user => $2, :host => $3, :channel => $4)

when /^:(\S+)!(\S+)@(\S+) PART (\S+)/
parse_event(:part, :type => :part, :nick => $1, :user => $2, :host => $3, :channel => $4, :message => $'.sub(/ :/, ''))

when /^:(\S+)!(\S+)@(\S+) QUIT/
parse_event(:quit, :type => :quit, :nick => $1, :user => $2, :host => $3, :message => $'.sub(/ :/, ''))

when /^:(\S+)!(\S+)@(\S+) NICK :/
parse_event(:nickchange, :type => :nickchange, :nick => $1, :user => $2, :host => $3, :new_nick => $')

when /^:(\S+)!(\S+)@(\S+) KICK (\S+) (\S+) :/
parse_event(:kick, :type => :kick, :nick => $1, :user => $2, :host => $3, :channel => $4, :victim => $5, :reason => $')

when /^:(\S+)!(\S+)@(\S+) TOPIC (\S+) :/
parse_event(:topic, :type => :topic, :nick => $1, :user => $2, :host => $3, :channel => $4, :topic => $')
else
event = MessageParser.parse(message)
parse_event(event) if event
end

# if there are pending deferrabels, check if the message suits their matching pattern
@deferrables.each { |d| d.try(message) }
end

# process callbacks with exception handling.
def process_callbacks(event_type, event_data)
def process_callbacks(event_type, event)
@callbacks[event_type].each do |callback|
# process chain of before_filters, callback handling and after_filters
fiber = Fiber.new do
Expand All @@ -162,19 +137,19 @@ def process_callbacks(event_type, event_data)

# before filters (specific filters first, then :all)
(@before_filters[event_type] + @before_filters[:all]).each do |filter|
if filter.call(event_type, event_data) == false
if filter.call(event) == false
stop_running = true
break
end
end

unless stop_running
# handling
callback.call(event_type, event_data)
callback.call(event)

# after filters (specific filters first, then :all)
(@after_filters[event_type] + @after_filters[:all]).each do |filter|
filter.call(event_type, event_data)
filter.call(event)
end
end
rescue => e
Expand Down Expand Up @@ -205,13 +180,13 @@ def after_filter(event_types = :all, match = //, &block)
private

# parses incoming traffic (types)
def parse_event(event_type, event_data = {})
if ((event_type == 376) || (event_type == 422)) && !@connected
def parse_event(event)
if ((event[:type] == 376) || (event[:type] == 422)) && !@connected
@connected = true
process_callbacks(:connect, event_data)
process_callbacks(:connect, event)
end

process_callbacks(event_type, event_data)
process_callbacks(event[:type], event)
end

def filter(filter_type, event_types = :all, match = //, block = Proc.new)
Expand Down

0 comments on commit 4618ea9

Please sign in to comment.