{- Leave a message with lambdabot, the faithful secretary

> 17:11 < davidhouse> @tell dmhouse foo
> 17:11 < hsbot> Consider it noted
> 17:11 < davidhouse> @tell dmhouse bar
> 17:11 < hsbot> Consider it noted
> 17:11 < dmhouse> hello!
> 17:11 < hsbot> dmhouse: You have 2 new messages. '/msg hsbot @messages' to read them.
> 17:11 < dmhouse> Notice how I'm speaking again, and hsbot isn't buzzing me more than that one time.
> 17:12 < dmhouse> It'll buzz me after a day's worth of not checking my messages.
> 17:12 < dmhouse> If I want to check them in the intermittent period, I can either send a /msg, or:
> 17:12 < dmhouse> @messages?
> 17:12 < hsbot> You have 2 messages
> 17:12 < dmhouse> Let's check them, shall we?
>
> [In a /msg to hsbot]
> 17:12 <hsbot> davidhouse said less than a minute ago: foo
> 17:12 <hsbot> davidhouse said less than a minute ago: bar
>
> [Back in the channel
> 17:12 < dmhouse> You needn't use a /msg, however. If you're not going to annoy the channel by printing 20 of
>                  your messages, feel free to just type '@messages' in the channel.
> 17:12 < davidhouse> @tell dmhouse foobar
> 17:12 < hsbot> Consider it noted
> 17:12 < davidhouse> @ask dmhouse barfoo
> 17:12 < hsbot> Consider it noted
> 17:12 < davidhouse> You can see there @ask. It's just a synonym for @tell, but it prints "foo asked X ago M",
>                     which is more natural. E.g. '@ask dons whether he's applied my latest patch yet?'
> 17:13 < dmhouse> For the admins, a useful little debugging tool is @print-notices.
> 17:13 < hsbot> dmhouse: You have 2 new messages. '/msg hsbot @messages' to read them.
> 17:14 < dmhouse> Notice that hsbot pinged me there, even though it's less than a day since I last checked my
>                  messages, because there have been some new ones posted.
> 17:14 < dmhouse> @print-notices
> 17:14 < hsbot> {"dmhouse":=(Just Thu Jun  8 17:13:46 BST 2006,[Note {noteSender = "davidhouse", noteContents =
>                "foobar", noteTime = Thu Jun  8 17:12:50 BST 2006, noteType = Tell},Note {noteSender =
                 "davidhouse", noteContents = "barfoo", noteTime = Thu Jun  8 17:12:55 BST 2006, noteType = Ask}])}
> 17:15 < dmhouse> There you can see the two notes. The internal state is a map from recipient nicks to a pair of
>                  (when we last buzzed them about having messages, a list of the notes they've got stacked up).
> 17:16 < dmhouse> Finally, if you don't want to bother checking your messages, then the following command will
>                  likely be useful.
> 17:16 < dmhouse> @clear-messages
> 17:16 < hsbot> Messages cleared.
> 17:16 < dmhouse> That's all, folks!
> 17:17 < dmhouse> Any comments, queries or complaints to dmhouse@gmail.com. The source should be fairly readable, so
>                  hack away!
-}

module Lambdabot.Plugin.Social.Tell (tellPlugin) where

import Lambdabot.Compat.AltTime
import Lambdabot.Compat.FreenodeNick
import Lambdabot.Plugin
import Lambdabot.Util

import Control.Monad
import qualified Data.Map as M
import Data.Maybe (fromMaybe)
import Text.Printf (printf)

-- | Was it @tell or @ask that was the original command?
data NoteType    = Tell | Ask deriving (Int -> NoteType -> String -> String
[NoteType] -> String -> String
NoteType -> String
forall a.
(Int -> a -> String -> String)
-> (a -> String) -> ([a] -> String -> String) -> Show a
showList :: [NoteType] -> String -> String
$cshowList :: [NoteType] -> String -> String
show :: NoteType -> String
$cshow :: NoteType -> String
showsPrec :: Int -> NoteType -> String -> String
$cshowsPrec :: Int -> NoteType -> String -> String
Show, NoteType -> NoteType -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: NoteType -> NoteType -> Bool
$c/= :: NoteType -> NoteType -> Bool
== :: NoteType -> NoteType -> Bool
$c== :: NoteType -> NoteType -> Bool
Eq, ReadPrec [NoteType]
ReadPrec NoteType
Int -> ReadS NoteType
ReadS [NoteType]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [NoteType]
$creadListPrec :: ReadPrec [NoteType]
readPrec :: ReadPrec NoteType
$creadPrec :: ReadPrec NoteType
readList :: ReadS [NoteType]
$creadList :: ReadS [NoteType]
readsPrec :: Int -> ReadS NoteType
$creadsPrec :: Int -> ReadS NoteType
Read)
-- | The Note datatype. Fields self-explanatory.
data Note        = Note { Note -> FreenodeNick
noteSender   :: FreenodeNick,
                          Note -> String
noteContents :: String,
                          Note -> ClockTime
noteTime     :: ClockTime,
                          Note -> NoteType
noteType     :: NoteType }
                   deriving (Note -> Note -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Note -> Note -> Bool
$c/= :: Note -> Note -> Bool
== :: Note -> Note -> Bool
$c== :: Note -> Note -> Bool
Eq, Int -> Note -> String -> String
[Note] -> String -> String
Note -> String
forall a.
(Int -> a -> String -> String)
-> (a -> String) -> ([a] -> String -> String) -> Show a
showList :: [Note] -> String -> String
$cshowList :: [Note] -> String -> String
show :: Note -> String
$cshow :: Note -> String
showsPrec :: Int -> Note -> String -> String
$cshowsPrec :: Int -> Note -> String -> String
Show, ReadPrec [Note]
ReadPrec Note
Int -> ReadS Note
ReadS [Note]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Note]
$creadListPrec :: ReadPrec [Note]
readPrec :: ReadPrec Note
$creadPrec :: ReadPrec Note
readList :: ReadS [Note]
$creadList :: ReadS [Note]
readsPrec :: Int -> ReadS Note
$creadsPrec :: Int -> ReadS Note
Read)
-- | The state. A map of (times we last told this nick they've got messages, the
--   messages themselves, the auto-reply)
type NoticeEntry = (Maybe ClockTime, [Note], Maybe String)
type NoticeBoard = M.Map FreenodeNick NoticeEntry

type Tell = ModuleT NoticeBoard LB

tellPlugin :: Module NoticeBoard
tellPlugin :: Module (Map FreenodeNick NoticeEntry)
tellPlugin = forall st. Module st
newModule
    { moduleCmds :: ModuleT (Map FreenodeNick NoticeEntry) LB [Command Tell]
moduleCmds = forall (m :: * -> *) a. Monad m => a -> m a
return
        [ (String -> Command Identity
command String
"tell")
            { help :: Cmd Tell ()
help = forall (m :: * -> *). Monad m => String -> Cmd m ()
say String
"tell <nick> <message>. When <nick> shows activity, tell them <message>."
            , process :: String -> Cmd Tell ()
process = NoteType -> [String] -> Cmd Tell ()
doTell NoteType
Tell forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
words
            }
        , (String -> Command Identity
command String
"ask")
            { help :: Cmd Tell ()
help = forall (m :: * -> *). Monad m => String -> Cmd m ()
say String
"ask <nick> <message>. When <nick> shows activity, ask them <message>."
            , process :: String -> Cmd Tell ()
process = NoteType -> [String] -> Cmd Tell ()
doTell NoteType
Ask forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
words
            }
        , (String -> Command Identity
command String
"messages")
            { help :: Cmd Tell ()
help = forall (m :: * -> *). Monad m => String -> Cmd m ()
say String
"messages. Check your messages, responding in private."
            , process :: String -> Cmd Tell ()
process = forall a b. a -> b -> a
const (Bool -> Cmd Tell ()
doMessages Bool
False)
            }
        , (String -> Command Identity
command String
"messages-loud")
            { help :: Cmd Tell ()
help = forall (m :: * -> *). Monad m => String -> Cmd m ()
say String
"messages. Check your messages, responding in public."
            , process :: String -> Cmd Tell ()
process = forall a b. a -> b -> a
const (Bool -> Cmd Tell ()
doMessages Bool
True)
            }
        , (String -> Command Identity
command String
"messages?")
            { help :: Cmd Tell ()
help = forall (m :: * -> *). Monad m => String -> Cmd m ()
say String
"messages?. Tells you whether you have any messages"
            , process :: String -> Cmd Tell ()
process = forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ do
                Nick
sender <- forall (m :: * -> *). Monad m => Cmd m Nick
getSender
                Maybe [Note]
ms <- Nick -> Cmd Tell (Maybe [Note])
getMessages Nick
sender
                case Maybe [Note]
ms of
                    Just [Note]
_      -> Nick -> (String -> Cmd Tell ()) -> Cmd Tell ()
doRemind Nick
sender forall (m :: * -> *). Monad m => String -> Cmd m ()
say
                    Maybe [Note]
Nothing     -> forall (m :: * -> *). Monad m => String -> Cmd m ()
say String
"Sorry, no messages today."
            }
        , (String -> Command Identity
command String
"clear-messages")
            { help :: Cmd Tell ()
help = forall (m :: * -> *). Monad m => String -> Cmd m ()
say String
"clear-messages. Clears your messages."
            , process :: String -> Cmd Tell ()
process = forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ do
                Nick
sender <- forall (m :: * -> *). Monad m => Cmd m Nick
getSender
                Nick -> Cmd Tell ()
clearMessages Nick
sender
                forall (m :: * -> *). Monad m => String -> Cmd m ()
say String
"Messages cleared."
            }
        , (String -> Command Identity
command String
"auto-reply")
            { help :: Cmd Tell ()
help = forall (m :: * -> *). Monad m => String -> Cmd m ()
say String
"auto-reply. Lets lambdabot auto-reply if someone sends you a message"
            , process :: String -> Cmd Tell ()
process = String -> Cmd Tell ()
doAutoReply
            }
        , (String -> Command Identity
command String
"auto-reply?")
            { help :: Cmd Tell ()
help = forall (m :: * -> *). Monad m => String -> Cmd m ()
say String
"auto-reply?. Tells you your auto-reply status"
            , process :: String -> Cmd Tell ()
process = forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ do
                Nick
sender <- forall (m :: * -> *). Monad m => Cmd m Nick
getSender
                Maybe String
a <- Nick -> Cmd Tell (Maybe String)
getAutoReply Nick
sender
                case Maybe String
a of
                    Just String
s      -> forall (m :: * -> *). Monad m => String -> Cmd m ()
say forall a b. (a -> b) -> a -> b
$ String
"Your auto-reply is \"" forall a. [a] -> [a] -> [a]
++ String
s forall a. [a] -> [a] -> [a]
++ String
"\"."
                    Maybe String
Nothing     -> forall (m :: * -> *). Monad m => String -> Cmd m ()
say String
"You do not have an auto-reply message set."
            }
        , (String -> Command Identity
command String
"clear-auto-reply")
            { help :: Cmd Tell ()
help = forall (m :: * -> *). Monad m => String -> Cmd m ()
say String
"clear-auto-reply. Clears your auto-reply message."
            , process :: String -> Cmd Tell ()
process = forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ do
                Nick
sender <- forall (m :: * -> *). Monad m => Cmd m Nick
getSender
                Nick -> Cmd Tell ()
clearAutoReply Nick
sender
                forall (m :: * -> *). Monad m => String -> Cmd m ()
say String
"Auto-reply message cleared."
            }
        , (String -> Command Identity
command String
"print-notices")
            { privileged :: Bool
privileged = Bool
True
            , help :: Cmd Tell ()
help = forall (m :: * -> *). Monad m => String -> Cmd m ()
say String
"print-notices. Print the current map of notes."
            , process :: String -> Cmd Tell ()
process = forall a b. a -> b -> a
const ((forall (m :: * -> *). Monad m => String -> Cmd m ()
say forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show) forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall (m :: * -> *). MonadLBState m => m (LBState m)
readMS)
            }
        , (String -> Command Identity
command String
"purge-notices")
            { privileged :: Bool
privileged = Bool
True
            , help :: Cmd Tell ()
help = forall (m :: * -> *). Monad m => String -> Cmd m ()
say forall a b. (a -> b) -> a -> b
$
                String
"purge-notices [<nick> [<nick> [<nick> ...]]]]. "
                forall a. [a] -> [a] -> [a]
++ String
"Clear all notes for specified nicks, or all notices if you don't "
                forall a. [a] -> [a] -> [a]
++ String
"specify a nick."
            , process :: String -> Cmd Tell ()
process = \String
args -> do
                [Nick]
users <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall (m :: * -> *). Monad m => String -> Cmd m Nick
readNick (String -> [String]
words String
args)
                if forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Nick]
users
                    then forall (m :: * -> *). MonadLBState m => LBState m -> m ()
writeMS forall k a. Map k a
M.empty
                    else forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Nick -> Cmd Tell ()
clearMessages [Nick]
users
                forall (m :: * -> *). Monad m => String -> Cmd m ()
say String
"Messages purged."
            }
        ]
    , moduleDefState :: LB (Map FreenodeNick NoticeEntry)
moduleDefState  = forall (m :: * -> *) a. Monad m => a -> m a
return forall k a. Map k a
M.empty
    , moduleSerialize :: Maybe (Serial (Map FreenodeNick NoticeEntry))
moduleSerialize = forall a. a -> Maybe a
Just forall k v.
(Ord k, Show k, Show v, Read k, Read v) =>
Serial (Map k v)
mapSerial
    -- Hook onto contextual. Grab nicks of incoming messages, and tell them
    -- if they have any messages, if it's less than a day since we last did so.
    , contextual :: String -> Cmd Tell ()
contextual = forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ do
        Nick
sender <- forall (m :: * -> *). Monad m => Cmd m Nick
getSender
        Bool
remp <- Nick -> Cmd Tell Bool
needToRemind Nick
sender
        if Bool
remp
            then Nick -> (String -> Cmd Tell ()) -> Cmd Tell ()
doRemind Nick
sender (forall (m :: * -> *) a. MonadLB m => LB a -> m a
lb forall b c a. (b -> c) -> (a -> b) -> a -> c
. Nick -> String -> LB ()
ircPrivmsg Nick
sender)
            else forall (m :: * -> *) a. Monad m => a -> m a
return ()
    }

