rofi-extras/app/pinentry-rofi.hs

78 lines
2.2 KiB
Haskell
Raw Normal View History

2021-06-25 23:42:29 -04:00
--------------------------------------------------------------------------------
-- | rofi-pinentry - a simply pinentry proxy for bitwarden
--
-- Rather than prompt the user like all the other pinentry programs, call the
-- bitwarden deamon and prompt for a password there
module Main where
2021-06-25 23:57:37 -04:00
import Data.List
import Bitwarden.Internal
import System.Environment
2021-06-25 23:42:29 -04:00
import System.Exit
main :: IO ()
main = do
n <- parseArgs =<< getArgs
2021-06-25 23:42:29 -04:00
putStrLn "Hello loser"
pinentryLoop n
2021-06-25 23:42:29 -04:00
parseArgs :: [String] -> IO String
parseArgs [n] = return n
parseArgs _ = do
putStrLn "Usage: pinentry-rofi [BWNAME]"
exitWith $ ExitFailure 1
pinentryLoop :: String -> IO ()
pinentryLoop n = do
2021-06-25 23:42:29 -04:00
c <- getLine
processLine n $ words c
pinentryLoop n
2021-06-25 23:42:29 -04:00
processLine :: String -> [String] -> IO ()
processLine _ [] = noop
processLine _ ["BYE"] = exitSuccess
processLine n ["GETPIN"] = getPin n
2021-06-25 23:42:29 -04:00
-- TODO this might be important
processLine _ ["OPTION", o] = processOption o
2021-06-25 23:42:29 -04:00
-- TODO this might be important
processLine _ ["GETINFO", _] = noop
2021-06-25 23:42:29 -04:00
-- these all take one arg and should do nothing here
processLine _ ["SETDESC", _] = noop
processLine _ ["SETOK", _] = noop
processLine _ ["SETNOTOK", _] = noop
processLine _ ["SETCANCEL", _] = noop
processLine _ ["SETPROMPT", _] = noop
processLine _ ["SETERROR", _] = noop
2021-06-25 23:42:29 -04:00
-- CONFIRM can take a flag
processLine _ ["CONFIRM"] = noop
processLine _ ["CONFIRM", "--one-button", _] = noop
2021-06-25 23:42:29 -04:00
processLine _ ss = unknownCommand $ unwords ss
2021-06-25 23:42:29 -04:00
unknownCommand :: String -> IO ()
unknownCommand c = putStrLn $ "ERR 275 Unknown command " ++ c
getPin :: String -> IO ()
getPin n = do
2021-06-25 23:57:37 -04:00
its <- getItems
let p = (password . login) =<< find (\i -> n == name i) its
2021-06-25 23:57:37 -04:00
maybe err printPin p
where
err = putStrLn "ERR 83886179 Operation canceled <rofi>"
printPin p = putStrLn ("D " ++ p) >> ok
2021-06-25 23:42:29 -04:00
processOption :: String -> IO ()
processOption = undefined
noop :: IO ()
noop = ok
ok :: IO ()
ok = putStrLn "OK"