module XMonad.Prompt.Ssh
(
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 (m :: * -> *) a. Monad m => a -> m a
return
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
[String]
sc <- IO [String] -> X [String]
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io IO [String]
sshComplList
Ssh -> XPConfig -> ComplFunction -> (String -> X ()) -> X ()
forall p.
XPrompt p =>
p -> XPConfig -> ComplFunction -> (String -> X ()) -> X ()
mkXPrompt Ssh
Ssh XPConfig
c (XPConfig -> [String] -> ComplFunction
mkComplFunFromList XPConfig
c [String]
sc) String -> X ()
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 (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
String
h <- String -> IO String
getEnv String
"HOME"
[String]
s1 <- ComplFunction
sshComplListFile ComplFunction -> ComplFunction
forall a b. (a -> b) -> a -> b
$ String
h String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"/.ssh/known_hosts"
[String]
s2 <- ComplFunction
sshComplListConf ComplFunction -> ComplFunction
forall a b. (a -> b) -> a -> b
$ String
h String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"/.ssh/config"
[String] -> IO [String]
forall (m :: * -> *) a. Monad m => a -> m a
return ([String] -> IO [String]) -> [String] -> IO [String]
forall a b. (a -> b) -> a -> b
$ [String]
s1 [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ [String]
s2
sshComplListGlobal :: IO [String]
sshComplListGlobal :: IO [String]
sshComplListGlobal = do
String
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"
[Maybe String]
fs <- (String -> IO (Maybe String)) -> [String] -> IO [Maybe String]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM String -> IO (Maybe String)
fileExists [ String
env
, String
"/usr/local/etc/ssh/ssh_known_hosts"
, String
"/usr/local/etc/ssh_known_hosts"
, String
"/etc/ssh/ssh_known_hosts"
, String
"/etc/ssh_known_hosts"
]
case [Maybe String] -> [String]
forall a. [Maybe a] -> [a]
catMaybes [Maybe String]
fs of
[] -> [String] -> IO [String]
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
Bool
f <- String -> IO Bool
doesFileExist String
kh
if Bool
f then ComplFunction
sshComplListFile' String
kh
else [String] -> IO [String]
forall (m :: * -> *) a. Monad m => a -> m a
return []
sshComplListFile' :: String -> IO [String]
sshComplListFile' :: ComplFunction
sshComplListFile' String
kh = do
String
l <- String -> IO String
readFile String
kh
[String] -> IO [String]
forall (m :: * -> *) a. Monad m => a -> m a
return ([String] -> IO [String]) -> [String] -> IO [String]
forall a b. (a -> b) -> a -> b
$ (String -> String) -> [String] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (String -> String
getWithPort (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
',') (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([String] -> String) -> (String -> [String]) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [String] -> [String]
forall a. Int -> [a] -> [a]
take Int
1 ([String] -> [String])
-> (String -> [String]) -> String -> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
words)
([String] -> [String]) -> [String] -> [String]
forall a b. (a -> b) -> a -> b
$ (String -> Bool) -> [String] -> [String]
forall a. (a -> Bool) -> [a] -> [a]
filter String -> Bool
nonComment
([String] -> [String]) -> [String] -> [String]
forall a b. (a -> b) -> a -> b
$ String -> [String]
lines String
l
sshComplListConf :: String -> IO [String]
sshComplListConf :: ComplFunction
sshComplListConf String
kh = do
Bool
f <- String -> IO Bool
doesFileExist String
kh
if Bool
f then ComplFunction
sshComplListConf' String
kh
else [String] -> IO [String]
forall (m :: * -> *) a. Monad m => a -> m a
return []
sshComplListConf' :: String -> IO [String]
sshComplListConf' :: ComplFunction
sshComplListConf' String
kh = do
String
l <- String -> IO String
readFile String
kh
[String] -> IO [String]
forall (m :: * -> *) a. Monad m => a -> m a
return ([String] -> IO [String]) -> [String] -> IO [String]
forall a b. (a -> b) -> a -> b
$ ([String] -> String) -> [[String]] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map ([String] -> Int -> String
forall a. [a] -> Int -> a
!!Int
1)
([[String]] -> [String]) -> [[String]] -> [String]
forall a b. (a -> b) -> a -> b
$ ([String] -> Bool) -> [[String]] -> [[String]]
forall a. (a -> Bool) -> [a] -> [a]
filter [String] -> Bool
isHost
([[String]] -> [[String]]) -> [[String]] -> [[String]]
forall a b. (a -> b) -> a -> b
$ (String -> [String]) -> [String] -> [[String]]
forall a b. (a -> b) -> [a] -> [b]
map String -> [String]
words
([String] -> [[String]]) -> [String] -> [[String]]
forall a b. (a -> b) -> a -> b
$ String -> [String]
lines String
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 (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
Bool
f <- String -> IO Bool
doesFileExist String
kh
if Bool
f then Maybe String -> IO (Maybe String)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe String -> IO (Maybe String))
-> Maybe String -> IO (Maybe String)
forall a b. (a -> b) -> a -> b
$ String -> Maybe String
forall a. a -> Maybe a
Just String
kh
else Maybe String -> IO (Maybe String)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe String
forall a. Maybe a
Nothing
nonComment :: String -> Bool
[] = Bool
False
nonComment (Char
'#':String
_) = Bool
False
nonComment (Char
'|':String
_) = Bool
False
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 (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 ) )