From fcdb18a449283cd608a4d2b2a8f11580d7303730 Mon Sep 17 00:00:00 2001 From: Michele Guerini Rocco Date: Sun, 19 Apr 2015 14:36:44 -0700 Subject: [PATCH] --- namecoin-update | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 namecoin-update diff --git a/namecoin-update b/namecoin-update new file mode 100644 index 0000000..3146591 --- /dev/null +++ b/namecoin-update @@ -0,0 +1,39 @@ +{-# Language DeriveGeneric, RecordWildCards #-} + +import System.Process +import System.Exit +import GHC.Generics +import Data.Aeson +import Data.ByteString.Lazy.Char8 (pack) + +readCommand = readProcess "namecoind" ["name_list"] "" +updateCommand n v = readProcessWithExitCode "namecoind" ["name_update", n, v] "" + +data Name = + Name { name :: String + , value :: String + , address :: String + , expires_in :: Int + } deriving (Show, Generic) + +instance FromJSON Name + +updateName :: Name -> IO Int +updateName Name{..} + | expires_in < 100 = do + (code, out, err) <- updateCommand name value + if code == ExitSuccess + then putStrLn (name ++ " updated: " ++ out) >> return 0 + else putStrLn (name ++ " update failed: " ++ err) >> return 1 + | otherwise = putStrLn ("No need to update " ++ name) >> return 0 + +main :: IO () +main = do + out <- pack <$> readCommand + case eitherDecode out of + Left err -> putStrLn ("Error communicating with namecoin: " ++ err) + Right names -> do + errs <- sum <$> mapM updateName (names :: [Name]) + if errs > 0 + then putStrLn (show errs ++ " updates failed") + else putStrLn "All ok" \ No newline at end of file