From 7f8654ed6582062a295e1be75ae70e99de41b323 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sun, 6 Oct 2013 16:06:57 -0400 Subject: [PATCH] Rename PostfixadminDb, RoundcubeDb without the Db suffix. Make PostfixadminDb a regular plugin. --- bin/mailshears | 3 -- doc/README | 7 ++-- lib/common/configuration.rb | 18 ++++----- lib/common/davical_plugin.rb | 3 +- ..._mailstore_plugin.rb => dovecot_plugin.rb} | 4 +- ...in_db_plugin.rb => postfixadmin_plugin.rb} | 18 ++++----- ...dcube_db_plugin.rb => roundcube_plugin.rb} | 4 +- .../{dovecot_mailstore.rb => dovecot.rb} | 6 +-- .../plugins/{roundcube_db.rb => roundcube.rb} | 6 +-- lib/prune/plugins/agendav.rb | 8 ++-- lib/prune/plugins/davical.rb | 39 ++----------------- .../{dovecot_mailstore.rb => dovecot.rb} | 6 +-- lib/prune/plugins/postfixadmin.rb | 24 ++++++++++++ lib/prune/plugins/roundcube.rb | 25 ++++++++++++ lib/prune/plugins/roundcube_db.rb | 25 ------------ .../{dovecot_mailstore.rb => dovecot.rb} | 6 +-- .../{postfixadmin_db.rb => postfixadmin.rb} | 6 +-- .../plugins/{roundcube_db.rb => roundcube.rb} | 6 +-- mailshears.example.conf.yml | 16 ++++---- 19 files changed, 109 insertions(+), 121 deletions(-) rename lib/common/{dovecot_mailstore_plugin.rb => dovecot_plugin.rb} (96%) rename lib/common/{postfixadmin_db_plugin.rb => postfixadmin_plugin.rb} (88%) rename lib/common/{roundcube_db_plugin.rb => roundcube_plugin.rb} (95%) rename lib/mv/plugins/{dovecot_mailstore.rb => dovecot.rb} (78%) rename lib/mv/plugins/{roundcube_db.rb => roundcube.rb} (90%) rename lib/prune/plugins/{dovecot_mailstore.rb => dovecot.rb} (79%) create mode 100644 lib/prune/plugins/postfixadmin.rb create mode 100644 lib/prune/plugins/roundcube.rb delete mode 100644 lib/prune/plugins/roundcube_db.rb rename lib/rm/plugins/{dovecot_mailstore.rb => dovecot.rb} (81%) rename lib/rm/plugins/{postfixadmin_db.rb => postfixadmin.rb} (97%) rename lib/rm/plugins/{roundcube_db.rb => roundcube.rb} (93%) diff --git a/bin/mailshears b/bin/mailshears index 37d6601..cb8ee41 100755 --- a/bin/mailshears +++ b/bin/mailshears @@ -57,9 +57,6 @@ cfg.plugins.each do |plugin_file| require "#{mode_name}/plugins/#{plugin_file}" end -# Always enabled, for now. -require "#{mode_name}/plugins/postfixadmin_db" - # And the runners. require "#{mode_name}/#{mode_name}_runner" require "#{mode_name}/#{mode_name}_dummy_runner" diff --git a/doc/README b/doc/README index 1f06558..3537424 100644 --- a/doc/README +++ b/doc/README @@ -46,10 +46,9 @@ while. Mailshears gets a list of users and domains from your PostfixAdmin database. It then delegates to the plugins. A plugin represents -something that you would like to clean up. For example, the -DovecotMailstore plugin represents the physical mail -files/directories. The RoundcubeDb plugin represents a Roundcube SQL -database. +something that you would like to clean up. For example, the Dovecot +plugin represents the physical mail files/directories. The Roundcube +plugin represents a Roundcube SQL database. Each plugin knows how to get a list of its own users and domains. If there's a user or domain in the plugin list that isn't in the diff --git a/lib/common/configuration.rb b/lib/common/configuration.rb index b2a37d0..43ef03b 100644 --- a/lib/common/configuration.rb +++ b/lib/common/configuration.rb @@ -41,15 +41,15 @@ class Configuration d['i_mean_business'] = false - d['dbhost'] = 'localhost' - d['dbport'] = 5432 - d['dbopts'] = '' - d['dbtty'] = '' - d['dbuser'] = 'postgres' - d['dbpass'] = '' - d['dbname'] = 'postfix' - - d['plugins'] = ['dovecot_mailstore', 'roundcube_db'] + d['postfixadmin_dbhost'] = 'localhost' + d['postfixadmin_dbport'] = 5432 + d['postfixadmin_dbopts'] = '' + d['postfixadmin_dbtty'] = '' + d['postfixadmin_dbuser'] = 'postgres' + d['postfixadmin_dbpass'] = '' + d['postfixadmin_dbname'] = 'postfixadmin' + + d['plugins'] = ['postfixadmin', 'dovecot', 'roundcube'] d['mail_root'] = '/var/spool/mail/vhosts' diff --git a/lib/common/davical_plugin.rb b/lib/common/davical_plugin.rb index 1cc06ce..c8a6726 100644 --- a/lib/common/davical_plugin.rb +++ b/lib/common/davical_plugin.rb @@ -82,7 +82,8 @@ module DavicalPlugin @db_user, @db_pass) - sql_query = "SELECT username FROM usr" + # User #1 is the super-user, and not tied to an email address. + sql_query = "SELECT username FROM usr WHERE user_no > 1" connection.query(sql_query) do |result| usernames = result.field_values('username') diff --git a/lib/common/dovecot_mailstore_plugin.rb b/lib/common/dovecot_plugin.rb similarity index 96% rename from lib/common/dovecot_mailstore_plugin.rb rename to lib/common/dovecot_plugin.rb index 90aa02c..2496bae 100644 --- a/lib/common/dovecot_mailstore_plugin.rb +++ b/lib/common/dovecot_plugin.rb @@ -1,8 +1,8 @@ require 'common/plugin' require 'common/filesystem' -module DovecotMailstorePlugin - # Code that all DovecotMailstore plugins (Prune, Rm, Mv...) will +module DovecotPlugin + # Code that all Dovecot plugins (Prune, Rm, Mv...) will # share. That is, we implement the Plugin interface. include Plugin diff --git a/lib/common/postfixadmin_db_plugin.rb b/lib/common/postfixadmin_plugin.rb similarity index 88% rename from lib/common/postfixadmin_db_plugin.rb rename to lib/common/postfixadmin_plugin.rb index da40879..4bfda9d 100644 --- a/lib/common/postfixadmin_db_plugin.rb +++ b/lib/common/postfixadmin_plugin.rb @@ -1,21 +1,21 @@ require 'common/plugin' require 'pg' -module PostfixadminDbPlugin - # Code that all PostfixadminDb plugins (Prune, Rm, Mv...) will +module PostfixadminPlugin + # Code that all Postfixadmin plugins (Prune, Rm, Mv...) will # share. That is, we implement the Plugin interface. include Plugin def initialize() cfg = Configuration.new() - @db_host = cfg.dbhost - @db_port = cfg.dbport - @db_opts = cfg.dbopts - @db_tty = cfg.dbtty - @db_name = cfg.dbname - @db_user = cfg.dbuser - @db_pass = cfg.dbpass + @db_host = cfg.postfixadmin_dbhost + @db_port = cfg.postfixadmin_dbport + @db_opts = cfg.postfixadmin_dbopts + @db_tty = cfg.postfixadmin_dbtty + @db_name = cfg.postfixadmin_dbname + @db_user = cfg.postfixadmin_dbuser + @db_pass = cfg.postfixadmin_dbpass end diff --git a/lib/common/roundcube_db_plugin.rb b/lib/common/roundcube_plugin.rb similarity index 95% rename from lib/common/roundcube_db_plugin.rb rename to lib/common/roundcube_plugin.rb index df5413f..8e9d622 100644 --- a/lib/common/roundcube_db_plugin.rb +++ b/lib/common/roundcube_plugin.rb @@ -1,7 +1,7 @@ require 'common/plugin' -module RoundcubeDbPlugin - # Code that all RoundcubeDb plugins (Prune, Rm, Mv...) will share. +module RoundcubePlugin + # Code that all Roundcube plugins (Prune, Rm, Mv...) will share. # That is, we implement the Plugin interface. include Plugin diff --git a/lib/mv/plugins/dovecot_mailstore.rb b/lib/mv/plugins/dovecot.rb similarity index 78% rename from lib/mv/plugins/dovecot_mailstore.rb rename to lib/mv/plugins/dovecot.rb index f16f116..0eb6888 100644 --- a/lib/mv/plugins/dovecot_mailstore.rb +++ b/lib/mv/plugins/dovecot.rb @@ -1,11 +1,11 @@ require 'common/filesystem' require 'common/mailstore' -require 'common/dovecot_mailstore_plugin' +require 'common/dovecot_plugin' require 'mv/mv_plugin' -class DovecotMailstoreMv < Mailstore +class DovecotMv < Mailstore - include DovecotMailstorePlugin + include DovecotPlugin include MvPlugin diff --git a/lib/mv/plugins/roundcube_db.rb b/lib/mv/plugins/roundcube.rb similarity index 90% rename from lib/mv/plugins/roundcube_db.rb rename to lib/mv/plugins/roundcube.rb index fa29aa6..50a52c3 100644 --- a/lib/mv/plugins/roundcube_db.rb +++ b/lib/mv/plugins/roundcube.rb @@ -1,11 +1,11 @@ require 'pg' -require 'common/roundcube_db_plugin' +require 'common/roundcube_plugin' require 'mv/mv_plugin' -class RoundcubeDbMv +class RoundcubeMv - include RoundcubeDbPlugin + include RoundcubePlugin include MvPlugin diff --git a/lib/prune/plugins/agendav.rb b/lib/prune/plugins/agendav.rb index 1578773..083a3e4 100644 --- a/lib/prune/plugins/agendav.rb +++ b/lib/prune/plugins/agendav.rb @@ -1,23 +1,23 @@ require 'pg' -require 'common/agendav_plugin' require 'prune/prune_plugin' require 'rm/plugins/agendav' class AgendavPrune < AgendavRm - include AgendavPlugin include PrunePlugin def get_leftover_domains(db_domains) - # AgenDAV doesn't have a concept of domains. + # AgenDAV doesn't have a concept of domains. We could parse the + # usernames to see what domains are present, but the point is + # moot: all leftover accounts will be pruned anyway. return [] end def get_leftover_accounts(db_accounts) # Get a list of all users who have logged in to AgenDAV. - ad_accounts = self.get_agendav_usernames() + ad_accounts = self.list_users() return ad_accounts - db_accounts end diff --git a/lib/prune/plugins/davical.rb b/lib/prune/plugins/davical.rb index ae21d9f..d13684e 100644 --- a/lib/prune/plugins/davical.rb +++ b/lib/prune/plugins/davical.rb @@ -1,6 +1,5 @@ require 'pg' -require 'common/davical_plugin' require 'prune/prune_plugin' require 'rm/plugins/davical' @@ -9,52 +8,22 @@ class DavicalPrune < DavicalRm # DAViCal only supports Postgres, so even if we ever are # database-agnostic, this plugin can't be. # - include DavicalPlugin include PrunePlugin def get_leftover_domains(db_domains) - # AgenDAV doesn't have a concept of domains. + # DAViCal doesn't have a concept of domains. We could parse the + # usernames to see what domains are present, but the point is + # moot: all leftover accounts will be pruned anyway. return [] end def get_leftover_accounts(db_accounts) # Get a list of all users who have logged in to DAViCal. - davical_accounts = self.get_davical_usernames() + davical_accounts = self.list_users() return davical_accounts - db_accounts end - protected; - - def get_davical_usernames() - usernames = [] - - begin - connection = PGconn.connect(@db_host, - @db_port, - @db_opts, - @db_tty, - @db_name, - @db_user, - @db_pass) - - # User #1 is the super-user, and not tied to an email address. - sql_query = 'SELECT username FROM usr WHERE user_no > 1;' - - connection.query(sql_query) do |result| - usernames = result.field_values('username') - end - - connection.close() - rescue PGError => e - # Pretend like we're database-agnostic in case we ever are. - raise DatabaseError.new(e) - end - - return usernames - end - - end diff --git a/lib/prune/plugins/dovecot_mailstore.rb b/lib/prune/plugins/dovecot.rb similarity index 79% rename from lib/prune/plugins/dovecot_mailstore.rb rename to lib/prune/plugins/dovecot.rb index eddd822..88e5c1c 100644 --- a/lib/prune/plugins/dovecot_mailstore.rb +++ b/lib/prune/plugins/dovecot.rb @@ -1,10 +1,8 @@ -require 'common/dovecot_mailstore_plugin' require 'prune/prune_plugin' -require 'rm/plugins/dovecot_mailstore' +require 'rm/plugins/dovecot' -class DovecotMailstorePrune < DovecotMailstoreRm +class DovecotPrune < DovecotRm - include DovecotMailstorePlugin include PrunePlugin diff --git a/lib/prune/plugins/postfixadmin.rb b/lib/prune/plugins/postfixadmin.rb new file mode 100644 index 0000000..d085489 --- /dev/null +++ b/lib/prune/plugins/postfixadmin.rb @@ -0,0 +1,24 @@ +require 'pg' + +require 'prune/prune_plugin' +require 'rm/plugins/postfixadmin' + +class PostfixadminPrune < PostfixadminRm + + include PrunePlugin + + #def get_leftover_domains(db_domains) + # AgenDAV doesn't have a concept of domains. We could parse the + # usernames to see what domains are present, but the point is + # moot: all leftover accounts will be pruned anyway. + #return [] +#end + + + #def get_leftover_accounts(db_accounts) + # Get a list of all users who have logged in to AgenDAV. + #ad_accounts = self.list_users() + # return ad_accounts - db_accounts + #end + +end diff --git a/lib/prune/plugins/roundcube.rb b/lib/prune/plugins/roundcube.rb new file mode 100644 index 0000000..77eacfc --- /dev/null +++ b/lib/prune/plugins/roundcube.rb @@ -0,0 +1,25 @@ +require 'pg' + +require 'prune/prune_plugin' +require 'rm/plugins/roundcube' + +class RoundcubePrune < RoundcubeRm + + include PrunePlugin + + + def get_leftover_domains(db_domains) + # Roundcube doesn't have a concept of domains. We could parse the + # usernames to see what domains are present, but the point is + # moot: all leftover accounts will be pruned anyway. + return [] + end + + + def get_leftover_accounts(db_accounts) + # Get a list of all users who have logged in to Roundcube. + rc_accounts = self.list__users() + return rc_accounts - db_accounts + end + +end diff --git a/lib/prune/plugins/roundcube_db.rb b/lib/prune/plugins/roundcube_db.rb deleted file mode 100644 index 6515a7b..0000000 --- a/lib/prune/plugins/roundcube_db.rb +++ /dev/null @@ -1,25 +0,0 @@ -require 'pg' - -require 'common/roundcube_db_plugin' -require 'prune/prune_plugin' -require 'rm/plugins/roundcube_db' - -class RoundcubeDbPrune < RoundcubeDbRm - - include RoundcubeDbPlugin - include PrunePlugin - - - def get_leftover_domains(db_domains) - # Roundcube doesn't have a concept of domains. - return [] - end - - - def get_leftover_accounts(db_accounts) - # Get a list of all users who have logged in to Roundcube. - rc_accounts = self.get_roundcube_usernames() - return rc_accounts - db_accounts - end - -end diff --git a/lib/rm/plugins/dovecot_mailstore.rb b/lib/rm/plugins/dovecot.rb similarity index 81% rename from lib/rm/plugins/dovecot_mailstore.rb rename to lib/rm/plugins/dovecot.rb index 8647b01..4283e9c 100644 --- a/lib/rm/plugins/dovecot_mailstore.rb +++ b/lib/rm/plugins/dovecot.rb @@ -1,12 +1,12 @@ # Needed for rm_r. require 'fileutils' -require 'common/dovecot_mailstore_plugin' +require 'common/dovecot_plugin' require 'rm/rm_plugin' -class DovecotMailstoreRm +class DovecotRm - include DovecotMailstorePlugin + include DovecotPlugin include RmPlugin diff --git a/lib/rm/plugins/postfixadmin_db.rb b/lib/rm/plugins/postfixadmin.rb similarity index 97% rename from lib/rm/plugins/postfixadmin_db.rb rename to lib/rm/plugins/postfixadmin.rb index ca91b8b..3883e86 100644 --- a/lib/rm/plugins/postfixadmin_db.rb +++ b/lib/rm/plugins/postfixadmin.rb @@ -1,11 +1,11 @@ require 'pg' -require 'common/postfixadmin_db_plugin' +require 'common/postfixadmin_plugin' require 'rm/rm_plugin' -class PostfixadminDbRm +class PostfixadminRm - include PostfixadminDbPlugin + include PostfixadminPlugin include RmPlugin diff --git a/lib/rm/plugins/roundcube_db.rb b/lib/rm/plugins/roundcube.rb similarity index 93% rename from lib/rm/plugins/roundcube_db.rb rename to lib/rm/plugins/roundcube.rb index 5cda273..3dbee8e 100644 --- a/lib/rm/plugins/roundcube_db.rb +++ b/lib/rm/plugins/roundcube.rb @@ -1,11 +1,11 @@ require 'pg' -require 'common/roundcube_db_plugin' +require 'common/roundcube_plugin' require 'rm/rm_plugin' -class RoundcubeDbRm +class RoundcubeRm - include RoundcubeDbPlugin + include RoundcubePlugin include RmPlugin def delete_account(account) diff --git a/mailshears.example.conf.yml b/mailshears.example.conf.yml index 2ab0fdc..fd703ff 100644 --- a/mailshears.example.conf.yml +++ b/mailshears.example.conf.yml @@ -1,14 +1,14 @@ i_mean_business: false -dbhost: localhost -dbport: 5432 -dbopts: -dbtty: -dbuser: postgres -dbpass: -dbname: postfix +postfixadmin_dbhost: localhost +postfixadmin_dbport: 5432 +postfixadmin_dbopts: +postfixadmin_dbtty: +postfixadmin_dbuser: postgres +postfixadmin_dbpass: +postfixadmin_dbname: postfixadmin -plugins: [dovecot_mailstore, roundcube_db, agendav, davical] +plugins: [postfixadmin, dovecot, roundcube, agendav, davical] mail_root: /var/spool/mail/vhosts -- 2.44.2