module Phizzle.Phizzly (loadPhishFile, maliciousLink) where
import qualified Data.ByteString.Lazy as LB
import Data.ByteString.Lazy (ByteString)
import qualified Data.Text as T
import Data.Text (Text)
import Text.Regex.PCRE
import Data.Aeson
import Control.Monad
import Network.URI
import Data.List (isPrefixOf, isSuffixOf)
import Phizzle.Types
getJsonFileData :: String -> IO ByteString
getJsonFileData = LB.readFile
jsonToWholePhish :: String -> IO (Either String [PhishTank])
jsonToWholePhish jData = eitherDecode <$> getJsonFileData jData
loadPhishFile :: String -> IO (Either String [PhishTank])
loadPhishFile filepath = jsonToWholePhish $ filepath
getURIParts :: Maybe URI -> Maybe String
getURIParts uriData = let path = uriPath <$> uriData
regName = uriRegName <$> (join $ uriAuthority <$> uriData)
in ( (flip (++)) . dropSlash ) <$> path <*> regName
where
dropSlash :: String -> String
dropSlash xs
| "/" `isSuffixOf` xs = init xs
| otherwise = xs
maliciousLink :: String -> [PhishTank] -> IO (Maybe Bool)
maliciousLink lnk pt = do
linkExists pt
where
linkExists [] = return $ Just False
linkExists (x:xs) = let lnkParts = getURIParts $ parseURI $ lnk
in case lnkParts of
Nothing -> return Nothing
Just v -> let choppedLnk = if "www." `isPrefixOf` v
then drop 4 v
else v
in case ( (T.unpack (url x)) =~ ("^(http|https)?(://)?(www.)?" ++ choppedLnk ++ "/?$") :: Bool) of
True -> return $ Just True
False -> linkExists xs