@@ -57,8 +57,7 | |||||
57 | require 'net/http' |
|
57 | require 'net/http' | |
58 | require 'net/https' |
|
58 | require 'net/https' | |
59 | require 'uri' |
|
59 | require 'uri' | |
60 |
require ' |
|
60 | require 'optparse' | |
61 | require 'rdoc/usage' |
|
|||
62 |
|
61 | |||
63 | module Net |
|
62 | module Net | |
64 | class HTTPS < HTTP |
|
63 | class HTTPS < HTTP | |
@@ -78,64 +77,68 module Net | |||||
78 | end |
|
77 | end | |
79 |
|
78 | |||
80 | class RedmineMailHandler |
|
79 | class RedmineMailHandler | |
81 |
VERSION = '0. |
|
80 | VERSION = '0.2' | |
82 |
|
81 | |||
83 | attr_accessor :verbose, :issue_attributes, :allow_override, :unknown_user, :no_permission_check, :url, :key, :no_check_certificate |
|
82 | attr_accessor :verbose, :issue_attributes, :allow_override, :unknown_user, :no_permission_check, :url, :key, :no_check_certificate | |
84 |
|
83 | |||
85 | def initialize |
|
84 | def initialize | |
86 | self.issue_attributes = {} |
|
85 | self.issue_attributes = {} | |
87 |
|
86 | |||
88 | opts = GetoptLong.new( |
|
87 | optparse = OptionParser.new do |opts| | |
89 | [ '--help', '-h', GetoptLong::NO_ARGUMENT ], |
|
88 | opts.banner = "Usage: rdm-mailhandler.rb [options] --url=<Redmine URL> --key=<API key>" | |
90 | [ '--version', '-V', GetoptLong::NO_ARGUMENT ], |
|
89 | opts.separator("") | |
91 | [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ], |
|
90 | opts.separator("Reads an email from standard input and forward it to a Redmine server through a HTTP request.") | |
92 | [ '--url', '-u', GetoptLong::REQUIRED_ARGUMENT ], |
|
91 | opts.separator("") | |
93 | [ '--key', '-k', GetoptLong::REQUIRED_ARGUMENT], |
|
92 | opts.separator("Required arguments:") | |
94 | [ '--key-file', GetoptLong::REQUIRED_ARGUMENT], |
|
93 | opts.on("-u", "--url URL", "URL of the Redmine server") {|v| self.url = v} | |
95 | [ '--project', '-p', GetoptLong::REQUIRED_ARGUMENT ], |
|
94 | opts.on("-k", "--key KEY", "Redmine API key") {|v| self.key = v} | |
96 | [ '--status', '-s', GetoptLong::REQUIRED_ARGUMENT ], |
|
95 | opts.separator("") | |
97 | [ '--tracker', '-t', GetoptLong::REQUIRED_ARGUMENT], |
|
96 | opts.separator("General options:") | |
98 | [ '--category', GetoptLong::REQUIRED_ARGUMENT], |
|
97 | opts.on("--unknown-user ACTION", "how to handle emails from an unknown user", | |
99 | [ '--priority', GetoptLong::REQUIRED_ARGUMENT], |
|
98 | "ACTION can be one of the following values:", | |
100 | [ '--allow-override', '-o', GetoptLong::REQUIRED_ARGUMENT], |
|
99 | "* ignore: email is ignored (default)", | |
101 | [ '--unknown-user', GetoptLong::REQUIRED_ARGUMENT], |
|
100 | "* accept: accept as anonymous user", | |
102 | [ '--no-permission-check', GetoptLong::NO_ARGUMENT], |
|
101 | "* create: create a user account") {|v| self.unknown_user = v} | |
103 | [ '--no-check-certificate', GetoptLong::NO_ARGUMENT] |
|
102 | opts.on("--no-permission-check", "disable permission checking when receiving", | |
104 | ) |
|
103 | "the email") {self.no_permission_check = '1'} | |
105 |
|
104 | opts.on("--key-file FILE", "path to a file that contains the Redmine", | ||
106 | opts.each do |opt, arg| |
|
105 | "API key (use this option instead of --key", | |
107 | case opt |
|
106 | "if you don't the key to appear in the", | |
108 | when '--url' |
|
107 | "command line)") {|v| read_key_from_file(v)} | |
109 | self.url = arg.dup |
|
108 | opts.on("--no-check-certificate", "do not check server certificate") {self.no_check_certificate = true} | |
110 | when '--key' |
|
109 | opts.on("-h", "--help", "show this help") {puts opts; exit 1} | |
111 | self.key = arg.dup |
|
110 | opts.on("-v", "--verbose", "show extra information") {self.verbose = true} | |
112 | when '--key-file' |
|
111 | opts.on("-V", "--version", "show version information and exit") {puts VERSION; exit} | |
113 | begin |
|
112 | opts.separator("") | |
114 | self.key = File.read(arg).strip |
|
113 | opts.separator("Issue attributes control options:") | |
115 | rescue Exception => e |
|
114 | opts.on("-p", "--project PROJECT", "identifier of the target project") {|v| self.issue_attributes['project'] = v} | |
116 | $stderr.puts "Unable to read the key from #{arg}: #{e.message}" |
|
115 | opts.on("-s", "--status STATUS", "name of the target status") {|v| self.issue_attributes['status'] = v} | |
117 | exit 1 |
|
116 | opts.on("-t", "--tracker TRACKER", "name of the target tracker") {|v| self.issue_attributes['tracker'] = v} | |
118 | end |
|
117 | opts.on( "--category CATEGORY", "name of the target category") {|v| self.issue_attributes['category'] = v} | |
119 | when '--help' |
|
118 | opts.on( "--priority PRIORITY", "name of the target priority") {|v| self.issue_attributes['priority'] = v} | |
120 | usage |
|
119 | opts.on("-o", "--allow-override ATTRS", "allow email content to override attributes", | |
121 | when '--verbose' |
|
120 | "specified by previous options", | |
122 | self.verbose = true |
|
121 | "ATTRS is a comma separated list of attributes") {|v| self.allow_override = v} | |
123 | when '--version' |
|
122 | opts.separator("") | |
124 | puts VERSION; exit |
|
123 | opts.separator("Examples:") | |
125 | when '--project', '--status', '--tracker', '--category', '--priority' |
|
124 | opts.separator("No project specified. Emails MUST contain the 'Project' keyword:") | |
126 | self.issue_attributes[opt.gsub(%r{^\-\-}, '')] = arg.dup |
|
125 | opts.separator(" rdm-mailhandler.rb --url http://redmine.domain.foo --key secret") | |
127 | when '--allow-override' |
|
126 | opts.separator("") | |
128 | self.allow_override = arg.dup |
|
127 | opts.separator("Fixed project and default tracker specified, but emails can override") | |
129 | when '--unknown-user' |
|
128 | opts.separator("both tracker and priority attributes using keywords:") | |
130 | self.unknown_user = arg.dup |
|
129 | opts.separator(" rdm-mailhandler.rb --url https://domain.foo/redmine --key secret \\") | |
131 | when '--no-permission-check' |
|
130 | opts.separator(" --project foo \\") | |
132 | self.no_permission_check = '1' |
|
131 | opts.separator(" --tracker bug \\") | |
133 | when '--no-check-certificate' |
|
132 | opts.separator(" --allow-override tracker,priority") | |
134 | self.no_check_certificate = true |
|
133 | ||
135 | end |
|
134 | opts.summary_width = 27 | |
136 | end |
|
135 | end | |
|
136 | optparse.parse! | |||
137 |
|
137 | |||
138 | RDoc.usage if url.nil? |
|
138 | unless url && key | |
|
139 | puts "Some arguments are missing. Use `rdm-mailhandler.rb --help` for getting help." | |||
|
140 | exit 1 | |||
|
141 | end | |||
139 | end |
|
142 | end | |
140 |
|
143 | |||
141 | def submit(email) |
|
144 | def submit(email) | |
@@ -181,6 +184,15 class RedmineMailHandler | |||||
181 | def debug(msg) |
|
184 | def debug(msg) | |
182 | puts msg if verbose |
|
185 | puts msg if verbose | |
183 | end |
|
186 | end | |
|
187 | ||||
|
188 | def read_key_from_file(filename) | |||
|
189 | begin | |||
|
190 | self.key = File.read(filename).strip | |||
|
191 | rescue Exception => e | |||
|
192 | $stderr.puts "Unable to read the key from #{filename}:\n#{e.message}" | |||
|
193 | exit 1 | |||
|
194 | end | |||
|
195 | end | |||
184 | end |
|
196 | end | |
185 |
|
197 | |||
186 | handler = RedmineMailHandler.new |
|
198 | handler = RedmineMailHandler.new |
General Comments 0
You need to be logged in to leave comments.
Login now