class Ayadn::CheckAccounts

Attributes

active_account[RW]
db[RW]
handle[RW]
paths[RW]
sql_path[RW]
userDB[RW]

Public Class Methods

new() click to toggle source
Calls superclass method Ayadn::CheckBase::new
# File lib/ayadn/diagnostics.rb, line 144
def initialize
  super
  @sql_path = Dir.home + "/ayadn/accounts.sqlite"
end

Public Instance Methods

check() click to toggle source
# File lib/ayadn/diagnostics.rb, line 149
def check
  begin
    @status.say_header("checking accounts database")
    if find_active_account
      users = @db.execute("SELECT * FROM Accounts")
      if users.blank?
        @status.say_red(:abort, "no registered Ayadn users")
      else
        @status.say_green(:accounts, users.map { |user| user[2] }.join(", "))
        if @active_account.blank?
          @status.say_red(:warning, "no active account")
        else
          @status.say_header("checking active account")
          check_id_handle
          check_token
          check_paths
          check_config
          @status.say_header("checking #{@handle}'s account database")
          if find_active_tables
            check_tables_schemas
          end
        end
      end
    end
  rescue Interrupt
    @status.say_error "operation canceled"
    exit
  rescue Amalgalite::SQLite3::Error => e
    @status.say_error "database error"
    @status.say_trace e
  rescue => e
    @status.say_error "unknown error"
    @status.say_trace e
  end
end

Private Instance Methods

check_TLIndex(table) click to toggle source
# File lib/ayadn/diagnostics.rb, line 318
def check_TLIndex(table)
  if table[1].columns.count != 3
    @status.say_red :error, "#{table[0]} table is corrupted"
    return false
  else
    table[1].columns.each do |name, data|
      if name == "count" && data.declared_data_type == "INTEGER"
        @status.say_green(name.to_sym, "OK")
      elsif name == "post_id" && data.declared_data_type == "INTEGER"
        @status.say_green(name.to_sym, "OK")
      elsif name == "content" && data.declared_data_type == "TEXT"
        @status.say_green(name.to_sym, "OK")
      else
        @status.say_red :error, "#{table[0]} table is corrupted"
        return false
      end
    end
  end
  return true
end
check_aliases(table) click to toggle source
# File lib/ayadn/diagnostics.rb, line 240
def check_aliases(table)
  if table[1].columns.count != 2
    @status.say_red :error, "#{table[0]} table is corrupted"
    return false
  else
    table[1].columns.each do |name, data|
      if name == "channel_id" && data.declared_data_type == "INTEGER"
        @status.say_green(name.to_sym, "OK")
      elsif name == "alias" && data.declared_data_type == "VARCHAR(255)"
        @status.say_green(name.to_sym, "OK")
      else
        @status.say_red :error, "#{table[0]} table is corrupted"
        return false
      end
    end
  end
  return true
end
check_blacklist(table) click to toggle source
# File lib/ayadn/diagnostics.rb, line 259
def check_blacklist(table)
  if table[1].columns.count != 2
    @status.say_red :error, "#{table[0]} table is corrupted"
    return false
  else
    table[1].columns.each do |name, data|
      if name == "type" && data.declared_data_type == "VARCHAR(255)"
        @status.say_green(name.to_sym, "OK")
      elsif name == "content" && data.declared_data_type == "TEXT"
        @status.say_green(name.to_sym, "OK")
      else
        @status.say_red :error, "#{table[0]} table is corrupted"
        return false
      end
    end
  end
  return true
end
check_bookmarks(table) click to toggle source
# File lib/ayadn/diagnostics.rb, line 221
def check_bookmarks(table)
  if table[1].columns.count != 2
    @status.say_red :error, "#{table[0]} table is corrupted"
    return false
  else
    table[1].columns.each do |name, data|
      if name == "post_id" && data.declared_data_type == "INTEGER"
        @status.say_green(name.to_sym, "OK")
      elsif name == "bookmark" && data.declared_data_type == "TEXT"
        @status.say_green(name.to_sym, "OK")
      else
        @status.say_red :error, "#{table[0]} table is corrupted"
        return false
      end
    end
  end
  return true
end
check_config() click to toggle source
# File lib/ayadn/diagnostics.rb, line 339
def check_config
  @status.say_header("checking active account settings")
  config_path = @paths[:config] + "/config.yml"
  config = YAML.load(File.read(config_path))
  if config.blank?
    @status.say_error("active user has no config file")
  else
    @status.say_green(:found, "active user config file")
  end
