Copyright (c) 1994 by Atul Butte. All Rights Reserved.
Needs AppleScript 1.1, MacTCP 2.0.4, Scriptable Text Editor and TCP/IP Scripting Addition 1.1
*)
global respondedToOurEcho
global respondedToOurSGA
global terminalType
on in_english(ttt)
set acc to {}
repeat with c in (characters of ttt)
set d to ASCII number of (contents of c)
if d = 255 then
set d to "iac"
else if d = 253 then
set d to "do"
else if d = 254 then
set d to "dont"
else if d = 251 then
set d to "will"
else if d = 252 then
set d to "wont"
else if d = 250 then
set d to "sb"
else if d = 240 then
set d to "se"
end if
set acc to acc & d
end repeat
return {acc, ttt}
end in_english
on firstConnect()
set respondedToOurEcho to false
set respondedToOurSGA to false
return (ASCII character (255)) & (ASCII character (253)) & (ASCII character (1)) & (ASCII character (255)) & (ASCII character (253)) & (ASCII character (3)) & (ASCII character (255)) & (ASCII character (251)) & (ASCII character (24))
end firstConnect
on parseAndRespond(incoming)
set toReturn to ""
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {ASCII character 255}
set incoming to "**" & incoming
set d to text items of incoming
set AppleScript's text item delimiters to oldDelimiters
if length of d = 1 then
return ""
end if
repeat with itemcode in (items 2 through end of d)
set theitem to contents of itemcode
set code to ASCII number (character 1 of theitem)
if (length of theitem > 1) then
set optionchar to character 2 of theitem
set option to ASCII number (optionchar)
--log "code: " & code & " option: " & option
else
set optionchar to ""
--log "code: " & code
end if
if code = 250 and option = 24 then
--log "sending terminal vt100"
set toReturn to toReturn & (ASCII character 255) & (ASCII character 250) & (ASCII character 24) & ¬
(ASCII character 0) & terminalType & (ASCII character 255) & (ASCII character 240)
end if
if code = 253 then
if option = 24 then
else -- server says iac do, we say iac wont
set toReturn to toReturn & (ASCII character 255) & (ASCII character 252) & optionchar
end if
end if
if code = 251 then -- server says iac will
if (option = 1 or option = 3) then
if option = 1 and not respondedToOurEcho then
set respondedToOurEcho to true
else if option = 3 and not respondedToOurSGA then
set respondedToOurSGA to true
else -- server says iac will, we say iac do
set toReturn to toReturn & (ASCII character 255) & (ASCII character 253) & optionchar
end if
else
set toReturn to toReturn & (ASCII character 255) & (ASCII character 254) & optionchar
end if
end if
if code = 254 then -- server says iac dont, we say iac wont
set toReturn to toReturn & (ASCII character 255) & (ASCII character 252) & optionchar
end if
if code = 252 then -- server says iac wont, we say iac dont
set toReturn to toReturn & (ASCII character 255) & (ASCII character 254) & optionchar
end if
end repeat
return toReturn
end parseAndRespond
set theHost to text returned of (display dialog "Login to where?" default answer "")
set terminalType to text returned of (display dialog "What kind of terminal should I say you have (all caps, please)?" default answer "VT100")
set sss to (tcp connect to host theHost port 23)
tcp write stream sss data (firstConnect())
try
tell application "Scriptable Text Editor"
activate
make new document at beginning
set selection to "(Once you see the login prompt, start typing at the > prompt; " & return & "type 'stop' to stop the script and disconnect)" & return
set style of paragraphs -3 through -2 of window 1 to {class:text style info, on styles:{bold}, off styles:{italic, underline, outline, shadow}}
set selection to ">"
set continueloop to true
repeat while continueloop
set t to last paragraph of window 1
if t = "" then
set oneBefore to (paragraph -2 of window 1) as string
if oneBefore = ">stop" then
exit repeat
end if
set selection to ">"
if oneBefore = ">" then
tcp write stream sss data return
else
set toSend to ((characters 2 through end of oneBefore) as string) & return
tcp write stream sss data toSend
end if
end if
set tcpstat to (tcp status stream sss)
if (connection status of tcpstat ≠ Connected) then
exit repeat
end if
set didWePrintSomething to false
repeat while (bytes waiting of tcpstat > 0)
set incoming to (tcp read stream sss as text)
if (length of incoming > 0) then
if (number of characters of window 1 > 20000) then
select (text from character 1 to character 20000)
delete selection
end if
if not didWePrintSomething then
set selection to return & "["
set didWePrintSomething to true
end if
set selection to incoming
set toSend to (my parseAndRespond(incoming))
if (length of toSend > 0) then
tcp write stream sss data toSend
end if
end if
set tcpstat to (tcp status stream sss)
if (connection status of tcpstat ≠ Connected) then