 tryOneEmail = (ddp, options, cb) ->
   userPrompt "Email: ", options, (err, res) ->
     return cb err if err
-    attemptLogin ddp, {email: res.account}, res.pass, options, cb
+    loginWithEmail ddp, res.account, res.pass, options, cb
 
 tryOneUser = (ddp, options, cb) ->
   userPrompt "Username: ", options, (err, res) ->
     return cb err if err
-    attemptLogin ddp, {user: res.account}, res.pass, options, cb
-
-tryOneEither = (ddp, options, cb) ->
+    loginWithUsername ddp, res.account, res.pass, options, cb
+
+tryOneAccount = (ddp, options, cb) ->
   userPrompt "Account: ", options, (err, res) ->
     return cb err if err
-    if isEmail res.account
-      attemptLogin ddp, {email: res.account}, res.pass, options, (err, tok) ->
-        return cb err, tok unless err and err.error is 400
-        attemptLogin ddp, {user: res.account}, res.pass, options, cb
-    else
-      attemptLogin ddp, {user: res.account}, res.pass, options, cb
+    loginWithAccount ddp, res.account, res.pass, options, cb
 
 #
 # When run standalone, the code below will execute

  m = addr.match matchEmail
  m isnt null

tryOneToken = (ddp, options, cb) ->
  ddp.call 'login', [{ resume: process.env[options.env] }], (err, res) ->
    return cb err, res?.token

attemptLogin = (ddp, user, pass, options, cb) ->
  hash = crypto.createHash 'sha256'
  hash.update pass, 'utf8'
  digest = { digest: hash.digest('hex'), algorithm: 'sha-256' }
  ddp.call 'login', [{user: user, password: digest}], (err, res) ->
    unless err and err.error is 400 and options.plaintext
      if err

    ddp.call 'login', [{user: user, password: pass}], (err, res) ->
      console.error 'Login failed: ', err.message if err
      return cb err, res?.token

loginWithUsername = (ddp, username, password, options..., cb) ->
   attemptLogin ddp, {user: username}, password, options[0], cb

loginWithEmail = (ddp, email, password, options..., cb) ->
   attemptLogin ddp, {email: email}, password, options[0], cb

loginWithAccount = (ddp, account, password, options..., cb) ->
  if isEmail account
    loginWithEmail ddp, account, password, options[0], (err, tok) ->
      return cb err, tok unless err and err.error is 400
      loginWithUsername ddp, {user: account}, password, options[0], cb
  else
    loginWithUsername ddp, {user: account}, password, options[0], cb

loginWithToken = (ddp, token, cb) ->
  ddp.call 'login', [{ resume: token }], cb

tryOneToken = (ddp, options, cb) ->
  loginWithToken ddp, process.env[options.env], (err, res) ->
    return cb err, res?.token

userPrompt = (prompt, options, cb) ->

