Class: Message

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

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.



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

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 do |t| 
            if Gurgitate::Header === t
                t.contents
            else
                t
            end
        end
        message.address.save if message.address.new_record?

        ml, type = Mailinglist.find_by_address(message.envelope_to[0])
        
        if ml then    
            if type == :mail then
                message.mailinglist = ml
                message.save
            end
        end

        return message

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

Instance Method Details

#bounceObject



311
312
313
314
# File 'app/models/message.rb', line 311

def bounce
    # TODO
    nil
end

#deliver(*smtpdetails) ⇒ Object

Delivers a message with the optional parameter hash smtpdetails.



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'app/models/message.rb', line 269

def deliver(*smtpdetails)

    if Hash === smtpdetails[0] then
        smtpdetails = smtpdetails[0]
    else
        smtpdetails = {}
    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

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

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

    begin
        generate_envelope_data        
    rescue UnregisteredUserException
        return bounce
    end

    Net::SMTP.start(smtpserver, smtpport, mysmtpname) do |smtp|
        smtp.send_message messagetext(smtpdetails), self.envelope_from, self.envelope_to
    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.



254
255
256
257
258
259
260
261
262
263
264
# File 'app/models/message.rb', line 254

def messagetext(smtpdetails={})
    if smtpdetails.has_key? :custom_message
        return smtpdetails[:custom_message]
    end

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

#to_sObject

Returns the text of the current message



317
318
319
# File 'app/models/message.rb', line 317

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