Fix compiler warning due to missing function prototype.
[svn.git] / tools / hook-scripts / commit-email.rb
blob631c8bf454b8c3358784a073cacfa12ea6041314
1 #!/usr/bin/env ruby
3 require 'English'
5 original_argv = ARGV.dup
6 argv = []
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
26 def extract_email_address(address)
27   if /<(.+?)>/ =~ address
28     $1
29   else
30     address
31   end
32 end
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
45 begin
46   require 'svn/commit-mailer'
47   Svn::CommitMailer.run(argv)
48 rescue Exception => error
49   require 'net/smtp'
50   require 'socket'
52   to = []
53   subject = "Error"
54   from = "#{ENV['USER']}@#{Socket.gethostname}"
55   server = nil
56   port = nil
57   begin
58     begin
59       Svn::CommitMailer
60     rescue NameError
61       raise OptionParser::ParseError
62     end
63     _, _, _to, options = Svn::CommitMailer.parse(argv)
64     to = [_to]
65     to = options.error_to unless options.error_to.empty?
66     from = options.from || from
67     subject = "#{options.name}: #{subject}" if options.name
68     server = options.server
69     port = options.port
70   rescue OptionParser::MissingArgument
71     argv.delete_if {|arg| $!.args.include?(arg)}
72     retry
73   rescue OptionParser::ParseError
74     if to.empty?
75       _, _, _to, *_ = ARGV.reject {|arg| /^-/.match(arg)}
76       to = [_to]
77     end
78   end
80   detail = <<-EOM
81 #{error.class}: #{error.message}
82 #{error.backtrace.join("\n")}
83 EOM
84   to = to.compact
85   if to.empty?
86     STDERR.puts detail
87   else
88     sendmail(to, from, <<-MAIL, server, port)
89 MIME-Version: 1.0
90 Content-Type: text/plain; charset=us-ascii
91 Content-Transfer-Encoding: 7bit
92 From: #{from}
93 To: #{to.join(', ')}
94 Subject: #{subject}
95 Date: #{Time.now.rfc2822}
97 #{detail}
98 MAIL
99   end