aboutsummaryrefslogtreecommitdiff
path: root/vendor/irc-client/Network/IRC/Client/Internal.hs
blob: c56bd571d84cddfd105e7bc76493ac03056d89d3 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}

-- |
-- Module      : Network.IRC.Client.Internal
-- Copyright   : (c) 2016 Michael Walker
-- License     : MIT
-- Maintainer  : Michael Walker <mike@barrucadu.co.uk>
-- Stability   : experimental
-- Portability : CPP, OverloadedStrings, ScopedTypeVariables
--
-- Most of the hairy code. This isn't all internal, due to messy
-- dependencies, but I've tried to make this as \"internal\" as
-- reasonably possible.
--
-- This module is NOT considered to form part of the public interface
-- of this library.
module Network.IRC.Client.Internal
  ( module Network.IRC.Client.Internal
  , module Network.IRC.Client.Internal.Lens
  , module Network.IRC.Client.Internal.Types
  ) where

import           Control.Concurrent                (forkIO, killThread,
                                                    myThreadId, threadDelay,
                                                    throwTo)
import           Control.Concurrent.STM            (STM, atomically, readTVar,
                                                    readTVarIO, writeTVar)
import           Control.Concurrent.STM.TBMChan    (TBMChan, closeTBMChan,
                                                    isClosedTBMChan,
                                                    isEmptyTBMChan, newTBMChan,
                                                    readTBMChan, writeTBMChan)
import           Control.Monad                     (forM_, unless, void, when)
import           Control.Monad.Catch               (SomeException, catch)
import           Control.Monad.IO.Class            (MonadIO, liftIO)
import           Control.Monad.Reader              (ask, runReaderT)
import           Data.ByteString                   (ByteString, isPrefixOf)
import           Data.Conduit                      (ConduitM, await,
                                                    awaitForever, yield, (.|))
import           Data.IORef                        (IORef, newIORef, readIORef,
                                                    writeIORef)
import qualified Data.Set                          as S
import           Data.Text                         (Text)
import           Data.Text.Encoding                (decodeUtf8', decodeLatin1,
                                                    encodeUtf8)
import           Data.Time.Clock                   (NominalDiffTime, UTCTime,
                                                    addUTCTime, diffUTCTime,
                                                    getCurrentTime)
import           Data.Time.Format                  (formatTime)
import           Data.Void                         (Void)
import           Network.IRC.Conduit               (Event(..), Message(..),
                                                    Source(..), floodProtector,
                                                    rawMessage, toByteString)

#if MIN_VERSION_time(1,5,0)
import           Data.Time.Format                  (defaultTimeLocale)
#else
import           System.Locale                     (defaultTimeLocale)
#endif

import           Network.IRC.Client.Internal.Lens
import           Network.IRC.Client.Internal.Types
import           Network.IRC.Client.Lens


-------------------------------------------------------------------------------
-- * Configuration

-- | Config to connect to a server using the supplied connection
-- function.
setupInternal
  :: (IO () -> ConduitM (Either ByteString (Event ByteString)) Void IO () -> ConduitM () (Message ByteString) IO () -> IO ())
  -- ^ Function to start the network conduits.
  -> IRC s ()
  -- ^ Connect handler
  -> (Maybe SomeException -> IRC s ())
  -- ^ Disconnect handler
  -> (Origin -> ByteString -> IO ())
  -- ^ Logging function
  -> ByteString
  -- ^ Server hostname
  -> Int
  -- ^ Server port
  -> ConnectionConfig s
setupInternal f oncon ondis logf host port_ = ConnectionConfig
  { _func         = f
  , _username     = "irc-client"
  , _realname     = "irc-client"
  , _password     = Nothing
  , _server       = host
  , _port         = port_
  , _flood        = 1
  , _timeout      = 300
  , _onconnect    = oncon
  , _ondisconnect = ondis
  , _logfunc      = logf
  }


-------------------------------------------------------------------------------
-- * Event loop

