]> gitweb.michael.orlitzky.com - mailshears.git/blob - lib/common/roundcube_plugin.rb
Clean up user/domain describing in the plugins.
[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_user(user)
21 user_id = self.get_user_id(user)
22 return "User ID: #{user_id}"
23 end
24
25
26 def list_users()
27 # Produce a list of Roundcube users. This is used in prune/rm, and
28 # is public because it is useful in testing.
29 usernames = []
30
31 # Just assume PostgreSQL for now.
32 begin
33 connection = PGconn.connect(@db_host,
34 @db_port,
35 @db_opts,
36 @db_tty,
37 @db_name,
38 @db_user,
39 @db_pass)
40
41 sql_query = "SELECT username FROM users;"
42 connection.query(sql_query) do |result|
43 usernames = result.field_values('username')
44 end
45
46 connection.close()
47 rescue PGError => e
48 # Pretend like we're database-agnostic in case we ever are.
49 raise DatabaseError.new(e)
50 end
51
52 return usernames.map{ |u| User.new(u) }
53 end
54
55 protected;
56
57 def get_user_id(user)
58 user_id = nil
59
60 begin
61 connection = PGconn.connect(@db_host,
62 @db_port,
63 @db_opts,
64 @db_tty,
65 @db_name,
66 @db_user,
67 @db_pass)
68
69 sql_query = "SELECT user_id FROM users WHERE username = $1;"
70
71 connection.query(sql_query, [user.to_s()]) do |result|
72 if result.num_tuples > 0
73 user_id = result[0]['user_id']
74 end
75 end
76
77 connection.close()
78
79 rescue PGError => e
80 # Pretend like we're database-agnostic in case we ever are.
81 raise DatabaseError.new(e)
82 end
83
84 return user_id
85 end
86
87
88 end