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