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