-- | The event loop.
runner :: IRC s ()
runner = do
  state <- getIRCState
  let cconf = _connectionConfig state

  -- Set the real- and user-name
  let theUser = get username cconf
  let theReal = get realname cconf
  let thePass = get password cconf

  -- Initialise the IRC session
  let initialise = flip runIRCAction state $ do
        liftIO . atomically $ writeTVar (_connectionState state) Connected
        mapM_ (\p -> sendBS $ rawMessage "PASS" [encodeUtf8 p]) thePass
        sendBS $ rawMessage "USER" [encodeUtf8 theUser, "-", "-", encodeUtf8 theReal]
        _onconnect cconf

  -- Run the event loop, and call the disconnect handler if the remote
  -- end closes the socket.
  antiflood <- liftIO $ floodProtector (_flood cconf)

  -- An IORef to keep track of the time of the last received message, to allow a local timeout.
  lastReceived <- liftIO $ newIORef =<< getCurrentTime

  squeue <- liftIO . readTVarIO $ _sendqueue state

  let source = sourceTBMChan squeue
               .| antiflood
               .| logConduit (_logfunc cconf FromClient . toByteString . concealPass)
  let sink   = forgetful
               .| logConduit (_logfunc cconf FromServer . _raw)
               .| eventSink lastReceived state

  -- Fork a thread to disconnect if the timeout elapses.
  mainTId <- liftIO myThreadId
  let time  = _timeout cconf
  let delayms = 1000000 * round time
  let timeoutThread = do
        now <- getCurrentTime
        prior <- readIORef lastReceived
        if diffUTCTime now prior >= time
          then throwTo mainTId Timeout
          else threadDelay delayms >> timeoutThread
  timeoutTId <- liftIO (forkIO timeoutThread)

  -- Start the client.
  (exc :: Maybe SomeException) <- liftIO $ catch
    (_func cconf initialise sink source >> killThread timeoutTId >> pure Nothing)
    (pure . Just)

  disconnect
  _ondisconnect cconf exc

-- | Forget failed decodings.
forgetful :: Monad m => ConduitM (Either a b) b m ()
forgetful = awaitForever go where
  go (Left  _) = return ()
  go (Right b) = yield b

