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