require 'common/plugin'
+require 'common/user'
+# Code that all Agendav plugins ({AgendavPrune}, {AgendavRm},
+# {AgendavMv}) share.
module AgendavPlugin
- # Code that all Agendav plugins (Prune, Rm, Mv...) will
- # share. That is, we implement the Plugin interface.
+
+ # We implement the Plugin "interface."
include Plugin
- def initialize()
- cfg = Configuration.new()
+ # Initialize this Agendav {Plugin} with values in *cfg*.
+ #
+ # @param cfg [Configuration] the configuration for this plugin.
+ #
+ def initialize(cfg)
@db_host = cfg.agendav_dbhost
@db_port = cfg.agendav_dbport
@db_opts = cfg.agendav_dbopts
end
- def describe_domain(domain)
- # AgenDAV doesn't have a concept of domains.
- return domain
- end
-
-
- def describe_account(account)
- if self.user_exists(account)
- return "Username: #{account}"
- else
- return 'User not found'
- end
- end
-
-
- protected;
-
-
+ # Return a list of Agendav users.
+ #
+ # @return [Array<User>] a list of users contained in the
+ # Agendav database.
+ #
def list_users()
- usernames = []
-
- # Just assume PostgreSQL for now.
- begin
- connection = PGconn.connect(@db_host,
- @db_port,
- @db_opts,
- @db_tty,
- @db_name,
- @db_user,
- @db_pass)
+ users = []
- sql_query = '(SELECT username FROM prefs)'
- sql_query += 'UNION'
- sql_query += '(SELECT user_from FROM shared);'
+ connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
+ @db_name, @db_user, @db_pass)
- connection.query(sql_query) do |result|
- usernames = result.field_values('username')
- end
+ sql_query = '(SELECT username FROM prefs)'
+ sql_query += 'UNION'
+ sql_query += '(SELECT user_from FROM shared);'
- connection.close()
- rescue PGError => e
- # Pretend like we're database-agnostic in case we ever are.
- raise DatabaseError.new(e)
+ connection.query(sql_query) do |result|
+ users = result.field_values('username')
end
- return usernames
+ connection.close()
+
+ return users.map{ |u| User.new(u) }
end
end