public
Fork of mully/redmine_ticket_emailer
Description: Redmine plugin to allow users to email tickets to the Redmine issue tracking system
Homepage: http://www.redmine.org
Clone URL: git://github.com/rollick/redmine_ticket_emailer.git
expanded mail model
www-data (author)
Mon May 26 03:24:54 -0700 2008
commit  ac2b1050d67527aa12106949821ba639e2d89dd0
tree    efa7e9bc0946386d9487dfda00e1b1e2048347e0
parent  423049fcc379633de7d7adcea45a1420b907d7d1
...
4
5
6
7
8
 
 
 
 
9
10
11
12
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
15
 
 
 
 
 
 
 
 
 
 
 
 
16
17
18
19
 
 
 
20
21
 
22
23
 
24
25
 
 
 
 
 
 
26
27
28
29
30
31
 
 
 
32
33
34
...
38
39
40
41
 
42
43
44
45
46
47
48
 
 
49
50
51
 
 
 
 
 
 
 
 
 
 
52
53
54
55
56
57
 
58
59
60
61
62
63
64
 
 
 
 
 
 
 
 
 
65
66
67
...
88
89
90
91
 
 
92
93
...
4
5
6
 
 
7
8
9
10
11
12
 
 
 
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 
 
44
45
46
47
 
48
49
 
50
51
52
53
54
55
56
57
58
59
60
61
 
 
 
62
63
64
65
66
67
...
71
72
73
 
74
75
76
77
78
79
 
 
80
81
82
 
 
83
84
85
86
87
88
89
90
91
92
93
 
 
 
94
 
95
96
97
 
 
 
 
 
98
99
100
101
102
103
104
105
106
107
108
109
...
130
131
132
 
133
134
135
136
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
+    
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
     
0
     if author.nil?
0
-       author_id = (Member.find_by_project_id @@project.id).id
0
-    else
0
-        author_id = author.id
0
+      #create the user with minimal permissions and no email notifications
0
+      author = User.new
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
+      
0
+      if author.save
0
+        p "User created"
0
+      else
0
+        p author.errors
0
+      end
0
+      
0
     end
0
-        
0
+    
0
+    author_id = author.id
0
+
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
+
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
+
0
     issue = Issue.create(
0
         :subject => email.subject,
0
-        :description => email.body,
0
-        :priority_id => 3,
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 => 3,
0
+        :tracker_id => tracker_id,
0
         :author_id => author_id,
0
-        :status_id => 1        
0
+        :status_id => status_id        
0
     )
0
     
0
+    if issue.save
0
+      p issue.to_s
0
+    else
0
+      p issue.errors
0
+    end
0
+        
0
     if email.has_attachments?
0
         for attachment in email.attachments        
0
             Attachment.create(:container => issue, 
0
-                                  :file => attachment,
0
-                                  :description => "",
0
-                                  :author_id => 2)
0
+              :file => attachment,
0
+              :description => "",
0
+              :author_id => author_id)
0
         end
0
     end
0
 
0
@@ -38,30 +71,39 @@ class MailReader < ActionMailer::Base
0
   
0
      begin
0
        require 'net/imap'
0
-     rescue LoadError
0
+     rescue LoadErroremail_folder
0
        raise RequiredLibraryNotFoundError.new('NET::Imap could not be loaded')
0
      end
0
 
0
      @@config_path = (RAILS_ROOT + '/config/emailer.yml')
0
      
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
      
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
+         
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
+        
0
+         unless @@project.nil?
0
+
0
+            #match yaml sections with a "project" key
0
+            @@config = YAML.load_file(@@config_path)[config_group].symbolize_keys
0
 
0
-        unless @@project.nil?
0
-            @@config = YAML.load_file(@@config_path)[project_name].symbolize_keys
0
-                 
0
             imap = Net::IMAP.new(@@config[:email_server], port=@@config[:email_port], usessl=@@config[:use_ssl])
0
-             
0
+         
0
             imap.login(@@config[:email_login], @@config[:email_password])
0
             imap.select(@@config[:email_folder])  
0
-                     
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
+              else
0
+                p "#{@@from_email} not whitelisted."
0
+              end   
0
               #Mark message as deleted and it will be removed from storage when user session closd
0
               imap.store(message_id, "+FLAGS", [:Deleted])
0
             end
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}"
0
   end
0
 end

Comments