annotate extra/mail_handler/rdm-mailhandler.rb @ 1600:ed9c467ef922 dockerise

Add hggit extension
author Chris Cannam
date Wed, 23 Aug 2017 11:32:50 +0100
parents e248c7af89ec
children
rev   line source
Chris@0 1 #!/usr/bin/env ruby
Chris@1464 2 # Redmine - project management software
Chris@1494 3 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1464 4 #
Chris@1464 5 # This program is free software; you can redistribute it and/or
Chris@1464 6 # modify it under the terms of the GNU General Public License
Chris@1464 7 # as published by the Free Software Foundation; either version 2
Chris@1464 8 # of the License, or (at your option) any later version.
Chris@1464 9 #
Chris@1464 10 # This program is distributed in the hope that it will be useful,
Chris@1464 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1464 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1464 13 # GNU General Public License for more details.
Chris@1464 14 #
Chris@1464 15 # You should have received a copy of the GNU General Public License
Chris@1464 16 # along with this program; if not, write to the Free Software
Chris@1464 17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 18
Chris@0 19 require 'net/http'
Chris@0 20 require 'net/https'
Chris@0 21 require 'uri'
Chris@1115 22 require 'optparse'
Chris@0 23
Chris@0 24 module Net
Chris@0 25 class HTTPS < HTTP
Chris@909 26 def self.post_form(url, params, headers, options={})
Chris@0 27 request = Post.new(url.path)
Chris@0 28 request.form_data = params
Chris@1294 29 request.initialize_http_header(headers)
Chris@0 30 request.basic_auth url.user, url.password if url.user
Chris@0 31 http = new(url.host, url.port)
Chris@0 32 http.use_ssl = (url.scheme == 'https')
Chris@909 33 if options[:no_check_certificate]
Chris@909 34 http.verify_mode = OpenSSL::SSL::VERIFY_NONE
Chris@909 35 end
Chris@0 36 http.start {|h| h.request(request) }
Chris@0 37 end
Chris@0 38 end
Chris@0 39 end
Chris@0 40
Chris@0 41 class RedmineMailHandler
Chris@1464 42 VERSION = '0.2.3'
Chris@1115 43
Chris@1464 44 attr_accessor :verbose, :issue_attributes, :allow_override, :unknown_user, :default_group, :no_permission_check,
Chris@1464 45 :url, :key, :no_check_certificate, :no_account_notice, :no_notification
Chris@0 46
Chris@0 47 def initialize
Chris@0 48 self.issue_attributes = {}
Chris@0 49
Chris@1115 50 optparse = OptionParser.new do |opts|
Chris@1115 51 opts.banner = "Usage: rdm-mailhandler.rb [options] --url=<Redmine URL> --key=<API key>"
Chris@1115 52 opts.separator("")
Chris@1464 53 opts.separator("Reads an email from standard input and forwards it to a Redmine server through a HTTP request.")
Chris@1115 54 opts.separator("")
Chris@1115 55 opts.separator("Required arguments:")
Chris@1115 56 opts.on("-u", "--url URL", "URL of the Redmine server") {|v| self.url = v}
Chris@1115 57 opts.on("-k", "--key KEY", "Redmine API key") {|v| self.key = v}
Chris@1115 58 opts.separator("")
Chris@1115 59 opts.separator("General options:")
Chris@1464 60 opts.on("--no-permission-check", "disable permission checking when receiving",
Chris@1464 61 "the email") {self.no_permission_check = '1'}
Chris@1464 62 opts.on("--key-file FILE", "full path to a file that contains your Redmine",
Chris@1464 63 "API key (use this option instead of --key if",
Chris@1464 64 "you don't want the key to appear in the command",
Chris@1464 65 "line)") {|v| read_key_from_file(v)}
Chris@1464 66 opts.on("--no-check-certificate", "do not check server certificate") {self.no_check_certificate = true}
Chris@1464 67 opts.on("-h", "--help", "show this help") {puts opts; exit 1}
Chris@1464 68 opts.on("-v", "--verbose", "show extra information") {self.verbose = true}
Chris@1464 69 opts.on("-V", "--version", "show version information and exit") {puts VERSION; exit}
Chris@1464 70 opts.separator("")
Chris@1464 71 opts.separator("User creation options:")
Chris@1115 72 opts.on("--unknown-user ACTION", "how to handle emails from an unknown user",
Chris@1115 73 "ACTION can be one of the following values:",
Chris@1115 74 "* ignore: email is ignored (default)",
Chris@1115 75 "* accept: accept as anonymous user",
Chris@1115 76 "* create: create a user account") {|v| self.unknown_user = v}
Chris@1464 77 opts.on("--default-group GROUP", "add created user to GROUP (none by default)",
Chris@1464 78 "GROUP can be a comma separated list of groups") { |v| self.default_group = v}
Chris@1464 79 opts.on("--no-account-notice", "don't send account information to the newly",
Chris@1464 80 "created user") { |v| self.no_account_notice = '1'}
Chris@1464 81 opts.on("--no-notification", "disable email notifications for the created",
Chris@1464 82 "user") { |v| self.no_notification = '1'}
Chris@1115 83 opts.separator("")
Chris@1115 84 opts.separator("Issue attributes control options:")
Chris@1115 85 opts.on("-p", "--project PROJECT", "identifier of the target project") {|v| self.issue_attributes['project'] = v}
Chris@1115 86 opts.on("-s", "--status STATUS", "name of the target status") {|v| self.issue_attributes['status'] = v}
Chris@1115 87 opts.on("-t", "--tracker TRACKER", "name of the target tracker") {|v| self.issue_attributes['tracker'] = v}
Chris@1115 88 opts.on( "--category CATEGORY", "name of the target category") {|v| self.issue_attributes['category'] = v}
Chris@1115 89 opts.on( "--priority PRIORITY", "name of the target priority") {|v| self.issue_attributes['priority'] = v}
Chris@1115 90 opts.on("-o", "--allow-override ATTRS", "allow email content to override attributes",
Chris@1115 91 "specified by previous options",
Chris@1115 92 "ATTRS is a comma separated list of attributes") {|v| self.allow_override = v}
Chris@1115 93 opts.separator("")
Chris@1115 94 opts.separator("Examples:")
Chris@1464 95 opts.separator("No project specified, emails MUST contain the 'Project' keyword:")
Chris@1115 96 opts.separator(" rdm-mailhandler.rb --url http://redmine.domain.foo --key secret")
Chris@1115 97 opts.separator("")
Chris@1115 98 opts.separator("Fixed project and default tracker specified, but emails can override")
Chris@1115 99 opts.separator("both tracker and priority attributes using keywords:")
Chris@1115 100 opts.separator(" rdm-mailhandler.rb --url https://domain.foo/redmine --key secret \\")
Chris@1115 101 opts.separator(" --project foo \\")
Chris@1115 102 opts.separator(" --tracker bug \\")
Chris@1115 103 opts.separator(" --allow-override tracker,priority")
Chris@1115 104
Chris@1115 105 opts.summary_width = 27
Chris@0 106 end
Chris@1115 107 optparse.parse!
Chris@1115 108
Chris@1115 109 unless url && key
Chris@1115 110 puts "Some arguments are missing. Use `rdm-mailhandler.rb --help` for getting help."
Chris@1115 111 exit 1
Chris@1115 112 end
Chris@0 113 end
Chris@1115 114
Chris@0 115 def submit(email)
Chris@0 116 uri = url.gsub(%r{/*$}, '') + '/mail_handler'
Chris@1115 117
Chris@128 118 headers = { 'User-Agent' => "Redmine mail handler/#{VERSION}" }
Chris@1115 119
Chris@1115 120 data = { 'key' => key, 'email' => email,
Chris@0 121 'allow_override' => allow_override,
Chris@0 122 'unknown_user' => unknown_user,
Chris@1464 123 'default_group' => default_group,
Chris@1464 124 'no_account_notice' => no_account_notice,
Chris@1464 125 'no_notification' => no_notification,
Chris@0 126 'no_permission_check' => no_permission_check}
Chris@0 127 issue_attributes.each { |attr, value| data["issue[#{attr}]"] = value }
Chris@1115 128
Chris@0 129 debug "Posting to #{uri}..."
Chris@1464 130 begin
Chris@1464 131 response = Net::HTTPS.post_form(URI.parse(uri), data, headers, :no_check_certificate => no_check_certificate)
Chris@1464 132 rescue SystemCallError => e # connection refused, etc.
Chris@1464 133 warn "An error occured while contacting your Redmine server: #{e.message}"
Chris@1464 134 return 75 # temporary failure
Chris@1464 135 end
Chris@0 136 debug "Response received: #{response.code}"
Chris@1115 137
Chris@0 138 case response.code.to_i
Chris@0 139 when 403
Chris@1115 140 warn "Request was denied by your Redmine server. " +
Chris@0 141 "Make sure that 'WS for incoming emails' is enabled in application settings and that you provided the correct API key."
Chris@0 142 return 77
Chris@0 143 when 422
Chris@0 144 warn "Request was denied by your Redmine server. " +
Chris@0 145 "Possible reasons: email is sent from an invalid email address or is missing some information."
Chris@0 146 return 77
Chris@0 147 when 400..499
Chris@0 148 warn "Request was denied by your Redmine server (#{response.code})."
Chris@0 149 return 77
Chris@0 150 when 500..599
Chris@0 151 warn "Failed to contact your Redmine server (#{response.code})."
Chris@0 152 return 75
Chris@0 153 when 201
Chris@0 154 debug "Proccessed successfully"
Chris@0 155 return 0
Chris@0 156 else
Chris@0 157 return 1
Chris@0 158 end
Chris@0 159 end
Chris@1115 160
Chris@0 161 private
Chris@1115 162
Chris@0 163 def debug(msg)
Chris@0 164 puts msg if verbose
Chris@0 165 end
Chris@1115 166
Chris@1115 167 def read_key_from_file(filename)
Chris@1115 168 begin
Chris@1115 169 self.key = File.read(filename).strip
Chris@1115 170 rescue Exception => e
Chris@1115 171 $stderr.puts "Unable to read the key from #{filename}:\n#{e.message}"
Chris@1115 172 exit 1
Chris@1115 173 end
Chris@1115 174 end
Chris@0 175 end
Chris@0 176
Chris@0 177 handler = RedmineMailHandler.new
Chris@0 178 exit(handler.submit(STDIN.read))