bump 0.1.2.5
[htalkat.git] / Notify.hs
blob3d34feaad03ab8c9c0da1af34551395f717a2397
1 -- This file is part of htalkat
2 -- Copyright (C) 2021 Martin Bays <mbays@sdf.org>
3 --
4 -- This program is free software: you can redistribute it and/or modify
5 -- it under the terms of version 3 of the GNU General Public License as
6 -- published by the Free Software Foundation, or any later version.
7 --
8 -- You should have received a copy of the GNU General Public License
9 -- along with this program. If not, see http://www.gnu.org/licenses/.
11 module Notify where
13 import Control.Monad (void)
14 import System.Directory (doesFileExist)
15 import System.FilePath ((</>))
16 import System.Process
18 #ifndef WINDOWS
19 import System.Posix.Files (ownerModes, setFileMode)
20 #endif
22 import Certificate
23 import Fingerprint
24 import Petname
25 import Util
27 #if !(MIN_VERSION_base(4,11,0))
28 import Data.Semigroup
29 #endif
31 notifyOfIncoming :: FilePath -> Certificate -> Petname -> IO ()
32 notifyOfIncoming ddir cert petname = do
33 case petname of
34 Named name -> putStrLn $ "Talk request from '" <> name <> "'; accept with: htalkat a "<> shellQuotePetname (Named name) <>""
35 p@(Unnamed _) -> do
36 putStrLn $ "Talk request from unknown caller " <> showPetname p
37 putStrLn $ " Fingerprint: " <> showFingerprint (spkiFingerprint cert)
38 putStrLn $ " Public name: " <> certCN cert
39 putStrLn $ "Accept with: htalkat a " <> shellQuotePetname p
40 createNotifyScriptIfNecessary ddir
41 void $ rawSystem (notifyScriptPath ddir)
42 [ showPetname petname
43 , showFingerprint $ spkiFingerprint cert
44 , certCN cert
47 notifyScriptPath :: FilePath -> FilePath
48 notifyScriptPath = (</> "notify.sh")
50 createNotifyScriptIfNecessary :: FilePath -> IO ()
51 createNotifyScriptIfNecessary ddir =
52 let spath = notifyScriptPath ddir
53 in doesFileExist spath >>! do
54 writeFile spath defaultNotifyScript
55 #ifndef WINDOWS
56 setFileMode spath ownerModes -- chmod 700
57 #endif
59 defaultNotifyScript :: String
60 defaultNotifyScript = unlines
61 [ "#!/usr/bin/env bash"
62 , "# This script is called when someone connects to the talkat server."
63 , "# The following positional arguments are given to this script:"
64 , "# $1: assigned name of caller, or +N for an unknown caller"
65 , "# $2: fingerprint of caller's public key"
66 , "# $3: \"public name\" set in CN field of caller's certificate"
67 , ""
68 , "# Example:"
69 , "#announce=\"$(echo -n \"Talkat request from $1\"; \\"
70 , "# [[ \"$1\" =~ ^\\+ ]] && echo -n \" $2 (\\\"$3\\\")\")\""
71 , "#write $USER <<<\"$announce\""
72 , "#mail -s \"$announce\" $USER <<<\"Answer with htalkat a '$1'\""
73 , "#notify-send \"$announce\""