]> gitweb.michael.orlitzky.com - dead/halcyon.git/blobdiff - src/Mail.hs
Fix remaining hlint suggestions.
[dead/halcyon.git] / src / Mail.hs
index 2e86a204e003ceda90bfb32994fe4e83f386ad48..db5673f62c84bef2964e2e1cb4fb188ec3d43a49 100644 (file)
@@ -5,6 +5,7 @@ where
 
 import Control.Concurrent
 import Control.Exception (evaluate)
+import Control.Monad (liftM)
 import Data.List (intercalate)
 import Data.Time (formatTime, getZonedTime)
 import System.Exit
@@ -15,7 +16,7 @@ import System.IO (hClose, hGetContents, hPutStr)
 
 type Header = String
 
--- |A crude model of an RFC821 email message.
+-- | A crude model of an RFC821 email message.
 data Message = Message { headers :: [Header],
                          subject :: String,
                          body    :: String,
@@ -23,44 +24,48 @@ data Message = Message { headers :: [Header],
                          to      :: String }
              deriving (Eq)
 
--- |The default headers attached to each message.
--- The MIME junk is needed for UTF-8 to work properly.
--- Note that your mail server should support the 8BITMIME extension.
+-- | The default headers attached to each message.  The MIME junk is
+--   needed for UTF-8 to work properly. Note that your mail server
+--   should support the 8BITMIME extension.
 default_headers :: [Header]
 default_headers = ["MIME-Version: 1.0",
                    "Content-Type: text/plain; charset=UTF-8",
                    "Content-Transfer-Encoding: 8bit"]
 
--- |Showing a message will print it in roughly RFC-compliant
--- form. This form is sufficient for handing the message off to
--- sendmail (or compatible).
+-- | Showing a message will print it in roughly RFC-compliant
+--   form. This form is sufficient for handing the message off to
+--   sendmail (or compatible).
 instance Show Message where
-    show m =
-        concat [ if (length (headers m) == 0) then "" else (intercalate "\n" (headers m)) ++ "\n",
-                 "Subject: " ++ (subject m) ++ "\n",
-                 "From: " ++ (from m) ++ "\n",
-                 "To: " ++ (to m) ++ "\n",
-                 "\n",
-                 (body m) ]
-
+  show m =
+    concat [ formatted_headers,
+             "Subject: " ++ (subject m) ++ "\n",
+             "From: " ++ (from m) ++ "\n",
+             "To: " ++ (to m) ++ "\n",
+             "\n",
+             (body m) ]
+    where
+      formatted_headers =
+        if null (headers m)
+        then ""
+        else (intercalate "\n" (headers m)) ++ "\n"
 
 
 -- |Pad a string on the left with zeros until the entire string has
 -- length n.
 pad_left :: String -> Int -> String
 pad_left str n
        | n < (length str) = str
        | otherwise = (replicate num_zeros '0') ++ str
-    where num_zeros = n - (length str)
+ | n < (length str) = str
+ | otherwise = (replicate num_zeros '0') ++ str
+   where num_zeros = n - (length str)
 
 
 
 -- | Constructs a 'String' in RFC822 date format for the current
 --   date/time.
 rfc822_now :: IO String
-rfc822_now = do
-  date <- getZonedTime
-  return $ formatTime defaultTimeLocale rfc822DateFormat date
+rfc822_now =
+  liftM (formatTime defaultTimeLocale rfc822DateFormat) getZonedTime
+
 
 
 
@@ -69,7 +74,8 @@ rfc822_now = do
 sendmail :: FilePath -> Message -> IO (String, String, ExitCode)
 sendmail sendmail_path message = do
   let sendmail_args = ["-f",
-                       (from message)]
+                       (from message),
+                       (to message)]
 
   (inh, outh, errh, ph) <-
       runInteractiveProcess sendmail_path sendmail_args Nothing Nothing
@@ -95,9 +101,9 @@ sendmail sendmail_path message = do
 -- errors, and exit codes.  This function pretty-prints one of those
 -- three-tuples.
 print_sendmail_result :: (String, String, ExitCode) -> IO ()
-print_sendmail_result (outs, errs, ec) = do
-    case ec of
-      ExitSuccess -> return ()
-      _ -> putStrLn $ concat ["Output: " ++ outs,
-                              "\nErrors: " ++ errs,
-                              "\nExit Code: " ++ (show ec)]
+print_sendmail_result (outs, errs, ec) =
+  case ec of
+    ExitSuccess -> return ()
+    _ -> putStrLn $ concat ["Output: " ++ outs,
+                            "\nErrors: " ++ errs,
+                            "\nExit Code: " ++ (show ec)]