##// END OF EJS Templates
Use RDoc.usage...
Jean-Philippe Lang -
r1865:9afaf26d66e5
parent child
Show More
@@ -1,125 +1,126
1 1 #!/usr/bin/ruby
2 2
3 # rdm-mailhandler
3 # == Synopsis
4 #
4 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 44 require 'net/http'
8 45 require 'net/https'
9 46 require 'uri'
10 47 require 'getoptlong'
48 require 'rdoc/usage'
11 49
12 50 module Net
13 51 class HTTPS < HTTP
14 52 def self.post_form(url, params)
15 53 request = Post.new(url.path)
16 54 request.form_data = params
17 55 request.basic_auth url.user, url.password if url.user
18 56 http = new(url.host, url.port)
19 57 http.use_ssl = (url.scheme == 'https')
20 58 http.start {|h| h.request(request) }
21 59 end
22 60 end
23 61 end
24 62
25 63 class RedmineMailHandler
26 64 VERSION = '0.1'
27 65
28 66 attr_accessor :verbose, :issue_attributes, :allow_override, :url, :key
29 67
30 68 def initialize
31 69 self.issue_attributes = {}
32 70
33 71 opts = GetoptLong.new(
34 72 [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
35 73 [ '--version', '-V', GetoptLong::NO_ARGUMENT ],
36 74 [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ],
37 75 [ '--url', '-u', GetoptLong::REQUIRED_ARGUMENT ],
38 76 [ '--key', '-k', GetoptLong::REQUIRED_ARGUMENT],
39 77 [ '--project', '-p', GetoptLong::REQUIRED_ARGUMENT ],
40 78 [ '--tracker', '-t', GetoptLong::REQUIRED_ARGUMENT],
41 79 [ '--category', GetoptLong::REQUIRED_ARGUMENT],
42 80 [ '--priority', GetoptLong::REQUIRED_ARGUMENT],
43 81 [ '--allow-override', '-o', GetoptLong::REQUIRED_ARGUMENT]
44 82 )
45 83
46 84 opts.each do |opt, arg|
47 85 case opt
48 86 when '--url'
49 87 self.url = arg.dup
50 88 when '--key'
51 89 self.key = arg.dup
52 90 when '--help'
53 91 usage
54 92 when '--verbose'
55 93 self.verbose = true
56 94 when '--version'
57 95 puts VERSION; exit
58 96 when '--project', '--tracker', '--category', '--priority'
59 97 self.issue_attributes[opt.gsub(%r{^\-\-}, '')] = arg.dup
60 98 when '--allow-override'
61 99 self.allow_override = arg.dup
62 100 end
63 101 end
64 102
65 usage if url.nil?
103 RDoc.usage if url.nil?
66 104 end
67 105
68 106 def submit(email)
69 107 uri = url.gsub(%r{/*$}, '') + '/mail_handler'
70 108
71 109 data = { 'key' => key, 'email' => email, 'allow_override' => allow_override }
72 110 issue_attributes.each { |attr, value| data["issue[#{attr}]"] = value }
73 111
74 112 debug "Posting to #{uri}..."
75 113 response = Net::HTTPS.post_form(URI.parse(uri), data)
76 114 debug "Response received: #{response.code}"
77 115 response.code == 201 ? 0 : 1
78 116 end
79 117
80 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 120 def debug(msg)
120 121 puts msg if verbose
121 122 end
122 123 end
123 124
124 125 handler = RedmineMailHandler.new
125 126 handler.submit(STDIN.read)
General Comments 0
You need to be logged in to leave comments. Login now