end
check_id_handle() click to toggle source
# File lib/ayadn/diagnostics.rb, line 350
def check_id_handle
  id = @active_account[1]
  @status.say_green(:id, id)
  @handle = @active_account[2]
  @status.say_green(:username, @handle)
end
check_pagination(table) click to toggle source
# File lib/ayadn/diagnostics.rb, line 299
def check_pagination(table)
  if table[1].columns.count != 2
    @status.say_red :error, "#{table[0]} table is corrupted"
    return false
  else
    table[1].columns.each do |name, data|
      if name == "name" && data.declared_data_type == "TEXT"
        @status.say_green(name.to_sym, "OK")
      elsif name == "post_id" && data.declared_data_type == "INTEGER"
        @status.say_green(name.to_sym, "OK")
      else
        @status.say_red :error, "#{table[0]} table is corrupted"
        return false
      end
    end
  end
  return true
end
check_paths() click to toggle source
# File lib/ayadn/diagnostics.rb, line 357
def check_paths
  home = @active_account[3]
  @status.say_green(:path, home)
  @paths = {
    log: "#{home}/log",
    db: "#{home}/db",
    config: "#{home}/config",
    downloads: "#{home}/downloads",
    posts: "#{home}/posts",
    messages: "#{home}/messages",
    lists: "#{home}/lists"
  }
  @paths.each do |key, value|
    if Dir.exist?(value)
      @status.say_green(key, value)
    else
      @status.say_red(:missing, value)
    end
  end
end
check_tables_schemas() click to toggle source
# File lib/ayadn/diagnostics.rb, line 196
def check_tables_schemas
  @status.say_header("checking tables schemas")
  if @userDB.schema.tables.count != 6
    @status.say_red :error, "#{@handle}'s account database is corrupted"
  else
    @userDB.schema.tables.each do |table|
      @status.say_info "checking table #{table[0]}"
      case table[0]
      when "Bookmarks"
        break if !check_bookmarks(table)
      when "Aliases"
        break if !check_aliases(table)
      when "Blacklist"
        break if !check_blacklist(table)
      when "Users"
        break if !check_users(table)
      when "Pagination"
        break if !check_pagination(table)
      when "TLIndex"
        break if !check_TLIndex(table)
      end
    end
  end
end
check_token() click to toggle source
# File lib/ayadn/diagnostics.rb, line 187
def check_token
  token = @active_account[5]
  if token.blank?
    @status.say_red(:missing, "authorization token")
  else
    @status.say_green(:auth_token, token[0..20] + "...")
  end
end
check_users(table) click to toggle source
# File lib/ayadn/diagnostics.rb, line 278
def check_users(table)
  if table[1].columns.count != 3
    @status.say_red :error, "#{table[0]} table is corrupted"
    return false
  else
    table[1].columns.each do |name, data|
      if name == "user_id" && data.declared_data_type == "INTEGER"
        @status.say_green(name.to_sym, "OK")
      elsif name == "username" && data.declared_data_type == "VARCHAR(20)"
        @status.say_green(name.to_sym, "OK")
      elsif name == "name" && data.declared_data_type == "TEXT"
        @status.say_green(name.to_sym, "OK")
      else
        @status.say_red :error, "#{table[0]} table is corrupted"
        return false
      end
    end
  end
  return true
end
find_active_account() click to toggle source
# File lib/ayadn/diagnostics.rb, line 378
def find_active_account
  if File.exist?(@sql_path)
    @status.say_green(:found, "database accounts file")
    @db = Amalgalite::Database.new(@sql_path)
    if @db.nil?
      @status.say_red :error, "accounts database is not readable"
      return false
    else
      @active_account = Databases.active_account(@db)
    end
  else
    @status.say_red :error, "accounts database is missing"
    return false
  end
  return true
end
find_active_tables() click to toggle source
# File lib/ayadn/diagnostics.rb, line 395
def find_active_tables
  tables_path = @paths[:db] + "/ayadn.sqlite"
  if File.exist?(tables_path)
    @status.say_green(:found, "#{@handle}'s database file")
    @userDB = Amalgalite::Database.new(tables_path)
    if @userDB.nil?
      @status.say_red :error, "#{@handle}'s database is not readable"
      return false
    end
  else
    @status.say_red :error, "#{@handle}'s database is missing"
    return false
  end
  return true
end