Chris@0
|
1 #!/usr/bin/env ruby
|
Chris@0
|
2
|
Chris@0
|
3 # == Synopsis
|
Chris@0
|
4 #
|
Chris@0
|
5 # Reads an email from standard input and forward it to a Redmine server
|
Chris@0
|
6 # through a HTTP request.
|
Chris@0
|
7 #
|
Chris@0
|
8 # == Usage
|
Chris@0
|
9 #
|
Chris@0
|
10 # rdm-mailhandler [options] --url=<Redmine URL> --key=<API key>
|
Chris@0
|
11 #
|
Chris@0
|
12 # == Arguments
|
Chris@0
|
13 #
|
Chris@0
|
14 # -u, --url URL of the Redmine server
|
Chris@0
|
15 # -k, --key Redmine API key
|
Chris@0
|
16 #
|
Chris@0
|
17 # General options:
|
Chris@0
|
18 # --unknown-user=ACTION how to handle emails from an unknown user
|
Chris@0
|
19 # ACTION can be one of the following values:
|
Chris@0
|
20 # ignore: email is ignored (default)
|
Chris@0
|
21 # accept: accept as anonymous user
|
Chris@0
|
22 # create: create a user account
|
Chris@0
|
23 # --no-permission-check disable permission checking when receiving
|
Chris@0
|
24 # the email
|
Chris@909
|
25 # --key-file=PATH path to a file that contains the Redmine
|
Chris@909
|
26 # API key (use this option instead of --key
|
Chris@909
|
27 # if you don't the key to appear in the
|
Chris@909
|
28 # command line)
|
Chris@909
|
29 # --no-check-certificate do not check server certificate
|
Chris@0
|
30 # -h, --help show this help
|
Chris@0
|
31 # -v, --verbose show extra information
|
Chris@0
|
32 # -V, --version show version information and exit
|
Chris@0
|
33 #
|
Chris@0
|
34 # Issue attributes control options:
|
Chris@0
|
35 # -p, --project=PROJECT identifier of the target project
|
Chris@0
|
36 # -s, --status=STATUS name of the target status
|
Chris@0
|
37 # -t, --tracker=TRACKER name of the target tracker
|
Chris@0
|
38 # --category=CATEGORY name of the target category
|
Chris@0
|
39 # --priority=PRIORITY name of the target priority
|
Chris@0
|
40 # -o, --allow-override=ATTRS allow email content to override attributes
|
Chris@0
|
41 # specified by previous options
|
Chris@0
|
42 # ATTRS is a comma separated list of attributes
|
Chris@0
|
43 #
|
Chris@0
|
44 # == Examples
|
Chris@0
|
45 # No project specified. Emails MUST contain the 'Project' keyword:
|
Chris@0
|
46 #
|
Chris@0
|
47 # rdm-mailhandler --url http://redmine.domain.foo --key secret
|
Chris@0
|
48 #
|
Chris@0
|
49 # Fixed project and default tracker specified, but emails can override
|
Chris@0
|
50 # both tracker and priority attributes using keywords:
|
Chris@0
|
51 #
|
Chris@0
|
52 # rdm-mailhandler --url https://domain.foo/redmine --key secret \\
|
Chris@0
|
53 # --project foo \\
|
Chris@0
|
54 # --tracker bug \\
|
Chris@0
|
55 # --allow-override tracker,priority
|
Chris@0
|
56
|
Chris@0
|
57 require 'net/http'
|
Chris@0
|
58 require 'net/https'
|
Chris@0
|
59 require 'uri'
|
Chris@0
|
60 require 'getoptlong'
|
Chris@0
|
61 require 'rdoc/usage'
|
Chris@0
|
62
|
Chris@0
|
63 module Net
|
Chris@0
|
64 class HTTPS < HTTP
|
Chris@909
|
65 def self.post_form(url, params, headers, options={})
|
Chris@0
|
66 request = Post.new(url.path)
|
Chris@0
|
67 request.form_data = params
|
Chris@0
|
68 request.basic_auth url.user, url.password if url.user
|
Chris@128
|
69 request.initialize_http_header(headers)
|
Chris@0
|
70 http = new(url.host, url.port)
|
Chris@0
|
71 http.use_ssl = (url.scheme == 'https')
|
Chris@909
|
72 if options[:no_check_certificate]
|
Chris@909
|
73 http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
Chris@909
|
74 end
|
Chris@0
|
75 http.start {|h| h.request(request) }
|
Chris@0
|
76 end
|
Chris@0
|
77 end
|
Chris@0
|
78 end
|
Chris@0
|
79
|
Chris@0
|
80 class RedmineMailHandler
|
Chris@0
|
81 VERSION = '0.1'
|
Chris@0
|
82
|
Chris@909
|
83 attr_accessor :verbose, :issue_attributes, :allow_override, :unknown_user, :no_permission_check, :url, :key, :no_check_certificate
|
Chris@0
|
84
|
Chris@0
|
85 def initialize
|
Chris@0
|
86 self.issue_attributes = {}
|
Chris@0
|
87
|
Chris@0
|
88 opts = GetoptLong.new(
|
Chris@0
|
89 [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
|
Chris@0
|
90 [ '--version', '-V', GetoptLong::NO_ARGUMENT ],
|
Chris@0
|
91 [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ],
|
Chris@0
|
92 [ '--url', '-u', GetoptLong::REQUIRED_ARGUMENT ],
|
Chris@0
|
93 [ '--key', '-k', GetoptLong::REQUIRED_ARGUMENT],
|
Chris@909
|
94 [ '--key-file', GetoptLong::REQUIRED_ARGUMENT],
|
Chris@0
|
95 [ '--project', '-p', GetoptLong::REQUIRED_ARGUMENT ],
|
Chris@0
|
96 [ '--status', '-s', GetoptLong::REQUIRED_ARGUMENT ],
|
Chris@0
|
97 [ '--tracker', '-t', GetoptLong::REQUIRED_ARGUMENT],
|
Chris@0
|
98 [ '--category', GetoptLong::REQUIRED_ARGUMENT],
|
Chris@0
|
99 [ '--priority', GetoptLong::REQUIRED_ARGUMENT],
|
Chris@0
|
100 [ '--allow-override', '-o', GetoptLong::REQUIRED_ARGUMENT],
|
Chris@0
|
101 [ '--unknown-user', GetoptLong::REQUIRED_ARGUMENT],
|
Chris@909
|
102 [ '--no-permission-check', GetoptLong::NO_ARGUMENT],
|
Chris@909
|
103 [ '--no-check-certificate', GetoptLong::NO_ARGUMENT]
|
Chris@0
|
104 )
|
Chris@0
|
105
|
Chris@0
|
106 opts.each do |opt, arg|
|
Chris@0
|
107 case opt
|
Chris@0
|
108 when '--url'
|
Chris@0
|
109 self.url = arg.dup
|
Chris@0
|
110 when '--key'
|
Chris@0
|
111 self.key = arg.dup
|
Chris@909
|
112 when '--key-file'
|
Chris@909
|
113 begin
|
Chris@909
|
114 self.key = File.read(arg).strip
|
Chris@909
|
115 rescue Exception => e
|
Chris@909
|
116 $stderr.puts "Unable to read the key from #{arg}: #{e.message}"
|
Chris@909
|
117 exit 1
|
Chris@909
|
118 end
|
Chris@0
|
119 when '--help'
|
Chris@0
|
120 usage
|
Chris@0
|
121 when '--verbose'
|
Chris@0
|
122 self.verbose = true
|
Chris@0
|
123 when '--version'
|
Chris@0
|
124 puts VERSION; exit
|
Chris@0
|
125 when '--project', '--status', '--tracker', '--category', '--priority'
|
Chris@0
|
126 self.issue_attributes[opt.gsub(%r{^\-\-}, '')] = arg.dup
|
Chris@0
|
127 when '--allow-override'
|
Chris@0
|
128 self.allow_override = arg.dup
|
Chris@0
|
129 when '--unknown-user'
|
Chris@0
|
130 self.unknown_user = arg.dup
|
Chris@0
|
131 when '--no-permission-check'
|
Chris@0
|
132 self.no_permission_check = '1'
|
Chris@909
|
133 when '--no-check-certificate'
|
Chris@909
|
134 self.no_check_certificate = true
|
Chris@0
|
135 end
|
Chris@0
|
136 end
|
Chris@0
|
137
|
Chris@0
|
138 RDoc.usage if url.nil?
|
Chris@0
|
139 end
|
Chris@0
|
140
|
Chris@0
|
141 def submit(email)
|
Chris@0
|
142 uri = url.gsub(%r{/*$}, '') + '/mail_handler'
|
Chris@0
|
143
|
Chris@128
|
144 headers = { 'User-Agent' => "Redmine mail handler/#{VERSION}" }
|
Chris@128
|
145
|
Chris@0
|
146 data = { 'key' => key, 'email' => email,
|
Chris@0
|
147 'allow_override' => allow_override,
|
Chris@0
|
148 'unknown_user' => unknown_user,
|
Chris@0
|
149 'no_permission_check' => no_permission_check}
|
Chris@0
|
150 issue_attributes.each { |attr, value| data["issue[#{attr}]"] = value }
|
Chris@0
|
151
|
Chris@0
|
152 debug "Posting to #{uri}..."
|
Chris@909
|
153 response = Net::HTTPS.post_form(URI.parse(uri), data, headers, :no_check_certificate => no_check_certificate)
|
Chris@0
|
154 debug "Response received: #{response.code}"
|
Chris@0
|
155
|
Chris@0
|
156 case response.code.to_i
|
Chris@0
|
157 when 403
|
Chris@0
|
158 warn "Request was denied by your Redmine server. " +
|
Chris@0
|
159 "Make sure that 'WS for incoming emails' is enabled in application settings and that you provided the correct API key."
|
Chris@0
|
160 return 77
|
Chris@0
|
161 when 422
|
Chris@0
|
162 warn "Request was denied by your Redmine server. " +
|
Chris@0
|
163 "Possible reasons: email is sent from an invalid email address or is missing some information."
|
Chris@0
|
164 return 77
|
Chris@0
|
165 when 400..499
|
Chris@0
|
166 warn "Request was denied by your Redmine server (#{response.code})."
|
Chris@0
|
167 return 77
|
Chris@0
|
168 when 500..599
|
Chris@0
|
169 warn "Failed to contact your Redmine server (#{response.code})."
|
Chris@0
|
170 return 75
|
Chris@0
|
171 when 201
|
Chris@0
|
172 debug "Proccessed successfully"
|
Chris@0
|
173 return 0
|
Chris@0
|
174 else
|
Chris@0
|
175 return 1
|
Chris@0
|
176 end
|
Chris@0
|
177 end
|
Chris@0
|
178
|
Chris@0
|
179 private
|
Chris@0
|
180
|
Chris@0
|
181 def debug(msg)
|
Chris@0
|
182 puts msg if verbose
|
Chris@0
|
183 end
|
Chris@0
|
184 end
|
Chris@0
|
185
|
Chris@0
|
186 handler = RedmineMailHandler.new
|
Chris@0
|
187 exit(handler.submit(STDIN.read))
|