0
@@ -4,31 +4,64 @@ class MailReader < ActionMailer::Base
0
# If the email exists for a user in the current project,
0
# use that user as the author. Otherwise, use the first
0
# user that is returned from the Member model
0
- author = User.find_by_mail @@from_email, :select=>"users.id", :joins=>"inner join members on members.user_id = users.id",
0
- :conditions=>["members.project_id=?", @@project.id]
0
+ # author = User.find_by_mail @@from_email, :select=>"users.id", :joins=>"inner join members on members.user_id = users.id",
0
+ # :conditions=>["members.project_id=?", @@project.id]
0
+ author = User.find_by_mail @@from_email
0
- author_id = (Member.find_by_project_id @@project.id).id
0
+ #create the user with minimal permissions and no email notifications
0
+ author.login = @@from_email.split('@')[0]
0
+ author.status = 3 #set status to locked
0
+ author.mail_notification = false
0
+ author.firstname = @@from_name.split(" ")[0] #hope name is first then last
0
+ author.lastname = @@from_name.split(" ")[1]
0
+ author.auth_source_id = 1 #this is the MEDDENT auth for the current install
0
+ author.mail = @@from_email
0
+ priorities = Enumeration.get_values('IPRI')
0
+ @DEFAULT_PRIORITY = priorities.find {|p| p.name == "Normal" }
0
+ @PRIORITY_MAPPING = {}
0
+ priorities.each { |priority| @PRIORITY_MAPPING[priority.name] = priority }
0
+ tracker_id = status_id = nil
0
+ tracker_id = Tracker.find_by_name(@@config[:issue_tracker]).id unless Tracker.find_by_name(@@config[:issue_tracker]).nil?
0
+ status_id = IssueStatus.find_by_name(@@config[:issue_status]).id unless IssueStatus.find_by_name(@@config[:issue_status]).nil?
0
:subject => email.subject,
0
- :description => email.body,
0
+# :description => email.body.gsub(/<(html|HTML)[^<]*<\/(html|HTML)>/im,''),
0
+ :description => email.body.split(/<(HTML|html)/)[0],
0
+ :priority_id => @DEFAULT_PRIORITY.id, #@PRIORITY_MAPPING[@priority].id || @DEFAULT_PRIORITY.id,
0
:project_id => @@project.id,
0
+ :tracker_id =>
tracker_id,
0
:author_id => author_id,
0
+ :status_id =>
status_id 0
if email.has_attachments?
0
for attachment in email.attachments
0
Attachment.create(:container => issue,
0
+ :author_id => author_id)
0
@@ -38,30 +71,39 @@ class MailReader < ActionMailer::Base
0
+ rescue LoadError
email_folder0
raise RequiredLibraryNotFoundError.new('NET::Imap could not be loaded')
0
@@config_path = (RAILS_ROOT + '/config/emailer.yml')
0
- # Cycle through all of the projects created in the yaml file
0
- YAML.load_file(@@config_path).keys.each do |project_name|
0
+ # Cycle through all of the projects created in the yaml file
0
+ YAML.load_file(@@config_path).keys.each do |config_group|
0
- #Find the project based off the name in the YAML if the emailer is enabled in Redmine
0
- @@project = Project.find_by_name project_name, :include=>:enabled_modules , :conditions=>"enabled_modules.name='ticket_emailer'"
0
+ #find the project relating to this config group
0
+ project_name = YAML.load_file(@@config_path)[config_group]["project"]
0
+ #Find the project based off the name in the YAML if the emailer is enabled in Redmine
0
+ @@project = Project.find_by_name project_name, :include=>:enabled_modules, :conditions=>"enabled_modules.name='ticket_emailer'"
0
+ #match yaml sections with a "project" key
0
+ @@config = YAML.load_file(@@config_path)[config_group].symbolize_keys
0
- @@config = YAML.load_file(@@config_path)[project_name].symbolize_keys
0
imap = Net::IMAP.new(@@config[:email_server], port=@@config[:email_port], usessl=@@config[:use_ssl])
0
imap.login(@@config[:email_login], @@config[:email_password])
0
imap.select(@@config[:email_folder])
0
- imap.search(['ALL']).each do |message_id|
0
- msg = imap.fetch(message_id,'RFC822')[0].attr['RFC822']
0
- @@from_email = from_email_address(imap, message_id)
0
- MailReader.receive(msg)
0
+ imap.search(['TO', @@config[:email_to]]).each do |message_id|
0
+ msg = imap.fetch(message_id,'RFC822')[0].attr['RFC822']
0
+ @@from_name, @@from_email = from_email_address(imap, message_id)
0
+ #check if email matches whitelistings
0
+ if @@config[:email_white_list].split(' ').find {|email_part| @@from_email.include? email_part}
0
+ MailReader.receive(msg)
0
+ p "#{@@from_email} not whitelisted."
0
#Mark message as deleted and it will be removed from storage when user session closd
0
imap.store(message_id, "+FLAGS", [:Deleted])
0
@@ -88,6 +130,7 @@ class MailReader < ActionMailer::Base
0
env = imap.fetch(msg_id, "ENVELOPE")[0].attr["ENVELOPE"]
0
mailbox = env.from[0].mailbox
0
host = env.from[0].host
0
- from = "#{mailbox}@#{host}"
0
+ name = env.from[0].name
0
+ from = "#{name}", "#{mailbox}@#{host}"