blob: aec0bc8d15c7cf03b923f5508078a7bdd78499d5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
87
88
89
90
91
|
#!/usr/bin/ruby
require 'rmail'
MAILIN="mail-in"
SPAM="spam-learn"
OUTBOX="outbox"
DOMAIN='lists.oftc.net'
FROM="listmoderators@lists.oftc.net"
HOSTNAME=`hostname`.chop
class Counter
@@counter = 0
def Counter.value
@@counter += 1
end
end
def uniqueName
"%d.%d_%d.%s"%[
Time.now.to_i,
$$,
Counter.value,
HOSTNAME]
end
def check_maildir(d)
throw "#{d} is not a maildir" unless FileTest.directory?(d)
throw "#{d} is not a maildir" unless FileTest.directory?(d+"/new")
throw "#{d} is not a maildir" unless FileTest.directory?(d+"/cur")
throw "#{d} is not a maildir" unless FileTest.directory?(d+"/tmp")
true
end
def store_in_maildir(md, msg)
fn = uniqueName
File.open(md+"/tmp/"+fn, "w", 0600) do |f|
f.puts msg
end
File.link(md+"/tmp/"+fn, md+"/new/"+fn)
File.unlink(md+"/tmp/"+fn)
md+"/new/"+fn
end
def process_mail(filename)
message = File.open(filename) { |f| RMail::Parser.read(f) }
# some sanity checks
throw "Mailman moderation mails are expected to have 3 mime parts" unless message.body.length == 3
throw "Mime Part 0 does have an unexpected content type: #{message.body[0].header['Content-Type']}" unless message.body[0].header['Content-Type'] == 'text/plain; charset="us-ascii"'
throw "Mime Part 1 does have an unexpected content type: #{message.body[1].header['Content-Type']}" unless message.body[1].header['Content-Type'] == 'message/rfc822'
throw "Mime Part 2 does have an unexpected content type: #{message.body[2].header['Content-Type']}" unless message.body[2].header['Content-Type'] == 'message/rfc822'
explanation_body = message.body[0].body
held_part = RMail::Parser.read( message.body[1].body )
discard_part = RMail::Parser.read( message.body[2].body )
# more sanity checks
throw "Did not find 'As list administrator, your..' boilerplate in mail" unless explanation_body =~ /^As list administrator, your authorization is requested for the/
throw "Did not find listname in mail" unless explanation_body =~ /^ *List: *.*@#{DOMAIN}/
throw "discard_part does have an unexpected content type: #{discard_part.header['Content-Type']}" unless discard_part.header['Content-Type'] == 'text/plain; charset="us-ascii"'
request_address = discard_part.header['From']
throw "discard_part does not have a from address" unless request_address
matchdata = /confirm ([0-9a-f]*)/.match discard_part.header['Subject']
throw "Could not find cookie in discard_part" unless matchdata and matchdata[1]
cookie = matchdata[1]
store_in_maildir(SPAM, held_part)
mail_request = RMail::Message.new()
mail_request.header['From'] = FROM
mail_request.header['To'] = request_address
mail_request.header['Subject'] = "Re: confirm #{cookie}"
mail_request.body = ''
store_in_maildir(OUTBOX, mail_request)
end
check_maildir MAILIN
check_maildir SPAM
check_maildir OUTBOX
Dir[MAILIN+"/new/*"].each do |filename|
begin
process_mail filename
File.unlink filename
rescue Exception => e
STDERR.puts "Error when processing #{filename}: #{e}"
end
end
|