File: dtmf_alpha.sh

package info (click to toggle)
mgetty 1.2.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,872 kB
  • sloc: ansic: 42,728; sh: 6,487; perl: 6,262; makefile: 1,457; tcl: 756; lisp: 283
file content (526 lines) | stat: -rw-r--r-- 13,719 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
#!/usr/bin/vm shell 
# Robert Jrdens <rjo@gmx.de>
#
# This is the dtmf script. It is called by vgetty when a dtmf code was send by
# the user.
#
# $1 - received DTMF code
#
# $2 - name of the recorded voice file or if "-cons_mode", switches on console
# mode so you can enter the dtmf-codes on your console and will hear all sound
# going to your soundcard! (you also have to toogle the magic line at the
# beginning!)
#

if [ "1$2" = "1-cons_mode" ]; then
	CONS_MODE="yes"
else
	CONS_MODE="no"
fi

#
BASENAME=`basename $0`
DTMF="$1"
ALREADY_REC="$2"
# Some dir's
VOICE_DIR=/var/spool/voice
MSG_DIR=$VOICE_DIR/messages
INC_DIR=$VOICE_DIR/incoming
# Some files (scripts may have to be written)
CODE=`cat $VOICE_DIR/.code`
FLAG=$VOICE_DIR/.flag
TIMESTAMP=$VOICE_DIR/.timestamp
CONNECT_SCRIPT=$VOICE_DIR/ppp.sh
FAX_ACTIVATE_SCRIPT=$VOICE_DIR/fax_activate.sh
# Sound-Files
GET_CODE=$MSG_DIR/get-code.rmd
INCORRECT=$MSG_DIR/incorrect.rmd
GOODBYE=$MSG_DIR/goodbye.rmd
NO_NEW_MESSAGES=$MSG_DIR/no_new_messages.rmd
GIMME_COMM=$MSG_DIR/gimme_comm.rmd
OKAY_DRIN=$MSG_DIR/okay_drin.rmd
BYE=$MSG_DIR/bye.rmd
# The TTY's where to toogle the Scroll-LED
LEDTTY=/dev/tty[1-8]
# Tries Code
MAXTRIES=3
# Timeout beetween two equal numbers to be recognized as two single. Under
# TIMEOUT: increment (see dtmf_alph)
TIMEOUT=2
# Type of your Modem
MODEM_TYPE=Rockwell
# Compression
COMPRESSION=4
# Sample Rate needed
SPEED=7200


### BEGIN FUNCTIONS

# Define the function to check for an answer and if it is not the desired it
# will quit!
answer () {
    ANSWER=`receive`
    if [ "$ANSWER" != "$1" ]; then
	logger -t "${BASENAME}[$$]" "Got '$ANSWER', expected '$1'. Harakiri"
       	kill -KILL $$
    fi
}

# Define the function to receive an answer from the voice library
receive () {
  if [ $CONS_MODE = "no" ]; then
     read -r INPUT <&$VOICE_INPUT
  else
     read -r -p "receive: " INPUT
  fi
  echo "$INPUT"
}

# Define the function to send a command to the voice library
send () {
  if [ $CONS_MODE = "no" ]; then
     echo $1 >&$VOICE_OUTPUT
     kill -PIPE $VOICE_PID
  else
     echo "send: $1"
  fi
}

# Define the function send a beep
beep () {
  if [ $CONS_MODE = "no" ]; then
     send "BEEP $1 $2"
     answer "BEEPING"
     answer "READY"
  else
     echo "beep: $1 $2"
  fi
}

# Define the function to play a file
play () {
   if [ $CONS_MODE = "no" ]; then
     send "PLAY $1"
     answer "PLAYING"
     answer "READY"
   else
     rmdtopvf $1 | pvfspeed -s 8000 | pvftobasic > /dev/audio
   fi
     logger -t "${BASENAME}[$$]" "Played $1"
}

