query :: String -> String -> IO [String]
query command args
    | null $ command ++ args = return [] -- fixes a bug where lambdabot never
                                         -- responds, thus hanging GoA
    | otherwise = do
    m <- readIORef state
    E.handle
        (\(e :: E.IOException) ->
          do writeIORef state Nothing -- blank old handles if we fail
             return ["Unable to run lambdabot: " ++ show e])
        (case m of
            Nothing           -> do
              -- maybe we can start the process automatically
              success <- wakeup'
              if success then query command args else return []
            Just (ST i o _ _) -> do
                -- some commands seem to assume no whitespace at the end so we trim it
                let s = reverse . dropWhile isSpace . reverse $ unwords [command,args]
                hPutStrLn i s >> hFlush i
          --    hGetLine o -- throw away irc message (and prompt on first line)
                result <- clean `fmap` getOutput o []
                return (lines result))
        where
        clean x
              | "lambdabot> " `isPrefixOf` x = drop 11 x
              | otherwise                    = x
        -- trim was spoiling some output
        -- trim = dropWhile isSpace . reverse . dropWhile isSpace . reverse

-- |
-- read output until next command is seen
--
query :: String -> String -> IO [String]
query command args
    | null $ command ++ args = return [] -- fixes a bug where lambdabot never
                                         -- responds, thus hanging GoA
    | otherwise = do
    m <- readIORef state
    E.handle
        (\(e :: E.IOException) ->
          do writeIORef state Nothing -- blank old handles if we fail
             return ["Unable to run lambdabot: " ++ show e])
        (case m of
            Nothing           -> do
              -- maybe we can start the process automatically
              success <- wakeup'
              if success then query command args else return []
            Just (ST i o _ _) -> do
                -- some commands seem to assume no whitespace at the end so we trim it
                let s = reverse . dropWhile isSpace . reverse $ unwords [command,args]
                hPutStrLn i s >> hFlush i
          --    hGetLine o -- throw away irc message (and prompt on first line)
                result <- clean `fmap` getOutput o []
                return (lines result))
        where
        clean x
              | "lambdabot> " `isPrefixOf` x = drop 11 x
              | otherwise                    = x
        -- trim was spoiling some output
        -- trim = dropWhile isSpace . reverse . dropWhile isSpace . reverse

-- |
-- read output until next command is seen
--
query command args
    | null $ command ++ args = return [] -- fixes a bug where lambdabot never
                                         -- responds, thus hanging GoA
    | otherwise = do
    m <- readIORef state
    E.handle
        (\(e :: E.IOException) ->
          do writeIORef state Nothing -- blank old handles if we fail
             return ["Unable to run lambdabot: " ++ show e])
        (case m of
            Nothing           -> do
              -- maybe we can start the process automatically
              success <- wakeup'
              if success then query command args else return []
            Just (ST i o _ _) -> do
                -- some commands seem to assume no whitespace at the end so we trim it
                let s = reverse . dropWhile isSpace . reverse $ unwords [command,args]
                hPutStrLn i s >> hFlush i
          --    hGetLine o -- throw away irc message (and prompt on first line)
                result <- clean `fmap` getOutput o []
                return (lines result))
        where
        clean x
              | "lambdabot> " `isPrefixOf` x = drop 11 x
              | otherwise                    = x
        -- trim was spoiling some output
        -- trim = dropWhile isSpace . reverse . dropWhile isSpace . reverse

-- |
-- read output until next command is seen
--