Class: IRC::Server::Modules::WordFilter

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

Constant Summary collapse

@@colors =
{
    :w => 15,
    :W => 0,
    :n => 1,
    :N => 14,
    :b => 2,
    :B => 12,
    :g => 3,
    :G => 9,
    :r => 5,
    :R => 4,
    :c => 10,
    :C => 11,
    :p => 6,
    :P => 13,
    :y => 7,
    :Y => 8,
}

Instance Attribute Summary

Attributes inherited from IRC::Server::Module

#server

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from IRC::Server::Module

#finalize

Constructor Details

#initialize(server) ⇒ WordFilter

Returns a new instance of WordFilter.



32
33
34
35
36
37
38
39
40
# File 'lib/failirc/server/modules/WordFilter.rb', line 32

def initialize (server)
    @events = {
        :custom => {
            :message => Event::Callback.new(self.method(:filter), -1),
        }
    }

    super(server)
end

Class Method Details

.color(char) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/failirc/server/modules/WordFilter.rb', line 106

def self.color (char)
    if !char
        return ''
    end

    color = @@colors[char.to_sym]

    if color
        return "\x03#{'%02d' % color}"
    else
        return "\x03"
    end
end

Instance Method Details

#filter(chain, fromRef, toRef, message, level = nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/failirc/server/modules/WordFilter.rb', line 60

def filter (chain, fromRef, toRef, message, level=nil)
    if chain != :input
        return
    end

    from = fromRef.value
    to   = toRef.value

    if to.is_a?(Channel)
        channel = to.modes

        if user = to.user(from)
            user = user.modes
        else
            user = Modes.new
        end
    else
        channel = Modes.new
        user    = Modes.new
    end

    client = from.modes

    if channel[:extended][:gay] || client[:extended][:gay] || user[:extended][:gay]
        rainbow(message, 'rrRRyyYYGGggccCCBBppPP')
    end
end

#rainbow(string, pattern = @rainbow) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/failirc/server/modules/WordFilter.rb', line 88

def rainbow (string, pattern=@rainbow)
    string.gsub!(/\x03((\d{1,2})?(,\d{1,2})?)?/, '')

    result   = ''
    position = 0

    string.each_char {|char|
        if position >= pattern.length
            position = 0
        end

        result << WordFilter.color(pattern[position, 1]) << char
        position += 1
    }

    string.replace result
end

#rehashObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/failirc/server/modules/WordFilter.rb', line 42

def rehash
    # config for the rainbow filter
    if tmp = @server.config.elements['config/modules/module[@name="WordFilter"]/rainbow']
        @rainbow = tmp.text
    else
        @rainbow = 'rrRRyyYYGGggccCCBBppPP'
    end

    # config for the replaces filter
    @replaces = []

    if tmp = @server.config.elements['config/modules/module[@name="WordFilter"]/replaces']
        tmp.elements.each('replace') {|element|
            @replaces.push({ :from => element.attributes['word'], :to => element.attributes['with'] })
        }
    end
end