# Define the function to play the new messages
messages () {
     if [ ! -f $TIMESTAMP ]; then
          MSGS=`find $INC_DIR/ -type f -name 'v*.rmd' -print`
     else
          MSGS=`find $INC_DIR/ -type f -name 'v*.rmd' -newer $TIMESTAMP -print`
          if [ -z "$MSGS" ]; then
               BASENAME=`basename $TIMESTAMP`
               NEWSTAMP=`find $VOICE_DIR -name $BASENAME -cmin -10 -print`
               if [ "$NEWSTAMP" = "$TIMESTAMP" ]; then
                    MSGS=`find $INC_DIR -type f -name 'v*.rmd' -print`
               fi
          fi
     fi
     touch $TIMESTAMP-n
     if [ -x $VOICE_DIR/speakdate.sh ]; then
          TIME=yes
     else
          TIME=no
     fi
     TMPDIR=${TMPDIR-/tmp}/dtmf.$$
     mkdir -m 0700 $TMPDIR || exit 1
     trap "rm -rf $TMPDIR" 0 1 2 3 7 13 15
     TMP=${TMPDIR}/time.rmd.$$
     LOCK=${TMPDIR}/time-lock.$$
     for i in $MSGS
     do
          if [ $TIME = yes ]; then
               (touch $LOCK ;\
               $VOICE_DIR/speakdate.sh $i $SPEED $MODEM_TYPE $COMPRESSION >$TMP ;\
               rm -f $LOCK) &
          fi
          beep 1320 100
          play $i
          beep 1320 100
          if [ $TIME = yes ]; then
               while [ -f $LOCK ]
               do
                    sleep 1
               done
               play $TMP
               rm -f $TMP
          fi
     done
     if [ -z "$MSGS" ]; then
          play "$NO_NEW_MESSAGES"
     fi
     beep 1320 1000
     rm -f $FLAG $TIMESTAMP
     mv $TIMESTAMP-n $TIMESTAMP
#     for tty in $LEDTTY; do
#         setleds -scroll < $tty
#     done
}

# Define the function to read one dtmf code string terminated by #
getcode () {
  if [ $CONS_MODE = "no" ]; then
     RECEIVED=""
     send "ENABLE EVENTS"
     answer "READY"
     send "WAIT 30"
     answer "WAITING"
     ANSWER=""
     while [ "$ANSWER" != "READY" ]
     do
          ANSWER=`receive`
          if [ "$ANSWER" = "RECEIVED_DTMF" ]; then
               ANSWER=`receive`
               if [ "$ANSWER" = "*" ]; then
                    RECEIVED=""
               else
                    case $ANSWER in
                    "#")
                         send "STOP"
                    ;;
                    0|1|2|3|4|5|6|7|8|9)
                         RECEIVED=$RECEIVED$ANSWER
                    ;;
                    *)
                         logger -t "${BASENAME}[$$]" "Ignoring DTMF $ANSWER"
                    ;;
                    esac
               fi
          else
               if [ "$ANSWER" = "SILENCE_DETECTED" ]; then
                    send "STOP"
               else
                    if [ "$ANSWER" != "READY" ]; then
                         logger -t "${BASENAME}[$$]" "Ignoring $ANSWER"
                    fi
               fi
          fi
     done
     send "DISABLE EVENTS"
     answer "READY"
  else
     read -r -p "Code: " RECEIVED
  fi
     echo "$RECEIVED"
     }

# Get a SMS-like String from DTMF's. The Code-Table is not standard!!
dtmf_alph () {
    TAB_0=(0 + - '#' '*' , '.' : ';' '?' '!' '|' @ / '\\' '$' %  '~'     )
    TAB_1=(" " '"' "'" "(" ")" "[" "]" "{" "}" "<" ">")
    TAB_2=(a b c 2 )
    TAB_3=(d e f 3)
    TAB_4=(g h i 4)
    TAB_5=(j k l 5)
    TAB_6=(m n o 6 )
    TAB_7=(p q r s 7 )
    TAB_8=(t u v 8 )
    TAB_9=(w x y z 9)
    TAB_r=()
    TAB_s=()
    
#    stty cbreak </dev/tty >/dev/tty 2>&1
    
    LAST_TIME=10000000000
    THIS_TIME="0"
    LAST_CHAR=""
    THIS_CHAR=""
    LAST_NUM=""
    THIS_NUM=""
    LIST_CHAR=()
    LIST_NUM=()
    REPT=0
    THIS_TAB=""
    THIS_POS=""
    LAST_POS=""
    ALPH=""
    CHAR=""
    
    while true; do
	THIS_NUM=`getone`
#        THIS_NUM=`perl -e 'print getc(STDIN);'`
#       echo -n "  "
        THIS_TIME=`date +'%s'`
        LIST_NUM=(${LIST_NUM[@]} $THIS_NUM)
        case $THIS_NUM in
        "*")
            if [ "1$LAST_CHAR" != "1" ]; then
                eval $LAST_CHAR=\${TAB_${LAST_NUM}[$REPT]}
            fi
            if [ "$CAPS" = "yes" ]; then
                caps="no"
            else
                caps="yes"
                if [ "1$LAST_CHAR" != "1" ]; then
                    LAST_CHAR=`echo "$LAST_CHAR" | tr "[:lower:]" "[:upper:]"`
                fi
            fi
