summaryrefslogtreecommitdiff
path: root/fig-bridge-irc-discord/src/Fig/Bridge/IRCDiscord.hs
blob: c3232cba6afcbcef7f7616fe8bd8bf11db035e8c (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
{-# Language QuasiQuotes #-}

module Fig.Bridge.IRCDiscord where

import Fig.Prelude

import qualified Data.List as List

import Fig.Bridge.IRCDiscord.Utils
import Fig.Utils.SExpr
import Fig.Bus.Client

bridge :: Config -> (Text, Text) -> IO ()
bridge cfg busAddr = do
  busClient busAddr
    (\cmds -> do
        cmds.subscribe [sexp|(monitor irc chat incoming)|]
        cmds.subscribe [sexp|(monitor discord chat incoming)|]
    )
    (\cmds d -> do
        case d of
          SExprList [ev, tchan, user, _, msg]
            | ev == [sexp|(monitor irc chat incoming)|]
            , SExprString chan <- tchan ->
              case List.find ((== chan) . snd) cfg.mapping of
                Nothing -> log $ mconcat
                  [ "Message on unmapped IRC channel: " <> tshow chan
                  ]
                Just (dchan, _) -> do
                  log $ mconcat
                    [ "Incoming message on IRC channel " <> tshow chan
                    , ", bridging to Discord channel " <> tshow dchan
                    ]
                  cmds.publish [sexp|(monitor discord chat outgoing)|]
                    [ SExprInteger $ fromIntegral dchan
                    , user
                    , msg
                    ]
            | ev == [sexp|(monitor discord chat incoming)|]
            , SExprInteger chan <- tchan ->
              case List.find ((== fromInteger chan) . fst) cfg.mapping of
                Nothing -> log $ mconcat
                  [ "Message on unmapped Discord channel: " <> tshow chan
                  ]
                Just (_, ichan) -> do
                  log $ mconcat
                    [ "Incoming message on Discord channel " <> tshow chan
                    , ", bridging to IRC channel " <> ichan
                    ]
                  cmds.publish [sexp|(monitor irc chat outgoing)|]
                    [ SExprString ichan
                    , user
                    , msg
                    ]
          _ -> log $ "Invalid message: " <> tshow d
    )
    (pure ())