summaryrefslogtreecommitdiffstats
path: root/main.hs
blob: dc8a14bb69209d062f933bf6a180cfb82a625812 (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
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}

module Main (main) where

import Prelude hiding (print)
import Scanner
import Event
import Blessings
import Data.Char
import Data.Monoid
import Control.Arrow
import Control.Concurrent
import Control.Concurrent.MSampleVarX
import Control.Exception
import Control.Monad
import Control.Monad.Reader (ask, asks)
import Control.Monad.IO.Class (liftIO)
import System.IO hiding (print)
import System.Posix.Signals
import Data.Time
import Data.Time.Clock.POSIX
import qualified Hirc


newRelay :: IO (a -> IO (), IO a)
newRelay = (putMVar &&& takeMVar) <$> newEmptyMVar

newSampler :: IO (Maybe a -> IO (), IO a)
newSampler = (write &&& readSV) <$> newEmptySV
  where write v n = maybe (emptySV v) (writeSV v) n

newSemaphore :: IO (IO (), IO ())
newSemaphore = (flip putMVar () &&& takeMVar) <$> newEmptyMVar


type Combo = [String]

nop :: a -> IO ()
nop = const (return ())

data State = State
    { s_now :: UTCTime
    , s_input :: String
    , s_output :: [String]
    , s_combo :: Combo
    , s_combo_hist :: [(UTCTime, Either Combo Combo)]
    , s_setTick :: Maybe Int -> IO ()
    , s_hircBot :: Maybe Hirc.Bot
    }

newState :: State
newState = State
    { s_now = posixSecondsToUTCTime 0
    , s_input = ""
    , s_output = []
    , s_combo = []
    , s_combo_hist = []
    , s_setTick = nop
    , s_hircBot = Nothing
    }


cleanup :: State -> IO ()
cleanup _q = do
    hPutStrLn stdout ""
    hSetEcho stdin True


main :: IO ()
main = bracket (return newState) cleanup $ \q0 -> do
    hSetEcho stdin False
    hSetBuffering stdin NoBuffering
    hSetBuffering stdout (BlockBuffering $ Just 4096)

    -- communication channels
    (sendEvent, receiveEvent) <- newRelay
    (shutdown, awaitShutdown) <- newSemaphore
    (setTick, getTick) <- newSampler

    let q1 = q0 { s_setTick = setTick }

    mapM_ (\(s, f) -> installHandler s (Catch f) Nothing)
        [ (sigINT, shutdown)
        -- , (28, winchHandler sendEvent)
        ]

    threadIds <- mapM forkIO
        [ forever $ scan stdin >>= sendEvent . EScan
        , run receiveEvent q1
        , runTick getTick sendEvent
        , runIrc sendEvent ic
        ]

    awaitShutdown
    mapM_ killThread threadIds

  where
    ic = Hirc.Config
            { Hirc.config_server = Hirc.Server "cd.retiolum" 6667
            , Hirc.config_nick = "hirc"
            , Hirc.config_chan = "#hirc-testing"
            }

runIrc :: (Event -> IO ()) -> Hirc.Config -> IO ()
runIrc sendEvent c =
    Hirc.runBot c Hirc.Hooks
        { Hirc.hooks_onConnect = onConnect
        , Hirc.hooks_onDisconnect = onDisconnect
        , Hirc.hooks_onError = onError
        , Hirc.hooks_onMessage = onMessage
        }
  where
    onConnect = do
        liftIO $ sendEvent $ EPutLn "Hirc Connect"
        bot <- ask
        liftIO $ sendEvent $ EHircConnect bot
        Hirc.read Hirc.bot_nick >>= \nick -> do
            send "NICK" nick
            send "USER" (nick ++ " 0 * :hirc bot")

    onDisconnect = do
        liftIO $ sendEvent $ EPutLn "Hirc Disconnect"

    onError e =
        liftIO
            $ sendEvent $ EPutLn
            $ pp $ SGR [31,1] $ "Hirc Error: " <> Plain (show e)

    onMessage m = do
        liftIO $ sendEvent (EPutLn $ Hirc.formatMessage m)

send :: String -> String -> Hirc.Net ()
send s t = do
    h <- asks Hirc.bot_socket
    --liftIO $ hPrintf h "%s %s\r\n" s t
    liftIO $ hPutStr h $ s <> " " <> t <> "\r\n"
    --liftIO $ printf    "> %s %s\n" s t
    --liftIO $ printf    "> %s %s\n" s t



runTick :: IO Int -> (Event -> IO ()) -> IO ()
runTick getTick sendEvent = forever $ do
    ms <- getTick
    sendEvent ETick
    threadDelay (ms * 1000)




run :: IO Event -> State -> IO ()
run receiveEvent = rec where
    rec q = do
      e <- receiveEvent
      t <- getCurrentTime -- TODO put into receiveEvent
      processEvent q { s_now = t } e >>= decay >>= print q >>= rec

