Module: Lich::Common::CLI::CLIOrchestration

Defined in:
documented/common/cli/cli_orchestration.rb

Overview

Orchestrates CLI operations: early-exit handlers for password management, data conversion, and login flow. Uses CliOptionsRegistry for declarative option registration and handler execution.

Class Method Summary collapse

Class Method Details

.check_conversion_needed_for_login ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Checks whether the legacy entry.dat format requires conversion before login is attempted.

If conversion is needed, prints a help message and exits with status 1.



66
67
68
69
70
71
72
# File 'documented/common/cli/cli_orchestration.rb', line 66

def self.
  # Check if conversion is required
  if Lich::Common::CLI::CLIConversion.conversion_needed?(DATA_DIR)
    Lich::Common::CLI::CLIConversion.print_conversion_help_message
    exit 1
  end
end

.execute ⇒ Object

Execute registered CLI operations Processes ARGV for early-exit CLI operations (password mgmt, conversion) Also handles conversion detection for login attempts



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'documented/common/cli/cli_orchestration.rb', line 27

def self.execute
  ActiveSessionsQuery.execute

  ARGV.each do |arg|
    case arg
    when /^--change-account-password$/, /^-cap$/
      
    when /^--add-account$/, /^-aa$/
      
    when /^--refresh-characters$/, /^-rc$/
      handle_refresh_characters
    when /^--add-character$/, /^-ac$/
      handle_add_character
    when /^--change-master-password$/, /^-cmp$/
      handle_change_master_password
    when /^--recover-master-password$/, /^-rmp$/
      handle_recover_master_password
    when /^--convert-entries$/
      handle_convert_entries
    when /^--change-encryption-mode$/, /^-cem$/
      handle_change_encryption_mode
    when /^--web-login-test$/
      
    end
  end

  # Check for conversion needed before login attempt
  # This is not an early-exit operation - it detects a precondition for login
  if ARGV.include?('--login')
    
  end
end

.handle_add_account ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Adds a new account with an optional frontend preference.

Reads ACCOUNT and PASSWORD from ARGV following the --add-account or -aa flag, and an optional --frontend flag. If the entry.yaml file does not exist but legacy entry.dat is present, automatically converts to plaintext YAML with a security warning. Validates ACCOUNT (rejects option-shaped values) and PASSWORD (presence only). Exits with the result code from CLIPassword.add_account or with status 1 on validation failure.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'documented/common/cli/cli_orchestration.rb', line 109

def self.
  idx = ARGV.index { |a| a =~ /^--add-account$|^-aa$/ }
   = ARGV[idx + 1]
  password = ARGV[idx + 2]

  lich_script = File.join(LICH_DIR, 'lich.rbw')
  usage = "Usage: ruby #{lich_script} --add-account ACCOUNT PASSWORD [--frontend FRONTEND]\n" \
          "   or: ruby #{lich_script} -aa ACCOUNT PASSWORD [--frontend FRONTEND]"

   = CliOptionValidator.require_positional(, name: 'ACCOUNT', usage: usage)
  # PASSWORD is only checked for presence: rejecting option-shaped values
  # would also refuse any password that starts with '-'.
  if password.nil?
    $stdout.puts 'error: Missing required arguments'
    $stdout.puts usage
    exit 1
  end

  # Validated before the legacy conversion below, so a malformed command
  # exits without writing entry.yaml. ACCOUNT and PASSWORD are left out of
  # the search so a password of literally '--frontend' isn't read as the flag.
  frontend = CliOptionValidator.extract_flag_value(
    '--frontend',
    usage: usage,
    valid_values: Lich::Common::Authentication::LoginHelpers::VALID_FRONTENDS,
    argv: ARGV[0..idx] + ARGV.drop(idx + 3)
  )

  # Check if YAML file exists; if not, check for DAT and auto-convert to plaintext
  yaml_file = Lich::Common::Authentication::EntryStore.yaml_file_path(DATA_DIR)
  unless File.exist?(yaml_file)
    if Lich::Common::CLI::CLIConversion.conversion_needed?(DATA_DIR)
      $stdout.puts ''
      $stdout.puts '=' * 80
      $stdout.puts 'WARNING: No YAML entry file found. Legacy entry.dat detected.'
      $stdout.puts 'Creating plaintext YAML file to support this operation.'
      $stdout.puts '=' * 80
      $stdout.puts ''
      $stdout.puts 'SECURITY NOTICE: Plaintext storage is not recommended except for'
      $stdout.puts 'accessibility requirements. Passwords will be stored in clear text.'
      $stdout.puts ''
      $stdout.puts 'To upgrade encryption later, use:'
      $stdout.puts "  ruby #{File.join(LICH_DIR, 'lich.rbw')} --change-encryption-mode enhanced"
      $stdout.puts '  or: -cem enhanced'
      $stdout.puts '=' * 80
      $stdout.puts ''

      # Perform plaintext conversion
      success = Lich::Common::CLI::CLIConversion.convert(DATA_DIR, :plaintext)
      unless success
        $stdout.puts 'error: Failed to create YAML file from legacy data.'
        exit 1
      end
      # Continue with add-account operation below
    end
    # No DAT file either - CLIPassword.add_account will create new YAML
  end

  exit Lich::Common::Authentication::CLIPassword.(, password, frontend)