#           echo -n "made '$LAST_CHAR' caps: '$CAPS'"
            continue
        ;;
        "#")
            LIST_CHAR[$LAST_POS]=$LAST_CHAR
            break
        ;;  
        0|1|2|3|4|5|6|7|8|9)
            if [ "$THIS_NUM" != "$LAST_NUM" -o $(($THIS_TIME - $TIMEOUT)) \
                                                -ge $LAST_TIME ]; then
                LIST_CHAR[$LAST_POS]=$LAST_CHAR
                LAST_NUM=$THIS_NUM
                LAST_POS=$THIS_POS
                THIS_POS=$(($THIS_POS + 1))
                REPT=0
#               echo -n "added '$LAST_CHAR'"
            else
                eval THIS_TAB=(\${TAB_${THIS_NUM}[@]})
                if [ "$REPT" -ge "${#THIS_TAB[@]}" ]; then
                    REPT=0
                else
                    REPT=$(($REPT + 1))
                fi  
#               echo -n "incremented to '$REPT'"
            fi
        ;;  
        *)
#           echo -n "Invalid: '$THIS_CHAR'"
            THIS_NUM=$LAST_NUM
            THIS_CHAR=$LAST_CHAR
            continue
        ;;  
        esac
        eval THIS_CHAR=\${TAB_${THIS_NUM}[$REPT]}
        if [ "$CAPS" = "yes" ]; then
            THIS_CHAR=`echo "$THIS_CHAR" | tr "[:lower:]" "[:upper:]"`
        fi  
        LAST_CHAR=$THIS_CHAR
        LAST_NUM=$THIS_NUM
        LAST_TIME=$THIS_TIME
#       echo "--now: list '${LIST_CHAR[@]}' and LAST_CHAR '$LAST_CHAR'"
    done
    
#    stty -cbreak </dev/tty >/dev/tty 2>&1
#    export LIST_CHAR
    for CHAR in "${LIST_CHAR[@]}"; do
#       echo -n "$CHAR"
        ALPH="$ALPH$CHAR"
    done
    echo "$ALPH"
}   

# exitvm
exitvm () {
  play $GOODBYE
  if [ $CONS_MODE = "no" ]; then
    send "GOODBYE"
    answer "GOODBYE SHELL"
  else
    echo "Bye"
  fi
  exit 0
}

# getone
getone () {
  if [ $CONS_MODE = "no" ]; then
     RECEIVED=""
     send "ENABLE EVENTS"
     answer "READY"
     send "WAIT 30"
     answer "WAITING"
     ANSWER=""
     while [ "$ANSWER" != "READY" ]
     do
          ANSWER=`receive`
          if [ "$ANSWER" = "RECEIVED_DTMF" ]; then
               ANSWER=`receive`
               send "STOP"
               RECEIVED=$ANSWER
          else
               if [ "$ANSWER" = "SILENCE_DETECTED" ]; then
                    send "STOP"
               else
                    if [ "$ANSWER" != "READY" ]; then
                         logger -t "${BASENAME}[$$]" "Ignoring $ANSWER"
                    fi
               fi
          fi
     done
     send "DISABLE EVENTS"
     answer "READY"
  else
     read -r -p "Tone: " RECEIVED
  fi
     echo "$RECEIVED"
     }

# Start the festival Server if necessary
check_fest () {
	if ps ax 2>&1 | grep festival_server | grep -v grep >/dev/null; then
		logger -t "${BASENAME}[$$]" "fesitval_server running"
	else
		nohup festival_server 2>&1 >/dev/null &
		logger -t "${BASENAME}[$$]" "fesitval_server started"
		sleep 5
	fi
}

# Say something
say () {
	check_fest
#	I really want vmshell to be able to play from a pipe! --async
	if [ "1$1" != "1" ]; then
	   echo "$1" | festival_client --tts_mode fundamental --ttw --otype ulaw | basictopvf | pvfspeed -s $SPEED | pvfamp -A 6 | pvftormd $MODEM_TYPE $COMPRESSION > $MSG_DIR/voice_say.rmd
	else
	   cat - | festival_client --tts_mode fundamental --ttw --otype ulaw | basictopvf | pvfspeed -s $SPEED | pvfamp -A 6 | pvftormd $MODEM_TYPE $COMPRESSION > $MSG_DIR/voice_say.rmd
	fi
	play $MSG_DIR/voice_say.rmd
	rm -f $MSG_DIR/voice_say.rmd
}