-- | Take a note and the current time, then display it
showNote :: ClockTime -> Note -> Cmd Tell String
showNote :: ClockTime -> Note -> Cmd Tell String
showNote ClockTime
time Note
note = do
    String
sender <- forall (m :: * -> *). Monad m => Nick -> Cmd m String
showNick (FreenodeNick -> Nick
getFreenodeNick (Note -> FreenodeNick
noteSender Note
note))
    let diff :: TimeDiff
diff    = ClockTime
time ClockTime -> ClockTime -> TimeDiff
`diffClockTimes` Note -> ClockTime
noteTime Note
note
        ago :: String
ago     = case TimeDiff -> String
timeDiffPretty TimeDiff
diff of
                    [] -> String
"less than a minute"
                    String
pr -> String
pr
        action :: String
action  = case Note -> NoteType
noteType Note
note of NoteType
Tell -> String
"said"; NoteType
Ask -> String
"asked"
    forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall r. PrintfType r => String -> r
printf String
"%s %s %s ago: %s" String
sender String
action String
ago (Note -> String
noteContents Note
note)

-- | Is it less than a day since we last reminded this nick they've got messages?
needToRemind :: Nick -> Cmd Tell Bool
needToRemind :: Nick -> Cmd Tell Bool
needToRemind Nick
n = do
  Map FreenodeNick NoticeEntry
