]> woffs.de Git - fd/haskell-amqp-utils.git/blobdiff - agitprop.hs
option cleanup
[fd/haskell-amqp-utils.git] / agitprop.hs
index c14f68b37350fcc4ce2d740e9e8cbb7d69620c8a..fd4922d298b621a7094b9e95db20168f8febe555 100644 (file)
@@ -5,9 +5,13 @@ import qualified Control.Exception as X
 import Control.Monad (forever)
 import qualified Data.ByteString.Lazy.Char8 as BL
 import qualified Data.Text as T
+import Data.List (isSuffixOf)
+import Data.Time
+import Data.Time.Clock.POSIX
 import Data.Version (showVersion)
 import Data.Word (Word64)
 import Network.AMQP
+import Network.AMQP.Types
 import Network.AMQP.Utils.Connection
 import Network.AMQP.Utils.Helpers
 import Network.AMQP.Utils.Options
@@ -15,8 +19,6 @@ import Paths_amqp_utils (version)
 import System.Environment
 import System.INotify
 import qualified System.Posix.Files as F
-import Data.Time
-import Data.Time.Clock.POSIX
 
 main :: IO ()
 main = do
@@ -49,7 +51,7 @@ main = do
              inotify
              [CloseWrite, MoveIn]
              (inputFile args)
-             (handleEvent publishOneMsg)
+             (handleEvent publishOneMsg (suffix args))
          hr $ "watching " ++ (inputFile args)
          _ <- forever $ threadDelay 1000000
          removeWatch wd
@@ -57,8 +59,8 @@ main = do
          hr $ "sending " ++ (inputFile args)
          messageFile <- BL.readFile (inputFile args)
          if (lineMode args)
-           then mapM_ publishOneMsg (BL.lines messageFile)
-           else publishOneMsg messageFile)
+           then mapM_ (publishOneMsg Nothing) (BL.lines messageFile)
+           else publishOneMsg (Just (inputFile args)) messageFile)
     (\exception -> printparam' "exception" $ show (exception :: X.SomeException))
   -- all done. wait and close.
   if (confirm args)
@@ -78,45 +80,52 @@ confirmCallback (deliveryTag, isAll, ackType) =
      (show ackType))
 
 -- | Hotfolder event handler
-handleEvent :: (BL.ByteString -> IO ()) -> Event -> IO ()
+handleEvent :: (Maybe String -> BL.ByteString -> IO ()) -> [ String ] -> Event -> IO ()
 -- just handle closewrite and movedin events
-handleEvent f (Closed False (Just x) True) = handleFile f x
-handleEvent f (MovedIn False x _) = handleFile f x
-handleEvent _ _ = return ()
+handleEvent f s (Closed False (Just x) True) = handleFile f s x
+handleEvent f s (MovedIn False x _) = handleFile f s x
+handleEvent _ _ = return ()
 
 -- | Hotfolder file handler
-handleFile :: (BL.ByteString -> IO ()) -> FilePath -> IO ()
-handleFile _ ('.':_) = return () -- ignore hidden files
-handleFile f x =
+handleFile :: (Maybe String -> BL.ByteString -> IO ()) -> [String] -> FilePath -> IO ()
+handleFile _ _ ('.':_) = return () -- ignore hidden files
+handleFile f s@(_:_) x = if any (flip isSuffixOf x) s then handleFile f [] x else return ()
+handleFile f [] x =
   X.catch
-    (hr ("sending " ++ x) >> BL.readFile x >>= f)
+    (hr ("sending " ++ x) >> BL.readFile x >>= f (Just x))
     (\exception ->
        printparam' "exception in handleFile" $
        show (exception :: X.SomeException))
 
 -- | Publish one message with our settings
-publishOneMsg' :: Channel -> Args -> BL.ByteString -> IO ()
-publishOneMsg' c a f = do
-      now <- getCurrentTime >>= return.utcTimeToPOSIXSeconds >>= return.floor
-      r <-
-        publishMsg
-          c
-          (T.pack $ currentExchange a)
-          (T.pack $ rKey a)
-          newMsg { msgBody = f
-                 , msgDeliveryMode = Just Persistent
-                 , msgTimestamp = Just now
-                 , msgID = msgid a
-                 , msgType = msgtype a
-                 , msgUserID = msguserid a
-                 , msgApplicationID = msgappid a
-                 , msgClusterID = msgclusterid a
-                 , msgContentType = msgcontenttype a
-                 , msgContentEncoding = msgcontentencoding a
-                 , msgReplyTo = msgreplyto a
-                 , msgPriority = msgprio a
-                 , msgCorrelationID = msgcorrid a
-                 , msgExpiration = msgexp a
-                 -- , msgHeaders =
-                 }
-      printparam "sent" $ fmap show r
+publishOneMsg' :: Channel -> Args -> Maybe String -> BL.ByteString -> IO ()
+publishOneMsg' c a fn f = do
+  now <- getCurrentTime >>= return . floor . utcTimeToPOSIXSeconds
+  r <-
+    publishMsg
+      c
+      (T.pack $ currentExchange a)
+      (T.pack $ rKey a)
+      newMsg
+        { msgBody = f
+        , msgDeliveryMode = Just Persistent
+        , msgTimestamp = Just now
+        , msgID = msgid a
+        , msgType = msgtype a
+        , msgUserID = userid a
+        , msgApplicationID = appid a
+        , msgClusterID = clusterid a
+        , msgContentType = contenttype a
+        , msgContentEncoding = contentencoding a
+        , msgReplyTo = replyto a
+        , msgPriority = prio a
+        , msgCorrelationID = corrid a
+        , msgExpiration = msgexp a
+        , msgHeaders = substheader (fnheader a) fn $ msgheader a
+        }
+  printparam "sent" $ fmap show r
+  where
+    substheader :: [String] -> Maybe String -> Maybe FieldTable -> Maybe FieldTable
+    substheader (s:r) (Just fname) old =
+      substheader r (Just fname) (addheader old (s ++ "=" ++ fname))
+    substheader _ _ old = old
don't click here