module Main where

import Network.CGI
import Data.Maybe

import DB

main = runCGI $ handleErrors cgiMain

cgiMain = do
    username   <- getInput "username"
    pubkey     <- getInput "pubkey"
    revocation <- getInput "revocation"
    let user = fromJust username
    if isJust pubkey
        then retrieveAndShow "pubkey" user
        else if isJust revocation
            then retrieveAndShow "revocation" user
            else output $ "Wrong parameters."

retrieveAndShow key user = do
    value <- liftIO $ db_firstvalue $ "SELECT " ++ key ++ " FROM users WHERE username LIKE '" ++ user ++ "'"
    setHeader "Content-Type" "text/plain"
    output $ value