st  <- forall (m :: * -> *). MonadLBState m => m (LBState m)
readMS
  ClockTime
now <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
io IO ClockTime
getClockTime
  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup (Nick -> FreenodeNick
FreenodeNick Nick
n) Map FreenodeNick NoticeEntry
st of
             Just (Just ClockTime
lastTime, [Note]
_, Maybe String
_) ->
               let diff :: TimeDiff
diff = ClockTime
now ClockTime -> ClockTime -> TimeDiff
`diffClockTimes` ClockTime
lastTime
               in TimeDiff
diff forall a. Ord a => a -> a -> Bool
> NominalDiffTime -> TimeDiff
TimeDiff NominalDiffTime
86400
             Just (Maybe ClockTime
Nothing,       [Note]
_, Maybe String
_) -> Bool
True
             Maybe NoticeEntry
Nothing                    -> Bool
True

-- | Add a note to the NoticeBoard
writeDown :: Nick -> Nick -> String -> NoteType -> Cmd Tell ()
writeDown :: Nick -> Nick -> String -> NoteType -> Cmd Tell ()
writeDown Nick
to Nick
from String
what NoteType
ntype = do
  ClockTime
time <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
io IO ClockTime
getClockTime
  let note :: Note
note = Note { noteSender :: FreenodeNick
noteSender   = Nick -> FreenodeNick
FreenodeNick Nick
from,
                    noteContents :: String
noteContents = String
what,
                    noteTime :: ClockTime
noteTime     = ClockTime
time,
                    noteType :: NoteType
noteType     = NoteType
ntype }
  Nick -> (NoticeEntry -> NoticeEntry) -> Cmd Tell ()
