1 -- This file is part of htalkat
2 -- Copyright (C) 2021 Martin Bays <mbays@sdf.org>
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.
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/.
13 import Control
.Monad
(void
)
14 import System
.Directory
(doesFileExist)
15 import System
.FilePath ((</>))
19 import System
.Posix
.Files
(ownerModes
, setFileMode
)
27 #if !(MIN_VERSION_base
(4,11,0))
31 notifyOfIncoming
:: FilePath -> Certificate
-> Petname
-> IO ()
32 notifyOfIncoming ddir cert petname
= do
34 Named name
-> putStrLn $ "Talk request from '" <> name
<> "'; accept with: htalkat a "<> shellQuotePetname
(Named name
) <>""
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
)
43 , showFingerprint
$ spkiFingerprint 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
56 setFileMode spath ownerModes
-- chmod 700
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"
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\""