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