modEntry Nick
to forall a b. (a -> b) -> a -> b
$ \(Maybe ClockTime
_, [Note]
ns, Maybe String
a) -> (forall a. Maybe a
Nothing, [Note]
ns forall a. [a] -> [a] -> [a]
++ [Note
note], Maybe String
a)

-- | Return a user's notes, or Nothing if they don't have any
getMessages :: Nick -> Cmd Tell (Maybe [Note])
getMessages :: Nick -> Cmd Tell (Maybe [Note])
getMessages Nick
sender = do
  Map FreenodeNick NoticeEntry
st  <- forall (m :: * -> *). MonadLBState m => m (LBState m)
readMS
  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup (Nick -> FreenodeNick
FreenodeNick Nick
sender) Map FreenodeNick NoticeEntry
st of
             Maybe NoticeEntry
Nothing ->         forall a. Maybe a
Nothing
             Just (Maybe ClockTime
_, [], Maybe String
_) -> forall a. Maybe a
Nothing
             Just (Maybe ClockTime
_, [Note]
ns, Maybe String
_) -> forall a. a -> Maybe a
Just [Note]
ns

-- | Set a user's messages.
setMessages :: Nick -> [Note] -> Cmd Tell ()
setMessages :: Nick -> [Note] -> Cmd Tell ()
setMessages Nick
sender [Note]
msgs = Nick -> (NoticeEntry -> NoticeEntry) -> Cmd Tell ()
modEntry Nick
sender forall a b. (a -> b) -> a -> b
$ \(Maybe ClockTime
t, [Note]
_, Maybe String
a) -> (Maybe ClockTime
t, [Note]
msgs, Maybe String
a)

