-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Prompt.Ssh
-- Description :  An ssh prompt.
-- Copyright   :  (C) 2007 Andrea Rossato
-- License     :  BSD3
--
-- Maintainer  :  andrea.rossato@unibz.it
-- Stability   :  unstable
-- Portability :  unportable
--
-- A ssh prompt for XMonad
--
-----------------------------------------------------------------------------

module XMonad.Prompt.Ssh
    ( -- * Usage
      -- $usage
      sshPrompt,
      Ssh,
    ) where

import XMonad
import XMonad.Prelude
import XMonad.Util.Run
import XMonad.Prompt

import System.Directory
import System.Environment
import Control.Exception as E

econst :: Monad m => a -> IOException -> m a
econst :: forall (m :: * -> *) a. Monad m => a -> IOException -> m a
econst = m a -> IOException -> m a
forall a b. a -> b -> a
const (m a -> IOException -> m a)
-> (a -> m a) -> a -> IOException -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return

-- $usage
-- 1. In your @xmonad.hs@:
--
-- > import XMonad.Prompt
-- > import XMonad.Prompt.Ssh
--
-- 2. In your keybindings add something like:
--
-- >   , ((modm .|. controlMask, xK_s), sshPrompt def)
--
-- Keep in mind, that if you want to use the completion you have to
-- disable the \"HashKnownHosts\" option in your ssh_config
--
-- For detailed instruction on editing the key binding see
-- <https://xmonad.org/TUTORIAL.html#customizing-xmonad the tutorial>.

data Ssh = Ssh

instance XPrompt Ssh where
    showXPrompt :: Ssh -> String
showXPrompt       Ssh
Ssh = String
"SSH to: "
    commandToComplete :: Ssh -> String -> String
commandToComplete Ssh
_ String
c = String
-> ((String, String) -> String) -> Maybe (String, String) -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
c (String, String) -> String
forall a b. (a, b) -> b
snd (String -> Maybe (String, String)
parseHost String
c)
    nextCompletion :: Ssh -> String -> [String] -> String
nextCompletion Ssh
_t String
c [String]
l = String
-> ((String, String) -> String) -> Maybe (String, String) -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
next (\(String
u,String
_h) -> String
u String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"@" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
next) Maybe (String, String)
hostPared
                            where
                              hostPared :: Maybe (String, String)
hostPared = String -> Maybe (String, String)
parseHost String
c
                              next :: String
next = String -> [String] -> String
getNextCompletion (String
-> ((String, String) -> String) -> Maybe (String, String) -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
c (String, String) -> String
forall a b. (a, b) -> b
snd Maybe (String, String)
hostPared) [String]
l

sshPrompt :: XPConfig -> X ()
sshPrompt :: XPConfig -> X ()
sshPrompt XPConfig
c = do
  sc <- IO [String] -> X [String]
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io IO [String]
sshComplList
  mkXPrompt Ssh c (mkComplFunFromList c sc) ssh

ssh :: String -> X ()
ssh :: String -> X ()
ssh = String -> String -> X ()
runInTerm String
"" (String -> X ()) -> (String -> String) -> String -> X ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
"ssh " String -> String -> String
forall a. [a] -> [a] -> [a]
++ )

sshComplList :: IO [String]
sshComplList :: IO [String]
sshComplList = [String] -> [String]
forall a. Ord a => [a] -> [a]
uniqSort ([String] -> [String]) -> IO [String] -> IO [String]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ([String] -> [String] -> [String])
-> IO [String] -> IO [String] -> IO [String]
forall a b c. (a -> b -> c) -> IO a -> IO b -> IO c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
(++) IO [String]
sshComplListLocal IO [String]
sshComplListGlobal

sshComplListLocal :: IO [String]
sshComplListLocal :: IO [String]
sshComplListLocal = do
  h <- String -> IO String
