##// END OF EJS Templates
Use RDoc.usage...
Jean-Philippe Lang -
r1865:9afaf26d66e5
parent child
Show More
@@ -1,125 +1,126
1 #!/usr/bin/ruby
1 #!/usr/bin/ruby
2
2
3 # rdm-mailhandler
3 # == Synopsis
4 #
4 # Reads an email from standard input and forward it to a Redmine server
5 # Reads an email from standard input and forward it to a Redmine server
5 # Can be used from a remote mail server
6 # through a HTTP request.
7 #
8 # == Usage
9 #
10 # rdm-mailhandler [options] --url=<Redmine URL> --key=<API key>
11 #
12 # == Arguments
13 #
14 # -u, --url URL of the Redmine server
15 # -k, --key Redmine API key
16 #
17 # General options:
18 # -h, --help show this help
19 # -v, --verbose show extra information
20 # -V, --version show version information and exit
21 #
22 # Issue attributes control options:
23 # -p, --project=PROJECT identifier of the target project
24 # -t, --tracker=TRACKER name of the target tracker
25 # --category=CATEGORY name of the target category
26 # --priority=PRIORITY name of the target priority
27 # -o, --allow-override=ATTRS allow email content to override attributes
28 # specified by previous options
29 # ATTRS is a comma separated list of attributes
30 #
31 # == Examples
32 # No project specified. Emails MUST contain the 'Project' keyword:
33 #
34 # rdm-mailhandler --url http://redmine.domain.foo --key secret
35 #
36 # Fixed project and default tracker specified, but emails can override
37 # both tracker and priority attributes using keywords:
38 #
39 # rdm-mailhandler --url https://domain.foo/redmine --key secret \\
40 # --project foo \\
41 # --tracker bug \\
42 # --allow-override tracker,priority
6
43
7 require 'net/http'
44 require 'net/http'
8 require 'net/https'
45 require 'net/https'
9 require 'uri'
46 require 'uri'
10 require 'getoptlong'
47 require 'getoptlong'
48 require 'rdoc/usage'
11
49
12 module Net
50 module Net
13 class HTTPS < HTTP
51 class HTTPS < HTTP
14 def self.post_form(url, params)
52 def self.post_form(url, params)
15 request = Post.new(url.path)
53 request = Post.new(url.path)
16 request.form_data = params
54 request.form_data = params
17 request.basic_auth url.user, url.password if url.user
55 request.basic_auth url.user, url.password if url.user
18 http = new(url.host, url.port)
56 http = new(url.host, url.port)
19 http.use_ssl = (url.scheme == 'https')
57 http.use_ssl = (url.scheme == 'https')
20 http.start {|h| h.request(request) }
58 http.start {|h| h.request(request) }
21 end
59 end
22 end
60 end
23 end
61 end
24
62
25 class RedmineMailHandler
63 class RedmineMailHandler
26 VERSION = '0.1'
64 VERSION = '0.1'
27
65
28 attr_accessor :verbose, :issue_attributes, :allow_override, :url, :key
66 attr_accessor :verbose, :issue_attributes, :allow_override, :url, :key
29
67
30 def initialize
68 def initialize
31 self.issue_attributes = {}
69 self.issue_attributes = {}
32
70
33 opts = GetoptLong.new(
71 opts = GetoptLong.new(
34 [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
72 [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
35 [ '--version', '-V', GetoptLong::NO_ARGUMENT ],
73 [ '--version', '-V', GetoptLong::NO_ARGUMENT ],
36 [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ],
74 [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ],
37 [ '--url', '-u', GetoptLong::REQUIRED_ARGUMENT ],
75 [ '--url', '-u', GetoptLong::REQUIRED_ARGUMENT ],
38 [ '--key', '-k', GetoptLong::REQUIRED_ARGUMENT],
76 [ '--key', '-k', GetoptLong::REQUIRED_ARGUMENT],
39 [ '--project', '-p', GetoptLong::REQUIRED_ARGUMENT ],
77 [ '--project', '-p', GetoptLong::REQUIRED_ARGUMENT ],
40 [ '--tracker', '-t', GetoptLong::REQUIRED_ARGUMENT],
78 [ '--tracker', '-t', GetoptLong::REQUIRED_ARGUMENT],
41 [ '--category', GetoptLong::REQUIRED_ARGUMENT],
79 [ '--category', GetoptLong::REQUIRED_ARGUMENT],
42 [ '--priority', GetoptLong::REQUIRED_ARGUMENT],
80 [ '--priority', GetoptLong::REQUIRED_ARGUMENT],
43 [ '--allow-override', '-o', GetoptLong::REQUIRED_ARGUMENT]
81 [ '--allow-override', '-o', GetoptLong::REQUIRED_ARGUMENT]
44 )
82 )
45
83
46 opts.each do |opt, arg|
84 opts.each do |opt, arg|
47 case opt
85 case opt
48 when '--url'
86 when '--url'
49 self.url = arg.dup
87 self.url = arg.dup
50 when '--key'
88 when '--key'
51 self.key = arg.dup
89 self.key = arg.dup
52 when '--help'
90 when '--help'
53 usage
91 usage
54 when '--verbose'
92 when '--verbose'
55 self.verbose = true
93 self.verbose = true
56 when '--version'
94 when '--version'
57 puts VERSION; exit
95 puts VERSION; exit
58 when '--project', '--tracker', '--category', '--priority'
96 when '--project', '--tracker', '--category', '--priority'
59 self.issue_attributes[opt.gsub(%r{^\-\-}, '')] = arg.dup
97 self.issue_attributes[opt.gsub(%r{^\-\-}, '')] = arg.dup
60 when '--allow-override'
98 when '--allow-override'
61 self.allow_override = arg.dup
99 self.allow_override = arg.dup
62 end
100 end
63 end
101 end
64
102
65 usage if url.nil?
103 RDoc.usage if url.nil?
66 end
104 end
67
105
68 def submit(email)
106 def submit(email)
69 uri = url.gsub(%r{/*$}, '') + '/mail_handler'
107 uri = url.gsub(%r{/*$}, '') + '/mail_handler'
70
108
71 data = { 'key' => key, 'email' => email, 'allow_override' => allow_override }
109 data = { 'key' => key, 'email' => email, 'allow_override' => allow_override }
72 issue_attributes.each { |attr, value| data["issue[#{attr}]"] = value }
110 issue_attributes.each { |attr, value| data["issue[#{attr}]"] = value }
73
111
74 debug "Posting to #{uri}..."
112 debug "Posting to #{uri}..."
75 response = Net::HTTPS.post_form(URI.parse(uri), data)
113 response = Net::HTTPS.post_form(URI.parse(uri), data)
76 debug "Response received: #{response.code}"
114 debug "Response received: #{response.code}"
77 response.code == 201 ? 0 : 1
115 response.code == 201 ? 0 : 1
78 end
116 end
79
117
80 private
118 private
81
119
82 def usage
83 puts <<-USAGE
84 Usage: rdm-mailhandler [options] --url=<Redmine URL> --key=<API key>
85 Reads an email from standard input and forward it to a Redmine server
86
87 Required:
88 -u, --url URL of the Redmine server
89 -k, --key Redmine API key
90
91 General options:
92 -h, --help show this help
93 -v, --verbose show extra information
94 -V, --version show version information and exit
95
96 Issue attributes control options:
97 -p, --project=PROJECT identifier of the target project
98 -t, --tracker=TRACKER name of the target tracker
99 --category=CATEGORY name of the target category
100 --priority=PRIORITY name of the target priority
101 -o, --allow-override=ATTRS allow email content to override attributes
102 specified by previous options
103 ATTRS is a comma separated list of attributes
104
105 Examples:
106 # No project specified. Emails MUST contain the 'Project' keyword:
107 rdm-mailhandler --url http://redmine.domain.foo --key secret
108
109 # Fixed project and default tracker specified, but emails can override
110 # both tracker and priority attributes:
111 rdm-mailhandler --url https://domain.foo/redmine --key secret \\
112 --project foo \\
113 --tracker bug \\
114 --allow-override tracker,priority
115 USAGE
116 exit
117 end
118
119 def debug(msg)
120 def debug(msg)
120 puts msg if verbose
121 puts msg if verbose
121 end
122 end
122 end
123 end
123
124
124 handler = RedmineMailHandler.new
125 handler = RedmineMailHandler.new
125 handler.submit(STDIN.read)
126 handler.submit(STDIN.read)
General Comments 0
You need to be logged in to leave comments. Login now