import Paths_twat (version)
import Data.Version (showVersion)
-import OptionalConfiguration
import ExitCodes
+import OptionalConfiguration
description :: String
description = "Twat twats tweets so you don't have to twitter."
where
import qualified OptionalConfiguration as OC
+import Usernames
data Cfg =
Cfg { consumer_key :: String,
from_address :: Maybe String,
to_address :: Maybe String,
verbose :: Bool,
- usernames :: [String] }
+ usernames :: Usernames }
deriving (Show)
from_address = Nothing,
to_address = Nothing,
verbose = False,
- usernames = [] }
+ usernames = Usernames [] }
merge_optional :: Cfg -> OC.OptionalCfg -> Cfg
merge_optional cfg opt_cfg =
merge' (Just _) (Just y) = Just y
-- If there are any optional usernames, use only those.
- all_usernames = if (null (OC.usernames opt_cfg))
- then (usernames cfg)
- else (OC.usernames opt_cfg)
+ all_usernames = if (null (get_usernames (OC.usernames opt_cfg)))
+ then (usernames cfg)
+ else (OC.usernames opt_cfg)
import Twitter.Http
import Twitter.Status
import Twitter.User
-
+import Usernames (Usernames(..))
-- | A wrapper around threadDelay which takes seconds instead of
-- microseconds as its argument.
-- set in either the config file or on the command-line.
let cfg = merge_optional default_config opt_config
- when (null $ usernames cfg) $ do
+ when (null $ get_usernames (usernames cfg)) $ do
hPutStrLn stderr "ERROR: no usernames supplied."
_ <- show_help
exitWith (ExitFailure exit_no_usernames)
-- Execute run_twat on each username in a new thread.
let run_twat_curried = run_twat cfg message
- _ <- mapM (forkIO . run_twat_curried) (usernames cfg)
+ _ <- mapM (forkIO . run_twat_curried) (get_usernames (usernames cfg))
_ <- forever $
-- This thread (the one executing main) doesn't do anything,
where
import qualified Data.Configurator as DC
-import qualified Data.Configurator.Types as DCT
+
import Data.Data (Data)
import Data.Maybe (fromMaybe)
import Data.Monoid (Monoid(..))
import Data.Typeable (Typeable)
+import Usernames
+
+
-- | The same as Cfg, except everything is optional. It's easy to
-- merge two of these by simply dropping the Nothings in favor of
-- the Justs. The 'usernames' are left un-maybed so that cmdargs
from_address :: Maybe String,
to_address :: Maybe String,
verbose :: Maybe Bool,
- usernames :: [String] }
+ usernames :: Usernames }
deriving (Show, Data, Typeable)
instance Monoid OptionalCfg where
Nothing
Nothing
Nothing
- []
+ (Usernames [])
cfg1 `mappend` cfg2 =
OptionalCfg
-- Use only the latter usernames if there are any.
all_usernames =
- usernames $ if (null (usernames cfg2))
+ usernames $ if (null (get_usernames (usernames cfg2)))
then cfg1
else cfg2
-instance DCT.Configured [String] where
- convert (DCT.List xs) =
- mapM convert_string xs
- where
- convert_string :: DCT.Value -> Maybe String
- convert_string = DCT.convert
-
- convert _ = Nothing
-
from_rc :: IO OptionalCfg
from_rc = do
cfg <- DC.load [ DC.Optional "$(HOME)/.twatrc" ]
cfg_from_address
cfg_to_address
cfg_verbose
- (fromMaybe [] cfg_usernames)
+ (fromMaybe (Usernames []) cfg_usernames)
--- /dev/null
+{-# LANGUAGE DeriveDataTypeable #-}
+
+-- | A newtype around a list of Strings which represent the usernames
+-- to watch. This is all to avoid an orphan instance of Configured
+-- for [String] if we had defined one in e.g. OptionalConfiguration.
+--
+module Usernames
+where
+
+-- DC is needed only for the DCT.Configured instance of String.
+import qualified Data.Configurator as DC()
+import qualified Data.Configurator.Types as DCT
+import Data.Data (Data)
+import System.Console.CmdArgs.Default (Default(..))
+import Data.Typeable (Typeable)
+
+
+newtype Usernames =
+ Usernames { get_usernames :: [String] }
+ deriving (Data, Show, Typeable)
+
+
+instance Default Usernames where
+ def = Usernames []
+
+
+instance DCT.Configured Usernames where
+ convert (DCT.List xs) =
+ fmap Usernames (mapM convert_string xs)
+ where
+ convert_string :: DCT.Value -> Maybe String
+ convert_string = DCT.convert
+
+ convert _ = Nothing