📄 ans
字号:
#!/bin/sh#-------------------------------------------------------------------------# ans A bash script to simulate an answering machine.# Discriminate voice, fax and data calls. Password-protected# remote mode to retrieve and delete messages. Remote change# of greeting and password.## Version: 0.6.1 Jul 14 1999## Author: Niccolo Rigacci <fd131@cleveland.freenet.edu>## Copyright (C) 1996-1999 Niccolo Rigacci## This program is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 2 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with this program; if not, write to the Free Software# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.#-------------------------------------------------------------------------#-------------------------------------------------------------------------# USER CONFIGURABLE PARAMETERS#-------------------------------------------------------------------------WAITRINGS=4# Umask assigned to recorded files.UMASK=0117# Discard message if shorter than SHORTMSG bytes. My modem# with default settings records approx. 3.5 kb per second,# but better compression methods (USR/GSM) can do less.SHORTMSG=4000# Max message length (seconds).TIMEOUT=120# Devices, directories and filenames#------------------------------------------ TTYS="ttyS0" DEVICE="/dev/${TTYS}" NULL="/dev/null" LIBDIR="/usr/local/lib/ans" FAXDIR="/var/spool/fax"VOICEDIR="/var/spool/voice" TMPDIR="/tmp" LOCK="/var/lock/LCK..${TTYS}" PASSWD="${LIBDIR}/passwd"STOPFILE="/var/run/ans.${TTYS}.stop" DOITNOW="/var/run/ans.${TTYS}.now" TMP1=${TMPDIR}/${$}.1.tmp TMP2=${TMPDIR}/${$}.2.tmp EXT=msg# Auxiliary binaries#------------------------------------------ VMCP="/usr/bin/vmcp -g -d${TTYS}" EFAX="/usr/bin/efax"# Standard auxiliary binaries#------------------------------------------ AGETTY="/sbin/agetty" CAT="/bin/cat" CP="/bin/cp" CUT="/bin/cut" DATE="/bin/date" ECHO="/bin/echo" DIFF="/usr/bin/diff" FIND="/usr/bin/find" KILL="/bin/kill" LS="/bin/ls" MV="/bin/mv" RM="/bin/rm" SED="/usr/bin/sed" SORT="/usr/bin/sort" TOUCH="/bin/touch" WC="/usr/bin/wc"# Modem commands#------------------------------------------# Reset the modem to factory defaults.AT_RESET="AT&F1"# Set some values (uncomment if needed):# - Modem sets CD line on remote carrier# - Modem hangs up and return to command mode on DTR switch# - Enable RTS/CTS hardware flow control# AT_LINE="AT&C1&D2&K3"# Initialize voice operations:# CLS Select voice mode# VBS Bits per sample# VSP Silence detection period (n*100 ms)# VSD Enable silence deletion# VLS Voice line select: generally 0 is telephone/handset, 4 with speaker on# S30 Timeout if off-hook and no data, or if BDR <> 0 and no data# BDR Slect baud rate (n*2400)# NOTE: Command line generally must be shorter than 60 chars!AT_VOICE="AT#CLS=8#VBS=4#VSP=20#VSD=1#VLS=4S30=60#BDR=16"# Select device for playback: generally 1 is handset, 2 is speaker.AT_PLAYDEV="AT#VLS=2"# Enable silence detection (1 quiet, 2 midrange, 3 noisy lines).AT_SILENCE_ENA="AT#VSS=2"# Disable silence detection.AT_SILENCE_DIS="AT#VSS=0"# Start voice transmit.AT_VTX="AT#VTX"# Start voice receive.AT_VRX="AT#VRX"# Play a beep.AT_BEEP="AT#VBT=3#VTS=#"# Select data mode.AT_DATA="AT#CLS=0"# Select fax mode.AT_FAX="AT#CLS=2"# Answer a call.AT_A="ATA"# Hang-up.AT_H="ATH"# Builtin messages files#------------------------------------------ ASK_PASSWD=${LIBDIR}/ask_passwd.$EXT CH_GREETING=${LIBDIR}/ch_greeting.$EXT GET_MSGS_MNU=${LIBDIR}/get_msgs_mnu.$EXT GREETING=${LIBDIR}/greeting.$EXT MSG_DELETED=${LIBDIR}/msg_deleted.$EXT PASSWD_CH=${LIBDIR}/passwd_ch.$EXTPASSWD_NOT_CH=${LIBDIR}/passwd_not_ch.$EXT REMOTE_MENU=${LIBDIR}/remote_menu.$EXT WRONG_PASSWD=${LIBDIR}/wrong_passwd.$EXT#-------------------------------------------------------------------------# END OF USER CONFIGURABLE PARAMETERS#-------------------------------------------------------------------------#-------------------------------------------------------------------------# FUNCTIONS#-------------------------------------------------------------------------#-------------------------------------------------------------------------# log(message)#-------------------------------------------------------------------------log() { $ECHO "$($DATE +%m-%d\ %H:%M) ${0}: ${1}" }#-------------------------------------------------------------------------# reset_modem(void)#-------------------------------------------------------------------------reset_modem() { $VMCP -z38400 -t8 -c"$AT_RESET" -wOK if [ $? -ne 0 ]; then log "Can't reset $DEVICE line or modem" exit fi if [ "$AT_LINE" != "" ]; then $VMCP -c"$AT_LINE" -wOK if [ $? -ne 0 ]; then log "Modem is not responding" exit fi fi }#-------------------------------------------------------------------------# play_beep(void)#-------------------------------------------------------------------------play_beep() { $VMCP -c"$AT_BEEP" -wOK }#-------------------------------------------------------------------------# wait_rings(rings_number)# Return after receiving rings_number RINGs and if $STOPFILE not found.# Return also if $DOITNOW file is created ("ans now" command was ran).#-------------------------------------------------------------------------wait_rings() { local N local WAIT WAIT=true while [ $WAIT = true ]; do if [ -f $STOPFILE ]; then log "Found ${STOPFILE}: waiting" while [ -f $STOPFILE ]; do sleep 10; done log "Stopfile removed" fi # Reset the modem here! If left in voice mode no RING reported. reset_modem $VMCP -l$LOCK -wRING -t0 if [ -f $DOITNOW ]; then WAIT=false elif [ ! -f $STOPFILE ]; then N=1 WAIT=false while [ $N -lt $1 -a $WAIT = false ]; do N=$[$N + 1] $VMCP -wRING -t5 if [ $? -ne 0 ]; then WAIT=true fi if [ -f $DOITNOW ]; then WAIT=false N=$1 fi done fi done }#-------------------------------------------------------------------------# play_message(filename, dle_stop_string) #-------------------------------------------------------------------------play_message() { local N $VMCP -c"$AT_VTX" -wCONNECT $VMCP -e -q -t$TIMEOUT -i$1 -x$2 N=$? if [ $N -eq 0 ]; then # Send <DLE><ETX>: terminate sending message gracefully. $VMCP -c"\c\020\003" -wVCON else # Send <DLE><CAN>: abort sending message (escaped char received). $VMCP -c"\c\020\030" -wVCON fi return $N }#-------------------------------------------------------------------------# record_message(filename, dle_stop_string) #-------------------------------------------------------------------------record_message() { local N play_beep # Record a message $VMCP -c"$AT_VRX" -W"\cCONNECT\r\n" -e -t$TIMEOUT -o$1 -x$2 N=$? $VMCP -c"\c\n" -wVCON play_beep return $N }#-------------------------------------------------------------------------# voice_call(filename)# Receive a voice call and store it in ${VOICEDIR}/filename.${EXT}#-------------------------------------------------------------------------voice_call() { local FNAME local N FNAME=${VOICEDIR}/${1}.${EXT} record_message $FNAME "bqd#sthc2e3" N=$? # Discard message if too short. if [ $($WC --bytes < $FNAME) -lt $SHORTMSG ]; then log "Short message discarded" $RM -f $FNAME fi case $N in 5|6|7) # Silence, off-hook or on-hook detected. log "Message discarded" $RM -f $FNAME ;; 8|9) log "After voice receiving a Fax" fax_call $1 ;; 10|11) log "After voice receiving a Data call" data_call ;; esac }#-------------------------------------------------------------------------# get_passwd(filename)# Ask for a password via DTMF (terminated with a "#") and store it in $1.# Return 1 if password entered correctly.#-------------------------------------------------------------------------get_passwd() { local N play_message $ASK_PASSWD "#" play_beep # Disable silence detection and listen for the password. $VMCP -c"$AT_SILENCE_DIS" -wOK $VMCP -c"$AT_VRX" -W"\cCONNECT\r\n" -e -t20 -s$1 -x"#bd" N=$? # Stop listening and enable silence detection. $VMCP -c"\c\n" -wVCON $VMCP -c"$AT_SILENCE_ENA" -wOK # Append a new-line to the file. $ECHO >> $1 return $N }#-------------------------------------------------------------------------# change_password(void)#-------------------------------------------------------------------------change_password() { get_passwd $TMP1 if [ $? -eq 1 ]; then get_passwd $TMP2 if [ $? -eq 1 ]; then $DIFF -q $TMP1 $TMP2 > $NULL if [ $? -ne 0 ]; then log "Password not changed" play_message $PASSWD_NOT_CH "#" else $MV -f $TMP1 $PASSWD log "Password changed"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -