Skip to content

Commit

Permalink
Fix few stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
meh committed Jul 13, 2011
1 parent 202d940 commit 649d96f
Show file tree
Hide file tree
Showing 12 changed files with 293 additions and 215 deletions.
4 changes: 2 additions & 2 deletions lib/failirc/common/events.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def event (chain, what)
if what.is_a?(Symbol) if what.is_a?(Symbol)
Event.new(self, chain, (@chains[chain][what] + @hooks.map {|hook| hook.chains[chain][what]}).flatten.compact, [what]) Event.new(self, chain, (@chains[chain][what] + @hooks.map {|hook| hook.chains[chain][what]}).flatten.compact, [what])
else else
callbacks = Hash.new { [] } callbacks = Hash.new {|hash, key| hash[key] = [] }


(@hooks + [self]).each {|hook| (@hooks + [self]).each {|hook|
hook.chains[chain].each {|key, value| hook.chains[chain].each {|key, value|
Expand All @@ -68,7 +68,7 @@ def event (chain, what)
regexps, callbacks = callbacks.to_a.select {|(name, callbacks)| regexps, callbacks = callbacks.to_a.select {|(name, callbacks)|
!name.is_a?(Symbol) !name.is_a?(Symbol)
}.select {|(regexp, callbacks)| }.select {|(regexp, callbacks)|
what.to_s.match(regexp) rescue nil what.to_s.match(regexp) rescue false
}.transpose }.transpose


aliases = (regexps || []).flatten.compact.map {|regexp| aliases = (regexps || []).flatten.compact.map {|regexp|
Expand Down
2 changes: 1 addition & 1 deletion lib/failirc/common/events/event.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def on (thing, string)
end end


def alias? (name) def alias? (name)
@aliases.member?(name.to_sym.downcase) @aliases.member?(name.to_s.downcase.to_sym)
end end


def call (*args, &block) def call (*args, &block)
Expand Down
8 changes: 4 additions & 4 deletions lib/failirc/common/extensions.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def interpolate (on=nil)
on.instance_eval("%{#{self}}") on.instance_eval("%{#{self}}")
end end
rescue Exception => e rescue Exception => e
debug e IRC.debug e
self self
end end
end end
Expand Down Expand Up @@ -388,9 +388,9 @@ class ThreadSafeHash < CaseInsensitiveHash
def self.def_threaded (*methods) def self.def_threaded (*methods)
methods.each {|method| methods.each {|method|
define_method method do |*args, &block| define_method method do |*args, &block|
@semaphore.synchronize { @semaphore.synchronize {
super super(*args, &block)
} }
end end
} }
end end
Expand Down
30 changes: 28 additions & 2 deletions lib/failirc/common/modes.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def initialize (data=nil)
end end
end end


def method_missing (name, *args) def method_missing (name, *args, &block)
name = name.to_s name = name.to_s


begin begin
Expand All @@ -52,10 +52,14 @@ def method_missing (name, *args)
self[name] self[name]
end end
rescue rescue
super super(name.to_sym, *args, &block)
end end
end end


def each (&block)
@modes.values.uniq.each &block
end

memoize memoize
def [] (name) def [] (name)
if !supports?(name) if !supports?(name)
Expand Down Expand Up @@ -113,10 +117,32 @@ def as (what)
@as = nil @as = nil
end end


def empty?
each {|mode|
return false if mode.enabled?
}

true
end

def to_hash def to_hash
@modes @modes
end end


def to_s
modes = []
values = []

each {|mode|
next unless mode.enabled?

modes << mode.code
values << mode.value unless mode.value === false or mode.value === true
}

"+#{modes.join}#{" #{values.join(' ')}" if values}"
end

def inspect def inspect
@@definitions.to_s @@definitions.to_s
end end
Expand Down
2 changes: 1 addition & 1 deletion lib/failirc/common/modules.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def initialize (owner, path)
end end


def load (name, options={}) def load (name, options={})
mod = Module.for(@owner).new mod = Module.for(@owner).new(options)


$:.each {|path| $:.each {|path|
path = "#{path}/#{@path}/#{name}.rb" path = "#{path}/#{@path}/#{name}.rb"
Expand Down
12 changes: 11 additions & 1 deletion lib/failirc/server.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
# along with failirc. If not, see <http://www.gnu.org/licenses/>. # along with failirc. If not, see <http://www.gnu.org/licenses/>.
#++ #++


require 'resolv'

require 'failirc/version'
require 'failirc/common/utils' require 'failirc/common/utils'
require 'failirc/common/events' require 'failirc/common/events'
require 'failirc/common/workers' require 'failirc/common/workers'
require 'failirc/common/modules' require 'failirc/common/modules'
require 'failirc/common/modes' require 'failirc/common/modes'
require 'failirc/common/mask'


require 'failirc/server/dispatcher' require 'failirc/server/dispatcher'


Expand All @@ -30,7 +34,7 @@ module IRC
class Server class Server
extend Forwardable extend Forwardable


attr_reader :options, :dispatcher attr_reader :options, :dispatcher, :created_on


def_delegators :@dispatcher, :listen def_delegators :@dispatcher, :listen
def_delegators :@events, :register, :dispatch, :observe, :fire, :hook def_delegators :@events, :register, :dispatch, :observe, :fire, :hook
Expand All @@ -40,6 +44,8 @@ class Server
def initialize (options={}) def initialize (options={})
@options = HashWithIndifferentAccess.new(options) @options = HashWithIndifferentAccess.new(options)


@created_on = Time.new

@dispatcher = Dispatcher.new(self) @dispatcher = Dispatcher.new(self)
@events = Events.new(self) @events = Events.new(self)
@workers = Workers.new(self) @workers = Workers.new(self)
Expand All @@ -57,6 +63,10 @@ def initialize (options={})
mod = @modules.load(name, data) mod = @modules.load(name, data)


if mod if mod
mod.define_singleton_method :server do
self
end

hook mod hook mod


IRC.debug "#{name} loaded" IRC.debug "#{name} loaded"
Expand Down
6 changes: 2 additions & 4 deletions lib/failirc/server/dispatcher.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def do
return unless @running or not @clients return unless @running or not @clients


erroring.each {|client| erroring.each {|client|
client.kill 'Input/output error', force: true client.disconnect 'Input/output error'
} }


reading.each {|thing| reading.each {|thing|
Expand All @@ -101,9 +101,7 @@ def clients
end end


def wakeup (options = {}) def wakeup (options = {})
if options[:reset] @clients = nil if options[:reset]
@clients = nil
end


@pipes.last.write '?' @pipes.last.write '?'
end end
Expand Down
52 changes: 31 additions & 21 deletions lib/failirc/server/dispatcher/client.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ def ssl?
end end


def receive def receive
ap disconnected? or killed? return if disconnected?

return if disconnected? or killed?


begin begin
input = '' input = ''
Expand All @@ -63,15 +61,15 @@ def receive
@input.push(string) @input.push(string)
} }
rescue IOError rescue IOError
kill 'Input/output error', :force => true disconnect 'Input/output error', :force => true
rescue Errno::EBADF, Errno::EPIPE, OpenSSL::SSL::SSLError rescue Errno::EBADF, Errno::EPIPE, OpenSSL::SSL::SSLError
kill 'Client exited', :force => true disconnect 'Client exited', :force => true
rescue Errno::ECONNRESET rescue Errno::ECONNRESET
kill 'Connection reset by peer', :force => true disconnect 'Connection reset by peer', :force => true
rescue Errno::ETIMEDOUT rescue Errno::ETIMEDOUT
kill 'Ping timeout', :force => true disconnect 'Ping timeout', :force => true
rescue Errno::EHOSTUNREACH rescue Errno::EHOSTUNREACH
kill 'No route to host', :force => true disconnect 'No route to host', :force => true
rescue Exception => e rescue Exception => e
IRC.debug e IRC.debug e
end end
Expand All @@ -98,15 +96,15 @@ def flush


@last = nil @last = nil
rescue IOError rescue IOError
kill 'Input/output error', :force => true disconnect 'Input/output error'
rescue Errno::EBADF, Errno::EPIPE, OpenSSL::SSL::SSLError rescue Errno::EBADF, Errno::EPIPE, OpenSSL::SSL::SSLError
kill 'Client exited', :force => true disconnect 'Client exited'
rescue Errno::ECONNRESET rescue Errno::ECONNRESET
kill 'Connection reset by peer', :force => true disconnect 'Connection reset by peer'
rescue Errno::ETIMEDOUT rescue Errno::ETIMEDOUT
kill 'Ping timeout', :force => true disconnect 'Ping timeout'
rescue Errno::EHOSTUNREACH rescue Errno::EHOSTUNREACH
kill 'No route to host', :force => true disconnect 'No route to host'
rescue Errno::EAGAIN, IO::WaitWritable rescue Errno::EAGAIN, IO::WaitWritable
rescue Exception => e rescue Exception => e
IRC.debug e IRC.debug e
Expand All @@ -133,20 +131,32 @@ def handle
} }
end end


def kill (message, options={}) def disconnect (message, options={})
return if killed? and !options[:force] return if disconnected? and !options[:force]


@killed = true server.fire :disconnect, self, message


server.fire :kill, IRC.debug "#{self} disconnecting because: #{message}"
end


def killed? connected_to.clients.delete(self)
!!@killed dispatcher.wakeup reset: true

unless disconnected?
flush
@socket.close
end
end end


def disconnected? def disconnected?
@socket.closed? begin
@socket.closed?
rescue Exception
true
end
end

def to_s
"#{host}"
end end
end end


Expand Down
4 changes: 4 additions & 0 deletions lib/failirc/server/dispatcher/server.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ def accept
end end
} }
end end

def to_s
"#{host}/#{port}"
end
end end


end; end; end end; end; end
Loading

0 comments on commit 649d96f

Please sign in to comment.