packages feed

funbot-0.5: src/FunBot/Commands/Shortcuts.hs

{- This file is part of funbot.
 -
 - Written in 2015, 2016 by fr33domlover <fr33domlover@riseup.net>.
 -
 - ♡ Copying is an act of love. Please copy, reuse and share.
 -
 - The author(s) have dedicated all copyright and related and neighboring
 - rights to this software to the public domain worldwide. This software is
 - distributed without any warranty.
 -
 - You should have received a copy of the CC0 Public Domain Dedication along
 - with this software. If not, see
 - <http://creativecommons.org/publicdomain/zero/1.0/>.
 -}

{-# LANGUAGE OverloadedStrings #-}

-- | Add-shortcut, delete-shortcut commands
--
-- Manage shortcuts
module FunBot.Commands.Shortcuts
    ( cmdAddShortcut
    , cmdDeleteShortcut
    )
where

import Control.Monad (unless, when)
import Data.List (find, intercalate)
import Data.Monoid ((<>))
import Data.Settings.Types (showOption)
import Data.Text (Text)
import FunBot.History (quote, reportHistory')
import FunBot.Memos (submitMemo)
import FunBot.Settings
import FunBot.Settings.Sections.Channels (addChannel)
import FunBot.Settings.Sections.Feeds (addFeed, deleteFeed)
import FunBot.Settings.Sections.Repos
import FunBot.Settings.Sections.Shortcuts (addShortcut, deleteShortcut)
import FunBot.Types
import FunBot.UserOptions
import FunBot.Util
import Network.IRC.Fun.Bot.Behavior
import Network.IRC.Fun.Bot.Chat
import Network.IRC.Fun.Bot.State
import Network.IRC.Fun.Bot.Types
import Text.Read (readMaybe)
import Network.IRC.Fun.Types.Base

import qualified Data.CaseInsensitive as CI
import qualified Data.Text as T
import qualified Data.Text.Read as TR

respondAddShortcut
    :: Maybe Channel
    -> Nickname
    -> [Text]
    -> (MsgContent -> BotSession ())
    -> BotSession ()
respondAddShortcut mchan nick [label, chan] send =
    if looksLikeChan $ Channel chan
        then do
            succ <- addShortcut (ShortcutLabel $ CI.mk label) (Channel chan)
            if succ
                then send $ MsgContent "Shortcut added."
                else failBack mchan nick $ OtherFail
                     "This shortcut is already registered."
        else send $ notchan $ Channel chan
respondAddShortcut mchan nick args _send =
    failBack mchan nick $ WrongNumArgsN (Just $ length args) (Just 2)

cmdAddShortcut = Command
    { cmdNames    = cmds ["shortcut+", "add-shortcut"]
    , cmdRespond  = respondAddShortcut
    , cmdHelp     = helps
        [ ( "shortcut+ <label> <channel>"
          , "add a shortcut to apply in the given channel, with default dummy \
            \settings. The label is just for convenient reference; pick a \
            \short meaningful one."
          )
        ]
    , cmdExamples =
        [ "shortcut+ issue #snowdrift"
        ]
    }

respondDeleteShortcut
    :: Maybe Channel
    -> Nickname
    -> [Text]
    -> (MsgContent -> BotSession ())
    -> BotSession ()
respondDeleteShortcut mchan nick [label] send = do
    succ <- deleteShortcut $ ShortcutLabel $ CI.mk label
    if succ
        then send $ MsgContent "Shortcut removed."
        else failBack mchan nick $ OtherFail "No such shortcut found."
respondDeleteShortcut mchan nick args _send =
    failBack mchan nick $ WrongNumArgsN (Just $ length args) (Just 1)

cmdDeleteShortcut = Command
    { cmdNames    = cmds ["shortcut-", "delete-shortcut"]
    , cmdRespond  = respondDeleteShortcut
    , cmdHelp     = helps
        [ ( "shortcut- <label>"
          , "remove the shortcut with the given label completely. If you just \
            \want to change the channels in which it applies, see the \
            \“shortcuts.<label>.channels” settings option."
          )
        ]
    , cmdExamples =
        [ "shortcut- issue"
        ]
    }