end

.handle_add_character ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Adds a new character to an account.

Reads ACCOUNT and CHAR_NAME from ARGV following the --add-character or -ac flag, --game-code CODE (required), and an optional --frontend flag. Validates all positional arguments (rejects option-shaped values) and game code against VALID_GAME_CODES. Exits with the result code from CLIPassword.add_character or with status 1 on validation failure.



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'documented/common/cli/cli_orchestration.rb', line 207

def self.handle_add_character
  idx = ARGV.index { |a| a =~ /^--add-character$|^-ac$/ }
   = ARGV[idx + 1]
  char_name = ARGV[idx + 2]

  lich_script = File.join(LICH_DIR, 'lich.rbw')
  usage = "Usage: ruby #{lich_script} --add-character ACCOUNT CHAR_NAME --game-code CODE [--frontend FRONTEND]\n" \
          "   or: ruby #{lich_script} -ac ACCOUNT CHAR_NAME --game-code CODE [--frontend FRONTEND]"

   = CliOptionValidator.require_positional(, name: 'ACCOUNT', usage: usage)
  char_name = CliOptionValidator.require_positional(char_name, name: 'CHAR_NAME', usage: usage)

  game_code = CliOptionValidator.extract_flag_value('--game-code', usage: usage)
  if game_code.nil?
    $stdout.puts 'error: --game-code is required'
    $stdout.puts usage
    exit 1
  end

  unless Lich::Common::Authentication::LoginHelpers.valid_game_code?(game_code)
    CliOptionValidator.reject_invalid_value(
      '--game-code',
      game_code,
      valid_values: Lich::Common::Authentication::LoginHelpers::VALID_GAME_CODES,
      usage: usage
    )
  end

  frontend = CliOptionValidator.extract_flag_value(
    '--frontend',
    usage: usage,
    valid_values: Lich::Common::Authentication::LoginHelpers::VALID_FRONTENDS
  )

  exit Lich::Common::Authentication::CLIPassword.add_character(
    ,
    char_name,
    game_code: game_code,
    frontend: frontend
  )
end

.handle_change_account_password ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Changes the password for an existing account.

Reads ACCOUNT and NEWPASSWORD from ARGV following the --change-account-password or -cap flag. Validates both arguments are present, prints usage and exits with status 1 on error, or exits with the result code from CLIPassword.change_account_password.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'documented/common/cli/cli_orchestration.rb', line 82

def self.
  idx = ARGV.index { |a| a =~ /^--change-account-password$|^-cap$/ }
   = ARGV[idx + 1]
  new_password = ARGV[idx + 2]

  if .nil? || new_password.nil?
    lich_script = File.join(LICH_DIR, 'lich.rbw')
    $stdout.puts 'error: Missing required arguments'
    $stdout.puts "Usage: ruby #{lich_script} --change-account-password ACCOUNT NEWPASSWORD"
    $stdout.puts "   or: ruby #{lich_script} -cap ACCOUNT NEWPASSWORD"
    exit 1
  end

  exit Lich::Common::Authentication::CLIPassword.(, new_password)
end

.handle_change_encryption_mode ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Changes the encryption mode for stored entries.

Reads MODE from ARGV following the --change-encryption-mode or -cem flag, and an optional --master-password or -mp flag for providing a master password (used when automating enhanced mode). MODE must be one of: plaintext, standard, or enhanced. Exits with the result code from EncryptionModeChange.change_mode or with status 1 if MODE is missing.



362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'documented/common/cli/cli_orchestration.rb', line 362

