An IRC bot for learning, fun and collaboration in the Freepost community.

[[ 🗃 ^VvM9v funbot ]] :: [📥 Inbox] [📤 Outbox] [🐤 Followers] [🤝 Collaborators] [🛠 Commits]

Clone

HTTPS: git clone https://vervis.peers.community/repos/VvM9v

SSH: git clone USERNAME@vervis.peers.community:VvM9v

Branches

Tags

master :: src / FunBot / Sources /

WebListener.hs

{- This file is part of funbot.
 -
 - Written in 2015 by fr33domlover <fr33domlover@rel4tion.org>.
 -
 - ♡ Copying is an act of love. Please copy, reuse and share.
 -
 - The author(s) have dedicated all copyright and related and neighboring
 - rights to this software to the public domain worldwide. This software is
 - distributed without any warranty.
 -
 - You should have received a copy of the CC0 Public Domain Dedication along
 - with this software. If not, see
 - <http://creativecommons.org/publicdomain/zero/1.0/>.
 -}

module FunBot.Sources.WebListener
    ( webListenerSource
    )
where

import Data.ByteString.Lazy (empty)
import FunBot.Sources.WebListener.Client (dispatchClient)
import FunBot.Sources.WebListener.GitLab (dispatchGitLab)
import FunBot.Sources.WebListener.Gogs (dispatchGogs)
import FunBot.Types
import Network.HTTP (Request (..), RequestMethod (..), Response (..))
import Network.HTTP.Listen (run)
import Network.IRC.Fun.Bot.Logger
import Network.IRC.Fun.Bot.State (askEnvS)
import Network.URI (uriPath)

resp = Response
    { rspCode    = (2, 0, 0)
    , rspReason  = "OK"
    , rspHeaders = []
    , rspBody    = empty
    }

listener dlogger elogger push pushMany request = do
    logLine dlogger $ show request
    logLine dlogger $ show $ rqBody request
    case (uriPath $ rqURI request, rqMethod request) of
        ("/gogs", POST)   -> do
            dispatchGogs push pushMany request
            return Nothing
        ("/gitlab", POST) -> do
            dispatchGitLab push pushMany request
            return $ Just resp
        ("/client", POST) -> do
            dispatchClient push pushMany request
            return Nothing
        _ -> do
            logLine elogger "Web listener source: \
                            \Unrecognized request received:"
            logLine elogger $ show request
            return Nothing

webListenerSource :: FilePath -> ExtEventSource
webListenerSource elogfile _config env push pushMany mkLogger = do
    elogger <- mkLogger elogfile
    dlogger <- mkLogger "state/web-access.log"
    putStrLn "Bot: Web listener source listening to HTTP requests"
    run (webHookSourcePort env) $ listener dlogger elogger push pushMany
[See repo JSON]