Chris@1494: #!/usr/bin/env ruby Chris@1494: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@1494: # Chris@1494: # This program is free software; you can redistribute it and/or Chris@1494: # modify it under the terms of the GNU General Public License Chris@1494: # as published by the Free Software Foundation; either version 2 Chris@1494: # of the License, or (at your option) any later version. Chris@1494: # Chris@1494: # This program is distributed in the hope that it will be useful, Chris@1494: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1494: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1494: # GNU General Public License for more details. Chris@1494: # Chris@1494: # You should have received a copy of the GNU General Public License Chris@1494: # along with this program; if not, write to the Free Software Chris@1494: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1494: Chris@1494: require 'net/http' Chris@1494: require 'net/https' Chris@1494: require 'uri' Chris@1494: require 'optparse' Chris@1494: Chris@1494: module Net Chris@1494: class HTTPS < HTTP Chris@1494: def self.post_form(url, params, headers, options={}) Chris@1494: request = Post.new(url.path) Chris@1494: request.form_data = params Chris@1494: request.initialize_http_header(headers) Chris@1494: request.basic_auth url.user, url.password if url.user Chris@1494: http = new(url.host, url.port) Chris@1494: http.use_ssl = (url.scheme == 'https') Chris@1494: if options[:no_check_certificate] Chris@1494: http.verify_mode = OpenSSL::SSL::VERIFY_NONE Chris@1494: end Chris@1494: http.start {|h| h.request(request) } Chris@1494: end Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: class RedmineMailHandler Chris@1494: VERSION = '0.2.3' Chris@1494: Chris@1494: attr_accessor :verbose, :issue_attributes, :allow_override, :unknown_user, :default_group, :no_permission_check, Chris@1494: :url, :key, :no_check_certificate, :no_account_notice, :no_notification Chris@1494: Chris@1494: def initialize Chris@1494: self.issue_attributes = {} Chris@1494: Chris@1494: optparse = OptionParser.new do |opts| Chris@1494: opts.banner = "Usage: rdm-mailhandler.rb [options] --url= --key=" Chris@1494: opts.separator("") Chris@1494: opts.separator("Reads an email from standard input and forwards it to a Redmine server through a HTTP request.") Chris@1494: opts.separator("") Chris@1494: opts.separator("Required arguments:") Chris@1494: opts.on("-u", "--url URL", "URL of the Redmine server") {|v| self.url = v} Chris@1494: opts.on("-k", "--key KEY", "Redmine API key") {|v| self.key = v} Chris@1494: opts.separator("") Chris@1494: opts.separator("General options:") Chris@1494: opts.on("--no-permission-check", "disable permission checking when receiving", Chris@1494: "the email") {self.no_permission_check = '1'} Chris@1494: opts.on("--key-file FILE", "full path to a file that contains your Redmine", Chris@1494: "API key (use this option instead of --key if", Chris@1494: "you don't want the key to appear in the command", Chris@1494: "line)") {|v| read_key_from_file(v)} Chris@1494: opts.on("--no-check-certificate", "do not check server certificate") {self.no_check_certificate = true} Chris@1494: opts.on("-h", "--help", "show this help") {puts opts; exit 1} Chris@1494: opts.on("-v", "--verbose", "show extra information") {self.verbose = true} Chris@1494: opts.on("-V", "--version", "show version information and exit") {puts VERSION; exit} Chris@1494: opts.separator("") Chris@1494: opts.separator("User creation options:") Chris@1494: opts.on("--unknown-user ACTION", "how to handle emails from an unknown user", Chris@1494: "ACTION can be one of the following values:", Chris@1494: "* ignore: email is ignored (default)", Chris@1494: "* accept: accept as anonymous user", Chris@1494: "* create: create a user account") {|v| self.unknown_user = v} Chris@1494: opts.on("--default-group GROUP", "add created user to GROUP (none by default)", Chris@1494: "GROUP can be a comma separated list of groups") { |v| self.default_group = v} Chris@1494: opts.on("--no-account-notice", "don't send account information to the newly", Chris@1494: "created user") { |v| self.no_account_notice = '1'} Chris@1494: opts.on("--no-notification", "disable email notifications for the created", Chris@1494: "user") { |v| self.no_notification = '1'} Chris@1494: opts.separator("") Chris@1494: opts.separator("Issue attributes control options:") Chris@1494: opts.on("-p", "--project PROJECT", "identifier of the target project") {|v| self.issue_attributes['project'] = v} Chris@1494: opts.on("-s", "--status STATUS", "name of the target status") {|v| self.issue_attributes['status'] = v} Chris@1494: opts.on("-t", "--tracker TRACKER", "name of the target tracker") {|v| self.issue_attributes['tracker'] = v} Chris@1494: opts.on( "--category CATEGORY", "name of the target category") {|v| self.issue_attributes['category'] = v} Chris@1494: opts.on( "--priority PRIORITY", "name of the target priority") {|v| self.issue_attributes['priority'] = v} Chris@1494: opts.on("-o", "--allow-override ATTRS", "allow email content to override attributes", Chris@1494: "specified by previous options", Chris@1494: "ATTRS is a comma separated list of attributes") {|v| self.allow_override = v} Chris@1494: opts.separator("") Chris@1494: opts.separator("Examples:") Chris@1494: opts.separator("No project specified, emails MUST contain the 'Project' keyword:") Chris@1494: opts.separator(" rdm-mailhandler.rb --url http://redmine.domain.foo --key secret") Chris@1494: opts.separator("") Chris@1494: opts.separator("Fixed project and default tracker specified, but emails can override") Chris@1494: opts.separator("both tracker and priority attributes using keywords:") Chris@1494: opts.separator(" rdm-mailhandler.rb --url https://domain.foo/redmine --key secret \\") Chris@1494: opts.separator(" --project foo \\") Chris@1494: opts.separator(" --tracker bug \\") Chris@1494: opts.separator(" --allow-override tracker,priority") Chris@1494: Chris@1494: opts.summary_width = 27 Chris@1494: end Chris@1494: optparse.parse! Chris@1494: Chris@1494: unless url && key Chris@1494: puts "Some arguments are missing. Use `rdm-mailhandler.rb --help` for getting help." Chris@1494: exit 1 Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: def submit(email) Chris@1494: uri = url.gsub(%r{/*$}, '') + '/mail_handler' Chris@1494: Chris@1494: headers = { 'User-Agent' => "Redmine mail handler/#{VERSION}" } Chris@1494: Chris@1494: data = { 'key' => key, 'email' => email, Chris@1494: 'allow_override' => allow_override, Chris@1494: 'unknown_user' => unknown_user, Chris@1494: 'default_group' => default_group, Chris@1494: 'no_account_notice' => no_account_notice, Chris@1494: 'no_notification' => no_notification, Chris@1494: 'no_permission_check' => no_permission_check} Chris@1494: issue_attributes.each { |attr, value| data["issue[#{attr}]"] = value } Chris@1494: Chris@1494: debug "Posting to #{uri}..." Chris@1494: begin Chris@1494: response = Net::HTTPS.post_form(URI.parse(uri), data, headers, :no_check_certificate => no_check_certificate) Chris@1494: rescue SystemCallError => e # connection refused, etc. Chris@1494: warn "An error occured while contacting your Redmine server: #{e.message}" Chris@1494: return 75 # temporary failure Chris@1494: end Chris@1494: debug "Response received: #{response.code}" Chris@1494: Chris@1494: case response.code.to_i Chris@1494: when 403 Chris@1494: warn "Request was denied by your Redmine server. " + Chris@1494: "Make sure that 'WS for incoming emails' is enabled in application settings and that you provided the correct API key." Chris@1494: return 77 Chris@1494: when 422 Chris@1494: warn "Request was denied by your Redmine server. " + Chris@1494: "Possible reasons: email is sent from an invalid email address or is missing some information." Chris@1494: return 77 Chris@1494: when 400..499 Chris@1494: warn "Request was denied by your Redmine server (#{response.code})." Chris@1494: return 77 Chris@1494: when 500..599 Chris@1494: warn "Failed to contact your Redmine server (#{response.code})." Chris@1494: return 75 Chris@1494: when 201 Chris@1494: debug "Proccessed successfully" Chris@1494: return 0 Chris@1494: else Chris@1494: return 1 Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: private Chris@1494: Chris@1494: def debug(msg) Chris@1494: puts msg if verbose Chris@1494: end Chris@1494: Chris@1494: def read_key_from_file(filename) Chris@1494: begin Chris@1494: self.key = File.read(filename).strip Chris@1494: rescue Exception => e Chris@1494: $stderr.puts "Unable to read the key from #{filename}:\n#{e.message}" Chris@1494: exit 1 Chris@1494: end Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: handler = RedmineMailHandler.new Chris@1494: exit(handler.submit(STDIN.read))