# Synthesise some Speech for later playing (to save time)
prep_say () {
	if [ "1$2" == "1" -o ! -s $MSG_DIR/message_${2-prep_say}.rmd ]; then
		check_fest
#		I really want vmshell to be able to play from a pipe! --async
		echo "$1" | festival_client --tts_mode fundamental --ttw --otype ulaw |	basictopvf | pvfspeed -s $SPEED | pvfamp -A 6 | pvftormd $MODEM_TYPE $COMPRESSION > $MSG_DIR/message_${2-prep_say}.rmd &
		sleep 5
	fi
}

# Play prepared voice
play_say () {
	play $MSG_DIR/message_${1-prep_say}.rmd
#	Don't remove special generated files -- maybe we need 'em later
#	rm -f $MSG_DIR/message_prep_say.rmd
}


### END FUNCTIONS
###  --MAIN--

if [ $CONS_MODE = "no" ]; then
  # Let's see if the voice library is talking to us
  answer "HELLO SHELL"
  # Let's answer the message
  send "HELLO VOICE PROGRAM"
  answer "READY"
  # Let's check the code
fi

TRIES=1
GOT_CORRECT_CODE=no

while [ $TRIES -le $MAXTRIES ]
do
     if [ "$DTMF" = "$CODE" ]; then
          if [ -f $ALREADY_REC ]; then
               rm -f $ALREADY_REC
          fi
          GOT_CORRECT_CODE=yes
          break
     else
          logger -t "${BASENAME}[$$]" "Incorrect DTMF code on try $TRIES"
          beep 1320 100
          play "$INCORRECT"
     fi
     if [ $TRIES -lt $MAXTRIES ]; then
          play "$GET_CODE"
          beep 1320 100
          DTMF=`getcode`
     else
          play "$BYE"
          play "$GOODBYE"
     fi
     TRIES=`expr $TRIES + 1`
done
#
###
### We are in!! Lets give the Menu!
###

if [ "$GOT_CORRECT_CODE" = "yes" ]; then
    prep_say "hash is menu" "help"
    
    prep_say "zeero is command mode. Enter your command with the provided table
    and press hash. Then confirm it with hash. -- One plays your list of
    messages. If there are no new and you ask again it will give you all. --
    Two will connect you to your internet provider in a bout one minute. --
    Three will activate faxgetty for the next 30 minutes. -- Four lets you
    record something from the soundcard and play it. -- Star leaves the menu
    and will hang up the phone." "menu" 

    play "$OKAY_DRIN"
    while true ; do
	play_say "help"
	ANSWER=`getone`
	case "$ANSWER" in
	    "#")
		play_say "menu"
	    ;;
	    0)
		logger -t "${BASENAME}[$$]" "Command mode"
		say "Command mode"
		COMMAND=`dtmf_alph`
		beep 1320 100
		say "Really __ ${COMMAND} __ ?"
		ANSWER=`getone`
		beep 1320 100
		case "$ANSWER" in
		"#")
		    logger -t "${BASENAME}[$$]" "Command '$COMMAND'"
		    su - rj -c "${COMMAND}" 2>&1 | say 
		;;
		*)
		    say "Aborted"
		;;
		esac
	    ;;
	    1)
		logger -t "${BASENAME}[$$]" "Playing messages"
		say "Your list of messages"
		messages
	    ;;
	    2)
		logger -t "${BASENAME}[$$]" "Connecting via PPP"
		say "In a bout 1 minute you will be connected"
		nohup $CONNECT_SCRIPT >/dev/null 2>&1 &
	    ;;
	    3)
		logger -t "${BASENAME}[$$]" "Run faxgetty for the next 30 min"
		say "In a bout 1 minute I will be able to receive faxes"
		nohup $ACTIVATE_FAX >/dev/null 2>&1 &
	    ;;
	    4)
		say "Seconds to receive and amplification?"
		SECS=`getcode`
		beep 1320 100
		AMP=`getcode`
		beep 1320 100
		dd if=/dev/audio bs=8000 count=$SECS | basictopvf | pvfspeed -s $SPEED | pvfamp -A $AMP | pvftormd $MODEM_TYPE $COMPRESSION > $MSG_DIR/abhorch.rmd
		beep 1320 100
		play "$MSG_DIR/abhorch.rmd"
		beep 1320 100
#		rm -f $MSG_DIR/abhorch.rmd
	    ;;
	    "*")
		play $BYE
		beep 1320 100
		exitvm
	    ;;
	esac
    done
fi

###
### All right, that's it, leave!
###

exitvm