Skip to content
Browse files

Smailr gets a smarter model

 * Adding mbox_for_address and for_address functions to the model.
 * Removing redundant code
  • Loading branch information...
1 parent d1794a9 commit 4e0fcd65df07fe231752a8de21ed0abf6bda7681 @sts committed Apr 26, 2012
Showing with 44 additions and 25 deletions.
  1. +9 −14 lib/smailr/alias.rb
  2. +13 −7 lib/smailr/mailbox.rb
  3. +22 −4 lib/smailr/model.rb
View
23 lib/smailr/alias.rb
@@ -4,32 +4,27 @@ def self.add(address, options)
mbox_localpart, mbox_fqdn = address.split('@')
alias_localpart, alias_fqdn = options.alias.split('@')
- # Check if alias_fqdn is a local domain
- if Model::Domain.where(:fqdn => alias_fqdn).empty?
+ # We don't want aliases for non-local domains, since the
+ # exim router won't accept it.
+ if not Model::Alias.domain(alias_fqdn).exists?
say_error "You are trying to add an alias for a non-local domain: #{alias_fqdn}"
exit 1
end
- # Lookup the mbox object
- mbox = Model::Mailbox[ :localpart => mbox_localpart,
- :domain => Model::Domain.where(:fqdn=> mbox_fqdn) ]
- if not mbox
+ mbox = Model::Alias.mbox_for_address(address)
+
+ # We don't want aliases which cannot be routed to a mailbox.
+ if not mbox.exists?
say_error "You are trying to add an alias for a non existing mailbox: #{address}"
- exit
+ exit 1
end
mbox.add_alias(:address => options.alias)
end
def self.rm(address, options)
- mbox_localpart, mbox_fqdn = address.split('@')
-
- # Lookup the mbox object
- mbox = Model::Mailbox[ :localpart => mbox_localpart,
- :domain => Model::Domain.where(:fqdn=> mbox_fqdn) ]
-
+ mbox = Model::Alias.mbox_for_address(address)
mbox.remove_alias(Model::Alias[:address => options.alias])
end
-
end
end
View
20 lib/smailr/mailbox.rb
@@ -10,20 +10,26 @@ def self.add(address, options)
else
password = options[:password]
end
-
+
localpart, fqdn = address.split('@')
-
+
domain = Model::Domain[:fqdn => fqdn]
mbox = Model::Mailbox.create(:localpart => localpart, :password => password)
domain.add_mailbox(mbox)
end
-
+
def self.rm(address, options)
localpart, fqdn = address.split('@')
-
- mbox = Model::Mailbox[:localpart => localpart]
- domain = Model::Domain[:fqdn => fqdn]
- domain.remove_mailbox(mbox)
+
+ mbox = Model::Mailbox.for_address(address)
+
+ # We don't want to end up with an inconsistent database here.
+ if not mbox.aliases.empty?
+ say_error "Trying to remove a mailbox, with existing aliases."
+ exit 1
+ end
+
+ mbox.destroy
end
end
end
View
26 lib/smailr/model.rb
@@ -1,24 +1,42 @@
module Smailr
module Model
-
class Domain < Sequel::Model
one_to_many :mailboxes
end
-
class Mailbox < Sequel::Model
many_to_one :domain
one_to_many :aliases
def password=(clear)
self[:password] = Digest::SHA1.hexdigest(clear)
end
- end
+ def self.domain(fqdn)
+ return Domain[:fqdn => fqdn]
+ end
+
+ def self.for_address(address)
+ localpart, fqdn = address.split('@')
+
+ return self[:localpart => localpart,
+ :domain => domain(fqdn)]
+ end
+ end
class Alias < Sequel::Model
many_to_one :mailbox
- end
+ def self.domain(fqdn)
+ return Domain[:fqdn => fqdn]
+ end
+
+ def self.mbox_for_address(address)
+ localpart, fqdn = address.split('@')
+
+ return Mailbox[:localpart => localpart,
+ :domain => domain(fqdn)]
+ end
+ end
end
end

0 comments on commit 4e0fcd6

Please sign in to comment.
Something went wrong with that request. Please try again.