module Main where

import Network.CGI
import Data.Maybe
import System.IO

import DB
import Process
import Session
import Now
import FancyHtml

main = runCGI $ handleErrors cgiMain

cgiMain = do
    sessionId <- getInput "session_id"
    key       <- getInput "key"
    validsess <- liftIO $ check_session_id $ fromJust sessionId
    user      <- liftIO $ get_session_user $ fromJust sessionId
    let pubkey = fromJust key
    if (validsess)
        then updateKey user pubkey
        else output $ header ++ errorBox "Incorrect session ID, go away!" ++ footer

updateKey user pubkey = do
    liftIO $ db_update "UPDATE users SET pubkey=?, revocation=? WHERE username LIKE ?" [pubkey, "", user]
    output $ header ++ "Your public key has been updated." ++ (quote "The sore truth is that complexity sells better. (It is not only the computer industry that has discovered that.) And it is even more diabolical in that we even use the complexity of our own constructs to impress ourselves." "http://www.schnada.de/quotes/contempt.html" "Edsger W. Dijkstra") ++ footer
