#!/usr/bin/expect -- # # Expect script for automating a login session at SK5BN. # # 1. Check for new messages and private mail. # 2. If any private mail, open a file named 'mailbox' and save the mail. # 3. If any new messages, open a file named 'msglist' and save the # message list to it, else end the session. # 4. Logout from the BBS, and launch 'vi' to select the messages to read. # 5. End the session if no messages selected, else login again, and read # selected messages to default log file. # 7. Logout, and launch 'vi' to read/edit the messages. proc chk_newmsgs {} { global msgfrom msgto expect { -re "Nya meddelanden: (\[0-9]+) to (\[0-9]+).*\r\n" { set msgfrom $expect_out(1,string) set msgto $expect_out(2,string) } } } proc chk_mail {} { global bbs_call expect { "till dig." { expect "$bbs_call>\r\n" send "rm\r" expect "rm\r\n" send "~o mailbox\r" expect "$bbs_call>\r\n" send "~c" expect "\r\n" send "km\r" exp_continue } "$bbs_call>\r\n" } } proc get_msglist {} { global bbs_call send "l\r" expect "l\r\n" send "~o msglist\r" expect "$bbs_call>\r\n" send "~c" expect "\r\n" } proc select_msgs {} { if {! [file exists msglist]} { return 1 } if {[catch {exec grep -q ^\[0-9]\[0-9]\[0-9] msglist} errcode]} { send_user "No messages in list.\n"; return 1 } catch {system vi msglist} errcode if {[catch {system awk 'BEGIN \{printf \"r \"\; n = 0\} \ /^\\t/\{printf \"%d \", \$1\; n++\} \ END \{printf \"\\n\"\; if (n) exit 0\; else exit 1\}' \ msglist > readcmd } errcode ]} { return 1 } else { return 0 } } proc get_msgs {} { global bbs_call set fd [open readcmd r] gets $fd cmd close $fd send "$cmd\r" expect "$cmd\r\n" send "~o newmsgs\r" expect "$bbs_call>\r\n" send "~c" expect "\r\n" } proc login {} { global bbs_call timeout expect -re "ECHO.*was.*\r\ncmd:" send "c $bbs_call\r" set timeout 30 set connected 0 expect { timeout { if { $connected == 0 } { send "d\r" expect -re "\\\*\\\*\\\* DISCONNECTED.*\r\n" send_user "Couldn't connect to BBS, exiting...\n" } else { send_user "No response from BBS, exiting...\n" } exit } -re "\\\*\\\*\\\* CONNECTED.*\r\n" { set connected 1; exp_continue } -re "\\\[FBB-.*]\r\n" { set timeout -1 } } } proc logout {} { send "b\r" expect -re "\\\*\\\*\\\* DISCONNECTED.*\r\n" send "" expect "cmd:" send "~q" expect eof } set bbs_call "SK5BN" if {$argc == 0} { if [file exists msglist] { exec rm msglist } spawn -nottyinit eterm login chk_newmsgs chk_mail if {$msgto == $msgfrom} { logout; exit } get_msglist logout } if {[select_msgs] == 0} { spawn -nottyinit eterm login chk_mail get_msgs logout catch {system vi newmsgs} errcode send_user "Remove 'newmsgs' (y/n)? " expect_user "y" { exec rm newmsgs } "n" } .