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 created 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



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

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
		m = Mail.new
		m.receive mail
		m
	end
end

Instance Method Details

#publishObject

An elt needs to be published as a mail



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
# File 'app/models/mail.rb', line 129

def publish
	logger.info "Publish mail"

	if message and not message.blank? and file
		mail = TMail::Mail.parse file
	else
		mail = MailNotify.create_publish elt
		self.mail_parents = mail.references
		self.file = mail.encoded
	end

	mail['Precedence'] = 'list'
	mail['X-Loop'] = ActionMailer::Base.server_settings[:domain]
	mail['List-Archive'] = "http://"+ActionMailer::Base.server_settings[:domain]
	mail['Errors-To'] = "errors@"+ActionMailer::Base.server_settings[:domain]

	mail.bcc = elt.all_recipients \
		.select { |i| i.email and not i.email.blank? } \
		.collect { |i| i.email } \
		.uniq \
		.join(', ')

	#
	# 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

	if mail.message_id
		# Let's not change the message id
		def mail.add_message_id
		end
	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['X-leparlement-elt-Id'] = elt.id

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

	# Do it after sending, to get the actual message id as generated by the MTA
	# Note that the id of a resent mail is regenerated, but we don't record it,
	# we record the initial id
	self.message = mail.message_id if not self.message or self.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



47
48
49
50
51
52
53
54
55
56
57
58
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
# File 'app/models/mail.rb', line 47

def receive(mail)
	logger.info "Receive mail #{mail.message_id}"
	if mail['X-Leparlement-Elt-Id'] then
		build_elt :id => mail['X-Leparlement-Elt-Id'].to_s,
			:created_on => mail.date,
			:subject => unquote(mail.subject),
			:body => '',
			:mail => self
	else
		build_elt :created_on => mail.date,
			:subject => unquote(mail.subject),
			:body => '',
			:mail => self
	end

	elt.person = Person.find_by_email(mail.from) \
		|| Person.find_by_name(unquote(mail.friendly_from)) \
		|| elt.build_person
	elt.person.name = unquote mail.friendly_from
	elt.person.email = mail.from.first

	# 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.in_reply_to
	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.server_settings[:domain]}/)
			parentId = to.match(/[\w-]*@#{ActionMailer::Base.server_settings[:domain]}/)[0] \
				.gsub(/@#{ActionMailer::Base.server_settings[:domain]}/, '')
			elt.parent = Elt.find parentId
		end

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

	rescue ActiveRecord::RecordNotFound
		elt.build_parent :parent_id => 'mail', :subject => parentId, :body => ''
		elt.parent.id = parentId
		elt.parent.publish
		elt.save
		elt.parent.parent.add_child elt.parent
	end

	mngAttachment(mail) if mail

	self.message = mail.message_id
	self.mail_parents = mail.references
	self.file = mail.encoded
	elt.publish
	elt.parent.add_child elt
end