Overhaul everything to get consistent error reports.
[mailshears.git] / lib / common / roundcube_plugin.rb
1 require 'common/plugin'
2 require 'common/user'
3
4 module RoundcubePlugin
5 # Code that all Roundcube plugins (Prune, Rm, Mv...) will share.
6 # That is, we implement the Plugin interface.
7 include Plugin
8
9 def initialize(cfg)
10 @db_host = cfg.roundcube_dbhost
11 @db_port = cfg.roundcube_dbport
12 @db_opts = cfg.roundcube_dbopts
13 @db_tty = cfg.roundcube_dbtty
14 @db_name = cfg.roundcube_dbname
15 @db_user = cfg.roundcube_dbuser
16 @db_pass = cfg.roundcube_dbpass
17 end
18
19
20 def describe_domain(domain)
21 # Roundcube doesn't have a concept of domains.
22 return domain.to_s()
23 end
24
25 def describe_user(user)
26 user_id = self.get_user_id(user)
27
28 if user_id.nil?
29 return 'User not found'
30 else
31 return "User ID: #{user_id}"
32 end
33 end
34
35
36 def list_users()
37 # Produce a list of Roundcube users. This is used in prune/rm, and
38 # is public because it is useful in testing.
39 usernames = []
40
41 # Just assume PostgreSQL for now.
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 username FROM users;"
52 connection.query(sql_query) do |result|
53 usernames = result.field_values('username')
54 end
55
56 connection.close()
57 rescue PGError => e
58 # Pretend like we're database-agnostic in case we ever are.
59 raise DatabaseError.new(e)
60 end
61
62 return usernames.map{ |u| User.new(u) }
63 end
64
65 protected;
66
67 def get_user_id(user)
68 user_id = nil
69
70 begin
71 connection = PGconn.connect(@db_host,
72 @db_port,
73 @db_opts,
74 @db_tty,
75 @db_name,
76 @db_user,
77 @db_pass)
78
79 sql_query = "SELECT user_id FROM users WHERE username = $1;"
80
81 connection.query(sql_query, [user.to_s()]) do |result|
82 if result.num_tuples > 0
83 user_id = result[0]['user_id']
84 end
85 end
86
87 connection.close()
88
89 rescue PGError => e
90 # Pretend like we're database-agnostic in case we ever are.
91 raise DatabaseError.new(e)
92 end
93
94 return user_id
95 end
96
97
98 end