Class: Message

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

Overview

The Message class is one of the most important classes in sugoi-mail–mainly because it does so much. But anyway. Herewith a Model With Some Controller-like Things In It.

This is the class that handles what a “message” is, and how it behaves. It makes considerable use of gurgitate-mail’s mail-parsing library, so it might not be a bad idea to familiarize yourself with that if trying to work on this.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_message(mess, recipient = nil, sender = nil) ⇒ Object Also known as: parse

Processes a Gurgitate::Mailmessage object or the text of a message and creates a new Message object.

mess

Either a Gurgitate::Mailmessage object (with envelope

information intact), or the text of a mail message (in which case the envelope information must be supplied)

recipient

The envelope recipient (SMTP’s RCPT To:) of the mail

message.

sender

The envelope sender (SMTP’s MAIL From:) of the mail

message.



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
# File 'app/models/message.rb', line 68

def from_message(mess, recipient=nil, sender=nil)
    # This method is way too long, sorry
    if Gurgitate::Mailmessage === mess then
        message = self.new
        message.messageid     = get_or_create_messageid mess
        message.timestamp     = get_or_create_timestamp mess
        addr = Address.find_or_create_by_address mess.from
        if addr.new_record? then addr.save end

        message.address       = addr
        message.headers       = mess.headers.to_s
        message.body          = mess.body.to_s
        message.parent        = find_parent mess
        message.envelope_from = mess.from
        message.envelope_to   = mess.to.map { |t| t.contents }

        message.address.save if message.address.new_record?

        ml, type = Mailinglist.find_by_address(message.envelope_to[0])

        if type == :mail and ml then
            message.mailinglist = ml
        end

        return message

    elsif String === mess then
        from_message Gurgitate::Mailmessage.new(mess),recipient,sender
    end
end

Instance Method Details

#bounceObject



290
291
292
293
# File 'app/models/message.rb', line 290

def bounce
    # TODO
    nil
end

#deliver(*smtpdetails) ⇒ Object

Delivers a message with the optional parameter hash smtpdetails.



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'app/models/message.rb', line 237

def deliver(*smtpdetails)
    if Hash === smtpdetails[0] then
        smtpdetails = smtpdetails[0]
    else
        smtpdetails = {}
    end

    if mailinglist.closed?
        if mailinglist.user.mailinglist.has_address? envelope_from or
           mailinglist.user.address == envelope_from then
        end
        unless mailinglist.user.mailinglist.has_address? self.envelope_from
            return bounce
        end
    end

    if mailinglist.confirmation?
        addresses = mailinglist.confirmed_addresses.map { |a| a.address }
    else
        addresses = mailinglist.expand_addresses.map { |a| a.address }
    end


    # Hm, not sure why this is *here* instead of somewhere
    # else.  Fix later.
    #
    # (This should actually be in the database somewhere, I'm thinking.)
    smtpserver = SysConfig.smtpserver
    smtpport   = SysConfig.smtpport
    mysmtpname = SysConfig.mysmtpname

    # smtpserver = "localhost"      # XXX MAGIC CONSTANT XXX
    # smtpport   = 25               # XXX MAGIC CONSTANT XXX
    # mysmtpname = "localhost"      # XXX MAGIC CONSTNAT XXX

    if smtpdetails.has_key? :smtpserver then
        smtpserver = smtpdetails[:smtpserver] 
    end
    if smtpdetails.has_key? :smtpport then
        smtpport = smtpdetails[:smtpport] || 25
    end

    fromaddress = mailinglist.name + SysConfig.address_separator +
        SysConfig.bounce_suffix + "@" + mailinglist.user.domain.name
    # XXX MAGIC CONSTANT XXX
    # fromaddress = "#{mailinglist.name}-bounces@#{mailinglist.user.domain.name}"
    Net::SMTP.start(smtpserver, smtpport, mysmtpname) do |smtp|
        # puts "Will send email to #{addresses.inspect}"
        # puts "Message id is #{id}"
        smtp.send_message messagetext, fromaddress, addresses
    end
end

#messagetext(smtpdetails = {}) ⇒ Object

Returns the text of the message that would be sent out by the “deliver” message. This might have headers rewritten to reflect mail aliases.



210
211
212
213
214
215
216
217
218
219
220
# File 'app/models/message.rb', line 210

def messagetext(smtpdetails={})
    if smtpdetails.length == 1 and smtpdetails[0].has_key? :custom_message
        smtpdetails[0][:custom_message]
    end

    if mailinglist.canonical_address? or mailinglist.closed? then
        proxify_addresses
    else
        to_s
    end
end

#proxy_deliver(from_address, to_address) ⇒ Object



222
223
224
225
226
227
228
229
230
231
# File 'app/models/message.rb', line 222

def proxy_deliver(from_address, to_address)
    text = proxify_addresses

    smtpserver  = "localhost"
    smtpport    = 25

    Net::SMTP.start(smtpserver, smtpport, "localhost") do |smtp|
        smtp.send_message text, from_address, to_address
    end
end

#to_sObject

Returns the text of the current message



296
297
298
# File 'app/models/message.rb', line 296

def to_s
    headers.to_s.chomp.chomp+"\n\n"+body.to_s
end