3 -- generic AMQP publisher
4 import Control.Concurrent
5 import qualified Control.Exception as X
6 import Control.Monad (forever)
7 import qualified Data.ByteString.Lazy.Char8 as BL
8 #if MIN_VERSION_hinotify(0,3,10)
9 import qualified Data.ByteString.Char8 as BS
11 import Data.List (isSuffixOf)
13 import qualified Data.Text as T
15 import Data.Time.Clock.POSIX
16 import Data.Version (showVersion)
17 import Data.Word (Word64)
20 import Network.AMQP.Types
21 import Network.AMQP.Utils.Connection
22 import Network.AMQP.Utils.Helpers
23 import Network.AMQP.Utils.Options
24 import Paths_amqp_utils (version)
25 import System.Directory
26 import System.Environment
29 import qualified System.Posix.Files as F
35 args <- getArgs >>= parseargs 'a'
36 printparam "client version" ["amqp-utils", showVersion version]
37 printparam "routing key" $ rKey args
38 printparam "exchange" $ currentExchange args
40 if inputFile args == "-"
42 else F.getFileStatus (inputFile args) >>= return . F.isDirectory
44 then printparam "hotfolder" $ inputFile args
52 (conn, chan) <- connect args
53 addChannelExceptionHandler chan (X.throwTo tid)
54 printparam "confirm mode" $ confirm args
57 confirmSelect chan False
58 addConfirmationListener chan confirmCallback
60 let publishOneMsg = publishOneMsg' chan args
65 then getDirectoryContents (inputFile args) >>= mapM_ (\fn -> handleFile publishOneMsg (suffix args) ((inputFile args) ++ "/" ++ fn))
67 inotify <- initINotify
72 #if MIN_VERSION_hinotify(0,3,10)
73 (BS.pack (inputFile args))
77 (handleEvent publishOneMsg (suffix args) (inputFile args))
78 hr $ "BEGIN watching " ++ (inputFile args)
79 _ <- forever $ threadDelay 1000000
81 hr $ "END watching " ++ (inputFile args)
85 if inputFile args == "-"
87 else BL.readFile (inputFile args)
89 then mapM_ (publishOneMsg Nothing) (BL.lines messageFile)
90 else publishOneMsg (Just (inputFile args)) messageFile
93 -- all done. wait and close.
95 then waitForConfirms chan >>= printparam "confirmed"
97 X.catch (closeConnection conn) exceptionHandler
99 -- | A handler for clean exit
100 exceptionHandler :: AMQPException -> IO ()
101 exceptionHandler (ChannelClosedException Normal txt) =
102 printparam "exit" txt >> exitWith ExitSuccess
103 exceptionHandler (ConnectionClosedException Normal txt) =
104 printparam "exit" txt >> exitWith ExitSuccess
105 exceptionHandler x = printparam "exception" x >> exitWith (ExitFailure 1)
107 -- | The handler for publisher confirms
108 confirmCallback :: (Word64, Bool, AckType) -> IO ()
109 confirmCallback (deliveryTag, isAll, ackType) =
119 -- | Hotfolder event handler
121 (Maybe String -> BL.ByteString -> IO ())
126 -- just handle closewrite and movedin events
127 #if MIN_VERSION_hinotify(0,3,10)
128 handleEvent func suffixes path (Closed False (Just fileName) True) =
129 handleFile func suffixes (path ++ "/" ++ (BS.unpack fileName))
130 handleEvent func suffixes path (MovedIn False fileName _) =
131 handleFile func suffixes (path ++ "/" ++ (BS.unpack fileName))
133 handleEvent func suffixes path (Closed False (Just fileName) True) = handleFile func suffixes (path ++ "/" ++ fileName)
134 handleEvent func suffixes path (MovedIn False fileName _) = handleFile func suffixes (path ++ "/" ++ fileName)
136 handleEvent _ _ _ _ = return ()
138 -- | Hotfolder file handler
140 (Maybe String -> BL.ByteString -> IO ()) -> [String] -> FilePath -> IO ()
141 handleFile _ _ ('.':_) = return () -- ignore hidden files
142 handleFile func suffixes@(_:_) fileName =
143 if any (flip isSuffixOf fileName) suffixes
144 then handleFile func [] fileName
146 handleFile func [] fileName =
148 (BL.readFile fileName >>= func (Just fileName))
149 (\e -> printparam "exception in handleFile" (e :: X.SomeException))
151 -- | Publish one message with our settings
152 publishOneMsg' :: Channel -> Args -> Maybe FilePath -> BL.ByteString -> IO ()
153 publishOneMsg' chan a fn content = do
154 printparam "sending" fn
155 (mtype, mencoding) <-
156 if (magic a) && isJust fn
158 m <- magicOpen [MagicMimeType]
160 t <- magicFile m (fromJust fn)
161 magicSetFlags m [MagicMimeEncoding]
162 e <- magicFile m (fromJust fn)
163 return (Just (T.pack t), Just (T.pack e))
164 else return ((contenttype a), (contentencoding a))
165 now <- getCurrentTime >>= return . floor . utcTimeToPOSIXSeconds
168 (T.pack $ currentExchange a)
172 , msgDeliveryMode = persistent a
173 , msgTimestamp = Just now
175 , msgType = msgtype a
176 , msgUserID = userid a
177 , msgApplicationID = appid a
178 , msgClusterID = clusterid a
179 , msgContentType = mtype
180 , msgContentEncoding = mencoding
181 , msgReplyTo = replyto a
182 , msgPriority = prio a
183 , msgCorrelationID = corrid a
184 , msgExpiration = msgexp a
185 , msgHeaders = substheader (fnheader a) fn $ msgheader a
188 removeSentFileIfRequested (removeSentFile a) fn
191 [String] -> Maybe String -> Maybe FieldTable -> Maybe FieldTable
192 substheader (s:r) (Just fname) old =
193 substheader r (Just fname) (addheader old (s ++ "=" ++ fname))
194 substheader _ _ old = old
195 removeSentFileIfRequested False _ = return ()
196 removeSentFileIfRequested True Nothing = return ()
197 removeSentFileIfRequested True (Just fname) = printparam "removing" fname >> removeFile fname