aboutsummaryrefslogtreecommitdiffstats
path: root/src/Reaktor/Plugins/Register.hs
blob: fd17f48457ee37ed1c9bdb38d2a4b1afa7a62ee8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
{-# LANGUAGE DeriveGeneric, DeriveAnyClass #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE OverloadedStrings #-}
module Reaktor.Plugins.Register (plugin) where

import           Control.Monad (when)
import           Data.Aeson
import qualified Data.ByteString.Char8 as BS
import           GHC.Generics
import           Reaktor.Types
import           Reaktor.Utils (nextNick,randomNick)


data RegisterConfig = RegisterConfig {
      channels :: [BS.ByteString]
    }
  deriving (FromJSON,Generic)


plugin :: Value -> IO Plugin
plugin = simplePlugin run


run :: RegisterConfig -> PluginFunc
run cfg msg = do
    nick_ <- getNick
    case msg of

      Message _ "<start>" _ -> do
        sendMsg (Message Nothing "NICK" [nick_])
        sendMsg (Message Nothing "USER" [nick_, "*", "0", nick_])

      Message (Just _self) "NICK" (newnick:[]) -> do
        when (newnick == nick_) $ do
          -- TODO JOIN only if not already joined
          --      i.e. not during subsequent nick changes
          sendMsg (Message Nothing "JOIN" [ BS.intercalate "," (channels cfg) ])

      -- RFC1459 ERR_NICKNAMEINUSE
      Message (Just _servername) "433" (_msgtarget:nickinuse:_reason:[]) -> do
        if nickinuse == nick_ then do
          let nick' = nextNick nickinuse
          sendMsg (Message Nothing "NICK" [nick'])
          -- TODO change state on "NICK"
          setNick nick'

        -- TODO is this just for NickServ? (also check that module if it has
        -- stuff only for "Register")
        else do
          nick' <- lift randomNick
          sendMsg (Message Nothing "NICK" [nick'])
          -- TODO set nick on "NICK" message
          setNick nick'

      -- RFC2812 ERR_UNAVAILRESOURCE
      --Message (Just _servername) "437" (_msgtarget:nickunavail:_reason:[]) -> do

      -- RFC2812 RPL_WELCOME
      Message _ "001" [_nick,_s] -> do
        --logStrLn $ SGR [32,1] (Plain s)
        sendMsg (Message Nothing "JOIN" [ BS.intercalate "," (channels cfg) ])


      _ -> return ()