nixos-configuration/hosts/aluminium/services/asterisk/default.nix
2023-11-09 00:50:15 +00:00

273 lines
8.5 KiB
Nix

args@{ config, lib, pkgs, custom-utils, ... }:
let
ports = import ../../ports.nix args;
secretConfigFiles = [
"ari"
"pjsip"
"voicemail"
];
rtp = {
start = builtins.elemAt ports.asterisk-rtp.udp.range 0;
end = builtins.elemAt ports.asterisk-rtp.udp.range 1;
};
voicemail-sounds = pkgs.callPackage ./voicemail-sounds { };
in {
systemd.services.asterisk-voicemail-sounds = {
wantedBy = ["asterisk.service"];
after = ["asterisk.service"];
script = ''
ln -sfn \
${voicemail-sounds}/unavail.wav \
/var/spool/asterisk/voicemail/lechner/876/unavail.wav
'';
restartTriggers = [voicemail-sounds];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
};
};
services.asterisk = {
enable = true;
confFiles = {
"extensions.conf" = ''
[doorbell]
exten = s,1,Set(__DYNAMIC_FEATURES=doorOpen)
same = n,Dial(PJSIP/10&PJSIP/11,${toString config.services.myintercom-doorbell.dialTime})
same = n,Hangup()
[door-open]
exten = s,1,Verbose(0, "opening the door")
same = n,System("${pkgs.myintercom-doorbell}/bin/myintercom-doorbell-open-door")
same = n,Hangup()
[sipgate-in]
exten = _499846876,1,Noop(Processing an incoming call)
same = n,Dial(PJSIP/10&PJSIP/11,25,tT)
same = n,VoiceMail(876@lechner,uS)
same = n,Hangup()
exten => _4998469779781,1,Verbose(3,Incoming fax)
; folder where your incoming faxes will initially be stored
same => n,Set(FAXDEST=/tmp)
exten = _4998469779782,1,Noop(Test call)
same = n,Dial(PJSIP/10)
; put a timestamp on this call so the resulting file is unique
same => n,Set(tempfax=''${STRFTIME(,,%C%y%m%d%H%M)})
same => n,ReceiveFax(''${FAXDEST}/''${tempfax}.tif)
same => n,Verbose(3,- Fax receipt completed with status: ''${FAXSTATUS})
same => n,System(echo | s-nail -a ''${FAXDEST}/''${tempfax} -s "Asterisk Fax" mail@jalr.de)
exten = _499846977892,1,Noop(Processing an incoming call)
same = n,Dial(PJSIP/12,30,tT)
;same = n,VoiceMail(977892@pauline,u)
same = n,Hangup()
exten = _499846977891,1,Noop(Processing an incoming call)
same = n,VoiceMail(876@lechner,uS)
;same = n,Dial(PJSIP/13&PJSIP/16,30,tT)
;same = n,VoiceMail(977891@jakob,u)
;same = n,Hangup()
[dect]
exten = 100,1,Answer()
same = n,Wait(1)
same = n,Playback(hello-world)
same = n,Hangup()
;exten = 10,1,Dial(PJSIP/10,10)
;same = n,VoiceMail(876@lechner,u)
exten = _1X,1,Dial(PJSIP/''${EXTEN},30)
same = n,Hangup()
exten = 50,1,Dial(PJSIP/10&PJSIP/11,30,tT)
same = n,Hangup()
exten = 99,1,Answer()
same = n,VoiceMailMain(876@lechner)
exten = 98,1,Answer()
same = n,VoiceMailMain(977892@pauline)
exten = _ZX.,1,Noop(Ortsgespraech)
same = n,Dial(PJSIP/09846''${EXTEN}@sipgate,300)
same = n,Hangup()
exten = _0ZXXXXX.,1,Noop(Deutsches Festnetz)
same = n,Dial(PJSIP/''${EXTEN}@sipgate,300)
same = n,Hangup()
exten = _00XXXXX.,1,Noop(Internationales Gespraech)
same = n,Dial(PJSIP/''${EXTEN}@sipgate,300)
same = n,Hangup()
[voicemail-callback]
exten = s,1,HasNewVoicemail(876@lechner)
exten = s,2,Hangup
exten = s,102,Dial(PJSIP/10&PJSIP/11,15)
exten = s,n,VoiceMailMain(876@lechner)
exten = s,n,Hangup
[fax-tx]
exten => s,1,NoOp(**** SENDING FAX ****)
;exten => s,n,Set(FAXFILE=output.tif)
exten => s,n,NoOp(**** SETTING FAXOPT ****)
exten => s,n,Set(FAXOPT(ecm)=yes)
;exten => s,n,Set(FAXOPT(localstationid)=098469779780)
exten => s,n,Set(FAXOPT(maxrate)=14400)
exten => s,n,Set(FAXOPT(minrate)=2400)
exten => s,n,NoOp(**** SENDING FAX : ''${FAXFILE} ****)
exten => s,n,SendFAX(/var/lib/asterisk/fax-outgoing/''${FAXFILE},d)
;exten => send,n,Wait(6)
;exten => send,n,Set(GLOBAL(FAXCOUNT)=''$[ ''${GLOBAL(FAXCOUNT)} + 1 ])
;exten => send,n,Set(FAXCOUNT=''${GLOBAL(FAXCOUNT)})
;exten => send,n,Set(FAXFILE=dw-faxout.tif)
; Set FAXOPTs
;exten => send,n,NoOp(**** SETTING FAXOPT ****)
;exten => send,n,Set(FAXOPT(ecm)=yes)
;exten => send,n,Set(FAXOPT(headerinfo)=Fax from ''${GLOBAL(LASTFAXCALLERNAME)} at ''${GLOBAL(LASTFAXCALLERNUM)} was received.)
;exten => send,n,Set(FAXOPT(maxrate)=14400)
; Send the fax
exten => send,n,SendFAX(/home/jalr/fax.tif,d)
'';
"features.conf" = ''
[applicationmap]
doorOpen => #9,peer,Gosub,"door-open,s,1"
'';
"http.conf" = ''
[general]
enabled=yes
bindaddr=127.0.0.1
; Port to bind to for HTTP sessions (default is 8088)
;bindport=8088
tlsdisablev1=yes
tlsdisablev11=yes
tlsdisablev12=yes
tlsservercipherorder=yes
'';
"rtp.conf" = ''
[general]
rtpstart=${toString rtp.start}
rtpend=${toString rtp.end}
'';
"dnsmgr.conf" = ''
[general]
enable=yes
refreshinterval=300
'';
};
useTheseDefaultConfFiles = [ ];
};
sops.secrets = (lib.listToAttrs (map
(name: lib.nameValuePair "asterisk-${name}" {
sopsFile = ../../secrets.yaml;
owner = config.users.users.asterisk.name;
})
secretConfigFiles));
environment.etc = lib.mapAttrs'
(name: _: lib.nameValuePair
"asterisk/${name}.conf"
{ source = config.sops.secrets."asterisk-${name}".path; })
(lib.listToAttrs (map (name: lib.nameValuePair name { }) secretConfigFiles));
networking.firewall = {
allowedUDPPortRanges = [
{
from = rtp.start;
to = rtp.end;
}
];
};
systemd.services."asterisk-reload-endpoint@" = {
description = "Check if asterisk endpoint is identified and reload it when it is not.";
serviceConfig = {
Type = "oneshot";
};
environment = {
ENDPOINT = "%I";
};
script = ''
export PATH=${pkgs.lib.makeBinPath [pkgs.asterisk pkgs.gnused pkgs.gnugrep]}
if ! asterisk -r -x "pjsip show endpoint $ENDPOINT" | sed -n '/^===/,/^\s*ParameterName/{//!p}' | grep -q 'Identify:'; then
asterisk -r -x "module reload res_pjsip_endpoint_identifier_ip.so"
fi
'';
};
systemd.timers.asterisk-reload-endpoint = {
description = "Check if asterisk endpoint is identified and reload it when it is not.";
after = [ "asterisk.service" ];
wantedBy = [ "timers.target" ];
timerConfig = {
Persistent = true;
OnCalendar = "*-*-* *:*:00";
Unit = "asterisk-reload-endpoint@sipgate.service";
};
};
systemd.services."asterisk-voicemail-call@" = {
description = "Check if voicemail exists and place a call to the voicemail application.";
serviceConfig = {
Type = "oneshot";
};
scriptArgs = "%I";
script = ''
export PATH=${pkgs.lib.makeBinPath [pkgs.asterisk pkgs.coreutils pkgs.findutils]}
number="$(echo "$1" | cut -d ':' -f 1)"
user="$(echo "$1" | cut -d ':' -f 2)"
channel="PJSIP/$(echo "$1" | cut -d ':' -f 3)"
if ! find "/var/spool/asterisk/voicemail/$user/$number/INBOX/" -mindepth 1 -maxdepth 1 | read; then
exit
fi
callfile="$(mktemp -p /tmp XXXXXXXXXX.call)"
chmod 644 "$callfile"
cat > "$callfile" << EOF
Channel: $channel
WaitTime: 15
Application: VoiceMailMain
Data: $number@$user
CallerID: Voicemail
EOF
mv "$callfile" /var/spool/asterisk/outgoing/
'';
};
systemd.timers.asterisk-voicemail-call-10 = {
description = "Check if voicemail exists and place a call to the voicemail application.";
after = [ "asterisk.service" ];
wantedBy = [ "timers.target" ];
timerConfig = {
Persistent = true;
OnCalendar = "*-*-* 07..22:00,20,40:00";
Unit = "asterisk-voicemail-call@876:lechner:10.service";
};
};
systemd.timers.asterisk-voicemail-call-11 = {
description = "Check if voicemail exists and place a call to the voicemail application.";
after = [ "asterisk.service" ];
wantedBy = [ "timers.target" ];
timerConfig = {
Persistent = true;
OnCalendar = "*-*-* 07..22:00,10,30:50";
Unit = "asterisk-voicemail-call@876:lechner:11.service";
};
};
#voicemailCallScript
}