Mercurial > hg > soundsoftware-site
comparison extra/mail_handler/.svn/text-base/rdm-mailhandler.rb.svn-base @ 0:513646585e45
* Import Redmine trunk SVN rev 3859
author | Chris Cannam |
---|---|
date | Fri, 23 Jul 2010 15:52:44 +0100 |
parents | |
children | 07fa8a8b56a8 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:513646585e45 |
---|---|
1 #!/usr/bin/env ruby | |
2 | |
3 # == Synopsis | |
4 # | |
5 # Reads an email from standard input and forward it to a Redmine 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 # --unknown-user=ACTION how to handle emails from an unknown user | |
19 # ACTION can be one of the following values: | |
20 # ignore: email is ignored (default) | |
21 # accept: accept as anonymous user | |
22 # create: create a user account | |
23 # --no-permission-check disable permission checking when receiving | |
24 # the email | |
25 # -h, --help show this help | |
26 # -v, --verbose show extra information | |
27 # -V, --version show version information and exit | |
28 # | |
29 # Issue attributes control options: | |
30 # -p, --project=PROJECT identifier of the target project | |
31 # -s, --status=STATUS name of the target status | |
32 # -t, --tracker=TRACKER name of the target tracker | |
33 # --category=CATEGORY name of the target category | |
34 # --priority=PRIORITY name of the target priority | |
35 # -o, --allow-override=ATTRS allow email content to override attributes | |
36 # specified by previous options | |
37 # ATTRS is a comma separated list of attributes | |
38 # | |
39 # == Examples | |
40 # No project specified. Emails MUST contain the 'Project' keyword: | |
41 # | |
42 # rdm-mailhandler --url http://redmine.domain.foo --key secret | |
43 # | |
44 # Fixed project and default tracker specified, but emails can override | |
45 # both tracker and priority attributes using keywords: | |
46 # | |
47 # rdm-mailhandler --url https://domain.foo/redmine --key secret \\ | |
48 # --project foo \\ | |
49 # --tracker bug \\ | |
50 # --allow-override tracker,priority | |
51 | |
52 require 'net/http' | |
53 require 'net/https' | |
54 require 'uri' | |
55 require 'getoptlong' | |
56 require 'rdoc/usage' | |
57 | |
58 module Net | |
59 class HTTPS < HTTP | |
60 def self.post_form(url, params) | |
61 request = Post.new(url.path) | |
62 request.form_data = params | |
63 request.basic_auth url.user, url.password if url.user | |
64 http = new(url.host, url.port) | |
65 http.use_ssl = (url.scheme == 'https') | |
66 http.start {|h| h.request(request) } | |
67 end | |
68 end | |
69 end | |
70 | |
71 class RedmineMailHandler | |
72 VERSION = '0.1' | |
73 | |
74 attr_accessor :verbose, :issue_attributes, :allow_override, :unknown_user, :no_permission_check, :url, :key | |
75 | |
76 def initialize | |
77 self.issue_attributes = {} | |
78 | |
79 opts = GetoptLong.new( | |
80 [ '--help', '-h', GetoptLong::NO_ARGUMENT ], | |
81 [ '--version', '-V', GetoptLong::NO_ARGUMENT ], | |
82 [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ], | |
83 [ '--url', '-u', GetoptLong::REQUIRED_ARGUMENT ], | |
84 [ '--key', '-k', GetoptLong::REQUIRED_ARGUMENT], | |
85 [ '--project', '-p', GetoptLong::REQUIRED_ARGUMENT ], | |
86 [ '--status', '-s', GetoptLong::REQUIRED_ARGUMENT ], | |
87 [ '--tracker', '-t', GetoptLong::REQUIRED_ARGUMENT], | |
88 [ '--category', GetoptLong::REQUIRED_ARGUMENT], | |
89 [ '--priority', GetoptLong::REQUIRED_ARGUMENT], | |
90 [ '--allow-override', '-o', GetoptLong::REQUIRED_ARGUMENT], | |
91 [ '--unknown-user', GetoptLong::REQUIRED_ARGUMENT], | |
92 [ '--no-permission-check', GetoptLong::NO_ARGUMENT] | |
93 ) | |
94 | |
95 opts.each do |opt, arg| | |
96 case opt | |
97 when '--url' | |
98 self.url = arg.dup | |
99 when '--key' | |
100 self.key = arg.dup | |
101 when '--help' | |
102 usage | |
103 when '--verbose' | |
104 self.verbose = true | |
105 when '--version' | |
106 puts VERSION; exit | |
107 when '--project', '--status', '--tracker', '--category', '--priority' | |
108 self.issue_attributes[opt.gsub(%r{^\-\-}, '')] = arg.dup | |
109 when '--allow-override' | |
110 self.allow_override = arg.dup | |
111 when '--unknown-user' | |
112 self.unknown_user = arg.dup | |
113 when '--no-permission-check' | |
114 self.no_permission_check = '1' | |
115 end | |
116 end | |
117 | |
118 RDoc.usage if url.nil? | |
119 end | |
120 | |
121 def submit(email) | |
122 uri = url.gsub(%r{/*$}, '') + '/mail_handler' | |
123 | |
124 data = { 'key' => key, 'email' => email, | |
125 'allow_override' => allow_override, | |
126 'unknown_user' => unknown_user, | |
127 'no_permission_check' => no_permission_check} | |
128 issue_attributes.each { |attr, value| data["issue[#{attr}]"] = value } | |
129 | |
130 debug "Posting to #{uri}..." | |
131 response = Net::HTTPS.post_form(URI.parse(uri), data) | |
132 debug "Response received: #{response.code}" | |
133 | |
134 case response.code.to_i | |
135 when 403 | |
136 warn "Request was denied by your Redmine server. " + | |
137 "Make sure that 'WS for incoming emails' is enabled in application settings and that you provided the correct API key." | |
138 return 77 | |
139 when 422 | |
140 warn "Request was denied by your Redmine server. " + | |
141 "Possible reasons: email is sent from an invalid email address or is missing some information." | |
142 return 77 | |
143 when 400..499 | |
144 warn "Request was denied by your Redmine server (#{response.code})." | |
145 return 77 | |
146 when 500..599 | |
147 warn "Failed to contact your Redmine server (#{response.code})." | |
148 return 75 | |
149 when 201 | |
150 debug "Proccessed successfully" | |
151 return 0 | |
152 else | |
153 return 1 | |
154 end | |
155 end | |
156 | |
157 private | |
158 | |
159 def debug(msg) | |
160 puts msg if verbose | |
161 end | |
162 end | |
163 | |
164 handler = RedmineMailHandler.new | |
165 exit(handler.submit(STDIN.read)) |