Class: Mail

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/mail.rb

Overview

Receive a mail from which an elt is created.

An associated mail is kept to make sure we don’t lose any data. Attachments are also managed in an associated table

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.receive(mail) ⇒ Object

Receive this new elt as an email, sent through app/helpers/mailman.rb

TODO Loop detection, will need more work



20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/mail.rb', line 20

def Mail.receive(mail)
	logger.info "Receive \"#{mail.subject}\""

	if Mail.find_by_message(mail.message_id) \
		or (mail['X-Message-Id'] \
				and Mail.find_by_message(mail['X-Message-Id'].to_s))
		logger.info "Already received (id: #{mail.message_id})...\n"

	else
		Mail.new.receive mail
	end
end

Instance Method Details

#before_createObject



33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/mail.rb', line 33

def before_create
	# Let's generate a nice looking id
	self.id ||= new_id =
		message.gsub(/\[[\w-]*\]/, '').strip \
		.gsub(/\s/, '_').gsub(/[^\w]+/, '') \
		.gsub(/_+/, '_').gsub(/(^_|_$)/, '') if message
	self.id ||= "mail"

	i = 0
	self.id = "#{new_id}_#{i+=1}" while self.class.find_by_id self.id
end

#publishObject

An elt needs to be published as a mail



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'app/models/mail.rb', line 146

def publish
	logger.info "Publish mail for elt '#{elt.subject}' [#{elt.id}]"
	#puts "Publish mail for elt '#{elt.subject}' [#{elt.id}]"

	if message and not message.blank? and file
		mail = TMail::Mail.parse file
		# Let's reuse an eventual id if it is simply resent (for example after a vote)
		# TODO add a test
		mail.message_id = message
	else
		mail = MailNotify.create_publish elt
		self.mail_parents = mail.references
		self.file = mail.encoded
	end

	# Record all people to whom it was sent
	mail.bcc = elt.all_subscriptions \
		.select { |s| s.person.email and not s.person.email.blank? } \
		.select { |s| #puts "Person: #{s.person.name}, elt: #{s.elt.id}, filter: #{s.filter}";
			not s.filter or (elt.result and elt.result >= s.filter) } \
		.reject { |s| recipients.include? s.person } \
		.uniq \
		.each { |r| person_mails.build :person => r.person } \
		.collect { |i| i.person.email } \
		.uniq

	logger.info "Recipients: #{recipients}"
	#puts "mail.bcc: #{mail.bcc}"

	#
	# Redefine the recipients used when delivering the mail to the local smtp server
	#
	# In fact, only send the mail to the bcc recipients, the to and/or cc are
	# just here for information
	#
	def mail.destinations(default = nil)
		ret = []
		%w( bcc ).each do |nm|
			if h = @header[nm]
				h.addrs.each {|i| ret.push i.address }
			end
		end
		ret.empty? ? default : ret
	end

	# Added to make sure it is not lost, but not modified if already existant
	mail['X-Message-Id'] = mail.message_id if not mail['X-Message-Id']
	mail['Precedence'] = 'list'
	mail['X-Loop'] = ActionMailer::Base.smtp_settings[:domain]
	mail['Errors-To'] = "errors@"+ActionMailer::Base.smtp_settings[:domain]
	mail['List-Archive'] = "http://"+ActionMailer::Base.smtp_settings[:domain]
	mail['Parlement-elt-id'] = elt.id

	if mail.message_id
		# Let's not change the message id
		def mail.add_message_id
		end
	end

	MailNotify::deliver(mail) if mail.bcc and not mail.bcc.empty?

	# Do it after sending, to get the actual message id as generated by the MTA
	self.message = mail.message_id if not message or message.blank?

	logger.info "Published with id \"#{message}\""
end

#receive(mail) ⇒ Object

Receive a mail that will be stored as an elt.

The algorithm to define its parent is simple:

  • header “references”

  • or any subject between []

  • the “to” part before @#:domain

  • in a “mail/lost+found” thread



59
60
61
62
63
64
65
66
67
68
69
70
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/models/mail.rb', line 59

def receive(mail)
	logger.info "Receive mail #{mail.message_id}"
	self.mail_parents = mail.references
	self.message = mail.message_id
	if mail.encoded.isutf8
		self.file = mail.encoded
	else
		charset = mail.type_param 'charset'
		charset ||= 'iso-8859-1'
		self.file = Iconv.iconv('utf-8', charset, mail.encoded).to_s
	end

	build_elt :created_on => mail.date,
		:subject => unquote(mail.subject),
		:body => '',
		:mail => self

	# Try to find its mail parent in the db
	if mail.in_reply_to and Mail.find_by_message mail.in_reply_to
		elt.parent_id = Mail.find_by_message(mail.in_reply_to).elt.id
	end

	# Try to find its mail parent in the db
	if not elt.parent and mail.references
		for parent in mail.references
			m = Mail.find_by_message parent.to_s
			elt.parent = m.elt if m
		end
	end

	begin
		# No reference matching an existing parent, let's try a mailing list we
		# deduce from an eventual [subject]
		if not elt.parent and elt.subject.match(/\[[\w-]*\]/)
			parentId = elt.subject.match(/\[[\w-]*\]/)
			parentId = parentId[0].gsub(/[\[\]]/, '') if parentId
			elt.parent = Elt.find parentId
		end

		# No reference matching an existing parent, let's try the "to" field
		to = mail["Envelope-to"].to_s + ', ' + mail.to.to_s
		if not elt.parent and to.match(/[\w-]*@#{ActionMailer::Base.smtp_settings[:domain]}/)
			parentId = to.match(/[\w-]*@#{ActionMailer::Base.smtp_settings[:domain]}/)[0] \
				.gsub(/@#{ActionMailer::Base.smtp_settings[:domain]}/, '')
			elt.parent = Elt.find parentId
		end

		if not elt.parent
			parentId = "lost+found"
			elt.parent = Elt.find parentId
		end

	rescue ActiveRecord::RecordNotFound
		logger.info "Creating a parent elt with id #{parentId}"
		#puts "Creating a parent elt with id #{parentId}"
		elt.build_parent :parent_id => 'mail', :subject => parentId, :body => ''
		elt.parent.publish
	end

	elt.save!
	mngAttachment mail if mail

	elt.person = Person.find_by_email(mail.from) \
		|| Person.find_by_name(unquote(mail.friendly_from)) \
		|| elt.build_person(:id =>  unquote(mail.friendly_from).gsub(/\s/, '_'),
												:name => unquote(mail.friendly_from),
												:email => mail.from.first
											 ) unless mail.from.first.match(/#{ANONYMOUS_POSTER}@#{ActionMailer::Base.smtp_settings[:domain]}/)

	elt.publish

	if mail.to.to_s.match(/people@#{ActionMailer::Base.smtp_settings[:domain]}/)
		elt.person.image = "/attachment/file/#{elt.attachments.first.file_relative_path}"
		elt.person.save
	end
	self
end