annotate .svn/pristine/f2/f2a9863e0b0c87d7fc194bdbb21d53799428f872.svn-base @ 1327:287f201c2802 redmine-2.2-integration

Add italic
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Wed, 19 Jun 2013 20:56:22 +0100
parents 038ba2d95de8
children
rev   line source
Chris@1296 1 #!/usr/bin/env ruby
Chris@1296 2
Chris@1296 3 require 'net/http'
Chris@1296 4 require 'net/https'
Chris@1296 5 require 'uri'
Chris@1296 6 require 'optparse'
Chris@1296 7
Chris@1296 8 module Net
Chris@1296 9 class HTTPS < HTTP
Chris@1296 10 def self.post_form(url, params, headers, options={})
Chris@1296 11 request = Post.new(url.path)
Chris@1296 12 request.form_data = params
Chris@1296 13 request.basic_auth url.user, url.password if url.user
Chris@1296 14 request.initialize_http_header(headers)
Chris@1296 15 http = new(url.host, url.port)
Chris@1296 16 http.use_ssl = (url.scheme == 'https')
Chris@1296 17 if options[:no_check_certificate]
Chris@1296 18 http.verify_mode = OpenSSL::SSL::VERIFY_NONE
Chris@1296 19 end
Chris@1296 20 http.start {|h| h.request(request) }
Chris@1296 21 end
Chris@1296 22 end
Chris@1296 23 end
Chris@1296 24
Chris@1296 25 class RedmineMailHandler
Chris@1296 26 VERSION = '0.2'
Chris@1296 27
Chris@1296 28 attr_accessor :verbose, :issue_attributes, :allow_override, :unknown_user, :no_permission_check, :url, :key, :no_check_certificate
Chris@1296 29
Chris@1296 30 def initialize
Chris@1296 31 self.issue_attributes = {}
Chris@1296 32
Chris@1296 33 optparse = OptionParser.new do |opts|
Chris@1296 34 opts.banner = "Usage: rdm-mailhandler.rb [options] --url=<Redmine URL> --key=<API key>"
Chris@1296 35 opts.separator("")
Chris@1296 36 opts.separator("Reads an email from standard input and forward it to a Redmine server through a HTTP request.")
Chris@1296 37 opts.separator("")
Chris@1296 38 opts.separator("Required arguments:")
Chris@1296 39 opts.on("-u", "--url URL", "URL of the Redmine server") {|v| self.url = v}
Chris@1296 40 opts.on("-k", "--key KEY", "Redmine API key") {|v| self.key = v}
Chris@1296 41 opts.separator("")
Chris@1296 42 opts.separator("General options:")
Chris@1296 43 opts.on("--unknown-user ACTION", "how to handle emails from an unknown user",
Chris@1296 44 "ACTION can be one of the following values:",
Chris@1296 45 "* ignore: email is ignored (default)",
Chris@1296 46 "* accept: accept as anonymous user",
Chris@1296 47 "* create: create a user account") {|v| self.unknown_user = v}
Chris@1296 48 opts.on("--no-permission-check", "disable permission checking when receiving",
Chris@1296 49 "the email") {self.no_permission_check = '1'}
Chris@1296 50 opts.on("--key-file FILE", "path to a file that contains the Redmine",
Chris@1296 51 "API key (use this option instead of --key",
Chris@1296 52 "if you don't the key to appear in the",
Chris@1296 53 "command line)") {|v| read_key_from_file(v)}
Chris@1296 54 opts.on("--no-check-certificate", "do not check server certificate") {self.no_check_certificate = true}
Chris@1296 55 opts.on("-h", "--help", "show this help") {puts opts; exit 1}
Chris@1296 56 opts.on("-v", "--verbose", "show extra information") {self.verbose = true}
Chris@1296 57 opts.on("-V", "--version", "show version information and exit") {puts VERSION; exit}
Chris@1296 58 opts.separator("")
Chris@1296 59 opts.separator("Issue attributes control options:")
Chris@1296 60 opts.on("-p", "--project PROJECT", "identifier of the target project") {|v| self.issue_attributes['project'] = v}
Chris@1296 61 opts.on("-s", "--status STATUS", "name of the target status") {|v| self.issue_attributes['status'] = v}
Chris@1296 62 opts.on("-t", "--tracker TRACKER", "name of the target tracker") {|v| self.issue_attributes['tracker'] = v}
Chris@1296 63 opts.on( "--category CATEGORY", "name of the target category") {|v| self.issue_attributes['category'] = v}
Chris@1296 64 opts.on( "--priority PRIORITY", "name of the target priority") {|v| self.issue_attributes['priority'] = v}
Chris@1296 65 opts.on("-o", "--allow-override ATTRS", "allow email content to override attributes",
Chris@1296 66 "specified by previous options",
Chris@1296 67 "ATTRS is a comma separated list of attributes") {|v| self.allow_override = v}
Chris@1296 68 opts.separator("")
Chris@1296 69 opts.separator("Examples:")
Chris@1296 70 opts.separator("No project specified. Emails MUST contain the 'Project' keyword:")
Chris@1296 71 opts.separator(" rdm-mailhandler.rb --url http://redmine.domain.foo --key secret")
Chris@1296 72 opts.separator("")
Chris@1296 73 opts.separator("Fixed project and default tracker specified, but emails can override")
Chris@1296 74 opts.separator("both tracker and priority attributes using keywords:")
Chris@1296 75 opts.separator(" rdm-mailhandler.rb --url https://domain.foo/redmine --key secret \\")
Chris@1296 76 opts.separator(" --project foo \\")
Chris@1296 77 opts.separator(" --tracker bug \\")
Chris@1296 78 opts.separator(" --allow-override tracker,priority")
Chris@1296 79
Chris@1296 80 opts.summary_width = 27
Chris@1296 81 end
Chris@1296 82 optparse.parse!
Chris@1296 83
Chris@1296 84 unless url && key
Chris@1296 85 puts "Some arguments are missing. Use `rdm-mailhandler.rb --help` for getting help."
Chris@1296 86 exit 1
Chris@1296 87 end
Chris@1296 88 end
Chris@1296 89
Chris@1296 90 def submit(email)
Chris@1296 91 uri = url.gsub(%r{/*$}, '') + '/mail_handler'
Chris@1296 92
Chris@1296 93 headers = { 'User-Agent' => "Redmine mail handler/#{VERSION}" }
Chris@1296 94
Chris@1296 95 data = { 'key' => key, 'email' => email,
Chris@1296 96 'allow_override' => allow_override,
Chris@1296 97 'unknown_user' => unknown_user,
Chris@1296 98 'no_permission_check' => no_permission_check}
Chris@1296 99 issue_attributes.each { |attr, value| data["issue[#{attr}]"] = value }
Chris@1296 100
Chris@1296 101 debug "Posting to #{uri}..."
Chris@1296 102 response = Net::HTTPS.post_form(URI.parse(uri), data, headers, :no_check_certificate => no_check_certificate)
Chris@1296 103 debug "Response received: #{response.code}"
Chris@1296 104
Chris@1296 105 case response.code.to_i
Chris@1296 106 when 403
Chris@1296 107 warn "Request was denied by your Redmine server. " +
Chris@1296 108 "Make sure that 'WS for incoming emails' is enabled in application settings and that you provided the correct API key."
Chris@1296 109 return 77
Chris@1296 110 when 422
Chris@1296 111 warn "Request was denied by your Redmine server. " +
Chris@1296 112 "Possible reasons: email is sent from an invalid email address or is missing some information."
Chris@1296 113 return 77
Chris@1296 114 when 400..499
Chris@1296 115 warn "Request was denied by your Redmine server (#{response.code})."
Chris@1296 116 return 77
Chris@1296 117 when 500..599
Chris@1296 118 warn "Failed to contact your Redmine server (#{response.code})."
Chris@1296 119 return 75
Chris@1296 120 when 201
Chris@1296 121 debug "Proccessed successfully"
Chris@1296 122 return 0
Chris@1296 123 else
Chris@1296 124 return 1
Chris@1296 125 end
Chris@1296 126 end
Chris@1296 127
Chris@1296 128 private
Chris@1296 129
Chris@1296 130 def debug(msg)
Chris@1296 131 puts msg if verbose
Chris@1296 132 end
Chris@1296 133
Chris@1296 134 def read_key_from_file(filename)
Chris@1296 135 begin
Chris@1296 136 self.key = File.read(filename).strip
Chris@1296 137 rescue Exception => e
Chris@1296 138 $stderr.puts "Unable to read the key from #{filename}:\n#{e.message}"
Chris@1296 139 exit 1
Chris@1296 140 end
Chris@1296 141 end
Chris@1296 142 end
Chris@1296 143
Chris@1296 144 handler = RedmineMailHandler.new
Chris@1296 145 exit(handler.submit(STDIN.read))