Class: Lita::Handlers::SshRun

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/ssh_run.rb

Instance Method Summary collapse

Instance Method Details

#flush_redis(response) ⇒ Object



64
65
66
# File 'lib/lita/handlers/ssh_run.rb', line 64

def flush_redis(response)
  redis.flushdb
end

#get_user_pass(response, server, *user_pass) ⇒ Object



38
39
40
41
42
# File 'lib/lita/handlers/ssh_run.rb', line 38

def get_user_pass(response, server, *user_pass)
  user_pass.each do |blah|
    response.reply_privately("No #{blah.capitalize} found for you on #{server}\nPlease privately reply with:\n`Lita, Set #{blah.capitalize} for #{server} to #{blah.upcase}`")
  end
end

#run_ssh(response) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/lita/handlers/ssh_run.rb', line 12

def run_ssh(response)

  cmd = response.matches[0][0]
  server = response.matches[0][1]
  id_server_user = response.user.id + "_" + server + "_username"
  id_server_pass = response.user.id + "_" + server + "_password"
  usernm = redis.get(id_server_user)
  passwd = redis.get(id_server_pass)
  get_user_pass(response, server, "Username") unless usernm
  get_user_pass(response, server, "Password") unless passwd

  if usernm && passwd
    Net::SSH.start(server, usernm, :password => passwd) do |ssh|
      output = ssh.exec!(cmd)
      response.reply("```" + output + "```")
    end
  elsif usernm
    response.reply_with_mention("I was unable to find a password for you on #{server}, please check you direct messages from me.")
  elsif passwd
    response.reply_with_mention("I was unable to find a username for you on #{server}, please check you direct messages from me.")
  else
    response.reply_with_mention("I was unable to find a username or password for you on #{server}, please check you direct messages from me.")
  end

end

#set_user_pass(response) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/lita/handlers/ssh_run.rb', line 44

def set_user_pass(response)
  request = response.matches[0][0]
  server = response.matches[0][1]
  value = response.matches[0][2]
  id_server_user_pass = response.user.id + "_" + server + "_" + request.downcase

  if request && server && value
    redis.set(id_server_user_pass, value)
    response.reply_privately("#{request.capitalize} set")
    id_server_user = response.user.id + "_" + server + "_username"
    id_server_pass = response.user.id + "_" + server + "_password"
    if redis.get(id_server_user) && redis.get(id_server_pass)
      response.reply("Username and Password now set, please rerun your request")

    end
  else
    response.reply_privately("There seems to have been a problem setting that for you")
  end
end