-- | Clear a user's messages.
clearMessages :: Nick -> Cmd Tell ()
clearMessages :: Nick -> Cmd Tell ()
clearMessages Nick
sender = Nick -> (NoticeEntry -> NoticeEntry) -> Cmd Tell ()
modEntry Nick
sender forall a b. (a -> b) -> a -> b
$ \(Maybe ClockTime
_, [Note]
_, Maybe String
a) -> (forall a. Maybe a
Nothing, [], Maybe String
a)

-- | Sets a user's auto-reply message
setAutoReply :: Nick -> String -> Cmd Tell ()
setAutoReply :: Nick -> String -> Cmd Tell ()
setAutoReply Nick
sender String
msg = Nick -> (NoticeEntry -> NoticeEntry) -> Cmd Tell ()
modEntry Nick
sender forall a b. (a -> b) -> a -> b
$ \(Maybe ClockTime
t, [Note]
ns, Maybe String
_) -> (Maybe ClockTime
t, [Note]
ns, forall a. a -> Maybe a
Just String
msg)

-- | Gets a user's auto-reply message
getAutoReply :: Nick -> Cmd Tell (Maybe String)
getAutoReply :: Nick -> Cmd Tell (Maybe String)
getAutoReply Nick
sender = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (m :: * -> *) a. Monad m => m (m a) -> m a
join forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(Maybe ClockTime
_,[Note]
_,Maybe String
a) -> Maybe String
a) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup (Nick -> FreenodeNick
FreenodeNick Nick
sender)) forall (m :: * -> *). MonadLBState m => m (LBState m)
readMS

-- | Clears the auto-reply message
clearAutoReply :: Nick -> Cmd Tell ()
clearAutoReply :: Nick -> Cmd Tell ()
clearAutoReply Nick
sender = Nick -> (NoticeEntry -> NoticeEntry) -> Cmd Tell ()
modEntry Nick
sender forall a b. (a -> b) -> a -> b
$ \(Maybe ClockTime
t, [Note]
ns, Maybe String
_) -> (Maybe ClockTime
t, [Note]
ns, forall a. Maybe a
Nothing)