-- | Block on receiving a message and invoke all matching handlers.
eventSink :: MonadIO m => IORef UTCTime -> IRCState s -> ConduitM (Event ByteString) o m ()
eventSink lastReceived ircstate = go where
  go = await >>= maybe (return ()) (\event -> do
    -- Record the current time.
    now <- liftIO getCurrentTime
    liftIO $ writeIORef lastReceived now

    -- Handle the event.
    let event' = decodeEncodingIRC <$> event
    ignored <- isIgnored ircstate event'
    unless ignored . liftIO $ do
      iconf <- snapshot instanceConfig ircstate
      forM_ (get handlers iconf) $ \(EventHandler matcher handler) ->
        maybe (pure ())
              (void . flip runIRCAction ircstate . handler (_source event'))
              (matcher event')

    -- If disconnected, do not loop.
    disconnected <- liftIO . atomically $ (==Disconnected) <$> getConnectionState ircstate
    unless disconnected go)

  decodeEncodingIRC :: ByteString -> Text
  decodeEncodingIRC bs =
    case decodeUtf8' bs of
      Left _ -> decodeLatin1 bs
      Right t -> t

-- | Check if an event is ignored or not.
isIgnored :: MonadIO m => IRCState s -> Event Text -> m Bool
isIgnored ircstate ev = do
  iconf <- liftIO . readTVarIO . _instanceConfig $ ircstate
  let ignoreList = _ignore iconf

  return $
    case _source ev of
      User      n ->  (n, Nothing) `elem` ignoreList
      Channel c n -> ((n, Nothing) `elem` ignoreList) || ((n, Just c) `elem` ignoreList)
      Server  _   -> False

-- |A conduit which logs everything which goes through it.
logConduit :: MonadIO m => (a -> IO ()) -> ConduitM a a m ()
logConduit logf = awaitForever $ \x -> do
  -- Call the logging function
  liftIO $ logf x

  -- And pass the message on
  yield x

-- | Print messages to stdout, with the current time.
stdoutLogger :: Origin -> ByteString -> IO ()
stdoutLogger origin x = do
  now <- getCurrentTime

  putStrLn $ unwords
    [ formatTime defaultTimeLocale "%c" now
    , if origin == FromServer then "<---" else "--->"
    , init . tail $ show x
    ]

-- | Append messages to a file, with the current time.
fileLogger :: FilePath -> Origin -> ByteString -> IO ()
fileLogger fp origin x = do
  now <- getCurrentTime

  appendFile fp $ unwords
    [ formatTime defaultTimeLocale "%c" now
    , if origin == FromServer then "--->" else "<---"
    , init . tail $ show x
    , "\n"
    ]

-- | Do no logging.
noopLogger :: a -> b -> IO ()
noopLogger _ _ = return ()

-- | Clear passwords from logs.
concealPass :: Message ByteString -> Message ByteString
concealPass (RawMsg msg)
  | "PASS " `isPrefixOf` msg = rawMessage "PASS" ["<password redacted>"]
concealPass m = m


-------------------------------------------------------------------------------
-- * Messaging

-- | Send a message as UTF-8, using TLS if enabled. This blocks if
-- messages are sent too rapidly.
send :: Message Text -> IRC s ()
send = sendBS . fmap encodeUtf8

-- | Send a message, using TLS if enabled. This blocks if messages are
-- sent too rapidly.
sendBS :: Message ByteString -> IRC s ()
sendBS msg = do
  qv <- _sendqueue <$> getIRCState
  liftIO . atomically $ flip writeTBMChan msg =<< readTVar qv


-------------------------------------------------------------------------------
-- * Disconnecting

-- | Disconnect from the server, properly tearing down the TLS session
-- (if there is one).
disconnect :: IRC s ()
disconnect = do
  s <- getIRCState

  liftIO $ do
    connState <- readTVarIO (_connectionState s)
    case connState of
      Connected -> do
        -- Set the state to @Disconnecting@
        atomically $ writeTVar (_connectionState s) Disconnecting

        -- Wait for all messages to be sent, or a minute has passed.
        timeoutBlock 60 . atomically $ do
          queue <- readTVar (_sendqueue s)
          (||) <$> isEmptyTBMChan queue <*> isClosedTBMChan queue

        -- Close the chan, which closes the sending conduit, and set
        -- the state to @Disconnected@.
        atomically $ do
          closeTBMChan =<< readTVar (_sendqueue s)
          writeTVar (_connectionState s) Disconnected

        -- Kill all managed threads. Don't wait for them to terminate
        -- here, as they might be masking exceptions and not pick up
        -- the 'Disconnect' for a while; just clear the list.
        mapM_ (`throwTo` Disconnect) =<< readTVarIO (_runningThreads s)
        atomically $ writeTVar (_runningThreads s) S.empty

      -- If already disconnected, or disconnecting, do nothing.
      _ -> pure ()

-- | Disconnect from the server (this will wait for all messages to be
-- sent, or a minute to pass), and then connect again.
--
-- This can be called after the client has already disconnected, in
-- which case it will just connect again.
--
-- Like 'runClient' and 'runClientWith', this will not return until
-- the client terminates (ie, disconnects without reconnecting).
reconnect :: IRC s ()
reconnect = do
  disconnect

  -- create a new send queue
  s <- getIRCState
  liftIO . atomically $
    writeTVar (_sendqueue s) =<< newTBMChan 16

  runner


-------------------------------------------------------------------------------
-- * Utils

-- | Interact with a client from the outside, by using its 'IRCState'.
runIRCAction :: MonadIO m => IRC s a -> IRCState s -> m a
runIRCAction ma = liftIO . runReaderT (runIRC ma)

-- | Access the client state.
getIRCState :: IRC s (IRCState s)
getIRCState = ask

-- | Get the connection state from an IRC state.
getConnectionState :: IRCState s -> STM ConnectionState
getConnectionState = readTVar . _connectionState

-- | Block until an action is successful or a timeout is reached.
timeoutBlock :: MonadIO m => NominalDiffTime -> IO Bool -> m ()
timeoutBlock dt check = liftIO $ do
  finish <- addUTCTime dt <$> getCurrentTime
  let wait = do
        now  <- getCurrentTime
        cond <- check
        when (now < finish && not cond) wait
  wait

-- | A simple wrapper around a TBMChan. As data is pushed into the
-- channel, the source will read it and pass it down the conduit
-- pipeline. When the channel is closed, the source will close also.
--
-- If the channel fills up, the pipeline will stall until values are
-- read.
--
-- From stm-conduit-3.0.0 (by Clark Gaebel <cg.wowus.cg@gmail.com>)
sourceTBMChan :: MonadIO m => TBMChan a -> ConduitM () a m ()
sourceTBMChan ch = loop where
  loop = do
    a <- liftIO . atomically $ readTBMChan ch
    case a of
      Just x  -> yield x >> loop
      Nothing -> pure ()