!  PURE
!	Author:	Antonio Flores Aldama antonio@doorsinternet.com
@ORIGINATE
@ANSWER
ifstr 5 1 "0"
serreset 115200, 0, 8, 1
jump 2
!
@LABEL 1
serreset 115200, 0, 8, 1
!
@LABEL 2
hsreset 0 0 0 0 0 0
settries 0
!
matchclr
matchstr 1 3 "OK\13\10"
write "AT\13"
matchread 60
pause 90
jump 101
!
! Apple made modems should return "Apple Internal Modem" for "ATi4" command
!
@LABEL 3
matchclr
matchstr 1 11 "Apple Internal Modem\13\10"
matchstr 2 6 "Fax\13\10"
write "ATI4\13"
matchread 30
jump 6
!
! Get the modem information
!
@LABEL 11
matchclr
matchstr 1 4 "S1\13\10"
matchstr 2 5 "D1\13\10"
write "ATI12\13"
matchread 90
!
@LABEL 4
flush
matchclr
matchstr 1 7   "OK\13\10"
matchstr 2 101 "ERROR\13\10"
write "ATZ\13"
pause 5
matchread 90
inctries
iftries 3 101
DTRClear
pause 5
DTRSet
flush
pause 90
jump 4
!
@LABEL 5
flush
matchclr
matchstr 1 7   "OK\13\10"
matchstr 2 101 "ERROR\13\10"
write "ATZ\13"
pause 5
matchread 90
inctries
iftries 3 101
DTRClear
pause 5
DTRSet
flush
pause 90
jump 5
!
@LABEL 6
flush
matchclr
matchstr 1 7   "OK\13\10"
matchstr 2 101 "ERROR\13\10"
write "ATZ\13"
pause 5
matchread 90
inctries
iftries 3 101
DTRClear
pause 5
DTRSet
flush
pause 90
jump 6
!
@LABEL 7
ifstr 4 8 "1"
ifstr 4 8 "2"
!
! Varstring 4 == 0, turn off reliable link protocol in modem (ARAP)
! 
matchclr
matchstr 1 9 "OK\13\10"
write "AT\\N0\13"
matchread 30
jump 101
!
@LABEL 8
ifstr 5 9 "1"
!
! Varstring 5 == 0, turn off compression protocol in modem.
!
matchclr
matchstr 1 9 "OK\13\10"
write "AT%C0\13"
matchread 30
jump 101
!
@LABEL 9
ifstr 2 13 "1"
pause 5
matchclr
matchstr 1 13 "OK\13\10"
write "ATM0\13"
matchread 30
jump 101
!
! Modem ready, wait for a call or originate a call
!
@LABEL 13
! 
ifANSWER 32
ifstr 6 17 "1"
ifstr 6 15 "2"
jump 19
!
@LABEL 15
! 
! Display ASK dialog with message.  Goto label 107 if dialog canceled.
! 
ifstr 1 16 ""
ASK 2 "Pick up the phone & dial ^1.  Hit OK when the phone rings, then hangup." 107
note "Manual dialing initiated" 3
jump 18
!
@LABEL 16
ASK 2 "Pick up the phone & dial the number.  Hit OK when the phone rings, then hangup." 107
note "Manual dialing initiated" 3
!
@LABEL 18
!
write "ATX1S6=4D\13"
jump 32
!
@LABEL 17
note "Dialing without tone" 3
matchclr
matchstr 1 19 "OK\13\10"
! X1S6=4 to ignore dialtone 4 seconds, & ignore busy for blind dialing.
write "ATX3S6=4\13"
matchread 30
jump 101
!
@LABEL 19
! 
! if there is no number, reports EMPTY error
ifstr 1 109 "" 
! 
! This is where we break up long dialstrings
! 
! Display the full dialstring contained in Varstring 1
note "Dialing ^1" 3
flush
!
ifstr 8 27 " "
ifstr 9 24 " "
!
!  Write dialstring in varstrings 7, 8 & 9
matchclr
matchstr 1 21 "OK\13\10"
write "ATD^3^7;\13"
matchread 400
jump 101
! 
@LABEL 21
matchclr
matchstr 1 22 "OK\13\10"
write "ATD^3^8;\13"
matchread 400
jump 101
! 
@LABEL 22
write "ATD^3^9\13"
jump 32
!
@LABEL 24
!  Write dialstring in varstrings 7 & 8
matchclr
matchstr 1 25 "OK\13\10"
write "ATD^3^7;\13"
matchread 400
jump 101
! 
@LABEL 25
write "ATD^3^8\13"
jump 32
!
@LABEL 27
!  Write dialstring in varstring 7
!
write "ATD^3^7\13"
!
!    ---- Connection response ----
!
! The following section will parse modem responses of five types:
!   1) PROTOCOL: xxx, COMPRESSION: xxx, CONNECT xxx
!   2) CONNECT xxx/ARQ/V42
!   3) Standard incoming and outgoing call progress messages
!   4) FAX/DATA discrimination messages
!   5) International DELAYED & BLACKLISTED restrictions.
!
@LABEL 32
matchclr
matchstr  1 88  "RING\13\10"
matchstr  2 102 "NO DIALTONE\13\10"
matchstr  3 103 "NO CARRIER"
matchstr  4 103 "ERROR\13\10"
matchstr  5 104 "BUSY\13\10"
matchstr  6 105 "NO ANSWER\13\10"
matchstr  7 110 "DELAYED "
matchstr  8 111 "BLACKLISTED\13\10"
matchstr  9 32  "CARRIER"
matchstr 10 86  "CONNECT\13\10"
matchstr 11 81  "PROTOCOL: "
matchstr 12 81  "+ER: "
matchstr 13 83  "COMPRESSION: "
matchstr 14 83  "+DR: "
matchstr 15 33  "CONNECT "
matchread 700
ifANSWER 32
jump 101
!
@LABEL 33
matchclr
!
matchstr  1 39 "2400\13"
matchstr  2 39 "2400/"
matchstr  3 40 "4800\13"
matchstr  4 40 "4800/"
matchstr  5 41 "7200"
matchstr  6 42 "9600"
matchstr  7 43 "12000"
matchstr  8 44 "14400"
matchstr  9 45 "16800"
matchstr 10 46 "19200"
matchstr 11 47 "21600"
matchstr 12 48 "24000"
matchstr 13 49 "26400"
matchstr 14 50 "28800"
matchstr 15 51 "29333"
matchstr 16 52 "30667"
matchstr 17 53 "31200"
matchstr 18 54 "32000"
matchstr 19 55 "33333"
matchstr 20 56 "33600"
matchstr 21 57 "34000"
matchstr 22 58 "34667"
matchstr 23 59 "36000"
matchstr 24 60 "37333"
matchstr 25 61 "38000"
matchstr 26 62 "38667"
matchstr 27 63 "40000"
matchstr 28 64 "41333"
matchstr 29 65 "42000"
matchstr 30 66 "42667"
matchstr 31 67 "44000"
matchstr 32 68 "45333"
matchstr 33 69 "46000"
matchstr 34 70 "46667"
matchstr 35 71 "48000"
matchstr 36 72 "49333"
matchstr 37 73 "50000"
matchstr 38 74 "50667"
matchstr 39 75 "52000"
matchstr 40 76 "53333"
matchstr 41 77 "54000"
matchstr 42 78 "54667"
matchstr 43 79 "56000"
matchread 30
jump 80
!
! -- Connection rates --
! CommunicatingAt informs ARA of the raw modem to modem
! connection speed.
!
@LABEL 39
note "Communicating at 2400 bps." 2
CommunicatingAt 2400
jump 86
!
@LABEL 40
note "Communicating at 4800 bps." 2
CommunicatingAt 4800
jump 86
!
@LABEL 41
note "Communicating at 7200 bps." 2
CommunicatingAt 7200
jump 86
!
@LABEL 42
note "Communicating at 9600 bps." 2
CommunicatingAt 9600
jump 86
!
@LABEL 43
note "Communicating at 12400 bps." 2
CommunicatingAt 12400
jump 86
!
@LABEL 44
note "Communicating at 14400 bps." 2
CommunicatingAt 14400
jump 86
!
@LABEL 45
note "Communicating at 16800 bps." 2
CommunicatingAt 16800
jump 86
!
@LABEL 46
note "Communicating at 19200 bps." 2
CommunicatingAt 19200
jump 86
!
@LABEL 47
note "Communicating at 21600 bps." 2
CommunicatingAt 21600
jump 86
!
@LABEL 48
note "Communicating at 24000 bps." 2
CommunicatingAt 24000
jump 86
!
@LABEL 49
note "Communicating at 26400 bps." 2
CommunicatingAt 26400
jump 86
!
@LABEL 50
note "Communicating at 28800 bps." 2
CommunicatingAt 28800
jump 86
!
@LABEL 51
note "Communicating at 29333 bps." 2
CommunicatingAt 29333
jump 86
!
@LABEL 52
note "Communicating at 30667 bps." 2
CommunicatingAt 30667
jump 86
!
@LABEL 53
note "Communicating at 31200 bps." 2
CommunicatingAt 31200
jump 86
!
@LABEL 54
note "Communicating at 32000 bps." 2
CommunicatingAt 32000
jump 86
!
@LABEL 55
note "Communicating at 33333 bps." 2
CommunicatingAt 33333
jump 86
!
@LABEL 56
note "Communicating at 33600 bps." 2
CommunicatingAt 33600
jump 86
!
@LABEL 57
note "Communicating at 34000 bps." 2
CommunicatingAt 34000
jump 86
!
@LABEL 58
note "Communicating at 34667 bps." 2
CommunicatingAt 34667
jump 86
!
@LABEL 59
note "Communicating at 36000 bps." 2
CommunicatingAt 36000
jump 86
!
@LABEL 60
note "Communicating at 37333 bps." 2
CommunicatingAt 37333
jump 86
!
@LABEL 61
note "Communicating at 38000 bps." 2
CommunicatingAt 38000
jump 86
!
@LABEL 62
note "Communicating at 38667 bps." 2
CommunicatingAt 38667
jump 86
!
@LABEL 63
note "Communicating at 40000 bps." 2
CommunicatingAt 40000
jump 86
!
@LABEL 64
note "Communicating at 41333 bps." 2
CommunicatingAt 41333
jump 86
!
@LABEL 65
note "Communicating at 42000 bps." 2
CommunicatingAt 42000
jump 86
!
@LABEL 66
note "Communicating at 42667 bps." 2
CommunicatingAt 42667
jump 86
!
@LABEL 67
note "Communicating at 44000 bps." 2
CommunicatingAt 44000
jump 86
!
@LABEL 68
note "Communicating at 45333 bps." 2
CommunicatingAt 45333
jump 86
!
@LABEL 69
note "Communicating at 46000 bps." 2
CommunicatingAt 46000
jump 86
!
@LABEL 70
note "Communicating at 46667 bps." 2
CommunicatingAt 46667
jump 86
!
@LABEL 71
note "Communicating at 48000 bps." 2
CommunicatingAt 48000
jump 86
!
@LABEL 72
note "Communicating at 49333 bps." 2
CommunicatingAt 49333
jump 86
!
@LABEL 73
note "Communicating at 50000 bps." 2
CommunicatingAt 50000
jump 86
!
@LABEL 74
note "Communicating at 50667 bps." 2
CommunicatingAt 50667
jump 86
!
@LABEL 75
note "Communicating at 52000 bps." 2
CommunicatingAt 52000
jump 86
!
@LABEL 76
note "Communicating at 53333 bps." 2
CommunicatingAt 53333
jump 86
!
@LABEL 77
note "Communicating at 54000 bps." 2
CommunicatingAt 54000
jump 86
!
@LABEL 78
note "Communicating at 54667 bps." 2
CommunicatingAt 54667
jump 86
!
@LABEL 79
note "Communicating at 56000 bps." 2
CommunicatingAt 56000
jump 86
!
@LABEL 80
note "Communicating at an unknown rate." 2
jump 86
!
! Look for data compression results 
!
@LABEL 81
matchclr
matchstr  1 82 "LAPM"
matchstr  2 82 "LAP-M"
matchstr  3 82 "LAP\13"
matchstr  4 82 "ALT"
matchstr  5 82 "MNP"
matchstr  6 82 "REL"
matchstr  7 82 "ARQ"
matchstr  8 82 "V42"
matchstr  9 82 "V42\13"
matchstr 10 82 "V.42"
matchstr 11 82 "V.42\13"
matchstr 12 85 "CONNECT "
matchread 30
jump 32
! 
! -- Modem error correction link negotiation --
! Userhook 2 informs ARA that a modem-to-modem error correcting protocol has been negotiated
!
@LABEL 82
note "Modem Reliable Link Established." 3
userhook 2
jump 32
!
@LABEL 85
note "Did not establish Modem Reliable Link." 3
jump 33
!
@LABEL 83
matchclr
matchstr  1 84 "MNP5"
matchstr  2 84 "ALT"
matchstr  3 84 "V42B\13"
matchstr  4 84 "V42BIS"
matchstr  5 84 "V42bis"
matchstr  6 84 "V.42B\13"
matchstr  7 84 "V.42BIS"
matchstr  8 84 "V.42bis"
matchstr  9 84 "CLASS 5"
matchstr 10 84 "CLASS\13"
matchstr 11 84 "COMP/"
matchstr 12 84 "COMP\13"
matchstr 13 84 "V\13"
matchstr 14 89 "CONNECT "
matchread 30
jump 32
!
! -- Compression negotiation --
! Userhook 3 informs ARA that a modem-to-modem compression protocol has been negotiated
!
@LABEL 84
note "Modem Compression Established." 3
userhook 3
jump 32
!
@LABEL 89
note "Did not establish Modem Compression." 3
jump 33
!
! -- Normal exit after "CONNECT" --
!  This modem has been setup to do CTS handshaking,
!  and we assume that a CTS handshaking cable is being used.
!
@LABEL 86
! Turn on CTS handshaking.
HSReset 0 1 0 0 0 0
!
ifANSWER 87
pause 30
@LABEL 87
exit 0
!
! ---- Answer calls ----
!
! A RING results from the modem in ANSWERING mode
! claims the serial port and answering the phone
!
@LABEL 88
ifORIGINATE 32
userhook 1
note "Answering phone..." 2
write "ATA\13"
jump 32
!  
! ---- Hang up and reset modem ----
!
@HANGUP 
@LABEL 90 
settries 0
HSReset 0 0 0 0 0 0
!
@LABEL 91
! 
! Try to get control of the modem
! 
flush
pause 1
write "+++"
pause 1 
matchclr
matchstr 1 93 "OK\13\10"
matchread 30
!
@LABEL 93
! 
! hangup
! 
flush
matchclr
matchstr 1 98 "NO CARRIER\13\10"
matchstr 2 98 "OK\13\10"
matchstr 3 98 "ERROR\13\10"
matchstr 4 98 "0\13\10"
matchstr 5 98 "DELAYED"
matchstr 6 98 "BLACKLISTED"
write "ATH\13"
matchread 30
! 
! Try again to get control of the modem by toggling DTR
!
@LABEL 95
DTRClear
Pause 2
DTRSet
flush
!
! Try the hangup sequence three times otherwise declare an error
! 
inctries
iftries 3 101
jump 91
!
@LABEL 98
! 
! Got control of the modem.  Recall the factory settings.  If it fails, jump 101.
! 
pause 2
matchclr
matchstr 1 99 "OK\13\10"
matchstr 2 101 "ERROR\13\10"
write "AT&F\13"
matchread 30
jump 101
!
@LABEL 99
exit 0
!
! ---- Error messages -----
!
! Modem Not Responding
@LABEL 101
write "AT&F\13"
pause 10
exit -6019
!
! No Dial Tone
@LABEL 102
matchstr 1 121 "OK\13\10"
write "AT&F\13"
matchread 30
@LABEL 121
exit -6020
!
! No Carrier or Error
@LABEL 103
matchstr 1 122 "OK\13\10"
write "AT&F\13"
matchread 30
@LABEL 122
exit -6021
!
! Busy
@LABEL 104
matchstr 1 123 "OK\13\10"
write "AT&F\13"
matchread 30
@LABEL 123
exit -6022
!
! No Answer
@LABEL 105
matchstr 1 124 "OK\13\10"
write "AT&F\13"
matchread 30
@LABEL 124
exit -6023
!
! User Cancellation
@LABEL 107
matchstr 1 108 "OK\13\10"
write "AT&F\13"
matchread 30
@LABEL 108
exit -6008
!
! Empty phone number
@LABEL 109
matchstr 1 125 "OK\13\10"
write "AT&F\13"
matchread 30
@LABEL 125
exit -6027
! 
! ---- DELAYED and BLACKLISTED functions ----
! 
@LABEL 110
matchclr
matchstr  1 119 "00:00:0"
matchstr  2 118 "00:00:1"
matchstr  3 117 "00:00:2"
matchstr  4 116 "00:00:3"
matchstr  5 115 "00:00:4"
matchstr  6 114 "00:00:5"
matchstr  7 113 "00:01:"
matchstr  8 112 "00:02:"
matchread 30
! 
! (modified on 10/5/98) - Added the display in case delayed for more than 3 minutes exit with User Cancellation
note "'DELAYED'. For more than 3 minutes."
pause 20
exit -6008
!
!The number is 'DELAYED' more than 3 minutes.  Exit the script telling the user to wait or reset his modem.
!exit -6019 "\13The modem responds 'DELAYED':\13(Cannot redial this number right now.)\13To try again immediately, turn modem OFF and ON before proceeding."
! 
@LABEL 111
!
! (modified on 10/5/98) - Added the display of the right string and exit with User Cancellation
note "'BLACKLISTED'."
pause 20
exit -6008
!
!The number is 'BLACKLISTED'.  Exit the script, telling the user to reset his modem.
!exit -6019 "\13The modem responds 'BLACKLISTED':\13(Cannot redial this number again.)\13Double-check the number, then turn your modem OFF and ON before proceeding."
!
@LABEL 112
note "'DELAYED'. Redialing in 3 minutes..."
pause 600
@LABEL 113
note "'DELAYED'. Redialing in 2 minutes..."
pause 600
@LABEL 114
note "'DELAYED'. Redialing in 1 minute... "
pause 100
@LABEL 115
note "'DELAYED'. Redialing in 50 seconds..."
pause 100
@LABEL 116
note "'DELAYED'. Redialing in 40 seconds..."
pause 100
@LABEL 117
note "'DELAYED'. Redialing in 30 seconds..."
pause 100
@LABEL 118
note "'DELAYED'. Redialing in 20 seconds..."
pause 100
@LABEL 119
note "'DELAYED'. Redialing in 10 seconds..."
pause 100
jump 1
! 
! Labels 121-128 are reserved for emergency hacks.
!