-- | Modifies an entry, taking care of missing entries and cleaning up empty entries.
-- (We consider an entry empty even if it still has a timestamp.)
modEntry :: Nick -> (NoticeEntry -> NoticeEntry) -> Cmd Tell ()
modEntry :: Nick -> (NoticeEntry -> NoticeEntry) -> Cmd Tell ()
modEntry Nick
sender NoticeEntry -> NoticeEntry
f = forall (m :: * -> *).
MonadLBState m =>
(LBState m -> LBState m) -> m ()
modifyMS forall a b. (a -> b) -> a -> b
$ forall k a.
Ord k =>
(Maybe a -> Maybe a) -> k -> Map k a -> Map k a
M.alter (forall {a} {a} {a}. (a, [a], Maybe a) -> Maybe (a, [a], Maybe a)
cleanup forall b c a. (b -> c) -> (a -> b) -> a -> c
. NoticeEntry -> NoticeEntry
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a -> a
fromMaybe forall {a} {a} {a}. (Maybe a, [a], Maybe a)
empty) (Nick -> FreenodeNick
FreenodeNick Nick
sender)
  where empty :: (Maybe a, [a], Maybe a)
empty = (forall a. Maybe a
Nothing, [], forall a. Maybe a
Nothing)
        cleanup :: (a, [a], Maybe a) -> Maybe (a, [a], Maybe a)
cleanup (a
_, [], Maybe a
Nothing) = forall a. Maybe a
Nothing
        cleanup (a, [a], Maybe a)
e = forall a. a -> Maybe a
Just (a, [a], Maybe a)
e

-- * Handlers
--

-- | Give a user their messages
doMessages :: Bool -> Cmd Tell ()
doMessages :: Bool -> Cmd Tell ()
doMessages Bool
loud = do
    Nick
sender <- forall (m :: * -> *). Monad m => Cmd m Nick
getSender
    Maybe [Note]
msgs <- Nick -> Cmd Tell (Maybe [Note])
getMessages Nick
sender

    let tellNote :: String -> Cmd Tell ()
tellNote = if Bool
loud
            then forall (m :: * -> *). Monad m => String -> Cmd m ()
say
            else forall (m :: * -> *) a. MonadLB m => LB a -> m a
lb forall b c a. (b -> c) -> (a -> b) -> a -> c
. Nick -> String -> LB ()
ircPrivmsg Nick
sender

    let loop :: [Note] -> Cmd Tell ()
loop [] = Nick -> Cmd Tell ()
clearMessages Nick
sender
        loop (Note
msg : [Note]
msgs) = do
            ClockTime
time <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
io IO ClockTime
getClockTime
            -- Note that 'showNote' may block and thus run into a timeout.
            -- Hence we update the list of pending messages after each message.
            ClockTime -> Note -> Cmd Tell String
showNote ClockTime
time Note
msg forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> Cmd Tell ()
tellNote
            Nick -> [Note] -> Cmd Tell ()
setMessages Nick
sender [Note]
msgs
            [Note] -> Cmd Tell ()
loop [Note]
msgs

    case Maybe [Note]
msgs of
        Maybe [Note]
Nothing -> forall (m :: * -> *). Monad m => String -> Cmd m ()
say String
"You don't have any messages"
        Just [Note]
msgs -> [Note] -> Cmd Tell ()
loop [Note]
msgs

verb :: NoteType -> String
verb :: NoteType -> String
verb NoteType
Ask = String
"ask"
verb NoteType
Tell= String
"tell"

-- | Execute a @tell or @ask command.
doTell :: NoteType -> [String] -> Cmd Tell ()
doTell :: NoteType -> [String] -> Cmd Tell ()
doTell NoteType
ntype []         = forall (m :: * -> *). Monad m => String -> Cmd m ()
say (String
"Who should I " forall a. [a] -> [a] -> [a]
++ NoteType -> String
verb NoteType
ntype forall a. [a] -> [a] -> [a]
++ String
"?")
doTell NoteType
ntype (String
who':[String]
args) = do
    let who :: String
who     = forall a. (a -> Bool) -> [a] -> [a]
dropFromEnd (forall a. Eq a => a -> a -> Bool
== Char
':') String
who'
    Nick
recipient   <- forall (m :: * -> *). Monad m => String -> Cmd m Nick
readNick String
who
    Nick
sender      <- forall (m :: * -> *). Monad m => Cmd m Nick
getSender
    Nick
