You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rdm-mailhandler.rb 8.2KB

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