]> gitweb.michael.orlitzky.com - mailshears.git/blob - lib/common/davical_plugin.rb
Rename PostfixadminDb, RoundcubeDb without the Db suffix.
[mailshears.git] / lib / common / davical_plugin.rb
1 require 'common/plugin'
2
3 module DavicalPlugin
4 # Code that all Davical plugins (Prune, Rm, Mv...) will share. That
5 # is, we implement the Plugin interface.
6 include Plugin
7
8 def initialize()
9 cfg = Configuration.new()
10 @db_host = cfg.davical_dbhost
11 @db_port = cfg.davical_dbport
12 @db_opts = cfg.davical_dbopts
13 @db_tty = cfg.davical_dbtty
14 @db_name = cfg.davical_dbname
15 @db_user = cfg.davical_dbuser
16 @db_pass = cfg.davical_dbpass
17 end
18
19
20 def describe_domain(domain)
21 # DAViCal doesn't have a concept of domains.
22 return domain
23 end
24
25
26 def describe_account(account)
27 principal_id = self.get_principal_id(account)
28
29 if principal_id.nil?
30 return 'User not found'
31 else
32 return "Principal ID: #{principal_id}"
33 end
34 end
35
36
37 protected;
38
39 def get_principal_id(account)
40 principal_id = nil
41
42 begin
43 connection = PGconn.connect(@db_host,
44 @db_port,
45 @db_opts,
46 @db_tty,
47 @db_name,
48 @db_user,
49 @db_pass)
50
51 sql_query = "SELECT principal.principal_id "
52 sql_query += "FROM (principal INNER JOIN usr "
53 sql_query += " ON principal.user_no = usr.user_no) "
54 sql_query += "WHERE usr.username = $1;"
55
56 connection.query(sql_query, [account]) do |result|
57 if result.num_tuples > 0
58 principal_id = result[0]['principal_id']
59 end
60 end
61
62 connection.close()
63
64 rescue PGError => e
65 # Pretend like we're database-agnostic in case we ever are.
66 raise DatabaseError.new(e)
67 end
68
69 return principal_id
70 end
71
72
73 def list_users()
74 usernames = []
75
76 begin
77 connection = PGconn.connect(@db_host,
78 @db_port,
79 @db_opts,
80 @db_tty,
81 @db_name,
82 @db_user,
83 @db_pass)
84
85 # User #1 is the super-user, and not tied to an email address.
86 sql_query = "SELECT username FROM usr WHERE user_no > 1"
87
88 connection.query(sql_query) do |result|
89 usernames = result.field_values('username')
90 end
91
92 connection.close()
93 rescue PGError => e
94 # Pretend like we're database-agnostic in case we ever are.
95 raise DatabaseError.new(e)
96 end
97
98 return usernames
99 end
100
101 end