e4bef48ac41239f49b7aa3656adbce782f25bd00
[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 'N/A'
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 end