X-Git-Url: http://gitweb.michael.orlitzky.com/?p=mailshears.git;a=blobdiff_plain;f=lib%2Fcommon%2Froundcube_plugin.rb;h=948a5b91b5577dc5ebd3c45e24ad150b99556c97;hp=1f9805a3e9d079243da75859f6992d7688e6f266;hb=20b843bddcd73833d41f98ff79d92ef59bb4d81e;hpb=bf7d0402eda27d9487ca9402156818545fdda286 diff --git a/lib/common/roundcube_plugin.rb b/lib/common/roundcube_plugin.rb index 1f9805a..948a5b9 100644 --- a/lib/common/roundcube_plugin.rb +++ b/lib/common/roundcube_plugin.rb @@ -1,4 +1,5 @@ require 'common/plugin' +require 'common/user' module RoundcubePlugin # Code that all Roundcube plugins (Prune, Rm, Mv...) will share. @@ -16,19 +17,9 @@ module RoundcubePlugin end - def describe_domain(domain) - # Roundcube doesn't have a concept of domains. - return domain - end - def describe_user(user) user_id = self.get_user_id(user) - - if user_id.nil? - return 'User not found' - else - return "User ID: #{user_id}" - end + return "User ID: #{user_id}" end @@ -37,28 +28,17 @@ module RoundcubePlugin # is public because it is useful in testing. usernames = [] - # Just assume PostgreSQL for now. - begin - connection = PGconn.connect(@db_host, - @db_port, - @db_opts, - @db_tty, - @db_name, - @db_user, - @db_pass) - - sql_query = "SELECT username FROM users;" - connection.query(sql_query) do |result| - usernames = result.field_values('username') - end + connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty, + @db_name, @db_user, @db_pass) - connection.close() - rescue PGError => e - # Pretend like we're database-agnostic in case we ever are. - raise DatabaseError.new(e) + sql_query = 'SELECT username FROM users;' + connection.query(sql_query) do |result| + usernames = result.field_values('username') end - return usernames + connection.close() + + return usernames.map{ |u| User.new(u) } end protected; @@ -66,30 +46,19 @@ module RoundcubePlugin def get_user_id(user) user_id = nil - begin - connection = PGconn.connect(@db_host, - @db_port, - @db_opts, - @db_tty, - @db_name, - @db_user, - @db_pass) - - sql_query = "SELECT user_id FROM users WHERE username = $1;" - - connection.query(sql_query, [user]) do |result| - if result.num_tuples > 0 - user_id = result[0]['user_id'] - end - end + connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty, + @db_name, @db_user, @db_pass) - connection.close() + sql_query = 'SELECT user_id FROM users WHERE username = $1;' - rescue PGError => e - # Pretend like we're database-agnostic in case we ever are. - raise DatabaseError.new(e) + connection.query(sql_query, [user.to_s()]) do |result| + if result.num_tuples > 0 + user_id = result[0]['user_id'] + end end + connection.close() + return user_id end