1 | #!/usr/bin/env ruby |
---|
2 | |
---|
3 | require 'English' |
---|
4 | |
---|
5 | original_argv = ARGV.dup |
---|
6 | argv = [] |
---|
7 | |
---|
8 | found_include_option = false |
---|
9 | while (arg = original_argv.shift) |
---|
10 | if found_include_option |
---|
11 | $LOAD_PATH.unshift(arg) |
---|
12 | found_include_option = false |
---|
13 | else |
---|
14 | case arg |
---|
15 | when "-I", "--include" |
---|
16 | found_include_option = true |
---|
17 | when /\A-I/, /\A--include=?/ |
---|
18 | path = $POSTMATCH |
---|
19 | $LOAD_PATH.unshift(path) unless path.empty? |
---|
20 | else |
---|
21 | argv << arg |
---|
22 | end |
---|
23 | end |
---|
24 | end |
---|
25 | |
---|
26 | def extract_email_address(address) |
---|
27 | if /<(.+?)>/ =~ address |
---|
28 | $1 |
---|
29 | else |
---|
30 | address |
---|
31 | end |
---|
32 | end |
---|
33 | |
---|
34 | def sendmail(to, from, mail, server=nil, port=nil) |
---|
35 | server ||= "localhost" |
---|
36 | from = extract_email_address(from) |
---|
37 | to = to.collect {|address| extract_email_address(address)} |
---|
38 | Net::SMTP.start(server, port) do |smtp| |
---|
39 | smtp.open_message_stream(from, to) do |f| |
---|
40 | f.print(mail) |
---|
41 | end |
---|
42 | end |
---|
43 | end |
---|
44 | |
---|
45 | begin |
---|
46 | require 'svn/commit-mailer' |
---|
47 | Svn::Locale.set |
---|
48 | Svn::CommitMailer.run(argv) |
---|
49 | rescue Exception => error |
---|
50 | require 'net/smtp' |
---|
51 | require 'socket' |
---|
52 | |
---|
53 | to = [] |
---|
54 | subject = "Error" |
---|
55 | from = "#{ENV['USER']}@#{Socket.gethostname}" |
---|
56 | server = nil |
---|
57 | port = nil |
---|
58 | begin |
---|
59 | begin |
---|
60 | Svn::CommitMailer |
---|
61 | rescue NameError |
---|
62 | raise OptionParser::ParseError |
---|
63 | end |
---|
64 | _, _, _to, options = Svn::CommitMailer.parse(argv) |
---|
65 | to = [_to] |
---|
66 | to = options.error_to unless options.error_to.empty? |
---|
67 | from = options.from || from |
---|
68 | subject = "#{options.name}: #{subject}" if options.name |
---|
69 | server = options.server |
---|
70 | port = options.port |
---|
71 | rescue OptionParser::MissingArgument |
---|
72 | argv.delete_if {|arg| $!.args.include?(arg)} |
---|
73 | retry |
---|
74 | rescue OptionParser::ParseError |
---|
75 | if to.empty? |
---|
76 | _, _, _to, *_ = ARGV.reject {|arg| /^-/.match(arg)} |
---|
77 | to = [_to] |
---|
78 | end |
---|
79 | end |
---|
80 | |
---|
81 | detail = <<-EOM |
---|
82 | #{error.class}: #{error.message} |
---|
83 | #{error.backtrace.join("\n")} |
---|
84 | EOM |
---|
85 | to = to.compact |
---|
86 | if to.empty? |
---|
87 | STDERR.puts detail |
---|
88 | else |
---|
89 | sendmail(to, from, <<-MAIL, server, port) |
---|
90 | MIME-Version: 1.0 |
---|
91 | Content-Type: text/plain; charset=us-ascii |
---|
92 | Content-Transfer-Encoding: 7bit |
---|
93 | From: #{from} |
---|
94 | To: #{to.join(', ')} |
---|
95 | Subject: #{subject} |
---|
96 | Date: #{Time.now.rfc2822} |
---|
97 | |
---|
98 | #{detail} |
---|
99 | MAIL |
---|
100 | end |
---|
101 | end |
---|