{-# Language OverloadedStrings #-}
module Irc.Commands
( ircAdmin
, ircAway
, ircCapEnd
, ircCapLs
, ircCapReq
, ircChantrace
, ircCnotice
, ircCprivmsg
, ircEtrace
, ircInfo
, ircInvite
, ircIson
, ircJoin
, ircKick
, ircKill
, ircKline
, ircKnock
, ircLinks
, ircList
, ircLusers
, ircMap
, ircMasktrace
, ircMode
, ircMotd
, ircNick
, ircNotice
, ircOper
, ircPart
, ircPass
, ircPing
, ircPong
, ircPrivmsg
, ircQuit
, ircRemove
, ircRules
, ircStats
, ircTestline
, ircTestmask
, ircTime
, ircTopic
, ircUnkline
, ircUser
, ircUserhost
, ircVersion
, ircWho
, ircWhois
, ircWhowas
, ircZnc
, AuthenticatePayload(..)
, ircAuthenticate
, ircAuthenticates
, encodePlainAuthentication
, encodeExternalAuthentication
) where
import Irc.RawIrcMsg
import Irc.Identifier
import Data.ByteString (ByteString)
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import qualified Data.ByteString as B
import qualified Data.ByteString.Base64 as Enc
nonempty :: Text -> [Text]
nonempty txt = filter (not . Text.null) [txt]
ircPrivmsg ::
Text ->
Text ->
RawIrcMsg
ircPrivmsg who msg = rawIrcMsg "PRIVMSG" [who, msg]
ircCprivmsg ::
Text ->
Text ->
Text ->
RawIrcMsg
ircCprivmsg nick chan msg = rawIrcMsg "CPRIVMSG" [nick, chan, msg]
ircCnotice ::
Text ->
Text ->
Text ->
RawIrcMsg
ircCnotice nick chan msg = rawIrcMsg "CNOTICE" [nick, chan, msg]
ircKnock ::
Text ->
Text ->
RawIrcMsg
ircKnock chan msg = rawIrcMsg "KNOCK" (chan : nonempty msg)
ircNotice ::
Text ->
Text ->
RawIrcMsg
ircNotice who msg = rawIrcMsg "NOTICE" [who, msg]
ircMode ::
Identifier ->
[Text] ->
RawIrcMsg
ircMode tgt params = rawIrcMsg "MODE" (idText tgt : params)
ircWhois ::
[Text] ->
RawIrcMsg
ircWhois = rawIrcMsg "WHOIS"
ircWho ::
[Text] ->
RawIrcMsg
ircWho = rawIrcMsg "WHO"
ircWhowas ::
[Text] ->
RawIrcMsg
ircWhowas = rawIrcMsg "WHOWAS"
ircWallops ::
Text ->
RawIrcMsg
ircWallops msg = rawIrcMsg "WALLOPS" [msg]
ircNick ::
Text ->
RawIrcMsg
ircNick nick = rawIrcMsg "NICK" [nick]
ircPart ::
Identifier ->
Text ->
RawIrcMsg
ircPart chan msg = rawIrcMsg "PART" (idText chan : nonempty msg)
ircJoin ::
Text ->
Maybe Text ->
RawIrcMsg
ircJoin chan (Just key) = rawIrcMsg "JOIN" [chan, key]
ircJoin chan Nothing = rawIrcMsg "JOIN" [chan]
ircInvite ::
Text ->
Identifier ->
RawIrcMsg
ircInvite nick channel = rawIrcMsg "INVITE" [nick, idText channel]
ircTopic ::
Identifier ->
Text ->
RawIrcMsg
ircTopic chan msg = rawIrcMsg "TOPIC" (idText chan : nonempty msg)
ircKick ::
Identifier ->
Text ->
Text ->
RawIrcMsg
ircKick chan who msg = rawIrcMsg "KICK" (idText chan : who : nonempty msg)
ircKill ::
Text ->
Text ->
RawIrcMsg
ircKill who msg = rawIrcMsg "KILL" (who : nonempty msg)
ircKline ::
Text ->
Text ->
Text ->
RawIrcMsg
ircKline minutes mask reason = rawIrcMsg "KLINE" [minutes, mask, reason]
ircUnkline ::
Text ->
RawIrcMsg
ircUnkline mask = rawIrcMsg "UNKLINE" [mask]
ircTestline ::
Text ->
RawIrcMsg
ircTestline mask = rawIrcMsg "TESTLINE" [mask]
ircTestmask ::
Text ->
Text ->
RawIrcMsg
ircTestmask mask gecos = rawIrcMsg "TESTMASK" (mask : nonempty gecos)
ircMasktrace ::
Text ->
Text ->
RawIrcMsg
ircMasktrace mask gecos = rawIrcMsg "MASKTRACE" [mask, gecos]
ircChantrace ::
Text ->
RawIrcMsg
ircChantrace channel = rawIrcMsg "CHANTRACE" [channel]
ircEtrace ::
Text ->
RawIrcMsg
ircEtrace arg = rawIrcMsg "ETRACE" [arg]
ircRemove ::
Identifier ->
Text ->
Text ->
RawIrcMsg
ircRemove chan who msg = rawIrcMsg "REMOVE" (idText chan : who : nonempty msg)
ircQuit :: Text -> RawIrcMsg
ircQuit = rawIrcMsg "QUIT" . nonempty
ircPass :: Text -> RawIrcMsg
ircPass pass = rawIrcMsg "PASS" [pass]
ircList ::
[Text] ->
RawIrcMsg
ircList = rawIrcMsg "LIST"
ircPing ::
[Text] ->
RawIrcMsg
ircPing = rawIrcMsg "PING"
ircPong ::
[Text] ->
RawIrcMsg
ircPong = rawIrcMsg "PONG"
ircIson ::
[Text] ->
RawIrcMsg
ircIson nicks = rawIrcMsg "ISON" [Text.unwords nicks]
ircTime ::
Text ->
RawIrcMsg
ircTime = rawIrcMsg "TIME" . nonempty
ircUserhost ::
[Text] ->
RawIrcMsg
ircUserhost = rawIrcMsg "USERHOST"
ircUserip ::
[Text] ->
RawIrcMsg
ircUserip = rawIrcMsg "USERIP"
ircUsers ::
Text ->
RawIrcMsg
ircUsers = rawIrcMsg "USERS" . nonempty
ircStats ::
[Text] ->
RawIrcMsg
ircStats = rawIrcMsg "STATS"
ircOper ::
Text ->
Text ->
RawIrcMsg
ircOper u p = rawIrcMsg "OPER" [u,p]
ircLinks ::
[Text] ->
RawIrcMsg
ircLinks = rawIrcMsg "LINKS"
ircAway ::
Text ->
RawIrcMsg
ircAway = rawIrcMsg "AWAY" . nonempty
ircMap :: RawIrcMsg
ircMap = rawIrcMsg "MAP" []
ircInfo :: RawIrcMsg
ircInfo = rawIrcMsg "INFO" []
ircRules ::
Text ->
RawIrcMsg
ircRules = rawIrcMsg "RULES" . nonempty
ircVersion ::
Text ->
RawIrcMsg
ircVersion = rawIrcMsg "VERSION" . nonempty
ircLusers ::
[Text] ->
RawIrcMsg
ircLusers = rawIrcMsg "LUSERS"
ircMotd ::
Text ->
RawIrcMsg
ircMotd = rawIrcMsg "MOTD" . nonempty
ircAdmin ::
Text ->
RawIrcMsg
ircAdmin = rawIrcMsg "ADMIN" . nonempty
ircUser ::
Text ->
Text -> RawIrcMsg
ircUser user real = rawIrcMsg "USER" [user, "0", "*", real]
ircCapReq ::
[Text] ->
RawIrcMsg
ircCapReq caps = rawIrcMsg "CAP" ["REQ", Text.unwords caps]
ircCapEnd :: RawIrcMsg
ircCapEnd = rawIrcMsg "CAP" ["END"]
ircCapLs :: RawIrcMsg
ircCapLs = rawIrcMsg "CAP" ["LS", "302"]
ircZnc ::
[Text] ->
RawIrcMsg
ircZnc = rawIrcMsg "ZNC"
newtype AuthenticatePayload = AuthenticatePayload ByteString
deriving Show
ircAuthenticate ::
Text ->
RawIrcMsg
ircAuthenticate msg = rawIrcMsg "AUTHENTICATE" [msg]
ircAuthenticates ::
AuthenticatePayload ->
[RawIrcMsg]
ircAuthenticates (AuthenticatePayload bytes) =
map (ircAuthenticate . Text.decodeUtf8) (chunks (Enc.encode bytes))
where
chunks :: ByteString -> [ByteString]
chunks b
| B.null b = ["+"]
| B.length b >= 400 = B.take 400 b : chunks (B.drop 400 b)
| otherwise = [b]
encodePlainAuthentication ::
Text ->
Text ->
AuthenticatePayload
encodePlainAuthentication user pass
= AuthenticatePayload
$ Text.encodeUtf8
$ Text.intercalate "\0" [user,user,pass]
encodeExternalAuthentication ::
Text ->
AuthenticatePayload
encodeExternalAuthentication
= AuthenticatePayload
. Text.encodeUtf8