def self.handle_change_encryption_mode
  idx = ARGV.index { |a| a =~ /^--change-encryption-mode$|^-cem$/ }
  mode_arg = ARGV[idx + 1]

  if mode_arg.nil?
    lich_script = File.join(LICH_DIR, 'lich.rbw')
    $stdout.puts 'error: Missing encryption mode'
    $stdout.puts "Usage: ruby #{lich_script} --change-encryption-mode MODE [--master-password PASSWORD]"
    $stdout.puts "       ruby #{lich_script} -cem MODE [-mp PASSWORD]"
    $stdout.puts 'Modes: plaintext, standard, enhanced'
    exit 1
  end

  new_mode = mode_arg.to_sym

  # Check for optional master password (for Enhanced mode, if automating)
  mp_index = ARGV.index('--master-password') || ARGV.index('-mp')
  master_password = ARGV[mp_index + 1] if mp_index

  exit Lich::Common::CLI::EncryptionModeChange.change_mode(new_mode, master_password)
end

.handle_change_master_password ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Changes the master password protecting encrypted entries.

Reads OLDPASSWORD and optional NEWPASSWORD from ARGV following the --change-master-password or -cmp flag. OLDPASSWORD is required; if NEWPASSWORD is not provided, the user will be prompted interactively for confirmation. Exits with the result code from CLIPassword.change_master_password or with status 1 if OLDPASSWORD is missing.



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'documented/common/cli/cli_orchestration.rb', line 259

def self.handle_change_master_password
  idx = ARGV.index { |a| a =~ /^--change-master-password$|^-cmp$/ }
  old_password = ARGV[idx + 1]
  new_password = ARGV[idx + 2]

  if old_password.nil?
    lich_script = File.join(LICH_DIR, 'lich.rbw')
    $stdout.puts 'error: Missing required arguments'
    $stdout.puts "Usage: ruby #{lich_script} --change-master-password OLDPASSWORD [NEWPASSWORD]"
    $stdout.puts "   or: ruby #{lich_script} -cmp OLDPASSWORD [NEWPASSWORD]"
    $stdout.puts 'Note: If NEWPASSWORD is not provided, you will be prompted for confirmation'
    exit 1
  end

  exit Lich::Common::Authentication::CLIPassword.change_master_password(old_password, new_password)
end

.handle_convert_entries ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Converts entries to a new encryption mode.

Reads ENCRYPTION_MODE from ARGV following the --convert-entries flag. ENCRYPTION_MODE must be one of: plaintext, standard, or enhanced. For enhanced mode, prompts for and stores a new master password in the keychain before conversion. Exits with status 0 on success or status 1 on validation failure, password creation cancellation, or conversion failure.



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'documented/common/cli/cli_orchestration.rb', line 302

def self.handle_convert_entries
  idx = ARGV.index('--convert-entries')
  encryption_mode_str = ARGV[idx + 1]

  if encryption_mode_str.nil?
    lich_script = File.join(LICH_DIR, 'lich.rbw')
    $stdout.puts 'error: Missing required argument'
    $stdout.puts "Usage: ruby #{lich_script} --convert-entries [plaintext|standard|enhanced]"
    exit 1
  end

  unless %w[plaintext standard enhanced].include?(encryption_mode_str)
    $stdout.puts "error: Invalid encryption mode: #{encryption_mode_str}"
    $stdout.puts 'Valid modes: plaintext, standard, enhanced'
    exit 1
  end

  # For enhanced mode, prompt for master password and store in keychain before conversion
  # This way migrate_from_legacy will find it in keychain and not try to show GUI dialog
  if encryption_mode_str == 'enhanced'
    master_password = Lich::Common::Authentication::CLIPassword.prompt_and_confirm_password('Enter new master password for enhanced encryption')
    if master_password.nil?
      puts 'error: Master password creation cancelled'
      exit 1
    end

    # Store password in keychain so ensure_master_password_exists finds it
    require_relative '../gui/master_password_manager'
    stored = Lich::Common::GUI::MasterPasswordManager.store_master_password(master_password)
    unless stored
      puts 'error: Failed to store master password in keychain'
      exit 1
    end
  end

  # Perform conversion
  success = Lich::Common::CLI::CLIConversion.convert(
    DATA_DIR,
    encryption_mode_str
  )

  if success
    $stdout.puts 'Conversion completed successfully!'
    exit 0
  else
    $stdout.puts 'Conversion failed. Please check the logs for details.'
    exit 1
  end
end

.handle_recover_master_password ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Recovers the master password by setting a new one.

Reads optional NEWPASSWORD from ARGV following the --recover-master-password or -rmp flag. If NEWPASSWORD is not provided, the user will be prompted interactively. Exits with the result code from CLIPassword.recover_master_password.



