1 -- generic AMQP publisher
2 import Control.Concurrent (threadDelay)
3 import qualified Control.Exception as X
4 import Control.Monad (forever)
5 import qualified Data.ByteString.Lazy.Char8 as BL
6 import Data.List (isSuffixOf)
8 import qualified Data.Text as T
10 import Data.Time.Clock.POSIX
11 import Data.Version (showVersion)
12 import Data.Word (Word64)
15 import Network.AMQP.Types
16 import Network.AMQP.Utils.Connection
17 import Network.AMQP.Utils.Helpers
18 import Network.AMQP.Utils.Options
19 import Paths_amqp_utils (version)
20 import System.Environment
22 import qualified System.Posix.Files as F
27 args <- getArgs >>= parseargs "agitprop"
28 printparam' "client version" $ "amqp-utils " ++ (showVersion version)
29 printparam' "routing key" $ rKey args
30 isDir <- F.getFileStatus (inputFile args) >>= return . F.isDirectory
32 then printparam' "hotfolder" $ inputFile args
33 else printparam' "input file" $
36 then " (line-by-line)"
38 (conn, chan) <- connect args
39 printparam' "confirm mode" $ show $ confirm args
42 confirmSelect chan False
43 addConfirmationListener chan confirmCallback
45 let publishOneMsg = publishOneMsg' chan args
49 inotify <- initINotify
55 (handleEvent publishOneMsg (suffix args) (inputFile args))
56 hr $ "watching " ++ (inputFile args)
57 _ <- forever $ threadDelay 1000000
60 hr $ "sending " ++ (inputFile args)
61 messageFile <- BL.readFile (inputFile args)
63 then mapM_ (publishOneMsg Nothing) (BL.lines messageFile)
64 else publishOneMsg (Just (inputFile args)) messageFile)
65 (\exception -> printparam' "exception" $ show (exception :: X.SomeException))
66 -- all done. wait and close.
68 then waitForConfirms chan >>= return . show >> return ()
72 -- | The handler for publisher confirms
73 confirmCallback :: (Word64, Bool, AckType) -> IO ()
74 confirmCallback (deliveryTag, isAll, ackType) =
77 ((show deliveryTag) ++
83 -- | Hotfolder event handler
85 (Maybe String -> BL.ByteString -> IO ())
90 -- just handle closewrite and movedin events
91 handleEvent f s p (Closed False (Just x) True) = handleFile f s (p ++ "/" ++ x)
92 handleEvent f s p (MovedIn False x _) = handleFile f s (p ++ "/" ++ x)
93 handleEvent _ _ _ _ = return ()
95 -- | Hotfolder file handler
97 (Maybe String -> BL.ByteString -> IO ()) -> [String] -> FilePath -> IO ()
98 handleFile _ _ ('.':_) = return () -- ignore hidden files
99 handleFile f s@(_:_) x =
100 if any (flip isSuffixOf x) s
101 then handleFile f [] x
105 (hr ("sending " ++ x) >> BL.readFile x >>= f (Just x))
107 printparam' "exception in handleFile" $
108 show (exception :: X.SomeException))
110 -- | Publish one message with our settings
111 publishOneMsg' :: Channel -> Args -> Maybe String -> BL.ByteString -> IO ()
112 publishOneMsg' c a fn f = do
113 (mtype, mencoding) <-
114 if (magic a) && isJust fn
116 m <- magicOpen [MagicMimeType]
118 t <- magicFile m (fromJust fn)
119 magicSetFlags m [MagicMimeEncoding]
120 e <- magicFile m (fromJust fn)
121 return (Just (T.pack t), Just (T.pack e))
122 else return ((contenttype a), (contentencoding a))
123 now <- getCurrentTime >>= return . floor . utcTimeToPOSIXSeconds
127 (T.pack $ currentExchange a)
131 , msgDeliveryMode = persistent a
132 , msgTimestamp = Just now
134 , msgType = msgtype a
135 , msgUserID = userid a
136 , msgApplicationID = appid a
137 , msgClusterID = clusterid a
138 , msgContentType = mtype
139 , msgContentEncoding = mencoding
140 , msgReplyTo = replyto a
141 , msgPriority = prio a
142 , msgCorrelationID = corrid a
143 , msgExpiration = msgexp a
144 , msgHeaders = substheader (fnheader a) fn $ msgheader a
146 printparam "sent" $ fmap show r
149 [String] -> Maybe String -> Maybe FieldTable -> Maybe FieldTable
150 substheader (s:r) (Just fname) old =
151 substheader r (Just fname) (addheader old (s ++ "=" ++ fname))
152 substheader _ _ old = old