--
-- Copyright (c) 2006 Don Stewart - http://www.cse.unsw.edu.au/~dons
-- GPL version 2 or later (see http://www.gnu.org/copyleft/gpl.html)
--
-- Syntactic sugar for developing plugins.
-- Simplifies import lists, and abstracts over common patterns
--
module Lambdabot.Plugin
    ( Module(..)
    , ModuleT
    , newModule

    , LB
    , MonadLB(..)
    , lim80
    , ios80

    , ChanName
    , mkCN
    , getCN

    , Nick(..)
    , ircPrivmsg

    , module Lambdabot.Config
    , module Lambdabot.Config.Core
    , module Lambdabot.Command
    , module Lambdabot.State
    , module Lambdabot.File
    , module Lambdabot.Util.Serial
    ) where

import Lambdabot.Bot
import Lambdabot.ChanName
import Lambdabot.Config
import Lambdabot.Config.Core
import Lambdabot.Command hiding (runCommand, execCmd)
import Lambdabot.File
import Lambdabot.Module
import Lambdabot.Monad
import Lambdabot.Nick
import Lambdabot.State
import Lambdabot.Util
import Lambdabot.Util.Serial

import Codec.Binary.UTF8.String
import Control.Monad
import Control.Monad.Trans
import Data.Char

lim80 :: Monad m => m String -> Cmd m ()
lim80 :: forall (m :: * -> *). Monad m => m String -> Cmd m ()
lim80 m String
action = do
    Nick
to <- forall (m :: * -> *). Monad m => Cmd m Nick
getTarget
    let lim :: [String] -> [String]
lim = case Nick -> String
nName Nick
to of
                  (Char
'#':String
_) -> forall a. Int -> [a] -> [a]
take Int
3 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (Int -> String -> String
limitStr Int
80) -- message to channel: be nice
                  String
_       -> forall a. a -> a
id          -- private message: get everything
        spaceOut :: String -> String
spaceOut = [String] -> String
unlines forall b c a. (b -> c) -> (a -> b) -> a -> c
. [String] -> [String]
lim forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (Char
' 'forall a. a -> [a] -> [a]
:) forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
lines
        removeControl :: String -> String
removeControl = forall a. (a -> Bool) -> [a] -> [a]
filter (\Char
x -> Char -> Bool
isSpace Char
x Bool -> Bool -> Bool
|| Bool -> Bool
not (Char -> Bool
isControl Char
x))
    (forall (m :: * -> *). Monad m => String -> Cmd m ()
say forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (String -> String
encodeString forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
spaceOut forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
removeControl forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
decodeString) m String
action

-- | convenience, similar to ios but also cut output to channel to 80 characters
-- usage:  @process _ _ to _ s = ios80 to (plugs s)@
ios80 :: MonadIO m => IO String -> Cmd m ()
ios80 :: forall (m :: * -> *). MonadIO m => IO String -> Cmd m ()
ios80 = forall (m :: * -> *). Monad m => m String -> Cmd m ()
lim80 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadIO m => IO a -> m a
io