Class: IRC::Server::Modules::Firewall

Inherits:
IRC::Server::Module show all
Defined in:
lib/failirc/server/modules/Firewall.rb

Constant Summary collapse

@@version =
'0.0.1'

Instance Attribute Summary

Attributes inherited from IRC::Server::Module

#server

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ Firewall

Returns a new instance of Firewall.



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/failirc/server/modules/Firewall.rb', line 39

def initialize (server)
    @events = {
        :pre  => Event::Callback.new(self.method(:dispatch), -9001),
        :post => Event::Callback.new(self.method(:dispatch), -9001),

        :custom => {
            :log  => self.method(:log),
            :kill => Event::Callback.new(self.method(:logKill), -9001),
        },
    }

    super(server)
end

Class Method Details

.versionObject



31
32
33
# File 'lib/failirc/server/modules/Firewall.rb', line 31

def self.version
    return @@version
end

Instance Method Details

#descriptionObject



35
36
37
# File 'lib/failirc/server/modules/Firewall.rb', line 35

def description
    "Firewall-#{Firewall.version}"
end

#dispatch(event, thing, string) ⇒ Object



73
74
75
76
77
78
# File 'lib/failirc/server/modules/Firewall.rb', line 73

def dispatch (event, thing, string)
    if (event.chain == :input && event.special == :pre) || (event.chain == :output && event.special == :post)
        @log.puts "[#{Time.now}] #{target(thing)} #{(event.chain == :input) ? '>' : '<'} #{string.inspect}"
        @log.flush
    end
end

#finalizeObject



67
68
69
70
71
# File 'lib/failirc/server/modules/Firewall.rb', line 67

def finalize
    if @log != $stdout
        @log.close
    end
end

#log(string) ⇒ Object



80
81
82
83
# File 'lib/failirc/server/modules/Firewall.rb', line 80

def log (string)
    @log.puts "[#{Time.now}] #{string}"
    @log.flush
end

#logKill(thing, message) ⇒ Object



85
86
87
# File 'lib/failirc/server/modules/Firewall.rb', line 85

def logKill (thing, message)
    log "#{target(thing)} KILL :#{message}"
end

#rehashObject



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/failirc/server/modules/Firewall.rb', line 53

def rehash
    if @log && @log != $stdout
        @log.close
    end

    file = @server.config.elements['config/modules/module[@name="Firewall"]/file']

    if file
        @log = File.open(file.text)
    else
        @log = $stdout
    end
end

#target(thing) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/failirc/server/modules/Firewall.rb', line 89

def target (thing)
    if thing.is_a?(Client) || thing.is_a?(User)
        target = "#{thing.nick || '*'}!#{thing.user || '*'}@#{thing.ip || '*'}"
    else
        target = thing.to_s
    end

    return target
end