<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -4,31 +4,64 @@ class MailReader &lt; ActionMailer::Base
     # If the email exists for a user in the current project,
     # use that user as the author.  Otherwise, use the first
     # user that is returned from the Member model
-    author = User.find_by_mail @@from_email, :select=&gt;&quot;users.id&quot;, :joins=&gt;&quot;inner join members on members.user_id = users.id&quot;,
-                              :conditions=&gt;[&quot;members.project_id=?&quot;, @@project.id]
+    
+    # author = User.find_by_mail @@from_email, :select=&gt;&quot;users.id&quot;, :joins=&gt;&quot;inner join members on members.user_id = users.id&quot;,
+    #                           :conditions=&gt;[&quot;members.project_id=?&quot;, @@project.id]
+    author = User.find_by_mail @@from_email
     
     if author.nil?
-       author_id = (Member.find_by_project_id @@project.id).id
-    else
-        author_id = author.id
+      #create the user with minimal permissions and no email notifications
+      author = User.new
+      author.login = @@from_email.split('@')[0]
+      author.status = 3 #set status to locked
+      author.mail_notification = false
+      author.firstname = @@from_name.split(&quot; &quot;)[0] #hope name is first then last
+      author.lastname = @@from_name.split(&quot; &quot;)[1]
+      author.auth_source_id = 1 #this is the MEDDENT auth for the current install
+      author.mail = @@from_email
+      
+      if author.save
+        p &quot;User created&quot;
+      else
+        p author.errors
+      end
+      
     end
-        
+    
+    author_id = author.id
+
+    priorities = Enumeration.get_values('IPRI')
+    @DEFAULT_PRIORITY = priorities.find {|p| p.name == &quot;Normal&quot; }
+    @PRIORITY_MAPPING = {}
+    priorities.each { |priority| @PRIORITY_MAPPING[priority.name] = priority }
+
+    tracker_id = status_id = nil
+    tracker_id = Tracker.find_by_name(@@config[:issue_tracker]).id unless Tracker.find_by_name(@@config[:issue_tracker]).nil?
+    status_id = IssueStatus.find_by_name(@@config[:issue_status]).id unless IssueStatus.find_by_name(@@config[:issue_status]).nil?
+
     issue = Issue.create(
         :subject =&gt; email.subject,
-        :description =&gt; email.body,
-        :priority_id =&gt; 3,
+#        :description =&gt; email.body.gsub(/&lt;(html|HTML)[^&lt;]*&lt;\/(html|HTML)&gt;/im,''),
+        :description =&gt; email.body.split(/&lt;(HTML|html)/)[0],
+        :priority_id =&gt; @DEFAULT_PRIORITY.id, #@PRIORITY_MAPPING[@priority].id || @DEFAULT_PRIORITY.id,
         :project_id =&gt; @@project.id,
-        :tracker_id =&gt; 3,
+        :tracker_id =&gt; tracker_id,
         :author_id =&gt; author_id,
-        :status_id =&gt; 1        
+        :status_id =&gt; status_id        
     )
     
+    if issue.save
+      p issue.to_s
+    else
+      p issue.errors
+    end
+        
     if email.has_attachments?
         for attachment in email.attachments        
             Attachment.create(:container =&gt; issue, 
-                                  :file =&gt; attachment,
-                                  :description =&gt; &quot;&quot;,
-                                  :author_id =&gt; 2)
+              :file =&gt; attachment,
+              :description =&gt; &quot;&quot;,
+              :author_id =&gt; author_id)
         end
     end
 
@@ -38,30 +71,39 @@ class MailReader &lt; ActionMailer::Base
   
      begin
        require 'net/imap'
-     rescue LoadError
+     rescue LoadErroremail_folder
        raise RequiredLibraryNotFoundError.new('NET::Imap could not be loaded')
      end
 
      @@config_path = (RAILS_ROOT + '/config/emailer.yml')
      
-     # Cycle through all of the projects created in the yaml file
-     YAML.load_file(@@config_path).keys.each do |project_name|
+     # Cycle through all of the projects created in the yaml file       
+      YAML.load_file(@@config_path).keys.each do |config_group|
      
-        #Find the project based off the name in the YAML if the emailer is enabled in Redmine
-        @@project = Project.find_by_name project_name, :include=&gt;:enabled_modules , :conditions=&gt;&quot;enabled_modules.name='ticket_emailer'&quot;
+         #find the project relating to this config group
+         project_name = YAML.load_file(@@config_path)[config_group][&quot;project&quot;]
+         
+         #Find the project based off the name in the YAML if the emailer is enabled in Redmine
+         @@project = Project.find_by_name project_name, :include=&gt;:enabled_modules, :conditions=&gt;&quot;enabled_modules.name='ticket_emailer'&quot;
+        
+         unless @@project.nil?
+
+            #match yaml sections with a &quot;project&quot; key
+            @@config = YAML.load_file(@@config_path)[config_group].symbolize_keys
 
-        unless @@project.nil?
-            @@config = YAML.load_file(@@config_path)[project_name].symbolize_keys
-                 
             imap = Net::IMAP.new(@@config[:email_server], port=@@config[:email_port], usessl=@@config[:use_ssl])
-             
+         
             imap.login(@@config[:email_login], @@config[:email_password])
             imap.select(@@config[:email_folder])  
-                     
-            imap.search(['ALL']).each do |message_id|
-              msg = imap.fetch(message_id,'RFC822')[0].attr['RFC822']
-              @@from_email = from_email_address(imap, message_id)
-              MailReader.receive(msg)          
+            imap.search(['TO', @@config[:email_to]]).each do |message_id|
+               msg = imap.fetch(message_id,'RFC822')[0].attr['RFC822']
+              @@from_name, @@from_email = from_email_address(imap, message_id)
+              #check if email matches whitelistings
+              if @@config[:email_white_list].split(' ').find {|email_part| @@from_email.include? email_part}
+                MailReader.receive(msg)
+              else
+                p &quot;#{@@from_email} not whitelisted.&quot;
+              end   
               #Mark message as deleted and it will be removed from storage when user session closd
               imap.store(message_id, &quot;+FLAGS&quot;, [:Deleted])
             end
@@ -88,6 +130,7 @@ class MailReader &lt; ActionMailer::Base
     env = imap.fetch(msg_id, &quot;ENVELOPE&quot;)[0].attr[&quot;ENVELOPE&quot;]
     mailbox = env.from[0].mailbox
     host    = env.from[0].host
-    from = &quot;#{mailbox}@#{host}&quot;
+    name    = env.from[0].name
+    from = &quot;#{name}&quot;, &quot;#{mailbox}@#{host}&quot;
   end
 end</diff>
      <filename>app/models/mail_reader.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>423049fcc379633de7d7adcea45a1420b907d7d1</id>
    </parent>
  </parents>
  <author>
    <name>www-data</name>
    <email>www-data@trishul.meddent.uwa.edu.au</email>
  </author>
  <url>http://github.com/rollick/redmine_ticket_emailer/commit/ac2b1050d67527aa12106949821ba639e2d89dd0</url>
  <id>ac2b1050d67527aa12106949821ba639e2d89dd0</id>
  <committed-date>2008-05-26T03:24:54-07:00</committed-date>
  <authored-date>2008-05-26T03:24:54-07:00</authored-date>
  <message>expanded mail model</message>
  <tree>efa7e9bc0946386d9487dfda00e1b1e2048347e0</tree>
  <committer>
    <name>www-data</name>
    <email>www-data@trishul.meddent.uwa.edu.au</email>
  </committer>
</commit>