getEnv String
"HOME"
  s1 <- sshComplListFile $ h ++ "/.ssh/known_hosts"
  s2 <- sshComplListConf $ h ++ "/.ssh/config"
  return $ s1 ++ s2

sshComplListGlobal :: IO [String]
sshComplListGlobal :: IO [String]
sshComplListGlobal = do
  env <- String -> IO String
getEnv String
"SSH_KNOWN_HOSTS" IO String -> (IOException -> IO String) -> IO String
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`E.catch` String -> IOException -> IO String
forall (m :: * -> *) a. Monad m => a -> IOException -> m a
econst String
"/nonexistent"
  fs <- mapM fileExists [ env
                        , "/usr/local/etc/ssh/ssh_known_hosts"
                        , "/usr/local/etc/ssh_known_hosts"
                        , "/etc/ssh/ssh_known_hosts"
                        , "/etc/ssh_known_hosts"
                        ]
  case catMaybes fs of
    []    -> [String] -> IO [String]
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return []
    (String
f:[String]
_) -> ComplFunction
sshComplListFile' String
f

sshComplListFile :: String -> IO [String]
sshComplListFile :: ComplFunction
sshComplListFile String
kh = do
  f <- String -> IO Bool
doesFileExist String
kh
  if f then sshComplListFile' kh
       else return []

sshComplListFile' :: String -> IO [String]
sshComplListFile' :: ComplFunction
sshComplListFile' String
kh = do
  l <- String -> IO String
readFile String
kh
  return $ map (getWithPort . takeWhile (/= ',') . concat . take 1 . words)
         $ filter nonComment
         $ lines l

sshComplListConf :: String -> IO [String]
sshComplListConf :: ComplFunction
sshComplListConf String
kh = do
  f <- String -> IO Bool
doesFileExist String
kh
  if f then sshComplListConf' kh
       else return []

sshComplListConf' :: String -> IO [String]
sshComplListConf' :: ComplFunction
sshComplListConf' String
kh = do
  l <- String -> IO String
readFile String
kh
  return $ map (!!1)
         $ filter isHost
         $ map words
         $ lines l
 where
   isHost :: [String] -> Bool
isHost [String]
ws = Int -> [String] -> [String]
forall a. Int -> [a] -> [a]
take Int
1 [String]
ws [String] -> [String] -> Bool
forall a. Eq a => a -> a -> Bool
== [String
"Host"] Bool -> Bool -> Bool
&& [String] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [String]
ws Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1

fileExists :: String -> IO (Maybe String)
fileExists :: String -> IO (Maybe String)
fileExists String
kh = do
  f <- String -> IO Bool
doesFileExist String
kh
  if f then return $ Just kh
       else return Nothing

nonComment :: String -> Bool
nonComment :: String -> Bool
nonComment []      = Bool
False
nonComment (Char
'#':String
_) = Bool
False
nonComment (Char
'|':String
_) = Bool
False -- hashed, undecodeable
nonComment String
_       = Bool
True

getWithPort :: String -> String
getWithPort :: String -> String
getWithPort (Char
'[':String
str) = String
host String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" -p " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
port
    where (String
host,String
p) = (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==Char
']') String
str
          port :: String
port = case String
p of
                   Char
']':Char
':':String
x -> String
x
                   String
_         -> String
"22"
getWithPort  String
str = String
str

parseHost :: String -> Maybe (String, String)
parseHost :: String -> Maybe (String, String)
parseHost String
a = Char -> String -> Maybe Int
forall a. Eq a => a -> [a] -> Maybe Int
elemIndex Char
'@' String
a  Maybe Int
-> (Int -> Maybe (String, String)) -> Maybe (String, String)
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (\Int
c-> (String, String) -> Maybe (String, String)
forall a. a -> Maybe a
Just ( Int -> String -> String
forall a. Int -> [a] -> [a]
take Int
c String
a, Int -> String -> String
forall a. Int -> [a] -> [a]
drop (Int
cInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) String
a ) )