284
285
286
287
288
289
290
# File 'documented/common/cli/cli_orchestration.rb', line 284

def self.handle_recover_master_password
  idx = ARGV.index { |a| a =~ /^--recover-master-password$|^-rmp$/ }
  new_password = ARGV[idx + 1]

  # new_password is optional - if not provided, user will be prompted interactively
  exit Lich::Common::Authentication::CLIPassword.recover_master_password(new_password)
end

.handle_refresh_characters ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Refreshes the character list for an account from the authentication service.

Reads ACCOUNT from ARGV following the --refresh-characters or -rc flag, and an optional --frontend flag. Validates ACCOUNT (rejects option-shaped values). Exits with the result code from CLIPassword.refresh_characters or with status 1 on validation failure.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'documented/common/cli/cli_orchestration.rb', line 179

def self.handle_refresh_characters
  idx = ARGV.index { |a| a =~ /^--refresh-characters$|^-rc$/ }
   = ARGV[idx + 1]

  lich_script = File.join(LICH_DIR, 'lich.rbw')
  usage = "Usage: ruby #{lich_script} --refresh-characters ACCOUNT [--frontend FRONTEND]\n" \
          "   or: ruby #{lich_script} -rc ACCOUNT [--frontend FRONTEND]"

   = CliOptionValidator.require_positional(, name: 'ACCOUNT', usage: usage)

  frontend = CliOptionValidator.extract_flag_value(
    '--frontend',
    usage: usage,
    valid_values: Lich::Common::Authentication::LoginHelpers::VALID_FRONTENDS
  )
  exit Lich::Common::Authentication::CLIPassword.refresh_characters(, frontend)
end

.handle_web_login_test ⇒ void

This method returns an undefined value.

Standalone probe of the HTTPS web-login fallback path (see docs/connection/web-login-protocol-analysis.md). Exercises WebLogin.auth_with_timeout directly against play.net -- independent of the real login path (Authenticator.authenticate), which also uses WebLogin, either forced via --auth-provider=web or automatically as a fallback when EAccess is unreachable.



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'documented/common/cli/cli_orchestration.rb', line 392

def self.
  idx = ARGV.index('--web-login-test')
   = ARGV[idx + 1]
  char_name = ARGV[idx + 2]

  lich_script = File.join(LICH_DIR, 'lich.rbw')
  usage = "Usage: ruby #{lich_script} --web-login-test ACCOUNT CHAR_NAME --game-code CODE\n" \
          "Reads the account's password from data/entry.yaml (ACCOUNT must already be saved there).\n" \
          'This is a standalone probe of the HTTPS web-login fallback path -- it does NOT ' \
          'touch the normal EAccess login flow.'

   = CliOptionValidator.require_positional(, name: 'ACCOUNT', usage: usage)
  char_name = CliOptionValidator.require_positional(char_name, name: 'CHAR_NAME', usage: usage)

  game_code = CliOptionValidator.extract_flag_value(
    '--game-code',
    usage: usage,
    valid_values: Lich::Common::Authentication::LoginHelpers::VALID_GAME_CODES
  )
  if game_code.nil?
    $stdout.puts 'error: --game-code is required'
    $stdout.puts usage
    exit 1
  end

  entries = Lich::Common::Authentication::EntryStore.load_saved_entries(DATA_DIR, false)
  entry = entries.find { |e| e[:user_id].to_s.casecmp?() }
  if entry.nil?
    $stdout.puts "error: Account '#{}' not found in #{Lich::Common::Authentication::EntryStore.yaml_file_path(DATA_DIR)}"
    exit 1
  end

  $stdout.puts "Probing web login fallback for #{} / #{char_name} (#{game_code})..."

  begin
     = Lich::Common::Authentication::WebLogin.auth_with_timeout(
      account: ,
      password: entry[:password],
      character: char_name,
      game_code: game_code
    )
    $stdout.puts 'Success:'
    # KEY is a live, usable one-time game-server credential -- printing
    # it would leave a real secret in terminal scrollback/log capture
    # for a probe that never consumes it. Only non-secret connection
    # metadata is shown.
    .each { |k, v| $stdout.puts "  #{k.upcase}=#{k == 'key' ? '[scrubbed]' : v}" }
    exit 0
  rescue Lich::Common::Authentication::WebLogin::AuthenticationError => e
    $stdout.puts "error: web login failed: #{e.error_code}"
    exit 1
  rescue StandardError => e
    $stdout.puts "error: #{e.class}: #{e.message}"
    exit 1
  end
end