verifyGPG :: ByteString
          -> ByteString
          -> ByteString
          -> IO Bool
verifyGPG kid sig txt = do
    ctx <- Gpg.ctxNew Nothing
    logDebug $ "Verifying signature "  ++ show sig ++ " for " ++ show txt
    res <- Ex.try $ Gpg.verifyDetach ctx txt sig -- Gpg.Error
    case res of
        Left (e :: Gpg.Error) -> do
            errorM "Pontarius.Xmpp"
                $ "Verifying signature threw exception" ++ show e
            return False
        Right [st] -> do
            gpgGuard ("could not verify signature: " ++ show st)
                (goodStat $ Gpg.status st)
            gpgGuard ("Fingerpringt doesn't match: " ++ show kid
                       ++ " /= " ++ show (Gpg.fingerprint st))
                     (Gpg.fingerprint st == kid)
            debugM "Pontarius.Xmpp" $ "Signature seems good"
            return True
        Right [] -> do
            errorM "Pontarius.Xmpp" "verifyGPG: Could not import pubkey"
            return False
        Right _ -> do
            debugM "Pontarius.Xmpp" "multiple signature results"
            return False

  where
    goodStat Gpg.SigStatGood = True
    goodStat _ = False
verifyGPG :: ByteString
          -> ByteString
          -> ByteString
          -> IO Bool
verifyGPG kid sig txt = do
    ctx <- Gpg.ctxNew Nothing
    logDebug $ "Verifying signature "  ++ show sig ++ " for " ++ show txt
    res <- Ex.try $ Gpg.verifyDetach ctx txt sig -- Gpg.Error
    case res of
        Left (e :: Gpg.Error) -> do
            errorM "Pontarius.Xmpp"
                $ "Verifying signature threw exception" ++ show e
            return False
        Right [st] -> do
            gpgGuard ("could not verify signature: " ++ show st)
                (goodStat $ Gpg.status st)
            gpgGuard ("Fingerpringt doesn't match: " ++ show kid
                       ++ " /= " ++ show (Gpg.fingerprint st))
                     (Gpg.fingerprint st == kid)
            debugM "Pontarius.Xmpp" $ "Signature seems good"
            return True
        Right [] -> do
            errorM "Pontarius.Xmpp" "verifyGPG: Could not import pubkey"
            return False
        Right _ -> do
            debugM "Pontarius.Xmpp" "multiple signature results"
            return False

  where
    goodStat Gpg.SigStatGood = True
    goodStat _ = False
verifyGPG kid sig txt = do
    ctx <- Gpg.ctxNew Nothing
    logDebug $ "Verifying signature "  ++ show sig ++ " for " ++ show txt
    res <- Ex.try $ Gpg.verifyDetach ctx txt sig -- Gpg.Error
    case res of
        Left (e :: Gpg.Error) -> do
            errorM "Pontarius.Xmpp"
                $ "Verifying signature threw exception" ++ show e
            return False
        Right [st] -> do
            gpgGuard ("could not verify signature: " ++ show st)
                (goodStat $ Gpg.status st)
            gpgGuard ("Fingerpringt doesn't match: " ++ show kid
                       ++ " /= " ++ show (Gpg.fingerprint st))
                     (Gpg.fingerprint st == kid)
            debugM "Pontarius.Xmpp" $ "Signature seems good"
            return True
        Right [] -> do
            errorM "Pontarius.Xmpp" "verifyGPG: Could not import pubkey"
            return False
        Right _ -> do
            debugM "Pontarius.Xmpp" "multiple signature results"
            return False

  where
    goodStat Gpg.SigStatGood = True
    goodStat _ = False