Class: Address

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

Overview

The addresses table is where all the email addresses are stored, and Address is the interface to that.

Address has the following data members:

id

The row ID in the database.

address

The actual email address

active

Whether the address is active or not. This should be reset to false upon a bounce, but currently isn’t because there isn’t a standard format for bounce messages and I haven’t yet written the code to figure out which address I attempted to send mail to failed.

delivery_attempts

The number of delivery attempts. This is also related to bouncing, and is thus a stub.

bounces

The number of times email sent to this address has bounced. Like the other two bounce-related members, this is merely a stub.

Address is also joined with other classes:

Mailinglist

The join between Mailinglist and Address reflects addresses are subscribed to a mailing list.

User

The join between Mailinglist and User reflects addresses that the User can send email to via the proxy in order to trigger email-address rewriting.

Message

Messages that were received from this Address.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(addrstr) ⇒ Object

Parses the address string in addrstr for an actual email address. This handles addresses of the forms:

Dave Brown <[email protected]>
"Dave A. Brown" <[email protected]>
[email protected] (Dave Brown)
[email protected]

In other words, things that are likely to be in the From: and To: headers in an average email message.

addrstr

The email address string.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/models/address.rb', line 48

def self.parse(addrstr) # not really OO, this
    matches = /\s*([^@]+\@\S+) \((.*)\)/.match(addrstr)
    if matches
        return matches[1], matches[2]
    end
    
    matches = /(\".*\") \<([^@]+\@\S+)\>/.match(addrstr)
    if matches
        return matches[2], matches[1]
    end

    matches = /((?:\w|\s)*) \<([^@]+\@\S+)\>/.match(addrstr)
    if matches
        return matches[2], matches[1]
    end
    
    matches = /(\S+@\S+)/.match(addrstr)
    if matches
        return matches[1],nil
    end
    
    return addrstr, nil # and this is being probably needlessly optimistic
end

.proxyaddress(addrstr) ⇒ Object

If the address in addrstr has a proxy address, returns the proxy address.

addrstr

The address string to search for a proxy address for



77
78
79
# File 'app/models/address.rb', line 77

def self.proxyaddress(addrstr)
    a = Address.find_or_create_by_address(addrstr) and a.proxyaddress
end

Instance Method Details

#outside?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'app/models/address.rb', line 86

def outside?
    not user
end

#proxified(ml) ⇒ Object

Returns an anonymous proxy address for this user and a mailing list



91
92
93
94
# File 'app/models/address.rb', line 91

def proxified ml
    ProxyLink.find_or_create_by_address_id_and_mailinglist_id(id, ml.id) .
        proxy_address
end

#proxyaddressObject

If the current address has a proxy address, return that.



82
83
84
# File 'app/models/address.rb', line 82

def proxyaddress
    user && user.address
end

#to_sObject

Return a string representation of the address.



106
107
108
# File 'app/models/address.rb', line 106

def to_s
    address
end

#userObject

If the address has a proxy address, then return the user that the proxy address belongs to.



98
99
100
101
102
103
# File 'app/models/address.rb', line 98

def user
    ml = mailinglists.find(:all) do |ml| 
        ml.id == ml.user.mailinglist_id && !ml.user.virtual?
    end[0]
    if ml then ml.user end
end