remailer 0.3.8 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/remailer/interpreter.rb +62 -16
- data/remailer.gemspec +1 -1
- data/test/unit/remailer_interpreter_test.rb +66 -0
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
data/lib/remailer/interpreter.rb
CHANGED
@@ -31,10 +31,16 @@ class Remailer::Interpreter
|
|
31
31
|
# Returns the states that are defined as a has with their associated
|
32
32
|
# options. The default keys are :initialized and :terminated.
|
33
33
|
def self.states
|
34
|
-
@states ||=
|
35
|
-
:
|
36
|
-
|
37
|
-
|
34
|
+
@states ||=
|
35
|
+
case (superclass.respond_to?(:states))
|
36
|
+
when true
|
37
|
+
superclass.states.dup
|
38
|
+
else
|
39
|
+
{
|
40
|
+
:initialized => { },
|
41
|
+
:terminated => { }
|
42
|
+
}
|
43
|
+
end
|
38
44
|
end
|
39
45
|
|
40
46
|
# Returns true if a given state is defined, false otherwise.
|
@@ -64,7 +70,7 @@ class Remailer::Interpreter
|
|
64
70
|
# Fixnum defines a minimum size to process, useful for packed binary
|
65
71
|
# streams, and a Regexp defines a pattern that must match before the parser
|
66
72
|
# is engaged.
|
67
|
-
def self.
|
73
|
+
def self.create_parser_for_spec(spec, &block)
|
68
74
|
case (spec)
|
69
75
|
when nil
|
70
76
|
block
|
@@ -91,7 +97,7 @@ class Remailer::Interpreter
|
|
91
97
|
# Defines a parser for this interpreter. The supplied block is executed in
|
92
98
|
# the context of a parser instance.
|
93
99
|
def self.parse(spec = nil, &block)
|
94
|
-
@parser =
|
100
|
+
@parser = create_parser_for_spec(spec, &block)
|
95
101
|
end
|
96
102
|
|
97
103
|
# Assigns the default interpreter.
|
@@ -107,17 +113,35 @@ class Remailer::Interpreter
|
|
107
113
|
|
108
114
|
# Returns the parser used when no state-specific parser has been defined.
|
109
115
|
def self.default_parser
|
110
|
-
@parser ||=
|
116
|
+
@parser ||=
|
117
|
+
case (superclass.respond_to?(:default_parser))
|
118
|
+
when true
|
119
|
+
superclass.default_parser
|
120
|
+
else
|
121
|
+
lambda { |s| _s = s.dup; s.replace(''); _s }
|
122
|
+
end
|
111
123
|
end
|
112
124
|
|
113
125
|
# Returns the current default_interpreter.
|
114
126
|
def self.default_interpreter
|
115
|
-
@default
|
127
|
+
@default ||=
|
128
|
+
case (superclass.respond_to?(:default_interpreter))
|
129
|
+
when true
|
130
|
+
superclass.default_interpreter
|
131
|
+
else
|
132
|
+
nil
|
133
|
+
end
|
116
134
|
end
|
117
135
|
|
118
136
|
# Returns the defined error handler
|
119
137
|
def self.on_error_handler
|
120
|
-
@on_error
|
138
|
+
@on_error ||=
|
139
|
+
case (superclass.respond_to?(:on_error_handler))
|
140
|
+
when true
|
141
|
+
superclass.on_error_handler
|
142
|
+
else
|
143
|
+
nil
|
144
|
+
end
|
121
145
|
end
|
122
146
|
|
123
147
|
# == Instance Methods =====================================================
|
@@ -191,17 +215,39 @@ class Remailer::Interpreter
|
|
191
215
|
# interpretation should be defined by declaring a state with an interpret
|
192
216
|
# block defined.
|
193
217
|
def interpret(*args)
|
194
|
-
object = args
|
218
|
+
object = args[0]
|
195
219
|
config = self.class.states[@state]
|
196
|
-
|
220
|
+
interpreters = (config and config[:interpret])
|
197
221
|
|
198
|
-
if (
|
199
|
-
|
200
|
-
|
222
|
+
if (interpreters)
|
223
|
+
match_result = nil
|
224
|
+
|
225
|
+
matched, proc = interpreters.find do |response, proc|
|
226
|
+
case (response)
|
227
|
+
when Regexp
|
228
|
+
match_result = response.match(object)
|
229
|
+
else
|
230
|
+
response === object
|
231
|
+
end
|
201
232
|
end
|
202
|
-
|
233
|
+
|
203
234
|
if (matched)
|
204
|
-
|
235
|
+
case (matched)
|
236
|
+
when Regexp
|
237
|
+
match_result = match_result.to_a
|
238
|
+
|
239
|
+
if (match_result.length > 1)
|
240
|
+
match_string = match_result.shift
|
241
|
+
args[0, 1] = match_result
|
242
|
+
else
|
243
|
+
args[0].sub!(match_result[0], '')
|
244
|
+
end
|
245
|
+
when String
|
246
|
+
args[0].sub!(matched, '')
|
247
|
+
else
|
248
|
+
args.shift
|
249
|
+
end
|
250
|
+
|
205
251
|
instance_exec(*args, &proc)
|
206
252
|
|
207
253
|
return true
|
data/remailer.gemspec
CHANGED
@@ -40,6 +40,26 @@ class LineInterpreter < Remailer::Interpreter
|
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
|
+
class LineInterpreterSubclass < LineInterpreter
|
44
|
+
parse(/^.*?\0/) do |data|
|
45
|
+
data.sub(/\0$/, '')
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
class RegexpInterpreter < Remailer::Interpreter
|
50
|
+
attr_reader :received
|
51
|
+
|
52
|
+
state :initialized do
|
53
|
+
interpret(/^HELO\s+/) do |line|
|
54
|
+
@received = [ :helo, line ]
|
55
|
+
end
|
56
|
+
|
57
|
+
interpret(/^MAIL FROM:<([^>]+)>/) do |line|
|
58
|
+
@received = [ :mail_from, line ]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
43
63
|
class ExampleInterpreter < Remailer::Interpreter
|
44
64
|
include TestTriggerHelper
|
45
65
|
|
@@ -91,6 +111,12 @@ class RemailerInterpreterTest < Test::Unit::TestCase
|
|
91
111
|
interpreter = Remailer::Interpreter.new
|
92
112
|
|
93
113
|
assert_equal :initialized, interpreter.state
|
114
|
+
|
115
|
+
buffer = 'a'
|
116
|
+
|
117
|
+
interpreter.parse(buffer)
|
118
|
+
|
119
|
+
assert_equal '', buffer
|
94
120
|
end
|
95
121
|
|
96
122
|
def test_delegate
|
@@ -188,6 +214,46 @@ class RemailerInterpreterTest < Test::Unit::TestCase
|
|
188
214
|
assert_equal 'INCOMPLETE LINE', interpreter.lines[-1]
|
189
215
|
assert_equal '', line
|
190
216
|
end
|
217
|
+
|
218
|
+
def test_interpreter_subclass
|
219
|
+
interpreter = LineInterpreterSubclass.new
|
220
|
+
|
221
|
+
assert_equal [ ], interpreter.lines
|
222
|
+
|
223
|
+
line = "TEST"
|
224
|
+
|
225
|
+
interpreter.process(line)
|
226
|
+
|
227
|
+
assert_equal nil, interpreter.lines[-1]
|
228
|
+
assert_equal "TEST", line
|
229
|
+
|
230
|
+
line << "\0"
|
231
|
+
|
232
|
+
interpreter.process(line)
|
233
|
+
|
234
|
+
assert_equal "TEST", interpreter.lines[-1]
|
235
|
+
assert_equal "", line
|
236
|
+
end
|
237
|
+
|
238
|
+
def test_regexp_interpreter
|
239
|
+
interpreter = RegexpInterpreter.new
|
240
|
+
|
241
|
+
assert_equal nil, interpreter.received
|
242
|
+
|
243
|
+
line = "HELO example.com"
|
244
|
+
|
245
|
+
interpreter.process(line)
|
246
|
+
assert_equal '', line
|
247
|
+
|
248
|
+
assert_equal [ :helo, 'example.com' ], interpreter.received
|
249
|
+
|
250
|
+
line = "MAIL FROM:<example@example.com>"
|
251
|
+
|
252
|
+
interpreter.process(line)
|
253
|
+
assert_equal '', line
|
254
|
+
|
255
|
+
assert_equal [ :mail_from, 'example@example.com' ], interpreter.received
|
256
|
+
end
|
191
257
|
|
192
258
|
def test_default_handler_for_interpreter
|
193
259
|
interpreter = ExampleInterpreter.new
|