-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPop3.hs
More file actions
74 lines (67 loc) · 2.09 KB
/
Pop3.hs
File metadata and controls
74 lines (67 loc) · 2.09 KB
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
module Pop3 where
import Network.Socket
import Control.Exception (bracketOnError,finally)
import System.IO
import Data.List (intercalate, isPrefixOf, isSuffixOf)
import Data.Char (isSpace, isControl)
import Data.Maybe
genAuth u p = ["USER "++u, "PASS "++p]
genList = ["LIST"]
genList' id = ["LIST " ++ show id]
genRetr id= ["RETR "++ show id]
genQuit = ["QUIT"]
stripSpace = dropWhile (\x -> isSpace x || isControl x)
getReply h doMultipleLine = do
x <- hGetLine h
if not doMultipleLine then
if "\r" `isSuffixOf` x then return $ init x
else return x
else do
rest <- readOther
return $ intercalate "\r\n" (x:rest)
where
readOther = do
line <- getReply h False
if line == "." then return []
else do
others <- readOther
return (line:others)
isMultipleLine:: String -> Bool
isMultipleLine x
| equiv [x] genList = True
| equiv [x] (genRetr 0) = True
|otherwise = False
where
unpackC = words . head
equiv a b = length ua == length ub && head ua == head ub where
ua = unpackC a
ub = unpackC b
checkReply contents
| isPrefixOf "+OK" $head strippedMsg = (True, logMsg)
| otherwise = (False, logMsg)
where strippedMsg = filter (not . null) $ map stripSpace $ lines contents
logMsg = intercalate "\r\n" strippedMsg
listMail = talk genList
listMail' id = talk (genList' id)
retrMail id = talk (genRetr id)
talk cmdList log popAddr user pass = bracketOnError (socket (addrFamily popAddr) Stream defaultProtocol)
sClose
(\sock -> do
connect sock (addrAddress popAddr)
socketToHandle sock ReadWriteMode
) >>= ( \ h -> (do
--hSetBuffering h LineBuffering
getReply h False
msg <- mapM (\cmd ->
hPutStr h (cmd++"\r\n") >>
hFlush h >>
log (cmd++"\r\n") >>
getReply h (isMultipleLine cmd) >>=
return . checkReply >>=
(\(res, msg)->
if res then return (cmd, msg) else fail $ "talking failed in " ++ cmd ++ "\n" ++ msg
))
(genAuth user pass ++ cmdList ++ genQuit)
return . fromMaybe "" . lookup (head cmdList) $msg
) `finally` hClose h
)