module Network.Gopher.Types
( GopherFileType (..)
, GopherResponse (..)
, GopherMenuItem (..)
, fileTypeToChar
, charToFileType
, isFile
)
where
import Prelude hiding (lookup)
import Network.Gopher.Util
import Data.ByteString (ByteString, pack, unpack)
import qualified Data.ByteString as B
import Data.Char (ord, chr)
import Data.Map (Map (), fromList, lookup)
import Data.Maybe (fromJust, fromMaybe)
import Data.Tuple (swap)
import Data.Word (Word8 ())
import System.FilePath (splitPath, takeBaseName)
data GopherMenuItem = Item GopherFileType ByteString FilePath (Maybe ByteString) (Maybe Integer)
deriving (Show, Eq)
data GopherResponse
= MenuResponse [GopherMenuItem]
| FileResponse ByteString
| ErrorResponse String
deriving (Show, Eq)
data GopherFileType
= File
| Directory
| PhoneBookServer
| Error
| BinHexMacintoshFile
| DOSArchive
| UnixUuencodedFile
| IndexSearchServer
| TelnetSession
| BinaryFile
| RedundantServer
| Tn3270Session
| GifFile
| ImageFile
| InfoLine
| Html
deriving (Show, Eq, Ord, Enum)
fileTypeToChar :: GopherFileType -> Word8
fileTypeToChar t = asciiOrd $
case t of
File -> '0'
Directory -> '1'
PhoneBookServer -> '2'
Error -> '3'
BinHexMacintoshFile -> '4'
DOSArchive -> '5'
UnixUuencodedFile -> '6'
IndexSearchServer -> '7'
TelnetSession -> '8'
BinaryFile -> '9'
RedundantServer -> '+'
Tn3270Session -> 'T'
GifFile -> 'g'
ImageFile -> 'I'
InfoLine -> 'i'
Html -> 'h'
charToFileType :: Word8 -> GopherFileType
charToFileType c =
case asciiChr c of
'0' -> File
'1' -> Directory
'2' -> PhoneBookServer
'3' -> Error
'4' -> BinHexMacintoshFile
'5' -> DOSArchive
'6' -> UnixUuencodedFile
'7' -> IndexSearchServer
'8' -> TelnetSession
'9' -> BinaryFile
'+' -> RedundantServer
'T' -> Tn3270Session
'g' -> GifFile
'I' -> ImageFile
'i' -> InfoLine
'h' -> Html
_ -> InfoLine
isFile :: GopherFileType -> Bool
isFile File = True
isFile BinHexMacintoshFile = True
isFile DOSArchive = True
isFile UnixUuencodedFile = True
isFile GifFile = True
isFile ImageFile = True
isFile _ = False