me          <- forall (m :: * -> *). Monad m => Cmd m Nick
getLambdabotName
    let rest :: String
rest = [String] -> String
unwords [String]
args
        (Bool
record, String
res)
            | Nick
sender    forall a. Eq a => a -> a -> Bool
== Nick
recipient   = (Bool
False, String
"You can " forall a. [a] -> [a] -> [a]
++ NoteType -> String
verb NoteType
ntype forall a. [a] -> [a] -> [a]
++ String
" yourself!")
            | Nick
recipient forall a. Eq a => a -> a -> Bool
== Nick
me          = (Bool
False, String
"Nice try ;)")
            | forall (t :: * -> *) a. Foldable t => t a -> Bool
null [String]
args                = (Bool
False, String
"What should I " forall a. [a] -> [a] -> [a]
++ NoteType -> String
verb NoteType
ntype forall a. [a] -> [a] -> [a]
++ String
" " forall a. [a] -> [a] -> [a]
++ String
who forall a. [a] -> [a] -> [a]
++ String
"?")
            | Bool
otherwise                = (Bool
True,  String
"Consider it noted.")
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
record forall a b. (a -> b) -> a -> b
$ do
        Maybe String
autoReply <- Nick -> Cmd Tell (Maybe String)
getAutoReply Nick
recipient
        case Maybe String
autoReply of
            Maybe String
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
            Just String
s -> forall (m :: * -> *). Monad m => String -> Cmd m ()
say forall a b. (a -> b) -> a -> b
$ String
who forall a. [a] -> [a] -> [a]
++ String
" lets you know: " forall a. [a] -> [a] -> [a]
++ String
s
        Nick -> Nick -> String -> NoteType -> Cmd Tell ()
writeDown Nick
recipient Nick
sender String
rest NoteType
ntype
    forall (m :: * -> *). Monad m => String -> Cmd m ()
say String
res

-- | Execute a @auto-reply
doAutoReply :: String -> Cmd Tell ()
doAutoReply :: String -> Cmd Tell ()
doAutoReply String
"" = forall (m :: * -> *). Monad m => String -> Cmd m ()
say String
"No auto-reply message given. Did you mean @clear-auto-reply?"
doAutoReply String
msg = do
    Nick
sender      <- forall (m :: * -> *). Monad m => Cmd m Nick
getSender
    Nick -> String -> Cmd Tell ()
setAutoReply Nick
sender String
msg
    forall (m :: * -> *). Monad m => String -> Cmd m ()
say String
"Auto-Reply messages noted. You can check the status with auto-reply? and clear it with clear-auto-reply."

-- | Remind a user that they have messages.
doRemind :: Nick -> (String -> Cmd Tell ()) -> Cmd Tell ()
doRemind :: Nick -> (String -> Cmd Tell ()) -> Cmd Tell ()
doRemind Nick
sender String -> Cmd Tell ()
remind = do
    Maybe [Note]
ms  <- Nick -> Cmd Tell (Maybe [Note])
getMessages Nick
sender
    ClockTime
now <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
io IO ClockTime
getClockTime
    Nick -> (NoticeEntry -> NoticeEntry) -> Cmd Tell ()
modEntry Nick
sender forall a b. (a -> b) -> a -> b
$ \(Maybe ClockTime
_,[Note]
ns,Maybe String
a) -> (forall a. a -> Maybe a
Just ClockTime
now, [Note]
ns, Maybe String
a)
    case Maybe [Note]
ms of
        Just [Note]
msgs -> do
            String
me <- forall (m :: * -> *). Monad m => Nick -> Cmd m String
showNick forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall (m :: * -> *). Monad m => Cmd m Nick
getLambdabotName
            let n :: Int
n = forall (t :: * -> *) a. Foldable t => t a -> Int
length [Note]
msgs
                (String
messages, String
pronoun)
                    | Int
n forall a. Ord a => a -> a -> Bool
> Int
1     = (String
"messages", String
"them")
                    | Bool
otherwise = (String
"message", String
"it")
            String -> Cmd Tell ()
remind forall a b. (a -> b) -> a -> b
$ forall r. PrintfType r => String -> r
printf String
"You have %d new %s. '/msg %s @messages' to read %s."
                        Int
n String
messages String
me String
pronoun
        Maybe [Note]
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return ()