diff options
Diffstat (limited to 'ymir/mlmmj-expose.nix')
-rw-r--r-- | ymir/mlmmj-expose.nix | 112 |
1 files changed, 17 insertions, 95 deletions
diff --git a/ymir/mlmmj-expose.nix b/ymir/mlmmj-expose.nix index 0873b0f7..b3f7499c 100644 --- a/ymir/mlmmj-expose.nix +++ b/ymir/mlmmj-expose.nix | |||
@@ -1,105 +1,27 @@ | |||
1 | { config, pkgs, ... }: | 1 | { config, pkgs, ... }: |
2 | 2 | ||
3 | let | 3 | let |
4 | haskellEnv = pkgs.haskellPackages.ghcWithPackages (pkgs: with pkgs; [ filepath directory cryptonite bytestring uuid ]); | 4 | haskellEnv = pkgs.haskellPackages.ghcWithPackages dependencies; |
5 | dependencies = pkgs: with pkgs; [ filepath | ||
6 | directory | ||
7 | cryptonite | ||
8 | bytestring | ||
9 | uuid | ||
10 | aeson | ||
11 | aeson-pretty | ||
12 | ]; | ||
5 | mlmmj-exposed = pkgs.stdenv.mkDerivation { | 13 | mlmmj-exposed = pkgs.stdenv.mkDerivation { |
6 | name = "mlmmj-exposed"; | 14 | name = "mlmmj-expose"; |
7 | src = pkgs.writeText "mlmmj-exposed.hs" '' | 15 | src = pkgs.substituteAll { |
8 | {-# LANGUAGE ViewPatterns #-} | 16 | src = ./mlmmj-expose.hs; |
9 | 17 | inherit (pkgs) mlmmj; | |
10 | import System.IO | 18 | }; |
11 | import System.IO.Error | ||
12 | import System.FilePath | ||
13 | import System.Environment | ||
14 | import System.Exit | ||
15 | import System.Directory | ||
16 | import System.Process | ||
17 | import Text.Printf | ||
18 | |||
19 | import Data.Char | ||
20 | |||
21 | import Control.Monad | ||
22 | |||
23 | import Crypto.Hash | ||
24 | |||
25 | import qualified Data.ByteString.Lazy as LBS | ||
26 | import qualified Data.ByteString.Char8 as CBS | ||
27 | |||
28 | import qualified Data.UUID as UUID (toString) | ||
29 | import qualified Data.UUID.V4 as UUID (nextRandom) | ||
30 | |||
31 | main :: IO () | ||
32 | main = do | ||
33 | progName <- takeFileName <$> getProgName | ||
34 | case progName of | ||
35 | "mlmmj-exposed" -> do | ||
36 | args <- getArgs | ||
37 | case args of | ||
38 | [listDir, (map toLower -> extension)] -> do | ||
39 | setCurrentDirectory listDir | ||
40 | identities <- getIdentities | ||
41 | subscribers <- getSubscribers | ||
42 | let hashes = filter ((==) extension . snd) [((ident, sub), hash' (ident, sub)) | ident <- identities, sub <- subscribers] | ||
43 | case hashes of | ||
44 | [((_, recipient), _)] -> do | ||
45 | uuid <- UUID.nextRandom | ||
46 | let fName = "queue" </> "exposed" <.> uuidTrans uuid | ||
47 | uuidTrans = uuidTrans' . UUID.toString | ||
48 | where | ||
49 | uuidTrans' [] = [] | ||
50 | uuidTrans' ('-':xs) = uuidTrans' xs | ||
51 | uuidTrans' (x:xs) = x : uuidTrans' xs | ||
52 | getContents >>= writeFile fName | ||
53 | hPrintf stdout "Forwarding mail to <%s>, subscribed to %s\n" recipient (takeBaseName listDir) | ||
54 | callProcess "${pkgs.mlmmj}/bin/mlmmj-send" ["-L", listDir, "-l", "6", "-m", fName, "-T", recipient] | ||
55 | removeFile fName | ||
56 | [] -> die "Unknown extension" | ||
57 | _ -> die "Ambiguous extension" | ||
58 | _ -> hPutStrLn stderr ("Called without expected arguments (<listDirectory> <recipientExtension>)") >> exitWith (ExitFailure 2) | ||
59 | "mlmmj-expose" -> do | ||
60 | args <- getArgs | ||
61 | case args of | ||
62 | [listDir, (map toLower -> ident)] -> do | ||
63 | setCurrentDirectory listDir | ||
64 | identities <- getIdentities | ||
65 | case ident `elem` identities of | ||
66 | True -> putStrLn "Identity is already known" | ||
67 | False -> writeFile "exposed.ids" . unlines $ ident : identities | ||
68 | _ -> hPutStrLn stderr ("Called without expected arguments (<listDirectory> <senderIdentity>)") >> exitWith (ExitFailure 2) | ||
69 | "mlmmj-get-exposed" -> do | ||
70 | args <- getArgs | ||
71 | case args of | ||
72 | (dropTrailingPathSeparator -> listDir) : (map toLower -> ident) : (map (map toLower) -> recipients) -> do | ||
73 | setCurrentDirectory listDir | ||
74 | identities <- getIdentities | ||
75 | unless (ident `elem` identities) . die $ "Unknown sender: ‘" ++ ident ++ "’" | ||
76 | subscribers <- getSubscribers | ||
77 | forM_ recipients (\recipient -> do { | ||
78 | unless (recipient `elem` subscribers) . die $ "Unknown recipient: ‘" ++ recipient ++ "’"; | ||
79 | putStrLn $ takeFileName listDir ++ "+" ++ hash' (ident, recipient) ++ "@subs.lists.yggdrasil.li"; | ||
80 | }) | ||
81 | _ -> hPutStrLn stderr ("Called without expected arguments (<listDirectory> <senderIdentity> [<recipient> [...]])") >> exitWith (ExitFailure 2) | ||
82 | _ -> hPutStrLn stderr ("Called under unsupported name ‘" ++ progName ++ "’") >> exitWith (ExitFailure 2) | ||
83 | getIdentities :: IO [String] | ||
84 | getIdentities = (filter (not . null) . lines <$> readFile "exposed.ids") `catchIOError` (\e -> if isDoesNotExistError e then return [] else ioError e) | ||
85 | |||
86 | getSubscribers :: IO [String] | ||
87 | getSubscribers = map (map toLower) . concat <$> mapM (flip catchIOError (\e -> if isDoesNotExistError e then return [] else ioError e) . readDir) ["subscribers.d", "digesters.d"] | ||
88 | where | ||
89 | readDir dir = concat <$> (mapM (fmap lines . readFile) . map (dir </>) . filter (not . (`elem` [".", ".."]))=<< (getDirectoryContents dir)) | ||
90 | |||
91 | hash' :: Show a => a -> String | ||
92 | hash' = take len . map toLower . show . (hash :: CBS.ByteString -> Digest SHA256) . CBS.pack . map toLower . show | ||
93 | |||
94 | len :: Int | ||
95 | len = 32 | ||
96 | ''; | ||
97 | buildCommand = '' | 19 | buildCommand = '' |
98 | mkdir -p $out/bin | 20 | mkdir -p $out/bin |
99 | #cp $src $out/bin/.mlmmj-exposed | 21 | #cp $src $out/bin/.mlmmj-exposed |
100 | ${haskellEnv}/bin/ghc -o $out/bin/.mlmmj-exposed -odir . -hidir . $src | 22 | ${haskellEnv}/bin/ghc -o $out/bin/.mlmmj-expose -odir . -hidir . $src |
101 | for f in mlmmj-exposed mlmmj-expose mlmmj-get-exposed; do | 23 | for f in mlmmj-exposed mlmmj-expose mlmmj-get-exposed mlmmj-serve-exposed; do |
102 | ln -s .mlmmj-exposed $out/bin/$f | 24 | ln -s .mlmmj-expose $out/bin/$f |
103 | done | 25 | done |
104 | ''; | 26 | ''; |
105 | }; | 27 | }; |