decay :: State -> IO State
decay q@State{s_now=now,s_combo_hist=hist,s_setTick=setTick} = do
    let hist' = filter ((<1000) . msAge now . fst) hist
    when (length hist' == 0) $ do
        setTick Nothing
    return q
        { s_combo_hist = hist'
        }


print :: State -> State -> IO State

-- print pending output if any
print _ q@State{s_output=xs@(_:_)} = do
    let q' = q { s_output = [] }
    mapM_ (flip putLn q') (map ("! "<>) xs)
    flush q'
    return q'

-- otherwise check if command line has to be redrawn
print State{s_combo=c0,s_input=i0} q@State{s_combo=c,s_input=i} = do
    when needRedraw (redrawCmdLn q >> flush q)
    return q
  where
    needRedraw = i0 /= i || c0 /= c



acceptCombo :: State -> IO State
acceptCombo q@State{s_setTick=setTick} = do
    setTick (Just 100)
    return q
        { s_combo = []
        , s_combo_hist = (s_now q, Right $ s_combo q) : s_combo_hist q
        }

rejectCombo :: State -> IO State
rejectCombo q@State{s_setTick=setTick} = do
    setTick (Just 100)
    return q
        { s_combo = []
        , s_combo_hist = (s_now q, Left $ s_combo q) : s_combo_hist q
        }


onKey :: State -> IO State

-- output non-empty input buffer
onKey q@State{s_combo=["\n"],s_input=i@(_:_),s_output=o} = do
    case s_hircBot q of
        Just Hirc.Bot{Hirc.bot_socket=h} ->
            hPutStr h $ i <> "\r\n"
        Nothing -> return ()
    acceptCombo q
        { s_input = ""
        , s_output = o ++ [i]
        }

-- delete char from input buffer
onKey q@State{s_combo=["\DEL"],s_input=i@(_:_)} =
    acceptCombo q { s_input = init i }

-- append printable chars to the input buffer
onKey q@State{s_combo=[k0],s_input=i} | length k0 == 1 && isPrint (k0!!0) =
    acceptCombo q { s_input = i ++ k0 }

-- cancel current combo
onKey q@State{s_combo="\ESC":_:_}=
    acceptCombo q { s_combo = ["\ESC"] }

onKey q =
    rejectCombo q


processEvent :: State -> Event -> IO State
processEvent q@State{s_now=_now} = \case
    --EFlash t ->
    --    return q { flashMessage = t }

    EScan (ScanKey s) -> onKey q { s_combo = s : s_combo q }

    EPutLn s -> do
        putLn s q
        flush q
        return q

    ETick -> do
        --putLn ("Tick: " ++ show now) q
        redrawCmdLn q
        flush q
        return q

    EHircConnect b -> do
        return q { s_hircBot = Just b }

    EHircMessage m -> do
        return q { s_output = s_output q ++ [show m] }

    _ev ->
        return q -- $ q ++ show ev
        --    { flashMessage = SGR [31,1] $ Plain $ "unhandled event: " <> show ev
        --    }

bell :: State -> IO ()
bell _q = do
    hPutStr stdout "\a"

redrawCmdLn :: State -> IO ()
redrawCmdLn q = delCmdLn q >> putCmdLn q

delCmdLn :: State -> IO ()
delCmdLn _q = do
    hPutStr stdout $ "\ESC[2K\ESC[666D"

flush :: State -> IO ()
flush _q = do
    hFlush stdout

putLn :: String -> State -> IO ()
putLn s q = do
    delCmdLn q
    hPutStrLn stdout s
    putCmdLn q

putCmdLn :: State -> IO ()
putCmdLn q@State{s_now=now} =do
    hPutStr stdout $ ""
        ++ "> "
        ++ s_input q
        ++ "\ESC[s"
        ++ " " ++ pp (SGR [38,5,162] $ showCombo (s_combo q))
        -- ++ " " ++ pp (showLastCombo (s_last_combo q))
        ++ pp (mconcat
                  $ map (showLastCombo now) (s_combo_hist q))
        ++ "\ESC[u"

-- TODO instance Show Combo
showCombo :: Combo -> Blessings String
showCombo ks = mconcat (concatMap (map renderChar) (reverse ks))

showLastCombo :: UTCTime -> (UTCTime, Either Combo Combo) -> Blessings String
showLastCombo now = \case
    (t, Right ks) -> SGR [38,5,selectByAge ackColors t] (showCombo ks)
    (t, Left ks)  -> SGR [38,5,selectByAge nakColors t] (showCombo ks)
  where
    selectByAge colors t = do
        let ms = fromIntegral (msAge now t) :: Float
            i = round (ms / 100)
        if i < length colors
            then colors !! i
            else 234

    ackColors = [ 46,  46,  46,  40,  34,  28,  22]
    nakColors = [196, 196, 196, 160, 124,  88,  52]



msAge :: UTCTime -> UTCTime -> Int
msAge t2 t1 = do
      let (s,ms) = properFraction (diffUTCTime t2 t1)
      s * 1000 + round (ms * 1000)


renderChar :: Char -> Blessings String
renderChar c =
    if isPrint c
        then Plain [c]
        else SGR [1] (Plain $ showLitChar c "")